make Statusbar optional in get_display_info()
[awesome.git] / awesome.c
blob980e20636d4b5edc5ff645de532a9a0760f70ec8
1 /*
2 * awesome.c - awesome main functions
3 *
4 * Copyright © 2007 Julien Danjou <julien@danjou.info>
5 *
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.
20 */
22 #include <errno.h>
23 #include <locale.h>
24 #include <stdio.h>
25 #include <unistd.h>
26 #include <sys/select.h>
27 #include <X11/cursorfont.h>
28 #include <X11/keysym.h>
29 #include <X11/Xatom.h>
30 #include <X11/Xproto.h>
31 #include <X11/Xutil.h>
32 #include <X11/extensions/shape.h>
33 #include <X11/extensions/Xrandr.h>
35 #include "awesome.h"
36 #include "event.h"
37 #include "layout.h"
38 #include "tag.h"
39 #include "screen.h"
40 #include "util.h"
41 #include "statusbar.h"
43 Client *clients = NULL;
44 Client *sel = NULL;
45 Client *stack = NULL;
46 DC *dc;
48 static int (*xerrorxlib) (Display *, XErrorEvent *);
49 static Bool readin = True, running = True;
51 /** Cleanup everything on quit
52 * \param disp Display ref
53 * \param drawcontext Drawcontext ref
54 * \param awesomeconf awesome config
56 static void
57 cleanup(Display *disp, DC *drawcontext, awesome_config *awesomeconf)
59 int screen, i;
61 close(STDIN_FILENO);
63 while(stack)
65 unban(stack);
66 unmanage(stack, drawcontext, NormalState, awesomeconf);
69 for(screen = 0; screen < get_screen_count(disp); screen++)
71 if(drawcontext[screen].font.set)
72 XFreeFontSet(disp, drawcontext[screen].font.set);
73 else
74 XFreeFont(disp, drawcontext[screen].font.xfont);
76 XUngrabKey(disp, AnyKey, AnyModifier, RootWindow(disp, screen));
78 XFreePixmap(disp, awesomeconf[screen].statusbar.drawable);
79 XFreeGC(disp, drawcontext[screen].gc);
80 XDestroyWindow(disp, awesomeconf[screen].statusbar.window);
81 XFreeCursor(disp, drawcontext[screen].cursor[CurNormal]);
82 XFreeCursor(disp, drawcontext[screen].cursor[CurResize]);
83 XFreeCursor(disp, drawcontext[screen].cursor[CurMove]);
85 for(i = 0; i < awesomeconf[screen].ntags; i++)
86 p_delete(&awesomeconf[screen].tags[i].name);
87 for(i = 0; i < awesomeconf[screen].nkeys; i++)
88 p_delete(&awesomeconf[screen].keys[i].arg);
89 for(i = 0; i < awesomeconf[screen].nlayouts; i++)
90 p_delete(&awesomeconf[screen].layouts[i].symbol);
91 for(i = 0; i < awesomeconf[screen].nrules; i++)
93 p_delete(&awesomeconf[screen].rules[i].prop);
94 p_delete(&awesomeconf[screen].rules[i].tags);
96 p_delete(&awesomeconf[screen].tags);
97 p_delete(&awesomeconf[screen].layouts);
98 p_delete(&awesomeconf[screen].rules);
99 p_delete(&awesomeconf[screen].keys);
101 XSetInputFocus(disp, PointerRoot, RevertToPointerRoot, CurrentTime);
102 XSync(disp, False);
103 p_delete(&awesomeconf);
104 p_delete(&dc);
107 /** Get a window state (WM_STATE)
108 * \param disp Display ref
109 * \param w Client window
110 * \return state
112 static long
113 getstate(Display *disp, Window w)
115 int format, status;
116 long result = -1;
117 unsigned char *p = NULL;
118 unsigned long n, extra;
119 Atom real;
120 status = XGetWindowProperty(disp, w, XInternAtom(disp, "WM_STATE", False),
121 0L, 2L, False, XInternAtom(disp, "WM_STATE", False),
122 &real, &format, &n, &extra, (unsigned char **) &p);
123 if(status != Success)
124 return -1;
125 if(n != 0)
126 result = *p;
127 XFree(p);
128 return result;
131 /** Scan X to find windows to manage
132 * \param disp Display ref
133 * \param screen Screen number
134 * \param drawcontext Drawcontext ref
135 * \param awesomeconf awesome config
137 static void
138 scan(Display *disp, DC *drawcontext, awesome_config *awesomeconf)
140 unsigned int i, num;
141 int screen, real_screen;
142 Window *wins = NULL, d1, d2;
143 XWindowAttributes wa;
145 for(screen = 0; screen < ScreenCount(disp); screen++)
147 if(XQueryTree(disp, RootWindow(disp, screen), &d1, &d2, &wins, &num))
149 real_screen = screen;
150 for(i = 0; i < num; i++)
152 if(!XGetWindowAttributes(disp, wins[i], &wa)
153 || wa.override_redirect
154 || XGetTransientForHint(disp, wins[i], &d1))
155 continue;
156 if(wa.map_state == IsViewable || getstate(disp, wins[i]) == IconicState)
158 if(screen == 0)
159 real_screen = get_screen_bycoord(disp, wa.x, wa.y);
160 manage(disp, &drawcontext[real_screen], wins[i], &wa, &awesomeconf[real_screen]);
163 /* now the transients */
164 for(i = 0; i < num; i++)
166 if(!XGetWindowAttributes(disp, wins[i], &wa))
167 continue;
168 if(XGetTransientForHint(disp, wins[i], &d1)
169 && (wa.map_state == IsViewable || getstate(disp, wins[i]) == IconicState))
171 if(screen == 0)
172 real_screen = get_screen_bycoord(disp, wa.x, wa.y);
173 manage(disp, &drawcontext[real_screen], wins[i], &wa, &awesomeconf[real_screen]);
177 if(wins)
178 XFree(wins);
182 /** Setup everything before running
183 * \param disp Display ref
184 * \param screen Screen number
185 * \param awesomeconf awesome config ref
186 * \todo clean things...
188 static void
189 setup(Display *disp, int screen, DC *drawcontext, awesome_config *awesomeconf)
191 XSetWindowAttributes wa;
192 int real_screen;
194 if(XineramaIsActive(disp))
195 real_screen = DefaultScreen(disp);
196 else
197 real_screen = screen;
199 /* init cursors */
200 drawcontext->cursor[CurNormal] = XCreateFontCursor(disp, XC_left_ptr);
201 drawcontext->cursor[CurResize] = XCreateFontCursor(disp, XC_sizing);
202 drawcontext->cursor[CurMove] = XCreateFontCursor(disp, XC_fleur);
204 /* select for events */
205 wa.event_mask = SubstructureRedirectMask | SubstructureNotifyMask
206 | EnterWindowMask | LeaveWindowMask | StructureNotifyMask;
207 wa.cursor = drawcontext->cursor[CurNormal];
209 XChangeWindowAttributes(disp, RootWindow(disp, real_screen), CWEventMask | CWCursor, &wa);
211 XSelectInput(disp, RootWindow(disp, real_screen), wa.event_mask);
213 grabkeys(disp, real_screen, awesomeconf);
215 compileregs(awesomeconf->rules, awesomeconf->nrules);
217 /* bar */
218 drawcontext->h = awesomeconf->statusbar.height = drawcontext->font.height + 2;
219 initstatusbar(disp, screen, drawcontext, &awesomeconf->statusbar);
220 drawcontext->gc = XCreateGC(disp, RootWindow(disp, real_screen), 0, 0);
221 XSetLineAttributes(disp, drawcontext->gc, 1, LineSolid, CapButt, JoinMiter);
223 if(!drawcontext->font.set)
224 XSetFont(disp, drawcontext->gc, drawcontext->font.xfont->fid);
226 loadawesomeprops(disp, awesomeconf);
229 /** Startup Error handler to check if another window manager
230 * is already running.
231 * \param disp Display ref
232 * \param ee Error event
234 static int __attribute__ ((noreturn))
235 xerrorstart(Display * disp __attribute__ ((unused)), XErrorEvent * ee __attribute__ ((unused)))
237 eprint("awesome: another window manager is already running\n");
240 /** Quit awesome
241 * \param disp Display ref
242 * \param screen Screen number
243 * \param drawcontext Drawcontext ref
244 * \param awesomeconf awesome config
245 * \param arg nothing
246 * \ingroup ui_callback
248 void
249 uicb_quit(Display *disp __attribute__ ((unused)),
250 DC *drawcontext __attribute__ ((unused)),
251 awesome_config *awesomeconf __attribute__((unused)),
252 const char *arg __attribute__ ((unused)))
254 readin = running = False;
257 /* There's no way to check accesses to destroyed windows, thus those cases are
258 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
259 * default error handler, which may call exit.
262 xerror(Display * edpy, XErrorEvent * ee)
264 if(ee->error_code == BadWindow
265 || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
266 || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
267 || (ee->request_code == X_PolyFillRectangle
268 && ee->error_code == BadDrawable)
269 || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
270 || (ee->request_code == X_ConfigureWindow
271 && ee->error_code == BadMatch) || (ee->request_code == X_GrabKey
272 && ee->error_code == BadAccess)
273 || (ee->request_code == X_CopyArea && ee->error_code == BadDrawable))
274 return 0;
275 fprintf(stderr, "awesome: fatal error: request code=%d, error code=%d\n",
276 ee->request_code, ee->error_code);
278 return xerrorxlib(edpy, ee); /* may call exit */
281 /** Hello, this is main
282 * \param argc who knows
283 * \param argv who knows
284 * \return EXIT_SUCCESS I hope
286 typedef void event_handler (XEvent *, awesome_config *);
288 main(int argc, char *argv[])
290 char *p;
291 const char *confpath = NULL;
292 int r, xfd, e_dummy;
293 fd_set rd;
294 XEvent ev;
295 Display * dpy;
296 awesome_config *awesomeconf;
297 int shape_event, randr_event_base;
298 int screen;
299 enum { NetSupported, NetWMName, NetLast }; /* EWMH atoms */
300 Atom netatom[NetLast];
301 event_handler **handler;
303 if(argc >= 2)
305 if(!a_strcmp("-v", argv[1]))
307 printf("awesome-" VERSION " © 2007 Julien Danjou\n");
308 return EXIT_SUCCESS;
310 else if(!a_strcmp("-c", argv[1]))
312 if(a_strlen(argv[2]))
313 confpath = argv[2];
314 else
315 eprint("awesome: -c require a file\n");
317 else
318 eprint("usage: awesome [-v | -c configfile]\n");
321 /* Tag won't be printed otherwised */
322 setlocale(LC_CTYPE, "");
324 if(!(dpy = XOpenDisplay(NULL)))
325 eprint("awesome: cannot open display\n");
327 xfd = ConnectionNumber(dpy);
329 XSetErrorHandler(xerrorstart);
330 for(screen = 0; screen < ScreenCount(dpy); screen++)
331 /* this causes an error if some other window manager is running */
332 XSelectInput(dpy, RootWindow(dpy, screen), SubstructureRedirectMask);
334 /* need to XSync to validate errorhandler */
335 XSync(dpy, False);
336 XSetErrorHandler(NULL);
337 xerrorxlib = XSetErrorHandler(xerror);
338 XSync(dpy, False);
340 netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
341 netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
343 /* allocate stuff */
344 dc = p_new(DC, get_screen_count(dpy));
345 awesomeconf = p_new(awesome_config, get_screen_count(dpy));
347 for(screen = 0; screen < get_screen_count(dpy); screen++)
349 parse_config(dpy, screen, &dc[screen], confpath, &awesomeconf[screen]);
350 setup(dpy, screen, &dc[screen], &awesomeconf[screen]);
351 XChangeProperty(dpy, RootWindow(dpy, screen), netatom[NetSupported],
352 XA_ATOM, 32, PropModeReplace, (unsigned char *) netatom, NetLast);
353 drawstatusbar(dpy, &dc[screen], &awesomeconf[screen]);
356 handler = p_new(event_handler *, LASTEvent);
357 handler[ButtonPress] = handle_event_buttonpress;
358 handler[ConfigureRequest] = handle_event_configurerequest;
359 handler[ConfigureNotify] = handle_event_configurenotify;
360 handler[DestroyNotify] = handle_event_destroynotify;
361 handler[EnterNotify] = handle_event_enternotify;
362 handler[LeaveNotify] = handle_event_leavenotify;
363 handler[Expose] = handle_event_expose;
364 handler[KeyPress] = handle_event_keypress;
365 handler[MappingNotify] = handle_event_mappingnotify;
366 handler[MapRequest] = handle_event_maprequest;
367 handler[PropertyNotify] = handle_event_propertynotify;
368 handler[UnmapNotify] = handle_event_unmapnotify;
370 /* check for shape extension */
371 if((awesomeconf[0].have_shape = XShapeQueryExtension(dpy, &shape_event, &e_dummy)))
373 p_realloc(&handler, shape_event + 1);
374 handler[shape_event] = handle_event_shape;
377 /* check for randr extension */
378 if((awesomeconf[0].have_randr = XRRQueryExtension(dpy, &randr_event_base, &e_dummy)))
380 p_realloc(&handler, randr_event_base + RRScreenChangeNotify + 1);
381 handler[randr_event_base + RRScreenChangeNotify] = handle_event_randr_screen_change_notify;
384 for(screen = 0; screen < get_screen_count(dpy); screen++)
386 awesomeconf[screen].have_shape = awesomeconf[0].have_shape;
387 awesomeconf[screen].have_randr = awesomeconf[0].have_randr;
390 scan(dpy, dc, awesomeconf);
392 XSync(dpy, False);
394 /* main event loop, also reads status text from stdin */
395 while(running)
397 FD_ZERO(&rd);
398 if(readin)
399 FD_SET(STDIN_FILENO, &rd);
400 FD_SET(xfd, &rd);
401 if(select(xfd + 1, &rd, NULL, NULL, NULL) == -1)
403 if(errno == EINTR)
404 continue;
405 eprint("select failed\n");
407 if(FD_ISSET(STDIN_FILENO, &rd))
409 switch (r = read(STDIN_FILENO, awesomeconf[0].statustext, sizeof(awesomeconf[0].statustext) - 1))
411 case -1:
412 a_strncpy(awesomeconf[0].statustext, sizeof(awesomeconf[0].statustext),
413 strerror(errno), sizeof(awesomeconf[0].statustext) - 1);
414 awesomeconf[0].statustext[sizeof(awesomeconf[0].statustext) - 1] = '\0';
415 readin = False;
416 break;
417 case 0:
418 a_strncpy(awesomeconf[0].statustext, sizeof(awesomeconf[0].statustext),
419 "EOF", 4);
420 readin = False;
421 break;
422 default:
423 for(awesomeconf[0].statustext[r] = '\0', p = awesomeconf[0].statustext + a_strlen(awesomeconf[0].statustext) - 1;
424 p >= awesomeconf[0].statustext && *p == '\n'; *p-- = '\0');
425 for(; p >= awesomeconf[0].statustext && *p != '\n'; --p);
426 if(p > awesomeconf[0].statustext)
427 a_strncpy(awesomeconf[0].statustext, sizeof(awesomeconf[0].statustext),
428 p + 1, sizeof(awesomeconf[0].statustext));
430 drawstatusbar(dpy, &dc[0], &awesomeconf[0]);
433 while(XPending(dpy))
435 XNextEvent(dpy, &ev);
436 if(handler[ev.type])
437 handler[ev.type](&ev, awesomeconf); /* call handler */
440 cleanup(dpy, dc, awesomeconf);
441 XCloseDisplay(dpy);
443 return EXIT_SUCCESS;