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