]> git.sur5r.net Git - i3/i3/blob - src/nc.c
68646c873ad2319dec0eaf33f9fd06633ae6ffc7
[i3/i3] / src / nc.c
1 /*
2  * vim:ts=4:sw=4:expandtab
3  */
4 #include <ev.h>
5 #include "all.h"
6
7 static int xkb_event_base;
8
9 int xkb_current_group;
10
11 extern Con *focused;
12
13 char **start_argv;
14
15 xcb_connection_t *conn;
16 xcb_event_handlers_t evenths;
17 xcb_property_handlers_t prophs;
18 xcb_atom_t atoms[NUM_ATOMS];
19
20 xcb_window_t root;
21 uint8_t root_depth;
22
23 xcb_key_symbols_t *keysyms;
24
25 /* The list of key bindings */
26 struct bindings_head *bindings;
27
28 /* The list of exec-lines */
29 struct autostarts_head autostarts = TAILQ_HEAD_INITIALIZER(autostarts);
30
31 /* The list of assignments */
32 struct assignments_head assignments = TAILQ_HEAD_INITIALIZER(assignments);
33
34 /*
35  * This callback is only a dummy, see xcb_prepare_cb and xcb_check_cb.
36  * See also man libev(3): "ev_prepare" and "ev_check" - customise your event loop
37  *
38  */
39 static void xcb_got_event(EV_P_ struct ev_io *w, int revents) {
40     /* empty, because xcb_prepare_cb and xcb_check_cb are used */
41 }
42
43 /*
44  * Flush before blocking (and waiting for new events)
45  *
46  */
47 static void xcb_prepare_cb(EV_P_ ev_prepare *w, int revents) {
48     xcb_flush(conn);
49 }
50
51 /*
52  * Instead of polling the X connection socket we leave this to
53  * xcb_poll_for_event() which knows better than we can ever know.
54  *
55  */
56 static void xcb_check_cb(EV_P_ ev_check *w, int revents) {
57     xcb_generic_event_t *event;
58
59     while ((event = xcb_poll_for_event(conn)) != NULL) {
60             xcb_event_handle(&evenths, event);
61             free(event);
62     }
63 }
64
65 int handle_map_request(void *unused, xcb_connection_t *conn, xcb_map_request_event_t *event) {
66     xcb_get_window_attributes_cookie_t cookie;
67
68     cookie = xcb_get_window_attributes_unchecked(conn, event->window);
69
70     LOG("window = 0x%08x, serial is %d.\n", event->window, event->sequence);
71     //add_ignore_event(event->sequence);
72
73     manage_window(event->window, cookie, false);
74     return 1;
75 }
76
77
78 int handle_unmap_notify_event(void *data, xcb_connection_t *conn, xcb_unmap_notify_event_t *event) {
79     LOG("unmap event for 0x%08x\n", event->window);
80     Con *con = con_by_window_id(event->window);
81     if (con == NULL) {
82         LOG("Not a managed window, ignoring\n");
83         return 1;
84     }
85
86     tree_close(con);
87     tree_render();
88     return 1;
89 }
90
91 int handle_expose_event(void *data, xcb_connection_t *conn, xcb_expose_event_t *event) {
92     Con *parent, *con;
93
94     /* event->count is the number of minimum remaining expose events for this window, so we
95        skip all events but the last one */
96     if (event->count != 0)
97             return 1;
98     LOG("expose-event, window = %08x\n", event->window);
99
100     if ((parent = con_by_frame_id(event->window)) == NULL) {
101         LOG("expose event for unknown window, ignoring\n");
102         return 1;
103     }
104
105     TAILQ_FOREACH(con, &(parent->nodes_head), nodes) {
106         LOG("expose for con %p / %s\n", con, con->name);
107         if (con->window)
108             x_draw_decoration(con);
109     }
110     xcb_flush(conn);
111
112     return 1;
113 }
114
115
116
117 void parse_command(const char *command) {
118     printf("received command: %s\n", command);
119
120     if (strcasecmp(command, "open") == 0)
121         tree_open_con(NULL);
122     else if (strcasecmp(command, "close") == 0)
123         tree_close_con();
124     else if (strcasecmp(command, "split h") == 0)
125         tree_split(focused, HORIZ);
126     else if (strcasecmp(command, "split v") == 0)
127         tree_split(focused, VERT);
128     else if (strcasecmp(command, "level up") == 0)
129         level_up();
130     else if (strcasecmp(command, "level down") == 0)
131         level_down();
132     else if (strcasecmp(command, "prev h") == 0)
133         tree_next('p', HORIZ);
134     else if (strcasecmp(command, "prev v") == 0)
135         tree_next('p', VERT);
136     else if (strcasecmp(command, "next h") == 0)
137         tree_next('n', HORIZ);
138     else if (strcasecmp(command, "next v") == 0)
139         tree_next('n', VERT);
140     else if (strncasecmp(command, "workspace ", strlen("workspace ")) == 0)
141         workspace_show(command + strlen("workspace "));
142     else if (strcasecmp(command, "stack") == 0) {
143         focused->layout = L_STACKED;
144         x_push_changes(croot);
145
146     }
147     else if (strcasecmp(command, "move before h") == 0)
148         tree_move('p', HORIZ);
149     else if (strcasecmp(command, "move before v") == 0)
150         tree_move('p', VERT);
151     else if (strcasecmp(command, "move after h") == 0)
152         tree_move('n', HORIZ);
153     else if (strcasecmp(command, "move after v") == 0)
154         tree_move('n', VERT);
155     else if (strncasecmp(command, "restore", strlen("restore")) == 0)
156         tree_append_json(command + strlen("restore "));
157     else if (strncasecmp(command, "exec", strlen("exec")) == 0)
158         start_application(command + strlen("exec "));
159     else if (strcasecmp(command, "restart") == 0)
160         i3_restart();
161     else if (strcasecmp(command, "floating") == 0)
162         toggle_floating_mode(focused, false);
163
164     tree_render();
165
166 #if 0
167     if (strcasecmp(command, "prev") == 0)
168         tree_prev(O_CURRENT);
169 #endif
170 }
171
172 int main(int argc, char *argv[]) {
173     int screens;
174     char *override_configpath = NULL;
175     bool autostart = true;
176     bool only_check_config = false;
177     bool force_xinerama = false;
178     xcb_intern_atom_cookie_t atom_cookies[NUM_ATOMS];
179     static struct option long_options[] = {
180         {"no-autostart", no_argument, 0, 'a'},
181         {"config", required_argument, 0, 'c'},
182         {"version", no_argument, 0, 'v'},
183         {"help", no_argument, 0, 'h'},
184         {"force-xinerama", no_argument, 0, 0},
185         {0, 0, 0, 0}
186     };
187     int option_index = 0, opt;
188
189     setlocale(LC_ALL, "");
190
191     /* Disable output buffering to make redirects in .xsession actually useful for debugging */
192     if (!isatty(fileno(stdout)))
193         setbuf(stdout, NULL);
194
195     start_argv = argv;
196
197     while ((opt = getopt_long(argc, argv, "c:Cvahld:V", long_options, &option_index)) != -1) {
198         switch (opt) {
199             case 'a':
200                 LOG("Autostart disabled using -a\n");
201                 autostart = false;
202                 break;
203             case 'c':
204                 override_configpath = sstrdup(optarg);
205                 break;
206             case 'C':
207                 LOG("Checking configuration file only (-C)\n");
208                 only_check_config = true;
209                 break;
210             case 'v':
211                 printf("i3 version " I3_VERSION " © 2009 Michael Stapelberg and contributors\n");
212                 exit(EXIT_SUCCESS);
213             case 'V':
214                 set_verbosity(true);
215                 break;
216             case 'd':
217                 LOG("Enabling debug loglevel %s\n", optarg);
218                 add_loglevel(optarg);
219                 break;
220             case 'l':
221                 /* DEPRECATED, ignored for the next 3 versions (3.e, 3.f, 3.g) */
222                 break;
223             case 0:
224                 if (strcmp(long_options[option_index].name, "force-xinerama") == 0) {
225                     force_xinerama = true;
226                     ELOG("Using Xinerama instead of RandR. This option should be "
227                          "avoided at all cost because it does not refresh the list "
228                          "of screens, so you cannot configure displays at runtime. "
229                          "Please check if your driver really does not support RandR "
230                          "and disable this option as soon as you can.\n");
231                     break;
232                 }
233                 /* fall-through */
234             default:
235                 fprintf(stderr, "Usage: %s [-c configfile] [-d loglevel] [-a] [-v] [-V] [-C]\n", argv[0]);
236                 fprintf(stderr, "\n");
237                 fprintf(stderr, "-a: disable autostart\n");
238                 fprintf(stderr, "-v: display version and exit\n");
239                 fprintf(stderr, "-V: enable verbose mode\n");
240                 fprintf(stderr, "-d <loglevel>: enable debug loglevel <loglevel>\n");
241                 fprintf(stderr, "-c <configfile>: use the provided configfile instead\n");
242                 fprintf(stderr, "-C: check configuration file and exit\n");
243                 fprintf(stderr, "--force-xinerama: Use Xinerama instead of RandR. This "
244                                 "option should only be used if you are stuck with the "
245                                 "nvidia closed source driver which does not support RandR.\n");
246                 exit(EXIT_FAILURE);
247         }
248     }
249
250     LOG("i3 (tree) version " I3_VERSION " starting\n");
251
252     conn = xcb_connect(NULL, &screens);
253     if (xcb_connection_has_error(conn))
254         errx(EXIT_FAILURE, "Cannot open display\n");
255
256     load_configuration(conn, override_configpath, false);
257     if (only_check_config) {
258         LOG("Done checking configuration file. Exiting.\n");
259         exit(0);
260     }
261
262     xcb_screen_t *root_screen = xcb_aux_get_screen(conn, screens);
263     root = root_screen->root;
264     root_depth = root_screen->root_depth;
265
266     uint32_t mask = XCB_CW_EVENT_MASK;
267     uint32_t values[] = { XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT |
268                           XCB_EVENT_MASK_STRUCTURE_NOTIFY |         /* when the user adds a screen (e.g. video
269                                                                            projector), the root window gets a
270                                                                            ConfigureNotify */
271                           XCB_EVENT_MASK_POINTER_MOTION |
272                           XCB_EVENT_MASK_PROPERTY_CHANGE |
273                           XCB_EVENT_MASK_ENTER_WINDOW };
274     xcb_void_cookie_t cookie;
275     cookie = xcb_change_window_attributes_checked(conn, root, mask, values);
276     check_error(conn, cookie, "Another window manager seems to be running");
277
278     /* Place requests for the atoms we need as soon as possible */
279     #define REQUEST_ATOM(name) atom_cookies[name] = xcb_intern_atom(conn, 0, strlen(#name), #name);
280
281     REQUEST_ATOM(_NET_SUPPORTED);
282     REQUEST_ATOM(_NET_WM_STATE_FULLSCREEN);
283     REQUEST_ATOM(_NET_SUPPORTING_WM_CHECK);
284     REQUEST_ATOM(_NET_WM_NAME);
285     REQUEST_ATOM(_NET_WM_STATE);
286     REQUEST_ATOM(_NET_WM_WINDOW_TYPE);
287     REQUEST_ATOM(_NET_WM_DESKTOP);
288     REQUEST_ATOM(_NET_WM_WINDOW_TYPE_DOCK);
289     REQUEST_ATOM(_NET_WM_WINDOW_TYPE_DIALOG);
290     REQUEST_ATOM(_NET_WM_WINDOW_TYPE_UTILITY);
291     REQUEST_ATOM(_NET_WM_WINDOW_TYPE_TOOLBAR);
292     REQUEST_ATOM(_NET_WM_WINDOW_TYPE_SPLASH);
293     REQUEST_ATOM(_NET_WM_STRUT_PARTIAL);
294     REQUEST_ATOM(WM_PROTOCOLS);
295     REQUEST_ATOM(WM_DELETE_WINDOW);
296     REQUEST_ATOM(UTF8_STRING);
297     REQUEST_ATOM(WM_STATE);
298     REQUEST_ATOM(WM_CLIENT_LEADER);
299     REQUEST_ATOM(_NET_CURRENT_DESKTOP);
300     REQUEST_ATOM(_NET_ACTIVE_WINDOW);
301     REQUEST_ATOM(_NET_WORKAREA);
302
303     memset(&evenths, 0, sizeof(xcb_event_handlers_t));
304     memset(&prophs, 0, sizeof(xcb_property_handlers_t));
305
306     xcb_event_handlers_init(conn, &evenths);
307     xcb_property_handlers_init(&prophs, &evenths);
308     xcb_event_set_key_press_handler(&evenths, handle_key_press, NULL);
309
310     xcb_event_set_button_press_handler(&evenths, handle_button_press, NULL);
311
312     xcb_event_set_map_request_handler(&evenths, handle_map_request, NULL);
313
314     xcb_event_set_unmap_notify_handler(&evenths, handle_unmap_notify_event, NULL);
315     //xcb_event_set_destroy_notify_handler(&evenths, handle_destroy_notify_event, NULL);
316
317     xcb_event_set_expose_handler(&evenths, handle_expose_event, NULL);
318
319     /* Enter window = user moved his mouse over the window */
320     xcb_event_set_enter_notify_handler(&evenths, handle_enter_notify, NULL);
321
322
323     /* Setup NetWM atoms */
324     #define GET_ATOM(name) \
325         do { \
326             xcb_intern_atom_reply_t *reply = xcb_intern_atom_reply(conn, atom_cookies[name], NULL); \
327             if (!reply) { \
328                 ELOG("Could not get atom " #name "\n"); \
329                 exit(-1); \
330             } \
331             atoms[name] = reply->atom; \
332             free(reply); \
333         } while (0)
334
335     GET_ATOM(_NET_SUPPORTED);
336     GET_ATOM(_NET_WM_STATE_FULLSCREEN);
337     GET_ATOM(_NET_SUPPORTING_WM_CHECK);
338     GET_ATOM(_NET_WM_NAME);
339     GET_ATOM(_NET_WM_STATE);
340     GET_ATOM(_NET_WM_WINDOW_TYPE);
341     GET_ATOM(_NET_WM_DESKTOP);
342     GET_ATOM(_NET_WM_WINDOW_TYPE_DOCK);
343     GET_ATOM(_NET_WM_WINDOW_TYPE_DIALOG);
344     GET_ATOM(_NET_WM_WINDOW_TYPE_UTILITY);
345     GET_ATOM(_NET_WM_WINDOW_TYPE_TOOLBAR);
346     GET_ATOM(_NET_WM_WINDOW_TYPE_SPLASH);
347     GET_ATOM(_NET_WM_STRUT_PARTIAL);
348     GET_ATOM(WM_PROTOCOLS);
349     GET_ATOM(WM_DELETE_WINDOW);
350     GET_ATOM(UTF8_STRING);
351     GET_ATOM(WM_STATE);
352     GET_ATOM(WM_CLIENT_LEADER);
353     GET_ATOM(_NET_CURRENT_DESKTOP);
354     GET_ATOM(_NET_ACTIVE_WINDOW);
355     GET_ATOM(_NET_WORKAREA);
356
357     /* Watch _NET_WM_NAME (title of the window encoded in UTF-8) */
358     xcb_property_set_handler(&prophs, atoms[_NET_WM_NAME], 128, handle_windowname_change, NULL);
359
360     /* Watch WM_NAME (title of the window encoded in COMPOUND_TEXT) */
361     xcb_watch_wm_name(&prophs, 128, handle_windowname_change_legacy, NULL);
362
363
364     keysyms = xcb_key_symbols_alloc(conn);
365
366     xcb_get_numlock_mask(conn);
367
368     translate_keysyms();
369     grab_all_keys(conn, false);
370
371     int randr_base;
372     if (force_xinerama) {
373         xinerama_init();
374     } else {
375         DLOG("Checking for XRandR...\n");
376         randr_init(&randr_base);
377
378 #if 0
379         xcb_event_set_handler(&evenths,
380                               randr_base + XCB_RANDR_SCREEN_CHANGE_NOTIFY,
381                               handle_screen_change,
382                               NULL);
383 #endif
384     }
385
386     if (!tree_restore())
387         tree_init();
388     tree_render();
389
390     /* proof-of-concept for assignments */
391     Con *ws = workspace_get("3");
392
393     Match *current_swallow = scalloc(sizeof(Match));
394     TAILQ_INSERT_TAIL(&(ws->swallow_head), current_swallow, matches);
395
396     current_swallow->insert_where = M_ACTIVE;
397     current_swallow->class = strdup("xterm");
398
399     struct ev_loop *loop = ev_loop_new(0);
400     if (loop == NULL)
401             die("Could not initialize libev. Bad LIBEV_FLAGS?\n");
402
403     /* Create the UNIX domain socket for IPC */
404     if (config.ipc_socket_path != NULL) {
405         int ipc_socket = ipc_create_socket(config.ipc_socket_path);
406         if (ipc_socket == -1) {
407             ELOG("Could not create the IPC socket, IPC disabled\n");
408         } else {
409             struct ev_io *ipc_io = scalloc(sizeof(struct ev_io));
410             ev_io_init(ipc_io, ipc_new_client, ipc_socket, EV_READ);
411             ev_io_start(loop, ipc_io);
412         }
413     }
414
415     struct ev_io *xcb_watcher = scalloc(sizeof(struct ev_io));
416     struct ev_check *xcb_check = scalloc(sizeof(struct ev_check));
417     struct ev_prepare *xcb_prepare = scalloc(sizeof(struct ev_prepare));
418
419     ev_io_init(xcb_watcher, xcb_got_event, xcb_get_file_descriptor(conn), EV_READ);
420     ev_io_start(loop, xcb_watcher);
421
422     ev_check_init(xcb_check, xcb_check_cb);
423     ev_check_start(loop, xcb_check);
424
425     ev_prepare_init(xcb_prepare, xcb_prepare_cb);
426     ev_prepare_start(loop, xcb_prepare);
427
428     xcb_flush(conn);
429
430     manage_existing_windows(root);
431
432     ev_loop(loop, 0);
433 }