i3/testcases/inject_randr1.5.c
Orestis Floros 38f28186ec
Remove some includes from all.h
Also removes duplicates from other headers

All used std* imports are included once in all.h for easy use

- getopt: Only used in main.c and inject_randr1.5.c
- glob: Not used in i3, only in i3bar & libi3
- inttypes: Only used in util.c
- locale: A bit specific for all.h
- math: Slow according to #4022
- unistd: I feel it's good to explicitly include per file that needs it
- yajl: Specific to yajl_utils.h and ipc.h

Related to #4022

Timing before:

```
Analyzing build trace from ...
**** Time summary:
Compilation (81 times):
  Parsing (frontend):           51.3 s
  Codegen & opts (backend):      7.7 s

**** Files that took longest to parse (compiler frontend):
  1254 ms: build/src/i3-commands.o
   972 ms: build/src/i3-resize.o
   945 ms: build/src/i3-con.o
   921 ms: build/src/i3-scratchpad.o
   907 ms: build/src/i3-main.o
   904 ms: build/src/i3-handlers.o
   904 ms: build/src/i3-config_directives.o
   893 ms: build/src/i3-restore_layout.o
   875 ms: build/src/i3-x.o
   854 ms: build/src/i3-ipc.o

**** Files that took longest to codegen (compiler backend):
   863 ms: build/src/i3-commands.o
   471 ms: build/i3bar/src/i3bar-xcb.o
   377 ms: build/src/i3-con.o
   360 ms: build/src/i3-ipc.o
   306 ms: build/src/i3-x.o
   290 ms: build/src/i3-main.o
   238 ms: build/src/i3-config_parser.o
   237 ms: build/src/i3-handlers.o
   220 ms: build/i3-config-wizard/i3_config_wizard-main.o
   214 ms: build/src/i3-bindings.o

**** Functions that took longest to compile:
   209 ms: main (../../i3/src/main.c)
    95 ms: manage_window (../../i3/src/manage.c)
    57 ms: reconfig_windows (../../i3/i3bar/src/xcb.c)
    55 ms: x_draw_decoration (../../i3/src/x.c)
    49 ms: x_push_node (../../i3/src/x.c)
    48 ms: handle_client_message (../../i3/src/handlers.c)
    48 ms: dump_node (../../i3/src/ipc.c)
    47 ms: GENERATED_call (../../i3/src/config_parser.c)
    45 ms: config_string_cb (../../i3/i3bar/src/config.c)
    44 ms: GENERATED_call (../../i3/src/commands_parser.c)
    42 ms: floating_check_size (../../i3/src/floating.c)
    40 ms: con_swap (../../i3/src/con.c)
    40 ms: parse_config (../../i3/src/config_parser.c)
    39 ms: main (../../i3/i3-nagbar/main.c)
    39 ms: cmd_rename_workspace (../../i3/src/commands.c)
    38 ms: window_update_normal_hints (../../i3/src/window.c)
    38 ms: cmd_swap (../../i3/src/commands.c)
    37 ms: dump_bar_config (../../i3/src/ipc.c)
    36 ms: translate_keysyms (../../i3/src/bindings.c)
    35 ms: tree_close_internal (../../i3/src/tree.c)
    34 ms: match_matches_window (../../i3/src/match.c)
    34 ms: floating_enable (../../i3/src/floating.c)
    34 ms: json_string (../../i3/src/load_layout.c)
    33 ms: x_push_changes (../../i3/src/x.c)
    33 ms: main (../../i3/i3-config-wizard/main.c)
    31 ms: free_configuration (../../i3/src/config.c)
    30 ms: parse_file (../../i3/src/config_parser.c)
    28 ms: load_font (../../i3/libi3/font.c)
    28 ms: handle_configure_request (../../i3/src/handlers.c)
    28 ms: parse_command (../../i3/src/commands_parser.c)

**** Function sets that took longest to compile / optimize:

*** Expensive headers:
29287 ms: ../../i3/include/libi3.h (included 78 times, avg 375 ms), included via:
  i3-resize.o all.h data.h  (576 ms)
  a-g_utf8_make_valid.o  (491 ms)
  a-dpi.o  (491 ms)
  a-get_colorpixel.o  (483 ms)
  a-is_debug_build.o  (478 ms)
  a-strndup.o  (478 ms)
  ...

29221 ms: ../../i3/include/all.h (included 39 times, avg 749 ms), included via:
  i3-resize.o  (946 ms)
  i3-scratchpad.o  (895 ms)
  i3-restore_layout.o  (865 ms)
  i3-handlers.o  (839 ms)
  i3-drag.o  (830 ms)
  i3-config_directives.o  (793 ms)
  ...

5195 ms: /usr/include/xcb/xcb.h (included 79 times, avg 65 ms), included via:
  i3-handlers.o all.h  (113 ms)
  i3-restore_layout.o all.h  (108 ms)
  a-g_utf8_make_valid.o libi3.h  (108 ms)
  i3bar-xcb.o common.h  (105 ms)
  i3-scratchpad.o all.h  (95 ms)
  a-get_colorpixel.o libi3.h  (89 ms)
  ...

4100 ms: /usr/include/math.h (included 41 times, avg 100 ms), included via:
  i3-scratchpad.o all.h  (180 ms)
  i3-fake_outputs.o all.h  (138 ms)
  i3-regex.o all.h  (130 ms)
  i3-restore_layout.o all.h  (128 ms)
  i3-xcb.o all.h  (121 ms)
  i3-move.o all.h  (119 ms)
  ...

4046 ms: ../../i3/i3bar/include/common.h (included 9 times, avg 449 ms), included via:
  i3bar-main.o  (503 ms)
  i3bar-xcb.o  (501 ms)
  i3bar-workspaces.o  (472 ms)
  i3bar-parse_json_header.o  (446 ms)
  i3bar-child.o  (438 ms)
  i3bar-ipc.o  (434 ms)
  ...

2713 ms: ../../i3/include/i3.h (included 41 times, avg 66 ms), included via:
  i3-xcursor.o  (450 ms)
  i3-config_directives.o all.h ipc.h configuration.h  (87 ms)
  i3-config.o all.h ipc.h configuration.h  (71 ms)
  i3-manage.o all.h ipc.h configuration.h  (70 ms)
  i3-window.o all.h ipc.h configuration.h  (68 ms)
  i3-x.o all.h ipc.h configuration.h  (61 ms)
  ...

1492 ms: /usr/include/xcb/xkb.h (included 42 times, avg 35 ms), included via:
  i3-config_directives.o all.h ipc.h configuration.h i3.h  (50 ms)
  i3-config.o all.h ipc.h configuration.h i3.h  (45 ms)
  i3-window.o all.h ipc.h configuration.h i3.h  (43 ms)
  i3-x.o all.h ipc.h configuration.h i3.h  (42 ms)
  i3-manage.o all.h ipc.h configuration.h i3.h  (41 ms)
  i3-config_parser.o all.h ipc.h configuration.h i3.h  (38 ms)
  ...

1432 ms: /usr/include/stdlib.h (included 79 times, avg 18 ms), included via:
  i3-scratchpad.o all.h  (48 ms)
  i3-restore_layout.o all.h  (36 ms)
  i3-regex.o all.h  (32 ms)
  i3-key_press.o all.h  (28 ms)
  i3-commands.o all.h  (28 ms)
  i3-bindings.o all.h  (24 ms)
  ...

1349 ms: /usr/include/pthread.h (included 79 times, avg 17 ms), included via:
  i3bar-xcb.o common.h xcb.h  (33 ms)
  a-ucs2_conversion.o libi3.h xcb.h  (32 ms)
  i3-match.o all.h xcb.h  (27 ms)
  i3-scratchpad.o all.h xcb.h  (25 ms)
  a-g_utf8_make_valid.o libi3.h xcb.h  (25 ms)
  i3_config_wizard-main.o xcb.h  (24 ms)
  ...

1151 ms: /usr/include/X11/Xlib.h (included 45 times, avg 25 ms), included via:
  i3-output.o all.h data.h sn-launcher.h sn-common.h  (50 ms)
  i3-config_parser.o all.h data.h sn-launcher.h sn-common.h  (43 ms)
  i3-x.o all.h data.h sn-launcher.h sn-common.h  (34 ms)
  i3-config_directives.o all.h data.h sn-launcher.h sn-common.h  (32 ms)
  i3_config_wizard-main.o sn-launchee.h sn-common.h  (30 ms)
  i3-drag.o all.h data.h sn-launcher.h sn-common.h  (29 ms)
  ...
```

