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>
37 #include "objects/client.h"
46 #include "common/version.h"
47 #include "common/atoms.h"
48 #include "common/xcursor.h"
49 #include "common/xutil.h"
50 #include "common/backtrace.h"
54 /** argv used to run awesome */
55 static char *awesome_argv
;
57 /** Call before exiting.
62 signal_object_emit(globalconf
.L
, &global_signals
, "exit", 0);
69 lua_close(globalconf
.L
);
71 xcb_flush(globalconf
.connection
);
73 /* Disconnect *after* closing lua */
74 xcb_disconnect(globalconf
.connection
);
79 /** Scan X to find windows to manage.
85 xcb_query_tree_cookie_t tree_c
;
86 xcb_query_tree_reply_t
*tree_r
;
87 xcb_window_t
*wins
= NULL
;
88 xcb_get_window_attributes_reply_t
*attr_r
;
89 xcb_get_geometry_reply_t
*geom_r
;
92 /* Get the window tree associated to this screen */
93 tree_c
= xcb_query_tree_unchecked(globalconf
.connection
,
94 globalconf
.screen
->root
);
96 tree_r
= xcb_query_tree_reply(globalconf
.connection
,
103 /* Get the tree of the children windows of the current root window */
104 if(!(wins
= xcb_query_tree_children(tree_r
)))
105 fatal("cannot get tree children");
107 tree_c_len
= xcb_query_tree_children_length(tree_r
);
108 xcb_get_window_attributes_cookie_t attr_wins
[tree_c_len
];
109 xcb_get_property_cookie_t state_wins
[tree_c_len
];
111 for(i
= 0; i
< tree_c_len
; i
++)
113 attr_wins
[i
] = xcb_get_window_attributes_unchecked(globalconf
.connection
,
116 state_wins
[i
] = xwindow_get_state_unchecked(wins
[i
]);
119 xcb_get_geometry_cookie_t
*geom_wins
[tree_c_len
];
121 for(i
= 0; i
< tree_c_len
; i
++)
123 attr_r
= xcb_get_window_attributes_reply(globalconf
.connection
,
127 state
= xwindow_get_state_reply(state_wins
[i
]);
129 if(!attr_r
|| attr_r
->override_redirect
130 || attr_r
->map_state
== XCB_MAP_STATE_UNMAPPED
131 || state
== XCB_WM_STATE_WITHDRAWN
)
140 /* Get the geometry of the current window */
141 geom_wins
[i
] = p_alloca(xcb_get_geometry_cookie_t
, 1);
142 *(geom_wins
[i
]) = xcb_get_geometry_unchecked(globalconf
.connection
, wins
[i
]);
145 for(i
= 0; i
< tree_c_len
; i
++)
147 if(!geom_wins
[i
] || !(geom_r
= xcb_get_geometry_reply(globalconf
.connection
,
148 *(geom_wins
[i
]), NULL
)))
151 client_manage(wins
[i
], geom_r
, true);
159 static xcb_visualtype_t
*
160 screen_default_visual(xcb_screen_t
*s
)
162 xcb_depth_iterator_t depth_iter
= xcb_screen_allowed_depths_iterator(s
);
165 for(; depth_iter
.rem
; xcb_depth_next (&depth_iter
))
166 for(xcb_visualtype_iterator_t visual_iter
= xcb_depth_visuals_iterator(depth_iter
.data
);
167 visual_iter
.rem
; xcb_visualtype_next (&visual_iter
))
168 if(s
->root_visual
== visual_iter
.data
->visual_id
)
169 return visual_iter
.data
;
175 a_refresh_cb(EV_P_ ev_prepare
*w
, int revents
)
181 a_xcb_check_cb(EV_P_ ev_check
*w
, int revents
)
183 xcb_generic_event_t
*mouse
= NULL
, *event
;
185 while((event
= xcb_poll_for_event(globalconf
.connection
)))
187 /* We will treat mouse events later.
188 * We cannot afford to treat all mouse motion events,
189 * because that would be too much CPU intensive, so we just
190 * take the last we get after a bunch of events. */
191 if(XCB_EVENT_RESPONSE_TYPE(event
) == XCB_MOTION_NOTIFY
)
211 a_xcb_io_cb(EV_P_ ev_io
*w
, int revents
)
217 signal_fatal(int signum
)
221 fatal("dumping backtrace\n%s", buf
.s
);
224 /** Function to exit on some signals.
225 * \param w the signal received, unused
226 * \param revents currently unused
229 exit_on_signal(EV_P_ ev_signal
*w
, int revents
)
235 awesome_restart(void)
238 a_exec(awesome_argv
);
241 /** Function to restart awesome on some signals.
242 * \param w the signal received, unused
243 * \param revents Currently unused
246 restart_on_signal(EV_P_ ev_signal
*w
, int revents
)
251 /** Print help and exit(2) with given exit_code.
252 * \param exit_code The exit code.
254 static void __attribute__ ((noreturn
))
255 exit_help(int exit_code
)
257 FILE *outfile
= (exit_code
== EXIT_SUCCESS
) ? stdout
: stderr
;
259 "Usage: awesome [OPTION]\n\
260 -h, --help show help\n\
261 -v, --version show version\n\
262 -c, --config FILE configuration file to use\n\
263 -k, --check check configuration file syntax\n");
267 /** Hello, this is main.
268 * \param argc Who knows.
269 * \param argv Who knows.
270 * \return EXIT_SUCCESS I hope.
273 main(int argc
, char **argv
)
275 char *confpath
= NULL
;
276 int xfd
, i
, opt
, colors_nbr
;
277 xcolor_init_request_t colors_reqs
[2];
280 static struct option long_options
[] =
282 { "help", 0, NULL
, 'h' },
283 { "version", 0, NULL
, 'v' },
284 { "config", 1, NULL
, 'c' },
285 { "check", 0, NULL
, 'k' },
289 /* event loop watchers */
290 ev_io xio
= { .fd
= -1 };
292 ev_prepare a_refresh
;
297 /* clear the globalconf structure */
298 p_clear(&globalconf
, 1);
299 globalconf
.keygrabber
= LUA_REFNIL
;
300 globalconf
.mousegrabber
= LUA_REFNIL
;
303 for(i
= 0; i
< argc
; i
++)
304 cmdlen
+= a_strlen(argv
[i
]) + 1;
306 awesome_argv
= p_new(char, cmdlen
);
307 a_strcpy(awesome_argv
, cmdlen
, argv
[0]);
309 for(i
= 1; i
< argc
; i
++)
311 a_strcat(awesome_argv
, cmdlen
, " ");
312 a_strcat(awesome_argv
, cmdlen
, argv
[i
]);
315 /* Text won't be printed correctly otherwise */
316 setlocale(LC_CTYPE
, "");
318 /* Get XDG basedir data */
325 while((opt
= getopt_long(argc
, argv
, "vhkc:",
326 long_options
, NULL
)) != -1)
333 exit_help(EXIT_SUCCESS
);
336 if(!luaA_parserc(&xdg
, confpath
, false))
338 fprintf(stderr
, "✘ Configuration file syntax error.\n");
343 fprintf(stderr
, "✔ Configuration file syntax OK.\n");
348 confpath
= a_strdup(optarg
);
350 fatal("-c option requires a file name");
354 globalconf
.loop
= ev_default_loop(EVFLAG_NOSIGFD
);
356 /* register function for signals */
357 ev_signal_init(&sigint
, exit_on_signal
, SIGINT
);
358 ev_signal_init(&sigterm
, exit_on_signal
, SIGTERM
);
359 ev_signal_init(&sighup
, restart_on_signal
, SIGHUP
);
360 ev_signal_start(globalconf
.loop
, &sigint
);
361 ev_signal_start(globalconf
.loop
, &sigterm
);
362 ev_signal_start(globalconf
.loop
, &sighup
);
363 ev_unref(globalconf
.loop
);
364 ev_unref(globalconf
.loop
);
365 ev_unref(globalconf
.loop
);
367 struct sigaction sa
= { .sa_handler
= signal_fatal
, .sa_flags
= 0 };
368 sigemptyset(&sa
.sa_mask
);
369 sigaction(SIGSEGV
, &sa
, 0);
372 globalconf
.connection
= xcb_connect(NULL
, &globalconf
.default_screen
);
373 if(xcb_connection_has_error(globalconf
.connection
))
374 fatal("cannot open display");
376 globalconf
.screen
= xcb_aux_get_screen(globalconf
.connection
, globalconf
.default_screen
);
377 globalconf
.visual
= screen_default_visual(globalconf
.screen
);
378 globalconf
.default_depth
= globalconf
.screen
->root_depth
;
379 globalconf
.default_cmap
= globalconf
.screen
->default_colormap
;
381 /* Prefetch all the extensions we might need */
382 xcb_prefetch_extension_data(globalconf
.connection
, &xcb_big_requests_id
);
383 xcb_prefetch_extension_data(globalconf
.connection
, &xcb_test_id
);
384 xcb_prefetch_extension_data(globalconf
.connection
, &xcb_randr_id
);
385 xcb_prefetch_extension_data(globalconf
.connection
, &xcb_xinerama_id
);
387 /* initialize dbus */
391 xcb_grab_server(globalconf
.connection
);
392 xcb_flush(globalconf
.connection
);
394 /* Get the file descriptor corresponding to the X connection */
395 xfd
= xcb_get_file_descriptor(globalconf
.connection
);
396 ev_io_init(&xio
, &a_xcb_io_cb
, xfd
, EV_READ
);
397 ev_io_start(globalconf
.loop
, &xio
);
398 ev_check_init(&xcheck
, &a_xcb_check_cb
);
399 ev_check_start(globalconf
.loop
, &xcheck
);
400 ev_unref(globalconf
.loop
);
401 ev_prepare_init(&a_refresh
, &a_refresh_cb
);
402 ev_prepare_start(globalconf
.loop
, &a_refresh
);
403 ev_unref(globalconf
.loop
);
406 const uint32_t select_input_val
= XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT
;
408 /* This causes an error if some other window manager is running */
409 xcb_change_window_attributes(globalconf
.connection
,
410 globalconf
.screen
->root
,
411 XCB_CW_EVENT_MASK
, &select_input_val
);
414 /* Need to xcb_flush to validate error handler */
415 xcb_aux_sync(globalconf
.connection
);
417 /* Process all errors in the queue if any. There can be no events yet, so if
418 * this function returns something, it must be an error. */
419 if (xcb_poll_for_event(globalconf
.connection
) != NULL
)
420 fatal("another window manager is already running");
422 /* Prefetch the maximum request length */
423 xcb_prefetch_maximum_request_length(globalconf
.connection
);
425 /* check for xtest extension */
426 const xcb_query_extension_reply_t
*xtest_query
;
427 xtest_query
= xcb_get_extension_data(globalconf
.connection
, &xcb_test_id
);
428 globalconf
.have_xtest
= xtest_query
->present
;
430 /* Allocate the key symbols */
431 globalconf
.keysyms
= xcb_key_symbols_alloc(globalconf
.connection
);
432 xcb_get_modifier_mapping_cookie_t xmapping_cookie
=
433 xcb_get_modifier_mapping_unchecked(globalconf
.connection
);
435 /* init atom cache */
436 atoms_init(globalconf
.connection
);
438 /* init screens information */
441 /* init default font and colors */
442 colors_reqs
[0] = xcolor_init_unchecked(&globalconf
.colors
.fg
,
443 "#000000", sizeof("#000000") - 1);
445 colors_reqs
[1] = xcolor_init_unchecked(&globalconf
.colors
.bg
,
446 "#ffffff", sizeof("#ffffff") - 1);
448 globalconf
.font
= font_new("sans 8");
450 for(colors_nbr
= 0; colors_nbr
< 2; colors_nbr
++)
451 xcolor_init_reply(colors_reqs
[colors_nbr
]);
453 xutil_lock_mask_get(globalconf
.connection
, xmapping_cookie
,
454 globalconf
.keysyms
, &globalconf
.numlockmask
,
455 &globalconf
.shiftlockmask
, &globalconf
.capslockmask
,
456 &globalconf
.modeswitchmask
);
458 /* do this only for real screen */
462 /* init spawn (sn) */
465 /* The default GC is just a newly created associated with a window with
466 * depth globalconf.default_depth */
467 globalconf
.gc
= xcb_generate_id(globalconf
.connection
);
468 xcb_create_gc(globalconf
.connection
, globalconf
.gc
, globalconf
.systray
.window
, XCB_GC_FOREGROUND
| XCB_GC_BACKGROUND
,
469 (const uint32_t[]) { globalconf
.screen
->black_pixel
, globalconf
.screen
->white_pixel
});
471 /* Parse and run configuration file */
472 if (!luaA_parserc(&xdg
, confpath
, true))
473 fatal("couldn't find any rc file");
479 /* scan existing windows */
483 /* select for events */
484 const uint32_t change_win_vals
[] =
486 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT
| XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY
487 | XCB_EVENT_MASK_ENTER_WINDOW
| XCB_EVENT_MASK_LEAVE_WINDOW
488 | XCB_EVENT_MASK_STRUCTURE_NOTIFY
489 | XCB_EVENT_MASK_PROPERTY_CHANGE
490 | XCB_EVENT_MASK_BUTTON_PRESS
491 | XCB_EVENT_MASK_BUTTON_RELEASE
492 | XCB_EVENT_MASK_FOCUS_CHANGE
495 xcb_change_window_attributes(globalconf
.connection
,
496 globalconf
.screen
->root
,
501 /* we will receive events, stop grabbing server */
502 xcb_ungrab_server(globalconf
.connection
);
503 xcb_flush(globalconf
.connection
);
505 /* main event loop */
506 ev_loop(globalconf
.loop
, 0);
508 /* cleanup event loop */
509 ev_ref(globalconf
.loop
);
510 ev_check_stop(globalconf
.loop
, &xcheck
);
511 ev_ref(globalconf
.loop
);
512 ev_prepare_stop(globalconf
.loop
, &a_refresh
);
513 ev_ref(globalconf
.loop
);
514 ev_io_stop(globalconf
.loop
, &xio
);
521 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80