Fix for the problem that cairo draws over the given path on very sharp angles
[awesome.git] / awesome.c
blob1438c6c6dde25406ea021155d256b88b74892945
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 "common/awesome-version.h"
42 #include "awesome.h"
43 #include "event.h"
44 #include "layout.h"
45 #include "screen.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"
53 #include "common/util.h"
55 static int (*xerrorxlib) (Display *, XErrorEvent *);
56 static Bool running = True;
58 AwesomeConf globalconf;
60 /** Scan X to find windows to manage
62 static void
63 scan()
65 unsigned int i, num;
66 int screen, real_screen;
67 Window *wins = NULL, d1, d2;
68 XWindowAttributes wa;
70 for(screen = 0; screen < ScreenCount(globalconf.display); screen++)
72 if(XQueryTree(globalconf.display, RootWindow(globalconf.display, screen), &d1, &d2, &wins, &num))
74 real_screen = screen;
75 for(i = 0; i < num; i++)
77 /* XGetWindowAttributes return 1 on success */
78 if(!XGetWindowAttributes(globalconf.display, wins[i], &wa)
79 || wa.override_redirect
80 || XGetTransientForHint(globalconf.display, wins[i], &d1))
81 continue;
82 if(wa.map_state == IsViewable || window_getstate(wins[i]) == IconicState)
84 if(screen == 0)
85 real_screen = get_screen_bycoord(wa.x, wa.y);
86 client_manage(wins[i], &wa, real_screen);
89 /* now the transients */
90 for(i = 0; i < num; i++)
92 if(!XGetWindowAttributes(globalconf.display, wins[i], &wa))
93 continue;
94 if(XGetTransientForHint(globalconf.display, wins[i], &d1)
95 && (wa.map_state == IsViewable || window_getstate(wins[i]) == IconicState))
97 if(screen == 0)
98 real_screen = get_screen_bycoord(wa.x, wa.y);
99 client_manage(wins[i], &wa, real_screen);
103 if(wins)
104 XFree(wins);
108 /** Setup everything before running
109 * \param screen Screen number
110 * \todo clean things...
112 static void
113 setup(int screen)
115 XSetWindowAttributes wa;
116 Statusbar *statusbar;
117 int phys_screen = get_phys_screen(screen);
119 /* init cursors */
120 globalconf.cursor[CurNormal] = XCreateFontCursor(globalconf.display, XC_left_ptr);
121 globalconf.cursor[CurResize] = XCreateFontCursor(globalconf.display, XC_sizing);
122 globalconf.cursor[CurMove] = XCreateFontCursor(globalconf.display, XC_fleur);
124 /* select for events */
125 wa.event_mask = SubstructureRedirectMask | SubstructureNotifyMask
126 | EnterWindowMask | LeaveWindowMask | StructureNotifyMask;
127 wa.cursor = globalconf.cursor[CurNormal];
129 XChangeWindowAttributes(globalconf.display,
130 RootWindow(globalconf.display, phys_screen),
131 CWEventMask | CWCursor, &wa);
133 XSelectInput(globalconf.display,
134 RootWindow(globalconf.display, phys_screen),
135 wa.event_mask);
137 grabkeys(phys_screen);
139 for(statusbar = globalconf.screens[screen].statusbar; statusbar; statusbar = statusbar->next)
140 statusbar_init(statusbar);
143 /** Startup Error handler to check if another window manager
144 * is already running.
145 * \param disp Display
146 * \param ee Error event
148 static int __attribute__ ((noreturn))
149 xerrorstart(Display * disp __attribute__ ((unused)),
150 XErrorEvent * ee __attribute__ ((unused)))
152 eprint("another window manager is already running\n");
155 /** Quit awesome
156 * \param screen Screen ID
157 * \param arg nothing
158 * \ingroup ui_callback
160 void
161 uicb_quit(int screen __attribute__ ((unused)), char *arg __attribute__ ((unused)))
163 running = False;
166 static void
167 exit_on_signal(int sig __attribute__ ((unused)))
169 running = False;
172 /* There's no way to check accesses to destroyed windows, thus those cases are
173 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
174 * default error handler, which may call exit.
176 static int
177 xerror(Display * edpy, XErrorEvent * ee)
179 if(ee->error_code == BadWindow)
180 return 0;
181 warn("fatal error: request code=%d, error code=%d\n", ee->request_code, ee->error_code);
182 return xerrorxlib(edpy, ee); /* may call exit */
185 /** Print help and exit(2) with given exit_code.
187 static void __attribute__ ((noreturn))
188 exit_help(int exit_code)
190 FILE *outfile = (exit_code == EXIT_SUCCESS) ? stdout : stderr;
191 fprintf(outfile, "Usage: awesome [-v | -h | -c configfile]\n");
192 exit(exit_code);
195 /** Hello, this is main
196 * \param argc who knows
197 * \param argv who knows
198 * \return EXIT_SUCCESS I hope
200 typedef void event_handler (XEvent *);
202 main(int argc, char *argv[])
204 char buf[1024];
205 const char *confpath = NULL;
206 int r, xfd, e_dummy, csfd;
207 fd_set rd;
208 XEvent ev;
209 Display * dpy;
210 int shape_event, randr_event_base;
211 int screen;
212 int i, cmdlen;
213 event_handler **handler;
214 struct sockaddr_un *addr;
215 int args_ok = 1;
217 /* check args */
218 if(argc >= 2)
220 args_ok = 0;
221 if(!a_strcmp("-v", argv[1]) || !a_strcmp("--version", argv[1]))
222 eprint_version("awesome");
223 else if(!a_strcmp("-h", argv[1]) || !a_strcmp("--help", argv[1]))
224 exit_help(EXIT_SUCCESS);
225 else if(!a_strcmp("-c", argv[1]))
227 if(a_strlen(argv[2]))
228 confpath = argv[2], args_ok = 1;
229 else
230 eprint("-c option requires a file name\n");
232 else
233 exit_help(EXIT_FAILURE);
235 if(!args_ok)
236 exit_help(EXIT_FAILURE);
238 /* Text won't be printed correctly otherwise */
239 setlocale(LC_CTYPE, "");
241 /* X stuff */
242 if(!(dpy = XOpenDisplay(NULL)))
243 eprint("cannot open display\n");
245 for(cmdlen = 0, i = 0; i < argc; i++)
246 cmdlen += a_strlen(argv[i] + 1);
247 globalconf.argv = p_new(char, cmdlen);
248 a_strcpy(globalconf.argv, cmdlen, argv[0]);
249 for(i = 1; i < argc; i++)
251 a_strcat(globalconf.argv, cmdlen, " ");
252 a_strcat(globalconf.argv, cmdlen, argv[i]);
255 xfd = ConnectionNumber(dpy);
257 XSetErrorHandler(xerrorstart);
258 for(screen = 0; screen < ScreenCount(dpy); screen++)
259 /* this causes an error if some other window manager is running */
260 XSelectInput(dpy, RootWindow(dpy, screen), SubstructureRedirectMask);
262 /* need to XSync to validate errorhandler */
263 XSync(dpy, False);
264 XSetErrorHandler(NULL);
265 xerrorxlib = XSetErrorHandler(xerror);
266 XSync(dpy, False);
268 /* store display */
269 globalconf.display = dpy;
271 /* init EWMH atoms */
272 ewmh_init_atoms();
274 /* init screens struct */
275 screen_build_screens();
276 focus_add_client(NULL);
278 /* parse config */
279 config_parse(confpath);
281 scan();
283 /* for each virtual screen */
284 for(screen = 0; screen < globalconf.nscreen; screen++)
285 setup(screen);
287 /* do this only for real screen */
288 for(screen = 0; screen < ScreenCount(dpy); screen++)
290 loadawesomeprops(screen);
291 ewmh_set_supported_hints(screen);
292 /* call this to at least grab root window clicks */
293 window_root_grabbuttons(screen);
296 handler = p_new(event_handler *, LASTEvent);
297 handler[ButtonPress] = handle_event_buttonpress;
298 handler[ConfigureRequest] = handle_event_configurerequest;
299 handler[ConfigureNotify] = handle_event_configurenotify;
300 handler[DestroyNotify] = handle_event_destroynotify;
301 handler[EnterNotify] = handle_event_enternotify;
302 handler[LeaveNotify] = handle_event_leavenotify;
303 handler[Expose] = handle_event_expose;
304 handler[KeyPress] = handle_event_keypress;
305 handler[MappingNotify] = handle_event_mappingnotify;
306 handler[MapRequest] = handle_event_maprequest;
307 handler[PropertyNotify] = handle_event_propertynotify;
308 handler[UnmapNotify] = handle_event_unmapnotify;
309 handler[ClientMessage] = handle_event_clientmessage;
311 /* check for shape extension */
312 if((globalconf.have_shape = XShapeQueryExtension(dpy, &shape_event, &e_dummy)))
314 p_realloc(&handler, shape_event + 1);
315 handler[shape_event] = handle_event_shape;
318 /* check for randr extension */
319 if((globalconf.have_randr = XRRQueryExtension(dpy, &randr_event_base, &e_dummy)))
321 p_realloc(&handler, randr_event_base + RRScreenChangeNotify + 1);
322 handler[randr_event_base + RRScreenChangeNotify] = handle_event_randr_screen_change_notify;
325 XSync(dpy, False);
327 /* get socket fd */
328 csfd = get_client_socket();
329 addr = get_client_addr(getenv("DISPLAY"));
331 if(bind(csfd, (const struct sockaddr *) addr, SUN_LEN(addr)))
333 if(errno == EADDRINUSE)
335 if(unlink(addr->sun_path))
336 perror("error unlinking existing file");
337 if(bind(csfd, (const struct sockaddr *) addr, SUN_LEN(addr)))
338 perror("error binding UNIX domain socket");
340 else
341 perror("error binding UNIX domain socket");
344 /* register function for signals */
345 signal(SIGINT, &exit_on_signal);
346 signal(SIGTERM, &exit_on_signal);
347 signal(SIGHUP, &exit_on_signal);
349 /* refresh everything before waiting events */
350 statusbar_refresh();
351 layout_refresh();
353 /* main event loop, also reads status text from socket */
354 while(running)
356 FD_ZERO(&rd);
357 if(csfd >= 0)
358 FD_SET(csfd, &rd);
359 FD_SET(xfd, &rd);
360 if(select(MAX(xfd, csfd) + 1, &rd, NULL, NULL, NULL) == -1)
362 if(errno == EINTR)
363 continue;
364 eprint("select failed\n");
366 if(csfd >= 0 && FD_ISSET(csfd, &rd))
367 switch (r = recv(csfd, buf, sizeof(buf)-1, MSG_TRUNC))
369 case -1:
370 perror("awesome: error reading UNIX domain socket");
371 csfd = -1;
372 break;
373 case 0:
374 break;
375 default:
376 if(r >= ssizeof(buf))
377 break;
378 buf[r] = '\0';
379 parse_control(buf);
380 statusbar_refresh();
381 layout_refresh();
383 /* two level XPending:
384 * we need to first check we have XEvent to handle
385 * and if so, we handle them all in a round.
386 * Then when we have refresh()'ed stuff so maybe new XEvent
387 * are available and select() won't tell us, so let's check
388 * with XPending() again.
390 while(XPending(dpy))
392 while(XPending(dpy))
394 XNextEvent(dpy, &ev);
395 if(handler[ev.type])
396 handler[ev.type](&ev); /* call handler */
398 /* drop events requested to */
399 if(globalconf.drop_events)
401 /* need to resync */
402 XSync(dpy, False);
403 while(XCheckMaskEvent(dpy, globalconf.drop_events, &ev));
404 globalconf.drop_events = NoEventMask;
407 /* need to resync */
408 XSync(dpy, False);
411 statusbar_refresh();
412 layout_refresh();
414 /* need to resync */
415 XSync(dpy, False);
419 if(csfd > 0 && close(csfd))
420 perror("error closing UNIX domain socket");
421 if(unlink(addr->sun_path))
422 perror("error unlinking UNIX domain socket");
423 p_delete(&addr);
425 XCloseDisplay(dpy);
427 return EXIT_SUCCESS;
429 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80