Timing after:

```
Analyzing build trace from ...
**** Time summary:
Compilation (81 times):
  Parsing (frontend):           47.6 s
  Codegen & opts (backend):      7.6 s

**** Files that took longest to parse (compiler frontend):
  1154 ms: build/src/i3-commands.o
   929 ms: build/src/i3-display_version.o
   852 ms: build/src/i3-bindings.o
   847 ms: build/src/i3-con.o
   806 ms: build/src/i3-ipc.o
   801 ms: build/src/i3-floating.o
   792 ms: build/src/i3-main.o
   792 ms: build/src/i3-drag.o
   792 ms: build/src/i3-window.o
   776 ms: build/src/i3-config_directives.o

**** Files that took longest to codegen (compiler backend):
   885 ms: build/src/i3-commands.o
   422 ms: build/i3bar/src/i3bar-xcb.o
   382 ms: build/src/i3-con.o
   348 ms: build/src/i3-x.o
   288 ms: build/src/i3-ipc.o
   268 ms: build/src/i3-handlers.o
   254 ms: build/src/i3-main.o
   251 ms: build/src/i3-floating.o
   249 ms: build/src/i3-config_parser.o
   194 ms: build/src/i3-randr.o

**** Functions that took longest to compile:
   186 ms: main (../../i3/src/main.c)
    95 ms: manage_window (../../i3/src/manage.c)
    65 ms: floating_check_size (../../i3/src/floating.c)
    63 ms: x_draw_decoration (../../i3/src/x.c)
    58 ms: handle_client_message (../../i3/src/handlers.c)
    55 ms: x_push_node (../../i3/src/x.c)
    54 ms: match_matches_window (../../i3/src/match.c)
    51 ms: parse_config (../../i3/src/config_parser.c)
    49 ms: dump_node (../../i3/src/ipc.c)
    47 ms: reconfig_windows (../../i3/i3bar/src/xcb.c)
    47 ms: config_string_cb (../../i3/i3bar/src/config.c)
    45 ms: GENERATED_call (../../i3/src/config_parser.c)
    45 ms: GENERATED_call (../../i3/src/commands_parser.c)
    43 ms: floating_enable (../../i3/src/floating.c)
    42 ms: handle_configure_request (../../i3/src/handlers.c)
    40 ms: con_swap (../../i3/src/con.c)
    36 ms: main (../../i3/i3-input/main.c)
    36 ms: main (../../i3/i3-msg/main.c)
    36 ms: main (../../i3/i3-nagbar/main.c)
    36 ms: cmd_move_con_to_workspace_number (../../i3/src/commands.c)
    35 ms: json_string (../../i3/src/load_layout.c)
    35 ms: tree_restore (../../i3/src/tree.c)
    35 ms: cmd_swap (../../i3/src/commands.c)
    34 ms: x_push_changes (../../i3/src/x.c)
    32 ms: main (../../i3/i3-config-wizard/main.c)
    32 ms: ewmh_setup_hints (../../i3/src/ewmh.c)
    31 ms: match_parse_property (../../i3/src/match.c)
    30 ms: cmd_mark (../../i3/src/commands.c)
    30 ms: translate_keysyms (../../i3/src/bindings.c)
    30 ms: window_update_normal_hints (../../i3/src/window.c)

**** Function sets that took longest to compile / optimize:

*** Expensive headers:
29596 ms: ../../i3/include/libi3.h (included 78 times, avg 379 ms), included via:
  a-get_config_path.o  (539 ms)
  i3_dump_log-main.o  (522 ms)
  i3_config_wizard-main.o  (501 ms)
  a-fake_configure_notify.o  (500 ms)
  a-root_atom_contents.o  (488 ms)
  i3-display_version.o all.h  (466 ms)
  ...

26054 ms: ../../i3/include/all.h (included 41 times, avg 635 ms), included via:
  i3-display_version.o  (901 ms)
  i3-drag.o  (775 ms)
  i3-ewmh.o  (703 ms)
  i3-startup.o  (693 ms)
  i3-commands.o  (687 ms)
  i3-xcb.o  (680 ms)
  ...

5345 ms: /usr/include/xcb/xcb.h (included 79 times, avg 67 ms), included via:
  i3-display_version.o all.h  (173 ms)
  i3_input-keysym2ucs.o keysym2ucs.h  (106 ms)
  i3-ewmh.o all.h  (106 ms)
  a-fake_configure_notify.o libi3.h  (103 ms)
  a-get_config_path.o libi3.h  (95 ms)
  i3bar-parse_json_header.o common.h  (93 ms)
  ...

4127 ms: ../../i3/i3bar/include/common.h (included 9 times, avg 458 ms), included via:
  i3bar-child.o  (524 ms)
  i3bar-mode.o  (486 ms)
  i3bar-outputs.o  (464 ms)
  i3bar-parse_json_header.o  (463 ms)
  i3bar-config.o  (457 ms)
  i3bar-ipc.o  (448 ms)
  ...

1542 ms: /usr/include/xcb/xkb.h (included 42 times, avg 36 ms), included via:
  i3-con.o all.h ipc.h configuration.h i3.h  (60 ms)
  i3-render.o all.h ipc.h configuration.h i3.h  (56 ms)
  i3-bindings.o all.h ipc.h configuration.h i3.h  (56 ms)
  i3-sighandler.o all.h ipc.h configuration.h i3.h  (48 ms)
  i3-xcb.o all.h ipc.h configuration.h i3.h  (47 ms)
  i3-resize.o all.h ipc.h configuration.h i3.h  (39 ms)
  ...

1456 ms: /usr/include/stdlib.h (included 79 times, avg 18 ms), included via:
  i3-drag.o all.h  (60 ms)
  i3-display_version.o all.h  (55 ms)
  i3-fake_outputs.o all.h  (39 ms)
  i3-config_directives.o all.h  (33 ms)
  i3-xcursor.o all.h  (30 ms)
  i3bar-mode.o common.h libi3.h pango.h pango-attributes.h pango-font.h pango-coverage.h glib-object.h gbinding.h glib.h gasyncqueue.h gthread.h gutils.h  (29 ms)
  ...

1136 ms: /usr/include/X11/Xlib.h (included 44 times, avg 25 ms), included via:
  i3-con.o all.h data.h sn-launcher.h sn-common.h  (35 ms)
  i3-resize.o all.h data.h sn-launcher.h sn-common.h  (34 ms)
  i3-util.o all.h data.h sn-launcher.h sn-common.h  (33 ms)
  i3-assignments.o all.h data.h sn-launcher.h sn-common.h  (33 ms)
  i3-sighandler.o all.h data.h sn-launcher.h sn-common.h  (31 ms)
  i3-xcb.o all.h data.h sn-launcher.h sn-common.h  (31 ms)
  ...

808 ms: /usr/include/stdio.h (included 79 times, avg 10 ms), included via:
  i3-drag.o all.h  (19 ms)
  i3-fake_outputs.o all.h  (18 ms)
  a-font.o libi3.h  (16 ms)
  i3bar-child.o common.h libi3.h  (15 ms)
  a-safewrappers.o libi3.h  (15 ms)
  a-ipc_send_message.o libi3.h  (13 ms)
  ...

770 ms: /usr/include/xcb/randr.h (included 42 times, avg 18 ms), included via:
  i3-click.o all.h data.h  (29 ms)
  i3-commands.o all.h data.h  (27 ms)
  i3-assignments.o all.h data.h  (25 ms)
  i3-xcb.o all.h data.h  (21 ms)
  i3-resize.o all.h data.h  (21 ms)
  i3-sighandler.o all.h data.h  (20 ms)
  ...

688 ms: /usr/include/math.h (included 6 times, avg 114 ms), included via:
  a-dpi.o  (145 ms)
  i3-render.o  (127 ms)
  i3-floating.o  (106 ms)
  a-root_atom_contents.o  (106 ms)
  i3-window.o  (102 ms)
  i3-bindings.o  (99 ms)
  ...
```
2020-04-20 05:54:17 +02:00

