summaryrefslogtreecommitdiffstats
path: root/qmaemo5homescreenadaptor/qmaemo5homescreenadaptor.cpp
blob: ddd962de9b4ce14eed3e984709eb8a8814c40729 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/****************************************************************************
**
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the examples of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** No Commercial Usage
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the Technology Preview License Agreement accompanying
** this package.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights.  These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
**
**
**
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "qmaemo5homescreenadaptor.h"

#include <QtCore/qsocketnotifier.h>
#include <QtCore/qpointer.h>

#include <QtGui/qapplication.h>
#include <QtGui/qx11info_x11.h>
#include <QtGui/qwidget.h>
#include <QtGui/qevent.h>

#include <X11/Xlib.h>
#include <X11/Xatom.h>
#include <X11/Xutil.h>

static QCoreApplication::EventFilter oldEventFilter;
static QList<QMaemo5HomescreenAdaptor *> allDesktopItems;

static Atom atomByName(const char *name)
{
    Atom atom = XInternAtom(QX11Info::display(), name, False);
    if (!atom)
        qWarning("Unable to obtain %s atom. This class requires a running Hildon session.", name);

    return atom;
}

enum HomescreenAtoms
{
    HildonAppletId               = 0,
    NetWmWindowType              = 1,
    Utf8String                   = 2,
    HildonTypeHomeApplet         = 3,
    HildonAppletSettings         = 4,
    HildonAppletShowSettings     = 5,
    HildonAppletOnCurrentDesktop = 6,
    EnumCount                    = 7
};

static Atom hsAtoms[EnumCount] = { 0, 0, 0, 0, 0, 0, 0 };

static void initAtoms()
{
    hsAtoms[HildonAppletId] = atomByName("_HILDON_APPLET_ID");
    hsAtoms[NetWmWindowType] = atomByName("_NET_WM_WINDOW_TYPE");
    hsAtoms[Utf8String] = atomByName("UTF8_STRING");
    hsAtoms[HildonTypeHomeApplet] = atomByName("_HILDON_WM_WINDOW_TYPE_HOME_APPLET");
    hsAtoms[HildonAppletSettings] = atomByName("_HILDON_APPLET_SETTINGS");
    hsAtoms[HildonAppletShowSettings] = atomByName("_HILDON_APPLET_SHOW_SETTINGS");
    hsAtoms[HildonAppletOnCurrentDesktop] = atomByName("_HILDON_APPLET_ON_CURRENT_DESKTOP");
}

/*! \class QMaemo5HomescreenAdaptor

    \brief The QMaemo5HomescreenAdaptor flags a top-level QWidget as homescreen widget

    QMaemo5HomescreenAdaptor is used in conjunction with the Qt for Maemo homescreen
    loader. It evaluates the two command line arguments "-plugin-id" and "-write-pipe"
    to set up a Qt top-level widget as Maemo 5 homescreen widget.

    Note: By default, the widget will have a black background. In order to make the
    widget transparent, set the Qt::WA_TranslucentBackground widget attribute.

    Example:

    \code
    QLabel *label = new QLabel("Hello Homescreen");
    new QMaemo5HomescreenAdaptor(label);
    label->show();
    \endcode

    Maemo 5 supports homescreen widgets with settings dialogs. To use it, set
    the settingsAvailable() property and show a settings dialog when the
    settingsRequested() signal is emitted.

    Maemo 5 supports more than one homescreen. In order to determine whether
    the homescreen widget is on the currently visible homescreen, connect to
    the homescreenChanged() signal.
*/

/*! \property QMaemo5HomescreenAdaptor::settingsAvailable

    Set this property to true if the widget can make use of a settings dialog,
    otherwise to false. When this property is set to true, the Maemo 5 homescreen
    renders a small settings icon on top of the homescreen widget when the
    user enters the desktop menu. When the user clicks that settings icon, the
    settingsRequested() signal is emitted.

    The default is false.

    \sa settingsRequested()
 */

/*! \fn void settingsRequested()

    This signal is emitted every time the homescreen widget's settings icon is
    invoked by the user. Note that this icon is only visible when the settingsAvailable()
    property is set.

    \sa settingsAvailable()
 */

/*! \fn void homescreenChanged(bool isOnCurrentHomescreen)

    This is signal is emitted when current homescreen changes and the homescreen
    widget becomes visible or invisible. The \a isOnCurrentHomescreen argument
    indicates whether the homescreen widget is on the current homescreen or not.

    This signal can be used to start/stop background processing in order to save
    battery life.
 */

/*!
    Constructs a new QMaemo5HomescreenAdaptor for the given \a widget.

    Note: The widget must be a top-level widget, and must not be reparented
    during the lifetime of this adaptor.

    Note: \a widget is also the parent of this class, if the widget is destroyed,
    so is this adaptor.
 */
