invalidate cache
[awesome.git] / awesome.c
blob5ee238382c657805220398dfee60d38b1c2fe41e
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 /** Scan X to find windows to manage
61 static void
62 scan()
64 unsigned int i, num;
65 int screen, real_screen;
66 Window *wins = NULL, d1, d2;
67 XWindowAttributes wa;
69 for(screen = 0; screen < ScreenCount(globalconf.display); screen++)
71 if(XQueryTree(globalconf.display, RootWindow(globalconf.display, screen), &d1, &d2, &wins, &num))
73 real_screen = screen;
74 for(i = 0; i < num; i++)
76 /* XGetWindowAttributes return 1 on success */
77 if(!XGetWindowAttributes(globalconf.display, wins[i], &wa)
78 || wa.override_redirect
79 || XGetTransientForHint(globalconf.display, wins[i], &d1))
80 continue;
81 if(wa.map_state == IsViewable || window_getstate(wins[i]) == IconicState)
83 if(screen == 0)
84 real_screen = get_screen_bycoord(wa.x, wa.y);
85 client_manage(wins[i], &wa, real_screen);
88 /* now the transients */
89 for(i = 0; i < num; i++)
91 if(!XGetWindowAttributes(globalconf.display, wins[i], &wa))
92 continue;
93 if(XGetTransientForHint(globalconf.display, wins[i], &d1)
94 && (wa.map_state == IsViewable || window_getstate(wins[i]) == IconicState))
96 if(screen == 0)
97 real_screen = get_screen_bycoord(wa.x, wa.y);
98 client_manage(wins[i], &wa, real_screen);
102 if(wins)
103 XFree(wins);
107 /** Setup everything before running
108 * \param screen Screen number
109 * \todo clean things...
111 static void
112 setup(int screen)
114 XSetWindowAttributes wa;
115 Statusbar *statusbar;
116 int phys_screen = get_phys_screen(screen);
118 /* init cursors */
119 globalconf.cursor[CurNormal] = XCreateFontCursor(globalconf.display, XC_left_ptr);
120 globalconf.cursor[CurResize] = XCreateFontCursor(globalconf.display, XC_sizing);
121 globalconf.cursor[CurMove] = XCreateFontCursor(globalconf.display, XC_fleur);
123 /* select for events */
124 wa.event_mask = SubstructureRedirectMask | SubstructureNotifyMask
125 | EnterWindowMask | LeaveWindowMask | StructureNotifyMask;
126 wa.cursor = globalconf.cursor[CurNormal];
128 XChangeWindowAttributes(globalconf.display,
129 RootWindow(globalconf.display, phys_screen),
130 CWEventMask | CWCursor, &wa);
132 XSelectInput(globalconf.display,
133 RootWindow(globalconf.display, phys_screen),
134 wa.event_mask);
136 grabkeys(phys_screen);
138 for(statusbar = globalconf.screens[screen].statusbar; statusbar; statusbar = statusbar->next)
139 statusbar_init(statusbar, screen);
142 /** Startup Error handler to check if another window manager
143 * is already running.
144 * \param disp Display
145 * \param ee Error event
147 static int __attribute__ ((noreturn))
148 xerrorstart(Display * disp __attribute__ ((unused)),
149 XErrorEvent * ee __attribute__ ((unused)))
151 eprint("another window manager is already running\n");
154 /** Quit awesome
155 * \param screen Screen ID
156 * \param arg nothing
157 * \ingroup ui_callback
159 void
160 uicb_quit(int screen __attribute__ ((unused)), char *arg __attribute__ ((unused)))
162 running = False;
165 static void
166 exit_on_signal(int sig __attribute__ ((unused)))
168 running = False;
171 /* There's no way to check accesses to destroyed windows, thus those cases are
172 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
173 * default error handler, which may call exit.
175 static int
176 xerror(Display * edpy, XErrorEvent * ee)
178 if(ee->error_code == BadWindow
179 || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
180 || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
181 || (ee->request_code == X_PolyFillRectangle
182 && ee->error_code == BadDrawable)
183 || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
184 || (ee->request_code == X_ConfigureWindow
185 && ee->error_code == BadMatch) || (ee->request_code == X_GrabKey
186 && ee->error_code == BadAccess)
187 || (ee->request_code == X_CopyArea && ee->error_code == BadDrawable))
188 return 0;
189 warn("fatal error: request code=%d, error code=%d\n", ee->request_code, ee->error_code);
190 return xerrorxlib(edpy, ee); /* may call exit */
193 /** Hello, this is main
194 * \param argc who knows
195 * \param argv who knows
196 * \return EXIT_SUCCESS I hope
198 typedef void event_handler (XEvent *);
200 main(int argc, char *argv[])
202 char buf[1024];
203 const char *confpath = NULL;
204 int r, xfd, e_dummy, csfd;
205 fd_set rd;
206 XEvent ev;
207 Display * dpy;
208 int shape_event, randr_event_base;
209 int screen, screen_count;
210 int i, cmdlen;
211 event_handler **handler;
212 struct sockaddr_un *addr;
214 /* check args */
215 if(argc >= 2)
217 if(!a_strcmp("-v", argv[1]) || !a_strcmp("--version", argv[1]))
219 printf("awesome " VERSION " (" RELEASE ")\n");
220 return EXIT_SUCCESS;
222 else if(!a_strcmp("-c", argv[1]))
224 if(a_strlen(argv[2]))
225 confpath = argv[2];
226 else
227 eprint("-c require a file\n");
229 else
230 eprint("options: [-v | -c configfile]\n");
233 /* Text won't be printed correctly otherwise */
234 setlocale(LC_CTYPE, "");
236 /* X stuff */
237 if(!(dpy = XOpenDisplay(NULL)))
238 eprint("cannot open display\n");
240 for(cmdlen = 0, i = 0; i < argc; i++)
241 cmdlen += a_strlen(argv[i] + 1);
242 globalconf.argv = p_new(char, cmdlen);
243 a_strcpy(globalconf.argv, cmdlen, argv[0]);
244 for(i = 1; i < argc; i++)
246 a_strcat(globalconf.argv, cmdlen, " ");
247 a_strcat(globalconf.argv, cmdlen, argv[i]);
250 xfd = ConnectionNumber(dpy);
252 XSetErrorHandler(xerrorstart);
253 for(screen = 0; screen < ScreenCount(dpy); screen++)
254 /* this causes an error if some other window manager is running */
255 XSelectInput(dpy, RootWindow(dpy, screen), SubstructureRedirectMask);
257 /* need to XSync to validate errorhandler */
258 XSync(dpy, False);
259 XSetErrorHandler(NULL);
260 xerrorxlib = XSetErrorHandler(xerror);
261 XSync(dpy, False);
263 /* store display */
264 globalconf.display = dpy;
266 /* init EWMH atoms */
267 ewmh_init_atoms();
269 /* init screens struct */
270 screen_count = get_screen_count();
271 globalconf.screens = p_new(VirtScreen, screen_count);
272 globalconf.nscreens = screen_count;
273 focus_add_client(NULL);
275 /* parse config */
276 config_parse(confpath);
278 /* for each virtual screen */
279 for(screen = 0; screen < get_screen_count(); screen++)
280 setup(screen);
282 /* do this only for real screen */
283 for(screen = 0; screen < ScreenCount(dpy); screen++)
285 loadawesomeprops(screen);
286 ewmh_set_supported_hints(screen);
289 handler = p_new(event_handler *, LASTEvent);
290 handler[ButtonPress] = handle_event_buttonpress;
291 handler[ConfigureRequest] = handle_event_configurerequest;
292 handler[ConfigureNotify] = handle_event_configurenotify;
293 handler[DestroyNotify] = handle_event_destroynotify;
294 handler[EnterNotify] = handle_event_enternotify;
295 handler[LeaveNotify] = handle_event_leavenotify;
296 handler[Expose] = handle_event_expose;
297 handler[KeyPress] = handle_event_keypress;
298 handler[MappingNotify] = handle_event_mappingnotify;
299 handler[MapRequest] = handle_event_maprequest;
300 handler[PropertyNotify] = handle_event_propertynotify;
301 handler[UnmapNotify] = handle_event_unmapnotify;
302 handler[ClientMessage] = handle_event_clientmessage;
304 /* check for shape extension */
305 if((globalconf.have_shape = XShapeQueryExtension(dpy, &shape_event, &e_dummy)))
307 p_realloc(&handler, shape_event + 1);
308 handler[shape_event] = handle_event_shape;
311 /* check for randr extension */
312 if((globalconf.have_randr = XRRQueryExtension(dpy, &randr_event_base, &e_dummy)))
314 p_realloc(&handler, randr_event_base + RRScreenChangeNotify + 1);
315 handler[randr_event_base + RRScreenChangeNotify] = handle_event_randr_screen_change_notify;
318 scan();
320 XSync(dpy, False);
322 /* get socket fd */
323 csfd = get_client_socket();
324 addr = get_client_addr(getenv("DISPLAY"));
326 if(bind(csfd, (const struct sockaddr *) addr, SUN_LEN(addr)))
328 if(errno == EADDRINUSE)
330 if(unlink(addr->sun_path))
331 perror("error unlinking existing file");
332 if(bind(csfd, (const struct sockaddr *) addr, SUN_LEN(addr)))
333 perror("error binding UNIX domain socket");
335 else
336 perror("error binding UNIX domain socket");
339 /* register function for signals */
340 signal(SIGINT, &exit_on_signal);
341 signal(SIGTERM, &exit_on_signal);
342 signal(SIGHUP, &exit_on_signal);
344 /* call this to at least grab root window clicks */
345 window_grabbuttons(DefaultScreen(dpy), None, False, True);
347 /* main event loop, also reads status text from socket */
348 while(running)
350 FD_ZERO(&rd);
351 if(csfd >= 0)
352 FD_SET(csfd, &rd);
353 FD_SET(xfd, &rd);
354 if(select(MAX(xfd, csfd) + 1, &rd, NULL, NULL, NULL) == -1)
356 if(errno == EINTR)
357 continue;
358 eprint("select failed\n");
360 if(csfd >= 0 && FD_ISSET(csfd, &rd))
361 switch (r = recv(csfd, buf, sizeof(buf)-1, MSG_TRUNC))
363 case -1:
364 perror("awesome: error reading UNIX domain socket");
365 csfd = -1;
366 break;
367 case 0:
368 break;
369 default:
370 if(r >= ssizeof(buf))
371 break;
372 buf[r] = '\0';
373 parse_control(buf);
376 while(XPending(dpy))
378 XNextEvent(dpy, &ev);
379 if(handler[ev.type])
380 handler[ev.type](&ev); /* call handler */
383 statusbar_refresh();
385 /* need to resync */
386 XSync(dpy, False);
389 if(csfd > 0 && close(csfd))
390 perror("error closing UNIX domain socket");
391 if(unlink(addr->sun_path))
392 perror("error unlinking UNIX domain socket");
393 p_delete(&addr);
395 XCloseDisplay(dpy);
397 return EXIT_SUCCESS;
399 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80