481 lines
15 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
* vim:ts=4:sw=4:expandtab
*
* i3 - an improved dynamic tiling window manager
* © 2009 Michael Stapelberg and contributors (see also: LICENSE)
*
* inject_randr1.5.c: An X11 proxy which interprets RandR 1.5 GetMonitors
* requests and overwrites their reply with a custom reply.
*
* This tool can be refactored as necessary in order to perform the same
* purpose for other request types. The RandR 1.5 specific portions of the code
* have been marked as such to make such a refactoring easier.
*
*/
#include "all.h"
#include <ev.h>
#include <fcntl.h>
#include <getopt.h>
#include <libgen.h>
#include <sys/mman.h>
#include <sys/resource.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/un.h>
#include <sys/wait.h>
#include <unistd.h>
static void uds_connection_cb(EV_P_ ev_io *w, int revents);
static void read_client_setup_request_cb(EV_P_ ev_io *w, int revents);
static void read_server_setup_reply_cb(EV_P_ ev_io *w, int revents);
static void read_client_x11_packet_cb(EV_P_ ev_io *w, int revents);
static void read_server_x11_packet_cb(EV_P_ ev_io *w, int revents);
static char *sun_path = NULL;
static void cleanup_socket(void) {
if (sun_path != NULL) {
unlink(sun_path);
free(sun_path);
sun_path = NULL;
}
}
struct injected_reply {
void *buf;
off_t len;
};
/* BEGIN RandR 1.5 specific */
static struct injected_reply getmonitors_reply = {NULL, 0};
static struct injected_reply getoutputinfo_reply = {NULL, 0};
/* END RandR 1.5 specific */
#define XCB_PAD(i) (-(i)&3)
struct connstate {
/* clientw is a libev watcher for the connection which we accept()ed. */
ev_io *clientw;
/* serverw is a libev watcher for the connection to X11 which we initiated
* on behalf of the client. */
ev_io *serverw;
/* sequence is the client-side sequence number counter. In X11s wire
* encoding, sequence counters are not included in requests, only in
* replies. */
int sequence;
/* BEGIN RandR 1.5 specific */
/* sequence number of the most recent GetExtension request for RANDR */
int getext_randr;
/* sequence number of the most recent RRGetMonitors request */
int getmonitors;
/* sequence number of the most recent RRGetOutputInfo request */
int getoutputinfo;
int randr_major_opcode;
/* END RandR 1.5 specific */
};
/*
* Returns 0 on EOF
* Returns -1 on error (with errno from read() untouched)
*
*/
static size_t readall_into(void *buffer, const size_t len, int fd) {
size_t read_bytes = 0;
while (read_bytes < len) {
ssize_t n = read(fd, buffer + read_bytes, len - read_bytes);
if (n <= 0) {
return n;
}
read_bytes += (size_t)n;
}
return read_bytes;
}
/*
* Exits the program with an error if the read failed.
*
*/
static void must_read(int n) {
if (n == -1) {
err(EXIT_FAILURE, "read()");
}
if (n == 0) {
errx(EXIT_FAILURE, "EOF");
}
}
/*
* Exits the program with an error if the write failed.
*
*/
static void must_write(int n) {
if (n == -1) {
err(EXIT_FAILURE, "write()");
}
}
static void uds_connection_cb(EV_P_ ev_io *w, int revents) {
struct sockaddr_un addr;
socklen_t addrlen = sizeof(addr);
const int clientfd = accept(w->fd, (struct sockaddr *)&addr, &addrlen);
if (clientfd == -1) {
if (errno == EINTR) {
return;
}
err(EXIT_FAILURE, "accept()");
}
struct connstate *connstate = scalloc(1, sizeof(struct connstate));
ev_io *clientw = scalloc(1, sizeof(ev_io));
connstate->clientw = clientw;
clientw->data = connstate;
ev_io_init(clientw, read_client_setup_request_cb, clientfd, EV_READ);
ev_io_start(EV_A_ clientw);
}
// https://www.x.org/releases/current/doc/xproto/x11protocol.html#Encoding::Connection_Setup
static void read_client_setup_request_cb(EV_P_ ev_io *w, int revents) {
ev_io_stop(EV_A_ w);
struct connstate *connstate = (struct connstate *)w->data;
/* Read X11 setup request in its entirety. */
xcb_setup_request_t setup_request;
must_read(readall_into(&setup_request, sizeof(setup_request), w->fd));
/* Establish a connection to X11. */
int fd = socket(AF_LOCAL, SOCK_STREAM, 0);
if (fd == -1) {
err(EXIT_FAILURE, "socket()");
}
char *host;
int displayp;
if (xcb_parse_display(getenv("DISPLAY"), &host, &displayp, NULL) == 0) {
errx(EXIT_FAILURE, "Could not parse DISPLAY=%s", getenv("DISPLAY"));
}
free(host);
struct sockaddr_un addr;
memset(&addr, 0, sizeof(addr));
addr.sun_family = AF_LOCAL;
snprintf(addr.sun_path, sizeof(addr.sun_path), "/tmp/.X11-unix/X%d", displayp);
if (connect(fd, (const struct sockaddr *)&addr, sizeof(struct sockaddr_un)) == -1) {
err(EXIT_FAILURE, "connect(%s)", addr.sun_path);
}
/* Relay setup request. */
must_write(writeall(fd, &setup_request, sizeof(setup_request)));
if (setup_request.authorization_protocol_name_len > 0 ||
setup_request.authorization_protocol_data_len > 0) {
const size_t authlen = setup_request.authorization_protocol_name_len +
XCB_PAD(setup_request.authorization_protocol_name_len) +
setup_request.authorization_protocol_data_len +
XCB_PAD(setup_request.authorization_protocol_data_len);
void *buf = smalloc(authlen);
must_read(readall_into(buf, authlen, w->fd));
must_write(writeall(fd, buf, authlen));
free(buf);
}
/* Wait for a response from the X11 server. */
ev_io *serverw = scalloc(1, sizeof(ev_io));
connstate->serverw = serverw;
serverw->data = connstate;
ev_io_init(serverw, read_server_setup_reply_cb, fd, EV_READ);
ev_io_start(EV_A_ serverw);
}
static void read_server_setup_reply_cb(EV_P_ ev_io *w, int revents) {
struct connstate *connstate = (struct connstate *)w->data;
xcb_setup_failed_t setup_failed;
must_read(readall_into(&setup_failed, sizeof(setup_failed), w->fd));
switch (setup_failed.status) {
case 0:
errx(EXIT_FAILURE, "error authenticating at the X11 server");
case 2:
errx(EXIT_FAILURE, "two-factor auth not implemented");
case 1:
must_write(writeall(connstate->clientw->fd, &setup_failed, sizeof(xcb_setup_failed_t)));
const size_t len = (setup_failed.length * 4);
void *buf = smalloc(len);
must_read(readall_into(buf, len, w->fd));
must_write(writeall(connstate->clientw->fd, buf, len));
free(buf);
ev_set_cb(connstate->clientw, read_client_x11_packet_cb);
ev_set_cb(connstate->serverw, read_server_x11_packet_cb);
ev_io_start(EV_A_ connstate->clientw);
break;
default:
errx(EXIT_FAILURE, "X11 protocol error: expected setup_failed.status in [0..2], got %d", setup_failed.status);
}
}
// https://www.x.org/releases/current/doc/xproto/x11protocol.html#request_format
typedef struct {
uint8_t opcode;
uint8_t pad0;
uint16_t length;
} generic_x11_request_t;
// https://www.x.org/releases/current/doc/xproto/x11protocol.html#reply_format
typedef struct {
uint8_t code; /* if 1, this is a reply. if 0, this is an error. else, an event */
uint8_t pad0;
uint16_t sequence;
uint32_t length;
} generic_x11_reply_t;
static void read_client_x11_packet_cb(EV_P_ ev_io *w, int revents) {
struct connstate *connstate = (struct connstate *)w->data;
void *request = smalloc(sizeof(generic_x11_request_t));
must_read(readall_into(request, sizeof(generic_x11_request_t), connstate->clientw->fd));
const size_t len = (((generic_x11_request_t *)request)->length * 4);
if (len > sizeof(generic_x11_request_t)) {
request = srealloc(request, len);
must_read(readall_into(request + sizeof(generic_x11_request_t),
len - sizeof(generic_x11_request_t),
connstate->clientw->fd));
}
// XXX: sequence counter wrapping is not implemented, but should not be
// necessary given that this tool is scoped for test cases.
connstate->sequence++;
/* BEGIN RandR 1.5 specific */
const uint8_t opcode = ((generic_x11_request_t *)request)->opcode;
if (opcode == XCB_QUERY_EXTENSION) {
xcb_query_extension_request_t *req = request;
const char *name = request + sizeof(xcb_query_extension_request_t);
if (req->name_len == strlen("RANDR") &&
strncmp(name, "RANDR", strlen("RANDR")) == 0) {
connstate->getext_randr = connstate->sequence;
}
} else if (opcode == connstate->randr_major_opcode) {
const uint8_t randr_opcode = ((generic_x11_request_t *)request)->pad0;
if (randr_opcode == XCB_RANDR_GET_MONITORS) {
connstate->getmonitors = connstate->sequence;
} else if (randr_opcode == XCB_RANDR_GET_OUTPUT_INFO) {
connstate->getoutputinfo = connstate->sequence;
}
}
/* END RandR 1.5 specific */
must_write(writeall(connstate->serverw->fd, request, len));
free(request);
}
static bool handle_sequence(struct connstate *connstate, uint16_t sequence) {
/* BEGIN RandR 1.5 specific */
if (sequence == connstate->getmonitors) {
printf("RRGetMonitors reply!\n");
if (getmonitors_reply.buf != NULL) {
printf("injecting reply\n");
((generic_x11_reply_t *)getmonitors_reply.buf)->sequence = sequence;
must_write(writeall(connstate->clientw->fd, getmonitors_reply.buf, getmonitors_reply.len));
return true;
}
}
if (sequence == connstate->getoutputinfo) {
printf("RRGetOutputInfo reply!\n");
if (getoutputinfo_reply.buf != NULL) {
printf("injecting reply\n");
((generic_x11_reply_t *)getoutputinfo_reply.buf)->sequence = sequence;
must_write(writeall(connstate->clientw->fd, getoutputinfo_reply.buf, getoutputinfo_reply.len));
return true;
}
}
/* END RandR 1.5 specific */
return false;
}
static void read_server_x11_packet_cb(EV_P_ ev_io *w, int revents) {
struct connstate *connstate = (struct connstate *)w->data;
// all packets from the server are at least 32 bytes in length
size_t len = 32;
void *packet = smalloc(len);
must_read(readall_into(packet, len, connstate->serverw->fd));
switch (((generic_x11_reply_t *)packet)->code) {
case 0: { // error
const uint16_t sequence = ((xcb_request_error_t *)packet)->sequence;
if (handle_sequence(connstate, sequence)) {
free(packet);
return;
}
break;
}
case 1: // reply
len += ((generic_x11_reply_t *)packet)->length * 4;
if (len > 32) {
packet = srealloc(packet, len);
must_read(readall_into(packet + 32, len - 32, connstate->serverw->fd));
}
/* BEGIN RandR 1.5 specific */
const uint16_t sequence = ((generic_x11_reply_t *)packet)->sequence;
if (sequence == connstate->getext_randr) {
xcb_query_extension_reply_t *reply = packet;
connstate->randr_major_opcode = reply->major_opcode;
}
/* END RandR 1.5 specific */
if (handle_sequence(connstate, sequence)) {
free(packet);
return;
}
break;
default: // event
break;
}
must_write(writeall(connstate->clientw->fd, packet, len));
free(packet);
}
static void child_cb(EV_P_ ev_child *w, int revents) {
ev_child_stop(EV_A_ w);
if (WIFEXITED(w->rstatus)) {
exit(WEXITSTATUS(w->rstatus));
} else {
exit(WTERMSIG(w->rstatus) + 128);
}
}
static void must_read_reply(const char *filename, struct injected_reply *reply) {
FILE *f;
if ((f = fopen(filename, "r")) == NULL) {
err(EXIT_FAILURE, "fopen(%s)", filename);
}
struct stat stbuf;
if (fstat(fileno(f), &stbuf) != 0) {
err(EXIT_FAILURE, "fstat(%s)", filename);
}
reply->len = stbuf.st_size;
reply->buf = smalloc(stbuf.st_size);
int n = fread(reply->buf, 1, stbuf.st_size, f);
if (n != stbuf.st_size) {
err(EXIT_FAILURE, "fread(%s)", filename);
}
fclose(f);
}
int main(int argc, char *argv[]) {
static struct option long_options[] = {
{"getmonitors_reply", required_argument, 0, 0},
{"getoutputinfo_reply", required_argument, 0, 0},
{0, 0, 0, 0},
};
char *options_string = "";
int opt;
int option_index = 0;
while ((opt = getopt_long(argc, argv, options_string, long_options, &option_index)) != -1) {
switch (opt) {
case 0: {
const char *option_name = long_options[option_index].name;
if (strcmp(option_name, "getmonitors_reply") == 0) {
must_read_reply(optarg, &getmonitors_reply);
} else if (strcmp(option_name, "getoutputinfo_reply") == 0) {
must_read_reply(optarg, &getoutputinfo_reply);
}
break;
}
default:
exit(EXIT_FAILURE);
}
}
if (optind >= argc) {
errx(EXIT_FAILURE, "syntax: %s [options] <command>", argv[0]);
}
int fd = socket(AF_LOCAL, SOCK_STREAM, 0);
if (fd == -1) {
err(EXIT_FAILURE, "socket(AF_UNIX)");
}
if (fcntl(fd, F_SETFD, FD_CLOEXEC)) {
warn("Could not set FD_CLOEXEC");
}
struct sockaddr_un addr;
memset(&addr, 0, sizeof(struct sockaddr_un));
addr.sun_family = AF_UNIX;
int i;
bool bound = false;
for (i = 0; i < 100; i++) {
/* XXX: The path to X11 sockets differs on some platforms (e.g. Trusted
* Solaris, HPUX), but since libxcb doesnt provide a function to
* generate the path, well just have to hard-code it for now. */
snprintf(addr.sun_path, sizeof(addr.sun_path), "/tmp/.X11-unix/X%d", i);
if (bind(fd, (struct sockaddr *)&addr, sizeof(struct sockaddr_un)) == -1) {
warn("bind(%s)", addr.sun_path);
} else {
bound = true;
/* Let the user know bind() was successful, so that they know the
* error messages can be disregarded. */
fprintf(stderr, "Successfuly bound to %s\n", addr.sun_path);
sun_path = sstrdup(addr.sun_path);
break;
}
}
if (!bound) {
err(EXIT_FAILURE, "bind()");
}
atexit(cleanup_socket);
/* This program will be started for each testcase which requires it, so we
* expect precisely one connection. */
if (listen(fd, 1) == -1) {
err(EXIT_FAILURE, "listen()");
}
pid_t child = fork();
if (child == -1) {
err(EXIT_FAILURE, "fork()");
}
if (child == 0) {
char *display;
sasprintf(&display, ":%d", i);
setenv("DISPLAY", display, 1);
free(display);
char **child_args = argv + optind;
execvp(child_args[0], child_args);
err(EXIT_FAILURE, "exec()");
}
struct ev_loop *loop = ev_default_loop(0);
ev_child cw;
ev_child_init(&cw, child_cb, child, 0);
ev_child_start(loop, &cw);
ev_io watcher;
ev_io_init(&watcher, uds_connection_cb, fd, EV_READ);
ev_io_start(loop, &watcher);
ev_run(loop, 0);
}