-
Notifications
You must be signed in to change notification settings - Fork 30
/
Window.cc
4334 lines (3632 loc) · 134 KB
/
Window.cc
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
// -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
// Window.cc for Blackbox - an X11 Window manager
// Copyright (c) 2001 - 2005 Sean 'Shaleh' Perry <[email protected]>
// Copyright (c) 1997 - 2000, 2002 - 2005
// Bradley T Hughes <bhughes at trolltech.com>
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
#include "gettext.h"
// make sure we get bt::textPropertyToString()
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include "Window.hh"
#include "Screen.hh"
#include "WindowGroup.hh"
#include "Windowmenu.hh"
#include "Workspace.hh"
#include "blackbox.hh"
#include <Pen.hh>
#include <PixmapCache.hh>
#include <Unicode.hh>
#include <X11/Xatom.h>
#ifdef SHAPE
# include <X11/extensions/shape.h>
#endif
#include <assert.h>
/*
sometimes C++ is a pain in the ass... it gives us stuff like the
default copy constructor and assignment operator (which does member
my member copy/assignment), but what we don't get is a default
comparison operator... how fucked up is that?
the language is designed to handle this:
struct foo { int a; int b };
foo a = { 0 , 0 }, b = a;
foo c;
c = a;
BUT, BUT, BUT, forget about doing this:
a = findFoo();
if (c == a)
return; // nothing to do
c = a;
ARGH!@#)(!*@#)(@#*$(!@#
*/
bool operator==(const WMNormalHints &x, const WMNormalHints &y);
// Event mask used for managed client windows.
const unsigned long client_window_event_mask =
(PropertyChangeMask | StructureNotifyMask);
/*
* Returns the appropriate WindowType based on the _NET_WM_WINDOW_TYPE
*/
static WindowType window_type_from_atom(const bt::EWMH &ewmh, Atom atom) {
if (atom == ewmh.wmWindowTypeDialog())
return WindowTypeDialog;
if (atom == ewmh.wmWindowTypeDesktop())
return WindowTypeDesktop;
if (atom == ewmh.wmWindowTypeDock())
return WindowTypeDock;
if (atom == ewmh.wmWindowTypeMenu())
return WindowTypeMenu;
if (atom == ewmh.wmWindowTypeNotification())
return WindowTypeNotification;
if (atom == ewmh.wmWindowTypeSplash())
return WindowTypeSplash;
if (atom == ewmh.wmWindowTypeToolbar())
return WindowTypeToolbar;
if (atom == ewmh.wmWindowTypeUtility())
return WindowTypeUtility;
if (atom == ewmh.wmWindowTypeCombo())
return WindowTypeCombo;
if (atom == ewmh.wmWindowTypeDnd())
return WindowTypeDnd;
if (atom == ewmh.wmWindowTypeDropdownMenu())
return WindowTypeDropdownMenu;
if (atom == ewmh.wmWindowTypePopupMenu())
return WindowTypePopupMenu;
if (atom == ewmh.wmWindowTypeTooltip())
return WindowTypeTooltip;
return WindowTypeNormal;
}
/*
* Determine the appropriate decorations and functions based on the
* given properties and hints.
*/
static void update_decorations(WindowDecorationFlags &decorations,
WindowFunctionFlags &functions,
bool transient,
const EWMH &ewmh,
const MotifHints &motifhints,
const WMNormalHints &wmnormal,
const WMProtocols &wmprotocols) {
decorations = AllWindowDecorations;
functions = AllWindowFunctions;
// transients should be kept on the same workspace are their parents
if (transient)
functions &= ~WindowFunctionChangeWorkspace;
// modify the window decorations/functions based on window type
switch (ewmh.window_type) {
case WindowTypeDialog:
decorations &= ~(WindowDecorationIconify |
WindowDecorationMaximize);
functions &= ~(WindowFunctionShade |
WindowFunctionIconify |
WindowFunctionMaximize |
WindowFunctionChangeLayer |
WindowFunctionFullScreen |
WindowFunctionStick);
break;
case WindowTypeDesktop:
case WindowTypeDock:
case WindowTypeSplash:
case WindowTypeNotification:
case WindowTypeCombo:
case WindowTypeDnd:
case WindowTypePopupMenu:
case WindowTypeDropdownMenu:
case WindowTypeTooltip:
decorations = NoWindowDecorations;
functions = NoWindowFunctions;
break;
case WindowTypeToolbar:
case WindowTypeMenu:
decorations &= ~(WindowDecorationHandle |
WindowDecorationGrip |
WindowDecorationBorder |
WindowDecorationIconify |
WindowDecorationMaximize);
functions &= ~(WindowFunctionResize |
WindowFunctionShade |
WindowFunctionIconify |
WindowFunctionMaximize |
WindowFunctionFullScreen);
break;
case WindowTypeUtility:
decorations &= ~(WindowDecorationIconify |
WindowDecorationMaximize);
functions &= ~(WindowFunctionShade |
WindowFunctionIconify |
WindowFunctionMaximize);
break;
default:
break;
}
// mask away stuff turned off by Motif hints
decorations &= motifhints.decorations;
functions &= motifhints.functions;
// disable shade if we do not have a titlebar
if (!(decorations & WindowDecorationTitlebar))
functions &= ~WindowFunctionShade;
// Remove decorations for windows in fullscreen mode
if (ewmh.fullscreen) {
decorations = NoWindowDecorations;
functions &= ~(WindowFunctionMove |
WindowFunctionResize |
WindowFunctionShade);
}
// disable grips and maximize if we have a fixed size window
if ((wmnormal.flags & (PMinSize|PMaxSize)) == (PMinSize|PMaxSize)
&& wmnormal.max_width <= wmnormal.min_width
&& wmnormal.max_height <= wmnormal.min_height) {
decorations &= ~(WindowDecorationMaximize |
WindowDecorationGrip);
functions &= ~(WindowFunctionResize |
WindowFunctionMaximize);
}
// cannot close if client doesn't understand WM_DELETE_WINDOW
if (!wmprotocols.wm_delete_window) {
decorations &= ~WindowDecorationClose;
functions &= ~WindowFunctionClose;
}
}
/*
* Calculate the frame margin based on the given decorations and
* style.
*/
static
bt::EWMH::Strut update_margin(WindowDecorationFlags decorations,
const WindowStyle &style) {
bt::EWMH::Strut margin;
const unsigned int bw = ((decorations & WindowDecorationBorder)
? style.frame_border_width
: 0u);
margin.top = margin.bottom = margin.left = margin.right = bw;
if (decorations & WindowDecorationTitlebar)
margin.top += style.title_height - bw;
if (decorations & WindowDecorationHandle)
margin.bottom += style.handle_height - bw;
return margin;
}
/*
* Add specified window to the appropriate window group, creating a
* new group if necessary.
*/
static BWindowGroup *update_window_group(Window window_group,
Blackbox *blackbox,
BlackboxWindow *win) {
BWindowGroup *group = win->findWindowGroup();
if (!group) {
new BWindowGroup(blackbox, window_group);
group = win->findWindowGroup();
assert(group != 0);
}
group->addWindow(win);
return group;
}
/*
* Calculate the size of the frame window and constrain it to the
* size specified by the size hints of the client window.
*
* 'rect' refers to the geometry of the frame in pixels.
*/
enum Corner {
TopLeft,
TopRight,
BottomLeft,
BottomRight
};
static bt::Rect constrain(const bt::Rect &rect,
const bt::EWMH::Strut &margin,
const WMNormalHints &wmnormal,
Corner corner) {
bt::Rect r;
// 'rect' represents the requested frame size, we need to strip
// 'margin' off and constrain the client size
r.setCoords(rect.left() + static_cast<signed>(margin.left),
rect.top() + static_cast<signed>(margin.top),
rect.right() - static_cast<signed>(margin.right),
rect.bottom() - static_cast<signed>(margin.bottom));
unsigned int dw = r.width(), dh = r.height();
const unsigned int base_width = (wmnormal.base_width
? wmnormal.base_width
: wmnormal.min_width),
base_height = (wmnormal.base_height
? wmnormal.base_height
: wmnormal.min_height);
// fit to min/max size
if (dw < wmnormal.min_width)
dw = wmnormal.min_width;
if (dh < wmnormal.min_height)
dh = wmnormal.min_height;
if (dw > wmnormal.max_width)
dw = wmnormal.max_width;
if (dh > wmnormal.max_height)
dh = wmnormal.max_height;
assert(dw >= base_width && dh >= base_height);
// fit to size increments
if (wmnormal.flags & PResizeInc) {
dw = (((dw - base_width) / wmnormal.width_inc)
* wmnormal.width_inc) + base_width;
dh = (((dh - base_height) / wmnormal.height_inc)
* wmnormal.height_inc) + base_height;
}
/*
* honor aspect ratios (based on twm which is based on uwm)
*
* The math looks like this:
*
* minAspectX dwidth maxAspectX
* ---------- <= ------- <= ----------
* minAspectY dheight maxAspectY
*
* If that is multiplied out, then the width and height are
* invalid in the following situations:
*
* minAspectX * dheight > minAspectY * dwidth
* maxAspectX * dheight < maxAspectY * dwidth
*
*/
if (wmnormal.flags & PAspect) {
unsigned int delta;
const unsigned int min_asp_x = wmnormal.min_aspect_x,
min_asp_y = wmnormal.min_aspect_y,
max_asp_x = wmnormal.max_aspect_x,
max_asp_y = wmnormal.max_aspect_y,
w_inc = wmnormal.width_inc,
h_inc = wmnormal.height_inc;
if (min_asp_x * dh > min_asp_y * dw) {
delta = ((min_asp_x * dh / min_asp_y - dw) * w_inc) / w_inc;
if (dw + delta <= wmnormal.max_width) {
dw += delta;
} else {
delta = ((dh - (dw * min_asp_y) / min_asp_x) * h_inc) / h_inc;
if (dh - delta >= wmnormal.min_height) dh -= delta;
}
}
if (max_asp_x * dh < max_asp_y * dw) {
delta = ((max_asp_y * dw / max_asp_x - dh) * h_inc) / h_inc;
if (dh + delta <= wmnormal.max_height) {
dh += delta;
} else {
delta = ((dw - (dh * max_asp_x) / max_asp_y) * w_inc) / w_inc;
if (dw - delta >= wmnormal.min_width) dw -= delta;
}
}
}
r.setSize(dw, dh);
// add 'margin' back onto 'r'
r.setCoords(r.left() - margin.left, r.top() - margin.top,
r.right() + margin.right, r.bottom() + margin.bottom);
// move 'r' to the specified corner
int dx = rect.right() - r.right();
int dy = rect.bottom() - r.bottom();
switch (corner) {
case TopLeft:
// nothing to do
break;
case TopRight:
r.setPos(r.x() + dx, r.y());
break;
case BottomLeft:
r.setPos(r.x(), r.y() + dy);
break;
case BottomRight:
r.setPos(r.x() + dx, r.y() + dy);
break;
}
return r;
}
/*
* Positions 'rect' according to the client window geometry and window
* gravity.
*/
static bt::Rect applyGravity(const bt::Rect &rect,
const bt::EWMH::Strut &margin,
int gravity) {
bt::Rect r;
// apply horizontal window gravity
switch (gravity) {
default:
case NorthWestGravity:
case SouthWestGravity:
case WestGravity:
r.setX(rect.x());
break;
case NorthGravity:
case SouthGravity:
case CenterGravity:
r.setX(rect.x() - (margin.left + margin.right) / 2);
break;
case NorthEastGravity:
case SouthEastGravity:
case EastGravity:
r.setX(rect.x() - (margin.left + margin.right) + 2);
break;
case ForgetGravity:
case StaticGravity:
r.setX(rect.x() - margin.left);
break;
}
// apply vertical window gravity
switch (gravity) {
default:
case NorthWestGravity:
case NorthEastGravity:
case NorthGravity:
r.setY(rect.y());
break;
case CenterGravity:
case EastGravity:
case WestGravity:
r.setY(rect.y() - ((margin.top + margin.bottom) / 2));
break;
case SouthWestGravity:
case SouthEastGravity:
case SouthGravity:
r.setY(rect.y() - (margin.bottom + margin.top) + 2);
break;
case ForgetGravity:
case StaticGravity:
r.setY(rect.y() - margin.top);
break;
}
r.setSize(rect.width() + margin.left + margin.right,
rect.height() + margin.top + margin.bottom);
return r;
}
/*
* The reverse of the applyGravity function.
*
* Positions 'rect' according to the frame window geometry and window
* gravity.
*/
static bt::Rect restoreGravity(const bt::Rect &rect,
const bt::EWMH::Strut &margin,
int gravity) {
bt::Rect r;
// restore horizontal window gravity
switch (gravity) {
default:
case NorthWestGravity:
case SouthWestGravity:
case WestGravity:
r.setX(rect.x());
break;
case NorthGravity:
case SouthGravity:
case CenterGravity:
r.setX(rect.x() + (margin.left + margin.right) / 2);
break;
case NorthEastGravity:
case SouthEastGravity:
case EastGravity:
r.setX(rect.x() + (margin.left + margin.right) - 2);
break;
case ForgetGravity:
case StaticGravity:
r.setX(rect.x() + margin.left);
break;
}
// restore vertical window gravity
switch (gravity) {
default:
case NorthWestGravity:
case NorthEastGravity:
case NorthGravity:
r.setY(rect.y());
break;
case CenterGravity:
case EastGravity:
case WestGravity:
r.setY(rect.y() + (margin.top + margin.bottom) / 2);
break;
case SouthWestGravity:
case SouthEastGravity:
case SouthGravity:
r.setY(rect.y() + (margin.top + margin.bottom) - 2);
break;
case ForgetGravity:
case StaticGravity:
r.setY(rect.y() + margin.top);
break;
}
r.setSize(rect.width() - margin.left - margin.right,
rect.height() - margin.top - margin.bottom);
return r;
}
static bt::ustring readWMName(Blackbox *blackbox, Window window) {
bt::ustring name;
if (!blackbox->ewmh().readWMName(window, name) || name.empty()) {
XTextProperty text_prop;
if (XGetWMName(blackbox->XDisplay(), window, &text_prop)) {
name = bt::toUnicode(bt::textPropertyToString(blackbox->XDisplay(),
text_prop));
XFree((char *) text_prop.value);
}
}
// TRANS The name to use for a window that has no name.
if (name.empty())
name = bt::toUnicode(gettext("Unnamed"));
return name;
}
static bt::ustring readWMIconName(Blackbox *blackbox, Window window) {
bt::ustring name;
if (!blackbox->ewmh().readWMIconName(window, name) || name.empty()) {
XTextProperty text_prop;
if (XGetWMIconName(blackbox->XDisplay(), window, &text_prop)) {
name = bt::toUnicode(bt::textPropertyToString(blackbox->XDisplay(),
text_prop));
XFree((char *) text_prop.value);
}
}
if (name.empty())
return bt::ustring();
return name;
}
static EWMH readEWMH(const bt::EWMH &bewmh,
Window window,
int currentWorkspace) {
EWMH ewmh;
ewmh.window_type = WindowTypeNormal;
ewmh.workspace = 0; // initialized properly below
ewmh.modal = false;
ewmh.maxv = false;
ewmh.maxh = false;
ewmh.shaded = false;
ewmh.skip_taskbar = false;
ewmh.skip_pager = false;
ewmh.hidden = false;
ewmh.fullscreen = false;
ewmh.above = false;
ewmh.below = false;
ewmh.sticky = false;
ewmh.urgent = false;
// note: wm_name and wm_icon_name are read separately
bool ret;
bt::EWMH::AtomList atoms;
ret = bewmh.readWMWindowType(window, atoms);
if (ret) {
bt::EWMH::AtomList::iterator it = atoms.begin(), end = atoms.end();
for (; it != end; ++it) {
if (bewmh.isSupportedWMWindowType(*it)) {
ewmh.window_type = ::window_type_from_atom(bewmh, *it);
break;
}
}
}
atoms.clear();
ret = bewmh.readWMState(window, atoms);
if (ret) {
bt::EWMH::AtomList::iterator it = atoms.begin(), end = atoms.end();
for (; it != end; ++it) {
Atom state = *it;
if (state == bewmh.wmStateModal()) {
ewmh.modal = true;
} else if (state == bewmh.wmStateMaximizedVert()) {
ewmh.maxv = true;
} else if (state == bewmh.wmStateMaximizedHorz()) {
ewmh.maxh = true;
} else if (state == bewmh.wmStateShaded()) {
ewmh.shaded = true;
} else if (state == bewmh.wmStateSkipTaskbar()) {
ewmh.skip_taskbar = true;
} else if (state == bewmh.wmStateSkipPager()) {
ewmh.skip_pager = true;
} else if (state == bewmh.wmStateHidden()) {
/*
ignore _NET_WM_STATE_HIDDEN if present, the wm sets this
state, not the application
*/
} else if (state == bewmh.wmStateFullscreen()) {
ewmh.fullscreen = true;
} else if (state == bewmh.wmStateAbove()) {
ewmh.above = true;
} else if (state == bewmh.wmStateBelow()) {
ewmh.below = true;
} else if (state == bewmh.wmStateSticky()) {
ewmh.sticky = true;
} else if (state == bewmh.wmStateDemandsAttention()) {
ewmh.urgent = true;
} else if (state == bewmh.wmStateFocused()) {
/*
* ignore _NET_WM_STATE_FOCUSED if present, the wm sets
* this state, not the application
*/
}
}
}
switch (ewmh.window_type) {
case WindowTypeDesktop:
case WindowTypeDock:
// these types should occupy all workspaces by default
ewmh.workspace = bt::BSENTINEL;
break;
default:
if (!bewmh.readWMDesktop(window, ewmh.workspace))
ewmh.workspace = currentWorkspace;
break;
} //switch
return ewmh;
}
/*
* Returns the MotifWM hints for the specified window.
*/
static MotifHints readMotifWMHints(Blackbox *blackbox, Window window) {
MotifHints motif;
motif.decorations = AllWindowDecorations;
motif.functions = AllWindowFunctions;
/*
this structure only contains 3 elements, even though the Motif 2.0
structure contains 5, because we only use the first 3
*/
struct PropMotifhints {
unsigned long flags;
unsigned long functions;
unsigned long decorations;
};
static const unsigned int PROP_MWM_HINTS_ELEMENTS = 3u;
enum { // MWM flags
MWM_HINTS_FUNCTIONS = 1<<0,
MWM_HINTS_DECORATIONS = 1<<1
};
enum { // MWM functions
MWM_FUNC_ALL = 1<<0,
MWM_FUNC_RESIZE = 1<<1,
MWM_FUNC_MOVE = 1<<2,
MWM_FUNC_MINIMIZE = 1<<3,
MWM_FUNC_MAXIMIZE = 1<<4,
MWM_FUNC_CLOSE = 1<<5
};
enum { // MWM decorations
MWM_DECOR_ALL = 1<<0,
MWM_DECOR_BORDER = 1<<1,
MWM_DECOR_RESIZEH = 1<<2,
MWM_DECOR_TITLE = 1<<3,
MWM_DECOR_MENU = 1<<4,
MWM_DECOR_MINIMIZE = 1<<5,
MWM_DECOR_MAXIMIZE = 1<<6
};
Atom atom_return;
PropMotifhints *prop = 0;
int format;
unsigned long num, len;
int ret = XGetWindowProperty(blackbox->XDisplay(), window,
blackbox->motifWmHintsAtom(), 0,
PROP_MWM_HINTS_ELEMENTS, False,
blackbox->motifWmHintsAtom(), &atom_return,
&format, &num, &len,
(unsigned char **) &prop);
if (ret != Success || !prop || num != PROP_MWM_HINTS_ELEMENTS) {
if (prop) XFree(prop);
return motif;
}
if (prop->flags & MWM_HINTS_FUNCTIONS) {
if (prop->functions & MWM_FUNC_ALL) {
motif.functions = AllWindowFunctions;
} else {
// default to the functions that cannot be set through
// _MOTIF_WM_HINTS
motif.functions = (WindowFunctionShade
| WindowFunctionChangeWorkspace
| WindowFunctionChangeLayer
| WindowFunctionFullScreen);
if (prop->functions & MWM_FUNC_RESIZE)
motif.functions |= WindowFunctionResize;
if (prop->functions & MWM_FUNC_MOVE)
motif.functions |= WindowFunctionMove;
if (prop->functions & MWM_FUNC_MINIMIZE)
motif.functions |= WindowFunctionIconify;
if (prop->functions & MWM_FUNC_MAXIMIZE)
motif.functions |= WindowFunctionMaximize;
if (prop->functions & MWM_FUNC_CLOSE)
motif.functions |= WindowFunctionClose;
}
}
if (prop->flags & MWM_HINTS_DECORATIONS) {
if (prop->decorations & MWM_DECOR_ALL) {
motif.decorations = AllWindowDecorations;
} else {
motif.decorations = NoWindowDecorations;
if (prop->decorations & MWM_DECOR_BORDER)
motif.decorations |= WindowDecorationBorder;
if (prop->decorations & MWM_DECOR_RESIZEH)
motif.decorations |= WindowDecorationHandle;
if (prop->decorations & MWM_DECOR_TITLE)
motif.decorations |= WindowDecorationTitlebar;
if (prop->decorations & MWM_DECOR_MINIMIZE)
motif.decorations |= WindowDecorationIconify;
if (prop->decorations & MWM_DECOR_MAXIMIZE)
motif.decorations |= WindowDecorationMaximize;
}
}
if (motif.decorations & WindowDecorationHandle) {
if (motif.functions & WindowFunctionResize)
motif.decorations |= WindowDecorationGrip;
else
motif.decorations &= ~WindowDecorationGrip;
}
if (motif.decorations & WindowDecorationTitlebar) {
if (motif.functions & WindowFunctionClose)
motif.decorations |= WindowDecorationClose;
else
motif.decorations &= ~WindowDecorationClose;
}
XFree(prop);
return motif;
}
/*
* Returns the value of the WM_HINTS property. If the property is not
* set, a set of default values is returned instead.
*/
static WMHints readWMHints(Blackbox *blackbox, Window window) {
WMHints wmh;
wmh.accept_focus = false;
wmh.window_group = None;
wmh.initial_state = NormalState;
wmh.urgency = false;
XWMHints *wmhint = XGetWMHints(blackbox->XDisplay(), window);
if (!wmhint) return wmh;
if (wmhint->flags & InputHint)
wmh.accept_focus = (wmhint->input == True);
if (wmhint->flags & StateHint)
wmh.initial_state = wmhint->initial_state;
if (wmhint->flags & WindowGroupHint)
wmh.window_group = wmhint->window_group;
if (wmhint->flags & XUrgencyHint)
wmh.urgency = true;
XFree(wmhint);
return wmh;
}
/*
* Returns the value of the WM_NORMAL_HINTS property. If the property
* is not set, a set of default values is returned instead.
*/
static WMNormalHints readWMNormalHints(Blackbox *blackbox,
Window window,
const bt::ScreenInfo &screenInfo) {
WMNormalHints wmnormal;
wmnormal.flags = 0;
wmnormal.min_width = wmnormal.min_height = 1u;
wmnormal.width_inc = wmnormal.height_inc = 1u;
wmnormal.min_aspect_x = wmnormal.min_aspect_y = 1u;
wmnormal.max_aspect_x = wmnormal.max_aspect_y = 1u;
wmnormal.base_width = wmnormal.base_height = 0u;
wmnormal.win_gravity = NorthWestGravity;
/*
use the full screen, not the strut modified size. otherwise when
the availableArea changes max_width/height will be incorrect and
lead to odd rendering bugs.
*/
const bt::Rect &rect = screenInfo.rect();
wmnormal.max_width = rect.width();
wmnormal.max_height = rect.height();
XSizeHints sizehint;
long unused;
if (! XGetWMNormalHints(blackbox->XDisplay(), window, &sizehint, &unused))
return wmnormal;
wmnormal.flags = sizehint.flags;
if (sizehint.flags & PMinSize) {
if (sizehint.min_width > 0)
wmnormal.min_width = sizehint.min_width;
if (sizehint.min_height > 0)
wmnormal.min_height = sizehint.min_height;
/*
if the minimum size is bigger then the screen, adjust the
maximum size
*/
if (wmnormal.min_width > wmnormal.max_width)
wmnormal.max_width = wmnormal.min_width;
if (wmnormal.min_height > wmnormal.max_height)
wmnormal.max_height = wmnormal.min_height;
}
if (sizehint.flags & PMaxSize) {
if (sizehint.max_width >= static_cast<signed>(wmnormal.min_width))
wmnormal.max_width = sizehint.max_width;
else
wmnormal.max_width = wmnormal.min_width;
if (sizehint.max_height >= static_cast<signed>(wmnormal.min_height))
wmnormal.max_height = sizehint.max_height;
else
wmnormal.max_height = wmnormal.min_height;
}
if (sizehint.flags & PResizeInc) {
wmnormal.width_inc = sizehint.width_inc;
wmnormal.height_inc = sizehint.height_inc;
}
if (sizehint.flags & PAspect) {
wmnormal.min_aspect_x = sizehint.min_aspect.x;
wmnormal.min_aspect_y = sizehint.min_aspect.y;
wmnormal.max_aspect_x = sizehint.max_aspect.x;
wmnormal.max_aspect_y = sizehint.max_aspect.y;
}
if (sizehint.flags & PBaseSize) {
if (sizehint.base_width <= static_cast<signed>(wmnormal.min_width))
wmnormal.base_width = sizehint.base_width;
if (sizehint.base_height <= static_cast<signed>(wmnormal.min_height))
wmnormal.base_height = sizehint.base_height;
}
if (sizehint.flags & PWinGravity)
wmnormal.win_gravity = sizehint.win_gravity;
return wmnormal;
}
/*
* Retrieve which Window Manager Protocols are supported by the client
* window.
*/
static WMProtocols readWMProtocols(Blackbox *blackbox,
Window window) {
WMProtocols protocols;
protocols.wm_delete_window = false;
protocols.wm_take_focus = false;
Atom *proto;
int num_return = 0;
if (XGetWMProtocols(blackbox->XDisplay(), window,
&proto, &num_return)) {
for (int i = 0; i < num_return; ++i) {
if (proto[i] == blackbox->wmDeleteWindowAtom()) {
protocols.wm_delete_window = true;
} else if (proto[i] == blackbox->wmTakeFocusAtom()) {
protocols.wm_take_focus = true;
}
}
XFree(proto);
}
return protocols;
}
/*
* Reads the value of the WM_TRANSIENT_FOR property and returns a
* pointer to the transient parent for this window. If the
* WM_TRANSIENT_FOR is missing or invalid, this function returns 0.
*
* 'client.wmhints' should be properly updated before calling this
* function.
*
* Note: a return value of ~0ul signifies a window that should be
* transient but has no discernible parent.
*/
static Window readTransientInfo(Blackbox *blackbox,
Window window,
const bt::ScreenInfo &screenInfo,
const WMHints &wmhints) {
Window trans_for = None;
if (!XGetTransientForHint(blackbox->XDisplay(), window, &trans_for)) {
// WM_TRANSIENT_FOR hint not set
return 0;
}
if (trans_for == window) {
// wierd client... treat this window as a normal window
return 0;
}
if (trans_for == None || trans_for == screenInfo.rootWindow()) {
/*
this is a violation of the ICCCM, yet the EWMH allows this as a
way to signify a group transient.
*/
trans_for = wmhints.window_group;
}
return trans_for;
}
static bool readState(unsigned long ¤t_state,
Blackbox *blackbox,
Window window) {
current_state = NormalState;
Atom atom_return;
bool ret = false;
int foo;
unsigned long *state, ulfoo, nitems;
if ((XGetWindowProperty(blackbox->XDisplay(), window,
blackbox->wmStateAtom(),
0l, 2l, False, blackbox->wmStateAtom(),
&atom_return, &foo, &nitems, &ulfoo,
(unsigned char **) &state) != Success) ||
(! state)) {
return false;
}
if (nitems >= 1) {
current_state = static_cast<unsigned long>(state[0]);
ret = true;
}
XFree((void *) state);
return ret;
}
static void clearState(Blackbox *blackbox, Window window) {
XDeleteProperty(blackbox->XDisplay(), window, blackbox->wmStateAtom());
const bt::EWMH& ewmh = blackbox->ewmh();
ewmh.removeProperty(window, ewmh.wmDesktop());
ewmh.removeProperty(window, ewmh.wmState());
ewmh.removeProperty(window, ewmh.wmAllowedActions());