change draw_color_new() proto to fill the struct and return status
[awesome.git] / awesome.c
blob821467723ad7cdf8d5adfbbb8da68f64e00e5390
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/awclient.h"
57 #include "common/util.h"
58 #include "common/awesome-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 real_screen = screen;
81 for(i = 0; i < num; i++)
83 /* XGetWindowAttributes return 1 on success */
84 if(!XGetWindowAttributes(globalconf.display, wins[i], &wa)
85 || wa.override_redirect
86 || XGetTransientForHint(globalconf.display, wins[i], &d1))
87 continue;
88 if(wa.map_state == IsViewable || window_getstate(wins[i]) == IconicState)
90 if(screen == 0)
91 real_screen = screen_get_bycoord(wa.x, wa.y);
92 client_manage(wins[i], &wa, real_screen);
95 /* now the transients */
96 for(i = 0; i < num; i++)
98 if(!XGetWindowAttributes(globalconf.display, wins[i], &wa))
99 continue;
100 if(XGetTransientForHint(globalconf.display, wins[i], &d1)
101 && (wa.map_state == IsViewable || window_getstate(wins[i]) == IconicState))
103 if(screen == 0)
104 real_screen = screen_get_bycoord(wa.x, wa.y);
105 client_manage(wins[i], &wa, real_screen);
109 if(wins)
110 XFree(wins);
114 /** Setup everything before running
115 * \param screen Screen number
116 * \todo clean things...
118 static void
119 setup(int screen)
121 XSetWindowAttributes wa;
122 Statusbar *statusbar;
123 int phys_screen = get_phys_screen(screen);
125 /* init cursors */
126 globalconf.cursor[CurNormal] = XCreateFontCursor(globalconf.display, XC_left_ptr);
127 globalconf.cursor[CurResize] = XCreateFontCursor(globalconf.display, XC_sizing);
128 globalconf.cursor[CurMove] = XCreateFontCursor(globalconf.display, XC_fleur);
130 /* select for events */
131 wa.event_mask = SubstructureRedirectMask | SubstructureNotifyMask
132 | EnterWindowMask | LeaveWindowMask | StructureNotifyMask;
133 wa.cursor = globalconf.cursor[CurNormal];
135 XChangeWindowAttributes(globalconf.display,
136 RootWindow(globalconf.display, phys_screen),
137 CWEventMask | CWCursor, &wa);
139 XSelectInput(globalconf.display,
140 RootWindow(globalconf.display, phys_screen),
141 wa.event_mask);
143 grabkeys(phys_screen);
145 /* view at least one tag */
146 tag_view(globalconf.screens[screen].tags, True);
148 for(statusbar = globalconf.screens[screen].statusbar; statusbar; statusbar = statusbar->next)
149 statusbar_init(statusbar);
152 /** Startup Error handler to check if another window manager
153 * is already running.
154 * \param disp Display
155 * \param ee Error event
157 static int __attribute__ ((noreturn))
158 xerrorstart(Display * disp __attribute__ ((unused)),
159 XErrorEvent * ee __attribute__ ((unused)))
161 eprint("another window manager is already running\n");
164 /** Quit awesome
165 * \param screen Screen ID
166 * \param arg nothing
167 * \ingroup ui_callback
169 void
170 uicb_quit(int screen __attribute__ ((unused)), char *arg __attribute__ ((unused)))
172 running = False;
175 static void
176 exit_on_signal(int sig __attribute__ ((unused)))
178 running = False;
181 /* There's no way to check accesses to destroyed windows, thus those cases are
182 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
183 * default error handler, which may call exit.
185 static int
186 xerror(Display *edpy, XErrorEvent *ee)
188 if(ee->error_code == BadWindow
189 || (ee->error_code == BadMatch && ee->request_code == X_SetInputFocus)
190 || (ee->error_code == BadValue && ee->request_code == X_KillClient))
191 return 0;
192 warn("fatal error: request code=%d, error code=%d\n", ee->request_code, ee->error_code);
193 return xerrorxlib(edpy, ee); /* may call exit */
196 /** Print help and exit(2) with given exit_code.
198 static void __attribute__ ((noreturn))
199 exit_help(int exit_code)
201 FILE *outfile = (exit_code == EXIT_SUCCESS) ? stdout : stderr;
202 fprintf(outfile,
203 "Usage: awesome [OPTION]\n\
204 -h, --help show help\n\
205 -v, --version show version\n\
206 -c, --config FILE configuration file to use\n\
207 -k, --check check configuration file syntax\n");
208 exit(exit_code);
211 /** Hello, this is main
212 * \param argc who knows
213 * \param argv who knows
214 * \return EXIT_SUCCESS I hope
216 typedef void event_handler (XEvent *);
218 main(int argc, char *argv[])
220 char buf[1024];
221 const char *confpath = NULL;
222 int r, xfd, e_dummy, csfd;
223 fd_set rd;
224 XEvent ev;
225 Display * dpy;
226 int shape_event, randr_event_base;
227 int screen;
228 int i, cmdlen;
229 event_handler **handler;
230 struct sockaddr_un *addr;
231 int opt;
232 Client *c;
233 static struct option long_options[] =
235 {"help", 0, NULL, 'h'},
236 {"version", 0, NULL, 'v'},
237 {"check", 0, NULL, 'k'},
238 {"config", 0, NULL, 'c'},
239 {NULL, 0, NULL, 0}
242 /* check args */
243 while((opt = getopt_long(argc, argv, "vhkc:",
244 long_options, NULL)) != -1)
245 switch(opt)
247 case 'v':
248 eprint_version("awesome");
249 break;
250 case 'h':
251 exit_help(EXIT_SUCCESS);
252 break;
253 case 'c':
254 if(a_strlen(optarg))
255 confpath = optarg;
256 else
257 eprint("-c option requires a file name\n");
258 break;
259 case 'k':
260 return config_check(confpath);
261 break;
264 /* Text won't be printed correctly otherwise */
265 setlocale(LC_CTYPE, "");
267 /* X stuff */
268 if(!(dpy = XOpenDisplay(NULL)))
269 eprint("cannot open display\n");
271 for(cmdlen = 0, i = 0; i < argc; i++)
272 cmdlen += a_strlen(argv[i] + 1);
273 globalconf.argv = p_new(char, cmdlen);
274 a_strcpy(globalconf.argv, cmdlen, argv[0]);
275 for(i = 1; i < argc; i++)
277 a_strcat(globalconf.argv, cmdlen, " ");
278 a_strcat(globalconf.argv, cmdlen, argv[i]);
281 xfd = ConnectionNumber(dpy);
283 XSetErrorHandler(xerrorstart);
284 for(screen = 0; screen < ScreenCount(dpy); screen++)
285 /* this causes an error if some other window manager is running */
286 XSelectInput(dpy, RootWindow(dpy, screen), SubstructureRedirectMask);
288 /* need to XSync to validate errorhandler */
289 XSync(dpy, False);
290 XSetErrorHandler(NULL);
291 xerrorxlib = XSetErrorHandler(xerror);
292 XSync(dpy, False);
294 /* store display */
295 globalconf.display = dpy;
297 /* init EWMH atoms */
298 ewmh_init_atoms();
300 /* init screens struct */
301 screen_build_screens();
302 focus_add_client(NULL);
304 /* parse config */
305 config_parse(confpath);
307 scan();
309 /* for each virtual screen */
310 for(screen = 0; screen < globalconf.nscreen; screen++)
311 setup(screen);
313 /* do this only for real screen */
314 for(screen = 0; screen < ScreenCount(dpy); screen++)
316 ewmh_set_supported_hints(screen);
317 /* call this to at least grab root window clicks */
318 window_root_grabbuttons(screen);
321 handler = p_new(event_handler *, LASTEvent);
322 handler[ButtonPress] = handle_event_buttonpress;
323 handler[ConfigureRequest] = handle_event_configurerequest;
324 handler[ConfigureNotify] = handle_event_configurenotify;
325 handler[DestroyNotify] = handle_event_destroynotify;
326 handler[EnterNotify] = handle_event_enternotify;
327 handler[LeaveNotify] = handle_event_leavenotify;
328 handler[Expose] = handle_event_expose;
329 handler[KeyPress] = handle_event_keypress;
330 handler[MappingNotify] = handle_event_mappingnotify;
331 handler[MapRequest] = handle_event_maprequest;
332 handler[PropertyNotify] = handle_event_propertynotify;
333 handler[UnmapNotify] = handle_event_unmapnotify;
334 handler[ClientMessage] = handle_event_clientmessage;
336 /* check for shape extension */
337 if((globalconf.have_shape = XShapeQueryExtension(dpy, &shape_event, &e_dummy)))
339 p_realloc(&handler, shape_event + 1);
340 handler[shape_event] = handle_event_shape;
343 /* check for randr extension */
344 if((globalconf.have_randr = XRRQueryExtension(dpy, &randr_event_base, &e_dummy)))
346 p_realloc(&handler, randr_event_base + RRScreenChangeNotify + 1);
347 handler[randr_event_base + RRScreenChangeNotify] = handle_event_randr_screen_change_notify;
350 XSync(dpy, False);
352 /* get socket fd */
353 csfd = get_client_socket();
354 addr = get_client_addr(getenv("DISPLAY"));
356 if(bind(csfd, (const struct sockaddr *) addr, SUN_LEN(addr)))
358 if(errno == EADDRINUSE)
360 if(unlink(addr->sun_path))
361 perror("error unlinking existing file");
362 if(bind(csfd, (const struct sockaddr *) addr, SUN_LEN(addr)))
363 perror("error binding UNIX domain socket");
365 else
366 perror("error binding UNIX domain socket");
369 /* register function for signals */
370 signal(SIGINT, &exit_on_signal);
371 signal(SIGTERM, &exit_on_signal);
372 signal(SIGHUP, &exit_on_signal);
374 /* refresh everything before waiting events */
375 statusbar_refresh();
376 layout_refresh();
378 /* main event loop, also reads status text from socket */
379 while(running)
381 FD_ZERO(&rd);
382 if(csfd >= 0)
383 FD_SET(csfd, &rd);
384 FD_SET(xfd, &rd);
385 if(select(MAX(xfd, csfd) + 1, &rd, NULL, NULL, NULL) == -1)
387 if(errno == EINTR)
388 continue;
389 eprint("select failed\n");
391 if(csfd >= 0 && FD_ISSET(csfd, &rd))
392 switch (r = recv(csfd, buf, sizeof(buf)-1, MSG_TRUNC))
394 case -1:
395 perror("awesome: error reading UNIX domain socket");
396 csfd = -1;
397 break;
398 case 0:
399 break;
400 default:
401 if(r >= ssizeof(buf))
402 break;
403 buf[r] = '\0';
404 parse_control(buf);
405 statusbar_refresh();
406 layout_refresh();
408 /* two level XPending:
409 * we need to first check we have XEvent to handle
410 * and if so, we handle them all in a round.
411 * Then when we have refresh()'ed stuff so maybe new XEvent
412 * are available and select() won't tell us, so let's check
413 * with XPending() again.
415 while(XPending(dpy))
417 while(XPending(dpy))
419 XNextEvent(dpy, &ev);
420 if(handler[ev.type])
421 handler[ev.type](&ev);
423 /* drop events requested to */
424 if(globalconf.drop_events)
426 /* need to resync */
427 XSync(dpy, False);
428 while(XCheckMaskEvent(dpy, globalconf.drop_events, &ev));
429 globalconf.drop_events = NoEventMask;
432 /* need to resync */
433 XSync(dpy, False);
436 statusbar_refresh();
437 layout_refresh();
439 /* need to resync */
440 XSync(dpy, False);
445 if(csfd > 0 && close(csfd))
446 perror("error closing UNIX domain socket");
447 if(unlink(addr->sun_path))
448 perror("error unlinking UNIX domain socket");
449 p_delete(&addr);
451 /* remap all clients since some WM won't handle them otherwise */
452 for(c = globalconf.clients; c; c = c->next)
453 client_unban(c);
455 XSync(globalconf.display, False);
457 XCloseDisplay(dpy);
459 return EXIT_SUCCESS;
461 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80