bug fix: rewrite handler allocation dynamicly to correctly add shape and xrandr events
[awesome.git] / awesome.c
blobed9d647b58b8fae8d3cd74baaf8f8ab68190aa78
1 /*
2 * awesome.c - awesome main functions
3 *
4 * Copyright © 2007 Julien Danjou <julien@danjou.info>
5 *
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.
20 */
22 #include <errno.h>
23 #include <locale.h>
24 #include <stdio.h>
25 #include <unistd.h>
26 #include <sys/select.h>
27 #include <X11/cursorfont.h>
28 #include <X11/keysym.h>
29 #include <X11/Xatom.h>
30 #include <X11/Xproto.h>
31 #include <X11/Xutil.h>
32 #include <X11/extensions/shape.h>
33 #include <X11/extensions/Xrandr.h>
35 #include "awesome.h"
36 #include "event.h"
37 #include "layout.h"
38 #include "tag.h"
39 #include "screen.h"
40 #include "util.h"
41 #include "statusbar.h"
43 Client *clients = NULL;
44 Client *sel = NULL;
45 Client *stack = NULL;
46 DC *dc;
48 static int (*xerrorxlib) (Display *, XErrorEvent *);
49 static Bool readin = True, running = True;
51 /** Cleanup everything on quit
52 * \param disp Display ref
53 * \param drawcontext Drawcontext ref
54 * \param awesomeconf awesome config
56 static void
57 cleanup(Display *disp, DC *drawcontext, awesome_config *awesomeconf)
59 int screen, i;
61 close(STDIN_FILENO);
63 while(stack)
65 unban(stack);
66 unmanage(stack, drawcontext, NormalState, awesomeconf);
69 for(screen = 0; screen < ScreenCount(disp); screen++)
71 if(drawcontext[screen].font.set)
72 XFreeFontSet(disp, drawcontext[screen].font.set);
73 else
74 XFreeFont(disp, drawcontext[screen].font.xfont);
76 XUngrabKey(disp, AnyKey, AnyModifier, RootWindow(disp, screen));
78 XFreePixmap(disp, awesomeconf[screen].statusbar.drawable);
79 XFreeGC(disp, drawcontext[screen].gc);
80 XDestroyWindow(disp, awesomeconf[screen].statusbar.window);
81 XFreeCursor(disp, drawcontext[screen].cursor[CurNormal]);
82 XFreeCursor(disp, drawcontext[screen].cursor[CurResize]);
83 XFreeCursor(disp, drawcontext[screen].cursor[CurMove]);
85 for(i = 0; i < awesomeconf[screen].ntags; i++)
86 p_delete(&awesomeconf[screen].tags[i]);
87 for(i = 0; i < awesomeconf[screen].nkeys; i++)
88 p_delete(&awesomeconf[screen].keys[i].arg);
89 for(i = 0; i < awesomeconf[screen].nlayouts; i++)
90 p_delete(&awesomeconf[screen].layouts[i].symbol);
91 for(i = 0; i < awesomeconf[screen].nrules; i++)
93 p_delete(&awesomeconf[screen].rules[i].prop);
94 p_delete(&awesomeconf[screen].rules[i].tags);
96 p_delete(&awesomeconf[screen].tags);
97 p_delete(&awesomeconf[screen].selected_tags);
98 p_delete(&awesomeconf[screen].prev_selected_tags);
99 p_delete(&awesomeconf[screen].tag_layouts);
100 p_delete(&awesomeconf[screen].layouts);
101 p_delete(&awesomeconf[screen].rules);
102 p_delete(&awesomeconf[screen].keys);
104 XSetInputFocus(disp, PointerRoot, RevertToPointerRoot, CurrentTime);
105 XSync(disp, False);
106 p_delete(&awesomeconf);
107 p_delete(&dc);
110 /** Get a window state (WM_STATE)
111 * \param disp Display ref
112 * \param w Client window
113 * \return state
115 static long
116 getstate(Display *disp, Window w)
118 int format, status;
119 long result = -1;
120 unsigned char *p = NULL;
121 unsigned long n, extra;
122 Atom real;
123 status = XGetWindowProperty(disp, w, XInternAtom(disp, "WM_STATE", False),
124 0L, 2L, False, XInternAtom(disp, "WM_STATE", False),
125 &real, &format, &n, &extra, (unsigned char **) &p);
126 if(status != Success)
127 return -1;
128 if(n != 0)
129 result = *p;
130 XFree(p);
131 return result;
134 /** Scan X to find windows to manage
135 * \param disp Display ref
136 * \param screen Screen number
137 * \param drawcontext Drawcontext ref
138 * \param awesomeconf awesome config
140 static void
141 scan(Display *disp, int screen, DC *drawcontext, awesome_config *awesomeconf)
143 unsigned int i, num;
144 Window *wins, d1, d2;
145 XWindowAttributes wa;
147 wins = NULL;
148 if(XQueryTree(disp, RootWindow(disp, screen), &d1, &d2, &wins, &num))
150 for(i = 0; i < num; i++)
152 if(!XGetWindowAttributes(disp, wins[i], &wa)
153 || wa.override_redirect
154 || XGetTransientForHint(disp, wins[i], &d1))
155 continue;
156 if(wa.map_state == IsViewable || getstate(disp, wins[i]) == IconicState)
157 manage(disp, drawcontext, wins[i], &wa, awesomeconf);
159 /* now the transients */
160 for(i = 0; i < num; i++)
162 if(!XGetWindowAttributes(disp, wins[i], &wa))
163 continue;
164 if(XGetTransientForHint(disp, wins[i], &d1)
165 && (wa.map_state == IsViewable || getstate(disp, wins[i]) == IconicState))
166 manage(disp, drawcontext, wins[i], &wa, awesomeconf);
169 if(wins)
170 XFree(wins);
173 /** Setup everything before running
174 * \param disp Display ref
175 * \param screen Screen number
176 * \param awesomeconf awesome config ref
177 * \todo clean things...
179 static void
180 setup(Display *disp, int screen, DC *drawcontext, awesome_config *awesomeconf)
182 XSetWindowAttributes wa;
184 /* init cursors */
185 drawcontext->cursor[CurNormal] = XCreateFontCursor(disp, XC_left_ptr);
186 drawcontext->cursor[CurResize] = XCreateFontCursor(disp, XC_sizing);
187 drawcontext->cursor[CurMove] = XCreateFontCursor(disp, XC_fleur);
189 /* select for events */
190 wa.event_mask = SubstructureRedirectMask | SubstructureNotifyMask
191 | EnterWindowMask | LeaveWindowMask | StructureNotifyMask;
192 wa.cursor = drawcontext->cursor[CurNormal];
194 XChangeWindowAttributes(disp, RootWindow(disp, screen), CWEventMask | CWCursor, &wa);
196 XSelectInput(disp, RootWindow(disp, screen), wa.event_mask);
198 grabkeys(disp, screen, awesomeconf);
200 compileregs(awesomeconf->rules, awesomeconf->nrules);
202 /* bar */
203 drawcontext->h = awesomeconf->statusbar.height = drawcontext->font.height + 2;
204 initstatusbar(disp, screen, drawcontext, &awesomeconf->statusbar);
205 drawcontext->gc = XCreateGC(disp, RootWindow(disp, screen), 0, 0);
206 XSetLineAttributes(disp, drawcontext->gc, 1, LineSolid, CapButt, JoinMiter);
208 if(!drawcontext->font.set)
209 XSetFont(disp, drawcontext->gc, drawcontext->font.xfont->fid);
211 loadawesomeprops(disp, awesomeconf);
214 /** Startup Error handler to check if another window manager
215 * is already running.
216 * \param disp Display ref
217 * \param ee Error event
219 static int __attribute__ ((noreturn))
220 xerrorstart(Display * disp __attribute__ ((unused)), XErrorEvent * ee __attribute__ ((unused)))
222 eprint("awesome: another window manager is already running\n");
225 /** Quit awesome
226 * \param disp Display ref
227 * \param screen Screen number
228 * \param drawcontext Drawcontext ref
229 * \param awesomeconf awesome config
230 * \param arg nothing
231 * \ingroup ui_callback
233 void
234 uicb_quit(Display *disp __attribute__ ((unused)),
235 DC *drawcontext __attribute__ ((unused)),
236 awesome_config *awesomeconf __attribute__((unused)),
237 const char *arg __attribute__ ((unused)))
239 readin = 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 *p;
276 int r, xfd, e_dummy;
277 fd_set rd;
278 XEvent ev;
279 Display * dpy;
280 awesome_config *awesomeconf;
281 int shape_event, randr_event_base;
282 int screen;
283 enum { NetSupported, NetWMName, NetLast }; /* EWMH atoms */
284 Atom netatom[NetLast];
285 event_handler **handler;
287 if(argc == 2 && !strcmp("-v", argv[1]))
289 printf("awesome-" VERSION " © 2007 Julien Danjou\n");
290 return EXIT_SUCCESS;
292 else if(argc != 1)
293 eprint("usage: awesome [-v]\n");
295 /* Tag won't be printed otherwised */
296 setlocale(LC_CTYPE, "");
298 if(!(dpy = XOpenDisplay(NULL)))
299 eprint("awesome: cannot open display\n");
301 xfd = ConnectionNumber(dpy);
303 XSetErrorHandler(xerrorstart);
304 for(screen = 0; screen < ScreenCount(dpy); screen++)
305 /* this causes an error if some other window manager is running */
306 XSelectInput(dpy, RootWindow(dpy, screen), SubstructureRedirectMask);
308 /* need to XSync to validate errorhandler */
309 XSync(dpy, False);
310 XSetErrorHandler(NULL);
311 xerrorxlib = XSetErrorHandler(xerror);
312 XSync(dpy, False);
314 netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
315 netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
317 /* allocate stuff */
318 dc = p_new(DC, ScreenCount(dpy));
319 awesomeconf = p_new(awesome_config, ScreenCount(dpy));
321 for(screen = 0; screen < ScreenCount(dpy); screen++)
323 parse_config(dpy, screen, &dc[screen], &awesomeconf[screen]);
324 setup(dpy, screen, &dc[screen], &awesomeconf[screen]);
325 XChangeProperty(dpy, RootWindow(dpy, screen), netatom[NetSupported],
326 XA_ATOM, 32, PropModeReplace, (unsigned char *) netatom, NetLast);
327 drawstatusbar(dpy, screen, &dc[screen], &awesomeconf[screen]);
330 handler = p_new(event_handler *, LASTEvent);
331 handler[ButtonPress] = handle_event_buttonpress;
332 handler[ConfigureRequest] = handle_event_configurerequest;
333 handler[ConfigureNotify] = handle_event_configurenotify;
334 handler[DestroyNotify] = handle_event_destroynotify;
335 handler[EnterNotify] = handle_event_enternotify;
336 handler[LeaveNotify] = handle_event_leavenotify;
337 handler[Expose] = handle_event_expose;
338 handler[KeyPress] = handle_event_keypress;
339 handler[MappingNotify] = handle_event_mappingnotify;
340 handler[MapRequest] = handle_event_maprequest;
341 handler[PropertyNotify] = handle_event_propertynotify;
342 handler[UnmapNotify] = handle_event_unmapnotify;
344 /* check for shape extension */
345 if((awesomeconf[0].have_shape = XShapeQueryExtension(dpy, &shape_event, &e_dummy)))
347 p_realloc(&handler, shape_event + 1);
348 handler[shape_event] = handle_event_shape;
351 /* check for randr extension */
352 if((awesomeconf[0].have_randr = XRRQueryExtension(dpy, &randr_event_base, &e_dummy)))
354 p_realloc(&handler, randr_event_base + RRScreenChangeNotify + 1);
355 handler[randr_event_base + RRScreenChangeNotify] = handle_event_randr_screen_change_notify;
358 for(screen = 0; screen < ScreenCount(dpy); screen++)
360 awesomeconf[screen].have_shape = awesomeconf[0].have_shape;
361 awesomeconf[screen].have_randr = awesomeconf[0].have_randr;
362 scan(dpy, screen, &dc[screen], &awesomeconf[screen]);
365 XSync(dpy, False);
367 /* main event loop, also reads status text from stdin */
368 while(running)
370 FD_ZERO(&rd);
371 if(readin)
372 FD_SET(STDIN_FILENO, &rd);
373 FD_SET(xfd, &rd);
374 if(select(xfd + 1, &rd, NULL, NULL, NULL) == -1)
376 if(errno == EINTR)
377 continue;
378 eprint("select failed\n");
380 if(FD_ISSET(STDIN_FILENO, &rd))
382 switch (r = read(STDIN_FILENO, awesomeconf[0].statustext, sizeof(awesomeconf[0].statustext) - 1))
384 case -1:
385 strncpy(awesomeconf[0].statustext, strerror(errno), sizeof(awesomeconf[0].statustext) - 1);
386 awesomeconf[0].statustext[sizeof(awesomeconf[0].statustext) - 1] = '\0';
387 readin = False;
388 break;
389 case 0:
390 strncpy(awesomeconf[0].statustext, "EOF", 4);
391 readin = False;
392 break;
393 default:
394 for(awesomeconf[0].statustext[r] = '\0', p = awesomeconf[0].statustext + a_strlen(awesomeconf[0].statustext) - 1;
395 p >= awesomeconf[0].statustext && *p == '\n'; *p-- = '\0');
396 for(; p >= awesomeconf[0].statustext && *p != '\n'; --p);
397 if(p > awesomeconf[0].statustext)
398 strncpy(awesomeconf[0].statustext, p + 1, sizeof(awesomeconf[0].statustext));
400 drawstatusbar(dpy, 0, &dc[0], &awesomeconf[0]);
403 while(XPending(dpy))
405 XNextEvent(dpy, &ev);
406 if(handler[ev.type])
407 handler[ev.type](&ev, awesomeconf); /* call handler */
410 cleanup(dpy, dc, awesomeconf);
411 XCloseDisplay(dpy);
413 return EXIT_SUCCESS;