Debian Codegraph @

contrib/autorandr_launcher/autorandr_launcher.c

raw
1 #include <signal.h>
2 #include <xcb/xcb.h>
3 #include <xcb/randr.h>
4 #include <getopt.h>
5 #include <unistd.h>
6 #include <stdarg.h>
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <ctype.h>
10 #include <sys/types.h>
11 #include <sys/wait.h>
12 #include <time.h>
13 #include <inttypes.h>
14 #include <errno.h>
15 #include <string.h>
16
17 #ifndef AUTORANDR_PATH
18 #define AUTORANDR_PATH "/usr/bin/autorandr"
19 #endif
20
21 // indent -kr -i8
22 static int VERBOSE = 0;
23 extern char **environ;
24
25 static void sigterm_handler(int signum)
26 {
27 signal(signum, SIG_DFL);
28 kill(getpid(), signum);
29 }
30
31 __attribute__((format(printf, 1, 2))) static void ar_log(const char *format, ...)
32 {
33 va_list args;
34
35 if (!VERBOSE)
36 return;
37 va_start(args, format);
38 vprintf(format, args);
39 va_end(args);
40 fflush(stdout);
41 }
42
43 static int ar_launch(void)
44 {
45 static const char *argv[] = { AUTORANDR_PATH, "--change", "--default", "default", NULL};
46 char **comm = malloc(sizeof argv);
47 memcpy(comm, argv, sizeof argv);
48
49 pid_t pid = fork();
50 if (pid == 0) {
51 if (execve(argv[0], comm, environ) == -1) {
52 int errsv = errno;
53 fprintf(stderr, "Error executing file: %s\n", strerror(errsv));
54 exit(errsv);
55 }
56
57 exit(127);
58 } else {
59 waitpid(pid, 0, 0);
60 free(comm);
61 }
62 return 0;
63 }
64
65 int main(int argc, char **argv)
66 {
67 int help = 0;
68 int version = 0;
69 int daemonize = 0;
70
71 const struct option long_options[] = {
72 { "help", no_argument, &help, 1 },
73 { "daemonize", no_argument, &daemonize, 1 },
74 { "version", no_argument, &version, 1 },
75 { "verbose", no_argument, &VERBOSE, 1 },
76 };
77 static const char *short_options = "hd";
78
79 const char *help_str =
80 "Usage: autorandr_launcher [OPTION]\n"
81 "\n"
82 "Listens to X server screen change events and launches autorandr after an event occurs.\n"
83 "\n"
84 "\t-h,--help\t\t\tDisplay this help and exit\n"
85 "\t-d, --daemonize\t\t\tDaemonize program\n"
86 "\t--verbose\t\t\tOutput debugging information (prevents daemonizing)\n"
87 "\t--version\t\t\tDisplay version and exit\n";
88 const char *version_str = "v.5\n";
89
90 int option_index = 0;
91 int ch = 0;
92 while (ch != -1) {
93 ch = getopt_long(argc, argv, short_options, long_options,
94 &option_index);
95 switch (ch) {
96 case 'h':
97 help = 1;
98 break;
99 case 'd':
100 daemonize = 1;
101 break;
102 }
103
104 }
105
106 if (help == 1) {
107 printf("%s", help_str);
108 exit(0);
109 }
110 if (version == 1) {
111 printf("%s", version_str);
112 exit(0);
113 }
114 // Check for already running daemon?
115
116 // Daemonize
117 if (daemonize == 1 && VERBOSE != 1) {
118 struct sigaction sa;
119 sa.sa_handler = sigterm_handler;
120 sigemptyset(&sa.sa_mask);
121 sa.sa_flags = 0;
122 sigaction(SIGINT, &sa, NULL);
123 sigaction(SIGTERM, &sa, NULL);
124 sigaction(SIGQUIT, &sa, NULL);
125 signal(SIGHUP, SIG_IGN);
126 if (daemon(0, 0)) {
127 fprintf(stderr, "Failed to daemonize!\n");
128 exit(1);
129 }
130 }
131
132 int screenNum;
133 xcb_connection_t *c = xcb_connect(NULL, &screenNum);
134 int conn_error = xcb_connection_has_error(c);
135 if (conn_error) {
136 fprintf(stderr, "Connection error!\n");
137 exit(conn_error);
138 }
139 // Get the screen whose number is screenNum
140 const xcb_setup_t *setup = xcb_get_setup(c);
141 xcb_screen_iterator_t iter = xcb_setup_roots_iterator(setup);
142
143 // we want the screen at index screenNum of the iterator
144 for (int i = 0; i < screenNum; ++i) {
145 xcb_screen_next(&iter);
146 }
147 xcb_screen_t *default_screen = iter.data;
148 ar_log("Connected to server\n");
149
150 // Subscribe to screen change events
151 xcb_randr_select_input(c, default_screen->root,
152 XCB_RANDR_NOTIFY_MASK_SCREEN_CHANGE);
153 xcb_flush(c);
154
155 xcb_timestamp_t last_timestamp = (xcb_timestamp_t) 0;
156 time_t last_time = time(NULL);
157 ar_log("Waiting for event\n");
158 xcb_generic_event_t *evt;
159 while ( (evt = xcb_wait_for_event(c)) ) {
160 ar_log("Event type: %" PRIu8 "\n", evt->response_type);
161
162 if (evt->response_type) {
163 xcb_randr_screen_change_notify_event_t *randr_evt =
164 (xcb_randr_screen_change_notify_event_t *) evt;
165 time_t evt_time = time(NULL);
166 if ((randr_evt->timestamp > last_timestamp)
167 && (evt_time > last_time + 1)) {
168 ar_log("Launch autorandr!\n");
169 ar_launch();
170 last_time = evt_time;
171 last_timestamp = randr_evt->timestamp;
172 }
173 }
174
175 free(evt);
176 }
177
178 }