add auto option to float in rules
[awesome.git] / awesome.c
blob5511d2b673710e98dc3c2b33b7401c9045bb75cd
1 /*
2 * awesome.c - awesome main functions
4 * Copyright © 2007-2008 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 /** Scan X to find windows to manage
61 static void
62 scan()
64 unsigned int i, num;
65 int screen, real_screen;
66 Window *wins = NULL, d1, d2;
67 XWindowAttributes wa;
69 for(screen = 0; screen < ScreenCount(globalconf.display); screen++)
71 if(XQueryTree(globalconf.display, RootWindow(globalconf.display, screen), &d1, &d2, &wins, &num))
73 real_screen = screen;
74 for(i = 0; i < num; i++)
76 if(!XGetWindowAttributes(globalconf.display, wins[i], &wa)
77 || wa.override_redirect
78 || XGetTransientForHint(globalconf.display, wins[i], &d1))
79 continue;
80 if(wa.map_state == IsViewable || window_getstate(wins[i]) == IconicState)
82 if(screen == 0)
83 real_screen = get_screen_bycoord(wa.x, wa.y);
84 client_manage(wins[i], &wa, real_screen);
87 /* now the transients */
88 for(i = 0; i < num; i++)
90 if(!XGetWindowAttributes(globalconf.display, wins[i], &wa))
91 continue;
92 if(XGetTransientForHint(globalconf.display, wins[i], &d1)
93 && (wa.map_state == IsViewable || window_getstate(wins[i]) == IconicState))
95 if(screen == 0)
96 real_screen = get_screen_bycoord(wa.x, wa.y);
97 client_manage(wins[i], &wa, real_screen);
101 if(wins)
102 XFree(wins);
106 /** Setup everything before running
107 * \param screen Screen number
108 * \todo clean things...
110 static void
111 setup(int screen)
113 XSetWindowAttributes wa;
114 Statusbar *statusbar;
115 int phys_screen = get_phys_screen(screen);
117 /* init cursors */
118 globalconf.cursor[CurNormal] = XCreateFontCursor(globalconf.display, XC_left_ptr);
119 globalconf.cursor[CurResize] = XCreateFontCursor(globalconf.display, XC_sizing);
120 globalconf.cursor[CurMove] = XCreateFontCursor(globalconf.display, XC_fleur);
122 /* select for events */
123 wa.event_mask = SubstructureRedirectMask | SubstructureNotifyMask
124 | EnterWindowMask | LeaveWindowMask | StructureNotifyMask;
125 wa.cursor = globalconf.cursor[CurNormal];
127 XChangeWindowAttributes(globalconf.display,
128 RootWindow(globalconf.display, phys_screen),
129 CWEventMask | CWCursor, &wa);
131 XSelectInput(globalconf.display,
132 RootWindow(globalconf.display, phys_screen),
133 wa.event_mask);
135 grabkeys(phys_screen);
137 for(statusbar = globalconf.screens[screen].statusbar; statusbar; statusbar = statusbar->next)
138 statusbar_init(statusbar, screen);
141 /** Startup Error handler to check if another window manager
142 * is already running.
143 * \param disp Display
144 * \param ee Error event
146 static int __attribute__ ((noreturn))
147 xerrorstart(Display * disp __attribute__ ((unused)),
148 XErrorEvent * ee __attribute__ ((unused)))
150 eprint("another window manager is already running\n");
153 /** Quit awesome
154 * \param screen Screen ID
155 * \param arg nothing
156 * \ingroup ui_callback
158 void
159 uicb_quit(int screen __attribute__ ((unused)), char *arg __attribute__ ((unused)))
161 running = False;
164 static void
165 exit_on_signal(int sig __attribute__ ((unused)))
167 running = False;
170 /* There's no way to check accesses to destroyed windows, thus those cases are
171 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
172 * default error handler, which may call exit.
174 static int
175 xerror(Display * edpy, XErrorEvent * ee)
177 if(ee->error_code == BadWindow
178 || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
179 || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
180 || (ee->request_code == X_PolyFillRectangle
181 && ee->error_code == BadDrawable)
182 || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
183 || (ee->request_code == X_ConfigureWindow
184 && ee->error_code == BadMatch) || (ee->request_code == X_GrabKey
185 && ee->error_code == BadAccess)
186 || (ee->request_code == X_CopyArea && ee->error_code == BadDrawable))
187 return 0;
188 warn("fatal error: request code=%d, error code=%d\n", ee->request_code, ee->error_code);
189 return xerrorxlib(edpy, ee); /* may call exit */
192 /** Hello, this is main
193 * \param argc who knows
194 * \param argv who knows
195 * \return EXIT_SUCCESS I hope
197 typedef void event_handler (XEvent *);
199 main(int argc, char *argv[])
201 char buf[1024];
202 const char *confpath = NULL;
203 int r, xfd, e_dummy, csfd;
204 fd_set rd;
205 XEvent ev;
206 Display * dpy;
207 int shape_event, randr_event_base;
208 int screen;
209 event_handler **handler;
210 struct sockaddr_un *addr;
212 /* check args */
213 if(argc >= 2)
215 if(!a_strcmp("-v", argv[1]) || !a_strcmp("--version", argv[1]))
217 printf("awesome " VERSION " (" RELEASE ")\n");
218 return EXIT_SUCCESS;
220 else if(!a_strcmp("-c", argv[1]))
222 if(a_strlen(argv[2]))
223 confpath = argv[2];
224 else
225 eprint("-c require a file\n");
227 else
228 eprint("options: [-v | -c configfile]\n");
231 /* X stuff */
232 if(!(dpy = XOpenDisplay(NULL)))
233 eprint("cannot open display\n");
235 xfd = ConnectionNumber(dpy);
237 XSetErrorHandler(xerrorstart);
238 for(screen = 0; screen < ScreenCount(dpy); screen++)
239 /* this causes an error if some other window manager is running */
240 XSelectInput(dpy, RootWindow(dpy, screen), SubstructureRedirectMask);
242 /* need to XSync to validate errorhandler */
243 XSync(dpy, False);
244 XSetErrorHandler(NULL);
245 xerrorxlib = XSetErrorHandler(xerror);
246 XSync(dpy, False);
248 /* store display */
249 globalconf.display = dpy;
251 /* init EWMH atoms */
252 ewmh_init_atoms();
254 /* init screens struct */
255 globalconf.screens = p_new(VirtScreen, get_screen_count());
256 focus_add_client(NULL);
258 /* parse config */
259 config_parse(confpath);
261 /* for each virtual screen */
262 for(screen = 0; screen < get_screen_count(); screen++)
263 setup(screen);
265 /* do this only for real screen */
266 for(screen = 0; screen < ScreenCount(dpy); screen++)
268 loadawesomeprops(screen);
269 ewmh_set_supported_hints(screen);
272 handler = p_new(event_handler *, LASTEvent);
273 handler[ButtonPress] = handle_event_buttonpress;
274 handler[ConfigureRequest] = handle_event_configurerequest;
275 handler[ConfigureNotify] = handle_event_configurenotify;
276 handler[DestroyNotify] = handle_event_destroynotify;
277 handler[EnterNotify] = handle_event_enternotify;
278 handler[LeaveNotify] = handle_event_leavenotify;
279 handler[Expose] = handle_event_expose;
280 handler[KeyPress] = handle_event_keypress;
281 handler[MappingNotify] = handle_event_mappingnotify;
282 handler[MapRequest] = handle_event_maprequest;
283 handler[PropertyNotify] = handle_event_propertynotify;
284 handler[UnmapNotify] = handle_event_unmapnotify;
285 handler[ClientMessage] = handle_event_clientmessage;
287 /* check for shape extension */
288 if((globalconf.have_shape = XShapeQueryExtension(dpy, &shape_event, &e_dummy)))
290 p_realloc(&handler, shape_event + 1);
291 handler[shape_event] = handle_event_shape;
294 /* check for randr extension */
295 if((globalconf.have_randr = XRRQueryExtension(dpy, &randr_event_base, &e_dummy)))
297 p_realloc(&handler, randr_event_base + RRScreenChangeNotify + 1);
298 handler[randr_event_base + RRScreenChangeNotify] = handle_event_randr_screen_change_notify;
301 scan();
303 XSync(dpy, False);
305 /* get socket fd */
306 csfd = get_client_socket();
307 addr = get_client_addr(getenv("DISPLAY"));
309 if(bind(csfd, (const struct sockaddr *) addr, SUN_LEN(addr)))
311 if(errno == EADDRINUSE)
313 if(unlink(addr->sun_path))
314 perror("error unlinking existing file");
315 if(bind(csfd, (const struct sockaddr *) addr, SUN_LEN(addr)))
316 perror("error binding UNIX domain socket");
318 else
319 perror("error binding UNIX domain socket");
322 /* register function for signals */
323 signal(SIGINT, &exit_on_signal);
324 signal(SIGTERM, &exit_on_signal);
325 signal(SIGHUP, &exit_on_signal);
327 /* main event loop, also reads status text from socket */
328 while(running)
330 FD_ZERO(&rd);
331 if(csfd >= 0)
332 FD_SET(csfd, &rd);
333 FD_SET(xfd, &rd);
334 if(select(MAX(xfd, csfd) + 1, &rd, NULL, NULL, NULL) == -1)
336 if(errno == EINTR)
337 continue;
338 eprint("select failed\n");
340 if(csfd >= 0 && FD_ISSET(csfd, &rd))
341 switch (r = recv(csfd, buf, sizeof(buf)-1, MSG_TRUNC))
343 case -1:
344 perror("awesome: error reading UNIX domain socket");
345 csfd = -1;
346 break;
347 case 0:
348 break;
349 default:
350 if(r >= ssizeof(buf))
351 break;
352 buf[r] = '\0';
353 parse_control(buf);
356 while(XPending(dpy))
358 XNextEvent(dpy, &ev);
359 if(handler[ev.type])
360 handler[ev.type](&ev); /* call handler */
364 if(csfd > 0 && close(csfd))
365 perror("error closing UNIX domain socket");
366 if(unlink(addr->sun_path))
367 perror("error unlinking UNIX domain socket");
368 p_delete(&addr);
370 XCloseDisplay(dpy);
372 return EXIT_SUCCESS;
374 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80