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>
46 #include "statusbar.h"
49 #include "awesome-client.h"
51 static int (*xerrorxlib
) (Display
*, XErrorEvent
*);
52 static Bool running
= True
;
55 cleanup_screen(awesome_config
*awesomeconf
)
59 XftFontClose(awesomeconf
->display
, awesomeconf
->font
);
60 XUngrabKey(awesomeconf
->display
, AnyKey
, AnyModifier
, RootWindow(awesomeconf
->display
, awesomeconf
->phys_screen
));
61 XFreePixmap(awesomeconf
->display
, awesomeconf
->statusbar
.drawable
);
62 XDestroyWindow(awesomeconf
->display
, awesomeconf
->statusbar
.window
);
63 XFreeCursor(awesomeconf
->display
, awesomeconf
->cursor
[CurNormal
]);
64 XFreeCursor(awesomeconf
->display
, awesomeconf
->cursor
[CurResize
]);
65 XFreeCursor(awesomeconf
->display
, awesomeconf
->cursor
[CurMove
]);
67 for(i
= 0; i
< awesomeconf
->ntags
; i
++)
68 p_delete(&awesomeconf
->tags
[i
].name
);
69 for(i
= 0; i
< awesomeconf
->nkeys
; i
++)
70 p_delete(&awesomeconf
->keys
[i
].arg
);
71 for(i
= 0; i
< awesomeconf
->nlayouts
; i
++)
72 p_delete(&awesomeconf
->layouts
[i
].symbol
);
73 for(i
= 0; i
< awesomeconf
->nrules
; i
++)
75 p_delete(&awesomeconf
->rules
[i
].prop
);
76 p_delete(&awesomeconf
->rules
[i
].tags
);
78 p_delete(&awesomeconf
->tags
);
79 p_delete(&awesomeconf
->layouts
);
80 p_delete(&awesomeconf
->rules
);
81 p_delete(&awesomeconf
->keys
);
82 p_delete(&awesomeconf
->configpath
);
85 /** Cleanup everything on quit
86 * \param awesomeconf awesome config
89 cleanup(awesome_config
*awesomeconf
)
93 while(*awesomeconf
->clients
)
95 client_unban(*awesomeconf
->clients
);
96 client_unmanage(*awesomeconf
->clients
, NormalState
, awesomeconf
);
99 for(screen
= 0; screen
< get_screen_count(awesomeconf
->display
); screen
++)
100 cleanup_screen(&awesomeconf
[screen
]);
102 XSetInputFocus(awesomeconf
->display
, PointerRoot
, RevertToPointerRoot
, CurrentTime
);
103 XSync(awesomeconf
->display
, False
);
105 p_delete(&awesomeconf
->clients
);
106 p_delete(&awesomeconf
);
109 /** Scan X to find windows to manage
110 * \param screen Screen number
111 * \param awesomeconf awesome config
114 scan(awesome_config
*awesomeconf
)
117 int screen
, real_screen
;
118 Window
*wins
= NULL
, d1
, d2
;
119 XWindowAttributes wa
;
121 for(screen
= 0; screen
< ScreenCount(awesomeconf
->display
); screen
++)
123 if(XQueryTree(awesomeconf
->display
, RootWindow(awesomeconf
->display
, screen
), &d1
, &d2
, &wins
, &num
))
125 real_screen
= screen
;
126 for(i
= 0; i
< num
; i
++)
128 if(!XGetWindowAttributes(awesomeconf
->display
, wins
[i
], &wa
)
129 || wa
.override_redirect
130 || XGetTransientForHint(awesomeconf
->display
, wins
[i
], &d1
))
132 if(wa
.map_state
== IsViewable
|| window_getstate(awesomeconf
->display
, wins
[i
]) == IconicState
)
135 real_screen
= get_screen_bycoord(awesomeconf
->display
, wa
.x
, wa
.y
);
136 client_manage(wins
[i
], &wa
, &awesomeconf
[real_screen
]);
139 /* now the transients */
140 for(i
= 0; i
< num
; i
++)
142 if(!XGetWindowAttributes(awesomeconf
->display
, wins
[i
], &wa
))
144 if(XGetTransientForHint(awesomeconf
->display
, wins
[i
], &d1
)
145 && (wa
.map_state
== IsViewable
|| window_getstate(awesomeconf
->display
, wins
[i
]) == IconicState
))
148 real_screen
= get_screen_bycoord(awesomeconf
->display
, wa
.x
, wa
.y
);
149 client_manage(wins
[i
], &wa
, &awesomeconf
[real_screen
]);
158 /** Setup everything before running
159 * \param awesomeconf awesome config ref
160 * \todo clean things...
163 setup(awesome_config
*awesomeconf
)
165 XSetWindowAttributes wa
;
168 awesomeconf
->cursor
[CurNormal
] = XCreateFontCursor(awesomeconf
->display
, XC_left_ptr
);
169 awesomeconf
->cursor
[CurResize
] = XCreateFontCursor(awesomeconf
->display
, XC_sizing
);
170 awesomeconf
->cursor
[CurMove
] = XCreateFontCursor(awesomeconf
->display
, XC_fleur
);
172 /* select for events */
173 wa
.event_mask
= SubstructureRedirectMask
| SubstructureNotifyMask
174 | EnterWindowMask
| LeaveWindowMask
| StructureNotifyMask
;
175 wa
.cursor
= awesomeconf
->cursor
[CurNormal
];
177 XChangeWindowAttributes(awesomeconf
->display
, RootWindow(awesomeconf
->display
, awesomeconf
->phys_screen
), CWEventMask
| CWCursor
, &wa
);
179 XSelectInput(awesomeconf
->display
, RootWindow(awesomeconf
->display
, awesomeconf
->phys_screen
), wa
.event_mask
);
181 grabkeys(awesomeconf
);
185 setup_screen(awesome_config
*awesomeconf
, const char *confpath
)
187 parse_config(confpath
, awesomeconf
);
189 initstatusbar(awesomeconf
->display
, awesomeconf
->screen
, &awesomeconf
->statusbar
,
190 awesomeconf
->cursor
[CurNormal
], awesomeconf
->font
,
191 awesomeconf
->layouts
, awesomeconf
->nlayouts
);
194 /** Startup Error handler to check if another window manager
195 * is already running.
196 * \param disp Display ref
197 * \param ee Error event
199 static int __attribute__ ((noreturn
))
200 xerrorstart(Display
* disp
__attribute__ ((unused
)), XErrorEvent
* ee
__attribute__ ((unused
)))
202 eprint("awesome: another window manager is already running\n");
206 * \param awesomeconf awesome config
208 * \ingroup ui_callback
211 uicb_quit(awesome_config
*awesomeconf
__attribute__((unused
)),
212 const char *arg
__attribute__ ((unused
)))
217 /* There's no way to check accesses to destroyed windows, thus those cases are
218 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
219 * default error handler, which may call exit.
222 xerror(Display
* edpy
, XErrorEvent
* ee
)
224 if(ee
->error_code
== BadWindow
225 || (ee
->request_code
== X_SetInputFocus
&& ee
->error_code
== BadMatch
)
226 || (ee
->request_code
== X_PolyText8
&& ee
->error_code
== BadDrawable
)
227 || (ee
->request_code
== X_PolyFillRectangle
228 && ee
->error_code
== BadDrawable
)
229 || (ee
->request_code
== X_PolySegment
&& ee
->error_code
== BadDrawable
)
230 || (ee
->request_code
== X_ConfigureWindow
231 && ee
->error_code
== BadMatch
) || (ee
->request_code
== X_GrabKey
232 && ee
->error_code
== BadAccess
)
233 || (ee
->request_code
== X_CopyArea
&& ee
->error_code
== BadDrawable
))
235 fprintf(stderr
, "awesome: fatal error: request code=%d, error code=%d\n",
236 ee
->request_code
, ee
->error_code
);
238 return xerrorxlib(edpy
, ee
); /* may call exit */
241 /** Hello, this is main
242 * \param argc who knows
243 * \param argv who knows
244 * \return EXIT_SUCCESS I hope
246 typedef void event_handler (XEvent
*, awesome_config
*);
248 main(int argc
, char *argv
[])
251 const char *confpath
= NULL
;
252 int r
, xfd
, e_dummy
, csfd
;
256 awesome_config
*awesomeconf
;
257 int shape_event
, randr_event_base
;
259 enum { NetSupported
, NetWMName
, NetLast
}; /* EWMH atoms */
260 Atom netatom
[NetLast
];
261 event_handler
**handler
;
263 struct sockaddr_un
*addr
;
267 if(!a_strcmp("-v", argv
[1]))
269 printf("awesome-" VERSION
" © 2007 Julien Danjou\n");
272 else if(!a_strcmp("-c", argv
[1]))
274 if(a_strlen(argv
[2]))
277 eprint("awesome: -c require a file\n");
280 eprint("usage: awesome [-v | -c configfile]\n");
283 /* Tag won't be printed otherwised */
284 setlocale(LC_CTYPE
, "");
286 if(!(dpy
= XOpenDisplay(NULL
)))
287 eprint("awesome: cannot open display\n");
289 xfd
= ConnectionNumber(dpy
);
291 XSetErrorHandler(xerrorstart
);
292 for(screen
= 0; screen
< ScreenCount(dpy
); screen
++)
293 /* this causes an error if some other window manager is running */
294 XSelectInput(dpy
, RootWindow(dpy
, screen
), SubstructureRedirectMask
);
296 /* need to XSync to validate errorhandler */
298 XSetErrorHandler(NULL
);
299 xerrorxlib
= XSetErrorHandler(xerror
);
303 awesomeconf
= p_new(awesome_config
, get_screen_count(dpy
));
304 clients
= p_new(Client
*, 1);
306 for(screen
= 0; screen
< get_screen_count(dpy
); screen
++)
309 awesomeconf
[screen
].display
= dpy
;
312 awesomeconf
[screen
].screen
= screen
;
313 setup_screen(&awesomeconf
[screen
], confpath
);
314 awesomeconf
[screen
].clients
= clients
;
315 drawstatusbar(&awesomeconf
[screen
]);
318 netatom
[NetSupported
] = XInternAtom(dpy
, "_NET_SUPPORTED", False
);
319 netatom
[NetWMName
] = XInternAtom(dpy
, "_NET_WM_NAME", False
);
321 /* do this only for real screen */
322 for(screen
= 0; screen
< ScreenCount(dpy
); screen
++)
324 loadawesomeprops(&awesomeconf
[screen
]);
325 XChangeProperty(dpy
, RootWindow(dpy
, screen
), netatom
[NetSupported
],
326 XA_ATOM
, 32, PropModeReplace
, (unsigned char *) netatom
, NetLast
);
329 handler
= p_new(event_handler
*, LASTEvent
);
330 handler
[ButtonPress
] = handle_event_buttonpress
;
331 handler
[ConfigureRequest
] = handle_event_configurerequest
;
332 handler
[ConfigureNotify
] = handle_event_configurenotify
;
333 handler
[DestroyNotify
] = handle_event_destroynotify
;
334 handler
[EnterNotify
] = handle_event_enternotify
;
335 handler
[LeaveNotify
] = handle_event_leavenotify
;
336 handler
[Expose
] = handle_event_expose
;
337 handler
[KeyPress
] = handle_event_keypress
;
338 handler
[MappingNotify
] = handle_event_mappingnotify
;
339 handler
[MapRequest
] = handle_event_maprequest
;
340 handler
[PropertyNotify
] = handle_event_propertynotify
;
341 handler
[UnmapNotify
] = handle_event_unmapnotify
;
343 /* check for shape extension */
344 if((awesomeconf
[0].have_shape
= XShapeQueryExtension(dpy
, &shape_event
, &e_dummy
)))
346 p_realloc(&handler
, shape_event
+ 1);
347 handler
[shape_event
] = handle_event_shape
;
350 /* check for randr extension */
351 if((awesomeconf
[0].have_randr
= XRRQueryExtension(dpy
, &randr_event_base
, &e_dummy
)))
353 p_realloc(&handler
, randr_event_base
+ RRScreenChangeNotify
+ 1);
354 handler
[randr_event_base
+ RRScreenChangeNotify
] = handle_event_randr_screen_change_notify
;
357 for(screen
= 0; screen
< get_screen_count(dpy
); screen
++)
359 awesomeconf
[screen
].have_shape
= awesomeconf
[0].have_shape
;
360 awesomeconf
[screen
].have_randr
= awesomeconf
[0].have_randr
;
368 csfd
= get_client_socket();
369 addr
= get_client_addr(getenv("DISPLAY"));
371 if(bind(csfd
, (const struct sockaddr
*) addr
, SUN_LEN(addr
)))
373 if(errno
== EADDRINUSE
)
375 if(unlink(addr
->sun_path
))
376 perror("error unlinking existing file");
377 if(bind(csfd
, (const struct sockaddr
*) addr
, SUN_LEN(addr
)))
378 perror("error binding UNIX domain socket");
381 perror("error binding UNIX domain socket");
384 /* main event loop, also reads status text from socket */
391 if(select(MAX(xfd
, csfd
) + 1, &rd
, NULL
, NULL
, NULL
) == -1)
395 eprint("select failed\n");
397 if(csfd
>= 0 && FD_ISSET(csfd
, &rd
))
398 switch (r
= recv(csfd
, buf
, sizeof(buf
)-1, MSG_TRUNC
))
401 perror("awesome: error reading UNIX domain socket");
402 a_strncpy(awesomeconf
[0].statustext
, sizeof(awesomeconf
[0].statustext
),
403 strerror(errno
), sizeof(awesomeconf
[0].statustext
) - 1);
404 awesomeconf
[0].statustext
[sizeof(awesomeconf
[0].statustext
) - 1] = '\0';
410 if(r
>= ssizeof(buf
))
413 parse_control(buf
, awesomeconf
);
418 XNextEvent(dpy
, &ev
);
420 handler
[ev
.type
](&ev
, awesomeconf
); /* call handler */
424 if(csfd
> 0 && close(csfd
))
425 perror("error closing UNIX domain socket");
426 if(unlink(addr
->sun_path
))
427 perror("error unlinking UNIX domain socket");
430 cleanup(awesomeconf
);
435 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99