nmaster is now configurable per tag
[awesome.git] / awesome.c
blob5febb0a773a2ac571278dc762d720ced17a15037
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;
59 XftFontClose(awesomeconf->display, awesomeconf->font);
60 XUngrabKey(awesomeconf->display, AnyKey, AnyModifier, RootWindow(awesomeconf->display, awesomeconf->phys_screen));
61 XFreePixmap(awesomeconf->display, awesomeconf->statusbar.drawable);
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(i = 0; i < awesomeconf->nlayouts; i++)
72 p_delete(&awesomeconf->layouts[i].symbol);
73 for(i = 0; i < awesomeconf->nrules; i++)
75 p_delete(&awesomeconf->rules[i].prop);
76 p_delete(&awesomeconf->rules[i].tags);
78 p_delete(&awesomeconf->tags);
79 p_delete(&awesomeconf->layouts);
80 p_delete(&awesomeconf->rules);
81 p_delete(&awesomeconf->keys);
82 p_delete(&awesomeconf->configpath);
85 /** Cleanup everything on quit
86 * \param awesomeconf awesome config
88 static void
89 cleanup(awesome_config *awesomeconf)
91 int screen;
93 while(*awesomeconf->clients)
95 client_unban(*awesomeconf->clients);
96 client_unmanage(*awesomeconf->clients, NormalState, awesomeconf);
99 for(screen = 0; screen < get_screen_count(awesomeconf->display); screen++)
100 cleanup_screen(&awesomeconf[screen]);
102 XSetInputFocus(awesomeconf->display, PointerRoot, RevertToPointerRoot, CurrentTime);
103 XSync(awesomeconf->display, False);
105 p_delete(&awesomeconf->clients);
106 p_delete(&awesomeconf);
109 /** Scan X to find windows to manage
110 * \param screen Screen number
111 * \param awesomeconf awesome config
113 static void
114 scan(awesome_config *awesomeconf)
116 unsigned int i, num;
117 int screen, real_screen;
118 Window *wins = NULL, d1, d2;
119 XWindowAttributes wa;
121 for(screen = 0; screen < ScreenCount(awesomeconf->display); screen++)
123 if(XQueryTree(awesomeconf->display, RootWindow(awesomeconf->display, screen), &d1, &d2, &wins, &num))
125 real_screen = screen;
126 for(i = 0; i < num; i++)
128 if(!XGetWindowAttributes(awesomeconf->display, wins[i], &wa)
129 || wa.override_redirect
130 || XGetTransientForHint(awesomeconf->display, wins[i], &d1))
131 continue;
132 if(wa.map_state == IsViewable || window_getstate(awesomeconf->display, wins[i]) == IconicState)
134 if(screen == 0)
135 real_screen = get_screen_bycoord(awesomeconf->display, wa.x, wa.y);
136 client_manage(wins[i], &wa, &awesomeconf[real_screen]);
139 /* now the transients */
140 for(i = 0; i < num; i++)
142 if(!XGetWindowAttributes(awesomeconf->display, wins[i], &wa))
143 continue;
144 if(XGetTransientForHint(awesomeconf->display, wins[i], &d1)
145 && (wa.map_state == IsViewable || window_getstate(awesomeconf->display, wins[i]) == IconicState))
147 if(screen == 0)
148 real_screen = get_screen_bycoord(awesomeconf->display, wa.x, wa.y);
149 client_manage(wins[i], &wa, &awesomeconf[real_screen]);
153 if(wins)
154 XFree(wins);
158 /** Setup everything before running
159 * \param awesomeconf awesome config ref
160 * \todo clean things...
162 static void
163 setup(awesome_config *awesomeconf)
165 XSetWindowAttributes wa;
167 /* init cursors */
168 awesomeconf->cursor[CurNormal] = XCreateFontCursor(awesomeconf->display, XC_left_ptr);
169 awesomeconf->cursor[CurResize] = XCreateFontCursor(awesomeconf->display, XC_sizing);
170 awesomeconf->cursor[CurMove] = XCreateFontCursor(awesomeconf->display, XC_fleur);
172 /* select for events */
173 wa.event_mask = SubstructureRedirectMask | SubstructureNotifyMask
174 | EnterWindowMask | LeaveWindowMask | StructureNotifyMask;
175 wa.cursor = awesomeconf->cursor[CurNormal];
177 XChangeWindowAttributes(awesomeconf->display, RootWindow(awesomeconf->display, awesomeconf->phys_screen), CWEventMask | CWCursor, &wa);
179 XSelectInput(awesomeconf->display, RootWindow(awesomeconf->display, awesomeconf->phys_screen), wa.event_mask);
181 grabkeys(awesomeconf);
184 void
185 setup_screen(awesome_config *awesomeconf, const char *confpath)
187 parse_config(confpath, awesomeconf);
188 setup(awesomeconf);
189 initstatusbar(awesomeconf->display, awesomeconf->screen, &awesomeconf->statusbar,
190 awesomeconf->cursor[CurNormal], awesomeconf->font,
191 awesomeconf->layouts, awesomeconf->nlayouts);
194 /** Startup Error handler to check if another window manager
195 * is already running.
196 * \param disp Display ref
197 * \param ee Error event
199 static int __attribute__ ((noreturn))
200 xerrorstart(Display * disp __attribute__ ((unused)), XErrorEvent * ee __attribute__ ((unused)))
202 eprint("awesome: another window manager is already running\n");
205 /** Quit awesome
206 * \param awesomeconf awesome config
207 * \param arg nothing
208 * \ingroup ui_callback
210 void
211 uicb_quit(awesome_config *awesomeconf __attribute__((unused)),
212 const char *arg __attribute__ ((unused)))
214 running = False;
217 /* There's no way to check accesses to destroyed windows, thus those cases are
218 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
219 * default error handler, which may call exit.
222 xerror(Display * edpy, XErrorEvent * ee)
224 if(ee->error_code == BadWindow
225 || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
226 || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
227 || (ee->request_code == X_PolyFillRectangle
228 && ee->error_code == BadDrawable)
229 || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
230 || (ee->request_code == X_ConfigureWindow
231 && ee->error_code == BadMatch) || (ee->request_code == X_GrabKey
232 && ee->error_code == BadAccess)
233 || (ee->request_code == X_CopyArea && ee->error_code == BadDrawable))
234 return 0;
235 fprintf(stderr, "awesome: fatal error: request code=%d, error code=%d\n",
236 ee->request_code, ee->error_code);
238 return xerrorxlib(edpy, ee); /* may call exit */
241 /** Hello, this is main
242 * \param argc who knows
243 * \param argv who knows
244 * \return EXIT_SUCCESS I hope
246 typedef void event_handler (XEvent *, awesome_config *);
248 main(int argc, char *argv[])
250 char buf[1024];
251 const char *confpath = NULL;
252 int r, xfd, e_dummy, csfd;
253 fd_set rd;
254 XEvent ev;
255 Display * dpy;
256 awesome_config *awesomeconf;
257 int shape_event, randr_event_base;
258 int screen;
259 enum { NetSupported, NetWMName, NetLast }; /* EWMH atoms */
260 Atom netatom[NetLast];
261 event_handler **handler;
262 Client **clients;
263 struct sockaddr_un *addr;
265 if(argc >= 2)
267 if(!a_strcmp("-v", argv[1]))
269 printf("awesome-" VERSION " © 2007 Julien Danjou\n");
270 return EXIT_SUCCESS;
272 else if(!a_strcmp("-c", argv[1]))
274 if(a_strlen(argv[2]))
275 confpath = argv[2];
276 else
277 eprint("awesome: -c require a file\n");
279 else
280 eprint("usage: awesome [-v | -c configfile]\n");
283 /* Tag won't be printed otherwised */
284 setlocale(LC_CTYPE, "");
286 if(!(dpy = XOpenDisplay(NULL)))
287 eprint("awesome: cannot open display\n");
289 xfd = ConnectionNumber(dpy);
291 XSetErrorHandler(xerrorstart);
292 for(screen = 0; screen < ScreenCount(dpy); screen++)
293 /* this causes an error if some other window manager is running */
294 XSelectInput(dpy, RootWindow(dpy, screen), SubstructureRedirectMask);
296 /* need to XSync to validate errorhandler */
297 XSync(dpy, False);
298 XSetErrorHandler(NULL);
299 xerrorxlib = XSetErrorHandler(xerror);
300 XSync(dpy, False);
302 /* allocate stuff */
303 awesomeconf = p_new(awesome_config, get_screen_count(dpy));
304 clients = p_new(Client *, 1);
306 for(screen = 0; screen < get_screen_count(dpy); screen++)
308 /* store display */
309 awesomeconf[screen].display = dpy;
311 /* set screen */
312 awesomeconf[screen].screen = screen;
313 setup_screen(&awesomeconf[screen], confpath);
314 awesomeconf[screen].clients = clients;
315 drawstatusbar(&awesomeconf[screen]);
318 netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
319 netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
321 /* do this only for real screen */
322 for(screen = 0; screen < ScreenCount(dpy); screen++)
324 loadawesomeprops(&awesomeconf[screen]);
325 XChangeProperty(dpy, RootWindow(dpy, screen), netatom[NetSupported],
326 XA_ATOM, 32, PropModeReplace, (unsigned char *) netatom, NetLast);
329 handler = p_new(event_handler *, LASTEvent);
330 handler[ButtonPress] = handle_event_buttonpress;
331 handler[ConfigureRequest] = handle_event_configurerequest;
332 handler[ConfigureNotify] = handle_event_configurenotify;
333 handler[DestroyNotify] = handle_event_destroynotify;
334 handler[EnterNotify] = handle_event_enternotify;
335 handler[LeaveNotify] = handle_event_leavenotify;
336 handler[Expose] = handle_event_expose;
337 handler[KeyPress] = handle_event_keypress;
338 handler[MappingNotify] = handle_event_mappingnotify;
339 handler[MapRequest] = handle_event_maprequest;
340 handler[PropertyNotify] = handle_event_propertynotify;
341 handler[UnmapNotify] = handle_event_unmapnotify;
343 /* check for shape extension */
344 if((awesomeconf[0].have_shape = XShapeQueryExtension(dpy, &shape_event, &e_dummy)))
346 p_realloc(&handler, shape_event + 1);
347 handler[shape_event] = handle_event_shape;
350 /* check for randr extension */
351 if((awesomeconf[0].have_randr = XRRQueryExtension(dpy, &randr_event_base, &e_dummy)))
353 p_realloc(&handler, randr_event_base + RRScreenChangeNotify + 1);
354 handler[randr_event_base + RRScreenChangeNotify] = handle_event_randr_screen_change_notify;
357 for(screen = 0; screen < get_screen_count(dpy); screen++)
359 awesomeconf[screen].have_shape = awesomeconf[0].have_shape;
360 awesomeconf[screen].have_randr = awesomeconf[0].have_randr;
363 scan(awesomeconf);
365 XSync(dpy, False);
367 /* get socket fd */
368 csfd = get_client_socket();
369 addr = get_client_addr(getenv("DISPLAY"));
371 if(bind(csfd, (const struct sockaddr *) addr, SUN_LEN(addr)))
373 if(errno == EADDRINUSE)
375 if(unlink(addr->sun_path))
376 perror("error unlinking existing file");
377 if(bind(csfd, (const struct sockaddr *) addr, SUN_LEN(addr)))
378 perror("error binding UNIX domain socket");
380 else
381 perror("error binding UNIX domain socket");
384 /* main event loop, also reads status text from socket */
385 while(running)
387 FD_ZERO(&rd);
388 if(csfd >= 0)
389 FD_SET(csfd, &rd);
390 FD_SET(xfd, &rd);
391 if(select(MAX(xfd, csfd) + 1, &rd, NULL, NULL, NULL) == -1)
393 if(errno == EINTR)
394 continue;
395 eprint("select failed\n");
397 if(csfd >= 0 && FD_ISSET(csfd, &rd))
398 switch (r = recv(csfd, buf, sizeof(buf)-1, MSG_TRUNC))
400 case -1:
401 perror("awesome: error reading UNIX domain socket");
402 a_strncpy(awesomeconf[0].statustext, sizeof(awesomeconf[0].statustext),
403 strerror(errno), sizeof(awesomeconf[0].statustext) - 1);
404 awesomeconf[0].statustext[sizeof(awesomeconf[0].statustext) - 1] = '\0';
405 csfd = -1;
406 break;
407 case 0:
408 break;
409 default:
410 if(r >= ssizeof(buf))
411 break;
412 buf[r] = '\0';
413 parse_control(buf, awesomeconf);
416 while(XPending(dpy))
418 XNextEvent(dpy, &ev);
419 if(handler[ev.type])
420 handler[ev.type](&ev, awesomeconf); /* call handler */
424 if(csfd > 0 && close(csfd))
425 perror("error closing UNIX domain socket");
426 if(unlink(addr->sun_path))
427 perror("error unlinking UNIX domain socket");
428 p_delete(&addr);
430 cleanup(awesomeconf);
431 XCloseDisplay(dpy);
433 return EXIT_SUCCESS;
435 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99