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.
38 #include "statusbar.h"
40 #include "common/version.h"
41 #include "common/atoms.h"
49 xcb_query_tree_cookie_t tree_cookie
;
52 /** Scan X to find windows to manage.
57 int i
, screen
, real_screen
, tree_c_len
;
58 const int screen_max
= xcb_setup_roots_length(xcb_get_setup(globalconf
.connection
));
59 root_win_t
*root_wins
= p_new(root_win_t
, screen_max
);
60 xcb_query_tree_reply_t
*tree_r
;
61 xcb_window_t
*wins
= NULL
;
62 xcb_get_window_attributes_cookie_t
*attr_wins
= NULL
;
63 xcb_get_property_cookie_t
*state_wins
= NULL
;
64 xcb_get_geometry_cookie_t
**geom_wins
= NULL
;
65 xcb_get_window_attributes_reply_t
*attr_r
;
66 xcb_get_geometry_reply_t
*geom_r
;
69 for(screen
= 0; screen
< screen_max
; screen
++)
71 /* Get the root window ID associated to this screen */
72 root_wins
[screen
].id
= xutil_screen_get(globalconf
.connection
, screen
)->root
;
74 /* Get the window tree associated to this screen */
75 root_wins
[screen
].tree_cookie
= xcb_query_tree_unchecked(globalconf
.connection
,
76 root_wins
[screen
].id
);
79 for(screen
= 0; screen
< screen_max
; screen
++)
81 tree_r
= xcb_query_tree_reply(globalconf
.connection
,
82 root_wins
[screen
].tree_cookie
,
88 /* Get the tree of the children windows of the current root window */
89 if(!(wins
= xcb_query_tree_children(tree_r
)))
90 fatal("E: cannot get tree children");
91 tree_c_len
= xcb_query_tree_children_length(tree_r
);
92 attr_wins
= p_new(xcb_get_window_attributes_cookie_t
, tree_c_len
);
93 state_wins
= p_new(xcb_get_property_cookie_t
, tree_c_len
);
95 for(i
= 0; i
< tree_c_len
; i
++)
97 attr_wins
[i
] = xcb_get_window_attributes_unchecked(globalconf
.connection
,
100 state_wins
[i
] = window_state_get_unchecked(wins
[i
]);
103 geom_wins
= p_new(xcb_get_geometry_cookie_t
*, tree_c_len
);
105 for(i
= 0; i
< tree_c_len
; i
++)
107 bool has_awesome_prop
;
109 attr_r
= xcb_get_window_attributes_reply(globalconf
.connection
,
113 state
= window_state_get_reply(state_wins
[i
]);
115 has_awesome_prop
= xutil_text_prop_get(globalconf
.connection
, wins
[i
],
116 _AWESOME_PROPERTIES
, NULL
, NULL
);
118 if(!attr_r
|| attr_r
->override_redirect
119 || (attr_r
->map_state
!= XCB_MAP_STATE_VIEWABLE
&& !has_awesome_prop
)
120 || (state
== XCB_WM_WITHDRAWN_STATE
&& !has_awesome_prop
))
128 /* Get the geometry of the current window */
129 geom_wins
[i
] = p_new(xcb_get_geometry_cookie_t
, 1);
130 *(geom_wins
[i
]) = xcb_get_geometry_unchecked(globalconf
.connection
, wins
[i
]);
133 p_delete(&state_wins
);
134 p_delete(&attr_wins
);
136 for(i
= 0; i
< tree_c_len
; i
++)
141 if(!(geom_r
= xcb_get_geometry_reply(globalconf
.connection
,
142 *(geom_wins
[i
]), NULL
)))
145 real_screen
= screen_get_bycoord(globalconf
.screens_info
, screen
,
146 geom_r
->x
, geom_r
->y
);
148 client_manage(wins
[i
], geom_r
, real_screen
);
151 p_delete(&geom_wins
[i
]);
154 p_delete(&geom_wins
);
157 p_delete(&root_wins
);
161 a_xcb_check_cb(EV_P_ ev_check
*w
, int revents
)
163 xcb_generic_event_t
*ev
;
165 while((ev
= xcb_poll_for_event(globalconf
.connection
)))
169 xcb_handle_event(globalconf
.evenths
, ev
);
172 while((ev
= xcb_poll_for_event(globalconf
.connection
)));
178 xcb_aux_sync(globalconf
.connection
);
183 a_xcb_io_cb(EV_P_ ev_io
*w
, int revents
)
188 /** Startup Error handler to check if another window manager
189 * is already running.
190 * \param data Additional optional parameters data.
191 * \param c X connection.
192 * \param error Error event.
194 static int __attribute__ ((noreturn
))
195 xerrorstart(void * data
__attribute__ ((unused
)),
196 xcb_connection_t
* c
__attribute__ ((unused
)),
197 xcb_generic_error_t
* error
__attribute__ ((unused
)))
199 fatal("another window manager is already running");
202 /** Function to exit on some signals.
203 * \param sig the signal received, unused
206 exit_on_signal(EV_P_ ev_signal
*w
, int revents
)
211 /** Function to restart aweome on some signals.
212 * \param sig the signam received, unused
215 restart_on_signal(EV_P_ ev_signal
*w
, int revents
)
220 /** \brief awesome xerror function.
221 * There's no way to check accesses to destroyed windows, thus those cases are
222 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
223 * default error handler, which may call exit.
224 * \param data Currently unused.
225 * \param c The connectiont to the X server.
226 * \param e The error event.
227 * \return 0 if no error, or xerror's xlib return status.
230 xerror(void *data
__attribute__ ((unused
)),
231 xcb_connection_t
*c
__attribute__ ((unused
)),
232 xcb_generic_error_t
*e
)
234 xutil_error_t
*err
= xutil_error_get(e
);
238 if(e
->error_code
== XUTIL_BAD_WINDOW
239 || (e
->error_code
== XUTIL_BAD_MATCH
&& err
->request_code
== XCB_SET_INPUT_FOCUS
)
240 || (e
->error_code
== XUTIL_BAD_VALUE
&& err
->request_code
== XCB_KILL_CLIENT
)
241 || (err
->request_code
== XCB_CONFIGURE_WINDOW
&& e
->error_code
== XUTIL_BAD_MATCH
))
243 xutil_error_delete(err
);
247 warn("fatal error: request=%s, error=%s", err
->request_label
, err
->error_label
);
248 xutil_error_delete(err
);
251 * Xlib code was using default X error handler, namely
252 * '_XDefaultError()', which displays more informations about the
253 * error and also exit if 'error_code'' equals to
254 * 'BadImplementation'
256 * \todo display more informations about the error (like the Xlib default error handler)
258 if(e
->error_code
== XUTIL_BAD_IMPLEMENTATION
)
264 /** Print help and exit(2) with given exit_code.
265 * \param exit_code The exit code.
267 static void __attribute__ ((noreturn
))
268 exit_help(int exit_code
)
270 FILE *outfile
= (exit_code
== EXIT_SUCCESS
) ? stdout
: stderr
;
272 "Usage: awesome [OPTION]\n\
273 -h, --help show help\n\
274 -v, --version show version\n\
275 -c, --config FILE configuration file to use\n");
279 /** Hello, this is main.
280 * \param argc Who knows.
281 * \param argv Who knows.
282 * \return EXIT_SUCCESS I hope.
285 main(int argc
, char **argv
)
287 const char *confpath
= NULL
;
288 int xfd
, i
, screen_nbr
, opt
, colors_nbr
;
289 xcolor_init_request_t colors_reqs
[2];
290 xcb_get_modifier_mapping_cookie_t xmapping_cookie
;
293 static struct option long_options
[] =
295 {"help", 0, NULL
, 'h'},
296 {"version", 0, NULL
, 'v'},
297 {"config", 1, NULL
, 'c'},
301 /* event loop watchers */
302 ev_io xio
= { .fd
= -1 };
308 /* clear the globalconf structure */
309 p_clear(&globalconf
, 1);
310 globalconf
.keygrabber
= LUA_REFNIL
;
313 for(i
= 0; i
< argc
; i
++)
314 cmdlen
+= a_strlen(argv
[i
]) + 1;
316 globalconf
.argv
= p_new(char, cmdlen
);
317 a_strcpy(globalconf
.argv
, cmdlen
, argv
[0]);
319 for(i
= 1; i
< argc
; i
++)
321 a_strcat(globalconf
.argv
, cmdlen
, " ");
322 a_strcat(globalconf
.argv
, cmdlen
, argv
[i
]);
326 while((opt
= getopt_long(argc
, argv
, "vhc:",
327 long_options
, NULL
)) != -1)
331 eprint_version("awesome");
334 exit_help(EXIT_SUCCESS
);
338 confpath
= a_strdup(optarg
);
340 fatal("-c option requires a file name");
344 /* Text won't be printed correctly otherwise */
345 setlocale(LC_CTYPE
, "");
346 globalconf
.loop
= ev_default_loop(0);
347 ev_timer_init(&globalconf
.timer
, &luaA_on_timer
, 0., 0.);
349 /* register function for signals */
350 ev_signal_init(&sigint
, exit_on_signal
, SIGINT
);
351 ev_signal_init(&sigterm
, exit_on_signal
, SIGTERM
);
352 ev_signal_init(&sighup
, restart_on_signal
, SIGHUP
);
353 ev_signal_start(globalconf
.loop
, &sigint
);
354 ev_signal_start(globalconf
.loop
, &sigterm
);
355 ev_signal_start(globalconf
.loop
, &sighup
);
356 ev_unref(globalconf
.loop
);
357 ev_unref(globalconf
.loop
);
358 ev_unref(globalconf
.loop
);
361 globalconf
.connection
= xcb_connect(NULL
, &globalconf
.default_screen
);
362 if(xcb_connection_has_error(globalconf
.connection
))
363 fatal("cannot open display");
365 /* Get the file descriptor corresponding to the X connection */
366 xfd
= xcb_get_file_descriptor(globalconf
.connection
);
367 ev_io_init(&xio
, &a_xcb_io_cb
, xfd
, EV_READ
);
368 ev_io_start(globalconf
.loop
, &xio
);
369 ev_check_init(&xcheck
, &a_xcb_check_cb
);
370 ev_check_start(globalconf
.loop
, &xcheck
);
371 ev_unref(globalconf
.loop
);
373 /* Allocate a handler which will holds all errors and events */
374 globalconf
.evenths
= xcb_alloc_event_handlers(globalconf
.connection
);
375 xutil_error_handler_catch_all_set(globalconf
.evenths
, xerrorstart
, NULL
);
378 screen_nbr
< xcb_setup_roots_length(xcb_get_setup(globalconf
.connection
));
381 const uint32_t select_input_val
= XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT
;
383 /* This causes an error if some other window manager is running */
384 xcb_change_window_attributes(globalconf
.connection
,
385 xutil_screen_get(globalconf
.connection
, screen_nbr
)->root
,
386 XCB_CW_EVENT_MASK
, &select_input_val
);
389 /* Need to xcb_flush to validate error handler */
390 xcb_aux_sync(globalconf
.connection
);
392 /* Process all errors in the queue if any */
393 xcb_poll_for_event_loop(globalconf
.evenths
);
395 /* Set the default xerror handler */
396 xutil_error_handler_catch_all_set(globalconf
.evenths
, xerror
, NULL
);
398 /* Allocate the key symbols */
399 globalconf
.keysyms
= xcb_key_symbols_alloc(globalconf
.connection
);
401 /* Send the request to get the NumLock, ShiftLock and CapsLock
403 xmapping_cookie
= xcb_get_modifier_mapping_unchecked(globalconf
.connection
);
405 /* init atom cache */
406 atoms_init(globalconf
.connection
);
408 /* init screens struct */
409 globalconf
.screens_info
= screensinfo_new(globalconf
.connection
);
410 globalconf
.screen_focus
= globalconf
.screens
= p_new(screen_t
, globalconf
.screens_info
->nscreen
);
411 /* \todo stop duplicating this */
412 for(screen_nbr
= 0; screen_nbr
< globalconf
.screens_info
->nscreen
; screen_nbr
++)
414 globalconf
.screens
[screen_nbr
].index
= screen_nbr
;
415 globalconf
.screens
[screen_nbr
].geometry
= globalconf
.screens_info
->geometry
[screen_nbr
];
418 /* init default font and colors */
419 colors_reqs
[0] = xcolor_init_unchecked(globalconf
.connection
, &globalconf
.colors
.fg
,
420 globalconf
.default_screen
, "black",
423 colors_reqs
[1] = xcolor_init_unchecked(globalconf
.connection
, &globalconf
.colors
.bg
,
424 globalconf
.default_screen
, "white",
427 globalconf
.font
= draw_font_new(globalconf
.connection
, globalconf
.default_screen
, "sans 8");
430 globalconf
.cursor
[CurNormal
] = xutil_cursor_new(globalconf
.connection
, XUTIL_CURSOR_LEFT_PTR
);
431 globalconf
.cursor
[CurResize
] = xutil_cursor_new(globalconf
.connection
, XUTIL_CURSOR_SIZING
);
432 globalconf
.cursor
[CurResizeH
] = xutil_cursor_new(globalconf
.connection
, XUTIL_CURSOR_DOUBLE_ARROW_HORIZ
);
433 globalconf
.cursor
[CurResizeV
] = xutil_cursor_new(globalconf
.connection
, XUTIL_CURSOR_DOUBLE_ARROW_VERT
);
434 globalconf
.cursor
[CurMove
] = xutil_cursor_new(globalconf
.connection
, XUTIL_CURSOR_FLEUR
);
435 globalconf
.cursor
[CurTopRight
] = xutil_cursor_new(globalconf
.connection
, XUTIL_CURSOR_TOP_RIGHT_CORNER
);
436 globalconf
.cursor
[CurTopLeft
] = xutil_cursor_new(globalconf
.connection
, XUTIL_CURSOR_TOP_LEFT_CORNER
);
437 globalconf
.cursor
[CurBotRight
] = xutil_cursor_new(globalconf
.connection
, XUTIL_CURSOR_BOTTOM_RIGHT_CORNER
);
438 globalconf
.cursor
[CurBotLeft
] = xutil_cursor_new(globalconf
.connection
, XUTIL_CURSOR_BOTTOM_LEFT_CORNER
);
440 for(colors_nbr
= 0; colors_nbr
< 2; colors_nbr
++)
441 xcolor_init_reply(globalconf
.connection
, colors_reqs
[colors_nbr
]);
443 /* Process the reply of previously sent mapping request */
444 xutil_lock_mask_get(globalconf
.connection
, xmapping_cookie
,
445 globalconf
.keysyms
, &globalconf
.numlockmask
,
446 &globalconf
.shiftlockmask
, &globalconf
.capslockmask
);
451 luaA_parserc(confpath
);
453 /* scan existing windows */
456 /* process all errors in the queue if any */
457 xcb_poll_for_event_loop(globalconf
.evenths
);
458 a_xcb_set_event_handlers();
460 /* do this only for real screen */
462 screen_nbr
< xcb_setup_roots_length(xcb_get_setup(globalconf
.connection
));
465 /* select for events */
466 const uint32_t change_win_vals
[] =
468 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT
| XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY
469 | XCB_EVENT_MASK_ENTER_WINDOW
| XCB_EVENT_MASK_LEAVE_WINDOW
470 | XCB_EVENT_MASK_STRUCTURE_NOTIFY
,
471 globalconf
.cursor
[CurNormal
]
474 xcb_change_window_attributes(globalconf
.connection
,
475 xutil_screen_get(globalconf
.connection
, screen_nbr
)->root
,
476 XCB_CW_EVENT_MASK
| XCB_CW_CURSOR
,
478 ewmh_init(screen_nbr
);
479 systray_init(screen_nbr
);
482 xcb_aux_sync(globalconf
.connection
);
487 /* refresh everything before waiting events */
492 /* main event loop */
493 ev_loop(globalconf
.loop
, 0);
495 /* cleanup event loop */
496 ev_ref(globalconf
.loop
);
497 ev_check_stop(globalconf
.loop
, &xcheck
);
498 ev_ref(globalconf
.loop
);
499 ev_io_stop(globalconf
.loop
, &xio
);
503 /* remap all clients since some WM won't handle them otherwise */
504 for(c
= globalconf
.clients
; c
; c
= c
->next
)
507 xcb_aux_sync(globalconf
.connection
);
509 xcb_disconnect(globalconf
.connection
);
514 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80