Merge branch 'fix-ldoc-set-spacing' of git://github.com/actionless/awesome
[awesome.git] / awesome.c
blob3931558b97247f4c15e94797bd0385b5f4a6f019
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 "awesome.h"
24 #include "banning.h"
25 #include "common/atoms.h"
26 #include "common/backtrace.h"
27 #include "common/version.h"
28 #include "common/xutil.h"
29 #include "dbus.h"
30 #include "event.h"
31 #include "ewmh.h"
32 #include "globalconf.h"
33 #include "objects/client.h"
34 #include "objects/screen.h"
35 #include "spawn.h"
36 #include "systray.h"
37 #include "xwindow.h"
39 #include <getopt.h>
41 #include <locale.h>
42 #include <stdio.h>
43 #include <unistd.h>
44 #include <signal.h>
45 #include <sys/time.h>
47 #include <xcb/bigreq.h>
48 #include <xcb/randr.h>
49 #include <xcb/xcb_aux.h>
50 #include <xcb/xcb_event.h>
51 #include <xcb/xinerama.h>
52 #include <xcb/xtest.h>
53 #include <xcb/shape.h>
55 #include <glib-unix.h>
57 awesome_t globalconf;
59 /** argv used to run awesome */
60 static char *awesome_argv;
62 /** time of last main loop wakeup */
63 static struct timeval last_wakeup;
65 /** current limit for the main loop's runtime */
66 static float main_loop_iteration_limit = 0.1;
68 /** Call before exiting.
70 void
71 awesome_atexit(bool restart)
73 lua_pushboolean(globalconf.L, restart);
74 signal_object_emit(globalconf.L, &global_signals, "exit", 1);
76 a_dbus_cleanup();
78 systray_cleanup();
80 /* Close Lua */
81 lua_close(globalconf.L);
83 /* X11 is a great protocol. There is a save-set so that reparenting WMs
84 * don't kill clients when they shut down. However, when a focused windows
85 * is saved, the focus will move to its parent with revert-to none.
86 * Immediately afterwards, this parent is destroyed and the focus is gone.
87 * Work around this by placing the focus where we like it to be.
89 xcb_set_input_focus(globalconf.connection, XCB_INPUT_FOCUS_POINTER_ROOT,
90 XCB_NONE, XCB_CURRENT_TIME);
91 xcb_aux_sync(globalconf.connection);
93 /* Disconnect *after* closing lua */
94 xcb_cursor_context_free(globalconf.cursor_ctx);
95 xcb_disconnect(globalconf.connection);
98 /** Scan X to find windows to manage.
100 static void
101 scan(xcb_query_tree_cookie_t tree_c)
103 int i, tree_c_len;
104 xcb_query_tree_reply_t *tree_r;
105 xcb_window_t *wins = NULL;
106 xcb_get_window_attributes_reply_t *attr_r;
107 xcb_get_geometry_reply_t *geom_r;
108 long state;
110 tree_r = xcb_query_tree_reply(globalconf.connection,
111 tree_c,
112 NULL);
114 if(!tree_r)
115 return;
117 /* Get the tree of the children windows of the current root window */
118 if(!(wins = xcb_query_tree_children(tree_r)))
119 fatal("cannot get tree children");
121 tree_c_len = xcb_query_tree_children_length(tree_r);
122 xcb_get_window_attributes_cookie_t attr_wins[tree_c_len];
123 xcb_get_property_cookie_t state_wins[tree_c_len];
124 xcb_get_geometry_cookie_t geom_wins[tree_c_len];
126 for(i = 0; i < tree_c_len; i++)
128 attr_wins[i] = xcb_get_window_attributes_unchecked(globalconf.connection,
129 wins[i]);
131 state_wins[i] = xwindow_get_state_unchecked(wins[i]);
132 geom_wins[i] = xcb_get_geometry_unchecked(globalconf.connection, wins[i]);
135 for(i = 0; i < tree_c_len; i++)
137 attr_r = xcb_get_window_attributes_reply(globalconf.connection,
138 attr_wins[i],
139 NULL);
140 geom_r = xcb_get_geometry_reply(globalconf.connection, geom_wins[i], NULL);
142 state = xwindow_get_state_reply(state_wins[i]);
144 if(!attr_r || attr_r->override_redirect
145 || attr_r->map_state == XCB_MAP_STATE_UNMAPPED
146 || state == XCB_ICCCM_WM_STATE_WITHDRAWN)
148 p_delete(&attr_r);
149 p_delete(&geom_r);
150 continue;
153 client_manage(wins[i], geom_r, attr_r);
155 p_delete(&attr_r);
156 p_delete(&geom_r);
159 p_delete(&tree_r);
162 static void
163 a_xcb_check(void)
165 xcb_generic_event_t *mouse = NULL, *event;
167 while((event = xcb_poll_for_event(globalconf.connection)))
169 /* We will treat mouse events later.
170 * We cannot afford to treat all mouse motion events,
171 * because that would be too much CPU intensive, so we just
172 * take the last we get after a bunch of events. */
173 if(XCB_EVENT_RESPONSE_TYPE(event) == XCB_MOTION_NOTIFY)
175 p_delete(&mouse);
176 mouse = event;
178 else
180 uint8_t type = XCB_EVENT_RESPONSE_TYPE(event);
181 if(mouse && (type == XCB_ENTER_NOTIFY || type == XCB_LEAVE_NOTIFY
182 || type == XCB_BUTTON_PRESS || type == XCB_BUTTON_RELEASE))
184 /* Make sure enter/motion/leave/press/release events are handled
185 * in the correct order */
186 event_handle(mouse);
187 p_delete(&mouse);
189 event_handle(event);
190 p_delete(&event);
194 if(mouse)
196 event_handle(mouse);
197 p_delete(&mouse);
201 static gboolean
202 a_xcb_io_cb(GIOChannel *source, GIOCondition cond, gpointer data)
204 /* a_xcb_check() already handled all events */
206 if(xcb_connection_has_error(globalconf.connection))
207 fatal("X server connection broke (error %d)",
208 xcb_connection_has_error(globalconf.connection));
210 return TRUE;
213 static gint
214 a_glib_poll(GPollFD *ufds, guint nfsd, gint timeout)
216 guint res;
217 struct timeval now, length_time;
218 float length;
220 /* Do all deferred work now */
221 awesome_refresh();
223 /* Check how long this main loop iteration took */
224 gettimeofday(&now, NULL);
225 timersub(&now, &last_wakeup, &length_time);
226 length = length_time.tv_sec + length_time.tv_usec * 1.0f / 1e6;
227 if (length > main_loop_iteration_limit) {
228 warn("Last main loop iteration took %.6f seconds! Increasing limit for "
229 "this warning to that value.", length);
230 main_loop_iteration_limit = length;
233 /* Actually do the polling, record time of wakeup and check for new xcb events */
234 res = g_poll(ufds, nfsd, timeout);
235 gettimeofday(&last_wakeup, NULL);
236 a_xcb_check();
238 return res;
241 static void
242 signal_fatal(int signum)
244 buffer_t buf;
245 backtrace_get(&buf);
246 fatal("signal %d, dumping backtrace\n%s", signum, buf.s);
249 /** Function to exit on some signals.
250 * \param data currently unused
252 static gboolean
253 exit_on_signal(gpointer data)
255 g_main_loop_quit(globalconf.loop);
256 return TRUE;
259 void
260 awesome_restart(void)
262 awesome_atexit(true);
263 a_exec(awesome_argv);
266 /** Function to restart awesome on some signals.
267 * \param data currently unused
269 static gboolean
270 restart_on_signal(gpointer data)
272 awesome_restart();
273 return TRUE;
276 /** Print help and exit(2) with given exit_code.
277 * \param exit_code The exit code.
279 static void __attribute__ ((noreturn))
280 exit_help(int exit_code)
282 FILE *outfile = (exit_code == EXIT_SUCCESS) ? stdout : stderr;
283 fprintf(outfile,
284 "Usage: awesome [OPTION]\n\
285 -h, --help show help\n\
286 -v, --version show version\n\
287 -c, --config FILE configuration file to use\n\
288 -k, --check check configuration file syntax\n");
289 exit(exit_code);
292 /** Hello, this is main.
293 * \param argc Who knows.
294 * \param argv Who knows.
295 * \return EXIT_SUCCESS I hope.
298 main(int argc, char **argv)
300 char *confpath = NULL;
301 int xfd, i, opt;
302 ssize_t cmdlen = 1;
303 xdgHandle xdg;
304 bool no_argb = false;
305 bool run_test = false;
306 xcb_query_tree_cookie_t tree_c;
307 static struct option long_options[] =
309 { "help", 0, NULL, 'h' },
310 { "version", 0, NULL, 'v' },
311 { "config", 1, NULL, 'c' },
312 { "check", 0, NULL, 'k' },
313 { "no-argb", 0, NULL, 'a' },
314 { NULL, 0, NULL, 0 }
317 /* clear the globalconf structure */
318 p_clear(&globalconf, 1);
319 globalconf.keygrabber = LUA_REFNIL;
320 globalconf.mousegrabber = LUA_REFNIL;
321 buffer_init(&globalconf.startup_errors);
323 /* save argv */
324 for(i = 0; i < argc; i++)
325 cmdlen += a_strlen(argv[i]) + 1;
327 awesome_argv = p_new(char, cmdlen);
328 a_strcpy(awesome_argv, cmdlen, argv[0]);
330 for(i = 1; i < argc; i++)
332 a_strcat(awesome_argv, cmdlen, " ");
333 a_strcat(awesome_argv, cmdlen, argv[i]);
336 /* Text won't be printed correctly otherwise */
337 setlocale(LC_CTYPE, "");
339 /* Get XDG basedir data */
340 xdgInitHandle(&xdg);
342 /* init lua */
343 luaA_init(&xdg);
345 /* check args */
346 while((opt = getopt_long(argc, argv, "vhkc:a",
347 long_options, NULL)) != -1)
348 switch(opt)
350 case 'v':
351 eprint_version();
352 break;
353 case 'h':
354 exit_help(EXIT_SUCCESS);
355 break;
356 case 'k':
357 run_test = true;
358 break;
359 case 'c':
360 if(a_strlen(optarg))
361 confpath = a_strdup(optarg);
362 else
363 fatal("-c option requires a file name");
364 break;
365 case 'a':
366 no_argb = true;
367 break;
370 if (run_test)
372 if(!luaA_parserc(&xdg, confpath, false))
374 fprintf(stderr, "✘ Configuration file syntax error.\n");
375 return EXIT_FAILURE;
377 else
379 fprintf(stderr, "✔ Configuration file syntax OK.\n");
380 return EXIT_SUCCESS;
384 /* register function for signals */
385 g_unix_signal_add(SIGINT, exit_on_signal, NULL);
386 g_unix_signal_add(SIGTERM, exit_on_signal, NULL);
387 g_unix_signal_add(SIGHUP, restart_on_signal, NULL);
389 struct sigaction sa = { .sa_handler = signal_fatal, .sa_flags = SA_RESETHAND };
390 sigemptyset(&sa.sa_mask);
391 sigaction(SIGABRT, &sa, 0);
392 sigaction(SIGBUS, &sa, 0);
393 sigaction(SIGFPE, &sa, 0);
394 sigaction(SIGILL, &sa, 0);
395 sigaction(SIGSEGV, &sa, 0);
397 /* We have no clue where the input focus is right now */
398 globalconf.focus.need_update = true;
400 /* X stuff */
401 globalconf.connection = xcb_connect(NULL, &globalconf.default_screen);
402 if(xcb_connection_has_error(globalconf.connection))
403 fatal("cannot open display (error %d)", xcb_connection_has_error(globalconf.connection));
405 globalconf.screen = xcb_aux_get_screen(globalconf.connection, globalconf.default_screen);
406 globalconf.default_visual = draw_default_visual(globalconf.screen);
407 if(!no_argb)
408 globalconf.visual = draw_argb_visual(globalconf.screen);
409 if(!globalconf.visual)
410 globalconf.visual = globalconf.default_visual;
411 globalconf.default_depth = draw_visual_depth(globalconf.screen, globalconf.visual->visual_id);
412 globalconf.default_cmap = globalconf.screen->default_colormap;
413 if(globalconf.default_depth != globalconf.screen->root_depth)
415 // We need our own color map if we aren't using the default depth
416 globalconf.default_cmap = xcb_generate_id(globalconf.connection);
417 xcb_create_colormap(globalconf.connection, XCB_COLORMAP_ALLOC_NONE,
418 globalconf.default_cmap, globalconf.screen->root,
419 globalconf.visual->visual_id);
422 /* Prefetch all the extensions we might need */
423 xcb_prefetch_extension_data(globalconf.connection, &xcb_big_requests_id);
424 xcb_prefetch_extension_data(globalconf.connection, &xcb_test_id);
425 xcb_prefetch_extension_data(globalconf.connection, &xcb_randr_id);
426 xcb_prefetch_extension_data(globalconf.connection, &xcb_xinerama_id);
427 xcb_prefetch_extension_data(globalconf.connection, &xcb_shape_id);
429 if (xcb_cursor_context_new(globalconf.connection, globalconf.screen, &globalconf.cursor_ctx) < 0)
430 fatal("Failed to initialize xcb-cursor");
432 /* initialize dbus */
433 a_dbus_init();
435 /* Get the file descriptor corresponding to the X connection */
436 xfd = xcb_get_file_descriptor(globalconf.connection);
437 GIOChannel *channel = g_io_channel_unix_new(xfd);
438 g_io_add_watch(channel, G_IO_IN, a_xcb_io_cb, NULL);
439 g_io_channel_unref(channel);
441 /* Grab server */
442 xcb_grab_server(globalconf.connection);
445 const uint32_t select_input_val = XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT;
446 xcb_void_cookie_t cookie;
448 /* This causes an error if some other window manager is running */
449 cookie = xcb_change_window_attributes_checked(globalconf.connection,
450 globalconf.screen->root,
451 XCB_CW_EVENT_MASK, &select_input_val);
452 if (xcb_request_check(globalconf.connection, cookie))
453 fatal("another window manager is already running");
456 /* Prefetch the maximum request length */
457 xcb_prefetch_maximum_request_length(globalconf.connection);
459 /* check for xtest extension */
460 const xcb_query_extension_reply_t *query;
461 query = xcb_get_extension_data(globalconf.connection, &xcb_test_id);
462 globalconf.have_xtest = query->present;
464 /* check for shape extension */
465 query = xcb_get_extension_data(globalconf.connection, &xcb_shape_id);
466 globalconf.have_shape = query->present;
468 /* Allocate the key symbols */
469 globalconf.keysyms = xcb_key_symbols_alloc(globalconf.connection);
470 xcb_get_modifier_mapping_cookie_t xmapping_cookie =
471 xcb_get_modifier_mapping_unchecked(globalconf.connection);
473 /* init atom cache */
474 atoms_init(globalconf.connection);
476 /* init screens information */
477 screen_scan();
479 xutil_lock_mask_get(globalconf.connection, xmapping_cookie,
480 globalconf.keysyms, &globalconf.numlockmask,
481 &globalconf.shiftlockmask, &globalconf.capslockmask,
482 &globalconf.modeswitchmask);
484 /* do this only for real screen */
485 ewmh_init();
486 systray_init();
488 /* init spawn (sn) */
489 spawn_init();
491 /* The default GC is just a newly created associated with a window with
492 * depth globalconf.default_depth */
493 xcb_window_t tmp_win = xcb_generate_id(globalconf.connection);
494 globalconf.gc = xcb_generate_id(globalconf.connection);
495 xcb_create_window(globalconf.connection, globalconf.default_depth,
496 tmp_win, globalconf.screen->root,
497 -1, -1, 1, 1, 0,
498 XCB_COPY_FROM_PARENT, globalconf.visual->visual_id,
499 XCB_CW_BACK_PIXEL | XCB_CW_BORDER_PIXEL | XCB_CW_COLORMAP,
500 (const uint32_t [])
502 globalconf.screen->black_pixel,
503 globalconf.screen->black_pixel,
504 globalconf.default_cmap
506 xcb_create_gc(globalconf.connection, globalconf.gc, tmp_win, XCB_GC_FOREGROUND | XCB_GC_BACKGROUND,
507 (const uint32_t[]) { globalconf.screen->black_pixel, globalconf.screen->white_pixel });
508 xcb_destroy_window(globalconf.connection, tmp_win);
510 /* Get the window tree associated to this screen */
511 tree_c = xcb_query_tree_unchecked(globalconf.connection,
512 globalconf.screen->root);
514 xcb_change_window_attributes(globalconf.connection,
515 globalconf.screen->root,
516 XCB_CW_EVENT_MASK,
517 ROOT_WINDOW_EVENT_MASK);
519 /* we will receive events, stop grabbing server */
520 xcb_ungrab_server(globalconf.connection);
521 xcb_flush(globalconf.connection);
523 /* Parse and run configuration file */
524 if (!luaA_parserc(&xdg, confpath, true))
525 fatal("couldn't find any rc file");
527 p_delete(&confpath);
529 xdgWipeHandle(&xdg);
531 /* scan existing windows */
532 scan(tree_c);
534 xcb_flush(globalconf.connection);
536 /* Setup the main context */
537 g_main_context_set_poll_func(g_main_context_default(), &a_glib_poll);
538 gettimeofday(&last_wakeup, NULL);
540 /* main event loop */
541 globalconf.loop = g_main_loop_new(NULL, FALSE);
542 g_main_loop_run(globalconf.loop);
543 g_main_loop_unref(globalconf.loop);
544 globalconf.loop = NULL;
546 awesome_atexit(false);
548 return EXIT_SUCCESS;
551 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80