fix some deprecated atom constants
[awesome.git] / awesome.c
blob0b532aefbf615dd612bfe28d5517d14b4076c9be
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(void)
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 signal_object_emit(globalconf.L, &global_signals, "exit", 0);
74 a_dbus_cleanup();
76 /* do this only for real screen */
77 const xcb_setup_t *setup = xcb_get_setup(globalconf.connection);
78 nscreens = setup ? xcb_setup_roots_length(setup) : -1;
79 for(screen_nbr = 0;
80 screen_nbr < nscreens;
81 screen_nbr++)
82 systray_cleanup(screen_nbr);
84 /* Close Lua */
85 lua_close(globalconf.L);
87 xcb_flush(globalconf.connection);
89 /* Disconnect *after* closing lua */
90 xcb_disconnect(globalconf.connection);
92 ev_default_destroy();
95 /** Scan X to find windows to manage.
97 static void
98 scan(void)
100 int i, phys_screen, tree_c_len;
101 const int screen_max = xcb_setup_roots_length(xcb_get_setup(globalconf.connection));
102 root_win_t root_wins[screen_max];
103 xcb_query_tree_reply_t *tree_r;
104 xcb_window_t *wins = NULL;
105 xcb_get_window_attributes_reply_t *attr_r;
106 xcb_get_geometry_reply_t *geom_r;
107 long state;
109 for(phys_screen = 0; phys_screen < screen_max; phys_screen++)
111 /* Get the root window ID associated to this screen */
112 root_wins[phys_screen].id = xutil_screen_get(globalconf.connection, phys_screen)->root;
114 /* Get the window tree associated to this screen */
115 root_wins[phys_screen].tree_cookie = xcb_query_tree_unchecked(globalconf.connection,
116 root_wins[phys_screen].id);
119 for(phys_screen = 0; phys_screen < screen_max; phys_screen++)
121 tree_r = xcb_query_tree_reply(globalconf.connection,
122 root_wins[phys_screen].tree_cookie,
123 NULL);
125 if(!tree_r)
126 continue;
128 /* Get the tree of the children windows of the current root window */
129 if(!(wins = xcb_query_tree_children(tree_r)))
130 fatal("cannot get tree children");
132 tree_c_len = xcb_query_tree_children_length(tree_r);
133 xcb_get_window_attributes_cookie_t attr_wins[tree_c_len];
134 xcb_get_property_cookie_t state_wins[tree_c_len];
136 for(i = 0; i < tree_c_len; i++)
138 attr_wins[i] = xcb_get_window_attributes_unchecked(globalconf.connection,
139 wins[i]);
141 state_wins[i] = window_state_get_unchecked(wins[i]);
144 xcb_get_geometry_cookie_t *geom_wins[tree_c_len];
146 for(i = 0; i < tree_c_len; i++)
148 attr_r = xcb_get_window_attributes_reply(globalconf.connection,
149 attr_wins[i],
150 NULL);
152 state = window_state_get_reply(state_wins[i]);
154 if(!attr_r || attr_r->override_redirect
155 || attr_r->map_state == XCB_MAP_STATE_UNMAPPED
156 || state == XCB_WM_STATE_WITHDRAWN)
158 geom_wins[i] = NULL;
159 p_delete(&attr_r);
160 continue;
163 p_delete(&attr_r);
165 /* Get the geometry of the current window */
166 geom_wins[i] = p_alloca(xcb_get_geometry_cookie_t, 1);
167 *(geom_wins[i]) = xcb_get_geometry_unchecked(globalconf.connection, wins[i]);
170 for(i = 0; i < tree_c_len; i++)
172 if(!geom_wins[i] || !(geom_r = xcb_get_geometry_reply(globalconf.connection,
173 *(geom_wins[i]), NULL)))
174 continue;
176 /* The window can be mapped, so force it to be undrawn for startup */
177 xcb_unmap_window(globalconf.connection, wins[i]);
179 client_manage(wins[i], geom_r, phys_screen, true);
181 p_delete(&geom_r);
184 p_delete(&tree_r);
188 static void
189 a_refresh_cb(EV_P_ ev_prepare *w, int revents)
191 awesome_refresh();
194 static void
195 a_xcb_check_cb(EV_P_ ev_check *w, int revents)
197 xcb_generic_event_t *mouse = NULL, *event;
199 while((event = xcb_poll_for_event(globalconf.connection)))
201 /* We will treat mouse events later.
202 * We cannot afford to treat all mouse motion events,
203 * because that would be too much CPU intensive, so we just
204 * take the last we get after a bunch of events. */
205 if(XCB_EVENT_RESPONSE_TYPE(event) == XCB_MOTION_NOTIFY)
207 p_delete(&mouse);
208 mouse = event;
210 else
212 event_handle(event);
213 p_delete(&event);
217 if(mouse)
219 event_handle(mouse);
220 p_delete(&mouse);
224 static void
225 a_xcb_io_cb(EV_P_ ev_io *w, int revents)
227 /* empty */
230 static void
231 signal_fatal(int signum)
233 buffer_t buf;
234 backtrace_get(&buf);
235 fatal("dumping backtrace\n%s", buf.s);
238 /** Function to exit on some signals.
239 * \param w the signal received, unused
240 * \param revents currently unused
242 static void
243 exit_on_signal(EV_P_ ev_signal *w, int revents)
245 ev_unloop(EV_A_ 1);
248 void
249 awesome_restart(void)
251 awesome_atexit();
252 a_exec(globalconf.argv);
255 /** Function to restart awesome on some signals.
256 * \param w the signal received, unused
257 * \param revents Currently unused
259 static void
260 restart_on_signal(EV_P_ ev_signal *w, int revents)
262 awesome_restart();
265 /** Print help and exit(2) with given exit_code.
266 * \param exit_code The exit code.
268 static void __attribute__ ((noreturn))
269 exit_help(int exit_code)
271 FILE *outfile = (exit_code == EXIT_SUCCESS) ? stdout : stderr;
272 fprintf(outfile,
273 "Usage: awesome [OPTION]\n\
274 -h, --help show help\n\
275 -v, --version show version\n\
276 -c, --config FILE configuration file to use\n\
277 -k, --check check configuration file syntax\n");
278 exit(exit_code);
281 /** Hello, this is main.
282 * \param argc Who knows.
283 * \param argv Who knows.
284 * \return EXIT_SUCCESS I hope.
287 main(int argc, char **argv)
289 char *confpath = NULL;
290 int xfd, i, screen_nbr, opt, colors_nbr;
291 xcolor_init_request_t colors_reqs[2];
292 ssize_t cmdlen = 1;
293 xdgHandle xdg;
294 static struct option long_options[] =
296 { "help", 0, NULL, 'h' },
297 { "version", 0, NULL, 'v' },
298 { "config", 1, NULL, 'c' },
299 { "check", 0, NULL, 'k' },
300 { NULL, 0, NULL, 0 }
303 /* event loop watchers */
304 ev_io xio = { .fd = -1 };
305 ev_check xcheck;
306 ev_prepare a_refresh;
307 ev_signal sigint;
308 ev_signal sigterm;
309 ev_signal sighup;
311 /* clear the globalconf structure */
312 p_clear(&globalconf, 1);
313 globalconf.keygrabber = LUA_REFNIL;
314 globalconf.mousegrabber = LUA_REFNIL;
316 /* save argv */
317 for(i = 0; i < argc; i++)
318 cmdlen += a_strlen(argv[i]) + 1;
320 globalconf.argv = p_new(char, cmdlen);
321 a_strcpy(globalconf.argv, cmdlen, argv[0]);
323 for(i = 1; i < argc; i++)
325 a_strcat(globalconf.argv, cmdlen, " ");
326 a_strcat(globalconf.argv, cmdlen, argv[i]);
329 /* Text won't be printed correctly otherwise */
330 setlocale(LC_CTYPE, "");
332 /* Get XDG basedir data */
333 xdgInitHandle(&xdg);
335 /* init lua */
336 luaA_init(&xdg);
338 /* check args */
339 while((opt = getopt_long(argc, argv, "vhkc:",
340 long_options, NULL)) != -1)
341 switch(opt)
343 case 'v':
344 eprint_version();
345 break;
346 case 'h':
347 exit_help(EXIT_SUCCESS);
348 break;
349 case 'k':
350 if(!luaA_parserc(&xdg, confpath, false))
352 fprintf(stderr, "✘ Configuration file syntax error.\n");
353 return EXIT_FAILURE;
355 else
357 fprintf(stderr, "✔ Configuration file syntax OK.\n");
358 return EXIT_SUCCESS;
360 case 'c':
361 if(a_strlen(optarg))
362 confpath = a_strdup(optarg);
363 else
364 fatal("-c option requires a file name");
365 break;
368 globalconf.loop = ev_default_loop(0);
369 ev_timer_init(&globalconf.timer, &luaA_on_timer, 0., 0.);
371 /* register function for signals */
372 ev_signal_init(&sigint, exit_on_signal, SIGINT);
373 ev_signal_init(&sigterm, exit_on_signal, SIGTERM);
374 ev_signal_init(&sighup, restart_on_signal, SIGHUP);
375 ev_signal_start(globalconf.loop, &sigint);
376 ev_signal_start(globalconf.loop, &sigterm);
377 ev_signal_start(globalconf.loop, &sighup);
378 ev_unref(globalconf.loop);
379 ev_unref(globalconf.loop);
380 ev_unref(globalconf.loop);
382 struct sigaction sa = { .sa_handler = signal_fatal, .sa_flags = 0 };
383 sigemptyset(&sa.sa_mask);
384 sigaction(SIGSEGV, &sa, 0);
386 /* X stuff */
387 globalconf.connection = xcb_connect(NULL, &globalconf.default_screen);
388 if(xcb_connection_has_error(globalconf.connection))
389 fatal("cannot open display");
391 /* Prefetch all the extensions we might need */
392 xcb_prefetch_extension_data(globalconf.connection, &xcb_big_requests_id);
393 xcb_prefetch_extension_data(globalconf.connection, &xcb_test_id);
394 xcb_prefetch_extension_data(globalconf.connection, &xcb_randr_id);
395 xcb_prefetch_extension_data(globalconf.connection, &xcb_xinerama_id);
396 xcb_prefetch_extension_data(globalconf.connection, &xcb_shape_id);
398 /* initialize dbus */
399 a_dbus_init();
401 /* Grab server */
402 xcb_grab_server(globalconf.connection);
403 xcb_flush(globalconf.connection);
405 /* Get the file descriptor corresponding to the X connection */
406 xfd = xcb_get_file_descriptor(globalconf.connection);
407 ev_io_init(&xio, &a_xcb_io_cb, xfd, EV_READ);
408 ev_io_start(globalconf.loop, &xio);
409 ev_check_init(&xcheck, &a_xcb_check_cb);
410 ev_check_start(globalconf.loop, &xcheck);
411 ev_unref(globalconf.loop);
412 ev_prepare_init(&a_refresh, &a_refresh_cb);
413 ev_prepare_start(globalconf.loop, &a_refresh);
414 ev_unref(globalconf.loop);
416 for(screen_nbr = 0;
417 screen_nbr < xcb_setup_roots_length(xcb_get_setup(globalconf.connection));
418 screen_nbr++)
420 const uint32_t select_input_val = XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT;
422 /* This causes an error if some other window manager is running */
423 xcb_change_window_attributes(globalconf.connection,
424 xutil_screen_get(globalconf.connection, screen_nbr)->root,
425 XCB_CW_EVENT_MASK, &select_input_val);
428 /* Need to xcb_flush to validate error handler */
429 xcb_aux_sync(globalconf.connection);
431 /* Process all errors in the queue if any. There can be no events yet, so if
432 * this function returns something, it must be an error. */
433 if (xcb_poll_for_event(globalconf.connection) != NULL)
434 fatal("another window manager is already running");
436 /* Prefetch the maximum request length */
437 xcb_prefetch_maximum_request_length(globalconf.connection);
439 /* check for xtest extension */
440 const xcb_query_extension_reply_t *xtest_query;
441 xtest_query = xcb_get_extension_data(globalconf.connection, &xcb_test_id);
442 globalconf.have_xtest = xtest_query->present;
444 /* Allocate the key symbols */
445 globalconf.keysyms = xcb_key_symbols_alloc(globalconf.connection);
446 xcb_get_modifier_mapping_cookie_t xmapping_cookie =
447 xcb_get_modifier_mapping_unchecked(globalconf.connection);
449 /* init atom cache */
450 atoms_init(globalconf.connection);
452 /* init screens information */
453 screen_scan();
455 /* init default font and colors */
456 colors_reqs[0] = xcolor_init_unchecked(&globalconf.colors.fg,
457 "black", sizeof("black") - 1);
459 colors_reqs[1] = xcolor_init_unchecked(&globalconf.colors.bg,
460 "white", sizeof("white") - 1);
462 globalconf.font = draw_font_new("sans 8");
464 for(colors_nbr = 0; colors_nbr < 2; colors_nbr++)
465 xcolor_init_reply(colors_reqs[colors_nbr]);
467 xutil_lock_mask_get(globalconf.connection, xmapping_cookie,
468 globalconf.keysyms, &globalconf.numlockmask,
469 &globalconf.shiftlockmask, &globalconf.capslockmask,
470 &globalconf.modeswitchmask);
472 /* do this only for real screen */
473 for(screen_nbr = 0;
474 screen_nbr < xcb_setup_roots_length(xcb_get_setup(globalconf.connection));
475 screen_nbr++)
477 /* select for events */
478 const uint32_t change_win_vals[] =
480 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT | XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY
481 | XCB_EVENT_MASK_ENTER_WINDOW | XCB_EVENT_MASK_LEAVE_WINDOW
482 | XCB_EVENT_MASK_STRUCTURE_NOTIFY
483 | XCB_EVENT_MASK_PROPERTY_CHANGE
484 | XCB_EVENT_MASK_BUTTON_PRESS
485 | XCB_EVENT_MASK_BUTTON_RELEASE
486 | XCB_EVENT_MASK_FOCUS_CHANGE
489 xcb_change_window_attributes(globalconf.connection,
490 xutil_screen_get(globalconf.connection, screen_nbr)->root,
491 XCB_CW_EVENT_MASK,
492 change_win_vals);
493 ewmh_init(screen_nbr);
494 systray_init(screen_nbr);
497 /* init spawn (sn) */
498 spawn_init();
500 /* Parse and run configuration file */
501 if (!luaA_parserc(&xdg, confpath, true))
502 fatal("couldn't find any rc file");
504 p_delete(&confpath);
506 xdgWipeHandle(&xdg);
508 /* scan existing windows */
509 scan();
511 /* we will receive events, stop grabbing server */
512 xcb_ungrab_server(globalconf.connection);
513 xcb_flush(globalconf.connection);
515 /* main event loop */
516 ev_loop(globalconf.loop, 0);
518 /* cleanup event loop */
519 ev_ref(globalconf.loop);
520 ev_check_stop(globalconf.loop, &xcheck);
521 ev_ref(globalconf.loop);
522 ev_prepare_stop(globalconf.loop, &a_refresh);
523 ev_ref(globalconf.loop);
524 ev_io_stop(globalconf.loop, &xio);
526 awesome_atexit();
528 return EXIT_SUCCESS;
531 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80