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.
29 #include <xcb/xtest.h>
30 #include <xcb/xcb_event.h>
44 #include "common/version.h"
45 #include "common/atoms.h"
46 #include "common/xcursor.h"
47 #include "common/xutil.h"
48 #include "common/backtrace.h"
55 xcb_query_tree_cookie_t tree_cookie
;
58 /** Call before exiting.
63 int screen_nbr
, nscreens
;
65 if(globalconf
.hooks
.exit
!= LUA_REFNIL
)
66 luaA_dofunction_from_registry(globalconf
.L
, globalconf
.hooks
.exit
, 0, 0);
68 signal_object_emit(globalconf
.L
, &global_signals
, "exit", 0);
72 /* reparent systray windows, otherwise they may die with their master */
73 for(int i
= 0; i
< globalconf
.embedded
.len
; i
++)
75 xcb_screen_t
*s
= xutil_screen_get(globalconf
.connection
,
76 globalconf
.embedded
.tab
[i
].phys_screen
);
77 xembed_window_unembed(globalconf
.connection
, globalconf
.embedded
.tab
[i
].win
, s
->root
);
80 /* do this only for real screen */
81 const xcb_setup_t
*setup
= xcb_get_setup(globalconf
.connection
);
82 nscreens
= setup
? xcb_setup_roots_length(setup
) : -1;
84 screen_nbr
< nscreens
;
86 systray_cleanup(screen_nbr
);
88 /* remap all clients since some WM won't handle them otherwise */
89 foreach(c
, globalconf
.clients
)
92 titlebar_client_detach(*c
);
96 lua_close(globalconf
.L
);
98 xcb_flush(globalconf
.connection
);
100 /* Disconnect *after* closing lua */
101 xcb_disconnect(globalconf
.connection
);
103 ev_default_destroy();
106 /** Scan X to find windows to manage.
111 int i
, phys_screen
, tree_c_len
;
112 const int screen_max
= xcb_setup_roots_length(xcb_get_setup(globalconf
.connection
));
113 root_win_t root_wins
[screen_max
];
114 xcb_query_tree_reply_t
*tree_r
;
115 xcb_window_t
*wins
= NULL
;
116 xcb_get_window_attributes_reply_t
*attr_r
;
117 xcb_get_geometry_reply_t
*geom_r
;
120 for(phys_screen
= 0; phys_screen
< screen_max
; phys_screen
++)
122 /* Get the root window ID associated to this screen */
123 root_wins
[phys_screen
].id
= xutil_screen_get(globalconf
.connection
, phys_screen
)->root
;
125 /* Get the window tree associated to this screen */
126 root_wins
[phys_screen
].tree_cookie
= xcb_query_tree_unchecked(globalconf
.connection
,
127 root_wins
[phys_screen
].id
);
130 for(phys_screen
= 0; phys_screen
< screen_max
; phys_screen
++)
132 tree_r
= xcb_query_tree_reply(globalconf
.connection
,
133 root_wins
[phys_screen
].tree_cookie
,
139 /* Get the tree of the children windows of the current root window */
140 if(!(wins
= xcb_query_tree_children(tree_r
)))
141 fatal("cannot get tree children");
143 tree_c_len
= xcb_query_tree_children_length(tree_r
);
144 xcb_get_window_attributes_cookie_t attr_wins
[tree_c_len
];
145 xcb_get_property_cookie_t state_wins
[tree_c_len
];
147 for(i
= 0; i
< tree_c_len
; i
++)
149 attr_wins
[i
] = xcb_get_window_attributes_unchecked(globalconf
.connection
,
152 state_wins
[i
] = window_state_get_unchecked(wins
[i
]);
155 xcb_get_geometry_cookie_t
*geom_wins
[tree_c_len
];
157 for(i
= 0; i
< tree_c_len
; i
++)
159 attr_r
= xcb_get_window_attributes_reply(globalconf
.connection
,
163 state
= window_state_get_reply(state_wins
[i
]);
165 if(!attr_r
|| attr_r
->override_redirect
166 || attr_r
->map_state
== XCB_MAP_STATE_UNVIEWABLE
167 || state
== XCB_WM_STATE_WITHDRAWN
)
176 /* Get the geometry of the current window */
177 geom_wins
[i
] = p_alloca(xcb_get_geometry_cookie_t
, 1);
178 *(geom_wins
[i
]) = xcb_get_geometry_unchecked(globalconf
.connection
, wins
[i
]);
181 for(i
= 0; i
< tree_c_len
; i
++)
183 if(!geom_wins
[i
] || !(geom_r
= xcb_get_geometry_reply(globalconf
.connection
,
184 *(geom_wins
[i
]), NULL
)))
187 /* The window can be mapped, so force it to be undrawn for startup */
188 xcb_unmap_window(globalconf
.connection
, wins
[i
]);
190 client_manage(wins
[i
], geom_r
, phys_screen
, true);
200 a_refresh_cb(EV_P_ ev_prepare
*w
, int revents
)
206 a_xcb_check_cb(EV_P_ ev_check
*w
, int revents
)
208 xcb_generic_event_t
*mouse
= NULL
, *event
;
210 while((event
= xcb_poll_for_event(globalconf
.connection
)))
212 /* We will treat mouse events later.
213 * We cannot afford to treat all mouse motion events,
214 * because that would be too much CPU intensive, so we just
215 * take the last we get after a bunch of events. */
216 if(XCB_EVENT_RESPONSE_TYPE(event
) == XCB_MOTION_NOTIFY
)
223 xcb_event_handle(&globalconf
.evenths
, event
);
230 xcb_event_handle(&globalconf
.evenths
, mouse
);
236 a_xcb_io_cb(EV_P_ ev_io
*w
, int revents
)
241 /** Startup Error handler to check if another window manager
242 * is already running.
243 * \param data Additional optional parameters data.
244 * \param c X connection.
245 * \param error Error event.
247 static int __attribute__ ((noreturn
))
248 xerrorstart(void * data
__attribute__ ((unused
)),
249 xcb_connection_t
* c
__attribute__ ((unused
)),
250 xcb_generic_error_t
* error
__attribute__ ((unused
)))
252 fatal("another window manager is already running");
256 signal_fatal(int signum
)
260 fatal("dumping backtrace\n%s", buf
.s
);
263 /** Function to exit on some signals.
264 * \param w the signal received, unused
265 * \param revents currently unused
268 exit_on_signal(EV_P_ ev_signal
*w
, int revents
)
274 awesome_restart(void)
277 a_exec(globalconf
.argv
);
280 /** Function to restart awesome on some signals.
281 * \param w the signal received, unused
282 * \param revents Currently unused
285 restart_on_signal(EV_P_ ev_signal
*w
, int revents
)
290 /** \brief awesome xerror function.
291 * There's no way to check accesses to destroyed windows, thus those cases are
292 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
293 * default error handler, which may call exit.
294 * \param data Currently unused.
295 * \param c The connection to the X server.
296 * \param e The error event.
297 * \return 0 if no error, or xerror's xlib return status.
300 xerror(void *data
__attribute__ ((unused
)),
301 xcb_connection_t
*c
__attribute__ ((unused
)),
302 xcb_generic_error_t
*e
)
305 if(e
->error_code
== XCB_EVENT_ERROR_BAD_WINDOW
306 || (e
->error_code
== XCB_EVENT_ERROR_BAD_MATCH
307 && e
->major_code
== XCB_SET_INPUT_FOCUS
)
308 || (e
->error_code
== XCB_EVENT_ERROR_BAD_VALUE
309 && e
->major_code
== XCB_KILL_CLIENT
)
310 || (e
->major_code
== XCB_CONFIGURE_WINDOW
311 && e
->error_code
== XCB_EVENT_ERROR_BAD_MATCH
))
314 warn("X error: request=%s, error=%s",
315 xcb_event_get_request_label(e
->major_code
),
316 xcb_event_get_error_label(e
->error_code
));
321 /** Print help and exit(2) with given exit_code.
322 * \param exit_code The exit code.
324 static void __attribute__ ((noreturn
))
325 exit_help(int exit_code
)
327 FILE *outfile
= (exit_code
== EXIT_SUCCESS
) ? stdout
: stderr
;
329 "Usage: awesome [OPTION]\n\
330 -h, --help show help\n\
331 -v, --version show version\n\
332 -c, --config FILE configuration file to use\n\
333 -k, --check check configuration file syntax\n");
337 /** Hello, this is main.
338 * \param argc Who knows.
339 * \param argv Who knows.
340 * \return EXIT_SUCCESS I hope.
343 main(int argc
, char **argv
)
345 char *confpath
= NULL
;
346 int xfd
, i
, screen_nbr
, opt
, colors_nbr
;
347 xcolor_init_request_t colors_reqs
[2];
350 static struct option long_options
[] =
352 { "help", 0, NULL
, 'h' },
353 { "version", 0, NULL
, 'v' },
354 { "config", 1, NULL
, 'c' },
355 { "check", 0, NULL
, 'k' },
359 /* event loop watchers */
360 ev_io xio
= { .fd
= -1 };
362 ev_prepare a_refresh
;
367 /* clear the globalconf structure */
368 p_clear(&globalconf
, 1);
369 globalconf
.keygrabber
= LUA_REFNIL
;
370 globalconf
.mousegrabber
= LUA_REFNIL
;
373 for(i
= 0; i
< argc
; i
++)
374 cmdlen
+= a_strlen(argv
[i
]) + 1;
376 globalconf
.argv
= p_new(char, cmdlen
);
377 a_strcpy(globalconf
.argv
, cmdlen
, argv
[0]);
379 for(i
= 1; i
< argc
; i
++)
381 a_strcat(globalconf
.argv
, cmdlen
, " ");
382 a_strcat(globalconf
.argv
, cmdlen
, argv
[i
]);
385 /* Text won't be printed correctly otherwise */
386 setlocale(LC_CTYPE
, "");
388 /* Get XDG basedir data */
395 while((opt
= getopt_long(argc
, argv
, "vhkc:",
396 long_options
, NULL
)) != -1)
403 exit_help(EXIT_SUCCESS
);
406 if(!luaA_parserc(&xdg
, confpath
, false))
408 fprintf(stderr
, "✘ Configuration file syntax error.\n");
413 fprintf(stderr
, "✔ Configuration file syntax OK.\n");
418 confpath
= a_strdup(optarg
);
420 fatal("-c option requires a file name");
424 globalconf
.loop
= ev_default_loop(0);
425 ev_timer_init(&globalconf
.timer
, &luaA_on_timer
, 0., 0.);
427 /* register function for signals */
428 ev_signal_init(&sigint
, exit_on_signal
, SIGINT
);
429 ev_signal_init(&sigterm
, exit_on_signal
, SIGTERM
);
430 ev_signal_init(&sighup
, restart_on_signal
, SIGHUP
);
431 ev_signal_start(globalconf
.loop
, &sigint
);
432 ev_signal_start(globalconf
.loop
, &sigterm
);
433 ev_signal_start(globalconf
.loop
, &sighup
);
434 ev_unref(globalconf
.loop
);
435 ev_unref(globalconf
.loop
);
436 ev_unref(globalconf
.loop
);
438 struct sigaction sa
= { .sa_handler
= signal_fatal
, .sa_flags
= 0 };
439 sigemptyset(&sa
.sa_mask
);
440 sigaction(SIGSEGV
, &sa
, 0);
443 globalconf
.connection
= xcb_connect(NULL
, &globalconf
.default_screen
);
444 if(xcb_connection_has_error(globalconf
.connection
))
445 fatal("cannot open display");
447 /* check for xtest extension */
448 const xcb_query_extension_reply_t
*xtest_query
;
449 xtest_query
= xcb_get_extension_data(globalconf
.connection
, &xcb_test_id
);
450 globalconf
.have_xtest
= xtest_query
->present
;
452 /* initialize dbus */
456 xcb_grab_server(globalconf
.connection
);
457 xcb_flush(globalconf
.connection
);
459 /* Get the file descriptor corresponding to the X connection */
460 xfd
= xcb_get_file_descriptor(globalconf
.connection
);
461 ev_io_init(&xio
, &a_xcb_io_cb
, xfd
, EV_READ
);
462 ev_io_start(globalconf
.loop
, &xio
);
463 ev_check_init(&xcheck
, &a_xcb_check_cb
);
464 ev_check_start(globalconf
.loop
, &xcheck
);
465 ev_unref(globalconf
.loop
);
466 ev_prepare_init(&a_refresh
, &a_refresh_cb
);
467 ev_prepare_start(globalconf
.loop
, &a_refresh
);
468 ev_unref(globalconf
.loop
);
470 /* Allocate a handler which will holds all errors and events */
471 xcb_event_handlers_init(globalconf
.connection
, &globalconf
.evenths
);
472 xutil_error_handler_catch_all_set(&globalconf
.evenths
, xerrorstart
, NULL
);
475 screen_nbr
< xcb_setup_roots_length(xcb_get_setup(globalconf
.connection
));
478 const uint32_t select_input_val
= XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT
;
480 /* This causes an error if some other window manager is running */
481 xcb_change_window_attributes(globalconf
.connection
,
482 xutil_screen_get(globalconf
.connection
, screen_nbr
)->root
,
483 XCB_CW_EVENT_MASK
, &select_input_val
);
486 /* Need to xcb_flush to validate error handler */
487 xcb_aux_sync(globalconf
.connection
);
489 /* Process all errors in the queue if any */
490 xcb_event_poll_for_event_loop(&globalconf
.evenths
);
492 /* Set the default xerror handler */
493 xutil_error_handler_catch_all_set(&globalconf
.evenths
, xerror
, NULL
);
495 /* Allocate the key symbols */
496 globalconf
.keysyms
= xcb_key_symbols_alloc(globalconf
.connection
);
497 xcb_get_modifier_mapping_cookie_t xmapping_cookie
=
498 xcb_get_modifier_mapping_unchecked(globalconf
.connection
);
500 /* init atom cache */
501 atoms_init(globalconf
.connection
);
503 /* init screens information */
506 /* init default font and colors */
507 colors_reqs
[0] = xcolor_init_unchecked(&globalconf
.colors
.fg
,
508 "black", sizeof("black") - 1);
510 colors_reqs
[1] = xcolor_init_unchecked(&globalconf
.colors
.bg
,
511 "white", sizeof("white") - 1);
513 globalconf
.font
= draw_font_new("sans 8");
515 for(colors_nbr
= 0; colors_nbr
< 2; colors_nbr
++)
516 xcolor_init_reply(colors_reqs
[colors_nbr
]);
518 xutil_lock_mask_get(globalconf
.connection
, xmapping_cookie
,
519 globalconf
.keysyms
, &globalconf
.numlockmask
,
520 &globalconf
.shiftlockmask
, &globalconf
.capslockmask
,
521 &globalconf
.modeswitchmask
);
523 /* do this only for real screen */
525 screen_nbr
< xcb_setup_roots_length(xcb_get_setup(globalconf
.connection
));
528 /* select for events */
529 const uint32_t change_win_vals
[] =
531 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT
| XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY
532 | XCB_EVENT_MASK_ENTER_WINDOW
| XCB_EVENT_MASK_LEAVE_WINDOW
533 | XCB_EVENT_MASK_STRUCTURE_NOTIFY
534 | XCB_EVENT_MASK_PROPERTY_CHANGE
535 | XCB_EVENT_MASK_BUTTON_PRESS
536 | XCB_EVENT_MASK_BUTTON_RELEASE
537 | XCB_EVENT_MASK_FOCUS_CHANGE
540 xcb_change_window_attributes(globalconf
.connection
,
541 xutil_screen_get(globalconf
.connection
, screen_nbr
)->root
,
544 ewmh_init(screen_nbr
);
545 systray_init(screen_nbr
);
548 /* init spawn (sn) */
551 /* Parse and run configuration file */
552 if (!luaA_parserc(&xdg
, confpath
, true))
553 fatal("couldn't find any rc file");
559 /* scan existing windows */
562 /* process all errors in the queue if any */
563 xcb_event_poll_for_event_loop(&globalconf
.evenths
);
564 a_xcb_set_event_handlers();
565 a_xcb_set_property_handlers();
567 /* we will receive events, stop grabbing server */
568 xcb_ungrab_server(globalconf
.connection
);
569 xcb_flush(globalconf
.connection
);
571 /* main event loop */
572 ev_loop(globalconf
.loop
, 0);
574 /* cleanup event loop */
575 ev_ref(globalconf
.loop
);
576 ev_check_stop(globalconf
.loop
, &xcheck
);
577 ev_ref(globalconf
.loop
);
578 ev_prepare_stop(globalconf
.loop
, &a_refresh
);
579 ev_ref(globalconf
.loop
);
580 ev_io_stop(globalconf
.loop
, &xio
);
587 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80