new feature: mouse wheel on statusbar do focus{next,prev}
[awesome.git] / awesome.c
blobc728830430dd8c3d68db66672a1d47c5174c7c3b
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 <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, awesomeconf[screen].phys_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);
212 /** Startup Error handler to check if another window manager
213 * is already running.
214 * \param disp Display ref
215 * \param ee Error event
217 static int __attribute__ ((noreturn))
218 xerrorstart(Display * disp __attribute__ ((unused)), XErrorEvent * ee __attribute__ ((unused)))
220 eprint("awesome: another window manager is already running\n");
223 /** Quit awesome
224 * \param disp Display ref
225 * \param drawcontext Drawcontext ref
226 * \param awesomeconf awesome config
227 * \param arg nothing
228 * \ingroup ui_callback
230 void
231 uicb_quit(Display *disp __attribute__ ((unused)),
232 DC *drawcontext __attribute__ ((unused)),
233 awesome_config *awesomeconf __attribute__((unused)),
234 const char *arg __attribute__ ((unused)))
236 readin = running = False;
239 /* There's no way to check accesses to destroyed windows, thus those cases are
240 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
241 * default error handler, which may call exit.
244 xerror(Display * edpy, XErrorEvent * ee)
246 if(ee->error_code == BadWindow
247 || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
248 || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
249 || (ee->request_code == X_PolyFillRectangle
250 && ee->error_code == BadDrawable)
251 || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
252 || (ee->request_code == X_ConfigureWindow
253 && ee->error_code == BadMatch) || (ee->request_code == X_GrabKey
254 && ee->error_code == BadAccess)
255 || (ee->request_code == X_CopyArea && ee->error_code == BadDrawable))
256 return 0;
257 fprintf(stderr, "awesome: fatal error: request code=%d, error code=%d\n",
258 ee->request_code, ee->error_code);
260 return xerrorxlib(edpy, ee); /* may call exit */
263 /** Hello, this is main
264 * \param argc who knows
265 * \param argv who knows
266 * \return EXIT_SUCCESS I hope
268 typedef void event_handler (XEvent *, awesome_config *);
270 main(int argc, char *argv[])
272 char *p;
273 const char *confpath = NULL;
274 int r, xfd, e_dummy;
275 fd_set rd;
276 XEvent ev;
277 Display * dpy;
278 awesome_config *awesomeconf;
279 int shape_event, randr_event_base;
280 int screen;
281 enum { NetSupported, NetWMName, NetLast }; /* EWMH atoms */
282 Atom netatom[NetLast];
283 event_handler **handler;
285 if(argc >= 2)
287 if(!a_strcmp("-v", argv[1]))
289 printf("awesome-" VERSION " © 2007 Julien Danjou\n");
290 return EXIT_SUCCESS;
292 else if(!a_strcmp("-c", argv[1]))
294 if(a_strlen(argv[2]))
295 confpath = argv[2];
296 else
297 eprint("awesome: -c require a file\n");
299 else
300 eprint("usage: awesome [-v | -c configfile]\n");
303 /* Tag won't be printed otherwised */
304 setlocale(LC_CTYPE, "");
306 if(!(dpy = XOpenDisplay(NULL)))
307 eprint("awesome: cannot open display\n");
309 xfd = ConnectionNumber(dpy);
311 XSetErrorHandler(xerrorstart);
312 for(screen = 0; screen < ScreenCount(dpy); screen++)
313 /* this causes an error if some other window manager is running */
314 XSelectInput(dpy, RootWindow(dpy, screen), SubstructureRedirectMask);
316 /* need to XSync to validate errorhandler */
317 XSync(dpy, False);
318 XSetErrorHandler(NULL);
319 xerrorxlib = XSetErrorHandler(xerror);
320 XSync(dpy, False);
322 /* allocate stuff */
323 dc = p_new(DC, get_screen_count(dpy));
324 awesomeconf = p_new(awesome_config, get_screen_count(dpy));
326 for(screen = 0; screen < get_screen_count(dpy); screen++)
328 parse_config(dpy, screen, &dc[screen], confpath, &awesomeconf[screen]);
329 setup(&dc[screen], &awesomeconf[screen]);
330 drawstatusbar(dpy, &dc[screen], &awesomeconf[screen]);
333 netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
334 netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
336 /* do this only for real screen */
337 for(screen = 0; screen < ScreenCount(dpy); screen++)
339 loadawesomeprops(dpy, &awesomeconf[screen]);
340 XChangeProperty(dpy, RootWindow(dpy, screen), netatom[NetSupported],
341 XA_ATOM, 32, PropModeReplace, (unsigned char *) netatom, NetLast);
344 handler = p_new(event_handler *, LASTEvent);
345 handler[ButtonPress] = handle_event_buttonpress;
346 handler[ConfigureRequest] = handle_event_configurerequest;
347 handler[ConfigureNotify] = handle_event_configurenotify;
348 handler[DestroyNotify] = handle_event_destroynotify;
349 handler[EnterNotify] = handle_event_enternotify;
350 handler[LeaveNotify] = handle_event_leavenotify;
351 handler[Expose] = handle_event_expose;
352 handler[KeyPress] = handle_event_keypress;
353 handler[MappingNotify] = handle_event_mappingnotify;
354 handler[MapRequest] = handle_event_maprequest;
355 handler[PropertyNotify] = handle_event_propertynotify;
356 handler[UnmapNotify] = handle_event_unmapnotify;
358 /* check for shape extension */
359 if((awesomeconf[0].have_shape = XShapeQueryExtension(dpy, &shape_event, &e_dummy)))
361 p_realloc(&handler, shape_event + 1);
362 handler[shape_event] = handle_event_shape;
365 /* check for randr extension */
366 if((awesomeconf[0].have_randr = XRRQueryExtension(dpy, &randr_event_base, &e_dummy)))
368 p_realloc(&handler, randr_event_base + RRScreenChangeNotify + 1);
369 handler[randr_event_base + RRScreenChangeNotify] = handle_event_randr_screen_change_notify;
372 for(screen = 0; screen < get_screen_count(dpy); screen++)
374 awesomeconf[screen].have_shape = awesomeconf[0].have_shape;
375 awesomeconf[screen].have_randr = awesomeconf[0].have_randr;
378 scan(dc, awesomeconf);
380 XSync(dpy, False);
382 /* main event loop, also reads status text from stdin */
383 while(running)
385 FD_ZERO(&rd);
386 if(readin)
387 FD_SET(STDIN_FILENO, &rd);
388 FD_SET(xfd, &rd);
389 if(select(xfd + 1, &rd, NULL, NULL, NULL) == -1)
391 if(errno == EINTR)
392 continue;
393 eprint("select failed\n");
395 if(FD_ISSET(STDIN_FILENO, &rd))
397 switch (r = read(STDIN_FILENO, awesomeconf[0].statustext, sizeof(awesomeconf[0].statustext) - 1))
399 case -1:
400 a_strncpy(awesomeconf[0].statustext, sizeof(awesomeconf[0].statustext),
401 strerror(errno), sizeof(awesomeconf[0].statustext) - 1);
402 awesomeconf[0].statustext[sizeof(awesomeconf[0].statustext) - 1] = '\0';
403 readin = False;
404 break;
405 case 0:
406 a_strncpy(awesomeconf[0].statustext, sizeof(awesomeconf[0].statustext),
407 "EOF", 4);
408 readin = False;
409 break;
410 default:
411 for(awesomeconf[0].statustext[r] = '\0', p = awesomeconf[0].statustext + a_strlen(awesomeconf[0].statustext) - 1;
412 p >= awesomeconf[0].statustext && *p == '\n'; *p-- = '\0');
413 for(; p >= awesomeconf[0].statustext && *p != '\n'; --p);
414 if(p > awesomeconf[0].statustext)
415 a_strncpy(awesomeconf[0].statustext, sizeof(awesomeconf[0].statustext),
416 p + 1, sizeof(awesomeconf[0].statustext));
418 drawstatusbar(dpy, &dc[0], &awesomeconf[0]);
421 while(XPending(dpy))
423 XNextEvent(dpy, &ev);
424 if(handler[ev.type])
425 handler[ev.type](&ev, awesomeconf); /* call handler */
428 cleanup(dc, awesomeconf);
429 XCloseDisplay(dpy);
431 return EXIT_SUCCESS;