key: move grabbing code to window
[awesome.git] / awesome.c
blob157880def4e5d60f27265b325ebf74c2dee5237d
1 /*
2 * awesome.c - awesome main functions
4 * Copyright © 2007-2008 Julien Danjou <julien@danjou.info>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include <getopt.h>
24 #include <locale.h>
25 #include <stdio.h>
26 #include <unistd.h>
27 #include <signal.h>
29 #include <xcb/xtest.h>
30 #include <xcb/xcb_event.h>
32 #include "awesome.h"
33 #include "spawn.h"
34 #include "client.h"
35 #include "window.h"
36 #include "ewmh.h"
37 #include "dbus.h"
38 #include "systray.h"
39 #include "event.h"
40 #include "property.h"
41 #include "screen.h"
42 #include "titlebar.h"
43 #include "luaa.h"
44 #include "common/version.h"
45 #include "common/atoms.h"
46 #include "common/xcursor.h"
47 #include "common/xutil.h"
48 #include "common/backtrace.h"
50 awesome_t globalconf;
52 typedef struct
54 xcb_window_t id;
55 xcb_query_tree_cookie_t tree_cookie;
56 } root_win_t;
58 /** Call before exiting.
60 void
61 awesome_atexit(void)
63 int screen_nbr, nscreens;
65 if(globalconf.hooks.exit != LUA_REFNIL)
66 luaA_dofunction_from_registry(globalconf.L, globalconf.hooks.exit, 0, 0);
68 signal_object_emit(globalconf.L, &global_signals, "exit", 0);
70 a_dbus_cleanup();
72 /* reparent systray windows, otherwise they may die with their master */
73 for(int i = 0; i < globalconf.embedded.len; i++)
75 xcb_screen_t *s = xutil_screen_get(globalconf.connection,
76 globalconf.embedded.tab[i].phys_screen);
77 xembed_window_unembed(globalconf.connection, globalconf.embedded.tab[i].win, s->root);
80 /* do this only for real screen */
81 const xcb_setup_t *setup = xcb_get_setup(globalconf.connection);
82 nscreens = setup ? xcb_setup_roots_length(setup) : -1;
83 for(screen_nbr = 0;
84 screen_nbr < nscreens;
85 screen_nbr++)
86 systray_cleanup(screen_nbr);
88 /* remap all clients since some WM won't handle them otherwise */
89 foreach(c, globalconf.clients)
91 client_unban(*c);
92 titlebar_client_detach(*c);
95 xcb_flush(globalconf.connection);
97 xcb_disconnect(globalconf.connection);
99 /* Close Lua */
100 lua_close(globalconf.L);
103 /** Scan X to find windows to manage.
105 static void
106 scan(void)
108 int i, phys_screen, tree_c_len;
109 const int screen_max = xcb_setup_roots_length(xcb_get_setup(globalconf.connection));
110 root_win_t root_wins[screen_max];
111 xcb_query_tree_reply_t *tree_r;
112 xcb_window_t *wins = NULL;
113 xcb_get_window_attributes_reply_t *attr_r;
114 xcb_get_geometry_reply_t *geom_r;
115 long state;
117 for(phys_screen = 0; phys_screen < screen_max; phys_screen++)
119 /* Get the root window ID associated to this screen */
120 root_wins[phys_screen].id = xutil_screen_get(globalconf.connection, phys_screen)->root;
122 /* Get the window tree associated to this screen */
123 root_wins[phys_screen].tree_cookie = xcb_query_tree_unchecked(globalconf.connection,
124 root_wins[phys_screen].id);
127 for(phys_screen = 0; phys_screen < screen_max; phys_screen++)
129 tree_r = xcb_query_tree_reply(globalconf.connection,
130 root_wins[phys_screen].tree_cookie,
131 NULL);
133 if(!tree_r)
134 continue;
136 /* Get the tree of the children windows of the current root window */
137 if(!(wins = xcb_query_tree_children(tree_r)))
138 fatal("cannot get tree children");
140 tree_c_len = xcb_query_tree_children_length(tree_r);
141 xcb_get_window_attributes_cookie_t attr_wins[tree_c_len];
142 xcb_get_property_cookie_t state_wins[tree_c_len];
144 for(i = 0; i < tree_c_len; i++)
146 attr_wins[i] = xcb_get_window_attributes_unchecked(globalconf.connection,
147 wins[i]);
149 state_wins[i] = window_state_get_unchecked(wins[i]);
152 xcb_get_geometry_cookie_t *geom_wins[tree_c_len];
154 for(i = 0; i < tree_c_len; i++)
156 attr_r = xcb_get_window_attributes_reply(globalconf.connection,
157 attr_wins[i],
158 NULL);
160 state = window_state_get_reply(state_wins[i]);
162 if(!attr_r || attr_r->override_redirect
163 || attr_r->map_state == XCB_MAP_STATE_UNVIEWABLE
164 || state == XCB_WM_STATE_WITHDRAWN)
166 geom_wins[i] = NULL;
167 p_delete(&attr_r);
168 continue;
171 p_delete(&attr_r);
173 /* Get the geometry of the current window */
174 geom_wins[i] = p_alloca(xcb_get_geometry_cookie_t, 1);
175 *(geom_wins[i]) = xcb_get_geometry_unchecked(globalconf.connection, wins[i]);
178 for(i = 0; i < tree_c_len; i++)
180 if(!geom_wins[i] || !(geom_r = xcb_get_geometry_reply(globalconf.connection,
181 *(geom_wins[i]), NULL)))
182 continue;
184 /* The window can be mapped, so force it to be undrawn for startup */
185 xcb_unmap_window(globalconf.connection, wins[i]);
187 client_manage(wins[i], geom_r, phys_screen, true);
189 p_delete(&geom_r);
192 p_delete(&tree_r);
196 static void
197 a_refresh_cb(EV_P_ ev_prepare *w, int revents)
199 awesome_refresh();
202 static void
203 a_xcb_check_cb(EV_P_ ev_check *w, int revents)
205 xcb_generic_event_t *mouse = NULL, *event;
207 while((event = xcb_poll_for_event(globalconf.connection)))
209 /* We will treat mouse events later.
210 * We cannot afford to treat all mouse motion events,
211 * because that would be too much CPU intensive, so we just
212 * take the last we get after a bunch of events. */
213 if(XCB_EVENT_RESPONSE_TYPE(event) == XCB_MOTION_NOTIFY)
215 p_delete(&mouse);
216 mouse = event;
218 else
220 xcb_event_handle(&globalconf.evenths, event);
221 p_delete(&event);
225 if(mouse)
227 xcb_event_handle(&globalconf.evenths, mouse);
228 p_delete(&mouse);
232 static void
233 a_xcb_io_cb(EV_P_ ev_io *w, int revents)
235 /* empty */
238 /** Startup Error handler to check if another window manager
239 * is already running.
240 * \param data Additional optional parameters data.
241 * \param c X connection.
242 * \param error Error event.
244 static int __attribute__ ((noreturn))
245 xerrorstart(void * data __attribute__ ((unused)),
246 xcb_connection_t * c __attribute__ ((unused)),
247 xcb_generic_error_t * error __attribute__ ((unused)))
249 fatal("another window manager is already running");
252 static void
253 signal_fatal(int signum)
255 buffer_t buf;
256 backtrace_get(&buf);
257 fatal("dumping backtrace\n%s", buf.s);
260 /** Function to exit on some signals.
261 * \param w the signal received, unused
262 * \param revents currently unused
264 static void
265 exit_on_signal(EV_P_ ev_signal *w, int revents)
267 ev_unloop(EV_A_ 1);
270 void
271 awesome_restart(void)
273 awesome_atexit();
274 a_exec(globalconf.argv);
277 /** Function to restart awesome on some signals.
278 * \param w the signal received, unused
279 * \param revents Currently unused
281 static void
282 restart_on_signal(EV_P_ ev_signal *w, int revents)
284 awesome_restart();
287 /** \brief awesome xerror function.
288 * There's no way to check accesses to destroyed windows, thus those cases are
289 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
290 * default error handler, which may call exit.
291 * \param data Currently unused.
292 * \param c The connection to the X server.
293 * \param e The error event.
294 * \return 0 if no error, or xerror's xlib return status.
296 static int
297 xerror(void *data __attribute__ ((unused)),
298 xcb_connection_t *c __attribute__ ((unused)),
299 xcb_generic_error_t *e)
301 /* ignore this */
302 if(e->error_code == XCB_EVENT_ERROR_BAD_WINDOW
303 || (e->error_code == XCB_EVENT_ERROR_BAD_MATCH
304 && e->major_code == XCB_SET_INPUT_FOCUS)
305 || (e->error_code == XCB_EVENT_ERROR_BAD_VALUE
306 && e->major_code == XCB_KILL_CLIENT)
307 || (e->major_code == XCB_CONFIGURE_WINDOW
308 && e->error_code == XCB_EVENT_ERROR_BAD_MATCH))
309 return 0;
311 warn("X error: request=%s, error=%s",
312 xcb_event_get_request_label(e->major_code),
313 xcb_event_get_error_label(e->error_code));
315 return 0;
318 /** Print help and exit(2) with given exit_code.
319 * \param exit_code The exit code.
321 static void __attribute__ ((noreturn))
322 exit_help(int exit_code)
324 FILE *outfile = (exit_code == EXIT_SUCCESS) ? stdout : stderr;
325 fprintf(outfile,
326 "Usage: awesome [OPTION]\n\
327 -h, --help show help\n\
328 -v, --version show version\n\
329 -c, --config FILE configuration file to use\n\
330 -k, --check check configuration file syntax\n");
331 exit(exit_code);
334 /** Hello, this is main.
335 * \param argc Who knows.
336 * \param argv Who knows.
337 * \return EXIT_SUCCESS I hope.
340 main(int argc, char **argv)
342 const char *confpath = NULL;
343 int xfd, i, screen_nbr, opt, colors_nbr;
344 xcolor_init_request_t colors_reqs[2];
345 ssize_t cmdlen = 1;
346 xdgHandle xdg;
347 static struct option long_options[] =
349 { "help", 0, NULL, 'h' },
350 { "version", 0, NULL, 'v' },
351 { "config", 1, NULL, 'c' },
352 { "check", 0, NULL, 'k' },
353 { NULL, 0, NULL, 0 }
356 /* event loop watchers */
357 ev_io xio = { .fd = -1 };
358 ev_check xcheck;
359 ev_prepare a_refresh;
360 ev_signal sigint;
361 ev_signal sigterm;
362 ev_signal sighup;
364 /* clear the globalconf structure */
365 p_clear(&globalconf, 1);
366 globalconf.keygrabber = LUA_REFNIL;
367 globalconf.mousegrabber = LUA_REFNIL;
369 /* save argv */
370 for(i = 0; i < argc; i++)
371 cmdlen += a_strlen(argv[i]) + 1;
373 globalconf.argv = p_new(char, cmdlen);
374 a_strcpy(globalconf.argv, cmdlen, argv[0]);
376 for(i = 1; i < argc; i++)
378 a_strcat(globalconf.argv, cmdlen, " ");
379 a_strcat(globalconf.argv, cmdlen, argv[i]);
382 /* Text won't be printed correctly otherwise */
383 setlocale(LC_CTYPE, "");
385 /* Get XDG basedir data */
386 xdgInitHandle(&xdg);
388 /* init lua */
389 luaA_init(&xdg);
391 /* check args */
392 while((opt = getopt_long(argc, argv, "vhkc:",
393 long_options, NULL)) != -1)
394 switch(opt)
396 case 'v':
397 eprint_version();
398 break;
399 case 'h':
400 exit_help(EXIT_SUCCESS);
401 break;
402 case 'k':
403 if(!luaA_parserc(&xdg, confpath, false))
405 fprintf(stderr, "✘ Configuration file syntax error.\n");
406 return EXIT_FAILURE;
408 else
410 fprintf(stderr, "✔ Configuration file syntax OK.\n");
411 return EXIT_SUCCESS;
413 case 'c':
414 if(a_strlen(optarg))
415 confpath = a_strdup(optarg);
416 else
417 fatal("-c option requires a file name");
418 break;
421 globalconf.loop = ev_default_loop(0);
422 ev_timer_init(&globalconf.timer, &luaA_on_timer, 0., 0.);
424 /* register function for signals */
425 ev_signal_init(&sigint, exit_on_signal, SIGINT);
426 ev_signal_init(&sigterm, exit_on_signal, SIGTERM);
427 ev_signal_init(&sighup, restart_on_signal, SIGHUP);
428 ev_signal_start(globalconf.loop, &sigint);
429 ev_signal_start(globalconf.loop, &sigterm);
430 ev_signal_start(globalconf.loop, &sighup);
431 ev_unref(globalconf.loop);
432 ev_unref(globalconf.loop);
433 ev_unref(globalconf.loop);
435 struct sigaction sa = { .sa_handler = signal_fatal, .sa_flags = 0 };
436 sigemptyset(&sa.sa_mask);
437 sigaction(SIGSEGV, &sa, 0);
439 /* X stuff */
440 globalconf.connection = xcb_connect(NULL, &globalconf.default_screen);
441 if(xcb_connection_has_error(globalconf.connection))
442 fatal("cannot open display");
444 /* check for xtest extension */
445 const xcb_query_extension_reply_t *xtest_query;
446 xtest_query = xcb_get_extension_data(globalconf.connection, &xcb_test_id);
447 globalconf.have_xtest = xtest_query->present;
449 /* initialize dbus */
450 a_dbus_init();
452 /* Grab server */
453 xcb_grab_server(globalconf.connection);
454 xcb_flush(globalconf.connection);
456 /* Get the file descriptor corresponding to the X connection */
457 xfd = xcb_get_file_descriptor(globalconf.connection);
458 ev_io_init(&xio, &a_xcb_io_cb, xfd, EV_READ);
459 ev_io_start(globalconf.loop, &xio);
460 ev_check_init(&xcheck, &a_xcb_check_cb);
461 ev_check_start(globalconf.loop, &xcheck);
462 ev_unref(globalconf.loop);
463 ev_prepare_init(&a_refresh, &a_refresh_cb);
464 ev_prepare_start(globalconf.loop, &a_refresh);
465 ev_unref(globalconf.loop);
467 /* Allocate a handler which will holds all errors and events */
468 xcb_event_handlers_init(globalconf.connection, &globalconf.evenths);
469 xutil_error_handler_catch_all_set(&globalconf.evenths, xerrorstart, NULL);
471 for(screen_nbr = 0;
472 screen_nbr < xcb_setup_roots_length(xcb_get_setup(globalconf.connection));
473 screen_nbr++)
475 const uint32_t select_input_val = XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT;
477 /* This causes an error if some other window manager is running */
478 xcb_change_window_attributes(globalconf.connection,
479 xutil_screen_get(globalconf.connection, screen_nbr)->root,
480 XCB_CW_EVENT_MASK, &select_input_val);
483 /* Need to xcb_flush to validate error handler */
484 xcb_aux_sync(globalconf.connection);
486 /* Process all errors in the queue if any */
487 xcb_event_poll_for_event_loop(&globalconf.evenths);
489 /* Set the default xerror handler */
490 xutil_error_handler_catch_all_set(&globalconf.evenths, xerror, NULL);
492 /* Allocate the key symbols */
493 globalconf.keysyms = xcb_key_symbols_alloc(globalconf.connection);
494 xcb_get_modifier_mapping_cookie_t xmapping_cookie =
495 xcb_get_modifier_mapping_unchecked(globalconf.connection);
497 /* init atom cache */
498 atoms_init(globalconf.connection);
500 /* init screens information */
501 screen_scan();
503 /* init default font and colors */
504 colors_reqs[0] = xcolor_init_unchecked(&globalconf.colors.fg,
505 "black", sizeof("black") - 1);
507 colors_reqs[1] = xcolor_init_unchecked(&globalconf.colors.bg,
508 "white", sizeof("white") - 1);
510 globalconf.font = draw_font_new("sans 8");
512 for(colors_nbr = 0; colors_nbr < 2; colors_nbr++)
513 xcolor_init_reply(colors_reqs[colors_nbr]);
515 xutil_lock_mask_get(globalconf.connection, xmapping_cookie,
516 globalconf.keysyms, &globalconf.numlockmask,
517 &globalconf.shiftlockmask, &globalconf.capslockmask,
518 &globalconf.modeswitchmask);
520 /* do this only for real screen */
521 for(screen_nbr = 0;
522 screen_nbr < xcb_setup_roots_length(xcb_get_setup(globalconf.connection));
523 screen_nbr++)
525 /* select for events */
526 const uint32_t change_win_vals[] =
528 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT | XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY
529 | XCB_EVENT_MASK_ENTER_WINDOW | XCB_EVENT_MASK_LEAVE_WINDOW
530 | XCB_EVENT_MASK_STRUCTURE_NOTIFY
531 | XCB_EVENT_MASK_PROPERTY_CHANGE
532 | XCB_EVENT_MASK_BUTTON_PRESS
533 | XCB_EVENT_MASK_BUTTON_RELEASE
534 | XCB_EVENT_MASK_FOCUS_CHANGE
537 xcb_change_window_attributes(globalconf.connection,
538 xutil_screen_get(globalconf.connection, screen_nbr)->root,
539 XCB_CW_EVENT_MASK,
540 change_win_vals);
541 ewmh_init(screen_nbr);
542 systray_init(screen_nbr);
545 /* init spawn (sn) */
546 spawn_init();
548 /* Parse and run configuration file */
549 if (!luaA_parserc(&xdg, confpath, true))
550 fatal("couldn't find any rc file");
552 xdgWipeHandle(&xdg);
554 /* scan existing windows */
555 scan();
557 /* process all errors in the queue if any */
558 xcb_event_poll_for_event_loop(&globalconf.evenths);
559 a_xcb_set_event_handlers();
560 a_xcb_set_property_handlers();
562 /* we will receive events, stop grabbing server */
563 xcb_ungrab_server(globalconf.connection);
564 xcb_flush(globalconf.connection);
566 /* main event loop */
567 ev_loop(globalconf.loop, 0);
569 /* cleanup event loop */
570 ev_ref(globalconf.loop);
571 ev_check_stop(globalconf.loop, &xcheck);
572 ev_ref(globalconf.loop);
573 ev_prepare_stop(globalconf.loop, &a_refresh);
574 ev_ref(globalconf.loop);
575 ev_io_stop(globalconf.loop, &xio);
577 awesome_atexit();
579 return EXIT_SUCCESS;
582 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80