client: Don't move clients around across restarts (FS#1159)
[awesome.git] / awesome.c
blob743010b75dc1b9de92d1400f91115d006acd1eca
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 <glib-unix.h>
38 #include "awesome.h"
39 #include "spawn.h"
40 #include "objects/client.h"
41 #include "xwindow.h"
42 #include "ewmh.h"
43 #include "dbus.h"
44 #include "systray.h"
45 #include "event.h"
46 #include "property.h"
47 #include "screen.h"
48 #include "luaa.h"
49 #include "common/version.h"
50 #include "common/atoms.h"
51 #include "common/xcursor.h"
52 #include "common/xutil.h"
53 #include "common/backtrace.h"
55 awesome_t globalconf;
57 /** argv used to run awesome */
58 static char *awesome_argv;
60 /** Call before exiting.
62 void
63 awesome_atexit(bool restart)
65 lua_pushboolean(globalconf.L, restart);
66 signal_object_emit(globalconf.L, &global_signals, "exit", 1);
68 /* TODO: Reparent windows back to root window, placing them according to
69 * their window gravity. Right now we pretend to use static gravity and that
70 * works automatically thanks to xcb_change_save_set(). See
71 * titlebar_resize().
74 a_dbus_cleanup();
76 systray_cleanup();
78 /* Close Lua */
79 lua_close(globalconf.L);
81 /* X11 is a great protocol. There is a save-set so that reparenting WMs
82 * don't kill clients when they shut down. However, when a focused windows
83 * is saved, the focus will move to its parent with revert-to none.
84 * Immediately afterwards, this parent is destroyed and the focus is gone.
85 * Work around this by placing the focus where we like it to be.
87 xcb_set_input_focus(globalconf.connection, XCB_INPUT_FOCUS_POINTER_ROOT,
88 XCB_NONE, XCB_CURRENT_TIME);
89 xcb_aux_sync(globalconf.connection);
91 /* Disconnect *after* closing lua */
92 xcb_cursor_context_free(globalconf.cursor_ctx);
93 xcb_disconnect(globalconf.connection);
96 /** Scan X to find windows to manage.
98 static void
99 scan(xcb_query_tree_cookie_t tree_c)
101 int i, tree_c_len;
102 xcb_query_tree_reply_t *tree_r;
103 xcb_window_t *wins = NULL;
104 xcb_get_window_attributes_reply_t *attr_r;
105 xcb_get_geometry_reply_t *geom_r;
106 long state;
108 tree_r = xcb_query_tree_reply(globalconf.connection,
109 tree_c,
110 NULL);
112 if(!tree_r)
113 return;
115 /* Get the tree of the children windows of the current root window */
116 if(!(wins = xcb_query_tree_children(tree_r)))
117 fatal("cannot get tree children");
119 tree_c_len = xcb_query_tree_children_length(tree_r);
120 xcb_get_window_attributes_cookie_t attr_wins[tree_c_len];
121 xcb_get_property_cookie_t state_wins[tree_c_len];
123 for(i = 0; i < tree_c_len; i++)
125 attr_wins[i] = xcb_get_window_attributes_unchecked(globalconf.connection,
126 wins[i]);
128 state_wins[i] = xwindow_get_state_unchecked(wins[i]);
131 xcb_get_geometry_cookie_t *geom_wins[tree_c_len];
133 for(i = 0; i < tree_c_len; i++)
135 attr_r = xcb_get_window_attributes_reply(globalconf.connection,
136 attr_wins[i],
137 NULL);
139 state = xwindow_get_state_reply(state_wins[i]);
141 if(!attr_r || attr_r->override_redirect
142 || attr_r->map_state == XCB_MAP_STATE_UNMAPPED
143 || state == XCB_ICCCM_WM_STATE_WITHDRAWN)
145 geom_wins[i] = NULL;
146 p_delete(&attr_r);
147 continue;
150 p_delete(&attr_r);
152 /* Get the geometry of the current window */
153 geom_wins[i] = p_alloca(xcb_get_geometry_cookie_t, 1);
154 *(geom_wins[i]) = xcb_get_geometry_unchecked(globalconf.connection, wins[i]);
157 for(i = 0; i < tree_c_len; i++)
159 if(!geom_wins[i] || !(geom_r = xcb_get_geometry_reply(globalconf.connection,
160 *(geom_wins[i]), NULL)))
161 continue;
163 client_manage(wins[i], geom_r, true);
165 p_delete(&geom_r);
168 p_delete(&tree_r);
171 static void
172 a_xcb_check(void)
174 xcb_generic_event_t *mouse = NULL, *event;
176 while((event = xcb_poll_for_event(globalconf.connection)))
178 /* We will treat mouse events later.
179 * We cannot afford to treat all mouse motion events,
180 * because that would be too much CPU intensive, so we just
181 * take the last we get after a bunch of events. */
182 if(XCB_EVENT_RESPONSE_TYPE(event) == XCB_MOTION_NOTIFY)
184 p_delete(&mouse);
185 mouse = event;
187 else
189 uint8_t type = XCB_EVENT_RESPONSE_TYPE(event);
190 if((type == XCB_ENTER_NOTIFY || type == XCB_LEAVE_NOTIFY) && mouse)
192 /* Make sure enter/motion/leave events are handled in the
193 * correct order */
194 event_handle(mouse);
195 p_delete(&mouse);
196 mouse = NULL;
198 event_handle(event);
199 p_delete(&event);
203 if(mouse)
205 event_handle(mouse);
206 p_delete(&mouse);
210 static gboolean
211 a_xcb_io_cb(GIOChannel *source, GIOCondition cond, gpointer data)
213 /* a_xcb_check() already handled all events */
215 if(xcb_connection_has_error(globalconf.connection))
216 fatal("X server connection broke");
218 return TRUE;
221 static gint
222 a_glib_poll(GPollFD *ufds, guint nfsd, gint timeout)
224 guint res;
225 awesome_refresh();
226 res = g_poll(ufds, nfsd, timeout);
227 a_xcb_check();
228 return res;
231 static void
232 signal_fatal(int signum)
234 buffer_t buf;
235 backtrace_get(&buf);
236 fatal("signal %d, dumping backtrace\n%s", signum, buf.s);
239 /** Function to exit on some signals.
240 * \param data currently unused
242 static gboolean
243 exit_on_signal(gpointer data)
245 g_main_loop_quit(globalconf.loop);
246 return TRUE;
249 void
250 awesome_restart(void)
252 awesome_atexit(true);
253 a_exec(awesome_argv);
256 /** Function to restart awesome on some signals.
257 * \param data currently unused
259 static gboolean
260 restart_on_signal(gpointer data)
262 awesome_restart();
263 return TRUE;
266 /** Print help and exit(2) with given exit_code.
267 * \param exit_code The exit code.
269 static void __attribute__ ((noreturn))
270 exit_help(int exit_code)
272 FILE *outfile = (exit_code == EXIT_SUCCESS) ? stdout : stderr;
273 fprintf(outfile,
274 "Usage: awesome [OPTION]\n\
275 -h, --help show help\n\
276 -v, --version show version\n\
277 -c, --config FILE configuration file to use\n\
278 -k, --check check configuration file syntax\n");
279 exit(exit_code);
282 /** Hello, this is main.
283 * \param argc Who knows.
284 * \param argv Who knows.
285 * \return EXIT_SUCCESS I hope.
288 main(int argc, char **argv)
290 char *confpath = NULL;
291 int xfd, i, opt;
292 ssize_t cmdlen = 1;
293 xdgHandle xdg;
294 bool no_argb = false;
295 bool run_test = false;
296 xcb_generic_event_t *event;
297 xcb_query_tree_cookie_t tree_c;
298 static struct option long_options[] =
300 { "help", 0, NULL, 'h' },
301 { "version", 0, NULL, 'v' },
302 { "config", 1, NULL, 'c' },
303 { "check", 0, NULL, 'k' },
304 { "no-argb", 0, NULL, 'a' },
305 { NULL, 0, NULL, 0 }
308 /* clear the globalconf structure */
309 p_clear(&globalconf, 1);
310 globalconf.keygrabber = LUA_REFNIL;
311 globalconf.mousegrabber = LUA_REFNIL;
312 buffer_init(&globalconf.startup_errors);
314 /* save argv */
315 for(i = 0; i < argc; i++)
316 cmdlen += a_strlen(argv[i]) + 1;
318 awesome_argv = p_new(char, cmdlen);
319 a_strcpy(awesome_argv, cmdlen, argv[0]);
321 for(i = 1; i < argc; i++)
323 a_strcat(awesome_argv, cmdlen, " ");
324 a_strcat(awesome_argv, cmdlen, argv[i]);
327 /* Text won't be printed correctly otherwise */
328 setlocale(LC_CTYPE, "");
330 /* Get XDG basedir data */
331 xdgInitHandle(&xdg);
333 /* init lua */
334 luaA_init(&xdg);
336 /* check args */
337 while((opt = getopt_long(argc, argv, "vhkc:a",
338 long_options, NULL)) != -1)
339 switch(opt)
341 case 'v':
342 eprint_version();
343 break;
344 case 'h':
345 exit_help(EXIT_SUCCESS);
346 break;
347 case 'k':
348 run_test = true;
349 break;
350 case 'c':
351 if(a_strlen(optarg))
352 confpath = a_strdup(optarg);
353 else
354 fatal("-c option requires a file name");
355 break;
356 case 'a':
357 no_argb = true;
358 break;
361 if (run_test)
363 if(!luaA_parserc(&xdg, confpath, false))
365 fprintf(stderr, "✘ Configuration file syntax error.\n");
366 return EXIT_FAILURE;
368 else
370 fprintf(stderr, "✔ Configuration file syntax OK.\n");
371 return EXIT_SUCCESS;
375 /* register function for signals */
376 g_unix_signal_add(SIGINT, exit_on_signal, NULL);
377 g_unix_signal_add(SIGTERM, exit_on_signal, NULL);
378 g_unix_signal_add(SIGHUP, restart_on_signal, NULL);
380 struct sigaction sa = { .sa_handler = signal_fatal, .sa_flags = 0 };
381 sigemptyset(&sa.sa_mask);
382 sigaction(SIGSEGV, &sa, 0);
384 /* We have no clue where the input focus is right now */
385 globalconf.focus.need_update = true;
387 /* X stuff */
388 globalconf.connection = xcb_connect(NULL, &globalconf.default_screen);
389 if(xcb_connection_has_error(globalconf.connection))
390 fatal("cannot open display");
392 globalconf.screen = xcb_aux_get_screen(globalconf.connection, globalconf.default_screen);
393 globalconf.default_visual = draw_default_visual(globalconf.screen);
394 if(!no_argb)
395 globalconf.visual = draw_argb_visual(globalconf.screen);
396 if(!globalconf.visual)
397 globalconf.visual = globalconf.default_visual;
398 globalconf.default_depth = draw_visual_depth(globalconf.screen, globalconf.visual->visual_id);
399 globalconf.default_cmap = globalconf.screen->default_colormap;
400 if(globalconf.default_depth != globalconf.screen->root_depth)
402 // We need our own color map if we aren't using the default depth
403 globalconf.default_cmap = xcb_generate_id(globalconf.connection);
404 xcb_create_colormap(globalconf.connection, XCB_COLORMAP_ALLOC_NONE,
405 globalconf.default_cmap, globalconf.screen->root,
406 globalconf.visual->visual_id);
409 /* Prefetch all the extensions we might need */
410 xcb_prefetch_extension_data(globalconf.connection, &xcb_big_requests_id);
411 xcb_prefetch_extension_data(globalconf.connection, &xcb_test_id);
412 xcb_prefetch_extension_data(globalconf.connection, &xcb_randr_id);
413 xcb_prefetch_extension_data(globalconf.connection, &xcb_xinerama_id);
414 xcb_prefetch_extension_data(globalconf.connection, &xcb_shape_id);
416 if (xcb_cursor_context_new(globalconf.connection, globalconf.screen, &globalconf.cursor_ctx) < 0)
417 fatal("Failed to initialize xcb-cursor");
419 /* Setup the main context */
420 g_main_context_set_poll_func(g_main_context_default(), &a_glib_poll);
422 /* initialize dbus */
423 a_dbus_init();
425 /* Get the file descriptor corresponding to the X connection */
426 xfd = xcb_get_file_descriptor(globalconf.connection);
427 GIOChannel *channel = g_io_channel_unix_new(xfd);
428 g_io_add_watch(channel, G_IO_IN, a_xcb_io_cb, NULL);
429 g_io_channel_unref(channel);
431 /* Grab server */
432 xcb_grab_server(globalconf.connection);
434 /* Make sure there are no pending events. Since we didn't really do anything
435 * at all yet, we will just discard all events which we received so far.
436 * The above GrabServer should make sure no new events are generated. */
437 xcb_aux_sync(globalconf.connection);
438 while ((event = xcb_poll_for_event(globalconf.connection)) != NULL)
440 /* Make sure errors are printed */
441 uint8_t response_type = XCB_EVENT_RESPONSE_TYPE(event);
442 if(response_type == 0)
443 event_handle(event);
444 p_delete(&event);
448 const uint32_t select_input_val = XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT;
450 /* This causes an error if some other window manager is running */
451 xcb_change_window_attributes(globalconf.connection,
452 globalconf.screen->root,
453 XCB_CW_EVENT_MASK, &select_input_val);
456 /* Need to xcb_flush to validate error handler */
457 xcb_aux_sync(globalconf.connection);
459 /* Process all errors in the queue if any. There can be no events yet, so if
460 * this function returns something, it must be an error. */
461 if (xcb_poll_for_event(globalconf.connection) != NULL)
462 fatal("another window manager is already running");
464 /* Prefetch the maximum request length */
465 xcb_prefetch_maximum_request_length(globalconf.connection);
467 /* check for xtest extension */
468 const xcb_query_extension_reply_t *xtest_query;
469 xtest_query = xcb_get_extension_data(globalconf.connection, &xcb_test_id);
470 globalconf.have_xtest = xtest_query->present;
472 /* Allocate the key symbols */
473 globalconf.keysyms = xcb_key_symbols_alloc(globalconf.connection);
474 xcb_get_modifier_mapping_cookie_t xmapping_cookie =
475 xcb_get_modifier_mapping_unchecked(globalconf.connection);
477 /* init atom cache */
478 atoms_init(globalconf.connection);
480 /* init screens information */
481 screen_scan();
483 xutil_lock_mask_get(globalconf.connection, xmapping_cookie,
484 globalconf.keysyms, &globalconf.numlockmask,
485 &globalconf.shiftlockmask, &globalconf.capslockmask,
486 &globalconf.modeswitchmask);
488 /* do this only for real screen */
489 ewmh_init();
490 systray_init();
492 /* init spawn (sn) */
493 spawn_init();
495 /* The default GC is just a newly created associated with a window with
496 * depth globalconf.default_depth */
497 xcb_window_t tmp_win = xcb_generate_id(globalconf.connection);
498 globalconf.gc = xcb_generate_id(globalconf.connection);
499 xcb_create_window(globalconf.connection, globalconf.default_depth,
500 tmp_win, globalconf.screen->root,
501 -1, -1, 1, 1, 0,
502 XCB_COPY_FROM_PARENT, globalconf.visual->visual_id,
503 XCB_CW_BACK_PIXEL | XCB_CW_BORDER_PIXEL | XCB_CW_COLORMAP,
504 (const uint32_t [])
506 globalconf.screen->black_pixel,
507 globalconf.screen->black_pixel,
508 globalconf.default_cmap
510 xcb_create_gc(globalconf.connection, globalconf.gc, tmp_win, XCB_GC_FOREGROUND | XCB_GC_BACKGROUND,
511 (const uint32_t[]) { globalconf.screen->black_pixel, globalconf.screen->white_pixel });
512 xcb_destroy_window(globalconf.connection, tmp_win);
514 /* Get the window tree associated to this screen */
515 tree_c = xcb_query_tree_unchecked(globalconf.connection,
516 globalconf.screen->root);
518 xcb_change_window_attributes(globalconf.connection,
519 globalconf.screen->root,
520 XCB_CW_EVENT_MASK,
521 ROOT_WINDOW_EVENT_MASK);
523 /* we will receive events, stop grabbing server */
524 xcb_ungrab_server(globalconf.connection);
525 xcb_flush(globalconf.connection);
527 /* Parse and run configuration file */
528 if (!luaA_parserc(&xdg, confpath, true))
529 fatal("couldn't find any rc file");
531 p_delete(&confpath);
533 xdgWipeHandle(&xdg);
535 /* scan existing windows */
536 scan(tree_c);
538 xcb_flush(globalconf.connection);
540 /* main event loop */
541 globalconf.loop = g_main_loop_new(NULL, FALSE);
542 g_main_loop_run(globalconf.loop);
543 g_main_loop_unref(globalconf.loop);
544 globalconf.loop = NULL;
546 awesome_atexit(false);
548 return EXIT_SUCCESS;
551 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80