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.
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>
46 #include "statusbar.h"
52 #include "awesome-client.h"
54 static int (*xerrorxlib
) (Display
*, XErrorEvent
*);
55 static Bool running
= True
;
57 AwesomeConf globalconf
;
60 cleanup_buttons(Button
*buttons
)
64 for(b
= buttons
; b
; b
= bn
)
73 cleanup_screen(int screen
)
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
)
89 for(l
= globalconf
.screens
[screen
].layouts
; l
; l
= ln
)
97 /** Cleanup everything on quit
98 * \param awesomeconf awesome config
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
)
120 p_delete(&r
->prop_r
);
121 p_delete(&r
->tags_r
);
122 p_delete(&r
->xpropval_r
);
128 for(k
= globalconf
.keys
; k
; k
= kn
)
135 cleanup_buttons(globalconf
.buttons
.root
);
136 cleanup_buttons(globalconf
.buttons
.client
);
138 p_delete(&globalconf
.configpath
);
140 for(screen
= 0; screen
< get_screen_count(); screen
++)
141 cleanup_screen(screen
);
143 XSetInputFocus(globalconf
.display
, PointerRoot
, RevertToPointerRoot
, CurrentTime
);
144 XSync(globalconf
.display
, False
);
146 p_delete(&globalconf
.clients
);
149 /** Scan X to find windows to manage
155 int screen
, real_screen
;
156 Window
*wins
= NULL
, d1
, d2
;
157 XWindowAttributes wa
;
159 for(screen
= 0; screen
< ScreenCount(globalconf
.display
); screen
++)
161 if(XQueryTree(globalconf
.display
, RootWindow(globalconf
.display
, screen
), &d1
, &d2
, &wins
, &num
))
163 real_screen
= screen
;
164 for(i
= 0; i
< num
; i
++)
166 if(!XGetWindowAttributes(globalconf
.display
, wins
[i
], &wa
)
167 || wa
.override_redirect
168 || XGetTransientForHint(globalconf
.display
, wins
[i
], &d1
))
170 if(wa
.map_state
== IsViewable
|| window_getstate(wins
[i
]) == IconicState
)
173 real_screen
= get_screen_bycoord(wa
.x
, wa
.y
);
174 client_manage(wins
[i
], &wa
, real_screen
);
177 /* now the transients */
178 for(i
= 0; i
< num
; i
++)
180 if(!XGetWindowAttributes(globalconf
.display
, wins
[i
], &wa
))
182 if(XGetTransientForHint(globalconf
.display
, wins
[i
], &d1
)
183 && (wa
.map_state
== IsViewable
|| window_getstate(wins
[i
]) == IconicState
))
186 real_screen
= get_screen_bycoord(wa
.x
, wa
.y
);
187 client_manage(wins
[i
], &wa
, real_screen
);
196 /** Setup everything before running
197 * \param screen Screen number
198 * \todo clean things...
203 XSetWindowAttributes wa
;
204 Statusbar
*statusbar
;
207 globalconf
.cursor
[CurNormal
] = XCreateFontCursor(globalconf
.display
, XC_left_ptr
);
208 globalconf
.cursor
[CurResize
] = XCreateFontCursor(globalconf
.display
, XC_sizing
);
209 globalconf
.cursor
[CurMove
] = XCreateFontCursor(globalconf
.display
, XC_fleur
);
211 /* select for events */
212 wa
.event_mask
= SubstructureRedirectMask
| SubstructureNotifyMask
213 | EnterWindowMask
| LeaveWindowMask
| StructureNotifyMask
;
214 wa
.cursor
= globalconf
.cursor
[CurNormal
];
216 XChangeWindowAttributes(globalconf
.display
,
217 RootWindow(globalconf
.display
, get_phys_screen(screen
)),
218 CWEventMask
| CWCursor
, &wa
);
220 XSelectInput(globalconf
.display
,
221 RootWindow(globalconf
.display
, get_phys_screen(screen
)),
224 grabkeys(get_phys_screen(screen
));
226 for(statusbar
= globalconf
.screens
[screen
].statusbar
; statusbar
; statusbar
= statusbar
->next
)
227 statusbar_init(statusbar
, screen
);
230 /** Startup Error handler to check if another window manager
231 * is already running.
232 * \param disp Display
233 * \param ee Error event
235 static int __attribute__ ((noreturn
))
236 xerrorstart(Display
* disp
__attribute__ ((unused
)),
237 XErrorEvent
* ee
__attribute__ ((unused
)))
239 eprint("another window manager is already running\n");
243 * \param screen Screen ID
245 * \ingroup ui_callback
248 uicb_quit(int screen
__attribute__ ((unused
)), char *arg
__attribute__ ((unused
)))
254 exit_on_signal(int sig
__attribute__ ((unused
)))
259 /* There's no way to check accesses to destroyed windows, thus those cases are
260 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
261 * default error handler, which may call exit.
264 xerror(Display
* edpy
, XErrorEvent
* ee
)
266 if(ee
->error_code
== BadWindow
267 || (ee
->request_code
== X_SetInputFocus
&& ee
->error_code
== BadMatch
)
268 || (ee
->request_code
== X_PolyText8
&& ee
->error_code
== BadDrawable
)
269 || (ee
->request_code
== X_PolyFillRectangle
270 && ee
->error_code
== BadDrawable
)
271 || (ee
->request_code
== X_PolySegment
&& ee
->error_code
== BadDrawable
)
272 || (ee
->request_code
== X_ConfigureWindow
273 && ee
->error_code
== BadMatch
) || (ee
->request_code
== X_GrabKey
274 && ee
->error_code
== BadAccess
)
275 || (ee
->request_code
== X_CopyArea
&& ee
->error_code
== BadDrawable
))
277 warn("fatal error: request code=%d, error code=%d\n", 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
*);
288 main(int argc
, char *argv
[])
291 const char *confpath
= NULL
;
292 int r
, xfd
, e_dummy
, csfd
;
296 int shape_event
, randr_event_base
;
298 event_handler
**handler
;
299 struct sockaddr_un
*addr
;
304 if(!a_strcmp("-v", argv
[1]) || !a_strcmp("--version", argv
[1]))
306 printf("awesome " VERSION
" (" RELEASE
")\n");
309 else if(!a_strcmp("-c", argv
[1]))
311 if(a_strlen(argv
[2]))
314 eprint("-c require a file\n");
317 eprint("options: [-v | -c configfile]\n");
321 if(!(dpy
= XOpenDisplay(NULL
)))
322 eprint("cannot open display\n");
324 xfd
= ConnectionNumber(dpy
);
326 XSetErrorHandler(xerrorstart
);
327 for(screen
= 0; screen
< ScreenCount(dpy
); screen
++)
328 /* this causes an error if some other window manager is running */
329 XSelectInput(dpy
, RootWindow(dpy
, screen
), SubstructureRedirectMask
);
331 /* need to XSync to validate errorhandler */
333 XSetErrorHandler(NULL
);
334 xerrorxlib
= XSetErrorHandler(xerror
);
338 globalconf
.display
= dpy
;
340 /* init EWMH atoms */
343 /* init screens struct */
344 globalconf
.screens
= p_new(VirtScreen
, get_screen_count());
345 focus_add_client(NULL
);
348 config_parse(confpath
);
350 /* for each virtual screen */
351 for(screen
= 0; screen
< get_screen_count(); screen
++)
354 /* do this only for real screen */
355 for(screen
= 0; screen
< ScreenCount(dpy
); screen
++)
357 loadawesomeprops(screen
);
358 ewmh_set_supported_hints(screen
);
361 handler
= p_new(event_handler
*, LASTEvent
);
362 handler
[ButtonPress
] = handle_event_buttonpress
;
363 handler
[ConfigureRequest
] = handle_event_configurerequest
;
364 handler
[ConfigureNotify
] = handle_event_configurenotify
;
365 handler
[DestroyNotify
] = handle_event_destroynotify
;
366 handler
[EnterNotify
] = handle_event_enternotify
;
367 handler
[LeaveNotify
] = handle_event_leavenotify
;
368 handler
[Expose
] = handle_event_expose
;
369 handler
[KeyPress
] = handle_event_keypress
;
370 handler
[MappingNotify
] = handle_event_mappingnotify
;
371 handler
[MapRequest
] = handle_event_maprequest
;
372 handler
[PropertyNotify
] = handle_event_propertynotify
;
373 handler
[UnmapNotify
] = handle_event_unmapnotify
;
374 handler
[ClientMessage
] = handle_event_clientmessage
;
376 /* check for shape extension */
377 if((globalconf
.have_shape
= XShapeQueryExtension(dpy
, &shape_event
, &e_dummy
)))
379 p_realloc(&handler
, shape_event
+ 1);
380 handler
[shape_event
] = handle_event_shape
;
383 /* check for randr extension */
384 if((globalconf
.have_randr
= XRRQueryExtension(dpy
, &randr_event_base
, &e_dummy
)))
386 p_realloc(&handler
, randr_event_base
+ RRScreenChangeNotify
+ 1);
387 handler
[randr_event_base
+ RRScreenChangeNotify
] = handle_event_randr_screen_change_notify
;
395 csfd
= get_client_socket();
396 addr
= get_client_addr(getenv("DISPLAY"));
398 if(bind(csfd
, (const struct sockaddr
*) addr
, SUN_LEN(addr
)))
400 if(errno
== EADDRINUSE
)
402 if(unlink(addr
->sun_path
))
403 perror("error unlinking existing file");
404 if(bind(csfd
, (const struct sockaddr
*) addr
, SUN_LEN(addr
)))
405 perror("error binding UNIX domain socket");
408 perror("error binding UNIX domain socket");
411 /* register function for signals */
412 signal(SIGINT
, &exit_on_signal
);
413 signal(SIGTERM
, &exit_on_signal
);
414 signal(SIGHUP
, &exit_on_signal
);
416 /* main event loop, also reads status text from socket */
423 if(select(MAX(xfd
, csfd
) + 1, &rd
, NULL
, NULL
, NULL
) == -1)
427 eprint("select failed\n");
429 if(csfd
>= 0 && FD_ISSET(csfd
, &rd
))
430 switch (r
= recv(csfd
, buf
, sizeof(buf
)-1, MSG_TRUNC
))
433 perror("awesome: error reading UNIX domain socket");
439 if(r
>= ssizeof(buf
))
447 XNextEvent(dpy
, &ev
);
449 handler
[ev
.type
](&ev
); /* call handler */
453 if(csfd
> 0 && close(csfd
))
454 perror("error closing UNIX domain socket");
455 if(unlink(addr
->sun_path
))
456 perror("error unlinking UNIX domain socket");
464 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80