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_geometry_cookie_t
**geom_wins
= NULL
;
64 xcb_get_window_attributes_reply_t
*attr_r
;
65 xcb_get_geometry_reply_t
*geom_r
;
68 for(screen
= 0; screen
< screen_max
; screen
++)
70 /* Get the root window ID associated to this screen */
71 root_wins
[screen
].id
= xutil_screen_get(globalconf
.connection
, screen
)->root
;
73 /* Get the window tree associated to this screen */
74 root_wins
[screen
].tree_cookie
= xcb_query_tree_unchecked(globalconf
.connection
,
75 root_wins
[screen
].id
);
78 for(screen
= 0; screen
< screen_max
; screen
++)
80 tree_r
= xcb_query_tree_reply(globalconf
.connection
,
81 root_wins
[screen
].tree_cookie
,
87 /* Get the tree of the children windows of the current root window */
88 if(!(wins
= xcb_query_tree_children(tree_r
)))
89 fatal("E: cannot get tree children");
90 tree_c_len
= xcb_query_tree_children_length(tree_r
);
91 attr_wins
= p_new(xcb_get_window_attributes_cookie_t
, tree_c_len
);
93 for(i
= 0; i
< tree_c_len
; i
++)
94 attr_wins
[i
] = xcb_get_window_attributes_unchecked(globalconf
.connection
,
97 geom_wins
= p_new(xcb_get_geometry_cookie_t
*, tree_c_len
);
99 for(i
= 0; i
< tree_c_len
; i
++)
101 attr_r
= xcb_get_window_attributes_reply(globalconf
.connection
,
105 state
= window_getstate(wins
[i
]);
107 if(!attr_r
|| attr_r
->override_redirect
108 || attr_r
->map_state
!= XCB_MAP_STATE_VIEWABLE
109 || state
== XCB_WM_WITHDRAWN_STATE
)
117 /* Get the geometry of the current window */
118 geom_wins
[i
] = p_new(xcb_get_geometry_cookie_t
, 1);
119 *(geom_wins
[i
]) = xcb_get_geometry_unchecked(globalconf
.connection
, wins
[i
]);
122 p_delete(&attr_wins
);
124 for(i
= 0; i
< tree_c_len
; i
++)
129 if(!(geom_r
= xcb_get_geometry_reply(globalconf
.connection
,
130 *(geom_wins
[i
]), NULL
)))
133 real_screen
= screen_get_bycoord(globalconf
.screens_info
, screen
,
134 geom_r
->x
, geom_r
->y
);
136 client_manage(wins
[i
], geom_r
, real_screen
);
139 p_delete(&geom_wins
[i
]);
142 p_delete(&geom_wins
);
145 p_delete(&root_wins
);
149 a_xcb_check_cb(EV_P_ ev_check
*w
, int revents
)
151 xcb_generic_event_t
*ev
;
153 while((ev
= xcb_poll_for_event(globalconf
.connection
)))
157 xcb_handle_event(globalconf
.evenths
, ev
);
160 while((ev
= xcb_poll_for_event(globalconf
.connection
)));
166 xcb_aux_sync(globalconf
.connection
);
171 a_xcb_io_cb(EV_P_ ev_io
*w
, int revents
)
176 /** Startup Error handler to check if another window manager
177 * is already running.
178 * \param data Additional optional parameters data.
179 * \param c X connection.
180 * \param error Error event.
182 static int __attribute__ ((noreturn
))
183 xerrorstart(void * data
__attribute__ ((unused
)),
184 xcb_connection_t
* c
__attribute__ ((unused
)),
185 xcb_generic_error_t
* error
__attribute__ ((unused
)))
187 fatal("another window manager is already running");
190 /** Function to exit on some signals.
191 * \param sig the signal received, unused
194 exit_on_signal(EV_P_ ev_signal
*w
, int revents
)
199 /** Function to restart aweome on some signals.
200 * \param sig the signam received, unused
203 restart_on_signal(EV_P_ ev_signal
*w
, int revents
)
208 /** \brief awesome xerror function.
209 * There's no way to check accesses to destroyed windows, thus those cases are
210 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
211 * default error handler, which may call exit.
212 * \param data Currently unused.
213 * \param c The connectiont to the X server.
214 * \param e The error event.
215 * \return 0 if no error, or xerror's xlib return status.
218 xerror(void *data
__attribute__ ((unused
)),
219 xcb_connection_t
*c
__attribute__ ((unused
)),
220 xcb_generic_error_t
*e
)
222 xutil_error_t
*err
= xutil_get_error(e
);
226 if(e
->error_code
== BadWindow
227 || (e
->error_code
== BadMatch
&& err
->request_code
== XCB_SET_INPUT_FOCUS
)
228 || (e
->error_code
== BadValue
&& err
->request_code
== XCB_KILL_CLIENT
)
229 || (err
->request_code
== XCB_CONFIGURE_WINDOW
&& e
->error_code
== BadMatch
))
231 xutil_delete_error(err
);
235 warn("fatal error: request=%s, error=%s", err
->request_label
, err
->error_label
);
236 xutil_delete_error(err
);
239 * Xlib code was using default X error handler, namely
240 * '_XDefaultError()', which displays more informations about the
241 * error and also exit if 'error_code'' equals to
242 * 'BadImplementation'
244 * \todo display more informations about the error (like the Xlib default error handler)
246 if(e
->error_code
== BadImplementation
)
252 /** Print help and exit(2) with given exit_code.
253 * \param exit_code The exit code.
255 static void __attribute__ ((noreturn
))
256 exit_help(int exit_code
)
258 FILE *outfile
= (exit_code
== EXIT_SUCCESS
) ? stdout
: stderr
;
260 "Usage: awesome [OPTION]\n\
261 -h, --help show help\n\
262 -v, --version show version\n\
263 -c, --config FILE configuration file to use\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 const char *confpath
= NULL
;
276 int xfd
, i
, screen_nbr
, opt
;
279 static struct option long_options
[] =
281 {"help", 0, NULL
, 'h'},
282 {"version", 0, NULL
, 'v'},
283 {"config", 1, NULL
, 'c'},
287 /* event loop watchers */
288 ev_io xio
= { .fd
= -1 };
294 /* clear the globalconf structure */
295 p_clear(&globalconf
, 1);
296 globalconf
.keygrabber
= LUA_REFNIL
;
299 for(i
= 0; i
< argc
; i
++)
300 cmdlen
+= a_strlen(argv
[i
]) + 1;
302 globalconf
.argv
= p_new(char, cmdlen
);
303 a_strcpy(globalconf
.argv
, cmdlen
, argv
[0]);
305 for(i
= 1; i
< argc
; i
++)
307 a_strcat(globalconf
.argv
, cmdlen
, " ");
308 a_strcat(globalconf
.argv
, cmdlen
, argv
[i
]);
312 while((opt
= getopt_long(argc
, argv
, "vhc:",
313 long_options
, NULL
)) != -1)
317 eprint_version("awesome");
320 exit_help(EXIT_SUCCESS
);
324 confpath
= a_strdup(optarg
);
326 fatal("-c option requires a file name");
330 /* Text won't be printed correctly otherwise */
331 setlocale(LC_CTYPE
, "");
332 globalconf
.loop
= ev_default_loop(0);
333 ev_timer_init(&globalconf
.timer
, &luaA_on_timer
, 0., 0.);
335 /* register function for signals */
336 ev_signal_init(&sigint
, exit_on_signal
, SIGINT
);
337 ev_signal_init(&sigterm
, exit_on_signal
, SIGTERM
);
338 ev_signal_init(&sighup
, restart_on_signal
, SIGHUP
);
339 ev_signal_start(globalconf
.loop
, &sigint
);
340 ev_signal_start(globalconf
.loop
, &sigterm
);
341 ev_signal_start(globalconf
.loop
, &sighup
);
342 ev_unref(globalconf
.loop
);
343 ev_unref(globalconf
.loop
);
344 ev_unref(globalconf
.loop
);
347 globalconf
.connection
= xcb_connect(NULL
, &globalconf
.default_screen
);
348 if(xcb_connection_has_error(globalconf
.connection
))
349 fatal("cannot open display");
351 /* Get the file descriptor corresponding to the X connection */
352 xfd
= xcb_get_file_descriptor(globalconf
.connection
);
353 ev_io_init(&xio
, &a_xcb_io_cb
, xfd
, EV_READ
);
354 ev_io_start(globalconf
.loop
, &xio
);
355 ev_check_init(&xcheck
, &a_xcb_check_cb
);
356 ev_check_start(globalconf
.loop
, &xcheck
);
357 ev_unref(globalconf
.loop
);
359 /* Allocate a handler which will holds all errors and events */
360 globalconf
.evenths
= xcb_alloc_event_handlers(globalconf
.connection
);
361 xutil_set_error_handler_catch_all(globalconf
.evenths
, xerrorstart
, NULL
);
364 screen_nbr
< xcb_setup_roots_length(xcb_get_setup(globalconf
.connection
));
367 const uint32_t select_input_val
= XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT
;
369 /* This causes an error if some other window manager is running */
370 xcb_change_window_attributes(globalconf
.connection
,
371 xutil_screen_get(globalconf
.connection
, screen_nbr
)->root
,
372 XCB_CW_EVENT_MASK
, &select_input_val
);
375 /* Need to xcb_flush to validate error handler */
376 xcb_aux_sync(globalconf
.connection
);
378 /* Process all errors in the queue if any */
379 xcb_poll_for_event_loop(globalconf
.evenths
);
381 /* Set the default xerror handler */
382 xutil_set_error_handler_catch_all(globalconf
.evenths
, xerror
, NULL
);
384 /* Allocate the key symbols */
385 globalconf
.keysyms
= xcb_key_symbols_alloc(globalconf
.connection
);
387 /* Get the NumLock, ShiftLock and CapsLock masks */
388 xutil_getlockmask(globalconf
.connection
, globalconf
.keysyms
, &globalconf
.numlockmask
,
389 &globalconf
.shiftlockmask
, &globalconf
.capslockmask
);
391 /* init atom cache */
392 atoms_init(globalconf
.connection
);
394 /* init screens struct */
395 globalconf
.screens_info
= screensinfo_new(globalconf
.connection
);
396 globalconf
.screen_focus
= globalconf
.screens
= p_new(screen_t
, globalconf
.screens_info
->nscreen
);
398 /* init default font and colors */
399 globalconf
.font
= draw_font_new(globalconf
.connection
, globalconf
.default_screen
, "sans 8");
400 xcolor_init(&globalconf
.colors
.fg
, globalconf
.connection
, globalconf
.default_screen
, "black", sizeof("black")-1);
401 xcolor_init(&globalconf
.colors
.bg
, globalconf
.connection
, globalconf
.default_screen
, "white", sizeof("white")-1);
404 globalconf
.cursor
[CurNormal
] = xutil_cursor_new(globalconf
.connection
, CURSOR_LEFT_PTR
);
405 globalconf
.cursor
[CurResize
] = xutil_cursor_new(globalconf
.connection
, CURSOR_SIZING
);
406 globalconf
.cursor
[CurResizeH
] = xutil_cursor_new(globalconf
.connection
, CURSOR_DOUBLE_ARROW_HORIZ
);
407 globalconf
.cursor
[CurResizeV
] = xutil_cursor_new(globalconf
.connection
, CURSOR_DOUBLE_ARROW_VERT
);
408 globalconf
.cursor
[CurMove
] = xutil_cursor_new(globalconf
.connection
, CURSOR_FLEUR
);
409 globalconf
.cursor
[CurTopRight
] = xutil_cursor_new(globalconf
.connection
, CURSOR_TOP_RIGHT_CORNER
);
410 globalconf
.cursor
[CurTopLeft
] = xutil_cursor_new(globalconf
.connection
, CURSOR_TOP_LEFT_CORNER
);
411 globalconf
.cursor
[CurBotRight
] = xutil_cursor_new(globalconf
.connection
, CURSOR_BOTTOM_RIGHT_CORNER
);
412 globalconf
.cursor
[CurBotLeft
] = xutil_cursor_new(globalconf
.connection
, CURSOR_BOTTOM_LEFT_CORNER
);
417 luaA_parserc(confpath
);
419 /* scan existing windows */
422 /* process all errors in the queue if any */
423 xcb_poll_for_event_loop(globalconf
.evenths
);
424 a_xcb_set_event_handlers();
426 /* do this only for real screen */
428 screen_nbr
< xcb_setup_roots_length(xcb_get_setup(globalconf
.connection
));
431 /* select for events */
432 const uint32_t change_win_vals
[] =
434 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT
| XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY
435 | XCB_EVENT_MASK_ENTER_WINDOW
| XCB_EVENT_MASK_LEAVE_WINDOW
436 | XCB_EVENT_MASK_STRUCTURE_NOTIFY
,
437 globalconf
.cursor
[CurNormal
]
440 xcb_change_window_attributes(globalconf
.connection
,
441 xutil_screen_get(globalconf
.connection
, screen_nbr
)->root
,
442 XCB_CW_EVENT_MASK
| XCB_CW_CURSOR
,
444 ewmh_init(screen_nbr
);
445 systray_init(screen_nbr
);
448 xcb_aux_sync(globalconf
.connection
);
453 /* refresh everything before waiting events */
458 /* main event loop */
459 ev_loop(globalconf
.loop
, 0);
461 /* cleanup event loop */
462 ev_ref(globalconf
.loop
);
463 ev_check_stop(globalconf
.loop
, &xcheck
);
464 ev_ref(globalconf
.loop
);
465 ev_io_stop(globalconf
.loop
, &xio
);
469 /* remap all clients since some WM won't handle them otherwise */
470 for(c
= globalconf
.clients
; c
; c
= c
->next
)
473 xcb_aux_sync(globalconf
.connection
);
475 xcb_disconnect(globalconf
.connection
);
480 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80