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>
32 #include <X11/cursorfont.h>
33 #include <X11/keysym.h>
34 #include <X11/Xatom.h>
35 #include <X11/Xproto.h>
36 #include <X11/Xutil.h>
37 #include <X11/extensions/shape.h>
38 #include <X11/extensions/Xrandr.h>
45 #include "statusbar.h"
50 #include "awesome-client.h"
52 static int (*xerrorxlib
) (Display
*, XErrorEvent
*);
53 static Bool running
= True
;
55 AwesomeConf globalconf
;
58 cleanup_buttons(Button
*buttons
)
62 for(b
= buttons
; b
; b
= bn
)
71 cleanup_screen(int screen
)
76 XftFontClose(globalconf
.display
, globalconf
.screens
[screen
].font
);
77 XUngrabKey(globalconf
.display
, AnyKey
, AnyModifier
, RootWindow(globalconf
.display
, get_phys_screen(screen
)));
78 XDestroyWindow(globalconf
.display
, globalconf
.screens
[screen
].statusbar
->window
);
80 for(t
= globalconf
.screens
[screen
].tags
; t
; t
= tn
)
87 for(l
= globalconf
.screens
[screen
].layouts
; l
; l
= ln
)
95 /** Cleanup everything on quit
96 * \param awesomeconf awesome config
105 while(globalconf
.clients
)
107 client_unban(globalconf
.clients
);
108 client_unmanage(globalconf
.clients
, NormalState
);
111 XFreeCursor(globalconf
.display
, globalconf
.cursor
[CurNormal
]);
112 XFreeCursor(globalconf
.display
, globalconf
.cursor
[CurResize
]);
113 XFreeCursor(globalconf
.display
, globalconf
.cursor
[CurMove
]);
115 for(r
= globalconf
.rules
; r
; r
= rn
)
120 p_delete(&r
->propregex
);
121 p_delete(&r
->tagregex
);
125 for(k
= globalconf
.keys
; k
; k
= kn
)
132 cleanup_buttons(globalconf
.buttons
.tag
);
133 cleanup_buttons(globalconf
.buttons
.title
);
134 cleanup_buttons(globalconf
.buttons
.layout
);
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(globalconf
.display
, 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(globalconf
.display
, 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
;
206 globalconf
.cursor
[CurNormal
] = XCreateFontCursor(globalconf
.display
, XC_left_ptr
);
207 globalconf
.cursor
[CurResize
] = XCreateFontCursor(globalconf
.display
, XC_sizing
);
208 globalconf
.cursor
[CurMove
] = XCreateFontCursor(globalconf
.display
, XC_fleur
);
210 /* select for events */
211 wa
.event_mask
= SubstructureRedirectMask
| SubstructureNotifyMask
212 | EnterWindowMask
| LeaveWindowMask
| StructureNotifyMask
;
213 wa
.cursor
= globalconf
.cursor
[CurNormal
];
215 XChangeWindowAttributes(globalconf
.display
,
216 RootWindow(globalconf
.display
, get_phys_screen(screen
)),
217 CWEventMask
| CWCursor
, &wa
);
219 XSelectInput(globalconf
.display
,
220 RootWindow(globalconf
.display
, get_phys_screen(screen
)),
226 /** Startup Error handler to check if another window manager
227 * is already running.
228 * \param disp Display
229 * \param ee Error event
231 static int __attribute__ ((noreturn
))
232 xerrorstart(Display
* disp
__attribute__ ((unused
)),
233 XErrorEvent
* ee
__attribute__ ((unused
)))
235 eprint("another window manager is already running\n");
239 * \param screen Screen ID
241 * \ingroup ui_callback
244 uicb_quit(int screen
__attribute__ ((unused
)), char *arg
__attribute__ ((unused
)))
249 /* There's no way to check accesses to destroyed windows, thus those cases are
250 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
251 * default error handler, which may call exit.
254 xerror(Display
* edpy
, XErrorEvent
* ee
)
256 if(ee
->error_code
== BadWindow
257 || (ee
->request_code
== X_SetInputFocus
&& ee
->error_code
== BadMatch
)
258 || (ee
->request_code
== X_PolyText8
&& ee
->error_code
== BadDrawable
)
259 || (ee
->request_code
== X_PolyFillRectangle
260 && ee
->error_code
== BadDrawable
)
261 || (ee
->request_code
== X_PolySegment
&& ee
->error_code
== BadDrawable
)
262 || (ee
->request_code
== X_ConfigureWindow
263 && ee
->error_code
== BadMatch
) || (ee
->request_code
== X_GrabKey
264 && ee
->error_code
== BadAccess
)
265 || (ee
->request_code
== X_CopyArea
&& ee
->error_code
== BadDrawable
))
267 warn("fatal error: request code=%d, error code=%d\n", ee
->request_code
, ee
->error_code
);
268 return xerrorxlib(edpy
, ee
); /* may call exit */
271 /** Hello, this is main
272 * \param argc who knows
273 * \param argv who knows
274 * \return EXIT_SUCCESS I hope
276 typedef void event_handler (XEvent
*);
278 main(int argc
, char *argv
[])
281 const char *confpath
= NULL
;
282 int r
, xfd
, e_dummy
, csfd
;
286 int shape_event
, randr_event_base
;
288 enum { NetSupported
, NetWMName
, NetLast
}; /* EWMH atoms */
289 Atom netatom
[NetLast
];
290 event_handler
**handler
;
291 struct sockaddr_un
*addr
;
295 if(!a_strcmp("-v", argv
[1]) || !a_strcmp("--version", argv
[1]))
297 printf("awesome-" VERSION
" (" RELEASE
")\n");
300 else if(!a_strcmp("-c", argv
[1]))
302 if(a_strlen(argv
[2]))
305 eprint("-c require a file\n");
308 eprint("options: [-v | -c configfile]\n");
311 /* Tag won't be printed otherwised */
312 setlocale(LC_CTYPE
, "");
314 if(!(dpy
= XOpenDisplay(NULL
)))
315 eprint("cannot open display\n");
317 xfd
= ConnectionNumber(dpy
);
319 XSetErrorHandler(xerrorstart
);
320 for(screen
= 0; screen
< ScreenCount(dpy
); screen
++)
321 /* this causes an error if some other window manager is running */
322 XSelectInput(dpy
, RootWindow(dpy
, screen
), SubstructureRedirectMask
);
324 /* need to XSync to validate errorhandler */
326 XSetErrorHandler(NULL
);
327 xerrorxlib
= XSetErrorHandler(xerror
);
329 globalconf
.display
= dpy
;
331 globalconf
.screens
= p_new(VirtScreen
, get_screen_count());
332 focus_add_client(NULL
);
334 config_parse(confpath
);
336 for(screen
= 0; screen
< get_screen_count(); screen
++)
340 statusbar_init(screen
);
341 statusbar_draw(screen
);
344 netatom
[NetSupported
] = XInternAtom(dpy
, "_NET_SUPPORTED", False
);
345 netatom
[NetWMName
] = XInternAtom(dpy
, "_NET_WM_NAME", False
);
347 /* do this only for real screen */
348 for(screen
= 0; screen
< ScreenCount(dpy
); screen
++)
350 loadawesomeprops(screen
);
351 XChangeProperty(dpy
, RootWindow(dpy
, screen
), netatom
[NetSupported
],
352 XA_ATOM
, 32, PropModeReplace
, (unsigned char *) netatom
, NetLast
);
355 handler
= p_new(event_handler
*, LASTEvent
);
356 handler
[ButtonPress
] = handle_event_buttonpress
;
357 handler
[ConfigureRequest
] = handle_event_configurerequest
;
358 handler
[ConfigureNotify
] = handle_event_configurenotify
;
359 handler
[DestroyNotify
] = handle_event_destroynotify
;
360 handler
[EnterNotify
] = handle_event_enternotify
;
361 handler
[LeaveNotify
] = handle_event_leavenotify
;
362 handler
[Expose
] = handle_event_expose
;
363 handler
[KeyPress
] = handle_event_keypress
;
364 handler
[MappingNotify
] = handle_event_mappingnotify
;
365 handler
[MapRequest
] = handle_event_maprequest
;
366 handler
[PropertyNotify
] = handle_event_propertynotify
;
367 handler
[UnmapNotify
] = handle_event_unmapnotify
;
369 /* check for shape extension */
370 if((globalconf
.have_shape
= XShapeQueryExtension(dpy
, &shape_event
, &e_dummy
)))
372 p_realloc(&handler
, shape_event
+ 1);
373 handler
[shape_event
] = handle_event_shape
;
376 /* check for randr extension */
377 if((globalconf
.have_randr
= XRRQueryExtension(dpy
, &randr_event_base
, &e_dummy
)))
379 p_realloc(&handler
, randr_event_base
+ RRScreenChangeNotify
+ 1);
380 handler
[randr_event_base
+ RRScreenChangeNotify
] = handle_event_randr_screen_change_notify
;
388 csfd
= get_client_socket();
389 addr
= get_client_addr(getenv("DISPLAY"));
391 if(bind(csfd
, (const struct sockaddr
*) addr
, SUN_LEN(addr
)))
393 if(errno
== EADDRINUSE
)
395 if(unlink(addr
->sun_path
))
396 perror("error unlinking existing file");
397 if(bind(csfd
, (const struct sockaddr
*) addr
, SUN_LEN(addr
)))
398 perror("error binding UNIX domain socket");
401 perror("error binding UNIX domain socket");
404 /* main event loop, also reads status text from socket */
411 if(select(MAX(xfd
, csfd
) + 1, &rd
, NULL
, NULL
, NULL
) == -1)
415 eprint("select failed\n");
417 if(csfd
>= 0 && FD_ISSET(csfd
, &rd
))
418 switch (r
= recv(csfd
, buf
, sizeof(buf
)-1, MSG_TRUNC
))
421 perror("awesome: error reading UNIX domain socket");
427 if(r
>= ssizeof(buf
))
435 XNextEvent(dpy
, &ev
);
437 handler
[ev
.type
](&ev
); /* call handler */
441 if(csfd
> 0 && close(csfd
))
442 perror("error closing UNIX domain socket");
443 if(unlink(addr
->sun_path
))
444 perror("error unlinking UNIX domain socket");
452 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80