simplify manage(), remove screen arg
[awesome.git] / awesome.c
blobd4d77e05cd8d8bfd1a3e787e92215562a3262edb
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, screen, 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 int screen __attribute__ ((unused)),
236 DC *drawcontext __attribute__ ((unused)),
237 awesome_config *awesomeconf __attribute__((unused)),
238 const char *arg __attribute__ ((unused)))
240 readin = running = False;
244 get_windows_area_x(Statusbar statusbar __attribute__ ((unused)))
246 return 0;
250 get_windows_area_y(Statusbar statusbar)
252 if(statusbar.position == BarTop)
253 return statusbar.height;
255 return 0;
259 get_windows_area_height(Display *disp, Statusbar statusbar)
261 if(statusbar.position == BarTop || statusbar.position == BarBot)
262 return DisplayHeight(disp, DefaultScreen(disp)) - statusbar.height;
264 return DisplayHeight(disp, DefaultScreen(disp));
268 get_windows_area_width(Display *disp,
269 Statusbar statusbar __attribute__ ((unused)))
271 return DisplayWidth(disp, DefaultScreen(disp));
274 /* There's no way to check accesses to destroyed windows, thus those cases are
275 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
276 * default error handler, which may call exit.
279 xerror(Display * edpy, XErrorEvent * ee)
281 if(ee->error_code == BadWindow
282 || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
283 || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
284 || (ee->request_code == X_PolyFillRectangle
285 && ee->error_code == BadDrawable)
286 || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
287 || (ee->request_code == X_ConfigureWindow
288 && ee->error_code == BadMatch) || (ee->request_code == X_GrabKey
289 && ee->error_code == BadAccess)
290 || (ee->request_code == X_CopyArea && ee->error_code == BadDrawable))
291 return 0;
292 fprintf(stderr, "awesome: fatal error: request code=%d, error code=%d\n",
293 ee->request_code, ee->error_code);
295 return xerrorxlib(edpy, ee); /* may call exit */
298 /** Hello, this is main
299 * \param argc who knows
300 * \param argv who knows
301 * \return EXIT_SUCCESS I hope
304 main(int argc, char *argv[])
306 char *p;
307 int r, xfd, e_dummy;
308 fd_set rd;
309 XEvent ev;
310 Display * dpy;
311 awesome_config *awesomeconf;
312 int shape_event, randr_event_base;
313 int screen;
314 enum { NetSupported, NetWMName, NetLast }; /* EWMH atoms */
315 Atom netatom[NetLast];
317 if(argc == 2 && !strcmp("-v", argv[1]))
319 printf("awesome-" VERSION " © 2007 Julien Danjou\n");
320 return EXIT_SUCCESS;
322 else if(argc != 1)
323 eprint("usage: awesome [-v]\n");
325 /* Tag won't be printed otherwised */
326 setlocale(LC_CTYPE, "");
328 if(!(dpy = XOpenDisplay(NULL)))
329 eprint("awesome: cannot open display\n");
331 xfd = ConnectionNumber(dpy);
333 XSetErrorHandler(xerrorstart);
334 for(screen = 0; screen < ScreenCount(dpy); screen++)
335 /* this causes an error if some other window manager is running */
336 XSelectInput(dpy, RootWindow(dpy, screen), SubstructureRedirectMask);
338 /* need to XSync to validate errorhandler */
339 XSync(dpy, False);
340 XSetErrorHandler(NULL);
341 xerrorxlib = XSetErrorHandler(xerror);
342 XSync(dpy, False);
344 netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
345 netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
347 /* allocate stuff */
348 dc = p_new(DC, ScreenCount(dpy));
349 awesomeconf = p_new(awesome_config, ScreenCount(dpy));
351 for(screen = 0; screen < ScreenCount(dpy); screen++)
353 parse_config(dpy, screen, &dc[screen], &awesomeconf[screen]);
354 setup(dpy, screen, &dc[screen], &awesomeconf[screen]);
355 XChangeProperty(dpy, RootWindow(dpy, screen), netatom[NetSupported],
356 XA_ATOM, 32, PropModeReplace, (unsigned char *) netatom, NetLast);
357 drawstatusbar(dpy, screen, &dc[screen], &awesomeconf[screen]);
360 void (*handler[LASTEvent]) (XEvent *, awesome_config *) =
362 [ButtonPress] = handle_event_buttonpress,
363 [ConfigureRequest] = handle_event_configurerequest,
364 [ConfigureNotify] = handle_event_configurenotify,
365 [DestroyNotify] = handle_event_destroynotify,
366 [EnterNotify] = handle_event_enternotify,
367 [LeaveNotify] = handle_event_leavenotify,
368 [Expose] = handle_event_expose,
369 [KeyPress] = handle_event_keypress,
370 [MappingNotify] = handle_event_mappingnotify,
371 [MapRequest] = handle_event_maprequest,
372 [PropertyNotify] = handle_event_propertynotify,
373 [UnmapNotify] = handle_event_unmapnotify,
376 /* check for shape extension */
377 if((awesomeconf[0].have_shape = XShapeQueryExtension(dpy, &shape_event, &e_dummy)))
378 handler[shape_event] = handle_event_shape;
380 /* check for randr extension */
381 if((awesomeconf[0].have_randr = XRRQueryExtension(dpy, &randr_event_base, &e_dummy)))
382 handler[randr_event_base + RRScreenChangeNotify] = handle_event_randr_screen_change_notify;
384 for(screen = 0; screen < ScreenCount(dpy); screen++)
386 awesomeconf[screen].have_shape = awesomeconf[0].have_shape;
387 awesomeconf[screen].have_randr = awesomeconf[0].have_randr;
388 scan(dpy, screen, &dc[screen], &awesomeconf[screen]);
391 XSync(dpy, False);
393 /* main event loop, also reads status text from stdin */
394 while(running)
396 FD_ZERO(&rd);
397 if(readin)
398 FD_SET(STDIN_FILENO, &rd);
399 FD_SET(xfd, &rd);
400 if(select(xfd + 1, &rd, NULL, NULL, NULL) == -1)
402 if(errno == EINTR)
403 continue;
404 eprint("select failed\n");
406 if(FD_ISSET(STDIN_FILENO, &rd))
408 switch (r = read(STDIN_FILENO, awesomeconf[0].statustext, sizeof(awesomeconf[0].statustext) - 1))
410 case -1:
411 strncpy(awesomeconf[0].statustext, strerror(errno), sizeof(awesomeconf[0].statustext) - 1);
412 awesomeconf[0].statustext[sizeof(awesomeconf[0].statustext) - 1] = '\0';
413 readin = False;
414 break;
415 case 0:
416 strncpy(awesomeconf[0].statustext, "EOF", 4);
417 readin = False;
418 break;
419 default:
420 for(awesomeconf[0].statustext[r] = '\0', p = awesomeconf[0].statustext + a_strlen(awesomeconf[0].statustext) - 1;
421 p >= awesomeconf[0].statustext && *p == '\n'; *p-- = '\0');
422 for(; p >= awesomeconf[0].statustext && *p != '\n'; --p);
423 if(p > awesomeconf[0].statustext)
424 strncpy(awesomeconf[0].statustext, p + 1, sizeof(awesomeconf[0].statustext));
426 drawstatusbar(dpy, 0, &dc[0], &awesomeconf[0]);
429 while(XPending(dpy))
431 XNextEvent(dpy, &ev);
432 if(handler[ev.type])
433 handler[ev.type](&ev, awesomeconf); /* call handler */
436 cleanup(dpy, dc, awesomeconf);
437 XCloseDisplay(dpy);
439 return EXIT_SUCCESS;