rename some screens function
[awesome.git] / awesome.c
blob77118f913fa0e002f6483266df2ed2642d00bcd3
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 "config.h"
42 #include "awesome.h"
43 #include "event.h"
44 #include "layout.h"
45 #include "screen.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 "common/awclient.h"
53 #include "common/util.h"
54 #include "common/awesome-version.h"
56 static int (*xerrorxlib) (Display *, XErrorEvent *);
57 static Bool running = True;
59 AwesomeConf globalconf;
61 /** Scan X to find windows to manage
63 static void
64 scan()
66 unsigned int i, num;
67 int screen, real_screen;
68 Window *wins = NULL, d1, d2;
69 XWindowAttributes wa;
71 for(screen = 0; screen < ScreenCount(globalconf.display); screen++)
73 if(XQueryTree(globalconf.display, RootWindow(globalconf.display, screen), &d1, &d2, &wins, &num))
75 real_screen = screen;
76 for(i = 0; i < num; i++)
78 /* XGetWindowAttributes return 1 on success */
79 if(!XGetWindowAttributes(globalconf.display, wins[i], &wa)
80 || wa.override_redirect
81 || XGetTransientForHint(globalconf.display, wins[i], &d1))
82 continue;
83 if(wa.map_state == IsViewable || window_getstate(wins[i]) == IconicState)
85 if(screen == 0)
86 real_screen = screen_get_bycoord(wa.x, wa.y);
87 client_manage(wins[i], &wa, real_screen);
90 /* now the transients */
91 for(i = 0; i < num; i++)
93 if(!XGetWindowAttributes(globalconf.display, wins[i], &wa))
94 continue;
95 if(XGetTransientForHint(globalconf.display, wins[i], &d1)
96 && (wa.map_state == IsViewable || window_getstate(wins[i]) == IconicState))
98 if(screen == 0)
99 real_screen = screen_get_bycoord(wa.x, wa.y);
100 client_manage(wins[i], &wa, real_screen);
104 if(wins)
105 XFree(wins);
109 /** Setup everything before running
110 * \param screen Screen number
111 * \todo clean things...
113 static void
114 setup(int screen)
116 XSetWindowAttributes wa;
117 Statusbar *statusbar;
118 int phys_screen = get_phys_screen(screen);
120 /* init cursors */
121 globalconf.cursor[CurNormal] = XCreateFontCursor(globalconf.display, XC_left_ptr);
122 globalconf.cursor[CurResize] = XCreateFontCursor(globalconf.display, XC_sizing);
123 globalconf.cursor[CurMove] = XCreateFontCursor(globalconf.display, XC_fleur);
125 /* select for events */
126 wa.event_mask = SubstructureRedirectMask | SubstructureNotifyMask
127 | EnterWindowMask | LeaveWindowMask | StructureNotifyMask;
128 wa.cursor = globalconf.cursor[CurNormal];
130 XChangeWindowAttributes(globalconf.display,
131 RootWindow(globalconf.display, phys_screen),
132 CWEventMask | CWCursor, &wa);
134 XSelectInput(globalconf.display,
135 RootWindow(globalconf.display, phys_screen),
136 wa.event_mask);
138 grabkeys(phys_screen);
140 for(statusbar = globalconf.screens[screen].statusbar; statusbar; statusbar = statusbar->next)
141 statusbar_init(statusbar);
144 /** Startup Error handler to check if another window manager
145 * is already running.
146 * \param disp Display
147 * \param ee Error event
149 static int __attribute__ ((noreturn))
150 xerrorstart(Display * disp __attribute__ ((unused)),
151 XErrorEvent * ee __attribute__ ((unused)))
153 eprint("another window manager is already running\n");
156 /** Quit awesome
157 * \param screen Screen ID
158 * \param arg nothing
159 * \ingroup ui_callback
161 void
162 uicb_quit(int screen __attribute__ ((unused)), char *arg __attribute__ ((unused)))
164 running = False;
167 static void
168 exit_on_signal(int sig __attribute__ ((unused)))
170 running = False;
173 /* There's no way to check accesses to destroyed windows, thus those cases are
174 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
175 * default error handler, which may call exit.
177 static int
178 xerror(Display * edpy, XErrorEvent * ee)
180 if(ee->error_code == BadWindow)
181 return 0;
182 warn("fatal error: request code=%d, error code=%d\n", ee->request_code, ee->error_code);
183 return xerrorxlib(edpy, ee); /* may call exit */
186 /** Print help and exit(2) with given exit_code.
188 static void __attribute__ ((noreturn))
189 exit_help(int exit_code)
191 FILE *outfile = (exit_code == EXIT_SUCCESS) ? stdout : stderr;
192 fprintf(outfile, "Usage: awesome [-v | -h | -c configfile]\n");
193 exit(exit_code);
196 /** Hello, this is main
197 * \param argc who knows
198 * \param argv who knows
199 * \return EXIT_SUCCESS I hope
201 typedef void event_handler (XEvent *);
203 main(int argc, char *argv[])
205 char buf[1024];
206 const char *confpath = NULL;
207 int r, xfd, e_dummy, csfd;
208 fd_set rd;
209 XEvent ev;
210 Display * dpy;
211 int shape_event, randr_event_base;
212 int screen;
213 int i, cmdlen;
214 event_handler **handler;
215 struct sockaddr_un *addr;
216 int args_ok = 1;
218 /* check args */
219 if(argc >= 2)
221 args_ok = 0;
222 if(!a_strcmp("-v", argv[1]) || !a_strcmp("--version", argv[1]))
223 eprint_version("awesome");
224 else if(!a_strcmp("-h", argv[1]) || !a_strcmp("--help", argv[1]))
225 exit_help(EXIT_SUCCESS);
226 else if(!a_strcmp("-c", argv[1]))
228 if(a_strlen(argv[2]))
229 confpath = argv[2], args_ok = 1;
230 else
231 eprint("-c option requires a file name\n");
233 else
234 exit_help(EXIT_FAILURE);
236 if(!args_ok)
237 exit_help(EXIT_FAILURE);
239 /* Text won't be printed correctly otherwise */
240 setlocale(LC_CTYPE, "");
242 /* X stuff */
243 if(!(dpy = XOpenDisplay(NULL)))
244 eprint("cannot open display\n");
246 for(cmdlen = 0, i = 0; i < argc; i++)
247 cmdlen += a_strlen(argv[i] + 1);
248 globalconf.argv = p_new(char, cmdlen);
249 a_strcpy(globalconf.argv, cmdlen, argv[0]);
250 for(i = 1; i < argc; i++)
252 a_strcat(globalconf.argv, cmdlen, " ");
253 a_strcat(globalconf.argv, cmdlen, argv[i]);
256 xfd = ConnectionNumber(dpy);
258 XSetErrorHandler(xerrorstart);
259 for(screen = 0; screen < ScreenCount(dpy); screen++)
260 /* this causes an error if some other window manager is running */
261 XSelectInput(dpy, RootWindow(dpy, screen), SubstructureRedirectMask);
263 /* need to XSync to validate errorhandler */
264 XSync(dpy, False);
265 XSetErrorHandler(NULL);
266 xerrorxlib = XSetErrorHandler(xerror);
267 XSync(dpy, False);
269 /* store display */
270 globalconf.display = dpy;
272 /* init EWMH atoms */
273 ewmh_init_atoms();
275 /* init screens struct */
276 screen_build_screens();
277 focus_add_client(NULL);
279 /* parse config */
280 config_parse(confpath);
282 scan();
284 /* for each virtual screen */
285 for(screen = 0; screen < globalconf.nscreen; screen++)
286 setup(screen);
288 /* do this only for real screen */
289 for(screen = 0; screen < ScreenCount(dpy); screen++)
291 loadawesomeprops(screen);
292 ewmh_set_supported_hints(screen);
293 /* call this to at least grab root window clicks */
294 window_root_grabbuttons(screen);
297 handler = p_new(event_handler *, LASTEvent);
298 handler[ButtonPress] = handle_event_buttonpress;
299 handler[ConfigureRequest] = handle_event_configurerequest;
300 handler[ConfigureNotify] = handle_event_configurenotify;
301 handler[DestroyNotify] = handle_event_destroynotify;
302 handler[EnterNotify] = handle_event_enternotify;
303 handler[LeaveNotify] = handle_event_leavenotify;
304 handler[Expose] = handle_event_expose;
305 handler[KeyPress] = handle_event_keypress;
306 handler[MappingNotify] = handle_event_mappingnotify;
307 handler[MapRequest] = handle_event_maprequest;
308 handler[PropertyNotify] = handle_event_propertynotify;
309 handler[UnmapNotify] = handle_event_unmapnotify;
310 handler[ClientMessage] = handle_event_clientmessage;
312 /* check for shape extension */
313 if((globalconf.have_shape = XShapeQueryExtension(dpy, &shape_event, &e_dummy)))
315 p_realloc(&handler, shape_event + 1);
316 handler[shape_event] = handle_event_shape;
319 /* check for randr extension */
320 if((globalconf.have_randr = XRRQueryExtension(dpy, &randr_event_base, &e_dummy)))
322 p_realloc(&handler, randr_event_base + RRScreenChangeNotify + 1);
323 handler[randr_event_base + RRScreenChangeNotify] = handle_event_randr_screen_change_notify;
326 XSync(dpy, False);
328 /* get socket fd */
329 csfd = get_client_socket();
330 addr = get_client_addr(getenv("DISPLAY"));
332 if(bind(csfd, (const struct sockaddr *) addr, SUN_LEN(addr)))
334 if(errno == EADDRINUSE)
336 if(unlink(addr->sun_path))
337 perror("error unlinking existing file");
338 if(bind(csfd, (const struct sockaddr *) addr, SUN_LEN(addr)))
339 perror("error binding UNIX domain socket");
341 else
342 perror("error binding UNIX domain socket");
345 /* register function for signals */
346 signal(SIGINT, &exit_on_signal);
347 signal(SIGTERM, &exit_on_signal);
348 signal(SIGHUP, &exit_on_signal);
350 /* refresh everything before waiting events */
351 statusbar_refresh();
352 layout_refresh();
354 /* main event loop, also reads status text from socket */
355 while(running)
357 FD_ZERO(&rd);
358 if(csfd >= 0)
359 FD_SET(csfd, &rd);
360 FD_SET(xfd, &rd);
361 if(select(MAX(xfd, csfd) + 1, &rd, NULL, NULL, NULL) == -1)
363 if(errno == EINTR)
364 continue;
365 eprint("select failed\n");
367 if(csfd >= 0 && FD_ISSET(csfd, &rd))
368 switch (r = recv(csfd, buf, sizeof(buf)-1, MSG_TRUNC))
370 case -1:
371 perror("awesome: error reading UNIX domain socket");
372 csfd = -1;
373 break;
374 case 0:
375 break;
376 default:
377 if(r >= ssizeof(buf))
378 break;
379 buf[r] = '\0';
380 parse_control(buf);
381 statusbar_refresh();
382 layout_refresh();
384 /* two level XPending:
385 * we need to first check we have XEvent to handle
386 * and if so, we handle them all in a round.
387 * Then when we have refresh()'ed stuff so maybe new XEvent
388 * are available and select() won't tell us, so let's check
389 * with XPending() again.
391 while(XPending(dpy))
393 while(XPending(dpy))
395 XNextEvent(dpy, &ev);
396 if(handler[ev.type])
397 handler[ev.type](&ev); /* call handler */
399 /* drop events requested to */
400 if(globalconf.drop_events)
402 /* need to resync */
403 XSync(dpy, False);
404 while(XCheckMaskEvent(dpy, globalconf.drop_events, &ev));
405 globalconf.drop_events = NoEventMask;
408 /* need to resync */
409 XSync(dpy, False);
412 statusbar_refresh();
413 layout_refresh();
415 /* need to resync */
416 XSync(dpy, False);
420 if(csfd > 0 && close(csfd))
421 perror("error closing UNIX domain socket");
422 if(unlink(addr->sun_path))
423 perror("error unlinking UNIX domain socket");
424 p_delete(&addr);
426 XCloseDisplay(dpy);
428 return EXIT_SUCCESS;
430 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80