fix refresh of colborders
[awesome.git] / awesome.c
blob3d303395b07505d5adb821de430362319a6caed8
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 <sys/types.h>
28 #include <sys/stat.h>
29 #include <fcntl.h>
30 #include <X11/cursorfont.h>
31 #include <X11/keysym.h>
32 #include <X11/Xatom.h>
33 #include <X11/Xproto.h>
34 #include <X11/Xutil.h>
35 #include <X11/extensions/shape.h>
36 #include <X11/extensions/Xrandr.h>
38 #include "awesome.h"
39 #include "event.h"
40 #include "layout.h"
41 #include "tag.h"
42 #include "screen.h"
43 #include "util.h"
44 #include "statusbar.h"
45 #include "uicb.h"
47 #define CONTROL_FIFO_PATH ".awesome_ctl"
49 static int (*xerrorxlib) (Display *, XErrorEvent *);
50 static Bool running = True;
52 /** Cleanup everything on quit
53 * \param awesomeconf awesome config
55 static void
56 cleanup(awesome_config *awesomeconf)
58 int screen, i;
60 while(*awesomeconf->clients)
62 unban(*awesomeconf->clients);
63 unmanage(*awesomeconf->clients, NormalState, awesomeconf);
66 for(screen = 0; screen < get_screen_count(awesomeconf->display); screen++)
68 XftFontClose(awesomeconf->display, awesomeconf->font);
70 XUngrabKey(awesomeconf->display, AnyKey, AnyModifier, RootWindow(awesomeconf->display, awesomeconf[screen].phys_screen));
72 XFreePixmap(awesomeconf->display, awesomeconf[screen].statusbar.drawable);
73 XDestroyWindow(awesomeconf->display, awesomeconf[screen].statusbar.window);
74 XFreeCursor(awesomeconf->display, awesomeconf[screen].cursor[CurNormal]);
75 XFreeCursor(awesomeconf->display, awesomeconf[screen].cursor[CurResize]);
76 XFreeCursor(awesomeconf->display, awesomeconf[screen].cursor[CurMove]);
78 for(i = 0; i < awesomeconf[screen].ntags; i++)
79 p_delete(&awesomeconf[screen].tags[i].name);
80 for(i = 0; i < awesomeconf[screen].nkeys; i++)
81 p_delete(&awesomeconf[screen].keys[i].arg);
82 for(i = 0; i < awesomeconf[screen].nlayouts; i++)
83 p_delete(&awesomeconf[screen].layouts[i].symbol);
84 for(i = 0; i < awesomeconf[screen].nrules; i++)
86 p_delete(&awesomeconf[screen].rules[i].prop);
87 p_delete(&awesomeconf[screen].rules[i].tags);
89 p_delete(&awesomeconf[screen].tags);
90 p_delete(&awesomeconf[screen].layouts);
91 p_delete(&awesomeconf[screen].rules);
92 p_delete(&awesomeconf[screen].keys);
94 XSetInputFocus(awesomeconf->display, PointerRoot, RevertToPointerRoot, CurrentTime);
95 XSync(awesomeconf->display, False);
96 p_delete(&awesomeconf->clients);
97 p_delete(&awesomeconf->client_sel);
98 p_delete(&awesomeconf);
101 /** Get a window state (WM_STATE)
102 * \param disp Display ref
103 * \param w Client window
104 * \return state
106 static long
107 getstate(Display *disp, Window w)
109 int format, status;
110 long result = -1;
111 unsigned char *p = NULL;
112 unsigned long n, extra;
113 Atom real;
114 status = XGetWindowProperty(disp, w, XInternAtom(disp, "WM_STATE", False),
115 0L, 2L, False, XInternAtom(disp, "WM_STATE", False),
116 &real, &format, &n, &extra, (unsigned char **) &p);
117 if(status != Success)
118 return -1;
119 if(n != 0)
120 result = *p;
121 p_delete(&p);
122 return result;
125 /** Scan X to find windows to manage
126 * \param screen Screen number
127 * \param awesomeconf awesome config
129 static void
130 scan(awesome_config *awesomeconf)
132 unsigned int i, num;
133 int screen, real_screen;
134 Window *wins = NULL, d1, d2;
135 XWindowAttributes wa;
137 for(screen = 0; screen < ScreenCount(awesomeconf->display); screen++)
139 if(XQueryTree(awesomeconf->display, RootWindow(awesomeconf->display, screen), &d1, &d2, &wins, &num))
141 real_screen = screen;
142 for(i = 0; i < num; i++)
144 if(!XGetWindowAttributes(awesomeconf->display, wins[i], &wa)
145 || wa.override_redirect
146 || XGetTransientForHint(awesomeconf->display, wins[i], &d1))
147 continue;
148 if(wa.map_state == IsViewable || getstate(awesomeconf->display, wins[i]) == IconicState)
150 if(screen == 0)
151 real_screen = get_screen_bycoord(awesomeconf->display, wa.x, wa.y);
152 manage(awesomeconf->display, wins[i], &wa, &awesomeconf[real_screen]);
155 /* now the transients */
156 for(i = 0; i < num; i++)
158 if(!XGetWindowAttributes(awesomeconf->display, wins[i], &wa))
159 continue;
160 if(XGetTransientForHint(awesomeconf->display, wins[i], &d1)
161 && (wa.map_state == IsViewable || getstate(awesomeconf->display, wins[i]) == IconicState))
163 if(screen == 0)
164 real_screen = get_screen_bycoord(awesomeconf->display, wa.x, wa.y);
165 manage(awesomeconf->display, wins[i], &wa, &awesomeconf[real_screen]);
169 if(wins)
170 XFree(wins);
174 /** Setup everything before running
175 * \param screen Screen number
176 * \param awesomeconf awesome config ref
177 * \todo clean things...
179 static void
180 setup(awesome_config *awesomeconf)
182 XSetWindowAttributes wa;
184 /* init cursors */
185 awesomeconf->cursor[CurNormal] = XCreateFontCursor(awesomeconf->display, XC_left_ptr);
186 awesomeconf->cursor[CurResize] = XCreateFontCursor(awesomeconf->display, XC_sizing);
187 awesomeconf->cursor[CurMove] = XCreateFontCursor(awesomeconf->display, XC_fleur);
189 /* select for events */
190 wa.event_mask = SubstructureRedirectMask | SubstructureNotifyMask
191 | EnterWindowMask | LeaveWindowMask | StructureNotifyMask;
192 wa.cursor = awesomeconf->cursor[CurNormal];
194 XChangeWindowAttributes(awesomeconf->display, RootWindow(awesomeconf->display, awesomeconf->phys_screen), CWEventMask | CWCursor, &wa);
196 XSelectInput(awesomeconf->display, RootWindow(awesomeconf->display, awesomeconf->phys_screen), wa.event_mask);
198 grabkeys(awesomeconf->display, awesomeconf->phys_screen, awesomeconf);
200 /* bar */
201 initstatusbar(awesomeconf->display, awesomeconf->screen, &awesomeconf->statusbar, awesomeconf->cursor[CurNormal], awesomeconf->font, awesomeconf->layouts, awesomeconf->nlayouts);
204 /** Startup Error handler to check if another window manager
205 * is already running.
206 * \param disp Display ref
207 * \param ee Error event
209 static int __attribute__ ((noreturn))
210 xerrorstart(Display * disp __attribute__ ((unused)), XErrorEvent * ee __attribute__ ((unused)))
212 eprint("awesome: another window manager is already running\n");
215 /** Quit awesome
216 * \param awesomeconf awesome config
217 * \param arg nothing
218 * \ingroup ui_callback
220 void
221 uicb_quit(awesome_config *awesomeconf __attribute__((unused)),
222 const char *arg __attribute__ ((unused)))
224 running = False;
227 /* There's no way to check accesses to destroyed windows, thus those cases are
228 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
229 * default error handler, which may call exit.
232 xerror(Display * edpy, XErrorEvent * ee)
234 if(ee->error_code == BadWindow
235 || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
236 || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
237 || (ee->request_code == X_PolyFillRectangle
238 && ee->error_code == BadDrawable)
239 || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
240 || (ee->request_code == X_ConfigureWindow
241 && ee->error_code == BadMatch) || (ee->request_code == X_GrabKey
242 && ee->error_code == BadAccess)
243 || (ee->request_code == X_CopyArea && ee->error_code == BadDrawable))
244 return 0;
245 fprintf(stderr, "awesome: fatal error: request code=%d, error code=%d\n",
246 ee->request_code, ee->error_code);
248 return xerrorxlib(edpy, ee); /* may call exit */
251 /** Hello, this is main
252 * \param argc who knows
253 * \param argv who knows
254 * \return EXIT_SUCCESS I hope
256 typedef void event_handler (XEvent *, awesome_config *);
258 main(int argc, char *argv[])
260 char *fifopath, buf[1024];
261 const char *confpath = NULL, *homedir;
262 int r, cfd, xfd, e_dummy;
263 fd_set rd;
264 XEvent ev;
265 Display * dpy;
266 awesome_config *awesomeconf;
267 int shape_event, randr_event_base;
268 int screen;
269 enum { NetSupported, NetWMName, NetLast }; /* EWMH atoms */
270 Atom netatom[NetLast];
271 event_handler **handler;
272 Client **clients, **sel;
273 struct stat fifost;
274 ssize_t fifopath_len;
276 if(argc >= 2)
278 if(!a_strcmp("-v", argv[1]))
280 printf("awesome-" VERSION " © 2007 Julien Danjou\n");
281 return EXIT_SUCCESS;
283 else if(!a_strcmp("-c", argv[1]))
285 if(a_strlen(argv[2]))
286 confpath = argv[2];
287 else
288 eprint("awesome: -c require a file\n");
290 else
291 eprint("usage: awesome [-v | -c configfile]\n");
294 /* Tag won't be printed otherwised */
295 setlocale(LC_CTYPE, "");
297 if(!(dpy = XOpenDisplay(NULL)))
298 eprint("awesome: cannot open display\n");
300 xfd = ConnectionNumber(dpy);
302 XSetErrorHandler(xerrorstart);
303 for(screen = 0; screen < ScreenCount(dpy); screen++)
304 /* this causes an error if some other window manager is running */
305 XSelectInput(dpy, RootWindow(dpy, screen), SubstructureRedirectMask);
307 /* need to XSync to validate errorhandler */
308 XSync(dpy, False);
309 XSetErrorHandler(NULL);
310 xerrorxlib = XSetErrorHandler(xerror);
311 XSync(dpy, False);
313 /* allocate stuff */
314 awesomeconf = p_new(awesome_config, get_screen_count(dpy));
315 clients = p_new(Client *, 1);
316 sel = p_new(Client *, 1);
318 for(screen = 0; screen < get_screen_count(dpy); screen++)
320 parse_config(dpy, screen, confpath, &awesomeconf[screen]);
321 setup(&awesomeconf[screen]);
322 awesomeconf[screen].clients = clients;
323 awesomeconf[screen].client_sel = sel;
324 drawstatusbar(dpy, &awesomeconf[screen]);
327 netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
328 netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
330 /* do this only for real screen */
331 for(screen = 0; screen < ScreenCount(dpy); screen++)
333 loadawesomeprops(dpy, &awesomeconf[screen]);
334 XChangeProperty(dpy, RootWindow(dpy, screen), netatom[NetSupported],
335 XA_ATOM, 32, PropModeReplace, (unsigned char *) netatom, NetLast);
338 handler = p_new(event_handler *, LASTEvent);
339 handler[ButtonPress] = handle_event_buttonpress;
340 handler[ConfigureRequest] = handle_event_configurerequest;
341 handler[ConfigureNotify] = handle_event_configurenotify;
342 handler[DestroyNotify] = handle_event_destroynotify;
343 handler[EnterNotify] = handle_event_enternotify;
344 handler[LeaveNotify] = handle_event_leavenotify;
345 handler[Expose] = handle_event_expose;
346 handler[KeyPress] = handle_event_keypress;
347 handler[MappingNotify] = handle_event_mappingnotify;
348 handler[MapRequest] = handle_event_maprequest;
349 handler[PropertyNotify] = handle_event_propertynotify;
350 handler[UnmapNotify] = handle_event_unmapnotify;
352 /* check for shape extension */
353 if((awesomeconf[0].have_shape = XShapeQueryExtension(dpy, &shape_event, &e_dummy)))
355 p_realloc(&handler, shape_event + 1);
356 handler[shape_event] = handle_event_shape;
359 /* check for randr extension */
360 if((awesomeconf[0].have_randr = XRRQueryExtension(dpy, &randr_event_base, &e_dummy)))
362 p_realloc(&handler, randr_event_base + RRScreenChangeNotify + 1);
363 handler[randr_event_base + RRScreenChangeNotify] = handle_event_randr_screen_change_notify;
366 for(screen = 0; screen < get_screen_count(dpy); screen++)
368 awesomeconf[screen].have_shape = awesomeconf[0].have_shape;
369 awesomeconf[screen].have_randr = awesomeconf[0].have_randr;
372 scan(awesomeconf);
374 XSync(dpy, False);
376 /* construct fifo path */
377 homedir = getenv("HOME");
378 fifopath_len = a_strlen(homedir) + a_strlen(CONTROL_FIFO_PATH) + 2;
379 fifopath = p_new(char, fifopath_len);
380 a_strcpy(fifopath, fifopath_len, homedir);
381 a_strcat(fifopath, fifopath_len, "/");
382 a_strcat(fifopath, fifopath_len, CONTROL_FIFO_PATH);
384 if(lstat(fifopath, &fifost) == -1)
385 if(mkfifo(fifopath, 0600) == -1)
386 perror("error creating control fifo");
388 cfd = open(fifopath, O_RDONLY | O_NDELAY);
390 /* main event loop, also reads status text from stdin */
391 while(running)
393 FD_ZERO(&rd);
394 if(cfd >= 0)
395 FD_SET(cfd, &rd);
396 FD_SET(xfd, &rd);
397 if(select(MAX(xfd, cfd) + 1, &rd, NULL, NULL, NULL) == -1)
399 if(errno == EINTR)
400 continue;
401 eprint("select failed\n");
403 if(cfd >= 0 && FD_ISSET(cfd, &rd))
405 switch (r = read(cfd, buf, sizeof(buf)))
407 case -1:
408 perror("awesome: error reading fifo");
409 a_strncpy(awesomeconf[0].statustext, sizeof(awesomeconf[0].statustext),
410 strerror(errno), sizeof(awesomeconf[0].statustext) - 1);
411 awesomeconf[0].statustext[sizeof(awesomeconf[0].statustext) - 1] = '\0';
412 cfd = -1;
413 break;
414 case 0:
415 close(cfd);
416 cfd = open(fifopath, O_RDONLY | O_NDELAY);
417 break;
418 default:
419 parse_control(buf, awesomeconf);
421 drawstatusbar(dpy, &awesomeconf[0]);
424 while(XPending(dpy))
426 XNextEvent(dpy, &ev);
427 if(handler[ev.type])
428 handler[ev.type](&ev, awesomeconf); /* call handler */
432 p_delete(&fifopath);
434 cleanup(awesomeconf);
435 XCloseDisplay(dpy);
437 return EXIT_SUCCESS;
439 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99