-
-
Notifications
You must be signed in to change notification settings - Fork 621
/
x11.cc
1540 lines (1321 loc) · 47.7 KB
/
x11.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
/*
*
* Conky, a system monitor, based on torsmo
*
* Any original torsmo code is licensed under the BSD license
*
* All code written since the fork of torsmo is licensed under the GPL
*
* Please see COPYING for details
*
* Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen
* Copyright (c) 2005-2024 Brenden Matthews, Philip Kovacs, et. al.
* (see AUTHORS)
* All rights reserved.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "config.h"
#include "x11-settings.h"
#include "x11.h"
#include <X11/X.h>
#include <X11/Xlibint.h>
#undef min
#undef max
#include <sys/types.h>
#include "common.h"
#include "conky.h"
#include "geometry.h"
#include "gui.h"
#include "logging.h"
#ifdef BUILD_XINPUT
#include "mouse-events.h"
#include <vector>
#endif
#include <algorithm>
#include <array>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <numeric>
#include <string>
// #ifndef OWN_WINDOW
// #include <iostream>
// #endif
extern "C" {
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wvariadic-macros"
#pragma GCC diagnostic ignored "-Wregister"
#include <X11/XKBlib.h>
#pragma GCC diagnostic pop
#include <X11/Xatom.h>
#include <X11/Xlib.h>
#include <X11/Xmd.h>
#include <X11/Xutil.h>
#ifdef BUILD_IMLIB2
#include "conky-imlib2.h"
#endif /* BUILD_IMLIB2 */
#ifdef BUILD_XFT
#include <X11/Xft/Xft.h>
#endif
#ifdef BUILD_XINERAMA
#include <X11/extensions/Xinerama.h>
#endif
#ifdef BUILD_XSHAPE
#include <X11/extensions/shape.h>
#endif /* BUILD_XSHAPE */
#ifdef BUILD_XFIXES
#include <X11/extensions/Xfixes.h>
#endif /* BUILD_XFIXES */
#ifdef BUILD_XINPUT
#include <X11/extensions/XInput.h>
#include <X11/extensions/XInput2.h>
#endif /* BUILD_XINPUT */
#ifdef HAVE_XCB_ERRORS
#include <xcb/xcb.h>
#include <xcb/xcb_errors.h>
#endif
#include <X11/Xresource.h>
}
Display *display = nullptr;
int screen;
#ifdef HAVE_XCB_ERRORS
xcb_connection_t *xcb_connection;
xcb_errors_context_t *xcb_errors_ctx;
#endif
/* Window stuff */
struct conky_x11_window window;
bool have_argb_visual = false;
/* local prototypes */
static Window find_desktop_window(Window *p_root, Window *p_desktop);
static Window find_desktop_window_impl(Window win, int w, int h);
/* WARNING, this type not in Xlib spec */
static int x11_error_handler(Display *d, XErrorEvent *err) {
char *error_name = nullptr;
bool name_allocated = false;
char *code_description = nullptr;
bool code_allocated = false;
#ifdef HAVE_XCB_ERRORS
if (xcb_errors_ctx != nullptr) {
const char *extension;
const char *base_name = xcb_errors_get_name_for_error(
xcb_errors_ctx, err->error_code, &extension);
if (extension != nullptr) {
const std::size_t size = strlen(base_name) + strlen(extension) + 4;
error_name = new char[size];
snprintf(error_name, size, "%s (%s)", base_name, extension);
name_allocated = true;
} else {
error_name = const_cast<char *>(base_name);
}
const char *major =
xcb_errors_get_name_for_major_code(xcb_errors_ctx, err->request_code);
const char *minor = xcb_errors_get_name_for_minor_code(
xcb_errors_ctx, err->request_code, err->minor_code);
if (minor != nullptr) {
const std::size_t size = strlen(major) + strlen(minor) + 4;
code_description = new char[size];
snprintf(code_description, size, "%s - %s", major, minor);
code_allocated = true;
} else {
code_description = const_cast<char *>(major);
}
}
#endif
if (error_name == nullptr) {
if (err->error_code > 0 && err->error_code < 17) {
static std::array<std::string, 17> NAMES = {
"request", "value", "window", "pixmap", "atom",
"cursor", "font", "match", "drawable", "access",
"alloc", "colormap", "G context", "ID choice", "name",
"length", "implementation"};
error_name = const_cast<char *>(NAMES[err->error_code].c_str());
} else {
static char code_name_buffer[5];
error_name = reinterpret_cast<char *>(&code_name_buffer);
snprintf(error_name, 4, "%d", err->error_code);
}
}
if (code_description == nullptr) {
const std::size_t size = 37;
code_description = new char[size];
snprintf(code_description, size, "error code: [major: %i, minor: %i]",
err->request_code, err->minor_code);
code_allocated = true;
}
DBGP(
"X %s Error:\n"
"Display: %lx, XID: %li, Serial: %lu\n"
"%s",
error_name, reinterpret_cast<uint64_t>(err->display),
static_cast<int64_t>(err->resourceid), err->serial, code_description);
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wfree-nonheap-object"
// *_allocated takes care of avoiding freeing unallocated objects
if (name_allocated) delete[] error_name;
if (code_allocated) delete[] code_description;
#pragma GCC diagnostic pop
return 0;
}
__attribute__((noreturn)) static int x11_ioerror_handler(Display *d) {
CRIT_ERR("X IO Error: Display %lx\n", reinterpret_cast<uint64_t>(d));
}
/// @brief Function to get virtual root windows of screen.
///
/// Some WMs (swm, tvtwm, amiwm, enlightenment, etc.) use virtual roots to
/// manage workspaces. These are direct descendants of root and WMs reparent all
/// children to them.
///
/// @param screen screen to get the (current) virtual root of
/// @return the virtual root window of the screen
static Window VRootWindowOfScreen(Screen *screen) {
Window root = RootWindowOfScreen(screen);
Display *dpy = DisplayOfScreen(screen);
/* go look for a virtual root */
Atom _NET_VIRTUAL_ROOTS = XInternAtom(display, "_NET_VIRTUAL_ROOTS", True);
if (_NET_VIRTUAL_ROOTS == 0) return root;
auto vroots = x11_atom_window_list(dpy, root, _NET_VIRTUAL_ROOTS);
if (vroots.empty()) return root;
Atom _NET_CURRENT_DESKTOP =
XInternAtom(display, "_NET_CURRENT_DESKTOP", True);
if (_NET_CURRENT_DESKTOP == 0) return root;
Atom actual_type;
int actual_format;
unsigned long nitems, bytesafter;
int *cardinal;
XGetWindowProperty(dpy, root, _NET_CURRENT_DESKTOP, 0, 1, False, XA_CARDINAL,
&actual_type, &actual_format, &nitems, &bytesafter,
(unsigned char **)&cardinal);
if (vroots.size() > *cardinal) { root = vroots[*cardinal]; }
XFree(cardinal);
return root;
}
inline Window VRootWindow(Display *display, int screen) {
return VRootWindowOfScreen(ScreenOfDisplay(display, screen));
}
inline Window DefaultVRootWindow(Display *display) {
return VRootWindowOfScreen(DefaultScreenOfDisplay(display));
}
/* X11 initializer */
void init_x11() {
DBGP("enter init_x11()");
if (display == nullptr) {
const std::string &dispstr = display_name.get(*state);
// passing nullptr to XOpenDisplay should open the default display
const char *disp = static_cast<unsigned int>(!dispstr.empty()) != 0u
? dispstr.c_str()
: nullptr;
if ((display = XOpenDisplay(disp)) == nullptr) {
std::string err =
std::string("can't open display: ") + XDisplayName(disp);
#ifdef BUILD_WAYLAND
NORM_ERR(err.c_str());
return;
#else /* BUILD_WAYLAND */
throw std::runtime_error(err);
#endif /* BUILD_WAYLAND */
}
}
info.x11.monitor.number = 1;
info.x11.monitor.current = 0;
info.x11.desktop.current = 1;
info.x11.desktop.number = 1;
info.x11.desktop.all_names.clear();
info.x11.desktop.name.clear();
screen = DefaultScreen(display);
XSetErrorHandler(&x11_error_handler);
XSetIOErrorHandler(&x11_ioerror_handler);
update_x11_resource_db(true);
update_x11_workarea();
get_x11_desktop_info(display, 0);
#ifdef HAVE_XCB_ERRORS
auto connection = xcb_connect(NULL, NULL);
if (!xcb_connection_has_error(connection)) {
if (xcb_errors_context_new(connection, &xcb_errors_ctx) != 0) {
xcb_errors_ctx = nullptr;
}
}
#endif /* HAVE_XCB_ERRORS */
DBGP("leave init_x11()");
}
void deinit_x11() {
if (display) {
DBGP("deinit_x11()");
XCloseDisplay(display);
display = nullptr;
}
}
// Source: dunst
// https://github.com/bebehei/dunst/blob/1bc3237a359f37905426012c0cca90d71c4b3b18/src/x11/x.c#L463
void update_x11_resource_db(bool first_run) {
XrmDatabase db;
XTextProperty prop;
Window root;
XFlush(display);
root = RootWindow(display, screen);
XLockDisplay(display);
if (XGetTextProperty(display, root, &prop, XA_RESOURCE_MANAGER)) {
if (!first_run) {
db = XrmGetDatabase(display);
XrmDestroyDatabase(db);
}
// https://github.com/dunst-project/dunst/blob/master/src/x11/x.c#L499
display->db = NULL; // should be new or deleted
db = XrmGetStringDatabase((const char *)prop.value);
XrmSetDatabase(display, db);
}
XUnlockDisplay(display);
XFlush(display);
XSync(display, false);
}
void update_x11_workarea() {
/* default work area is display */
workarea = conky::absolute_rect<int>(
conky::vec2i::Zero(), conky::vec2i(DisplayWidth(display, screen),
DisplayHeight(display, screen)));
#ifdef BUILD_XINERAMA
/* if xinerama is being used, adjust workarea to the head's area */
int useless1, useless2;
if (XineramaQueryExtension(display, &useless1, &useless2) == 0) {
return; /* doesn't even have xinerama */
}
if (XineramaIsActive(display) == 0) {
return; /* has xinerama but isn't using it */
}
int heads = 0;
XineramaScreenInfo *si = XineramaQueryScreens(display, &heads);
if (si == nullptr) {
NORM_ERR(
"warning: XineramaQueryScreen returned nullptr, ignoring head "
"settings");
return; /* queryscreens failed? */
}
int i = head_index.get(*state);
if (i < 0 || i >= heads) {
NORM_ERR("warning: invalid head index, ignoring head settings");
return;
}
XineramaScreenInfo *ps = &si[i];
workarea.set_pos(ps->x_org, ps->y_org);
workarea.set_size(ps->width, ps->height);
XFree(si);
DBGP("Fixed xinerama area to: %d %d %d %d", workarea[0], workarea[1],
workarea[2], workarea[3]);
#endif
}
/* Find root window and desktop window.
* Return desktop window on success,
* and set root and desktop byref return values.
* Return 0 on failure. */
static Window find_desktop_window(Window root) {
Window desktop = root;
/* get subwindows from root */
int display_width = DisplayWidth(display, screen);
int display_height = DisplayHeight(display, screen);
desktop = find_desktop_window_impl(root, display_width, display_height);
update_x11_workarea();
desktop =
find_desktop_window_impl(desktop, workarea.width(), workarea.height());
if (desktop != root) {
NORM_ERR("desktop window (0x%lx) is subwindow of root window (0x%lx)",
desktop, root);
} else {
NORM_ERR("desktop window (0x%lx) is root window", desktop);
}
return desktop;
}
#ifdef OWN_WINDOW
#ifdef BUILD_ARGB
namespace {
/* helper function for set_transparent_background() */
void do_set_background(Window win, uint8_t alpha) {
Colour colour = background_colour.get(*state);
colour.alpha = alpha;
unsigned long xcolor =
colour.to_x11_color(display, screen, have_argb_visual, true);
XSetWindowBackground(display, win, xcolor);
}
} // namespace
#endif /* BUILD_ARGB */
/* if no argb visual is configured sets background to ParentRelative for the
Window and all parents, else real transparency is used */
void set_transparent_background(Window win) {
#ifdef BUILD_ARGB
if (have_argb_visual) {
// real transparency
do_set_background(win, set_transparent.get(*state)
? 0
: own_window_argb_value.get(*state));
return;
}
#endif /* BUILD_ARGB */
// pseudo transparency
if (set_transparent.get(*state)) {
Window parent = win;
unsigned int i;
for (i = 0; i < 50 && parent != RootWindow(display, screen); i++) {
Window r, *children;
unsigned int n;
XSetWindowBackgroundPixmap(display, parent, ParentRelative);
XQueryTree(display, parent, &r, &parent, &children, &n);
XFree(children);
}
return;
}
#ifdef BUILD_ARGB
do_set_background(win, 0);
#endif /* BUILD_ARGB */
}
#endif /* OWN_WINDOW */
#ifdef BUILD_ARGB
static int get_argb_visual(Visual **visual, int *depth) {
/* code from gtk project, gdk_screen_get_rgba_visual */
XVisualInfo visual_template;
XVisualInfo *visual_list;
int nxvisuals = 0, i;
visual_template.screen = screen;
visual_list =
XGetVisualInfo(display, VisualScreenMask, &visual_template, &nxvisuals);
for (i = 0; i < nxvisuals; i++) {
if (visual_list[i].depth == 32 && (visual_list[i].red_mask == 0xff0000 &&
visual_list[i].green_mask == 0x00ff00 &&
visual_list[i].blue_mask == 0x0000ff)) {
*visual = visual_list[i].visual;
*depth = visual_list[i].depth;
DBGP("Found ARGB Visual");
XFree(visual_list);
return 1;
}
}
// no argb visual available
DBGP("No ARGB Visual found");
XFree(visual_list);
return 0;
}
#endif /* BUILD_ARGB */
void destroy_window() {
#ifdef BUILD_XFT
if (window.xftdraw != nullptr) { XftDrawDestroy(window.xftdraw); }
#endif /* BUILD_XFT */
if (window.gc != nullptr) { XFreeGC(display, window.gc); }
memset(&window, 0, sizeof(struct conky_x11_window));
}
void x11_init_window(lua::state &l, bool own) {
DBGP("enter x11_init_window()");
// own is unused if OWN_WINDOW is not defined
(void)own;
window.root = VRootWindow(display, screen);
if (window.root == None) {
DBGP2("no desktop window found");
return;
}
window.desktop = find_desktop_window(window.root);
window.visual = DefaultVisual(display, screen);
window.colourmap = DefaultColormap(display, screen);
#ifdef OWN_WINDOW
if (own) {
int depth = 0, flags = CWOverrideRedirect | CWBackingStore;
Visual *visual = nullptr;
depth = CopyFromParent;
visual = CopyFromParent;
#ifdef BUILD_ARGB
if (use_argb_visual.get(l) && (get_argb_visual(&visual, &depth) != 0)) {
have_argb_visual = true;
window.visual = visual;
window.colourmap = XCreateColormap(display, DefaultRootWindow(display),
window.visual, AllocNone);
}
#endif /* BUILD_ARGB */
int b = border_inner_margin.get(l) + border_width.get(l) +
border_outer_margin.get(l);
/* Sanity check to avoid making an invalid 0x0 window */
if (b == 0) { b = 1; }
XClassHint classHint;
// class_name must be a named local variable, so that c_str() remains
// valid until we call XmbSetWMProperties() or XSetClassHint. We use
// const_cast because, for whatever reason, res_name is not declared as
// const char *. XmbSetWMProperties hopefully doesn't modify the value
// (hell, even their own example app assigns a literal string constant to
// the field)
const std::string &class_name = own_window_class.get(l);
classHint.res_name = const_cast<char *>(class_name.c_str());
classHint.res_class = classHint.res_name;
if (own_window_type.get(l) == window_type::OVERRIDE) {
/* An override_redirect True window.
* No WM hints or button processing needed. */
XSetWindowAttributes attrs = {ParentRelative,
0L,
0,
0L,
0,
0,
Always,
0L,
0L,
False,
StructureNotifyMask | ExposureMask,
0L,
True,
0,
0};
flags |= CWBackPixel;
if (have_argb_visual) {
attrs.colormap = window.colourmap;
flags &= ~CWBackPixel;
flags |= CWBorderPixel | CWColormap;
}
/* Parent is desktop window (which might be a child of root) */
window.window = XCreateWindow(
display, window.desktop, window.geometry.x(), window.geometry.y(), b,
b, 0, depth, InputOutput, visual, flags, &attrs);
XLowerWindow(display, window.window);
XSetClassHint(display, window.window, &classHint);
NORM_ERR("window type - override");
} else { /* own_window_type.get(l) != TYPE_OVERRIDE */
/* A window managed by the window manager.
* Process hints and buttons. */
XSetWindowAttributes attrs = {
ParentRelative,
0L,
0,
0L,
0,
0,
Always,
0L,
0L,
False,
StructureNotifyMask | ExposureMask | ButtonPressMask |
ButtonReleaseMask,
0L,
own_window_type.get(l) == window_type::UTILITY ? True : False,
0,
0};
XWMHints wmHint;
Atom xa;
flags |= CWBackPixel;
if (have_argb_visual) {
attrs.colormap = window.colourmap;
flags &= ~CWBackPixel;
flags |= CWBorderPixel | CWColormap;
}
if (own_window_type.get(l) == window_type::DOCK) {
window.geometry.set_pos(conky::vec2i::Zero());
}
/* Parent is root window so WM can take control */
window.window = XCreateWindow(display, window.root, window.geometry.x(),
window.geometry.y(), b, b, 0, depth,
InputOutput, visual, flags, &attrs);
uint16_t hints = own_window_hints.get(l);
wmHint.flags = InputHint | StateHint;
/* allow decorated windows to be given input focus by WM */
wmHint.input = TEST_HINT(hints, window_hints::UNDECORATED) ? False : True;
#ifdef BUILD_XSHAPE
#ifdef BUILD_XFIXES
if (own_window_type.get(l) == window_type::UTILITY) {
XRectangle rect;
XserverRegion region = XFixesCreateRegion(display, &rect, 1);
XFixesSetWindowShapeRegion(display, window.window, ShapeInput, 0, 0,
region);
XFixesDestroyRegion(display, region);
}
#endif /* BUILD_XFIXES */
if (!wmHint.input) {
/* allow only decorated windows to be given mouse input */
int major_version;
int minor_version;
if (XShapeQueryVersion(display, &major_version, &minor_version) == 0) {
NORM_ERR("Input shapes are not supported");
} else {
if (own_window.get(*state) &&
(own_window_type.get(*state) != window_type::NORMAL ||
((TEST_HINT(own_window_hints.get(*state),
window_hints::UNDECORATED)) != 0))) {
XShapeCombineRectangles(display, window.window, ShapeInput, 0, 0,
nullptr, 0, ShapeSet, Unsorted);
}
}
}
#endif /* BUILD_XSHAPE */
if (own_window_type.get(l) == window_type::DOCK ||
own_window_type.get(l) == window_type::PANEL) {
// Docks and panels MUST have WithdrawnState initially
// See: https://github.com/brndnmtthws/conky/issues/2046
wmHint.initial_state = WithdrawnState;
} else {
wmHint.initial_state = NormalState;
}
XmbSetWMProperties(display, window.window, nullptr, nullptr, argv_copy,
argc_copy, nullptr, &wmHint, &classHint);
XStoreName(display, window.window, own_window_title.get(l).c_str());
/* Sets an empty WM_PROTOCOLS property */
XSetWMProtocols(display, window.window, nullptr, 0);
/* Set window type */
if ((xa = ATOM(_NET_WM_WINDOW_TYPE)) != None) {
Atom prop;
switch (own_window_type.get(l)) {
case window_type::DESKTOP:
prop = ATOM(_NET_WM_WINDOW_TYPE_DESKTOP);
NORM_ERR("window type - desktop");
break;
case window_type::DOCK:
prop = ATOM(_NET_WM_WINDOW_TYPE_DOCK);
NORM_ERR("window type - dock");
break;
case window_type::PANEL:
prop = ATOM(_NET_WM_WINDOW_TYPE_DOCK);
NORM_ERR("window type - panel");
break;
case window_type::UTILITY:
prop = ATOM(_NET_WM_WINDOW_TYPE_UTILITY);
NORM_ERR("window type - utility");
break;
case window_type::NORMAL:
default:
prop = ATOM(_NET_WM_WINDOW_TYPE_NORMAL);
NORM_ERR("window type - normal");
break;
}
XChangeProperty(display, window.window, xa, XA_ATOM, 32,
PropModeReplace,
reinterpret_cast<unsigned char *>(&prop), 1);
}
/* Set desired hints */
/* Window decorations */
if (TEST_HINT(hints, window_hints::UNDECORATED)) {
DBGP("hint - undecorated");
xa = ATOM(_MOTIF_WM_HINTS);
if (xa != None) {
long prop[5] = {2, 0, 0, 0, 0};
XChangeProperty(display, window.window, xa, xa, 32, PropModeReplace,
reinterpret_cast<unsigned char *>(prop), 5);
}
}
/* Below other windows */
if (TEST_HINT(hints, window_hints::BELOW)) {
DBGP("hint - below");
xa = ATOM(_WIN_LAYER);
if (xa != None) {
long prop = 0;
XChangeProperty(display, window.window, xa, XA_CARDINAL, 32,
PropModeAppend,
reinterpret_cast<unsigned char *>(&prop), 1);
}
xa = ATOM(_NET_WM_STATE);
if (xa != None) {
Atom xa_prop = ATOM(_NET_WM_STATE_BELOW);
XChangeProperty(display, window.window, xa, XA_ATOM, 32,
PropModeAppend,
reinterpret_cast<unsigned char *>(&xa_prop), 1);
}
}
/* Above other windows */
if (TEST_HINT(hints, window_hints::ABOVE)) {
DBGP("hint - above");
xa = ATOM(_WIN_LAYER);
if (xa != None) {
long prop = 6;
XChangeProperty(display, window.window, xa, XA_CARDINAL, 32,
PropModeAppend,
reinterpret_cast<unsigned char *>(&prop), 1);
}
xa = ATOM(_NET_WM_STATE);
if (xa != None) {
Atom xa_prop = ATOM(_NET_WM_STATE_ABOVE);
XChangeProperty(display, window.window, xa, XA_ATOM, 32,
PropModeAppend,
reinterpret_cast<unsigned char *>(&xa_prop), 1);
}
}
/* Sticky */
if (TEST_HINT(hints, window_hints::STICKY)) {
DBGP("hint - sticky");
xa = ATOM(_NET_WM_DESKTOP);
if (xa != None) {
CARD32 xa_prop = 0xFFFFFFFF;
XChangeProperty(display, window.window, xa, XA_CARDINAL, 32,
PropModeAppend,
reinterpret_cast<unsigned char *>(&xa_prop), 1);
}
xa = ATOM(_NET_WM_STATE);
if (xa != None) {
Atom xa_prop = ATOM(_NET_WM_STATE_STICKY);
XChangeProperty(display, window.window, xa, XA_ATOM, 32,
PropModeAppend,
reinterpret_cast<unsigned char *>(&xa_prop), 1);
}
}
/* Skip taskbar */
if (TEST_HINT(hints, window_hints::SKIP_TASKBAR)) {
DBGP("hint - skip taskbar");
xa = ATOM(_NET_WM_STATE);
if (xa != None) {
Atom xa_prop = ATOM(_NET_WM_STATE_SKIP_TASKBAR);
XChangeProperty(display, window.window, xa, XA_ATOM, 32,
PropModeAppend,
reinterpret_cast<unsigned char *>(&xa_prop), 1);
}
}
/* Skip pager */
if (TEST_HINT(hints, window_hints::SKIP_PAGER)) {
DBGP("hint - skip pager");
xa = ATOM(_NET_WM_STATE);
if (xa != None) {
Atom xa_prop = ATOM(_NET_WM_STATE_SKIP_PAGER);
XChangeProperty(display, window.window, xa, XA_ATOM, 32,
PropModeAppend,
reinterpret_cast<unsigned char *>(&xa_prop), 1);
}
}
}
NORM_ERR("drawing to created window (0x%lx)", window.window);
XMapWindow(display, window.window);
} else
#endif /* OWN_WINDOW */
{
XWindowAttributes attrs;
if (window.window == None) { window.window = window.desktop; }
if (XGetWindowAttributes(display, window.window, &attrs) != 0) {
window.geometry.set_size(attrs.width, attrs.height);
}
NORM_ERR("drawing to desktop window");
}
/* Drawable is same as window. This may be changed by double buffering. */
window.drawable = window.window;
XFlush(display);
int64_t input_mask = ExposureMask | PropertyChangeMask;
#ifdef OWN_WINDOW
if (own_window.get(l)) {
input_mask |= StructureNotifyMask;
#if !defined(BUILD_XINPUT)
input_mask |= ButtonPressMask | ButtonReleaseMask;
#endif
}
#if defined(BUILD_MOUSE_EVENTS) || defined(BUILD_XINPUT)
bool xinput_ok = false;
#ifdef BUILD_XINPUT
// not a loop; substitutes goto with break - if checks fail
do {
int _ignored; // segfault if NULL
if (!XQueryExtension(display, "XInputExtension", &window.xi_opcode,
&_ignored, &_ignored)) {
// events will still ~work but let the user know why they're buggy
NORM_ERR("XInput extension is not supported by X11!");
break;
}
int major = 2, minor = 0;
int retval = XIQueryVersion(display, &major, &minor);
if (retval != 0) {
NORM_ERR("Error: XInput 2.0 is not supported!");
break;
}
const std::size_t mask_size = (XI_LASTEVENT + 7) / 8;
unsigned char mask_bytes[mask_size] = {0}; /* must be zeroed! */
XISetMask(mask_bytes, XI_HierarchyChanged);
#ifdef BUILD_MOUSE_EVENTS
XISetMask(mask_bytes, XI_Motion);
#endif /* BUILD_MOUSE_EVENTS */
// Capture click events for "override" window type
if (!own) {
XISetMask(mask_bytes, XI_ButtonPress);
XISetMask(mask_bytes, XI_ButtonRelease);
}
XIEventMask ev_masks[1];
ev_masks[0].deviceid = XIAllDevices;
ev_masks[0].mask_len = sizeof(mask_bytes);
ev_masks[0].mask = mask_bytes;
XISelectEvents(display, window.root, ev_masks, 1);
if (own) {
#ifdef BUILD_MOUSE_EVENTS
XIClearMask(mask_bytes, XI_Motion);
#endif /* BUILD_MOUSE_EVENTS */
XISetMask(mask_bytes, XI_ButtonPress);
XISetMask(mask_bytes, XI_ButtonRelease);
ev_masks[0].deviceid = XIAllDevices;
ev_masks[0].mask_len = sizeof(mask_bytes);
ev_masks[0].mask = mask_bytes;
XISelectEvents(display, window.window, ev_masks, 1);
}
// setup cache
int num_devices;
XDeviceInfo *info = XListInputDevices(display, &num_devices);
for (int i = 0; i < num_devices; i++) {
if (info[i].use == IsXPointer || info[i].use == IsXExtensionPointer) {
conky::device_info::from_xi_id(info[i].id, display);
}
}
XFreeDeviceList(info);
xinput_ok = true;
} while (false);
#endif /* BUILD_XINPUT */
// Fallback to basic X11 enter/leave events if xinput fails to init.
// It's not recommended to add event masks to special windows in X; causes a
// crash (thus own_window_type != TYPE_DESKTOP)
#ifdef BUILD_MOUSE_EVENTS
if (!xinput_ok && own && own_window_type.get(l) != window_type::DESKTOP) {
input_mask |= PointerMotionMask | EnterWindowMask | LeaveWindowMask;
}
#endif /* BUILD_MOUSE_EVENTS */
#endif /* BUILD_MOUSE_EVENTS || BUILD_XINPUT */
#endif /* OWN_WINDOW */
window.event_mask = input_mask;
XSelectInput(display, window.window, input_mask);
window_created = 1;
DBGP("leave x11_init_window()");
}
static Window find_desktop_window_impl(Window win, int w, int h) {
unsigned int i, j;
Window troot, parent, *children;
unsigned int n;
/* search subwindows with same size as display or work area */
for (i = 0; i < 10; i++) {
XQueryTree(display, win, &troot, &parent, &children, &n);
for (j = 0; j < n; j++) {
XWindowAttributes attrs;
if (XGetWindowAttributes(display, children[j], &attrs) != 0) {
/* Window must be mapped and same size as display or
* work space */
if (attrs.map_state == IsViewable && attrs.override_redirect == false &&
((attrs.width == w && attrs.height == h))) {
win = children[j];
break;
}
}
}
XFree(children);
if (j == n) { break; }
}
return win;
}
void create_gc() {
XGCValues values;
values.graphics_exposures = 0;
values.function = GXcopy;
window.gc = XCreateGC(display, window.drawable,
GCFunction | GCGraphicsExposures, &values);
}
// Get current desktop number
static inline void get_x11_desktop_current(Display *current_display,
Window root, Atom atom) {
Atom actual_type;
int actual_format;
unsigned long nitems;
unsigned long bytes_after;
unsigned char *prop = nullptr;
struct information *current_info = &info;
if (atom == None) { return; }
if ((XGetWindowProperty(current_display, root, atom, 0, 1L, False,
XA_CARDINAL, &actual_type, &actual_format, &nitems,
&bytes_after, &prop) == 0) &&
(actual_type == XA_CARDINAL) && (nitems == 1L) && (actual_format == 32)) {
current_info->x11.desktop.current = prop[0] + 1;
}
if (prop != nullptr) { XFree(prop); }
}
// Get total number of available desktops
static inline void get_x11_desktop_number(Display *current_display, Window root,
Atom atom) {
Atom actual_type;
int actual_format;
unsigned long nitems;
unsigned long bytes_after;
unsigned char *prop = nullptr;
struct information *current_info = &info;
if (atom == None) { return; }
if ((XGetWindowProperty(current_display, root, atom, 0, 1L, False,
XA_CARDINAL, &actual_type, &actual_format, &nitems,
&bytes_after, &prop) == 0) &&
(actual_type == XA_CARDINAL) && (nitems == 1L) && (actual_format == 32)) {
current_info->x11.desktop.number = prop[0];
}
if (prop != nullptr) { XFree(prop); }
}
// Get all desktop names
static inline void get_x11_desktop_names(Display *current_display, Window root,
Atom atom) {
Atom actual_type;
int actual_format;
unsigned long nitems;
unsigned long bytes_after;
unsigned char *prop = nullptr;
struct information *current_info = &info;
if (atom == None) { return; }
if ((XGetWindowProperty(current_display, root, atom, 0, (~0L), False,
ATOM(UTF8_STRING), &actual_type, &actual_format,
&nitems, &bytes_after, &prop) == 0) &&