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>
38 #include <glib-unix.h>
42 #include "objects/client.h"
51 #include "common/version.h"
52 #include "common/atoms.h"
53 #include "common/xcursor.h"
54 #include "common/xutil.h"
55 #include "common/backtrace.h"
59 /** argv used to run awesome */
60 static char *awesome_argv
;
62 /** Call before exiting.
65 awesome_atexit(bool restart
)
67 lua_pushboolean(globalconf
.L
, restart
);
68 signal_object_emit(globalconf
.L
, &global_signals
, "exit", 1);
75 lua_close(globalconf
.L
);
77 xcb_flush(globalconf
.connection
);
79 /* Disconnect *after* closing lua */
80 xcb_disconnect(globalconf
.connection
);
83 /** Scan X to find windows to manage.
86 scan(xcb_query_tree_cookie_t tree_c
)
89 xcb_query_tree_reply_t
*tree_r
;
90 xcb_window_t
*wins
= NULL
;
91 xcb_get_window_attributes_reply_t
*attr_r
;
92 xcb_get_geometry_reply_t
*geom_r
;
95 tree_r
= xcb_query_tree_reply(globalconf
.connection
,
102 /* Get the tree of the children windows of the current root window */
103 if(!(wins
= xcb_query_tree_children(tree_r
)))
104 fatal("cannot get tree children");
106 tree_c_len
= xcb_query_tree_children_length(tree_r
);
107 xcb_get_window_attributes_cookie_t attr_wins
[tree_c_len
];
108 xcb_get_property_cookie_t state_wins
[tree_c_len
];
110 for(i
= 0; i
< tree_c_len
; i
++)
112 attr_wins
[i
] = xcb_get_window_attributes_unchecked(globalconf
.connection
,
115 state_wins
[i
] = xwindow_get_state_unchecked(wins
[i
]);
118 xcb_get_geometry_cookie_t
*geom_wins
[tree_c_len
];
120 for(i
= 0; i
< tree_c_len
; i
++)
122 attr_r
= xcb_get_window_attributes_reply(globalconf
.connection
,
126 state
= xwindow_get_state_reply(state_wins
[i
]);
128 if(!attr_r
|| attr_r
->override_redirect
129 || attr_r
->map_state
== XCB_MAP_STATE_UNMAPPED
130 || state
== XCB_ICCCM_WM_STATE_WITHDRAWN
)
139 /* Get the geometry of the current window */
140 geom_wins
[i
] = p_alloca(xcb_get_geometry_cookie_t
, 1);
141 *(geom_wins
[i
]) = xcb_get_geometry_unchecked(globalconf
.connection
, wins
[i
]);
144 for(i
= 0; i
< tree_c_len
; i
++)
146 if(!geom_wins
[i
] || !(geom_r
= xcb_get_geometry_reply(globalconf
.connection
,
147 *(geom_wins
[i
]), NULL
)))
150 client_manage(wins
[i
], geom_r
, true);
161 xcb_generic_event_t
*mouse
= NULL
, *event
;
163 while((event
= xcb_poll_for_event(globalconf
.connection
)))
165 /* We will treat mouse events later.
166 * We cannot afford to treat all mouse motion events,
167 * because that would be too much CPU intensive, so we just
168 * take the last we get after a bunch of events. */
169 if(XCB_EVENT_RESPONSE_TYPE(event
) == XCB_MOTION_NOTIFY
)
176 uint8_t type
= XCB_EVENT_RESPONSE_TYPE(event
);
177 if((type
== XCB_ENTER_NOTIFY
|| type
== XCB_LEAVE_NOTIFY
) && mouse
)
179 /* Make sure enter/motion/leave events are handled in the
198 a_xcb_io_cb(GIOChannel
*source
, GIOCondition cond
, gpointer data
)
200 /* a_xcb_check() already handled all events */
202 if(xcb_connection_has_error(globalconf
.connection
))
203 fatal("X server connection broke");
209 a_glib_poll(GPollFD
*ufds
, guint nfsd
, gint timeout
)
213 res
= g_poll(ufds
, nfsd
, timeout
);
219 signal_fatal(int signum
)
223 fatal("signal %d, dumping backtrace\n%s", signum
, buf
.s
);
226 /** Function to exit on some signals.
227 * \param data currently unused
230 exit_on_signal(gpointer data
)
232 g_main_loop_quit(globalconf
.loop
);
237 awesome_restart(void)
239 awesome_atexit(true);
240 a_exec(awesome_argv
);
243 /** Function to restart awesome on some signals.
244 * \param data currently unused
247 restart_on_signal(gpointer data
)
253 /** Print help and exit(2) with given exit_code.
254 * \param exit_code The exit code.
256 static void __attribute__ ((noreturn
))
257 exit_help(int exit_code
)
259 FILE *outfile
= (exit_code
== EXIT_SUCCESS
) ? stdout
: stderr
;
261 "Usage: awesome [OPTION]\n\
262 -h, --help show help\n\
263 -v, --version show version\n\
264 -c, --config FILE configuration file to use\n\
265 -k, --check check configuration file syntax\n");
269 /** Hello, this is main.
270 * \param argc Who knows.
271 * \param argv Who knows.
272 * \return EXIT_SUCCESS I hope.
275 main(int argc
, char **argv
)
277 char *confpath
= NULL
;
281 bool no_argb
= false;
282 xcb_generic_event_t
*event
;
283 xcb_query_tree_cookie_t tree_c
;
284 static struct option long_options
[] =
286 { "help", 0, NULL
, 'h' },
287 { "version", 0, NULL
, 'v' },
288 { "config", 1, NULL
, 'c' },
289 { "check", 0, NULL
, 'k' },
290 { "no-argb", 0, NULL
, 'a' },
294 /* clear the globalconf structure */
295 p_clear(&globalconf
, 1);
296 globalconf
.keygrabber
= LUA_REFNIL
;
297 globalconf
.mousegrabber
= LUA_REFNIL
;
298 buffer_init(&globalconf
.startup_errors
);
301 for(i
= 0; i
< argc
; i
++)
302 cmdlen
+= a_strlen(argv
[i
]) + 1;
304 awesome_argv
= p_new(char, cmdlen
);
305 a_strcpy(awesome_argv
, cmdlen
, argv
[0]);
307 for(i
= 1; i
< argc
; i
++)
309 a_strcat(awesome_argv
, cmdlen
, " ");
310 a_strcat(awesome_argv
, cmdlen
, argv
[i
]);
313 /* Text won't be printed correctly otherwise */
314 setlocale(LC_CTYPE
, "");
316 /* Get XDG basedir data */
323 while((opt
= getopt_long(argc
, argv
, "vhkc:a",
324 long_options
, NULL
)) != -1)
331 exit_help(EXIT_SUCCESS
);
334 if(!luaA_parserc(&xdg
, confpath
, false))
336 fprintf(stderr
, "✘ Configuration file syntax error.\n");
341 fprintf(stderr
, "✔ Configuration file syntax OK.\n");
346 confpath
= a_strdup(optarg
);
348 fatal("-c option requires a file name");
355 /* register function for signals */
356 g_unix_signal_add(SIGINT
, exit_on_signal
, NULL
);
357 g_unix_signal_add(SIGTERM
, exit_on_signal
, NULL
);
358 g_unix_signal_add(SIGHUP
, restart_on_signal
, NULL
);
360 struct sigaction sa
= { .sa_handler
= signal_fatal
, .sa_flags
= 0 };
361 sigemptyset(&sa
.sa_mask
);
362 sigaction(SIGSEGV
, &sa
, 0);
365 globalconf
.display
= XOpenDisplay(NULL
);
366 if (globalconf
.display
== NULL
) {
367 fatal("cannot open display");
369 XSetEventQueueOwner(globalconf
.display
, XCBOwnsEventQueue
);
370 globalconf
.default_screen
= XDefaultScreen(globalconf
.display
);
371 globalconf
.connection
= XGetXCBConnection(globalconf
.display
);
373 /* Double checking that connection is good and operatable with xcb */
374 if(xcb_connection_has_error(globalconf
.connection
))
375 fatal("cannot open display");
377 globalconf
.screen
= xcb_aux_get_screen(globalconf
.connection
, globalconf
.default_screen
);
378 globalconf
.default_visual
= draw_default_visual(globalconf
.screen
);
380 globalconf
.visual
= draw_argb_visual(globalconf
.screen
);
381 if(!globalconf
.visual
)
382 globalconf
.visual
= globalconf
.default_visual
;
383 globalconf
.default_depth
= draw_visual_depth(globalconf
.screen
, globalconf
.visual
->visual_id
);
384 globalconf
.default_cmap
= globalconf
.screen
->default_colormap
;
385 if(globalconf
.default_depth
!= globalconf
.screen
->root_depth
)
387 // We need our own color map if we aren't using the default depth
388 globalconf
.default_cmap
= xcb_generate_id(globalconf
.connection
);
389 xcb_create_colormap(globalconf
.connection
, XCB_COLORMAP_ALLOC_NONE
,
390 globalconf
.default_cmap
, globalconf
.screen
->root
,
391 globalconf
.visual
->visual_id
);
394 /* Prefetch all the extensions we might need */
395 xcb_prefetch_extension_data(globalconf
.connection
, &xcb_big_requests_id
);
396 xcb_prefetch_extension_data(globalconf
.connection
, &xcb_test_id
);
397 xcb_prefetch_extension_data(globalconf
.connection
, &xcb_randr_id
);
398 xcb_prefetch_extension_data(globalconf
.connection
, &xcb_xinerama_id
);
399 xcb_prefetch_extension_data(globalconf
.connection
, &xcb_shape_id
);
401 /* Setup the main context */
402 g_main_context_set_poll_func(g_main_context_default(), &a_glib_poll
);
404 /* initialize dbus */
407 /* Get the file descriptor corresponding to the X connection */
408 xfd
= xcb_get_file_descriptor(globalconf
.connection
);
409 GIOChannel
*channel
= g_io_channel_unix_new(xfd
);
410 g_io_add_watch(channel
, G_IO_IN
, a_xcb_io_cb
, NULL
);
411 g_io_channel_unref(channel
);
414 xcb_grab_server(globalconf
.connection
);
416 /* Make sure there are no pending events. Since we didn't really do anything
417 * at all yet, we will just discard all events which we received so far.
418 * The above GrabServer should make sure no new events are generated. */
419 xcb_aux_sync(globalconf
.connection
);
420 while ((event
= xcb_poll_for_event(globalconf
.connection
)) != NULL
)
422 /* Make sure errors are printed */
423 uint8_t response_type
= XCB_EVENT_RESPONSE_TYPE(event
);
424 if(response_type
== 0)
430 const uint32_t select_input_val
= XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT
;
432 /* This causes an error if some other window manager is running */
433 xcb_change_window_attributes(globalconf
.connection
,
434 globalconf
.screen
->root
,
435 XCB_CW_EVENT_MASK
, &select_input_val
);
438 /* Need to xcb_flush to validate error handler */
439 xcb_aux_sync(globalconf
.connection
);
441 /* Process all errors in the queue if any. There can be no events yet, so if
442 * this function returns something, it must be an error. */
443 if (xcb_poll_for_event(globalconf
.connection
) != NULL
)
444 fatal("another window manager is already running");
446 /* Prefetch the maximum request length */
447 xcb_prefetch_maximum_request_length(globalconf
.connection
);
449 /* check for xtest extension */
450 const xcb_query_extension_reply_t
*xtest_query
;
451 xtest_query
= xcb_get_extension_data(globalconf
.connection
, &xcb_test_id
);
452 globalconf
.have_xtest
= xtest_query
->present
;
454 /* Allocate the key symbols */
455 globalconf
.keysyms
= xcb_key_symbols_alloc(globalconf
.connection
);
456 xcb_get_modifier_mapping_cookie_t xmapping_cookie
=
457 xcb_get_modifier_mapping_unchecked(globalconf
.connection
);
459 /* init atom cache */
460 atoms_init(globalconf
.connection
);
462 /* init screens information */
465 xutil_lock_mask_get(globalconf
.connection
, xmapping_cookie
,
466 globalconf
.keysyms
, &globalconf
.numlockmask
,
467 &globalconf
.shiftlockmask
, &globalconf
.capslockmask
,
468 &globalconf
.modeswitchmask
);
470 /* do this only for real screen */
474 /* init spawn (sn) */
477 /* The default GC is just a newly created associated with a window with
478 * depth globalconf.default_depth */
479 xcb_window_t tmp_win
= xcb_generate_id(globalconf
.connection
);
480 globalconf
.gc
= xcb_generate_id(globalconf
.connection
);
481 xcb_create_window(globalconf
.connection
, globalconf
.default_depth
,
482 tmp_win
, globalconf
.screen
->root
,
484 XCB_COPY_FROM_PARENT
, globalconf
.visual
->visual_id
,
485 XCB_CW_BACK_PIXEL
| XCB_CW_BORDER_PIXEL
| XCB_CW_COLORMAP
,
488 globalconf
.screen
->black_pixel
,
489 globalconf
.screen
->black_pixel
,
490 globalconf
.default_cmap
492 xcb_create_gc(globalconf
.connection
, globalconf
.gc
, tmp_win
, XCB_GC_FOREGROUND
| XCB_GC_BACKGROUND
,
493 (const uint32_t[]) { globalconf
.screen
->black_pixel
, globalconf
.screen
->white_pixel
});
494 xcb_destroy_window(globalconf
.connection
, tmp_win
);
496 /* Get the window tree associated to this screen */
497 tree_c
= xcb_query_tree_unchecked(globalconf
.connection
,
498 globalconf
.screen
->root
);
500 xcb_change_window_attributes(globalconf
.connection
,
501 globalconf
.screen
->root
,
503 ROOT_WINDOW_EVENT_MASK
);
505 /* we will receive events, stop grabbing server */
506 xcb_ungrab_server(globalconf
.connection
);
508 /* Parse and run configuration file */
509 if (!luaA_parserc(&xdg
, confpath
, true))
510 fatal("couldn't find any rc file");
516 /* scan existing windows */
519 xcb_flush(globalconf
.connection
);
521 /* main event loop */
522 globalconf
.loop
= g_main_loop_new(NULL
, FALSE
);
523 g_main_loop_run(globalconf
.loop
);
524 g_main_loop_unref(globalconf
.loop
);
525 globalconf
.loop
= NULL
;
527 awesome_atexit(false);
532 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80