fix statusbar display when on right
[awesome.git] / awesome.c
blob1b60e77d826eab6a0a7d1fcab9eb10926b40c878
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->buttons.ntitle; i++)
72 p_delete(&awesomeconf->buttons.title[i].arg);
73 for(i = 0; i< awesomeconf->buttons.nlayout; i++)
74 p_delete(&awesomeconf->buttons.layout[i].arg);
75 for(i = 0; i < awesomeconf->nlayouts; i++)
76 p_delete(&awesomeconf->layouts[i].symbol);
77 for(i = 0; i < awesomeconf->nrules; i++)
79 p_delete(&awesomeconf->rules[i].prop);
80 p_delete(&awesomeconf->rules[i].tags);
82 p_delete(&awesomeconf->tags);
83 p_delete(&awesomeconf->layouts);
84 p_delete(&awesomeconf->rules);
85 p_delete(&awesomeconf->keys);
86 p_delete(&awesomeconf->buttons.tag);
87 p_delete(&awesomeconf->buttons.title);
88 p_delete(&awesomeconf->buttons.layout);
89 p_delete(&awesomeconf->configpath);
92 /** Cleanup everything on quit
93 * \param awesomeconf awesome config
95 static void
96 cleanup(awesome_config *awesomeconf)
98 int screen;
100 while(*awesomeconf->clients)
102 client_unban(*awesomeconf->clients);
103 client_unmanage(*awesomeconf->clients, NormalState, awesomeconf);
106 for(screen = 0; screen < get_screen_count(awesomeconf->display); screen++)
107 cleanup_screen(&awesomeconf[screen]);
109 XSetInputFocus(awesomeconf->display, PointerRoot, RevertToPointerRoot, CurrentTime);
110 XSync(awesomeconf->display, False);
112 p_delete(&awesomeconf->clients);
113 p_delete(&awesomeconf);
116 /** Scan X to find windows to manage
117 * \param screen Screen number
118 * \param awesomeconf awesome config
120 static void
121 scan(awesome_config *awesomeconf)
123 unsigned int i, num;
124 int screen, real_screen;
125 Window *wins = NULL, d1, d2;
126 XWindowAttributes wa;
128 for(screen = 0; screen < ScreenCount(awesomeconf->display); screen++)
130 if(XQueryTree(awesomeconf->display, RootWindow(awesomeconf->display, screen), &d1, &d2, &wins, &num))
132 real_screen = screen;
133 for(i = 0; i < num; i++)
135 if(!XGetWindowAttributes(awesomeconf->display, wins[i], &wa)
136 || wa.override_redirect
137 || XGetTransientForHint(awesomeconf->display, wins[i], &d1))
138 continue;
139 if(wa.map_state == IsViewable || window_getstate(awesomeconf->display, wins[i]) == IconicState)
141 if(screen == 0)
142 real_screen = get_screen_bycoord(awesomeconf->display, wa.x, wa.y);
143 client_manage(wins[i], &wa, &awesomeconf[real_screen]);
146 /* now the transients */
147 for(i = 0; i < num; i++)
149 if(!XGetWindowAttributes(awesomeconf->display, wins[i], &wa))
150 continue;
151 if(XGetTransientForHint(awesomeconf->display, wins[i], &d1)
152 && (wa.map_state == IsViewable || window_getstate(awesomeconf->display, wins[i]) == IconicState))
154 if(screen == 0)
155 real_screen = get_screen_bycoord(awesomeconf->display, wa.x, wa.y);
156 client_manage(wins[i], &wa, &awesomeconf[real_screen]);
160 if(wins)
161 XFree(wins);
165 /** Setup everything before running
166 * \param awesomeconf awesome config ref
167 * \todo clean things...
169 static void
170 setup(awesome_config *awesomeconf)
172 XSetWindowAttributes wa;
174 /* init cursors */
175 awesomeconf->cursor[CurNormal] = XCreateFontCursor(awesomeconf->display, XC_left_ptr);
176 awesomeconf->cursor[CurResize] = XCreateFontCursor(awesomeconf->display, XC_sizing);
177 awesomeconf->cursor[CurMove] = XCreateFontCursor(awesomeconf->display, XC_fleur);
179 /* select for events */
180 wa.event_mask = SubstructureRedirectMask | SubstructureNotifyMask
181 | EnterWindowMask | LeaveWindowMask | StructureNotifyMask;
182 wa.cursor = awesomeconf->cursor[CurNormal];
184 XChangeWindowAttributes(awesomeconf->display, RootWindow(awesomeconf->display, awesomeconf->phys_screen), CWEventMask | CWCursor, &wa);
186 XSelectInput(awesomeconf->display, RootWindow(awesomeconf->display, awesomeconf->phys_screen), wa.event_mask);
188 grabkeys(awesomeconf);
191 void
192 setup_screen(awesome_config *awesomeconf, const char *confpath)
194 parse_config(confpath, awesomeconf);
195 setup(awesomeconf);
196 initstatusbar(awesomeconf->display, awesomeconf->screen, &awesomeconf->statusbar,
197 awesomeconf->cursor[CurNormal], awesomeconf->font,
198 awesomeconf->layouts, awesomeconf->nlayouts);
201 /** Startup Error handler to check if another window manager
202 * is already running.
203 * \param disp Display ref
204 * \param ee Error event
206 static int __attribute__ ((noreturn))
207 xerrorstart(Display * disp __attribute__ ((unused)), XErrorEvent * ee __attribute__ ((unused)))
209 eprint("awesome: another window manager is already running\n");
212 /** Quit awesome
213 * \param awesomeconf awesome config
214 * \param arg nothing
215 * \ingroup ui_callback
217 void
218 uicb_quit(awesome_config *awesomeconf __attribute__((unused)),
219 const char *arg __attribute__ ((unused)))
221 running = False;
224 /* There's no way to check accesses to destroyed windows, thus those cases are
225 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
226 * default error handler, which may call exit.
229 xerror(Display * edpy, XErrorEvent * ee)
231 if(ee->error_code == BadWindow
232 || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
233 || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
234 || (ee->request_code == X_PolyFillRectangle
235 && ee->error_code == BadDrawable)
236 || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
237 || (ee->request_code == X_ConfigureWindow
238 && ee->error_code == BadMatch) || (ee->request_code == X_GrabKey
239 && ee->error_code == BadAccess)
240 || (ee->request_code == X_CopyArea && ee->error_code == BadDrawable))
241 return 0;
242 fprintf(stderr, "awesome: fatal error: request code=%d, error code=%d\n",
243 ee->request_code, ee->error_code);
245 return xerrorxlib(edpy, ee); /* may call exit */
248 /** Hello, this is main
249 * \param argc who knows
250 * \param argv who knows
251 * \return EXIT_SUCCESS I hope
253 typedef void event_handler (XEvent *, awesome_config *);
255 main(int argc, char *argv[])
257 char buf[1024];
258 const char *confpath = NULL;
259 int r, xfd, e_dummy, csfd;
260 fd_set rd;
261 XEvent ev;
262 Display * dpy;
263 awesome_config *awesomeconf;
264 int shape_event, randr_event_base;
265 int screen;
266 enum { NetSupported, NetWMName, NetLast }; /* EWMH atoms */
267 Atom netatom[NetLast];
268 event_handler **handler;
269 Client **clients;
270 struct sockaddr_un *addr;
272 if(argc >= 2)
274 if(!a_strcmp("-v", argv[1]))
276 printf("awesome-" VERSION " © 2007 Julien Danjou\n");
277 return EXIT_SUCCESS;
279 else if(!a_strcmp("-c", argv[1]))
281 if(a_strlen(argv[2]))
282 confpath = argv[2];
283 else
284 eprint("awesome: -c require a file\n");
286 else
287 eprint("usage: awesome [-v | -c configfile]\n");
290 /* Tag won't be printed otherwised */
291 setlocale(LC_CTYPE, "");
293 if(!(dpy = XOpenDisplay(NULL)))
294 eprint("awesome: cannot open display\n");
296 xfd = ConnectionNumber(dpy);
298 XSetErrorHandler(xerrorstart);
299 for(screen = 0; screen < ScreenCount(dpy); screen++)
300 /* this causes an error if some other window manager is running */
301 XSelectInput(dpy, RootWindow(dpy, screen), SubstructureRedirectMask);
303 /* need to XSync to validate errorhandler */
304 XSync(dpy, False);
305 XSetErrorHandler(NULL);
306 xerrorxlib = XSetErrorHandler(xerror);
307 XSync(dpy, False);
309 /* allocate stuff */
310 awesomeconf = p_new(awesome_config, get_screen_count(dpy));
311 clients = p_new(Client *, 1);
313 for(screen = 0; screen < get_screen_count(dpy); screen++)
315 /* store display */
316 awesomeconf[screen].display = dpy;
318 /* set screen */
319 awesomeconf[screen].screen = screen;
320 setup_screen(&awesomeconf[screen], confpath);
321 awesomeconf[screen].clients = clients;
322 drawstatusbar(&awesomeconf[screen]);
325 netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
326 netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
328 /* do this only for real screen */
329 for(screen = 0; screen < ScreenCount(dpy); screen++)
331 loadawesomeprops(&awesomeconf[screen]);
332 XChangeProperty(dpy, RootWindow(dpy, screen), netatom[NetSupported],
333 XA_ATOM, 32, PropModeReplace, (unsigned char *) netatom, NetLast);
336 handler = p_new(event_handler *, LASTEvent);
337 handler[ButtonPress] = handle_event_buttonpress;
338 handler[ConfigureRequest] = handle_event_configurerequest;
339 handler[ConfigureNotify] = handle_event_configurenotify;
340 handler[DestroyNotify] = handle_event_destroynotify;
341 handler[EnterNotify] = handle_event_enternotify;
342 handler[LeaveNotify] = handle_event_leavenotify;
343 handler[Expose] = handle_event_expose;
344 handler[KeyPress] = handle_event_keypress;
345 handler[MappingNotify] = handle_event_mappingnotify;
346 handler[MapRequest] = handle_event_maprequest;
347 handler[PropertyNotify] = handle_event_propertynotify;
348 handler[UnmapNotify] = handle_event_unmapnotify;
350 /* check for shape extension */
351 if((awesomeconf[0].have_shape = XShapeQueryExtension(dpy, &shape_event, &e_dummy)))
353 p_realloc(&handler, shape_event + 1);
354 handler[shape_event] = handle_event_shape;
357 /* check for randr extension */
358 if((awesomeconf[0].have_randr = XRRQueryExtension(dpy, &randr_event_base, &e_dummy)))
360 p_realloc(&handler, randr_event_base + RRScreenChangeNotify + 1);
361 handler[randr_event_base + RRScreenChangeNotify] = handle_event_randr_screen_change_notify;
364 for(screen = 0; screen < get_screen_count(dpy); screen++)
366 awesomeconf[screen].have_shape = awesomeconf[0].have_shape;
367 awesomeconf[screen].have_randr = awesomeconf[0].have_randr;
370 scan(awesomeconf);
372 XSync(dpy, False);
374 /* get socket fd */
375 csfd = get_client_socket();
376 addr = get_client_addr(getenv("DISPLAY"));
378 if(bind(csfd, (const struct sockaddr *) addr, SUN_LEN(addr)))
380 if(errno == EADDRINUSE)
382 if(unlink(addr->sun_path))
383 perror("error unlinking existing file");
384 if(bind(csfd, (const struct sockaddr *) addr, SUN_LEN(addr)))
385 perror("error binding UNIX domain socket");
387 else
388 perror("error binding UNIX domain socket");
391 /* main event loop, also reads status text from socket */
392 while(running)
394 FD_ZERO(&rd);
395 if(csfd >= 0)
396 FD_SET(csfd, &rd);
397 FD_SET(xfd, &rd);
398 if(select(MAX(xfd, csfd) + 1, &rd, NULL, NULL, NULL) == -1)
400 if(errno == EINTR)
401 continue;
402 eprint("select failed\n");
404 if(csfd >= 0 && FD_ISSET(csfd, &rd))
405 switch (r = recv(csfd, buf, sizeof(buf)-1, MSG_TRUNC))
407 case -1:
408 perror("awesome: error reading UNIX domain socket");
409 a_strncpy(awesomeconf[0].statustext, sizeof(awesomeconf[0].statustext),
410 strerror(errno), sizeof(awesomeconf[0].statustext) - 1);
411 awesomeconf[0].statustext[sizeof(awesomeconf[0].statustext) - 1] = '\0';
412 csfd = -1;
413 break;
414 case 0:
415 break;
416 default:
417 if(r >= ssizeof(buf))
418 break;
419 buf[r] = '\0';
420 parse_control(buf, awesomeconf);
423 while(XPending(dpy))
425 XNextEvent(dpy, &ev);
426 if(handler[ev.type])
427 handler[ev.type](&ev, awesomeconf); /* call handler */
431 if(csfd > 0 && close(csfd))
432 perror("error closing UNIX domain socket");
433 if(unlink(addr->sun_path))
434 perror("error unlinking UNIX domain socket");
435 p_delete(&addr);
437 cleanup(awesomeconf);
438 XCloseDisplay(dpy);
440 return EXIT_SUCCESS;
442 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99