Merge branch 'master' of ssh://git.naquadah.org/var/cache/git/awesome
[awesome.git] / awesome.c
blob8c30c0876ca2bd049ae721e0a85cf06e3eb2e6d2
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 enum { NetSupported, NetWMName, NetLast }; /* EWMH atoms */
49 Atom netatom[NetWMName];
51 /* static */
53 static int (*xerrorxlib) (Display *, XErrorEvent *);
54 static Bool readin = True, running = True;
56 static void
57 cleanup(Display *disp, DC *drawcontext, awesome_config *awesomeconf)
59 int screen;
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]);
84 p_delete(&awesomeconf[screen].tags);
85 p_delete(&awesomeconf[screen].selected_tags);
86 p_delete(&awesomeconf[screen].prev_selected_tags);
87 p_delete(&awesomeconf[screen].tag_layouts);
88 p_delete(&awesomeconf[screen].layouts);
89 p_delete(&awesomeconf[screen].rules);
90 p_delete(&awesomeconf[screen].keys);
92 XSetInputFocus(disp, PointerRoot, RevertToPointerRoot, CurrentTime);
93 XSync(disp, False);
94 p_delete(&awesomeconf);
95 p_delete(&dc);
98 static long
99 getstate(Display *disp, Window w)
101 int format, status;
102 long result = -1;
103 unsigned char *p = NULL;
104 unsigned long n, extra;
105 Atom real;
106 status = XGetWindowProperty(disp, w, XInternAtom(disp, "WM_STATE", False),
107 0L, 2L, False, XInternAtom(disp, "WM_STATE", False),
108 &real, &format, &n, &extra, (unsigned char **) &p);
109 if(status != Success)
110 return -1;
111 if(n != 0)
112 result = *p;
113 XFree(p);
114 return result;
117 static void
118 scan(Display *disp, int screen, DC *drawcontext, awesome_config *awesomeconf)
120 unsigned int i, num;
121 Window *wins, d1, d2;
122 XWindowAttributes wa;
124 wins = NULL;
125 if(XQueryTree(disp, RootWindow(disp, screen), &d1, &d2, &wins, &num))
127 for(i = 0; i < num; i++)
129 if(!XGetWindowAttributes(disp, wins[i], &wa)
130 || wa.override_redirect
131 || XGetTransientForHint(disp, wins[i], &d1))
132 continue;
133 if(wa.map_state == IsViewable || getstate(disp, wins[i]) == IconicState)
134 manage(disp, screen, drawcontext, wins[i], &wa, awesomeconf);
136 /* now the transients */
137 for(i = 0; i < num; i++)
139 if(!XGetWindowAttributes(disp, wins[i], &wa))
140 continue;
141 if(XGetTransientForHint(disp, wins[i], &d1)
142 && (wa.map_state == IsViewable || getstate(disp, wins[i]) == IconicState))
143 manage(disp, screen, drawcontext, wins[i], &wa, awesomeconf);
146 if(wins)
147 XFree(wins);
150 /** Setup everything before running
151 * \param disp Display ref
152 * \param screen Screen number
153 * \param awesomeconf awesome config ref
154 * \todo clean things...
156 static void
157 setup(Display *disp, int screen, DC *drawcontext, awesome_config *awesomeconf)
159 XSetWindowAttributes wa;
161 /* init cursors */
162 drawcontext->cursor[CurNormal] = XCreateFontCursor(disp, XC_left_ptr);
163 drawcontext->cursor[CurResize] = XCreateFontCursor(disp, XC_sizing);
164 drawcontext->cursor[CurMove] = XCreateFontCursor(disp, XC_fleur);
165 /* select for events */
166 wa.event_mask = SubstructureRedirectMask | SubstructureNotifyMask
167 | EnterWindowMask | LeaveWindowMask | StructureNotifyMask;
168 wa.cursor = drawcontext->cursor[CurNormal];
169 XChangeWindowAttributes(disp, RootWindow(disp, screen), CWEventMask | CWCursor, &wa);
170 XSelectInput(disp, RootWindow(disp, screen), wa.event_mask);
171 grabkeys(disp, screen, awesomeconf);
172 compileregs(awesomeconf->rules, awesomeconf->nrules);
173 /* bar */
174 drawcontext->h = awesomeconf->statusbar.height = drawcontext->font.height + 2;
175 initstatusbar(disp, screen, drawcontext, &awesomeconf->statusbar);
176 drawcontext->gc = XCreateGC(disp, RootWindow(disp, screen), 0, 0);
177 XSetLineAttributes(disp, drawcontext->gc, 1, LineSolid, CapButt, JoinMiter);
178 if(!drawcontext->font.set)
179 XSetFont(disp, drawcontext->gc, drawcontext->font.xfont->fid);
180 loadawesomeprops(disp, screen, awesomeconf);
184 * Startup Error handler to check if another window manager
185 * is already running.
187 static int __attribute__ ((noreturn))
188 xerrorstart(Display * dsply __attribute__ ((unused)), XErrorEvent * ee __attribute__ ((unused)))
190 eprint("awesome: another window manager is already running\n");
193 /* extern */
195 void
196 uicb_quit(Display *disp __attribute__ ((unused)),
197 int screen __attribute__ ((unused)),
198 DC *drawcontext __attribute__ ((unused)),
199 awesome_config *awesomeconf __attribute__((unused)),
200 const char *arg __attribute__ ((unused)))
202 readin = running = False;
206 get_windows_area_x(Statusbar statusbar __attribute__ ((unused)))
208 return 0;
212 get_windows_area_y(Statusbar statusbar)
214 if(statusbar.position == BarTop)
215 return statusbar.height;
217 return 0;
221 get_windows_area_height(Display *disp, Statusbar statusbar)
223 if(statusbar.position == BarTop || statusbar.position == BarBot)
224 return DisplayHeight(disp, DefaultScreen(disp)) - statusbar.height;
226 return DisplayHeight(disp, DefaultScreen(disp));
230 get_windows_area_width(Display *disp,
231 Statusbar statusbar __attribute__ ((unused)))
233 return DisplayWidth(disp, DefaultScreen(disp));
236 /* There's no way to check accesses to destroyed windows, thus those cases are
237 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
238 * default error handler, which may call exit.
241 xerror(Display * edpy, XErrorEvent * ee)
243 if(ee->error_code == BadWindow
244 || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
245 || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
246 || (ee->request_code == X_PolyFillRectangle
247 && ee->error_code == BadDrawable)
248 || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
249 || (ee->request_code == X_ConfigureWindow
250 && ee->error_code == BadMatch) || (ee->request_code == X_GrabKey
251 && ee->error_code == BadAccess)
252 || (ee->request_code == X_CopyArea && ee->error_code == BadDrawable))
253 return 0;
254 fprintf(stderr, "awesome: fatal error: request code=%d, error code=%d\n",
255 ee->request_code, ee->error_code);
257 return xerrorxlib(edpy, ee); /* may call exit */
261 main(int argc, char *argv[])
263 char *p;
264 int r, xfd, e_dummy;
265 fd_set rd;
266 XEvent ev;
267 Display * dpy;
268 awesome_config *awesomeconf;
269 int shape_event, randr_event_base;
270 int screen;
272 if(argc == 2 && !strcmp("-v", argv[1]))
273 eprint("awesome-" VERSION " © 2007 Julien Danjou\n");
274 else if(argc != 1)
275 eprint("usage: awesome [-v]\n");
276 setlocale(LC_CTYPE, "");
278 if(!(dpy = XOpenDisplay(NULL)))
279 eprint("awesome: cannot open display\n");
280 xfd = ConnectionNumber(dpy);
281 XSetErrorHandler(xerrorstart);
283 XSync(dpy, False);
284 XSetErrorHandler(NULL);
285 xerrorxlib = XSetErrorHandler(xerror);
286 XSync(dpy, False);
288 netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
289 netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
291 dc = p_new(DC, ScreenCount(dpy));
292 awesomeconf = p_new(awesome_config, ScreenCount(dpy));
294 for(screen = 0; screen < ScreenCount(dpy); screen++)
295 /* this causes an error if some other window manager is running */
296 XSelectInput(dpy, RootWindow(dpy, screen), SubstructureRedirectMask);
297 for(screen = 0; screen < ScreenCount(dpy); screen++)
299 parse_config(dpy, screen, &dc[screen], &awesomeconf[screen]);
300 setup(dpy, screen, &dc[screen], &awesomeconf[screen]);
301 XChangeProperty(dpy, RootWindow(dpy, screen), netatom[NetSupported],
302 XA_ATOM, 32, PropModeReplace, (unsigned char *) netatom, NetLast);
303 drawstatusbar(dpy, screen, &dc[screen], &awesomeconf[screen]);
306 void (*handler[LASTEvent]) (XEvent *, awesome_config *) =
308 [ButtonPress] = handle_event_buttonpress,
309 [ConfigureRequest] = handle_event_configurerequest,
310 [ConfigureNotify] = handle_event_configurenotify,
311 [DestroyNotify] = handle_event_destroynotify,
312 [EnterNotify] = handle_event_enternotify,
313 [LeaveNotify] = handle_event_leavenotify,
314 [Expose] = handle_event_expose,
315 [KeyPress] = handle_event_keypress,
316 [MappingNotify] = handle_event_mappingnotify,
317 [MapRequest] = handle_event_maprequest,
318 [PropertyNotify] = handle_event_propertynotify,
319 [UnmapNotify] = handle_event_unmapnotify,
322 /* check for shape extension */
323 if((awesomeconf[0].have_shape = XShapeQueryExtension(dpy, &shape_event, &e_dummy)))
324 handler[shape_event] = handle_event_shape;
326 /* check for randr extension */
327 if((awesomeconf[0].have_randr = XRRQueryExtension(dpy, &randr_event_base, &e_dummy)))
328 handler[randr_event_base + RRScreenChangeNotify] = handle_event_randr_screen_change_notify;
330 for(screen = 0; screen < ScreenCount(dpy); screen++)
332 awesomeconf[screen].have_shape = awesomeconf[0].have_shape;
333 awesomeconf[screen].have_randr = awesomeconf[0].have_randr;
334 scan(dpy, screen, &dc[screen], &awesomeconf[screen]);
337 XSync(dpy, False);
339 /* main event loop, also reads status text from stdin */
340 while(running)
342 FD_ZERO(&rd);
343 if(readin)
344 FD_SET(STDIN_FILENO, &rd);
345 FD_SET(xfd, &rd);
346 if(select(xfd + 1, &rd, NULL, NULL, NULL) == -1)
348 if(errno == EINTR)
349 continue;
350 eprint("select failed\n");
352 if(FD_ISSET(STDIN_FILENO, &rd))
354 switch (r = read(STDIN_FILENO, awesomeconf[0].statustext, sizeof(awesomeconf[0].statustext) - 1))
356 case -1:
357 strncpy(awesomeconf[0].statustext, strerror(errno), sizeof(awesomeconf[0].statustext) - 1);
358 awesomeconf[0].statustext[sizeof(awesomeconf[0].statustext) - 1] = '\0';
359 readin = False;
360 break;
361 case 0:
362 strncpy(awesomeconf[0].statustext, "EOF", 4);
363 readin = False;
364 break;
365 default:
366 for(awesomeconf[0].statustext[r] = '\0', p = awesomeconf[0].statustext + a_strlen(awesomeconf[0].statustext) - 1;
367 p >= awesomeconf[0].statustext && *p == '\n'; *p-- = '\0');
368 for(; p >= awesomeconf[0].statustext && *p != '\n'; --p);
369 if(p > awesomeconf[0].statustext)
370 strncpy(awesomeconf[0].statustext, p + 1, sizeof(awesomeconf[0].statustext));
372 drawstatusbar(dpy, 0, &dc[0], &awesomeconf[0]);
375 while(XPending(dpy))
377 XNextEvent(dpy, &ev);
378 if(handler[ev.type])
379 handler[ev.type](&ev, awesomeconf); /* call handler */
382 cleanup(dpy, dc, awesomeconf);
383 XCloseDisplay(dpy);
385 return 0;