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/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>
43 #include "objects/client.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"
60 /** argv used to run awesome */
61 static char *awesome_argv
;
63 /** Call before exiting.
66 awesome_atexit(bool restart
)
68 lua_pushboolean(globalconf
.L
, restart
);
69 signal_object_emit(globalconf
.L
, &global_signals
, "exit", 1);
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.
95 scan(xcb_query_tree_cookie_t tree_c
)
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
;
104 tree_r
= xcb_query_tree_reply(globalconf
.connection
,
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
,
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
,
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
)
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
)))
159 client_manage(wins
[i
], geom_r
, true);
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
)
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
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");
218 a_glib_poll(GPollFD
*ufds
, guint nfsd
, gint timeout
)
222 res
= g_poll(ufds
, nfsd
, timeout
);
228 signal_fatal(int signum
)
232 fatal("signal %d, dumping backtrace\n%s", signum
, buf
.s
);
235 /** Function to exit on some signals.
236 * \param data currently unused
239 exit_on_signal(gpointer data
)
241 g_main_loop_quit(globalconf
.loop
);
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
256 restart_on_signal(gpointer data
)
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
;
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");
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
;
290 bool no_argb
= false;
291 bool run_test
= false;
292 xcb_generic_event_t
*event
;
293 xcb_query_tree_cookie_t tree_c
;
294 static struct option long_options
[] =
296 { "help", 0, NULL
, 'h' },
297 { "version", 0, NULL
, 'v' },
298 { "config", 1, NULL
, 'c' },
299 { "check", 0, NULL
, 'k' },
300 { "no-argb", 0, NULL
, 'a' },
304 /* clear the globalconf structure */
305 p_clear(&globalconf
, 1);
306 globalconf
.keygrabber
= LUA_REFNIL
;
307 globalconf
.mousegrabber
= LUA_REFNIL
;
308 buffer_init(&globalconf
.startup_errors
);
311 for(i
= 0; i
< argc
; i
++)
312 cmdlen
+= a_strlen(argv
[i
]) + 1;
314 awesome_argv
= p_new(char, cmdlen
);
315 a_strcpy(awesome_argv
, cmdlen
, argv
[0]);
317 for(i
= 1; i
< argc
; i
++)
319 a_strcat(awesome_argv
, cmdlen
, " ");
320 a_strcat(awesome_argv
, cmdlen
, argv
[i
]);
323 /* Text won't be printed correctly otherwise */
324 setlocale(LC_CTYPE
, "");
326 /* Get XDG basedir data */
333 while((opt
= getopt_long(argc
, argv
, "vhkc:a",
334 long_options
, NULL
)) != -1)
341 exit_help(EXIT_SUCCESS
);
348 confpath
= a_strdup(optarg
);
350 fatal("-c option requires a file name");
359 if(!luaA_parserc(&xdg
, confpath
, false))
361 fprintf(stderr
, "✘ Configuration file syntax error.\n");
366 fprintf(stderr
, "✔ Configuration file syntax OK.\n");
371 /* register function for signals */
372 g_unix_signal_add(SIGINT
, exit_on_signal
, NULL
);
373 g_unix_signal_add(SIGTERM
, exit_on_signal
, NULL
);
374 g_unix_signal_add(SIGHUP
, restart_on_signal
, NULL
);
376 struct sigaction sa
= { .sa_handler
= signal_fatal
, .sa_flags
= 0 };
377 sigemptyset(&sa
.sa_mask
);
378 sigaction(SIGSEGV
, &sa
, 0);
381 XkbIgnoreExtension(True
);
384 globalconf
.display
= XOpenDisplay(NULL
);
385 if (globalconf
.display
== NULL
) {
386 fatal("cannot open display");
388 XSetEventQueueOwner(globalconf
.display
, XCBOwnsEventQueue
);
389 globalconf
.default_screen
= XDefaultScreen(globalconf
.display
);
390 globalconf
.connection
= XGetXCBConnection(globalconf
.display
);
391 /* We have no clue where the input focus is right now */
392 globalconf
.focus
.need_update
= true;
394 /* Double checking that connection is good and operatable with xcb */
395 if(xcb_connection_has_error(globalconf
.connection
))
396 fatal("cannot open display");
398 globalconf
.screen
= xcb_aux_get_screen(globalconf
.connection
, globalconf
.default_screen
);
399 globalconf
.default_visual
= draw_default_visual(globalconf
.screen
);
401 globalconf
.visual
= draw_argb_visual(globalconf
.screen
);
402 if(!globalconf
.visual
)
403 globalconf
.visual
= globalconf
.default_visual
;
404 globalconf
.default_depth
= draw_visual_depth(globalconf
.screen
, globalconf
.visual
->visual_id
);
405 globalconf
.default_cmap
= globalconf
.screen
->default_colormap
;
406 if(globalconf
.default_depth
!= globalconf
.screen
->root_depth
)
408 // We need our own color map if we aren't using the default depth
409 globalconf
.default_cmap
= xcb_generate_id(globalconf
.connection
);
410 xcb_create_colormap(globalconf
.connection
, XCB_COLORMAP_ALLOC_NONE
,
411 globalconf
.default_cmap
, globalconf
.screen
->root
,
412 globalconf
.visual
->visual_id
);
415 /* Prefetch all the extensions we might need */
416 xcb_prefetch_extension_data(globalconf
.connection
, &xcb_big_requests_id
);
417 xcb_prefetch_extension_data(globalconf
.connection
, &xcb_test_id
);
418 xcb_prefetch_extension_data(globalconf
.connection
, &xcb_randr_id
);
419 xcb_prefetch_extension_data(globalconf
.connection
, &xcb_xinerama_id
);
420 xcb_prefetch_extension_data(globalconf
.connection
, &xcb_shape_id
);
422 /* Setup the main context */
423 g_main_context_set_poll_func(g_main_context_default(), &a_glib_poll
);
425 /* initialize dbus */
428 /* Get the file descriptor corresponding to the X connection */
429 xfd
= xcb_get_file_descriptor(globalconf
.connection
);
430 GIOChannel
*channel
= g_io_channel_unix_new(xfd
);
431 g_io_add_watch(channel
, G_IO_IN
, a_xcb_io_cb
, NULL
);
432 g_io_channel_unref(channel
);
435 xcb_grab_server(globalconf
.connection
);
437 /* Make sure there are no pending events. Since we didn't really do anything
438 * at all yet, we will just discard all events which we received so far.
439 * The above GrabServer should make sure no new events are generated. */
440 xcb_aux_sync(globalconf
.connection
);
441 while ((event
= xcb_poll_for_event(globalconf
.connection
)) != NULL
)
443 /* Make sure errors are printed */
444 uint8_t response_type
= XCB_EVENT_RESPONSE_TYPE(event
);
445 if(response_type
== 0)
451 const uint32_t select_input_val
= XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT
;
453 /* This causes an error if some other window manager is running */
454 xcb_change_window_attributes(globalconf
.connection
,
455 globalconf
.screen
->root
,
456 XCB_CW_EVENT_MASK
, &select_input_val
);
459 /* Need to xcb_flush to validate error handler */
460 xcb_aux_sync(globalconf
.connection
);
462 /* Process all errors in the queue if any. There can be no events yet, so if
463 * this function returns something, it must be an error. */
464 if (xcb_poll_for_event(globalconf
.connection
) != NULL
)
465 fatal("another window manager is already running");
467 /* Prefetch the maximum request length */
468 xcb_prefetch_maximum_request_length(globalconf
.connection
);
470 /* check for xtest extension */
471 const xcb_query_extension_reply_t
*xtest_query
;
472 xtest_query
= xcb_get_extension_data(globalconf
.connection
, &xcb_test_id
);
473 globalconf
.have_xtest
= xtest_query
->present
;
475 /* Allocate the key symbols */
476 globalconf
.keysyms
= xcb_key_symbols_alloc(globalconf
.connection
);
477 xcb_get_modifier_mapping_cookie_t xmapping_cookie
=
478 xcb_get_modifier_mapping_unchecked(globalconf
.connection
);
480 /* init atom cache */
481 atoms_init(globalconf
.connection
);
483 /* init screens information */
486 xutil_lock_mask_get(globalconf
.connection
, xmapping_cookie
,
487 globalconf
.keysyms
, &globalconf
.numlockmask
,
488 &globalconf
.shiftlockmask
, &globalconf
.capslockmask
,
489 &globalconf
.modeswitchmask
);
491 /* do this only for real screen */
495 /* init spawn (sn) */
498 /* The default GC is just a newly created associated with a window with
499 * depth globalconf.default_depth */
500 xcb_window_t tmp_win
= xcb_generate_id(globalconf
.connection
);
501 globalconf
.gc
= xcb_generate_id(globalconf
.connection
);
502 xcb_create_window(globalconf
.connection
, globalconf
.default_depth
,
503 tmp_win
, globalconf
.screen
->root
,
505 XCB_COPY_FROM_PARENT
, globalconf
.visual
->visual_id
,
506 XCB_CW_BACK_PIXEL
| XCB_CW_BORDER_PIXEL
| XCB_CW_COLORMAP
,
509 globalconf
.screen
->black_pixel
,
510 globalconf
.screen
->black_pixel
,
511 globalconf
.default_cmap
513 xcb_create_gc(globalconf
.connection
, globalconf
.gc
, tmp_win
, XCB_GC_FOREGROUND
| XCB_GC_BACKGROUND
,
514 (const uint32_t[]) { globalconf
.screen
->black_pixel
, globalconf
.screen
->white_pixel
});
515 xcb_destroy_window(globalconf
.connection
, tmp_win
);
517 /* Get the window tree associated to this screen */
518 tree_c
= xcb_query_tree_unchecked(globalconf
.connection
,
519 globalconf
.screen
->root
);
521 xcb_change_window_attributes(globalconf
.connection
,
522 globalconf
.screen
->root
,
524 ROOT_WINDOW_EVENT_MASK
);
526 /* we will receive events, stop grabbing server */
527 xcb_ungrab_server(globalconf
.connection
);
529 /* Parse and run configuration file */
530 if (!luaA_parserc(&xdg
, confpath
, true))
531 fatal("couldn't find any rc file");
537 /* scan existing windows */
540 xcb_flush(globalconf
.connection
);
542 /* main event loop */
543 globalconf
.loop
= g_main_loop_new(NULL
, FALSE
);
544 g_main_loop_run(globalconf
.loop
);
545 g_main_loop_unref(globalconf
.loop
);
546 globalconf
.loop
= NULL
;
548 awesome_atexit(false);
553 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80