beautiful: Track the font for oopango
[awesome.git] / awesome.c
bloba4be417f439cd980e2522934c0d60557f179d8ca
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(void)
62 signal_object_emit(globalconf.L, &global_signals, "exit", 0);
64 a_dbus_cleanup();
66 systray_cleanup();
68 /* Close Lua */
69 lua_close(globalconf.L);
71 xcb_flush(globalconf.connection);
73 /* Disconnect *after* closing lua */
74 xcb_disconnect(globalconf.connection);
76 ev_default_destroy();
79 /** Scan X to find windows to manage.
81 static void
82 scan(void)
84 int i, tree_c_len;
85 xcb_query_tree_cookie_t tree_c;
86 xcb_query_tree_reply_t *tree_r;
87 xcb_window_t *wins = NULL;
88 xcb_get_window_attributes_reply_t *attr_r;
89 xcb_get_geometry_reply_t *geom_r;
90 long state;
92 /* Get the window tree associated to this screen */
93 tree_c = xcb_query_tree_unchecked(globalconf.connection,
94 globalconf.screen->root);
96 tree_r = xcb_query_tree_reply(globalconf.connection,
97 tree_c,
98 NULL);
100 if(!tree_r)
101 return;
103 /* Get the tree of the children windows of the current root window */
104 if(!(wins = xcb_query_tree_children(tree_r)))
105 fatal("cannot get tree children");
107 tree_c_len = xcb_query_tree_children_length(tree_r);
108 xcb_get_window_attributes_cookie_t attr_wins[tree_c_len];
109 xcb_get_property_cookie_t state_wins[tree_c_len];
111 for(i = 0; i < tree_c_len; i++)
113 attr_wins[i] = xcb_get_window_attributes_unchecked(globalconf.connection,
114 wins[i]);
116 state_wins[i] = xwindow_get_state_unchecked(wins[i]);
119 xcb_get_geometry_cookie_t *geom_wins[tree_c_len];
121 for(i = 0; i < tree_c_len; i++)
123 attr_r = xcb_get_window_attributes_reply(globalconf.connection,
124 attr_wins[i],
125 NULL);
127 state = xwindow_get_state_reply(state_wins[i]);
129 if(!attr_r || attr_r->override_redirect
130 || attr_r->map_state == XCB_MAP_STATE_UNMAPPED
131 || state == XCB_WM_STATE_WITHDRAWN)
133 geom_wins[i] = NULL;
134 p_delete(&attr_r);
135 continue;
138 p_delete(&attr_r);
140 /* Get the geometry of the current window */
141 geom_wins[i] = p_alloca(xcb_get_geometry_cookie_t, 1);
142 *(geom_wins[i]) = xcb_get_geometry_unchecked(globalconf.connection, wins[i]);
145 for(i = 0; i < tree_c_len; i++)
147 if(!geom_wins[i] || !(geom_r = xcb_get_geometry_reply(globalconf.connection,
148 *(geom_wins[i]), NULL)))
149 continue;
151 client_manage(wins[i], geom_r, true);
153 p_delete(&geom_r);
156 p_delete(&tree_r);
159 static xcb_visualtype_t *
160 a_default_visual(xcb_screen_t *s)
162 xcb_depth_iterator_t depth_iter = xcb_screen_allowed_depths_iterator(s);
164 if(depth_iter.data)
165 for(; depth_iter.rem; xcb_depth_next (&depth_iter))
166 for(xcb_visualtype_iterator_t visual_iter = xcb_depth_visuals_iterator(depth_iter.data);
167 visual_iter.rem; xcb_visualtype_next (&visual_iter))
168 if(s->root_visual == visual_iter.data->visual_id)
169 return visual_iter.data;
171 return NULL;
174 static xcb_visualtype_t *
175 a_argb_visual(xcb_screen_t *s)
177 xcb_depth_iterator_t depth_iter = xcb_screen_allowed_depths_iterator(s);
179 if(depth_iter.data)
180 for(; depth_iter.rem; xcb_depth_next (&depth_iter))
181 if(depth_iter.data->depth == 32)
182 for(xcb_visualtype_iterator_t visual_iter = xcb_depth_visuals_iterator(depth_iter.data);
183 visual_iter.rem; xcb_visualtype_next (&visual_iter))
184 return visual_iter.data;
186 return NULL;
189 static uint8_t
190 a_visual_depth(xcb_screen_t *s, xcb_visualid_t vis)
192 xcb_depth_iterator_t depth_iter = xcb_screen_allowed_depths_iterator(s);
194 if(depth_iter.data)
195 for(; depth_iter.rem; xcb_depth_next (&depth_iter))
196 for(xcb_visualtype_iterator_t visual_iter = xcb_depth_visuals_iterator(depth_iter.data);
197 visual_iter.rem; xcb_visualtype_next (&visual_iter))
198 if(vis == visual_iter.data->visual_id)
199 return depth_iter.data->depth;
201 fatal("Could not find a visual's depth");
204 static void
205 a_refresh_cb(EV_P_ ev_prepare *w, int revents)
207 awesome_refresh();
210 static void
211 a_xcb_check_cb(EV_P_ ev_check *w, int revents)
213 xcb_generic_event_t *mouse = NULL, *event;
215 while((event = xcb_poll_for_event(globalconf.connection)))
217 /* We will treat mouse events later.
218 * We cannot afford to treat all mouse motion events,
219 * because that would be too much CPU intensive, so we just
220 * take the last we get after a bunch of events. */
221 if(XCB_EVENT_RESPONSE_TYPE(event) == XCB_MOTION_NOTIFY)
223 p_delete(&mouse);
224 mouse = event;
226 else
228 event_handle(event);
229 p_delete(&event);
233 if(mouse)
235 event_handle(mouse);
236 p_delete(&mouse);
240 static void
241 a_xcb_io_cb(EV_P_ ev_io *w, int revents)
243 /* empty */
246 static void
247 signal_fatal(int signum)
249 buffer_t buf;
250 backtrace_get(&buf);
251 fatal("dumping backtrace\n%s", buf.s);
254 /** Function to exit on some signals.
255 * \param w the signal received, unused
256 * \param revents currently unused
258 static void
259 exit_on_signal(EV_P_ ev_signal *w, int revents)
261 ev_unloop(EV_A_ 1);
264 void
265 awesome_restart(void)
267 awesome_atexit();
268 a_exec(awesome_argv);
271 /** Function to restart awesome on some signals.
272 * \param w the signal received, unused
273 * \param revents Currently unused
275 static void
276 restart_on_signal(EV_P_ ev_signal *w, int revents)
278 awesome_restart();
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 char *confpath = NULL;
306 int xfd, i, opt, colors_nbr;
307 xcolor_init_request_t colors_reqs[2];
308 ssize_t cmdlen = 1;
309 xdgHandle xdg;
310 bool no_argb = false;
311 static struct option long_options[] =
313 { "help", 0, NULL, 'h' },
314 { "version", 0, NULL, 'v' },
315 { "config", 1, NULL, 'c' },
316 { "check", 0, NULL, 'k' },
317 { "no-argb", 0, NULL, 'a' },
318 { NULL, 0, NULL, 0 }
321 /* event loop watchers */
322 ev_io xio = { .fd = -1 };
323 ev_check xcheck;
324 ev_prepare a_refresh;
325 ev_signal sigint;
326 ev_signal sigterm;
327 ev_signal sighup;
329 /* clear the globalconf structure */
330 p_clear(&globalconf, 1);
331 globalconf.keygrabber = LUA_REFNIL;
332 globalconf.mousegrabber = LUA_REFNIL;
334 /* save argv */
335 for(i = 0; i < argc; i++)
336 cmdlen += a_strlen(argv[i]) + 1;
338 awesome_argv = p_new(char, cmdlen);
339 a_strcpy(awesome_argv, cmdlen, argv[0]);
341 for(i = 1; i < argc; i++)
343 a_strcat(awesome_argv, cmdlen, " ");
344 a_strcat(awesome_argv, cmdlen, argv[i]);
347 /* Text won't be printed correctly otherwise */
348 setlocale(LC_CTYPE, "");
350 /* Get XDG basedir data */
351 xdgInitHandle(&xdg);
353 /* init lua */
354 luaA_init(&xdg);
356 /* check args */
357 while((opt = getopt_long(argc, argv, "vhkc:",
358 long_options, NULL)) != -1)
359 switch(opt)
361 case 'v':
362 eprint_version();
363 break;
364 case 'h':
365 exit_help(EXIT_SUCCESS);
366 break;
367 case 'k':
368 if(!luaA_parserc(&xdg, confpath, false))
370 fprintf(stderr, "✘ Configuration file syntax error.\n");
371 return EXIT_FAILURE;
373 else
375 fprintf(stderr, "✔ Configuration file syntax OK.\n");
376 return EXIT_SUCCESS;
378 case 'c':
379 if(a_strlen(optarg))
380 confpath = a_strdup(optarg);
381 else
382 fatal("-c option requires a file name");
383 break;
384 case 'a':
385 no_argb = true;
386 break;
389 globalconf.loop = ev_default_loop(EVFLAG_NOSIGFD);
391 /* register function for signals */
392 ev_signal_init(&sigint, exit_on_signal, SIGINT);
393 ev_signal_init(&sigterm, exit_on_signal, SIGTERM);
394 ev_signal_init(&sighup, restart_on_signal, SIGHUP);
395 ev_signal_start(globalconf.loop, &sigint);
396 ev_signal_start(globalconf.loop, &sigterm);
397 ev_signal_start(globalconf.loop, &sighup);
398 ev_unref(globalconf.loop);
399 ev_unref(globalconf.loop);
400 ev_unref(globalconf.loop);
402 struct sigaction sa = { .sa_handler = signal_fatal, .sa_flags = 0 };
403 sigemptyset(&sa.sa_mask);
404 sigaction(SIGSEGV, &sa, 0);
406 /* X stuff */
407 globalconf.connection = xcb_connect(NULL, &globalconf.default_screen);
408 if(xcb_connection_has_error(globalconf.connection))
409 fatal("cannot open display");
411 globalconf.screen = xcb_aux_get_screen(globalconf.connection, globalconf.default_screen);
412 if(!no_argb)
413 globalconf.visual = a_argb_visual(globalconf.screen);
414 if(!globalconf.visual)
415 globalconf.visual = a_default_visual(globalconf.screen);
416 globalconf.default_depth = a_visual_depth(globalconf.screen, globalconf.visual->visual_id);
417 globalconf.default_cmap = globalconf.screen->default_colormap;
418 if(globalconf.default_depth != globalconf.screen->root_depth)
420 // We need our own color map if we aren't using the default depth
421 globalconf.default_cmap = xcb_generate_id(globalconf.connection);
422 xcb_create_colormap(globalconf.connection, XCB_COLORMAP_ALLOC_NONE,
423 globalconf.default_cmap, globalconf.screen->root,
424 globalconf.visual->visual_id);
427 /* Prefetch all the extensions we might need */
428 xcb_prefetch_extension_data(globalconf.connection, &xcb_big_requests_id);
429 xcb_prefetch_extension_data(globalconf.connection, &xcb_test_id);
430 xcb_prefetch_extension_data(globalconf.connection, &xcb_randr_id);
431 xcb_prefetch_extension_data(globalconf.connection, &xcb_xinerama_id);
433 /* initialize dbus */
434 a_dbus_init();
436 /* Grab server */
437 xcb_grab_server(globalconf.connection);
438 xcb_flush(globalconf.connection);
440 /* Get the file descriptor corresponding to the X connection */
441 xfd = xcb_get_file_descriptor(globalconf.connection);
442 ev_io_init(&xio, &a_xcb_io_cb, xfd, EV_READ);
443 ev_io_start(globalconf.loop, &xio);
444 ev_check_init(&xcheck, &a_xcb_check_cb);
445 ev_check_start(globalconf.loop, &xcheck);
446 ev_unref(globalconf.loop);
447 ev_prepare_init(&a_refresh, &a_refresh_cb);
448 ev_prepare_start(globalconf.loop, &a_refresh);
449 ev_unref(globalconf.loop);
452 const uint32_t select_input_val = XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT;
454 /* This causes an error if some other window manager is running */
455 xcb_change_window_attributes(globalconf.connection,
456 globalconf.screen->root,
457 XCB_CW_EVENT_MASK, &select_input_val);
460 /* Need to xcb_flush to validate error handler */
461 xcb_aux_sync(globalconf.connection);
463 /* Process all errors in the queue if any. There can be no events yet, so if
464 * this function returns something, it must be an error. */
465 if (xcb_poll_for_event(globalconf.connection) != NULL)
466 fatal("another window manager is already running");
468 /* Prefetch the maximum request length */
469 xcb_prefetch_maximum_request_length(globalconf.connection);
471 /* check for xtest extension */
472 const xcb_query_extension_reply_t *xtest_query;
473 xtest_query = xcb_get_extension_data(globalconf.connection, &xcb_test_id);
474 globalconf.have_xtest = xtest_query->present;
476 /* Allocate the key symbols */
477 globalconf.keysyms = xcb_key_symbols_alloc(globalconf.connection);
478 xcb_get_modifier_mapping_cookie_t xmapping_cookie =
479 xcb_get_modifier_mapping_unchecked(globalconf.connection);
481 /* init atom cache */
482 atoms_init(globalconf.connection);
484 /* init screens information */
485 screen_scan();
487 /* init default font and colors */
488 colors_reqs[0] = xcolor_init_unchecked(&globalconf.colors.fg,
489 "#000000", sizeof("#000000") - 1);
491 colors_reqs[1] = xcolor_init_unchecked(&globalconf.colors.bg,
492 "#ffffff", sizeof("#ffffff") - 1);
494 globalconf.font = font_new("sans 8");
496 for(colors_nbr = 0; colors_nbr < 2; colors_nbr++)
497 xcolor_init_reply(colors_reqs[colors_nbr]);
499 xutil_lock_mask_get(globalconf.connection, xmapping_cookie,
500 globalconf.keysyms, &globalconf.numlockmask,
501 &globalconf.shiftlockmask, &globalconf.capslockmask,
502 &globalconf.modeswitchmask);
504 /* do this only for real screen */
505 ewmh_init();
506 systray_init();
508 /* init spawn (sn) */
509 spawn_init();
511 /* The default GC is just a newly created associated with a window with
512 * depth globalconf.default_depth */
513 xcb_window_t tmp_win = xcb_generate_id(globalconf.connection);
514 globalconf.gc = xcb_generate_id(globalconf.connection);
515 xcb_create_window(globalconf.connection, globalconf.default_depth,
516 tmp_win, globalconf.screen->root,
517 -1, -1, 1, 1, 0,
518 XCB_COPY_FROM_PARENT, globalconf.visual->visual_id,
519 XCB_CW_BACK_PIXEL | XCB_CW_BORDER_PIXEL | XCB_CW_COLORMAP,
520 (const uint32_t [])
522 globalconf.colors.bg.pixel,
523 globalconf.colors.bg.pixel,
524 globalconf.default_cmap
526 xcb_create_gc(globalconf.connection, globalconf.gc, tmp_win, XCB_GC_FOREGROUND | XCB_GC_BACKGROUND,
527 (const uint32_t[]) { globalconf.screen->black_pixel, globalconf.screen->white_pixel });
528 xcb_destroy_window(globalconf.connection, tmp_win);
530 /* Parse and run configuration file */
531 if (!luaA_parserc(&xdg, confpath, true))
532 fatal("couldn't find any rc file");
534 p_delete(&confpath);
536 xdgWipeHandle(&xdg);
538 /* scan existing windows */
539 scan();
542 /* select for events */
543 const uint32_t change_win_vals[] =
545 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT | XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY
546 | XCB_EVENT_MASK_ENTER_WINDOW | XCB_EVENT_MASK_LEAVE_WINDOW
547 | XCB_EVENT_MASK_STRUCTURE_NOTIFY
548 | XCB_EVENT_MASK_PROPERTY_CHANGE
549 | XCB_EVENT_MASK_BUTTON_PRESS
550 | XCB_EVENT_MASK_BUTTON_RELEASE
551 | XCB_EVENT_MASK_FOCUS_CHANGE
554 xcb_change_window_attributes(globalconf.connection,
555 globalconf.screen->root,
556 XCB_CW_EVENT_MASK,
557 change_win_vals);
560 /* we will receive events, stop grabbing server */
561 xcb_ungrab_server(globalconf.connection);
562 xcb_flush(globalconf.connection);
564 /* main event loop */
565 ev_loop(globalconf.loop, 0);
567 /* cleanup event loop */
568 ev_ref(globalconf.loop);
569 ev_check_stop(globalconf.loop, &xcheck);
570 ev_ref(globalconf.loop);
571 ev_prepare_stop(globalconf.loop, &a_refresh);
572 ev_ref(globalconf.loop);
573 ev_io_stop(globalconf.loop, &xio);
575 awesome_atexit();
577 return EXIT_SUCCESS;
580 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80