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 <X11/cursorfont.h>
28 #include <X11/keysym.h>
29 #include <X11/Xatom.h>
30 #include <X11/Xproto.h>
31 #include <X11/Xutil.h>
32 #include <X11/extensions/shape.h>
33 #include <X11/extensions/Xrandr.h>
41 #include "statusbar.h"
43 Client
*clients
= NULL
;
48 static int (*xerrorxlib
) (Display
*, XErrorEvent
*);
49 static Bool readin
= True
, running
= True
;
51 /** Cleanup everything on quit
52 * \param disp Display ref
53 * \param drawcontext Drawcontext ref
54 * \param awesomeconf awesome config
57 cleanup(Display
*disp
, DC
*drawcontext
, awesome_config
*awesomeconf
)
66 unmanage(stack
, drawcontext
, NormalState
, awesomeconf
);
69 for(screen
= 0; screen
< ScreenCount(disp
); screen
++)
71 if(drawcontext
[screen
].font
.set
)
72 XFreeFontSet(disp
, drawcontext
[screen
].font
.set
);
74 XFreeFont(disp
, drawcontext
[screen
].font
.xfont
);
76 XUngrabKey(disp
, AnyKey
, AnyModifier
, RootWindow(disp
, screen
));
78 XFreePixmap(disp
, awesomeconf
[screen
].statusbar
.drawable
);
79 XFreeGC(disp
, drawcontext
[screen
].gc
);
80 XDestroyWindow(disp
, awesomeconf
[screen
].statusbar
.window
);
81 XFreeCursor(disp
, drawcontext
[screen
].cursor
[CurNormal
]);
82 XFreeCursor(disp
, drawcontext
[screen
].cursor
[CurResize
]);
83 XFreeCursor(disp
, drawcontext
[screen
].cursor
[CurMove
]);
85 for(i
= 0; i
< awesomeconf
[screen
].ntags
; i
++)
86 p_delete(&awesomeconf
[screen
].tags
[i
].name
);
87 for(i
= 0; i
< awesomeconf
[screen
].nkeys
; i
++)
88 p_delete(&awesomeconf
[screen
].keys
[i
].arg
);
89 for(i
= 0; i
< awesomeconf
[screen
].nlayouts
; i
++)
90 p_delete(&awesomeconf
[screen
].layouts
[i
].symbol
);
91 for(i
= 0; i
< awesomeconf
[screen
].nrules
; i
++)
93 p_delete(&awesomeconf
[screen
].rules
[i
].prop
);
94 p_delete(&awesomeconf
[screen
].rules
[i
].tags
);
96 p_delete(&awesomeconf
[screen
].tags
);
97 p_delete(&awesomeconf
[screen
].layouts
);
98 p_delete(&awesomeconf
[screen
].rules
);
99 p_delete(&awesomeconf
[screen
].keys
);
101 XSetInputFocus(disp
, PointerRoot
, RevertToPointerRoot
, CurrentTime
);
103 p_delete(&awesomeconf
);
107 /** Get a window state (WM_STATE)
108 * \param disp Display ref
109 * \param w Client window
113 getstate(Display
*disp
, Window w
)
117 unsigned char *p
= NULL
;
118 unsigned long n
, extra
;
120 status
= XGetWindowProperty(disp
, w
, XInternAtom(disp
, "WM_STATE", False
),
121 0L, 2L, False
, XInternAtom(disp
, "WM_STATE", False
),
122 &real
, &format
, &n
, &extra
, (unsigned char **) &p
);
123 if(status
!= Success
)
131 /** Scan X to find windows to manage
132 * \param disp Display ref
133 * \param screen Screen number
134 * \param drawcontext Drawcontext ref
135 * \param awesomeconf awesome config
138 scan(Display
*disp
, int screen
, DC
*drawcontext
, awesome_config
*awesomeconf
)
141 Window
*wins
, d1
, d2
;
142 XWindowAttributes wa
;
145 if(XQueryTree(disp
, RootWindow(disp
, screen
), &d1
, &d2
, &wins
, &num
))
147 for(i
= 0; i
< num
; i
++)
149 if(!XGetWindowAttributes(disp
, wins
[i
], &wa
)
150 || wa
.override_redirect
151 || XGetTransientForHint(disp
, wins
[i
], &d1
))
153 if(wa
.map_state
== IsViewable
|| getstate(disp
, wins
[i
]) == IconicState
)
154 manage(disp
, drawcontext
, wins
[i
], &wa
, awesomeconf
);
156 /* now the transients */
157 for(i
= 0; i
< num
; i
++)
159 if(!XGetWindowAttributes(disp
, wins
[i
], &wa
))
161 if(XGetTransientForHint(disp
, wins
[i
], &d1
)
162 && (wa
.map_state
== IsViewable
|| getstate(disp
, wins
[i
]) == IconicState
))
163 manage(disp
, drawcontext
, wins
[i
], &wa
, awesomeconf
);
170 /** Setup everything before running
171 * \param disp Display ref
172 * \param screen Screen number
173 * \param awesomeconf awesome config ref
174 * \todo clean things...
177 setup(Display
*disp
, int screen
, DC
*drawcontext
, awesome_config
*awesomeconf
)
179 XSetWindowAttributes wa
;
182 drawcontext
->cursor
[CurNormal
] = XCreateFontCursor(disp
, XC_left_ptr
);
183 drawcontext
->cursor
[CurResize
] = XCreateFontCursor(disp
, XC_sizing
);
184 drawcontext
->cursor
[CurMove
] = XCreateFontCursor(disp
, XC_fleur
);
186 /* select for events */
187 wa
.event_mask
= SubstructureRedirectMask
| SubstructureNotifyMask
188 | EnterWindowMask
| LeaveWindowMask
| StructureNotifyMask
;
189 wa
.cursor
= drawcontext
->cursor
[CurNormal
];
191 XChangeWindowAttributes(disp
, RootWindow(disp
, screen
), CWEventMask
| CWCursor
, &wa
);
193 XSelectInput(disp
, RootWindow(disp
, screen
), wa
.event_mask
);
195 grabkeys(disp
, screen
, awesomeconf
);
197 compileregs(awesomeconf
->rules
, awesomeconf
->nrules
);
200 drawcontext
->h
= awesomeconf
->statusbar
.height
= drawcontext
->font
.height
+ 2;
201 initstatusbar(disp
, screen
, drawcontext
, &awesomeconf
->statusbar
);
202 drawcontext
->gc
= XCreateGC(disp
, RootWindow(disp
, screen
), 0, 0);
203 XSetLineAttributes(disp
, drawcontext
->gc
, 1, LineSolid
, CapButt
, JoinMiter
);
205 if(!drawcontext
->font
.set
)
206 XSetFont(disp
, drawcontext
->gc
, drawcontext
->font
.xfont
->fid
);
208 loadawesomeprops(disp
, awesomeconf
);
211 /** Startup Error handler to check if another window manager
212 * is already running.
213 * \param disp Display ref
214 * \param ee Error event
216 static int __attribute__ ((noreturn
))
217 xerrorstart(Display
* disp
__attribute__ ((unused
)), XErrorEvent
* ee
__attribute__ ((unused
)))
219 eprint("awesome: another window manager is already running\n");
223 * \param disp Display ref
224 * \param screen Screen number
225 * \param drawcontext Drawcontext ref
226 * \param awesomeconf awesome config
228 * \ingroup ui_callback
231 uicb_quit(Display
*disp
__attribute__ ((unused
)),
232 DC
*drawcontext
__attribute__ ((unused
)),
233 awesome_config
*awesomeconf
__attribute__((unused
)),
234 const char *arg
__attribute__ ((unused
)))
236 readin
= running
= False
;
239 /* There's no way to check accesses to destroyed windows, thus those cases are
240 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
241 * default error handler, which may call exit.
244 xerror(Display
* edpy
, XErrorEvent
* ee
)
246 if(ee
->error_code
== BadWindow
247 || (ee
->request_code
== X_SetInputFocus
&& ee
->error_code
== BadMatch
)
248 || (ee
->request_code
== X_PolyText8
&& ee
->error_code
== BadDrawable
)
249 || (ee
->request_code
== X_PolyFillRectangle
250 && ee
->error_code
== BadDrawable
)
251 || (ee
->request_code
== X_PolySegment
&& ee
->error_code
== BadDrawable
)
252 || (ee
->request_code
== X_ConfigureWindow
253 && ee
->error_code
== BadMatch
) || (ee
->request_code
== X_GrabKey
254 && ee
->error_code
== BadAccess
)
255 || (ee
->request_code
== X_CopyArea
&& ee
->error_code
== BadDrawable
))
257 fprintf(stderr
, "awesome: fatal error: request code=%d, error code=%d\n",
258 ee
->request_code
, ee
->error_code
);
260 return xerrorxlib(edpy
, ee
); /* may call exit */
263 /** Hello, this is main
264 * \param argc who knows
265 * \param argv who knows
266 * \return EXIT_SUCCESS I hope
268 typedef void event_handler (XEvent
*, awesome_config
*);
270 main(int argc
, char *argv
[])
277 awesome_config
*awesomeconf
;
278 int shape_event
, randr_event_base
;
280 enum { NetSupported
, NetWMName
, NetLast
}; /* EWMH atoms */
281 Atom netatom
[NetLast
];
282 event_handler
**handler
;
284 if(argc
== 2 && !a_strcmp("-v", argv
[1]))
286 printf("awesome-" VERSION
" © 2007 Julien Danjou\n");
290 eprint("usage: awesome [-v]\n");
292 /* Tag won't be printed otherwised */
293 setlocale(LC_CTYPE
, "");
295 if(!(dpy
= XOpenDisplay(NULL
)))
296 eprint("awesome: cannot open display\n");
298 xfd
= ConnectionNumber(dpy
);
300 XSetErrorHandler(xerrorstart
);
301 for(screen
= 0; screen
< ScreenCount(dpy
); screen
++)
302 /* this causes an error if some other window manager is running */
303 XSelectInput(dpy
, RootWindow(dpy
, screen
), SubstructureRedirectMask
);
305 /* need to XSync to validate errorhandler */
307 XSetErrorHandler(NULL
);
308 xerrorxlib
= XSetErrorHandler(xerror
);
311 netatom
[NetSupported
] = XInternAtom(dpy
, "_NET_SUPPORTED", False
);
312 netatom
[NetWMName
] = XInternAtom(dpy
, "_NET_WM_NAME", False
);
315 dc
= p_new(DC
, ScreenCount(dpy
));
316 awesomeconf
= p_new(awesome_config
, ScreenCount(dpy
));
318 for(screen
= 0; screen
< ScreenCount(dpy
); screen
++)
320 parse_config(dpy
, screen
, &dc
[screen
], &awesomeconf
[screen
]);
321 setup(dpy
, screen
, &dc
[screen
], &awesomeconf
[screen
]);
322 XChangeProperty(dpy
, RootWindow(dpy
, screen
), netatom
[NetSupported
],
323 XA_ATOM
, 32, PropModeReplace
, (unsigned char *) netatom
, NetLast
);
324 drawstatusbar(dpy
, &dc
[screen
], &awesomeconf
[screen
]);
327 handler
= p_new(event_handler
*, LASTEvent
);
328 handler
[ButtonPress
] = handle_event_buttonpress
;
329 handler
[ConfigureRequest
] = handle_event_configurerequest
;
330 handler
[ConfigureNotify
] = handle_event_configurenotify
;
331 handler
[DestroyNotify
] = handle_event_destroynotify
;
332 handler
[EnterNotify
] = handle_event_enternotify
;
333 handler
[LeaveNotify
] = handle_event_leavenotify
;
334 handler
[Expose
] = handle_event_expose
;
335 handler
[KeyPress
] = handle_event_keypress
;
336 handler
[MappingNotify
] = handle_event_mappingnotify
;
337 handler
[MapRequest
] = handle_event_maprequest
;
338 handler
[PropertyNotify
] = handle_event_propertynotify
;
339 handler
[UnmapNotify
] = handle_event_unmapnotify
;
341 /* check for shape extension */
342 if((awesomeconf
[0].have_shape
= XShapeQueryExtension(dpy
, &shape_event
, &e_dummy
)))
344 p_realloc(&handler
, shape_event
+ 1);
345 handler
[shape_event
] = handle_event_shape
;
348 /* check for randr extension */
349 if((awesomeconf
[0].have_randr
= XRRQueryExtension(dpy
, &randr_event_base
, &e_dummy
)))
351 p_realloc(&handler
, randr_event_base
+ RRScreenChangeNotify
+ 1);
352 handler
[randr_event_base
+ RRScreenChangeNotify
] = handle_event_randr_screen_change_notify
;
355 for(screen
= 0; screen
< ScreenCount(dpy
); screen
++)
357 awesomeconf
[screen
].have_shape
= awesomeconf
[0].have_shape
;
358 awesomeconf
[screen
].have_randr
= awesomeconf
[0].have_randr
;
359 scan(dpy
, screen
, &dc
[screen
], &awesomeconf
[screen
]);
364 /* main event loop, also reads status text from stdin */
369 FD_SET(STDIN_FILENO
, &rd
);
371 if(select(xfd
+ 1, &rd
, NULL
, NULL
, NULL
) == -1)
375 eprint("select failed\n");
377 if(FD_ISSET(STDIN_FILENO
, &rd
))
379 switch (r
= read(STDIN_FILENO
, awesomeconf
[0].statustext
, sizeof(awesomeconf
[0].statustext
) - 1))
382 a_strncpy(awesomeconf
[0].statustext
, sizeof(awesomeconf
[0].statustext
),
383 strerror(errno
), sizeof(awesomeconf
[0].statustext
) - 1);
384 awesomeconf
[0].statustext
[sizeof(awesomeconf
[0].statustext
) - 1] = '\0';
388 a_strncpy(awesomeconf
[0].statustext
, sizeof(awesomeconf
[0].statustext
),
393 for(awesomeconf
[0].statustext
[r
] = '\0', p
= awesomeconf
[0].statustext
+ a_strlen(awesomeconf
[0].statustext
) - 1;
394 p
>= awesomeconf
[0].statustext
&& *p
== '\n'; *p
-- = '\0');
395 for(; p
>= awesomeconf
[0].statustext
&& *p
!= '\n'; --p
);
396 if(p
> awesomeconf
[0].statustext
)
397 a_strncpy(awesomeconf
[0].statustext
, sizeof(awesomeconf
[0].statustext
),
398 p
+ 1, sizeof(awesomeconf
[0].statustext
));
400 drawstatusbar(dpy
, &dc
[0], &awesomeconf
[0]);
405 XNextEvent(dpy
, &ev
);
407 handler
[ev
.type
](&ev
, awesomeconf
); /* call handler */
410 cleanup(dpy
, dc
, awesomeconf
);