add support for NET_WM_STATE and NET_WM_STATE_STICKY
[awesome.git] / awesome.c
blobcee7f94b512a30e65936c368c970caa73aea58c4
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 <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);
121 p_delete(&r->tags);
122 p_delete(&r->propregex);
123 p_delete(&r->tagregex);
124 p_delete(&r);
127 for(k = globalconf.keys; k; k = kn)
129 kn = k->next;
130 p_delete(&k->arg);
131 p_delete(&k);
134 cleanup_buttons(globalconf.buttons.root);
135 cleanup_buttons(globalconf.buttons.client);
137 p_delete(&globalconf.configpath);
139 for(screen = 0; screen < get_screen_count(); screen++)
140 cleanup_screen(screen);
142 XSetInputFocus(globalconf.display, PointerRoot, RevertToPointerRoot, CurrentTime);
143 XSync(globalconf.display, False);
145 p_delete(&globalconf.clients);
148 /** Scan X to find windows to manage
150 static void
151 scan()
153 unsigned int i, num;
154 int screen, real_screen;
155 Window *wins = NULL, d1, d2;
156 XWindowAttributes wa;
158 for(screen = 0; screen < ScreenCount(globalconf.display); screen++)
160 if(XQueryTree(globalconf.display, RootWindow(globalconf.display, screen), &d1, &d2, &wins, &num))
162 real_screen = screen;
163 for(i = 0; i < num; i++)
165 if(!XGetWindowAttributes(globalconf.display, wins[i], &wa)
166 || wa.override_redirect
167 || XGetTransientForHint(globalconf.display, wins[i], &d1))
168 continue;
169 if(wa.map_state == IsViewable || window_getstate(globalconf.display, wins[i]) == IconicState)
171 if(screen == 0)
172 real_screen = get_screen_bycoord(wa.x, wa.y);
173 client_manage(wins[i], &wa, real_screen);
176 /* now the transients */
177 for(i = 0; i < num; i++)
179 if(!XGetWindowAttributes(globalconf.display, wins[i], &wa))
180 continue;
181 if(XGetTransientForHint(globalconf.display, wins[i], &d1)
182 && (wa.map_state == IsViewable || window_getstate(globalconf.display, wins[i]) == IconicState))
184 if(screen == 0)
185 real_screen = get_screen_bycoord(wa.x, wa.y);
186 client_manage(wins[i], &wa, real_screen);
190 if(wins)
191 XFree(wins);
195 /** Setup everything before running
196 * \param screen Screen number
197 * \todo clean things...
199 static void
200 setup(int screen)
202 XSetWindowAttributes wa;
204 /* init cursors */
205 globalconf.cursor[CurNormal] = XCreateFontCursor(globalconf.display, XC_left_ptr);
206 globalconf.cursor[CurResize] = XCreateFontCursor(globalconf.display, XC_sizing);
207 globalconf.cursor[CurMove] = XCreateFontCursor(globalconf.display, XC_fleur);
209 /* select for events */
210 wa.event_mask = SubstructureRedirectMask | SubstructureNotifyMask
211 | EnterWindowMask | LeaveWindowMask | StructureNotifyMask;
212 wa.cursor = globalconf.cursor[CurNormal];
214 XChangeWindowAttributes(globalconf.display,
215 RootWindow(globalconf.display, get_phys_screen(screen)),
216 CWEventMask | CWCursor, &wa);
218 XSelectInput(globalconf.display,
219 RootWindow(globalconf.display, get_phys_screen(screen)),
220 wa.event_mask);
222 grabkeys(get_phys_screen(screen));
225 /** Startup Error handler to check if another window manager
226 * is already running.
227 * \param disp Display
228 * \param ee Error event
230 static int __attribute__ ((noreturn))
231 xerrorstart(Display * disp __attribute__ ((unused)),
232 XErrorEvent * ee __attribute__ ((unused)))
234 eprint("another window manager is already running\n");
237 /** Quit awesome
238 * \param screen Screen ID
239 * \param arg nothing
240 * \ingroup ui_callback
242 void
243 uicb_quit(int screen __attribute__ ((unused)), char *arg __attribute__ ((unused)))
245 running = False;
248 static void
249 exit_on_signal(int sig __attribute__ ((unused)))
251 running = False;
254 /* There's no way to check accesses to destroyed windows, thus those cases are
255 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
256 * default error handler, which may call exit.
259 xerror(Display * edpy, XErrorEvent * ee)
261 if(ee->error_code == BadWindow
262 || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
263 || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
264 || (ee->request_code == X_PolyFillRectangle
265 && ee->error_code == BadDrawable)
266 || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
267 || (ee->request_code == X_ConfigureWindow
268 && ee->error_code == BadMatch) || (ee->request_code == X_GrabKey
269 && ee->error_code == BadAccess)
270 || (ee->request_code == X_CopyArea && ee->error_code == BadDrawable))
271 return 0;
272 warn("fatal error: request code=%d, error code=%d\n", ee->request_code, ee->error_code);
273 return xerrorxlib(edpy, ee); /* may call exit */
276 /** Hello, this is main
277 * \param argc who knows
278 * \param argv who knows
279 * \return EXIT_SUCCESS I hope
281 typedef void event_handler (XEvent *);
283 main(int argc, char *argv[])
285 char buf[1024];
286 const char *confpath = NULL;
287 int r, xfd, e_dummy, csfd;
288 fd_set rd;
289 XEvent ev;
290 Display * dpy;
291 int shape_event, randr_event_base;
292 int screen;
293 event_handler **handler;
294 struct sockaddr_un *addr;
296 if(argc >= 2)
298 if(!a_strcmp("-v", argv[1]) || !a_strcmp("--version", argv[1]))
300 printf("awesome-" VERSION " (" RELEASE ")\n");
301 return EXIT_SUCCESS;
303 else if(!a_strcmp("-c", argv[1]))
305 if(a_strlen(argv[2]))
306 confpath = argv[2];
307 else
308 eprint("-c require a file\n");
310 else
311 eprint("options: [-v | -c configfile]\n");
314 /* Tag won't be printed otherwised */
315 setlocale(LC_CTYPE, "");
317 if(!(dpy = XOpenDisplay(NULL)))
318 eprint("cannot open display\n");
320 xfd = ConnectionNumber(dpy);
322 XSetErrorHandler(xerrorstart);
323 for(screen = 0; screen < ScreenCount(dpy); screen++)
324 /* this causes an error if some other window manager is running */
325 XSelectInput(dpy, RootWindow(dpy, screen), SubstructureRedirectMask);
327 /* need to XSync to validate errorhandler */
328 XSync(dpy, False);
329 XSetErrorHandler(NULL);
330 xerrorxlib = XSetErrorHandler(xerror);
331 XSync(dpy, False);
332 globalconf.display = dpy;
334 ewmh_init_atoms();
336 globalconf.screens = p_new(VirtScreen, get_screen_count());
337 focus_add_client(NULL);
338 /* store display */
339 config_parse(confpath);
341 for(screen = 0; screen < get_screen_count(); screen++)
343 /* set screen */
344 setup(screen);
345 statusbar_init(screen);
346 statusbar_draw(screen);
349 /* do this only for real screen */
350 for(screen = 0; screen < ScreenCount(dpy); screen++)
352 loadawesomeprops(screen);
353 ewmh_set_supported_hints(screen);
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;
369 handler[ClientMessage] = handle_event_clientmessage;
371 /* check for shape extension */
372 if((globalconf.have_shape = XShapeQueryExtension(dpy, &shape_event, &e_dummy)))
374 p_realloc(&handler, shape_event + 1);
375 handler[shape_event] = handle_event_shape;
378 /* check for randr extension */
379 if((globalconf.have_randr = XRRQueryExtension(dpy, &randr_event_base, &e_dummy)))
381 p_realloc(&handler, randr_event_base + RRScreenChangeNotify + 1);
382 handler[randr_event_base + RRScreenChangeNotify] = handle_event_randr_screen_change_notify;
385 scan();
387 XSync(dpy, False);
389 /* get socket fd */
390 csfd = get_client_socket();
391 addr = get_client_addr(getenv("DISPLAY"));
393 if(bind(csfd, (const struct sockaddr *) addr, SUN_LEN(addr)))
395 if(errno == EADDRINUSE)
397 if(unlink(addr->sun_path))
398 perror("error unlinking existing file");
399 if(bind(csfd, (const struct sockaddr *) addr, SUN_LEN(addr)))
400 perror("error binding UNIX domain socket");
402 else
403 perror("error binding UNIX domain socket");
406 /* register function for signals */
407 signal(SIGINT, &exit_on_signal);
408 signal(SIGTERM, &exit_on_signal);
409 signal(SIGHUP, &exit_on_signal);
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 csfd = -1;
430 break;
431 case 0:
432 break;
433 default:
434 if(r >= ssizeof(buf))
435 break;
436 buf[r] = '\0';
437 parse_control(buf);
440 while(XPending(dpy))
442 XNextEvent(dpy, &ev);
443 if(handler[ev.type])
444 handler[ev.type](&ev); /* call handler */
448 if(csfd > 0 && close(csfd))
449 perror("error closing UNIX domain socket");
450 if(unlink(addr->sun_path))
451 perror("error unlinking UNIX domain socket");
452 p_delete(&addr);
454 cleanup();
455 XCloseDisplay(dpy);
457 return EXIT_SUCCESS;
459 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80