Do nothing it the client already has focus
[awesome.git] / awesome.c
blobcb6ea427439f237f2cb4e164fc41ab30353a020b
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 "tag.h"
56 #include "common/socket.h"
57 #include "common/util.h"
58 #include "common/version.h"
59 #include "common/configopts.h"
60 #include "common/xscreen.h"
62 static int (*xerrorxlib) (Display *, XErrorEvent *);
63 static Bool running = True;
65 AwesomeConf globalconf;
67 /** Scan X to find windows to manage
69 static void
70 scan()
72 unsigned int i, num;
73 int screen, real_screen;
74 Window *wins = NULL, d1, d2;
75 XWindowAttributes wa;
77 for(screen = 0; screen < ScreenCount(globalconf.display); screen++)
79 if(XQueryTree(globalconf.display, RootWindow(globalconf.display, screen), &d1, &d2, &wins, &num))
81 for(i = 0; i < num; i++)
83 /* XGetWindowAttributes return 1 on success */
84 if(!XGetWindowAttributes(globalconf.display, wins[i], &wa)
85 || wa.override_redirect
86 || XGetTransientForHint(globalconf.display, wins[i], &d1))
87 continue;
88 if(wa.map_state == IsViewable || window_getstate(wins[i]) == IconicState)
90 real_screen = screen_get_bycoord(globalconf.screens_info, screen, 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 real_screen = screen_get_bycoord(globalconf.screens_info, screen, wa.x, wa.y);
103 client_manage(wins[i], &wa, real_screen);
107 if(wins)
108 XFree(wins);
112 /** Startup Error handler to check if another window manager
113 * is already running.
114 * \param disp Display
115 * \param ee Error event
117 static int __attribute__ ((noreturn))
118 xerrorstart(Display * disp __attribute__ ((unused)),
119 XErrorEvent * ee __attribute__ ((unused)))
121 eprint("another window manager is already running\n");
124 /** Quit awesome
125 * \param screen Screen ID
126 * \param arg nothing
127 * \ingroup ui_callback
129 void
130 uicb_quit(int screen __attribute__ ((unused)), char *arg __attribute__ ((unused)))
132 running = False;
135 static void
136 exit_on_signal(int sig __attribute__ ((unused)))
138 running = False;
141 /** \brief awesome xerror function
142 * There's no way to check accesses to destroyed windows, thus those cases are
143 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
144 * default error handler, which may call exit.
145 * \param edpy display ref
146 * \param ee XErrorEvent event
147 * \return 0 if no error, or xerror's xlib return status
149 static int
150 xerror(Display *edpy, XErrorEvent *ee)
152 if(ee->error_code == BadWindow
153 || (ee->error_code == BadMatch && ee->request_code == X_SetInputFocus)
154 || (ee->error_code == BadValue && ee->request_code == X_KillClient)
155 || (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch))
156 return 0;
157 warn("fatal error: request code=%d, error code=%d\n", ee->request_code, ee->error_code);
158 return xerrorxlib(edpy, ee); /* may call exit */
161 /** Print help and exit(2) with given exit_code.
163 static void __attribute__ ((noreturn))
164 exit_help(int exit_code)
166 FILE *outfile = (exit_code == EXIT_SUCCESS) ? stdout : stderr;
167 fprintf(outfile,
168 "Usage: awesome [OPTION]\n\
169 -h, --help show help\n\
170 -v, --version show version\n\
171 -c, --config FILE configuration file to use\n\
172 -k, --check check configuration file syntax\n\
173 -s --sync enable synchronization (X debug)\n");
174 exit(exit_code);
177 /** Hello, this is main
178 * \param argc who knows
179 * \param argv who knows
180 * \return EXIT_SUCCESS I hope
182 typedef void event_handler (XEvent *);
184 main(int argc, char *argv[])
186 char buf[1024];
187 const char *confpath = NULL;
188 int r, xfd, e_dummy, csfd, shape_event, randr_event_base, i, screen, opt;
189 ssize_t cmdlen = 1;
190 Statusbar *statusbar;
191 fd_set rd;
192 XEvent ev;
193 Display * dpy;
194 event_handler **handler;
195 struct sockaddr_un *addr;
196 Client *c;
197 XSetWindowAttributes wa;
198 Bool xsync = False;
199 static struct option long_options[] =
201 {"help", 0, NULL, 'h'},
202 {"version", 0, NULL, 'v'},
203 {"check", 0, NULL, 'k'},
204 {"config", 1, NULL, 'c'},
205 {"sync", 0, NULL, 's'},
206 {NULL, 0, NULL, 0}
209 /* save argv */
210 for(i = 0; i < argc; i++)
211 cmdlen += a_strlen(argv[i]) + 1;
213 globalconf.argv = p_new(char, cmdlen);
214 a_strcpy(globalconf.argv, cmdlen, argv[0]);
216 for(i = 1; i < argc; i++)
218 a_strcat(globalconf.argv, cmdlen, " ");
219 a_strcat(globalconf.argv, cmdlen, argv[i]);
222 /* check args */
223 while((opt = getopt_long(argc, argv, "vhkcs:",
224 long_options, NULL)) != -1)
225 switch(opt)
227 case 'v':
228 eprint_version("awesome");
229 break;
230 case 'h':
231 exit_help(EXIT_SUCCESS);
232 break;
233 case 'c':
234 if(a_strlen(optarg))
235 confpath = optarg;
236 else
237 eprint("-c option requires a file name\n");
238 break;
239 case 'k':
240 return config_check(confpath);
241 break;
242 case 's':
243 xsync = True;
244 break;
247 /* Text won't be printed correctly otherwise */
248 setlocale(LC_CTYPE, "");
250 /* X stuff */
251 if(!(dpy = XOpenDisplay(NULL)))
252 eprint("cannot open display\n");
254 xfd = ConnectionNumber(dpy);
256 XSetErrorHandler(xerrorstart);
257 for(screen = 0; screen < ScreenCount(dpy); screen++)
258 /* this causes an error if some other window manager is running */
259 XSelectInput(dpy, RootWindow(dpy, screen), SubstructureRedirectMask);
261 /* need to XSync to validate errorhandler */
262 XSync(dpy, False);
263 XSetErrorHandler(NULL);
264 xerrorxlib = XSetErrorHandler(xerror);
265 XSync(dpy, False);
267 if(xsync)
268 XSynchronize(dpy, True);
270 /* store display */
271 globalconf.display = dpy;
273 /* init EWMH atoms */
274 ewmh_init_atoms();
276 /* init screens struct */
277 globalconf.screens_info = screensinfo_new(dpy);
278 globalconf.screens = p_new(VirtScreen, globalconf.screens_info->nscreen);
279 focus_add_client(NULL);
281 /* parse config */
282 config_parse(confpath);
284 /* init cursors */
285 globalconf.cursor[CurNormal] = XCreateFontCursor(globalconf.display, XC_left_ptr);
286 globalconf.cursor[CurResize] = XCreateFontCursor(globalconf.display, XC_sizing);
287 globalconf.cursor[CurMove] = XCreateFontCursor(globalconf.display, XC_fleur);
289 /* for each virtual screen */
290 for(screen = 0; screen < globalconf.screens_info->nscreen; screen++)
292 /* view at least one tag */
293 tag_view(globalconf.screens[screen].tags, True);
295 for(statusbar = globalconf.screens[screen].statusbar; statusbar; statusbar = statusbar->next)
296 statusbar_init(statusbar);
299 /* select for events */
300 wa.event_mask = SubstructureRedirectMask | SubstructureNotifyMask
301 | EnterWindowMask | LeaveWindowMask | StructureNotifyMask | PointerMotionMask;
302 wa.cursor = globalconf.cursor[CurNormal];
304 /* do this only for real screen */
305 for(screen = 0; screen < ScreenCount(dpy); screen++)
307 XChangeWindowAttributes(globalconf.display,
308 RootWindow(globalconf.display, screen),
309 CWEventMask | CWCursor, &wa);
310 XSelectInput(globalconf.display,
311 RootWindow(globalconf.display, screen),
312 wa.event_mask);
313 ewmh_set_supported_hints(screen);
314 /* call this to at least grab root window clicks */
315 window_root_grabbuttons(screen);
316 window_root_grabkeys(screen);
319 /* scan existing windows */
320 scan();
322 handler = p_new(event_handler *, LASTEvent);
323 handler[ButtonPress] = event_handle_buttonpress;
324 handler[ConfigureRequest] = event_handle_configurerequest;
325 handler[ConfigureNotify] = event_handle_configurenotify;
326 handler[DestroyNotify] = event_handle_destroynotify;
327 handler[EnterNotify] = event_handle_enternotify;
328 handler[LeaveNotify] = event_handle_leavenotify;
329 handler[MotionNotify] = event_handle_motionnotify;
330 handler[Expose] = event_handle_expose;
331 handler[KeyPress] = event_handle_keypress;
332 handler[MappingNotify] = event_handle_mappingnotify;
333 handler[MapRequest] = event_handle_maprequest;
334 handler[PropertyNotify] = event_handle_propertynotify;
335 handler[UnmapNotify] = event_handle_unmapnotify;
336 handler[ClientMessage] = event_handle_clientmessage;
338 /* check for shape extension */
339 if((globalconf.have_shape = XShapeQueryExtension(dpy, &shape_event, &e_dummy)))
341 p_realloc(&handler, shape_event + 1);
342 handler[shape_event] = event_handle_shape;
345 /* check for randr extension */
346 if((globalconf.have_randr = XRRQueryExtension(dpy, &randr_event_base, &e_dummy)))
348 p_realloc(&handler, randr_event_base + RRScreenChangeNotify + 1);
349 handler[randr_event_base + RRScreenChangeNotify] = event_handle_randr_screen_change_notify;
352 XSync(dpy, False);
354 /* get socket fd */
355 csfd = get_client_socket();
356 addr = get_client_addr(getenv("DISPLAY"));
358 if(bind(csfd, (const struct sockaddr *) addr, SUN_LEN(addr)))
360 if(errno == EADDRINUSE)
362 if(unlink(addr->sun_path))
363 warn("error unlinking existing file: %s\n", strerror(errno));
364 if(bind(csfd, (const struct sockaddr *) addr, SUN_LEN(addr)))
365 warn("error binding UNIX domain socket: %s\n", strerror(errno));
367 else
368 warn("error binding UNIX domain socket: %s\n", strerror(errno));
371 /* register function for signals */
372 signal(SIGINT, &exit_on_signal);
373 signal(SIGTERM, &exit_on_signal);
374 signal(SIGHUP, &exit_on_signal);
376 /* refresh everything before waiting events */
377 statusbar_refresh();
378 layout_refresh();
380 /* main event loop, also reads status text from socket */
381 while(running)
383 FD_ZERO(&rd);
384 if(csfd >= 0)
385 FD_SET(csfd, &rd);
386 FD_SET(xfd, &rd);
387 if(select(MAX(xfd, csfd) + 1, &rd, NULL, NULL, NULL) == -1)
389 if(errno == EINTR)
390 continue;
391 eprint("select failed\n");
393 if(csfd >= 0 && FD_ISSET(csfd, &rd))
394 switch (r = recv(csfd, buf, sizeof(buf)-1, MSG_TRUNC))
396 case -1:
397 perror("awesome: error reading UNIX domain socket");
398 csfd = -1;
399 break;
400 case 0:
401 break;
402 default:
403 if(r >= ssizeof(buf))
404 break;
405 buf[r] = '\0';
406 parse_control(buf);
407 statusbar_refresh();
408 layout_refresh();
410 /* two level XPending:
411 * we need to first check we have XEvent to handle
412 * and if so, we handle them all in a round.
413 * Then when we have refresh()'ed stuff so maybe new XEvent
414 * are available and select() won't tell us, so let's check
415 * with XPending() again.
417 while(XPending(dpy))
419 while(XPending(dpy))
421 XNextEvent(dpy, &ev);
422 if(handler[ev.type])
423 handler[ev.type](&ev);
425 /* need to resync */
426 XSync(dpy, False);
429 statusbar_refresh();
430 layout_refresh();
432 /* need to resync */
433 XSync(dpy, False);
438 if(csfd > 0 && close(csfd))
439 perror("error closing UNIX domain socket");
440 if(unlink(addr->sun_path))
441 perror("error unlinking UNIX domain socket");
442 p_delete(&addr);
444 /* remap all clients since some WM won't handle them otherwise */
445 for(c = globalconf.clients; c; c = c->next)
446 client_unban(c);
448 XSync(globalconf.display, False);
450 XCloseDisplay(dpy);
452 return EXIT_SUCCESS;
454 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80