call grab with phys_screen
[awesome.git] / awesome.c
blobd30b4e98b9000517dee0196765216d04e5322889
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 #define _GNU_SOURCE
23 #include <getopt.h>
25 #include <errno.h>
26 #include <locale.h>
27 #include <stdio.h>
28 #include <unistd.h>
29 #include <sys/select.h>
30 #include <sys/types.h>
31 #include <sys/stat.h>
32 #include <sys/socket.h>
33 #include <sys/un.h>
34 #include <fcntl.h>
35 #include <signal.h>
36 #include <X11/cursorfont.h>
37 #include <X11/keysym.h>
38 #include <X11/Xatom.h>
39 #include <X11/Xproto.h>
40 #include <X11/Xutil.h>
41 #include <X11/extensions/shape.h>
42 #include <X11/extensions/Xrandr.h>
44 #include "config.h"
45 #include "awesome.h"
46 #include "event.h"
47 #include "layout.h"
48 #include "screen.h"
49 #include "statusbar.h"
50 #include "uicb.h"
51 #include "window.h"
52 #include "client.h"
53 #include "focus.h"
54 #include "ewmh.h"
55 #include "common/awclient.h"
56 #include "common/util.h"
57 #include "common/awesome-version.h"
58 #include "common/configopts.h"
60 static int (*xerrorxlib) (Display *, XErrorEvent *);
61 static Bool running = True;
63 AwesomeConf globalconf;
65 /** Scan X to find windows to manage
67 static void
68 scan()
70 unsigned int i, num;
71 int screen, real_screen;
72 Window *wins = NULL, d1, d2;
73 XWindowAttributes wa;
75 for(screen = 0; screen < ScreenCount(globalconf.display); screen++)
77 if(XQueryTree(globalconf.display, RootWindow(globalconf.display, screen), &d1, &d2, &wins, &num))
79 real_screen = screen;
80 for(i = 0; i < num; i++)
82 /* XGetWindowAttributes return 1 on success */
83 if(!XGetWindowAttributes(globalconf.display, wins[i], &wa)
84 || wa.override_redirect
85 || XGetTransientForHint(globalconf.display, wins[i], &d1))
86 continue;
87 if(wa.map_state == IsViewable || window_getstate(wins[i]) == IconicState)
89 if(screen == 0)
90 real_screen = screen_get_bycoord(wa.x, wa.y);
91 client_manage(wins[i], &wa, real_screen);
94 /* now the transients */
95 for(i = 0; i < num; i++)
97 if(!XGetWindowAttributes(globalconf.display, wins[i], &wa))
98 continue;
99 if(XGetTransientForHint(globalconf.display, wins[i], &d1)
100 && (wa.map_state == IsViewable || window_getstate(wins[i]) == IconicState))
102 if(screen == 0)
103 real_screen = screen_get_bycoord(wa.x, wa.y);
104 client_manage(wins[i], &wa, real_screen);
108 if(wins)
109 XFree(wins);
113 /** Setup everything before running
114 * \param screen Screen number
115 * \todo clean things...
117 static void
118 setup(int screen)
120 XSetWindowAttributes wa;
121 Statusbar *statusbar;
122 int phys_screen = get_phys_screen(screen);
124 /* init cursors */
125 globalconf.cursor[CurNormal] = XCreateFontCursor(globalconf.display, XC_left_ptr);
126 globalconf.cursor[CurResize] = XCreateFontCursor(globalconf.display, XC_sizing);
127 globalconf.cursor[CurMove] = XCreateFontCursor(globalconf.display, XC_fleur);
129 /* select for events */
130 wa.event_mask = SubstructureRedirectMask | SubstructureNotifyMask
131 | EnterWindowMask | LeaveWindowMask | StructureNotifyMask;
132 wa.cursor = globalconf.cursor[CurNormal];
134 XChangeWindowAttributes(globalconf.display,
135 RootWindow(globalconf.display, phys_screen),
136 CWEventMask | CWCursor, &wa);
138 XSelectInput(globalconf.display,
139 RootWindow(globalconf.display, phys_screen),
140 wa.event_mask);
142 grabkeys(phys_screen);
144 for(statusbar = globalconf.screens[screen].statusbar; statusbar; statusbar = statusbar->next)
145 statusbar_init(statusbar);
148 /** Startup Error handler to check if another window manager
149 * is already running.
150 * \param disp Display
151 * \param ee Error event
153 static int __attribute__ ((noreturn))
154 xerrorstart(Display * disp __attribute__ ((unused)),
155 XErrorEvent * ee __attribute__ ((unused)))
157 eprint("another window manager is already running\n");
160 /** Quit awesome
161 * \param screen Screen ID
162 * \param arg nothing
163 * \ingroup ui_callback
165 void
166 uicb_quit(int screen __attribute__ ((unused)), char *arg __attribute__ ((unused)))
168 running = False;
171 static void
172 exit_on_signal(int sig __attribute__ ((unused)))
174 running = False;
177 /* There's no way to check accesses to destroyed windows, thus those cases are
178 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
179 * default error handler, which may call exit.
181 static int
182 xerror(Display *edpy, XErrorEvent *ee)
184 if(ee->error_code == BadWindow
185 || (ee->error_code == BadMatch && ee->request_code == X_SetInputFocus)
186 || (ee->error_code == BadValue && ee->request_code == X_KillClient))
187 return 0;
188 warn("fatal error: request code=%d, error code=%d\n", ee->request_code, ee->error_code);
189 return xerrorxlib(edpy, ee); /* may call exit */
192 /** Print help and exit(2) with given exit_code.
194 static void __attribute__ ((noreturn))
195 exit_help(int exit_code)
197 FILE *outfile = (exit_code == EXIT_SUCCESS) ? stdout : stderr;
198 fprintf(outfile,
199 "Usage: awesome [OPTION]\n\
200 -h, --help show help\n\
201 -v, --version show version\n\
202 -c, --config FILE configuration file to use\n\
203 -k, --check check configuration file syntax\n");
204 exit(exit_code);
207 /** Hello, this is main
208 * \param argc who knows
209 * \param argv who knows
210 * \return EXIT_SUCCESS I hope
212 typedef void event_handler (XEvent *);
214 main(int argc, char *argv[])
216 char buf[1024];
217 const char *confpath = NULL;
218 int r, xfd, e_dummy, csfd;
219 fd_set rd;
220 XEvent ev;
221 Display * dpy;
222 int shape_event, randr_event_base;
223 int screen;
224 int i, cmdlen;
225 event_handler **handler;
226 struct sockaddr_un *addr;
227 int opt;
228 Client *c;
229 static struct option long_options[] =
231 {"help", 0, NULL, 'h'},
232 {"version", 0, NULL, 'v'},
233 {"check", 0, NULL, 'k'},
234 {"config", 0, NULL, 'c'},
235 {NULL, 0, NULL, 0}
238 /* check args */
239 while((opt = getopt_long(argc, argv, "vhkc:",
240 long_options, NULL)) != -1)
241 switch(opt)
243 case 'v':
244 eprint_version("awesome");
245 break;
246 case 'h':
247 exit_help(EXIT_SUCCESS);
248 break;
249 case 'c':
250 if(a_strlen(optarg))
251 confpath = optarg;
252 else
253 eprint("-c option requires a file name\n");
254 break;
255 case 'k':
256 return config_check(confpath);
257 break;
260 /* Text won't be printed correctly otherwise */
261 setlocale(LC_CTYPE, "");
263 /* X stuff */
264 if(!(dpy = XOpenDisplay(NULL)))
265 eprint("cannot open display\n");
267 for(cmdlen = 0, i = 0; i < argc; i++)
268 cmdlen += a_strlen(argv[i] + 1);
269 globalconf.argv = p_new(char, cmdlen);
270 a_strcpy(globalconf.argv, cmdlen, argv[0]);
271 for(i = 1; i < argc; i++)
273 a_strcat(globalconf.argv, cmdlen, " ");
274 a_strcat(globalconf.argv, cmdlen, argv[i]);
277 xfd = ConnectionNumber(dpy);
279 XSetErrorHandler(xerrorstart);
280 for(screen = 0; screen < ScreenCount(dpy); screen++)
281 /* this causes an error if some other window manager is running */
282 XSelectInput(dpy, RootWindow(dpy, screen), SubstructureRedirectMask);
284 /* need to XSync to validate errorhandler */
285 XSync(dpy, False);
286 XSetErrorHandler(NULL);
287 xerrorxlib = XSetErrorHandler(xerror);
288 XSync(dpy, False);
290 /* store display */
291 globalconf.display = dpy;
293 /* init EWMH atoms */
294 ewmh_init_atoms();
296 /* init screens struct */
297 screen_build_screens();
298 focus_add_client(NULL);
300 /* parse config */
301 config_parse(confpath);
303 scan();
305 /* for each virtual screen */
306 for(screen = 0; screen < globalconf.nscreen; screen++)
307 setup(screen);
309 /* do this only for real screen */
310 for(screen = 0; screen < ScreenCount(dpy); screen++)
312 loadawesomeprops(screen);
313 ewmh_set_supported_hints(screen);
314 /* call this to at least grab root window clicks */
315 window_root_grabbuttons(screen);
318 handler = p_new(event_handler *, LASTEvent);
319 handler[ButtonPress] = handle_event_buttonpress;
320 handler[ConfigureRequest] = handle_event_configurerequest;
321 handler[ConfigureNotify] = handle_event_configurenotify;
322 handler[DestroyNotify] = handle_event_destroynotify;
323 handler[EnterNotify] = handle_event_enternotify;
324 handler[LeaveNotify] = handle_event_leavenotify;
325 handler[Expose] = handle_event_expose;
326 handler[KeyPress] = handle_event_keypress;
327 handler[MappingNotify] = handle_event_mappingnotify;
328 handler[MapRequest] = handle_event_maprequest;
329 handler[PropertyNotify] = handle_event_propertynotify;
330 handler[UnmapNotify] = handle_event_unmapnotify;
331 handler[ClientMessage] = handle_event_clientmessage;
333 /* check for shape extension */
334 if((globalconf.have_shape = XShapeQueryExtension(dpy, &shape_event, &e_dummy)))
336 p_realloc(&handler, shape_event + 1);
337 handler[shape_event] = handle_event_shape;
340 /* check for randr extension */
341 if((globalconf.have_randr = XRRQueryExtension(dpy, &randr_event_base, &e_dummy)))
343 p_realloc(&handler, randr_event_base + RRScreenChangeNotify + 1);
344 handler[randr_event_base + RRScreenChangeNotify] = handle_event_randr_screen_change_notify;
347 XSync(dpy, False);
349 /* get socket fd */
350 csfd = get_client_socket();
351 addr = get_client_addr(getenv("DISPLAY"));
353 if(bind(csfd, (const struct sockaddr *) addr, SUN_LEN(addr)))
355 if(errno == EADDRINUSE)
357 if(unlink(addr->sun_path))
358 perror("error unlinking existing file");
359 if(bind(csfd, (const struct sockaddr *) addr, SUN_LEN(addr)))
360 perror("error binding UNIX domain socket");
362 else
363 perror("error binding UNIX domain socket");
366 /* register function for signals */
367 signal(SIGINT, &exit_on_signal);
368 signal(SIGTERM, &exit_on_signal);
369 signal(SIGHUP, &exit_on_signal);
371 /* refresh everything before waiting events */
372 statusbar_refresh();
373 layout_refresh();
375 /* main event loop, also reads status text from socket */
376 while(running)
378 FD_ZERO(&rd);
379 if(csfd >= 0)
380 FD_SET(csfd, &rd);
381 FD_SET(xfd, &rd);
382 if(select(MAX(xfd, csfd) + 1, &rd, NULL, NULL, NULL) == -1)
384 if(errno == EINTR)
385 continue;
386 eprint("select failed\n");
388 if(csfd >= 0 && FD_ISSET(csfd, &rd))
389 switch (r = recv(csfd, buf, sizeof(buf)-1, MSG_TRUNC))
391 case -1:
392 perror("awesome: error reading UNIX domain socket");
393 csfd = -1;
394 break;
395 case 0:
396 break;
397 default:
398 if(r >= ssizeof(buf))
399 break;
400 buf[r] = '\0';
401 parse_control(buf);
402 statusbar_refresh();
403 layout_refresh();
405 /* two level XPending:
406 * we need to first check we have XEvent to handle
407 * and if so, we handle them all in a round.
408 * Then when we have refresh()'ed stuff so maybe new XEvent
409 * are available and select() won't tell us, so let's check
410 * with XPending() again.
412 while(XPending(dpy))
414 while(XPending(dpy))
416 XNextEvent(dpy, &ev);
417 if(handler[ev.type])
418 handler[ev.type](&ev);
420 /* drop events requested to */
421 if(globalconf.drop_events)
423 /* need to resync */
424 XSync(dpy, False);
425 while(XCheckMaskEvent(dpy, globalconf.drop_events, &ev));
426 globalconf.drop_events = NoEventMask;
429 /* need to resync */
430 XSync(dpy, False);
433 statusbar_refresh();
434 layout_refresh();
436 /* need to resync */
437 XSync(dpy, False);
442 if(csfd > 0 && close(csfd))
443 perror("error closing UNIX domain socket");
444 if(unlink(addr->sun_path))
445 perror("error unlinking UNIX domain socket");
446 p_delete(&addr);
448 /* remap all clients since some WM won't handle them otherwise */
449 for(c = globalconf.clients; c; c = c->next)
450 client_unban(c);
452 XSync(globalconf.display, False);
454 XCloseDisplay(dpy);
456 return EXIT_SUCCESS;
458 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80