ewmh: store NET_WM icon
[awesome.git] / awesome.c
blobc258f5c793d971e37e502a2bfc20edbe1114e9ce
1 /*
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.
22 #define _GNU_SOURCE
23 #include <getopt.h>
25 #include <locale.h>
26 #include <stdio.h>
27 #include <unistd.h>
28 #include <signal.h>
30 #include <ev.h>
32 #include "client.h"
33 #include "titlebar.h"
34 #include "event.h"
35 #include "window.h"
36 #include "ewmh.h"
37 #include "dbus.h"
38 #include "statusbar.h"
39 #include "systray.h"
40 #include "common/version.h"
41 #include "common/atoms.h"
42 #include "config.h"
44 awesome_t globalconf;
46 typedef struct
48 xcb_window_t id;
49 xcb_query_tree_cookie_t tree_cookie;
50 } root_win_t;
52 /** Scan X to find windows to manage.
54 static void
55 scan(void)
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;
66 long state;
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,
82 NULL);
84 if(!tree_r)
85 continue;
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,
95 wins[i]);
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,
104 attr_wins[i],
105 NULL);
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
112 || has_awesome_prop
113 || attr_r->map_state != XCB_MAP_STATE_VIEWABLE
114 || state == XCB_WM_WITHDRAWN_STATE)
116 p_delete(&attr_r);
117 continue;
120 p_delete(&attr_r);
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++)
131 if(!geom_wins[i])
132 continue;
134 if(!(geom_r = xcb_get_geometry_reply(globalconf.connection,
135 *(geom_wins[i]), NULL)))
136 continue;
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);
143 p_delete(&geom_r);
144 p_delete(&geom_wins[i]);
147 p_delete(&geom_wins);
148 p_delete(&tree_r);
150 p_delete(&root_wins);
153 static void
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);
163 p_delete(&ev);
165 while((ev = xcb_poll_for_event(globalconf.connection)));
167 layout_refresh();
168 statusbar_refresh();
169 titlebar_refresh();
171 xcb_aux_sync(globalconf.connection);
175 static void
176 a_xcb_io_cb(EV_P_ ev_io *w, int revents)
178 /* empty */
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
198 static void
199 exit_on_signal(EV_P_ ev_signal *w, int revents)
201 ev_unloop(EV_A_ 1);
204 /** Function to restart aweome on some signals.
205 * \param sig the signam received, unused
207 static void
208 restart_on_signal(EV_P_ ev_signal *w, int revents)
210 ewmh_restart();
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.
222 static int
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);
228 if(!err)
229 return 0;
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);
237 return 0;
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)
252 exit(EXIT_FAILURE);
254 return 0;
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;
264 fprintf(outfile,
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");
269 exit(exit_code);
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;
282 ssize_t cmdlen = 1;
283 client_t *c;
284 static struct option long_options[] =
286 {"help", 0, NULL, 'h'},
287 {"version", 0, NULL, 'v'},
288 {"config", 1, NULL, 'c'},
289 {NULL, 0, NULL, 0}
292 /* event loop watchers */
293 ev_io xio = { .fd = -1 };
294 ev_check xcheck;
295 ev_signal sigint;
296 ev_signal sigterm;
297 ev_signal sighup;
299 /* clear the globalconf structure */
300 p_clear(&globalconf, 1);
301 globalconf.keygrabber = LUA_REFNIL;
303 /* save argv */
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]);
316 /* check args */
317 while((opt = getopt_long(argc, argv, "vhc:",
318 long_options, NULL)) != -1)
319 switch(opt)
321 case 'v':
322 eprint_version("awesome");
323 break;
324 case 'h':
325 exit_help(EXIT_SUCCESS);
326 break;
327 case 'c':
328 if(a_strlen(optarg))
329 confpath = a_strdup(optarg);
330 else
331 fatal("-c option requires a file name");
332 break;
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);
351 /* X stuff */
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);
368 for(screen_nbr = 0;
369 screen_nbr < xcb_setup_roots_length(xcb_get_setup(globalconf.connection));
370 screen_nbr++)
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);
414 /* init cursors */
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);
425 /* init lua */
426 luaA_init();
428 luaA_parserc(confpath);
430 /* scan existing windows */
431 scan();
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 */
438 for(screen_nbr = 0;
439 screen_nbr < xcb_setup_roots_length(xcb_get_setup(globalconf.connection));
440 screen_nbr++)
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,
454 change_win_vals);
455 ewmh_init(screen_nbr);
456 systray_init(screen_nbr);
459 xcb_aux_sync(globalconf.connection);
461 luaA_cs_init();
462 a_dbus_init();
464 /* refresh everything before waiting events */
465 layout_refresh();
466 statusbar_refresh();
467 titlebar_refresh();
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);
477 a_dbus_cleanup();
478 luaA_cs_cleanup();
480 /* remap all clients since some WM won't handle them otherwise */
481 for(c = globalconf.clients; c; c = c->next)
482 client_unban(c);
484 xcb_aux_sync(globalconf.connection);
486 xcb_disconnect(globalconf.connection);
488 return EXIT_SUCCESS;
491 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80