-
Notifications
You must be signed in to change notification settings - Fork 4
/
ewmh.c
2271 lines (1974 loc) · 57.6 KB
/
ewmh.c
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2014 Olaf Seibert
*/
/*
* Implements some of the Extended Window Manager Hints, as (extremely
* poorly) documented at
* http://standards.freedesktop.org/wm-spec/wm-spec-1.3.html .
* In fact, the wiki page that refers to that as being the current version
* (http://www.freedesktop.org/wiki/Specifications/wm-spec/)
* neglects to tell us there are newer versions 1.4 and 1.5 at
* http://standards.freedesktop.org/wm-spec/wm-spec-1.5.html
* (which has a listable directory that I discovered by accident).
* The same wiki page also has lots of dead links to a CVS repository.
* Nevertheless, it is where one ends up if one starts at
* http://www.freedesktop.org/wiki/Specifications/ .
*
* EWMH is an extension to the ICCCM (Inter-Client Communication
* Conventions Manual).
* http://tronche.com/gui/x/icccm/
*
* To fill in lots of details, the source code of other window managers
* has been consulted.
*/
#include "ctwm.h"
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <stdint.h>
#include <assert.h>
#include <X11/Xatom.h>
#include <X11/extensions/shape.h>
#include "ctwm_atoms.h"
#include "ctwm_shutdown.h"
#include "ewmh_atoms.h"
#include "screen.h"
#include "events.h"
#include "event_handlers.h"
#include "functions_defs.h"
#include "icons.h"
#include "otp.h"
#include "image.h"
#include "list.h"
#include "functions.h"
#include "occupation.h"
#include "r_layout.h"
#include "util.h"
#include "vscreen.h"
#include "win_iconify.h"
#include "win_ops.h"
#include "win_resize.h"
#include "win_utils.h"
#include "workspace_utils.h"
/* #define DEBUG_EWMH */
Atom XEWMHAtom[NUM_EWMH_XATOMS];
#define NET_WM_STATE_REMOVE 0 /* remove/unset property */
#define NET_WM_STATE_ADD 1 /* add/set property */
#define NET_WM_STATE_TOGGLE 2 /* toggle property */
#define _NET_WM_MOVERESIZE_SIZE_TOPLEFT 0
#define _NET_WM_MOVERESIZE_SIZE_TOP 1
#define _NET_WM_MOVERESIZE_SIZE_TOPRIGHT 2
#define _NET_WM_MOVERESIZE_SIZE_RIGHT 3
#define _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT 4
#define _NET_WM_MOVERESIZE_SIZE_BOTTOM 5
#define _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT 6
#define _NET_WM_MOVERESIZE_SIZE_LEFT 7
#define _NET_WM_MOVERESIZE_MOVE 8 /* movement only */
#define _NET_WM_MOVERESIZE_SIZE_KEYBOARD 9 /* size via keyboard */
#define _NET_WM_MOVERESIZE_MOVE_KEYBOARD 10 /* move via keyboard */
#define _NET_WM_MOVERESIZE_CANCEL 11 /* cancel operation */
static Image *ExtractIcon(ScreenInfo *scr, unsigned long *prop, int width,
int height);
static void EwmhClientMessage_NET_WM_DESKTOP(XClientMessageEvent *msg);
static void EwmhClientMessage_NET_WM_STATE(XClientMessageEvent *msg);
static void EwmhClientMessage_NET_ACTIVE_WINDOW(XClientMessageEvent *msg);
static void EwmhClientMessage_NET_WM_MOVERESIZE(XClientMessageEvent *msg);
static void EwmhClientMessage_NET_CLOSE_WINDOW(XClientMessageEvent *msg);
static XEvent synth_btnevent_for_moveresize(TwmWindow *twm_win);
static unsigned long EwmhGetWindowProperty(Window w, Atom name, Atom type);
static void EwmhGetStrut(TwmWindow *twm_win, bool update);
static void EwmhRemoveStrut(TwmWindow *twm_win);
static void EwmhSet_NET_WORKAREA(ScreenInfo *scr);
static int EwmhGet_NET_WM_STATE(TwmWindow *twm_win);
static void EwmhClientMessage_NET_WM_STATEchange(TwmWindow *twm_win, int change,
int newVal);
#define ALL_WORKSPACES 0xFFFFFFFFU
static void SendPropertyMessage(Window to, Window about,
Atom messagetype,
long l0, long l1, long l2, long l3, long l4,
long mask)
{
XEvent e;
e.xclient.type = ClientMessage;
e.xclient.message_type = messagetype;
e.xclient.display = dpy;
e.xclient.window = about;
e.xclient.format = 32;
e.xclient.data.l[0] = l0;
e.xclient.data.l[1] = l1;
e.xclient.data.l[2] = l2;
e.xclient.data.l[3] = l3;
e.xclient.data.l[4] = l4;
XSendEvent(dpy, to, False, mask, &e);
}
static void EwmhInitAtoms(void)
{
XInternAtoms(dpy, XEWMHAtomNames, NUM_EWMH_XATOMS, False, XEWMHAtom);
}
static bool caughtError;
static int CatchError(Display *display, XErrorEvent *event)
{
caughtError = true;
return 0;
}
void EwmhInit(void)
{
EwmhInitAtoms();
}
/*
* Force-generate some event, so that we know the current time.
*
* Suggested in the ICCCM:
* http://tronche.com/gui/x/icccm/sec-2.html#s-2.1
*/
static void GenerateTimestamp(ScreenInfo *scr)
{
XEvent event;
struct timespec tosleep;
int timeout = 200; /* 0.2 seconds in ms */
int found;
/* Sleep in 10ms chunks */
tosleep.tv_sec = 0;
tosleep.tv_nsec = (10 * 1000 * 1000);
if(EventTime > 0) {
return;
}
XChangeProperty(dpy, scr->icccm_Window,
XA_WM_CLASS, XA_STRING,
8, PropModeAppend, NULL, 0);
while(timeout > 0) {
found = XCheckTypedWindowEvent(dpy, scr->icccm_Window, PropertyNotify, &event);
if(found) {
break;
}
nanosleep(&tosleep, NULL);
timeout -= 10;
}
if(found) {
#ifdef DEBUG_EWMH
fprintf(stderr, "GenerateTimestamp: time = %ld, timeout left = %d\n",
event.xproperty.time, timeout);
#endif /* DEBUG_EWMH */
if(EventTime < event.xproperty.time) {
EventTime = event.xproperty.time;
}
}
}
/*
* Perform the "replace the window manager" protocol, as vaguely hinted
* at by the ICCCM section 4.3.
* http://tronche.com/gui/x/icccm/sec-4.html#s-4.3
*
* We do want to run through this even if we're not running with
* --replace ourselves, because it also sets up the bits for other
* invocations to --replace us.
*
* TODO: convert the selection to atom VERSION.
*/
static bool EwmhReplaceWM(ScreenInfo *scr)
{
char atomname[32];
Atom wmAtom;
Window selectionOwner;
snprintf(atomname, sizeof(atomname), "WM_S%d", scr->screen);
wmAtom = XInternAtom(dpy, atomname, False);
selectionOwner = XGetSelectionOwner(dpy, wmAtom);
if(selectionOwner == scr->icccm_Window) {
selectionOwner = None;
}
if(selectionOwner != None) {
XErrorHandler oldHandler;
/*
* Check if that owner still exists, and if it does, we want
* StructureNotify-kind events from it.
*/
caughtError = false;
oldHandler = XSetErrorHandler(CatchError);
XSelectInput(dpy, selectionOwner, StructureNotifyMask);
XSync(dpy, False);
XSetErrorHandler(oldHandler);
if(caughtError) {
selectionOwner = None;
}
}
/*
* If something else is owning things and we're not running
* --replace, give up now and return failure.
*/
if(selectionOwner != None && !CLarg.ewmh_replace) {
#ifdef DEBUG_EWMH
fprintf(stderr, "A window manager is already running on screen %d\n",
scr->screen);
#endif
return false;
}
/* We're asked to --replace, so try to do so */
XSetSelectionOwner(dpy, wmAtom, scr->icccm_Window, CurrentTime);
if(XGetSelectionOwner(dpy, wmAtom) != scr->icccm_Window) {
fprintf(stderr, "Did not get window manager selection on screen %d\n",
scr->screen);
return false;
}
/*
* If there was a previous selection owner, wait for it
* to go away.
*/
if(selectionOwner != None) {
int timeout = 10 * 1000; /* 10 seconds in ms */
XEvent event;
struct timespec tosleep;
/* Sleep in 100ms chunks */
tosleep.tv_sec = 0;
tosleep.tv_nsec = (100 * 1000 * 1000);
while(timeout > 0) {
int found = XCheckTypedWindowEvent(dpy, selectionOwner, DestroyNotify, &event);
if(found) {
break;
}
nanosleep(&tosleep, NULL);
timeout -= 100;
}
if(timeout <= 0) {
fprintf(stderr, "Timed out waiting for other window manager "
"on screen %d to quit\n",
scr->screen);
return false;
}
}
/*
* Send a message to confirm we're now managing the screen.
* ICCCM, end of chapter 2, section "Manager Selections".
* http://tronche.com/gui/x/icccm/sec-2.html#s-2.8
*
* ICCCM says StructureNotifyMask,
* OpenBox says SubstructureNotifyMask.
*/
GenerateTimestamp(scr);
SendPropertyMessage(scr->XineramaRoot, scr->XineramaRoot,
XA_MANAGER, EventTime, wmAtom, scr->icccm_Window, 0, 0,
StructureNotifyMask);
return true;
}
/*
* This function is called very early in initialisation.
*
* Only scr->screen and scr->XineramaRoot are valid: we want to know if
* it makes sense to continue with the full initialisation.
*
* Create the ICCCM window that owns the WM_Sn selection.
*/
bool EwmhInitScreenEarly(ScreenInfo *scr)
{
XSetWindowAttributes attrib;
#ifdef DEBUG_EWMH
fprintf(stderr, "EwmhInitScreenEarly: XCreateWindow\n");
#endif
attrib.event_mask = PropertyChangeMask;
attrib.override_redirect = True;
scr->icccm_Window = XCreateWindow(dpy, scr->XineramaRoot,
-100, -100, 1, 1, 0,
CopyFromParent, InputOutput,
CopyFromParent,
CWEventMask | CWOverrideRedirect,
&attrib);
XMapWindow(dpy, scr->icccm_Window);
XLowerWindow(dpy, scr->icccm_Window);
#ifdef DEBUG_EWMH
fprintf(stderr, "EwmhInitScreenEarly: call EwmhReplaceWM\n");
#endif
if(!EwmhReplaceWM(scr)) {
XDestroyWindow(dpy, scr->icccm_Window);
scr->icccm_Window = None;
#ifdef DEBUG_EWMH
fprintf(stderr, "EwmhInitScreenEarly: return false\n");
#endif
return false;
}
#ifdef DEBUG_EWMH
fprintf(stderr, "EwmhInitScreenEarly: return true\n");
#endif
return true;
}
/*
* This initialisation is called late, when scr has been set up
* completely.
*/
void EwmhInitScreenLate(ScreenInfo *scr)
{
long data[2];
/* Set _NET_SUPPORTING_WM_CHECK on root window */
data[0] = scr->icccm_Window;
XChangeProperty(dpy, scr->XineramaRoot,
XA__NET_SUPPORTING_WM_CHECK, XA_WINDOW,
32, PropModeReplace,
(unsigned char *)data, 1);
/*
* Set properties on the window;
* this also belongs with _NET_SUPPORTING_WM_CHECK
*/
XChangeProperty(dpy, scr->icccm_Window,
XA__NET_WM_NAME, XA_UTF8_STRING,
8, PropModeReplace,
(unsigned char *)"ctwm", 4);
data[0] = scr->icccm_Window;
XChangeProperty(dpy, scr->icccm_Window,
XA__NET_SUPPORTING_WM_CHECK, XA_WINDOW,
32, PropModeReplace,
(unsigned char *)data, 1);
/*
* Add supported properties to the root window.
*/
data[0] = 0;
data[1] = 0;
XChangeProperty(dpy, scr->XineramaRoot,
XA__NET_DESKTOP_VIEWPORT, XA_CARDINAL,
32, PropModeReplace,
(unsigned char *)data, 2);
data[0] = scr->rootw;
data[1] = scr->rooth;
XChangeProperty(dpy, scr->XineramaRoot,
XA__NET_DESKTOP_GEOMETRY, XA_CARDINAL,
32, PropModeReplace,
(unsigned char *)data, 2);
EwmhSet_NET_WORKAREA(scr);
if(scr->workSpaceManagerActive) {
data[0] = scr->workSpaceMgr.count;
}
else {
data[0] = 1;
}
XChangeProperty(dpy, scr->XineramaRoot,
XA__NET_NUMBER_OF_DESKTOPS, XA_CARDINAL,
32, PropModeReplace,
(unsigned char *)data, 1);
if(scr->workSpaceManagerActive) {
/* TODO: this is for the first Virtual Screen only... */
/*data[0] = scr->workSpaceMgr.workSpaceWindowList->currentwspc->number; */
data[0] = 0;
}
else {
data[0] = 0;
}
XChangeProperty(dpy, scr->XineramaRoot,
XA__NET_CURRENT_DESKTOP, XA_CARDINAL,
32, PropModeReplace,
(unsigned char *)data, 1);
EwmhSet_NET_SHOWING_DESKTOP(0);
long supported[30];
int i = 0;
supported[i++] = XA__NET_SUPPORTING_WM_CHECK;
supported[i++] = XA__NET_DESKTOP_VIEWPORT;
supported[i++] = XA__NET_NUMBER_OF_DESKTOPS;
supported[i++] = XA__NET_CURRENT_DESKTOP;
supported[i++] = XA__NET_DESKTOP_GEOMETRY;
supported[i++] = XA__NET_WM_ICON;
supported[i++] = XA__NET_WM_DESKTOP;
supported[i++] = XA__NET_CLIENT_LIST;
supported[i++] = XA__NET_CLIENT_LIST_STACKING;
supported[i++] = XA__NET_WM_WINDOW_TYPE;
supported[i++] = XA__NET_WM_WINDOW_TYPE_NORMAL;
supported[i++] = XA__NET_WM_WINDOW_TYPE_DESKTOP;
supported[i++] = XA__NET_WM_WINDOW_TYPE_DOCK;
supported[i++] = XA__NET_WM_STRUT;
supported[i++] = XA__NET_WM_STRUT_PARTIAL;
supported[i++] = XA__NET_SHOWING_DESKTOP;
supported[i++] = XA__NET_WM_STATE;
supported[i++] = XA__NET_WM_STATE_MAXIMIZED_VERT;
supported[i++] = XA__NET_WM_STATE_MAXIMIZED_HORZ;
supported[i++] = XA__NET_WM_STATE_FULLSCREEN;
supported[i++] = XA__NET_ACTIVE_WINDOW;
supported[i++] = XA__NET_WORKAREA;
supported[i++] = XA__NET_WM_MOVERESIZE;
supported[i++] = XA__NET_WM_STATE_SHADED;
supported[i++] = XA__NET_WM_STATE_ABOVE;
supported[i++] = XA__NET_WM_STATE_BELOW;
supported[i++] = XA__NET_CLOSE_WINDOW;
XChangeProperty(dpy, scr->XineramaRoot,
XA__NET_SUPPORTED, XA_ATOM,
32, PropModeReplace,
(unsigned char *)supported, i);
}
#ifdef VSCREEN
/*
* Set up the _NET_VIRTUAL_ROOTS property, which indicates that we're
* using virtual root windows.
* This applies only if we have multiple virtual screens.
*
* Also record this as a supported atom in _NET_SUPPORTED.
*
* Really, our virtual screens (with their virtual root windows) don't quite
* fit in the EWMH idiom. Several root window properties (such as
* _NET_CURRENT_DESKTOP) are more appropriate on the virtual root windows. But
* that is not where other clients would look for them.
*
* The idea seems to be that the virtual roots as used for workspaces (desktops
* in EWMH terminology) are only mapped one at a time.
*/
void EwmhInitVirtualRoots(ScreenInfo *scr)
{
int numVscreens = scr->numVscreens;
if(numVscreens > 1) {
long *data;
long d0;
VirtualScreen *vs;
int i;
data = calloc(numVscreens, sizeof(long));
for(vs = scr->vScreenList, i = 0;
vs != NULL && i < numVscreens;
vs = vs->next, i++) {
data[i] = vs->window;
}
XChangeProperty(dpy, scr->XineramaRoot,
XA__NET_VIRTUAL_ROOTS, XA_WINDOW,
32, PropModeReplace,
(unsigned char *)data, numVscreens);
/* This might fail, but what can we do about it? */
free(data);
d0 = XA__NET_VIRTUAL_ROOTS;
XChangeProperty(dpy, scr->XineramaRoot,
XA__NET_SUPPORTED, XA_ATOM,
32, PropModeAppend,
(unsigned char *)&d0, 1);
}
}
#endif
static void EwmhTerminateScreen(ScreenInfo *scr)
{
XDeleteProperty(dpy, scr->XineramaRoot, XA__NET_SUPPORTED);
/*
* Don't delete scr->icccm_Window; let it be deleted automatically
* when we terminate the X server connection. A replacement window
* manager may want to start working immediately after it has
* disappeared.
*/
}
/*
* Clear everything that needs to be cleared before we exit.
*/
void EwmhTerminate(void)
{
int scrnum;
ScreenInfo *scr;
for(scrnum = 0; scrnum < NumScreens; scrnum++) {
if((scr = ScreenList[scrnum]) == NULL) {
continue;
}
EwmhTerminateScreen(scr);
}
}
/*
* Event handler: lost the WM_Sn selection
* (that's the only selection we have).
*/
void EwmhSelectionClear(XSelectionClearEvent *sev)
{
#ifdef DEBUG_EWMH
fprintf(stderr, "sev->window = %x\n", (unsigned)sev->window);
#endif
DoShutdown();
}
/*
* When accepting client messages to the root window,
* be accepting and accept both the real root window and the
* current virtual screen.
*
* Should perhaps also accept any other virtual screen.
*/
bool EwmhClientMessage(XClientMessageEvent *msg)
{
if(msg->format != 32) {
return false;
}
/* Messages regarding any window */
if(msg->message_type == XA__NET_WM_DESKTOP) {
EwmhClientMessage_NET_WM_DESKTOP(msg);
return true;
}
else if(msg->message_type == XA__NET_WM_STATE) {
EwmhClientMessage_NET_WM_STATE(msg);
return true;
}
else if(msg->message_type == XA__NET_ACTIVE_WINDOW) {
EwmhClientMessage_NET_ACTIVE_WINDOW(msg);
return true;
}
else if(msg->message_type == XA__NET_WM_MOVERESIZE) {
EwmhClientMessage_NET_WM_MOVERESIZE(msg);
return true;
}
else if(msg->message_type == XA__NET_CLOSE_WINDOW) {
EwmhClientMessage_NET_CLOSE_WINDOW(msg);
return true;
}
/* Messages regarding the root window */
if(msg->window != Scr->XineramaRoot &&
msg->window != Scr->Root) {
#ifdef DEBUG_EWMH
fprintf(stderr, "Received unrecognized client message: %s\n",
XGetAtomName(dpy, msg->message_type));
#endif
return false;
}
if(msg->message_type == XA__NET_CURRENT_DESKTOP) {
GotoWorkSpaceByNumber(Scr->currentvs, msg->data.l[0]);
return true;
}
else if(msg->message_type == XA__NET_SHOWING_DESKTOP) {
ShowBackground(Scr->currentvs, msg->data.l[0] ? 1 : 0);
return true;
}
else {
#ifdef DEBUG_EWMH
fprintf(stderr, "Received unrecognized client message about root window: %s\n",
XGetAtomName(dpy, msg->message_type));
#endif
}
return false;
}
/*
* The format of the _NET_WM_ICON property is
*
* [0] width
* [1] height
* height repetitions of
* row, which is
* width repetitions of
* pixel: ARGB
* repeat for next size.
*
* Some icons can be 256x256 CARDINALs which is 65536 CARDINALS!
* Therefore we fetch in pieces and skip the pixels of large icons
* until needed.
*
* First scan all sizes. Keep a record of the closest smaller and larger
* size. At the end, choose from one of those.
* Finally, go and fetch the pixel data.
*/
Image *EwmhGetIcon(ScreenInfo *scr, TwmWindow *twm_win)
{
int fetch_offset;
Atom actual_type;
int actual_format;
unsigned long nitems, bytes_after;
unsigned long *prop;
int wanted_area;
int smaller, larger;
int offset;
int smaller_offset, larger_offset;
int i;
int width, height;
fetch_offset = 0;
if(XGetWindowProperty(dpy, twm_win->w, XA__NET_WM_ICON,
fetch_offset, 8 * 1024, False, XA_CARDINAL,
&actual_type, &actual_format, &nitems,
&bytes_after, (unsigned char **)&prop) != Success || nitems == 0) {
return NULL;
}
if(actual_format != 32) {
XFree(prop);
return NULL;
}
#ifdef DEBUG_EWMH
fprintf(stderr, "_NET_WM_ICON data fetched\n");
#endif
/*
* Usually the icons are square, but that is not a rule.
* So we measure the area instead.
*
* Approach wanted size from both directions and at the end,
* choose the "nearest".
*/
wanted_area = Scr->PreferredIconWidth * Scr->PreferredIconHeight;
smaller = 0;
larger = 999999;
smaller_offset = -1;
larger_offset = -1;
i = 0;
for(;;) {
offset = i;
int w = prop[i++];
int h = prop[i++];
int size = w * h;
const int area = w * h;
#ifdef DEBUG_EWMH
fprintf(stderr, "[%d+%d] w=%d h=%d\n", fetch_offset, offset, w, h);
#endif
if(area == wanted_area) {
#ifdef DEBUG_EWMH
fprintf(stderr, "exact match [%d+%d=%d] w=%d h=%d\n", fetch_offset, offset,
fetch_offset + offset, w, h);
#endif /* DEBUG_EWMH */
smaller_offset = fetch_offset + offset;
smaller = area;
larger_offset = -1;
break;
}
else if(area < wanted_area) {
if(area > smaller) {
#ifdef DEBUG_EWMH
fprintf(stderr, "increase smaller, was [%d]\n", smaller_offset);
#endif /* DEBUG_EWMH */
smaller = area;
smaller_offset = fetch_offset + offset;
}
}
else { /* area > wanted_area */
if(area < larger) {
#ifdef DEBUG_EWMH
fprintf(stderr, "decrease larger, was [%d]\n", larger_offset);
#endif /* DEBUG_EWMH */
larger = area;
larger_offset = fetch_offset + offset;
}
}
if(i + size + 2 > nitems) {
#ifdef DEBUG_EWMH
fprintf(stderr, "not enough data: %d + %d > %ld \n", i, size, nitems);
#endif /* DEBUG_EWMH */
if(i + size + 2 <= nitems + bytes_after / 4) {
/* we can fetch some more... */
XFree(prop);
fetch_offset += i + size;
if(XGetWindowProperty(dpy, twm_win->w, XA__NET_WM_ICON,
fetch_offset, 8 * 1024, False, XA_CARDINAL,
&actual_type, &actual_format, &nitems,
&bytes_after, (unsigned char **)&prop) != Success) {
continue;
}
i = 0;
continue;
}
break;
}
i += size;
}
/*
* Choose which icon approximates our desired size best.
*/
int area = 0;
ALLOW_DEAD_STORE(area); // all branches below init it
if(smaller_offset >= 0) {
if(larger_offset >= 0) {
/* choose the nearest */
#ifdef DEBUG_EWMH
fprintf(stderr, "choose nearest %d %d\n", smaller, larger);
#endif /* DEBUG_EWMH */
if((double)larger / wanted_area > (double)wanted_area / smaller) {
offset = smaller_offset;
area = smaller;
}
else {
offset = larger_offset;
area = larger;
}
}
else {
/* choose smaller */
#ifdef DEBUG_EWMH
fprintf(stderr, "choose smaller (only) %d\n", smaller);
#endif /* DEBUG_EWMH */
offset = smaller_offset;
area = smaller;
}
}
else if(larger_offset >= 0) {
/* choose larger */
#ifdef DEBUG_EWMH
fprintf(stderr, "choose larger (only) %d\n", larger);
#endif /* DEBUG_EWMH */
offset = larger_offset;
area = larger;
}
else {
/* no icons found at all? */
#ifdef DEBUG_EWMH
fprintf(stderr, "nothing to choose from\n");
#endif /* DEBUG_EWMH */
XFree(prop);
return NULL;
}
/*
* Now fetch the pixels.
*/
#ifdef DEBUG_EWMH
fprintf(stderr, "offset = %d fetch_offset = %d\n", offset, fetch_offset);
fprintf(stderr, "offset + 2 + area = %d fetch_offset + nitems = %ld\n",
offset + 2 + area, fetch_offset + nitems);
#endif /* DEBUG_EWMH */
if(offset < fetch_offset ||
offset + 2 + area > fetch_offset + nitems) {
XFree(prop);
fetch_offset = offset;
#ifdef DEBUG_EWMH
fprintf(stderr, "refetching from %d\n", fetch_offset);
#endif /* DEBUG_EWMH */
if(XGetWindowProperty(dpy, twm_win->w, XA__NET_WM_ICON,
fetch_offset, 2 + area, False, XA_CARDINAL,
&actual_type, &actual_format, &nitems,
&bytes_after, (unsigned char **)&prop) != Success) {
return NULL;
}
}
i = offset - fetch_offset;
width = prop[i++];
height = prop[i++];
#ifdef DEBUG_EWMH
fprintf(stderr, "Chosen [%d] w=%d h=%d area=%d\n", offset, width, height, area);
#endif /* DEBUG_EWMH */
assert(width * height == area);
Image *image = ExtractIcon(scr, &prop[i], width, height);
XFree(prop);
return image;
}
static uint16_t *buffer_16bpp;
static uint32_t *buffer_32bpp;
static void convert_for_16(int w, int x, int y, int argb)
{
int r = (argb >> 16) & 0xFF;
int g = (argb >> 8) & 0xFF;
int b = (argb >> 0) & 0xFF;
buffer_16bpp [y * w + x] = ((r >> 3) << 11) + ((g >> 2) << 5) + (b >> 3);
}
static void convert_for_32(int w, int x, int y, int argb)
{
buffer_32bpp [y * w + x] = argb & 0x00FFFFFF;
}
static Image *ExtractIcon(ScreenInfo *scr, unsigned long *prop, int width,
int height)
{
XImage *ximage;
void (*store_data)(int w, int x, int y, int argb);
int x, y, transparency;
int rowbytes;
unsigned char *maskbits;
GC gc;
Pixmap pixret;
Pixmap mask;
Image *image;
int i;
ximage = NULL;
/** XXX sort of duplicated from util.c:LoadJpegImage() */
if(scr->d_depth == 16) {
store_data = convert_for_16;
buffer_16bpp = malloc(width * height * sizeof(buffer_16bpp[0]));
buffer_32bpp = NULL;
ximage = XCreateImage(dpy, CopyFromParent, scr->d_depth, ZPixmap, 0,
(char *) buffer_16bpp, width, height, 16, width * 2);
}
else if(scr->d_depth == 24 || scr->d_depth == 32) {
store_data = convert_for_32;
buffer_32bpp = malloc(width * height * sizeof(buffer_32bpp[0]));
buffer_16bpp = NULL;
ximage = XCreateImage(dpy, CopyFromParent, scr->d_depth, ZPixmap, 0,
(char *) buffer_32bpp, width, height, 32, width * 4);
}
else {
#ifdef DEBUG_EWMH
fprintf(stderr, "Screen unsupported depth for 32-bit icon: %d\n", scr->d_depth);
#endif /* DEBUG_EWMH */
XFree(prop);
return NULL;
}
if(ximage == NULL) {
#ifdef DEBUG_EWMH
fprintf(stderr, "cannot create image for icon\n");
#endif /* DEBUG_EWMH */
XFree(prop);
return NULL;
}
transparency = 0;
rowbytes = (width + 7) / 8;
maskbits = calloc(height, rowbytes);
/*
* Copy all ARGB pixels to the pixmap (the RGB part), and the bitmap (the
* Alpha, or opaqueness part). If any pixels are transparent, we're going
* to need a shape.
*/
i = 0;
for(y = 0; y < height; y++) {
for(x = 0; x < width; x++) {
unsigned long argb = prop[i++];
store_data(width, x, y, argb);
int opaque = ((argb >> 24) & 0xFF) >= 0x80; /* arbitrary cutoff */
if(opaque) {
maskbits [rowbytes * y + (x / 8)] |= 0x01 << (x % 8);
}
else {
transparency = 1;
}
}
}
gc = DefaultGC(dpy, scr->screen);
pixret = XCreatePixmap(dpy, scr->Root, width, height, scr->d_depth);
XPutImage(dpy, pixret, gc, ximage, 0, 0, 0, 0, width, height);
XDestroyImage(ximage); /* also frees buffer_{16,32}bpp */
ximage = NULL;
mask = None;
if(transparency) {
mask = XCreatePixmapFromBitmapData(dpy, scr->Root, (char *)maskbits,
width, height, 1, 0, 1);
}
free(maskbits);
image = AllocImage();
image->width = width;
image->height = height;
image->pixmap = pixret;
image->mask = mask;
return image;
}
/*
* Handle a PropertyNotify on _NET_WM_ICON.
*/
static void EwmhHandle_NET_WM_ICONNotify(XPropertyEvent *event,
TwmWindow *twm_win)
{
unsigned long valuemask; /* mask for create windows */
XSetWindowAttributes attributes; /* attributes for create windows */
Icon *icon = twm_win->icon;
int x;
#ifdef DEBUG_EWMH
fprintf(stderr, "EwmhHandlePropertyNotify: NET_WM_ICON\n");
#endif /* DEBUG_EWMH */
/*
* If there is no icon yet, we'll look at this property
* later, if and when we do create an icon.
*/
if(!icon || icon->match != match_net_wm_icon) {
#ifdef DEBUG_EWMH
fprintf(stderr, "no icon, or not match_net_wm_icon\n");
#endif /* DEBUG_EWMH */
return;
}
Image *image = EwmhGetIcon(Scr, twm_win);
/* TODO: de-duplicate with handling of XA_WM_HINTS */
{
Image *old_image = icon->image;
icon->image = image;
FreeImage(old_image);
}
if(twm_win->icon->bm_w) {
XDestroyWindow(dpy, twm_win->icon->bm_w);
}
valuemask = CWBackPixmap;
attributes.background_pixmap = image->pixmap;
x = GetIconOffset(twm_win->icon);
twm_win->icon->bm_w =
XCreateWindow(dpy, twm_win->icon->w, x, 0,
twm_win->icon->width,
twm_win->icon->height,
0, Scr->d_depth,
CopyFromParent, Scr->d_visual,
valuemask, &attributes);
if(image->mask) {
XShapeCombineMask(dpy, twm_win->icon->bm_w, ShapeBounding, 0, 0, image->mask,
ShapeSet);
XShapeCombineMask(dpy, twm_win->icon->w, ShapeBounding, x, 0, image->mask,
ShapeSet);
}