This repository has been archived by the owner on Apr 18, 2023. It is now read-only.
/
xcb.c
2086 lines (1827 loc) · 79 KB
/
xcb.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
/*
* vim:ts=4:sw=4:expandtab
*
* i3bar - an xcb-based status- and ws-bar for i3
* © 2010 Axel Wagner and contributors (see also: LICENSE)
*
* xcb.c: Communicating with X
*
*/
#include "common.h"
#include <xcb/xcb.h>
#include <xcb/xkb.h>
#include <xcb/xproto.h>
#include <xcb/xcb_aux.h>
#include <xcb/xcb_cursor.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <i3/ipc.h>
#include <ev.h>
#include <errno.h>
#include <limits.h>
#include <err.h>
#include <X11/Xlib.h>
#include <X11/XKBlib.h>
#include <X11/extensions/XKB.h>
#ifdef I3_ASAN_ENABLED
#include <sanitizer/lsan_interface.h>
#endif
#include "libi3.h"
/** This is the equivalent of XC_left_ptr. I’m not sure why xcb doesn’t have a
* constant for that. */
#define XCB_CURSOR_LEFT_PTR 68
/* We save the atoms in an easy to access array, indexed by an enum */
enum {
#define ATOM_DO(name) name,
#include "xcb_atoms.def"
NUM_ATOMS
};
xcb_intern_atom_cookie_t atom_cookies[NUM_ATOMS];
xcb_atom_t atoms[NUM_ATOMS];
/* Variables, that are the same for all functions at all times */
xcb_connection_t *xcb_connection;
int screen;
xcb_screen_t *root_screen;
xcb_window_t xcb_root;
static xcb_cursor_t cursor;
/* selection window for tray support */
static xcb_window_t selwin = XCB_NONE;
static xcb_intern_atom_reply_t *tray_reply = NULL;
/* This is needed for integration with libi3 */
xcb_connection_t *conn;
/* The font we'll use */
static i3Font font;
/* Icon size (based on font size) */
int icon_size;
xcb_visualtype_t *visual_type;
uint8_t depth;
xcb_colormap_t colormap;
/* Overall height of the bar (based on font size) */
int bar_height;
/* These are only relevant for XKB, which we only need for grabbing modifiers */
int xkb_base;
bool mod_pressed = 0;
/* Event watchers, to interact with the user */
ev_prepare *xcb_prep;
ev_io *xcb_io;
ev_io *xkb_io;
/* The name of current binding mode */
static mode binding;
/* Indicates whether a new binding mode was recently activated */
bool activated_mode = false;
/* The output in which the tray should be displayed. */
static i3_output *output_for_tray;
/* The parsed colors */
struct xcb_colors_t {
color_t bar_fg;
color_t bar_bg;
color_t sep_fg;
color_t focus_bar_fg;
color_t focus_bar_bg;
color_t focus_sep_fg;
color_t active_ws_fg;
color_t active_ws_bg;
color_t active_ws_border;
color_t inactive_ws_fg;
color_t inactive_ws_bg;
color_t inactive_ws_border;
color_t urgent_ws_bg;
color_t urgent_ws_fg;
color_t urgent_ws_border;
color_t focus_ws_bg;
color_t focus_ws_fg;
color_t focus_ws_border;
color_t binding_mode_bg;
color_t binding_mode_fg;
color_t binding_mode_border;
};
struct xcb_colors_t colors;
/* Horizontal offset between a workspace label and button borders */
static const int ws_hoff_px = 4;
/* Vertical offset between a workspace label and button borders */
static const int ws_voff_px = 3;
/* Offset between two workspace buttons */
static const int ws_spacing_px = 1;
/* Offset between the statusline and 1) workspace buttons on the left
* 2) the tray or screen edge on the right */
static const int sb_hoff_px = 4;
/* Additional offset between the tray and the statusline, if the tray is not empty */
static const int tray_loff_px = 2;
/* Vertical offset between the bar and a separator */
static const int sep_voff_px = 4;
int _xcb_request_failed(xcb_void_cookie_t cookie, char *err_msg, int line) {
xcb_generic_error_t *err;
if ((err = xcb_request_check(xcb_connection, cookie)) != NULL) {
fprintf(stderr, "[%s:%d] ERROR: %s. X Error Code: %d\n", __FILE__, line, err_msg, err->error_code);
return err->error_code;
}
return 0;
}
static uint32_t get_sep_offset(struct status_block *block) {
if (!block->no_separator && block->sep_block_width > 0)
return block->sep_block_width / 2 + block->sep_block_width % 2;
return 0;
}
static int get_tray_width(struct tc_head *trayclients) {
trayclient *trayclient;
int tray_width = 0;
TAILQ_FOREACH_REVERSE(trayclient, trayclients, tc_head, tailq) {
if (!trayclient->mapped)
continue;
tray_width += icon_size + logical_px(config.tray_padding);
}
if (tray_width > 0)
tray_width += logical_px(tray_loff_px);
return tray_width;
}
/*
* Draws a separator for the given block if necessary.
*
*/
static void draw_separator(i3_output *output, uint32_t x, struct status_block *block, bool use_focus_colors) {
color_t sep_fg = (use_focus_colors ? colors.focus_sep_fg : colors.sep_fg);
color_t bar_bg = (use_focus_colors ? colors.focus_bar_bg : colors.bar_bg);
uint32_t sep_offset = get_sep_offset(block);
if (TAILQ_NEXT(block, blocks) == NULL || sep_offset == 0)
return;
uint32_t center_x = x - sep_offset;
if (config.separator_symbol == NULL) {
/* Draw a classic one pixel, vertical separator. */
draw_util_rectangle(&output->statusline_buffer, sep_fg,
center_x,
logical_px(sep_voff_px),
logical_px(1),
bar_height - 2 * logical_px(sep_voff_px));
} else {
/* Draw a custom separator. */
uint32_t separator_x = MAX(x - block->sep_block_width, center_x - separator_symbol_width / 2);
draw_util_text(config.separator_symbol, &output->statusline_buffer, sep_fg, bar_bg,
separator_x, logical_px(ws_voff_px), x - separator_x);
}
}
static uint32_t predict_statusline_length(bool use_short_text) {
uint32_t width = 0;
struct status_block *block;
TAILQ_FOREACH(block, &statusline_head, blocks) {
i3String *text = block->full_text;
struct status_block_render_desc *render = &block->full_render;
if (use_short_text && block->short_text != NULL) {
text = block->short_text;
render = &block->short_render;
}
if (i3string_get_num_bytes(text) == 0)
continue;
render->width = predict_text_width(text);
if (block->border)
render->width += logical_px(block->border_left + block->border_right);
/* Compute offset and append for text aligment in min_width. */
if (block->min_width <= render->width) {
render->x_offset = 0;
render->x_append = 0;
} else {
uint32_t padding_width = block->min_width - render->width;
switch (block->align) {
case ALIGN_LEFT:
render->x_append = padding_width;
break;
case ALIGN_RIGHT:
render->x_offset = padding_width;
break;
case ALIGN_CENTER:
render->x_offset = padding_width / 2;
render->x_append = padding_width / 2 + padding_width % 2;
break;
}
}
width += render->width + render->x_offset + render->x_append;
/* If this is not the last block, add some pixels for a separator. */
if (TAILQ_NEXT(block, blocks) != NULL)
width += block->sep_block_width;
}
return width;
}
/*
* Redraws the statusline to the output's statusline_buffer
*/
static void draw_statusline(i3_output *output, uint32_t clip_left, bool use_focus_colors, bool use_short_text) {
struct status_block *block;
color_t bar_color = (use_focus_colors ? colors.focus_bar_bg : colors.bar_bg);
draw_util_clear_surface(&output->statusline_buffer, bar_color);
/* Use unsigned integer wraparound to clip off the left side.
* For example, if clip_left is 75, then x will start at the very large
* number INT_MAX-75, which is way outside the surface dimensions. Drawing
* to that x position is a no-op which XCB and Cairo safely ignore. Once x moves
* up by 75 and goes past INT_MAX, it will wrap around again to 0, and we start
* actually rendering content to the surface. */
uint32_t x = 0 - clip_left;
/* Draw the text of each block */
TAILQ_FOREACH(block, &statusline_head, blocks) {
i3String *text = block->full_text;
struct status_block_render_desc *render = &block->full_render;
if (use_short_text && block->short_text != NULL) {
text = block->short_text;
render = &block->short_render;
}
if (i3string_get_num_bytes(text) == 0)
continue;
color_t fg_color;
if (block->urgent) {
fg_color = colors.urgent_ws_fg;
} else if (block->color) {
fg_color = draw_util_hex_to_color(block->color);
} else if (use_focus_colors) {
fg_color = colors.focus_bar_fg;
} else {
fg_color = colors.bar_fg;
}
color_t bg_color = bar_color;
int full_render_width = render->width + render->x_offset + render->x_append;
int has_border = block->border ? 1 : 0;
if (block->border || block->background || block->urgent) {
/* Let's determine the colors first. */
color_t border_color = bar_color;
if (block->urgent) {
border_color = colors.urgent_ws_border;
bg_color = colors.urgent_ws_bg;
} else {
if (block->border)
border_color = draw_util_hex_to_color(block->border);
if (block->background)
bg_color = draw_util_hex_to_color(block->background);
}
/* Draw the border. */
draw_util_rectangle(&output->statusline_buffer, border_color,
x, logical_px(1),
full_render_width,
bar_height - logical_px(2));
/* Draw the background. */
draw_util_rectangle(&output->statusline_buffer, bg_color,
x + has_border * logical_px(block->border_left),
logical_px(1) + has_border * logical_px(block->border_top),
full_render_width - has_border * logical_px(block->border_right + block->border_left),
bar_height - has_border * logical_px(block->border_bottom + block->border_top) - logical_px(2));
}
draw_util_text(text, &output->statusline_buffer, fg_color, bg_color,
x + render->x_offset + has_border * logical_px(block->border_left),
bar_height / 2 - font.height / 2,
render->width - has_border * logical_px(block->border_left + block->border_right));
x += full_render_width;
/* If this is not the last block, draw a separator. */
if (TAILQ_NEXT(block, blocks) != NULL) {
x += block->sep_block_width;
draw_separator(output, x, block, use_focus_colors);
}
}
}
/*
* Hides all bars (unmaps them)
*
*/
static void hide_bars(void) {
if ((config.hide_on_modifier == M_DOCK) || (config.hidden_state == S_SHOW && config.hide_on_modifier == M_HIDE)) {
return;
}
i3_output *walk;
SLIST_FOREACH(walk, outputs, slist) {
if (!walk->active) {
continue;
}
xcb_unmap_window(xcb_connection, walk->bar.id);
}
stop_child();
}
/*
* Unhides all bars (maps them)
*
*/
static void unhide_bars(void) {
if (config.hide_on_modifier != M_HIDE) {
return;
}
i3_output *walk;
xcb_void_cookie_t cookie;
uint32_t mask;
uint32_t values[5];
cont_child();
SLIST_FOREACH(walk, outputs, slist) {
if (walk->bar.id == XCB_NONE) {
continue;
}
mask = XCB_CONFIG_WINDOW_X |
XCB_CONFIG_WINDOW_Y |
XCB_CONFIG_WINDOW_WIDTH |
XCB_CONFIG_WINDOW_HEIGHT |
XCB_CONFIG_WINDOW_STACK_MODE;
values[0] = walk->rect.x;
if (config.position == POS_TOP)
values[1] = walk->rect.y;
else
values[1] = walk->rect.y + walk->rect.h - bar_height;
values[2] = walk->rect.w;
values[3] = bar_height;
values[4] = XCB_STACK_MODE_ABOVE;
DLOG("Reconfiguring window for output %s to %d,%d\n", walk->name, values[0], values[1]);
cookie = xcb_configure_window_checked(xcb_connection,
walk->bar.id,
mask,
values);
if (xcb_request_failed(cookie, "Could not reconfigure window")) {
exit(EXIT_FAILURE);
}
xcb_map_window(xcb_connection, walk->bar.id);
}
}
/*
* Parse the colors into a format that we can use
*
*/
void init_colors(const struct xcb_color_strings_t *new_colors) {
#define PARSE_COLOR(name, def) \
do { \
colors.name = draw_util_hex_to_color(new_colors->name ? new_colors->name : def); \
} while (0)
PARSE_COLOR(bar_fg, "#FFFFFF");
PARSE_COLOR(bar_bg, "#000000");
PARSE_COLOR(sep_fg, "#666666");
PARSE_COLOR(active_ws_fg, "#FFFFFF");
PARSE_COLOR(active_ws_bg, "#333333");
PARSE_COLOR(active_ws_border, "#333333");
PARSE_COLOR(inactive_ws_fg, "#888888");
PARSE_COLOR(inactive_ws_bg, "#222222");
PARSE_COLOR(inactive_ws_border, "#333333");
PARSE_COLOR(urgent_ws_fg, "#FFFFFF");
PARSE_COLOR(urgent_ws_bg, "#900000");
PARSE_COLOR(urgent_ws_border, "#2f343a");
PARSE_COLOR(focus_ws_fg, "#FFFFFF");
PARSE_COLOR(focus_ws_bg, "#285577");
PARSE_COLOR(focus_ws_border, "#4c7899");
#undef PARSE_COLOR
#define PARSE_COLOR_FALLBACK(name, fallback) \
do { \
colors.name = new_colors->name ? draw_util_hex_to_color(new_colors->name) : colors.fallback; \
} while (0)
/* For the binding mode indicator colors, we don't hardcode a default.
* Instead, we fall back to urgent_ws_* colors. */
PARSE_COLOR_FALLBACK(binding_mode_fg, urgent_ws_fg);
PARSE_COLOR_FALLBACK(binding_mode_bg, urgent_ws_bg);
PARSE_COLOR_FALLBACK(binding_mode_border, urgent_ws_border);
/* Similarly, for unspecified focused bar colors, we fall back to the
* regular bar colors. */
PARSE_COLOR_FALLBACK(focus_bar_fg, bar_fg);
PARSE_COLOR_FALLBACK(focus_bar_bg, bar_bg);
PARSE_COLOR_FALLBACK(focus_sep_fg, sep_fg);
#undef PARSE_COLOR_FALLBACK
init_tray_colors();
xcb_flush(xcb_connection);
}
static bool execute_custom_command(xcb_keycode_t input_code, bool event_is_release) {
binding_t *binding;
TAILQ_FOREACH(binding, &(config.bindings), bindings) {
if ((binding->input_code != input_code) || (binding->release != event_is_release))
continue;
i3_send_msg(I3_IPC_MESSAGE_TYPE_RUN_COMMAND, binding->command);
return true;
}
return false;
}
static void child_handle_button(xcb_button_press_event_t *event, i3_output *output, uint32_t statusline_x) {
if (statusline_x > (uint32_t)output->statusline_width) {
return;
}
/* x of the start of the current block relative to the statusline. */
uint32_t last_block_x = 0;
struct status_block *block;
TAILQ_FOREACH(block, &statusline_head, blocks) {
i3String *text;
struct status_block_render_desc *render;
if (output->statusline_short_text && block->short_text != NULL) {
text = block->short_text;
render = &block->short_render;
} else {
text = block->full_text;
render = &block->full_render;
}
if (i3string_get_num_bytes(text) == 0) {
continue;
}
/* Include the whole block in our calculations: when min_width is
* specified, we have to take padding width into account. */
const uint32_t full_render_width = render->width + render->x_offset + render->x_append;
/* x of the click event relative to the current block. */
const uint32_t relative_x = statusline_x - last_block_x;
if (relative_x <= full_render_width) {
send_block_clicked(event->detail, block->name, block->instance,
event->root_x, event->root_y, relative_x,
event->event_y, full_render_width, bar_height,
event->state);
return;
}
last_block_x += full_render_width + block->sep_block_width;
if (last_block_x > statusline_x) {
/* Click was on a separator. */
return;
}
}
}
/*
* Handle a button press event (i.e. a mouse click on one of our bars).
* We determine, whether the click occurred on a workspace button or if the scroll-
* wheel was used and change the workspace appropriately
*
*/
static void handle_button(xcb_button_press_event_t *event) {
/* Determine, which bar was clicked */
i3_output *walk;
xcb_window_t bar = event->event;
SLIST_FOREACH(walk, outputs, slist) {
if (walk->bar.id == bar) {
break;
}
}
if (walk == NULL) {
DLOG("Unknown bar clicked!\n");
return;
}
DLOG("Got button %d\n", event->detail);
/* During button release events, only check for custom commands. */
const bool event_is_release = (event->response_type & ~0x80) == XCB_BUTTON_RELEASE;
int32_t x = event->event_x >= 0 ? event->event_x : 0;
int workspace_width = 0;
i3_ws *cur_ws = NULL, *clicked_ws = NULL, *ws_walk;
TAILQ_FOREACH(ws_walk, walk->workspaces, tailq) {
int w = 2 * logical_px(ws_hoff_px) + 2 * logical_px(1) + ws_walk->name_width;
if (x >= workspace_width && x <= workspace_width + w)
clicked_ws = ws_walk;
if (ws_walk->visible)
cur_ws = ws_walk;
workspace_width += w;
if (TAILQ_NEXT(ws_walk, tailq) != NULL)
workspace_width += logical_px(ws_spacing_px);
}
if (child_want_click_events() && x > workspace_width) {
const int tray_width = get_tray_width(walk->trayclients);
/* Calculate the horizontal coordinate (x) of the start of the
* statusline by subtracting its width and the width of the tray from
* the bar width. */
const int offset = walk->rect.w - walk->statusline_width -
tray_width - logical_px((tray_width > 0) * sb_hoff_px);
if (x >= offset) {
/* Click was after the start of the statusline, return to avoid
* executing any other actions even if a click event is not
* produced eventually. */
if (!event_is_release) {
/* x of the click event relative to the start of the
* statusline. */
const uint32_t statusline_x = x - offset;
child_handle_button(event, walk, statusline_x);
}
return;
}
}
/* If a custom command was specified for this mouse button, it overrides
* the default behavior. */
if (execute_custom_command(event->detail, event_is_release) || event_is_release) {
return;
}
if (cur_ws == NULL) {
DLOG("No workspace active?\n");
return;
}
switch (event->detail) {
case XCB_BUTTON_SCROLL_UP:
case XCB_BUTTON_SCROLL_LEFT:
/* Mouse wheel up. We select the previous ws, if any.
* If there is no more workspace, don’t even send the workspace
* command, otherwise (with workspace auto_back_and_forth) we’d end
* up on the wrong workspace. */
if (cur_ws == TAILQ_FIRST(walk->workspaces))
return;
cur_ws = TAILQ_PREV(cur_ws, ws_head, tailq);
break;
case XCB_BUTTON_SCROLL_DOWN:
case XCB_BUTTON_SCROLL_RIGHT:
/* Mouse wheel down. We select the next ws, if any.
* If there is no more workspace, don’t even send the workspace
* command, otherwise (with workspace auto_back_and_forth) we’d end
* up on the wrong workspace. */
if (cur_ws == TAILQ_LAST(walk->workspaces, ws_head))
return;
cur_ws = TAILQ_NEXT(cur_ws, tailq);
break;
case 1:
cur_ws = clicked_ws;
/* if no workspace was clicked, focus our currently visible
* workspace if it is not already focused */
if (cur_ws == NULL) {
TAILQ_FOREACH(cur_ws, walk->workspaces, tailq) {
if (cur_ws->visible && !cur_ws->focused)
break;
}
}
/* if there is nothing to focus, we are done */
if (cur_ws == NULL)
return;
break;
default:
return;
}
/* To properly handle workspace names with double quotes in them, we need
* to escape the double quotes. Unfortunately, that’s rather ugly in C: We
* first count the number of double quotes, then we allocate a large enough
* buffer, then we copy character by character. */
int num_quotes = 0;
size_t namelen = 0;
const char *utf8_name = cur_ws->canonical_name;
for (const char *walk = utf8_name; *walk != '\0'; walk++) {
if (*walk == '"' || *walk == '\\')
num_quotes++;
/* While we’re looping through the name anyway, we can save one
* strlen(). */
namelen++;
}
const size_t len = namelen + strlen("workspace \"\"") + 1;
char *buffer = scalloc(len + num_quotes, 1);
memcpy(buffer, "workspace \"", strlen("workspace \""));
size_t inpos, outpos;
for (inpos = 0, outpos = strlen("workspace \"");
inpos < namelen;
inpos++, outpos++) {
if (utf8_name[inpos] == '"' || utf8_name[inpos] == '\\') {
buffer[outpos] = '\\';
outpos++;
}
buffer[outpos] = utf8_name[inpos];
}
buffer[outpos] = '"';
i3_send_msg(I3_IPC_MESSAGE_TYPE_RUN_COMMAND, buffer);
free(buffer);
}
/*
* Handle visibility notifications: when none of the bars are visible, e.g.
* if windows are in fullscreen on each output, suspend the child process.
*
*/
static void handle_visibility_notify(xcb_visibility_notify_event_t *event) {
bool visible = (event->state != XCB_VISIBILITY_FULLY_OBSCURED);
int num_visible = 0;
i3_output *output;
SLIST_FOREACH(output, outputs, slist) {
if (!output->active) {
continue;
}
if (output->bar.id == event->window) {
if (output->visible == visible) {
return;
}
output->visible = visible;
}
num_visible += output->visible;
}
if (num_visible == 0) {
stop_child();
} else if (num_visible == visible) {
/* Wake the child only when transitioning from 0 to 1 visible bar.
* We cannot transition from 0 to 2 or more visible bars at once since
* visibility events are delivered to each window separately */
cont_child();
}
}
/*
* Adjusts the size of the tray window and alignment of the tray clients by
* configuring their respective x coordinates. To be called when mapping or
* unmapping a tray client window.
*
*/
static void configure_trayclients(void) {
trayclient *trayclient;
i3_output *output;
SLIST_FOREACH(output, outputs, slist) {
if (!output->active)
continue;
int clients = 0;
TAILQ_FOREACH_REVERSE(trayclient, output->trayclients, tc_head, tailq) {
if (!trayclient->mapped)
continue;
clients++;
DLOG("Configuring tray window %08x to x=%d\n",
trayclient->win, output->rect.w - (clients * (icon_size + logical_px(config.tray_padding))));
uint32_t x = output->rect.w - (clients * (icon_size + logical_px(config.tray_padding)));
xcb_configure_window(xcb_connection,
trayclient->win,
XCB_CONFIG_WINDOW_X,
&x);
}
}
}
/*
* Handles ClientMessages (messages sent from another client directly to us).
*
* At the moment, only the tray window will receive client messages. All
* supported client messages currently are _NET_SYSTEM_TRAY_OPCODE.
*
*/
static void handle_client_message(xcb_client_message_event_t *event) {
if (event->type == atoms[I3_SYNC]) {
xcb_window_t window = event->data.data32[0];
uint32_t rnd = event->data.data32[1];
/* Forward the request to i3 via the IPC interface so that all pending
* IPC messages are guaranteed to be handled. */
char *payload = NULL;
sasprintf(&payload, "{\"rnd\":%d, \"window\":%d}", rnd, window);
i3_send_msg(I3_IPC_MESSAGE_TYPE_SYNC, payload);
free(payload);
} else if (event->type == atoms[_NET_SYSTEM_TRAY_OPCODE] &&
event->format == 32) {
DLOG("_NET_SYSTEM_TRAY_OPCODE received\n");
/* event->data.data32[0] is the timestamp */
uint32_t op = event->data.data32[1];
uint32_t mask;
uint32_t values[2];
if (op == SYSTEM_TRAY_REQUEST_DOCK) {
xcb_window_t client = event->data.data32[2];
mask = XCB_CW_EVENT_MASK;
/* Needed to get the most recent value of XEMBED_MAPPED. */
values[0] = XCB_EVENT_MASK_PROPERTY_CHANGE;
/* Needed for UnmapNotify events. */
values[0] |= XCB_EVENT_MASK_STRUCTURE_NOTIFY;
/* Needed because some tray applications (e.g., VLC) use
* override_redirect which causes no ConfigureRequest to be sent. */
values[0] |= XCB_EVENT_MASK_RESIZE_REDIRECT;
xcb_change_window_attributes(xcb_connection, client, mask, values);
/* Request the _XEMBED_INFO property. The XEMBED specification
* (which is referred by the tray specification) says this *has* to
* be set, but VLC does not set it… */
bool map_it = true;
int xe_version = 1;
xcb_get_property_cookie_t xembedc;
xcb_generic_error_t *error;
xembedc = xcb_get_property(xcb_connection,
0,
client,
atoms[_XEMBED_INFO],
XCB_GET_PROPERTY_TYPE_ANY,
0,
2 * 32);
xcb_get_property_reply_t *xembedr = xcb_get_property_reply(xcb_connection,
xembedc,
&error);
if (error != NULL) {
ELOG("Error getting _XEMBED_INFO property: error_code %d\n",
error->error_code);
free(error);
return;
}
if (xembedr != NULL && xembedr->length != 0) {
DLOG("xembed format = %d, len = %d\n", xembedr->format, xembedr->length);
uint32_t *xembed = xcb_get_property_value(xembedr);
DLOG("xembed version = %d\n", xembed[0]);
DLOG("xembed flags = %d\n", xembed[1]);
map_it = ((xembed[1] & XEMBED_MAPPED) == XEMBED_MAPPED);
xe_version = xembed[0];
if (xe_version > 1)
xe_version = 1;
free(xembedr);
} else {
ELOG("Window %08x violates the XEMBED protocol, _XEMBED_INFO not set\n", client);
}
DLOG("X window %08x requested docking\n", client);
if (output_for_tray == NULL) {
ELOG("No output found for tray\n");
return;
}
xcb_void_cookie_t rcookie = xcb_reparent_window(xcb_connection,
client,
output_for_tray->bar.id,
output_for_tray->rect.w - icon_size - logical_px(config.tray_padding),
logical_px(config.tray_padding));
if (xcb_request_failed(rcookie, "Could not reparent window. Maybe it is using an incorrect depth/visual?"))
return;
/* We reconfigure the window to use a reasonable size. The systray
* specification explicitly says:
* Tray icons may be assigned any size by the system tray, and
* should do their best to cope with any size effectively
*/
mask = XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT;
values[0] = icon_size;
values[1] = icon_size;
xcb_configure_window(xcb_connection,
client,
mask,
values);
/* send the XEMBED_EMBEDDED_NOTIFY message */
void *event = scalloc(32, 1);
xcb_client_message_event_t *ev = event;
ev->response_type = XCB_CLIENT_MESSAGE;
ev->window = client;
ev->type = atoms[_XEMBED];
ev->format = 32;
ev->data.data32[0] = XCB_CURRENT_TIME;
ev->data.data32[1] = XEMBED_EMBEDDED_NOTIFY;
ev->data.data32[2] = output_for_tray->bar.id;
ev->data.data32[3] = xe_version;
xcb_send_event(xcb_connection,
0,
client,
XCB_EVENT_MASK_NO_EVENT,
(char *)ev);
free(event);
/* Put the client inside the save set. Upon termination (whether
* killed or normal exit does not matter) of i3bar, these clients
* will be correctly reparented to their most closest living
* ancestor. Without this, tray icons might die when i3bar
* exits/crashes. */
xcb_change_save_set(xcb_connection, XCB_SET_MODE_INSERT, client);
trayclient *tc = smalloc(sizeof(trayclient));
tc->win = client;
tc->xe_version = xe_version;
tc->mapped = false;
TAILQ_INSERT_TAIL(output_for_tray->trayclients, tc, tailq);
if (map_it) {
DLOG("Mapping dock client\n");
xcb_map_window(xcb_connection, client);
} else {
DLOG("Not mapping dock client yet\n");
}
/* Trigger an update to copy the statusline text to the appropriate
* position */
configure_trayclients();
draw_bars(false);
}
}
}
/*
* Handles DestroyNotify events by removing the tray client from the data
* structure. According to the XEmbed protocol, this is one way for a tray
* client to finish the protocol. After this event is received, there is no
* further interaction with the tray client.
*
* See: https://standards.freedesktop.org/xembed-spec/xembed-spec-latest.html
*
*/
static void handle_destroy_notify(xcb_destroy_notify_event_t *event) {
DLOG("DestroyNotify for window = %08x, event = %08x\n", event->window, event->event);
i3_output *walk;
SLIST_FOREACH(walk, outputs, slist) {
if (!walk->active)
continue;
DLOG("checking output %s\n", walk->name);
trayclient *trayclient;
TAILQ_FOREACH(trayclient, walk->trayclients, tailq) {
if (trayclient->win != event->window) {
continue;
}
DLOG("Removing tray client with window ID %08x\n", event->window);
TAILQ_REMOVE(walk->trayclients, trayclient, tailq);
FREE(trayclient);
/* Trigger an update, we now have more space for the statusline */
configure_trayclients();
draw_bars(false);
return;
}
}
}
/*
* Handles MapNotify events. These events happen when a tray client shows its
* window. We respond by realigning the tray clients.
*
*/
static void handle_map_notify(xcb_map_notify_event_t *event) {
DLOG("MapNotify for window = %08x, event = %08x\n", event->window, event->event);
i3_output *walk;
SLIST_FOREACH(walk, outputs, slist) {
if (!walk->active)
continue;
DLOG("checking output %s\n", walk->name);
trayclient *trayclient;
TAILQ_FOREACH(trayclient, walk->trayclients, tailq) {
if (trayclient->win != event->window)
continue;
DLOG("Tray client mapped (window ID %08x). Adjusting tray.\n", event->window);
trayclient->mapped = true;
/* Trigger an update, we now have more space for the statusline */
configure_trayclients();
draw_bars(false);
return;
}
}
}
/*
* Handles UnmapNotify events. These events happen when a tray client hides its
* window. We respond by realigning the tray clients.
*
*/
static void handle_unmap_notify(xcb_unmap_notify_event_t *event) {
DLOG("UnmapNotify for window = %08x, event = %08x\n", event->window, event->event);
i3_output *walk;
SLIST_FOREACH(walk, outputs, slist) {
if (!walk->active)
continue;
DLOG("checking output %s\n", walk->name);
trayclient *trayclient;
TAILQ_FOREACH(trayclient, walk->trayclients, tailq) {
if (trayclient->win != event->window)
continue;
DLOG("Tray client unmapped (window ID %08x). Adjusting tray.\n", event->window);
trayclient->mapped = false;
/* Trigger an update, we now have more space for the statusline */
configure_trayclients();
draw_bars(false);
return;
}
}
}
/*
* Handle PropertyNotify messages. Currently only the _XEMBED_INFO property is
* handled, which tells us whether a dock client should be mapped or unmapped.
*
*/
static void handle_property_notify(xcb_property_notify_event_t *event) {
DLOG("PropertyNotify\n");
if (event->atom == atoms[_XEMBED_INFO] &&
event->state == XCB_PROPERTY_NEW_VALUE) {
DLOG("xembed_info updated\n");
trayclient *trayclient = NULL, *walk;
i3_output *o_walk;
SLIST_FOREACH(o_walk, outputs, slist) {
if (!o_walk->active)
continue;
TAILQ_FOREACH(walk, o_walk->trayclients, tailq) {
if (walk->win != event->window)
continue;
trayclient = walk;
break;
}
if (trayclient)
break;
}
if (!trayclient) {
ELOG("PropertyNotify received for unknown window %08x\n",
event->window);
return;
}
xcb_get_property_cookie_t xembedc;
xembedc = xcb_get_property_unchecked(xcb_connection,
0,
trayclient->win,
atoms[_XEMBED_INFO],
XCB_GET_PROPERTY_TYPE_ANY,
0,
2 * 32);
xcb_get_property_reply_t *xembedr = xcb_get_property_reply(xcb_connection,
xembedc,
NULL);