[awesome-menu] Remove a bunch of useless keybindings and add support for ISO_Left_Tab
[awesome.git] / awesome.c
blobe631a2d6fe446d1ac10e22a3d885b6243b303126
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 <errno.h>
26 #include <locale.h>
27 #include <stdio.h>
28 #include <unistd.h>
29 #include <sys/select.h>
30 #include <sys/types.h>
31 #include <sys/stat.h>
32 #include <sys/socket.h>
33 #include <sys/un.h>
34 #include <fcntl.h>
35 #include <signal.h>
36 #include <X11/cursorfont.h>
37 #include <X11/keysym.h>
38 #include <X11/Xatom.h>
39 #include <X11/Xproto.h>
40 #include <X11/Xutil.h>
41 #include <X11/extensions/shape.h>
42 #include <X11/extensions/Xrandr.h>
44 #include "config.h"
45 #include "awesome.h"
46 #include "event.h"
47 #include "layout.h"
48 #include "screen.h"
49 #include "statusbar.h"
50 #include "uicb.h"
51 #include "window.h"
52 #include "client.h"
53 #include "focus.h"
54 #include "ewmh.h"
55 #include "tag.h"
56 #include "common/socket.h"
57 #include "common/util.h"
58 #include "common/version.h"
59 #include "common/configopts.h"
61 static int (*xerrorxlib) (Display *, XErrorEvent *);
62 static Bool running = True;
64 AwesomeConf globalconf;
66 /** Scan X to find windows to manage
68 static void
69 scan()
71 unsigned int i, num;
72 int screen, real_screen;
73 Window *wins = NULL, d1, d2;
74 XWindowAttributes wa;
76 for(screen = 0; screen < ScreenCount(globalconf.display); screen++)
78 if(XQueryTree(globalconf.display, RootWindow(globalconf.display, screen), &d1, &d2, &wins, &num))
80 for(i = 0; i < num; i++)
82 /* XGetWindowAttributes return 1 on success */
83 if(!XGetWindowAttributes(globalconf.display, wins[i], &wa)
84 || wa.override_redirect
85 || XGetTransientForHint(globalconf.display, wins[i], &d1))
86 continue;
87 if(wa.map_state == IsViewable || window_getstate(wins[i]) == IconicState)
89 real_screen = screen_get_bycoord(screen, wa.x, wa.y);
90 client_manage(wins[i], &wa, real_screen);
93 /* now the transients */
94 for(i = 0; i < num; i++)
96 if(!XGetWindowAttributes(globalconf.display, wins[i], &wa))
97 continue;
98 if(XGetTransientForHint(globalconf.display, wins[i], &d1)
99 && (wa.map_state == IsViewable || window_getstate(wins[i]) == IconicState))
101 real_screen = screen_get_bycoord(screen, wa.x, wa.y);
102 client_manage(wins[i], &wa, real_screen);
106 if(wins)
107 XFree(wins);
111 /** Startup Error handler to check if another window manager
112 * is already running.
113 * \param disp Display
114 * \param ee Error event
116 static int __attribute__ ((noreturn))
117 xerrorstart(Display * disp __attribute__ ((unused)),
118 XErrorEvent * ee __attribute__ ((unused)))
120 eprint("another window manager is already running\n");
123 /** Quit awesome
124 * \param screen Screen ID
125 * \param arg nothing
126 * \ingroup ui_callback
128 void
129 uicb_quit(int screen __attribute__ ((unused)), char *arg __attribute__ ((unused)))
131 running = False;
134 static void
135 exit_on_signal(int sig __attribute__ ((unused)))
137 running = False;
140 /** \brief awesome xerror function
141 * There's no way to check accesses to destroyed windows, thus those cases are
142 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
143 * default error handler, which may call exit.
144 * \param edpy display ref
145 * \param ee XErrorEvent event
146 * \return 0 if no error, or xerror's xlib return status
148 static int
149 xerror(Display *edpy, XErrorEvent *ee)
151 if(ee->error_code == BadWindow
152 || (ee->error_code == BadMatch && ee->request_code == X_SetInputFocus)
153 || (ee->error_code == BadValue && ee->request_code == X_KillClient)
154 || (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch))
155 return 0;
156 warn("fatal error: request code=%d, error code=%d\n", ee->request_code, ee->error_code);
157 return xerrorxlib(edpy, ee); /* may call exit */
160 /** Print help and exit(2) with given exit_code.
162 static void __attribute__ ((noreturn))
163 exit_help(int exit_code)
165 FILE *outfile = (exit_code == EXIT_SUCCESS) ? stdout : stderr;
166 fprintf(outfile,
167 "Usage: awesome [OPTION]\n\
168 -h, --help show help\n\
169 -v, --version show version\n\
170 -c, --config FILE configuration file to use\n\
171 -k, --check check configuration file syntax\n");
172 exit(exit_code);
175 /** Hello, this is main
176 * \param argc who knows
177 * \param argv who knows
178 * \return EXIT_SUCCESS I hope
180 typedef void event_handler (XEvent *);
182 main(int argc, char *argv[])
184 char buf[1024];
185 const char *confpath = NULL;
186 int r, xfd, e_dummy, csfd, shape_event, randr_event_base, i, screen, opt;
187 ssize_t cmdlen = 1;
188 Statusbar *statusbar;
189 fd_set rd;
190 XEvent ev;
191 Display * dpy;
192 event_handler **handler;
193 struct sockaddr_un *addr;
194 Client *c;
195 XSetWindowAttributes wa;
196 static struct option long_options[] =
198 {"help", 0, NULL, 'h'},
199 {"version", 0, NULL, 'v'},
200 {"check", 0, NULL, 'k'},
201 {"config", 0, NULL, 'c'},
202 {NULL, 0, NULL, 0}
205 /* save argv */
206 for(i = 0; i < argc; i++)
207 cmdlen += a_strlen(argv[i]) + 1;
209 globalconf.argv = p_new(char, cmdlen);
210 a_strcpy(globalconf.argv, cmdlen, argv[0]);
212 for(i = 1; i < argc; i++)
214 a_strcat(globalconf.argv, cmdlen, " ");
215 a_strcat(globalconf.argv, cmdlen, argv[i]);
218 /* check args */
219 while((opt = getopt_long(argc, argv, "vhkc:",
220 long_options, NULL)) != -1)
221 switch(opt)
223 case 'v':
224 eprint_version("awesome");
225 break;
226 case 'h':
227 exit_help(EXIT_SUCCESS);
228 break;
229 case 'c':
230 if(a_strlen(optarg))
231 confpath = optarg;
232 else
233 eprint("-c option requires a file name\n");
234 break;
235 case 'k':
236 return config_check(confpath);
237 break;
240 /* Text won't be printed correctly otherwise */
241 setlocale(LC_CTYPE, "");
243 /* X stuff */
244 if(!(dpy = XOpenDisplay(NULL)))
245 eprint("cannot open display\n");
247 xfd = ConnectionNumber(dpy);
249 XSetErrorHandler(xerrorstart);
250 for(screen = 0; screen < ScreenCount(dpy); screen++)
251 /* this causes an error if some other window manager is running */
252 XSelectInput(dpy, RootWindow(dpy, screen), SubstructureRedirectMask);
254 /* need to XSync to validate errorhandler */
255 XSync(dpy, False);
256 XSetErrorHandler(NULL);
257 xerrorxlib = XSetErrorHandler(xerror);
258 XSync(dpy, False);
260 /* store display */
261 globalconf.display = dpy;
263 /* init EWMH atoms */
264 ewmh_init_atoms();
266 /* init screens struct */
267 screen_build_screens();
268 focus_add_client(NULL);
270 /* parse config */
271 config_parse(confpath);
273 /* scan existing windows */
274 scan();
276 /* init cursors */
277 globalconf.cursor[CurNormal] = XCreateFontCursor(globalconf.display, XC_left_ptr);
278 globalconf.cursor[CurResize] = XCreateFontCursor(globalconf.display, XC_sizing);
279 globalconf.cursor[CurMove] = XCreateFontCursor(globalconf.display, XC_fleur);
281 /* for each virtual screen */
282 for(screen = 0; screen < globalconf.nscreen; screen++)
284 /* view at least one tag */
285 tag_view(globalconf.screens[screen].tags, True);
287 for(statusbar = globalconf.screens[screen].statusbar; statusbar; statusbar = statusbar->next)
288 statusbar_init(statusbar);
291 /* select for events */
292 wa.event_mask = SubstructureRedirectMask | SubstructureNotifyMask
293 | EnterWindowMask | LeaveWindowMask | StructureNotifyMask | PointerMotionMask;
294 wa.cursor = globalconf.cursor[CurNormal];
296 /* do this only for real screen */
297 for(screen = 0; screen < ScreenCount(dpy); screen++)
299 XChangeWindowAttributes(globalconf.display,
300 RootWindow(globalconf.display, screen),
301 CWEventMask | CWCursor, &wa);
302 XSelectInput(globalconf.display,
303 RootWindow(globalconf.display, screen),
304 wa.event_mask);
305 ewmh_set_supported_hints(screen);
306 /* call this to at least grab root window clicks */
307 window_root_grabbuttons(screen);
308 window_root_grabkeys(screen);
311 handler = p_new(event_handler *, LASTEvent);
312 handler[ButtonPress] = event_handle_buttonpress;
313 handler[ConfigureRequest] = event_handle_configurerequest;
314 handler[ConfigureNotify] = event_handle_configurenotify;
315 handler[DestroyNotify] = event_handle_destroynotify;
316 handler[EnterNotify] = event_handle_enternotify;
317 handler[LeaveNotify] = event_handle_leavenotify;
318 handler[MotionNotify] = event_handle_motionnotify;
319 handler[Expose] = event_handle_expose;
320 handler[KeyPress] = event_handle_keypress;
321 handler[MappingNotify] = event_handle_mappingnotify;
322 handler[MapRequest] = event_handle_maprequest;
323 handler[PropertyNotify] = event_handle_propertynotify;
324 handler[UnmapNotify] = event_handle_unmapnotify;
325 handler[ClientMessage] = event_handle_clientmessage;
327 /* check for shape extension */
328 if((globalconf.have_shape = XShapeQueryExtension(dpy, &shape_event, &e_dummy)))
330 p_realloc(&handler, shape_event + 1);
331 handler[shape_event] = event_handle_shape;
334 /* check for randr extension */
335 if((globalconf.have_randr = XRRQueryExtension(dpy, &randr_event_base, &e_dummy)))
337 p_realloc(&handler, randr_event_base + RRScreenChangeNotify + 1);
338 handler[randr_event_base + RRScreenChangeNotify] = event_handle_randr_screen_change_notify;
341 XSync(dpy, False);
343 /* get socket fd */
344 csfd = get_client_socket();
345 addr = get_client_addr(getenv("DISPLAY"));
347 if(bind(csfd, (const struct sockaddr *) addr, SUN_LEN(addr)))
349 if(errno == EADDRINUSE)
351 if(unlink(addr->sun_path))
352 perror("error unlinking existing file");
353 if(bind(csfd, (const struct sockaddr *) addr, SUN_LEN(addr)))
354 perror("error binding UNIX domain socket");
356 else
357 perror("error binding UNIX domain socket");
360 /* register function for signals */
361 signal(SIGINT, &exit_on_signal);
362 signal(SIGTERM, &exit_on_signal);
363 signal(SIGHUP, &exit_on_signal);
365 /* refresh everything before waiting events */
366 statusbar_refresh();
367 layout_refresh();
369 /* main event loop, also reads status text from socket */
370 while(running)
372 FD_ZERO(&rd);
373 if(csfd >= 0)
374 FD_SET(csfd, &rd);
375 FD_SET(xfd, &rd);
376 if(select(MAX(xfd, csfd) + 1, &rd, NULL, NULL, NULL) == -1)
378 if(errno == EINTR)
379 continue;
380 eprint("select failed\n");
382 if(csfd >= 0 && FD_ISSET(csfd, &rd))
383 switch (r = recv(csfd, buf, sizeof(buf)-1, MSG_TRUNC))
385 case -1:
386 perror("awesome: error reading UNIX domain socket");
387 csfd = -1;
388 break;
389 case 0:
390 break;
391 default:
392 if(r >= ssizeof(buf))
393 break;
394 buf[r] = '\0';
395 parse_control(buf);
396 statusbar_refresh();
397 layout_refresh();
399 /* two level XPending:
400 * we need to first check we have XEvent to handle
401 * and if so, we handle them all in a round.
402 * Then when we have refresh()'ed stuff so maybe new XEvent
403 * are available and select() won't tell us, so let's check
404 * with XPending() again.
406 while(XPending(dpy))
408 while(XPending(dpy))
410 XNextEvent(dpy, &ev);
411 if(handler[ev.type])
412 handler[ev.type](&ev);
414 /* need to resync */
415 XSync(dpy, False);
418 statusbar_refresh();
419 layout_refresh();
421 /* need to resync */
422 XSync(dpy, False);
427 if(csfd > 0 && close(csfd))
428 perror("error closing UNIX domain socket");
429 if(unlink(addr->sun_path))
430 perror("error unlinking UNIX domain socket");
431 p_delete(&addr);
433 /* remap all clients since some WM won't handle them otherwise */
434 for(c = globalconf.clients; c; c = c->next)
435 client_unban(c);
437 XSync(globalconf.display, False);
439 XCloseDisplay(dpy);
441 return EXIT_SUCCESS;
443 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80