change codename
[awesome.git] / awesome.c
bloba25c901eccd693e34192543a24c4b62b4321521a
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/bigreq.h>
30 #include <xcb/randr.h>
31 #include <xcb/shape.h>
32 #include <xcb/xcb_event.h>
33 #include <xcb/xinerama.h>
34 #include <xcb/xtest.h>
36 #include "awesome.h"
37 #include "spawn.h"
38 #include "client.h"
39 #include "window.h"
40 #include "ewmh.h"
41 #include "dbus.h"
42 #include "systray.h"
43 #include "event.h"
44 #include "property.h"
45 #include "screen.h"
46 #include "titlebar.h"
47 #include "luaa.h"
48 #include "common/version.h"
49 #include "common/atoms.h"
50 #include "common/xcursor.h"
51 #include "common/xutil.h"
52 #include "common/backtrace.h"
54 awesome_t globalconf;
56 /** Call before exiting.
58 void
59 awesome_atexit(bool restart)
61 int screen_nbr, nscreens;
63 if(globalconf.hooks.exit != LUA_REFNIL)
64 luaA_dofunction_from_registry(globalconf.L, globalconf.hooks.exit, 0, 0);
66 lua_pushboolean(globalconf.L, restart);
67 signal_object_emit(globalconf.L, &global_signals, "exit", 1);
69 a_dbus_cleanup();
71 /* do this only for real screen */
72 const xcb_setup_t *setup = xcb_get_setup(globalconf.connection);
73 nscreens = setup ? xcb_setup_roots_length(setup) : -1;
74 for(screen_nbr = 0;
75 screen_nbr < nscreens;
76 screen_nbr++)
77 systray_cleanup(screen_nbr);
79 /* Close Lua */
80 lua_close(globalconf.L);
82 xcb_flush(globalconf.connection);
84 /* Disconnect *after* closing lua */
85 xcb_disconnect(globalconf.connection);
87 ev_default_destroy();
90 /** Scan X to find windows to manage.
92 static void
93 scan(xcb_query_tree_cookie_t tree_c[])
95 int i, phys_screen, tree_c_len;
96 const int screen_max = xcb_setup_roots_length(xcb_get_setup(globalconf.connection));
97 xcb_query_tree_reply_t *tree_r;
98 xcb_window_t *wins = NULL;
99 xcb_get_window_attributes_reply_t *attr_r;
100 xcb_get_geometry_reply_t *geom_r;
101 long state;
103 for(phys_screen = 0; phys_screen < screen_max; phys_screen++)
105 tree_r = xcb_query_tree_reply(globalconf.connection,
106 tree_c[phys_screen],
107 NULL);
109 if(!tree_r)
110 continue;
112 /* Get the tree of the children windows of the current root window */
113 if(!(wins = xcb_query_tree_children(tree_r)))
114 fatal("cannot get tree children");
116 tree_c_len = xcb_query_tree_children_length(tree_r);
117 xcb_get_window_attributes_cookie_t attr_wins[tree_c_len];
118 xcb_get_property_cookie_t state_wins[tree_c_len];
120 for(i = 0; i < tree_c_len; i++)
122 attr_wins[i] = xcb_get_window_attributes_unchecked(globalconf.connection,
123 wins[i]);
125 state_wins[i] = window_state_get_unchecked(wins[i]);
128 xcb_get_geometry_cookie_t *geom_wins[tree_c_len];
130 for(i = 0; i < tree_c_len; i++)
132 attr_r = xcb_get_window_attributes_reply(globalconf.connection,
133 attr_wins[i],
134 NULL);
136 state = window_state_get_reply(state_wins[i]);
138 if(!attr_r || attr_r->override_redirect
139 || attr_r->map_state == XCB_MAP_STATE_UNMAPPED
140 || state == XCB_ICCCM_WM_STATE_WITHDRAWN)
142 geom_wins[i] = NULL;
143 p_delete(&attr_r);
144 continue;
147 p_delete(&attr_r);
149 /* Get the geometry of the current window */
150 geom_wins[i] = p_alloca(xcb_get_geometry_cookie_t, 1);
151 *(geom_wins[i]) = xcb_get_geometry_unchecked(globalconf.connection, wins[i]);
154 for(i = 0; i < tree_c_len; i++)
156 if(!geom_wins[i] || !(geom_r = xcb_get_geometry_reply(globalconf.connection,
157 *(geom_wins[i]), NULL)))
158 continue;
160 /* The window can be mapped, so force it to be undrawn for startup */
161 xcb_unmap_window(globalconf.connection, wins[i]);
163 client_manage(wins[i], geom_r, phys_screen, true);
165 p_delete(&geom_r);
168 p_delete(&tree_r);
172 static void
173 a_refresh_cb(EV_P_ ev_prepare *w, int revents)
175 awesome_refresh();
178 static void
179 a_xcb_check_cb(EV_P_ ev_check *w, int revents)
181 xcb_generic_event_t *mouse = NULL, *event;
183 while((event = xcb_poll_for_event(globalconf.connection)))
185 /* We will treat mouse events later.
186 * We cannot afford to treat all mouse motion events,
187 * because that would be too much CPU intensive, so we just
188 * take the last we get after a bunch of events. */
189 if(XCB_EVENT_RESPONSE_TYPE(event) == XCB_MOTION_NOTIFY)
191 p_delete(&mouse);
192 mouse = event;
194 else
196 event_handle(event);
197 p_delete(&event);
201 if(mouse)
203 event_handle(mouse);
204 p_delete(&mouse);
208 static void
209 a_xcb_io_cb(EV_P_ ev_io *w, int revents)
211 /* a_xcb_check_cb() already handled all events */
213 if(xcb_connection_has_error(globalconf.connection))
214 fatal("X server connection broke");
217 static void
218 signal_fatal(int signum)
220 buffer_t buf;
221 backtrace_get(&buf);
222 fatal("dumping backtrace\n%s", buf.s);
225 /** Function to exit on some signals.
226 * \param w the signal received, unused
227 * \param revents currently unused
229 static void
230 exit_on_signal(EV_P_ ev_signal *w, int revents)
232 ev_unloop(EV_A_ 1);
235 void
236 awesome_restart(void)
238 awesome_atexit(true);
239 a_exec(globalconf.argv);
242 /** Function to restart awesome on some signals.
243 * \param w the signal received, unused
244 * \param revents Currently unused
246 static void
247 restart_on_signal(EV_P_ ev_signal *w, int revents)
249 awesome_restart();
252 /** Print help and exit(2) with given exit_code.
253 * \param exit_code The exit code.
255 static void __attribute__ ((noreturn))
256 exit_help(int exit_code)
258 FILE *outfile = (exit_code == EXIT_SUCCESS) ? stdout : stderr;
259 fprintf(outfile,
260 "Usage: awesome [OPTION]\n\
261 -h, --help show help\n\
262 -v, --version show version\n\
263 -c, --config FILE configuration file to use\n\
264 -k, --check check configuration file syntax\n");
265 exit(exit_code);
268 /** Hello, this is main.
269 * \param argc Who knows.
270 * \param argv Who knows.
271 * \return EXIT_SUCCESS I hope.
274 main(int argc, char **argv)
276 char *confpath = NULL;
277 int xfd, i, screen_nbr, opt, colors_nbr;
278 xcolor_init_request_t colors_reqs[2];
279 ssize_t cmdlen = 1;
280 xdgHandle xdg;
281 xcb_generic_event_t *event;
282 static struct option long_options[] =
284 { "help", 0, NULL, 'h' },
285 { "version", 0, NULL, 'v' },
286 { "config", 1, NULL, 'c' },
287 { "check", 0, NULL, 'k' },
288 { NULL, 0, NULL, 0 }
291 /* event loop watchers */
292 ev_io xio = { .fd = -1 };
293 ev_check xcheck;
294 ev_prepare a_refresh;
295 ev_signal sigint;
296 ev_signal sigterm;
297 ev_signal sighup;
299 /* clear the globalconf structure */
300 p_clear(&globalconf, 1);
301 globalconf.keygrabber = LUA_REFNIL;
302 globalconf.mousegrabber = LUA_REFNIL;
303 buffer_init(&globalconf.startup_errors);
305 /* save argv */
306 for(i = 0; i < argc; i++)
307 cmdlen += a_strlen(argv[i]) + 1;
309 globalconf.argv = p_new(char, cmdlen);
310 a_strcpy(globalconf.argv, cmdlen, argv[0]);
312 for(i = 1; i < argc; i++)
314 a_strcat(globalconf.argv, cmdlen, " ");
315 a_strcat(globalconf.argv, cmdlen, argv[i]);
318 /* Text won't be printed correctly otherwise */
319 setlocale(LC_CTYPE, "");
321 /* Get XDG basedir data */
322 xdgInitHandle(&xdg);
324 /* init lua */
325 luaA_init(&xdg);
327 /* check args */
328 while((opt = getopt_long(argc, argv, "vhkc:",
329 long_options, NULL)) != -1)
330 switch(opt)
332 case 'v':
333 eprint_version();
334 break;
335 case 'h':
336 exit_help(EXIT_SUCCESS);
337 break;
338 case 'k':
339 if(!luaA_parserc(&xdg, confpath, false))
341 fprintf(stderr, "✘ Configuration file syntax error.\n");
342 return EXIT_FAILURE;
344 else
346 fprintf(stderr, "✔ Configuration file syntax OK.\n");
347 return EXIT_SUCCESS;
349 case 'c':
350 if(a_strlen(optarg))
351 confpath = a_strdup(optarg);
352 else
353 fatal("-c option requires a file name");
354 break;
357 globalconf.loop = ev_default_loop(0);
358 ev_timer_init(&globalconf.timer, &luaA_on_timer, 0., 0.);
360 /* register function for signals */
361 ev_signal_init(&sigint, exit_on_signal, SIGINT);
362 ev_signal_init(&sigterm, exit_on_signal, SIGTERM);
363 ev_signal_init(&sighup, restart_on_signal, SIGHUP);
364 ev_signal_start(globalconf.loop, &sigint);
365 ev_signal_start(globalconf.loop, &sigterm);
366 ev_signal_start(globalconf.loop, &sighup);
367 ev_unref(globalconf.loop);
368 ev_unref(globalconf.loop);
369 ev_unref(globalconf.loop);
371 struct sigaction sa = { .sa_handler = signal_fatal, .sa_flags = 0 };
372 sigemptyset(&sa.sa_mask);
373 sigaction(SIGSEGV, &sa, 0);
375 /* X stuff */
376 globalconf.connection = xcb_connect(NULL, &globalconf.default_screen);
377 if(xcb_connection_has_error(globalconf.connection))
378 fatal("cannot open display");
380 /* Prefetch all the extensions we might need */
381 xcb_prefetch_extension_data(globalconf.connection, &xcb_big_requests_id);
382 xcb_prefetch_extension_data(globalconf.connection, &xcb_test_id);
383 xcb_prefetch_extension_data(globalconf.connection, &xcb_randr_id);
384 xcb_prefetch_extension_data(globalconf.connection, &xcb_xinerama_id);
385 xcb_prefetch_extension_data(globalconf.connection, &xcb_shape_id);
387 /* initialize dbus */
388 a_dbus_init();
390 /* Get the file descriptor corresponding to the X connection */
391 xfd = xcb_get_file_descriptor(globalconf.connection);
392 ev_io_init(&xio, &a_xcb_io_cb, xfd, EV_READ);
393 ev_io_start(globalconf.loop, &xio);
394 ev_check_init(&xcheck, &a_xcb_check_cb);
395 ev_check_start(globalconf.loop, &xcheck);
396 ev_unref(globalconf.loop);
397 ev_prepare_init(&a_refresh, &a_refresh_cb);
398 ev_prepare_start(globalconf.loop, &a_refresh);
399 ev_unref(globalconf.loop);
401 /* Grab server */
402 xcb_grab_server(globalconf.connection);
404 /* Make sure there are no pending events. Since we didn't really do anything
405 * at all yet, we will just discard all events which we received so far.
406 * The above GrabServer should make sure no new events are generated. */
407 xcb_aux_sync(globalconf.connection);
408 while ((event = xcb_poll_for_event(globalconf.connection)) != NULL)
410 /* Make sure errors are printed */
411 uint8_t response_type = XCB_EVENT_RESPONSE_TYPE(event);
412 if(response_type == 0)
413 event_handle(event);
414 p_delete(&event);
417 for(screen_nbr = 0;
418 screen_nbr < xcb_setup_roots_length(xcb_get_setup(globalconf.connection));
419 screen_nbr++)
421 const uint32_t select_input_val = XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT;
423 /* This causes an error if some other window manager is running */
424 xcb_change_window_attributes(globalconf.connection,
425 xutil_screen_get(globalconf.connection, screen_nbr)->root,
426 XCB_CW_EVENT_MASK, &select_input_val);
429 /* Need to xcb_flush to validate error handler */
430 xcb_aux_sync(globalconf.connection);
432 /* Process all errors in the queue if any. There can be no events yet, so if
433 * this function returns something, it must be an error. */
434 if (xcb_poll_for_event(globalconf.connection) != NULL)
435 fatal("another window manager is already running");
437 /* Prefetch the maximum request length */
438 xcb_prefetch_maximum_request_length(globalconf.connection);
440 /* check for xtest extension */
441 const xcb_query_extension_reply_t *xtest_query;
442 xtest_query = xcb_get_extension_data(globalconf.connection, &xcb_test_id);
443 globalconf.have_xtest = xtest_query->present;
445 /* Allocate the key symbols */
446 globalconf.keysyms = xcb_key_symbols_alloc(globalconf.connection);
447 xcb_get_modifier_mapping_cookie_t xmapping_cookie =
448 xcb_get_modifier_mapping_unchecked(globalconf.connection);
450 /* init atom cache */
451 atoms_init(globalconf.connection);
453 /* init screens information */
454 screen_scan();
456 /* init default font and colors */
457 colors_reqs[0] = xcolor_init_unchecked(&globalconf.colors.fg,
458 "black", sizeof("black") - 1);
460 colors_reqs[1] = xcolor_init_unchecked(&globalconf.colors.bg,
461 "white", sizeof("white") - 1);
463 globalconf.font = draw_font_new("sans 8");
465 for(colors_nbr = 0; colors_nbr < 2; colors_nbr++)
466 xcolor_init_reply(colors_reqs[colors_nbr]);
468 xutil_lock_mask_get(globalconf.connection, xmapping_cookie,
469 globalconf.keysyms, &globalconf.numlockmask,
470 &globalconf.shiftlockmask, &globalconf.capslockmask,
471 &globalconf.modeswitchmask);
473 /* Get the window tree associated to this screen */
474 const int screen_max = xcb_setup_roots_length(xcb_get_setup(globalconf.connection));
475 xcb_query_tree_cookie_t tree_c[screen_max];
477 /* do this only for real screen */
478 for(screen_nbr = 0; screen_nbr < screen_max; screen_nbr++)
480 /* select for events */
481 const uint32_t change_win_vals[] =
483 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT | XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY
484 | XCB_EVENT_MASK_ENTER_WINDOW | XCB_EVENT_MASK_LEAVE_WINDOW
485 | XCB_EVENT_MASK_STRUCTURE_NOTIFY
486 | XCB_EVENT_MASK_PROPERTY_CHANGE
487 | XCB_EVENT_MASK_BUTTON_PRESS
488 | XCB_EVENT_MASK_BUTTON_RELEASE
489 | XCB_EVENT_MASK_FOCUS_CHANGE
492 tree_c[screen_nbr] = xcb_query_tree_unchecked(globalconf.connection,
493 xutil_screen_get(globalconf.connection, screen_nbr)->root);
495 xcb_change_window_attributes(globalconf.connection,
496 xutil_screen_get(globalconf.connection, screen_nbr)->root,
497 XCB_CW_EVENT_MASK,
498 change_win_vals);
499 ewmh_init(screen_nbr);
500 systray_init(screen_nbr);
503 /* init spawn (sn) */
504 spawn_init();
506 /* we will receive events, stop grabbing server */
507 xcb_ungrab_server(globalconf.connection);
509 /* Parse and run configuration file */
510 if (!luaA_parserc(&xdg, confpath, true))
511 fatal("couldn't find any rc file");
513 scan(tree_c);
515 xcb_flush(globalconf.connection);
517 /* main event loop */
518 ev_loop(globalconf.loop, 0);
520 /* cleanup event loop */
521 ev_ref(globalconf.loop);
522 ev_check_stop(globalconf.loop, &xcheck);
523 ev_ref(globalconf.loop);
524 ev_prepare_stop(globalconf.loop, &a_refresh);
525 ev_ref(globalconf.loop);
526 ev_io_stop(globalconf.loop, &xio);
528 awesome_atexit(false);
530 return EXIT_SUCCESS;
533 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80