awful.layout.suit.magnifier: fix background client geometry
[awesome.git] / awesome.c
blob7f217b7e03ce718a8184523bf95680736bf944f6
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;
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 for(screen_nbr = 0;
78 screen_nbr < xcb_setup_roots_length(xcb_get_setup(globalconf.connection));
79 screen_nbr++)
80 systray_cleanup(screen_nbr);
82 /* remap all clients since some WM won't handle them otherwise */
83 for(c = globalconf.clients; c; c = c->next)
85 client_unban(c);
86 titlebar_client_detach(c);
89 xcb_flush(globalconf.connection);
91 xcb_disconnect(globalconf.connection);
94 /** Scan X to find windows to manage.
96 static void
97 scan(void)
99 int i, phys_screen, tree_c_len;
100 const int screen_max = xcb_setup_roots_length(xcb_get_setup(globalconf.connection));
101 root_win_t root_wins[screen_max];
102 xcb_query_tree_reply_t *tree_r;
103 xcb_window_t *wins = NULL;
104 xcb_get_window_attributes_reply_t *attr_r;
105 xcb_get_geometry_reply_t *geom_r;
106 long state;
108 for(phys_screen = 0; phys_screen < screen_max; phys_screen++)
110 /* Get the root window ID associated to this screen */
111 root_wins[phys_screen].id = xutil_screen_get(globalconf.connection, phys_screen)->root;
113 /* Get the window tree associated to this screen */
114 root_wins[phys_screen].tree_cookie = xcb_query_tree_unchecked(globalconf.connection,
115 root_wins[phys_screen].id);
118 for(phys_screen = 0; phys_screen < screen_max; phys_screen++)
120 tree_r = xcb_query_tree_reply(globalconf.connection,
121 root_wins[phys_screen].tree_cookie,
122 NULL);
124 if(!tree_r)
125 continue;
127 /* Get the tree of the children windows of the current root window */
128 if(!(wins = xcb_query_tree_children(tree_r)))
129 fatal("E: cannot get tree children");
131 tree_c_len = xcb_query_tree_children_length(tree_r);
132 xcb_get_window_attributes_cookie_t attr_wins[tree_c_len];
133 xcb_get_property_cookie_t state_wins[tree_c_len];
135 for(i = 0; i < tree_c_len; i++)
137 attr_wins[i] = xcb_get_window_attributes_unchecked(globalconf.connection,
138 wins[i]);
140 state_wins[i] = window_state_get_unchecked(wins[i]);
143 xcb_get_geometry_cookie_t *geom_wins[tree_c_len];
145 for(i = 0; i < tree_c_len; i++)
147 bool has_awesome_prop;
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 has_awesome_prop = xutil_text_prop_get(globalconf.connection, wins[i],
156 _AWESOME_TAGS, NULL, NULL);
158 if(!attr_r || attr_r->override_redirect
159 || (attr_r->map_state != XCB_MAP_STATE_VIEWABLE && !has_awesome_prop)
160 || (state == XCB_WM_STATE_WITHDRAWN && !has_awesome_prop))
162 geom_wins[i] = NULL;
163 p_delete(&attr_r);
164 continue;
167 p_delete(&attr_r);
169 /* Get the geometry of the current window */
170 geom_wins[i] = p_alloca(xcb_get_geometry_cookie_t, 1);
171 *(geom_wins[i]) = xcb_get_geometry_unchecked(globalconf.connection, wins[i]);
174 for(i = 0; i < tree_c_len; i++)
176 if(!geom_wins[i] || !(geom_r = xcb_get_geometry_reply(globalconf.connection,
177 *(geom_wins[i]), NULL)))
178 continue;
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_xcb_check_cb(EV_P_ ev_check *w, int revents)
192 xcb_event_poll_for_event_loop(&globalconf.evenths);
193 awesome_refresh(globalconf.connection);
196 static void
197 a_xcb_io_cb(EV_P_ ev_io *w, int revents)
199 /* empty */
202 /** Startup Error handler to check if another window manager
203 * is already running.
204 * \param data Additional optional parameters data.
205 * \param c X connection.
206 * \param error Error event.
208 static int __attribute__ ((noreturn))
209 xerrorstart(void * data __attribute__ ((unused)),
210 xcb_connection_t * c __attribute__ ((unused)),
211 xcb_generic_error_t * error __attribute__ ((unused)))
213 fatal("another window manager is already running");
216 /** Function to exit on some signals.
217 * \param sig the signal received, unused
219 static void
220 exit_on_signal(EV_P_ ev_signal *w, int revents)
222 ev_unloop(EV_A_ 1);
225 void
226 awesome_restart(void)
228 awesome_atexit();
229 a_exec(globalconf.argv);
232 /** Function to restart aweome on some signals.
233 * \param sig the signam received, unused
235 static void
236 restart_on_signal(EV_P_ ev_signal *w, int revents)
238 awesome_restart();
241 /** \brief awesome xerror function.
242 * There's no way to check accesses to destroyed windows, thus those cases are
243 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
244 * default error handler, which may call exit.
245 * \param data Currently unused.
246 * \param c The connectiont to the X server.
247 * \param e The error event.
248 * \return 0 if no error, or xerror's xlib return status.
250 static int
251 xerror(void *data __attribute__ ((unused)),
252 xcb_connection_t *c __attribute__ ((unused)),
253 xcb_generic_error_t *e)
255 xutil_error_t err;
257 if(!xutil_error_init(e, &err))
258 return 0;
260 /* ignore this */
261 if(e->error_code == XUTIL_BAD_WINDOW
262 || (e->error_code == XUTIL_BAD_MATCH && err.request_code == XCB_SET_INPUT_FOCUS)
263 || (e->error_code == XUTIL_BAD_VALUE && err.request_code == XCB_KILL_CLIENT)
264 || (err.request_code == XCB_CONFIGURE_WINDOW && e->error_code == XUTIL_BAD_MATCH))
265 goto bailout;
267 warn("X error: request=%s, error=%s", err.request_label, err.error_label);
270 * Xlib code was using default X error handler, namely
271 * '_XDefaultError()', which displays more informations about the
272 * error and also exit if 'error_code'' equals to
273 * 'BadImplementation'
275 * \todo display more informations about the error (like the Xlib default error handler)
277 if(e->error_code == XUTIL_BAD_IMPLEMENTATION)
278 fatal("X error: request=%s, error=%s", err.request_label, err.error_label);
280 bailout:
281 xutil_error_wipe(&err);
282 return 0;
285 /** Print help and exit(2) with given exit_code.
286 * \param exit_code The exit code.
288 static void __attribute__ ((noreturn))
289 exit_help(int exit_code)
291 FILE *outfile = (exit_code == EXIT_SUCCESS) ? stdout : stderr;
292 fprintf(outfile,
293 "Usage: awesome [OPTION]\n\
294 -h, --help show help\n\
295 -v, --version show version\n\
296 -c, --config FILE configuration file to use\n\
297 -k, --check check configuration file syntax\n");
298 exit(exit_code);
301 /** Hello, this is main.
302 * \param argc Who knows.
303 * \param argv Who knows.
304 * \return EXIT_SUCCESS I hope.
307 main(int argc, char **argv)
309 const char *confpath = NULL;
310 int xfd, i, screen_nbr, opt, colors_nbr;
311 xcolor_init_request_t colors_reqs[2];
312 xcb_get_modifier_mapping_cookie_t xmapping_cookie;
313 ssize_t cmdlen = 1;
314 static struct option long_options[] =
316 { "help", 0, NULL, 'h' },
317 { "version", 0, NULL, 'v' },
318 { "config", 1, NULL, 'c' },
319 { "check", 0, NULL, 'k' },
320 { NULL, 0, NULL, 0 }
323 /* event loop watchers */
324 ev_io xio = { .fd = -1 };
325 ev_check xcheck;
326 ev_signal sigint;
327 ev_signal sigterm;
328 ev_signal sighup;
330 /* clear the globalconf structure */
331 p_clear(&globalconf, 1);
332 globalconf.keygrabber = LUA_REFNIL;
333 globalconf.mousegrabber = LUA_REFNIL;
335 /* save argv */
336 for(i = 0; i < argc; i++)
337 cmdlen += a_strlen(argv[i]) + 1;
339 globalconf.argv = p_new(char, cmdlen);
340 a_strcpy(globalconf.argv, cmdlen, argv[0]);
342 for(i = 1; i < argc; i++)
344 a_strcat(globalconf.argv, cmdlen, " ");
345 a_strcat(globalconf.argv, cmdlen, argv[i]);
348 /* Text won't be printed correctly otherwise */
349 setlocale(LC_CTYPE, "");
351 /* init lua */
352 luaA_init();
354 /* check args */
355 while((opt = getopt_long(argc, argv, "vhkc:",
356 long_options, NULL)) != -1)
357 switch(opt)
359 case 'v':
360 eprint_version("awesome");
361 break;
362 case 'h':
363 exit_help(EXIT_SUCCESS);
364 break;
365 case 'k':
366 if(!luaA_parserc(confpath, false))
368 fprintf(stderr, "✘ Configuration file syntax error.\n");
369 return EXIT_FAILURE;
371 else
373 fprintf(stderr, "✔ Configuration file syntax OK.\n");
374 return EXIT_SUCCESS;
376 case 'c':
377 if(a_strlen(optarg))
378 confpath = a_strdup(optarg);
379 else
380 fatal("-c option requires a file name");
381 break;
384 globalconf.loop = ev_default_loop(0);
385 ev_timer_init(&globalconf.timer, &luaA_on_timer, 0., 0.);
387 /* register function for signals */
388 ev_signal_init(&sigint, exit_on_signal, SIGINT);
389 ev_signal_init(&sigterm, exit_on_signal, SIGTERM);
390 ev_signal_init(&sighup, restart_on_signal, SIGHUP);
391 ev_signal_start(globalconf.loop, &sigint);
392 ev_signal_start(globalconf.loop, &sigterm);
393 ev_signal_start(globalconf.loop, &sighup);
394 ev_unref(globalconf.loop);
395 ev_unref(globalconf.loop);
396 ev_unref(globalconf.loop);
398 /* X stuff */
399 globalconf.connection = xcb_connect(NULL, &globalconf.default_screen);
400 if(xcb_connection_has_error(globalconf.connection))
401 fatal("cannot open display");
403 /* check for xtest extension */
404 const xcb_query_extension_reply_t *xtest_query;
405 xtest_query = xcb_get_extension_data(globalconf.connection, &xcb_test_id);
406 globalconf.have_xtest = xtest_query->present;
408 /* initiliaze dbus */
409 a_dbus_init();
411 /* Grab server */
412 xcb_grab_server(globalconf.connection);
413 xcb_flush(globalconf.connection);
415 /* Get the file descriptor corresponding to the X connection */
416 xfd = xcb_get_file_descriptor(globalconf.connection);
417 ev_io_init(&xio, &a_xcb_io_cb, xfd, EV_READ);
418 ev_io_start(globalconf.loop, &xio);
419 ev_check_init(&xcheck, &a_xcb_check_cb);
420 ev_check_start(globalconf.loop, &xcheck);
421 ev_unref(globalconf.loop);
423 /* Allocate a handler which will holds all errors and events */
424 xcb_event_handlers_init(globalconf.connection, &globalconf.evenths);
425 xutil_error_handler_catch_all_set(&globalconf.evenths, xerrorstart, NULL);
427 for(screen_nbr = 0;
428 screen_nbr < xcb_setup_roots_length(xcb_get_setup(globalconf.connection));
429 screen_nbr++)
431 const uint32_t select_input_val = XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT;
433 /* This causes an error if some other window manager is running */
434 xcb_change_window_attributes(globalconf.connection,
435 xutil_screen_get(globalconf.connection, screen_nbr)->root,
436 XCB_CW_EVENT_MASK, &select_input_val);
439 /* Need to xcb_flush to validate error handler */
440 xcb_aux_sync(globalconf.connection);
442 /* Process all errors in the queue if any */
443 xcb_event_poll_for_event_loop(&globalconf.evenths);
445 /* Set the default xerror handler */
446 xutil_error_handler_catch_all_set(&globalconf.evenths, xerror, NULL);
448 /* Allocate the key symbols */
449 globalconf.keysyms = xcb_key_symbols_alloc(globalconf.connection);
451 /* Send the request to get the NumLock, ShiftLock and CapsLock
452 masks */
453 xmapping_cookie = xcb_get_modifier_mapping_unchecked(globalconf.connection);
455 /* init atom cache */
456 atoms_init(globalconf.connection);
458 /* init screens information */
459 screen_scan();
461 /* init default font and colors */
462 colors_reqs[0] = xcolor_init_unchecked(&globalconf.colors.fg,
463 "black", sizeof("black") - 1);
465 colors_reqs[1] = xcolor_init_unchecked(&globalconf.colors.bg,
466 "white", sizeof("white") - 1);
468 globalconf.font = draw_font_new("sans 8");
470 for(colors_nbr = 0; colors_nbr < 2; colors_nbr++)
471 xcolor_init_reply(colors_reqs[colors_nbr]);
473 /* Process the reply of previously sent mapping request */
474 xutil_lock_mask_get(globalconf.connection, xmapping_cookie,
475 globalconf.keysyms, &globalconf.numlockmask,
476 &globalconf.shiftlockmask, &globalconf.capslockmask);
478 /* do this only for real screen */
479 for(screen_nbr = 0;
480 screen_nbr < xcb_setup_roots_length(xcb_get_setup(globalconf.connection));
481 screen_nbr++)
483 /* select for events */
484 const uint32_t change_win_vals[] =
486 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT | XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY
487 | XCB_EVENT_MASK_ENTER_WINDOW | XCB_EVENT_MASK_LEAVE_WINDOW
488 | XCB_EVENT_MASK_STRUCTURE_NOTIFY
489 | XCB_EVENT_MASK_PROPERTY_CHANGE,
490 xcursor_new(globalconf.connection, XC_left_ptr)
493 xcb_change_window_attributes(globalconf.connection,
494 xutil_screen_get(globalconf.connection, screen_nbr)->root,
495 XCB_CW_EVENT_MASK | XCB_CW_CURSOR,
496 change_win_vals);
497 ewmh_init(screen_nbr);
498 systray_init(screen_nbr);
501 /* Parse and run configuration file */
502 luaA_parserc(confpath, true);
504 /* scan existing windows */
505 scan();
507 /* process all errors in the queue if any */
508 xcb_event_poll_for_event_loop(&globalconf.evenths);
509 a_xcb_set_event_handlers();
510 a_xcb_set_property_handlers();
512 /* we will receive events, stop grabbing server */
513 xcb_ungrab_server(globalconf.connection);
515 luaA_cs_init();
517 /* refresh everything before waiting events */
518 awesome_refresh(globalconf.connection);
520 /* main event loop */
521 ev_loop(globalconf.loop, 0);
523 /* cleanup event loop */
524 ev_ref(globalconf.loop);
525 ev_check_stop(globalconf.loop, &xcheck);
526 ev_ref(globalconf.loop);
527 ev_io_stop(globalconf.loop, &xio);
529 awesome_atexit();
531 return EXIT_SUCCESS;
534 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80