movemouse() is now an uicb function
[awesome.git] / awesome.c
blob09b383cb0de470179d9d6d994d362adb66cda6cf
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 <X11/cursorfont.h>
33 #include <X11/keysym.h>
34 #include <X11/Xatom.h>
35 #include <X11/Xproto.h>
36 #include <X11/Xutil.h>
37 #include <X11/extensions/shape.h>
38 #include <X11/extensions/Xrandr.h>
40 #include "awesome.h"
41 #include "event.h"
42 #include "layout.h"
43 #include "tag.h"
44 #include "screen.h"
45 #include "util.h"
46 #include "statusbar.h"
47 #include "uicb.h"
48 #include "window.h"
49 #include "awesome-client.h"
51 static int (*xerrorxlib) (Display *, XErrorEvent *);
52 static Bool running = True;
54 void
55 cleanup_screen(awesome_config *awesomeconf)
57 int i;
58 Button *b, *bn;
60 XftFontClose(awesomeconf->display, awesomeconf->font);
61 XUngrabKey(awesomeconf->display, AnyKey, AnyModifier, RootWindow(awesomeconf->display, awesomeconf->phys_screen));
62 XDestroyWindow(awesomeconf->display, awesomeconf->statusbar.window);
63 XFreeCursor(awesomeconf->display, awesomeconf->cursor[CurNormal]);
64 XFreeCursor(awesomeconf->display, awesomeconf->cursor[CurResize]);
65 XFreeCursor(awesomeconf->display, awesomeconf->cursor[CurMove]);
67 for(i = 0; i < awesomeconf->ntags; i++)
68 p_delete(&awesomeconf->tags[i].name);
69 for(i = 0; i < awesomeconf->nkeys; i++)
70 p_delete(&awesomeconf->keys[i].arg);
71 for(b = awesomeconf->buttons.tag; b; b = bn)
73 bn = b->next;
74 p_delete(&b);
76 for(b = awesomeconf->buttons.title; b; b = bn)
78 bn = b->next;
79 p_delete(&b->arg);
80 p_delete(&b);
82 for(b = awesomeconf->buttons.layout; b; b = bn)
84 bn = b->next;
85 p_delete(&b->arg);
86 p_delete(&b);
88 for(b = awesomeconf->buttons.root; b; b = bn)
90 bn = b->next;
91 p_delete(&b->arg);
92 p_delete(&b);
94 for(i = 0; i < awesomeconf->nlayouts; i++)
95 p_delete(&awesomeconf->layouts[i].symbol);
96 for(i = 0; i < awesomeconf->nrules; i++)
98 p_delete(&awesomeconf->rules[i].prop);
99 p_delete(&awesomeconf->rules[i].tags);
101 p_delete(&awesomeconf->tags);
102 p_delete(&awesomeconf->layouts);
103 p_delete(&awesomeconf->rules);
104 p_delete(&awesomeconf->keys);
105 p_delete(&awesomeconf->configpath);
108 /** Cleanup everything on quit
109 * \param awesomeconf awesome config
111 static void
112 cleanup(awesome_config *awesomeconf)
114 int screen;
116 while(*awesomeconf->clients)
118 client_unban(*awesomeconf->clients);
119 client_unmanage(*awesomeconf->clients, NormalState, awesomeconf);
122 for(screen = 0; screen < get_screen_count(awesomeconf->display); screen++)
123 cleanup_screen(&awesomeconf[screen]);
125 XSetInputFocus(awesomeconf->display, PointerRoot, RevertToPointerRoot, CurrentTime);
126 XSync(awesomeconf->display, False);
128 p_delete(&awesomeconf->clients);
129 p_delete(&awesomeconf);
132 /** Scan X to find windows to manage
133 * \param screen Screen number
134 * \param awesomeconf awesome config
136 static void
137 scan(awesome_config *awesomeconf)
139 unsigned int i, num;
140 int screen, real_screen;
141 Window *wins = NULL, d1, d2;
142 XWindowAttributes wa;
144 for(screen = 0; screen < ScreenCount(awesomeconf->display); screen++)
146 if(XQueryTree(awesomeconf->display, RootWindow(awesomeconf->display, screen), &d1, &d2, &wins, &num))
148 real_screen = screen;
149 for(i = 0; i < num; i++)
151 if(!XGetWindowAttributes(awesomeconf->display, wins[i], &wa)
152 || wa.override_redirect
153 || XGetTransientForHint(awesomeconf->display, wins[i], &d1))
154 continue;
155 if(wa.map_state == IsViewable || window_getstate(awesomeconf->display, wins[i]) == IconicState)
157 if(screen == 0)
158 real_screen = get_screen_bycoord(awesomeconf->display, wa.x, wa.y);
159 client_manage(wins[i], &wa, &awesomeconf[real_screen]);
162 /* now the transients */
163 for(i = 0; i < num; i++)
165 if(!XGetWindowAttributes(awesomeconf->display, wins[i], &wa))
166 continue;
167 if(XGetTransientForHint(awesomeconf->display, wins[i], &d1)
168 && (wa.map_state == IsViewable || window_getstate(awesomeconf->display, wins[i]) == IconicState))
170 if(screen == 0)
171 real_screen = get_screen_bycoord(awesomeconf->display, wa.x, wa.y);
172 client_manage(wins[i], &wa, &awesomeconf[real_screen]);
176 if(wins)
177 XFree(wins);
181 /** Setup everything before running
182 * \param awesomeconf awesome config ref
183 * \todo clean things...
185 static void
186 setup(awesome_config *awesomeconf)
188 XSetWindowAttributes wa;
190 /* init cursors */
191 awesomeconf->cursor[CurNormal] = XCreateFontCursor(awesomeconf->display, XC_left_ptr);
192 awesomeconf->cursor[CurResize] = XCreateFontCursor(awesomeconf->display, XC_sizing);
193 awesomeconf->cursor[CurMove] = XCreateFontCursor(awesomeconf->display, XC_fleur);
195 /* select for events */
196 wa.event_mask = SubstructureRedirectMask | SubstructureNotifyMask
197 | EnterWindowMask | LeaveWindowMask | StructureNotifyMask;
198 wa.cursor = awesomeconf->cursor[CurNormal];
200 XChangeWindowAttributes(awesomeconf->display, RootWindow(awesomeconf->display, awesomeconf->phys_screen), CWEventMask | CWCursor, &wa);
202 XSelectInput(awesomeconf->display, RootWindow(awesomeconf->display, awesomeconf->phys_screen), wa.event_mask);
204 grabkeys(awesomeconf);
207 void
208 setup_screen(awesome_config *awesomeconf, const char *confpath)
210 parse_config(confpath, awesomeconf);
211 setup(awesomeconf);
212 initstatusbar(awesomeconf->display, awesomeconf->screen, &awesomeconf->statusbar,
213 awesomeconf->cursor[CurNormal], awesomeconf->font,
214 awesomeconf->layouts, awesomeconf->nlayouts);
217 /** Startup Error handler to check if another window manager
218 * is already running.
219 * \param disp Display ref
220 * \param ee Error event
222 static int __attribute__ ((noreturn))
223 xerrorstart(Display * disp __attribute__ ((unused)), XErrorEvent * ee __attribute__ ((unused)))
225 eprint("awesome: another window manager is already running\n");
228 /** Quit awesome
229 * \param awesomeconf awesome config
230 * \param arg nothing
231 * \ingroup ui_callback
233 void
234 uicb_quit(awesome_config *awesomeconf __attribute__((unused)),
235 const char *arg __attribute__ ((unused)))
237 running = False;
240 /* There's no way to check accesses to destroyed windows, thus those cases are
241 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
242 * default error handler, which may call exit.
245 xerror(Display * edpy, XErrorEvent * ee)
247 if(ee->error_code == BadWindow
248 || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
249 || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
250 || (ee->request_code == X_PolyFillRectangle
251 && ee->error_code == BadDrawable)
252 || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
253 || (ee->request_code == X_ConfigureWindow
254 && ee->error_code == BadMatch) || (ee->request_code == X_GrabKey
255 && ee->error_code == BadAccess)
256 || (ee->request_code == X_CopyArea && ee->error_code == BadDrawable))
257 return 0;
258 fprintf(stderr, "awesome: fatal error: request code=%d, error code=%d\n",
259 ee->request_code, ee->error_code);
261 return xerrorxlib(edpy, ee); /* may call exit */
264 /** Hello, this is main
265 * \param argc who knows
266 * \param argv who knows
267 * \return EXIT_SUCCESS I hope
269 typedef void event_handler (XEvent *, awesome_config *);
271 main(int argc, char *argv[])
273 char buf[1024];
274 const char *confpath = NULL;
275 int r, xfd, e_dummy, csfd;
276 fd_set rd;
277 XEvent ev;
278 Display * dpy;
279 awesome_config *awesomeconf;
280 int shape_event, randr_event_base;
281 int screen;
282 enum { NetSupported, NetWMName, NetLast }; /* EWMH atoms */
283 Atom netatom[NetLast];
284 event_handler **handler;
285 Client **clients;
286 struct sockaddr_un *addr;
288 if(argc >= 2)
290 if(!a_strcmp("-v", argv[1]))
292 printf("awesome-" VERSION " © 2007 Julien Danjou\n");
293 return EXIT_SUCCESS;
295 else if(!a_strcmp("-c", argv[1]))
297 if(a_strlen(argv[2]))
298 confpath = argv[2];
299 else
300 eprint("awesome: -c require a file\n");
302 else
303 eprint("usage: awesome [-v | -c configfile]\n");
306 /* Tag won't be printed otherwised */
307 setlocale(LC_CTYPE, "");
309 if(!(dpy = XOpenDisplay(NULL)))
310 eprint("awesome: cannot open display\n");
312 xfd = ConnectionNumber(dpy);
314 XSetErrorHandler(xerrorstart);
315 for(screen = 0; screen < ScreenCount(dpy); screen++)
316 /* this causes an error if some other window manager is running */
317 XSelectInput(dpy, RootWindow(dpy, screen), SubstructureRedirectMask);
319 /* need to XSync to validate errorhandler */
320 XSync(dpy, False);
321 XSetErrorHandler(NULL);
322 xerrorxlib = XSetErrorHandler(xerror);
323 XSync(dpy, False);
325 /* allocate stuff */
326 awesomeconf = p_new(awesome_config, get_screen_count(dpy));
327 clients = p_new(Client *, 1);
329 for(screen = 0; screen < get_screen_count(dpy); screen++)
331 /* store display */
332 awesomeconf[screen].display = dpy;
334 /* set screen */
335 awesomeconf[screen].screen = screen;
336 setup_screen(&awesomeconf[screen], confpath);
337 awesomeconf[screen].clients = clients;
338 drawstatusbar(&awesomeconf[screen]);
341 netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
342 netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
344 /* do this only for real screen */
345 for(screen = 0; screen < ScreenCount(dpy); screen++)
347 loadawesomeprops(&awesomeconf[screen]);
348 XChangeProperty(dpy, RootWindow(dpy, screen), netatom[NetSupported],
349 XA_ATOM, 32, PropModeReplace, (unsigned char *) netatom, NetLast);
352 handler = p_new(event_handler *, LASTEvent);
353 handler[ButtonPress] = handle_event_buttonpress;
354 handler[ConfigureRequest] = handle_event_configurerequest;
355 handler[ConfigureNotify] = handle_event_configurenotify;
356 handler[DestroyNotify] = handle_event_destroynotify;
357 handler[EnterNotify] = handle_event_enternotify;
358 handler[LeaveNotify] = handle_event_leavenotify;
359 handler[Expose] = handle_event_expose;
360 handler[KeyPress] = handle_event_keypress;
361 handler[MappingNotify] = handle_event_mappingnotify;
362 handler[MapRequest] = handle_event_maprequest;
363 handler[PropertyNotify] = handle_event_propertynotify;
364 handler[UnmapNotify] = handle_event_unmapnotify;
366 /* check for shape extension */
367 if((awesomeconf[0].have_shape = XShapeQueryExtension(dpy, &shape_event, &e_dummy)))
369 p_realloc(&handler, shape_event + 1);
370 handler[shape_event] = handle_event_shape;
373 /* check for randr extension */
374 if((awesomeconf[0].have_randr = XRRQueryExtension(dpy, &randr_event_base, &e_dummy)))
376 p_realloc(&handler, randr_event_base + RRScreenChangeNotify + 1);
377 handler[randr_event_base + RRScreenChangeNotify] = handle_event_randr_screen_change_notify;
380 for(screen = 0; screen < get_screen_count(dpy); screen++)
382 awesomeconf[screen].have_shape = awesomeconf[0].have_shape;
383 awesomeconf[screen].have_randr = awesomeconf[0].have_randr;
386 scan(awesomeconf);
388 XSync(dpy, False);
390 /* get socket fd */
391 csfd = get_client_socket();
392 addr = get_client_addr(getenv("DISPLAY"));
394 if(bind(csfd, (const struct sockaddr *) addr, SUN_LEN(addr)))
396 if(errno == EADDRINUSE)
398 if(unlink(addr->sun_path))
399 perror("error unlinking existing file");
400 if(bind(csfd, (const struct sockaddr *) addr, SUN_LEN(addr)))
401 perror("error binding UNIX domain socket");
403 else
404 perror("error binding UNIX domain socket");
407 /* main event loop, also reads status text from socket */
408 while(running)
410 FD_ZERO(&rd);
411 if(csfd >= 0)
412 FD_SET(csfd, &rd);
413 FD_SET(xfd, &rd);
414 if(select(MAX(xfd, csfd) + 1, &rd, NULL, NULL, NULL) == -1)
416 if(errno == EINTR)
417 continue;
418 eprint("select failed\n");
420 if(csfd >= 0 && FD_ISSET(csfd, &rd))
421 switch (r = recv(csfd, buf, sizeof(buf)-1, MSG_TRUNC))
423 case -1:
424 perror("awesome: error reading UNIX domain socket");
425 a_strncpy(awesomeconf[0].statustext, sizeof(awesomeconf[0].statustext),
426 strerror(errno), sizeof(awesomeconf[0].statustext) - 1);
427 awesomeconf[0].statustext[sizeof(awesomeconf[0].statustext) - 1] = '\0';
428 csfd = -1;
429 break;
430 case 0:
431 break;
432 default:
433 if(r >= ssizeof(buf))
434 break;
435 buf[r] = '\0';
436 parse_control(buf, awesomeconf);
439 while(XPending(dpy))
441 XNextEvent(dpy, &ev);
442 if(handler[ev.type])
443 handler[ev.type](&ev, awesomeconf); /* call handler */
447 if(csfd > 0 && close(csfd))
448 perror("error closing UNIX domain socket");
449 if(unlink(addr->sun_path))
450 perror("error unlinking UNIX domain socket");
451 p_delete(&addr);
453 cleanup(awesomeconf);
454 XCloseDisplay(dpy);
456 return EXIT_SUCCESS;
458 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99