-
-
Notifications
You must be signed in to change notification settings - Fork 609
/
display-x11.cc
1000 lines (876 loc) · 29.2 KB
/
display-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
*
* Please see COPYING for details
*
* Copyright (C) 2018-2021 François Revol et al.
* 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 <X11/extensions/XI2.h>
#include <config.h>
#ifdef BUILD_X11
#include <X11/X.h>
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wvariadic-macros"
#include <X11/Xutil.h>
#ifdef BUILD_XFT
#include <X11/Xlib.h>
#endif /* BUILD_XFT */
#pragma GCC diagnostic pop
#ifdef BUILD_XDAMAGE
#include <X11/extensions/Xdamage.h>
#endif /* BUILD_XDAMAGE */
#include "fonts.h"
#ifdef BUILD_IMLIB2
#include "imlib2.h"
#endif /* BUILD_IMLIB2 */
#ifdef BUILD_MOUSE_EVENTS
#include "mouse-events.h"
#ifdef BUILD_XINPUT
#include <X11/extensions/XInput2.h>
#endif /* BUILD_XINPUT */
#endif /* BUILD_MOUSE_EVENTS */
#endif /* BUILD_X11 */
#include <cstdint>
#include <iostream>
#include <sstream>
#include <unordered_map>
#include <vector>
#include "colours.h"
#include "conky.h"
#include "display-x11.hh"
#include "gui.h"
#include "llua.h"
/* TODO: cleanup global namespace */
#ifdef BUILD_X11
#include "logging.h"
#include "x11.h"
// TODO: cleanup externs (move to conky.h ?)
#ifdef OWN_WINDOW
extern int fixed_size, fixed_pos;
#endif /* OWN_WINDOW */
extern int text_start_x, text_start_y; /* text start position in window */
extern int text_offset_x, text_offset_y; /* offset for start position */
extern int text_width,
text_height; /* initially 1 so no zero-sized window is created */
extern double current_update_time, next_update_time, last_update_time;
void update_text();
extern int need_to_update;
int get_border_total();
extern conky::range_config_setting<int> maximum_width;
extern Colour current_color;
#ifdef BUILD_XFT
static int xft_dpi = -1;
#endif /* BUILD_XFT */
/* for x_fonts */
struct x_font_list {
XFontStruct *font;
XFontSet fontset;
#ifdef BUILD_XFT
XftFont *xftfont;
int font_alpha;
#endif
x_font_list()
: font(nullptr),
fontset(nullptr)
#ifdef BUILD_XFT
,
xftfont(nullptr),
font_alpha(0xffff)
#endif
{
}
};
static std::vector<x_font_list> x_fonts; /* indexed by selected_font */
#ifdef BUILD_XFT
namespace {
class xftalpha_setting : public conky::simple_config_setting<float> {
using Base = conky::simple_config_setting<float>;
protected:
void lua_setter(lua::state &l, bool init) override {
lua::stack_sentry s(l, -2);
Base::lua_setter(l, init);
if (init && out_to_x.get(*state)) {
x_fonts.resize(std::max(1, static_cast<int>(fonts.size())));
x_fonts[0].font_alpha = do_convert(l, -1).first * 0xffff;
}
++s;
}
public:
xftalpha_setting() : Base("xftalpha", 1.0, false) {}
};
xftalpha_setting xftalpha;
} // namespace
#endif /* BUILD_XFT */
static void X11_create_window();
struct _x11_stuff_s {
Region region;
#ifdef BUILD_XDAMAGE
Damage damage;
XserverRegion region2, part;
int event_base, error_base;
#endif
} x11_stuff;
static void X11_create_window() {
if (!window.window) { return; }
setup_fonts();
load_fonts(utf8_mode.get(*state));
#ifdef BUILD_XFT
if (use_xft.get(*state)) {
auto dpi = XGetDefault(display, "Xft", "dpi");
if (dpi) { xft_dpi = strtol(dpi, nullptr, 10); }
}
#endif /* BUILD_XFT */
update_text_area(); /* to position text/window on screen */
#ifdef OWN_WINDOW
if (own_window.get(*state)) {
if (fixed_pos == 0) {
XMoveWindow(display, window.window, window.x, window.y);
}
set_transparent_background(window.window);
}
#endif
create_gc();
draw_stuff();
x11_stuff.region = XCreateRegion();
#ifdef BUILD_XDAMAGE
if (XDamageQueryExtension(display, &x11_stuff.event_base,
&x11_stuff.error_base) == 0) {
NORM_ERR("Xdamage extension unavailable");
x11_stuff.damage = 0;
} else {
x11_stuff.damage =
XDamageCreate(display, window.window, XDamageReportNonEmpty);
x11_stuff.region2 = XFixesCreateRegionFromWindow(display, window.window, 0);
x11_stuff.part = XFixesCreateRegionFromWindow(display, window.window, 0);
}
#endif /* BUILD_XDAMAGE */
selected_font = 0;
update_text_area(); /* to get initial size of the window */
}
#endif /* BUILD_X11 */
namespace conky {
namespace {
#ifdef BUILD_X11
conky::display_output_x11 x11_output;
#else
conky::disabled_display_output x11_output_disabled("x11", "BUILD_X11");
#endif
} // namespace
extern void init_x11_output() {}
namespace priv {} // namespace priv
#ifdef BUILD_X11
display_output_x11::display_output_x11() : display_output_base("x11") {
is_graphical = true;
priority = 2;
}
bool display_output_x11::detect() {
if (out_to_x.get(*state)) {
DBGP2("Display output '%s' enabled in config.", name.c_str());
return true;
}
return false;
}
bool display_output_x11::initialize() {
X11_create_window();
return true;
}
bool display_output_x11::shutdown() {
deinit_x11();
return true;
}
bool display_output_x11::main_loop_wait(double t) {
/* wait for X event or timeout */
if (!display || !window.gc) return true;
if (XPending(display) == 0) {
fd_set fdsr;
struct timeval tv {};
int s;
// t = next_update_time - get_time();
t = std::min(std::max(t, 0.0), active_update_interval());
tv.tv_sec = static_cast<long>(t);
tv.tv_usec = static_cast<long>(t * 1000000) % 1000000;
FD_ZERO(&fdsr);
FD_SET(ConnectionNumber(display), &fdsr);
s = select(ConnectionNumber(display) + 1, &fdsr, nullptr, nullptr, &tv);
if (s == -1) {
if (errno != EINTR) { NORM_ERR("can't select(): %s", strerror(errno)); }
} else {
/* timeout */
if (s == 0) { update_text(); }
}
}
if (need_to_update != 0) {
#ifdef OWN_WINDOW
int wx = window.x, wy = window.y;
#endif
need_to_update = 0;
selected_font = 0;
update_text_area();
#ifdef OWN_WINDOW
if (own_window.get(*state)) {
int changed = 0;
int border_total = get_border_total();
/* resize window if it isn't right size */
if ((fixed_size == 0) &&
(text_width + 2 * border_total != window.width ||
text_height + 2 * border_total != window.height)) {
window.width = text_width + 2 * border_total;
window.height = text_height + 2 * border_total;
draw_stuff(); /* redraw everything in our newly sized window */
XResizeWindow(display, window.window, window.width,
window.height); /* resize window */
set_transparent_background(window.window);
#ifdef BUILD_XDBE
/* swap buffers */
xdbe_swap_buffers();
#else
if (use_xpmdb.get(*state)) {
XFreePixmap(display, window.back_buffer);
window.back_buffer =
XCreatePixmap(display, window.window, window.width, window.height,
DefaultDepth(display, screen));
if (window.back_buffer != None) {
window.drawable = window.back_buffer;
} else {
// this is probably reallllly bad
NORM_ERR("Failed to allocate back buffer");
}
XSetForeground(display, window.gc, 0);
XFillRectangle(display, window.drawable, window.gc, 0, 0,
window.width, window.height);
}
#endif
changed++;
/* update lua window globals */
llua_update_window_table(text_start_x, text_start_y, text_width,
text_height);
}
/* move window if it isn't in right position */
if ((fixed_pos == 0) && (window.x != wx || window.y != wy)) {
XMoveWindow(display, window.window, window.x, window.y);
changed++;
}
/* update struts */
if ((changed != 0) && own_window_type.get(*state) == TYPE_PANEL) {
int sidenum = -1;
DBGP("%s", _(PACKAGE_NAME ": defining struts\n"));
fflush(stderr);
switch (text_alignment.get(*state)) {
case TOP_LEFT:
case TOP_RIGHT:
case TOP_MIDDLE: {
sidenum = 2;
break;
}
case BOTTOM_LEFT:
case BOTTOM_RIGHT:
case BOTTOM_MIDDLE: {
sidenum = 3;
break;
}
case MIDDLE_LEFT: {
sidenum = 0;
break;
}
case MIDDLE_RIGHT: {
sidenum = 1;
break;
}
case NONE:
case MIDDLE_MIDDLE: /* XXX What about these? */;
}
set_struts(sidenum);
}
}
#endif
clear_text(1);
#if defined(BUILD_XDBE)
if (use_xdbe.get(*state)) {
#else
if (use_xpmdb.get(*state)) {
#endif
XRectangle r;
int border_total = get_border_total();
r.x = text_start_x - border_total;
r.y = text_start_y - border_total;
r.width = text_width + 2 * border_total;
r.height = text_height + 2 * border_total;
XUnionRectWithRegion(&r, x11_stuff.region, x11_stuff.region);
}
}
/* handle X events */
while (XPending(display) != 0) {
XEvent ev;
/* indicates whether processed event was consumed */
bool consumed = false;
XNextEvent(display, &ev);
#if defined(OWN_WINDOW) && defined(BUILD_MOUSE_EVENTS) && defined(BUILD_XINPUT)
// no need to check whether these events have been consumed because
// they're global and shouldn't be propagated
if (ev.type == GenericEvent && ev.xcookie.extension == window.xi_opcode) {
if (!XGetEventData(display, &ev.xcookie)) {
NORM_ERR("unable to get XInput event data");
continue;
}
auto *data = reinterpret_cast<XIDeviceEvent *>(ev.xcookie.data);
// the only way to differentiate between a scroll and move event is
// though valuators - move has first 2 set, other axis movements have
// other.
bool is_cursor_move =
data->valuators.mask_len >= 1 &&
(data->valuators.mask[0] & 3) == data->valuators.mask[0];
for (std::size_t i = 1; i < data->valuators.mask_len; i++) {
if (data->valuators.mask[i] != 0) {
is_cursor_move = false;
break;
}
}
if (data->evtype == XI_Motion && is_cursor_move) {
Window query_result =
query_x11_window_at_pos(display, data->root_x, data->root_y);
static bool cursor_inside = false;
if ((query_result != 0 && query_result == window.window) ||
((query_result == window.desktop || query_result == window.root ||
query_result == 0) &&
data->root_x >= window.x &&
data->root_x < (window.x + window.width) &&
data->root_y >= window.y &&
data->root_y < (window.y + window.height))) {
// cursor is inside conky
if (!cursor_inside) {
llua_mouse_hook(mouse_crossing_event(
mouse_event_t::AREA_ENTER, data->root_x - window.x,
data->root_y - window.x, data->root_x, data->root_y));
}
cursor_inside = true;
} else if (cursor_inside) {
llua_mouse_hook(mouse_crossing_event(
mouse_event_t::AREA_LEAVE, data->root_x - window.x,
data->root_y - window.x, data->root_x, data->root_y));
cursor_inside = false;
}
}
XFreeEventData(display, &ev.xcookie);
continue;
}
#endif /* BUILD_MOUSE_EVENTS && BUILD_XINPUT */
// Any of the remaining events apply to conky window
if (ev.xany.window != window.window) continue;
switch (ev.type) {
case Expose: {
XRectangle r;
r.x = ev.xexpose.x;
r.y = ev.xexpose.y;
r.width = ev.xexpose.width;
r.height = ev.xexpose.height;
XUnionRectWithRegion(&r, x11_stuff.region, x11_stuff.region);
XSync(display, False);
continue;
}
case PropertyNotify: {
if (ev.xproperty.state == PropertyNewValue) {
get_x11_desktop_info(ev.xproperty.display, ev.xproperty.atom);
}
#ifdef USE_ARGB
if (!have_argb_visual) {
#endif
if (ev.xproperty.atom == ATOM(_XROOTPMAP_ID) ||
ev.xproperty.atom == ATOM(_XROOTMAP_ID)) {
if (forced_redraw.get(*state)) {
draw_stuff();
next_update_time = get_time();
need_to_update = 1;
}
}
#ifdef USE_ARGB
}
#endif
continue;
}
#ifdef OWN_WINDOW
case ReparentNotify:
/* make background transparent */
if (own_window.get(*state)) {
set_transparent_background(window.window);
}
continue;
case ConfigureNotify:
if (own_window.get(*state)) {
/* if window size isn't what expected, set fixed size */
if (ev.xconfigure.width != window.width ||
ev.xconfigure.height != window.height) {
if (window.width != 0 && window.height != 0) { fixed_size = 1; }
/* clear old stuff before screwing up
* size and pos */
clear_text(1);
{
XWindowAttributes attrs;
if (XGetWindowAttributes(display, window.window, &attrs) != 0) {
window.width = attrs.width;
window.height = attrs.height;
}
}
int border_total = get_border_total();
text_width = window.width - 2 * border_total;
text_height = window.height - 2 * border_total;
int mw = this->dpi_scale(maximum_width.get(*state));
if (text_width > mw && mw > 0) { text_width = mw; }
}
/* if position isn't what expected, set fixed pos
* total_updates avoids setting fixed_pos when window
* is set to weird locations when started */
/* // this is broken
if (total_updates >= 2 && !fixed_pos
&& (window.x != ev.xconfigure.x
|| window.y != ev.xconfigure.y)
&& (ev.xconfigure.x != 0
|| ev.xconfigure.y != 0)) {
fixed_pos = 1;
} */
}
continue;
case ButtonPress:
#ifdef BUILD_MOUSE_EVENTS
{
modifier_state_t mods = x11_modifier_state(ev.xbutton.state);
if (4 <= ev.xbutton.button && ev.xbutton.button <= 7) {
scroll_direction_t direction =
x11_scroll_direction(ev.xbutton.button);
consumed = llua_mouse_hook(
mouse_scroll_event(ev.xbutton.x, ev.xbutton.y, ev.xbutton.x_root,
ev.xbutton.y_root, direction, mods));
} else {
mouse_button_t button = x11_mouse_button_code(ev.xbutton.button);
consumed = llua_mouse_hook(mouse_button_event(
mouse_event_t::MOUSE_PRESS, ev.xbutton.x, ev.xbutton.y,
ev.xbutton.x_root, ev.xbutton.y_root, button, mods));
}
}
#endif /* BUILD_MOUSE_EVENTS */
if (own_window.get(*state)) {
/* if an ordinary window with decorations */
if ((own_window_type.get(*state) == TYPE_NORMAL &&
!TEST_HINT(own_window_hints.get(*state), HINT_UNDECORATED)) ||
own_window_type.get(*state) == TYPE_DESKTOP) {
/* allow conky to hold input focus. */
break;
}
}
break;
case ButtonRelease:
#ifdef BUILD_MOUSE_EVENTS
/* don't report scroll release events */
if (4 > ev.xbutton.button || ev.xbutton.button > 7) {
modifier_state_t mods = x11_modifier_state(ev.xbutton.state);
mouse_button_t button = x11_mouse_button_code(ev.xbutton.button);
consumed = llua_mouse_hook(mouse_button_event(
mouse_event_t::MOUSE_RELEASE, ev.xbutton.x, ev.xbutton.y,
ev.xbutton.x_root, ev.xbutton.y_root, button, mods));
}
#endif /* BUILD_MOUSE_EVENTS */
if (own_window.get(*state)) {
/* if an ordinary window with decorations */
if ((own_window_type.get(*state) == TYPE_NORMAL) &&
!TEST_HINT(own_window_hints.get(*state), HINT_UNDECORATED)) {
/* allow conky to hold input focus. */
break;
}
}
break;
#ifdef BUILD_MOUSE_EVENTS
/*
windows below are notified for the following events as well;
can't forward the event without filtering XQueryTree output.
*/
case MotionNotify: {
modifier_state_t mods = x11_modifier_state(ev.xmotion.state);
consumed = llua_mouse_hook(mouse_move_event(ev.xmotion.x, ev.xmotion.y,
ev.xmotion.x_root,
ev.xmotion.y_root, mods));
} break;
case LeaveNotify:
case EnterNotify:
if (window.xi_opcode == 0) {
bool not_over_conky =
ev.xcrossing.x_root <= window.x ||
ev.xcrossing.y_root <= window.y ||
ev.xcrossing.x_root >= window.x + window.width ||
ev.xcrossing.y_root >= window.y + window.height;
if ((not_over_conky && ev.xcrossing.type == LeaveNotify) ||
(!not_over_conky && ev.xcrossing.type == EnterNotify)) {
llua_mouse_hook(mouse_crossing_event(
ev.xcrossing.type == EnterNotify ? mouse_event_t::AREA_ENTER
: mouse_event_t::AREA_LEAVE,
ev.xcrossing.x, ev.xcrossing.y, ev.xcrossing.x_root,
ev.xcrossing.y_root));
}
}
// can't propagate these events in a way that makes sense for desktop
continue;
#endif /* BUILD_MOUSE_EVENTS */
#endif /* OWN_WINDOW */
default:
#ifdef BUILD_XDAMAGE
if (ev.type == x11_stuff.event_base + XDamageNotify) {
auto *dev = reinterpret_cast<XDamageNotifyEvent *>(&ev);
XFixesSetRegion(display, x11_stuff.part, &dev->area, 1);
XFixesUnionRegion(display, x11_stuff.region2, x11_stuff.region2,
x11_stuff.part);
continue; // TODO: Propagate damage
}
#endif /* BUILD_XDAMAGE */
break;
}
if (!consumed) {
propagate_x11_event(ev);
} else {
InputEvent *i_ev = xev_as_input_event(ev);
if (i_ev != nullptr) {
XSetInputFocus(display, window.window, RevertToParent,
i_ev->common.time);
}
}
}
#ifdef BUILD_XDAMAGE
if (x11_stuff.damage) {
XDamageSubtract(display, x11_stuff.damage, x11_stuff.region2, None);
XFixesSetRegion(display, x11_stuff.region2, nullptr, 0);
}
#endif /* BUILD_XDAMAGE */
/* XDBE doesn't seem to provide a way to clear the back buffer
* without interfering with the front buffer, other than passing
* XdbeBackground to XdbeSwapBuffers. That means that if we're
* using XDBE, we need to redraw the text even if it wasn't part of
* the exposed area. OTOH, if we're not going to call draw_stuff at
* all, then no swap happens and we can safely do nothing. */
if (XEmptyRegion(x11_stuff.region) == 0) {
#if defined(BUILD_XDBE)
if (use_xdbe.get(*state)) {
#else
if (use_xpmdb.get(*state)) {
#endif
XRectangle r;
int border_total = get_border_total();
r.x = text_start_x - border_total;
r.y = text_start_y - border_total;
r.width = text_width + 2 * border_total;
r.height = text_height + 2 * border_total;
XUnionRectWithRegion(&r, x11_stuff.region, x11_stuff.region);
}
XSetRegion(display, window.gc, x11_stuff.region);
#ifdef BUILD_XFT
if (use_xft.get(*state)) {
XftDrawSetClip(window.xftdraw, x11_stuff.region);
}
#endif
draw_stuff();
XDestroyRegion(x11_stuff.region);
x11_stuff.region = XCreateRegion();
}
// handled
return true;
}
void display_output_x11::sigterm_cleanup() {
XDestroyRegion(x11_stuff.region);
x11_stuff.region = nullptr;
#ifdef BUILD_XDAMAGE
if (x11_stuff.damage) {
XDamageDestroy(display, x11_stuff.damage);
XFixesDestroyRegion(display, x11_stuff.region2);
XFixesDestroyRegion(display, x11_stuff.part);
}
#endif /* BUILD_XDAMAGE */
}
void display_output_x11::cleanup() {
if (window_created == 1) {
int border_total = get_border_total();
XClearArea(display, window.window, text_start_x - border_total,
text_start_y - border_total, text_width + 2 * border_total,
text_height + 2 * border_total, 0);
}
destroy_window();
free_fonts(utf8_mode.get(*state));
if (x11_stuff.region != nullptr) {
XDestroyRegion(x11_stuff.region);
x11_stuff.region = nullptr;
}
#ifdef BUILD_XFT
FcFini();
#endif /* BUILD_XFT */
}
void display_output_x11::set_foreground_color(Colour c) {
current_color = c;
#ifdef BUILD_ARGB
if (have_argb_visual) {
current_color.alpha = own_window_argb_value.get(*state);
}
#endif /* BUILD_ARGB */
XSetForeground(display, window.gc,
current_color.to_x11_color(display, screen));
}
int display_output_x11::calc_text_width(const char *s) {
std::size_t slen = strlen(s);
#ifdef BUILD_XFT
if (use_xft.get(*state)) {
XGlyphInfo gi;
if (utf8_mode.get(*state)) {
XftTextExtentsUtf8(display, x_fonts[selected_font].xftfont,
reinterpret_cast<const FcChar8 *>(s), slen, &gi);
} else {
XftTextExtents8(display, x_fonts[selected_font].xftfont,
reinterpret_cast<const FcChar8 *>(s), slen, &gi);
}
return gi.xOff;
}
#endif /* BUILD_XFT */
return XTextWidth(x_fonts[selected_font].font, s, slen);
}
void display_output_x11::draw_string_at(int x, int y, const char *s, int w) {
#ifdef BUILD_XFT
if (use_xft.get(*state)) {
XColor c{};
XftColor c2{};
c.pixel = current_color.to_x11_color(display, screen);
// query color on custom colormap
XQueryColor(display, window.colourmap, &c);
c2.pixel = c.pixel;
c2.color.red = c.red;
c2.color.green = c.green;
c2.color.blue = c.blue;
c2.color.alpha = x_fonts[selected_font].font_alpha;
if (utf8_mode.get(*state)) {
XftDrawStringUtf8(window.xftdraw, &c2, x_fonts[selected_font].xftfont, x,
y, reinterpret_cast<const XftChar8 *>(s), w);
} else {
XftDrawString8(window.xftdraw, &c2, x_fonts[selected_font].xftfont, x, y,
reinterpret_cast<const XftChar8 *>(s), w);
}
} else
#endif
{
if (utf8_mode.get(*state)) {
Xutf8DrawString(display, window.drawable, x_fonts[selected_font].fontset,
window.gc, x, y, s, w);
} else {
XDrawString(display, window.drawable, window.gc, x, y, s, w);
}
}
}
void display_output_x11::set_line_style(int w, bool solid) {
XSetLineAttributes(display, window.gc, w, solid ? LineSolid : LineOnOffDash,
CapButt, JoinMiter);
}
void display_output_x11::set_dashes(char *s) {
XSetDashes(display, window.gc, 0, s, 2);
}
void display_output_x11::draw_line(int x1, int y1, int x2, int y2) {
XDrawLine(display, window.drawable, window.gc, x1, y1, x2, y2);
}
void display_output_x11::draw_rect(int x, int y, int w, int h) {
XDrawRectangle(display, window.drawable, window.gc, x, y, w, h);
}
void display_output_x11::fill_rect(int x, int y, int w, int h) {
XFillRectangle(display, window.drawable, window.gc, x, y, w, h);
}
void display_output_x11::draw_arc(int x, int y, int w, int h, int a1, int a2) {
XDrawArc(display, window.drawable, window.gc, x, y, w, h, a1, a2);
}
void display_output_x11::move_win(int x, int y) {
window.x = x;
window.y = y;
XMoveWindow(display, window.window, x, y);
}
int display_output_x11::dpi_scale(int value) {
#if defined(BUILD_XFT)
if (use_xft.get(*state) && xft_dpi > 0) {
return (value * xft_dpi + (value > 0 ? 48 : -48)) / 96;
} else {
return value;
}
#else /* defined(BUILD_XFT) */
return value;
#endif /* defined(BUILD_XFT) */
}
void display_output_x11::end_draw_stuff() {
#if defined(BUILD_XDBE)
xdbe_swap_buffers();
#else
xpmdb_swap_buffers();
#endif
}
void display_output_x11::clear_text(int exposures) {
#ifdef BUILD_XDBE
if (use_xdbe.get(*state)) {
/* The swap action is XdbeBackground, which clears */
return;
}
#else
if (use_xpmdb.get(*state)) {
return;
} else
#endif
if ((display != nullptr) &&
(window.window != 0u)) { // make sure these are !null
/* there is some extra space for borders and outlines */
int border_total = get_border_total();
XClearArea(display, window.window, text_start_x - border_total,
text_start_y - border_total, text_width + 2 * border_total,
text_height + 2 * border_total, exposures != 0 ? True : 0);
}
}
#ifdef BUILD_XFT
int display_output_x11::font_height(unsigned int f) {
assert(f < x_fonts.size());
if (use_xft.get(*state)) {
return x_fonts[f].xftfont->ascent + x_fonts[f].xftfont->descent;
} else {
return x_fonts[f].font->max_bounds.ascent +
x_fonts[f].font->max_bounds.descent;
}
}
int display_output_x11::font_ascent(unsigned int f) {
assert(f < x_fonts.size());
if (use_xft.get(*state)) {
return x_fonts[f].xftfont->ascent;
} else {
return x_fonts[f].font->max_bounds.ascent;
}
}
int display_output_x11::font_descent(unsigned int f) {
assert(f < x_fonts.size());
if (use_xft.get(*state)) {
return x_fonts[f].xftfont->descent;
} else {
return x_fonts[f].font->max_bounds.descent;
}
}
#else
int display_output_x11::font_height(unsigned int f) {
assert(f < x_fonts.size());
return x_fonts[f].font->max_bounds.ascent +
x_fonts[f].font->max_bounds.descent;
}
int display_output_x11::font_ascent(unsigned int f) {
assert(f < x_fonts.size());
return x_fonts[f].font->max_bounds.ascent;
}
int display_output_x11::font_descent(unsigned int f) {
assert(f < x_fonts.size());
return x_fonts[f].font->max_bounds.descent;
}
#endif
void display_output_x11::setup_fonts(void) {
#ifdef BUILD_XFT
if (use_xft.get(*state)) {
if (window.xftdraw != nullptr) {
XftDrawDestroy(window.xftdraw);
window.xftdraw = nullptr;
}
window.xftdraw = XftDrawCreate(display, window.drawable, window.visual,
window.colourmap);
}
#endif /* BUILD_XFT */
}
void display_output_x11::set_font(unsigned int f) {
if (f >= x_fonts.size()) {
DBGP("%d >= x_fonts.size()", f);
return;
}
#ifdef BUILD_XFT
if (use_xft.get(*state)) { return; }
#endif /* BUILD_XFT */
if (x_fonts.size() > f && x_fonts[f].font != nullptr &&
window.gc != nullptr) {
XSetFont(display, window.gc, x_fonts[f].font->fid);
}
}
void display_output_x11::free_fonts(bool utf8) {
for (auto &font : x_fonts) {
#ifdef BUILD_XFT
if (use_xft.get(*state)) {
/* Close each font if it has been initialized */
if (font.xftfont) { XftFontClose(display, font.xftfont); }
} else
#endif /* BUILD_XFT */
{
if (font.font != nullptr) { XFreeFont(display, font.font); }
if (utf8 && (font.fontset != nullptr)) {
XFreeFontSet(display, font.fontset);
}
}
}
x_fonts.clear();
#ifdef BUILD_XFT
if (window.xftdraw != nullptr) {
XftDrawDestroy(window.xftdraw);
window.xftdraw = nullptr;
}
#endif /* BUILD_XFT */
}
void display_output_x11::load_fonts(bool utf8) {
x_fonts.resize(fonts.size());
for (unsigned int i = 0; i < fonts.size(); i++) {
auto &font = fonts[i];
auto &xfont = x_fonts[i];
#ifdef BUILD_XFT
/* load Xft font */
if (use_xft.get(*state)) {
if (xfont.xftfont == nullptr) {
xfont.xftfont = XftFontOpenName(display, screen, font.name.c_str());
}
if (xfont.xftfont != nullptr) { continue; }
NORM_ERR("can't load Xft font '%s'", font.name.c_str());
if ((xfont.xftfont = XftFontOpenName(display, screen, "courier-12")) !=
nullptr) {
continue;
}
CRIT_ERR("can't load Xft font '%s'", "courier-12");
continue;
}
#endif
if (utf8 && xfont.fontset == nullptr) {
char **missing;
int missingnum;
char *missingdrawn;
xfont.fontset = XCreateFontSet(display, font.name.c_str(), &missing,
&missingnum, &missingdrawn);
XFreeStringList(missing);
if (xfont.fontset == nullptr) {
NORM_ERR("can't load font '%s'", font.name.c_str());
xfont.fontset = XCreateFontSet(display, "fixed", &missing, &missingnum,
&missingdrawn);
if (xfont.fontset == nullptr) {
CRIT_ERR("can't load font '%s'", "fixed");
}
}
}
/* load normal font */
if ((xfont.font == nullptr) &&
(xfont.font = XLoadQueryFont(display, font.name.c_str())) == nullptr) {
NORM_ERR("can't load font '%s'", font.name.c_str());
if ((xfont.font = XLoadQueryFont(display, "fixed")) == nullptr) {
CRIT_ERR("can't load font '%s'", "fixed");
}
}
}
}
#endif /* BUILD_X11 */
} // namespace conky