change codename
[awesome.git] / awesome.c
blobd8ca0993c8343982905cddf8b854cfc200842761
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 #define _GNU_SOURCE
23 #include <getopt.h>
25 #include <locale.h>
26 #include <stdio.h>
27 #include <unistd.h>
28 #include <signal.h>
30 #include <ev.h>
32 #include "client.h"
33 #include "titlebar.h"
34 #include "event.h"
35 #include "window.h"
36 #include "ewmh.h"
37 #include "dbus.h"
38 #include "statusbar.h"
39 #include "systray.h"
40 #include "common/version.h"
41 #include "common/atoms.h"
42 #include "config.h"
44 awesome_t globalconf;
46 typedef struct
48 xcb_window_t id;
49 xcb_query_tree_cookie_t tree_cookie;
50 } root_win_t;
52 /** Scan X to find windows to manage.
54 static void
55 scan(void)
57 int i, screen, real_screen, tree_c_len;
58 const int screen_max = xcb_setup_roots_length(xcb_get_setup(globalconf.connection));
59 root_win_t *root_wins = p_new(root_win_t, screen_max);
60 xcb_query_tree_reply_t *tree_r;
61 xcb_window_t *wins = NULL;
62 xcb_get_window_attributes_cookie_t *attr_wins = NULL;
63 xcb_get_property_cookie_t *state_wins = NULL;
64 xcb_get_geometry_cookie_t **geom_wins = NULL;
65 xcb_get_window_attributes_reply_t *attr_r;
66 xcb_get_geometry_reply_t *geom_r;
67 long state;
69 for(screen = 0; screen < screen_max; screen++)
71 /* Get the root window ID associated to this screen */
72 root_wins[screen].id = xutil_screen_get(globalconf.connection, screen)->root;
74 /* Get the window tree associated to this screen */
75 root_wins[screen].tree_cookie = xcb_query_tree_unchecked(globalconf.connection,
76 root_wins[screen].id);
79 for(screen = 0; screen < screen_max; screen++)
81 tree_r = xcb_query_tree_reply(globalconf.connection,
82 root_wins[screen].tree_cookie,
83 NULL);
85 if(!tree_r)
86 continue;
88 /* Get the tree of the children windows of the current root window */
89 if(!(wins = xcb_query_tree_children(tree_r)))
90 fatal("E: cannot get tree children");
91 tree_c_len = xcb_query_tree_children_length(tree_r);
92 attr_wins = p_new(xcb_get_window_attributes_cookie_t, tree_c_len);
93 state_wins = p_new(xcb_get_property_cookie_t, tree_c_len);
95 for(i = 0; i < tree_c_len; i++)
97 attr_wins[i] = xcb_get_window_attributes_unchecked(globalconf.connection,
98 wins[i]);
100 state_wins[i] = window_state_get_unchecked(wins[i]);
103 geom_wins = p_new(xcb_get_geometry_cookie_t *, tree_c_len);
105 for(i = 0; i < tree_c_len; i++)
107 bool has_awesome_prop;
109 attr_r = xcb_get_window_attributes_reply(globalconf.connection,
110 attr_wins[i],
111 NULL);
113 state = window_state_get_reply(state_wins[i]);
115 has_awesome_prop = xutil_text_prop_get(globalconf.connection, wins[i],
116 _AWESOME_PROPERTIES, NULL, NULL);
118 if(!attr_r || attr_r->override_redirect
119 || (attr_r->map_state != XCB_MAP_STATE_VIEWABLE && !has_awesome_prop)
120 || (state == XCB_WM_WITHDRAWN_STATE && !has_awesome_prop))
122 p_delete(&attr_r);
123 continue;
126 p_delete(&attr_r);
128 /* Get the geometry of the current window */
129 geom_wins[i] = p_new(xcb_get_geometry_cookie_t, 1);
130 *(geom_wins[i]) = xcb_get_geometry_unchecked(globalconf.connection, wins[i]);
133 p_delete(&state_wins);
134 p_delete(&attr_wins);
136 for(i = 0; i < tree_c_len; i++)
138 if(!geom_wins[i])
139 continue;
141 if(!(geom_r = xcb_get_geometry_reply(globalconf.connection,
142 *(geom_wins[i]), NULL)))
143 continue;
145 real_screen = screen_get_bycoord(globalconf.screens_info, screen,
146 geom_r->x, geom_r->y);
148 client_manage(wins[i], geom_r, real_screen);
150 p_delete(&geom_r);
151 p_delete(&geom_wins[i]);
154 p_delete(&geom_wins);
155 p_delete(&tree_r);
157 p_delete(&root_wins);
160 static void
161 a_xcb_check_cb(EV_P_ ev_check *w, int revents)
163 xcb_generic_event_t *ev;
165 while((ev = xcb_poll_for_event(globalconf.connection)))
169 xcb_handle_event(globalconf.evenths, ev);
170 p_delete(&ev);
172 while((ev = xcb_poll_for_event(globalconf.connection)));
174 layout_refresh();
175 statusbar_refresh();
176 titlebar_refresh();
178 xcb_aux_sync(globalconf.connection);
182 static void
183 a_xcb_io_cb(EV_P_ ev_io *w, int revents)
185 /* empty */
188 /** Startup Error handler to check if another window manager
189 * is already running.
190 * \param data Additional optional parameters data.
191 * \param c X connection.
192 * \param error Error event.
194 static int __attribute__ ((noreturn))
195 xerrorstart(void * data __attribute__ ((unused)),
196 xcb_connection_t * c __attribute__ ((unused)),
197 xcb_generic_error_t * error __attribute__ ((unused)))
199 fatal("another window manager is already running");
202 /** Function to exit on some signals.
203 * \param sig the signal received, unused
205 static void
206 exit_on_signal(EV_P_ ev_signal *w, int revents)
208 ev_unloop(EV_A_ 1);
211 /** Function to restart aweome on some signals.
212 * \param sig the signam received, unused
214 static void
215 restart_on_signal(EV_P_ ev_signal *w, int revents)
217 ewmh_restart();
220 /** \brief awesome xerror function.
221 * There's no way to check accesses to destroyed windows, thus those cases are
222 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
223 * default error handler, which may call exit.
224 * \param data Currently unused.
225 * \param c The connectiont to the X server.
226 * \param e The error event.
227 * \return 0 if no error, or xerror's xlib return status.
229 static int
230 xerror(void *data __attribute__ ((unused)),
231 xcb_connection_t *c __attribute__ ((unused)),
232 xcb_generic_error_t *e)
234 xutil_error_t *err = xutil_error_get(e);
235 if(!err)
236 return 0;
238 if(e->error_code == XUTIL_BAD_WINDOW
239 || (e->error_code == XUTIL_BAD_MATCH && err->request_code == XCB_SET_INPUT_FOCUS)
240 || (e->error_code == XUTIL_BAD_VALUE && err->request_code == XCB_KILL_CLIENT)
241 || (err->request_code == XCB_CONFIGURE_WINDOW && e->error_code == XUTIL_BAD_MATCH))
243 xutil_error_delete(err);
244 return 0;
247 warn("fatal error: request=%s, error=%s", err->request_label, err->error_label);
248 xutil_error_delete(err);
251 * Xlib code was using default X error handler, namely
252 * '_XDefaultError()', which displays more informations about the
253 * error and also exit if 'error_code'' equals to
254 * 'BadImplementation'
256 * \todo display more informations about the error (like the Xlib default error handler)
258 if(e->error_code == XUTIL_BAD_IMPLEMENTATION)
259 exit(EXIT_FAILURE);
261 return 0;
264 /** Print help and exit(2) with given exit_code.
265 * \param exit_code The exit code.
267 static void __attribute__ ((noreturn))
268 exit_help(int exit_code)
270 FILE *outfile = (exit_code == EXIT_SUCCESS) ? stdout : stderr;
271 fprintf(outfile,
272 "Usage: awesome [OPTION]\n\
273 -h, --help show help\n\
274 -v, --version show version\n\
275 -c, --config FILE configuration file to use\n");
276 exit(exit_code);
279 /** Hello, this is main.
280 * \param argc Who knows.
281 * \param argv Who knows.
282 * \return EXIT_SUCCESS I hope.
285 main(int argc, char **argv)
287 const char *confpath = NULL;
288 int xfd, i, screen_nbr, opt, colors_nbr;
289 xcolor_init_request_t colors_reqs[2];
290 xcb_get_modifier_mapping_cookie_t xmapping_cookie;
291 ssize_t cmdlen = 1;
292 client_t *c;
293 static struct option long_options[] =
295 {"help", 0, NULL, 'h'},
296 {"version", 0, NULL, 'v'},
297 {"config", 1, NULL, 'c'},
298 {NULL, 0, NULL, 0}
301 /* event loop watchers */
302 ev_io xio = { .fd = -1 };
303 ev_check xcheck;
304 ev_signal sigint;
305 ev_signal sigterm;
306 ev_signal sighup;
308 /* clear the globalconf structure */
309 p_clear(&globalconf, 1);
310 globalconf.keygrabber = LUA_REFNIL;
312 /* save argv */
313 for(i = 0; i < argc; i++)
314 cmdlen += a_strlen(argv[i]) + 1;
316 globalconf.argv = p_new(char, cmdlen);
317 a_strcpy(globalconf.argv, cmdlen, argv[0]);
319 for(i = 1; i < argc; i++)
321 a_strcat(globalconf.argv, cmdlen, " ");
322 a_strcat(globalconf.argv, cmdlen, argv[i]);
325 /* check args */
326 while((opt = getopt_long(argc, argv, "vhc:",
327 long_options, NULL)) != -1)
328 switch(opt)
330 case 'v':
331 eprint_version("awesome");
332 break;
333 case 'h':
334 exit_help(EXIT_SUCCESS);
335 break;
336 case 'c':
337 if(a_strlen(optarg))
338 confpath = a_strdup(optarg);
339 else
340 fatal("-c option requires a file name");
341 break;
344 /* Text won't be printed correctly otherwise */
345 setlocale(LC_CTYPE, "");
346 globalconf.loop = ev_default_loop(0);
347 ev_timer_init(&globalconf.timer, &luaA_on_timer, 0., 0.);
349 /* register function for signals */
350 ev_signal_init(&sigint, exit_on_signal, SIGINT);
351 ev_signal_init(&sigterm, exit_on_signal, SIGTERM);
352 ev_signal_init(&sighup, restart_on_signal, SIGHUP);
353 ev_signal_start(globalconf.loop, &sigint);
354 ev_signal_start(globalconf.loop, &sigterm);
355 ev_signal_start(globalconf.loop, &sighup);
356 ev_unref(globalconf.loop);
357 ev_unref(globalconf.loop);
358 ev_unref(globalconf.loop);
360 /* X stuff */
361 globalconf.connection = xcb_connect(NULL, &globalconf.default_screen);
362 if(xcb_connection_has_error(globalconf.connection))
363 fatal("cannot open display");
365 /* Get the file descriptor corresponding to the X connection */
366 xfd = xcb_get_file_descriptor(globalconf.connection);
367 ev_io_init(&xio, &a_xcb_io_cb, xfd, EV_READ);
368 ev_io_start(globalconf.loop, &xio);
369 ev_check_init(&xcheck, &a_xcb_check_cb);
370 ev_check_start(globalconf.loop, &xcheck);
371 ev_unref(globalconf.loop);
373 /* Allocate a handler which will holds all errors and events */
374 globalconf.evenths = xcb_alloc_event_handlers(globalconf.connection);
375 xutil_error_handler_catch_all_set(globalconf.evenths, xerrorstart, NULL);
377 for(screen_nbr = 0;
378 screen_nbr < xcb_setup_roots_length(xcb_get_setup(globalconf.connection));
379 screen_nbr++)
381 const uint32_t select_input_val = XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT;
383 /* This causes an error if some other window manager is running */
384 xcb_change_window_attributes(globalconf.connection,
385 xutil_screen_get(globalconf.connection, screen_nbr)->root,
386 XCB_CW_EVENT_MASK, &select_input_val);
389 /* Need to xcb_flush to validate error handler */
390 xcb_aux_sync(globalconf.connection);
392 /* Process all errors in the queue if any */
393 xcb_poll_for_event_loop(globalconf.evenths);
395 /* Set the default xerror handler */
396 xutil_error_handler_catch_all_set(globalconf.evenths, xerror, NULL);
398 /* Allocate the key symbols */
399 globalconf.keysyms = xcb_key_symbols_alloc(globalconf.connection);
401 /* Send the request to get the NumLock, ShiftLock and CapsLock
402 masks */
403 xmapping_cookie = xcb_get_modifier_mapping_unchecked(globalconf.connection);
405 /* init atom cache */
406 atoms_init(globalconf.connection);
408 /* init screens struct */
409 globalconf.screens_info = screensinfo_new(globalconf.connection);
410 globalconf.screen_focus = globalconf.screens = p_new(screen_t, globalconf.screens_info->nscreen);
411 /* \todo stop duplicating this */
412 for(screen_nbr = 0; screen_nbr < globalconf.screens_info->nscreen; screen_nbr++)
414 globalconf.screens[screen_nbr].index = screen_nbr;
415 globalconf.screens[screen_nbr].geometry = globalconf.screens_info->geometry[screen_nbr];
418 /* init default font and colors */
419 colors_reqs[0] = xcolor_init_unchecked(globalconf.connection, &globalconf.colors.fg,
420 globalconf.default_screen, "black",
421 sizeof("black")-1);
423 colors_reqs[1] = xcolor_init_unchecked(globalconf.connection, &globalconf.colors.bg,
424 globalconf.default_screen, "white",
425 sizeof("white")-1);
427 globalconf.font = draw_font_new(globalconf.connection, globalconf.default_screen, "sans 8");
429 /* init cursors */
430 globalconf.cursor[CurNormal] = xutil_cursor_new(globalconf.connection, XUTIL_CURSOR_LEFT_PTR);
431 globalconf.cursor[CurResize] = xutil_cursor_new(globalconf.connection, XUTIL_CURSOR_SIZING);
432 globalconf.cursor[CurResizeH] = xutil_cursor_new(globalconf.connection, XUTIL_CURSOR_DOUBLE_ARROW_HORIZ);
433 globalconf.cursor[CurResizeV] = xutil_cursor_new(globalconf.connection, XUTIL_CURSOR_DOUBLE_ARROW_VERT);
434 globalconf.cursor[CurMove] = xutil_cursor_new(globalconf.connection, XUTIL_CURSOR_FLEUR);
435 globalconf.cursor[CurTopRight] = xutil_cursor_new(globalconf.connection, XUTIL_CURSOR_TOP_RIGHT_CORNER);
436 globalconf.cursor[CurTopLeft] = xutil_cursor_new(globalconf.connection, XUTIL_CURSOR_TOP_LEFT_CORNER);
437 globalconf.cursor[CurBotRight] = xutil_cursor_new(globalconf.connection, XUTIL_CURSOR_BOTTOM_RIGHT_CORNER);
438 globalconf.cursor[CurBotLeft] = xutil_cursor_new(globalconf.connection, XUTIL_CURSOR_BOTTOM_LEFT_CORNER);
440 for(colors_nbr = 0; colors_nbr < 2; colors_nbr++)
441 xcolor_init_reply(globalconf.connection, colors_reqs[colors_nbr]);
443 /* Process the reply of previously sent mapping request */
444 xutil_lock_mask_get(globalconf.connection, xmapping_cookie,
445 globalconf.keysyms, &globalconf.numlockmask,
446 &globalconf.shiftlockmask, &globalconf.capslockmask);
448 /* init lua */
449 luaA_init();
451 luaA_parserc(confpath);
453 /* scan existing windows */
454 scan();
456 /* process all errors in the queue if any */
457 xcb_poll_for_event_loop(globalconf.evenths);
458 a_xcb_set_event_handlers();
460 /* do this only for real screen */
461 for(screen_nbr = 0;
462 screen_nbr < xcb_setup_roots_length(xcb_get_setup(globalconf.connection));
463 screen_nbr++)
465 /* select for events */
466 const uint32_t change_win_vals[] =
468 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT | XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY
469 | XCB_EVENT_MASK_ENTER_WINDOW | XCB_EVENT_MASK_LEAVE_WINDOW
470 | XCB_EVENT_MASK_STRUCTURE_NOTIFY,
471 globalconf.cursor[CurNormal]
474 xcb_change_window_attributes(globalconf.connection,
475 xutil_screen_get(globalconf.connection, screen_nbr)->root,
476 XCB_CW_EVENT_MASK | XCB_CW_CURSOR,
477 change_win_vals);
478 ewmh_init(screen_nbr);
479 systray_init(screen_nbr);
482 xcb_aux_sync(globalconf.connection);
484 luaA_cs_init();
485 a_dbus_init();
487 /* refresh everything before waiting events */
488 layout_refresh();
489 statusbar_refresh();
490 titlebar_refresh();
492 /* main event loop */
493 ev_loop(globalconf.loop, 0);
495 /* cleanup event loop */
496 ev_ref(globalconf.loop);
497 ev_check_stop(globalconf.loop, &xcheck);
498 ev_ref(globalconf.loop);
499 ev_io_stop(globalconf.loop, &xio);
500 a_dbus_cleanup();
501 luaA_cs_cleanup();
503 /* remap all clients since some WM won't handle them otherwise */
504 for(c = globalconf.clients; c; c = c->next)
505 client_unban(c);
507 xcb_aux_sync(globalconf.connection);
509 xcb_disconnect(globalconf.connection);
511 return EXIT_SUCCESS;
514 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80