suit.fair: restored C version fair layout's behaviour
[awesome.git] / awesome.c
blobb2130c03cc95c27e3229b9b3839fc5f384624e2a
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/xcb_event.h>
33 #include "awesome.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 "common/version.h"
44 #include "common/atoms.h"
45 #include "common/xcursor.h"
46 #include "config.h"
48 awesome_t globalconf;
50 typedef struct
52 xcb_window_t id;
53 xcb_query_tree_cookie_t tree_cookie;
54 } root_win_t;
56 /** Call before exiting.
58 void
59 awesome_atexit(void)
61 client_t *c;
62 int screen_nbr;
64 a_dbus_cleanup();
65 luaA_cs_cleanup();
67 /* reparent systray windows, otherwise they may die with their master */
68 for(int i = 0; i < globalconf.embedded.len; i++)
70 xcb_screen_t *s = xutil_screen_get(globalconf.connection,
71 globalconf.embedded.tab[i].phys_screen);
72 xembed_window_unembed(globalconf.connection, globalconf.embedded.tab[i].win, s->root);
75 /* do this only for real screen */
76 for(screen_nbr = 0;
77 screen_nbr < xcb_setup_roots_length(xcb_get_setup(globalconf.connection));
78 screen_nbr++)
79 systray_cleanup(screen_nbr);
81 /* remap all clients since some WM won't handle them otherwise */
82 for(c = globalconf.clients; c; c = c->next)
84 client_unban(c);
85 titlebar_client_detach(c);
88 xcb_flush(globalconf.connection);
90 xcb_disconnect(globalconf.connection);
93 /** Scan X to find windows to manage.
95 static void
96 scan(void)
98 int i, screen, phys_screen, tree_c_len;
99 const int screen_max = xcb_setup_roots_length(xcb_get_setup(globalconf.connection));
100 root_win_t root_wins[screen_max];
101 xcb_query_tree_reply_t *tree_r;
102 xcb_window_t *wins = NULL;
103 xcb_get_window_attributes_reply_t *attr_r;
104 xcb_get_geometry_reply_t *geom_r;
105 long state;
107 for(phys_screen = 0; phys_screen < screen_max; phys_screen++)
109 /* Get the root window ID associated to this screen */
110 root_wins[phys_screen].id = xutil_screen_get(globalconf.connection, phys_screen)->root;
112 /* Get the window tree associated to this screen */
113 root_wins[phys_screen].tree_cookie = xcb_query_tree_unchecked(globalconf.connection,
114 root_wins[phys_screen].id);
117 for(phys_screen = 0; phys_screen < screen_max; phys_screen++)
119 tree_r = xcb_query_tree_reply(globalconf.connection,
120 root_wins[phys_screen].tree_cookie,
121 NULL);
123 if(!tree_r)
124 continue;
126 /* Get the tree of the children windows of the current root window */
127 if(!(wins = xcb_query_tree_children(tree_r)))
128 fatal("E: cannot get tree children");
130 tree_c_len = xcb_query_tree_children_length(tree_r);
131 xcb_get_window_attributes_cookie_t attr_wins[tree_c_len];
132 xcb_get_property_cookie_t state_wins[tree_c_len];
134 for(i = 0; i < tree_c_len; i++)
136 attr_wins[i] = xcb_get_window_attributes_unchecked(globalconf.connection,
137 wins[i]);
139 state_wins[i] = window_state_get_unchecked(wins[i]);
142 xcb_get_geometry_cookie_t *geom_wins[tree_c_len];
144 for(i = 0; i < tree_c_len; i++)
146 bool has_awesome_prop;
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 has_awesome_prop = xutil_text_prop_get(globalconf.connection, wins[i],
155 _AWESOME_TAGS, NULL, NULL);
157 if(!attr_r || attr_r->override_redirect
158 || (attr_r->map_state != XCB_MAP_STATE_VIEWABLE && !has_awesome_prop)
159 || (state == XCB_WM_STATE_WITHDRAWN && !has_awesome_prop))
161 geom_wins[i] = NULL;
162 p_delete(&attr_r);
163 continue;
166 p_delete(&attr_r);
168 /* Get the geometry of the current window */
169 geom_wins[i] = p_alloca(xcb_get_geometry_cookie_t, 1);
170 *(geom_wins[i]) = xcb_get_geometry_unchecked(globalconf.connection, wins[i]);
173 for(i = 0; i < tree_c_len; i++)
175 if(!geom_wins[i] || !(geom_r = xcb_get_geometry_reply(globalconf.connection,
176 *(geom_wins[i]), NULL)))
177 continue;
179 screen = screen_getbycoord(phys_screen, geom_r->x, geom_r->y);
181 client_manage(wins[i], geom_r, phys_screen, screen);
183 p_delete(&geom_r);
186 p_delete(&tree_r);
190 static void
191 a_xcb_check_cb(EV_P_ ev_check *w, int revents)
193 xcb_event_poll_for_event_loop(&globalconf.evenths);
194 awesome_refresh(globalconf.connection);
197 static void
198 a_xcb_io_cb(EV_P_ ev_io *w, int revents)
200 /* empty */
203 /** Startup Error handler to check if another window manager
204 * is already running.
205 * \param data Additional optional parameters data.
206 * \param c X connection.
207 * \param error Error event.
209 static int __attribute__ ((noreturn))
210 xerrorstart(void * data __attribute__ ((unused)),
211 xcb_connection_t * c __attribute__ ((unused)),
212 xcb_generic_error_t * error __attribute__ ((unused)))
214 fatal("another window manager is already running");
217 /** Function to exit on some signals.
218 * \param sig the signal received, unused
220 static void
221 exit_on_signal(EV_P_ ev_signal *w, int revents)
223 ev_unloop(EV_A_ 1);
226 void
227 awesome_restart(void)
229 awesome_atexit();
230 a_exec(globalconf.argv);
233 /** Function to restart aweome on some signals.
234 * \param sig the signam received, unused
236 static void
237 restart_on_signal(EV_P_ ev_signal *w, int revents)
239 awesome_restart();
242 /** \brief awesome xerror function.
243 * There's no way to check accesses to destroyed windows, thus those cases are
244 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
245 * default error handler, which may call exit.
246 * \param data Currently unused.
247 * \param c The connectiont to the X server.
248 * \param e The error event.
249 * \return 0 if no error, or xerror's xlib return status.
251 static int
252 xerror(void *data __attribute__ ((unused)),
253 xcb_connection_t *c __attribute__ ((unused)),
254 xcb_generic_error_t *e)
256 xutil_error_t err;
258 if(!xutil_error_init(e, &err))
259 return 0;
261 /* ignore this */
262 if(e->error_code == XUTIL_BAD_WINDOW
263 || (e->error_code == XUTIL_BAD_MATCH && err.request_code == XCB_SET_INPUT_FOCUS)
264 || (e->error_code == XUTIL_BAD_VALUE && err.request_code == XCB_KILL_CLIENT)
265 || (err.request_code == XCB_CONFIGURE_WINDOW && e->error_code == XUTIL_BAD_MATCH))
266 goto bailout;
268 warn("X error: request=%s, error=%s", err.request_label, err.error_label);
271 * Xlib code was using default X error handler, namely
272 * '_XDefaultError()', which displays more informations about the
273 * error and also exit if 'error_code'' equals to
274 * 'BadImplementation'
276 * \todo display more informations about the error (like the Xlib default error handler)
278 if(e->error_code == XUTIL_BAD_IMPLEMENTATION)
279 fatal("X error: request=%s, error=%s", err.request_label, err.error_label);
281 bailout:
282 xutil_error_wipe(&err);
283 return 0;
286 /** Print help and exit(2) with given exit_code.
287 * \param exit_code The exit code.
289 static void __attribute__ ((noreturn))
290 exit_help(int exit_code)
292 FILE *outfile = (exit_code == EXIT_SUCCESS) ? stdout : stderr;
293 fprintf(outfile,
294 "Usage: awesome [OPTION]\n\
295 -h, --help show help\n\
296 -v, --version show version\n\
297 -c, --config FILE configuration file to use\n\
298 -k, --check check configuration file syntax\n");
299 exit(exit_code);
302 /** Hello, this is main.
303 * \param argc Who knows.
304 * \param argv Who knows.
305 * \return EXIT_SUCCESS I hope.
308 main(int argc, char **argv)
310 const char *confpath = NULL;
311 int xfd, i, screen_nbr, opt, colors_nbr;
312 xcolor_init_request_t colors_reqs[2];
313 xcb_get_modifier_mapping_cookie_t xmapping_cookie;
314 ssize_t cmdlen = 1;
315 static struct option long_options[] =
317 { "help", 0, NULL, 'h' },
318 { "version", 0, NULL, 'v' },
319 { "config", 1, NULL, 'c' },
320 { "check", 0, NULL, 'k' },
321 { NULL, 0, NULL, 0 }
324 /* event loop watchers */
325 ev_io xio = { .fd = -1 };
326 ev_check xcheck;
327 ev_signal sigint;
328 ev_signal sigterm;
329 ev_signal sighup;
331 /* clear the globalconf structure */
332 p_clear(&globalconf, 1);
333 globalconf.keygrabber = LUA_REFNIL;
334 globalconf.mousegrabber = LUA_REFNIL;
336 /* save argv */
337 for(i = 0; i < argc; i++)
338 cmdlen += a_strlen(argv[i]) + 1;
340 globalconf.argv = p_new(char, cmdlen);
341 a_strcpy(globalconf.argv, cmdlen, argv[0]);
343 for(i = 1; i < argc; i++)
345 a_strcat(globalconf.argv, cmdlen, " ");
346 a_strcat(globalconf.argv, cmdlen, argv[i]);
349 /* Text won't be printed correctly otherwise */
350 setlocale(LC_CTYPE, "");
352 /* init lua */
353 luaA_init();
355 /* check args */
356 while((opt = getopt_long(argc, argv, "vhkc:",
357 long_options, NULL)) != -1)
358 switch(opt)
360 case 'v':
361 eprint_version("awesome");
362 break;
363 case 'h':
364 exit_help(EXIT_SUCCESS);
365 break;
366 case 'k':
367 if(!luaA_parserc(confpath, false))
369 fprintf(stderr, "✘ Configuration file syntax error.\n");
370 return EXIT_FAILURE;
372 else
374 fprintf(stderr, "✔ Configuration file syntax OK.\n");
375 return EXIT_SUCCESS;
377 case 'c':
378 if(a_strlen(optarg))
379 confpath = a_strdup(optarg);
380 else
381 fatal("-c option requires a file name");
382 break;
385 globalconf.loop = ev_default_loop(0);
386 ev_timer_init(&globalconf.timer, &luaA_on_timer, 0., 0.);
388 /* register function for signals */
389 ev_signal_init(&sigint, exit_on_signal, SIGINT);
390 ev_signal_init(&sigterm, exit_on_signal, SIGTERM);
391 ev_signal_init(&sighup, restart_on_signal, SIGHUP);
392 ev_signal_start(globalconf.loop, &sigint);
393 ev_signal_start(globalconf.loop, &sigterm);
394 ev_signal_start(globalconf.loop, &sighup);
395 ev_unref(globalconf.loop);
396 ev_unref(globalconf.loop);
397 ev_unref(globalconf.loop);
399 /* X stuff */
400 globalconf.connection = xcb_connect(NULL, &globalconf.default_screen);
401 if(xcb_connection_has_error(globalconf.connection))
402 fatal("cannot open display");
404 /* initiliaze dbus */
405 a_dbus_init();
407 /* Grab server */
408 xcb_grab_server(globalconf.connection);
409 xcb_flush(globalconf.connection);
411 /* Get the file descriptor corresponding to the X connection */
412 xfd = xcb_get_file_descriptor(globalconf.connection);
413 ev_io_init(&xio, &a_xcb_io_cb, xfd, EV_READ);
414 ev_io_start(globalconf.loop, &xio);
415 ev_check_init(&xcheck, &a_xcb_check_cb);
416 ev_check_start(globalconf.loop, &xcheck);
417 ev_unref(globalconf.loop);
419 /* Allocate a handler which will holds all errors and events */
420 xcb_event_handlers_init(globalconf.connection, &globalconf.evenths);
421 xutil_error_handler_catch_all_set(&globalconf.evenths, xerrorstart, NULL);
423 for(screen_nbr = 0;
424 screen_nbr < xcb_setup_roots_length(xcb_get_setup(globalconf.connection));
425 screen_nbr++)
427 const uint32_t select_input_val = XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT;
429 /* This causes an error if some other window manager is running */
430 xcb_change_window_attributes(globalconf.connection,
431 xutil_screen_get(globalconf.connection, screen_nbr)->root,
432 XCB_CW_EVENT_MASK, &select_input_val);
435 /* Need to xcb_flush to validate error handler */
436 xcb_aux_sync(globalconf.connection);
438 /* Process all errors in the queue if any */
439 xcb_event_poll_for_event_loop(&globalconf.evenths);
441 /* Set the default xerror handler */
442 xutil_error_handler_catch_all_set(&globalconf.evenths, xerror, NULL);
444 /* Allocate the key symbols */
445 globalconf.keysyms = xcb_key_symbols_alloc(globalconf.connection);
447 /* Send the request to get the NumLock, ShiftLock and CapsLock
448 masks */
449 xmapping_cookie = xcb_get_modifier_mapping_unchecked(globalconf.connection);
451 /* init atom cache */
452 atoms_init(globalconf.connection);
454 /* init screens information */
455 screen_scan();
457 /* init default font and colors */
458 colors_reqs[0] = xcolor_init_unchecked(&globalconf.colors.fg,
459 "black", sizeof("black") - 1);
461 colors_reqs[1] = xcolor_init_unchecked(&globalconf.colors.bg,
462 "white", sizeof("white") - 1);
464 globalconf.font = draw_font_new("sans 8");
466 for(colors_nbr = 0; colors_nbr < 2; colors_nbr++)
467 xcolor_init_reply(colors_reqs[colors_nbr]);
469 /* Process the reply of previously sent mapping request */
470 xutil_lock_mask_get(globalconf.connection, xmapping_cookie,
471 globalconf.keysyms, &globalconf.numlockmask,
472 &globalconf.shiftlockmask, &globalconf.capslockmask);
474 /* do this only for real screen */
475 for(screen_nbr = 0;
476 screen_nbr < xcb_setup_roots_length(xcb_get_setup(globalconf.connection));
477 screen_nbr++)
479 /* select for events */
480 const uint32_t change_win_vals[] =
482 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT | XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY
483 | XCB_EVENT_MASK_ENTER_WINDOW | XCB_EVENT_MASK_LEAVE_WINDOW
484 | XCB_EVENT_MASK_STRUCTURE_NOTIFY
485 | XCB_EVENT_MASK_PROPERTY_CHANGE,
486 xcursor_new(globalconf.connection, XC_left_ptr)
489 xcb_change_window_attributes(globalconf.connection,
490 xutil_screen_get(globalconf.connection, screen_nbr)->root,
491 XCB_CW_EVENT_MASK | XCB_CW_CURSOR,
492 change_win_vals);
493 ewmh_init(screen_nbr);
494 systray_init(screen_nbr);
497 /* Parse and run configuration file */
498 luaA_parserc(confpath, true);
500 /* scan existing windows */
501 scan();
503 /* process all errors in the queue if any */
504 xcb_event_poll_for_event_loop(&globalconf.evenths);
505 a_xcb_set_event_handlers();
506 a_xcb_set_property_handlers();
508 /* we will receive events, stop grabbing server */
509 xcb_ungrab_server(globalconf.connection);
511 luaA_cs_init();
513 /* refresh everything before waiting events */
514 awesome_refresh(globalconf.connection);
516 /* main event loop */
517 ev_loop(globalconf.loop, 0);
519 /* cleanup event loop */
520 ev_ref(globalconf.loop);
521 ev_check_stop(globalconf.loop, &xcheck);
522 ev_ref(globalconf.loop);
523 ev_io_stop(globalconf.loop, &xio);
525 awesome_atexit();
527 return EXIT_SUCCESS;
530 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80