also update rcoords on configure request
[awesome.git] / awesome.c
blob7cb97eaffd32e6d67cea5e60204d87ff5afcbbc7
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 "awesome-client.h"
50 static int (*xerrorxlib) (Display *, XErrorEvent *);
51 static Bool running = True;
54 static inline void
55 cleanup_buttons(Button *buttons)
57 Button *b, *bn;
59 for(b = buttons; b; b = bn)
61 bn = b->next;
62 p_delete(&b->arg);
63 p_delete(&b);
67 void
68 cleanup_screen(awesome_config *awesomeconf)
70 int i;
71 Key *k, *kn;
72 Rule *r, *rn;
74 XftFontClose(awesomeconf->display, awesomeconf->font);
75 XUngrabKey(awesomeconf->display, AnyKey, AnyModifier, RootWindow(awesomeconf->display, awesomeconf->phys_screen));
76 XDestroyWindow(awesomeconf->display, awesomeconf->statusbar.window);
77 XFreeCursor(awesomeconf->display, awesomeconf->cursor[CurNormal]);
78 XFreeCursor(awesomeconf->display, awesomeconf->cursor[CurResize]);
79 XFreeCursor(awesomeconf->display, awesomeconf->cursor[CurMove]);
81 for(i = 0; i < awesomeconf->ntags; i++)
82 p_delete(&awesomeconf->tags[i].name);
83 for(k = awesomeconf->keys; k; k = kn)
85 kn = k->next;
86 p_delete(&k);
89 cleanup_buttons(awesomeconf->buttons.tag);
90 cleanup_buttons(awesomeconf->buttons.title);
91 cleanup_buttons(awesomeconf->buttons.layout);
92 cleanup_buttons(awesomeconf->buttons.root);
93 cleanup_buttons(awesomeconf->buttons.client);
95 for(i = 0; i < awesomeconf->nlayouts; i++)
96 p_delete(&awesomeconf->layouts[i].symbol);
97 for(r = awesomeconf->rules; r; r = rn)
99 rn = r->next;
100 p_delete(&r->prop);
101 p_delete(&r->tags);
102 p_delete(&r);
105 p_delete(&awesomeconf->tags);
106 p_delete(&awesomeconf->layouts);
107 p_delete(&awesomeconf->configpath);
110 /** Cleanup everything on quit
111 * \param awesomeconf awesome config
113 static void
114 cleanup(awesome_config *awesomeconf)
116 int screen;
118 while(*awesomeconf->clients)
120 client_unban(*awesomeconf->clients);
121 client_unmanage(*awesomeconf->clients, NormalState, awesomeconf);
124 for(screen = 0; screen < get_screen_count(awesomeconf->display); screen++)
125 cleanup_screen(&awesomeconf[screen]);
127 XSetInputFocus(awesomeconf->display, PointerRoot, RevertToPointerRoot, CurrentTime);
128 XSync(awesomeconf->display, False);
130 p_delete(&awesomeconf->clients);
131 p_delete(&awesomeconf);
134 /** Scan X to find windows to manage
135 * \param screen Screen number
136 * \param awesomeconf awesome config
138 static void
139 scan(awesome_config *awesomeconf)
141 unsigned int i, num;
142 int screen, real_screen;
143 Window *wins = NULL, d1, d2;
144 XWindowAttributes wa;
146 for(screen = 0; screen < ScreenCount(awesomeconf->display); screen++)
148 if(XQueryTree(awesomeconf->display, RootWindow(awesomeconf->display, screen), &d1, &d2, &wins, &num))
150 real_screen = screen;
151 for(i = 0; i < num; i++)
153 if(!XGetWindowAttributes(awesomeconf->display, wins[i], &wa)
154 || wa.override_redirect
155 || XGetTransientForHint(awesomeconf->display, wins[i], &d1))
156 continue;
157 if(wa.map_state == IsViewable || window_getstate(awesomeconf->display, wins[i]) == IconicState)
159 if(screen == 0)
160 real_screen = get_screen_bycoord(awesomeconf->display, wa.x, wa.y);
161 client_manage(wins[i], &wa, &awesomeconf[real_screen]);
164 /* now the transients */
165 for(i = 0; i < num; i++)
167 if(!XGetWindowAttributes(awesomeconf->display, wins[i], &wa))
168 continue;
169 if(XGetTransientForHint(awesomeconf->display, wins[i], &d1)
170 && (wa.map_state == IsViewable || window_getstate(awesomeconf->display, wins[i]) == IconicState))
172 if(screen == 0)
173 real_screen = get_screen_bycoord(awesomeconf->display, wa.x, wa.y);
174 client_manage(wins[i], &wa, &awesomeconf[real_screen]);
178 if(wins)
179 XFree(wins);
183 /** Setup everything before running
184 * \param awesomeconf awesome config ref
185 * \todo clean things...
187 static void
188 setup(awesome_config *awesomeconf)
190 XSetWindowAttributes wa;
192 /* init cursors */
193 awesomeconf->cursor[CurNormal] = XCreateFontCursor(awesomeconf->display, XC_left_ptr);
194 awesomeconf->cursor[CurResize] = XCreateFontCursor(awesomeconf->display, XC_sizing);
195 awesomeconf->cursor[CurMove] = XCreateFontCursor(awesomeconf->display, XC_fleur);
197 /* select for events */
198 wa.event_mask = SubstructureRedirectMask | SubstructureNotifyMask
199 | EnterWindowMask | LeaveWindowMask | StructureNotifyMask;
200 wa.cursor = awesomeconf->cursor[CurNormal];
202 XChangeWindowAttributes(awesomeconf->display, RootWindow(awesomeconf->display, awesomeconf->phys_screen), CWEventMask | CWCursor, &wa);
204 XSelectInput(awesomeconf->display, RootWindow(awesomeconf->display, awesomeconf->phys_screen), wa.event_mask);
206 grabkeys(awesomeconf);
209 void
210 setup_screen(awesome_config *awesomeconf, const char *confpath)
212 parse_config(confpath, awesomeconf);
213 setup(awesomeconf);
214 initstatusbar(awesomeconf->display, awesomeconf->screen, &awesomeconf->statusbar,
215 awesomeconf->cursor[CurNormal], awesomeconf->font,
216 awesomeconf->layouts, awesomeconf->nlayouts);
219 /** Startup Error handler to check if another window manager
220 * is already running.
221 * \param disp Display ref
222 * \param ee Error event
224 static int __attribute__ ((noreturn))
225 xerrorstart(Display * disp __attribute__ ((unused)), XErrorEvent * ee __attribute__ ((unused)))
227 eprint("awesome: another window manager is already running\n");
230 /** Quit awesome
231 * \param awesomeconf awesome config
232 * \param arg nothing
233 * \ingroup ui_callback
235 void
236 uicb_quit(awesome_config *awesomeconf __attribute__((unused)),
237 const char *arg __attribute__ ((unused)))
239 running = False;
242 /* There's no way to check accesses to destroyed windows, thus those cases are
243 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
244 * default error handler, which may call exit.
247 xerror(Display * edpy, XErrorEvent * ee)
249 if(ee->error_code == BadWindow
250 || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
251 || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
252 || (ee->request_code == X_PolyFillRectangle
253 && ee->error_code == BadDrawable)
254 || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
255 || (ee->request_code == X_ConfigureWindow
256 && ee->error_code == BadMatch) || (ee->request_code == X_GrabKey
257 && ee->error_code == BadAccess)
258 || (ee->request_code == X_CopyArea && ee->error_code == BadDrawable))
259 return 0;
260 fprintf(stderr, "awesome: fatal error: request code=%d, error code=%d\n",
261 ee->request_code, ee->error_code);
263 return xerrorxlib(edpy, ee); /* may call exit */
266 /** Hello, this is main
267 * \param argc who knows
268 * \param argv who knows
269 * \return EXIT_SUCCESS I hope
271 typedef void event_handler (XEvent *, awesome_config *);
273 main(int argc, char *argv[])
275 char buf[1024];
276 const char *confpath = NULL;
277 int r, xfd, e_dummy, csfd;
278 fd_set rd;
279 XEvent ev;
280 Display * dpy;
281 awesome_config *awesomeconf;
282 int shape_event, randr_event_base;
283 int screen;
284 enum { NetSupported, NetWMName, NetLast }; /* EWMH atoms */
285 Atom netatom[NetLast];
286 event_handler **handler;
287 Client **clients;
288 struct sockaddr_un *addr;
290 if(argc >= 2)
292 if(!a_strcmp("-v", argv[1]))
294 printf("awesome-" VERSION " © 2007 Julien Danjou\n");
295 return EXIT_SUCCESS;
297 else if(!a_strcmp("-c", argv[1]))
299 if(a_strlen(argv[2]))
300 confpath = argv[2];
301 else
302 eprint("awesome: -c require a file\n");
304 else
305 eprint("usage: awesome [-v | -c configfile]\n");
308 /* Tag won't be printed otherwised */
309 setlocale(LC_CTYPE, "");
311 if(!(dpy = XOpenDisplay(NULL)))
312 eprint("awesome: cannot open display\n");
314 xfd = ConnectionNumber(dpy);
316 XSetErrorHandler(xerrorstart);
317 for(screen = 0; screen < ScreenCount(dpy); screen++)
318 /* this causes an error if some other window manager is running */
319 XSelectInput(dpy, RootWindow(dpy, screen), SubstructureRedirectMask);
321 /* need to XSync to validate errorhandler */
322 XSync(dpy, False);
323 XSetErrorHandler(NULL);
324 xerrorxlib = XSetErrorHandler(xerror);
325 XSync(dpy, False);
327 /* allocate stuff */
328 awesomeconf = p_new(awesome_config, get_screen_count(dpy));
329 clients = p_new(Client *, 1);
331 for(screen = 0; screen < get_screen_count(dpy); screen++)
333 /* store display */
334 awesomeconf[screen].display = dpy;
336 /* set screen */
337 awesomeconf[screen].screen = screen;
338 setup_screen(&awesomeconf[screen], confpath);
339 awesomeconf[screen].clients = clients;
340 drawstatusbar(&awesomeconf[screen]);
343 netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
344 netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
346 /* do this only for real screen */
347 for(screen = 0; screen < ScreenCount(dpy); screen++)
349 loadawesomeprops(&awesomeconf[screen]);
350 XChangeProperty(dpy, RootWindow(dpy, screen), netatom[NetSupported],
351 XA_ATOM, 32, PropModeReplace, (unsigned char *) netatom, NetLast);
354 handler = p_new(event_handler *, LASTEvent);
355 handler[ButtonPress] = handle_event_buttonpress;
356 handler[ConfigureRequest] = handle_event_configurerequest;
357 handler[ConfigureNotify] = handle_event_configurenotify;
358 handler[DestroyNotify] = handle_event_destroynotify;
359 handler[EnterNotify] = handle_event_enternotify;
360 handler[LeaveNotify] = handle_event_leavenotify;
361 handler[Expose] = handle_event_expose;
362 handler[KeyPress] = handle_event_keypress;
363 handler[MappingNotify] = handle_event_mappingnotify;
364 handler[MapRequest] = handle_event_maprequest;
365 handler[PropertyNotify] = handle_event_propertynotify;
366 handler[UnmapNotify] = handle_event_unmapnotify;
368 /* check for shape extension */
369 if((awesomeconf[0].have_shape = XShapeQueryExtension(dpy, &shape_event, &e_dummy)))
371 p_realloc(&handler, shape_event + 1);
372 handler[shape_event] = handle_event_shape;
375 /* check for randr extension */
376 if((awesomeconf[0].have_randr = XRRQueryExtension(dpy, &randr_event_base, &e_dummy)))
378 p_realloc(&handler, randr_event_base + RRScreenChangeNotify + 1);
379 handler[randr_event_base + RRScreenChangeNotify] = handle_event_randr_screen_change_notify;
382 for(screen = 0; screen < get_screen_count(dpy); screen++)
384 awesomeconf[screen].have_shape = awesomeconf[0].have_shape;
385 awesomeconf[screen].have_randr = awesomeconf[0].have_randr;
388 scan(awesomeconf);
390 XSync(dpy, False);
392 /* get socket fd */
393 csfd = get_client_socket();
394 addr = get_client_addr(getenv("DISPLAY"));
396 if(bind(csfd, (const struct sockaddr *) addr, SUN_LEN(addr)))
398 if(errno == EADDRINUSE)
400 if(unlink(addr->sun_path))
401 perror("error unlinking existing file");
402 if(bind(csfd, (const struct sockaddr *) addr, SUN_LEN(addr)))
403 perror("error binding UNIX domain socket");
405 else
406 perror("error binding UNIX domain socket");
409 /* main event loop, also reads status text from socket */
410 while(running)
412 FD_ZERO(&rd);
413 if(csfd >= 0)
414 FD_SET(csfd, &rd);
415 FD_SET(xfd, &rd);
416 if(select(MAX(xfd, csfd) + 1, &rd, NULL, NULL, NULL) == -1)
418 if(errno == EINTR)
419 continue;
420 eprint("select failed\n");
422 if(csfd >= 0 && FD_ISSET(csfd, &rd))
423 switch (r = recv(csfd, buf, sizeof(buf)-1, MSG_TRUNC))
425 case -1:
426 perror("awesome: error reading UNIX domain socket");
427 a_strncpy(awesomeconf[0].statustext, sizeof(awesomeconf[0].statustext),
428 strerror(errno), sizeof(awesomeconf[0].statustext) - 1);
429 awesomeconf[0].statustext[sizeof(awesomeconf[0].statustext) - 1] = '\0';
430 csfd = -1;
431 break;
432 case 0:
433 break;
434 default:
435 if(r >= ssizeof(buf))
436 break;
437 buf[r] = '\0';
438 parse_control(buf, awesomeconf);
441 while(XPending(dpy))
443 XNextEvent(dpy, &ev);
444 if(handler[ev.type])
445 handler[ev.type](&ev, awesomeconf); /* call handler */
449 if(csfd > 0 && close(csfd))
450 perror("error closing UNIX domain socket");
451 if(unlink(addr->sun_path))
452 perror("error unlinking UNIX domain socket");
453 p_delete(&addr);
455 cleanup(awesomeconf);
456 XCloseDisplay(dpy);
458 return EXIT_SUCCESS;
460 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99