[config] add missing documentation
[awesome.git] / awesome.c
blobf16a65817ccbf99484382efff56f7d42a3b162fb
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"
60 #include "common/xscreen.h"
62 static int (*xerrorxlib) (Display *, XErrorEvent *);
63 static Bool running = True;
65 AwesomeConf globalconf;
67 /** Scan X to find windows to manage
69 static void
70 scan()
72 unsigned int i, num;
73 int screen, real_screen;
74 Window *wins = NULL, d1, d2;
75 XWindowAttributes wa;
77 for(screen = 0; screen < ScreenCount(globalconf.display); screen++)
79 if(XQueryTree(globalconf.display, RootWindow(globalconf.display, screen), &d1, &d2, &wins, &num))
80 for(i = 0; i < num; i++)
81 /* XGetWindowAttributes return 1 on success */
82 if(XGetWindowAttributes(globalconf.display, wins[i], &wa)
83 && !wa.override_redirect
84 && (wa.map_state == IsViewable || window_getstate(wins[i]) == IconicState))
86 real_screen = screen_get_bycoord(globalconf.screens_info, screen, wa.x, wa.y);
87 client_manage(wins[i], &wa, real_screen);
89 if(wins)
90 XFree(wins);
94 /** Startup Error handler to check if another window manager
95 * is already running.
96 * \param disp Display
97 * \param ee Error event
99 static int __attribute__ ((noreturn))
100 xerrorstart(Display * disp __attribute__ ((unused)),
101 XErrorEvent * ee __attribute__ ((unused)))
103 eprint("another window manager is already running\n");
106 /** Quit awesome
107 * \param screen Screen ID
108 * \param arg nothing
109 * \ingroup ui_callback
111 void
112 uicb_quit(int screen __attribute__ ((unused)), char *arg __attribute__ ((unused)))
114 running = False;
117 static void
118 exit_on_signal(int sig __attribute__ ((unused)))
120 running = False;
123 /** \brief awesome xerror function
124 * There's no way to check accesses to destroyed windows, thus those cases are
125 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
126 * default error handler, which may call exit.
127 * \param edpy display ref
128 * \param ee XErrorEvent event
129 * \return 0 if no error, or xerror's xlib return status
131 static int
132 xerror(Display *edpy, XErrorEvent *ee)
134 if(ee->error_code == BadWindow
135 || (ee->error_code == BadMatch && ee->request_code == X_SetInputFocus)
136 || (ee->error_code == BadValue && ee->request_code == X_KillClient)
137 || (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch))
138 return 0;
139 warn("fatal error: request code=%d, error code=%d\n", ee->request_code, ee->error_code);
140 return xerrorxlib(edpy, ee); /* may call exit */
143 /** Print help and exit(2) with given exit_code.
145 static void __attribute__ ((noreturn))
146 exit_help(int exit_code)
148 FILE *outfile = (exit_code == EXIT_SUCCESS) ? stdout : stderr;
149 fprintf(outfile,
150 "Usage: awesome [OPTION]\n\
151 -h, --help show help\n\
152 -v, --version show version\n\
153 -c, --config FILE configuration file to use\n\
154 -k, --check check configuration file syntax\n\
155 -s --sync enable synchronization (X debug)\n");
156 exit(exit_code);
159 /** Hello, this is main
160 * \param argc who knows
161 * \param argv who knows
162 * \return EXIT_SUCCESS I hope
164 typedef void event_handler (XEvent *);
166 main(int argc, char *argv[])
168 char buf[1024];
169 const char *confpath = NULL;
170 int r, xfd, e_dummy, csfd, shape_event, randr_event_base, i, screen, opt;
171 ssize_t cmdlen = 1;
172 Statusbar *statusbar;
173 fd_set rd;
174 XEvent ev;
175 Display * dpy;
176 event_handler **handler;
177 struct sockaddr_un *addr;
178 Client *c;
179 XSetWindowAttributes wa;
180 Bool xsync = False, confcheck = False;
181 static struct option long_options[] =
183 {"help", 0, NULL, 'h'},
184 {"version", 0, NULL, 'v'},
185 {"check", 0, NULL, 'k'},
186 {"config", 1, NULL, 'c'},
187 {"sync", 0, NULL, 's'},
188 {NULL, 0, NULL, 0}
191 /* save argv */
192 for(i = 0; i < argc; i++)
193 cmdlen += a_strlen(argv[i]) + 1;
195 globalconf.argv = p_new(char, cmdlen);
196 a_strcpy(globalconf.argv, cmdlen, argv[0]);
198 for(i = 1; i < argc; i++)
200 a_strcat(globalconf.argv, cmdlen, " ");
201 a_strcat(globalconf.argv, cmdlen, argv[i]);
204 /* check args */
205 while((opt = getopt_long(argc, argv, "vhkc:s",
206 long_options, NULL)) != -1)
207 switch(opt)
209 case 'v':
210 eprint_version("awesome");
211 break;
212 case 'h':
213 exit_help(EXIT_SUCCESS);
214 break;
215 case 'c':
216 if(a_strlen(optarg))
217 confpath = a_strdup(optarg);
218 else
219 eprint("-c option requires a file name\n");
220 break;
221 case 'k':
222 confcheck = True;
223 break;
224 case 's':
225 xsync = True;
226 break;
229 /* Text won't be printed correctly otherwise */
230 setlocale(LC_CTYPE, "");
232 if(confcheck)
233 return config_check(confpath);
235 /* X stuff */
236 if(!(dpy = XOpenDisplay(NULL)))
237 eprint("cannot open display\n");
239 xfd = ConnectionNumber(dpy);
241 XSetErrorHandler(xerrorstart);
242 for(screen = 0; screen < ScreenCount(dpy); screen++)
243 /* this causes an error if some other window manager is running */
244 XSelectInput(dpy, RootWindow(dpy, screen), SubstructureRedirectMask);
246 /* need to XSync to validate errorhandler */
247 XSync(dpy, False);
248 XSetErrorHandler(NULL);
249 xerrorxlib = XSetErrorHandler(xerror);
250 XSync(dpy, False);
252 if(xsync)
253 XSynchronize(dpy, True);
255 /* store display */
256 globalconf.display = dpy;
258 /* init EWMH atoms */
259 ewmh_init_atoms();
261 /* init screens struct */
262 globalconf.screens_info = screensinfo_new(dpy);
263 globalconf.screens = p_new(VirtScreen, globalconf.screens_info->nscreen);
264 focus_add_client(NULL);
266 /* parse config */
267 config_parse(confpath);
269 /* init cursors */
270 globalconf.cursor[CurNormal] = XCreateFontCursor(globalconf.display, XC_left_ptr);
271 globalconf.cursor[CurResize] = XCreateFontCursor(globalconf.display, XC_sizing);
272 globalconf.cursor[CurMove] = XCreateFontCursor(globalconf.display, XC_fleur);
274 /* for each virtual screen */
275 for(screen = 0; screen < globalconf.screens_info->nscreen; screen++)
277 /* view at least one tag */
278 tag_view(globalconf.screens[screen].tags, True);
280 for(statusbar = globalconf.screens[screen].statusbar; statusbar; statusbar = statusbar->next)
281 statusbar_init(statusbar);
284 /* select for events */
285 wa.event_mask = SubstructureRedirectMask | SubstructureNotifyMask
286 | EnterWindowMask | LeaveWindowMask | StructureNotifyMask | PointerMotionMask;
287 wa.cursor = globalconf.cursor[CurNormal];
289 /* do this only for real screen */
290 for(screen = 0; screen < ScreenCount(dpy); screen++)
292 XChangeWindowAttributes(globalconf.display,
293 RootWindow(globalconf.display, screen),
294 CWEventMask | CWCursor, &wa);
295 XSelectInput(globalconf.display,
296 RootWindow(globalconf.display, screen),
297 wa.event_mask);
298 ewmh_set_supported_hints(screen);
299 /* call this to at least grab root window clicks */
300 window_root_grabbuttons(screen);
301 window_root_grabkeys(screen);
304 /* scan existing windows */
305 scan();
307 handler = p_new(event_handler *, LASTEvent);
308 handler[ButtonPress] = event_handle_buttonpress;
309 handler[ConfigureRequest] = event_handle_configurerequest;
310 handler[ConfigureNotify] = event_handle_configurenotify;
311 handler[DestroyNotify] = event_handle_destroynotify;
312 handler[EnterNotify] = event_handle_enternotify;
313 handler[LeaveNotify] = event_handle_leavenotify;
314 handler[MotionNotify] = event_handle_motionnotify;
315 handler[Expose] = event_handle_expose;
316 handler[KeyPress] = event_handle_keypress;
317 handler[MappingNotify] = event_handle_mappingnotify;
318 handler[MapRequest] = event_handle_maprequest;
319 handler[PropertyNotify] = event_handle_propertynotify;
320 handler[UnmapNotify] = event_handle_unmapnotify;
321 handler[ClientMessage] = event_handle_clientmessage;
323 /* check for shape extension */
324 if((globalconf.have_shape = XShapeQueryExtension(dpy, &shape_event, &e_dummy)))
326 p_realloc(&handler, shape_event + 1);
327 handler[shape_event] = event_handle_shape;
330 /* check for randr extension */
331 if((globalconf.have_randr = XRRQueryExtension(dpy, &randr_event_base, &e_dummy)))
333 p_realloc(&handler, randr_event_base + RRScreenChangeNotify + 1);
334 handler[randr_event_base + RRScreenChangeNotify] = event_handle_randr_screen_change_notify;
337 XSync(dpy, False);
339 /* get socket fd */
340 csfd = socket_getclient();
341 addr = socket_getaddr(getenv("DISPLAY"));
343 if(bind(csfd, (const struct sockaddr *) addr, SUN_LEN(addr)))
345 if(errno == EADDRINUSE)
347 if(unlink(addr->sun_path))
348 warn("error unlinking existing file: %s\n", strerror(errno));
349 if(bind(csfd, (const struct sockaddr *) addr, SUN_LEN(addr)))
350 warn("error binding UNIX domain socket: %s\n", strerror(errno));
352 else
353 warn("error binding UNIX domain socket: %s\n", strerror(errno));
356 /* register function for signals */
357 signal(SIGINT, &exit_on_signal);
358 signal(SIGTERM, &exit_on_signal);
359 signal(SIGHUP, &exit_on_signal);
361 /* refresh everything before waiting events */
362 statusbar_refresh();
363 layout_refresh();
365 /* main event loop, also reads status text from socket */
366 while(running)
368 FD_ZERO(&rd);
369 if(csfd >= 0)
370 FD_SET(csfd, &rd);
371 FD_SET(xfd, &rd);
372 if(select(MAX(xfd, csfd) + 1, &rd, NULL, NULL, NULL) == -1)
374 if(errno == EINTR)
375 continue;
376 eprint("select failed\n");
378 if(csfd >= 0 && FD_ISSET(csfd, &rd))
379 switch (r = recv(csfd, buf, sizeof(buf)-1, MSG_TRUNC))
381 case -1:
382 warn("error reading UNIX domain socket: %s\n", strerror(errno));
383 csfd = -1;
384 break;
385 case 0:
386 break;
387 default:
388 if(r >= ssizeof(buf))
389 break;
390 buf[r] = '\0';
391 parse_control(buf);
392 statusbar_refresh();
393 layout_refresh();
395 /* two level XPending:
396 * we need to first check we have XEvent to handle
397 * and if so, we handle them all in a round.
398 * Then when we have refresh()'ed stuff so maybe new XEvent
399 * are available and select() won't tell us, so let's check
400 * with XPending() again.
402 while(XPending(dpy))
404 while(XPending(dpy))
406 XNextEvent(dpy, &ev);
407 if(handler[ev.type])
408 handler[ev.type](&ev);
410 /* need to resync */
411 XSync(dpy, False);
414 statusbar_refresh();
415 layout_refresh();
417 /* need to resync */
418 XSync(dpy, False);
423 if(csfd > 0 && close(csfd))
424 warn("error closing UNIX domain socket: %s\n", strerror(errno));
425 if(unlink(addr->sun_path))
426 warn("error unlinking UNIX domain socket: %s\n", strerror(errno));
427 p_delete(&addr);
429 /* remap all clients since some WM won't handle them otherwise */
430 for(c = globalconf.clients; c; c = c->next)
431 client_unban(c);
433 XSync(globalconf.display, False);
435 XCloseDisplay(dpy);
437 return EXIT_SUCCESS;
439 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80