Add a "deep" option to awful.util.table.clone
[awesome.git] / awesome.c
blobe4945d94e0639bae0dc036eb44867ff53a0db83a
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>
28 #include <sys/time.h>
30 #include <xcb/bigreq.h>
31 #include <xcb/randr.h>
32 #include <xcb/xcb_event.h>
33 #include <xcb/xinerama.h>
34 #include <xcb/xtest.h>
35 #include <xcb/shape.h>
37 #include <glib-unix.h>
39 #include "awesome.h"
40 #include "spawn.h"
41 #include "objects/client.h"
42 #include "xwindow.h"
43 #include "ewmh.h"
44 #include "dbus.h"
45 #include "systray.h"
46 #include "event.h"
47 #include "property.h"
48 #include "screen.h"
49 #include "luaa.h"
50 #include "common/version.h"
51 #include "common/atoms.h"
52 #include "common/xcursor.h"
53 #include "common/xutil.h"
54 #include "common/backtrace.h"
56 awesome_t globalconf;
58 /** argv used to run awesome */
59 static char *awesome_argv;
61 /** time of last main loop wakeup */
62 static struct timeval last_wakeup;
64 /** current limit for the main loop's runtime */
65 static float main_loop_iteration_limit = 0.1;
67 /** Call before exiting.
69 void
70 awesome_atexit(bool restart)
72 lua_pushboolean(globalconf.L, restart);
73 signal_object_emit(globalconf.L, &global_signals, "exit", 1);
75 a_dbus_cleanup();
77 systray_cleanup();
79 /* Close Lua */
80 lua_close(globalconf.L);
82 /* X11 is a great protocol. There is a save-set so that reparenting WMs
83 * don't kill clients when they shut down. However, when a focused windows
84 * is saved, the focus will move to its parent with revert-to none.
85 * Immediately afterwards, this parent is destroyed and the focus is gone.
86 * Work around this by placing the focus where we like it to be.
88 xcb_set_input_focus(globalconf.connection, XCB_INPUT_FOCUS_POINTER_ROOT,
89 XCB_NONE, XCB_CURRENT_TIME);
90 xcb_aux_sync(globalconf.connection);
92 /* Disconnect *after* closing lua */
93 xcb_cursor_context_free(globalconf.cursor_ctx);
94 xcb_disconnect(globalconf.connection);
97 /** Scan X to find windows to manage.
99 static void
100 scan(xcb_query_tree_cookie_t tree_c)
102 int i, tree_c_len;
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 tree_r = xcb_query_tree_reply(globalconf.connection,
110 tree_c,
111 NULL);
113 if(!tree_r)
114 return;
116 /* Get the tree of the children windows of the current root window */
117 if(!(wins = xcb_query_tree_children(tree_r)))
118 fatal("cannot get tree children");
120 tree_c_len = xcb_query_tree_children_length(tree_r);
121 xcb_get_window_attributes_cookie_t attr_wins[tree_c_len];
122 xcb_get_property_cookie_t state_wins[tree_c_len];
124 for(i = 0; i < tree_c_len; i++)
126 attr_wins[i] = xcb_get_window_attributes_unchecked(globalconf.connection,
127 wins[i]);
129 state_wins[i] = xwindow_get_state_unchecked(wins[i]);
132 xcb_get_geometry_cookie_t *geom_wins[tree_c_len];
134 for(i = 0; i < tree_c_len; i++)
136 attr_r = xcb_get_window_attributes_reply(globalconf.connection,
137 attr_wins[i],
138 NULL);
140 state = xwindow_get_state_reply(state_wins[i]);
142 if(!attr_r || attr_r->override_redirect
143 || attr_r->map_state == XCB_MAP_STATE_UNMAPPED
144 || state == XCB_ICCCM_WM_STATE_WITHDRAWN)
146 geom_wins[i] = NULL;
147 p_delete(&attr_r);
148 continue;
151 p_delete(&attr_r);
153 /* Get the geometry of the current window */
154 geom_wins[i] = p_alloca(xcb_get_geometry_cookie_t, 1);
155 *(geom_wins[i]) = xcb_get_geometry_unchecked(globalconf.connection, wins[i]);
158 for(i = 0; i < tree_c_len; i++)
160 if(!geom_wins[i] || !(geom_r = xcb_get_geometry_reply(globalconf.connection,
161 *(geom_wins[i]), NULL)))
162 continue;
164 client_manage(wins[i], geom_r, true);
166 p_delete(&geom_r);
169 p_delete(&tree_r);
172 static void
173 a_xcb_check(void)
175 xcb_generic_event_t *mouse = NULL, *event;
177 while((event = xcb_poll_for_event(globalconf.connection)))
179 /* We will treat mouse events later.
180 * We cannot afford to treat all mouse motion events,
181 * because that would be too much CPU intensive, so we just
182 * take the last we get after a bunch of events. */
183 if(XCB_EVENT_RESPONSE_TYPE(event) == XCB_MOTION_NOTIFY)
185 p_delete(&mouse);
186 mouse = event;
188 else
190 uint8_t type = XCB_EVENT_RESPONSE_TYPE(event);
191 if(mouse && (type == XCB_ENTER_NOTIFY || type == XCB_LEAVE_NOTIFY
192 || type == XCB_BUTTON_PRESS || type == XCB_BUTTON_RELEASE))
194 /* Make sure enter/motion/leave/press/release events are handled
195 * in the correct order */
196 event_handle(mouse);
197 p_delete(&mouse);
199 event_handle(event);
200 p_delete(&event);
204 if(mouse)
206 event_handle(mouse);
207 p_delete(&mouse);
211 static gboolean
212 a_xcb_io_cb(GIOChannel *source, GIOCondition cond, gpointer data)
214 /* a_xcb_check() already handled all events */
216 if(xcb_connection_has_error(globalconf.connection))
217 fatal("X server connection broke (error %d)",
218 xcb_connection_has_error(globalconf.connection));
220 return TRUE;
223 static gint
224 a_glib_poll(GPollFD *ufds, guint nfsd, gint timeout)
226 guint res;
227 struct timeval now, length_time;
228 float length;
230 /* Do all deferred work now */
231 awesome_refresh();
233 /* Check how long this main loop iteration took */
234 gettimeofday(&now, NULL);
235 timersub(&now, &last_wakeup, &length_time);
236 length = length_time.tv_sec + length_time.tv_usec * 1.0f / 1e6;
237 if (length > main_loop_iteration_limit) {
238 warn("Last main loop iteration took %.6f seconds! Increasing limit for "
239 "this warning to that value.", length);
240 main_loop_iteration_limit = length;
243 /* Actually do the polling, record time of wakeup and check for new xcb events */
244 res = g_poll(ufds, nfsd, timeout);
245 gettimeofday(&last_wakeup, NULL);
246 a_xcb_check();
248 return res;
251 static void
252 signal_fatal(int signum)
254 buffer_t buf;
255 backtrace_get(&buf);
256 fatal("signal %d, dumping backtrace\n%s", signum, buf.s);
259 /** Function to exit on some signals.
260 * \param data currently unused
262 static gboolean
263 exit_on_signal(gpointer data)
265 g_main_loop_quit(globalconf.loop);
266 return TRUE;
269 void
270 awesome_restart(void)
272 awesome_atexit(true);
273 a_exec(awesome_argv);
276 /** Function to restart awesome on some signals.
277 * \param data currently unused
279 static gboolean
280 restart_on_signal(gpointer data)
282 awesome_restart();
283 return TRUE;
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 char *confpath = NULL;
311 int xfd, i, opt;
312 ssize_t cmdlen = 1;
313 xdgHandle xdg;
314 bool no_argb = false;
315 bool run_test = false;
316 xcb_query_tree_cookie_t tree_c;
317 static struct option long_options[] =
319 { "help", 0, NULL, 'h' },
320 { "version", 0, NULL, 'v' },
321 { "config", 1, NULL, 'c' },
322 { "check", 0, NULL, 'k' },
323 { "no-argb", 0, NULL, 'a' },
324 { NULL, 0, NULL, 0 }
327 /* clear the globalconf structure */
328 p_clear(&globalconf, 1);
329 globalconf.keygrabber = LUA_REFNIL;
330 globalconf.mousegrabber = LUA_REFNIL;
331 buffer_init(&globalconf.startup_errors);
333 /* save argv */
334 for(i = 0; i < argc; i++)
335 cmdlen += a_strlen(argv[i]) + 1;
337 awesome_argv = p_new(char, cmdlen);
338 a_strcpy(awesome_argv, cmdlen, argv[0]);
340 for(i = 1; i < argc; i++)
342 a_strcat(awesome_argv, cmdlen, " ");
343 a_strcat(awesome_argv, cmdlen, argv[i]);
346 /* Text won't be printed correctly otherwise */
347 setlocale(LC_CTYPE, "");
349 /* Get XDG basedir data */
350 xdgInitHandle(&xdg);
352 /* init lua */
353 luaA_init(&xdg);
355 /* check args */
356 while((opt = getopt_long(argc, argv, "vhkc:a",
357 long_options, NULL)) != -1)
358 switch(opt)
360 case 'v':
361 eprint_version();
362 break;
363 case 'h':
364 exit_help(EXIT_SUCCESS);
365 break;
366 case 'k':
367 run_test = true;
368 break;
369 case 'c':
370 if(a_strlen(optarg))
371 confpath = a_strdup(optarg);
372 else
373 fatal("-c option requires a file name");
374 break;
375 case 'a':
376 no_argb = true;
377 break;
380 if (run_test)
382 if(!luaA_parserc(&xdg, confpath, false))
384 fprintf(stderr, "✘ Configuration file syntax error.\n");
385 return EXIT_FAILURE;
387 else
389 fprintf(stderr, "✔ Configuration file syntax OK.\n");
390 return EXIT_SUCCESS;
394 /* register function for signals */
395 g_unix_signal_add(SIGINT, exit_on_signal, NULL);
396 g_unix_signal_add(SIGTERM, exit_on_signal, NULL);
397 g_unix_signal_add(SIGHUP, restart_on_signal, NULL);
399 struct sigaction sa = { .sa_handler = signal_fatal, .sa_flags = SA_RESETHAND };
400 sigemptyset(&sa.sa_mask);
401 sigaction(SIGABRT, &sa, 0);
402 sigaction(SIGBUS, &sa, 0);
403 sigaction(SIGFPE, &sa, 0);
404 sigaction(SIGILL, &sa, 0);
405 sigaction(SIGSEGV, &sa, 0);
407 /* We have no clue where the input focus is right now */
408 globalconf.focus.need_update = true;
410 /* X stuff */
411 globalconf.connection = xcb_connect(NULL, &globalconf.default_screen);
412 if(xcb_connection_has_error(globalconf.connection))
413 fatal("cannot open display (error %d)", xcb_connection_has_error(globalconf.connection));
415 globalconf.screen = xcb_aux_get_screen(globalconf.connection, globalconf.default_screen);
416 globalconf.default_visual = draw_default_visual(globalconf.screen);
417 if(!no_argb)
418 globalconf.visual = draw_argb_visual(globalconf.screen);
419 if(!globalconf.visual)
420 globalconf.visual = globalconf.default_visual;
421 globalconf.default_depth = draw_visual_depth(globalconf.screen, globalconf.visual->visual_id);
422 globalconf.default_cmap = globalconf.screen->default_colormap;
423 if(globalconf.default_depth != globalconf.screen->root_depth)
425 // We need our own color map if we aren't using the default depth
426 globalconf.default_cmap = xcb_generate_id(globalconf.connection);
427 xcb_create_colormap(globalconf.connection, XCB_COLORMAP_ALLOC_NONE,
428 globalconf.default_cmap, globalconf.screen->root,
429 globalconf.visual->visual_id);
432 /* Prefetch all the extensions we might need */
433 xcb_prefetch_extension_data(globalconf.connection, &xcb_big_requests_id);
434 xcb_prefetch_extension_data(globalconf.connection, &xcb_test_id);
435 xcb_prefetch_extension_data(globalconf.connection, &xcb_randr_id);
436 xcb_prefetch_extension_data(globalconf.connection, &xcb_xinerama_id);
437 xcb_prefetch_extension_data(globalconf.connection, &xcb_shape_id);
439 if (xcb_cursor_context_new(globalconf.connection, globalconf.screen, &globalconf.cursor_ctx) < 0)
440 fatal("Failed to initialize xcb-cursor");
442 /* initialize dbus */
443 a_dbus_init();
445 /* Get the file descriptor corresponding to the X connection */
446 xfd = xcb_get_file_descriptor(globalconf.connection);
447 GIOChannel *channel = g_io_channel_unix_new(xfd);
448 g_io_add_watch(channel, G_IO_IN, a_xcb_io_cb, NULL);
449 g_io_channel_unref(channel);
451 /* Grab server */
452 xcb_grab_server(globalconf.connection);
455 const uint32_t select_input_val = XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT;
456 xcb_void_cookie_t cookie;
458 /* This causes an error if some other window manager is running */
459 cookie = xcb_change_window_attributes_checked(globalconf.connection,
460 globalconf.screen->root,
461 XCB_CW_EVENT_MASK, &select_input_val);
462 if (xcb_request_check(globalconf.connection, cookie))
463 fatal("another window manager is already running");
466 /* Prefetch the maximum request length */
467 xcb_prefetch_maximum_request_length(globalconf.connection);
469 /* check for xtest extension */
470 const xcb_query_extension_reply_t *query;
471 query = xcb_get_extension_data(globalconf.connection, &xcb_test_id);
472 globalconf.have_xtest = query->present;
474 /* check for shape extension */
475 query = xcb_get_extension_data(globalconf.connection, &xcb_shape_id);
476 globalconf.have_shape = query->present;
478 /* Allocate the key symbols */
479 globalconf.keysyms = xcb_key_symbols_alloc(globalconf.connection);
480 xcb_get_modifier_mapping_cookie_t xmapping_cookie =
481 xcb_get_modifier_mapping_unchecked(globalconf.connection);
483 /* init atom cache */
484 atoms_init(globalconf.connection);
486 /* init screens information */
487 screen_scan();
489 xutil_lock_mask_get(globalconf.connection, xmapping_cookie,
490 globalconf.keysyms, &globalconf.numlockmask,
491 &globalconf.shiftlockmask, &globalconf.capslockmask,
492 &globalconf.modeswitchmask);
494 /* do this only for real screen */
495 ewmh_init();
496 systray_init();
498 /* init spawn (sn) */
499 spawn_init();
501 /* The default GC is just a newly created associated with a window with
502 * depth globalconf.default_depth */
503 xcb_window_t tmp_win = xcb_generate_id(globalconf.connection);
504 globalconf.gc = xcb_generate_id(globalconf.connection);
505 xcb_create_window(globalconf.connection, globalconf.default_depth,
506 tmp_win, globalconf.screen->root,
507 -1, -1, 1, 1, 0,
508 XCB_COPY_FROM_PARENT, globalconf.visual->visual_id,
509 XCB_CW_BACK_PIXEL | XCB_CW_BORDER_PIXEL | XCB_CW_COLORMAP,
510 (const uint32_t [])
512 globalconf.screen->black_pixel,
513 globalconf.screen->black_pixel,
514 globalconf.default_cmap
516 xcb_create_gc(globalconf.connection, globalconf.gc, tmp_win, XCB_GC_FOREGROUND | XCB_GC_BACKGROUND,
517 (const uint32_t[]) { globalconf.screen->black_pixel, globalconf.screen->white_pixel });
518 xcb_destroy_window(globalconf.connection, tmp_win);
520 /* Get the window tree associated to this screen */
521 tree_c = xcb_query_tree_unchecked(globalconf.connection,
522 globalconf.screen->root);
524 xcb_change_window_attributes(globalconf.connection,
525 globalconf.screen->root,
526 XCB_CW_EVENT_MASK,
527 ROOT_WINDOW_EVENT_MASK);
529 /* we will receive events, stop grabbing server */
530 xcb_ungrab_server(globalconf.connection);
531 xcb_flush(globalconf.connection);
533 /* Parse and run configuration file */
534 if (!luaA_parserc(&xdg, confpath, true))
535 fatal("couldn't find any rc file");
537 p_delete(&confpath);
539 xdgWipeHandle(&xdg);
541 /* scan existing windows */
542 scan(tree_c);
544 xcb_flush(globalconf.connection);
546 /* Setup the main context */
547 g_main_context_set_poll_func(g_main_context_default(), &a_glib_poll);
548 gettimeofday(&last_wakeup, NULL);
550 /* main event loop */
551 globalconf.loop = g_main_loop_new(NULL, FALSE);
552 g_main_loop_run(globalconf.loop);
553 g_main_loop_unref(globalconf.loop);
554 globalconf.loop = NULL;
556 awesome_atexit(false);
558 return EXIT_SUCCESS;
561 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80