Makes awful.screen.focus keep the cursor position on screen
[awesome.git] / awesome.c
bloba04f27d395d22a2bf28c49b4a360993c55d4f645
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(xcb_query_tree_cookie_t tree_c)
85 int i, tree_c_len;
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 tree_r = xcb_query_tree_reply(globalconf.connection,
93 tree_c,
94 NULL);
96 if(!tree_r)
97 return;
99 /* Get the tree of the children windows of the current root window */
100 if(!(wins = xcb_query_tree_children(tree_r)))
101 fatal("cannot get tree children");
103 tree_c_len = xcb_query_tree_children_length(tree_r);
104 xcb_get_window_attributes_cookie_t attr_wins[tree_c_len];
105 xcb_get_property_cookie_t state_wins[tree_c_len];
107 for(i = 0; i < tree_c_len; i++)
109 attr_wins[i] = xcb_get_window_attributes_unchecked(globalconf.connection,
110 wins[i]);
112 state_wins[i] = xwindow_get_state_unchecked(wins[i]);
115 xcb_get_geometry_cookie_t *geom_wins[tree_c_len];
117 for(i = 0; i < tree_c_len; i++)
119 attr_r = xcb_get_window_attributes_reply(globalconf.connection,
120 attr_wins[i],
121 NULL);
123 state = xwindow_get_state_reply(state_wins[i]);
125 if(!attr_r || attr_r->override_redirect
126 || attr_r->map_state == XCB_MAP_STATE_UNMAPPED
127 || state == XCB_ICCCM_WM_STATE_WITHDRAWN)
129 geom_wins[i] = NULL;
130 p_delete(&attr_r);
131 continue;
134 p_delete(&attr_r);
136 /* Get the geometry of the current window */
137 geom_wins[i] = p_alloca(xcb_get_geometry_cookie_t, 1);
138 *(geom_wins[i]) = xcb_get_geometry_unchecked(globalconf.connection, wins[i]);
141 for(i = 0; i < tree_c_len; i++)
143 if(!geom_wins[i] || !(geom_r = xcb_get_geometry_reply(globalconf.connection,
144 *(geom_wins[i]), NULL)))
145 continue;
147 client_manage(wins[i], geom_r, true);
149 p_delete(&geom_r);
152 p_delete(&tree_r);
155 static void
156 a_refresh_cb(EV_P_ ev_prepare *w, int revents)
158 awesome_refresh();
161 static void
162 a_xcb_check_cb(EV_P_ ev_check *w, int revents)
164 xcb_generic_event_t *mouse = NULL, *event;
166 while((event = xcb_poll_for_event(globalconf.connection)))
168 /* We will treat mouse events later.
169 * We cannot afford to treat all mouse motion events,
170 * because that would be too much CPU intensive, so we just
171 * take the last we get after a bunch of events. */
172 if(XCB_EVENT_RESPONSE_TYPE(event) == XCB_MOTION_NOTIFY)
174 p_delete(&mouse);
175 mouse = event;
177 else
179 uint8_t type = XCB_EVENT_RESPONSE_TYPE(event);
180 if((type == XCB_ENTER_NOTIFY || type == XCB_LEAVE_NOTIFY) && mouse)
182 /* Make sure enter/motion/leave events are handled in the
183 * correct order */
184 event_handle(mouse);
185 p_delete(&mouse);
186 mouse = NULL;
188 event_handle(event);
189 p_delete(&event);
193 if(mouse)
195 event_handle(mouse);
196 p_delete(&mouse);
200 static void
201 a_xcb_io_cb(EV_P_ ev_io *w, int revents)
203 /* a_xcb_check_cb() already handled all events */
205 if(xcb_connection_has_error(globalconf.connection))
206 fatal("X server connection broke");
209 static void
210 signal_fatal(int signum)
212 buffer_t buf;
213 backtrace_get(&buf);
214 fatal("signal %d, dumping backtrace\n%s", signum, buf.s);
217 /** Function to exit on some signals.
218 * \param w the signal received, unused
219 * \param revents currently unused
221 static void
222 exit_on_signal(EV_P_ ev_signal *w, int revents)
224 ev_unloop(EV_A_ 1);
227 void
228 awesome_restart(void)
230 awesome_atexit(true);
231 a_exec(awesome_argv);
234 /** Function to restart awesome on some signals.
235 * \param w the signal received, unused
236 * \param revents Currently unused
238 static void
239 restart_on_signal(EV_P_ ev_signal *w, int revents)
241 awesome_restart();
244 /** Print help and exit(2) with given exit_code.
245 * \param exit_code The exit code.
247 static void __attribute__ ((noreturn))
248 exit_help(int exit_code)
250 FILE *outfile = (exit_code == EXIT_SUCCESS) ? stdout : stderr;
251 fprintf(outfile,
252 "Usage: awesome [OPTION]\n\
253 -h, --help show help\n\
254 -v, --version show version\n\
255 -c, --config FILE configuration file to use\n\
256 -k, --check check configuration file syntax\n");
257 exit(exit_code);
260 /** Hello, this is main.
261 * \param argc Who knows.
262 * \param argv Who knows.
263 * \return EXIT_SUCCESS I hope.
266 main(int argc, char **argv)
268 char *confpath = NULL;
269 int xfd, i, opt;
270 ssize_t cmdlen = 1;
271 xdgHandle xdg;
272 bool no_argb = false;
273 xcb_generic_event_t *event;
274 xcb_query_tree_cookie_t tree_c;
275 static struct option long_options[] =
277 { "help", 0, NULL, 'h' },
278 { "version", 0, NULL, 'v' },
279 { "config", 1, NULL, 'c' },
280 { "check", 0, NULL, 'k' },
281 { "no-argb", 0, NULL, 'a' },
282 { NULL, 0, NULL, 0 }
285 /* event loop watchers */
286 ev_io xio = { .fd = -1 };
287 ev_check xcheck;
288 ev_prepare a_refresh;
289 ev_signal sigint;
290 ev_signal sigterm;
291 ev_signal sighup;
293 /* clear the globalconf structure */
294 p_clear(&globalconf, 1);
295 globalconf.keygrabber = LUA_REFNIL;
296 globalconf.mousegrabber = LUA_REFNIL;
297 buffer_init(&globalconf.startup_errors);
299 /* save argv */
300 for(i = 0; i < argc; i++)
301 cmdlen += a_strlen(argv[i]) + 1;
303 awesome_argv = p_new(char, cmdlen);
304 a_strcpy(awesome_argv, cmdlen, argv[0]);
306 for(i = 1; i < argc; i++)
308 a_strcat(awesome_argv, cmdlen, " ");
309 a_strcat(awesome_argv, cmdlen, argv[i]);
312 /* Text won't be printed correctly otherwise */
313 setlocale(LC_CTYPE, "");
315 /* Get XDG basedir data */
316 xdgInitHandle(&xdg);
318 /* init lua */
319 luaA_init(&xdg);
321 /* check args */
322 while((opt = getopt_long(argc, argv, "vhkc:a",
323 long_options, NULL)) != -1)
324 switch(opt)
326 case 'v':
327 eprint_version();
328 break;
329 case 'h':
330 exit_help(EXIT_SUCCESS);
331 break;
332 case 'k':
333 if(!luaA_parserc(&xdg, confpath, false))
335 fprintf(stderr, "✘ Configuration file syntax error.\n");
336 return EXIT_FAILURE;
338 else
340 fprintf(stderr, "✔ Configuration file syntax OK.\n");
341 return EXIT_SUCCESS;
343 case 'c':
344 if(a_strlen(optarg))
345 confpath = a_strdup(optarg);
346 else
347 fatal("-c option requires a file name");
348 break;
349 case 'a':
350 no_argb = true;
351 break;
354 globalconf.loop = ev_default_loop(EVFLAG_NOSIGFD);
356 /* register function for signals */
357 ev_signal_init(&sigint, exit_on_signal, SIGINT);
358 ev_signal_init(&sigterm, exit_on_signal, SIGTERM);
359 ev_signal_init(&sighup, restart_on_signal, SIGHUP);
360 ev_signal_start(globalconf.loop, &sigint);
361 ev_signal_start(globalconf.loop, &sigterm);
362 ev_signal_start(globalconf.loop, &sighup);
363 ev_unref(globalconf.loop);
364 ev_unref(globalconf.loop);
365 ev_unref(globalconf.loop);
367 struct sigaction sa = { .sa_handler = signal_fatal, .sa_flags = 0 };
368 sigemptyset(&sa.sa_mask);
369 sigaction(SIGSEGV, &sa, 0);
371 /* X stuff */
372 globalconf.connection = xcb_connect(NULL, &globalconf.default_screen);
373 if(xcb_connection_has_error(globalconf.connection))
374 fatal("cannot open display");
376 globalconf.screen = xcb_aux_get_screen(globalconf.connection, globalconf.default_screen);
377 globalconf.default_visual = draw_default_visual(globalconf.screen);
378 if(!no_argb)
379 globalconf.visual = draw_argb_visual(globalconf.screen);
380 if(!globalconf.visual)
381 globalconf.visual = globalconf.default_visual;
382 globalconf.default_depth = draw_visual_depth(globalconf.screen, globalconf.visual->visual_id);
383 globalconf.default_cmap = globalconf.screen->default_colormap;
384 if(globalconf.default_depth != globalconf.screen->root_depth)
386 // We need our own color map if we aren't using the default depth
387 globalconf.default_cmap = xcb_generate_id(globalconf.connection);
388 xcb_create_colormap(globalconf.connection, XCB_COLORMAP_ALLOC_NONE,
389 globalconf.default_cmap, globalconf.screen->root,
390 globalconf.visual->visual_id);
393 /* Prefetch all the extensions we might need */
394 xcb_prefetch_extension_data(globalconf.connection, &xcb_big_requests_id);
395 xcb_prefetch_extension_data(globalconf.connection, &xcb_test_id);
396 xcb_prefetch_extension_data(globalconf.connection, &xcb_randr_id);
397 xcb_prefetch_extension_data(globalconf.connection, &xcb_xinerama_id);
399 /* initialize dbus */
400 a_dbus_init();
402 /* Get the file descriptor corresponding to the X connection */
403 xfd = xcb_get_file_descriptor(globalconf.connection);
404 ev_io_init(&xio, &a_xcb_io_cb, xfd, EV_READ);
405 ev_io_start(globalconf.loop, &xio);
406 ev_check_init(&xcheck, &a_xcb_check_cb);
407 ev_check_start(globalconf.loop, &xcheck);
408 ev_unref(globalconf.loop);
409 ev_prepare_init(&a_refresh, &a_refresh_cb);
410 ev_prepare_start(globalconf.loop, &a_refresh);
411 ev_unref(globalconf.loop);
413 /* Grab server */
414 xcb_grab_server(globalconf.connection);
416 /* Make sure there are no pending events. Since we didn't really do anything
417 * at all yet, we will just discard all events which we received so far.
418 * The above GrabServer should make sure no new events are generated. */
419 xcb_aux_sync(globalconf.connection);
420 while ((event = xcb_poll_for_event(globalconf.connection)) != NULL)
422 /* Make sure errors are printed */
423 uint8_t response_type = XCB_EVENT_RESPONSE_TYPE(event);
424 if(response_type == 0)
425 event_handle(event);
426 p_delete(&event);
430 const uint32_t select_input_val = XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT;
432 /* This causes an error if some other window manager is running */
433 xcb_change_window_attributes(globalconf.connection,
434 globalconf.screen->root,
435 XCB_CW_EVENT_MASK, &select_input_val);
438 /* Need to xcb_flush to validate error handler */
439 xcb_aux_sync(globalconf.connection);
441 /* Process all errors in the queue if any. There can be no events yet, so if
442 * this function returns something, it must be an error. */
443 if (xcb_poll_for_event(globalconf.connection) != NULL)
444 fatal("another window manager is already running");
446 /* Prefetch the maximum request length */
447 xcb_prefetch_maximum_request_length(globalconf.connection);
449 /* check for xtest extension */
450 const xcb_query_extension_reply_t *xtest_query;
451 xtest_query = xcb_get_extension_data(globalconf.connection, &xcb_test_id);
452 globalconf.have_xtest = xtest_query->present;
454 /* Allocate the key symbols */
455 globalconf.keysyms = xcb_key_symbols_alloc(globalconf.connection);
456 xcb_get_modifier_mapping_cookie_t xmapping_cookie =
457 xcb_get_modifier_mapping_unchecked(globalconf.connection);
459 /* init atom cache */
460 atoms_init(globalconf.connection);
462 /* init screens information */
463 screen_scan();
465 xutil_lock_mask_get(globalconf.connection, xmapping_cookie,
466 globalconf.keysyms, &globalconf.numlockmask,
467 &globalconf.shiftlockmask, &globalconf.capslockmask,
468 &globalconf.modeswitchmask);
470 /* do this only for real screen */
471 ewmh_init();
472 systray_init();
474 /* init spawn (sn) */
475 spawn_init();
477 /* The default GC is just a newly created associated with a window with
478 * depth globalconf.default_depth */
479 xcb_window_t tmp_win = xcb_generate_id(globalconf.connection);
480 globalconf.gc = xcb_generate_id(globalconf.connection);
481 xcb_create_window(globalconf.connection, globalconf.default_depth,
482 tmp_win, globalconf.screen->root,
483 -1, -1, 1, 1, 0,
484 XCB_COPY_FROM_PARENT, globalconf.visual->visual_id,
485 XCB_CW_BACK_PIXEL | XCB_CW_BORDER_PIXEL | XCB_CW_COLORMAP,
486 (const uint32_t [])
488 globalconf.screen->black_pixel,
489 globalconf.screen->black_pixel,
490 globalconf.default_cmap
492 xcb_create_gc(globalconf.connection, globalconf.gc, tmp_win, XCB_GC_FOREGROUND | XCB_GC_BACKGROUND,
493 (const uint32_t[]) { globalconf.screen->black_pixel, globalconf.screen->white_pixel });
494 xcb_destroy_window(globalconf.connection, tmp_win);
496 /* Get the window tree associated to this screen */
497 tree_c = xcb_query_tree_unchecked(globalconf.connection,
498 globalconf.screen->root);
500 xcb_change_window_attributes(globalconf.connection,
501 globalconf.screen->root,
502 XCB_CW_EVENT_MASK,
503 ROOT_WINDOW_EVENT_MASK);
505 /* we will receive events, stop grabbing server */
506 xcb_ungrab_server(globalconf.connection);
508 /* Parse and run configuration file */
509 if (!luaA_parserc(&xdg, confpath, true))
510 fatal("couldn't find any rc file");
512 p_delete(&confpath);
514 xdgWipeHandle(&xdg);
516 /* scan existing windows */
517 scan(tree_c);
519 xcb_flush(globalconf.connection);
521 /* main event loop */
522 ev_loop(globalconf.loop, 0);
524 /* cleanup event loop */
525 ev_ref(globalconf.loop);
526 ev_check_stop(globalconf.loop, &xcheck);
527 ev_ref(globalconf.loop);
528 ev_prepare_stop(globalconf.loop, &a_refresh);
529 ev_ref(globalconf.loop);
530 ev_io_stop(globalconf.loop, &xio);
532 awesome_atexit(false);
534 return EXIT_SUCCESS;
537 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80