change behaviour of uicb_setlayout: no arg now reset to first/default layout ; add...
[awesome.git] / awesome.c
blob75788b0b356b914522a9ccfe073883b359f4826d
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 drawcontext Drawcontext ref
53 * \param awesomeconf awesome config
55 static void
56 cleanup(DC *drawcontext, awesome_config *awesomeconf)
58 int screen, i;
60 close(STDIN_FILENO);
62 while(stack)
64 unban(stack);
65 unmanage(stack, drawcontext, NormalState, awesomeconf);
68 for(screen = 0; screen < get_screen_count(awesomeconf->display); screen++)
70 XftFontClose(awesomeconf->display, drawcontext->font);
72 XUngrabKey(awesomeconf->display, AnyKey, AnyModifier, RootWindow(awesomeconf->display, screen));
74 XFreePixmap(awesomeconf->display, awesomeconf[screen].statusbar.drawable);
75 XFreeGC(awesomeconf->display, drawcontext[screen].gc);
76 XDestroyWindow(awesomeconf->display, awesomeconf[screen].statusbar.window);
77 XFreeCursor(awesomeconf->display, drawcontext[screen].cursor[CurNormal]);
78 XFreeCursor(awesomeconf->display, drawcontext[screen].cursor[CurResize]);
79 XFreeCursor(awesomeconf->display, drawcontext[screen].cursor[CurMove]);
81 for(i = 0; i < awesomeconf[screen].ntags; i++)
82 p_delete(&awesomeconf[screen].tags[i].name);
83 for(i = 0; i < awesomeconf[screen].nkeys; i++)
84 p_delete(&awesomeconf[screen].keys[i].arg);
85 for(i = 0; i < awesomeconf[screen].nlayouts; i++)
86 p_delete(&awesomeconf[screen].layouts[i].symbol);
87 for(i = 0; i < awesomeconf[screen].nrules; i++)
89 p_delete(&awesomeconf[screen].rules[i].prop);
90 p_delete(&awesomeconf[screen].rules[i].tags);
92 p_delete(&awesomeconf[screen].tags);
93 p_delete(&awesomeconf[screen].layouts);
94 p_delete(&awesomeconf[screen].rules);
95 p_delete(&awesomeconf[screen].keys);
97 XSetInputFocus(awesomeconf->display, PointerRoot, RevertToPointerRoot, CurrentTime);
98 XSync(awesomeconf->display, False);
99 p_delete(&awesomeconf);
100 p_delete(&dc);
103 /** Get a window state (WM_STATE)
104 * \param disp Display ref
105 * \param w Client window
106 * \return state
108 static long
109 getstate(Display *disp, Window w)
111 int format, status;
112 long result = -1;
113 unsigned char *p = NULL;
114 unsigned long n, extra;
115 Atom real;
116 status = XGetWindowProperty(disp, w, XInternAtom(disp, "WM_STATE", False),
117 0L, 2L, False, XInternAtom(disp, "WM_STATE", False),
118 &real, &format, &n, &extra, (unsigned char **) &p);
119 if(status != Success)
120 return -1;
121 if(n != 0)
122 result = *p;
123 XFree(p);
124 return result;
127 /** Scan X to find windows to manage
128 * \param screen Screen number
129 * \param drawcontext Drawcontext ref
130 * \param awesomeconf awesome config
132 static void
133 scan(DC *drawcontext, awesome_config *awesomeconf)
135 unsigned int i, num;
136 int screen, real_screen;
137 Window *wins = NULL, d1, d2;
138 XWindowAttributes wa;
140 for(screen = 0; screen < ScreenCount(awesomeconf->display); screen++)
142 if(XQueryTree(awesomeconf->display, RootWindow(awesomeconf->display, screen), &d1, &d2, &wins, &num))
144 real_screen = screen;
145 for(i = 0; i < num; i++)
147 if(!XGetWindowAttributes(awesomeconf->display, wins[i], &wa)
148 || wa.override_redirect
149 || XGetTransientForHint(awesomeconf->display, wins[i], &d1))
150 continue;
151 if(wa.map_state == IsViewable || getstate(awesomeconf->display, wins[i]) == IconicState)
153 if(screen == 0)
154 real_screen = get_screen_bycoord(awesomeconf->display, wa.x, wa.y);
155 manage(awesomeconf->display, &drawcontext[real_screen], wins[i], &wa, &awesomeconf[real_screen]);
158 /* now the transients */
159 for(i = 0; i < num; i++)
161 if(!XGetWindowAttributes(awesomeconf->display, wins[i], &wa))
162 continue;
163 if(XGetTransientForHint(awesomeconf->display, wins[i], &d1)
164 && (wa.map_state == IsViewable || getstate(awesomeconf->display, wins[i]) == IconicState))
166 if(screen == 0)
167 real_screen = get_screen_bycoord(awesomeconf->display, wa.x, wa.y);
168 manage(awesomeconf->display, &drawcontext[real_screen], wins[i], &wa, &awesomeconf[real_screen]);
172 if(wins)
173 XFree(wins);
177 /** Setup everything before running
178 * \param screen Screen number
179 * \param awesomeconf awesome config ref
180 * \todo clean things...
182 static void
183 setup(DC *drawcontext, awesome_config *awesomeconf)
185 XSetWindowAttributes wa;
187 /* init cursors */
188 drawcontext->cursor[CurNormal] = XCreateFontCursor(awesomeconf->display, XC_left_ptr);
189 drawcontext->cursor[CurResize] = XCreateFontCursor(awesomeconf->display, XC_sizing);
190 drawcontext->cursor[CurMove] = XCreateFontCursor(awesomeconf->display, XC_fleur);
192 /* select for events */
193 wa.event_mask = SubstructureRedirectMask | SubstructureNotifyMask
194 | EnterWindowMask | LeaveWindowMask | StructureNotifyMask;
195 wa.cursor = drawcontext->cursor[CurNormal];
197 XChangeWindowAttributes(awesomeconf->display, RootWindow(awesomeconf->display, awesomeconf->phys_screen), CWEventMask | CWCursor, &wa);
199 XSelectInput(awesomeconf->display, RootWindow(awesomeconf->display, awesomeconf->phys_screen), wa.event_mask);
201 grabkeys(awesomeconf->display, awesomeconf->phys_screen, awesomeconf);
203 compileregs(awesomeconf->rules, awesomeconf->nrules);
205 /* bar */
206 drawcontext->h = awesomeconf->statusbar.height = drawcontext->font->height + 2;
207 initstatusbar(awesomeconf->display, awesomeconf->screen, drawcontext, &awesomeconf->statusbar);
208 drawcontext->gc = XCreateGC(awesomeconf->display, RootWindow(awesomeconf->display, awesomeconf->phys_screen), 0, 0);
209 XSetLineAttributes(awesomeconf->display, drawcontext->gc, 1, LineSolid, CapButt, JoinMiter);
211 loadawesomeprops(awesomeconf->display, 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 drawcontext Drawcontext ref
228 * \param awesomeconf awesome config
229 * \param arg nothing
230 * \ingroup ui_callback
232 void
233 uicb_quit(Display *disp __attribute__ ((unused)),
234 DC *drawcontext __attribute__ ((unused)),
235 awesome_config *awesomeconf __attribute__((unused)),
236 const char *arg __attribute__ ((unused)))
238 readin = running = False;
241 /* There's no way to check accesses to destroyed windows, thus those cases are
242 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
243 * default error handler, which may call exit.
246 xerror(Display * edpy, XErrorEvent * ee)
248 if(ee->error_code == BadWindow
249 || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
250 || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
251 || (ee->request_code == X_PolyFillRectangle
252 && ee->error_code == BadDrawable)
253 || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
254 || (ee->request_code == X_ConfigureWindow
255 && ee->error_code == BadMatch) || (ee->request_code == X_GrabKey
256 && ee->error_code == BadAccess)
257 || (ee->request_code == X_CopyArea && ee->error_code == BadDrawable))
258 return 0;
259 fprintf(stderr, "awesome: fatal error: request code=%d, error code=%d\n",
260 ee->request_code, ee->error_code);
262 return xerrorxlib(edpy, ee); /* may call exit */
265 /** Hello, this is main
266 * \param argc who knows
267 * \param argv who knows
268 * \return EXIT_SUCCESS I hope
270 typedef void event_handler (XEvent *, awesome_config *);
272 main(int argc, char *argv[])
274 char *p;
275 const char *confpath = NULL;
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)
289 if(!a_strcmp("-v", argv[1]))
291 printf("awesome-" VERSION " © 2007 Julien Danjou\n");
292 return EXIT_SUCCESS;
294 else if(!a_strcmp("-c", argv[1]))
296 if(a_strlen(argv[2]))
297 confpath = argv[2];
298 else
299 eprint("awesome: -c require a file\n");
301 else
302 eprint("usage: awesome [-v | -c configfile]\n");
305 /* Tag won't be printed otherwised */
306 setlocale(LC_CTYPE, "");
308 if(!(dpy = XOpenDisplay(NULL)))
309 eprint("awesome: cannot open display\n");
311 xfd = ConnectionNumber(dpy);
313 XSetErrorHandler(xerrorstart);
314 for(screen = 0; screen < ScreenCount(dpy); screen++)
315 /* this causes an error if some other window manager is running */
316 XSelectInput(dpy, RootWindow(dpy, screen), SubstructureRedirectMask);
318 /* need to XSync to validate errorhandler */
319 XSync(dpy, False);
320 XSetErrorHandler(NULL);
321 xerrorxlib = XSetErrorHandler(xerror);
322 XSync(dpy, False);
324 netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
325 netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
327 /* allocate stuff */
328 dc = p_new(DC, get_screen_count(dpy));
329 awesomeconf = p_new(awesome_config, get_screen_count(dpy));
331 for(screen = 0; screen < get_screen_count(dpy); screen++)
333 parse_config(dpy, screen, &dc[screen], confpath, &awesomeconf[screen]);
334 setup(&dc[screen], &awesomeconf[screen]);
335 XChangeProperty(dpy, RootWindow(dpy, screen), netatom[NetSupported],
336 XA_ATOM, 32, PropModeReplace, (unsigned char *) netatom, NetLast);
337 drawstatusbar(dpy, &dc[screen], &awesomeconf[screen]);
340 handler = p_new(event_handler *, LASTEvent);
341 handler[ButtonPress] = handle_event_buttonpress;
342 handler[ConfigureRequest] = handle_event_configurerequest;
343 handler[ConfigureNotify] = handle_event_configurenotify;
344 handler[DestroyNotify] = handle_event_destroynotify;
345 handler[EnterNotify] = handle_event_enternotify;
346 handler[LeaveNotify] = handle_event_leavenotify;
347 handler[Expose] = handle_event_expose;
348 handler[KeyPress] = handle_event_keypress;
349 handler[MappingNotify] = handle_event_mappingnotify;
350 handler[MapRequest] = handle_event_maprequest;
351 handler[PropertyNotify] = handle_event_propertynotify;
352 handler[UnmapNotify] = handle_event_unmapnotify;
354 /* check for shape extension */
355 if((awesomeconf[0].have_shape = XShapeQueryExtension(dpy, &shape_event, &e_dummy)))
357 p_realloc(&handler, shape_event + 1);
358 handler[shape_event] = handle_event_shape;
361 /* check for randr extension */
362 if((awesomeconf[0].have_randr = XRRQueryExtension(dpy, &randr_event_base, &e_dummy)))
364 p_realloc(&handler, randr_event_base + RRScreenChangeNotify + 1);
365 handler[randr_event_base + RRScreenChangeNotify] = handle_event_randr_screen_change_notify;
368 for(screen = 0; screen < get_screen_count(dpy); screen++)
370 awesomeconf[screen].have_shape = awesomeconf[0].have_shape;
371 awesomeconf[screen].have_randr = awesomeconf[0].have_randr;
374 scan(dc, awesomeconf);
376 XSync(dpy, False);
378 /* main event loop, also reads status text from stdin */
379 while(running)
381 FD_ZERO(&rd);
382 if(readin)
383 FD_SET(STDIN_FILENO, &rd);
384 FD_SET(xfd, &rd);
385 if(select(xfd + 1, &rd, NULL, NULL, NULL) == -1)
387 if(errno == EINTR)
388 continue;
389 eprint("select failed\n");
391 if(FD_ISSET(STDIN_FILENO, &rd))
393 switch (r = read(STDIN_FILENO, awesomeconf[0].statustext, sizeof(awesomeconf[0].statustext) - 1))
395 case -1:
396 a_strncpy(awesomeconf[0].statustext, sizeof(awesomeconf[0].statustext),
397 strerror(errno), sizeof(awesomeconf[0].statustext) - 1);
398 awesomeconf[0].statustext[sizeof(awesomeconf[0].statustext) - 1] = '\0';
399 readin = False;
400 break;
401 case 0:
402 a_strncpy(awesomeconf[0].statustext, sizeof(awesomeconf[0].statustext),
403 "EOF", 4);
404 readin = False;
405 break;
406 default:
407 for(awesomeconf[0].statustext[r] = '\0', p = awesomeconf[0].statustext + a_strlen(awesomeconf[0].statustext) - 1;
408 p >= awesomeconf[0].statustext && *p == '\n'; *p-- = '\0');
409 for(; p >= awesomeconf[0].statustext && *p != '\n'; --p);
410 if(p > awesomeconf[0].statustext)
411 a_strncpy(awesomeconf[0].statustext, sizeof(awesomeconf[0].statustext),
412 p + 1, sizeof(awesomeconf[0].statustext));
414 drawstatusbar(dpy, &dc[0], &awesomeconf[0]);
417 while(XPending(dpy))
419 XNextEvent(dpy, &ev);
420 if(handler[ev.type])
421 handler[ev.type](&ev, awesomeconf); /* call handler */
424 cleanup(dc, awesomeconf);
425 XCloseDisplay(dpy);
427 return EXIT_SUCCESS;