This repository has been archived by the owner on Dec 17, 2017. It is now read-only.
/
razortray.cpp
362 lines (292 loc) · 9.24 KB
/
razortray.cpp
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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
/* BEGIN_COMMON_COPYRIGHT_HEADER
* (c)LGPL2+
*
* Razor - a lightweight, Qt based, desktop toolset
* http://razor-qt.org
*
* Copyright: 2011 Razor team
* Authors:
* Alexander Sokoloff <sokoloff.a@gmail.com>
*
* This program or library is free software; you can redistribute it
* and/or modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General
* Public License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301 USA
*
* END_COMMON_COPYRIGHT_HEADER */
/********************************************************************
Inspired by freedesktops tint2 ;)
*********************************************************************/
// Warning: order of those include is important.
#include <QApplication>
#include <QtDebug>
#include "trayicon.h"
#include <razorqt/razorgridlayout.h>
#include "../panel/irazorpanel.h"
#include <QX11Info>
#include "razortray.h"
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xatom.h>
#include <X11/extensions/Xrender.h>
#include <X11/extensions/Xdamage.h>
#undef Bool // defined as int in X11/Xlib.h
#include "razorqt/xfitman.h"
#include <QtCore/QTimer>
#include "../panel/irazorpanelplugin.h"
#define _NET_SYSTEM_TRAY_ORIENTATION_HORZ 0
#define _NET_SYSTEM_TRAY_ORIENTATION_VERT 1
#define SYSTEM_TRAY_REQUEST_DOCK 0
#define SYSTEM_TRAY_BEGIN_MESSAGE 1
#define SYSTEM_TRAY_CANCEL_MESSAGE 2
#define XEMBED_EMBEDDED_NOTIFY 0
#define XEMBED_MAPPED (1 << 0)
/************************************************
************************************************/
RazorTray::RazorTray(IRazorPanelPlugin *plugin, QWidget *parent):
QFrame(parent),
mValid(false),
mTrayId(0),
mDamageEvent(0),
mDamageError(0),
mIconSize(TRAY_ICON_SIZE_DEFAULT, TRAY_ICON_SIZE_DEFAULT),
mPlugin(plugin)
{
mLayout = new RazorGridLayout(this);
realign();
// Init the selection later just to ensure that no signals are sent until
// after construction is done and the creating object has a chance to connect.
QTimer::singleShot(0, this, SLOT(startTray()));
}
/************************************************
************************************************/
RazorTray::~RazorTray()
{
stopTray();
}
/************************************************
************************************************/
void RazorTray::x11EventFilter(XEvent* event)
{
TrayIcon* icon;
switch (event->type)
{
case ClientMessage:
clientMessageEvent(&(event->xclient));
break;
// case ConfigureNotify:
// icon = findIcon(event->xconfigure.window);
// if (icon)
// icon->configureEvent(&(event->xconfigure));
// break;
case DestroyNotify:
icon = findIcon(event->xany.window);
if (icon)
{
mIcons.removeAll(icon);
delete icon;
}
break;
default:
if (event->type == mDamageEvent + XDamageNotify)
{
XDamageNotifyEvent* dmg = reinterpret_cast<XDamageNotifyEvent*>(event);
icon = findIcon(dmg->drawable);
if (icon)
icon->update();
}
break;
}
}
/************************************************
************************************************/
void RazorTray::realign()
{
mLayout->setEnabled(false);
IRazorPanel *panel = mPlugin->panel();
if (panel->isHorizontal())
{
mLayout->setRowCount(panel->lineCount());
mLayout->setColumnCount(0);
}
else
{
mLayout->setColumnCount(panel->lineCount());
mLayout->setRowCount(0);
}
mLayout->setEnabled(true);
}
/************************************************
************************************************/
void RazorTray::clientMessageEvent(XClientMessageEvent* e)
{
unsigned long opcode;
opcode = e->data.l[1];
Window id;
switch (opcode)
{
case SYSTEM_TRAY_REQUEST_DOCK:
id = e->data.l[2];
if (id)
addIcon(id);
break;
case SYSTEM_TRAY_BEGIN_MESSAGE:
case SYSTEM_TRAY_CANCEL_MESSAGE:
qDebug() << "we don't show balloon messages.";
break;
default:
// if (opcode == xfitMan().atom("_NET_SYSTEM_TRAY_MESSAGE_DATA"))
// qDebug() << "message from dockapp:" << e->data.b;
// else
// qDebug() << "SYSTEM_TRAY : unknown message type" << opcode;
break;
}
}
/************************************************
************************************************/
TrayIcon* RazorTray::findIcon(Window id)
{
foreach(TrayIcon* icon, mIcons)
{
if (icon->iconId() == id || icon->windowId() == id)
return icon;
}
return 0;
}
/************************************************
************************************************/
void RazorTray::setIconSize(QSize iconSize)
{
mIconSize = iconSize;
foreach(TrayIcon* icon, mIcons)
icon->setIconSize(mIconSize);
}
/************************************************
************************************************/
VisualID RazorTray::getVisual()
{
VisualID visualId = 0;
Display* dsp = QX11Info::display();
XVisualInfo templ;
templ.screen=QX11Info::appScreen();
templ.depth=32;
templ.c_class=TrueColor;
int nvi;
XVisualInfo* xvi = XGetVisualInfo(dsp, VisualScreenMask|VisualDepthMask|VisualClassMask, &templ, &nvi);
if (xvi)
{
int i;
XRenderPictFormat* format;
for (i = 0; i < nvi; i++)
{
format = XRenderFindVisualFormat(dsp, xvi[i].visual);
if (format &&
format->type == PictTypeDirect &&
format->direct.alphaMask)
{
visualId = xvi[i].visualid;
break;
}
}
XFree(xvi);
}
return visualId;
}
/************************************************
freedesktop systray specification
************************************************/
void RazorTray::startTray()
{
Display* dsp = QX11Info::display();
Window root = QX11Info::appRootWindow();
QString s = QString("_NET_SYSTEM_TRAY_S%1").arg(DefaultScreen(dsp));
Atom _NET_SYSTEM_TRAY_S = xfitMan().atom(s.toAscii());
if (XGetSelectionOwner(dsp, _NET_SYSTEM_TRAY_S) != None)
{
qWarning() << "Another systray is running";
mValid = false;
return;
}
// init systray protocol
mTrayId = XCreateSimpleWindow(dsp, root, -1, -1, 1, 1, 0, 0, 0);
XSetSelectionOwner(dsp, _NET_SYSTEM_TRAY_S, mTrayId, CurrentTime);
if (XGetSelectionOwner(dsp, _NET_SYSTEM_TRAY_S) != mTrayId)
{
qWarning() << "Can't get systray manager";
stopTray();
mValid = false;
return;
}
int orientation = _NET_SYSTEM_TRAY_ORIENTATION_HORZ;
XChangeProperty(dsp,
mTrayId,
xfitMan().atom("_NET_SYSTEM_TRAY_ORIENTATION"),
XA_CARDINAL,
32,
PropModeReplace,
(unsigned char *) &orientation,
1);
// ** Visual ********************************
VisualID visualId = getVisual();
if (visualId)
{
XChangeProperty(QX11Info::display(),
mTrayId,
xfitMan().atom("_NET_SYSTEM_TRAY_VISUAL"),
XA_VISUALID,
32,
PropModeReplace,
(unsigned char*)&visualId,
1);
}
// ******************************************
XClientMessageEvent ev;
ev.type = ClientMessage;
ev.window = root;
ev.message_type = xfitMan().atom("MANAGER");
ev.format = 32;
ev.data.l[0] = CurrentTime;
ev.data.l[1] = _NET_SYSTEM_TRAY_S;
ev.data.l[2] = mTrayId;
ev.data.l[3] = 0;
ev.data.l[4] = 0;
XSendEvent(dsp, root, False, StructureNotifyMask, (XEvent*)&ev);
XDamageQueryExtension(QX11Info::display(), &mDamageEvent, &mDamageError);
qDebug() << "Systray started";
mValid = true;
}
/************************************************
************************************************/
void RazorTray::stopTray()
{
qDeleteAll(mIcons);
if (mTrayId)
{
XDestroyWindow(QX11Info::display(), mTrayId);
mTrayId = 0;
}
mValid = false;
}
/************************************************
************************************************/
void RazorTray::addIcon(Window winId)
{
TrayIcon* icon = new TrayIcon(winId, this);
if (!icon->isValid())
{
delete icon;
return;
}
icon->setIconSize(mIconSize);
mIcons.append(icon);
mLayout->addWidget(icon);
}