awesomerc: use fullgeometry in mouse warping (FS#351)
[awesome.git] / awesome.c
blob2ff811b086a953ccc14710c8731129088ffcb419
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 #define _GNU_SOURCE
23 #include <getopt.h>
25 #include <locale.h>
26 #include <stdio.h>
27 #include <unistd.h>
28 #include <signal.h>
30 #include <ev.h>
32 #include <xcb/xcb_event.h>
34 #include "awesome.h"
35 #include "client.h"
36 #include "window.h"
37 #include "ewmh.h"
38 #include "dbus.h"
39 #include "systray.h"
40 #include "event.h"
41 #include "property.h"
42 #include "screen.h"
43 #include "common/version.h"
44 #include "common/atoms.h"
45 #include "config.h"
47 awesome_t globalconf;
49 typedef struct
51 xcb_window_t id;
52 xcb_query_tree_cookie_t tree_cookie;
53 } root_win_t;
55 /** Call before exiting.
57 void
58 awesome_atexit(void)
60 client_t *c;
61 xembed_window_t *em;
62 int screen_nbr;
64 a_dbus_cleanup();
65 luaA_cs_cleanup();
67 /* reparent systray windows, otherwise they may die with their master */
68 for(em = globalconf.embedded; em; em = em->next)
70 xcb_screen_t *s = xutil_screen_get(globalconf.connection, em->phys_screen);
71 xembed_window_unembed(globalconf.connection, em->win, s->root);
74 /* do this only for real screen */
75 for(screen_nbr = 0;
76 screen_nbr < xcb_setup_roots_length(xcb_get_setup(globalconf.connection));
77 screen_nbr++)
78 systray_cleanup(screen_nbr);
80 /* remap all clients since some WM won't handle them otherwise */
81 for(c = globalconf.clients; c; c = c->next)
82 client_unban(c);
84 xcb_flush(globalconf.connection);
86 xcb_disconnect(globalconf.connection);
89 /** Scan X to find windows to manage.
91 static void
92 scan(void)
94 int i, screen, phys_screen, tree_c_len;
95 const int screen_max = xcb_setup_roots_length(xcb_get_setup(globalconf.connection));
96 root_win_t root_wins[screen_max];
97 xcb_query_tree_reply_t *tree_r;
98 xcb_window_t *wins = NULL;
99 xcb_get_window_attributes_reply_t *attr_r;
100 xcb_get_geometry_reply_t *geom_r;
101 long state;
103 for(phys_screen = 0; phys_screen < screen_max; phys_screen++)
105 /* Get the root window ID associated to this screen */
106 root_wins[phys_screen].id = xutil_screen_get(globalconf.connection, phys_screen)->root;
108 /* Get the window tree associated to this screen */
109 root_wins[phys_screen].tree_cookie = xcb_query_tree_unchecked(globalconf.connection,
110 root_wins[phys_screen].id);
113 for(phys_screen = 0; phys_screen < screen_max; phys_screen++)
115 tree_r = xcb_query_tree_reply(globalconf.connection,
116 root_wins[phys_screen].tree_cookie,
117 NULL);
119 if(!tree_r)
120 continue;
122 /* Get the tree of the children windows of the current root window */
123 if(!(wins = xcb_query_tree_children(tree_r)))
124 fatal("E: cannot get tree children");
126 tree_c_len = xcb_query_tree_children_length(tree_r);
127 xcb_get_window_attributes_cookie_t attr_wins[tree_c_len];
128 xcb_get_property_cookie_t state_wins[tree_c_len];
130 for(i = 0; i < tree_c_len; i++)
132 attr_wins[i] = xcb_get_window_attributes_unchecked(globalconf.connection,
133 wins[i]);
135 state_wins[i] = window_state_get_unchecked(wins[i]);
138 xcb_get_geometry_cookie_t *geom_wins[tree_c_len];
140 for(i = 0; i < tree_c_len; i++)
142 bool has_awesome_prop;
144 attr_r = xcb_get_window_attributes_reply(globalconf.connection,
145 attr_wins[i],
146 NULL);
148 state = window_state_get_reply(state_wins[i]);
150 has_awesome_prop = xutil_text_prop_get(globalconf.connection, wins[i],
151 _AWESOME_TAGS, NULL, NULL);
153 if(!attr_r || attr_r->override_redirect
154 || (attr_r->map_state != XCB_MAP_STATE_VIEWABLE && !has_awesome_prop)
155 || (state == XCB_WM_STATE_WITHDRAWN && !has_awesome_prop))
157 geom_wins[i] = NULL;
158 p_delete(&attr_r);
159 continue;
162 p_delete(&attr_r);
164 /* Get the geometry of the current window */
165 geom_wins[i] = p_alloca(xcb_get_geometry_cookie_t, 1);
166 *(geom_wins[i]) = xcb_get_geometry_unchecked(globalconf.connection, wins[i]);
169 for(i = 0; i < tree_c_len; i++)
171 if(!geom_wins[i] || !(geom_r = xcb_get_geometry_reply(globalconf.connection,
172 *(geom_wins[i]), NULL)))
173 continue;
175 screen = screen_getbycoord(phys_screen, geom_r->x, geom_r->y);
177 client_manage(wins[i], geom_r, phys_screen, screen);
179 p_delete(&geom_r);
182 p_delete(&tree_r);
186 static void
187 a_xcb_check_cb(EV_P_ ev_check *w, int revents)
189 xcb_event_poll_for_event_loop(&globalconf.evenths);
190 awesome_refresh(globalconf.connection);
193 static void
194 a_xcb_io_cb(EV_P_ ev_io *w, int revents)
196 /* empty */
199 /** Startup Error handler to check if another window manager
200 * is already running.
201 * \param data Additional optional parameters data.
202 * \param c X connection.
203 * \param error Error event.
205 static int __attribute__ ((noreturn))
206 xerrorstart(void * data __attribute__ ((unused)),
207 xcb_connection_t * c __attribute__ ((unused)),
208 xcb_generic_error_t * error __attribute__ ((unused)))
210 fatal("another window manager is already running");
213 /** Function to exit on some signals.
214 * \param sig the signal received, unused
216 static void
217 exit_on_signal(EV_P_ ev_signal *w, int revents)
219 ev_unloop(EV_A_ 1);
222 void
223 awesome_restart(void)
225 awesome_atexit();
226 a_exec(globalconf.argv);
229 /** Function to restart aweome on some signals.
230 * \param sig the signam received, unused
232 static void
233 restart_on_signal(EV_P_ ev_signal *w, int revents)
235 awesome_restart();
238 /** \brief awesome xerror function.
239 * There's no way to check accesses to destroyed windows, thus those cases are
240 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
241 * default error handler, which may call exit.
242 * \param data Currently unused.
243 * \param c The connectiont to the X server.
244 * \param e The error event.
245 * \return 0 if no error, or xerror's xlib return status.
247 static int
248 xerror(void *data __attribute__ ((unused)),
249 xcb_connection_t *c __attribute__ ((unused)),
250 xcb_generic_error_t *e)
252 xutil_error_t err;
254 if(!xutil_error_init(e, &err))
255 return 0;
257 /* ignore this */
258 if(e->error_code == XUTIL_BAD_WINDOW
259 || (e->error_code == XUTIL_BAD_MATCH && err.request_code == XCB_SET_INPUT_FOCUS)
260 || (e->error_code == XUTIL_BAD_VALUE && err.request_code == XCB_KILL_CLIENT)
261 || (err.request_code == XCB_CONFIGURE_WINDOW && e->error_code == XUTIL_BAD_MATCH))
262 goto bailout;
264 warn("X error: request=%s, error=%s", err.request_label, err.error_label);
267 * Xlib code was using default X error handler, namely
268 * '_XDefaultError()', which displays more informations about the
269 * error and also exit if 'error_code'' equals to
270 * 'BadImplementation'
272 * \todo display more informations about the error (like the Xlib default error handler)
274 if(e->error_code == XUTIL_BAD_IMPLEMENTATION)
275 fatal("X error: request=%s, error=%s", err.request_label, err.error_label);
277 bailout:
278 xutil_error_wipe(&err);
279 return 0;
282 /** Print help and exit(2) with given exit_code.
283 * \param exit_code The exit code.
285 static void __attribute__ ((noreturn))
286 exit_help(int exit_code)
288 FILE *outfile = (exit_code == EXIT_SUCCESS) ? stdout : stderr;
289 fprintf(outfile,
290 "Usage: awesome [OPTION]\n\
291 -h, --help show help\n\
292 -v, --version show version\n\
293 -c, --config FILE configuration file to use\n\
294 -k, --check check configuration file syntax\n");
295 exit(exit_code);
298 /** Hello, this is main.
299 * \param argc Who knows.
300 * \param argv Who knows.
301 * \return EXIT_SUCCESS I hope.
304 main(int argc, char **argv)
306 const char *confpath = NULL;
307 int xfd, i, screen_nbr, opt, colors_nbr;
308 xcolor_init_request_t colors_reqs[2];
309 xcb_get_modifier_mapping_cookie_t xmapping_cookie;
310 ssize_t cmdlen = 1;
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 { NULL, 0, NULL, 0 }
320 /* event loop watchers */
321 ev_io xio = { .fd = -1 };
322 ev_check xcheck;
323 ev_signal sigint;
324 ev_signal sigterm;
325 ev_signal sighup;
327 /* clear the globalconf structure */
328 p_clear(&globalconf, 1);
329 globalconf.keygrabber = LUA_REFNIL;
331 /* save argv */
332 for(i = 0; i < argc; i++)
333 cmdlen += a_strlen(argv[i]) + 1;
335 globalconf.argv = p_new(char, cmdlen);
336 a_strcpy(globalconf.argv, cmdlen, argv[0]);
338 for(i = 1; i < argc; i++)
340 a_strcat(globalconf.argv, cmdlen, " ");
341 a_strcat(globalconf.argv, cmdlen, argv[i]);
344 /* Text won't be printed correctly otherwise */
345 setlocale(LC_CTYPE, "");
347 /* init lua */
348 luaA_init();
350 /* check args */
351 while((opt = getopt_long(argc, argv, "vhkc:",
352 long_options, NULL)) != -1)
353 switch(opt)
355 case 'v':
356 eprint_version("awesome");
357 break;
358 case 'h':
359 exit_help(EXIT_SUCCESS);
360 break;
361 case 'k':
362 if(!luaA_parserc(confpath, false))
364 fprintf(stderr, "✘ Configuration file error.\n");
365 return EXIT_FAILURE;
367 else
369 fprintf(stderr, "✔ Configuration file OK.\n");
370 return EXIT_SUCCESS;
372 case 'c':
373 if(a_strlen(optarg))
374 confpath = a_strdup(optarg);
375 else
376 fatal("-c option requires a file name");
377 break;
380 globalconf.loop = ev_default_loop(0);
381 ev_timer_init(&globalconf.timer, &luaA_on_timer, 0., 0.);
383 /* register function for signals */
384 ev_signal_init(&sigint, exit_on_signal, SIGINT);
385 ev_signal_init(&sigterm, exit_on_signal, SIGTERM);
386 ev_signal_init(&sighup, restart_on_signal, SIGHUP);
387 ev_signal_start(globalconf.loop, &sigint);
388 ev_signal_start(globalconf.loop, &sigterm);
389 ev_signal_start(globalconf.loop, &sighup);
390 ev_unref(globalconf.loop);
391 ev_unref(globalconf.loop);
392 ev_unref(globalconf.loop);
394 /* X stuff */
395 globalconf.connection = xcb_connect(NULL, &globalconf.default_screen);
396 if(xcb_connection_has_error(globalconf.connection))
397 fatal("cannot open display");
399 /* Grab server */
400 xcb_grab_server(globalconf.connection);
401 xcb_flush(globalconf.connection);
403 /* Get the file descriptor corresponding to the X connection */
404 xfd = xcb_get_file_descriptor(globalconf.connection);
405 ev_io_init(&xio, &a_xcb_io_cb, xfd, EV_READ);
406 ev_io_start(globalconf.loop, &xio);
407 ev_check_init(&xcheck, &a_xcb_check_cb);
408 ev_check_start(globalconf.loop, &xcheck);
409 ev_unref(globalconf.loop);
411 /* Allocate a handler which will holds all errors and events */
412 xcb_event_handlers_init(globalconf.connection, &globalconf.evenths);
413 xutil_error_handler_catch_all_set(&globalconf.evenths, xerrorstart, NULL);
415 for(screen_nbr = 0;
416 screen_nbr < xcb_setup_roots_length(xcb_get_setup(globalconf.connection));
417 screen_nbr++)
419 const uint32_t select_input_val = XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT;
421 /* This causes an error if some other window manager is running */
422 xcb_change_window_attributes(globalconf.connection,
423 xutil_screen_get(globalconf.connection, screen_nbr)->root,
424 XCB_CW_EVENT_MASK, &select_input_val);
427 /* Need to xcb_flush to validate error handler */
428 xcb_aux_sync(globalconf.connection);
430 /* Process all errors in the queue if any */
431 xcb_event_poll_for_event_loop(&globalconf.evenths);
433 /* Set the default xerror handler */
434 xutil_error_handler_catch_all_set(&globalconf.evenths, xerror, NULL);
436 /* Allocate the key symbols */
437 globalconf.keysyms = xcb_key_symbols_alloc(globalconf.connection);
439 /* Send the request to get the NumLock, ShiftLock and CapsLock
440 masks */
441 xmapping_cookie = xcb_get_modifier_mapping_unchecked(globalconf.connection);
443 /* init atom cache */
444 atoms_init(globalconf.connection);
446 /* init screens information */
447 screen_scan();
449 /* init default font and colors */
450 colors_reqs[0] = xcolor_init_unchecked(&globalconf.colors.fg,
451 "black", sizeof("black") - 1);
453 colors_reqs[1] = xcolor_init_unchecked(&globalconf.colors.bg,
454 "white", sizeof("white") - 1);
456 globalconf.font = draw_font_new(globalconf.default_screen, "sans 8");
458 /* init cursors */
459 globalconf.cursor[CurNormal] = xutil_cursor_new(globalconf.connection, XUTIL_CURSOR_LEFT_PTR);
460 globalconf.cursor[CurResize] = xutil_cursor_new(globalconf.connection, XUTIL_CURSOR_SIZING);
461 globalconf.cursor[CurResizeH] = xutil_cursor_new(globalconf.connection, XUTIL_CURSOR_DOUBLE_ARROW_HORIZ);
462 globalconf.cursor[CurResizeV] = xutil_cursor_new(globalconf.connection, XUTIL_CURSOR_DOUBLE_ARROW_VERT);
463 globalconf.cursor[CurMove] = xutil_cursor_new(globalconf.connection, XUTIL_CURSOR_FLEUR);
464 globalconf.cursor[CurTopRight] = xutil_cursor_new(globalconf.connection, XUTIL_CURSOR_TOP_RIGHT_CORNER);
465 globalconf.cursor[CurTopLeft] = xutil_cursor_new(globalconf.connection, XUTIL_CURSOR_TOP_LEFT_CORNER);
466 globalconf.cursor[CurBotRight] = xutil_cursor_new(globalconf.connection, XUTIL_CURSOR_BOTTOM_RIGHT_CORNER);
467 globalconf.cursor[CurBotLeft] = xutil_cursor_new(globalconf.connection, XUTIL_CURSOR_BOTTOM_LEFT_CORNER);
469 for(colors_nbr = 0; colors_nbr < 2; colors_nbr++)
470 xcolor_init_reply(colors_reqs[colors_nbr]);
472 /* Process the reply of previously sent mapping request */
473 xutil_lock_mask_get(globalconf.connection, xmapping_cookie,
474 globalconf.keysyms, &globalconf.numlockmask,
475 &globalconf.shiftlockmask, &globalconf.capslockmask);
477 /* do this only for real screen */
478 for(screen_nbr = 0;
479 screen_nbr < xcb_setup_roots_length(xcb_get_setup(globalconf.connection));
480 screen_nbr++)
482 /* select for events */
483 const uint32_t change_win_vals[] =
485 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT | XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY
486 | XCB_EVENT_MASK_ENTER_WINDOW | XCB_EVENT_MASK_LEAVE_WINDOW
487 | XCB_EVENT_MASK_STRUCTURE_NOTIFY
488 | XCB_EVENT_MASK_PROPERTY_CHANGE,
489 globalconf.cursor[CurNormal]
492 xcb_change_window_attributes(globalconf.connection,
493 xutil_screen_get(globalconf.connection, screen_nbr)->root,
494 XCB_CW_EVENT_MASK | XCB_CW_CURSOR,
495 change_win_vals);
496 ewmh_init(screen_nbr);
497 systray_init(screen_nbr);
500 /* Parse and run configuration file */
501 luaA_parserc(confpath, true);
503 /* scan existing windows */
504 scan();
506 /* process all errors in the queue if any */
507 xcb_event_poll_for_event_loop(&globalconf.evenths);
508 a_xcb_set_event_handlers();
509 a_xcb_set_property_handlers();
511 /* Grab server */
512 xcb_ungrab_server(globalconf.connection);
514 xcb_aux_sync(globalconf.connection);
516 luaA_cs_init();
517 a_dbus_init();
519 /* refresh everything before waiting events */
520 awesome_refresh(globalconf.connection);
522 /* main event loop */
523 ev_loop(globalconf.loop, 0);
525 /* cleanup event loop */
526 ev_ref(globalconf.loop);
527 ev_check_stop(globalconf.loop, &xcheck);
528 ev_ref(globalconf.loop);
529 ev_io_stop(globalconf.loop, &xio);
531 awesome_atexit();
533 return EXIT_SUCCESS;
536 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80