stack graph mode works with max_value
[awesome.git] / awesome.c
blob56c614133c3ea1a61986b1b899ab5c88182bfb31
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 /* do this only for real screen */
73 const xcb_setup_t *setup = xcb_get_setup(globalconf.connection);
74 nscreens = setup ? xcb_setup_roots_length(setup) : -1;
75 for(screen_nbr = 0;
76 screen_nbr < nscreens;
77 screen_nbr++)
78 systray_cleanup(screen_nbr);
80 /* Close Lua */
81 lua_close(globalconf.L);
83 xcb_flush(globalconf.connection);
85 /* Disconnect *after* closing lua */
86 xcb_disconnect(globalconf.connection);
88 ev_default_destroy();
91 /** Scan X to find windows to manage.
93 static void
94 scan(void)
96 int i, phys_screen, tree_c_len;
97 const int screen_max = xcb_setup_roots_length(xcb_get_setup(globalconf.connection));
98 root_win_t root_wins[screen_max];
99 xcb_query_tree_reply_t *tree_r;
100 xcb_window_t *wins = NULL;
101 xcb_get_window_attributes_reply_t *attr_r;
102 xcb_get_geometry_reply_t *geom_r;
103 long state;
105 for(phys_screen = 0; phys_screen < screen_max; phys_screen++)
107 /* Get the root window ID associated to this screen */
108 root_wins[phys_screen].id = xutil_screen_get(globalconf.connection, phys_screen)->root;
110 /* Get the window tree associated to this screen */
111 root_wins[phys_screen].tree_cookie = xcb_query_tree_unchecked(globalconf.connection,
112 root_wins[phys_screen].id);
115 for(phys_screen = 0; phys_screen < screen_max; phys_screen++)
117 tree_r = xcb_query_tree_reply(globalconf.connection,
118 root_wins[phys_screen].tree_cookie,
119 NULL);
121 if(!tree_r)
122 continue;
124 /* Get the tree of the children windows of the current root window */
125 if(!(wins = xcb_query_tree_children(tree_r)))
126 fatal("cannot get tree children");
128 tree_c_len = xcb_query_tree_children_length(tree_r);
129 xcb_get_window_attributes_cookie_t attr_wins[tree_c_len];
130 xcb_get_property_cookie_t state_wins[tree_c_len];
132 for(i = 0; i < tree_c_len; i++)
134 attr_wins[i] = xcb_get_window_attributes_unchecked(globalconf.connection,
135 wins[i]);
137 state_wins[i] = window_state_get_unchecked(wins[i]);
140 xcb_get_geometry_cookie_t *geom_wins[tree_c_len];
142 for(i = 0; i < tree_c_len; i++)
144 attr_r = xcb_get_window_attributes_reply(globalconf.connection,
145 attr_wins[i],
146 NULL);
148 state = window_state_get_reply(state_wins[i]);
150 if(!attr_r || attr_r->override_redirect
151 || attr_r->map_state == XCB_MAP_STATE_UNMAPPED
152 || state == XCB_WM_STATE_WITHDRAWN)
154 geom_wins[i] = NULL;
155 p_delete(&attr_r);
156 continue;
159 p_delete(&attr_r);
161 /* Get the geometry of the current window */
162 geom_wins[i] = p_alloca(xcb_get_geometry_cookie_t, 1);
163 *(geom_wins[i]) = xcb_get_geometry_unchecked(globalconf.connection, wins[i]);
166 for(i = 0; i < tree_c_len; i++)
168 if(!geom_wins[i] || !(geom_r = xcb_get_geometry_reply(globalconf.connection,
169 *(geom_wins[i]), NULL)))
170 continue;
172 /* The window can be mapped, so force it to be undrawn for startup */
173 xcb_unmap_window(globalconf.connection, wins[i]);
175 client_manage(wins[i], geom_r, phys_screen, true);
177 p_delete(&geom_r);
180 p_delete(&tree_r);
184 static void
185 a_refresh_cb(EV_P_ ev_prepare *w, int revents)
187 awesome_refresh();
190 static void
191 a_xcb_check_cb(EV_P_ ev_check *w, int revents)
193 xcb_generic_event_t *mouse = NULL, *event;
195 while((event = xcb_poll_for_event(globalconf.connection)))
197 /* We will treat mouse events later.
198 * We cannot afford to treat all mouse motion events,
199 * because that would be too much CPU intensive, so we just
200 * take the last we get after a bunch of events. */
201 if(XCB_EVENT_RESPONSE_TYPE(event) == XCB_MOTION_NOTIFY)
203 p_delete(&mouse);
204 mouse = event;
206 else
208 xcb_event_handle(&globalconf.evenths, event);
209 p_delete(&event);
213 if(mouse)
215 xcb_event_handle(&globalconf.evenths, mouse);
216 p_delete(&mouse);
220 static void
221 a_xcb_io_cb(EV_P_ ev_io *w, int revents)
223 /* empty */
226 /** Startup Error handler to check if another window manager
227 * is already running.
228 * \param data Additional optional parameters data.
229 * \param c X connection.
230 * \param error Error event.
232 static int __attribute__ ((noreturn))
233 xerrorstart(void * data __attribute__ ((unused)),
234 xcb_connection_t * c __attribute__ ((unused)),
235 xcb_generic_error_t * error __attribute__ ((unused)))
237 fatal("another window manager is already running");
240 static void
241 signal_fatal(int signum)
243 buffer_t buf;
244 backtrace_get(&buf);
245 fatal("dumping backtrace\n%s", buf.s);
248 /** Function to exit on some signals.
249 * \param w the signal received, unused
250 * \param revents currently unused
252 static void
253 exit_on_signal(EV_P_ ev_signal *w, int revents)
255 ev_unloop(EV_A_ 1);
258 void
259 awesome_restart(void)
261 awesome_atexit();
262 a_exec(globalconf.argv);
265 /** Function to restart awesome on some signals.
266 * \param w the signal received, unused
267 * \param revents Currently unused
269 static void
270 restart_on_signal(EV_P_ ev_signal *w, int revents)
272 awesome_restart();
275 /** \brief awesome xerror function.
276 * There's no way to check accesses to destroyed windows, thus those cases are
277 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
278 * default error handler, which may call exit.
279 * \param data Currently unused.
280 * \param c The connection to the X server.
281 * \param e The error event.
282 * \return 0 if no error, or xerror's xlib return status.
284 static int
285 xerror(void *data __attribute__ ((unused)),
286 xcb_connection_t *c __attribute__ ((unused)),
287 xcb_generic_error_t *e)
289 /* ignore this */
290 if(e->error_code == XCB_EVENT_ERROR_BAD_WINDOW
291 || (e->error_code == XCB_EVENT_ERROR_BAD_MATCH
292 && e->major_code == XCB_SET_INPUT_FOCUS)
293 || (e->error_code == XCB_EVENT_ERROR_BAD_VALUE
294 && e->major_code == XCB_KILL_CLIENT)
295 || (e->major_code == XCB_CONFIGURE_WINDOW
296 && e->error_code == XCB_EVENT_ERROR_BAD_MATCH))
297 return 0;
299 warn("X error: request=%s, error=%s",
300 xcb_event_get_request_label(e->major_code),
301 xcb_event_get_error_label(e->error_code));
303 return 0;
306 /** Print help and exit(2) with given exit_code.
307 * \param exit_code The exit code.
309 static void __attribute__ ((noreturn))
310 exit_help(int exit_code)
312 FILE *outfile = (exit_code == EXIT_SUCCESS) ? stdout : stderr;
313 fprintf(outfile,
314 "Usage: awesome [OPTION]\n\
315 -h, --help show help\n\
316 -v, --version show version\n\
317 -c, --config FILE configuration file to use\n\
318 -k, --check check configuration file syntax\n");
319 exit(exit_code);
322 /** Hello, this is main.
323 * \param argc Who knows.
324 * \param argv Who knows.
325 * \return EXIT_SUCCESS I hope.
328 main(int argc, char **argv)
330 char *confpath = NULL;
331 int xfd, i, screen_nbr, opt, colors_nbr;
332 xcolor_init_request_t colors_reqs[2];
333 ssize_t cmdlen = 1;
334 xdgHandle xdg;
335 static struct option long_options[] =
337 { "help", 0, NULL, 'h' },
338 { "version", 0, NULL, 'v' },
339 { "config", 1, NULL, 'c' },
340 { "check", 0, NULL, 'k' },
341 { NULL, 0, NULL, 0 }
344 /* event loop watchers */
345 ev_io xio = { .fd = -1 };
346 ev_check xcheck;
347 ev_prepare a_refresh;
348 ev_signal sigint;
349 ev_signal sigterm;
350 ev_signal sighup;
352 /* clear the globalconf structure */
353 p_clear(&globalconf, 1);
354 globalconf.keygrabber = LUA_REFNIL;
355 globalconf.mousegrabber = LUA_REFNIL;
357 /* save argv */
358 for(i = 0; i < argc; i++)
359 cmdlen += a_strlen(argv[i]) + 1;
361 globalconf.argv = p_new(char, cmdlen);
362 a_strcpy(globalconf.argv, cmdlen, argv[0]);
364 for(i = 1; i < argc; i++)
366 a_strcat(globalconf.argv, cmdlen, " ");
367 a_strcat(globalconf.argv, cmdlen, argv[i]);
370 /* Text won't be printed correctly otherwise */
371 setlocale(LC_CTYPE, "");
373 /* Get XDG basedir data */
374 xdgInitHandle(&xdg);
376 /* init lua */
377 luaA_init(&xdg);
379 /* check args */
380 while((opt = getopt_long(argc, argv, "vhkc:",
381 long_options, NULL)) != -1)
382 switch(opt)
384 case 'v':
385 eprint_version();
386 break;
387 case 'h':
388 exit_help(EXIT_SUCCESS);
389 break;
390 case 'k':
391 if(!luaA_parserc(&xdg, confpath, false))
393 fprintf(stderr, "✘ Configuration file syntax error.\n");
394 return EXIT_FAILURE;
396 else
398 fprintf(stderr, "✔ Configuration file syntax OK.\n");
399 return EXIT_SUCCESS;
401 case 'c':
402 if(a_strlen(optarg))
403 confpath = a_strdup(optarg);
404 else
405 fatal("-c option requires a file name");
406 break;
409 globalconf.loop = ev_default_loop(0);
410 ev_timer_init(&globalconf.timer, &luaA_on_timer, 0., 0.);
412 /* register function for signals */
413 ev_signal_init(&sigint, exit_on_signal, SIGINT);
414 ev_signal_init(&sigterm, exit_on_signal, SIGTERM);
415 ev_signal_init(&sighup, restart_on_signal, SIGHUP);
416 ev_signal_start(globalconf.loop, &sigint);
417 ev_signal_start(globalconf.loop, &sigterm);
418 ev_signal_start(globalconf.loop, &sighup);
419 ev_unref(globalconf.loop);
420 ev_unref(globalconf.loop);
421 ev_unref(globalconf.loop);
423 struct sigaction sa = { .sa_handler = signal_fatal, .sa_flags = 0 };
424 sigemptyset(&sa.sa_mask);
425 sigaction(SIGSEGV, &sa, 0);
427 /* X stuff */
428 globalconf.connection = xcb_connect(NULL, &globalconf.default_screen);
429 if(xcb_connection_has_error(globalconf.connection))
430 fatal("cannot open display");
432 /* check for xtest extension */
433 const xcb_query_extension_reply_t *xtest_query;
434 xtest_query = xcb_get_extension_data(globalconf.connection, &xcb_test_id);
435 globalconf.have_xtest = xtest_query->present;
437 /* initialize dbus */
438 a_dbus_init();
440 /* Grab server */
441 xcb_grab_server(globalconf.connection);
442 xcb_flush(globalconf.connection);
444 /* Get the file descriptor corresponding to the X connection */
445 xfd = xcb_get_file_descriptor(globalconf.connection);
446 ev_io_init(&xio, &a_xcb_io_cb, xfd, EV_READ);
447 ev_io_start(globalconf.loop, &xio);
448 ev_check_init(&xcheck, &a_xcb_check_cb);
449 ev_check_start(globalconf.loop, &xcheck);
450 ev_unref(globalconf.loop);
451 ev_prepare_init(&a_refresh, &a_refresh_cb);
452 ev_prepare_start(globalconf.loop, &a_refresh);
453 ev_unref(globalconf.loop);
455 /* Allocate a handler which will holds all errors and events */
456 xcb_event_handlers_init(globalconf.connection, &globalconf.evenths);
457 xutil_error_handler_catch_all_set(&globalconf.evenths, xerrorstart, NULL);
459 for(screen_nbr = 0;
460 screen_nbr < xcb_setup_roots_length(xcb_get_setup(globalconf.connection));
461 screen_nbr++)
463 const uint32_t select_input_val = XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT;
465 /* This causes an error if some other window manager is running */
466 xcb_change_window_attributes(globalconf.connection,
467 xutil_screen_get(globalconf.connection, screen_nbr)->root,
468 XCB_CW_EVENT_MASK, &select_input_val);
471 /* Need to xcb_flush to validate error handler */
472 xcb_aux_sync(globalconf.connection);
474 /* Process all errors in the queue if any */
475 xcb_event_poll_for_event_loop(&globalconf.evenths);
477 /* Set the default xerror handler */
478 xutil_error_handler_catch_all_set(&globalconf.evenths, xerror, NULL);
480 /* Allocate the key symbols */
481 globalconf.keysyms = xcb_key_symbols_alloc(globalconf.connection);
482 xcb_get_modifier_mapping_cookie_t xmapping_cookie =
483 xcb_get_modifier_mapping_unchecked(globalconf.connection);
485 /* init atom cache */
486 atoms_init(globalconf.connection);
488 /* init screens information */
489 screen_scan();
491 /* init default font and colors */
492 colors_reqs[0] = xcolor_init_unchecked(&globalconf.colors.fg,
493 "black", sizeof("black") - 1);
495 colors_reqs[1] = xcolor_init_unchecked(&globalconf.colors.bg,
496 "white", sizeof("white") - 1);
498 globalconf.font = draw_font_new("sans 8");
500 for(colors_nbr = 0; colors_nbr < 2; colors_nbr++)
501 xcolor_init_reply(colors_reqs[colors_nbr]);
503 xutil_lock_mask_get(globalconf.connection, xmapping_cookie,
504 globalconf.keysyms, &globalconf.numlockmask,
505 &globalconf.shiftlockmask, &globalconf.capslockmask,
506 &globalconf.modeswitchmask);
508 /* do this only for real screen */
509 for(screen_nbr = 0;
510 screen_nbr < xcb_setup_roots_length(xcb_get_setup(globalconf.connection));
511 screen_nbr++)
513 /* select for events */
514 const uint32_t change_win_vals[] =
516 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT | XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY
517 | XCB_EVENT_MASK_ENTER_WINDOW | XCB_EVENT_MASK_LEAVE_WINDOW
518 | XCB_EVENT_MASK_STRUCTURE_NOTIFY
519 | XCB_EVENT_MASK_PROPERTY_CHANGE
520 | XCB_EVENT_MASK_BUTTON_PRESS
521 | XCB_EVENT_MASK_BUTTON_RELEASE
522 | XCB_EVENT_MASK_FOCUS_CHANGE
525 xcb_change_window_attributes(globalconf.connection,
526 xutil_screen_get(globalconf.connection, screen_nbr)->root,
527 XCB_CW_EVENT_MASK,
528 change_win_vals);
529 ewmh_init(screen_nbr);
530 systray_init(screen_nbr);
533 /* init spawn (sn) */
534 spawn_init();
536 /* Parse and run configuration file */
537 if (!luaA_parserc(&xdg, confpath, true))
538 fatal("couldn't find any rc file");
540 p_delete(&confpath);
542 xdgWipeHandle(&xdg);
544 /* scan existing windows */
545 scan();
547 /* process all errors in the queue if any */
548 xcb_event_poll_for_event_loop(&globalconf.evenths);
549 a_xcb_set_event_handlers();
550 a_xcb_set_property_handlers();
552 /* we will receive events, stop grabbing server */
553 xcb_ungrab_server(globalconf.connection);
554 xcb_flush(globalconf.connection);
556 /* main event loop */
557 ev_loop(globalconf.loop, 0);
559 /* cleanup event loop */
560 ev_ref(globalconf.loop);
561 ev_check_stop(globalconf.loop, &xcheck);
562 ev_ref(globalconf.loop);
563 ev_prepare_stop(globalconf.loop, &a_refresh);
564 ev_ref(globalconf.loop);
565 ev_io_stop(globalconf.loop, &xio);
567 awesome_atexit();
569 return EXIT_SUCCESS;
572 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80