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
)
90 cleanup_buttons(awesomeconf
->buttons
.tag
);
91 cleanup_buttons(awesomeconf
->buttons
.title
);
92 cleanup_buttons(awesomeconf
->buttons
.layout
);
93 cleanup_buttons(awesomeconf
->buttons
.root
);
94 cleanup_buttons(awesomeconf
->buttons
.client
);
96 for(i
= 0; i
< awesomeconf
->nlayouts
; i
++)
97 p_delete(&awesomeconf
->layouts
[i
].symbol
);
98 for(r
= awesomeconf
->rules
; r
; r
= rn
)
106 p_delete(&awesomeconf
->tags
);
107 p_delete(&awesomeconf
->layouts
);
108 p_delete(&awesomeconf
->configpath
);
111 /** Cleanup everything on quit
112 * \param awesomeconf awesome config
115 cleanup(awesome_config
*awesomeconf
)
119 while(*awesomeconf
->clients
)
121 client_unban(*awesomeconf
->clients
);
122 client_unmanage(*awesomeconf
->clients
, NormalState
, awesomeconf
);
125 for(screen
= 0; screen
< get_screen_count(awesomeconf
->display
); screen
++)
126 cleanup_screen(&awesomeconf
[screen
]);
128 XSetInputFocus(awesomeconf
->display
, PointerRoot
, RevertToPointerRoot
, CurrentTime
);
129 XSync(awesomeconf
->display
, False
);
131 p_delete(&awesomeconf
->clients
);
132 p_delete(&awesomeconf
);
135 /** Scan X to find windows to manage
136 * \param screen Screen number
137 * \param awesomeconf awesome config
140 scan(awesome_config
*awesomeconf
)
143 int screen
, real_screen
;
144 Window
*wins
= NULL
, d1
, d2
;
145 XWindowAttributes wa
;
147 for(screen
= 0; screen
< ScreenCount(awesomeconf
->display
); screen
++)
149 if(XQueryTree(awesomeconf
->display
, RootWindow(awesomeconf
->display
, screen
), &d1
, &d2
, &wins
, &num
))
151 real_screen
= screen
;
152 for(i
= 0; i
< num
; i
++)
154 if(!XGetWindowAttributes(awesomeconf
->display
, wins
[i
], &wa
)
155 || wa
.override_redirect
156 || XGetTransientForHint(awesomeconf
->display
, wins
[i
], &d1
))
158 if(wa
.map_state
== IsViewable
|| window_getstate(awesomeconf
->display
, wins
[i
]) == IconicState
)
161 real_screen
= get_screen_bycoord(awesomeconf
->display
, wa
.x
, wa
.y
);
162 client_manage(wins
[i
], &wa
, &awesomeconf
[real_screen
]);
165 /* now the transients */
166 for(i
= 0; i
< num
; i
++)
168 if(!XGetWindowAttributes(awesomeconf
->display
, wins
[i
], &wa
))
170 if(XGetTransientForHint(awesomeconf
->display
, wins
[i
], &d1
)
171 && (wa
.map_state
== IsViewable
|| window_getstate(awesomeconf
->display
, wins
[i
]) == IconicState
))
174 real_screen
= get_screen_bycoord(awesomeconf
->display
, wa
.x
, wa
.y
);
175 client_manage(wins
[i
], &wa
, &awesomeconf
[real_screen
]);
184 /** Setup everything before running
185 * \param awesomeconf awesome config ref
186 * \todo clean things...
189 setup(awesome_config
*awesomeconf
)
191 XSetWindowAttributes wa
;
194 awesomeconf
->cursor
[CurNormal
] = XCreateFontCursor(awesomeconf
->display
, XC_left_ptr
);
195 awesomeconf
->cursor
[CurResize
] = XCreateFontCursor(awesomeconf
->display
, XC_sizing
);
196 awesomeconf
->cursor
[CurMove
] = XCreateFontCursor(awesomeconf
->display
, XC_fleur
);
198 /* select for events */
199 wa
.event_mask
= SubstructureRedirectMask
| SubstructureNotifyMask
200 | EnterWindowMask
| LeaveWindowMask
| StructureNotifyMask
;
201 wa
.cursor
= awesomeconf
->cursor
[CurNormal
];
203 XChangeWindowAttributes(awesomeconf
->display
, RootWindow(awesomeconf
->display
, awesomeconf
->phys_screen
), CWEventMask
| CWCursor
, &wa
);
205 XSelectInput(awesomeconf
->display
, RootWindow(awesomeconf
->display
, awesomeconf
->phys_screen
), wa
.event_mask
);
207 grabkeys(awesomeconf
);
211 setup_screen(awesome_config
*awesomeconf
, const char *confpath
)
213 parse_config(confpath
, awesomeconf
);
215 initstatusbar(awesomeconf
->display
, awesomeconf
->screen
, &awesomeconf
->statusbar
,
216 awesomeconf
->cursor
[CurNormal
], awesomeconf
->font
,
217 awesomeconf
->layouts
, awesomeconf
->nlayouts
);
220 /** Startup Error handler to check if another window manager
221 * is already running.
222 * \param disp Display ref
223 * \param ee Error event
225 static int __attribute__ ((noreturn
))
226 xerrorstart(Display
* disp
__attribute__ ((unused
)), XErrorEvent
* ee
__attribute__ ((unused
)))
228 eprint("awesome: another window manager is already running\n");
232 * \param awesomeconf awesome config
234 * \ingroup ui_callback
237 uicb_quit(awesome_config
*awesomeconf
__attribute__((unused
)),
238 const char *arg
__attribute__ ((unused
)))
243 /* There's no way to check accesses to destroyed windows, thus those cases are
244 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
245 * default error handler, which may call exit.
248 xerror(Display
* edpy
, XErrorEvent
* ee
)
250 if(ee
->error_code
== BadWindow
251 || (ee
->request_code
== X_SetInputFocus
&& ee
->error_code
== BadMatch
)
252 || (ee
->request_code
== X_PolyText8
&& ee
->error_code
== BadDrawable
)
253 || (ee
->request_code
== X_PolyFillRectangle
254 && ee
->error_code
== BadDrawable
)
255 || (ee
->request_code
== X_PolySegment
&& ee
->error_code
== BadDrawable
)
256 || (ee
->request_code
== X_ConfigureWindow
257 && ee
->error_code
== BadMatch
) || (ee
->request_code
== X_GrabKey
258 && ee
->error_code
== BadAccess
)
259 || (ee
->request_code
== X_CopyArea
&& ee
->error_code
== BadDrawable
))
261 fprintf(stderr
, "awesome: fatal error: request code=%d, error code=%d\n",
262 ee
->request_code
, ee
->error_code
);
264 return xerrorxlib(edpy
, ee
); /* may call exit */
267 /** Hello, this is main
268 * \param argc who knows
269 * \param argv who knows
270 * \return EXIT_SUCCESS I hope
272 typedef void event_handler (XEvent
*, awesome_config
*);
274 main(int argc
, char *argv
[])
277 const char *confpath
= NULL
;
278 int r
, xfd
, e_dummy
, csfd
;
282 awesome_config
*awesomeconf
;
283 int shape_event
, randr_event_base
;
285 enum { NetSupported
, NetWMName
, NetLast
}; /* EWMH atoms */
286 Atom netatom
[NetLast
];
287 event_handler
**handler
;
289 struct sockaddr_un
*addr
;
293 if(!a_strcmp("-v", argv
[1]))
295 printf("awesome-" VERSION
" © 2007 Julien Danjou\n");
298 else if(!a_strcmp("-c", argv
[1]))
300 if(a_strlen(argv
[2]))
303 eprint("awesome: -c require a file\n");
306 eprint("usage: awesome [-v | -c configfile]\n");
309 /* Tag won't be printed otherwised */
310 setlocale(LC_CTYPE
, "");
312 if(!(dpy
= XOpenDisplay(NULL
)))
313 eprint("awesome: cannot open display\n");
315 xfd
= ConnectionNumber(dpy
);
317 XSetErrorHandler(xerrorstart
);
318 for(screen
= 0; screen
< ScreenCount(dpy
); screen
++)
319 /* this causes an error if some other window manager is running */
320 XSelectInput(dpy
, RootWindow(dpy
, screen
), SubstructureRedirectMask
);
322 /* need to XSync to validate errorhandler */
324 XSetErrorHandler(NULL
);
325 xerrorxlib
= XSetErrorHandler(xerror
);
329 awesomeconf
= p_new(awesome_config
, get_screen_count(dpy
));
330 clients
= p_new(Client
*, 1);
332 for(screen
= 0; screen
< get_screen_count(dpy
); screen
++)
335 awesomeconf
[screen
].display
= dpy
;
338 awesomeconf
[screen
].screen
= screen
;
339 setup_screen(&awesomeconf
[screen
], confpath
);
340 awesomeconf
[screen
].clients
= clients
;
341 drawstatusbar(&awesomeconf
[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(&awesomeconf
[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((awesomeconf
[0].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((awesomeconf
[0].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
;
383 for(screen
= 0; screen
< get_screen_count(dpy
); screen
++)
385 awesomeconf
[screen
].have_shape
= awesomeconf
[0].have_shape
;
386 awesomeconf
[screen
].have_randr
= awesomeconf
[0].have_randr
;
394 csfd
= get_client_socket();
395 addr
= get_client_addr(getenv("DISPLAY"));
397 if(bind(csfd
, (const struct sockaddr
*) addr
, SUN_LEN(addr
)))
399 if(errno
== EADDRINUSE
)
401 if(unlink(addr
->sun_path
))
402 perror("error unlinking existing file");
403 if(bind(csfd
, (const struct sockaddr
*) addr
, SUN_LEN(addr
)))
404 perror("error binding UNIX domain socket");
407 perror("error binding UNIX domain socket");
410 /* main event loop, also reads status text from socket */
417 if(select(MAX(xfd
, csfd
) + 1, &rd
, NULL
, NULL
, NULL
) == -1)
421 eprint("select failed\n");
423 if(csfd
>= 0 && FD_ISSET(csfd
, &rd
))
424 switch (r
= recv(csfd
, buf
, sizeof(buf
)-1, MSG_TRUNC
))
427 perror("awesome: error reading UNIX domain socket");
428 a_strncpy(awesomeconf
[0].statustext
, sizeof(awesomeconf
[0].statustext
),
429 strerror(errno
), sizeof(awesomeconf
[0].statustext
) - 1);
430 awesomeconf
[0].statustext
[sizeof(awesomeconf
[0].statustext
) - 1] = '\0';
436 if(r
>= ssizeof(buf
))
439 parse_control(buf
, awesomeconf
);
444 XNextEvent(dpy
, &ev
);
446 handler
[ev
.type
](&ev
, awesomeconf
); /* call handler */
450 if(csfd
> 0 && close(csfd
))
451 perror("error closing UNIX domain socket");
452 if(unlink(addr
->sun_path
))
453 perror("error unlinking UNIX domain socket");
456 cleanup(awesomeconf
);
461 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99