add Nikos Ntarmos in AUTHORS
[awesome.git] / awesome.c
blob574b6a985b0f857416163275149b63a24a839da7
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;
53 void
54 cleanup_screen(awesome_config *awesomeconf)
56 int i;
57 Button *b, *bn;
58 Key *k, *kn;
59 Rule *r, *rn;
61 XftFontClose(awesomeconf->display, awesomeconf->font);
62 XUngrabKey(awesomeconf->display, AnyKey, AnyModifier, RootWindow(awesomeconf->display, awesomeconf->phys_screen));
63 XDestroyWindow(awesomeconf->display, awesomeconf->statusbar.window);
64 XFreeCursor(awesomeconf->display, awesomeconf->cursor[CurNormal]);
65 XFreeCursor(awesomeconf->display, awesomeconf->cursor[CurResize]);
66 XFreeCursor(awesomeconf->display, awesomeconf->cursor[CurMove]);
68 for(i = 0; i < awesomeconf->ntags; i++)
69 p_delete(&awesomeconf->tags[i].name);
70 for(k = awesomeconf->keys; k; k = kn)
72 kn = k->next;
73 p_delete(&k);
75 for(b = awesomeconf->buttons.tag; b; b = bn)
77 bn = b->next;
78 p_delete(&b);
80 for(b = awesomeconf->buttons.title; b; b = bn)
82 bn = b->next;
83 p_delete(&b->arg);
84 p_delete(&b);
86 for(b = awesomeconf->buttons.layout; b; b = bn)
88 bn = b->next;
89 p_delete(&b->arg);
90 p_delete(&b);
92 for(b = awesomeconf->buttons.root; b; b = bn)
94 bn = b->next;
95 p_delete(&b->arg);
96 p_delete(&b);
98 for(i = 0; i < awesomeconf->nlayouts; i++)
99 p_delete(&awesomeconf->layouts[i].symbol);
100 for(r = awesomeconf->rules; r; r = rn)
102 rn = r->next;
103 p_delete(&r->prop);
104 p_delete(&r->tags);
105 p_delete(&r);
107 p_delete(&awesomeconf->tags);
108 p_delete(&awesomeconf->layouts);
109 p_delete(&awesomeconf->configpath);
112 /** Cleanup everything on quit
113 * \param awesomeconf awesome config
115 static void
116 cleanup(awesome_config *awesomeconf)
118 int screen;
120 while(*awesomeconf->clients)
122 client_unban(*awesomeconf->clients);
123 client_unmanage(*awesomeconf->clients, NormalState, awesomeconf);
126 for(screen = 0; screen < get_screen_count(awesomeconf->display); screen++)
127 cleanup_screen(&awesomeconf[screen]);
129 XSetInputFocus(awesomeconf->display, PointerRoot, RevertToPointerRoot, CurrentTime);
130 XSync(awesomeconf->display, False);
132 p_delete(&awesomeconf->clients);
133 p_delete(&awesomeconf);
136 /** Scan X to find windows to manage
137 * \param screen Screen number
138 * \param awesomeconf awesome config
140 static void
141 scan(awesome_config *awesomeconf)
143 unsigned int i, num;
144 int screen, real_screen;
145 Window *wins = NULL, d1, d2;
146 XWindowAttributes wa;
148 for(screen = 0; screen < ScreenCount(awesomeconf->display); screen++)
150 if(XQueryTree(awesomeconf->display, RootWindow(awesomeconf->display, screen), &d1, &d2, &wins, &num))
152 real_screen = screen;
153 for(i = 0; i < num; i++)
155 if(!XGetWindowAttributes(awesomeconf->display, wins[i], &wa)
156 || wa.override_redirect
157 || XGetTransientForHint(awesomeconf->display, wins[i], &d1))
158 continue;
159 if(wa.map_state == IsViewable || window_getstate(awesomeconf->display, wins[i]) == IconicState)
161 if(screen == 0)
162 real_screen = get_screen_bycoord(awesomeconf->display, wa.x, wa.y);
163 client_manage(wins[i], &wa, &awesomeconf[real_screen]);
166 /* now the transients */
167 for(i = 0; i < num; i++)
169 if(!XGetWindowAttributes(awesomeconf->display, wins[i], &wa))
170 continue;
171 if(XGetTransientForHint(awesomeconf->display, wins[i], &d1)
172 && (wa.map_state == IsViewable || window_getstate(awesomeconf->display, wins[i]) == IconicState))
174 if(screen == 0)
175 real_screen = get_screen_bycoord(awesomeconf->display, wa.x, wa.y);
176 client_manage(wins[i], &wa, &awesomeconf[real_screen]);
180 if(wins)
181 XFree(wins);
185 /** Setup everything before running
186 * \param awesomeconf awesome config ref
187 * \todo clean things...
189 static void
190 setup(awesome_config *awesomeconf)
192 XSetWindowAttributes wa;
194 /* init cursors */
195 awesomeconf->cursor[CurNormal] = XCreateFontCursor(awesomeconf->display, XC_left_ptr);
196 awesomeconf->cursor[CurResize] = XCreateFontCursor(awesomeconf->display, XC_sizing);
197 awesomeconf->cursor[CurMove] = XCreateFontCursor(awesomeconf->display, XC_fleur);
199 /* select for events */
200 wa.event_mask = SubstructureRedirectMask | SubstructureNotifyMask
201 | EnterWindowMask | LeaveWindowMask | StructureNotifyMask;
202 wa.cursor = awesomeconf->cursor[CurNormal];
204 XChangeWindowAttributes(awesomeconf->display, RootWindow(awesomeconf->display, awesomeconf->phys_screen), CWEventMask | CWCursor, &wa);
206 XSelectInput(awesomeconf->display, RootWindow(awesomeconf->display, awesomeconf->phys_screen), wa.event_mask);
208 grabkeys(awesomeconf);
211 void
212 setup_screen(awesome_config *awesomeconf, const char *confpath)
214 parse_config(confpath, awesomeconf);
215 setup(awesomeconf);
216 initstatusbar(awesomeconf->display, awesomeconf->screen, &awesomeconf->statusbar,
217 awesomeconf->cursor[CurNormal], awesomeconf->font,
218 awesomeconf->layouts, awesomeconf->nlayouts);
221 /** Startup Error handler to check if another window manager
222 * is already running.
223 * \param disp Display ref
224 * \param ee Error event
226 static int __attribute__ ((noreturn))
227 xerrorstart(Display * disp __attribute__ ((unused)), XErrorEvent * ee __attribute__ ((unused)))
229 eprint("awesome: another window manager is already running\n");
232 /** Quit awesome
233 * \param awesomeconf awesome config
234 * \param arg nothing
235 * \ingroup ui_callback
237 void
238 uicb_quit(awesome_config *awesomeconf __attribute__((unused)),
239 const char *arg __attribute__ ((unused)))
241 running = False;
244 /* There's no way to check accesses to destroyed windows, thus those cases are
245 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
246 * default error handler, which may call exit.
249 xerror(Display * edpy, XErrorEvent * ee)
251 if(ee->error_code == BadWindow
252 || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
253 || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
254 || (ee->request_code == X_PolyFillRectangle
255 && ee->error_code == BadDrawable)
256 || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
257 || (ee->request_code == X_ConfigureWindow
258 && ee->error_code == BadMatch) || (ee->request_code == X_GrabKey
259 && ee->error_code == BadAccess)
260 || (ee->request_code == X_CopyArea && ee->error_code == BadDrawable))
261 return 0;
262 fprintf(stderr, "awesome: fatal error: request code=%d, error code=%d\n",
263 ee->request_code, ee->error_code);
265 return xerrorxlib(edpy, ee); /* may call exit */
268 /** Hello, this is main
269 * \param argc who knows
270 * \param argv who knows
271 * \return EXIT_SUCCESS I hope
273 typedef void event_handler (XEvent *, awesome_config *);
275 main(int argc, char *argv[])
277 char buf[1024];
278 const char *confpath = NULL;
279 int r, xfd, e_dummy, csfd;
280 fd_set rd;
281 XEvent ev;
282 Display * dpy;
283 awesome_config *awesomeconf;
284 int shape_event, randr_event_base;
285 int screen;
286 enum { NetSupported, NetWMName, NetLast }; /* EWMH atoms */
287 Atom netatom[NetLast];
288 event_handler **handler;
289 Client **clients;
290 struct sockaddr_un *addr;
292 if(argc >= 2)
294 if(!a_strcmp("-v", argv[1]))
296 printf("awesome-" VERSION " © 2007 Julien Danjou\n");
297 return EXIT_SUCCESS;
299 else if(!a_strcmp("-c", argv[1]))
301 if(a_strlen(argv[2]))
302 confpath = argv[2];
303 else
304 eprint("awesome: -c require a file\n");
306 else
307 eprint("usage: awesome [-v | -c configfile]\n");
310 /* Tag won't be printed otherwised */
311 setlocale(LC_CTYPE, "");
313 if(!(dpy = XOpenDisplay(NULL)))
314 eprint("awesome: cannot open display\n");
316 xfd = ConnectionNumber(dpy);
318 XSetErrorHandler(xerrorstart);
319 for(screen = 0; screen < ScreenCount(dpy); screen++)
320 /* this causes an error if some other window manager is running */
321 XSelectInput(dpy, RootWindow(dpy, screen), SubstructureRedirectMask);
323 /* need to XSync to validate errorhandler */
324 XSync(dpy, False);
325 XSetErrorHandler(NULL);
326 xerrorxlib = XSetErrorHandler(xerror);
327 XSync(dpy, False);
329 /* allocate stuff */
330 awesomeconf = p_new(awesome_config, get_screen_count(dpy));
331 clients = p_new(Client *, 1);
333 for(screen = 0; screen < get_screen_count(dpy); screen++)
335 /* store display */
336 awesomeconf[screen].display = dpy;
338 /* set screen */
339 awesomeconf[screen].screen = screen;
340 setup_screen(&awesomeconf[screen], confpath);
341 awesomeconf[screen].clients = clients;
342 drawstatusbar(&awesomeconf[screen]);
345 netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
346 netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
348 /* do this only for real screen */
349 for(screen = 0; screen < ScreenCount(dpy); screen++)
351 loadawesomeprops(&awesomeconf[screen]);
352 XChangeProperty(dpy, RootWindow(dpy, screen), netatom[NetSupported],
353 XA_ATOM, 32, PropModeReplace, (unsigned char *) netatom, NetLast);
356 handler = p_new(event_handler *, LASTEvent);
357 handler[ButtonPress] = handle_event_buttonpress;
358 handler[ConfigureRequest] = handle_event_configurerequest;
359 handler[ConfigureNotify] = handle_event_configurenotify;
360 handler[DestroyNotify] = handle_event_destroynotify;
361 handler[EnterNotify] = handle_event_enternotify;
362 handler[LeaveNotify] = handle_event_leavenotify;
363 handler[Expose] = handle_event_expose;
364 handler[KeyPress] = handle_event_keypress;
365 handler[MappingNotify] = handle_event_mappingnotify;
366 handler[MapRequest] = handle_event_maprequest;
367 handler[PropertyNotify] = handle_event_propertynotify;
368 handler[UnmapNotify] = handle_event_unmapnotify;
370 /* check for shape extension */
371 if((awesomeconf[0].have_shape = XShapeQueryExtension(dpy, &shape_event, &e_dummy)))
373 p_realloc(&handler, shape_event + 1);
374 handler[shape_event] = handle_event_shape;
377 /* check for randr extension */
378 if((awesomeconf[0].have_randr = XRRQueryExtension(dpy, &randr_event_base, &e_dummy)))
380 p_realloc(&handler, randr_event_base + RRScreenChangeNotify + 1);
381 handler[randr_event_base + RRScreenChangeNotify] = handle_event_randr_screen_change_notify;
384 for(screen = 0; screen < get_screen_count(dpy); screen++)
386 awesomeconf[screen].have_shape = awesomeconf[0].have_shape;
387 awesomeconf[screen].have_randr = awesomeconf[0].have_randr;
390 scan(awesomeconf);
392 XSync(dpy, False);
394 /* get socket fd */
395 csfd = get_client_socket();
396 addr = get_client_addr(getenv("DISPLAY"));
398 if(bind(csfd, (const struct sockaddr *) addr, SUN_LEN(addr)))
400 if(errno == EADDRINUSE)
402 if(unlink(addr->sun_path))
403 perror("error unlinking existing file");
404 if(bind(csfd, (const struct sockaddr *) addr, SUN_LEN(addr)))
405 perror("error binding UNIX domain socket");
407 else
408 perror("error binding UNIX domain socket");
411 /* main event loop, also reads status text from socket */
412 while(running)
414 FD_ZERO(&rd);
415 if(csfd >= 0)
416 FD_SET(csfd, &rd);
417 FD_SET(xfd, &rd);
418 if(select(MAX(xfd, csfd) + 1, &rd, NULL, NULL, NULL) == -1)
420 if(errno == EINTR)
421 continue;
422 eprint("select failed\n");
424 if(csfd >= 0 && FD_ISSET(csfd, &rd))
425 switch (r = recv(csfd, buf, sizeof(buf)-1, MSG_TRUNC))
427 case -1:
428 perror("awesome: error reading UNIX domain socket");
429 a_strncpy(awesomeconf[0].statustext, sizeof(awesomeconf[0].statustext),
430 strerror(errno), sizeof(awesomeconf[0].statustext) - 1);
431 awesomeconf[0].statustext[sizeof(awesomeconf[0].statustext) - 1] = '\0';
432 csfd = -1;
433 break;
434 case 0:
435 break;
436 default:
437 if(r >= ssizeof(buf))
438 break;
439 buf[r] = '\0';
440 parse_control(buf, awesomeconf);
443 while(XPending(dpy))
445 XNextEvent(dpy, &ev);
446 if(handler[ev.type])
447 handler[ev.type](&ev, awesomeconf); /* call handler */
451 if(csfd > 0 && close(csfd))
452 perror("error closing UNIX domain socket");
453 if(unlink(addr->sun_path))
454 perror("error unlinking UNIX domain socket");
455 p_delete(&addr);
457 cleanup(awesomeconf);
458 XCloseDisplay(dpy);
460 return EXIT_SUCCESS;
462 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99