don't stick textbox size to the first textsize sent via widget_tell
[awesome.git] / awesome.c
blob47b2e0f2bab26d28f99c5995d01330a8ee210c2a
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 <signal.h>
33 #include <X11/cursorfont.h>
34 #include <X11/keysym.h>
35 #include <X11/Xatom.h>
36 #include <X11/Xproto.h>
37 #include <X11/Xutil.h>
38 #include <X11/extensions/shape.h>
39 #include <X11/extensions/Xrandr.h>
41 #include "awesome.h"
42 #include "event.h"
43 #include "layout.h"
44 #include "screen.h"
45 #include "util.h"
46 #include "statusbar.h"
47 #include "uicb.h"
48 #include "window.h"
49 #include "client.h"
50 #include "focus.h"
51 #include "ewmh.h"
52 #include "awesome-client.h"
54 static int (*xerrorxlib) (Display *, XErrorEvent *);
55 static Bool running = True;
57 AwesomeConf globalconf;
59 static inline void
60 cleanup_buttons(Button *buttons)
62 Button *b, *bn;
64 for(b = buttons; b; b = bn)
66 bn = b->next;
67 p_delete(&b->arg);
68 p_delete(&b);
72 static void
73 cleanup_screen(int screen)
75 Layout *l, *ln;
76 Tag *t, *tn;
78 XftFontClose(globalconf.display, globalconf.screens[screen].font);
79 XUngrabKey(globalconf.display, AnyKey, AnyModifier, RootWindow(globalconf.display, get_phys_screen(screen)));
80 XDestroyWindow(globalconf.display, globalconf.screens[screen].statusbar->window);
82 for(t = globalconf.screens[screen].tags; t; t = tn)
84 tn = t->next;
85 p_delete(&t->name);
86 p_delete(&t);
89 for(l = globalconf.screens[screen].layouts; l; l = ln)
91 ln = l->next;
92 p_delete(&l->image);
93 p_delete(&l);
97 /** Cleanup everything on quit
98 * \param awesomeconf awesome config
100 static void
101 cleanup()
103 int screen;
104 Rule *r, *rn;
105 Key *k, *kn;
107 while(globalconf.clients)
109 client_unban(globalconf.clients);
110 client_unmanage(globalconf.clients, NormalState);
113 XFreeCursor(globalconf.display, globalconf.cursor[CurNormal]);
114 XFreeCursor(globalconf.display, globalconf.cursor[CurResize]);
115 XFreeCursor(globalconf.display, globalconf.cursor[CurMove]);
117 for(r = globalconf.rules; r; r = rn)
119 rn = r->next;
120 p_delete(&r->prop);
121 p_delete(&r->tags);
122 p_delete(&r->propregex);
123 p_delete(&r->tagregex);
124 p_delete(&r);
127 for(k = globalconf.keys; k; k = kn)
129 kn = k->next;
130 p_delete(&k->arg);
131 p_delete(&k);
134 cleanup_buttons(globalconf.buttons.root);
135 cleanup_buttons(globalconf.buttons.client);
137 p_delete(&globalconf.configpath);
139 for(screen = 0; screen < get_screen_count(); screen++)
140 cleanup_screen(screen);
142 XSetInputFocus(globalconf.display, PointerRoot, RevertToPointerRoot, CurrentTime);
143 XSync(globalconf.display, False);
145 p_delete(&globalconf.clients);
148 /** Scan X to find windows to manage
150 static void
151 scan()
153 unsigned int i, num;
154 int screen, real_screen;
155 Window *wins = NULL, d1, d2;
156 XWindowAttributes wa;
158 for(screen = 0; screen < ScreenCount(globalconf.display); screen++)
160 if(XQueryTree(globalconf.display, RootWindow(globalconf.display, screen), &d1, &d2, &wins, &num))
162 real_screen = screen;
163 for(i = 0; i < num; i++)
165 if(!XGetWindowAttributes(globalconf.display, wins[i], &wa)
166 || wa.override_redirect
167 || XGetTransientForHint(globalconf.display, wins[i], &d1))
168 continue;
169 if(wa.map_state == IsViewable || window_getstate(wins[i]) == IconicState)
171 if(screen == 0)
172 real_screen = get_screen_bycoord(wa.x, wa.y);
173 client_manage(wins[i], &wa, real_screen);
176 /* now the transients */
177 for(i = 0; i < num; i++)
179 if(!XGetWindowAttributes(globalconf.display, wins[i], &wa))
180 continue;
181 if(XGetTransientForHint(globalconf.display, wins[i], &d1)
182 && (wa.map_state == IsViewable || window_getstate(wins[i]) == IconicState))
184 if(screen == 0)
185 real_screen = get_screen_bycoord(wa.x, wa.y);
186 client_manage(wins[i], &wa, real_screen);
190 if(wins)
191 XFree(wins);
195 /** Setup everything before running
196 * \param screen Screen number
197 * \todo clean things...
199 static void
200 setup(int screen)
202 XSetWindowAttributes wa;
204 /* init cursors */
205 globalconf.cursor[CurNormal] = XCreateFontCursor(globalconf.display, XC_left_ptr);
206 globalconf.cursor[CurResize] = XCreateFontCursor(globalconf.display, XC_sizing);
207 globalconf.cursor[CurMove] = XCreateFontCursor(globalconf.display, XC_fleur);
209 /* select for events */
210 wa.event_mask = SubstructureRedirectMask | SubstructureNotifyMask
211 | EnterWindowMask | LeaveWindowMask | StructureNotifyMask;
212 wa.cursor = globalconf.cursor[CurNormal];
214 XChangeWindowAttributes(globalconf.display,
215 RootWindow(globalconf.display, get_phys_screen(screen)),
216 CWEventMask | CWCursor, &wa);
218 XSelectInput(globalconf.display,
219 RootWindow(globalconf.display, get_phys_screen(screen)),
220 wa.event_mask);
222 grabkeys(get_phys_screen(screen));
225 /** Startup Error handler to check if another window manager
226 * is already running.
227 * \param disp Display
228 * \param ee Error event
230 static int __attribute__ ((noreturn))
231 xerrorstart(Display * disp __attribute__ ((unused)),
232 XErrorEvent * ee __attribute__ ((unused)))
234 eprint("another window manager is already running\n");
237 /** Quit awesome
238 * \param screen Screen ID
239 * \param arg nothing
240 * \ingroup ui_callback
242 void
243 uicb_quit(int screen __attribute__ ((unused)), char *arg __attribute__ ((unused)))
245 running = False;
248 static void
249 exit_on_signal(int sig __attribute__ ((unused)))
251 running = False;
254 /* There's no way to check accesses to destroyed windows, thus those cases are
255 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
256 * default error handler, which may call exit.
259 xerror(Display * edpy, XErrorEvent * ee)
261 if(ee->error_code == BadWindow
262 || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
263 || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
264 || (ee->request_code == X_PolyFillRectangle
265 && ee->error_code == BadDrawable)
266 || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
267 || (ee->request_code == X_ConfigureWindow
268 && ee->error_code == BadMatch) || (ee->request_code == X_GrabKey
269 && ee->error_code == BadAccess)
270 || (ee->request_code == X_CopyArea && ee->error_code == BadDrawable))
271 return 0;
272 warn("fatal error: request code=%d, error code=%d\n", ee->request_code, ee->error_code);
273 return xerrorxlib(edpy, ee); /* may call exit */
276 /** Hello, this is main
277 * \param argc who knows
278 * \param argv who knows
279 * \return EXIT_SUCCESS I hope
281 typedef void event_handler (XEvent *);
283 main(int argc, char *argv[])
285 char buf[1024];
286 const char *confpath = NULL;
287 int r, xfd, e_dummy, csfd;
288 fd_set rd;
289 XEvent ev;
290 Display * dpy;
291 int shape_event, randr_event_base;
292 int screen;
293 event_handler **handler;
294 struct sockaddr_un *addr;
295 Statusbar *statusbar;
297 if(argc >= 2)
299 if(!a_strcmp("-v", argv[1]) || !a_strcmp("--version", argv[1]))
301 printf("awesome-" VERSION " (" RELEASE ")\n");
302 return EXIT_SUCCESS;
304 else if(!a_strcmp("-c", argv[1]))
306 if(a_strlen(argv[2]))
307 confpath = argv[2];
308 else
309 eprint("-c require a file\n");
311 else
312 eprint("options: [-v | -c configfile]\n");
315 /* Tag won't be printed otherwised */
316 setlocale(LC_CTYPE, "");
318 if(!(dpy = XOpenDisplay(NULL)))
319 eprint("cannot open display\n");
321 xfd = ConnectionNumber(dpy);
323 XSetErrorHandler(xerrorstart);
324 for(screen = 0; screen < ScreenCount(dpy); screen++)
325 /* this causes an error if some other window manager is running */
326 XSelectInput(dpy, RootWindow(dpy, screen), SubstructureRedirectMask);
328 /* need to XSync to validate errorhandler */
329 XSync(dpy, False);
330 XSetErrorHandler(NULL);
331 xerrorxlib = XSetErrorHandler(xerror);
332 XSync(dpy, False);
333 globalconf.display = dpy;
335 ewmh_init_atoms();
337 globalconf.screens = p_new(VirtScreen, get_screen_count());
338 focus_add_client(NULL);
339 /* store display */
340 config_parse(confpath);
342 for(screen = 0; screen < get_screen_count(); screen++)
344 /* set screen */
345 setup(screen);
346 for(statusbar = globalconf.screens[screen].statusbar; statusbar; statusbar = statusbar->next)
347 statusbar_init(statusbar, screen);
350 /* do this only for real screen */
351 for(screen = 0; screen < ScreenCount(dpy); screen++)
353 loadawesomeprops(screen);
354 ewmh_set_supported_hints(screen);
357 handler = p_new(event_handler *, LASTEvent);
358 handler[ButtonPress] = handle_event_buttonpress;
359 handler[ConfigureRequest] = handle_event_configurerequest;
360 handler[ConfigureNotify] = handle_event_configurenotify;
361 handler[DestroyNotify] = handle_event_destroynotify;
362 handler[EnterNotify] = handle_event_enternotify;
363 handler[LeaveNotify] = handle_event_leavenotify;
364 handler[Expose] = handle_event_expose;
365 handler[KeyPress] = handle_event_keypress;
366 handler[MappingNotify] = handle_event_mappingnotify;
367 handler[MapRequest] = handle_event_maprequest;
368 handler[PropertyNotify] = handle_event_propertynotify;
369 handler[UnmapNotify] = handle_event_unmapnotify;
370 handler[ClientMessage] = handle_event_clientmessage;
372 /* check for shape extension */
373 if((globalconf.have_shape = XShapeQueryExtension(dpy, &shape_event, &e_dummy)))
375 p_realloc(&handler, shape_event + 1);
376 handler[shape_event] = handle_event_shape;
379 /* check for randr extension */
380 if((globalconf.have_randr = XRRQueryExtension(dpy, &randr_event_base, &e_dummy)))
382 p_realloc(&handler, randr_event_base + RRScreenChangeNotify + 1);
383 handler[randr_event_base + RRScreenChangeNotify] = handle_event_randr_screen_change_notify;
386 scan();
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 /* register function for signals */
408 signal(SIGINT, &exit_on_signal);
409 signal(SIGTERM, &exit_on_signal);
410 signal(SIGHUP, &exit_on_signal);
412 /* main event loop, also reads status text from socket */
413 while(running)
415 FD_ZERO(&rd);
416 if(csfd >= 0)
417 FD_SET(csfd, &rd);
418 FD_SET(xfd, &rd);
419 if(select(MAX(xfd, csfd) + 1, &rd, NULL, NULL, NULL) == -1)
421 if(errno == EINTR)
422 continue;
423 eprint("select failed\n");
425 if(csfd >= 0 && FD_ISSET(csfd, &rd))
426 switch (r = recv(csfd, buf, sizeof(buf)-1, MSG_TRUNC))
428 case -1:
429 perror("awesome: error reading UNIX domain socket");
430 csfd = -1;
431 break;
432 case 0:
433 break;
434 default:
435 if(r >= ssizeof(buf))
436 break;
437 buf[r] = '\0';
438 parse_control(buf);
441 while(XPending(dpy))
443 XNextEvent(dpy, &ev);
444 if(handler[ev.type])
445 handler[ev.type](&ev); /* call handler */
449 if(csfd > 0 && close(csfd))
450 perror("error closing UNIX domain socket");
451 if(unlink(addr->sun_path))
452 perror("error unlinking UNIX domain socket");
453 p_delete(&addr);
455 cleanup();
456 XCloseDisplay(dpy);
458 return EXIT_SUCCESS;
460 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80