-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwindow-x11.c
1333 lines (1184 loc) · 49.4 KB
/
window-x11.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Copyright (c) 2019-2022, Evgeniy Baskov. All rights reserved */
#include "feature.h"
#if USE_PPOLL
# define _GNU_SOURCE
#endif
#include "config.h"
#include "mouse.h"
#include "poller.h"
#include "term.h"
#include "uri.h"
#include "util.h"
#include "window-impl.h"
#include "window-x11.h"
#include <errno.h>
#include <inttypes.h>
#include <poll.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <xcb/xcb_cursor.h>
#include <xcb/xcb.h>
#include <xcb/xkb.h>
#include <xkbcommon/xkbcommon-x11.h>
struct cursor {
ht_head_t link;
char *name;
int32_t refcount;
xcb_cursor_t xcursor;
};
struct context {
struct event *con_evt;
xcb_screen_t *screen;
xcb_colormap_t mid;
xcb_visualtype_t *vis;
xcb_cursor_context_t *cursor_ctx;
hashtable_t cursors;
xcb_cursor_t hidden_cursor;
struct atom_ {
xcb_atom_t _NET_WM_PID;
xcb_atom_t _NET_WM_NAME;
xcb_atom_t _NET_WM_ICON_NAME;
xcb_atom_t _NET_WM_STATE;
xcb_atom_t _NET_WM_STATE_FULLSCREEN;
xcb_atom_t _NET_WM_STATE_MAXIMIZED_VERT;
xcb_atom_t _NET_WM_STATE_MAXIMIZED_HORZ;
xcb_atom_t _NET_ACTIVE_WINDOW;
xcb_atom_t _NET_MOVERESIZE_WINDOW;
xcb_atom_t WM_DELETE_WINDOW;
xcb_atom_t WM_PROTOCOLS;
xcb_atom_t WM_NORMAL_HINTS;
xcb_atom_t WM_SIZE_HINTS;
xcb_atom_t WM_CHANGE_STATE;
xcb_atom_t UTF8_STRING;
xcb_atom_t CLIPBOARD;
xcb_atom_t INCR;
xcb_atom_t TARGETS;
} atom;
struct xkb_context *xkb_ctx;
struct xkb_state *xkb_state;
struct xkb_keymap *xkb_keymap;
int32_t xkb_core_kbd;
uint8_t xkb_base_event;
uint8_t xkb_base_err;
void (*renderer_recolor_border)(struct window *);
void (*renderer_free)(struct window *);
void (*renderer_free_context)(void);
};
static struct context ctx;
xcb_connection_t *con = NULL;
static struct window *window_for_xid(xcb_window_t xid) {
LIST_FOREACH(it, &win_list_head) {
struct window *win = CONTAINEROF(it, struct window, link);
if (get_plat(win)->wid == xid) {
win->any_event_happend = true;
return win;
}
}
return NULL;
}
static xcb_atom_t intern_atom(const char *atom) {
xcb_atom_t at;
xcb_generic_error_t *err = NULL;
xcb_intern_atom_cookie_t c = xcb_intern_atom(con, 0, strlen(atom), atom);
xcb_intern_atom_reply_t *reply = xcb_intern_atom_reply(con, c, &err);
if (err) {
warn("Can't intern atom: %s", atom);
free(err);
}
at = reply->atom;
free(reply);
return at;
}
static bool update_keymap(void) {
struct xkb_keymap *new_keymap = xkb_x11_keymap_new_from_device(ctx.xkb_ctx, con, ctx.xkb_core_kbd, XKB_KEYMAP_COMPILE_NO_FLAGS);
if (!new_keymap) {
warn("Can't create XKB keymap");
return 0;
}
struct xkb_state *new_state = xkb_x11_state_new_from_device(new_keymap, con, ctx.xkb_core_kbd);
if (!new_state) {
xkb_keymap_unref(new_keymap);
warn("Can't get window xkb state");
return 0;
}
if (ctx.xkb_state)
xkb_state_unref(ctx.xkb_state);
if (ctx.xkb_keymap)
xkb_keymap_unref(ctx.xkb_keymap);
ctx.xkb_keymap = new_keymap;
ctx.xkb_state = new_state;
return 1;
}
static bool configure_xkb(void) {
uint16_t xkb_min = 0, xkb_maj = 0;
int res = xkb_x11_setup_xkb_extension(con, XKB_X11_MIN_MAJOR_XKB_VERSION,
XKB_X11_MIN_MINOR_XKB_VERSION, XKB_X11_SETUP_XKB_EXTENSION_NO_FLAGS,
&xkb_maj, &xkb_min, &ctx.xkb_base_event, &ctx.xkb_base_err);
if (!res || xkb_maj < XKB_X11_MIN_MAJOR_XKB_VERSION) {
warn("Can't get suitable XKB verion");
return 0;
}
ctx.xkb_core_kbd = xkb_x11_get_core_keyboard_device_id(con);
if (ctx.xkb_core_kbd == -1) {
warn("Can't get core keyboard device");
return 0;
}
ctx.xkb_ctx = xkb_context_new(XKB_CONTEXT_NO_FLAGS);
if (!ctx.xkb_ctx) {
warn("Can't create XKB context");
return 0;
}
if (!update_keymap())
goto cleanup_context;
const uint32_t events = XCB_XKB_EVENT_TYPE_NEW_KEYBOARD_NOTIFY |
XCB_XKB_EVENT_TYPE_MAP_NOTIFY | XCB_XKB_EVENT_TYPE_STATE_NOTIFY;
const uint32_t nkn_details = XCB_XKB_NKN_DETAIL_KEYCODES;
const uint32_t map_parts = XCB_XKB_MAP_PART_KEY_TYPES | XCB_XKB_MAP_PART_KEY_SYMS |
XCB_XKB_MAP_PART_MODIFIER_MAP | XCB_XKB_MAP_PART_EXPLICIT_COMPONENTS |
XCB_XKB_MAP_PART_KEY_ACTIONS | XCB_XKB_MAP_PART_VIRTUAL_MODS |
XCB_XKB_MAP_PART_VIRTUAL_MOD_MAP;
const uint32_t state_details = XCB_XKB_STATE_PART_MODIFIER_BASE | XCB_XKB_STATE_PART_MODIFIER_LATCH |
XCB_XKB_STATE_PART_MODIFIER_LOCK | XCB_XKB_STATE_PART_GROUP_BASE |
XCB_XKB_STATE_PART_GROUP_LATCH | XCB_XKB_STATE_PART_GROUP_LOCK;
const xcb_xkb_select_events_details_t details = {
.affectNewKeyboard = nkn_details, .newKeyboardDetails = nkn_details,
.affectState = state_details, .stateDetails = state_details,
};
xcb_void_cookie_t c;
c = xcb_xkb_select_events_aux_checked(con, ctx.xkb_core_kbd, events,
0, 0, map_parts, map_parts, &details);
if (check_void_cookie(c)) {
warn("Can't select XKB events");
goto cleanup_state;
}
if (!update_keymap()) {
warn("Can't update keymap");
goto cleanup_state;
}
return 1;
cleanup_state:
xkb_state_unref(ctx.xkb_state);
xkb_keymap_unref(ctx.xkb_keymap);
cleanup_context:
xkb_context_unref(ctx.xkb_ctx);
return 0;
}
static void x11_update_colors(struct window *win) {
uint32_t values2[2];
values2[0] = values2[1] = win->bg_premul;
xcb_change_window_attributes(con, get_plat(win)->wid, XCB_CW_BACK_PIXEL, values2);
xcb_change_gc(con, get_plat(win)->gc, XCB_GC_FOREGROUND | XCB_GC_BACKGROUND, values2);
ctx.renderer_recolor_border(win);
}
static bool x11_resize_window(struct window *win, int16_t width, int16_t height) {
if (win->w.height != height || win->w.width != width) {
uint32_t vals[] = {width, height};
xcb_void_cookie_t c = xcb_configure_window_checked(con, get_plat(win)->wid,
XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT, vals);
return !check_void_cookie(c);
}
return false;
}
static void x11_move_window(struct window *win, int16_t x, int16_t y) {
uint32_t vals[] = {x, y};
xcb_configure_window(con, get_plat(win)->wid, XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y, vals);
}
static void send_wm_client_event(xcb_window_t win, uint32_t type, uint32_t data0, uint32_t data1) {
xcb_client_message_event_t ev = {
.response_type = XCB_CLIENT_MESSAGE,
.format = 32,
.sequence = 0,
.window = win,
.type = type,
.data = {
.data32 = {
data0,
data1,
0, 0, 0,
}
}
};
xcb_send_event(con, 0, ctx.screen->root, XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY |
XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT, (const char *)&ev);
}
#define WM_STATE_WITHDRAWN 0
#define WM_STATE_NORMAL 1
#define WM_STATE_ICONIC 3
#define _NET_WM_STATE_REMOVE 0
#define _NET_WM_STATE_ADD 1
#define _NET_WM_STATE_TOGGLE 2
static inline void save_pos(struct window *win) {
if (get_plat(win)->saved.width > 0) {
struct extent p = window_get_position(win);
struct extent s = window_get_size(win);
get_plat(win)->saved = (struct rect) { p.width, p.height, s.width, s.height };
assert(s.width > 0);
}
}
static inline bool restore_pos(struct window *win) {
xcb_void_cookie_t c;
if (get_plat(win)->saved.width > 0) {
uint32_t vals[] = {get_plat(win)->saved.x, get_plat(win)->saved.y, get_plat(win)->saved.width, get_plat(win)->saved.height};
c = xcb_configure_window_checked(con, get_plat(win)->wid, XCB_CONFIG_WINDOW_WIDTH |
XCB_CONFIG_WINDOW_HEIGHT | XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y, vals);
get_plat(win)->saved.width = 0;
return !check_void_cookie(c);
}
return false;
}
static bool x11_window_action(struct window *win, enum window_action act) {
switch (act) {
uint32_t val;
case action_minimize:
send_wm_client_event(get_plat(win)->wid, ctx.atom.WM_CHANGE_STATE, WM_STATE_ICONIC, 0);
break;
case action_restore_minimized:
send_wm_client_event(get_plat(win)->wid, ctx.atom._NET_ACTIVE_WINDOW, 1, XCB_CURRENT_TIME);
break;
case action_lower:
val = XCB_STACK_MODE_BELOW;
xcb_configure_window(con, get_plat(win)->wid, XCB_CONFIG_WINDOW_STACK_MODE, &val);
break;
case action_raise:
val = XCB_STACK_MODE_ABOVE;
xcb_configure_window(con, get_plat(win)->wid, XCB_CONFIG_WINDOW_STACK_MODE, &val);
break;
case action_maximize:
save_pos(win);
send_wm_client_event(get_plat(win)->wid, ctx.atom._NET_WM_STATE, _NET_WM_STATE_REMOVE, ctx.atom._NET_WM_STATE_MAXIMIZED_HORZ);
send_wm_client_event(get_plat(win)->wid, ctx.atom._NET_WM_STATE, _NET_WM_STATE_REMOVE, ctx.atom._NET_WM_STATE_MAXIMIZED_VERT);
uint32_t vals[] = {0, 0, ctx.screen->width_in_pixels, ctx.screen->height_in_pixels};
xcb_void_cookie_t c;
c = xcb_configure_window_checked(con, get_plat(win)->wid, XCB_CONFIG_WINDOW_WIDTH |
XCB_CONFIG_WINDOW_HEIGHT | XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y, vals);
return !check_void_cookie(c);
case action_maximize_width:
save_pos(win);
send_wm_client_event(get_plat(win)->wid, ctx.atom._NET_WM_STATE,
_NET_WM_STATE_ADD, ctx.atom._NET_WM_STATE_MAXIMIZED_HORZ);
break;
case action_maximize_height:
save_pos(win);
send_wm_client_event(get_plat(win)->wid, ctx.atom._NET_WM_STATE,
_NET_WM_STATE_ADD, ctx.atom._NET_WM_STATE_MAXIMIZED_VERT);
break;
case action_fullscreen:
save_pos(win);
send_wm_client_event(get_plat(win)->wid, ctx.atom._NET_WM_STATE,
_NET_WM_STATE_ADD, ctx.atom._NET_WM_STATE_FULLSCREEN);
break;
case action_restore:
send_wm_client_event(get_plat(win)->wid, ctx.atom._NET_WM_STATE, _NET_WM_STATE_REMOVE, ctx.atom._NET_WM_STATE_MAXIMIZED_HORZ);
send_wm_client_event(get_plat(win)->wid, ctx.atom._NET_WM_STATE, _NET_WM_STATE_REMOVE, ctx.atom._NET_WM_STATE_MAXIMIZED_VERT);
send_wm_client_event(get_plat(win)->wid, ctx.atom._NET_WM_STATE, _NET_WM_STATE_REMOVE, ctx.atom._NET_WM_STATE_FULLSCREEN);
return restore_pos(win);
case action_toggle_fullscreen:
return x11_window_action(win, get_plat(win)->saved.width > 0 ? action_restore : action_fullscreen);
case action_none:
break;
}
return false;
}
static struct extent x11_get_position(struct window *win) {
xcb_get_geometry_cookie_t gc = xcb_get_geometry(con, get_plat(win)->wid);
xcb_get_geometry_reply_t *rep = xcb_get_geometry_reply(con, gc, NULL);
struct extent res = {0, 0};
if (rep) {
res.width = rep->x;
res.height = rep->y;
free(rep);
}
return res;
}
struct extent x11_get_screen_size(struct window *win) {
(void)win;
return (struct extent) { ctx.screen->width_in_pixels, ctx.screen->height_in_pixels };
}
static void x11_get_pointer(struct window *win, struct extent *p, int32_t *pmask) {
xcb_query_pointer_cookie_t c = xcb_query_pointer(con, get_plat(win)->wid);
xcb_query_pointer_reply_t *qre = xcb_query_pointer_reply(con, c, NULL);
if (qre) {
p->width = MIN(MAX(0, qre->win_x), win->w.width);
p->height = MIN(MAX(0, qre->win_y), win->w.height);
*pmask = qre->mask;
free(qre);
}
}
#define WM_HINTS_LEN 8
static void x11_set_urgency(struct window *win, bool set) {
xcb_get_property_cookie_t c = xcb_get_property(con, 0, get_plat(win)->wid, XCB_ATOM_WM_HINTS, XCB_ATOM_WM_HINTS, 0, WM_HINTS_LEN);
xcb_get_property_reply_t *rep = xcb_get_property_reply(con, c, NULL);
if (rep) {
uint32_t *hints = xcb_get_property_value(rep);
if (set) *hints |= 256; /* UrgentcyHint */
else *hints &= ~256; /* UrgentcyHint */
xcb_change_property(con, XCB_PROP_MODE_REPLACE, get_plat(win)->wid, XCB_ATOM_WM_HINTS, XCB_ATOM_WM_HINTS, 32, WM_HINTS_LEN, hints);
free(rep);
}
}
static void x11_bell(struct window *win, uint8_t vol) {
xcb_xkb_bell(con, XCB_XKB_ID_USE_CORE_KBD, XCB_XKB_ID_DFLT_XI_CLASS,
XCB_XKB_ID_DFLT_XI_ID, vol, 1, 0, 0, 0, XCB_ATOM_ANY, get_plat(win)->wid);
}
static bool cursor_cmp(const ht_head_t *a, const ht_head_t *b) {
const struct cursor *ua = (const struct cursor *)a;
const struct cursor *ub = (const struct cursor *)b;
return !strcmp(ua->name, ub->name);
}
static struct cursor *get_cursor(const char *name, bool complain) {
struct cursor dummy = {
.name = (char *)name,
.link.hash = hash64(name, strlen(name)),
};
ht_head_t **it = ht_lookup_ptr(&ctx.cursors, &dummy.link);
if (*it) {
struct cursor *found = (struct cursor *)*it;
found->refcount++;
return found;
}
xcb_cursor_t xc = xcb_cursor_load_cursor(ctx.cursor_ctx, name);
if (xc == XCB_NONE) {
if (name && *name) {
if (complain)
warn("Unable to load cursor '%s'", name);
else if (gconfig.trace_misc)
info("Unable to load cursor '%s'", name);
}
return NULL;
}
struct cursor *new = xalloc(sizeof *new);
*new = (struct cursor) {
.link.hash = dummy.link.hash,
.name = strdup(name),
.refcount = 1,
.xcursor = xc,
};
ht_insert_hint(&ctx.cursors, it, &new->link);
return new;
}
static inline struct cursor *get_any_cursor(ssize_t count, const char **names) {
struct cursor *csr = NULL;
for (ssize_t i = 0; i < count; i++)
if (names[i] && (csr = get_cursor(names[i], i == 0))) break;
if (!csr) {
csr = get_cursor("default", false);
assert(csr);
}
return csr;
}
static void unref_cursor(struct cursor *csr) {
if (!csr) return;
if (!--csr->refcount) {
ht_erase(&ctx.cursors, &csr->link);
xcb_free_cursor(con, csr->xcursor);
free(csr->name);
free(csr);
}
}
static inline struct cursor *ref_cursor(struct cursor *csr) {
csr->refcount++;
return csr;
}
static void update_hide_cursor(struct window *win) {
bool hide = (get_plat(win)->cursor_mode == hide_always ||
(get_plat(win)->cursor_mode == hide_no_tracking &&
term_get_mstate(win->term)->mouse_mode == mouse_mode_none));
if (hide && !get_plat(win)->cursor_is_hidden) {
uint32_t xc = ctx.hidden_cursor;
xcb_change_window_attributes(con, get_plat(win)->wid, XCB_CW_CURSOR, &xc);
get_plat(win)->cursor_is_hidden = true;
} else if (!hide && get_plat(win)->cursor_is_hidden) {
assert(get_plat(win)->cursor);
uint32_t xc = get_plat(win)->cursor->xcursor;
xcb_change_window_attributes(con, get_plat(win)->wid, XCB_CW_CURSOR, &xc);
get_plat(win)->cursor_is_hidden = false;
}
}
static void x11_enable_mouse_events(struct window *win, bool enabled) {
if (enabled) get_plat(win)->ev_mask |= XCB_EVENT_MASK_POINTER_MOTION;
else get_plat(win)->ev_mask &= ~XCB_EVENT_MASK_POINTER_MOTION;
xcb_change_window_attributes(con, get_plat(win)->wid, XCB_CW_EVENT_MASK, &get_plat(win)->ev_mask);
update_hide_cursor(win);
}
static void select_cursor(struct window *win, struct cursor *csr) {
ref_cursor(csr);
unref_cursor(get_plat(win)->cursor);
get_plat(win)->cursor = csr;
if (!get_plat(win)->cursor_is_hidden)
xcb_change_window_attributes(con, get_plat(win)->wid, XCB_CW_CURSOR, &csr->xcursor);
}
static void do_create_hidden_cursor(void) {
ctx.hidden_cursor = xcb_generate_id(con);
/* This is is leaked, but it's fine, since we craete it only once */
xcb_pixmap_t pm = xcb_generate_id(con);
xcb_void_cookie_t c;
c = xcb_create_pixmap_checked(con, 1, pm, ctx.screen->root, 1, 1);
if (check_void_cookie(c)) return;
c = xcb_create_cursor_checked(con, ctx.hidden_cursor, pm, pm, 0, 0, 0, 0, 0, 0, 0, 0);
if (check_void_cookie(c)) return;
xcb_free_pixmap(con, pm);
}
static void x11_select_cursor(struct window *win, const char *name) {
if (!ctx.cursor_ctx) return;
struct cursor *csr = get_cursor(name, true);
unref_cursor(get_plat(win)->cursor_user);
get_plat(win)->cursor_user = csr;
if (!csr) csr = get_plat(win)->cursor_default;
select_cursor(win, csr);
}
static void x11_set_pointer_mode(struct window *win, enum hide_pointer_mode mode) {
get_plat(win)->cursor_mode = mode;
update_hide_cursor(win);
}
static void x11_set_title(struct window *win, const char *title, bool utf8) {
xcb_change_property(con, XCB_PROP_MODE_REPLACE, get_plat(win)->wid,
utf8 ? ctx.atom._NET_WM_NAME : XCB_ATOM_WM_NAME,
utf8 ? ctx.atom.UTF8_STRING : XCB_ATOM_STRING, 8, strlen(title), title);
}
static void x11_set_icon_label(struct window *win, const char *title, bool utf8) {
xcb_change_property(con, XCB_PROP_MODE_REPLACE, get_plat(win)->wid,
utf8 ? ctx.atom._NET_WM_ICON_NAME : XCB_ATOM_WM_ICON_NAME,
utf8 ? ctx.atom.UTF8_STRING : XCB_ATOM_STRING, 8, strlen(title), title);
}
static char *get_full_property(xcb_window_t wid, xcb_atom_t prop, xcb_atom_t *type, size_t *psize) {
size_t left = 0, offset = 0, size = 0, data_len = 0;
char *data = NULL;
do {
xcb_get_property_cookie_t c = xcb_get_property(con, 0, wid,
prop, XCB_GET_PROPERTY_TYPE_ANY, offset, PASTE_BLOCK_SIZE/4);
xcb_get_property_reply_t *rep = xcb_get_property_reply(con, c, NULL);
if (!rep || !rep->value_len) {
free(rep);
break;
}
size_t len = rep->value_len * rep->format / 8;
left = rep->bytes_after;
if (type && !offset) *type = rep->type;
offset += len/4;
data = xrealloc(data, data_len, size + len + 1);
data_len = size + len + 1;
memcpy(data + size, xcb_get_property_value(rep), len);
data[size += len] = 0;
free(rep);
} while (left);
if (psize) *psize = size - 1;
return data;
}
static void x11_get_title(struct window *win, enum title_target which, char **name, bool *utf8) {
xcb_atom_t type = XCB_ATOM_ANY;
char *data = NULL;
if (which & target_title) {
data = get_full_property(get_plat(win)->wid, ctx.atom._NET_WM_NAME, &type, NULL);
if (!data) data = get_full_property(get_plat(win)->wid, XCB_ATOM_WM_NAME, &type, NULL);
} else if (which & target_icon_label) {
data = get_full_property(get_plat(win)->wid, ctx.atom._NET_WM_ICON_NAME, &type, NULL);
if (!data) data = get_full_property(get_plat(win)->wid, XCB_ATOM_WM_ICON_NAME, &type, NULL);
}
if (utf8) *utf8 = type == ctx.atom.UTF8_STRING;
if (name) *name = data;
else free(data);
}
static inline uint32_t get_win_gravity_from_config(bool nx, bool ny) {
switch (nx + 2 * ny) {
case 0: return XCB_GRAVITY_NORTH_WEST;
case 1: return XCB_GRAVITY_NORTH_EAST;
case 2: return XCB_GRAVITY_SOUTH_WEST;
default: return XCB_GRAVITY_NORTH_EAST;
}
}
enum {
HINT_USPOSITION = (1 << 0),
HINT_USSIZE = (1 << 1),
HINT_PPOSITION = (1 << 2),
HINT_PSIZE = (1 << 3),
HINT_PMINSIZE = (1 << 4),
HINT_PMAXSIZE = (1 << 5),
HINT_PRESIZEINC = (1 << 6),
HINT_PASPECT = (1 << 7),
HINT_PBASESIZE = (1 << 8),
HINT_PWINGRAVITY = (1 << 9),
};
struct normal_hints {
uint32_t flags;
uint32_t x, y;
uint32_t width, height;
uint32_t min_width, min_height;
uint32_t max_width, max_height;
uint32_t inc_width, inc_height;
struct {
uint32_t num;
uint32_t den;
} min_aspect, max_aspect;
uint32_t base_width, base_height;
uint32_t gravity;
};
enum {
HINT_INPUT = (1 << 0),
HINT_STATE = (1 << 1),
HINT_ICONPIXMAP = (1 << 2),
HINT_ICONWINDOW = (1 << 3),
HINT_ICONPOSITION = (1 << 4),
HINT_ICONMASK = (1 << 5),
HINT_WINDOWGROUP = (1 << 6),
HINT_MESSAGE = (1 << 7),
HINT_URGENCY = (1 << 8),
};
struct wm_hints {
uint32_t flags;
uint32_t input;
uint32_t initial_state;
uint32_t icon_pixmap;
uint32_t icon_window;
uint32_t icon_x;
uint32_t icon_y;
uint32_t icon_mask;
};
void x11_update_window_props(struct window *win) {
uint32_t pid = getpid();
xcb_change_property(con, XCB_PROP_MODE_REPLACE, get_plat(win)->wid, ctx.atom._NET_WM_PID, XCB_ATOM_CARDINAL, 32, 1, &pid);
xcb_change_property(con, XCB_PROP_MODE_REPLACE, get_plat(win)->wid, ctx.atom.WM_PROTOCOLS, XCB_ATOM_ATOM, 32, 1, &ctx.atom.WM_DELETE_WINDOW);
const char *extra;
xcb_change_property(con, XCB_PROP_MODE_REPLACE, get_plat(win)->wid, XCB_ATOM_WM_CLASS, XCB_ATOM_STRING, 8, sizeof(NSST_CLASS), NSST_CLASS);
if ((extra = win->cfg.window_class))
xcb_change_property(con, XCB_PROP_MODE_APPEND, get_plat(win)->wid, XCB_ATOM_WM_CLASS, XCB_ATOM_STRING, 8, strlen(extra), extra);
struct normal_hints nhints = {
.flags = HINT_PMINSIZE | HINT_PBASESIZE,
.x = win->cfg.geometry.r.x, .y = win->cfg.geometry.r.y,
.width = win->w.width, .height = win->w.height,
.min_width = win->cfg.border.left + win->cfg.border.right + 2 * win->char_width,
.min_height = win->cfg.border.top + win->cfg.border.bottom + win->char_depth + win->char_height,
.base_width = win->w.width, .base_height = win->w.height,
.gravity = get_win_gravity_from_config(win->cfg.geometry.stick_to_right, win->cfg.geometry.stick_to_bottom),
};
if (!win->cfg.smooth_resize) {
nhints.flags |= HINT_PRESIZEINC;
nhints.inc_width = win->char_width;
nhints.inc_height = win->char_depth + win->char_height;
}
if (win->cfg.geometry.has_position)
nhints.flags |= HINT_USPOSITION | HINT_PWINGRAVITY;
else
nhints.flags |= HINT_PPOSITION;
if (win->cfg.geometry.has_extent)
nhints.flags |= HINT_USSIZE;
else
nhints.flags |= HINT_PSIZE;
if (win->cfg.fixed) {
nhints.flags |= HINT_PMAXSIZE;
nhints.max_width = nhints.min_width = nhints.width;
nhints.max_height = nhints.min_height = nhints.height;
}
struct wm_hints wmhints = {
.flags = HINT_INPUT,
.input = XCB_WINDOW_CLASS_INPUT_OUTPUT, /* Input */
};
xcb_change_property(con, XCB_PROP_MODE_REPLACE, get_plat(win)->wid, ctx.atom.WM_NORMAL_HINTS,
ctx.atom.WM_SIZE_HINTS, 8*sizeof(uint32_t), sizeof(nhints)/sizeof(uint32_t), &nhints);
xcb_change_property(con, XCB_PROP_MODE_REPLACE, get_plat(win)->wid, XCB_ATOM_WM_HINTS,
XCB_ATOM_WM_HINTS, 8*sizeof(uint32_t), sizeof(wmhints)/sizeof(uint32_t), &wmhints);
}
static struct extent apply_gravity(struct window *win, struct geometry *geometry) {
struct extent ssize = x11_get_screen_size(win);
struct extent position = { geometry->r.x, geometry->r.y };
if (geometry->stick_to_right)
position.width += ssize.width - win->w.width - 2;
if (geometry->stick_to_bottom)
position.height += ssize.height - win->w.height - 2;
return position;
}
void x11_apply_geometry(struct window *win, struct geometry *geometry) {
if (geometry->char_geometry) {
win->c.width = MAX(geometry->r.width, 2);
win->c.height = MAX(geometry->r.height, 1);
win->w = win_derive_window_size(win, win->c.width, win->c.height);
struct extent position = apply_gravity(win, geometry);
uint32_t vals[] = { position.width, position.height, win->w.width, win->w.height };
xcb_configure_window(con, get_plat(win)->wid,
XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y |
XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT, vals);
} else {
win->w.width = geometry->r.width;
win->w.height = geometry->r.height;
win->c = win_derive_grid_size(win, win->w.width, win->w.height);
}
}
static void x11_reload_cursors(struct window *win) {
if (get_plat(win)->cursor_uri)
unref_cursor(get_plat(win)->cursor_uri);
get_plat(win)->cursor_uri = get_any_cursor(4, (const char *[]){win->cfg.uri_pointer, "hand2", "pointer", "pointing_hand"});
if (get_plat(win)->cursor_default)
unref_cursor(get_plat(win)->cursor_default);
get_plat(win)->cursor_default = get_any_cursor(4, (const char *[]){win->cfg.normal_pointer, "xterm", "text", "ibeam"});
if (get_plat(win)->cursor_user != get_plat(win)->cursor)
select_cursor(win, get_plat(win)->cursor_default);
update_hide_cursor(win);
}
static bool x11_init_window(struct window *win) {
xcb_void_cookie_t c;
get_plat(win)->ev_mask = XCB_EVENT_MASK_EXPOSURE | XCB_EVENT_MASK_VISIBILITY_CHANGE |
XCB_EVENT_MASK_KEY_PRESS | XCB_EVENT_MASK_FOCUS_CHANGE | XCB_EVENT_MASK_STRUCTURE_NOTIFY |
XCB_EVENT_MASK_BUTTON_MOTION | XCB_EVENT_MASK_BUTTON_PRESS | XCB_EVENT_MASK_BUTTON_RELEASE | XCB_EVENT_MASK_PROPERTY_CHANGE;
uint32_t mask1 = XCB_CW_BACK_PIXEL | XCB_CW_BORDER_PIXEL |
XCB_CW_BIT_GRAVITY | XCB_CW_EVENT_MASK | XCB_CW_COLORMAP;
uint32_t values1[5] = { win->bg_premul, win->bg_premul, XCB_GRAVITY_NORTH_WEST, get_plat(win)->ev_mask, ctx.mid };
/* Adjust geometry. It will be incorrect if specified in characters. */
struct extent position = {0};
if (!win->cfg.geometry.char_geometry) {
position = apply_gravity(win, &win->cfg.geometry);
win->w.width = win->cfg.geometry.r.width;
win->w.height = win->cfg.geometry.r.height;
} else /* Dummy size */
win->w.width = 640, win->w.height = 480;
get_plat(win)->wid = xcb_generate_id(con);
c = xcb_create_window_checked(con, TRUE_COLOR_ALPHA_DEPTH, get_plat(win)->wid, ctx.screen->root,
position.width, position.height, win->w.width, win->w.height, 0,
XCB_WINDOW_CLASS_INPUT_OUTPUT,
ctx.vis->visual_id, mask1, values1);
if (check_void_cookie(c)) return false;
get_plat(win)->gc = xcb_generate_id(con);
uint32_t mask2 = XCB_GC_FOREGROUND | XCB_GC_BACKGROUND | XCB_GC_GRAPHICS_EXPOSURES;
uint32_t values2[3] = { win->bg_premul, win->bg_premul, 0 };
c = xcb_create_gc_checked(con, get_plat(win)->gc, get_plat(win)->wid, mask2, values2);
if (check_void_cookie(c)) return false;
x11_reload_cursors(win);
return true;
}
static void x11_map_window(struct window *win) {
xcb_map_window(con, get_plat(win)->wid);
xcb_flush(con);
}
static void x11_free_window(struct window *win) {
unref_cursor(get_plat(win)->cursor);
unref_cursor(get_plat(win)->cursor_default);
unref_cursor(get_plat(win)->cursor_uri);
unref_cursor(get_plat(win)->cursor_user);
if (get_plat(win)->wid) {
xcb_unmap_window(con, get_plat(win)->wid);
ctx.renderer_free(win);
xcb_free_gc(con, get_plat(win)->gc);
xcb_destroy_window(con, get_plat(win)->wid);
xcb_flush(con);
}
}
static inline xcb_atom_t target_to_atom(enum clip_target target) {
switch (target) {
case clip_secondary: return XCB_ATOM_SECONDARY;
case clip_primary: return XCB_ATOM_PRIMARY;
default: return ctx.atom.CLIPBOARD;
}
}
#define CLIP_TIME_NOW 0
static bool x11_set_clip(struct window *win, enum clip_target target) {
xcb_set_selection_owner(con, get_plat(win)->wid, target_to_atom(target), CLIP_TIME_NOW);
xcb_get_selection_owner_cookie_t so = xcb_get_selection_owner_unchecked(con, target_to_atom(target));
xcb_get_selection_owner_reply_t *rep = xcb_get_selection_owner_reply(con, so, NULL);
bool res = rep && rep->owner == get_plat(win)->wid;
free(rep);
return res;
}
static void x11_paste(struct window *win, enum clip_target target) {
xcb_convert_selection(con, get_plat(win)->wid, target_to_atom(target),
term_is_utf8_enabled(win->term) ? ctx.atom.UTF8_STRING : XCB_ATOM_STRING, target_to_atom(target), XCB_CURRENT_TIME);
}
static void send_selection_data(struct window *win, xcb_window_t req, xcb_atom_t sel, xcb_atom_t target, xcb_atom_t prop, xcb_timestamp_t time) {
xcb_selection_notify_event_t ev;
ev.property = XCB_NONE;
ev.requestor = req;
ev.response_type = XCB_SELECTION_NOTIFY;
ev.selection = sel;
ev.target = target;
ev.time = time;
if (prop == XCB_NONE) prop = target;
if (target == ctx.atom.TARGETS) {
uint32_t data[] = {ctx.atom.TARGETS, ctx.atom.UTF8_STRING, XCB_ATOM_STRING};
ev.property = prop;
xcb_change_property(con, XCB_PROP_MODE_REPLACE, req, prop, XCB_ATOM_ATOM, 32, LEN(data), data);
} else if (target == ctx.atom.UTF8_STRING || target == XCB_ATOM_STRING) {
uint8_t *data = NULL;
if (sel == XCB_ATOM_PRIMARY) data = win->clipped[clip_primary];
else if (sel == XCB_ATOM_SECONDARY) data = win->clipped[clip_secondary];
else if (sel == ctx.atom.CLIPBOARD) data = term_is_keep_clipboard_enabled(win->term) ?
win->clipboard : win->clipped[clip_clipboard];
if (data) {
// TODO Implement INCR selection
// In order to do that we need to remember every ongoing INCR
// request and associate an offset and data being sent.
xcb_change_property(con, XCB_PROP_MODE_REPLACE, req, prop, target, 8, strlen((char *)data), data);
ev.property = prop;
}
}
xcb_send_event(con, 0, req, 0, (const char *)&ev);
}
static void receive_selection_data(struct window *win, xcb_atom_t prop, bool pnotify) {
if (prop == XCB_NONE) return;
size_t left, offset = 0;
do {
xcb_get_property_cookie_t pc = xcb_get_property(con, 0, get_plat(win)->wid, prop, XCB_GET_PROPERTY_TYPE_ANY, offset, PASTE_BLOCK_SIZE/4);
xcb_generic_error_t *err = NULL;
xcb_get_property_reply_t *rep = xcb_get_property_reply(con, pc, &err);
if (err || !rep) {
free(err);
free(rep);
return;
}
left = rep->bytes_after;
if (pnotify && !rep->value_len && !left) {
get_plat(win)->ev_mask &= ~XCB_EVENT_MASK_PROPERTY_CHANGE;
xcb_change_window_attributes(con, get_plat(win)->wid, XCB_CW_EVENT_MASK, &get_plat(win)->ev_mask);
}
if (rep->type == ctx.atom.INCR) {
get_plat(win)->ev_mask |= XCB_EVENT_MASK_PROPERTY_CHANGE;
xcb_change_window_attributes(con, get_plat(win)->wid, XCB_CW_EVENT_MASK, &get_plat(win)->ev_mask);
xcb_delete_property(con, get_plat(win)->wid, prop);
xcb_flush(con);
continue;
}
ssize_t size = rep->format * rep->value_len / 8;
if (size || offset || left) {
term_paste(win->term, xcb_get_property_value(rep), size,
rep->type == ctx.atom.UTF8_STRING, !offset, !left);
}
free(rep);
offset += size / 4;
} while (left > 0);
xcb_delete_property(con, get_plat(win)->wid, prop);
}
static bool x11_has_error(void) {
return xcb_connection_has_error(con);
}
static ssize_t x11_get_opaque_size(void) {
return sizeof(struct platform_window);
}
static void update_cursor(struct window *win) {
struct cursor *new = NULL;
if (get_plat(win)->cursor_user)
new = get_plat(win)->cursor_user;
else if (win->rcstate.active_uri != EMPTY_URI && !win->rcstate.uri_pressed)
new = get_plat(win)->cursor_uri;
else
new = get_plat(win)->cursor_default;
if (new != get_plat(win)->cursor)
select_cursor(win, new);
}
static bool do_handle_events(bool can_read) {
bool any = false;
for (xcb_generic_event_t *event, *nextev = NULL; nextev || (event = can_read ? xcb_poll_for_event(con) : xcb_poll_for_queued_event(con)); free(event)) {
any = true;
if (nextev) event = nextev, nextev = NULL;
switch (event->response_type &= 0x7F) {
struct window *win;
case XCB_EXPOSE: {
xcb_expose_event_t *ev = (xcb_expose_event_t *)event;
if (!(win = window_for_xid(ev->window))) break;
if (gconfig.trace_events) {
info("Event: event=Expose win=0x%x x=%x y=%d width=%d height=%d",
ev->window, ev->x, ev->y, ev->width, ev->height);
}
struct rect damage = {ev->x, ev->y, ev->width, ev->height}, win_rect = window_rect(win);
if (intersect_with(&damage, &win_rect))
pvtbl->update(win, damage);
break;
}
case XCB_CONFIGURE_NOTIFY: {
xcb_configure_notify_event_t *ev = (xcb_configure_notify_event_t *)event;
if (!(win = window_for_xid(ev->window))) break;
if (gconfig.trace_events) {
info("Event: event=ConfigureWindow win=0x%x x=%x y=%d width=%d"
" height=%d border=%d redir=%d above_win=0x%x event_win=0x%x",
ev->window, ev->x, ev->y, ev->width, ev->height, ev->border_width,
ev->override_redirect, ev->above_sibling, ev->event);
}
if (ev->width != win->w.width || ev->height != win->w.height)
handle_resize(win, ev->width, ev->height, false);
break;
}
case XCB_KEY_RELEASE: {
xcb_key_release_event_t *ev = (xcb_key_release_event_t *)event;
if (!(win = window_for_xid(ev->event))) break;
if (gconfig.trace_events) {
info("Event: event=KeyRelease win=0x%x keycode=0x%x", ev->event, ev->detail);
}
/* Skip key repeats if disabled */
if (!win->autorepeat && (nextev = xcb_poll_for_queued_event(con)) &&
(nextev->response_type &= 0x7F) == XCB_KEY_PRESS &&
event->full_sequence == nextev->full_sequence) {
free(nextev);
nextev = NULL;
}
break;
}
case XCB_KEY_PRESS: {
xcb_key_press_event_t *ev = (xcb_key_press_event_t *)event;
if (!(win = window_for_xid(ev->event))) break;
if (gconfig.trace_events) {
info("Event: event=KeyPress win=0x%x keycode=0x%x state=%x", ev->event, ev->detail, ev->state);
}
handle_keydown(win, ctx.xkb_state, ev->detail);
break;
}
case XCB_FOCUS_IN:
case XCB_FOCUS_OUT: {
xcb_focus_in_event_t *ev = (xcb_focus_in_event_t *)event;
if (!(win = window_for_xid(ev->event))) break;
if (gconfig.trace_events) {
info("Event: event=%s win=0x%x", ev->response_type == XCB_FOCUS_IN ?
"FocusIn" : "FocusOut", ev->event);
}
if (event->response_type == XCB_FOCUS_IN) {
xcb_query_pointer_cookie_t c = xcb_query_pointer(con, get_plat(win)->wid);
xcb_query_pointer_reply_t *qre = xcb_query_pointer_reply(con, c, NULL);
if (qre && (unsigned)qre->win_x < (unsigned)win->w.width &&
(unsigned)qre->win_y < (unsigned)win->w.height)
update_cursor(win);
free(qre);
}
handle_focus(win, event->response_type == XCB_FOCUS_IN);
break;
}
case XCB_BUTTON_RELEASE: /* All these events have same structure */
case XCB_BUTTON_PRESS:
case XCB_MOTION_NOTIFY: {
xcb_motion_notify_event_t *ev = (xcb_motion_notify_event_t *)event;
if (!(win = window_for_xid(ev->event))) break;
if (gconfig.trace_events) {
info("Event: event=%s mask=%d button=%d x=%d y=%d",
ev->response_type == XCB_BUTTON_PRESS ? "ButtonPress" :
ev->response_type == XCB_BUTTON_RELEASE ? "ButtonRelease" : "MotionNotify",
ev->state, ev->detail, ev->event_x, ev->event_y);
}
update_cursor(win);
mouse_handle_input(win->term, (struct mouse_event) {
/* XCB_BUTTON_PRESS -> mouse_event_press
* XCB_BUTTON_RELEASE -> mouse_event_release
* XCB_MOTION_NOTIFY -> mouse_event_motion */
.event = (ev->response_type & 0xF7) - 4,
.mask = ev->state & mask_state_mask,
.x = ev->event_x,
.y = ev->event_y,
.button = ev->detail - XCB_BUTTON_INDEX_1,
});
break;
}
case XCB_SELECTION_CLEAR: {
xcb_selection_clear_event_t *ev = (xcb_selection_clear_event_t *)event;
if (!(win = window_for_xid(ev->owner))) break;
if (gconfig.trace_events) {
info("Event: event=SelectionClear owner=0x%x selection=0x%x", ev->owner, ev->selection);
}
/* Clear even if set keep? */
screen_damage_selection(term_screen(win->term));
selection_clear(term_get_sstate(win->term));
break;
}
case XCB_PROPERTY_NOTIFY: {
xcb_property_notify_event_t *ev = (xcb_property_notify_event_t *)event;
if (!(win = window_for_xid(ev->window))) break;