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
;
55 cleanup_buttons(Button
*buttons
)
59 for(b
= buttons
; b
; b
= bn
)
68 cleanup_screen(awesome_config
*awesomeconf
)
74 XftFontClose(awesomeconf
->display
, awesomeconf
->font
);
75 XUngrabKey(awesomeconf
->display
, AnyKey
, AnyModifier
, RootWindow(awesomeconf
->display
, awesomeconf
->phys_screen
));
76 XDestroyWindow(awesomeconf
->display
, awesomeconf
->statusbar
.window
);
77 XFreeCursor(awesomeconf
->display
, awesomeconf
->cursor
[CurNormal
]);
78 XFreeCursor(awesomeconf
->display
, awesomeconf
->cursor
[CurResize
]);
79 XFreeCursor(awesomeconf
->display
, awesomeconf
->cursor
[CurMove
]);
81 for(i
= 0; i
< awesomeconf
->ntags
; i
++)
82 p_delete(&awesomeconf
->tags
[i
].name
);
83 for(k
= awesomeconf
->keys
; k
; k
= kn
)
89 cleanup_buttons(awesomeconf
->buttons
.tag
);
90 cleanup_buttons(awesomeconf
->buttons
.title
);
91 cleanup_buttons(awesomeconf
->buttons
.layout
);
92 cleanup_buttons(awesomeconf
->buttons
.root
);
93 cleanup_buttons(awesomeconf
->buttons
.client
);
95 for(i
= 0; i
< awesomeconf
->nlayouts
; i
++)
96 p_delete(&awesomeconf
->layouts
[i
].symbol
);
97 for(r
= awesomeconf
->rules
; r
; r
= rn
)
105 p_delete(&awesomeconf
->tags
);
106 p_delete(&awesomeconf
->layouts
);
107 p_delete(&awesomeconf
->configpath
);
110 /** Cleanup everything on quit
111 * \param awesomeconf awesome config
114 cleanup(awesome_config
*awesomeconf
)
118 while(*awesomeconf
->clients
)
120 client_unban(*awesomeconf
->clients
);
121 client_unmanage(*awesomeconf
->clients
, NormalState
, awesomeconf
);
124 for(screen
= 0; screen
< get_screen_count(awesomeconf
->display
); screen
++)
125 cleanup_screen(&awesomeconf
[screen
]);
127 XSetInputFocus(awesomeconf
->display
, PointerRoot
, RevertToPointerRoot
, CurrentTime
);
128 XSync(awesomeconf
->display
, False
);
130 p_delete(&awesomeconf
->clients
);
131 p_delete(&awesomeconf
);
134 /** Scan X to find windows to manage
135 * \param screen Screen number
136 * \param awesomeconf awesome config
139 scan(awesome_config
*awesomeconf
)
142 int screen
, real_screen
;
143 Window
*wins
= NULL
, d1
, d2
;
144 XWindowAttributes wa
;
146 for(screen
= 0; screen
< ScreenCount(awesomeconf
->display
); screen
++)
148 if(XQueryTree(awesomeconf
->display
, RootWindow(awesomeconf
->display
, screen
), &d1
, &d2
, &wins
, &num
))
150 real_screen
= screen
;
151 for(i
= 0; i
< num
; i
++)
153 if(!XGetWindowAttributes(awesomeconf
->display
, wins
[i
], &wa
)
154 || wa
.override_redirect
155 || XGetTransientForHint(awesomeconf
->display
, wins
[i
], &d1
))
157 if(wa
.map_state
== IsViewable
|| window_getstate(awesomeconf
->display
, wins
[i
]) == IconicState
)
160 real_screen
= get_screen_bycoord(awesomeconf
->display
, wa
.x
, wa
.y
);
161 client_manage(wins
[i
], &wa
, &awesomeconf
[real_screen
]);
164 /* now the transients */
165 for(i
= 0; i
< num
; i
++)
167 if(!XGetWindowAttributes(awesomeconf
->display
, wins
[i
], &wa
))
169 if(XGetTransientForHint(awesomeconf
->display
, wins
[i
], &d1
)
170 && (wa
.map_state
== IsViewable
|| window_getstate(awesomeconf
->display
, wins
[i
]) == IconicState
))
173 real_screen
= get_screen_bycoord(awesomeconf
->display
, wa
.x
, wa
.y
);
174 client_manage(wins
[i
], &wa
, &awesomeconf
[real_screen
]);
183 /** Setup everything before running
184 * \param awesomeconf awesome config ref
185 * \todo clean things...
188 setup(awesome_config
*awesomeconf
)
190 XSetWindowAttributes wa
;
193 awesomeconf
->cursor
[CurNormal
] = XCreateFontCursor(awesomeconf
->display
, XC_left_ptr
);
194 awesomeconf
->cursor
[CurResize
] = XCreateFontCursor(awesomeconf
->display
, XC_sizing
);
195 awesomeconf
->cursor
[CurMove
] = XCreateFontCursor(awesomeconf
->display
, XC_fleur
);
197 /* select for events */
198 wa
.event_mask
= SubstructureRedirectMask
| SubstructureNotifyMask
199 | EnterWindowMask
| LeaveWindowMask
| StructureNotifyMask
;
200 wa
.cursor
= awesomeconf
->cursor
[CurNormal
];
202 XChangeWindowAttributes(awesomeconf
->display
, RootWindow(awesomeconf
->display
, awesomeconf
->phys_screen
), CWEventMask
| CWCursor
, &wa
);
204 XSelectInput(awesomeconf
->display
, RootWindow(awesomeconf
->display
, awesomeconf
->phys_screen
), wa
.event_mask
);
206 grabkeys(awesomeconf
);
210 setup_screen(awesome_config
*awesomeconf
, const char *confpath
)
212 parse_config(confpath
, awesomeconf
);
214 initstatusbar(awesomeconf
->display
, awesomeconf
->screen
, &awesomeconf
->statusbar
,
215 awesomeconf
->cursor
[CurNormal
], awesomeconf
->font
,
216 awesomeconf
->layouts
, awesomeconf
->nlayouts
);
219 /** Startup Error handler to check if another window manager
220 * is already running.
221 * \param disp Display ref
222 * \param ee Error event
224 static int __attribute__ ((noreturn
))
225 xerrorstart(Display
* disp
__attribute__ ((unused
)), XErrorEvent
* ee
__attribute__ ((unused
)))
227 eprint("awesome: another window manager is already running\n");
231 * \param awesomeconf awesome config
233 * \ingroup ui_callback
236 uicb_quit(awesome_config
*awesomeconf
__attribute__((unused
)),
237 const char *arg
__attribute__ ((unused
)))
242 /* There's no way to check accesses to destroyed windows, thus those cases are
243 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
244 * default error handler, which may call exit.
247 xerror(Display
* edpy
, XErrorEvent
* ee
)
249 if(ee
->error_code
== BadWindow
250 || (ee
->request_code
== X_SetInputFocus
&& ee
->error_code
== BadMatch
)
251 || (ee
->request_code
== X_PolyText8
&& ee
->error_code
== BadDrawable
)
252 || (ee
->request_code
== X_PolyFillRectangle
253 && ee
->error_code
== BadDrawable
)
254 || (ee
->request_code
== X_PolySegment
&& ee
->error_code
== BadDrawable
)
255 || (ee
->request_code
== X_ConfigureWindow
256 && ee
->error_code
== BadMatch
) || (ee
->request_code
== X_GrabKey
257 && ee
->error_code
== BadAccess
)
258 || (ee
->request_code
== X_CopyArea
&& ee
->error_code
== BadDrawable
))
260 fprintf(stderr
, "awesome: fatal error: request code=%d, error code=%d\n",
261 ee
->request_code
, ee
->error_code
);
263 return xerrorxlib(edpy
, ee
); /* may call exit */
266 /** Hello, this is main
267 * \param argc who knows
268 * \param argv who knows
269 * \return EXIT_SUCCESS I hope
271 typedef void event_handler (XEvent
*, awesome_config
*);
273 main(int argc
, char *argv
[])
276 const char *confpath
= NULL
;
277 int r
, xfd
, e_dummy
, csfd
;
281 awesome_config
*awesomeconf
;
282 int shape_event
, randr_event_base
;
284 enum { NetSupported
, NetWMName
, NetLast
}; /* EWMH atoms */
285 Atom netatom
[NetLast
];
286 event_handler
**handler
;
288 struct sockaddr_un
*addr
;
292 if(!a_strcmp("-v", argv
[1]))
294 printf("awesome-" VERSION
" © 2007 Julien Danjou\n");
297 else if(!a_strcmp("-c", argv
[1]))
299 if(a_strlen(argv
[2]))
302 eprint("awesome: -c require a file\n");
305 eprint("usage: awesome [-v | -c configfile]\n");
308 /* Tag won't be printed otherwised */
309 setlocale(LC_CTYPE
, "");
311 if(!(dpy
= XOpenDisplay(NULL
)))
312 eprint("awesome: cannot open display\n");
314 xfd
= ConnectionNumber(dpy
);
316 XSetErrorHandler(xerrorstart
);
317 for(screen
= 0; screen
< ScreenCount(dpy
); screen
++)
318 /* this causes an error if some other window manager is running */
319 XSelectInput(dpy
, RootWindow(dpy
, screen
), SubstructureRedirectMask
);
321 /* need to XSync to validate errorhandler */
323 XSetErrorHandler(NULL
);
324 xerrorxlib
= XSetErrorHandler(xerror
);
328 awesomeconf
= p_new(awesome_config
, get_screen_count(dpy
));
329 clients
= p_new(Client
*, 1);
331 for(screen
= 0; screen
< get_screen_count(dpy
); screen
++)
334 awesomeconf
[screen
].display
= dpy
;
337 awesomeconf
[screen
].screen
= screen
;
338 setup_screen(&awesomeconf
[screen
], confpath
);
339 awesomeconf
[screen
].clients
= clients
;
340 drawstatusbar(&awesomeconf
[screen
]);
343 netatom
[NetSupported
] = XInternAtom(dpy
, "_NET_SUPPORTED", False
);
344 netatom
[NetWMName
] = XInternAtom(dpy
, "_NET_WM_NAME", False
);
346 /* do this only for real screen */
347 for(screen
= 0; screen
< ScreenCount(dpy
); screen
++)
349 loadawesomeprops(&awesomeconf
[screen
]);
350 XChangeProperty(dpy
, RootWindow(dpy
, screen
), netatom
[NetSupported
],
351 XA_ATOM
, 32, PropModeReplace
, (unsigned char *) netatom
, NetLast
);
354 handler
= p_new(event_handler
*, LASTEvent
);
355 handler
[ButtonPress
] = handle_event_buttonpress
;
356 handler
[ConfigureRequest
] = handle_event_configurerequest
;
357 handler
[ConfigureNotify
] = handle_event_configurenotify
;
358 handler
[DestroyNotify
] = handle_event_destroynotify
;
359 handler
[EnterNotify
] = handle_event_enternotify
;
360 handler
[LeaveNotify
] = handle_event_leavenotify
;
361 handler
[Expose
] = handle_event_expose
;
362 handler
[KeyPress
] = handle_event_keypress
;
363 handler
[MappingNotify
] = handle_event_mappingnotify
;
364 handler
[MapRequest
] = handle_event_maprequest
;
365 handler
[PropertyNotify
] = handle_event_propertynotify
;
366 handler
[UnmapNotify
] = handle_event_unmapnotify
;
368 /* check for shape extension */
369 if((awesomeconf
[0].have_shape
= XShapeQueryExtension(dpy
, &shape_event
, &e_dummy
)))
371 p_realloc(&handler
, shape_event
+ 1);
372 handler
[shape_event
] = handle_event_shape
;
375 /* check for randr extension */
376 if((awesomeconf
[0].have_randr
= XRRQueryExtension(dpy
, &randr_event_base
, &e_dummy
)))
378 p_realloc(&handler
, randr_event_base
+ RRScreenChangeNotify
+ 1);
379 handler
[randr_event_base
+ RRScreenChangeNotify
] = handle_event_randr_screen_change_notify
;
382 for(screen
= 0; screen
< get_screen_count(dpy
); screen
++)
384 awesomeconf
[screen
].have_shape
= awesomeconf
[0].have_shape
;
385 awesomeconf
[screen
].have_randr
= awesomeconf
[0].have_randr
;
393 csfd
= get_client_socket();
394 addr
= get_client_addr(getenv("DISPLAY"));
396 if(bind(csfd
, (const struct sockaddr
*) addr
, SUN_LEN(addr
)))
398 if(errno
== EADDRINUSE
)
400 if(unlink(addr
->sun_path
))
401 perror("error unlinking existing file");
402 if(bind(csfd
, (const struct sockaddr
*) addr
, SUN_LEN(addr
)))
403 perror("error binding UNIX domain socket");
406 perror("error binding UNIX domain socket");
409 /* main event loop, also reads status text from socket */
416 if(select(MAX(xfd
, csfd
) + 1, &rd
, NULL
, NULL
, NULL
) == -1)
420 eprint("select failed\n");
422 if(csfd
>= 0 && FD_ISSET(csfd
, &rd
))
423 switch (r
= recv(csfd
, buf
, sizeof(buf
)-1, MSG_TRUNC
))
426 perror("awesome: error reading UNIX domain socket");
427 a_strncpy(awesomeconf
[0].statustext
, sizeof(awesomeconf
[0].statustext
),
428 strerror(errno
), sizeof(awesomeconf
[0].statustext
) - 1);
429 awesomeconf
[0].statustext
[sizeof(awesomeconf
[0].statustext
) - 1] = '\0';
435 if(r
>= ssizeof(buf
))
438 parse_control(buf
, awesomeconf
);
443 XNextEvent(dpy
, &ev
);
445 handler
[ev
.type
](&ev
, awesomeconf
); /* call handler */
449 if(csfd
> 0 && close(csfd
))
450 perror("error closing UNIX domain socket");
451 if(unlink(addr
->sun_path
))
452 perror("error unlinking UNIX domain socket");
455 cleanup(awesomeconf
);
460 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99