delete key->arg on cleanup
[awesome.git] / awesome.c
blob56ca2b9b6b6d2ee28e6e711982938f96561b7351
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 <sys/socket.h>
30 #include <sys/un.h>
31 #include <fcntl.h>
32 #include <X11/cursorfont.h>
33 #include <X11/keysym.h>
34 #include <X11/Xatom.h>
35 #include <X11/Xproto.h>
36 #include <X11/Xutil.h>
37 #include <X11/extensions/shape.h>
38 #include <X11/extensions/Xrandr.h>
40 #include "awesome.h"
41 #include "event.h"
42 #include "layout.h"
43 #include "screen.h"
44 #include "util.h"
45 #include "statusbar.h"
46 #include "uicb.h"
47 #include "window.h"
48 #include "awesome-client.h"
50 static int (*xerrorxlib) (Display *, XErrorEvent *);
51 static Bool running = True;
54 static inline void
55 cleanup_buttons(Button *buttons)
57 Button *b, *bn;
59 for(b = buttons; b; b = bn)
61 bn = b->next;
62 p_delete(&b->arg);
63 p_delete(&b);
67 void
68 cleanup_screen(awesome_config *awesomeconf)
70 int i;
71 Key *k, *kn;
72 Rule *r, *rn;
74 XftFontClose(awesomeconf->display, awesomeconf->font);
75 XUngrabKey(awesomeconf->display, AnyKey, AnyModifier, RootWindow(awesomeconf->display, awesomeconf->phys_screen));
76 XDestroyWindow(awesomeconf->display, awesomeconf->statusbar.window);
77 XFreeCursor(awesomeconf->display, awesomeconf->cursor[CurNormal]);
78 XFreeCursor(awesomeconf->display, awesomeconf->cursor[CurResize]);
79 XFreeCursor(awesomeconf->display, awesomeconf->cursor[CurMove]);
81 for(i = 0; i < awesomeconf->ntags; i++)
82 p_delete(&awesomeconf->tags[i].name);
83 for(k = awesomeconf->keys; k; k = kn)
85 kn = k->next;
86 p_delete(&k->arg);
87 p_delete(&k);
90 cleanup_buttons(awesomeconf->buttons.tag);
91 cleanup_buttons(awesomeconf->buttons.title);
92 cleanup_buttons(awesomeconf->buttons.layout);
93 cleanup_buttons(awesomeconf->buttons.root);
94 cleanup_buttons(awesomeconf->buttons.client);
96 for(i = 0; i < awesomeconf->nlayouts; i++)
97 p_delete(&awesomeconf->layouts[i].symbol);
98 for(r = awesomeconf->rules; r; r = rn)
100 rn = r->next;
101 p_delete(&r->prop);
102 p_delete(&r->tags);
103 p_delete(&r);
106 p_delete(&awesomeconf->tags);
107 p_delete(&awesomeconf->layouts);
108 p_delete(&awesomeconf->configpath);
111 /** Cleanup everything on quit
112 * \param awesomeconf awesome config
114 static void
115 cleanup(awesome_config *awesomeconf)
117 int screen;
119 while(*awesomeconf->clients)
121 client_unban(*awesomeconf->clients);
122 client_unmanage(*awesomeconf->clients, NormalState, awesomeconf);
125 for(screen = 0; screen < get_screen_count(awesomeconf->display); screen++)
126 cleanup_screen(&awesomeconf[screen]);
128 XSetInputFocus(awesomeconf->display, PointerRoot, RevertToPointerRoot, CurrentTime);
129 XSync(awesomeconf->display, False);
131 p_delete(&awesomeconf->clients);
132 p_delete(&awesomeconf);
135 /** Scan X to find windows to manage
136 * \param screen Screen number
137 * \param awesomeconf awesome config
139 static void
140 scan(awesome_config *awesomeconf)
142 unsigned int i, num;
143 int screen, real_screen;
144 Window *wins = NULL, d1, d2;
145 XWindowAttributes wa;
147 for(screen = 0; screen < ScreenCount(awesomeconf->display); screen++)
149 if(XQueryTree(awesomeconf->display, RootWindow(awesomeconf->display, screen), &d1, &d2, &wins, &num))
151 real_screen = screen;
152 for(i = 0; i < num; i++)
154 if(!XGetWindowAttributes(awesomeconf->display, wins[i], &wa)
155 || wa.override_redirect
156 || XGetTransientForHint(awesomeconf->display, wins[i], &d1))
157 continue;
158 if(wa.map_state == IsViewable || window_getstate(awesomeconf->display, wins[i]) == IconicState)
160 if(screen == 0)
161 real_screen = get_screen_bycoord(awesomeconf->display, wa.x, wa.y);
162 client_manage(wins[i], &wa, &awesomeconf[real_screen]);
165 /* now the transients */
166 for(i = 0; i < num; i++)
168 if(!XGetWindowAttributes(awesomeconf->display, wins[i], &wa))
169 continue;
170 if(XGetTransientForHint(awesomeconf->display, wins[i], &d1)
171 && (wa.map_state == IsViewable || window_getstate(awesomeconf->display, wins[i]) == IconicState))
173 if(screen == 0)
174 real_screen = get_screen_bycoord(awesomeconf->display, wa.x, wa.y);
175 client_manage(wins[i], &wa, &awesomeconf[real_screen]);
179 if(wins)
180 XFree(wins);
184 /** Setup everything before running
185 * \param awesomeconf awesome config ref
186 * \todo clean things...
188 static void
189 setup(awesome_config *awesomeconf)
191 XSetWindowAttributes wa;
193 /* init cursors */
194 awesomeconf->cursor[CurNormal] = XCreateFontCursor(awesomeconf->display, XC_left_ptr);
195 awesomeconf->cursor[CurResize] = XCreateFontCursor(awesomeconf->display, XC_sizing);
196 awesomeconf->cursor[CurMove] = XCreateFontCursor(awesomeconf->display, XC_fleur);
198 /* select for events */
199 wa.event_mask = SubstructureRedirectMask | SubstructureNotifyMask
200 | EnterWindowMask | LeaveWindowMask | StructureNotifyMask;
201 wa.cursor = awesomeconf->cursor[CurNormal];
203 XChangeWindowAttributes(awesomeconf->display, RootWindow(awesomeconf->display, awesomeconf->phys_screen), CWEventMask | CWCursor, &wa);
205 XSelectInput(awesomeconf->display, RootWindow(awesomeconf->display, awesomeconf->phys_screen), wa.event_mask);
207 grabkeys(awesomeconf);
210 void
211 setup_screen(awesome_config *awesomeconf, const char *confpath)
213 parse_config(confpath, awesomeconf);
214 setup(awesomeconf);
215 initstatusbar(awesomeconf->display, awesomeconf->screen, &awesomeconf->statusbar,
216 awesomeconf->cursor[CurNormal], awesomeconf->font,
217 awesomeconf->layouts, awesomeconf->nlayouts);
220 /** Startup Error handler to check if another window manager
221 * is already running.
222 * \param disp Display ref
223 * \param ee Error event
225 static int __attribute__ ((noreturn))
226 xerrorstart(Display * disp __attribute__ ((unused)), XErrorEvent * ee __attribute__ ((unused)))
228 eprint("awesome: another window manager is already running\n");
231 /** Quit awesome
232 * \param awesomeconf awesome config
233 * \param arg nothing
234 * \ingroup ui_callback
236 void
237 uicb_quit(awesome_config *awesomeconf __attribute__((unused)),
238 const char *arg __attribute__ ((unused)))
240 running = False;
243 /* There's no way to check accesses to destroyed windows, thus those cases are
244 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
245 * default error handler, which may call exit.
248 xerror(Display * edpy, XErrorEvent * ee)
250 if(ee->error_code == BadWindow
251 || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
252 || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
253 || (ee->request_code == X_PolyFillRectangle
254 && ee->error_code == BadDrawable)
255 || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
256 || (ee->request_code == X_ConfigureWindow
257 && ee->error_code == BadMatch) || (ee->request_code == X_GrabKey
258 && ee->error_code == BadAccess)
259 || (ee->request_code == X_CopyArea && ee->error_code == BadDrawable))
260 return 0;
261 fprintf(stderr, "awesome: fatal error: request code=%d, error code=%d\n",
262 ee->request_code, ee->error_code);
264 return xerrorxlib(edpy, ee); /* may call exit */
267 /** Hello, this is main
268 * \param argc who knows
269 * \param argv who knows
270 * \return EXIT_SUCCESS I hope
272 typedef void event_handler (XEvent *, awesome_config *);
274 main(int argc, char *argv[])
276 char buf[1024];
277 const char *confpath = NULL;
278 int r, xfd, e_dummy, csfd;
279 fd_set rd;
280 XEvent ev;
281 Display * dpy;
282 awesome_config *awesomeconf;
283 int shape_event, randr_event_base;
284 int screen;
285 enum { NetSupported, NetWMName, NetLast }; /* EWMH atoms */
286 Atom netatom[NetLast];
287 event_handler **handler;
288 Client **clients;
289 struct sockaddr_un *addr;
291 if(argc >= 2)
293 if(!a_strcmp("-v", argv[1]))
295 printf("awesome-" VERSION " © 2007 Julien Danjou\n");
296 return EXIT_SUCCESS;
298 else if(!a_strcmp("-c", argv[1]))
300 if(a_strlen(argv[2]))
301 confpath = argv[2];
302 else
303 eprint("awesome: -c require a file\n");
305 else
306 eprint("usage: awesome [-v | -c configfile]\n");
309 /* Tag won't be printed otherwised */
310 setlocale(LC_CTYPE, "");
312 if(!(dpy = XOpenDisplay(NULL)))
313 eprint("awesome: cannot open display\n");
315 xfd = ConnectionNumber(dpy);
317 XSetErrorHandler(xerrorstart);
318 for(screen = 0; screen < ScreenCount(dpy); screen++)
319 /* this causes an error if some other window manager is running */
320 XSelectInput(dpy, RootWindow(dpy, screen), SubstructureRedirectMask);
322 /* need to XSync to validate errorhandler */
323 XSync(dpy, False);
324 XSetErrorHandler(NULL);
325 xerrorxlib = XSetErrorHandler(xerror);
326 XSync(dpy, False);
328 /* allocate stuff */
329 awesomeconf = p_new(awesome_config, get_screen_count(dpy));
330 clients = p_new(Client *, 1);
332 for(screen = 0; screen < get_screen_count(dpy); screen++)
334 /* store display */
335 awesomeconf[screen].display = dpy;
337 /* set screen */
338 awesomeconf[screen].screen = screen;
339 setup_screen(&awesomeconf[screen], confpath);
340 awesomeconf[screen].clients = clients;
341 drawstatusbar(&awesomeconf[screen]);
344 netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
345 netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
347 /* do this only for real screen */
348 for(screen = 0; screen < ScreenCount(dpy); screen++)
350 loadawesomeprops(&awesomeconf[screen]);
351 XChangeProperty(dpy, RootWindow(dpy, screen), netatom[NetSupported],
352 XA_ATOM, 32, PropModeReplace, (unsigned char *) netatom, NetLast);
355 handler = p_new(event_handler *, LASTEvent);
356 handler[ButtonPress] = handle_event_buttonpress;
357 handler[ConfigureRequest] = handle_event_configurerequest;
358 handler[ConfigureNotify] = handle_event_configurenotify;
359 handler[DestroyNotify] = handle_event_destroynotify;
360 handler[EnterNotify] = handle_event_enternotify;
361 handler[LeaveNotify] = handle_event_leavenotify;
362 handler[Expose] = handle_event_expose;
363 handler[KeyPress] = handle_event_keypress;
364 handler[MappingNotify] = handle_event_mappingnotify;
365 handler[MapRequest] = handle_event_maprequest;
366 handler[PropertyNotify] = handle_event_propertynotify;
367 handler[UnmapNotify] = handle_event_unmapnotify;
369 /* check for shape extension */
370 if((awesomeconf[0].have_shape = XShapeQueryExtension(dpy, &shape_event, &e_dummy)))
372 p_realloc(&handler, shape_event + 1);
373 handler[shape_event] = handle_event_shape;
376 /* check for randr extension */
377 if((awesomeconf[0].have_randr = XRRQueryExtension(dpy, &randr_event_base, &e_dummy)))
379 p_realloc(&handler, randr_event_base + RRScreenChangeNotify + 1);
380 handler[randr_event_base + RRScreenChangeNotify] = handle_event_randr_screen_change_notify;
383 for(screen = 0; screen < get_screen_count(dpy); screen++)
385 awesomeconf[screen].have_shape = awesomeconf[0].have_shape;
386 awesomeconf[screen].have_randr = awesomeconf[0].have_randr;
389 scan(awesomeconf);
391 XSync(dpy, False);
393 /* get socket fd */
394 csfd = get_client_socket();
395 addr = get_client_addr(getenv("DISPLAY"));
397 if(bind(csfd, (const struct sockaddr *) addr, SUN_LEN(addr)))
399 if(errno == EADDRINUSE)
401 if(unlink(addr->sun_path))
402 perror("error unlinking existing file");
403 if(bind(csfd, (const struct sockaddr *) addr, SUN_LEN(addr)))
404 perror("error binding UNIX domain socket");
406 else
407 perror("error binding UNIX domain socket");
410 /* main event loop, also reads status text from socket */
411 while(running)
413 FD_ZERO(&rd);
414 if(csfd >= 0)
415 FD_SET(csfd, &rd);
416 FD_SET(xfd, &rd);
417 if(select(MAX(xfd, csfd) + 1, &rd, NULL, NULL, NULL) == -1)
419 if(errno == EINTR)
420 continue;
421 eprint("select failed\n");
423 if(csfd >= 0 && FD_ISSET(csfd, &rd))
424 switch (r = recv(csfd, buf, sizeof(buf)-1, MSG_TRUNC))
426 case -1:
427 perror("awesome: error reading UNIX domain socket");
428 a_strncpy(awesomeconf[0].statustext, sizeof(awesomeconf[0].statustext),
429 strerror(errno), sizeof(awesomeconf[0].statustext) - 1);
430 awesomeconf[0].statustext[sizeof(awesomeconf[0].statustext) - 1] = '\0';
431 csfd = -1;
432 break;
433 case 0:
434 break;
435 default:
436 if(r >= ssizeof(buf))
437 break;
438 buf[r] = '\0';
439 parse_control(buf, awesomeconf);
442 while(XPending(dpy))
444 XNextEvent(dpy, &ev);
445 if(handler[ev.type])
446 handler[ev.type](&ev, awesomeconf); /* call handler */
450 if(csfd > 0 && close(csfd))
451 perror("error closing UNIX domain socket");
452 if(unlink(addr->sun_path))
453 perror("error unlinking UNIX domain socket");
454 p_delete(&addr);
456 cleanup(awesomeconf);
457 XCloseDisplay(dpy);
459 return EXIT_SUCCESS;
461 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99