Remove useless loop in hide()
[awesome.git] / awesome.c
blob5917a00670a896f4f4d5900de7055054012e234b
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 "spawn.h"
36 #include "client.h"
37 #include "window.h"
38 #include "ewmh.h"
39 #include "dbus.h"
40 #include "systray.h"
41 #include "event.h"
42 #include "property.h"
43 #include "screen.h"
44 #include "titlebar.h"
45 #include "common/version.h"
46 #include "common/atoms.h"
47 #include "common/xcursor.h"
48 #include "common/xutil.h"
50 awesome_t globalconf;
52 typedef struct
54 xcb_window_t id;
55 xcb_query_tree_cookie_t tree_cookie;
56 } root_win_t;
58 /** Call before exiting.
60 void
61 awesome_atexit(void)
63 int screen_nbr, nscreens;
65 a_dbus_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 const xcb_setup_t *setup = xcb_get_setup(globalconf.connection);
77 nscreens = setup ? xcb_setup_roots_length(setup) : -1;
78 for(screen_nbr = 0;
79 screen_nbr < nscreens;
80 screen_nbr++)
81 systray_cleanup(screen_nbr);
83 /* remap all clients since some WM won't handle them otherwise */
84 foreach(c, globalconf.clients)
86 client_unban(*c);
87 titlebar_client_detach(*c);
90 xcb_flush(globalconf.connection);
92 xcb_disconnect(globalconf.connection);
95 /** Scan X to find windows to manage.
97 static void
98 scan(void)
100 int i, phys_screen, tree_c_len;
101 const int screen_max = xcb_setup_roots_length(xcb_get_setup(globalconf.connection));
102 root_win_t root_wins[screen_max];
103 xcb_query_tree_reply_t *tree_r;
104 xcb_window_t *wins = NULL;
105 xcb_get_window_attributes_reply_t *attr_r;
106 xcb_get_geometry_reply_t *geom_r;
107 long state;
109 for(phys_screen = 0; phys_screen < screen_max; phys_screen++)
111 /* Get the root window ID associated to this screen */
112 root_wins[phys_screen].id = xutil_screen_get(globalconf.connection, phys_screen)->root;
114 /* Get the window tree associated to this screen */
115 root_wins[phys_screen].tree_cookie = xcb_query_tree_unchecked(globalconf.connection,
116 root_wins[phys_screen].id);
119 for(phys_screen = 0; phys_screen < screen_max; phys_screen++)
121 tree_r = xcb_query_tree_reply(globalconf.connection,
122 root_wins[phys_screen].tree_cookie,
123 NULL);
125 if(!tree_r)
126 continue;
128 /* Get the tree of the children windows of the current root window */
129 if(!(wins = xcb_query_tree_children(tree_r)))
130 fatal("E: cannot get tree children");
132 tree_c_len = xcb_query_tree_children_length(tree_r);
133 xcb_get_window_attributes_cookie_t attr_wins[tree_c_len];
134 xcb_get_property_cookie_t state_wins[tree_c_len];
136 for(i = 0; i < tree_c_len; i++)
138 attr_wins[i] = xcb_get_window_attributes_unchecked(globalconf.connection,
139 wins[i]);
141 state_wins[i] = window_state_get_unchecked(wins[i]);
144 xcb_get_geometry_cookie_t *geom_wins[tree_c_len];
146 for(i = 0; i < tree_c_len; i++)
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 if(!attr_r || attr_r->override_redirect
155 || attr_r->map_state != XCB_MAP_STATE_VIEWABLE
156 || state == XCB_WM_STATE_WITHDRAWN)
158 geom_wins[i] = NULL;
159 p_delete(&attr_r);
160 continue;
163 p_delete(&attr_r);
165 /* Get the geometry of the current window */
166 geom_wins[i] = p_alloca(xcb_get_geometry_cookie_t, 1);
167 *(geom_wins[i]) = xcb_get_geometry_unchecked(globalconf.connection, wins[i]);
170 for(i = 0; i < tree_c_len; i++)
172 if(!geom_wins[i] || !(geom_r = xcb_get_geometry_reply(globalconf.connection,
173 *(geom_wins[i]), NULL)))
174 continue;
176 client_manage(wins[i], geom_r, phys_screen, true);
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_generic_event_t *mouse = NULL, *event;
190 while((event = xcb_poll_for_event(globalconf.connection)))
192 /* We will treat mouse events later.
193 * We cannot afford to treat all mouse motion events,
194 * because that would be too much CPU intensive, so we just
195 * take the last we get after a bunch of events. */
196 if(XCB_EVENT_RESPONSE_TYPE(event) == XCB_MOTION_NOTIFY)
198 p_delete(&mouse);
199 mouse = event;
201 else
203 xcb_event_handle(&globalconf.evenths, event);
204 p_delete(&event);
208 if(mouse)
210 xcb_event_handle(&globalconf.evenths, mouse);
211 p_delete(&mouse);
214 awesome_refresh();
217 static void
218 a_xcb_io_cb(EV_P_ ev_io *w, int revents)
220 /* empty */
223 /** Startup Error handler to check if another window manager
224 * is already running.
225 * \param data Additional optional parameters data.
226 * \param c X connection.
227 * \param error Error event.
229 static int __attribute__ ((noreturn))
230 xerrorstart(void * data __attribute__ ((unused)),
231 xcb_connection_t * c __attribute__ ((unused)),
232 xcb_generic_error_t * error __attribute__ ((unused)))
234 fatal("another window manager is already running");
237 /** Function to exit on some signals.
238 * \param sig the signal received, unused
240 static void
241 exit_on_signal(EV_P_ ev_signal *w, int revents)
243 ev_unloop(EV_A_ 1);
246 void
247 awesome_restart(void)
249 awesome_atexit();
250 a_exec(globalconf.argv);
253 /** Function to restart aweome on some signals.
254 * \param sig the signam received, unused
256 static void
257 restart_on_signal(EV_P_ ev_signal *w, int revents)
259 awesome_restart();
262 /** \brief awesome xerror function.
263 * There's no way to check accesses to destroyed windows, thus those cases are
264 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
265 * default error handler, which may call exit.
266 * \param data Currently unused.
267 * \param c The connectiont to the X server.
268 * \param e The error event.
269 * \return 0 if no error, or xerror's xlib return status.
271 static int
272 xerror(void *data __attribute__ ((unused)),
273 xcb_connection_t *c __attribute__ ((unused)),
274 xcb_generic_error_t *e)
276 /* ignore this */
277 if(e->error_code == XCB_EVENT_ERROR_BAD_WINDOW
278 || (e->error_code == XCB_EVENT_ERROR_BAD_MATCH
279 && XCB_EVENT_REQUEST_TYPE(e) == XCB_SET_INPUT_FOCUS)
280 || (e->error_code == XCB_EVENT_ERROR_BAD_VALUE
281 && XCB_EVENT_REQUEST_TYPE(e) == XCB_KILL_CLIENT)
282 || (XCB_EVENT_REQUEST_TYPE(e) == XCB_CONFIGURE_WINDOW
283 && e->error_code == XCB_EVENT_ERROR_BAD_MATCH))
284 return 0;
286 warn("X error: request=%s, error=%s",
287 xcb_event_get_request_label(XCB_EVENT_REQUEST_TYPE(e)),
288 xcb_event_get_error_label(e->error_code));
290 return 0;
293 /** Print help and exit(2) with given exit_code.
294 * \param exit_code The exit code.
296 static void __attribute__ ((noreturn))
297 exit_help(int exit_code)
299 FILE *outfile = (exit_code == EXIT_SUCCESS) ? stdout : stderr;
300 fprintf(outfile,
301 "Usage: awesome [OPTION]\n\
302 -h, --help show help\n\
303 -v, --version show version\n\
304 -c, --config FILE configuration file to use\n\
305 -k, --check check configuration file syntax\n");
306 exit(exit_code);
309 /** Hello, this is main.
310 * \param argc Who knows.
311 * \param argv Who knows.
312 * \return EXIT_SUCCESS I hope.
315 main(int argc, char **argv)
317 const char *confpath = NULL;
318 int xfd, i, screen_nbr, opt, colors_nbr;
319 xcolor_init_request_t colors_reqs[2];
320 ssize_t cmdlen = 1;
321 xdgHandle xdg;
322 static struct option long_options[] =
324 { "help", 0, NULL, 'h' },
325 { "version", 0, NULL, 'v' },
326 { "config", 1, NULL, 'c' },
327 { "check", 0, NULL, 'k' },
328 { NULL, 0, NULL, 0 }
331 /* event loop watchers */
332 ev_io xio = { .fd = -1 };
333 ev_check xcheck;
334 ev_signal sigint;
335 ev_signal sigterm;
336 ev_signal sighup;
338 /* clear the globalconf structure */
339 p_clear(&globalconf, 1);
340 globalconf.keygrabber = LUA_REFNIL;
341 globalconf.mousegrabber = LUA_REFNIL;
343 /* save argv */
344 for(i = 0; i < argc; i++)
345 cmdlen += a_strlen(argv[i]) + 1;
347 globalconf.argv = p_new(char, cmdlen);
348 a_strcpy(globalconf.argv, cmdlen, argv[0]);
350 for(i = 1; i < argc; i++)
352 a_strcat(globalconf.argv, cmdlen, " ");
353 a_strcat(globalconf.argv, cmdlen, argv[i]);
356 /* Text won't be printed correctly otherwise */
357 setlocale(LC_CTYPE, "");
359 /* Get XDG basedir data */
360 xdgInitHandle(&xdg);
362 /* init lua */
363 luaA_init(&xdg);
365 /* check args */
366 while((opt = getopt_long(argc, argv, "vhkc:",
367 long_options, NULL)) != -1)
368 switch(opt)
370 case 'v':
371 eprint_version();
372 break;
373 case 'h':
374 exit_help(EXIT_SUCCESS);
375 break;
376 case 'k':
377 if(!luaA_parserc(&xdg, confpath, false))
379 fprintf(stderr, "✘ Configuration file syntax error.\n");
380 return EXIT_FAILURE;
382 else
384 fprintf(stderr, "✔ Configuration file syntax OK.\n");
385 return EXIT_SUCCESS;
387 case 'c':
388 if(a_strlen(optarg))
389 confpath = a_strdup(optarg);
390 else
391 fatal("-c option requires a file name");
392 break;
395 globalconf.loop = ev_default_loop(0);
396 ev_timer_init(&globalconf.timer, &luaA_on_timer, 0., 0.);
398 /* register function for signals */
399 ev_signal_init(&sigint, exit_on_signal, SIGINT);
400 ev_signal_init(&sigterm, exit_on_signal, SIGTERM);
401 ev_signal_init(&sighup, restart_on_signal, SIGHUP);
402 ev_signal_start(globalconf.loop, &sigint);
403 ev_signal_start(globalconf.loop, &sigterm);
404 ev_signal_start(globalconf.loop, &sighup);
405 ev_unref(globalconf.loop);
406 ev_unref(globalconf.loop);
407 ev_unref(globalconf.loop);
409 /* X stuff */
410 globalconf.connection = xcb_connect(NULL, &globalconf.default_screen);
411 if(xcb_connection_has_error(globalconf.connection))
412 fatal("cannot open display");
414 /* check for xtest extension */
415 const xcb_query_extension_reply_t *xtest_query;
416 xtest_query = xcb_get_extension_data(globalconf.connection, &xcb_test_id);
417 globalconf.have_xtest = xtest_query->present;
419 /* initiliaze dbus */
420 a_dbus_init();
422 /* Grab server */
423 xcb_grab_server(globalconf.connection);
424 xcb_flush(globalconf.connection);
426 /* Get the file descriptor corresponding to the X connection */
427 xfd = xcb_get_file_descriptor(globalconf.connection);
428 ev_io_init(&xio, &a_xcb_io_cb, xfd, EV_READ);
429 ev_io_start(globalconf.loop, &xio);
430 ev_check_init(&xcheck, &a_xcb_check_cb);
431 ev_check_start(globalconf.loop, &xcheck);
432 ev_unref(globalconf.loop);
434 /* Allocate a handler which will holds all errors and events */
435 xcb_event_handlers_init(globalconf.connection, &globalconf.evenths);
436 xutil_error_handler_catch_all_set(&globalconf.evenths, xerrorstart, NULL);
438 for(screen_nbr = 0;
439 screen_nbr < xcb_setup_roots_length(xcb_get_setup(globalconf.connection));
440 screen_nbr++)
442 const uint32_t select_input_val = XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT;
444 /* This causes an error if some other window manager is running */
445 xcb_change_window_attributes(globalconf.connection,
446 xutil_screen_get(globalconf.connection, screen_nbr)->root,
447 XCB_CW_EVENT_MASK, &select_input_val);
450 /* Need to xcb_flush to validate error handler */
451 xcb_aux_sync(globalconf.connection);
453 /* Process all errors in the queue if any */
454 xcb_event_poll_for_event_loop(&globalconf.evenths);
456 /* Set the default xerror handler */
457 xutil_error_handler_catch_all_set(&globalconf.evenths, xerror, NULL);
459 /* Allocate the key symbols */
460 globalconf.keysyms = xcb_key_symbols_alloc(globalconf.connection);
461 xcb_get_modifier_mapping_cookie_t xmapping_cookie =
462 xcb_get_modifier_mapping_unchecked(globalconf.connection);
464 /* init atom cache */
465 atoms_init(globalconf.connection);
467 /* init screens information */
468 screen_scan();
470 /* init default font and colors */
471 colors_reqs[0] = xcolor_init_unchecked(&globalconf.colors.fg,
472 "black", sizeof("black") - 1);
474 colors_reqs[1] = xcolor_init_unchecked(&globalconf.colors.bg,
475 "white", sizeof("white") - 1);
477 globalconf.font = draw_font_new("sans 8");
479 for(colors_nbr = 0; colors_nbr < 2; colors_nbr++)
480 xcolor_init_reply(colors_reqs[colors_nbr]);
482 xutil_lock_mask_get(globalconf.connection, xmapping_cookie,
483 globalconf.keysyms, &globalconf.numlockmask,
484 &globalconf.shiftlockmask, &globalconf.capslockmask,
485 &globalconf.modeswitchmask);
487 /* do this only for real screen */
488 for(screen_nbr = 0;
489 screen_nbr < xcb_setup_roots_length(xcb_get_setup(globalconf.connection));
490 screen_nbr++)
492 /* select for events */
493 const uint32_t change_win_vals[] =
495 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT | XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY
496 | XCB_EVENT_MASK_ENTER_WINDOW | XCB_EVENT_MASK_LEAVE_WINDOW
497 | XCB_EVENT_MASK_STRUCTURE_NOTIFY
498 | XCB_EVENT_MASK_PROPERTY_CHANGE
499 | XCB_EVENT_MASK_BUTTON_PRESS
500 | XCB_EVENT_MASK_BUTTON_RELEASE
501 | XCB_EVENT_MASK_FOCUS_CHANGE
504 xcb_change_window_attributes(globalconf.connection,
505 xutil_screen_get(globalconf.connection, screen_nbr)->root,
506 XCB_CW_EVENT_MASK,
507 change_win_vals);
508 ewmh_init(screen_nbr);
509 systray_init(screen_nbr);
512 /* init spawn (sn) */
513 spawn_init();
515 /* Parse and run configuration file */
516 luaA_parserc(&xdg, confpath, true);
518 xdgWipeHandle(&xdg);
520 /* scan existing windows */
521 scan();
523 /* process all errors in the queue if any */
524 xcb_event_poll_for_event_loop(&globalconf.evenths);
525 a_xcb_set_event_handlers();
526 a_xcb_set_property_handlers();
528 /* we will receive events, stop grabbing server */
529 xcb_ungrab_server(globalconf.connection);
530 xcb_flush(globalconf.connection);
532 /* refresh everything before waiting events */
533 awesome_refresh();
535 /* main event loop */
536 ev_loop(globalconf.loop, 0);
538 /* cleanup event loop */
539 ev_ref(globalconf.loop);
540 ev_check_stop(globalconf.loop, &xcheck);
541 ev_ref(globalconf.loop);
542 ev_io_stop(globalconf.loop, &xio);
544 awesome_atexit();
546 return EXIT_SUCCESS;
549 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80