10 #include <sys/types.h>
15 static int VERBOSE = 0;
16 extern char **environ;
18 static void sigterm_handler(int signum)
20 signal(signum, SIG_DFL);
21 kill(getpid(), signum);
24 static void ar_log(const char *format, ...)
30 va_start(args, format);
31 vprintf(format, args);
36 static int ar_launch()
41 { "/usr/bin/autorandr", "--change", "--default", "default", NULL };
42 execve(argv[0], argv, environ);
50 int main(int argc, char **argv)
56 const struct option long_options[] = {
57 { "help", no_argument, &help, 1 },
58 { "daemonize", no_argument, &daemonize, 1 },
59 { "version", no_argument, &version, 1 },
60 { "verbose", no_argument, &VERBOSE, 1 },
62 static const char *short_options = "hd";
64 const char *help_str =
65 "Usage: autorandr_launcher [OPTION]\n"
67 "Listens to X server screen change events and launches autorandr after an event occurs.\n"
69 "\t-h,--help\t\t\tDisplay this help and exit\n"
70 "\t-d, --daemonize\t\t\tDaemonize program\n"
71 "\t--verbose\t\t\tOutput debugging information (prevents daemonizing)\n"
72 "\t--version\t\t\tDisplay version and exit\n";
73 const char *version_str = "v.5\n";
78 ch = getopt_long(argc, argv, short_options, long_options,
92 printf("%s", help_str);
96 printf("%s", version_str);
99 // Check for already running daemon?
102 if (daemonize == 1 && VERBOSE != 1) {
104 sa.sa_handler = sigterm_handler;
105 sigemptyset(&sa.sa_mask);
107 sigaction(SIGINT, &sa, NULL);
108 sigaction(SIGTERM, &sa, NULL);
109 sigaction(SIGQUIT, &sa, NULL);
110 signal(SIGHUP, SIG_IGN);
112 fprintf(stderr, "Failed to daemonize!\n");
118 xcb_connection_t *c = xcb_connect(NULL, &screenNum);
119 int conn_error = xcb_connection_has_error(c);
121 fprintf(stderr, "Connection error!\n");
124 // Get the screen whose number is screenNum
125 const xcb_setup_t *setup = xcb_get_setup(c);
126 xcb_screen_iterator_t iter = xcb_setup_roots_iterator(setup);
128 // we want the screen at index screenNum of the iterator
129 for (int i = 0; i < screenNum; ++i) {
130 xcb_screen_next(&iter);
132 xcb_screen_t *default_screen = iter.data;
133 ar_log("Connected to server\n");
135 // Subscribe to screen change events
136 xcb_randr_select_input(c, default_screen->root,
137 XCB_RANDR_NOTIFY_MASK_SCREEN_CHANGE);
140 xcb_timestamp_t last_timestamp = (xcb_timestamp_t) 0;
141 time_t last_time = time(NULL);
144 ar_log("Waiting for event\n");
145 xcb_generic_event_t *evt = xcb_wait_for_event(c);
150 // ar_log("Event type: %" PRIu8 "\n", evt->response_type);
151 // ar_log("screen change masked: %" PRIu8 "\n",
152 // evt->response_type &
153 // XCB_RANDR_NOTIFY_MASK_SCREEN_CHANGE);
155 if (evt->response_type &
156 XCB_RANDR_NOTIFY_MASK_SCREEN_CHANGE) {
157 xcb_randr_screen_change_notify_event_t *randr_evt =
158 (xcb_randr_screen_change_notify_event_t *) evt;
159 time_t evt_time = time(NULL);
160 if ((randr_evt->timestamp > last_timestamp)
161 && (evt_time > last_time + 1)) {
162 ar_log("Launch autorandr!\n");
164 last_time = evt_time;
165 last_timestamp = randr_evt->timestamp;