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.
26 #include <sys/select.h>
27 #include <sys/types.h>
29 #include <sys/socket.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 "common/awesome-version.h"
46 #include "statusbar.h"
52 #include "awesome-client.h"
53 #include "common/util.h"
55 static int (*xerrorxlib
) (Display
*, XErrorEvent
*);
56 static Bool running
= True
;
58 AwesomeConf globalconf
;
60 /** Scan X to find windows to manage
66 int screen
, real_screen
;
67 Window
*wins
= NULL
, d1
, d2
;
70 for(screen
= 0; screen
< ScreenCount(globalconf
.display
); screen
++)
72 if(XQueryTree(globalconf
.display
, RootWindow(globalconf
.display
, screen
), &d1
, &d2
, &wins
, &num
))
75 for(i
= 0; i
< num
; i
++)
77 /* XGetWindowAttributes return 1 on success */
78 if(!XGetWindowAttributes(globalconf
.display
, wins
[i
], &wa
)
79 || wa
.override_redirect
80 || XGetTransientForHint(globalconf
.display
, wins
[i
], &d1
))
82 if(wa
.map_state
== IsViewable
|| window_getstate(wins
[i
]) == IconicState
)
85 real_screen
= get_screen_bycoord(wa
.x
, wa
.y
);
86 client_manage(wins
[i
], &wa
, real_screen
);
89 /* now the transients */
90 for(i
= 0; i
< num
; i
++)
92 if(!XGetWindowAttributes(globalconf
.display
, wins
[i
], &wa
))
94 if(XGetTransientForHint(globalconf
.display
, wins
[i
], &d1
)
95 && (wa
.map_state
== IsViewable
|| window_getstate(wins
[i
]) == IconicState
))
98 real_screen
= get_screen_bycoord(wa
.x
, wa
.y
);
99 client_manage(wins
[i
], &wa
, real_screen
);
108 /** Setup everything before running
109 * \param screen Screen number
110 * \todo clean things...
115 XSetWindowAttributes wa
;
116 Statusbar
*statusbar
;
117 int phys_screen
= get_phys_screen(screen
);
120 globalconf
.cursor
[CurNormal
] = XCreateFontCursor(globalconf
.display
, XC_left_ptr
);
121 globalconf
.cursor
[CurResize
] = XCreateFontCursor(globalconf
.display
, XC_sizing
);
122 globalconf
.cursor
[CurMove
] = XCreateFontCursor(globalconf
.display
, XC_fleur
);
124 /* select for events */
125 wa
.event_mask
= SubstructureRedirectMask
| SubstructureNotifyMask
126 | EnterWindowMask
| LeaveWindowMask
| StructureNotifyMask
;
127 wa
.cursor
= globalconf
.cursor
[CurNormal
];
129 XChangeWindowAttributes(globalconf
.display
,
130 RootWindow(globalconf
.display
, phys_screen
),
131 CWEventMask
| CWCursor
, &wa
);
133 XSelectInput(globalconf
.display
,
134 RootWindow(globalconf
.display
, phys_screen
),
137 grabkeys(phys_screen
);
139 for(statusbar
= globalconf
.screens
[screen
].statusbar
; statusbar
; statusbar
= statusbar
->next
)
140 statusbar_init(statusbar
);
143 /** Startup Error handler to check if another window manager
144 * is already running.
145 * \param disp Display
146 * \param ee Error event
148 static int __attribute__ ((noreturn
))
149 xerrorstart(Display
* disp
__attribute__ ((unused
)),
150 XErrorEvent
* ee
__attribute__ ((unused
)))
152 eprint("another window manager is already running\n");
156 * \param screen Screen ID
158 * \ingroup ui_callback
161 uicb_quit(int screen
__attribute__ ((unused
)), char *arg
__attribute__ ((unused
)))
167 exit_on_signal(int sig
__attribute__ ((unused
)))
172 /* There's no way to check accesses to destroyed windows, thus those cases are
173 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
174 * default error handler, which may call exit.
177 xerror(Display
* edpy
, XErrorEvent
* ee
)
179 if(ee
->error_code
== BadWindow
)
181 warn("fatal error: request code=%d, error code=%d\n", ee
->request_code
, ee
->error_code
);
182 return xerrorxlib(edpy
, ee
); /* may call exit */
185 /** Print help and exit(2) with given exit_code.
187 static void __attribute__ ((noreturn
))
188 exit_help(int exit_code
)
190 FILE *outfile
= (exit_code
== EXIT_SUCCESS
) ? stdout
: stderr
;
191 fprintf(outfile
, "Usage: awesome [-v | -h | -c configfile]\n");
195 /** Hello, this is main
196 * \param argc who knows
197 * \param argv who knows
198 * \return EXIT_SUCCESS I hope
200 typedef void event_handler (XEvent
*);
202 main(int argc
, char *argv
[])
205 const char *confpath
= NULL
;
206 int r
, xfd
, e_dummy
, csfd
;
210 int shape_event
, randr_event_base
;
213 event_handler
**handler
;
214 struct sockaddr_un
*addr
;
221 if(!a_strcmp("-v", argv
[1]) || !a_strcmp("--version", argv
[1]))
222 eprint_version("awesome");
223 else if(!a_strcmp("-h", argv
[1]) || !a_strcmp("--help", argv
[1]))
224 exit_help(EXIT_SUCCESS
);
225 else if(!a_strcmp("-c", argv
[1]))
227 if(a_strlen(argv
[2]))
228 confpath
= argv
[2], args_ok
= 1;
230 eprint("-c option requires a file name\n");
233 exit_help(EXIT_FAILURE
);
236 exit_help(EXIT_FAILURE
);
238 /* Text won't be printed correctly otherwise */
239 setlocale(LC_CTYPE
, "");
242 if(!(dpy
= XOpenDisplay(NULL
)))
243 eprint("cannot open display\n");
245 for(cmdlen
= 0, i
= 0; i
< argc
; i
++)
246 cmdlen
+= a_strlen(argv
[i
] + 1);
247 globalconf
.argv
= p_new(char, cmdlen
);
248 a_strcpy(globalconf
.argv
, cmdlen
, argv
[0]);
249 for(i
= 1; i
< argc
; i
++)
251 a_strcat(globalconf
.argv
, cmdlen
, " ");
252 a_strcat(globalconf
.argv
, cmdlen
, argv
[i
]);
255 xfd
= ConnectionNumber(dpy
);
257 XSetErrorHandler(xerrorstart
);
258 for(screen
= 0; screen
< ScreenCount(dpy
); screen
++)
259 /* this causes an error if some other window manager is running */
260 XSelectInput(dpy
, RootWindow(dpy
, screen
), SubstructureRedirectMask
);
262 /* need to XSync to validate errorhandler */
264 XSetErrorHandler(NULL
);
265 xerrorxlib
= XSetErrorHandler(xerror
);
269 globalconf
.display
= dpy
;
271 /* init EWMH atoms */
274 /* init screens struct */
275 screen_build_screens();
276 focus_add_client(NULL
);
279 config_parse(confpath
);
283 /* for each virtual screen */
284 for(screen
= 0; screen
< globalconf
.nscreen
; screen
++)
287 /* do this only for real screen */
288 for(screen
= 0; screen
< ScreenCount(dpy
); screen
++)
290 loadawesomeprops(screen
);
291 ewmh_set_supported_hints(screen
);
292 /* call this to at least grab root window clicks */
293 window_root_grabbuttons(screen
);
296 handler
= p_new(event_handler
*, LASTEvent
);
297 handler
[ButtonPress
] = handle_event_buttonpress
;
298 handler
[ConfigureRequest
] = handle_event_configurerequest
;
299 handler
[ConfigureNotify
] = handle_event_configurenotify
;
300 handler
[DestroyNotify
] = handle_event_destroynotify
;
301 handler
[EnterNotify
] = handle_event_enternotify
;
302 handler
[LeaveNotify
] = handle_event_leavenotify
;
303 handler
[Expose
] = handle_event_expose
;
304 handler
[KeyPress
] = handle_event_keypress
;
305 handler
[MappingNotify
] = handle_event_mappingnotify
;
306 handler
[MapRequest
] = handle_event_maprequest
;
307 handler
[PropertyNotify
] = handle_event_propertynotify
;
308 handler
[UnmapNotify
] = handle_event_unmapnotify
;
309 handler
[ClientMessage
] = handle_event_clientmessage
;
311 /* check for shape extension */
312 if((globalconf
.have_shape
= XShapeQueryExtension(dpy
, &shape_event
, &e_dummy
)))
314 p_realloc(&handler
, shape_event
+ 1);
315 handler
[shape_event
] = handle_event_shape
;
318 /* check for randr extension */
319 if((globalconf
.have_randr
= XRRQueryExtension(dpy
, &randr_event_base
, &e_dummy
)))
321 p_realloc(&handler
, randr_event_base
+ RRScreenChangeNotify
+ 1);
322 handler
[randr_event_base
+ RRScreenChangeNotify
] = handle_event_randr_screen_change_notify
;
328 csfd
= get_client_socket();
329 addr
= get_client_addr(getenv("DISPLAY"));
331 if(bind(csfd
, (const struct sockaddr
*) addr
, SUN_LEN(addr
)))
333 if(errno
== EADDRINUSE
)
335 if(unlink(addr
->sun_path
))
336 perror("error unlinking existing file");
337 if(bind(csfd
, (const struct sockaddr
*) addr
, SUN_LEN(addr
)))
338 perror("error binding UNIX domain socket");
341 perror("error binding UNIX domain socket");
344 /* register function for signals */
345 signal(SIGINT
, &exit_on_signal
);
346 signal(SIGTERM
, &exit_on_signal
);
347 signal(SIGHUP
, &exit_on_signal
);
349 /* refresh everything before waiting events */
353 /* main event loop, also reads status text from socket */
360 if(select(MAX(xfd
, csfd
) + 1, &rd
, NULL
, NULL
, NULL
) == -1)
364 eprint("select failed\n");
366 if(csfd
>= 0 && FD_ISSET(csfd
, &rd
))
367 switch (r
= recv(csfd
, buf
, sizeof(buf
)-1, MSG_TRUNC
))
370 perror("awesome: error reading UNIX domain socket");
376 if(r
>= ssizeof(buf
))
383 /* two level XPending:
384 * we need to first check we have XEvent to handle
385 * and if so, we handle them all in a round.
386 * Then when we have refresh()'ed stuff so maybe new XEvent
387 * are available and select() won't tell us, so let's check
388 * with XPending() again.
394 XNextEvent(dpy
, &ev
);
396 handler
[ev
.type
](&ev
); /* call handler */
398 /* drop events requested to */
399 if(globalconf
.drop_events
)
403 while(XCheckMaskEvent(dpy
, globalconf
.drop_events
, &ev
));
404 globalconf
.drop_events
= NoEventMask
;
419 if(csfd
> 0 && close(csfd
))
420 perror("error closing UNIX domain socket");
421 if(unlink(addr
->sun_path
))
422 perror("error unlinking UNIX domain socket");
429 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80