-
-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathvmside.c
2623 lines (2387 loc) · 91.3 KB
/
vmside.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
/*
* The Qubes OS Project, http://www.qubes-os.org
*
* Copyright (C) 2010 Rafal Wojtczuk <[email protected]>
* Copyright (C) 2010 Joanna Rutkowska <[email protected]>
*
* 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 2
* 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, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
#include <assert.h>
#include <errno.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h>
#include <signal.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <grp.h>
#include <err.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/extensions/Xcomposite.h>
#include <X11/extensions/Xdamage.h>
#include <X11/XKBlib.h>
#include <X11/Xatom.h>
#include <X11/cursorfont.h>
#include <qubes-gui-protocol.h>
#include <qubes-xorg-tray-defs.h>
#include "xdriver-shm-cmd.h"
#include "txrx.h"
#include "list.h"
#include "error.h"
#include "encoding.h"
#include <libvchan.h>
#include <poll.h>
#include <linux/input.h>
#include <linux/uinput.h>
#include <time.h>
/* Time in milliseconds after which the clipboard data should be wiped */
#define CLIPBOARD_WIPE_TIME 60000
/* Get the size of an array. Error out on pointers. */
#define QUBES_ARRAY_SIZE(x) (0 * sizeof(struct { \
uint8_t tried_to_compute_number_of_array_elements_in_a_pointer: \
8 - 16*__builtin_types_compatible_p(__typeof__(x), __typeof__(&((x)[0]))); \
}) + sizeof(x)/sizeof((x)[0]))
#define SOCKET_ADDRESS "/var/run/xf86-qubes-socket"
/* Supported protocol version */
#define PROTOCOL_VERSION_MAJOR 1
#define PROTOCOL_VERSION_MINOR 8
#define PROTOCOL_VERSION (PROTOCOL_VERSION_MAJOR << 16 | PROTOCOL_VERSION_MINOR)
#if !(PROTOCOL_VERSION_MAJOR == QUBES_GUID_PROTOCOL_VERSION_MAJOR && \
PROTOCOL_VERSION_MINOR <= QUBES_GUID_PROTOCOL_VERSION_MINOR)
# error Incompatible qubes-gui-protocol.h.
#endif
#ifdef __GNUC__
# define UNUSED(x) UNUSED_ ## x __attribute__((__unused__))
#else
# define UNUSED(x) UNUSED_ ## x
#endif
static int damage_event, damage_error;
static int xfixes_event, xfixes_error;
/* from gui-common/error.c */
extern int print_x11_errors;
static char **saved_argv;
typedef struct {
Display *display;
int screen; /* shortcut to the default screen */
Window root_win; /* root attributes */
GC context;
Atom wmDeleteWindow; /* Atom: WM_DELETE_WINDOW */
Atom wmProtocols; /* Atom: WM_PROTOCOLS */
Atom wm_hints; /* Atom: WM_HINTS */
Atom wm_class; /* Atom: WM_CLASS */
Atom tray_selection; /* Atom: _NET_SYSTEM_TRAY_SELECTION_S<creen number> */
Atom tray_opcode; /* Atom: _NET_SYSTEM_TRAY_OPCODE */
Atom xembed_info; /* Atom: _XEMBED_INFO */
Atom utf8_string_atom; /* Atom: UTF8_STRING */
Atom wm_state; /* Atom: WM_STATE */
Atom net_wm_state; /* Atom: _NET_WM_STATE */
Atom wm_state_fullscreen; /* Atom: _NET_WM_STATE_FULLSCREEN */
Atom wm_state_demands_attention; /* Atom: _NET_WM_STATE_DEMANDS_ATTENTION */
Atom wm_take_focus; /* Atom: WM_TAKE_FOCUS */
Atom net_wm_name; /* Atom: _NET_WM_NAME */
Atom wm_normal_hints; /* Atom: WM_NORMAL_HINTS */
Atom clipboard; /* Atom: CLIPBOARD */
Atom targets; /* Atom: TARGETS */
Atom qprop; /* Atom: QUBES_SELECTION */
Atom compound_text; /* Atom: COMPOUND_TEXT */
Atom xembed; /* Atom: _XEMBED */
int xserver_fd;
int xserver_listen_fd;
libvchan_t *vchan;
Window stub_win; /* window for clipboard operations and to simulate LeaveNotify events */
unsigned char *clipboard_data;
unsigned int clipboard_data_len;
Time clipboard_last_access;
bool clipboard_wipe;
int log_level;
int sync_all_modifiers;
int composite_redirect_automatic;
pid_t x_pid;
uint32_t domid;
uint32_t protocol_version;
Time time;
int uinput_fd;
int created_input_device;
uint8_t last_known_modifier_states;
} Ghandles;
struct window_data {
int is_docked; /* is it docked icon window */
XID embeder; /* for docked icon points embeder window */
int input_hint; /* the window should get input focus - False=Never */
int support_delete_window;
int support_take_focus;
int window_dump_pending; /* send MSG_WINDOW_DUMP at next damage notification */
};
struct embeder_data {
XID icon_window;
};
static struct genlist *windows_list;
static struct genlist *embeder_list;
static Ghandles *ghandles_for_vchan_reinitialize;
#define SKIP_NONMANAGED_WINDOW do { \
if (!list_lookup(windows_list, window)) { \
if (g->log_level > 0) \
fprintf(stderr, "Skipping unmanaged window 0x%x\n", \
(int) window); \
return; \
} \
} while (0)
/* Cursor name translation. See X11/cursorfont.h. */
struct supported_cursor {
const char *name;
uint32_t cursor_id;
};
static struct supported_cursor supported_cursors[] = {
/* Names as defined by Xlib. Most programs will use these. */
{ "X_cursor", XC_X_cursor },
{ "arrow", XC_arrow },
{ "based_arrow_down", XC_based_arrow_down },
{ "based_arrow_up", XC_based_arrow_up },
{ "boat", XC_boat },
{ "bogosity", XC_bogosity },
{ "bottom_left_corner", XC_bottom_left_corner },
{ "bottom_right_corner", XC_bottom_right_corner },
{ "bottom_side", XC_bottom_side },
{ "bottom_tee", XC_bottom_tee },
{ "box_spiral", XC_box_spiral },
{ "center_ptr", XC_center_ptr },
{ "circle", XC_circle },
{ "clock", XC_clock },
{ "coffee_mug", XC_coffee_mug },
{ "cross", XC_cross },
{ "cross_reverse", XC_cross_reverse },
{ "crosshair", XC_crosshair },
{ "diamond_cross", XC_diamond_cross },
{ "dot", XC_dot },
{ "dotbox", XC_dotbox },
{ "double_arrow", XC_double_arrow },
{ "draft_large", XC_draft_large },
{ "draft_small", XC_draft_small },
{ "draped_box", XC_draped_box },
{ "exchange", XC_exchange },
{ "fleur", XC_fleur },
{ "gobbler", XC_gobbler },
{ "gumby", XC_gumby },
{ "hand1", XC_hand1 },
{ "hand2", XC_hand2 },
{ "heart", XC_heart },
{ "icon", XC_icon },
{ "iron_cross", XC_iron_cross },
{ "left_ptr", XC_left_ptr },
{ "left_side", XC_left_side },
{ "left_tee", XC_left_tee },
{ "leftbutton", XC_leftbutton },
{ "ll_angle", XC_ll_angle },
{ "lr_angle", XC_lr_angle },
{ "man", XC_man },
{ "middlebutton", XC_middlebutton },
{ "mouse", XC_mouse },
{ "pencil", XC_pencil },
{ "pirate", XC_pirate },
{ "plus", XC_plus },
{ "question_arrow", XC_question_arrow },
{ "right_ptr", XC_right_ptr },
{ "right_side", XC_right_side },
{ "right_tee", XC_right_tee },
{ "rightbutton", XC_rightbutton },
{ "rtl_logo", XC_rtl_logo },
{ "sailboat", XC_sailboat },
{ "sb_down_arrow", XC_sb_down_arrow },
{ "sb_h_double_arrow", XC_sb_h_double_arrow },
{ "sb_left_arrow", XC_sb_left_arrow },
{ "sb_right_arrow", XC_sb_right_arrow },
{ "sb_up_arrow", XC_sb_up_arrow },
{ "sb_v_double_arrow", XC_sb_v_double_arrow },
{ "shuttle", XC_shuttle },
{ "sizing", XC_sizing },
{ "spider", XC_spider },
{ "spraycan", XC_spraycan },
{ "star", XC_star },
{ "target", XC_target },
{ "tcross", XC_tcross },
{ "top_left_arrow", XC_top_left_arrow },
{ "top_left_corner", XC_top_left_corner },
{ "top_right_corner", XC_top_right_corner },
{ "top_side", XC_top_side },
{ "top_tee", XC_top_tee },
{ "trek", XC_trek },
{ "ul_angle", XC_ul_angle },
{ "umbrella", XC_umbrella },
{ "ur_angle", XC_ur_angle },
{ "watch", XC_watch },
{ "xterm", XC_xterm },
/* Chromium (and derived projects) use different names.
* See: https://github.com/chromium/chromium/blob/ccd149af47315e4c6f2fc45d55be1b271f39062c/ui/base/cursor/cursor_loader_x11.cc#L25
*/
{ "pointer", XC_hand2 },
{ "progress", XC_watch },
{ "wait", XC_watch },
{ "cell", XC_plus },
{ "all-scroll", XC_fleur },
{ "v-scroll", XC_fleur },
{ "h-scroll", XC_fleur },
{ "crosshair", XC_cross },
{ "text", XC_xterm },
// { "not-allowed", x11::None },
{ "grabbing", XC_hand2 },
{ "col-resize", XC_sb_h_double_arrow },
{ "row-resize", XC_sb_v_double_arrow },
{ "n-resize", XC_top_side },
{ "e-resize", XC_right_side },
{ "s-resize", XC_bottom_side },
{ "w-resize", XC_left_side },
{ "ne-resize", XC_top_right_corner },
{ "nw-resize", XC_top_left_corner },
{ "se-resize", XC_bottom_right_corner },
{ "sw-resize", XC_bottom_left_corner },
{ "ew-resize", XC_sb_h_double_arrow },
{ "ns-resize", XC_sb_v_double_arrow },
// { "nesw-resize",x11::None},
// { "nwse-resize",x11::None},
{ "dnd-none", XC_hand2 },
{ "dnd-move", XC_hand2 },
{ "dnd-copy", XC_hand2 },
{ "dnd-link", XC_hand2 },
};
#define NUM_SUPPORTED_CURSORS (QUBES_ARRAY_SIZE(supported_cursors))
static int compare_supported_cursors(const void *a, const void *b) {
return strcmp(((const struct supported_cursor *)a)->name,
((const struct supported_cursor *)b)->name);
}
static void send_event(Ghandles * g, const struct input_event *iev) {
int status = write(g->uinput_fd, iev, sizeof(struct input_event));
if ( status < 0 ) {
if (g->log_level > 0) {
fprintf(stderr, "write failed, falling back to xdriver. TYPE:%d CODE:%d VALUE: %d WRITE ERROR CODE: %d\n", iev->type, iev->code, iev->value, status);
}
g->created_input_device = 0;
}
// send syn
const struct input_event syn = {.type = EV_SYN, .code = 0, .value = 0};
status = write(g->uinput_fd, &(syn), sizeof(struct input_event));
if ( status < 0 ) {
if (g->log_level > 0) {
fprintf(stderr, "writing SYN failed, falling back to xdriver. WRITE ERROR CODE: %d\n", status);
}
g->created_input_device = 0;
}
}
static void send_wmname(Ghandles * g, XID window);
static void send_wmnormalhints(Ghandles * g, XID window, int ignore_fail);
static void send_wmclass(Ghandles * g, XID window, int ignore_fail);
static void send_pixmap_grant_refs(Ghandles * g, XID window);
static void retrieve_wmhints(Ghandles * g, XID window, int ignore_fail);
static void retrieve_wmprotocols(Ghandles * g, XID window, int ignore_fail);
static void process_xevent_damage(Ghandles * g, XID window,
int x, int y, int width, int height)
{
struct msg_shmimage mx;
struct msg_hdr hdr;
struct genlist *l;
struct window_data *wd;
l = list_lookup(windows_list, window);
if (!l)
return;
wd = l->data;
if (wd->window_dump_pending) {
send_pixmap_grant_refs(g, window);
wd->window_dump_pending = False;
}
hdr.type = MSG_SHMIMAGE;
hdr.window = window;
mx.x = x;
mx.y = y;
mx.width = width;
mx.height = height;
write_message(g->vchan, hdr, mx);
}
static void send_cursor(Ghandles *g, XID window, uint32_t cursor)
{
struct msg_hdr hdr;
struct msg_cursor msg;
hdr.type = MSG_CURSOR;
hdr.window = window;
msg.cursor = cursor;
write_message(g->vchan, hdr, msg);
}
static uint32_t find_cursor(Ghandles *g, Atom atom)
{
char *name;
struct supported_cursor c;
struct supported_cursor *found;
if (atom == None)
return CURSOR_DEFAULT;
name = XGetAtomName(g->display, atom);
if (!name)
return CURSOR_DEFAULT;
c.name = name;
found = bsearch(&c, supported_cursors, NUM_SUPPORTED_CURSORS,
sizeof(supported_cursors[0]),
compare_supported_cursors);
XFree(name);
if (found) {
uint32_t cursor = CURSOR_X11 + found->cursor_id;
assert(cursor < CURSOR_X11_MAX);
return cursor;
}
return CURSOR_DEFAULT;
}
static void process_xevent_cursor(Ghandles *g, XFixesCursorNotifyEvent *ev)
{
if (ev->subtype == XFixesDisplayCursorNotify) {
/* The event from XFixes specifies only the root window, so we need to
* find out the window under pointer.
*/
Window root, window_under_pointer;
int root_x, root_y, win_x, win_y;
unsigned int mask;
Bool ret;
int cursor;
ret = XQueryPointer(g->display, ev->window, &root,
&window_under_pointer,
&root_x, &root_y, &win_x, &win_y, &mask);
if (!ret || window_under_pointer == None)
return;
if (!list_lookup(windows_list, window_under_pointer))
return;
cursor = find_cursor(g, ev->cursor_name);
send_cursor(g, window_under_pointer, cursor);
}
}
static void process_xevent_createnotify(Ghandles * g, XCreateWindowEvent * ev)
{
struct msg_hdr hdr;
struct msg_create crt;
struct window_data *wd;
XWindowAttributes attr;
int ret;
ret = XGetWindowAttributes(g->display, ev->window, &attr);
if (ret != 1) {
fprintf(stderr, "XGetWindowAttributes for 0x%x failed in "
"handle_create, ret=0x%x\n", (int) ev->window,
ret);
return;
}
if (g->log_level > 0)
fprintf(stderr, "Create for 0x%x class 0x%x\n",
(int) ev->window, attr.class);
if (list_lookup(windows_list, ev->window)) {
fprintf(stderr, "CREATE for already existing 0x%x\n",
(int) ev->window);
return;
}
if (ev->parent != g->root_win) {
/* GUI daemon no longer supports this */
fprintf(stderr,
"CREATE with non-root parent window 0x%x\n",
(unsigned int)ev->parent);
return;
}
if (list_lookup(embeder_list, ev->window)) {
/* ignore CreateNotify for embeder window */
if (g->log_level > 1)
fprintf(stderr, "CREATE for embeder 0x%x\n",
(int) ev->window);
return;
}
/* Initialize window_data structure */
wd = (struct window_data*)malloc(sizeof(struct window_data));
if (!wd) {
fprintf(stderr, "OUT OF MEMORY\n");
return;
}
/* Default values for window_data. By default, window should receive InputFocus events */
wd->is_docked = False;
wd->input_hint = True;
wd->support_delete_window = False;
wd->support_take_focus = False;
wd->window_dump_pending = False;
list_insert(windows_list, ev->window, wd);
if (attr.border_width > 0) {
XSetWindowBorderWidth(g->display, ev->window, 0);
}
if (attr.class != InputOnly)
XDamageCreate(g->display, ev->window,
XDamageReportRawRectangles);
// the following hopefully avoids missed damage events
XSync(g->display, False);
XSelectInput(g->display, ev->window, PropertyChangeMask);
hdr.type = MSG_CREATE;
hdr.window = ev->window;
crt.width = ev->width;
crt.height = ev->height;
crt.parent = ev->parent;
crt.x = ev->x;
crt.y = ev->y;
crt.override_redirect = ev->override_redirect;
write_message(g->vchan, hdr, crt);
/* handle properties set before we process XCreateNotify */
send_wmnormalhints(g, hdr.window, 1);
send_wmname(g, hdr.window);
send_wmclass(g, hdr.window, 1);
retrieve_wmprotocols(g, hdr.window, 1);
retrieve_wmhints(g, hdr.window, 1);
}
static void feed_xdriver(Ghandles * g, int type, int arg1, int arg2)
{
char ans;
ssize_t ret;
struct xdriver_cmd cmd;
cmd.type = type;
cmd.arg1 = arg1;
cmd.arg2 = arg2;
if (write(g->xserver_fd, &cmd, sizeof(cmd)) != sizeof(cmd))
err(1, "unix write");
ans = '1';
ret = read(g->xserver_fd, &ans, 1);
if (ret != 1 || ans != '0') {
perror("unix read");
err(1, "read returned %zd, char read=0x%x\n", ret, (int) ans);
}
}
void send_pixmap_grant_refs(Ghandles * g, XID window)
{
struct msg_hdr hdr;
uint8_t *wd_msg_buf;
size_t wd_msg_len;
size_t rcvd;
int ret;
feed_xdriver(g, 'W', (int) window, 0);
if (read(g->xserver_fd, &wd_msg_len, sizeof(wd_msg_len)) != sizeof(wd_msg_len))
err(1, "unix read wd_msg_len");
if (wd_msg_len == 0) {
fprintf(stderr, "Failed to get window dump for window 0x%lx\n",
window);
return;
}
wd_msg_buf = malloc(wd_msg_len);
if (!wd_msg_buf) {
fprintf(stderr, "Failed to allocate memory for window dump 0x%lx\n",
window);
exit(1);
}
rcvd = 0;
while (rcvd < wd_msg_len) {
ret = read(g->xserver_fd, wd_msg_buf + rcvd, wd_msg_len - rcvd);
if (ret == 0)
errx(1, "unix read EOF");
if (ret < 0)
err(1, "unix read error");
rcvd += ret;
}
hdr.type = MSG_WINDOW_DUMP;
hdr.window = window;
hdr.untrusted_len = wd_msg_len;
write_struct(g->vchan, hdr);
write_data(g->vchan, (char *) wd_msg_buf, wd_msg_len);
free(wd_msg_buf);
if (g->protocol_version < QUBES_GUID_MIN_MSG_WINDOW_DUMP_ACK)
feed_xdriver(g, 'a', 0, 0);
}
/* return 1 on success, 0 otherwise */
static int get_net_wmname(Ghandles * g, XID window, char *outbuf, size_t bufsize) {
Atom type_return;
int format_return;
unsigned long bytes_after_return, items_return;
unsigned char *property_data;
if (XGetWindowProperty(g->display, window, g->net_wm_name,
0, /* offset, in 32-bit quantities */
bufsize/4, /* length, in 32-bit quantities */
False, /* delete */
g->utf8_string_atom, /* req_type */
&type_return, /* actual_type_return */
&format_return, /* actual_format_return */
&items_return, /* nitems_return */
&bytes_after_return, /* bytes_after_return */
&property_data /* prop_return */
) == Success) {
if (type_return != g->utf8_string_atom) {
if (g->log_level > 0)
fprintf(stderr, "get_net_wmname(0x%lx): unexpected property type: 0x%lx\n",
window, type_return);
/* property_data not filled in this case */
return 0;
}
if (format_return != 8) {
if (g->log_level > 0)
fprintf(stderr, "get_net_wmname(0x%lx): unexpected format: %d\n",
window, format_return);
XFree(property_data);
return 0;
}
if (bytes_after_return > 0) {
if (g->log_level > 0)
fprintf(stderr, "get_net_wmname(0x%lx): window title too long, %ld bytes truncated\n",
window, bytes_after_return);
}
if (items_return > bufsize) {
if (g->log_level > 0)
fprintf(stderr, "get_net_wmname(0x%lx): too much data returned (%ld), bug?\n",
window, items_return);
XFree(property_data);
return 0;
}
memcpy(outbuf, property_data, items_return);
/* make sure there is trailing \0 */
outbuf[bufsize-1] = 0;
XFree(property_data);
if (g->log_level > 0)
fprintf(stderr, "got net_wm_name=%s\n", outbuf);
return 1;
}
if (g->log_level > 1)
fprintf(stderr, "window %lx has no _NET_WM_NAME\n", window);
return 0;
}
/* return 1 on success, 0 otherwise */
static int getwmname_tochar(Ghandles * g, XID window, char *outbuf, int bufsize)
{
XTextProperty text_prop_return;
char **list;
int count;
outbuf[0] = 0;
if (!XGetWMName(g->display, window, &text_prop_return) ||
!text_prop_return.value || !text_prop_return.nitems)
return 0;
if (Xutf8TextPropertyToTextList(g->display,
&text_prop_return, &list,
&count) < 0 || count <= 0
|| !*list) {
XFree(text_prop_return.value);
return 0;
}
strncat(outbuf, list[0], bufsize - 1);
XFree(text_prop_return.value);
XFreeStringList(list);
if (g->log_level > 0)
fprintf(stderr, "got wmname=%s\n", outbuf);
return 1;
}
void send_wmname(Ghandles * g, XID window)
{
struct msg_hdr hdr;
struct msg_wmname msg;
memset(&msg, 0, sizeof(msg));
/* try _NET_WM_NAME, then fallback to WM_NAME */
if (!get_net_wmname(g, window, msg.data, sizeof(msg.data)))
if (!getwmname_tochar(g, window, msg.data, sizeof(msg.data)))
return;
hdr.window = window;
hdr.type = MSG_WMNAME;
write_message(g->vchan, hdr, msg);
}
/* Retrieve the supported WM Protocols
We don't forward the info to dom0 as we only need specific client protocols
*/
void retrieve_wmprotocols(Ghandles * g, XID window, int ignore_fail)
{
int nitems;
Atom *supported_protocols;
int i;
struct genlist *l;
if (!((l=list_lookup(windows_list, window)) && (l->data))) {
fprintf(stderr, "ERROR retrieve_wmprotocols: Window 0x%x data not initialized\n", (int)window);
return;
}
if (XGetWMProtocols(g->display, window, &supported_protocols, &nitems) == 1) {
for (i=0; i < nitems; i++) {
if (supported_protocols[i] == g->wm_take_focus) {
if (g->log_level > 1)
fprintf(stderr, "Protocol take_focus supported for Window 0x%x\n", (int)window);
((struct window_data*)l->data)->support_take_focus = True;
} else if (supported_protocols[i] == g->wmDeleteWindow) {
if (g->log_level > 1)
fprintf(stderr, "Protocol delete_window supported for Window 0x%x\n", (int)window);
((struct window_data*)l->data)->support_delete_window = True;
}
}
} else {
if (!ignore_fail)
fprintf(stderr, "ERROR reading WM_PROTOCOLS\n");
return;
}
XFree(supported_protocols);
}
/* Retrieve the 'real' WMHints.
We don't forward the info to dom0 as we only need InputHint and dom0 doesn't care about it
*/
void retrieve_wmhints(Ghandles * g, XID window, int ignore_fail)
{
XWMHints *wm_hints;
struct genlist *l;
if (!((l=list_lookup(windows_list, window)) && (l->data))) {
fprintf(stderr, "ERROR retrieve_wmhints: Window 0x%x data not initialized\n", (int)window);
return;
}
if (!(wm_hints = XGetWMHints(g->display, window))) {
if (!ignore_fail)
fprintf(stderr, "ERROR reading WM_HINTS\n");
return;
}
if (wm_hints->flags & InputHint) {
((struct window_data*)l->data)->input_hint = wm_hints->input;
if (g->log_level > 1)
fprintf(stderr, "Received input hint 0x%x for Window 0x%x\n", wm_hints->input, (int)window);
} else {
// Default value
if (g->log_level > 1)
fprintf(stderr, "Received WMHints without input hint set for Window 0x%x\n", (int)window);
((struct window_data*)l->data)->input_hint = True;
}
XFree(wm_hints);
}
void send_wmnormalhints(Ghandles * g, XID window, int ignore_fail)
{
struct msg_hdr hdr;
struct msg_window_hints msg;
XSizeHints size_hints;
long supplied_hints;
if (!XGetWMNormalHints
(g->display, window, &size_hints, &supplied_hints)) {
if (!ignore_fail)
fprintf(stderr, "error reading WM_NORMAL_HINTS\n");
return;
}
/* Nasty workaround for KDE bug affecting gnome-terminal (shrinks to minimal size) */
/* https://bugzilla.redhat.com/show_bug.cgi?id=707664 */
if ((size_hints.flags & (PBaseSize|PMinSize|PResizeInc)) ==
(PBaseSize|PMinSize|PResizeInc)) {
/* KDE incorrectly uses PMinSize when both are provided */
if (size_hints.width_inc > 1)
/* round up to neareset multiply of width_inc */
size_hints.min_width =
((size_hints.min_width-size_hints.base_width+1) / size_hints.width_inc)
* size_hints.width_inc + size_hints.base_width;
if (size_hints.height_inc > 1)
/* round up to neareset multiply of height_inc */
size_hints.min_height =
((size_hints.min_height-size_hints.base_height+1) / size_hints.height_inc)
* size_hints.height_inc + size_hints.base_height;
}
// pass only some hints
msg.flags =
size_hints.flags & (USPosition | PPosition | PMinSize | PMaxSize |
PResizeInc | PBaseSize);
msg.min_width = size_hints.min_width;
msg.min_height = size_hints.min_height;
msg.max_width = size_hints.max_width;
msg.max_height = size_hints.max_height;
msg.width_inc = size_hints.width_inc;
msg.height_inc = size_hints.height_inc;
msg.base_width = size_hints.base_width;
msg.base_height = size_hints.base_height;
hdr.window = window;
hdr.type = MSG_WINDOW_HINTS;
write_message(g->vchan, hdr, msg);
}
void send_wmclass(Ghandles * g, XID window, int ignore_fail)
{
struct msg_hdr hdr;
struct msg_wmclass msg;
XClassHint class_hint;
if (!XGetClassHint(g->display, window, &class_hint)) {
if (!ignore_fail)
fprintf(stderr, "error reading WM_CLASS\n");
return;
}
strncpy(msg.res_class, class_hint.res_class, sizeof(msg.res_class)-1);
msg.res_class[sizeof(msg.res_class)-1] = '\0';
strncpy(msg.res_name, class_hint.res_name, sizeof(msg.res_name)-1);
msg.res_name[sizeof(msg.res_name)-1] = '\0';
XFree(class_hint.res_class);
XFree(class_hint.res_name);
hdr.window = window;
hdr.type = MSG_WMCLASS;
write_message(g->vchan, hdr, msg);
}
static inline uint32_t flags_from_atom(Ghandles * g, Atom a) {
if (a == g->wm_state_fullscreen)
return WINDOW_FLAG_FULLSCREEN;
else if (a == g->wm_state_demands_attention)
return WINDOW_FLAG_DEMANDS_ATTENTION;
else {
/* ignore unsupported states */
}
return 0;
}
static void send_window_state(Ghandles * g, XID window)
{
int ret;
unsigned i;
Atom *state_list;
Atom act_type;
int act_fmt;
unsigned long nitems, bytesleft;
struct msg_hdr hdr;
struct msg_window_flags flags;
/* FIXME: only first 10 elements are parsed */
ret = XGetWindowProperty(g->display, window, g->net_wm_state, 0, 10,
False, XA_ATOM, &act_type, &act_fmt, &nitems, &bytesleft, (unsigned char**)&state_list);
if (ret != Success)
return;
flags.flags_set = 0;
flags.flags_unset = 0;
for (i=0; i < nitems; i++) {
flags.flags_set |= flags_from_atom(g, state_list[i]);
}
hdr.window = window;
hdr.type = MSG_WINDOW_FLAGS;
write_message(g->vchan, hdr, flags);
XFree(state_list);
}
static void process_xevent_map(Ghandles * g, XID window)
{
XWindowAttributes attr;
long new_wm_state[2];
struct msg_hdr hdr;
struct msg_map_info map_info;
Window transient;
struct window_data *wd;
SKIP_NONMANAGED_WINDOW;
wd = list_lookup(windows_list, window)->data;
if (g->log_level > 1)
fprintf(stderr, "MAP for window 0x%x\n", (int)window);
wd->window_dump_pending = True;
send_window_state(g, window);
XGetWindowAttributes(g->display, window, &attr);
if (XGetTransientForHint(g->display, window, &transient))
map_info.transient_for = transient;
else
map_info.transient_for = 0;
map_info.override_redirect = attr.override_redirect;
hdr.type = MSG_MAP;
hdr.window = window;
write_message(g->vchan, hdr, map_info);
send_wmname(g, window);
if (!attr.override_redirect) {
/* WM_STATE is always set to normal */
new_wm_state[0] = NormalState; /* state */
new_wm_state[1] = None; /* icon */
XChangeProperty(g->display, window, g->wm_state, g->wm_state, 32, PropModeReplace, (unsigned char *)new_wm_state, 2);
}
}
static void process_xevent_unmap(Ghandles * g, XID window)
{
struct msg_hdr hdr;
SKIP_NONMANAGED_WINDOW;
if (g->log_level > 1)
fprintf(stderr, "UNMAP for window 0x%x\n", (int)window);
hdr.type = MSG_UNMAP;
hdr.window = window;
hdr.untrusted_len = 0;
write_struct(g->vchan, hdr);
XDeleteProperty(g->display, window, g->wm_state);
XDeleteProperty(g->display, window, g->net_wm_state);
}
static void process_xevent_destroy(Ghandles * g, XID window)
{
struct msg_hdr hdr;
struct genlist *l;
/* embeders are not manged windows, so must be handled before SKIP_NONMANAGED_WINDOW */
if ((l = list_lookup(embeder_list, window))) {
if (l->data) {
free(l->data);
}
list_remove(l);
}
SKIP_NONMANAGED_WINDOW;
if (g->log_level > 0)
fprintf(stderr, "handle destroy 0x%x\n", (int) window);
hdr.type = MSG_DESTROY;
hdr.window = window;
hdr.untrusted_len = 0;
write_struct(g->vchan, hdr);
l = list_lookup(windows_list, window);
if (l->data) {
if (((struct window_data*)l->data)->is_docked) {
XDestroyWindow(g->display, ((struct window_data*)l->data)->embeder);
}
free(l->data);
}
list_remove(l);
}
static void process_xevent_configure(Ghandles * g, XID window,
XConfigureEvent * ev)
{
struct msg_hdr hdr;
struct msg_configure conf;
struct genlist *l;
/* SKIP_NONMANAGED_WINDOW; */
if (!(l=list_lookup(windows_list, window))) {
/* if not real managed window, check if this is embeder for another window */
struct genlist *e;
if ((e=list_lookup(embeder_list, window))) {
window = ((struct embeder_data*)e->data)->icon_window;
if (!list_lookup(windows_list, window))
/* probably icon window have just destroyed, so ignore message */
/* "l" not updated intentionally - when configure notify comes
* from the embeder, it should be passed to dom0 (in most cases as
* ACK for earlier configure request) */
return;
} else {
/* ignore not managed windows */
return;
}
}
if (g->log_level > 1)
fprintf(stderr,
"handle configure event 0x%x w=%d h=%d ovr=%d\n",
(int) window, ev->width, ev->height,
(int) ev->override_redirect);
if (l && l->data && ((struct window_data*)l->data)->is_docked) {
/* for docked icon, ensure that it fills embeder window; don't send any
* message to dom0 - it will be done for embeder itself*/
XWindowAttributes attr;
int ret;
ret = XGetWindowAttributes(g->display, ((struct window_data*)l->data)->embeder, &attr);
if (ret != 1) {
fprintf(stderr,
"XGetWindowAttributes for 0x%x failed in "
"handle_xevent_configure, ret=0x%x\n", (int) ((struct window_data*)l->data)->embeder, ret);
return;
}
if (ev->x != 0 || ev->y != 0 || ev->width != attr.width || ev->height != attr.height) {
XMoveResizeWindow(g->display, window, 0, 0, attr.width, attr.height);
}
return;
}
if (ev->border_width > 0) {
XSetWindowBorderWidth(g->display, window, 0);
}
hdr.type = MSG_CONFIGURE;
hdr.window = window;
conf.x = ev->x;
conf.y = ev->y;
conf.width = ev->width;
conf.height = ev->height;
conf.override_redirect = ev->override_redirect;
write_message(g->vchan, hdr, conf);
send_pixmap_grant_refs(g, window);
}
static void send_clipboard_data(libvchan_t *vchan, XID window, char *data, uint32_t len, int protocol_version)
{
struct msg_hdr hdr;
hdr.type = MSG_CLIPBOARD_DATA;
hdr.window = window;
if ((protocol_version < QUBES_GUID_MIN_CLIPBOARD_4X) && (len > MAX_CLIPBOARD_SIZE)) {
// The dumb case. Truncate the data to the old size. User might lose
// some inter-vm clipboard data without being notified.
len = MAX_CLIPBOARD_SIZE;
} else if (len > MAX_CLIPBOARD_BUFFER_SIZE + 1) {
// xside is capable of receiving (up to) 4X of the previous size.
// it is also smarter. send one byte over the new buffer limit.
// A simple sign for xside to reject it.
len = MAX_CLIPBOARD_BUFFER_SIZE + 1;
}
hdr.untrusted_len = len;
write_struct(vchan, hdr);
write_data(vchan, (char *) data, len);