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 <glib-unix.h>
40 #include "objects/client.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"
57 /** argv used to run awesome */
58 static char *awesome_argv
;
60 /** Call before exiting.
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
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.
99 scan(xcb_query_tree_cookie_t tree_c
)
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
;
108 tree_r
= xcb_query_tree_reply(globalconf
.connection
,
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
,
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
,
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
)
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
)))
163 client_manage(wins
[i
], geom_r
, true);
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
)
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
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");
222 a_glib_poll(GPollFD
*ufds
, guint nfsd
, gint timeout
)
226 res
= g_poll(ufds
, nfsd
, timeout
);
232 signal_fatal(int signum
)
236 fatal("signal %d, dumping backtrace\n%s", signum
, buf
.s
);
239 /** Function to exit on some signals.
240 * \param data currently unused
243 exit_on_signal(gpointer data
)
245 g_main_loop_quit(globalconf
.loop
);
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
260 restart_on_signal(gpointer data
)
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
;
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");
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
;
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' },
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
);
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 */
337 while((opt
= getopt_long(argc
, argv
, "vhkc:a",
338 long_options
, NULL
)) != -1)
345 exit_help(EXIT_SUCCESS
);
352 confpath
= a_strdup(optarg
);
354 fatal("-c option requires a file name");
363 if(!luaA_parserc(&xdg
, confpath
, false))
365 fprintf(stderr
, "✘ Configuration file syntax error.\n");
370 fprintf(stderr
, "✔ Configuration file syntax OK.\n");
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;
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
);
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 */
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
);
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)
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 */
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 */
492 /* init spawn (sn) */
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
,
502 XCB_COPY_FROM_PARENT
, globalconf
.visual
->visual_id
,
503 XCB_CW_BACK_PIXEL
| XCB_CW_BORDER_PIXEL
| XCB_CW_COLORMAP
,
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
,
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");
535 /* scan existing windows */
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);
551 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80