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