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 bool has_awesome_prop
;
103 attr_r
= xcb_get_window_attributes_reply(globalconf
.connection
,
107 state
= window_getstate(wins
[i
]);
109 has_awesome_prop
= xutil_text_prop_get(globalconf
.connection
, wins
[1], _AWESOME_PROPERTIES
, NULL
, NULL
);
111 if(!attr_r
|| attr_r
->override_redirect
113 || attr_r
->map_state
!= XCB_MAP_STATE_VIEWABLE
114 || state
== XCB_WM_WITHDRAWN_STATE
)
122 /* Get the geometry of the current window */
123 geom_wins
[i
] = p_new(xcb_get_geometry_cookie_t
, 1);
124 *(geom_wins
[i
]) = xcb_get_geometry_unchecked(globalconf
.connection
, wins
[i
]);
127 p_delete(&attr_wins
);
129 for(i
= 0; i
< tree_c_len
; i
++)
134 if(!(geom_r
= xcb_get_geometry_reply(globalconf
.connection
,
135 *(geom_wins
[i
]), NULL
)))
138 real_screen
= screen_get_bycoord(globalconf
.screens_info
, screen
,
139 geom_r
->x
, geom_r
->y
);
141 client_manage(wins
[i
], geom_r
, real_screen
);
144 p_delete(&geom_wins
[i
]);
147 p_delete(&geom_wins
);
150 p_delete(&root_wins
);
154 a_xcb_check_cb(EV_P_ ev_check
*w
, int revents
)
156 xcb_generic_event_t
*ev
;
158 while((ev
= xcb_poll_for_event(globalconf
.connection
)))
162 xcb_handle_event(globalconf
.evenths
, ev
);
165 while((ev
= xcb_poll_for_event(globalconf
.connection
)));
171 xcb_aux_sync(globalconf
.connection
);
176 a_xcb_io_cb(EV_P_ ev_io
*w
, int revents
)
181 /** Startup Error handler to check if another window manager
182 * is already running.
183 * \param data Additional optional parameters data.
184 * \param c X connection.
185 * \param error Error event.
187 static int __attribute__ ((noreturn
))
188 xerrorstart(void * data
__attribute__ ((unused
)),
189 xcb_connection_t
* c
__attribute__ ((unused
)),
190 xcb_generic_error_t
* error
__attribute__ ((unused
)))
192 fatal("another window manager is already running");
195 /** Function to exit on some signals.
196 * \param sig the signal received, unused
199 exit_on_signal(EV_P_ ev_signal
*w
, int revents
)
204 /** Function to restart aweome on some signals.
205 * \param sig the signam received, unused
208 restart_on_signal(EV_P_ ev_signal
*w
, int revents
)
213 /** \brief awesome xerror function.
214 * There's no way to check accesses to destroyed windows, thus those cases are
215 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
216 * default error handler, which may call exit.
217 * \param data Currently unused.
218 * \param c The connectiont to the X server.
219 * \param e The error event.
220 * \return 0 if no error, or xerror's xlib return status.
223 xerror(void *data
__attribute__ ((unused
)),
224 xcb_connection_t
*c
__attribute__ ((unused
)),
225 xcb_generic_error_t
*e
)
227 xutil_error_t
*err
= xutil_error_get(e
);
231 if(e
->error_code
== XUTIL_BAD_WINDOW
232 || (e
->error_code
== XUTIL_BAD_MATCH
&& err
->request_code
== XCB_SET_INPUT_FOCUS
)
233 || (e
->error_code
== XUTIL_BAD_VALUE
&& err
->request_code
== XCB_KILL_CLIENT
)
234 || (err
->request_code
== XCB_CONFIGURE_WINDOW
&& e
->error_code
== XUTIL_BAD_MATCH
))
236 xutil_error_delete(err
);
240 warn("fatal error: request=%s, error=%s", err
->request_label
, err
->error_label
);
241 xutil_error_delete(err
);
244 * Xlib code was using default X error handler, namely
245 * '_XDefaultError()', which displays more informations about the
246 * error and also exit if 'error_code'' equals to
247 * 'BadImplementation'
249 * \todo display more informations about the error (like the Xlib default error handler)
251 if(e
->error_code
== XUTIL_BAD_IMPLEMENTATION
)
257 /** Print help and exit(2) with given exit_code.
258 * \param exit_code The exit code.
260 static void __attribute__ ((noreturn
))
261 exit_help(int exit_code
)
263 FILE *outfile
= (exit_code
== EXIT_SUCCESS
) ? stdout
: stderr
;
265 "Usage: awesome [OPTION]\n\
266 -h, --help show help\n\
267 -v, --version show version\n\
268 -c, --config FILE configuration file to use\n");
272 /** Hello, this is main.
273 * \param argc Who knows.
274 * \param argv Who knows.
275 * \return EXIT_SUCCESS I hope.
278 main(int argc
, char **argv
)
280 const char *confpath
= NULL
;
281 int xfd
, i
, screen_nbr
, opt
;
284 static struct option long_options
[] =
286 {"help", 0, NULL
, 'h'},
287 {"version", 0, NULL
, 'v'},
288 {"config", 1, NULL
, 'c'},
292 /* event loop watchers */
293 ev_io xio
= { .fd
= -1 };
299 /* clear the globalconf structure */
300 p_clear(&globalconf
, 1);
301 globalconf
.keygrabber
= LUA_REFNIL
;
304 for(i
= 0; i
< argc
; i
++)
305 cmdlen
+= a_strlen(argv
[i
]) + 1;
307 globalconf
.argv
= p_new(char, cmdlen
);
308 a_strcpy(globalconf
.argv
, cmdlen
, argv
[0]);
310 for(i
= 1; i
< argc
; i
++)
312 a_strcat(globalconf
.argv
, cmdlen
, " ");
313 a_strcat(globalconf
.argv
, cmdlen
, argv
[i
]);
317 while((opt
= getopt_long(argc
, argv
, "vhc:",
318 long_options
, NULL
)) != -1)
322 eprint_version("awesome");
325 exit_help(EXIT_SUCCESS
);
329 confpath
= a_strdup(optarg
);
331 fatal("-c option requires a file name");
335 /* Text won't be printed correctly otherwise */
336 setlocale(LC_CTYPE
, "");
337 globalconf
.loop
= ev_default_loop(0);
338 ev_timer_init(&globalconf
.timer
, &luaA_on_timer
, 0., 0.);
340 /* register function for signals */
341 ev_signal_init(&sigint
, exit_on_signal
, SIGINT
);
342 ev_signal_init(&sigterm
, exit_on_signal
, SIGTERM
);
343 ev_signal_init(&sighup
, restart_on_signal
, SIGHUP
);
344 ev_signal_start(globalconf
.loop
, &sigint
);
345 ev_signal_start(globalconf
.loop
, &sigterm
);
346 ev_signal_start(globalconf
.loop
, &sighup
);
347 ev_unref(globalconf
.loop
);
348 ev_unref(globalconf
.loop
);
349 ev_unref(globalconf
.loop
);
352 globalconf
.connection
= xcb_connect(NULL
, &globalconf
.default_screen
);
353 if(xcb_connection_has_error(globalconf
.connection
))
354 fatal("cannot open display");
356 /* Get the file descriptor corresponding to the X connection */
357 xfd
= xcb_get_file_descriptor(globalconf
.connection
);
358 ev_io_init(&xio
, &a_xcb_io_cb
, xfd
, EV_READ
);
359 ev_io_start(globalconf
.loop
, &xio
);
360 ev_check_init(&xcheck
, &a_xcb_check_cb
);
361 ev_check_start(globalconf
.loop
, &xcheck
);
362 ev_unref(globalconf
.loop
);
364 /* Allocate a handler which will holds all errors and events */
365 globalconf
.evenths
= xcb_alloc_event_handlers(globalconf
.connection
);
366 xutil_error_handler_catch_all_set(globalconf
.evenths
, xerrorstart
, NULL
);
369 screen_nbr
< xcb_setup_roots_length(xcb_get_setup(globalconf
.connection
));
372 const uint32_t select_input_val
= XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT
;
374 /* This causes an error if some other window manager is running */
375 xcb_change_window_attributes(globalconf
.connection
,
376 xutil_screen_get(globalconf
.connection
, screen_nbr
)->root
,
377 XCB_CW_EVENT_MASK
, &select_input_val
);
380 /* Need to xcb_flush to validate error handler */
381 xcb_aux_sync(globalconf
.connection
);
383 /* Process all errors in the queue if any */
384 xcb_poll_for_event_loop(globalconf
.evenths
);
386 /* Set the default xerror handler */
387 xutil_error_handler_catch_all_set(globalconf
.evenths
, xerror
, NULL
);
389 /* Allocate the key symbols */
390 globalconf
.keysyms
= xcb_key_symbols_alloc(globalconf
.connection
);
392 /* Get the NumLock, ShiftLock and CapsLock masks */
393 xutil_lock_mask_get(globalconf
.connection
, globalconf
.keysyms
, &globalconf
.numlockmask
,
394 &globalconf
.shiftlockmask
, &globalconf
.capslockmask
);
396 /* init atom cache */
397 atoms_init(globalconf
.connection
);
399 /* init screens struct */
400 globalconf
.screens_info
= screensinfo_new(globalconf
.connection
);
401 globalconf
.screen_focus
= globalconf
.screens
= p_new(screen_t
, globalconf
.screens_info
->nscreen
);
402 /* \todo stop duplicating this */
403 for(screen_nbr
= 0; screen_nbr
< globalconf
.screens_info
->nscreen
; screen_nbr
++)
405 globalconf
.screens
[screen_nbr
].index
= screen_nbr
;
406 globalconf
.screens
[screen_nbr
].geometry
= globalconf
.screens_info
->geometry
[screen_nbr
];
409 /* init default font and colors */
410 globalconf
.font
= draw_font_new(globalconf
.connection
, globalconf
.default_screen
, "sans 8");
411 xcolor_init(&globalconf
.colors
.fg
, globalconf
.connection
, globalconf
.default_screen
, "black", sizeof("black")-1);
412 xcolor_init(&globalconf
.colors
.bg
, globalconf
.connection
, globalconf
.default_screen
, "white", sizeof("white")-1);
415 globalconf
.cursor
[CurNormal
] = xutil_cursor_new(globalconf
.connection
, XUTIL_CURSOR_LEFT_PTR
);
416 globalconf
.cursor
[CurResize
] = xutil_cursor_new(globalconf
.connection
, XUTIL_CURSOR_SIZING
);
417 globalconf
.cursor
[CurResizeH
] = xutil_cursor_new(globalconf
.connection
, XUTIL_CURSOR_DOUBLE_ARROW_HORIZ
);
418 globalconf
.cursor
[CurResizeV
] = xutil_cursor_new(globalconf
.connection
, XUTIL_CURSOR_DOUBLE_ARROW_VERT
);
419 globalconf
.cursor
[CurMove
] = xutil_cursor_new(globalconf
.connection
, XUTIL_CURSOR_FLEUR
);
420 globalconf
.cursor
[CurTopRight
] = xutil_cursor_new(globalconf
.connection
, XUTIL_CURSOR_TOP_RIGHT_CORNER
);
421 globalconf
.cursor
[CurTopLeft
] = xutil_cursor_new(globalconf
.connection
, XUTIL_CURSOR_TOP_LEFT_CORNER
);
422 globalconf
.cursor
[CurBotRight
] = xutil_cursor_new(globalconf
.connection
, XUTIL_CURSOR_BOTTOM_RIGHT_CORNER
);
423 globalconf
.cursor
[CurBotLeft
] = xutil_cursor_new(globalconf
.connection
, XUTIL_CURSOR_BOTTOM_LEFT_CORNER
);
428 luaA_parserc(confpath
);
430 /* scan existing windows */
433 /* process all errors in the queue if any */
434 xcb_poll_for_event_loop(globalconf
.evenths
);
435 a_xcb_set_event_handlers();
437 /* do this only for real screen */
439 screen_nbr
< xcb_setup_roots_length(xcb_get_setup(globalconf
.connection
));
442 /* select for events */
443 const uint32_t change_win_vals
[] =
445 XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT
| XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY
446 | XCB_EVENT_MASK_ENTER_WINDOW
| XCB_EVENT_MASK_LEAVE_WINDOW
447 | XCB_EVENT_MASK_STRUCTURE_NOTIFY
,
448 globalconf
.cursor
[CurNormal
]
451 xcb_change_window_attributes(globalconf
.connection
,
452 xutil_screen_get(globalconf
.connection
, screen_nbr
)->root
,
453 XCB_CW_EVENT_MASK
| XCB_CW_CURSOR
,
455 ewmh_init(screen_nbr
);
456 systray_init(screen_nbr
);
459 xcb_aux_sync(globalconf
.connection
);
464 /* refresh everything before waiting events */
469 /* main event loop */
470 ev_loop(globalconf
.loop
, 0);
472 /* cleanup event loop */
473 ev_ref(globalconf
.loop
);
474 ev_check_stop(globalconf
.loop
, &xcheck
);
475 ev_ref(globalconf
.loop
);
476 ev_io_stop(globalconf
.loop
, &xio
);
480 /* remap all clients since some WM won't handle them otherwise */
481 for(c
= globalconf
.clients
; c
; c
= c
->next
)
484 xcb_aux_sync(globalconf
.connection
);
486 xcb_disconnect(globalconf
.connection
);
491 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80