-
Notifications
You must be signed in to change notification settings - Fork 0
/
dwm-5.8.2-systray.diff
340 lines (327 loc) · 8.21 KB
/
dwm-5.8.2-systray.diff
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
--- a/dwm.c 2011-01-10 15:12:04.433336369 -0500
+++ b/dwm.c 2011-01-10 15:04:59.603337417 -0500
@@ -54,11 +54,16 @@
#define TAGMASK ((1 << LENGTH(tags)) - 1)
#define TEXTW(X) (textnw(X, strlen(X)) + dc.font.height)
+#define XEMBED_EMBEDDED_NOTIFY 0
+#define IFREE(x) if(x) free(x)
+#define zcalloc(size) calloc(1, (size))
+#define ROOT RootWindow(dpy, DefaultScreen(dpy))
+
/* enums */
enum { CurNormal, CurResize, CurMove, CurLast }; /* cursor */
enum { ColBorder, ColFG, ColBG, ColLast }; /* color */
enum { NetSupported, NetWMName, NetWMState,
- NetWMFullscreen, NetLast }; /* EWMH atoms */
+ NetWMFullscreen, NetLast, NetSystemTray }; /* EWMH atoms */
enum { WMProtocols, WMDelete, WMState, WMLast }; /* default atoms */
enum { ClkTagBar, ClkLtSymbol, ClkStatusText, ClkWinTitle,
ClkClientWin, ClkRootWin, ClkLast }; /* clicks */
@@ -140,6 +145,7 @@
Monitor *next;
Window barwin;
const Layout *lt[2];
+ int primary;
};
typedef struct {
@@ -151,6 +157,13 @@
int monitor;
} Rule;
+typedef struct Systray Systray;
+struct Systray {
+ Window win;
+ XRectangle geo;
+ Systray *next, *prev;
+};
+
/* function declarations */
static void applyrules(Client *c);
static Bool applysizehints(Client *c, int *x, int *y, int *w, int *h, Bool interact);
@@ -167,7 +180,7 @@
static void configure(Client *c);
static void configurenotify(XEvent *e);
static void configurerequest(XEvent *e);
-static Monitor *createmon(void);
+static Monitor *createmon(int primary);
static void destroynotify(XEvent *e);
static void detach(Client *c);
static void detachstack(Client *c);
@@ -243,6 +256,14 @@
static int xerrorstart(Display *dpy, XErrorEvent *ee);
static void zoom(const Arg *arg);
+static Bool systray_acquire(void);
+static void systray_add(Window win);
+static void systray_del(Systray *s);
+static void systray_freeicons(void);
+static Systray *systray_find(Window win);
+static int systray_get_width(void);
+static void systray_update(void);
+
/* variables */
static const char broken[] = "broken";
static char stext[256];
@@ -275,6 +296,9 @@
static Monitor *mons = NULL, *selmon = NULL;
static Window root;
+Systray *trayicons;
+Window traywin;
+
/* configuration, allows nested code to access above variables */
#include "config.h"
@@ -609,7 +633,7 @@
}
Monitor *
-createmon(void) {
+createmon(int primary) {
Monitor *m;
if(!(m = (Monitor *)calloc(1, sizeof(Monitor))))
@@ -621,16 +645,24 @@
m->lt[0] = &layouts[0];
m->lt[1] = &layouts[1 % LENGTH(layouts)];
strncpy(m->ltsymbol, layouts[0].symbol, sizeof m->ltsymbol);
+
+ m->primary = primary;
+
return m;
}
void
destroynotify(XEvent *e) {
Client *c;
+ Systray *s;
XDestroyWindowEvent *ev = &e->xdestroywindow;
if((c = wintoclient(ev->window)))
unmanage(c, True);
+ else if((s = systray_find(ev->window))){
+ systray_del(s);
+ systray_update();
+ }
}
void
@@ -709,6 +741,7 @@
if(m == selmon) { /* status is only drawn on selected monitor */
dc.w = TEXTW(stext);
dc.x = m->ww - dc.w;
+ if(m->primary == 1) dc.x -= systray_get_width(); // subtract systray width
if(dc.x < x) {
dc.x = x;
dc.w = m->ww - x;
@@ -1328,6 +1361,12 @@
arrange(c->mon);
}
}
+ if(cme->window == traywin) {
+ if(cme->data.l[1] == XEMBED_EMBEDDED_NOTIFY){
+ systray_add(cme->data.l[2]);
+ systray_update();
+ }
+ }
}
void
@@ -1529,7 +1568,7 @@
initfont(font);
sw = DisplayWidth(dpy, screen);
sh = DisplayHeight(dpy, screen);
- bh = dc.h = dc.font.height + 2;
+ bh = dc.h = dc.font.height + 2;
updategeom();
/* init atoms */
wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
@@ -1539,6 +1578,7 @@
netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
netatom[NetWMState] = XInternAtom(dpy, "_NET_WM_STATE", False);
netatom[NetWMFullscreen] = XInternAtom(dpy, "_NET_WM_STATE_FULLSCREEN", False);
+ netatom[NetSystemTray] = XInternAtom(dpy, "_NET_SYSTEM_TRAY_S0", False);
/* init cursors */
cursor[CurNormal] = XCreateFontCursor(dpy, XC_left_ptr);
cursor[CurResize] = XCreateFontCursor(dpy, XC_sizing);
@@ -1558,6 +1598,7 @@
/* init bars */
updatebars();
updatestatus();
+ systray_acquire();
/* EWMH support per view */
XChangeProperty(dpy, root, netatom[NetSupported], XA_ATOM, 32,
PropModeReplace, (unsigned char *) netatom, NetLast);
@@ -1802,12 +1843,13 @@
XFree(info);
nn = j;
if(n <= nn) {
- for(i = 0; i < (nn - n); i++) { /* new monitors available */
+ for(m = mons; m && m->next; m = m->next);
+ if(m) m->next = createmon(1);
+ else mons = createmon(1);
+ for(i = 1; i < (nn - n); i++) { /* new monitors available */
for(m = mons; m && m->next; m = m->next);
- if(m)
- m->next = createmon();
- else
- mons = createmon();
+ if(m) m->next = createmon(0);
+ else mons = createmon(0);
}
for(i = 0, m = mons; i < nn && m; m = m->next, i++)
if(i >= n
@@ -1847,7 +1889,7 @@
/* default monitor setup */
{
if(!mons)
- mons = createmon();
+ mons = createmon(1);
if(mons->mw != sw || mons->mh != sh) {
dirty = True;
mons->mw = mons->ww = sw;
@@ -2045,6 +2087,155 @@
arrange(c->mon);
}
+Bool
+systray_acquire(void) {
+ XSetWindowAttributes wattr;
+
+ if(!systray_enable || traywin) return False;
+
+ if(XGetSelectionOwner(dpy, netatom[NetSystemTray]) != None) {
+ fprintf(stderr, "Can't initialize system tray: owned by another process\n");
+ return False;
+ }
+
+ // Init traywin window
+ wattr.event_mask = ButtonPressMask | ExposureMask;
+ wattr.override_redirect = True;
+ wattr.background_pixmap = ParentRelative;
+ wattr.background_pixel = dc.norm[ColBG];
+
+ traywin = XCreateSimpleWindow(dpy, root, 0, 0, 1, 1, 0, 0, dc.norm[ColBG]);
+
+ XChangeWindowAttributes(dpy, traywin, CWEventMask | CWOverrideRedirect | CWBackPixel, &wattr);
+ XSelectInput(dpy, traywin, KeyPressMask | ButtonPressMask);
+
+ XMapRaised(dpy, traywin);
+
+ XSetSelectionOwner(dpy, netatom[NetSystemTray], traywin, CurrentTime);
+
+ if(XGetSelectionOwner(dpy, netatom[NetSystemTray]) != traywin) {
+ systray_freeicons();
+ fprintf(stderr, "System tray: can't get systray manager\n");
+ return False;
+ }
+ XSync(dpy, False);
+
+ return True;
+}
+
+void
+systray_add(Window win) {
+ Systray *s;
+
+ if(!systray_enable) return;
+
+ s = zcalloc(sizeof(Systray));
+ s->win = win;
+
+ s->geo.height = bh;
+ s->geo.width = bh;
+
+ XSelectInput(dpy, s->win, StructureNotifyMask | PropertyChangeMask| EnterWindowMask | FocusChangeMask);
+ XReparentWindow(dpy, s->win, traywin, 0, 0);
+
+ // Attach
+ if(trayicons) trayicons->prev = s;
+
+ s->next = trayicons;
+ trayicons = s;
+
+ return;
+}
+
+void
+systray_del(Systray *s) {
+ Systray **ss;
+
+ if(!systray_enable) return;
+
+ for(ss = &trayicons; *ss && *ss != s; ss = &(*ss)->next);
+ *ss = s->next;
+
+ IFREE(s);
+ return;
+}
+
+void
+systray_freeicons(void) {
+ Systray *i;
+
+ if(!systray_enable) return;
+
+ for(i = trayicons; i; i = i->next) {
+ XUnmapWindow(dpy, i->win);
+ XReparentWindow(dpy, i->win, ROOT, 0, 0);
+ IFREE(i);
+ }
+
+ XSetSelectionOwner(dpy, netatom[NetSystemTray], None, CurrentTime);
+ XDestroyWindow(dpy, traywin);
+ XSync(dpy, 0);
+
+ return;
+}
+
+Systray*
+systray_find(Window win) {
+ Systray *i;
+
+ if(!systray_enable) return NULL;
+
+ for(i = trayicons; i; i = i->next)
+ if(i->win == win) return i;
+
+ return NULL;
+}
+
+int
+systray_get_width(void) {
+ int w = 0;
+ Systray *i;
+
+ if(!systray_enable) return 0;
+
+ for(i = trayicons; i; i = i->next)
+ w += i->geo.width + systray_spacing + 1;
+
+ return w;
+}
+
+void
+systray_update(void) {
+ Systray *i;
+ Monitor *m;
+ int x = 1;
+ int pos = blw;
+
+ if(!systray_enable) return;
+
+ for(m = mons; m; m = m -> next){
+ if(m->primary == 1) pos = m -> ww;
+ }
+
+ if(!trayicons) {
+ pos -= 1;
+ XMoveResizeWindow(dpy, traywin, pos, 0, 1, 1);
+ return;
+ }
+
+ for(i = trayicons; i; i = i->next) {
+ XMapWindow(dpy, i->win);
+ XMoveResizeWindow(dpy, i->win, (i->geo.x = x), 0, i->geo.width, i->geo.height);
+
+ x += i->geo.width + systray_spacing;
+ }
+
+ pos -= x;
+ XMoveResizeWindow(dpy, traywin, pos, 0, x, bh);
+
+ return;
+}
+
int
main(int argc, char *argv[]) {
if(argc == 2 && !strcmp("-v", argv[1]))