beautiful: change description
[awesome.git] / awesome.c
blob174ecfb5b62fb7ca725449c6c31cfe4c4890d981
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 "common/version.h"
43 #include "common/atoms.h"
44 #include "config.h"
46 awesome_t globalconf;
48 typedef struct
50 xcb_window_t id;
51 xcb_query_tree_cookie_t tree_cookie;
52 } root_win_t;
54 /** Call before exiting.
56 void
57 awesome_atexit(void)
59 client_t *c;
60 xembed_window_t *em;
61 int screen_nbr;
63 a_dbus_cleanup();
64 luaA_cs_cleanup();
66 /* reparent systray windows, otherwise they may die with their master */
67 for(em = globalconf.embedded; em; em = em->next)
69 xcb_screen_t *s = xutil_screen_get(globalconf.connection, em->phys_screen);
70 xembed_window_unembed(globalconf.connection, em->win, s->root);
73 /* do this only for real screen */
74 for(screen_nbr = 0;
75 screen_nbr < xcb_setup_roots_length(xcb_get_setup(globalconf.connection));
76 screen_nbr++)
77 systray_cleanup(screen_nbr);
79 /* remap all clients since some WM won't handle them otherwise */
80 for(c = globalconf.clients; c; c = c->next)
81 client_unban(c);
83 xcb_flush(globalconf.connection);
85 xcb_disconnect(globalconf.connection);
88 /** Scan X to find windows to manage.
90 static void
91 scan(void)
93 int i, screen, phys_screen, tree_c_len;
94 const int screen_max = xcb_setup_roots_length(xcb_get_setup(globalconf.connection));
95 root_win_t root_wins[screen_max];
96 xcb_query_tree_reply_t *tree_r;
97 xcb_window_t *wins = NULL;
98 xcb_get_window_attributes_reply_t *attr_r;
99 xcb_get_geometry_reply_t *geom_r;
100 long state;
102 for(phys_screen = 0; phys_screen < screen_max; phys_screen++)
104 /* Get the root window ID associated to this screen */
105 root_wins[phys_screen].id = xutil_screen_get(globalconf.connection, phys_screen)->root;
107 /* Get the window tree associated to this screen */
108 root_wins[phys_screen].tree_cookie = xcb_query_tree_unchecked(globalconf.connection,
109 root_wins[phys_screen].id);
112 for(phys_screen = 0; phys_screen < screen_max; phys_screen++)
114 tree_r = xcb_query_tree_reply(globalconf.connection,
115 root_wins[phys_screen].tree_cookie,
116 NULL);
118 if(!tree_r)
119 continue;
121 /* Get the tree of the children windows of the current root window */
122 if(!(wins = xcb_query_tree_children(tree_r)))
123 fatal("E: cannot get tree children");
125 tree_c_len = xcb_query_tree_children_length(tree_r);
126 xcb_get_window_attributes_cookie_t attr_wins[tree_c_len];
127 xcb_get_property_cookie_t state_wins[tree_c_len];
129 for(i = 0; i < tree_c_len; i++)
131 attr_wins[i] = xcb_get_window_attributes_unchecked(globalconf.connection,
132 wins[i]);
134 state_wins[i] = window_state_get_unchecked(wins[i]);
137 xcb_get_geometry_cookie_t *geom_wins[tree_c_len];
139 for(i = 0; i < tree_c_len; i++)
141 bool has_awesome_prop;
143 attr_r = xcb_get_window_attributes_reply(globalconf.connection,
144 attr_wins[i],
145 NULL);
147 state = window_state_get_reply(state_wins[i]);
149 has_awesome_prop = xutil_text_prop_get(globalconf.connection, wins[i],
150 _AWESOME_TAGS, NULL, NULL);
152 if(!attr_r || attr_r->override_redirect
153 || (attr_r->map_state != XCB_MAP_STATE_VIEWABLE && !has_awesome_prop)
154 || (state == XCB_WM_STATE_WITHDRAWN && !has_awesome_prop))
156 geom_wins[i] = NULL;
157 p_delete(&attr_r);
158 continue;
161 p_delete(&attr_r);
163 /* Get the geometry of the current window */
164 geom_wins[i] = p_alloca(xcb_get_geometry_cookie_t, 1);
165 *(geom_wins[i]) = xcb_get_geometry_unchecked(globalconf.connection, wins[i]);
168 for(i = 0; i < tree_c_len; i++)
170 if(!geom_wins[i] || !(geom_r = xcb_get_geometry_reply(globalconf.connection,
171 *(geom_wins[i]), NULL)))
172 continue;
174 screen = screen_getbycoord(phys_screen, geom_r->x, geom_r->y);
176 client_manage(wins[i], geom_r, phys_screen, screen);
178 p_delete(&geom_r);
181 p_delete(&tree_r);
185 static void
186 a_xcb_check_cb(EV_P_ ev_check *w, int revents)
188 xcb_event_poll_for_event_loop(&globalconf.evenths);
189 awesome_refresh(globalconf.connection);
192 static void
193 a_xcb_io_cb(EV_P_ ev_io *w, int revents)
195 /* empty */
198 /** Startup Error handler to check if another window manager
199 * is already running.
200 * \param data Additional optional parameters data.
201 * \param c X connection.
202 * \param error Error event.
204 static int __attribute__ ((noreturn))
205 xerrorstart(void * data __attribute__ ((unused)),
206 xcb_connection_t * c __attribute__ ((unused)),
207 xcb_generic_error_t * error __attribute__ ((unused)))
209 fatal("another window manager is already running");
212 /** Function to exit on some signals.
213 * \param sig the signal received, unused
215 static void
216 exit_on_signal(EV_P_ ev_signal *w, int revents)
218 ev_unloop(EV_A_ 1);
221 void
222 awesome_restart(void)
224 awesome_atexit();
225 a_exec(globalconf.argv);
228 /** Function to restart aweome on some signals.
229 * \param sig the signam received, unused
231 static void
232 restart_on_signal(EV_P_ ev_signal *w, int revents)
234 awesome_restart();
237 /** \brief awesome xerror function.
238 * There's no way to check accesses to destroyed windows, thus those cases are
239 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
240 * default error handler, which may call exit.
241 * \param data Currently unused.
242 * \param c The connectiont to the X server.
243 * \param e The error event.
244 * \return 0 if no error, or xerror's xlib return status.
246 static int
247 xerror(void *data __attribute__ ((unused)),
248 xcb_connection_t *c __attribute__ ((unused)),
249 xcb_generic_error_t *e)
251 xutil_error_t err;
253 if(!xutil_error_init(e, &err))
254 return 0;
256 /* ignore this */
257 if(e->error_code == XUTIL_BAD_WINDOW
258 || (e->error_code == XUTIL_BAD_MATCH && err.request_code == XCB_SET_INPUT_FOCUS)
259 || (e->error_code == XUTIL_BAD_VALUE && err.request_code == XCB_KILL_CLIENT)
260 || (err.request_code == XCB_CONFIGURE_WINDOW && e->error_code == XUTIL_BAD_MATCH))
261 goto bailout;
263 warn("X error: request=%s, error=%s", err.request_label, err.error_label);
266 * Xlib code was using default X error handler, namely
267 * '_XDefaultError()', which displays more informations about the
268 * error and also exit if 'error_code'' equals to
269 * 'BadImplementation'
271 * \todo display more informations about the error (like the Xlib default error handler)
273 if(e->error_code == XUTIL_BAD_IMPLEMENTATION)
274 fatal("X error: request=%s, error=%s", err.request_label, err.error_label);
276 bailout:
277 xutil_error_wipe(&err);
278 return 0;
281 /** Print help and exit(2) with given exit_code.
282 * \param exit_code The exit code.
284 static void __attribute__ ((noreturn))
285 exit_help(int exit_code)
287 FILE *outfile = (exit_code == EXIT_SUCCESS) ? stdout : stderr;
288 fprintf(outfile,
289 "Usage: awesome [OPTION]\n\
290 -h, --help show help\n\
291 -v, --version show version\n\
292 -c, --config FILE configuration file to use\n\
293 -k, --check check configuration file syntax\n");
294 exit(exit_code);
297 /** Hello, this is main.
298 * \param argc Who knows.
299 * \param argv Who knows.
300 * \return EXIT_SUCCESS I hope.
303 main(int argc, char **argv)
305 const char *confpath = NULL;
306 int xfd, i, screen_nbr, opt, colors_nbr;
307 xcolor_init_request_t colors_reqs[2];
308 xcb_get_modifier_mapping_cookie_t xmapping_cookie;
309 ssize_t cmdlen = 1;
310 static struct option long_options[] =
312 { "help", 0, NULL, 'h' },
313 { "version", 0, NULL, 'v' },
314 { "config", 1, NULL, 'c' },
315 { "check", 0, NULL, 'k' },
316 { NULL, 0, NULL, 0 }
319 /* event loop watchers */
320 ev_io xio = { .fd = -1 };
321 ev_check xcheck;
322 ev_signal sigint;
323 ev_signal sigterm;
324 ev_signal sighup;
326 /* clear the globalconf structure */
327 p_clear(&globalconf, 1);
328 globalconf.keygrabber = LUA_REFNIL;
330 /* save argv */
331 for(i = 0; i < argc; i++)
332 cmdlen += a_strlen(argv[i]) + 1;
334 globalconf.argv = p_new(char, cmdlen);
335 a_strcpy(globalconf.argv, cmdlen, argv[0]);
337 for(i = 1; i < argc; i++)
339 a_strcat(globalconf.argv, cmdlen, " ");
340 a_strcat(globalconf.argv, cmdlen, argv[i]);
343 /* Text won't be printed correctly otherwise */
344 setlocale(LC_CTYPE, "");
346 /* init lua */
347 luaA_init();
349 /* check args */
350 while((opt = getopt_long(argc, argv, "vhkc:",
351 long_options, NULL)) != -1)
352 switch(opt)
354 case 'v':
355 eprint_version("awesome");
356 break;
357 case 'h':
358 exit_help(EXIT_SUCCESS);
359 break;
360 case 'k':
361 if(!luaA_parserc(confpath, false))
363 fprintf(stderr, "✘ Configuration file error.\n");
364 return EXIT_FAILURE;
366 else
368 fprintf(stderr, "✔ Configuration file OK.\n");
369 return EXIT_SUCCESS;
371 case 'c':
372 if(a_strlen(optarg))
373 confpath = a_strdup(optarg);
374 else
375 fatal("-c option requires a file name");
376 break;
379 globalconf.loop = ev_default_loop(0);
380 ev_timer_init(&globalconf.timer, &luaA_on_timer, 0., 0.);
382 /* register function for signals */
383 ev_signal_init(&sigint, exit_on_signal, SIGINT);
384 ev_signal_init(&sigterm, exit_on_signal, SIGTERM);
385 ev_signal_init(&sighup, restart_on_signal, SIGHUP);
386 ev_signal_start(globalconf.loop, &sigint);
387 ev_signal_start(globalconf.loop, &sigterm);
388 ev_signal_start(globalconf.loop, &sighup);
389 ev_unref(globalconf.loop);
390 ev_unref(globalconf.loop);
391 ev_unref(globalconf.loop);
393 /* X stuff */
394 globalconf.connection = xcb_connect(NULL, &globalconf.default_screen);
395 if(xcb_connection_has_error(globalconf.connection))
396 fatal("cannot open display");
398 /* Grab server */
399 xcb_grab_server(globalconf.connection);
400 xcb_flush(globalconf.connection);
402 /* Get the file descriptor corresponding to the X connection */
403 xfd = xcb_get_file_descriptor(globalconf.connection);
404 ev_io_init(&xio, &a_xcb_io_cb, xfd, EV_READ);
405 ev_io_start(globalconf.loop, &xio);
406 ev_check_init(&xcheck, &a_xcb_check_cb);
407 ev_check_start(globalconf.loop, &xcheck);
408 ev_unref(globalconf.loop);
410 /* Allocate a handler which will holds all errors and events */
411 xcb_event_handlers_init(globalconf.connection, &globalconf.evenths);
412 xutil_error_handler_catch_all_set(&globalconf.evenths, xerrorstart, NULL);
414 for(screen_nbr = 0;
415 screen_nbr < xcb_setup_roots_length(xcb_get_setup(globalconf.connection));
416 screen_nbr++)
418 const uint32_t select_input_val = XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT;
420 /* This causes an error if some other window manager is running */
421 xcb_change_window_attributes(globalconf.connection,
422 xutil_screen_get(globalconf.connection, screen_nbr)->root,
423 XCB_CW_EVENT_MASK, &select_input_val);
426 /* Need to xcb_flush to validate error handler */
427 xcb_aux_sync(globalconf.connection);
429 /* Process all errors in the queue if any */
430 xcb_event_poll_for_event_loop(&globalconf.evenths);
432 /* Set the default xerror handler */
433 xutil_error_handler_catch_all_set(&globalconf.evenths, xerror, NULL);
435 /* Allocate the key symbols */
436 globalconf.keysyms = xcb_key_symbols_alloc(globalconf.connection);
438 /* Send the request to get the NumLock, ShiftLock and CapsLock
439 masks */
440 xmapping_cookie = xcb_get_modifier_mapping_unchecked(globalconf.connection);
442 /* init atom cache */
443 atoms_init(globalconf.connection);
445 /* init screens information */
446 screen_scan();
448 /* init default font and colors */
449 colors_reqs[0] = xcolor_init_unchecked(&globalconf.colors.fg,
450 "black", sizeof("black") - 1);
452 colors_reqs[1] = xcolor_init_unchecked(&globalconf.colors.bg,
453 "white", sizeof("white") - 1);
455 globalconf.font = draw_font_new("sans 8");
457 /* init cursors */
458 globalconf.cursor[CurNormal] = xutil_cursor_new(globalconf.connection, XUTIL_CURSOR_LEFT_PTR);
459 globalconf.cursor[CurResize] = xutil_cursor_new(globalconf.connection, XUTIL_CURSOR_SIZING);
460 globalconf.cursor[CurResizeH] = xutil_cursor_new(globalconf.connection, XUTIL_CURSOR_DOUBLE_ARROW_HORIZ);
461 globalconf.cursor[CurResizeV] = xutil_cursor_new(globalconf.connection, XUTIL_CURSOR_DOUBLE_ARROW_VERT);
462 globalconf.cursor[CurMove] = xutil_cursor_new(globalconf.connection, XUTIL_CURSOR_FLEUR);
463 globalconf.cursor[CurTopRight] = xutil_cursor_new(globalconf.connection, XUTIL_CURSOR_TOP_RIGHT_CORNER);
464 globalconf.cursor[CurTopLeft] = xutil_cursor_new(globalconf.connection, XUTIL_CURSOR_TOP_LEFT_CORNER);
465 globalconf.cursor[CurBotRight] = xutil_cursor_new(globalconf.connection, XUTIL_CURSOR_BOTTOM_RIGHT_CORNER);
466 globalconf.cursor[CurBotLeft] = xutil_cursor_new(globalconf.connection, XUTIL_CURSOR_BOTTOM_LEFT_CORNER);
468 for(colors_nbr = 0; colors_nbr < 2; colors_nbr++)
469 xcolor_init_reply(colors_reqs[colors_nbr]);
471 /* Process the reply of previously sent mapping request */
472 xutil_lock_mask_get(globalconf.connection, xmapping_cookie,
473 globalconf.keysyms, &globalconf.numlockmask,
474 &globalconf.shiftlockmask, &globalconf.capslockmask);
476 /* do this only for real screen */
477 for(screen_nbr = 0;
478 screen_nbr < xcb_setup_roots_length(xcb_get_setup(globalconf.connection));
479 screen_nbr++)
481 /* select for events */
482 const uint32_t change_win_vals[] =
484 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT | XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY
485 | XCB_EVENT_MASK_ENTER_WINDOW | XCB_EVENT_MASK_LEAVE_WINDOW
486 | XCB_EVENT_MASK_STRUCTURE_NOTIFY
487 | XCB_EVENT_MASK_PROPERTY_CHANGE,
488 globalconf.cursor[CurNormal]
491 xcb_change_window_attributes(globalconf.connection,
492 xutil_screen_get(globalconf.connection, screen_nbr)->root,
493 XCB_CW_EVENT_MASK | XCB_CW_CURSOR,
494 change_win_vals);
495 ewmh_init(screen_nbr);
496 systray_init(screen_nbr);
499 /* Parse and run configuration file */
500 luaA_parserc(confpath, true);
502 /* scan existing windows */
503 scan();
505 /* process all errors in the queue if any */
506 xcb_event_poll_for_event_loop(&globalconf.evenths);
507 a_xcb_set_event_handlers();
508 a_xcb_set_property_handlers();
510 /* Grab server */
511 xcb_ungrab_server(globalconf.connection);
513 xcb_aux_sync(globalconf.connection);
515 luaA_cs_init();
516 a_dbus_init();
518 /* refresh everything before waiting events */
519 awesome_refresh(globalconf.connection);
521 /* main event loop */
522 ev_loop(globalconf.loop, 0);
524 /* cleanup event loop */
525 ev_ref(globalconf.loop);
526 ev_check_stop(globalconf.loop, &xcheck);
527 ev_ref(globalconf.loop);
528 ev_io_stop(globalconf.loop, &xio);
530 awesome_atexit();
532 return EXIT_SUCCESS;
535 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80