1
/***************************************************************************
2
* Copyright (C) 2005 - 2007 by *
3
* Christian Muehlhaeuser, Last.fm Ltd <chris@last.fm> *
4
* Erik Jaelevik, Last.fm Ltd <erik@last.fm> *
5
***************************************************************************/
8
* trayicon_x11.cpp - X11 trayicon (for use with KDE and GNOME)
9
* Copyright (C) 2003 Justin Karneges
10
* GNOME2 Notification Area support: Tomasz Sterna
12
* This library is free software; you can redistribute it and/or
13
* modify it under the terms of the GNU Lesser General Public
14
* License as published by the Free Software Foundation; either
15
* version 2.1 of the License, or (at your option) any later version.
17
* This library is distributed in the hope that it will be useful,
18
* but WITHOUT ANY WARRANTY; without even the implied warranty of
19
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20
* Lesser General Public License for more details.
22
* You should have received a copy of the GNU Lesser General Public
23
* License along with this library; if not, write to the Free Software
24
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30
#include <QApplication>
36
#include <QMouseEvent>
38
#include <QPaintEvent>
39
#include <QCloseEvent>
40
#include <QDesktopWidget>
44
#include <X11/Xutil.h>
45
#include <X11/Xatom.h>
47
//#if QT_VERSION < 0x030200
48
extern Time qt_x_time;
51
//----------------------------------------------------------------------------
53
//----------------------------------------------------------------------------
55
// for Gnome2 Notification Area
56
static XErrorHandler old_handler = 0;
57
static int dock_xerror = 0;
58
extern "C" int dock_xerrhandler(Display* dpy, XErrorEvent* err)
60
dock_xerror = err->error_code;
61
return old_handler(dpy, err);
64
static void trap_errors()
67
old_handler = XSetErrorHandler(dock_xerrhandler);
70
static bool untrap_errors()
72
XSetErrorHandler(old_handler);
73
return (dock_xerror == 0);
76
static bool send_message(
77
Display* dpy, /* display */
78
Window w, /* sender (tray icon window) */
79
long message, /* message opcode */
80
long data1, /* message data 1 */
81
long data2, /* message data 2 */
82
long data3 /* message data 3 */
86
memset(&ev, 0, sizeof(ev));
87
ev.xclient.type = ClientMessage;
88
ev.xclient.window = w;
89
ev.xclient.message_type = XInternAtom (dpy, "_NET_SYSTEM_TRAY_OPCODE", False );
90
ev.xclient.format = 32;
91
ev.xclient.data.l[0] = CurrentTime;
92
ev.xclient.data.l[1] = message;
93
ev.xclient.data.l[2] = data1;
94
ev.xclient.data.l[3] = data2;
95
ev.xclient.data.l[4] = data3;
98
XSendEvent(dpy, w, False, NoEventMask, &ev);
100
return untrap_errors();
103
#define SYSTEM_TRAY_REQUEST_DOCK 0
104
#define SYSTEM_TRAY_BEGIN_MESSAGE 1
105
#define SYSTEM_TRAY_CANCEL_MESSAGE 2
107
//----------------------------------------------------------------------------
108
// TrayIcon::TrayIconPrivate
109
//----------------------------------------------------------------------------
111
class TrayIcon::TrayIconPrivate : public QWidget
114
TrayIconPrivate(TrayIcon *object, int size);
115
~TrayIconPrivate() { }
117
virtual void initWM(WId icon);
119
virtual void setPixmap(const QPixmap &pm);
121
virtual void paintEvent(QPaintEvent *);
122
virtual void enterEvent(QEvent *);
123
virtual void mouseMoveEvent(QMouseEvent *e);
124
virtual void mousePressEvent(QMouseEvent *e);
125
virtual void mouseReleaseEvent(QMouseEvent *e);
126
virtual void mouseDoubleClickEvent(QMouseEvent *e);
127
virtual void closeEvent(QCloseEvent *e);
130
TrayIcon *iconObject;
135
TrayIcon::TrayIconPrivate::TrayIconPrivate(TrayIcon *object, int _size)
138
setAutoFillBackground( false );
139
// setAttribute( Qt::WA_OpaquePaintEvent );
140
setAttribute( Qt::WA_NoSystemBackground );
141
setAttribute( Qt::WA_Hover );
142
setAttribute( Qt::WA_PaintOnScreen );
147
setFocusPolicy(Qt::NoFocus);
148
// setBackgroundRole(/* Qt::X11ParentRelative */ QPalette::Window );
150
setMinimumSize(size, size);
151
setMaximumSize(size, size);
154
// This base stuff is required by both FreeDesktop specification and WindowMaker
155
void TrayIcon::TrayIconPrivate::initWM(WId icon)
157
Display *dsp = x11Info().display();
158
WId leader = winId();
160
// set the class hint
161
XClassHint classhint;
162
classhint.res_name = (char*)"wpdock";
163
classhint.res_class = (char*)"Wolfpack";
164
XSetClassHint(dsp, leader, &classhint);
166
// set the Window Manager hints
168
hints = XGetWMHints(dsp, leader); // init hints
169
hints->flags = WindowGroupHint | IconWindowHint | StateHint; // set the window group hint
170
hints->window_group = leader; // set the window hint
171
hints->initial_state = WithdrawnState; // initial state
172
hints->icon_window = icon; // in WM, this should be winId() of separate widget
175
XSetWMHints(dsp, leader, hints); // set the window hints for WM to use.
179
void TrayIcon::TrayIconPrivate::setPixmap(const QPixmap &pm)
182
setWindowIcon(QIcon(pix));
186
void TrayIcon::TrayIconPrivate::paintEvent(QPaintEvent *)
189
p.drawPixmap((width() - pix.width())/2, (height() - pix.height())/2, pix);
192
void TrayIcon::TrayIconPrivate::enterEvent(QEvent *e)
194
QWidget::enterEvent(e);
197
void TrayIcon::TrayIconPrivate::mouseMoveEvent(QMouseEvent *e)
199
QApplication::sendEvent(iconObject, e);
202
void TrayIcon::TrayIconPrivate::mousePressEvent(QMouseEvent *e)
204
QApplication::sendEvent(iconObject, e);
207
void TrayIcon::TrayIconPrivate::mouseReleaseEvent(QMouseEvent *e)
209
QApplication::sendEvent(iconObject, e);
212
void TrayIcon::TrayIconPrivate::mouseDoubleClickEvent(QMouseEvent *e)
214
QApplication::sendEvent(iconObject, e);
217
void TrayIcon::TrayIconPrivate::closeEvent(QCloseEvent *e)
219
iconObject->gotCloseEvent();
223
//----------------------------------------------------------------------------
224
// TrayIconFreeDesktop
225
//----------------------------------------------------------------------------
227
class TrayIconFreeDesktop : public TrayIcon::TrayIconPrivate
230
TrayIconFreeDesktop(TrayIcon *object, const QPixmap &pm);
232
virtual bool x11Event(XEvent*);
235
TrayIconFreeDesktop::TrayIconFreeDesktop(TrayIcon *object, const QPixmap &pm)
236
: TrayIconPrivate(object, 22)
241
Display *dsp = x11Info().display();
243
// dock the widget (adapted from SIM-ICQ)
244
Screen *screen = XDefaultScreenOfDisplay(dsp); // get the screen
245
int screen_id = XScreenNumberOfScreen(screen); // and it's number
247
// tell X that we want to see ClientMessage and Deleted events, which
248
// are picked up by QApplication::x11EventFilter
249
Window root_window = QApplication::desktop()->winId();
250
XWindowAttributes attr;
252
XGetWindowAttributes(dsp, root_window, &attr);
253
XSelectInput(dsp, root_window, attr.your_event_mask | StructureNotifyMask);
256
snprintf(buf, sizeof(buf), "_NET_SYSTEM_TRAY_S%d", screen_id);
257
Atom selection_atom = XInternAtom(dsp, buf, false);
259
Window manager_window = XGetSelectionOwner(dsp, selection_atom);
260
if ( manager_window != None )
261
XSelectInput(dsp, manager_window, StructureNotifyMask);
265
if ( manager_window != None )
266
send_message(dsp, manager_window, SYSTEM_TRAY_REQUEST_DOCK, winId(), 0, 0);
273
// some KDE mumbo-jumbo... why is it there? anybody?
274
Atom kwm_dockwindow_atom = XInternAtom(dsp, "KWM_DOCKWINDOW", false);
275
Atom kde_net_system_tray_window_for_atom = XInternAtom(dsp, "_KDE_NET_WM_SYSTEM_TRAY_WINDOW_FOR", false);
278
XChangeProperty(dsp, winId(), kwm_dockwindow_atom, kwm_dockwindow_atom, 32, PropModeReplace, (uchar*)&data, 1);
279
XChangeProperty(dsp, winId(), kde_net_system_tray_window_for_atom, XA_WINDOW, 32, PropModeReplace, (uchar*)&data, 1);
284
bool TrayIconFreeDesktop::x11Event(XEvent *ev)
295
//----------------------------------------------------------------------------
296
// TrayIconWindowMaker
297
//----------------------------------------------------------------------------
299
class TrayIconWharf : public TrayIcon::TrayIconPrivate
302
TrayIconWharf(TrayIcon *object, const QPixmap &pm)
303
: TrayIconPrivate(object, 44)
305
// set the class hint
306
XClassHint classhint;
307
classhint.res_name = (char*)"psidock-wharf";
308
classhint.res_class = (char*)"Psi";
309
XSetClassHint(x11Info().display(), winId(), &classhint);
314
void setPixmap(const QPixmap &_pm)
317
QImage i = _pm.toImage();
318
i = i.scaled(i.width() * 2, i.height() * 2);
321
TrayIconPrivate::setPixmap(pm);
323
// thanks to Robert Spier for this:
324
// for some reason the repaint() isn't being honored, or isn't for
325
// the icon. So force one on the widget behind the icon
327
QPaintEvent pe( rect() );
332
class TrayIconWindowMaker : public TrayIcon::TrayIconPrivate
335
TrayIconWindowMaker(TrayIcon *object, const QPixmap &pm);
336
~TrayIconWindowMaker();
338
void setPixmap(const QPixmap &pm);
341
TrayIconWharf *wharf;
344
TrayIconWindowMaker::TrayIconWindowMaker(TrayIcon *object, const QPixmap &pm)
345
: TrayIconPrivate(object, 32)
347
wharf = new TrayIconWharf(object, pm);
349
initWM( wharf->winId() );
352
TrayIconWindowMaker::~TrayIconWindowMaker()
357
void TrayIconWindowMaker::setPixmap(const QPixmap &pm)
359
wharf->setPixmap(pm);
362
//----------------------------------------------------------------------------
364
//----------------------------------------------------------------------------
366
void TrayIcon::sysInstall()
372
d = (TrayIconPrivate *)(new TrayIconWindowMaker(this, pm));
374
d = (TrayIconPrivate *)(new TrayIconFreeDesktop(this, pm));
382
void TrayIcon::sysRemove()
391
void TrayIcon::sysUpdateIcon()
400
void TrayIcon::sysUpdateToolTip()
408
d->setToolTip( tip );