use Area in Widget
[awesome.git] / awesome.c
blob78eba872a19e4457d6f8d29e09e5196fc8101ed8
1 /*
2 * awesome.c - awesome main functions
4 * Copyright © 2007-2008 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 <signal.h>
33 #include <X11/cursorfont.h>
34 #include <X11/keysym.h>
35 #include <X11/Xatom.h>
36 #include <X11/Xproto.h>
37 #include <X11/Xutil.h>
38 #include <X11/extensions/shape.h>
39 #include <X11/extensions/Xrandr.h>
41 #include "awesome.h"
42 #include "event.h"
43 #include "layout.h"
44 #include "screen.h"
45 #include "util.h"
46 #include "statusbar.h"
47 #include "uicb.h"
48 #include "window.h"
49 #include "client.h"
50 #include "focus.h"
51 #include "ewmh.h"
52 #include "awesome-client.h"
54 static int (*xerrorxlib) (Display *, XErrorEvent *);
55 static Bool running = True;
57 AwesomeConf globalconf;
59 static inline void
60 cleanup_buttons(Button *buttons)
62 Button *b, *bn;
64 for(b = buttons; b; b = bn)
66 bn = b->next;
67 p_delete(&b->arg);
68 p_delete(&b);
72 static void
73 cleanup_screen(int screen)
75 Layout *l, *ln;
76 Tag *t, *tn;
78 XftFontClose(globalconf.display, globalconf.screens[screen].font);
79 XUngrabKey(globalconf.display, AnyKey, AnyModifier, RootWindow(globalconf.display, get_phys_screen(screen)));
80 XDestroyWindow(globalconf.display, globalconf.screens[screen].statusbar->window);
82 for(t = globalconf.screens[screen].tags; t; t = tn)
84 tn = t->next;
85 p_delete(&t->name);
86 p_delete(&t);
89 for(l = globalconf.screens[screen].layouts; l; l = ln)
91 ln = l->next;
92 p_delete(&l->image);
93 p_delete(&l);
97 /** Cleanup everything on quit
98 * \param awesomeconf awesome config
100 static void
101 cleanup()
103 int screen;
104 Rule *r, *rn;
105 Key *k, *kn;
107 while(globalconf.clients)
109 client_unban(globalconf.clients);
110 client_unmanage(globalconf.clients, NormalState);
113 XFreeCursor(globalconf.display, globalconf.cursor[CurNormal]);
114 XFreeCursor(globalconf.display, globalconf.cursor[CurResize]);
115 XFreeCursor(globalconf.display, globalconf.cursor[CurMove]);
117 for(r = globalconf.rules; r; r = rn)
119 rn = r->next;
120 p_delete(&r->prop_r);
121 p_delete(&r->tags_r);
122 p_delete(&r->xpropval_r);
123 p_delete(&r->icon);
124 p_delete(&r->xprop);
125 p_delete(&r);
128 for(k = globalconf.keys; k; k = kn)
130 kn = k->next;
131 p_delete(&k->arg);
132 p_delete(&k);
135 cleanup_buttons(globalconf.buttons.root);
136 cleanup_buttons(globalconf.buttons.client);
138 p_delete(&globalconf.configpath);
140 for(screen = 0; screen < get_screen_count(); screen++)
141 cleanup_screen(screen);
143 XSetInputFocus(globalconf.display, PointerRoot, RevertToPointerRoot, CurrentTime);
144 XSync(globalconf.display, False);
146 p_delete(&globalconf.clients);
149 /** Scan X to find windows to manage
151 static void
152 scan()
154 unsigned int i, num;
155 int screen, real_screen;
156 Window *wins = NULL, d1, d2;
157 XWindowAttributes wa;
159 for(screen = 0; screen < ScreenCount(globalconf.display); screen++)
161 if(XQueryTree(globalconf.display, RootWindow(globalconf.display, screen), &d1, &d2, &wins, &num))
163 real_screen = screen;
164 for(i = 0; i < num; i++)
166 if(!XGetWindowAttributes(globalconf.display, wins[i], &wa)
167 || wa.override_redirect
168 || XGetTransientForHint(globalconf.display, wins[i], &d1))
169 continue;
170 if(wa.map_state == IsViewable || window_getstate(wins[i]) == IconicState)
172 if(screen == 0)
173 real_screen = get_screen_bycoord(wa.x, wa.y);
174 client_manage(wins[i], &wa, real_screen);
177 /* now the transients */
178 for(i = 0; i < num; i++)
180 if(!XGetWindowAttributes(globalconf.display, wins[i], &wa))
181 continue;
182 if(XGetTransientForHint(globalconf.display, wins[i], &d1)
183 && (wa.map_state == IsViewable || window_getstate(wins[i]) == IconicState))
185 if(screen == 0)
186 real_screen = get_screen_bycoord(wa.x, wa.y);
187 client_manage(wins[i], &wa, real_screen);
191 if(wins)
192 XFree(wins);
196 /** Setup everything before running
197 * \param screen Screen number
198 * \todo clean things...
200 static void
201 setup(int screen)
203 XSetWindowAttributes wa;
204 Statusbar *statusbar;
205 int phys_screen = get_phys_screen(screen);
207 /* init cursors */
208 globalconf.cursor[CurNormal] = XCreateFontCursor(globalconf.display, XC_left_ptr);
209 globalconf.cursor[CurResize] = XCreateFontCursor(globalconf.display, XC_sizing);
210 globalconf.cursor[CurMove] = XCreateFontCursor(globalconf.display, XC_fleur);
212 /* select for events */
213 wa.event_mask = SubstructureRedirectMask | SubstructureNotifyMask
214 | EnterWindowMask | LeaveWindowMask | StructureNotifyMask;
215 wa.cursor = globalconf.cursor[CurNormal];
217 XChangeWindowAttributes(globalconf.display,
218 RootWindow(globalconf.display, phys_screen),
219 CWEventMask | CWCursor, &wa);
221 XSelectInput(globalconf.display,
222 RootWindow(globalconf.display, phys_screen),
223 wa.event_mask);
225 grabkeys(phys_screen);
227 for(statusbar = globalconf.screens[screen].statusbar; statusbar; statusbar = statusbar->next)
228 statusbar_init(statusbar, screen);
231 /** Startup Error handler to check if another window manager
232 * is already running.
233 * \param disp Display
234 * \param ee Error event
236 static int __attribute__ ((noreturn))
237 xerrorstart(Display * disp __attribute__ ((unused)),
238 XErrorEvent * ee __attribute__ ((unused)))
240 eprint("another window manager is already running\n");
243 /** Quit awesome
244 * \param screen Screen ID
245 * \param arg nothing
246 * \ingroup ui_callback
248 void
249 uicb_quit(int screen __attribute__ ((unused)), char *arg __attribute__ ((unused)))
251 running = False;
254 static void
255 exit_on_signal(int sig __attribute__ ((unused)))
257 running = False;
260 /* There's no way to check accesses to destroyed windows, thus those cases are
261 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
262 * default error handler, which may call exit.
265 xerror(Display * edpy, XErrorEvent * ee)
267 if(ee->error_code == BadWindow
268 || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
269 || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
270 || (ee->request_code == X_PolyFillRectangle
271 && ee->error_code == BadDrawable)
272 || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
273 || (ee->request_code == X_ConfigureWindow
274 && ee->error_code == BadMatch) || (ee->request_code == X_GrabKey
275 && ee->error_code == BadAccess)
276 || (ee->request_code == X_CopyArea && ee->error_code == BadDrawable))
277 return 0;
278 warn("fatal error: request code=%d, error code=%d\n", ee->request_code, ee->error_code);
279 return xerrorxlib(edpy, ee); /* may call exit */
282 /** Hello, this is main
283 * \param argc who knows
284 * \param argv who knows
285 * \return EXIT_SUCCESS I hope
287 typedef void event_handler (XEvent *);
289 main(int argc, char *argv[])
291 char buf[1024];
292 const char *confpath = NULL;
293 int r, xfd, e_dummy, csfd;
294 fd_set rd;
295 XEvent ev;
296 Display * dpy;
297 int shape_event, randr_event_base;
298 int screen;
299 event_handler **handler;
300 struct sockaddr_un *addr;
302 /* check args */
303 if(argc >= 2)
305 if(!a_strcmp("-v", argv[1]) || !a_strcmp("--version", argv[1]))
307 printf("awesome " VERSION " (" RELEASE ")\n");
308 return EXIT_SUCCESS;
310 else if(!a_strcmp("-c", argv[1]))
312 if(a_strlen(argv[2]))
313 confpath = argv[2];
314 else
315 eprint("-c require a file\n");
317 else
318 eprint("options: [-v | -c configfile]\n");
321 /* X stuff */
322 if(!(dpy = XOpenDisplay(NULL)))
323 eprint("cannot open display\n");
325 xfd = ConnectionNumber(dpy);
327 XSetErrorHandler(xerrorstart);
328 for(screen = 0; screen < ScreenCount(dpy); screen++)
329 /* this causes an error if some other window manager is running */
330 XSelectInput(dpy, RootWindow(dpy, screen), SubstructureRedirectMask);
332 /* need to XSync to validate errorhandler */
333 XSync(dpy, False);
334 XSetErrorHandler(NULL);
335 xerrorxlib = XSetErrorHandler(xerror);
336 XSync(dpy, False);
338 /* store display */
339 globalconf.display = dpy;
341 /* init EWMH atoms */
342 ewmh_init_atoms();
344 /* init screens struct */
345 globalconf.screens = p_new(VirtScreen, get_screen_count());
346 focus_add_client(NULL);
348 /* parse config */
349 config_parse(confpath);
351 /* for each virtual screen */
352 for(screen = 0; screen < get_screen_count(); screen++)
353 setup(screen);
355 /* do this only for real screen */
356 for(screen = 0; screen < ScreenCount(dpy); screen++)
358 loadawesomeprops(screen);
359 ewmh_set_supported_hints(screen);
362 handler = p_new(event_handler *, LASTEvent);
363 handler[ButtonPress] = handle_event_buttonpress;
364 handler[ConfigureRequest] = handle_event_configurerequest;
365 handler[ConfigureNotify] = handle_event_configurenotify;
366 handler[DestroyNotify] = handle_event_destroynotify;
367 handler[EnterNotify] = handle_event_enternotify;
368 handler[LeaveNotify] = handle_event_leavenotify;
369 handler[Expose] = handle_event_expose;
370 handler[KeyPress] = handle_event_keypress;
371 handler[MappingNotify] = handle_event_mappingnotify;
372 handler[MapRequest] = handle_event_maprequest;
373 handler[PropertyNotify] = handle_event_propertynotify;
374 handler[UnmapNotify] = handle_event_unmapnotify;
375 handler[ClientMessage] = handle_event_clientmessage;
377 /* check for shape extension */
378 if((globalconf.have_shape = XShapeQueryExtension(dpy, &shape_event, &e_dummy)))
380 p_realloc(&handler, shape_event + 1);
381 handler[shape_event] = handle_event_shape;
384 /* check for randr extension */
385 if((globalconf.have_randr = XRRQueryExtension(dpy, &randr_event_base, &e_dummy)))
387 p_realloc(&handler, randr_event_base + RRScreenChangeNotify + 1);
388 handler[randr_event_base + RRScreenChangeNotify] = handle_event_randr_screen_change_notify;
391 scan();
393 XSync(dpy, False);
395 /* get socket fd */
396 csfd = get_client_socket();
397 addr = get_client_addr(getenv("DISPLAY"));
399 if(bind(csfd, (const struct sockaddr *) addr, SUN_LEN(addr)))
401 if(errno == EADDRINUSE)
403 if(unlink(addr->sun_path))
404 perror("error unlinking existing file");
405 if(bind(csfd, (const struct sockaddr *) addr, SUN_LEN(addr)))
406 perror("error binding UNIX domain socket");
408 else
409 perror("error binding UNIX domain socket");
412 /* register function for signals */
413 signal(SIGINT, &exit_on_signal);
414 signal(SIGTERM, &exit_on_signal);
415 signal(SIGHUP, &exit_on_signal);
417 /* main event loop, also reads status text from socket */
418 while(running)
420 FD_ZERO(&rd);
421 if(csfd >= 0)
422 FD_SET(csfd, &rd);
423 FD_SET(xfd, &rd);
424 if(select(MAX(xfd, csfd) + 1, &rd, NULL, NULL, NULL) == -1)
426 if(errno == EINTR)
427 continue;
428 eprint("select failed\n");
430 if(csfd >= 0 && FD_ISSET(csfd, &rd))
431 switch (r = recv(csfd, buf, sizeof(buf)-1, MSG_TRUNC))
433 case -1:
434 perror("awesome: error reading UNIX domain socket");
435 csfd = -1;
436 break;
437 case 0:
438 break;
439 default:
440 if(r >= ssizeof(buf))
441 break;
442 buf[r] = '\0';
443 parse_control(buf);
446 while(XPending(dpy))
448 XNextEvent(dpy, &ev);
449 if(handler[ev.type])
450 handler[ev.type](&ev); /* call handler */
454 if(csfd > 0 && close(csfd))
455 perror("error closing UNIX domain socket");
456 if(unlink(addr->sun_path))
457 perror("error unlinking UNIX domain socket");
458 p_delete(&addr);
460 cleanup();
461 XCloseDisplay(dpy);
463 return EXIT_SUCCESS;
465 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80