awful.mouse: Follow C API changes
[awesome.git] / awesome.c
blob4755d1c4ea590816de860dcba79aea46e93388f3
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>
34 #include <xcb/shape.h>
36 #include <X11/Xlib-xcb.h>
37 #include <X11/XKBlib.h>
39 #include <glib-unix.h>
41 #include "awesome.h"
42 #include "spawn.h"
43 #include "objects/client.h"
44 #include "xwindow.h"
45 #include "ewmh.h"
46 #include "dbus.h"
47 #include "systray.h"
48 #include "event.h"
49 #include "property.h"
50 #include "screen.h"
51 #include "luaa.h"
52 #include "common/version.h"
53 #include "common/atoms.h"
54 #include "common/xcursor.h"
55 #include "common/xutil.h"
56 #include "common/backtrace.h"
58 awesome_t globalconf;
60 /** argv used to run awesome */
61 static char *awesome_argv;
63 /** Call before exiting.
65 void
66 awesome_atexit(bool restart)
68 lua_pushboolean(globalconf.L, restart);
69 signal_object_emit(globalconf.L, &global_signals, "exit", 1);
71 a_dbus_cleanup();
73 systray_cleanup();
75 /* Close Lua */
76 lua_close(globalconf.L);
78 /* X11 is a great protocol. There is a save-set so that reparenting WMs
79 * don't kill clients when they shut down. However, when a focused windows
80 * is saved, the focus will move to its parent with revert-to none.
81 * Immediately afterwards, this parent is destroyed and the focus is gone.
82 * Work around this by placing the focus where we like it to be.
84 xcb_set_input_focus(globalconf.connection, XCB_INPUT_FOCUS_POINTER_ROOT,
85 XCB_NONE, XCB_CURRENT_TIME);
86 xcb_aux_sync(globalconf.connection);
88 /* Disconnect *after* closing lua */
89 xcb_disconnect(globalconf.connection);
92 /** Scan X to find windows to manage.
94 static void
95 scan(xcb_query_tree_cookie_t tree_c)
97 int i, tree_c_len;
98 xcb_query_tree_reply_t *tree_r;
99 xcb_window_t *wins = NULL;
100 xcb_get_window_attributes_reply_t *attr_r;
101 xcb_get_geometry_reply_t *geom_r;
102 long state;
104 tree_r = xcb_query_tree_reply(globalconf.connection,
105 tree_c,
106 NULL);
108 if(!tree_r)
109 return;
111 /* Get the tree of the children windows of the current root window */
112 if(!(wins = xcb_query_tree_children(tree_r)))
113 fatal("cannot get tree children");
115 tree_c_len = xcb_query_tree_children_length(tree_r);
116 xcb_get_window_attributes_cookie_t attr_wins[tree_c_len];
117 xcb_get_property_cookie_t state_wins[tree_c_len];
119 for(i = 0; i < tree_c_len; i++)
121 attr_wins[i] = xcb_get_window_attributes_unchecked(globalconf.connection,
122 wins[i]);
124 state_wins[i] = xwindow_get_state_unchecked(wins[i]);
127 xcb_get_geometry_cookie_t *geom_wins[tree_c_len];
129 for(i = 0; i < tree_c_len; i++)
131 attr_r = xcb_get_window_attributes_reply(globalconf.connection,
132 attr_wins[i],
133 NULL);
135 state = xwindow_get_state_reply(state_wins[i]);
137 if(!attr_r || attr_r->override_redirect
138 || attr_r->map_state == XCB_MAP_STATE_UNMAPPED
139 || state == XCB_ICCCM_WM_STATE_WITHDRAWN)
141 geom_wins[i] = NULL;
142 p_delete(&attr_r);
143 continue;
146 p_delete(&attr_r);
148 /* Get the geometry of the current window */
149 geom_wins[i] = p_alloca(xcb_get_geometry_cookie_t, 1);
150 *(geom_wins[i]) = xcb_get_geometry_unchecked(globalconf.connection, wins[i]);
153 for(i = 0; i < tree_c_len; i++)
155 if(!geom_wins[i] || !(geom_r = xcb_get_geometry_reply(globalconf.connection,
156 *(geom_wins[i]), NULL)))
157 continue;
159 client_manage(wins[i], geom_r, true);
161 p_delete(&geom_r);
164 p_delete(&tree_r);
167 static void
168 a_xcb_check(void)
170 xcb_generic_event_t *mouse = NULL, *event;
172 while((event = xcb_poll_for_event(globalconf.connection)))
174 /* We will treat mouse events later.
175 * We cannot afford to treat all mouse motion events,
176 * because that would be too much CPU intensive, so we just
177 * take the last we get after a bunch of events. */
178 if(XCB_EVENT_RESPONSE_TYPE(event) == XCB_MOTION_NOTIFY)
180 p_delete(&mouse);
181 mouse = event;
183 else
185 uint8_t type = XCB_EVENT_RESPONSE_TYPE(event);
186 if((type == XCB_ENTER_NOTIFY || type == XCB_LEAVE_NOTIFY) && mouse)
188 /* Make sure enter/motion/leave events are handled in the
189 * correct order */
190 event_handle(mouse);
191 p_delete(&mouse);
192 mouse = NULL;
194 event_handle(event);
195 p_delete(&event);
199 if(mouse)
201 event_handle(mouse);
202 p_delete(&mouse);
206 static gboolean
207 a_xcb_io_cb(GIOChannel *source, GIOCondition cond, gpointer data)
209 /* a_xcb_check() already handled all events */
211 if(xcb_connection_has_error(globalconf.connection))
212 fatal("X server connection broke");
214 return TRUE;
217 static gint
218 a_glib_poll(GPollFD *ufds, guint nfsd, gint timeout)
220 guint res;
221 awesome_refresh();
222 res = g_poll(ufds, nfsd, timeout);
223 a_xcb_check();
224 return res;
227 static void
228 signal_fatal(int signum)
230 buffer_t buf;
231 backtrace_get(&buf);
232 fatal("signal %d, dumping backtrace\n%s", signum, buf.s);
235 /** Function to exit on some signals.
236 * \param data currently unused
238 static gboolean
239 exit_on_signal(gpointer data)
241 g_main_loop_quit(globalconf.loop);
242 return TRUE;
245 void
246 awesome_restart(void)
248 awesome_atexit(true);
249 a_exec(awesome_argv);
252 /** Function to restart awesome on some signals.
253 * \param data currently unused
255 static gboolean
256 restart_on_signal(gpointer data)
258 awesome_restart();
259 return TRUE;
262 /** Print help and exit(2) with given exit_code.
263 * \param exit_code The exit code.
265 static void __attribute__ ((noreturn))
266 exit_help(int exit_code)
268 FILE *outfile = (exit_code == EXIT_SUCCESS) ? stdout : stderr;
269 fprintf(outfile,
270 "Usage: awesome [OPTION]\n\
271 -h, --help show help\n\
272 -v, --version show version\n\
273 -c, --config FILE configuration file to use\n\
274 -k, --check check configuration file syntax\n");
275 exit(exit_code);
278 /** Hello, this is main.
279 * \param argc Who knows.
280 * \param argv Who knows.
281 * \return EXIT_SUCCESS I hope.
284 main(int argc, char **argv)
286 char *confpath = NULL;
287 int xfd, i, opt;
288 ssize_t cmdlen = 1;
289 xdgHandle xdg;
290 bool no_argb = false;
291 xcb_generic_event_t *event;
292 xcb_query_tree_cookie_t tree_c;
293 static struct option long_options[] =
295 { "help", 0, NULL, 'h' },
296 { "version", 0, NULL, 'v' },
297 { "config", 1, NULL, 'c' },
298 { "check", 0, NULL, 'k' },
299 { "no-argb", 0, NULL, 'a' },
300 { NULL, 0, NULL, 0 }
303 /* clear the globalconf structure */
304 p_clear(&globalconf, 1);
305 globalconf.keygrabber = LUA_REFNIL;
306 globalconf.mousegrabber = LUA_REFNIL;
307 buffer_init(&globalconf.startup_errors);
309 /* save argv */
310 for(i = 0; i < argc; i++)
311 cmdlen += a_strlen(argv[i]) + 1;
313 awesome_argv = p_new(char, cmdlen);
314 a_strcpy(awesome_argv, cmdlen, argv[0]);
316 for(i = 1; i < argc; i++)
318 a_strcat(awesome_argv, cmdlen, " ");
319 a_strcat(awesome_argv, cmdlen, argv[i]);
322 /* Text won't be printed correctly otherwise */
323 setlocale(LC_CTYPE, "");
325 /* Get XDG basedir data */
326 xdgInitHandle(&xdg);
328 /* init lua */
329 luaA_init(&xdg);
331 /* check args */
332 while((opt = getopt_long(argc, argv, "vhkc:a",
333 long_options, NULL)) != -1)
334 switch(opt)
336 case 'v':
337 eprint_version();
338 break;
339 case 'h':
340 exit_help(EXIT_SUCCESS);
341 break;
342 case 'k':
343 if(!luaA_parserc(&xdg, confpath, false))
345 fprintf(stderr, "✘ Configuration file syntax error.\n");
346 return EXIT_FAILURE;
348 else
350 fprintf(stderr, "✔ Configuration file syntax OK.\n");
351 return EXIT_SUCCESS;
353 case 'c':
354 if(a_strlen(optarg))
355 confpath = a_strdup(optarg);
356 else
357 fatal("-c option requires a file name");
358 break;
359 case 'a':
360 no_argb = true;
361 break;
364 /* register function for signals */
365 g_unix_signal_add(SIGINT, exit_on_signal, NULL);
366 g_unix_signal_add(SIGTERM, exit_on_signal, NULL);
367 g_unix_signal_add(SIGHUP, restart_on_signal, NULL);
369 struct sigaction sa = { .sa_handler = signal_fatal, .sa_flags = 0 };
370 sigemptyset(&sa.sa_mask);
371 sigaction(SIGSEGV, &sa, 0);
373 /* XLib sucks */
374 XkbIgnoreExtension(True);
376 /* X stuff */
377 globalconf.display = XOpenDisplay(NULL);
378 if (globalconf.display == NULL) {
379 fatal("cannot open display");
381 XSetEventQueueOwner(globalconf.display, XCBOwnsEventQueue);
382 globalconf.default_screen = XDefaultScreen(globalconf.display);
383 globalconf.connection = XGetXCBConnection(globalconf.display);
384 /* We have no clue where the input focus is right now */
385 globalconf.focus.need_update = true;
387 /* Double checking that connection is good and operatable with xcb */
388 if(xcb_connection_has_error(globalconf.connection))
389 fatal("cannot open display");
391 globalconf.screen = xcb_aux_get_screen(globalconf.connection, globalconf.default_screen);
392 globalconf.default_visual = draw_default_visual(globalconf.screen);
393 if(!no_argb)
394 globalconf.visual = draw_argb_visual(globalconf.screen);
395 if(!globalconf.visual)
396 globalconf.visual = globalconf.default_visual;
397 globalconf.default_depth = draw_visual_depth(globalconf.screen, globalconf.visual->visual_id);
398 globalconf.default_cmap = globalconf.screen->default_colormap;
399 if(globalconf.default_depth != globalconf.screen->root_depth)
401 // We need our own color map if we aren't using the default depth
402 globalconf.default_cmap = xcb_generate_id(globalconf.connection);
403 xcb_create_colormap(globalconf.connection, XCB_COLORMAP_ALLOC_NONE,
404 globalconf.default_cmap, globalconf.screen->root,
405 globalconf.visual->visual_id);
408 /* Prefetch all the extensions we might need */
409 xcb_prefetch_extension_data(globalconf.connection, &xcb_big_requests_id);
410 xcb_prefetch_extension_data(globalconf.connection, &xcb_test_id);
411 xcb_prefetch_extension_data(globalconf.connection, &xcb_randr_id);
412 xcb_prefetch_extension_data(globalconf.connection, &xcb_xinerama_id);
413 xcb_prefetch_extension_data(globalconf.connection, &xcb_shape_id);
415 /* Setup the main context */
416 g_main_context_set_poll_func(g_main_context_default(), &a_glib_poll);
418 /* initialize dbus */
419 a_dbus_init();
421 /* Get the file descriptor corresponding to the X connection */
422 xfd = xcb_get_file_descriptor(globalconf.connection);
423 GIOChannel *channel = g_io_channel_unix_new(xfd);
424 g_io_add_watch(channel, G_IO_IN, a_xcb_io_cb, NULL);
425 g_io_channel_unref(channel);
427 /* Grab server */
428 xcb_grab_server(globalconf.connection);
430 /* Make sure there are no pending events. Since we didn't really do anything
431 * at all yet, we will just discard all events which we received so far.
432 * The above GrabServer should make sure no new events are generated. */
433 xcb_aux_sync(globalconf.connection);
434 while ((event = xcb_poll_for_event(globalconf.connection)) != NULL)
436 /* Make sure errors are printed */
437 uint8_t response_type = XCB_EVENT_RESPONSE_TYPE(event);
438 if(response_type == 0)
439 event_handle(event);
440 p_delete(&event);
444 const uint32_t select_input_val = XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT;
446 /* This causes an error if some other window manager is running */
447 xcb_change_window_attributes(globalconf.connection,
448 globalconf.screen->root,
449 XCB_CW_EVENT_MASK, &select_input_val);
452 /* Need to xcb_flush to validate error handler */
453 xcb_aux_sync(globalconf.connection);
455 /* Process all errors in the queue if any. There can be no events yet, so if
456 * this function returns something, it must be an error. */
457 if (xcb_poll_for_event(globalconf.connection) != NULL)
458 fatal("another window manager is already running");
460 /* Prefetch the maximum request length */
461 xcb_prefetch_maximum_request_length(globalconf.connection);
463 /* check for xtest extension */
464 const xcb_query_extension_reply_t *xtest_query;
465 xtest_query = xcb_get_extension_data(globalconf.connection, &xcb_test_id);
466 globalconf.have_xtest = xtest_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);
522 /* Parse and run configuration file */
523 if (!luaA_parserc(&xdg, confpath, true))
524 fatal("couldn't find any rc file");
526 p_delete(&confpath);
528 xdgWipeHandle(&xdg);
530 /* scan existing windows */
531 scan(tree_c);
533 xcb_flush(globalconf.connection);
535 /* main event loop */
536 globalconf.loop = g_main_loop_new(NULL, FALSE);
537 g_main_loop_run(globalconf.loop);
538 g_main_loop_unref(globalconf.loop);
539 globalconf.loop = NULL;
541 awesome_atexit(false);
543 return EXIT_SUCCESS;
546 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80