]> git.sur5r.net Git - i3/i3/blob - src/main.c
Re-Implement support for RandR changes
[i3/i3] / src / main.c
1 /*
2  * vim:ts=4:sw=4:expandtab
3  */
4 #include <ev.h>
5 #include <fcntl.h>
6 #include <limits.h>
7 #include "all.h"
8
9 static int xkb_event_base;
10
11 int xkb_current_group;
12
13 extern Con *focused;
14
15 char **start_argv;
16
17 xcb_connection_t *conn;
18 xcb_event_handlers_t evenths;
19 xcb_property_handlers_t prophs;
20 xcb_atom_t atoms[NUM_ATOMS];
21
22 xcb_window_t root;
23 uint8_t root_depth;
24
25 xcb_key_symbols_t *keysyms;
26
27 /* Those are our connections to X11 for use with libXcursor and XKB */
28 Display *xlibdpy, *xkbdpy;
29
30 /* The list of key bindings */
31 struct bindings_head *bindings;
32
33 /* The list of exec-lines */
34 struct autostarts_head autostarts = TAILQ_HEAD_INITIALIZER(autostarts);
35
36 /* The list of assignments */
37 struct assignments_head assignments = TAILQ_HEAD_INITIALIZER(assignments);
38
39 /* We hope that those are supported and set them to true */
40 bool xcursor_supported = true;
41 bool xkb_supported = true;
42
43 /*
44  * This callback is only a dummy, see xcb_prepare_cb and xcb_check_cb.
45  * See also man libev(3): "ev_prepare" and "ev_check" - customise your event loop
46  *
47  */
48 static void xcb_got_event(EV_P_ struct ev_io *w, int revents) {
49     /* empty, because xcb_prepare_cb and xcb_check_cb are used */
50 }
51
52 /*
53  * Flush before blocking (and waiting for new events)
54  *
55  */
56 static void xcb_prepare_cb(EV_P_ ev_prepare *w, int revents) {
57     xcb_flush(conn);
58 }
59
60 /*
61  * Instead of polling the X connection socket we leave this to
62  * xcb_poll_for_event() which knows better than we can ever know.
63  *
64  */
65 static void xcb_check_cb(EV_P_ ev_check *w, int revents) {
66     xcb_generic_event_t *event;
67
68     while ((event = xcb_poll_for_event(conn)) != NULL) {
69             xcb_event_handle(&evenths, event);
70             free(event);
71     }
72 }
73
74 int main(int argc, char *argv[]) {
75     //parse_cmd("[ foo ] attach, attach ; focus");
76     int screens;
77     char *override_configpath = NULL;
78     bool autostart = true;
79     char *layout_path = NULL;
80     bool delete_layout_path;
81     bool only_check_config = false;
82     bool force_xinerama = false;
83     xcb_intern_atom_cookie_t atom_cookies[NUM_ATOMS];
84     static struct option long_options[] = {
85         {"no-autostart", no_argument, 0, 'a'},
86         {"config", required_argument, 0, 'c'},
87         {"version", no_argument, 0, 'v'},
88         {"help", no_argument, 0, 'h'},
89         {"layout", required_argument, 0, 'L'},
90         {"restart", required_argument, 0, 0},
91         {"force-xinerama", no_argument, 0, 0},
92         {0, 0, 0, 0}
93     };
94     int option_index = 0, opt;
95
96     setlocale(LC_ALL, "");
97
98     /* Disable output buffering to make redirects in .xsession actually useful for debugging */
99     if (!isatty(fileno(stdout)))
100         setbuf(stdout, NULL);
101
102     start_argv = argv;
103
104     while ((opt = getopt_long(argc, argv, "c:CvaL:hld:V", long_options, &option_index)) != -1) {
105         switch (opt) {
106             case 'a':
107                 LOG("Autostart disabled using -a\n");
108                 autostart = false;
109                 break;
110             case 'L':
111                 layout_path = sstrdup(optarg);
112                 delete_layout_path = false;
113                 break;
114             case 'c':
115                 override_configpath = sstrdup(optarg);
116                 break;
117             case 'C':
118                 LOG("Checking configuration file only (-C)\n");
119                 only_check_config = true;
120                 break;
121             case 'v':
122                 printf("i3 version " I3_VERSION " © 2009 Michael Stapelberg and contributors\n");
123                 exit(EXIT_SUCCESS);
124             case 'V':
125                 set_verbosity(true);
126                 break;
127             case 'd':
128                 LOG("Enabling debug loglevel %s\n", optarg);
129                 add_loglevel(optarg);
130                 break;
131             case 'l':
132                 /* DEPRECATED, ignored for the next 3 versions (3.e, 3.f, 3.g) */
133                 break;
134             case 0:
135                 if (strcmp(long_options[option_index].name, "force-xinerama") == 0) {
136                     force_xinerama = true;
137                     ELOG("Using Xinerama instead of RandR. This option should be "
138                          "avoided at all cost because it does not refresh the list "
139                          "of screens, so you cannot configure displays at runtime. "
140                          "Please check if your driver really does not support RandR "
141                          "and disable this option as soon as you can.\n");
142                     break;
143                 } else if (strcmp(long_options[option_index].name, "restart") == 0) {
144                     layout_path = sstrdup(optarg);
145                     delete_layout_path = true;
146                     break;
147                 }
148                 /* fall-through */
149             default:
150                 fprintf(stderr, "Usage: %s [-c configfile] [-d loglevel] [-a] [-v] [-V] [-C]\n", argv[0]);
151                 fprintf(stderr, "\n");
152                 fprintf(stderr, "-a: disable autostart\n");
153                 fprintf(stderr, "-L <layoutfile>: load the layout from <layoutfile>\n");
154                 fprintf(stderr, "-v: display version and exit\n");
155                 fprintf(stderr, "-V: enable verbose mode\n");
156                 fprintf(stderr, "-d <loglevel>: enable debug loglevel <loglevel>\n");
157                 fprintf(stderr, "-c <configfile>: use the provided configfile instead\n");
158                 fprintf(stderr, "-C: check configuration file and exit\n");
159                 fprintf(stderr, "--force-xinerama: Use Xinerama instead of RandR. This "
160                                 "option should only be used if you are stuck with the "
161                                 "nvidia closed source driver which does not support RandR.\n");
162                 exit(EXIT_FAILURE);
163         }
164     }
165
166     LOG("i3 (tree) version " I3_VERSION " starting\n");
167
168     conn = xcb_connect(NULL, &screens);
169     if (xcb_connection_has_error(conn))
170         errx(EXIT_FAILURE, "Cannot open display\n");
171
172     load_configuration(conn, override_configpath, false);
173     if (only_check_config) {
174         LOG("Done checking configuration file. Exiting.\n");
175         exit(0);
176     }
177
178     xcb_screen_t *root_screen = xcb_aux_get_screen(conn, screens);
179     root = root_screen->root;
180     root_depth = root_screen->root_depth;
181
182     uint32_t mask = XCB_CW_EVENT_MASK;
183     uint32_t values[] = { XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT |
184                           XCB_EVENT_MASK_STRUCTURE_NOTIFY |         /* when the user adds a screen (e.g. video
185                                                                            projector), the root window gets a
186                                                                            ConfigureNotify */
187                           XCB_EVENT_MASK_POINTER_MOTION |
188                           XCB_EVENT_MASK_PROPERTY_CHANGE |
189                           XCB_EVENT_MASK_ENTER_WINDOW };
190     xcb_void_cookie_t cookie;
191     cookie = xcb_change_window_attributes_checked(conn, root, mask, values);
192     check_error(conn, cookie, "Another window manager seems to be running");
193
194     /* Place requests for the atoms we need as soon as possible */
195     #define REQUEST_ATOM(name) atom_cookies[name] = xcb_intern_atom(conn, 0, strlen(#name), #name);
196
197     REQUEST_ATOM(_NET_SUPPORTED);
198     REQUEST_ATOM(_NET_WM_STATE_FULLSCREEN);
199     REQUEST_ATOM(_NET_SUPPORTING_WM_CHECK);
200     REQUEST_ATOM(_NET_WM_NAME);
201     REQUEST_ATOM(_NET_WM_STATE);
202     REQUEST_ATOM(_NET_WM_WINDOW_TYPE);
203     REQUEST_ATOM(_NET_WM_DESKTOP);
204     REQUEST_ATOM(_NET_WM_WINDOW_TYPE_DOCK);
205     REQUEST_ATOM(_NET_WM_WINDOW_TYPE_DIALOG);
206     REQUEST_ATOM(_NET_WM_WINDOW_TYPE_UTILITY);
207     REQUEST_ATOM(_NET_WM_WINDOW_TYPE_TOOLBAR);
208     REQUEST_ATOM(_NET_WM_WINDOW_TYPE_SPLASH);
209     REQUEST_ATOM(_NET_WM_STRUT_PARTIAL);
210     REQUEST_ATOM(WM_PROTOCOLS);
211     REQUEST_ATOM(WM_DELETE_WINDOW);
212     REQUEST_ATOM(UTF8_STRING);
213     REQUEST_ATOM(WM_STATE);
214     REQUEST_ATOM(WM_CLIENT_LEADER);
215     REQUEST_ATOM(_NET_CURRENT_DESKTOP);
216     REQUEST_ATOM(_NET_ACTIVE_WINDOW);
217     REQUEST_ATOM(_NET_WORKAREA);
218
219     /* Initialize the Xlib connection */
220     xlibdpy = xkbdpy = XOpenDisplay(NULL);
221
222     /* Try to load the X cursors and initialize the XKB extension */
223     if (xlibdpy == NULL) {
224         ELOG("ERROR: XOpenDisplay() failed, disabling libXcursor/XKB support\n");
225         xcursor_supported = false;
226         xkb_supported = false;
227     } else if (fcntl(ConnectionNumber(xlibdpy), F_SETFD, FD_CLOEXEC) == -1) {
228         ELOG("Could not set FD_CLOEXEC on xkbdpy\n");
229         return 1;
230     } else {
231         xcursor_load_cursors();
232         /*init_xkb();*/
233     }
234
235     memset(&evenths, 0, sizeof(xcb_event_handlers_t));
236     memset(&prophs, 0, sizeof(xcb_property_handlers_t));
237
238     xcb_event_handlers_init(conn, &evenths);
239     xcb_property_handlers_init(&prophs, &evenths);
240     xcb_event_set_key_press_handler(&evenths, handle_key_press, NULL);
241
242     xcb_event_set_button_press_handler(&evenths, handle_button_press, NULL);
243
244     xcb_event_set_map_request_handler(&evenths, handle_map_request, NULL);
245
246     xcb_event_set_unmap_notify_handler(&evenths, handle_unmap_notify_event, NULL);
247     xcb_event_set_destroy_notify_handler(&evenths, handle_destroy_notify_event, NULL);
248
249     xcb_event_set_expose_handler(&evenths, handle_expose_event, NULL);
250
251     xcb_event_set_motion_notify_handler(&evenths, handle_motion_notify, NULL);
252
253     /* Enter window = user moved his mouse over the window */
254     xcb_event_set_enter_notify_handler(&evenths, handle_enter_notify, NULL);
255
256     /* Client message are sent to the root window. The only interesting client message
257        for us is _NET_WM_STATE, we honour _NET_WM_STATE_FULLSCREEN */
258     xcb_event_set_client_message_handler(&evenths, handle_client_message, NULL);
259
260     /* Configure request = window tried to change size on its own */
261     xcb_event_set_configure_request_handler(&evenths, handle_configure_request, NULL);
262
263     /* Setup NetWM atoms */
264     #define GET_ATOM(name) \
265         do { \
266             xcb_intern_atom_reply_t *reply = xcb_intern_atom_reply(conn, atom_cookies[name], NULL); \
267             if (!reply) { \
268                 ELOG("Could not get atom " #name "\n"); \
269                 exit(-1); \
270             } \
271             atoms[name] = reply->atom; \
272             free(reply); \
273         } while (0)
274
275     GET_ATOM(_NET_SUPPORTED);
276     GET_ATOM(_NET_WM_STATE_FULLSCREEN);
277     GET_ATOM(_NET_SUPPORTING_WM_CHECK);
278     GET_ATOM(_NET_WM_NAME);
279     GET_ATOM(_NET_WM_STATE);
280     GET_ATOM(_NET_WM_WINDOW_TYPE);
281     GET_ATOM(_NET_WM_DESKTOP);
282     GET_ATOM(_NET_WM_WINDOW_TYPE_DOCK);
283     GET_ATOM(_NET_WM_WINDOW_TYPE_DIALOG);
284     GET_ATOM(_NET_WM_WINDOW_TYPE_UTILITY);
285     GET_ATOM(_NET_WM_WINDOW_TYPE_TOOLBAR);
286     GET_ATOM(_NET_WM_WINDOW_TYPE_SPLASH);
287     GET_ATOM(_NET_WM_STRUT_PARTIAL);
288     GET_ATOM(WM_PROTOCOLS);
289     GET_ATOM(WM_DELETE_WINDOW);
290     GET_ATOM(UTF8_STRING);
291     GET_ATOM(WM_STATE);
292     GET_ATOM(WM_CLIENT_LEADER);
293     GET_ATOM(_NET_CURRENT_DESKTOP);
294     GET_ATOM(_NET_ACTIVE_WINDOW);
295     GET_ATOM(_NET_WORKAREA);
296
297     /* Watch _NET_WM_NAME (title of the window encoded in UTF-8) */
298     xcb_property_set_handler(&prophs, atoms[_NET_WM_NAME], 128, handle_windowname_change, NULL);
299
300     /* Watch WM_HINTS (contains the urgent property) */
301     xcb_property_set_handler(&prophs, WM_HINTS, UINT_MAX, handle_hints, NULL);
302
303     /* Watch WM_NAME (title of the window encoded in COMPOUND_TEXT) */
304     xcb_watch_wm_name(&prophs, 128, handle_windowname_change_legacy, NULL);
305
306     /* Watch WM_NORMAL_HINTS (aspect ratio, size increments, …) */
307     xcb_property_set_handler(&prophs, WM_NORMAL_HINTS, UINT_MAX, handle_normal_hints, NULL);
308
309     /* Watch WM_CLIENT_LEADER (= logical parent window for toolbars etc.) */
310     xcb_property_set_handler(&prophs, atoms[WM_CLIENT_LEADER], UINT_MAX, handle_clientleader_change, NULL);
311
312     /* Watch WM_TRANSIENT_FOR property (to which client this popup window belongs) */
313     xcb_property_set_handler(&prophs, WM_TRANSIENT_FOR, UINT_MAX, handle_transient_for, NULL);
314
315     /* Set up the atoms we support */
316     xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root, atoms[_NET_SUPPORTED], ATOM, 32, 7, atoms);
317     /* Set up the window manager’s name */
318     xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root, atoms[_NET_SUPPORTING_WM_CHECK], WINDOW, 32, 1, &root);
319     xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root, atoms[_NET_WM_NAME], atoms[UTF8_STRING], 8, strlen("i3"), "i3");
320
321     keysyms = xcb_key_symbols_alloc(conn);
322
323     xcb_get_numlock_mask(conn);
324
325     translate_keysyms();
326     grab_all_keys(conn, false);
327
328     bool needs_tree_init = true;
329     if (layout_path) {
330         LOG("Trying to restore the layout from %s...", layout_path);
331         needs_tree_init = !tree_restore(layout_path);
332         if (delete_layout_path)
333             unlink(layout_path);
334         free(layout_path);
335     }
336     if (needs_tree_init)
337         tree_init();
338
339     int randr_base;
340     if (force_xinerama) {
341         xinerama_init();
342     } else {
343         DLOG("Checking for XRandR...\n");
344         randr_init(&randr_base);
345
346         xcb_event_set_handler(&evenths,
347                               randr_base + XCB_RANDR_SCREEN_CHANGE_NOTIFY,
348                               handle_screen_change,
349                               NULL);
350     }
351
352     tree_render();
353
354     struct ev_loop *loop = ev_loop_new(0);
355     if (loop == NULL)
356             die("Could not initialize libev. Bad LIBEV_FLAGS?\n");
357
358     /* Create the UNIX domain socket for IPC */
359     if (config.ipc_socket_path != NULL) {
360         int ipc_socket = ipc_create_socket(config.ipc_socket_path);
361         if (ipc_socket == -1) {
362             ELOG("Could not create the IPC socket, IPC disabled\n");
363         } else {
364             struct ev_io *ipc_io = scalloc(sizeof(struct ev_io));
365             ev_io_init(ipc_io, ipc_new_client, ipc_socket, EV_READ);
366             ev_io_start(loop, ipc_io);
367         }
368     }
369
370     struct ev_io *xcb_watcher = scalloc(sizeof(struct ev_io));
371     struct ev_check *xcb_check = scalloc(sizeof(struct ev_check));
372     struct ev_prepare *xcb_prepare = scalloc(sizeof(struct ev_prepare));
373
374     ev_io_init(xcb_watcher, xcb_got_event, xcb_get_file_descriptor(conn), EV_READ);
375     ev_io_start(loop, xcb_watcher);
376
377     ev_check_init(xcb_check, xcb_check_cb);
378     ev_check_start(loop, xcb_check);
379
380     ev_prepare_init(xcb_prepare, xcb_prepare_cb);
381     ev_prepare_start(loop, xcb_prepare);
382
383     xcb_flush(conn);
384
385     manage_existing_windows(root);
386
387     setup_signal_handler();
388
389     /* Ignore SIGPIPE to survive errors when an IPC client disconnects
390      * while we are sending him a message */
391     signal(SIGPIPE, SIG_IGN);
392
393     /* Autostarting exec-lines */
394     if (autostart) {
395         struct Autostart *exec;
396         TAILQ_FOREACH(exec, &autostarts, autostarts) {
397             LOG("auto-starting %s\n", exec->command);
398             start_application(exec->command);
399         }
400     }
401
402     ev_loop(loop, 0);
403 }