Use awesome.conffile in menu
[awesome.git] / awesome.c
blob1258b4cc7878826f33fa3a64a0ddb6e12fff764d
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 typedef struct
58 xcb_window_t id;
59 xcb_query_tree_cookie_t tree_cookie;
60 } root_win_t;
62 /** Call before exiting.
64 void
65 awesome_atexit(bool restart)
67 int screen_nbr, nscreens;
69 if(globalconf.hooks.exit != LUA_REFNIL)
70 luaA_dofunction_from_registry(globalconf.L, globalconf.hooks.exit, 0, 0);
72 lua_pushboolean(globalconf.L, restart);
73 signal_object_emit(globalconf.L, &global_signals, "exit", 1);
75 a_dbus_cleanup();
77 /* do this only for real screen */
78 const xcb_setup_t *setup = xcb_get_setup(globalconf.connection);
79 nscreens = setup ? xcb_setup_roots_length(setup) : -1;
80 for(screen_nbr = 0;
81 screen_nbr < nscreens;
82 screen_nbr++)
83 systray_cleanup(screen_nbr);
85 /* Close Lua */
86 lua_close(globalconf.L);
88 xcb_flush(globalconf.connection);
90 /* Disconnect *after* closing lua */
91 xcb_disconnect(globalconf.connection);
93 ev_default_destroy();
96 /** Scan X to find windows to manage.
98 static void
99 scan(void)
101 int i, phys_screen, tree_c_len;
102 const int screen_max = xcb_setup_roots_length(xcb_get_setup(globalconf.connection));
103 root_win_t root_wins[screen_max];
104 xcb_query_tree_reply_t *tree_r;
105 xcb_window_t *wins = NULL;
106 xcb_get_window_attributes_reply_t *attr_r;
107 xcb_get_geometry_reply_t *geom_r;
108 long state;
110 for(phys_screen = 0; phys_screen < screen_max; phys_screen++)
112 /* Get the root window ID associated to this screen */
113 root_wins[phys_screen].id = xutil_screen_get(globalconf.connection, phys_screen)->root;
115 /* Get the window tree associated to this screen */
116 root_wins[phys_screen].tree_cookie = xcb_query_tree_unchecked(globalconf.connection,
117 root_wins[phys_screen].id);
120 for(phys_screen = 0; phys_screen < screen_max; phys_screen++)
122 tree_r = xcb_query_tree_reply(globalconf.connection,
123 root_wins[phys_screen].tree_cookie,
124 NULL);
126 if(!tree_r)
127 continue;
129 /* Get the tree of the children windows of the current root window */
130 if(!(wins = xcb_query_tree_children(tree_r)))
131 fatal("cannot get tree children");
133 tree_c_len = xcb_query_tree_children_length(tree_r);
134 xcb_get_window_attributes_cookie_t attr_wins[tree_c_len];
135 xcb_get_property_cookie_t state_wins[tree_c_len];
137 for(i = 0; i < tree_c_len; i++)
139 attr_wins[i] = xcb_get_window_attributes_unchecked(globalconf.connection,
140 wins[i]);
142 state_wins[i] = window_state_get_unchecked(wins[i]);
145 xcb_get_geometry_cookie_t *geom_wins[tree_c_len];
147 for(i = 0; i < tree_c_len; i++)
149 attr_r = xcb_get_window_attributes_reply(globalconf.connection,
150 attr_wins[i],
151 NULL);
153 state = window_state_get_reply(state_wins[i]);
155 if(!attr_r || attr_r->override_redirect
156 || attr_r->map_state == XCB_MAP_STATE_UNMAPPED
157 || state == XCB_ICCCM_WM_STATE_WITHDRAWN)
159 geom_wins[i] = NULL;
160 p_delete(&attr_r);
161 continue;
164 p_delete(&attr_r);
166 /* Get the geometry of the current window */
167 geom_wins[i] = p_alloca(xcb_get_geometry_cookie_t, 1);
168 *(geom_wins[i]) = xcb_get_geometry_unchecked(globalconf.connection, wins[i]);
171 for(i = 0; i < tree_c_len; i++)
173 if(!geom_wins[i] || !(geom_r = xcb_get_geometry_reply(globalconf.connection,
174 *(geom_wins[i]), NULL)))
175 continue;
177 /* The window can be mapped, so force it to be undrawn for startup */
178 xcb_unmap_window(globalconf.connection, wins[i]);
180 client_manage(wins[i], geom_r, phys_screen, true);
182 p_delete(&geom_r);
185 p_delete(&tree_r);
189 static void
190 a_refresh_cb(EV_P_ ev_prepare *w, int revents)
192 awesome_refresh();
195 static void
196 a_xcb_check_cb(EV_P_ ev_check *w, int revents)
198 xcb_generic_event_t *mouse = NULL, *event;
200 while((event = xcb_poll_for_event(globalconf.connection)))
202 /* We will treat mouse events later.
203 * We cannot afford to treat all mouse motion events,
204 * because that would be too much CPU intensive, so we just
205 * take the last we get after a bunch of events. */
206 if(XCB_EVENT_RESPONSE_TYPE(event) == XCB_MOTION_NOTIFY)
208 p_delete(&mouse);
209 mouse = event;
211 else
213 event_handle(event);
214 p_delete(&event);
218 if(mouse)
220 event_handle(mouse);
221 p_delete(&mouse);
225 static void
226 a_xcb_io_cb(EV_P_ ev_io *w, int revents)
228 /* a_xcb_check_cb() already handled all events */
230 if(xcb_connection_has_error(globalconf.connection))
231 fatal("X server connection broke");
234 static void
235 signal_fatal(int signum)
237 buffer_t buf;
238 backtrace_get(&buf);
239 fatal("dumping backtrace\n%s", buf.s);
242 /** Function to exit on some signals.
243 * \param w the signal received, unused
244 * \param revents currently unused
246 static void
247 exit_on_signal(EV_P_ ev_signal *w, int revents)
249 ev_unloop(EV_A_ 1);
252 void
253 awesome_restart(void)
255 awesome_atexit(true);
256 a_exec(globalconf.argv);
259 /** Function to restart awesome on some signals.
260 * \param w the signal received, unused
261 * \param revents Currently unused
263 static void
264 restart_on_signal(EV_P_ ev_signal *w, int revents)
266 awesome_restart();
269 /** Print help and exit(2) with given exit_code.
270 * \param exit_code The exit code.
272 static void __attribute__ ((noreturn))
273 exit_help(int exit_code)
275 FILE *outfile = (exit_code == EXIT_SUCCESS) ? stdout : stderr;
276 fprintf(outfile,
277 "Usage: awesome [OPTION]\n\
278 -h, --help show help\n\
279 -v, --version show version\n\
280 -c, --config FILE configuration file to use\n\
281 -k, --check check configuration file syntax\n");
282 exit(exit_code);
285 /** Hello, this is main.
286 * \param argc Who knows.
287 * \param argv Who knows.
288 * \return EXIT_SUCCESS I hope.
291 main(int argc, char **argv)
293 char *confpath = NULL;
294 int xfd, i, screen_nbr, opt, colors_nbr;
295 xcolor_init_request_t colors_reqs[2];
296 ssize_t cmdlen = 1;
297 xdgHandle xdg;
298 xcb_generic_event_t *event;
299 static struct option long_options[] =
301 { "help", 0, NULL, 'h' },
302 { "version", 0, NULL, 'v' },
303 { "config", 1, NULL, 'c' },
304 { "check", 0, NULL, 'k' },
305 { NULL, 0, NULL, 0 }
308 /* event loop watchers */
309 ev_io xio = { .fd = -1 };
310 ev_check xcheck;
311 ev_prepare a_refresh;
312 ev_signal sigint;
313 ev_signal sigterm;
314 ev_signal sighup;
316 /* clear the globalconf structure */
317 p_clear(&globalconf, 1);
318 globalconf.keygrabber = LUA_REFNIL;
319 globalconf.mousegrabber = LUA_REFNIL;
321 /* save argv */
322 for(i = 0; i < argc; i++)
323 cmdlen += a_strlen(argv[i]) + 1;
325 globalconf.argv = p_new(char, cmdlen);
326 a_strcpy(globalconf.argv, cmdlen, argv[0]);
328 for(i = 1; i < argc; i++)
330 a_strcat(globalconf.argv, cmdlen, " ");
331 a_strcat(globalconf.argv, cmdlen, argv[i]);
334 /* Text won't be printed correctly otherwise */
335 setlocale(LC_CTYPE, "");
337 /* Get XDG basedir data */
338 xdgInitHandle(&xdg);
340 /* init lua */
341 luaA_init(&xdg);
343 /* check args */
344 while((opt = getopt_long(argc, argv, "vhkc:",
345 long_options, NULL)) != -1)
346 switch(opt)
348 case 'v':
349 eprint_version();
350 break;
351 case 'h':
352 exit_help(EXIT_SUCCESS);
353 break;
354 case 'k':
355 if(!luaA_parserc(&xdg, confpath, false))
357 fprintf(stderr, "✘ Configuration file syntax error.\n");
358 return EXIT_FAILURE;
360 else
362 fprintf(stderr, "✔ Configuration file syntax OK.\n");
363 return EXIT_SUCCESS;
365 case 'c':
366 if(a_strlen(optarg))
367 confpath = a_strdup(optarg);
368 else
369 fatal("-c option requires a file name");
370 break;
373 globalconf.loop = ev_default_loop(0);
374 ev_timer_init(&globalconf.timer, &luaA_on_timer, 0., 0.);
376 /* register function for signals */
377 ev_signal_init(&sigint, exit_on_signal, SIGINT);
378 ev_signal_init(&sigterm, exit_on_signal, SIGTERM);
379 ev_signal_init(&sighup, restart_on_signal, SIGHUP);
380 ev_signal_start(globalconf.loop, &sigint);
381 ev_signal_start(globalconf.loop, &sigterm);
382 ev_signal_start(globalconf.loop, &sighup);
383 ev_unref(globalconf.loop);
384 ev_unref(globalconf.loop);
385 ev_unref(globalconf.loop);
387 struct sigaction sa = { .sa_handler = signal_fatal, .sa_flags = 0 };
388 sigemptyset(&sa.sa_mask);
389 sigaction(SIGSEGV, &sa, 0);
391 /* X stuff */
392 globalconf.connection = xcb_connect(NULL, &globalconf.default_screen);
393 if(xcb_connection_has_error(globalconf.connection))
394 fatal("cannot open display");
396 /* Prefetch all the extensions we might need */
397 xcb_prefetch_extension_data(globalconf.connection, &xcb_big_requests_id);
398 xcb_prefetch_extension_data(globalconf.connection, &xcb_test_id);
399 xcb_prefetch_extension_data(globalconf.connection, &xcb_randr_id);
400 xcb_prefetch_extension_data(globalconf.connection, &xcb_xinerama_id);
401 xcb_prefetch_extension_data(globalconf.connection, &xcb_shape_id);
403 /* initialize dbus */
404 a_dbus_init();
406 /* Get the file descriptor corresponding to the X connection */
407 xfd = xcb_get_file_descriptor(globalconf.connection);
408 ev_io_init(&xio, &a_xcb_io_cb, xfd, EV_READ);
409 ev_io_start(globalconf.loop, &xio);
410 ev_check_init(&xcheck, &a_xcb_check_cb);
411 ev_check_start(globalconf.loop, &xcheck);
412 ev_unref(globalconf.loop);
413 ev_prepare_init(&a_refresh, &a_refresh_cb);
414 ev_prepare_start(globalconf.loop, &a_refresh);
415 ev_unref(globalconf.loop);
417 /* Grab server */
418 xcb_grab_server(globalconf.connection);
420 /* Make sure there are no pending events. Since we didn't really do anything
421 * at all yet, we will just discard all events which we received so far.
422 * The above GrabServer should make sure no new events are generated. */
423 xcb_aux_sync(globalconf.connection);
424 while ((event = xcb_poll_for_event(globalconf.connection)) != NULL)
426 /* Make sure errors are printed */
427 uint8_t response_type = XCB_EVENT_RESPONSE_TYPE(event);
428 if(response_type == 0)
429 event_handle(event);
430 p_delete(&event);
433 for(screen_nbr = 0;
434 screen_nbr < xcb_setup_roots_length(xcb_get_setup(globalconf.connection));
435 screen_nbr++)
437 const uint32_t select_input_val = XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT;
439 /* This causes an error if some other window manager is running */
440 xcb_change_window_attributes(globalconf.connection,
441 xutil_screen_get(globalconf.connection, screen_nbr)->root,
442 XCB_CW_EVENT_MASK, &select_input_val);
445 /* Need to xcb_flush to validate error handler */
446 xcb_aux_sync(globalconf.connection);
448 /* Process all errors in the queue if any. There can be no events yet, so if
449 * this function returns something, it must be an error. */
450 if (xcb_poll_for_event(globalconf.connection) != NULL)
451 fatal("another window manager is already running");
453 /* Prefetch the maximum request length */
454 xcb_prefetch_maximum_request_length(globalconf.connection);
456 /* check for xtest extension */
457 const xcb_query_extension_reply_t *xtest_query;
458 xtest_query = xcb_get_extension_data(globalconf.connection, &xcb_test_id);
459 globalconf.have_xtest = xtest_query->present;
461 /* Allocate the key symbols */
462 globalconf.keysyms = xcb_key_symbols_alloc(globalconf.connection);
463 xcb_get_modifier_mapping_cookie_t xmapping_cookie =
464 xcb_get_modifier_mapping_unchecked(globalconf.connection);
466 /* init atom cache */
467 atoms_init(globalconf.connection);
469 /* init screens information */
470 screen_scan();
472 /* init default font and colors */
473 colors_reqs[0] = xcolor_init_unchecked(&globalconf.colors.fg,
474 "black", sizeof("black") - 1);
476 colors_reqs[1] = xcolor_init_unchecked(&globalconf.colors.bg,
477 "white", sizeof("white") - 1);
479 globalconf.font = draw_font_new("sans 8");
481 for(colors_nbr = 0; colors_nbr < 2; colors_nbr++)
482 xcolor_init_reply(colors_reqs[colors_nbr]);
484 xutil_lock_mask_get(globalconf.connection, xmapping_cookie,
485 globalconf.keysyms, &globalconf.numlockmask,
486 &globalconf.shiftlockmask, &globalconf.capslockmask,
487 &globalconf.modeswitchmask);
489 /* do this only for real screen */
490 for(screen_nbr = 0;
491 screen_nbr < xcb_setup_roots_length(xcb_get_setup(globalconf.connection));
492 screen_nbr++)
494 /* select for events */
495 const uint32_t change_win_vals[] =
497 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT | XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY
498 | XCB_EVENT_MASK_ENTER_WINDOW | XCB_EVENT_MASK_LEAVE_WINDOW
499 | XCB_EVENT_MASK_STRUCTURE_NOTIFY
500 | XCB_EVENT_MASK_PROPERTY_CHANGE
501 | XCB_EVENT_MASK_BUTTON_PRESS
502 | XCB_EVENT_MASK_BUTTON_RELEASE
503 | XCB_EVENT_MASK_FOCUS_CHANGE
506 xcb_change_window_attributes(globalconf.connection,
507 xutil_screen_get(globalconf.connection, screen_nbr)->root,
508 XCB_CW_EVENT_MASK,
509 change_win_vals);
510 ewmh_init(screen_nbr);
511 systray_init(screen_nbr);
514 /* init spawn (sn) */
515 spawn_init();
517 /* Parse and run configuration file */
518 if (!luaA_parserc(&xdg, confpath, true))
519 fatal("couldn't find any rc file");
521 p_delete(&confpath);
523 xdgWipeHandle(&xdg);
525 /* scan existing windows */
526 scan();
528 /* we will receive events, stop grabbing server */
529 xcb_ungrab_server(globalconf.connection);
530 xcb_flush(globalconf.connection);
532 /* main event loop */
533 ev_loop(globalconf.loop, 0);
535 /* cleanup event loop */
536 ev_ref(globalconf.loop);
537 ev_check_stop(globalconf.loop, &xcheck);
538 ev_ref(globalconf.loop);
539 ev_prepare_stop(globalconf.loop, &a_refresh);
540 ev_ref(globalconf.loop);
541 ev_io_stop(globalconf.loop, &xio);
543 awesome_atexit(false);
545 return EXIT_SUCCESS;
548 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80