QMaemo5HomescreenAdaptor::QMaemo5HomescreenAdaptor(QWidget *widget)
    : QObject(widget),
      hasSettings(false)
{
    Q_ASSERT(widget->isWindow());

    if (!hsAtoms[0])
        initAtoms();

    Display *display = QX11Info::display();

    const QStringList args = QApplication::arguments();

    // parse the command line arguments.
    int idx;
    if ((idx = args.indexOf(QLatin1String("-plugin-id"))) != -1) {
        appletId = args.value(idx + 1);
        const QByteArray pluginId = appletId.toUtf8();
        if (!pluginId.isEmpty()) {
            XChangeProperty(display,
                    widget->winId(),
                    hsAtoms[HildonAppletId],
                    hsAtoms[Utf8String], 8, PropModeReplace,
                    reinterpret_cast<const unsigned char *>(pluginId.constData()),
                    pluginId.length());
        }
    }
    if ((idx = args.indexOf(QLatin1String("-write-pipe"))) != -1) {
        bool ok;
        int sockId = args.value(idx + 1).toInt(&ok);
        if (ok) {
            socketNotifier = new QSocketNotifier(sockId, QSocketNotifier::Exception, this);
            connect(socketNotifier, SIGNAL(activated(int)), this, SLOT(socketException()));
        }
    }

    // set the X11 atoms to flag our widget as homescreen widget
    if (!appletId.isEmpty()) {
        XChangeProperty(display,
                widget->winId(),
                hsAtoms[NetWmWindowType],
                XA_ATOM, 32, PropModeReplace,
                reinterpret_cast<const unsigned char *>(&hsAtoms[HildonTypeHomeApplet]),
                1);

        updateStatus();

        // --- make this window a child of root
        XSetTransientForHint(display, widget->winId(),
                             RootWindow(display, widget->x11Info().screen()));

        // --- add an x11 event filter
        if (!oldEventFilter)
            oldEventFilter = QCoreApplication::instance()->setEventFilter(applicationEventFilter);

        allDesktopItems.append(this);

        // --- set WM input hints indicating that we don't want focus events
        XWMHints *h = XGetWMHints(display, widget->winId());
        XWMHints wm_hints;
        if (!h) {
            memset(&wm_hints, 0, sizeof(wm_hints)); // make valgrind happy
            h = &wm_hints;
        }
        h->flags |= InputHint;
        h->input = False;

        XSetWMHints(display, widget->winId(), h);
        if (h != &wm_hints)
            XFree(h);

        widget->setMouseTracking(true);
    }
}

QMaemo5HomescreenAdaptor::~QMaemo5HomescreenAdaptor()
{
    allDesktopItems.removeOne(this);
}

/*! \internal */
void QMaemo5HomescreenAdaptor::updateStatus()
{
    if (appletId.isEmpty())
        return;

    Display *display = QX11Info::display();

    // Set or remove settings property
    if (hasSettings)
        XChangeProperty(display,
                appletWidget()->winId(),
                hsAtoms[HildonAppletSettings],
                XA_CARDINAL, 32, PropModeReplace,
                (const unsigned char *) &(hasSettings), 1);
    else
        XDeleteProperty(display,
                appletWidget()->winId(),
                hsAtoms[HildonAppletSettings]);
}

/*! \internal */
void QMaemo5HomescreenAdaptor::socketException()
{
    socketNotifier->setEnabled(false);
    appletWidget()->close();
}

bool qt_sendSpontaneousEvent(QObject *receiver, QEvent *event)
{
    return QCoreApplication::sendSpontaneousEvent(receiver, event);
}



/*! \internal */
bool QMaemo5HomescreenAdaptor::applicationEventFilter(void *message, long *result)
{
    static QPointer<QWidget> lastMouseWidget;
    bool retval = false;

    if (oldEventFilter)
        retval = oldEventFilter(message, result);

    if (allDesktopItems.isEmpty())
        return retval;

    XEvent *ev = reinterpret_cast<XEvent *>(message);

    // Generate a mouse release for a leave Notify (as we don't get the mouse release from X11)
    if (ev->type == ButtonPress) {
        QPoint globalPos( ev->xbutton.x_root, ev->xbutton.y_root);
        QWidget *widget = QWidget::find((WId)ev->xany.window);
        if (widget) {
            lastMouseWidget = widget->childAt(widget->mapFromGlobal(globalPos));
            if (!lastMouseWidget)
                lastMouseWidget = widget;
        }

    } else if (ev->type == ButtonRelease) {
        lastMouseWidget = 0;

    } else if (ev->type == LeaveNotify) {
        if (lastMouseWidget) {
            // create a mouse up event that lies in Nirvana.
            QPoint pos(-1000, -1000);
            QMouseEvent e(QEvent::MouseButtonRelease, pos, pos, Qt::LeftButton, Qt::NoButton, Qt::NoModifier);
            qt_sendSpontaneousEvent(lastMouseWidget, &e);
            lastMouseWidget = 0;
       }

    } else if (ev->type == ClientMessage) {
        XClientMessageEvent *cm = (XClientMessageEvent *)message;
        if (cm->message_type == hsAtoms[HildonAppletShowSettings]) {
            for (int i = 0; i < allDesktopItems.count(); ++i) {
                if (allDesktopItems.at(i)->appletWidget()->winId() == ev->xproperty.window) {
                    emit allDesktopItems.at(i)->settingsRequested();
                    retval = true;
                }
            }
        }
    } else if (ev->type == PropertyNotify) {
        if (ev->xproperty.atom == hsAtoms[HildonAppletOnCurrentDesktop]) {
            for (int i = 0; i < allDesktopItems.count(); ++i) {
                if (allDesktopItems.at(i)->appletWidget()->winId() == ev->xproperty.window) {
                    emit allDesktopItems.at(i)->homescreenChanged(ev->xproperty.window == 0);
                    retval = true;
                }
            }
        }
    }

    return retval;
}