-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxinfo.c
1613 lines (1471 loc) · 57.1 KB
/
xinfo.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
/**
* xinfo - Dump information about the currently running X server instance.
*
* This is free and unencumbered software released into the public domain.
*
*/
#define _POSIX_C_SOURCE 200112L
#include <errno.h>
#include <limits.h>
#include <netdb.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/un.h>
#include <unistd.h>
#if defined(__GNUC__)
#define NORETURN __attribute__((__noreturn__))
#else
#define NORETURN
#endif
NORETURN static void die(const char *msg) {
fprintf(stderr, "FATAL ERROR: %s\n", msg);
exit(1);
}
static ssize_t read_n(int fd, void *buffer, size_t n) {
char *buf = buffer;
size_t total_read;
for (total_read = 0; total_read < n;) {
ssize_t num_read = read(fd, buf, n - total_read);
if (num_read == 0) /* EOF */
return total_read;
if (num_read == -1) {
if (errno == EINTR)
continue; /* We got interrupted, try again */
return -1;
}
total_read += num_read;
buf += num_read;
}
return total_read;
}
static ssize_t write_n(int fd, const void *buffer, size_t n) {
const char *buf = buffer;
size_t total_written;
for (total_written = 0; total_written < n;) {
ssize_t num_written = write(fd, buf, n - total_written);
if (num_written <= 0) {
if (num_written == -1 && errno == EINTR)
continue; /* We got interrupted, try again */
return -1;
}
total_written += num_written;
buf += num_written;
}
return total_written;
}
#define X_VERSION_MAJOR 11
#define X_VERSION_MINOR 0
#define X_BASE_TCP_PORT 6000
#define X_PADDING(x) ((4 - ((x) % 4)) % 4)
#define X_PAD(x) ((x) + X_PADDING((x)))
#define X_REPLY 1
#define X_CONNECTION_STATUS_SUCCESS 1
#define X_BYTE_ORDER_LITTLE_ENDIAN 0
#define X_BITMAP_FORMAT_BIT_ORDER_LEAST_SIGNIFICANT 0
#define X_BACKING_STORES_NEVER 0
#define X_BACKING_STORES_WHEN_MAPPED 1
#define X_EVENT_MASK_KEY_PRESS 0x00000001u
#define X_EVENT_MASK_KEY_RELEASE 0x00000002u
#define X_EVENT_MASK_BUTTON_PRESS 0x00000004u
#define X_EVENT_MASK_BUTTON_RELEASE 0x00000008u
#define X_EVENT_MASK_ENTER_WINDOW 0x00000010u
#define X_EVENT_MASK_LEAVE_WINDOW 0x00000020u
#define X_EVENT_MASK_POINTER_MOTION 0x00000040u
#define X_EVENT_MASK_POINTER_MOTION_HINT 0x00000080u
#define X_EVENT_MASK_BUTTON1_MOTION 0x00000100u
#define X_EVENT_MASK_BUTTON2_MOTION 0x00000200u
#define X_EVENT_MASK_BUTTON3_MOTION 0x00000400u
#define X_EVENT_MASK_BUTTON4_MOTION 0x00000800u
#define X_EVENT_MASK_BUTTON5_MOTION 0x00001000u
#define X_EVENT_MASK_BUTTON_MOTION 0x00002000u
#define X_EVENT_MASK_KEYMAP_STATE 0x00004000u
#define X_EVENT_MASK_EXPOSURE 0x00008000u
#define X_EVENT_MASK_VISIBILITY_CHANGE 0x00010000u
#define X_EVENT_MASK_STRUCTURE_NOTIFY 0x00020000u
#define X_EVENT_MASK_RESIZE_REDIRECT 0x00040000u
#define X_EVENT_MASK_SUBSTRUCTURE_NOTIFY 0x00080000u
#define X_EVENT_MASK_SUBSTRUCTURE_REDIRECT 0x00100000u
#define X_EVENT_MASK_FOCUS_CHANGE 0x00200000u
#define X_EVENT_MASK_PROPERTY_CHANGE 0x00400000u
#define X_EVENT_MASK_COLORMAP_CHANGE 0x00800000u
#define X_EVENT_MASK_OWNER_GRAB_BUTTON 0x01000000u
#define X_OPCODE_GET_FONT_PATH 52
#define X_OPCODE_QUERY_EXTENSION 98
#define X_OPCODE_LIST_EXTENSIONS 99
struct x_setup_request {
uint8_t byte_order; /* Either 'B' for big endian, or 'l' for little endian */
uint8_t pad1;
uint16_t protocol_version_major;
uint16_t protocol_version_minor;
uint16_t auth_protocol_name_len;
uint16_t auth_data_len;
uint16_t pad2;
};
struct x_setup_response {
uint8_t status;
uint8_t failure_reason_length;
uint16_t protocol_version_major;
uint16_t protocol_version_minor;
uint16_t additional_data_len;
};
struct x_setup_data_impl {
uint32_t release_number;
uint32_t resource_id_base;
uint32_t resource_id_mask;
uint32_t motion_buffer_size;
uint16_t vendor_length;
uint16_t maximum_request_len;
uint8_t num_roots;
uint8_t num_pixmap_formats;
uint8_t image_byte_order;
uint8_t bitmap_format_bit_order;
uint8_t bitmap_format_scanline_unit;
uint8_t bitmap_format_scanline_pad;
uint8_t min_keycode;
uint8_t max_keycode;
uint32_t pad;
};
struct x_format {
uint8_t depth;
uint8_t bits_per_pixel;
uint8_t scanline_pad;
uint8_t pad[5];
};
struct x_screen_data {
uint32_t root;
uint32_t default_colormap;
uint32_t white_pixel;
uint32_t black_pixel;
uint32_t current_input_mask;
uint16_t width_in_pixels;
uint16_t height_in_pixels;
uint16_t width_in_millimeters;
uint16_t height_in_millimeters;
uint16_t min_installed_maps;
uint16_t max_installed_maps;
uint32_t root_visual_id;
uint8_t backing_stores;
uint8_t save_unders;
uint8_t root_depth;
uint8_t num_allowed_depths;
};
struct x_depth_data {
uint8_t depth;
uint8_t pad1;
uint16_t num_visuals;
uint32_t pad2;
};
struct x_visual_type {
uint32_t visual_id;
uint8_t visual_class;
uint8_t bits_per_rgb_value;
uint16_t colormap_entries;
uint32_t red_mask;
uint32_t green_mask;
uint32_t blue_mask;
uint32_t pad;
};
struct x_depth {
struct x_depth_data data;
struct x_visual_type *visuals;
};
struct x_screen {
struct x_screen_data data;
struct x_depth *allowed_depths;
};
struct x_setup_data {
char *vendor_name;
struct x_format *pixmap_formats;
struct x_screen *roots;
struct x_setup_data_impl data;
uint16_t version_major;
uint16_t version_minor;
};
static struct {
struct x_setup_data setup_data;
int fd;
} x_connection = {
.fd = -1,
};
struct x_get_font_path_request {
uint8_t opcode;
uint8_t pad;
uint16_t request_len;
};
struct x_get_font_path_reply {
uint8_t status;
uint8_t pad1;
uint16_t sequence_number;
uint32_t data_len;
uint16_t num_strings;
uint8_t pad[22];
};
struct x_list_extensions_request {
uint8_t opcode;
uint8_t pad;
uint16_t request_len;
};
struct x_list_extensions_reply {
uint8_t status;
uint8_t num_names;
uint16_t sequence_number;
uint32_t data_len;
uint8_t pad[24];
};
struct x_query_extension_request {
uint8_t opcode;
uint8_t pad1;
uint16_t request_len;
uint16_t name_len;
uint16_t pad2;
};
struct x_query_extension_reply {
uint8_t status;
uint8_t pad1;
uint16_t sequence_number;
uint32_t data_len;
uint8_t present;
uint8_t major_opcode;
uint8_t first_event;
uint8_t first_error;
uint8_t pad[20];
};
static void x_disconnect(void) {
if (x_connection.fd != -1) {
close(x_connection.fd);
x_connection.fd = -1;
}
if (x_connection.setup_data.roots) {
for (size_t i = 0; i < x_connection.setup_data.data.num_roots; ++i) {
if (x_connection.setup_data.roots[i].allowed_depths) {
for (size_t j = 0;
j < x_connection.setup_data.roots[i].data.num_allowed_depths; ++j)
free(x_connection.setup_data.roots[i].allowed_depths[j].visuals);
free(x_connection.setup_data.roots[i].allowed_depths);
}
}
free(x_connection.setup_data.roots);
}
free(x_connection.setup_data.pixmap_formats);
free(x_connection.setup_data.vendor_name);
}
static void x_connect_to_socket(int fd, size_t protocol_name_len,
const char *protocol_name, size_t auth_data_len,
const char *auth_data) {
x_connection.fd = fd;
/* Build the connection request to send to the X server */
struct x_setup_request setup_request = {
.byte_order = 'l', /* little endian */
.protocol_version_major = X_VERSION_MAJOR,
.protocol_version_minor = X_VERSION_MINOR,
.auth_protocol_name_len = protocol_name_len,
.auth_data_len = auth_data_len,
};
size_t protocol_len = X_PAD(protocol_name_len);
size_t data_len = X_PAD(auth_data_len);
size_t total_request_size = sizeof(setup_request) + protocol_len + data_len;
/* Put all the required data in a single buffer instead of needlessly calling
* write multiple times for the initial data and then for the authentication
* info */
char *request_buffer = calloc(total_request_size, 1);
if (!request_buffer) {
x_disconnect();
die("Memory allocation failed");
}
memcpy(request_buffer, &setup_request, sizeof(setup_request));
memcpy(request_buffer + sizeof(setup_request), protocol_name,
protocol_name_len);
memcpy(request_buffer + sizeof(setup_request) + protocol_len, auth_data,
auth_data_len);
/* Send the connection request to the server */
ssize_t num_written =
write_n(x_connection.fd, request_buffer, total_request_size);
free(request_buffer);
if (num_written != (ssize_t)total_request_size)
die("Failed to send connection request to X server");
/* Read the connection setup response */
struct x_setup_response response = {0};
ssize_t num_read = read_n(x_connection.fd, &response, sizeof(response));
if (num_read != sizeof(response)) {
x_disconnect();
die("Failed to read connection setup response from X server");
}
/* Read additional data */
size_t additional_data_len = 4 * response.additional_data_len;
char *additional_data = calloc(additional_data_len, 1);
if (!additional_data)
die("Memory allocation failed");
num_read = read_n(x_connection.fd, additional_data, additional_data_len);
if (num_read != (ssize_t)additional_data_len) {
free(additional_data);
x_disconnect();
die("Failed to read additional connection information from X server");
}
if (response.status != X_CONNECTION_STATUS_SUCCESS) {
fprintf(stderr, "ERROR: %s\n", additional_data);
free(additional_data);
x_disconnect();
die("Connection to X server failed");
}
x_connection.setup_data.version_major = response.protocol_version_major;
x_connection.setup_data.version_minor = response.protocol_version_minor;
/* Read the setup data header and the vendor name string */
const char *curr_data = additional_data;
memcpy(&x_connection.setup_data.data, curr_data,
sizeof(x_connection.setup_data.data));
curr_data += sizeof(x_connection.setup_data.data);
x_connection.setup_data.vendor_name =
calloc(x_connection.setup_data.data.vendor_length + 1, 1);
if (!x_connection.setup_data.vendor_name) {
free(additional_data);
x_disconnect();
die("Memory allocation failed");
}
memcpy(x_connection.setup_data.vendor_name, curr_data,
x_connection.setup_data.data.vendor_length);
curr_data += x_connection.setup_data.data.vendor_length;
/* Read the pixmap format data */
x_connection.setup_data.pixmap_formats = calloc(
x_connection.setup_data.data.num_pixmap_formats, sizeof(struct x_format));
if (!x_connection.setup_data.pixmap_formats) {
free(additional_data);
x_disconnect();
die("Memory allocation failed");
}
for (size_t i = 0; i < x_connection.setup_data.data.num_pixmap_formats; ++i) {
memcpy(&x_connection.setup_data.pixmap_formats[i], curr_data,
sizeof(struct x_format));
curr_data += sizeof(struct x_format);
}
/* Read the screen data */
x_connection.setup_data.roots =
calloc(x_connection.setup_data.data.num_roots, sizeof(struct x_screen));
if (!x_connection.setup_data.roots) {
free(additional_data);
x_disconnect();
die("Memory allocation failed");
}
for (size_t i = 0; i < x_connection.setup_data.data.num_roots; ++i) {
struct x_screen *screen = &x_connection.setup_data.roots[i];
memcpy(&screen->data, curr_data, sizeof(struct x_screen_data));
curr_data += sizeof(struct x_screen_data);
/* Read allowed depths data */
screen->allowed_depths =
calloc(screen->data.num_allowed_depths, sizeof(struct x_depth));
if (!screen->allowed_depths) {
free(additional_data);
x_disconnect();
die("Memory allocation failed");
}
for (size_t j = 0; j < screen->data.num_allowed_depths; ++j) {
struct x_depth *depth = &screen->allowed_depths[j];
memcpy(&depth->data, curr_data, sizeof(struct x_depth_data));
curr_data += sizeof(struct x_depth_data);
/* Read visuals data */
depth->visuals =
calloc(depth->data.num_visuals, sizeof(struct x_visual_type));
if (!depth->visuals) {
free(additional_data);
x_disconnect();
die("Memory allocation failed");
}
for (size_t k = 0; k < depth->data.num_visuals; ++k) {
memcpy(&depth->visuals[k], curr_data, sizeof(struct x_visual_type));
curr_data += sizeof(struct x_visual_type);
}
}
}
free(additional_data);
}
static void parse_x_display_name(const char *full_name, char *hostname,
size_t *hostname_len, unsigned long *number,
unsigned long *screen) {
/**
* An X display string is of the form
* hostname:D.S
* where:
* - hostname is the name of the computer where the X server is running. An
* omitted hostname means localhost.
* - D is a sequence number (usually zero). It can vary if there are multiple
* displays connected to a single computer.
* - S is the screen number. While a display can have multiple screens, there
* is usually just one. Zero is the default value if S is omitted.
*
* In X parlance, a "display" is a collection of monitors linked to a common
* input system (e.g., keyboard + mouse).
*
* Examples:
* localhost:2
* remote-server.com:0.0
* :0.1
*
* A name of the form hostname:D.S means screen S on display D on host
* hostname. The X server for this display is listening on TCP port 6000+D.
* A name of the form hostname/unix:D.S indicates that the connection to the X
* server has to go through a UNIX socket located at /tmp/.X11-unix/X$D
* instead of being a TCP connection.
* :D.S is equivalent to localhost/unix:D.S.
*/
const char *curr = full_name;
while (*curr && *curr++ != ':')
*hostname_len += 1;
snprintf(hostname, *hostname_len, "%s", full_name);
char *end = 0;
errno = 0;
*number = strtoul(curr, &end, 10);
if (errno != 0)
die("Invalid X display sequence number in display name");
if(*end == '\0') {
*screen = 0;
} else {
curr = end + 1; /* Skip the dot */
*screen = strtoul(curr, 0, 10);
if (errno != 0)
die("Invalid X screen number in display name");
}
}
static int get_socket_for_display(const char *full_display_name) {
char hostname[HOST_NAME_MAX] = {0};
size_t hostname_len = 0;
unsigned long number = 0;
unsigned long screen = 0;
parse_x_display_name(full_display_name, hostname, &hostname_len, &number,
&screen);
/* Check whether we are connecting through a UNIX domain socket. If so, it
* must be to the local host. */
int is_unix_connection = hostname_len == 0;
if (hostname_len > 5 && hostname[hostname_len - 5] == '/' &&
hostname[hostname_len - 4] == 'u' && hostname[hostname_len - 3] == 'n' &&
hostname[hostname_len - 2] == 'i' && hostname[hostname_len - 1] == 'x')
is_unix_connection = 1;
int fd = -1;
if (is_unix_connection) {
fd = socket(AF_UNIX, SOCK_STREAM, 0);
if (fd == -1)
die("Failed to create UNIX domain socket");
struct sockaddr_un server_addr = {.sun_family = AF_UNIX};
char path[sizeof(server_addr.sun_path)] = {0};
snprintf(path, sizeof(path), "/tmp/.X11-unix/X%lu", number);
memcpy(server_addr.sun_path, path, sizeof(path));
if (connect(fd, (struct sockaddr *)&server_addr, sizeof(server_addr)) ==
-1) {
int errno_save = errno;
close(fd);
errno = errno_save;
die("Failed to connect to X server");
}
} else {
unsigned int port = X_BASE_TCP_PORT + number;
struct addrinfo hints = {
.ai_family = AF_UNSPEC,
.ai_socktype = SOCK_STREAM,
.ai_flags = AI_V4MAPPED | AI_ADDRCONFIG,
.ai_protocol = 0,
};
struct addrinfo *info = 0;
char port_string[16];
snprintf(port_string, sizeof(port_string), "%u", port);
if (getaddrinfo(hostname, port_string, &hints, &info) != 0)
die("Failed to resolve X server host name");
/* Connect either via IPv4 or IPv6 depending on what is available and
* configured */
struct addrinfo *current_info = info;
for (; current_info != 0; current_info = current_info->ai_next) {
fd = socket(current_info->ai_family, current_info->ai_socktype,
current_info->ai_protocol);
if (fd == -1)
continue;
if (connect(fd, current_info->ai_addr, current_info->ai_addrlen) != -1)
break;
/* Connection failed, close the socket and try the next one */
close(fd);
}
int failed = current_info == 0;
freeaddrinfo(info);
if (failed)
die("Failed to connect to X server");
}
return fd;
}
static void x_connect_to_display_with_auth_data(const char *full_display_name,
size_t protocol_name_len,
const char *protocol_name,
size_t auth_data_len,
const char *auth_data) {
int fd = get_socket_for_display(full_display_name);
x_connect_to_socket(fd, protocol_name_len, protocol_name, auth_data_len,
auth_data);
}
static int readu16be(FILE *file, uint16_t *result) {
uint8_t res[2];
if (fread(res, 1, 2, file) != 2)
return 0;
*result = res[0] * 256 + res[1];
return 1;
}
static int read_counted_string(FILE *file, uint16_t *length, char **string) {
if (!readu16be(file, length) || length == 0)
return 0;
*string = calloc(*length + 1, 1);
if (!*string)
die("Memory allocation failed");
if (fread(*string, 1, *length, file) != *length) {
free(*string);
return 0;
}
return 1;
}
static void x_connect_to_display(const char *full_display_name) {
char hostname[HOST_NAME_MAX] = {0};
size_t hostname_len = 0;
unsigned long number = 0;
unsigned long screen = 0;
parse_x_display_name(full_display_name, hostname, &hostname_len, &number,
&screen);
/* If there is no hostname in the display name then get the name of the local
* host */
if (hostname_len == 0)
gethostname(hostname, HOST_NAME_MAX);
/* Now that we have a valid host name, we can try to find the appropriate
* authentication method to connect to the selected display. We read the
* Xauthority file to determine which protocol and authentication data should
* be used. */
char *xauthority_path = getenv("XAUTHORITY");
if (!xauthority_path) {
/* If the path to the Xauthority file is not given in the environment, try
* in the user's home directory */
xauthority_path = "~/.Xauthority";
}
FILE *xauthority = fopen(xauthority_path, "rb");
if (!xauthority)
die("Failed to open Xauthority file");
struct stat file_stat;
fstat(fileno(xauthority), &file_stat);
size_t file_size = file_stat.st_size;
/* Go through the entire file and search for an entry that corresponds to our
* target display */
size_t auth_protocol_name_len = 0;
char *auth_protocol_name = 0;
size_t auth_data_len = 0;
char *auth_data = 0;
while (ftell(xauthority) != (long)file_size) {
uint16_t family;
uint16_t xauth_hostname_len;
char *xauth_hostname;
uint16_t xauth_number_len;
char *xauth_number;
uint16_t xauth_protocol_name_len;
char *xauth_protocol_name;
uint16_t xauth_data_len;
char *xauth_data;
if (readu16be(xauthority, &family) &&
read_counted_string(xauthority, &xauth_hostname_len, &xauth_hostname) &&
read_counted_string(xauthority, &xauth_number_len, &xauth_number) &&
read_counted_string(xauthority, &xauth_protocol_name_len,
&xauth_protocol_name) &&
read_counted_string(xauthority, &xauth_data_len, &xauth_data)) {
if (strtoul(xauth_number, 0, 10) == number &&
strcmp(hostname, xauth_hostname) == 0) {
auth_protocol_name_len = xauth_protocol_name_len;
auth_protocol_name = xauth_protocol_name;
auth_data_len = xauth_data_len;
auth_data = xauth_data;
} else {
free(xauth_protocol_name);
free(xauth_data);
}
free(xauth_hostname);
free(xauth_number);
}
}
fclose(xauthority);
if (!auth_data) {
free(auth_protocol_name);
die("No X authentication data for the specified display");
}
x_connect_to_display_with_auth_data(full_display_name, auth_protocol_name_len,
auth_protocol_name, auth_data_len,
auth_data);
free(auth_protocol_name);
free(auth_data);
}
static const char *bool_to_string(int value) { return value ? "yes" : "no"; }
static void x_connect(void) {
/* The DISPLAY environment variable contains the name of the display on which
* the application was started */
char *display_name = getenv("DISPLAY");
if (!display_name) {
fprintf(stderr, "WARNING: No DISPLAY environment variable found, trying "
"default X display name ':0'\n");
display_name = ":0";
}
x_connect_to_display(display_name);
}
static unsigned int x_get_extension_opcode(const char *name) {
size_t name_len = strlen(name);
struct x_query_extension_request request = {
.opcode = X_OPCODE_QUERY_EXTENSION,
.request_len = 2 + (X_PAD(name_len) / 4),
.name_len = name_len,
};
unsigned int opcode = 0;
size_t request_len = sizeof(request) + X_PAD(name_len);
char *request_buffer = calloc(request_len, 1);
if (!request_buffer)
goto end;
struct x_query_extension_reply reply = {0};
memcpy(request_buffer, &request, sizeof(request));
memcpy(request_buffer + sizeof(request), name, name_len);
ssize_t num_written = write_n(x_connection.fd, request_buffer, request_len);
if (num_written != (long)request_len)
goto end;
ssize_t num_read = read_n(x_connection.fd, &reply, sizeof(reply));
if (num_read != sizeof(reply) || reply.status != X_REPLY)
goto end;
if (reply.present)
opcode = reply.major_opcode;
end:
free(request_buffer);
return opcode;
}
struct x_big_requests_enable_request {
uint8_t opcode;
uint8_t extension_opcode;
uint16_t request_len;
};
struct x_big_requests_enable_reply {
uint8_t status;
uint8_t pad1;
uint16_t sequence_number;
uint32_t additional_data_len;
uint32_t max_request_len;
uint8_t pad[18];
};
struct x_xtest_query_version_request {
uint8_t opcode;
uint8_t extension_opcode;
uint16_t request_len;
uint8_t version_major;
uint8_t pad;
uint16_t version_minor;
};
struct x_xtest_query_version_reply {
uint8_t status;
uint8_t version_major;
uint16_t sequence_number;
uint32_t additional_data_len;
uint16_t version_minor;
uint8_t pad[22];
};
struct x_generic_query_version8_request {
uint8_t opcode;
uint8_t extension_opcode;
uint16_t request_len;
uint8_t version_major;
uint8_t version_minor;
uint16_t pad;
};
struct x_generic_query_version8_reply {
uint8_t status;
uint8_t pad1;
uint16_t sequence_number;
uint32_t additional_data_len;
uint8_t version_major;
uint8_t version_minor;
uint8_t pad[22];
};
struct x_generic_query_version16_request {
uint8_t opcode;
uint8_t extension_opcode;
uint16_t request_len;
uint16_t version_major;
uint16_t version_minor;
};
struct x_generic_query_version16_reply {
uint8_t status;
uint8_t pad1;
uint16_t sequence_number;
uint32_t additional_data_len;
uint16_t version_major;
uint16_t version_minor;
uint8_t pad[20];
};
struct x_generic_query_version32_request {
uint8_t opcode;
uint8_t extension_opcode;
uint16_t request_len;
uint32_t version_major;
uint32_t version_minor;
};
struct x_generic_query_version32_reply {
uint8_t status;
uint8_t pad1;
uint16_t sequence_number;
uint32_t additional_data_len;
uint32_t version_major;
uint32_t version_minor;
uint8_t pad[16];
};
struct x_generic_query_version16_noparam_request {
uint8_t opcode;
uint8_t extension_opcode;
uint16_t request_len;
};
struct x_generic_query_version16_noparam_reply {
uint8_t status;
uint8_t pad1;
uint16_t sequence_number;
uint32_t additional_data_len;
uint16_t version_major;
uint16_t version_minor;
uint8_t pad[20];
};
struct x_generic_query_version32_noparam_request {
uint8_t opcode;
uint8_t extension_opcode;
uint16_t request_len;
};
struct x_generic_query_version32_noparam_reply {
uint8_t status;
uint8_t pad1;
uint16_t sequence_number;
uint32_t additional_data_len;
uint32_t version_major;
uint32_t version_minor;
uint8_t pad[16];
};
static int generic_query_version8(unsigned int opcode,
unsigned int extension_opcode,
unsigned int *version_major,
unsigned int *version_minor) {
struct x_generic_query_version8_request request = {
.opcode = opcode,
.extension_opcode = extension_opcode,
.request_len = sizeof(struct x_generic_query_version8_request) / 4,
.version_major = (uint8_t)-1,
.version_minor = (uint8_t)-1,
};
struct x_generic_query_version8_reply reply = {0};
ssize_t num_written = write_n(x_connection.fd, &request, sizeof(request));
if (num_written != sizeof(request))
return 1;
ssize_t num_read = read_n(x_connection.fd, &reply, sizeof(reply));
if (num_read != sizeof(reply) || reply.status != X_REPLY)
return 1;
*version_major = reply.version_major;
*version_minor = reply.version_minor;
return 0;
}
static int generic_query_version16(unsigned int opcode,
unsigned int extension_opcode,
unsigned int *version_major,
unsigned int *version_minor) {
struct x_generic_query_version16_request request = {
.opcode = opcode,
.extension_opcode = extension_opcode,
.request_len = sizeof(struct x_generic_query_version16_request) / 4,
.version_major = (uint16_t)-1,
.version_minor = (uint16_t)-1,
};
struct x_generic_query_version16_reply reply = {0};
ssize_t num_written = write_n(x_connection.fd, &request, sizeof(request));
if (num_written != sizeof(request))
return 1;
ssize_t num_read = read_n(x_connection.fd, &reply, sizeof(reply));
if (num_read != sizeof(reply) || reply.status != X_REPLY)
return 1;
*version_major = reply.version_major;
*version_minor = reply.version_minor;
return 0;
}
static int generic_query_version32(unsigned int opcode,
unsigned int extension_opcode,
unsigned int *version_major,
unsigned int *version_minor) {
struct x_generic_query_version32_request request = {
.opcode = opcode,
.extension_opcode = extension_opcode,
.request_len = sizeof(struct x_generic_query_version32_request) / 4,
.version_major = (uint32_t)-1,
.version_minor = (uint32_t)-1,
};
struct x_generic_query_version32_reply reply = {0};
ssize_t num_written = write_n(x_connection.fd, &request, sizeof(request));
if (num_written != sizeof(request))
return 1;
ssize_t num_read = read_n(x_connection.fd, &reply, sizeof(reply));
if (num_read != sizeof(reply) || reply.status != X_REPLY)
return 1;
*version_major = reply.version_major;
*version_minor = reply.version_minor;
return 0;
}
static int generic_query_version16_noparam(unsigned int opcode,
unsigned int extension_opcode,
unsigned int *major,
unsigned int *minor) {
struct x_generic_query_version16_noparam_request request = {
.opcode = opcode,
.extension_opcode = extension_opcode,
.request_len =
sizeof(struct x_generic_query_version16_noparam_request) / 4,
};
struct x_generic_query_version16_noparam_reply reply = {0};
ssize_t num_written = write_n(x_connection.fd, &request, sizeof(request));
if (num_written != sizeof(request))
return 1;
ssize_t num_read = read_n(x_connection.fd, &reply, sizeof(reply));
if (num_read != sizeof(reply) || reply.status != X_REPLY)
return 1;
*major = reply.version_major;
*minor = reply.version_minor;
return 0;
}
static int generic_query_version32_noparam(unsigned int opcode,
unsigned int extension_opcode,
unsigned int *major,
unsigned int *minor) {
struct x_generic_query_version32_noparam_request request = {
.opcode = opcode,
.extension_opcode = extension_opcode,
.request_len =
sizeof(struct x_generic_query_version32_noparam_request) / 4,
};
struct x_generic_query_version32_noparam_reply reply = {0};
ssize_t num_written = write_n(x_connection.fd, &request, sizeof(request));
if (num_written != sizeof(request))
return 1;
ssize_t num_read = read_n(x_connection.fd, &reply, sizeof(reply));
if (num_read != sizeof(reply) || reply.status != X_REPLY)
return 1;
*major = reply.version_major;
*minor = reply.version_minor;
return 0;
}
#define X_EXTENSION_NAME_APPLE_DRI "Apple-DRI"
#define X_EXTENSION_NAME_APPLE_WM "Apple-WM"
#define X_EXTENSION_NAME_BIG_REQUESTS "BIG-REQUESTS"
#define X_EXTENSION_NAME_COMPOSITE "Composite"
#define X_EXTENSION_NAME_DAMAGE "DAMAGE"
#define X_EXTENSION_NAME_DOUBLE_BUFFER "DOUBLE-BUFFER"
#define X_EXTENSION_NAME_DPMS "DPMS"
#define X_EXTENSION_NAME_DMX "DMX"
#define X_EXTENSION_NAME_DRI2 "DRI2"
#define X_EXTENSION_NAME_DRI3 "DRI3"
#define X_EXTENSION_NAME_EXTENDED_VISUAL_INFORMATION \
"Extended-Visual-Information"
#define X_EXTENSION_NAME_FONT_CACHE "FontCache"
#define X_EXTENSION_NAME_GLX "GLX"
#define X_EXTENSION_NAME_GENERIC_EVENT_EXTENSION "Generic Event Extension"
#define X_EXTENSION_NAME_LBX "LBX"
#define X_EXTENSION_NAME_LGE "LGE"
#define X_EXTENSION_NAME_MIT_SCREEN_SAVER "MIT-SCREEN-SAVER"
#define X_EXTENSION_NAME_MIT_SHM "MIT-SHM"
#define X_EXTENSION_NAME_NV_CONTROL "NV-CONTROL"
#define X_EXTENSION_NAME_NV_GLX "NV-GLX"
#define X_EXTENSION_NAME_PRESENT "Present"
#define X_EXTENSION_NAME_RANDR "RANDR"
#define X_EXTENSION_NAME_RECORD "RECORD"
#define X_EXTENSION_NAME_RENDER "RENDER"
#define X_EXTENSION_NAME_SECURITY "SECURITY"
#define X_EXTENSION_NAME_SELINUX "SELinux"
#define X_EXTENSION_NAME_SGI_GLX "SGI-GLX"
#define X_EXTENSION_NAME_SHAPE "SHAPE"
#define X_EXTENSION_NAME_SYNC "SYNC"
#define X_EXTENSION_NAME_TOG_CUP "TOG-CUP"
#define X_EXTENSION_NAME_WINDOWS_WM "Windows-WM"
#define X_EXTENSION_NAME_X_RESOURCE "X-Resource"
#define X_EXTENSION_NAME_XC_APPGROUP "XC-APPGROUP"
#define X_EXTENSION_NAME_XC_MISC "XC-MISC"
#define X_EXTENSION_NAME_XC_VID_MODE_EXTENSION "XC-VidModeExtension"
#define X_EXTENSION_NAME_XCALIBRATE "XCALIBRATE"
#define X_EXTENSION_NAME_XFIXES "XFIXES"
#define X_EXTENSION_NAME_XFREE86_BIGFONT "XFree86-Bigfont"
#define X_EXTENSION_NAME_XFREE86_DGA "XFree86-DGA"
#define X_EXTENSION_NAME_XFREE86_DRI "XFree86-DRI"
#define X_EXTENSION_NAME_XFREE86_MISC "XFree86-Misc"
#define X_EXTENSION_NAME_XFREE86_RUSH "XFree86-Rush"
#define X_EXTENSION_NAME_XFREE86_VID_MODE_EXTENSION "XFree86-VidModeExtension"
#define X_EXTENSION_NAME_XINERAMA "XINERAMA"
#define X_EXTENSION_NAME_XINPUT_EXTENSION "XInputExtension"
#define X_EXTENSION_NAME_XKEYBOARD "XKEYBOARD"
#define X_EXTENSION_NAME_XPRINT "XpExtension"
#define X_EXTENSION_NAME_XTEST "XTEST"
#define X_EXTENSION_NAME_XVIDEO "XVideo"
#define X_EXTENSION_NAME_XVIDEO_MOTION_COMPENSATION "XVideo-MotionCompensation"
#define X_OPCODE_BIG_REQUESTS_ENABLE 0
#define X_OPCODE_GLX_QUERY_VERSION 7
#define X_OPCODE_MIT_SCREEN_SAVER_QUERY_VERSION 0
#define X_OPCODE_SELINUX_QUERY_VERSION 0