client: make `ontop' windows under fullscreen
[awesome.git] / awesome.c
blobfd6bfff0f27e5207bc9232f9a9655e813a3ca81c
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 <ev.h>
31 #include <xcb/xtest.h>
32 #include <xcb/xcb_event.h>
34 #include "awesome.h"
35 #include "client.h"
36 #include "window.h"
37 #include "ewmh.h"
38 #include "dbus.h"
39 #include "systray.h"
40 #include "event.h"
41 #include "property.h"
42 #include "screen.h"
43 #include "titlebar.h"
44 #include "common/version.h"
45 #include "common/atoms.h"
46 #include "common/xcursor.h"
47 #include "config.h"
49 awesome_t globalconf;
51 typedef struct
53 xcb_window_t id;
54 xcb_query_tree_cookie_t tree_cookie;
55 } root_win_t;
57 /** Call before exiting.
59 void
60 awesome_atexit(void)
62 client_t *c;
63 int screen_nbr, nscreens;
65 a_dbus_cleanup();
66 luaA_cs_cleanup();
68 /* reparent systray windows, otherwise they may die with their master */
69 for(int i = 0; i < globalconf.embedded.len; i++)
71 xcb_screen_t *s = xutil_screen_get(globalconf.connection,
72 globalconf.embedded.tab[i].phys_screen);
73 xembed_window_unembed(globalconf.connection, globalconf.embedded.tab[i].win, s->root);
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 /* remap all clients since some WM won't handle them otherwise */
85 for(c = globalconf.clients; c; c = c->next)
87 client_unban(c);
88 titlebar_client_detach(c);
91 xcb_flush(globalconf.connection);
93 xcb_disconnect(globalconf.connection);
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("E: 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 bool has_awesome_prop;
151 attr_r = xcb_get_window_attributes_reply(globalconf.connection,
152 attr_wins[i],
153 NULL);
155 state = window_state_get_reply(state_wins[i]);
157 has_awesome_prop = xutil_text_prop_get(globalconf.connection, wins[i],
158 _AWESOME_TAGS, NULL, NULL);
160 if(!attr_r || attr_r->override_redirect
161 || (attr_r->map_state != XCB_MAP_STATE_VIEWABLE && !has_awesome_prop)
162 || (state == XCB_WM_STATE_WITHDRAWN && !has_awesome_prop))
164 geom_wins[i] = NULL;
165 p_delete(&attr_r);
166 continue;
169 p_delete(&attr_r);
171 /* Get the geometry of the current window */
172 geom_wins[i] = p_alloca(xcb_get_geometry_cookie_t, 1);
173 *(geom_wins[i]) = xcb_get_geometry_unchecked(globalconf.connection, wins[i]);
176 for(i = 0; i < tree_c_len; i++)
178 if(!geom_wins[i] || !(geom_r = xcb_get_geometry_reply(globalconf.connection,
179 *(geom_wins[i]), NULL)))
180 continue;
182 client_manage(wins[i], geom_r, phys_screen, true);
184 p_delete(&geom_r);
187 p_delete(&tree_r);
191 static void
192 a_xcb_check_cb(EV_P_ ev_check *w, int revents)
194 xcb_generic_event_t *mouse = NULL, *event;
196 while((event = xcb_poll_for_event(globalconf.connection)))
198 /* We will treat mouse events later.
199 * We cannot afford to treat all mouse motion events,
200 * because that would be too much CPU intensive, so we just
201 * take the last we get after a bunch of events. */
202 if(XCB_EVENT_RESPONSE_TYPE(event) == XCB_MOTION_NOTIFY)
204 p_delete(&mouse);
205 mouse = event;
207 else
209 xcb_event_handle(&globalconf.evenths, event);
210 p_delete(&event);
214 if(mouse)
216 xcb_event_handle(&globalconf.evenths, mouse);
217 p_delete(&mouse);
220 awesome_refresh(globalconf.connection);
223 static void
224 a_xcb_io_cb(EV_P_ ev_io *w, int revents)
226 /* empty */
229 /** Startup Error handler to check if another window manager
230 * is already running.
231 * \param data Additional optional parameters data.
232 * \param c X connection.
233 * \param error Error event.
235 static int __attribute__ ((noreturn))
236 xerrorstart(void * data __attribute__ ((unused)),
237 xcb_connection_t * c __attribute__ ((unused)),
238 xcb_generic_error_t * error __attribute__ ((unused)))
240 fatal("another window manager is already running");
243 /** Function to exit on some signals.
244 * \param sig the signal received, 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();
256 a_exec(globalconf.argv);
259 /** Function to restart aweome on some signals.
260 * \param sig the signam received, unused
262 static void
263 restart_on_signal(EV_P_ ev_signal *w, int revents)
265 awesome_restart();
268 /** \brief awesome xerror function.
269 * There's no way to check accesses to destroyed windows, thus those cases are
270 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
271 * default error handler, which may call exit.
272 * \param data Currently unused.
273 * \param c The connectiont to the X server.
274 * \param e The error event.
275 * \return 0 if no error, or xerror's xlib return status.
277 static int
278 xerror(void *data __attribute__ ((unused)),
279 xcb_connection_t *c __attribute__ ((unused)),
280 xcb_generic_error_t *e)
282 xutil_error_t err;
284 if(!xutil_error_init(e, &err))
285 return 0;
287 /* ignore this */
288 if(e->error_code == XUTIL_BAD_WINDOW
289 || (e->error_code == XUTIL_BAD_MATCH && err.request_code == XCB_SET_INPUT_FOCUS)
290 || (e->error_code == XUTIL_BAD_VALUE && err.request_code == XCB_KILL_CLIENT)
291 || (err.request_code == XCB_CONFIGURE_WINDOW && e->error_code == XUTIL_BAD_MATCH))
292 goto bailout;
294 warn("X error: request=%s, error=%s", err.request_label, err.error_label);
297 * Xlib code was using default X error handler, namely
298 * '_XDefaultError()', which displays more informations about the
299 * error and also exit if 'error_code'' equals to
300 * 'BadImplementation'
302 * \todo display more informations about the error (like the Xlib default error handler)
304 if(e->error_code == XUTIL_BAD_IMPLEMENTATION)
305 fatal("X error: request=%s, error=%s", err.request_label, err.error_label);
307 bailout:
308 xutil_error_wipe(&err);
309 return 0;
312 /** Print help and exit(2) with given exit_code.
313 * \param exit_code The exit code.
315 static void __attribute__ ((noreturn))
316 exit_help(int exit_code)
318 FILE *outfile = (exit_code == EXIT_SUCCESS) ? stdout : stderr;
319 fprintf(outfile,
320 "Usage: awesome [OPTION]\n\
321 -h, --help show help\n\
322 -v, --version show version\n\
323 -c, --config FILE configuration file to use\n\
324 -k, --check check configuration file syntax\n");
325 exit(exit_code);
328 /** Hello, this is main.
329 * \param argc Who knows.
330 * \param argv Who knows.
331 * \return EXIT_SUCCESS I hope.
334 main(int argc, char **argv)
336 const char *confpath = NULL;
337 int xfd, i, screen_nbr, opt, colors_nbr;
338 xcolor_init_request_t colors_reqs[2];
339 xcb_get_modifier_mapping_cookie_t xmapping_cookie;
340 ssize_t cmdlen = 1;
341 static struct option long_options[] =
343 { "help", 0, NULL, 'h' },
344 { "version", 0, NULL, 'v' },
345 { "config", 1, NULL, 'c' },
346 { "check", 0, NULL, 'k' },
347 { NULL, 0, NULL, 0 }
350 /* event loop watchers */
351 ev_io xio = { .fd = -1 };
352 ev_check xcheck;
353 ev_signal sigint;
354 ev_signal sigterm;
355 ev_signal sighup;
357 /* clear the globalconf structure */
358 p_clear(&globalconf, 1);
359 globalconf.keygrabber = LUA_REFNIL;
360 globalconf.mousegrabber = LUA_REFNIL;
362 /* save argv */
363 for(i = 0; i < argc; i++)
364 cmdlen += a_strlen(argv[i]) + 1;
366 globalconf.argv = p_new(char, cmdlen);
367 a_strcpy(globalconf.argv, cmdlen, argv[0]);
369 for(i = 1; i < argc; i++)
371 a_strcat(globalconf.argv, cmdlen, " ");
372 a_strcat(globalconf.argv, cmdlen, argv[i]);
375 /* Text won't be printed correctly otherwise */
376 setlocale(LC_CTYPE, "");
378 /* init lua */
379 luaA_init();
381 /* check args */
382 while((opt = getopt_long(argc, argv, "vhkc:",
383 long_options, NULL)) != -1)
384 switch(opt)
386 case 'v':
387 eprint_version("awesome");
388 break;
389 case 'h':
390 exit_help(EXIT_SUCCESS);
391 break;
392 case 'k':
393 if(!luaA_parserc(confpath, false))
395 fprintf(stderr, "✘ Configuration file syntax error.\n");
396 return EXIT_FAILURE;
398 else
400 fprintf(stderr, "✔ Configuration file syntax OK.\n");
401 return EXIT_SUCCESS;
403 case 'c':
404 if(a_strlen(optarg))
405 confpath = a_strdup(optarg);
406 else
407 fatal("-c option requires a file name");
408 break;
411 globalconf.loop = ev_default_loop(0);
412 ev_timer_init(&globalconf.timer, &luaA_on_timer, 0., 0.);
414 /* register function for signals */
415 ev_signal_init(&sigint, exit_on_signal, SIGINT);
416 ev_signal_init(&sigterm, exit_on_signal, SIGTERM);
417 ev_signal_init(&sighup, restart_on_signal, SIGHUP);
418 ev_signal_start(globalconf.loop, &sigint);
419 ev_signal_start(globalconf.loop, &sigterm);
420 ev_signal_start(globalconf.loop, &sighup);
421 ev_unref(globalconf.loop);
422 ev_unref(globalconf.loop);
423 ev_unref(globalconf.loop);
425 /* X stuff */
426 globalconf.connection = xcb_connect(NULL, &globalconf.default_screen);
427 if(xcb_connection_has_error(globalconf.connection))
428 fatal("cannot open display");
430 /* check for xtest extension */
431 const xcb_query_extension_reply_t *xtest_query;
432 xtest_query = xcb_get_extension_data(globalconf.connection, &xcb_test_id);
433 globalconf.have_xtest = xtest_query->present;
435 /* initiliaze dbus */
436 a_dbus_init();
438 /* Grab server */
439 xcb_grab_server(globalconf.connection);
440 xcb_flush(globalconf.connection);
442 /* Get the file descriptor corresponding to the X connection */
443 xfd = xcb_get_file_descriptor(globalconf.connection);
444 ev_io_init(&xio, &a_xcb_io_cb, xfd, EV_READ);
445 ev_io_start(globalconf.loop, &xio);
446 ev_check_init(&xcheck, &a_xcb_check_cb);
447 ev_check_start(globalconf.loop, &xcheck);
448 ev_unref(globalconf.loop);
450 /* Allocate a handler which will holds all errors and events */
451 xcb_event_handlers_init(globalconf.connection, &globalconf.evenths);
452 xutil_error_handler_catch_all_set(&globalconf.evenths, xerrorstart, NULL);
454 for(screen_nbr = 0;
455 screen_nbr < xcb_setup_roots_length(xcb_get_setup(globalconf.connection));
456 screen_nbr++)
458 const uint32_t select_input_val = XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT;
460 /* This causes an error if some other window manager is running */
461 xcb_change_window_attributes(globalconf.connection,
462 xutil_screen_get(globalconf.connection, screen_nbr)->root,
463 XCB_CW_EVENT_MASK, &select_input_val);
466 /* Need to xcb_flush to validate error handler */
467 xcb_aux_sync(globalconf.connection);
469 /* Process all errors in the queue if any */
470 xcb_event_poll_for_event_loop(&globalconf.evenths);
472 /* Set the default xerror handler */
473 xutil_error_handler_catch_all_set(&globalconf.evenths, xerror, NULL);
475 /* Allocate the key symbols */
476 globalconf.keysyms = xcb_key_symbols_alloc(globalconf.connection);
478 /* Send the request to get the NumLock, ShiftLock and CapsLock
479 masks */
480 xmapping_cookie = xcb_get_modifier_mapping_unchecked(globalconf.connection);
482 /* init atom cache */
483 atoms_init(globalconf.connection);
485 /* init screens information */
486 screen_scan();
488 /* init default font and colors */
489 colors_reqs[0] = xcolor_init_unchecked(&globalconf.colors.fg,
490 "black", sizeof("black") - 1);
492 colors_reqs[1] = xcolor_init_unchecked(&globalconf.colors.bg,
493 "white", sizeof("white") - 1);
495 globalconf.font = draw_font_new("sans 8");
497 for(colors_nbr = 0; colors_nbr < 2; colors_nbr++)
498 xcolor_init_reply(colors_reqs[colors_nbr]);
500 /* Process the reply of previously sent mapping request */
501 xutil_lock_mask_get(globalconf.connection, xmapping_cookie,
502 globalconf.keysyms, &globalconf.numlockmask,
503 &globalconf.shiftlockmask, &globalconf.capslockmask);
505 /* do this only for real screen */
506 for(screen_nbr = 0;
507 screen_nbr < xcb_setup_roots_length(xcb_get_setup(globalconf.connection));
508 screen_nbr++)
510 /* select for events */
511 const uint32_t change_win_vals[] =
513 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT | XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY
514 | XCB_EVENT_MASK_ENTER_WINDOW | XCB_EVENT_MASK_LEAVE_WINDOW
515 | XCB_EVENT_MASK_STRUCTURE_NOTIFY
516 | XCB_EVENT_MASK_PROPERTY_CHANGE,
517 xcursor_new(globalconf.connection, XC_left_ptr)
520 xcb_change_window_attributes(globalconf.connection,
521 xutil_screen_get(globalconf.connection, screen_nbr)->root,
522 XCB_CW_EVENT_MASK | XCB_CW_CURSOR,
523 change_win_vals);
524 ewmh_init(screen_nbr);
525 systray_init(screen_nbr);
528 /* Parse and run configuration file */
529 luaA_parserc(confpath, true);
531 /* scan existing windows */
532 scan();
534 /* process all errors in the queue if any */
535 xcb_event_poll_for_event_loop(&globalconf.evenths);
536 a_xcb_set_event_handlers();
537 a_xcb_set_property_handlers();
539 /* we will receive events, stop grabbing server */
540 xcb_ungrab_server(globalconf.connection);
542 luaA_cs_init();
544 /* refresh everything before waiting events */
545 awesome_refresh(globalconf.connection);
547 /* main event loop */
548 ev_loop(globalconf.loop, 0);
550 /* cleanup event loop */
551 ev_ref(globalconf.loop);
552 ev_check_stop(globalconf.loop, &xcheck);
553 ev_ref(globalconf.loop);
554 ev_io_stop(globalconf.loop, &xio);
556 awesome_atexit();
558 return EXIT_SUCCESS;
561 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80