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"
48 #include "awesome-client.h"
50 static int (*xerrorxlib
) (Display
*, XErrorEvent
*);
51 static Bool running
= True
;
54 cleanup_screen(awesome_config
*awesomeconf
)
61 XftFontClose(awesomeconf
->display
, awesomeconf
->font
);
62 XUngrabKey(awesomeconf
->display
, AnyKey
, AnyModifier
, RootWindow(awesomeconf
->display
, awesomeconf
->phys_screen
));
63 XDestroyWindow(awesomeconf
->display
, awesomeconf
->statusbar
.window
);
64 XFreeCursor(awesomeconf
->display
, awesomeconf
->cursor
[CurNormal
]);
65 XFreeCursor(awesomeconf
->display
, awesomeconf
->cursor
[CurResize
]);
66 XFreeCursor(awesomeconf
->display
, awesomeconf
->cursor
[CurMove
]);
68 for(i
= 0; i
< awesomeconf
->ntags
; i
++)
69 p_delete(&awesomeconf
->tags
[i
].name
);
70 for(k
= awesomeconf
->keys
; k
; k
= kn
)
75 for(b
= awesomeconf
->buttons
.tag
; b
; b
= bn
)
80 for(b
= awesomeconf
->buttons
.title
; b
; b
= bn
)
86 for(b
= awesomeconf
->buttons
.layout
; b
; b
= bn
)
92 for(b
= awesomeconf
->buttons
.root
; b
; b
= bn
)
98 for(i
= 0; i
< awesomeconf
->nlayouts
; i
++)
99 p_delete(&awesomeconf
->layouts
[i
].symbol
);
100 for(r
= awesomeconf
->rules
; r
; r
= rn
)
107 p_delete(&awesomeconf
->tags
);
108 p_delete(&awesomeconf
->layouts
);
109 p_delete(&awesomeconf
->configpath
);
112 /** Cleanup everything on quit
113 * \param awesomeconf awesome config
116 cleanup(awesome_config
*awesomeconf
)
120 while(*awesomeconf
->clients
)
122 client_unban(*awesomeconf
->clients
);
123 client_unmanage(*awesomeconf
->clients
, NormalState
, awesomeconf
);
126 for(screen
= 0; screen
< get_screen_count(awesomeconf
->display
); screen
++)
127 cleanup_screen(&awesomeconf
[screen
]);
129 XSetInputFocus(awesomeconf
->display
, PointerRoot
, RevertToPointerRoot
, CurrentTime
);
130 XSync(awesomeconf
->display
, False
);
132 p_delete(&awesomeconf
->clients
);
133 p_delete(&awesomeconf
);
136 /** Scan X to find windows to manage
137 * \param screen Screen number
138 * \param awesomeconf awesome config
141 scan(awesome_config
*awesomeconf
)
144 int screen
, real_screen
;
145 Window
*wins
= NULL
, d1
, d2
;
146 XWindowAttributes wa
;
148 for(screen
= 0; screen
< ScreenCount(awesomeconf
->display
); screen
++)
150 if(XQueryTree(awesomeconf
->display
, RootWindow(awesomeconf
->display
, screen
), &d1
, &d2
, &wins
, &num
))
152 real_screen
= screen
;
153 for(i
= 0; i
< num
; i
++)
155 if(!XGetWindowAttributes(awesomeconf
->display
, wins
[i
], &wa
)
156 || wa
.override_redirect
157 || XGetTransientForHint(awesomeconf
->display
, wins
[i
], &d1
))
159 if(wa
.map_state
== IsViewable
|| window_getstate(awesomeconf
->display
, wins
[i
]) == IconicState
)
162 real_screen
= get_screen_bycoord(awesomeconf
->display
, wa
.x
, wa
.y
);
163 client_manage(wins
[i
], &wa
, &awesomeconf
[real_screen
]);
166 /* now the transients */
167 for(i
= 0; i
< num
; i
++)
169 if(!XGetWindowAttributes(awesomeconf
->display
, wins
[i
], &wa
))
171 if(XGetTransientForHint(awesomeconf
->display
, wins
[i
], &d1
)
172 && (wa
.map_state
== IsViewable
|| window_getstate(awesomeconf
->display
, wins
[i
]) == IconicState
))
175 real_screen
= get_screen_bycoord(awesomeconf
->display
, wa
.x
, wa
.y
);
176 client_manage(wins
[i
], &wa
, &awesomeconf
[real_screen
]);
185 /** Setup everything before running
186 * \param awesomeconf awesome config ref
187 * \todo clean things...
190 setup(awesome_config
*awesomeconf
)
192 XSetWindowAttributes wa
;
195 awesomeconf
->cursor
[CurNormal
] = XCreateFontCursor(awesomeconf
->display
, XC_left_ptr
);
196 awesomeconf
->cursor
[CurResize
] = XCreateFontCursor(awesomeconf
->display
, XC_sizing
);
197 awesomeconf
->cursor
[CurMove
] = XCreateFontCursor(awesomeconf
->display
, XC_fleur
);
199 /* select for events */
200 wa
.event_mask
= SubstructureRedirectMask
| SubstructureNotifyMask
201 | EnterWindowMask
| LeaveWindowMask
| StructureNotifyMask
;
202 wa
.cursor
= awesomeconf
->cursor
[CurNormal
];
204 XChangeWindowAttributes(awesomeconf
->display
, RootWindow(awesomeconf
->display
, awesomeconf
->phys_screen
), CWEventMask
| CWCursor
, &wa
);
206 XSelectInput(awesomeconf
->display
, RootWindow(awesomeconf
->display
, awesomeconf
->phys_screen
), wa
.event_mask
);
208 grabkeys(awesomeconf
);
212 setup_screen(awesome_config
*awesomeconf
, const char *confpath
)
214 parse_config(confpath
, awesomeconf
);
216 initstatusbar(awesomeconf
->display
, awesomeconf
->screen
, &awesomeconf
->statusbar
,
217 awesomeconf
->cursor
[CurNormal
], awesomeconf
->font
,
218 awesomeconf
->layouts
, awesomeconf
->nlayouts
);
221 /** Startup Error handler to check if another window manager
222 * is already running.
223 * \param disp Display ref
224 * \param ee Error event
226 static int __attribute__ ((noreturn
))
227 xerrorstart(Display
* disp
__attribute__ ((unused
)), XErrorEvent
* ee
__attribute__ ((unused
)))
229 eprint("awesome: another window manager is already running\n");
233 * \param awesomeconf awesome config
235 * \ingroup ui_callback
238 uicb_quit(awesome_config
*awesomeconf
__attribute__((unused
)),
239 const char *arg
__attribute__ ((unused
)))
244 /* There's no way to check accesses to destroyed windows, thus those cases are
245 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
246 * default error handler, which may call exit.
249 xerror(Display
* edpy
, XErrorEvent
* ee
)
251 if(ee
->error_code
== BadWindow
252 || (ee
->request_code
== X_SetInputFocus
&& ee
->error_code
== BadMatch
)
253 || (ee
->request_code
== X_PolyText8
&& ee
->error_code
== BadDrawable
)
254 || (ee
->request_code
== X_PolyFillRectangle
255 && ee
->error_code
== BadDrawable
)
256 || (ee
->request_code
== X_PolySegment
&& ee
->error_code
== BadDrawable
)
257 || (ee
->request_code
== X_ConfigureWindow
258 && ee
->error_code
== BadMatch
) || (ee
->request_code
== X_GrabKey
259 && ee
->error_code
== BadAccess
)
260 || (ee
->request_code
== X_CopyArea
&& ee
->error_code
== BadDrawable
))
262 fprintf(stderr
, "awesome: fatal error: request code=%d, error code=%d\n",
263 ee
->request_code
, ee
->error_code
);
265 return xerrorxlib(edpy
, ee
); /* may call exit */
268 /** Hello, this is main
269 * \param argc who knows
270 * \param argv who knows
271 * \return EXIT_SUCCESS I hope
273 typedef void event_handler (XEvent
*, awesome_config
*);
275 main(int argc
, char *argv
[])
278 const char *confpath
= NULL
;
279 int r
, xfd
, e_dummy
, csfd
;
283 awesome_config
*awesomeconf
;
284 int shape_event
, randr_event_base
;
286 enum { NetSupported
, NetWMName
, NetLast
}; /* EWMH atoms */
287 Atom netatom
[NetLast
];
288 event_handler
**handler
;
290 struct sockaddr_un
*addr
;
294 if(!a_strcmp("-v", argv
[1]))
296 printf("awesome-" VERSION
" © 2007 Julien Danjou\n");
299 else if(!a_strcmp("-c", argv
[1]))
301 if(a_strlen(argv
[2]))
304 eprint("awesome: -c require a file\n");
307 eprint("usage: awesome [-v | -c configfile]\n");
310 /* Tag won't be printed otherwised */
311 setlocale(LC_CTYPE
, "");
313 if(!(dpy
= XOpenDisplay(NULL
)))
314 eprint("awesome: cannot open display\n");
316 xfd
= ConnectionNumber(dpy
);
318 XSetErrorHandler(xerrorstart
);
319 for(screen
= 0; screen
< ScreenCount(dpy
); screen
++)
320 /* this causes an error if some other window manager is running */
321 XSelectInput(dpy
, RootWindow(dpy
, screen
), SubstructureRedirectMask
);
323 /* need to XSync to validate errorhandler */
325 XSetErrorHandler(NULL
);
326 xerrorxlib
= XSetErrorHandler(xerror
);
330 awesomeconf
= p_new(awesome_config
, get_screen_count(dpy
));
331 clients
= p_new(Client
*, 1);
333 for(screen
= 0; screen
< get_screen_count(dpy
); screen
++)
336 awesomeconf
[screen
].display
= dpy
;
339 awesomeconf
[screen
].screen
= screen
;
340 setup_screen(&awesomeconf
[screen
], confpath
);
341 awesomeconf
[screen
].clients
= clients
;
342 drawstatusbar(&awesomeconf
[screen
]);
345 netatom
[NetSupported
] = XInternAtom(dpy
, "_NET_SUPPORTED", False
);
346 netatom
[NetWMName
] = XInternAtom(dpy
, "_NET_WM_NAME", False
);
348 /* do this only for real screen */
349 for(screen
= 0; screen
< ScreenCount(dpy
); screen
++)
351 loadawesomeprops(&awesomeconf
[screen
]);
352 XChangeProperty(dpy
, RootWindow(dpy
, screen
), netatom
[NetSupported
],
353 XA_ATOM
, 32, PropModeReplace
, (unsigned char *) netatom
, NetLast
);
356 handler
= p_new(event_handler
*, LASTEvent
);
357 handler
[ButtonPress
] = handle_event_buttonpress
;
358 handler
[ConfigureRequest
] = handle_event_configurerequest
;
359 handler
[ConfigureNotify
] = handle_event_configurenotify
;
360 handler
[DestroyNotify
] = handle_event_destroynotify
;
361 handler
[EnterNotify
] = handle_event_enternotify
;
362 handler
[LeaveNotify
] = handle_event_leavenotify
;
363 handler
[Expose
] = handle_event_expose
;
364 handler
[KeyPress
] = handle_event_keypress
;
365 handler
[MappingNotify
] = handle_event_mappingnotify
;
366 handler
[MapRequest
] = handle_event_maprequest
;
367 handler
[PropertyNotify
] = handle_event_propertynotify
;
368 handler
[UnmapNotify
] = handle_event_unmapnotify
;
370 /* check for shape extension */
371 if((awesomeconf
[0].have_shape
= XShapeQueryExtension(dpy
, &shape_event
, &e_dummy
)))
373 p_realloc(&handler
, shape_event
+ 1);
374 handler
[shape_event
] = handle_event_shape
;
377 /* check for randr extension */
378 if((awesomeconf
[0].have_randr
= XRRQueryExtension(dpy
, &randr_event_base
, &e_dummy
)))
380 p_realloc(&handler
, randr_event_base
+ RRScreenChangeNotify
+ 1);
381 handler
[randr_event_base
+ RRScreenChangeNotify
] = handle_event_randr_screen_change_notify
;
384 for(screen
= 0; screen
< get_screen_count(dpy
); screen
++)
386 awesomeconf
[screen
].have_shape
= awesomeconf
[0].have_shape
;
387 awesomeconf
[screen
].have_randr
= awesomeconf
[0].have_randr
;
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 /* main event loop, also reads status text from socket */
418 if(select(MAX(xfd
, csfd
) + 1, &rd
, NULL
, NULL
, NULL
) == -1)
422 eprint("select failed\n");
424 if(csfd
>= 0 && FD_ISSET(csfd
, &rd
))
425 switch (r
= recv(csfd
, buf
, sizeof(buf
)-1, MSG_TRUNC
))
428 perror("awesome: error reading UNIX domain socket");
429 a_strncpy(awesomeconf
[0].statustext
, sizeof(awesomeconf
[0].statustext
),
430 strerror(errno
), sizeof(awesomeconf
[0].statustext
) - 1);
431 awesomeconf
[0].statustext
[sizeof(awesomeconf
[0].statustext
) - 1] = '\0';
437 if(r
>= ssizeof(buf
))
440 parse_control(buf
, awesomeconf
);
445 XNextEvent(dpy
, &ev
);
447 handler
[ev
.type
](&ev
, awesomeconf
); /* call handler */
451 if(csfd
> 0 && close(csfd
))
452 perror("error closing UNIX domain socket");
453 if(unlink(addr
->sun_path
))
454 perror("error unlinking UNIX domain socket");
457 cleanup(awesomeconf
);
462 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99