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.
30 #include <xcb/bigreq.h>
31 #include <xcb/randr.h>
32 #include <xcb/xcb_event.h>
33 #include <xcb/xinerama.h>
34 #include <xcb/xtest.h>
35 #include <xcb/shape.h>
37 #include <glib-unix.h>
41 #include "objects/client.h"
50 #include "common/version.h"
51 #include "common/atoms.h"
52 #include "common/xcursor.h"
53 #include "common/xutil.h"
54 #include "common/backtrace.h"
58 /** argv used to run awesome */
59 static char *awesome_argv
;
61 /** time of last main loop wakeup */
62 static struct timeval last_wakeup
;
64 /** current limit for the main loop's runtime */
65 static float main_loop_iteration_limit
= 0.1;
67 /** Call before exiting.
70 awesome_atexit(bool restart
)
72 lua_pushboolean(globalconf
.L
, restart
);
73 signal_object_emit(globalconf
.L
, &global_signals
, "exit", 1);
80 lua_close(globalconf
.L
);
82 /* X11 is a great protocol. There is a save-set so that reparenting WMs
83 * don't kill clients when they shut down. However, when a focused windows
84 * is saved, the focus will move to its parent with revert-to none.
85 * Immediately afterwards, this parent is destroyed and the focus is gone.
86 * Work around this by placing the focus where we like it to be.
88 xcb_set_input_focus(globalconf
.connection
, XCB_INPUT_FOCUS_POINTER_ROOT
,
89 XCB_NONE
, XCB_CURRENT_TIME
);
90 xcb_aux_sync(globalconf
.connection
);
92 /* Disconnect *after* closing lua */
93 xcb_cursor_context_free(globalconf
.cursor_ctx
);
94 xcb_disconnect(globalconf
.connection
);
97 /** Scan X to find windows to manage.
100 scan(xcb_query_tree_cookie_t tree_c
)
103 xcb_query_tree_reply_t
*tree_r
;
104 xcb_window_t
*wins
= NULL
;
105 xcb_get_window_attributes_reply_t
*attr_r
;
106 xcb_get_geometry_reply_t
*geom_r
;
109 tree_r
= xcb_query_tree_reply(globalconf
.connection
,
116 /* Get the tree of the children windows of the current root window */
117 if(!(wins
= xcb_query_tree_children(tree_r
)))
118 fatal("cannot get tree children");
120 tree_c_len
= xcb_query_tree_children_length(tree_r
);
121 xcb_get_window_attributes_cookie_t attr_wins
[tree_c_len
];
122 xcb_get_property_cookie_t state_wins
[tree_c_len
];
123 xcb_get_geometry_cookie_t geom_wins
[tree_c_len
];
125 for(i
= 0; i
< tree_c_len
; i
++)
127 attr_wins
[i
] = xcb_get_window_attributes_unchecked(globalconf
.connection
,
130 state_wins
[i
] = xwindow_get_state_unchecked(wins
[i
]);
131 geom_wins
[i
] = xcb_get_geometry_unchecked(globalconf
.connection
, wins
[i
]);
134 for(i
= 0; i
< tree_c_len
; i
++)
136 attr_r
= xcb_get_window_attributes_reply(globalconf
.connection
,
139 geom_r
= xcb_get_geometry_reply(globalconf
.connection
, geom_wins
[i
], NULL
);
141 state
= xwindow_get_state_reply(state_wins
[i
]);
143 if(!attr_r
|| attr_r
->override_redirect
144 || attr_r
->map_state
== XCB_MAP_STATE_UNMAPPED
145 || state
== XCB_ICCCM_WM_STATE_WITHDRAWN
)
152 client_manage(wins
[i
], geom_r
, attr_r
);
164 xcb_generic_event_t
*mouse
= NULL
, *event
;
166 while((event
= xcb_poll_for_event(globalconf
.connection
)))
168 /* We will treat mouse events later.
169 * We cannot afford to treat all mouse motion events,
170 * because that would be too much CPU intensive, so we just
171 * take the last we get after a bunch of events. */
172 if(XCB_EVENT_RESPONSE_TYPE(event
) == XCB_MOTION_NOTIFY
)
179 uint8_t type
= XCB_EVENT_RESPONSE_TYPE(event
);
180 if(mouse
&& (type
== XCB_ENTER_NOTIFY
|| type
== XCB_LEAVE_NOTIFY
181 || type
== XCB_BUTTON_PRESS
|| type
== XCB_BUTTON_RELEASE
))
183 /* Make sure enter/motion/leave/press/release events are handled
184 * in the correct order */
201 a_xcb_io_cb(GIOChannel
*source
, GIOCondition cond
, gpointer data
)
203 /* a_xcb_check() already handled all events */
205 if(xcb_connection_has_error(globalconf
.connection
))
206 fatal("X server connection broke (error %d)",
207 xcb_connection_has_error(globalconf
.connection
));
213 a_glib_poll(GPollFD
*ufds
, guint nfsd
, gint timeout
)
216 struct timeval now
, length_time
;
219 /* Do all deferred work now */
222 /* Check how long this main loop iteration took */
223 gettimeofday(&now
, NULL
);
224 timersub(&now
, &last_wakeup
, &length_time
);
225 length
= length_time
.tv_sec
+ length_time
.tv_usec
* 1.0f
/ 1e6
;
226 if (length
> main_loop_iteration_limit
) {
227 warn("Last main loop iteration took %.6f seconds! Increasing limit for "
228 "this warning to that value.", length
);
229 main_loop_iteration_limit
= length
;
232 /* Actually do the polling, record time of wakeup and check for new xcb events */
233 res
= g_poll(ufds
, nfsd
, timeout
);
234 gettimeofday(&last_wakeup
, NULL
);
241 signal_fatal(int signum
)
245 fatal("signal %d, dumping backtrace\n%s", signum
, buf
.s
);
248 /** Function to exit on some signals.
249 * \param data currently unused
252 exit_on_signal(gpointer data
)
254 g_main_loop_quit(globalconf
.loop
);
259 awesome_restart(void)
261 awesome_atexit(true);
262 a_exec(awesome_argv
);
265 /** Function to restart awesome on some signals.
266 * \param data currently unused
269 restart_on_signal(gpointer data
)
275 /** Print help and exit(2) with given exit_code.
276 * \param exit_code The exit code.
278 static void __attribute__ ((noreturn
))
279 exit_help(int exit_code
)
281 FILE *outfile
= (exit_code
== EXIT_SUCCESS
) ? stdout
: stderr
;
283 "Usage: awesome [OPTION]\n\
284 -h, --help show help\n\
285 -v, --version show version\n\
286 -c, --config FILE configuration file to use\n\
287 -k, --check check configuration file syntax\n");
291 /** Hello, this is main.
292 * \param argc Who knows.
293 * \param argv Who knows.
294 * \return EXIT_SUCCESS I hope.
297 main(int argc
, char **argv
)
299 char *confpath
= NULL
;
303 bool no_argb
= false;
304 bool run_test
= false;
305 xcb_query_tree_cookie_t tree_c
;
306 static struct option long_options
[] =
308 { "help", 0, NULL
, 'h' },
309 { "version", 0, NULL
, 'v' },
310 { "config", 1, NULL
, 'c' },
311 { "check", 0, NULL
, 'k' },
312 { "no-argb", 0, NULL
, 'a' },
316 /* clear the globalconf structure */
317 p_clear(&globalconf
, 1);
318 globalconf
.keygrabber
= LUA_REFNIL
;
319 globalconf
.mousegrabber
= LUA_REFNIL
;
320 buffer_init(&globalconf
.startup_errors
);
323 for(i
= 0; i
< argc
; i
++)
324 cmdlen
+= a_strlen(argv
[i
]) + 1;
326 awesome_argv
= p_new(char, cmdlen
);
327 a_strcpy(awesome_argv
, cmdlen
, argv
[0]);
329 for(i
= 1; i
< argc
; i
++)
331 a_strcat(awesome_argv
, cmdlen
, " ");
332 a_strcat(awesome_argv
, cmdlen
, argv
[i
]);
335 /* Text won't be printed correctly otherwise */
336 setlocale(LC_CTYPE
, "");
338 /* Get XDG basedir data */
345 while((opt
= getopt_long(argc
, argv
, "vhkc:a",
346 long_options
, NULL
)) != -1)
353 exit_help(EXIT_SUCCESS
);
360 confpath
= a_strdup(optarg
);
362 fatal("-c option requires a file name");
371 if(!luaA_parserc(&xdg
, confpath
, false))
373 fprintf(stderr
, "✘ Configuration file syntax error.\n");
378 fprintf(stderr
, "✔ Configuration file syntax OK.\n");
383 /* register function for signals */
384 g_unix_signal_add(SIGINT
, exit_on_signal
, NULL
);
385 g_unix_signal_add(SIGTERM
, exit_on_signal
, NULL
);
386 g_unix_signal_add(SIGHUP
, restart_on_signal
, NULL
);
388 struct sigaction sa
= { .sa_handler
= signal_fatal
, .sa_flags
= SA_RESETHAND
};
389 sigemptyset(&sa
.sa_mask
);
390 sigaction(SIGABRT
, &sa
, 0);
391 sigaction(SIGBUS
, &sa
, 0);
392 sigaction(SIGFPE
, &sa
, 0);
393 sigaction(SIGILL
, &sa
, 0);
394 sigaction(SIGSEGV
, &sa
, 0);
396 /* We have no clue where the input focus is right now */
397 globalconf
.focus
.need_update
= true;
400 globalconf
.connection
= xcb_connect(NULL
, &globalconf
.default_screen
);
401 if(xcb_connection_has_error(globalconf
.connection
))
402 fatal("cannot open display (error %d)", xcb_connection_has_error(globalconf
.connection
));
404 globalconf
.screen
= xcb_aux_get_screen(globalconf
.connection
, globalconf
.default_screen
);
405 globalconf
.default_visual
= draw_default_visual(globalconf
.screen
);
407 globalconf
.visual
= draw_argb_visual(globalconf
.screen
);
408 if(!globalconf
.visual
)
409 globalconf
.visual
= globalconf
.default_visual
;
410 globalconf
.default_depth
= draw_visual_depth(globalconf
.screen
, globalconf
.visual
->visual_id
);
411 globalconf
.default_cmap
= globalconf
.screen
->default_colormap
;
412 if(globalconf
.default_depth
!= globalconf
.screen
->root_depth
)
414 // We need our own color map if we aren't using the default depth
415 globalconf
.default_cmap
= xcb_generate_id(globalconf
.connection
);
416 xcb_create_colormap(globalconf
.connection
, XCB_COLORMAP_ALLOC_NONE
,
417 globalconf
.default_cmap
, globalconf
.screen
->root
,
418 globalconf
.visual
->visual_id
);
421 /* Prefetch all the extensions we might need */
422 xcb_prefetch_extension_data(globalconf
.connection
, &xcb_big_requests_id
);
423 xcb_prefetch_extension_data(globalconf
.connection
, &xcb_test_id
);
424 xcb_prefetch_extension_data(globalconf
.connection
, &xcb_randr_id
);
425 xcb_prefetch_extension_data(globalconf
.connection
, &xcb_xinerama_id
);
426 xcb_prefetch_extension_data(globalconf
.connection
, &xcb_shape_id
);
428 if (xcb_cursor_context_new(globalconf
.connection
, globalconf
.screen
, &globalconf
.cursor_ctx
) < 0)
429 fatal("Failed to initialize xcb-cursor");
431 /* initialize dbus */
434 /* Get the file descriptor corresponding to the X connection */
435 xfd
= xcb_get_file_descriptor(globalconf
.connection
);
436 GIOChannel
*channel
= g_io_channel_unix_new(xfd
);
437 g_io_add_watch(channel
, G_IO_IN
, a_xcb_io_cb
, NULL
);
438 g_io_channel_unref(channel
);
441 xcb_grab_server(globalconf
.connection
);
444 const uint32_t select_input_val
= XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT
;
445 xcb_void_cookie_t cookie
;
447 /* This causes an error if some other window manager is running */
448 cookie
= xcb_change_window_attributes_checked(globalconf
.connection
,
449 globalconf
.screen
->root
,
450 XCB_CW_EVENT_MASK
, &select_input_val
);
451 if (xcb_request_check(globalconf
.connection
, cookie
))
452 fatal("another window manager is already running");
455 /* Prefetch the maximum request length */
456 xcb_prefetch_maximum_request_length(globalconf
.connection
);
458 /* check for xtest extension */
459 const xcb_query_extension_reply_t
*query
;
460 query
= xcb_get_extension_data(globalconf
.connection
, &xcb_test_id
);
461 globalconf
.have_xtest
= query
->present
;
463 /* check for shape extension */
464 query
= xcb_get_extension_data(globalconf
.connection
, &xcb_shape_id
);
465 globalconf
.have_shape
= query
->present
;
467 /* Allocate the key symbols */
468 globalconf
.keysyms
= xcb_key_symbols_alloc(globalconf
.connection
);
469 xcb_get_modifier_mapping_cookie_t xmapping_cookie
=
470 xcb_get_modifier_mapping_unchecked(globalconf
.connection
);
472 /* init atom cache */
473 atoms_init(globalconf
.connection
);
475 /* init screens information */
478 xutil_lock_mask_get(globalconf
.connection
, xmapping_cookie
,
479 globalconf
.keysyms
, &globalconf
.numlockmask
,
480 &globalconf
.shiftlockmask
, &globalconf
.capslockmask
,
481 &globalconf
.modeswitchmask
);
483 /* do this only for real screen */
487 /* init spawn (sn) */
490 /* The default GC is just a newly created associated with a window with
491 * depth globalconf.default_depth */
492 xcb_window_t tmp_win
= xcb_generate_id(globalconf
.connection
);
493 globalconf
.gc
= xcb_generate_id(globalconf
.connection
);
494 xcb_create_window(globalconf
.connection
, globalconf
.default_depth
,
495 tmp_win
, globalconf
.screen
->root
,
497 XCB_COPY_FROM_PARENT
, globalconf
.visual
->visual_id
,
498 XCB_CW_BACK_PIXEL
| XCB_CW_BORDER_PIXEL
| XCB_CW_COLORMAP
,
501 globalconf
.screen
->black_pixel
,
502 globalconf
.screen
->black_pixel
,
503 globalconf
.default_cmap
505 xcb_create_gc(globalconf
.connection
, globalconf
.gc
, tmp_win
, XCB_GC_FOREGROUND
| XCB_GC_BACKGROUND
,
506 (const uint32_t[]) { globalconf
.screen
->black_pixel
, globalconf
.screen
->white_pixel
});
507 xcb_destroy_window(globalconf
.connection
, tmp_win
);
509 /* Get the window tree associated to this screen */
510 tree_c
= xcb_query_tree_unchecked(globalconf
.connection
,
511 globalconf
.screen
->root
);
513 xcb_change_window_attributes(globalconf
.connection
,
514 globalconf
.screen
->root
,
516 ROOT_WINDOW_EVENT_MASK
);
518 /* we will receive events, stop grabbing server */
519 xcb_ungrab_server(globalconf
.connection
);
520 xcb_flush(globalconf
.connection
);
522 /* Parse and run configuration file */
523 if (!luaA_parserc(&xdg
, confpath
, true))
524 fatal("couldn't find any rc file");
530 /* scan existing windows */
533 xcb_flush(globalconf
.connection
);
535 /* Setup the main context */
536 g_main_context_set_poll_func(g_main_context_default(), &a_glib_poll
);
537 gettimeofday(&last_wakeup
, NULL
);
539 /* main event loop */
540 globalconf
.loop
= g_main_loop_new(NULL
, FALSE
);
541 g_main_loop_run(globalconf
.loop
);
542 g_main_loop_unref(globalconf
.loop
);
543 globalconf
.loop
= NULL
;
545 awesome_atexit(false);
550 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80