fix initstatusbar()
[awesome.git] / awesome.c
blob1bf5a6559af32a86aa9b16ad1a25c390574dd64c
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 awesomeconf awesome config ref
176 * \todo clean things...
178 static void
179 setup(awesome_config *awesomeconf)
181 XSetWindowAttributes wa;
183 /* init cursors */
184 awesomeconf->cursor[CurNormal] = XCreateFontCursor(awesomeconf->display, XC_left_ptr);
185 awesomeconf->cursor[CurResize] = XCreateFontCursor(awesomeconf->display, XC_sizing);
186 awesomeconf->cursor[CurMove] = XCreateFontCursor(awesomeconf->display, XC_fleur);
188 /* select for events */
189 wa.event_mask = SubstructureRedirectMask | SubstructureNotifyMask
190 | EnterWindowMask | LeaveWindowMask | StructureNotifyMask;
191 wa.cursor = awesomeconf->cursor[CurNormal];
193 XChangeWindowAttributes(awesomeconf->display, RootWindow(awesomeconf->display, awesomeconf->phys_screen), CWEventMask | CWCursor, &wa);
195 XSelectInput(awesomeconf->display, RootWindow(awesomeconf->display, awesomeconf->phys_screen), wa.event_mask);
197 grabkeys(awesomeconf);
200 /** Startup Error handler to check if another window manager
201 * is already running.
202 * \param disp Display ref
203 * \param ee Error event
205 static int __attribute__ ((noreturn))
206 xerrorstart(Display * disp __attribute__ ((unused)), XErrorEvent * ee __attribute__ ((unused)))
208 eprint("awesome: another window manager is already running\n");
211 /** Quit awesome
212 * \param awesomeconf awesome config
213 * \param arg nothing
214 * \ingroup ui_callback
216 void
217 uicb_quit(awesome_config *awesomeconf __attribute__((unused)),
218 const char *arg __attribute__ ((unused)))
220 running = False;
223 /* There's no way to check accesses to destroyed windows, thus those cases are
224 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
225 * default error handler, which may call exit.
228 xerror(Display * edpy, XErrorEvent * ee)
230 if(ee->error_code == BadWindow
231 || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
232 || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
233 || (ee->request_code == X_PolyFillRectangle
234 && ee->error_code == BadDrawable)
235 || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
236 || (ee->request_code == X_ConfigureWindow
237 && ee->error_code == BadMatch) || (ee->request_code == X_GrabKey
238 && ee->error_code == BadAccess)
239 || (ee->request_code == X_CopyArea && ee->error_code == BadDrawable))
240 return 0;
241 fprintf(stderr, "awesome: fatal error: request code=%d, error code=%d\n",
242 ee->request_code, ee->error_code);
244 return xerrorxlib(edpy, ee); /* may call exit */
247 /** Hello, this is main
248 * \param argc who knows
249 * \param argv who knows
250 * \return EXIT_SUCCESS I hope
252 typedef void event_handler (XEvent *, awesome_config *);
254 main(int argc, char *argv[])
256 char *fifopath, buf[1024];
257 const char *confpath = NULL, *homedir;
258 int r, cfd, xfd, e_dummy;
259 fd_set rd;
260 XEvent ev;
261 Display * dpy;
262 awesome_config *awesomeconf;
263 int shape_event, randr_event_base;
264 int screen;
265 enum { NetSupported, NetWMName, NetLast }; /* EWMH atoms */
266 Atom netatom[NetLast];
267 event_handler **handler;
268 Client **clients, **sel;
269 struct stat fifost;
270 ssize_t fifopath_len;
272 if(argc >= 2)
274 if(!a_strcmp("-v", argv[1]))
276 printf("awesome-" VERSION " © 2007 Julien Danjou\n");
277 return EXIT_SUCCESS;
279 else if(!a_strcmp("-c", argv[1]))
281 if(a_strlen(argv[2]))
282 confpath = argv[2];
283 else
284 eprint("awesome: -c require a file\n");
286 else
287 eprint("usage: awesome [-v | -c configfile]\n");
290 /* Tag won't be printed otherwised */
291 setlocale(LC_CTYPE, "");
293 if(!(dpy = XOpenDisplay(NULL)))
294 eprint("awesome: cannot open display\n");
296 xfd = ConnectionNumber(dpy);
298 XSetErrorHandler(xerrorstart);
299 for(screen = 0; screen < ScreenCount(dpy); screen++)
300 /* this causes an error if some other window manager is running */
301 XSelectInput(dpy, RootWindow(dpy, screen), SubstructureRedirectMask);
303 /* need to XSync to validate errorhandler */
304 XSync(dpy, False);
305 XSetErrorHandler(NULL);
306 xerrorxlib = XSetErrorHandler(xerror);
307 XSync(dpy, False);
309 /* allocate stuff */
310 awesomeconf = p_new(awesome_config, get_screen_count(dpy));
311 clients = p_new(Client *, 1);
312 sel = p_new(Client *, 1);
314 for(screen = 0; screen < get_screen_count(dpy); screen++)
316 parse_config(dpy, screen, confpath, &awesomeconf[screen]);
317 setup(&awesomeconf[screen]);
318 awesomeconf[screen].clients = clients;
319 awesomeconf[screen].client_sel = sel;
320 initstatusbar(awesomeconf[screen].display, screen, &awesomeconf[screen].statusbar,
321 awesomeconf[screen].cursor[CurNormal], awesomeconf[screen].font,
322 awesomeconf[screen].layouts, awesomeconf[screen].nlayouts);
323 drawstatusbar(&awesomeconf[screen]);
326 netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
327 netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
329 /* do this only for real screen */
330 for(screen = 0; screen < ScreenCount(dpy); screen++)
332 loadawesomeprops(&awesomeconf[screen]);
333 XChangeProperty(dpy, RootWindow(dpy, screen), netatom[NetSupported],
334 XA_ATOM, 32, PropModeReplace, (unsigned char *) netatom, NetLast);
337 handler = p_new(event_handler *, LASTEvent);
338 handler[ButtonPress] = handle_event_buttonpress;
339 handler[ConfigureRequest] = handle_event_configurerequest;
340 handler[ConfigureNotify] = handle_event_configurenotify;
341 handler[DestroyNotify] = handle_event_destroynotify;
342 handler[EnterNotify] = handle_event_enternotify;
343 handler[LeaveNotify] = handle_event_leavenotify;
344 handler[Expose] = handle_event_expose;
345 handler[KeyPress] = handle_event_keypress;
346 handler[MappingNotify] = handle_event_mappingnotify;
347 handler[MapRequest] = handle_event_maprequest;
348 handler[PropertyNotify] = handle_event_propertynotify;
349 handler[UnmapNotify] = handle_event_unmapnotify;
351 /* check for shape extension */
352 if((awesomeconf[0].have_shape = XShapeQueryExtension(dpy, &shape_event, &e_dummy)))
354 p_realloc(&handler, shape_event + 1);
355 handler[shape_event] = handle_event_shape;
358 /* check for randr extension */
359 if((awesomeconf[0].have_randr = XRRQueryExtension(dpy, &randr_event_base, &e_dummy)))
361 p_realloc(&handler, randr_event_base + RRScreenChangeNotify + 1);
362 handler[randr_event_base + RRScreenChangeNotify] = handle_event_randr_screen_change_notify;
365 for(screen = 0; screen < get_screen_count(dpy); screen++)
367 awesomeconf[screen].have_shape = awesomeconf[0].have_shape;
368 awesomeconf[screen].have_randr = awesomeconf[0].have_randr;
371 scan(awesomeconf);
373 XSync(dpy, False);
375 /* construct fifo path */
376 homedir = getenv("HOME");
377 fifopath_len = a_strlen(homedir) + a_strlen(CONTROL_FIFO_PATH) + 2;
378 fifopath = p_new(char, fifopath_len);
379 a_strcpy(fifopath, fifopath_len, homedir);
380 a_strcat(fifopath, fifopath_len, "/");
381 a_strcat(fifopath, fifopath_len, CONTROL_FIFO_PATH);
383 if(lstat(fifopath, &fifost) == -1)
384 if(mkfifo(fifopath, 0600) == -1)
385 perror("error creating control fifo");
387 cfd = open(fifopath, O_RDONLY | O_NDELAY);
389 /* main event loop, also reads status text from stdin */
390 while(running)
392 FD_ZERO(&rd);
393 if(cfd >= 0)
394 FD_SET(cfd, &rd);
395 FD_SET(xfd, &rd);
396 if(select(MAX(xfd, cfd) + 1, &rd, NULL, NULL, NULL) == -1)
398 if(errno == EINTR)
399 continue;
400 eprint("select failed\n");
402 if(cfd >= 0 && FD_ISSET(cfd, &rd))
403 switch (r = read(cfd, buf, sizeof(buf)))
405 case -1:
406 perror("awesome: error reading fifo");
407 a_strncpy(awesomeconf[0].statustext, sizeof(awesomeconf[0].statustext),
408 strerror(errno), sizeof(awesomeconf[0].statustext) - 1);
409 awesomeconf[0].statustext[sizeof(awesomeconf[0].statustext) - 1] = '\0';
410 cfd = -1;
411 break;
412 case 0:
413 close(cfd);
414 cfd = open(fifopath, O_RDONLY | O_NDELAY);
415 break;
416 default:
417 parse_control(buf, awesomeconf);
420 while(XPending(dpy))
422 XNextEvent(dpy, &ev);
423 if(handler[ev.type])
424 handler[ev.type](&ev, awesomeconf); /* call handler */
428 p_delete(&fifopath);
430 cleanup(awesomeconf);
431 XCloseDisplay(dpy);
433 return EXIT_SUCCESS;
435 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99