prompt: search in prompt history using exact string match
[awesome.git] / awesome.c
blobf37552f9a182e95f99a7a8f4092dedf220a8b5a8
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 #include <getopt.h>
24 #include <locale.h>
25 #include <stdio.h>
26 #include <unistd.h>
27 #include <signal.h>
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>
34 #include <xcb/shape.h>
36 #include <glib-unix.h>
38 #include "awesome.h"
39 #include "spawn.h"
40 #include "objects/client.h"
41 #include "xwindow.h"
42 #include "ewmh.h"
43 #include "dbus.h"
44 #include "systray.h"
45 #include "event.h"
46 #include "property.h"
47 #include "screen.h"
48 #include "luaa.h"
49 #include "common/version.h"
50 #include "common/atoms.h"
51 #include "common/xcursor.h"
52 #include "common/xutil.h"
53 #include "common/backtrace.h"
55 awesome_t globalconf;
57 /** argv used to run awesome */
58 static char *awesome_argv;
60 /** Call before exiting.
62 void
63 awesome_atexit(bool restart)
65 lua_pushboolean(globalconf.L, restart);
66 signal_object_emit(globalconf.L, &global_signals, "exit", 1);
68 a_dbus_cleanup();
70 systray_cleanup();
72 /* Close Lua */
73 lua_close(globalconf.L);
75 /* X11 is a great protocol. There is a save-set so that reparenting WMs
76 * don't kill clients when they shut down. However, when a focused windows
77 * is saved, the focus will move to its parent with revert-to none.
78 * Immediately afterwards, this parent is destroyed and the focus is gone.
79 * Work around this by placing the focus where we like it to be.
81 xcb_set_input_focus(globalconf.connection, XCB_INPUT_FOCUS_POINTER_ROOT,
82 XCB_NONE, XCB_CURRENT_TIME);
83 xcb_aux_sync(globalconf.connection);
85 /* Disconnect *after* closing lua */
86 xcb_cursor_context_free(globalconf.cursor_ctx);
87 xcb_disconnect(globalconf.connection);
90 /** Scan X to find windows to manage.
92 static void
93 scan(xcb_query_tree_cookie_t tree_c)
95 int i, tree_c_len;
96 xcb_query_tree_reply_t *tree_r;
97 xcb_window_t *wins = NULL;
98 xcb_get_window_attributes_reply_t *attr_r;
99 xcb_get_geometry_reply_t *geom_r;
100 long state;
102 tree_r = xcb_query_tree_reply(globalconf.connection,
103 tree_c,
104 NULL);
106 if(!tree_r)
107 return;
109 /* Get the tree of the children windows of the current root window */
110 if(!(wins = xcb_query_tree_children(tree_r)))
111 fatal("cannot get tree children");
113 tree_c_len = xcb_query_tree_children_length(tree_r);
114 xcb_get_window_attributes_cookie_t attr_wins[tree_c_len];
115 xcb_get_property_cookie_t state_wins[tree_c_len];
117 for(i = 0; i < tree_c_len; i++)
119 attr_wins[i] = xcb_get_window_attributes_unchecked(globalconf.connection,
120 wins[i]);
122 state_wins[i] = xwindow_get_state_unchecked(wins[i]);
125 xcb_get_geometry_cookie_t *geom_wins[tree_c_len];
127 for(i = 0; i < tree_c_len; i++)
129 attr_r = xcb_get_window_attributes_reply(globalconf.connection,
130 attr_wins[i],
131 NULL);
133 state = xwindow_get_state_reply(state_wins[i]);
135 if(!attr_r || attr_r->override_redirect
136 || attr_r->map_state == XCB_MAP_STATE_UNMAPPED
137 || state == XCB_ICCCM_WM_STATE_WITHDRAWN)
139 geom_wins[i] = NULL;
140 p_delete(&attr_r);
141 continue;
144 p_delete(&attr_r);
146 /* Get the geometry of the current window */
147 geom_wins[i] = p_alloca(xcb_get_geometry_cookie_t, 1);
148 *(geom_wins[i]) = xcb_get_geometry_unchecked(globalconf.connection, wins[i]);
151 for(i = 0; i < tree_c_len; i++)
153 if(!geom_wins[i] || !(geom_r = xcb_get_geometry_reply(globalconf.connection,
154 *(geom_wins[i]), NULL)))
155 continue;
157 client_manage(wins[i], geom_r, true);
159 p_delete(&geom_r);
162 p_delete(&tree_r);
165 static void
166 a_xcb_check(void)
168 xcb_generic_event_t *mouse = NULL, *event;
170 while((event = xcb_poll_for_event(globalconf.connection)))
172 /* We will treat mouse events later.
173 * We cannot afford to treat all mouse motion events,
174 * because that would be too much CPU intensive, so we just
175 * take the last we get after a bunch of events. */
176 if(XCB_EVENT_RESPONSE_TYPE(event) == XCB_MOTION_NOTIFY)
178 p_delete(&mouse);
179 mouse = event;
181 else
183 uint8_t type = XCB_EVENT_RESPONSE_TYPE(event);
184 if(mouse && (type == XCB_ENTER_NOTIFY || type == XCB_LEAVE_NOTIFY
185 || type == XCB_BUTTON_PRESS || type == XCB_BUTTON_RELEASE))
187 /* Make sure enter/motion/leave/press/release events are handled
188 * in the correct order */
189 event_handle(mouse);
190 p_delete(&mouse);
192 event_handle(event);
193 p_delete(&event);
197 if(mouse)
199 event_handle(mouse);
200 p_delete(&mouse);
204 static gboolean
205 a_xcb_io_cb(GIOChannel *source, GIOCondition cond, gpointer data)
207 /* a_xcb_check() already handled all events */
209 if(xcb_connection_has_error(globalconf.connection))
210 fatal("X server connection broke (error %d)",
211 xcb_connection_has_error(globalconf.connection));
213 return TRUE;
216 static gint
217 a_glib_poll(GPollFD *ufds, guint nfsd, gint timeout)
219 guint res;
220 awesome_refresh();
221 res = g_poll(ufds, nfsd, timeout);
222 a_xcb_check();
223 return res;
226 static void
227 signal_fatal(int signum)
229 buffer_t buf;
230 backtrace_get(&buf);
231 fatal("signal %d, dumping backtrace\n%s", signum, buf.s);
234 /** Function to exit on some signals.
235 * \param data currently unused
237 static gboolean
238 exit_on_signal(gpointer data)
240 g_main_loop_quit(globalconf.loop);
241 return TRUE;
244 void
245 awesome_restart(void)
247 awesome_atexit(true);
248 a_exec(awesome_argv);
251 /** Function to restart awesome on some signals.
252 * \param data currently unused
254 static gboolean
255 restart_on_signal(gpointer data)
257 awesome_restart();
258 return TRUE;
261 /** Print help and exit(2) with given exit_code.
262 * \param exit_code The exit code.
264 static void __attribute__ ((noreturn))
265 exit_help(int exit_code)
267 FILE *outfile = (exit_code == EXIT_SUCCESS) ? stdout : stderr;
268 fprintf(outfile,
269 "Usage: awesome [OPTION]\n\
270 -h, --help show help\n\
271 -v, --version show version\n\
272 -c, --config FILE configuration file to use\n\
273 -k, --check check configuration file syntax\n");
274 exit(exit_code);
277 /** Hello, this is main.
278 * \param argc Who knows.
279 * \param argv Who knows.
280 * \return EXIT_SUCCESS I hope.
283 main(int argc, char **argv)
285 char *confpath = NULL;
286 int xfd, i, opt;
287 ssize_t cmdlen = 1;
288 xdgHandle xdg;
289 bool no_argb = false;
290 bool run_test = false;
291 xcb_generic_event_t *event;
292 xcb_query_tree_cookie_t tree_c;
293 static struct option long_options[] =
295 { "help", 0, NULL, 'h' },
296 { "version", 0, NULL, 'v' },
297 { "config", 1, NULL, 'c' },
298 { "check", 0, NULL, 'k' },
299 { "no-argb", 0, NULL, 'a' },
300 { NULL, 0, NULL, 0 }
303 /* clear the globalconf structure */
304 p_clear(&globalconf, 1);
305 globalconf.keygrabber = LUA_REFNIL;
306 globalconf.mousegrabber = LUA_REFNIL;
307 buffer_init(&globalconf.startup_errors);
309 /* save argv */
310 for(i = 0; i < argc; i++)
311 cmdlen += a_strlen(argv[i]) + 1;
313 awesome_argv = p_new(char, cmdlen);
314 a_strcpy(awesome_argv, cmdlen, argv[0]);
316 for(i = 1; i < argc; i++)
318 a_strcat(awesome_argv, cmdlen, " ");
319 a_strcat(awesome_argv, cmdlen, argv[i]);
322 /* Text won't be printed correctly otherwise */
323 setlocale(LC_CTYPE, "");
325 /* Get XDG basedir data */
326 xdgInitHandle(&xdg);
328 /* init lua */
329 luaA_init(&xdg);
331 /* check args */
332 while((opt = getopt_long(argc, argv, "vhkc:a",
333 long_options, NULL)) != -1)
334 switch(opt)
336 case 'v':
337 eprint_version();
338 break;
339 case 'h':
340 exit_help(EXIT_SUCCESS);
341 break;
342 case 'k':
343 run_test = true;
344 break;
345 case 'c':
346 if(a_strlen(optarg))
347 confpath = a_strdup(optarg);
348 else
349 fatal("-c option requires a file name");
350 break;
351 case 'a':
352 no_argb = true;
353 break;
356 if (run_test)
358 if(!luaA_parserc(&xdg, confpath, false))
360 fprintf(stderr, "✘ Configuration file syntax error.\n");
361 return EXIT_FAILURE;
363 else
365 fprintf(stderr, "✔ Configuration file syntax OK.\n");
366 return EXIT_SUCCESS;
370 /* register function for signals */
371 g_unix_signal_add(SIGINT, exit_on_signal, NULL);
372 g_unix_signal_add(SIGTERM, exit_on_signal, NULL);
373 g_unix_signal_add(SIGHUP, restart_on_signal, NULL);
375 struct sigaction sa = { .sa_handler = signal_fatal, .sa_flags = 0 };
376 sigemptyset(&sa.sa_mask);
377 sigaction(SIGSEGV, &sa, 0);
379 /* We have no clue where the input focus is right now */
380 globalconf.focus.need_update = true;
382 /* X stuff */
383 globalconf.connection = xcb_connect(NULL, &globalconf.default_screen);
384 if(xcb_connection_has_error(globalconf.connection))
385 fatal("cannot open display (error %d)", xcb_connection_has_error(globalconf.connection));
387 globalconf.screen = xcb_aux_get_screen(globalconf.connection, globalconf.default_screen);
388 globalconf.default_visual = draw_default_visual(globalconf.screen);
389 if(!no_argb)
390 globalconf.visual = draw_argb_visual(globalconf.screen);
391 if(!globalconf.visual)
392 globalconf.visual = globalconf.default_visual;
393 globalconf.default_depth = draw_visual_depth(globalconf.screen, globalconf.visual->visual_id);
394 globalconf.default_cmap = globalconf.screen->default_colormap;
395 if(globalconf.default_depth != globalconf.screen->root_depth)
397 // We need our own color map if we aren't using the default depth
398 globalconf.default_cmap = xcb_generate_id(globalconf.connection);
399 xcb_create_colormap(globalconf.connection, XCB_COLORMAP_ALLOC_NONE,
400 globalconf.default_cmap, globalconf.screen->root,
401 globalconf.visual->visual_id);
404 /* Prefetch all the extensions we might need */
405 xcb_prefetch_extension_data(globalconf.connection, &xcb_big_requests_id);
406 xcb_prefetch_extension_data(globalconf.connection, &xcb_test_id);
407 xcb_prefetch_extension_data(globalconf.connection, &xcb_randr_id);
408 xcb_prefetch_extension_data(globalconf.connection, &xcb_xinerama_id);
409 xcb_prefetch_extension_data(globalconf.connection, &xcb_shape_id);
411 if (xcb_cursor_context_new(globalconf.connection, globalconf.screen, &globalconf.cursor_ctx) < 0)
412 fatal("Failed to initialize xcb-cursor");
414 /* Setup the main context */
415 g_main_context_set_poll_func(g_main_context_default(), &a_glib_poll);
417 /* initialize dbus */
418 a_dbus_init();
420 /* Get the file descriptor corresponding to the X connection */
421 xfd = xcb_get_file_descriptor(globalconf.connection);
422 GIOChannel *channel = g_io_channel_unix_new(xfd);
423 g_io_add_watch(channel, G_IO_IN, a_xcb_io_cb, NULL);
424 g_io_channel_unref(channel);
426 /* Grab server */
427 xcb_grab_server(globalconf.connection);
429 /* Make sure there are no pending events. Since we didn't really do anything
430 * at all yet, we will just discard all events which we received so far.
431 * The above GrabServer should make sure no new events are generated. */
432 xcb_aux_sync(globalconf.connection);
433 while ((event = xcb_poll_for_event(globalconf.connection)) != NULL)
435 /* Make sure errors are printed */
436 uint8_t response_type = XCB_EVENT_RESPONSE_TYPE(event);
437 if(response_type == 0)
438 event_handle(event);
439 p_delete(&event);
443 const uint32_t select_input_val = XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT;
445 /* This causes an error if some other window manager is running */
446 xcb_change_window_attributes(globalconf.connection,
447 globalconf.screen->root,
448 XCB_CW_EVENT_MASK, &select_input_val);
451 /* Need to xcb_flush to validate error handler */
452 xcb_aux_sync(globalconf.connection);
454 /* Process all errors in the queue if any. There can be no events yet, so if
455 * this function returns something, it must be an error. */
456 if (xcb_poll_for_event(globalconf.connection) != NULL)
457 fatal("another window manager is already running");
459 /* Prefetch the maximum request length */
460 xcb_prefetch_maximum_request_length(globalconf.connection);
462 /* check for xtest extension */
463 const xcb_query_extension_reply_t *query;
464 query = xcb_get_extension_data(globalconf.connection, &xcb_test_id);
465 globalconf.have_xtest = query->present;
467 /* check for shape extension */
468 query = xcb_get_extension_data(globalconf.connection, &xcb_shape_id);
469 globalconf.have_shape = query->present;
471 /* Allocate the key symbols */
472 globalconf.keysyms = xcb_key_symbols_alloc(globalconf.connection);
473 xcb_get_modifier_mapping_cookie_t xmapping_cookie =
474 xcb_get_modifier_mapping_unchecked(globalconf.connection);
476 /* init atom cache */
477 atoms_init(globalconf.connection);
479 /* init screens information */
480 screen_scan();
482 xutil_lock_mask_get(globalconf.connection, xmapping_cookie,
483 globalconf.keysyms, &globalconf.numlockmask,
484 &globalconf.shiftlockmask, &globalconf.capslockmask,
485 &globalconf.modeswitchmask);
487 /* do this only for real screen */
488 ewmh_init();
489 systray_init();
491 /* init spawn (sn) */
492 spawn_init();
494 /* The default GC is just a newly created associated with a window with
495 * depth globalconf.default_depth */
496 xcb_window_t tmp_win = xcb_generate_id(globalconf.connection);
497 globalconf.gc = xcb_generate_id(globalconf.connection);
498 xcb_create_window(globalconf.connection, globalconf.default_depth,
499 tmp_win, globalconf.screen->root,
500 -1, -1, 1, 1, 0,
501 XCB_COPY_FROM_PARENT, globalconf.visual->visual_id,
502 XCB_CW_BACK_PIXEL | XCB_CW_BORDER_PIXEL | XCB_CW_COLORMAP,
503 (const uint32_t [])
505 globalconf.screen->black_pixel,
506 globalconf.screen->black_pixel,
507 globalconf.default_cmap
509 xcb_create_gc(globalconf.connection, globalconf.gc, tmp_win, XCB_GC_FOREGROUND | XCB_GC_BACKGROUND,
510 (const uint32_t[]) { globalconf.screen->black_pixel, globalconf.screen->white_pixel });
511 xcb_destroy_window(globalconf.connection, tmp_win);
513 /* Get the window tree associated to this screen */
514 tree_c = xcb_query_tree_unchecked(globalconf.connection,
515 globalconf.screen->root);
517 xcb_change_window_attributes(globalconf.connection,
518 globalconf.screen->root,
519 XCB_CW_EVENT_MASK,
520 ROOT_WINDOW_EVENT_MASK);
522 /* we will receive events, stop grabbing server */
523 xcb_ungrab_server(globalconf.connection);
524 xcb_flush(globalconf.connection);
526 /* Parse and run configuration file */
527 if (!luaA_parserc(&xdg, confpath, true))
528 fatal("couldn't find any rc file");
530 p_delete(&confpath);
532 xdgWipeHandle(&xdg);
534 /* scan existing windows */
535 scan(tree_c);
537 xcb_flush(globalconf.connection);
539 /* main event loop */
540 globalconf.loop = g_main_loop_new(NULL, FALSE);
541 g_main_loop_run(globalconf.loop);
542 g_main_loop_unref(globalconf.loop);
543 globalconf.loop = NULL;
545 awesome_atexit(false);
547 return EXIT_SUCCESS;
550 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80