Add imlibsetroot setter to awsetbg script.
[awesome.git] / awesome.c
blob9327808ab6233304032702c6fced4af4a729b621
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 <xcb/bigreq.h>
30 #include <xcb/randr.h>
31 #include <xcb/xcb_event.h>
32 #include <xcb/xinerama.h>
33 #include <xcb/xtest.h>
35 #include "awesome.h"
36 #include "spawn.h"
37 #include "objects/client.h"
38 #include "xwindow.h"
39 #include "ewmh.h"
40 #include "dbus.h"
41 #include "systray.h"
42 #include "event.h"
43 #include "property.h"
44 #include "screen.h"
45 #include "luaa.h"
46 #include "common/version.h"
47 #include "common/atoms.h"
48 #include "common/xcursor.h"
49 #include "common/xutil.h"
50 #include "common/backtrace.h"
52 awesome_t globalconf;
54 /** argv used to run awesome */
55 static char *awesome_argv;
57 /** Call before exiting.
59 void
60 awesome_atexit(bool restart)
62 lua_pushboolean(globalconf.L, restart);
63 signal_object_emit(globalconf.L, &global_signals, "exit", 1);
65 a_dbus_cleanup();
67 systray_cleanup();
69 /* Close Lua */
70 lua_close(globalconf.L);
72 xcb_flush(globalconf.connection);
74 /* Disconnect *after* closing lua */
75 xcb_disconnect(globalconf.connection);
77 ev_default_destroy();
80 /** Scan X to find windows to manage.
82 static void
83 scan(void)
85 int i, tree_c_len;
86 xcb_query_tree_cookie_t tree_c;
87 xcb_query_tree_reply_t *tree_r;
88 xcb_window_t *wins = NULL;
89 xcb_get_window_attributes_reply_t *attr_r;
90 xcb_get_geometry_reply_t *geom_r;
91 long state;
93 /* Get the window tree associated to this screen */
94 tree_c = xcb_query_tree_unchecked(globalconf.connection,
95 globalconf.screen->root);
97 tree_r = xcb_query_tree_reply(globalconf.connection,
98 tree_c,
99 NULL);
101 if(!tree_r)
102 return;
104 /* Get the tree of the children windows of the current root window */
105 if(!(wins = xcb_query_tree_children(tree_r)))
106 fatal("cannot get tree children");
108 tree_c_len = xcb_query_tree_children_length(tree_r);
109 xcb_get_window_attributes_cookie_t attr_wins[tree_c_len];
110 xcb_get_property_cookie_t state_wins[tree_c_len];
112 for(i = 0; i < tree_c_len; i++)
114 attr_wins[i] = xcb_get_window_attributes_unchecked(globalconf.connection,
115 wins[i]);
117 state_wins[i] = xwindow_get_state_unchecked(wins[i]);
120 xcb_get_geometry_cookie_t *geom_wins[tree_c_len];
122 for(i = 0; i < tree_c_len; i++)
124 attr_r = xcb_get_window_attributes_reply(globalconf.connection,
125 attr_wins[i],
126 NULL);
128 state = xwindow_get_state_reply(state_wins[i]);
130 if(!attr_r || attr_r->override_redirect
131 || attr_r->map_state == XCB_MAP_STATE_UNMAPPED
132 || state == XCB_WM_STATE_WITHDRAWN)
134 geom_wins[i] = NULL;
135 p_delete(&attr_r);
136 continue;
139 p_delete(&attr_r);
141 /* Get the geometry of the current window */
142 geom_wins[i] = p_alloca(xcb_get_geometry_cookie_t, 1);
143 *(geom_wins[i]) = xcb_get_geometry_unchecked(globalconf.connection, wins[i]);
146 for(i = 0; i < tree_c_len; i++)
148 if(!geom_wins[i] || !(geom_r = xcb_get_geometry_reply(globalconf.connection,
149 *(geom_wins[i]), NULL)))
150 continue;
152 client_manage(wins[i], geom_r, true);
154 p_delete(&geom_r);
157 p_delete(&tree_r);
160 static xcb_visualtype_t *
161 a_default_visual(xcb_screen_t *s)
163 xcb_depth_iterator_t depth_iter = xcb_screen_allowed_depths_iterator(s);
165 if(depth_iter.data)
166 for(; depth_iter.rem; xcb_depth_next (&depth_iter))
167 for(xcb_visualtype_iterator_t visual_iter = xcb_depth_visuals_iterator(depth_iter.data);
168 visual_iter.rem; xcb_visualtype_next (&visual_iter))
169 if(s->root_visual == visual_iter.data->visual_id)
170 return visual_iter.data;
172 return NULL;
175 static xcb_visualtype_t *
176 a_argb_visual(xcb_screen_t *s)
178 xcb_depth_iterator_t depth_iter = xcb_screen_allowed_depths_iterator(s);
180 if(depth_iter.data)
181 for(; depth_iter.rem; xcb_depth_next (&depth_iter))
182 if(depth_iter.data->depth == 32)
183 for(xcb_visualtype_iterator_t visual_iter = xcb_depth_visuals_iterator(depth_iter.data);
184 visual_iter.rem; xcb_visualtype_next (&visual_iter))
185 return visual_iter.data;
187 return NULL;
190 static uint8_t
191 a_visual_depth(xcb_screen_t *s, xcb_visualid_t vis)
193 xcb_depth_iterator_t depth_iter = xcb_screen_allowed_depths_iterator(s);
195 if(depth_iter.data)
196 for(; depth_iter.rem; xcb_depth_next (&depth_iter))
197 for(xcb_visualtype_iterator_t visual_iter = xcb_depth_visuals_iterator(depth_iter.data);
198 visual_iter.rem; xcb_visualtype_next (&visual_iter))
199 if(vis == visual_iter.data->visual_id)
200 return depth_iter.data->depth;
202 fatal("Could not find a visual's depth");
205 static void
206 a_refresh_cb(EV_P_ ev_prepare *w, int revents)
208 awesome_refresh();
211 static void
212 a_xcb_check_cb(EV_P_ ev_check *w, int revents)
214 xcb_generic_event_t *mouse = NULL, *event;
216 while((event = xcb_poll_for_event(globalconf.connection)))
218 /* We will treat mouse events later.
219 * We cannot afford to treat all mouse motion events,
220 * because that would be too much CPU intensive, so we just
221 * take the last we get after a bunch of events. */
222 if(XCB_EVENT_RESPONSE_TYPE(event) == XCB_MOTION_NOTIFY)
224 p_delete(&mouse);
225 mouse = event;
227 else
229 uint8_t type = XCB_EVENT_RESPONSE_TYPE(event);
230 if((type == XCB_ENTER_NOTIFY || type == XCB_LEAVE_NOTIFY) && mouse)
232 /* Make sure enter/motion/leave events are handled in the
233 * correct order */
234 event_handle(mouse);
235 p_delete(&mouse);
236 mouse = NULL;
238 event_handle(event);
239 p_delete(&event);
243 if(mouse)
245 event_handle(mouse);
246 p_delete(&mouse);
250 static void
251 a_xcb_io_cb(EV_P_ ev_io *w, int revents)
253 /* a_xcb_check_cb() already handled all events */
255 if(xcb_connection_has_error(globalconf.connection))
256 fatal("X server connection broke");
259 static void
260 signal_fatal(int signum)
262 buffer_t buf;
263 backtrace_get(&buf);
264 fatal("dumping backtrace\n%s", buf.s);
267 /** Function to exit on some signals.
268 * \param w the signal received, unused
269 * \param revents currently unused
271 static void
272 exit_on_signal(EV_P_ ev_signal *w, int revents)
274 ev_unloop(EV_A_ 1);
277 void
278 awesome_restart(void)
280 awesome_atexit(true);
281 a_exec(awesome_argv);
284 /** Function to restart awesome on some signals.
285 * \param w the signal received, unused
286 * \param revents Currently unused
288 static void
289 restart_on_signal(EV_P_ ev_signal *w, int revents)
291 awesome_restart();
294 /** Print help and exit(2) with given exit_code.
295 * \param exit_code The exit code.
297 static void __attribute__ ((noreturn))
298 exit_help(int exit_code)
300 FILE *outfile = (exit_code == EXIT_SUCCESS) ? stdout : stderr;
301 fprintf(outfile,
302 "Usage: awesome [OPTION]\n\
303 -h, --help show help\n\
304 -v, --version show version\n\
305 -c, --config FILE configuration file to use\n\
306 -k, --check check configuration file syntax\n");
307 exit(exit_code);
310 /** Hello, this is main.
311 * \param argc Who knows.
312 * \param argv Who knows.
313 * \return EXIT_SUCCESS I hope.
316 main(int argc, char **argv)
318 char *confpath = NULL;
319 int xfd, i, opt;
320 ssize_t cmdlen = 1;
321 xdgHandle xdg;
322 bool no_argb = false;
323 xcb_generic_event_t *event;
324 static struct option long_options[] =
326 { "help", 0, NULL, 'h' },
327 { "version", 0, NULL, 'v' },
328 { "config", 1, NULL, 'c' },
329 { "check", 0, NULL, 'k' },
330 { "no-argb", 0, NULL, 'a' },
331 { NULL, 0, NULL, 0 }
334 /* event loop watchers */
335 ev_io xio = { .fd = -1 };
336 ev_check xcheck;
337 ev_prepare a_refresh;
338 ev_signal sigint;
339 ev_signal sigterm;
340 ev_signal sighup;
342 /* clear the globalconf structure */
343 p_clear(&globalconf, 1);
344 globalconf.keygrabber = LUA_REFNIL;
345 globalconf.mousegrabber = LUA_REFNIL;
347 /* save argv */
348 for(i = 0; i < argc; i++)
349 cmdlen += a_strlen(argv[i]) + 1;
351 awesome_argv = p_new(char, cmdlen);
352 a_strcpy(awesome_argv, cmdlen, argv[0]);
354 for(i = 1; i < argc; i++)
356 a_strcat(awesome_argv, cmdlen, " ");
357 a_strcat(awesome_argv, cmdlen, argv[i]);
360 /* Text won't be printed correctly otherwise */
361 setlocale(LC_CTYPE, "");
363 /* Get XDG basedir data */
364 xdgInitHandle(&xdg);
366 /* init lua */
367 luaA_init(&xdg);
369 /* check args */
370 while((opt = getopt_long(argc, argv, "vhkc:a",
371 long_options, NULL)) != -1)
372 switch(opt)
374 case 'v':
375 eprint_version();
376 break;
377 case 'h':
378 exit_help(EXIT_SUCCESS);
379 break;
380 case 'k':
381 if(!luaA_parserc(&xdg, confpath, false))
383 fprintf(stderr, "✘ Configuration file syntax error.\n");
384 return EXIT_FAILURE;
386 else
388 fprintf(stderr, "✔ Configuration file syntax OK.\n");
389 return EXIT_SUCCESS;
391 case 'c':
392 if(a_strlen(optarg))
393 confpath = a_strdup(optarg);
394 else
395 fatal("-c option requires a file name");
396 break;
397 case 'a':
398 no_argb = true;
399 break;
402 globalconf.loop = ev_default_loop(EVFLAG_NOSIGFD);
404 /* register function for signals */
405 ev_signal_init(&sigint, exit_on_signal, SIGINT);
406 ev_signal_init(&sigterm, exit_on_signal, SIGTERM);
407 ev_signal_init(&sighup, restart_on_signal, SIGHUP);
408 ev_signal_start(globalconf.loop, &sigint);
409 ev_signal_start(globalconf.loop, &sigterm);
410 ev_signal_start(globalconf.loop, &sighup);
411 ev_unref(globalconf.loop);
412 ev_unref(globalconf.loop);
413 ev_unref(globalconf.loop);
415 struct sigaction sa = { .sa_handler = signal_fatal, .sa_flags = 0 };
416 sigemptyset(&sa.sa_mask);
417 sigaction(SIGSEGV, &sa, 0);
419 /* X stuff */
420 globalconf.connection = xcb_connect(NULL, &globalconf.default_screen);
421 if(xcb_connection_has_error(globalconf.connection))
422 fatal("cannot open display");
424 globalconf.screen = xcb_aux_get_screen(globalconf.connection, globalconf.default_screen);
425 /* FIXME The following two assignments were swapped on purpose */
426 if(!no_argb)
427 globalconf.visual = a_default_visual(globalconf.screen);
428 if(!globalconf.visual)
429 globalconf.visual = a_argb_visual(globalconf.screen);
430 globalconf.default_depth = a_visual_depth(globalconf.screen, globalconf.visual->visual_id);
431 globalconf.default_cmap = globalconf.screen->default_colormap;
432 if(globalconf.default_depth != globalconf.screen->root_depth)
434 // We need our own color map if we aren't using the default depth
435 globalconf.default_cmap = xcb_generate_id(globalconf.connection);
436 xcb_create_colormap(globalconf.connection, XCB_COLORMAP_ALLOC_NONE,
437 globalconf.default_cmap, globalconf.screen->root,
438 globalconf.visual->visual_id);
441 /* Prefetch all the extensions we might need */
442 xcb_prefetch_extension_data(globalconf.connection, &xcb_big_requests_id);
443 xcb_prefetch_extension_data(globalconf.connection, &xcb_test_id);
444 xcb_prefetch_extension_data(globalconf.connection, &xcb_randr_id);
445 xcb_prefetch_extension_data(globalconf.connection, &xcb_xinerama_id);
447 /* initialize dbus */
448 a_dbus_init();
450 /* Get the file descriptor corresponding to the X connection */
451 xfd = xcb_get_file_descriptor(globalconf.connection);
452 ev_io_init(&xio, &a_xcb_io_cb, xfd, EV_READ);
453 ev_io_start(globalconf.loop, &xio);
454 ev_check_init(&xcheck, &a_xcb_check_cb);
455 ev_check_start(globalconf.loop, &xcheck);
456 ev_unref(globalconf.loop);
457 ev_prepare_init(&a_refresh, &a_refresh_cb);
458 ev_prepare_start(globalconf.loop, &a_refresh);
459 ev_unref(globalconf.loop);
461 /* Grab server */
462 xcb_grab_server(globalconf.connection);
464 /* Make sure there are no pending events. Since we didn't really do anything
465 * at all yet, we will just discard all events which we received so far.
466 * The above GrabServer should make sure no new events are generated. */
467 xcb_aux_sync(globalconf.connection);
468 while ((event = xcb_poll_for_event(globalconf.connection)) != NULL)
470 /* Make sure errors are printed */
471 uint8_t response_type = XCB_EVENT_RESPONSE_TYPE(event);
472 if(response_type == 0)
473 event_handle(event);
474 p_delete(&event);
478 const uint32_t select_input_val = XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT;
480 /* This causes an error if some other window manager is running */
481 xcb_change_window_attributes(globalconf.connection,
482 globalconf.screen->root,
483 XCB_CW_EVENT_MASK, &select_input_val);
486 /* Need to xcb_flush to validate error handler */
487 xcb_aux_sync(globalconf.connection);
489 /* Process all errors in the queue if any. There can be no events yet, so if
490 * this function returns something, it must be an error. */
491 if (xcb_poll_for_event(globalconf.connection) != NULL)
492 fatal("another window manager is already running");
494 /* Prefetch the maximum request length */
495 xcb_prefetch_maximum_request_length(globalconf.connection);
497 /* check for xtest extension */
498 const xcb_query_extension_reply_t *xtest_query;
499 xtest_query = xcb_get_extension_data(globalconf.connection, &xcb_test_id);
500 globalconf.have_xtest = xtest_query->present;
502 /* Allocate the key symbols */
503 globalconf.keysyms = xcb_key_symbols_alloc(globalconf.connection);
504 xcb_get_modifier_mapping_cookie_t xmapping_cookie =
505 xcb_get_modifier_mapping_unchecked(globalconf.connection);
507 /* init atom cache */
508 atoms_init(globalconf.connection);
510 /* init screens information */
511 screen_scan();
513 xutil_lock_mask_get(globalconf.connection, xmapping_cookie,
514 globalconf.keysyms, &globalconf.numlockmask,
515 &globalconf.shiftlockmask, &globalconf.capslockmask,
516 &globalconf.modeswitchmask);
518 /* do this only for real screen */
519 ewmh_init();
520 systray_init();
522 /* init spawn (sn) */
523 spawn_init();
525 /* The default GC is just a newly created associated with a window with
526 * depth globalconf.default_depth */
527 xcb_window_t tmp_win = xcb_generate_id(globalconf.connection);
528 globalconf.gc = xcb_generate_id(globalconf.connection);
529 xcb_create_window(globalconf.connection, globalconf.default_depth,
530 tmp_win, globalconf.screen->root,
531 -1, -1, 1, 1, 0,
532 XCB_COPY_FROM_PARENT, globalconf.visual->visual_id,
533 XCB_CW_BACK_PIXEL | XCB_CW_BORDER_PIXEL | XCB_CW_COLORMAP,
534 (const uint32_t [])
536 globalconf.screen->black_pixel,
537 globalconf.screen->black_pixel,
538 globalconf.default_cmap
540 xcb_create_gc(globalconf.connection, globalconf.gc, tmp_win, XCB_GC_FOREGROUND | XCB_GC_BACKGROUND,
541 (const uint32_t[]) { globalconf.screen->black_pixel, globalconf.screen->white_pixel });
542 xcb_destroy_window(globalconf.connection, tmp_win);
544 /* Parse and run configuration file */
545 if (!luaA_parserc(&xdg, confpath, true))
546 fatal("couldn't find any rc file");
548 p_delete(&confpath);
550 xdgWipeHandle(&xdg);
552 /* scan existing windows */
553 scan();
556 /* select for events */
557 const uint32_t change_win_vals[] =
559 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT | XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY
560 | XCB_EVENT_MASK_ENTER_WINDOW | XCB_EVENT_MASK_LEAVE_WINDOW
561 | XCB_EVENT_MASK_STRUCTURE_NOTIFY
562 | XCB_EVENT_MASK_BUTTON_PRESS
563 | XCB_EVENT_MASK_BUTTON_RELEASE
564 | XCB_EVENT_MASK_FOCUS_CHANGE
567 xcb_change_window_attributes(globalconf.connection,
568 globalconf.screen->root,
569 XCB_CW_EVENT_MASK,
570 change_win_vals);
573 /* we will receive events, stop grabbing server */
574 xcb_ungrab_server(globalconf.connection);
575 xcb_flush(globalconf.connection);
577 /* main event loop */
578 ev_loop(globalconf.loop, 0);
580 /* cleanup event loop */
581 ev_ref(globalconf.loop);
582 ev_check_stop(globalconf.loop, &xcheck);
583 ev_ref(globalconf.loop);
584 ev_prepare_stop(globalconf.loop, &a_refresh);
585 ev_ref(globalconf.loop);
586 ev_io_stop(globalconf.loop, &xio);
588 awesome_atexit(false);
590 return EXIT_SUCCESS;
593 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80