add copyright
[awesome.git] / awesome.c
blob2b9f2acda5374001f466053e8a3c4e2c6eec387b
1 /*
2 * awesome.c - awesome main functions
4 * Copyright © 2007 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 <errno.h>
23 #include <locale.h>
24 #include <stdio.h>
25 #include <unistd.h>
26 #include <sys/select.h>
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #include <sys/socket.h>
30 #include <sys/un.h>
31 #include <fcntl.h>
32 #include <X11/cursorfont.h>
33 #include <X11/keysym.h>
34 #include <X11/Xatom.h>
35 #include <X11/Xproto.h>
36 #include <X11/Xutil.h>
37 #include <X11/extensions/shape.h>
38 #include <X11/extensions/Xrandr.h>
40 #include "awesome.h"
41 #include "event.h"
42 #include "layout.h"
43 #include "screen.h"
44 #include "util.h"
45 #include "statusbar.h"
46 #include "uicb.h"
47 #include "window.h"
48 #include "client.h"
49 #include "focus.h"
50 #include "awesome-client.h"
52 static int (*xerrorxlib) (Display *, XErrorEvent *);
53 static Bool running = True;
55 AwesomeConf globalconf;
57 static inline void
58 cleanup_buttons(Button *buttons)
60 Button *b, *bn;
62 for(b = buttons; b; b = bn)
64 bn = b->next;
65 p_delete(&b->arg);
66 p_delete(&b);
70 static void
71 cleanup_screen(int screen)
73 Layout *l, *ln;
74 Tag *t, *tn;
76 XftFontClose(globalconf.display, globalconf.screens[screen].font);
77 XUngrabKey(globalconf.display, AnyKey, AnyModifier, RootWindow(globalconf.display, get_phys_screen(screen)));
78 XDestroyWindow(globalconf.display, globalconf.screens[screen].statusbar->window);
80 for(t = globalconf.screens[screen].tags; t; t = tn)
82 tn = t->next;
83 p_delete(&t->name);
84 p_delete(&t);
87 for(l = globalconf.screens[screen].layouts; l; l = ln)
89 ln = l->next;
90 p_delete(&l->symbol);
91 p_delete(&l);
95 /** Cleanup everything on quit
96 * \param awesomeconf awesome config
98 static void
99 cleanup()
101 int screen;
102 Rule *r, *rn;
103 Key *k, *kn;
105 while(globalconf.clients)
107 client_unban(globalconf.clients);
108 client_unmanage(globalconf.clients, NormalState);
111 XFreeCursor(globalconf.display, globalconf.cursor[CurNormal]);
112 XFreeCursor(globalconf.display, globalconf.cursor[CurResize]);
113 XFreeCursor(globalconf.display, globalconf.cursor[CurMove]);
115 for(r = globalconf.rules; r; r = rn)
117 rn = r->next;
118 p_delete(&r->prop);
119 p_delete(&r->tags);
120 p_delete(&r->propregex);
121 p_delete(&r->tagregex);
122 p_delete(&r);
125 for(k = globalconf.keys; k; k = kn)
127 kn = k->next;
128 p_delete(&k->arg);
129 p_delete(&k);
132 cleanup_buttons(globalconf.buttons.tag);
133 cleanup_buttons(globalconf.buttons.title);
134 cleanup_buttons(globalconf.buttons.layout);
135 cleanup_buttons(globalconf.buttons.root);
136 cleanup_buttons(globalconf.buttons.client);
138 p_delete(&globalconf.configpath);
140 for(screen = 0; screen < get_screen_count(); screen++)
141 cleanup_screen(screen);
143 XSetInputFocus(globalconf.display, PointerRoot, RevertToPointerRoot, CurrentTime);
144 XSync(globalconf.display, False);
146 p_delete(&globalconf.clients);
149 /** Scan X to find windows to manage
151 static void
152 scan()
154 unsigned int i, num;
155 int screen, real_screen;
156 Window *wins = NULL, d1, d2;
157 XWindowAttributes wa;
159 for(screen = 0; screen < ScreenCount(globalconf.display); screen++)
161 if(XQueryTree(globalconf.display, RootWindow(globalconf.display, screen), &d1, &d2, &wins, &num))
163 real_screen = screen;
164 for(i = 0; i < num; i++)
166 if(!XGetWindowAttributes(globalconf.display, wins[i], &wa)
167 || wa.override_redirect
168 || XGetTransientForHint(globalconf.display, wins[i], &d1))
169 continue;
170 if(wa.map_state == IsViewable || window_getstate(globalconf.display, wins[i]) == IconicState)
172 if(screen == 0)
173 real_screen = get_screen_bycoord(wa.x, wa.y);
174 client_manage(wins[i], &wa, real_screen);
177 /* now the transients */
178 for(i = 0; i < num; i++)
180 if(!XGetWindowAttributes(globalconf.display, wins[i], &wa))
181 continue;
182 if(XGetTransientForHint(globalconf.display, wins[i], &d1)
183 && (wa.map_state == IsViewable || window_getstate(globalconf.display, wins[i]) == IconicState))
185 if(screen == 0)
186 real_screen = get_screen_bycoord(wa.x, wa.y);
187 client_manage(wins[i], &wa, real_screen);
191 if(wins)
192 XFree(wins);
196 /** Setup everything before running
197 * \param screen Screen number
198 * \todo clean things...
200 static void
201 setup(int screen)
203 XSetWindowAttributes wa;
205 /* init cursors */
206 globalconf.cursor[CurNormal] = XCreateFontCursor(globalconf.display, XC_left_ptr);
207 globalconf.cursor[CurResize] = XCreateFontCursor(globalconf.display, XC_sizing);
208 globalconf.cursor[CurMove] = XCreateFontCursor(globalconf.display, XC_fleur);
210 /* select for events */
211 wa.event_mask = SubstructureRedirectMask | SubstructureNotifyMask
212 | EnterWindowMask | LeaveWindowMask | StructureNotifyMask;
213 wa.cursor = globalconf.cursor[CurNormal];
215 XChangeWindowAttributes(globalconf.display,
216 RootWindow(globalconf.display, get_phys_screen(screen)),
217 CWEventMask | CWCursor, &wa);
219 XSelectInput(globalconf.display,
220 RootWindow(globalconf.display, get_phys_screen(screen)),
221 wa.event_mask);
223 grabkeys(screen);
226 /** Startup Error handler to check if another window manager
227 * is already running.
228 * \param disp Display
229 * \param ee Error event
231 static int __attribute__ ((noreturn))
232 xerrorstart(Display * disp __attribute__ ((unused)),
233 XErrorEvent * ee __attribute__ ((unused)))
235 eprint("another window manager is already running\n");
238 /** Quit awesome
239 * \param screen Screen ID
240 * \param arg nothing
241 * \ingroup ui_callback
243 void
244 uicb_quit(int screen __attribute__ ((unused)), char *arg __attribute__ ((unused)))
246 running = False;
249 /* There's no way to check accesses to destroyed windows, thus those cases are
250 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
251 * default error handler, which may call exit.
254 xerror(Display * edpy, XErrorEvent * ee)
256 if(ee->error_code == BadWindow
257 || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
258 || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
259 || (ee->request_code == X_PolyFillRectangle
260 && ee->error_code == BadDrawable)
261 || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
262 || (ee->request_code == X_ConfigureWindow
263 && ee->error_code == BadMatch) || (ee->request_code == X_GrabKey
264 && ee->error_code == BadAccess)
265 || (ee->request_code == X_CopyArea && ee->error_code == BadDrawable))
266 return 0;
267 warn("fatal error: request code=%d, error code=%d\n", ee->request_code, ee->error_code);
268 return xerrorxlib(edpy, ee); /* may call exit */
271 /** Hello, this is main
272 * \param argc who knows
273 * \param argv who knows
274 * \return EXIT_SUCCESS I hope
276 typedef void event_handler (XEvent *);
278 main(int argc, char *argv[])
280 char buf[1024];
281 const char *confpath = NULL;
282 int r, xfd, e_dummy, csfd;
283 fd_set rd;
284 XEvent ev;
285 Display * dpy;
286 int shape_event, randr_event_base;
287 int screen;
288 enum { NetSupported, NetWMName, NetLast }; /* EWMH atoms */
289 Atom netatom[NetLast];
290 event_handler **handler;
291 struct sockaddr_un *addr;
293 if(argc >= 2)
295 if(!a_strcmp("-v", argv[1]) || !a_strcmp("--version", argv[1]))
297 printf("awesome-" VERSION " (" RELEASE ")\n");
298 return EXIT_SUCCESS;
300 else if(!a_strcmp("-c", argv[1]))
302 if(a_strlen(argv[2]))
303 confpath = argv[2];
304 else
305 eprint("-c require a file\n");
307 else
308 eprint("options: [-v | -c configfile]\n");
311 /* Tag won't be printed otherwised */
312 setlocale(LC_CTYPE, "");
314 if(!(dpy = XOpenDisplay(NULL)))
315 eprint("cannot open display\n");
317 xfd = ConnectionNumber(dpy);
319 XSetErrorHandler(xerrorstart);
320 for(screen = 0; screen < ScreenCount(dpy); screen++)
321 /* this causes an error if some other window manager is running */
322 XSelectInput(dpy, RootWindow(dpy, screen), SubstructureRedirectMask);
324 /* need to XSync to validate errorhandler */
325 XSync(dpy, False);
326 XSetErrorHandler(NULL);
327 xerrorxlib = XSetErrorHandler(xerror);
328 XSync(dpy, False);
329 globalconf.display = dpy;
331 globalconf.screens = p_new(VirtScreen, get_screen_count());
332 focus_add_client(NULL);
333 /* store display */
334 config_parse(confpath);
336 for(screen = 0; screen < get_screen_count(); screen++)
338 /* set screen */
339 setup(screen);
340 statusbar_init(screen);
341 statusbar_draw(screen);
344 netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
345 netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
347 /* do this only for real screen */
348 for(screen = 0; screen < ScreenCount(dpy); screen++)
350 loadawesomeprops(screen);
351 XChangeProperty(dpy, RootWindow(dpy, screen), netatom[NetSupported],
352 XA_ATOM, 32, PropModeReplace, (unsigned char *) netatom, NetLast);
355 handler = p_new(event_handler *, LASTEvent);
356 handler[ButtonPress] = handle_event_buttonpress;
357 handler[ConfigureRequest] = handle_event_configurerequest;
358 handler[ConfigureNotify] = handle_event_configurenotify;
359 handler[DestroyNotify] = handle_event_destroynotify;
360 handler[EnterNotify] = handle_event_enternotify;
361 handler[LeaveNotify] = handle_event_leavenotify;
362 handler[Expose] = handle_event_expose;
363 handler[KeyPress] = handle_event_keypress;
364 handler[MappingNotify] = handle_event_mappingnotify;
365 handler[MapRequest] = handle_event_maprequest;
366 handler[PropertyNotify] = handle_event_propertynotify;
367 handler[UnmapNotify] = handle_event_unmapnotify;
369 /* check for shape extension */
370 if((globalconf.have_shape = XShapeQueryExtension(dpy, &shape_event, &e_dummy)))
372 p_realloc(&handler, shape_event + 1);
373 handler[shape_event] = handle_event_shape;
376 /* check for randr extension */
377 if((globalconf.have_randr = XRRQueryExtension(dpy, &randr_event_base, &e_dummy)))
379 p_realloc(&handler, randr_event_base + RRScreenChangeNotify + 1);
380 handler[randr_event_base + RRScreenChangeNotify] = handle_event_randr_screen_change_notify;
383 scan();
385 XSync(dpy, False);
387 /* get socket fd */
388 csfd = get_client_socket();
389 addr = get_client_addr(getenv("DISPLAY"));
391 if(bind(csfd, (const struct sockaddr *) addr, SUN_LEN(addr)))
393 if(errno == EADDRINUSE)
395 if(unlink(addr->sun_path))
396 perror("error unlinking existing file");
397 if(bind(csfd, (const struct sockaddr *) addr, SUN_LEN(addr)))
398 perror("error binding UNIX domain socket");
400 else
401 perror("error binding UNIX domain socket");
404 /* main event loop, also reads status text from socket */
405 while(running)
407 FD_ZERO(&rd);
408 if(csfd >= 0)
409 FD_SET(csfd, &rd);
410 FD_SET(xfd, &rd);
411 if(select(MAX(xfd, csfd) + 1, &rd, NULL, NULL, NULL) == -1)
413 if(errno == EINTR)
414 continue;
415 eprint("select failed\n");
417 if(csfd >= 0 && FD_ISSET(csfd, &rd))
418 switch (r = recv(csfd, buf, sizeof(buf)-1, MSG_TRUNC))
420 case -1:
421 perror("awesome: error reading UNIX domain socket");
422 csfd = -1;
423 break;
424 case 0:
425 break;
426 default:
427 if(r >= ssizeof(buf))
428 break;
429 buf[r] = '\0';
430 parse_control(buf);
433 while(XPending(dpy))
435 XNextEvent(dpy, &ev);
436 if(handler[ev.type])
437 handler[ev.type](&ev); /* call handler */
441 if(csfd > 0 && close(csfd))
442 perror("error closing UNIX domain socket");
443 if(unlink(addr->sun_path))
444 perror("error unlinking UNIX domain socket");
445 p_delete(&addr);
447 cleanup();
448 XCloseDisplay(dpy);
450 return EXIT_SUCCESS;
452 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80