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
]);
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
].selected_tags
);
98 p_delete(&awesomeconf
[screen
].prev_selected_tags
);
99 p_delete(&awesomeconf
[screen
].tag_layouts
);
100 p_delete(&awesomeconf
[screen
].layouts
);
101 p_delete(&awesomeconf
[screen
].rules
);
102 p_delete(&awesomeconf
[screen
].keys
);
104 XSetInputFocus(disp
, PointerRoot
, RevertToPointerRoot
, CurrentTime
);
106 p_delete(&awesomeconf
);
110 /** Get a window state (WM_STATE)
111 * \param disp Display ref
112 * \param w Client window
116 getstate(Display
*disp
, Window w
)
120 unsigned char *p
= NULL
;
121 unsigned long n
, extra
;
123 status
= XGetWindowProperty(disp
, w
, XInternAtom(disp
, "WM_STATE", False
),
124 0L, 2L, False
, XInternAtom(disp
, "WM_STATE", False
),
125 &real
, &format
, &n
, &extra
, (unsigned char **) &p
);
126 if(status
!= Success
)
134 /** Scan X to find windows to manage
135 * \param disp Display ref
136 * \param screen Screen number
137 * \param drawcontext Drawcontext ref
138 * \param awesomeconf awesome config
141 scan(Display
*disp
, int screen
, DC
*drawcontext
, awesome_config
*awesomeconf
)
144 Window
*wins
, d1
, d2
;
145 XWindowAttributes wa
;
148 if(XQueryTree(disp
, RootWindow(disp
, screen
), &d1
, &d2
, &wins
, &num
))
150 for(i
= 0; i
< num
; i
++)
152 if(!XGetWindowAttributes(disp
, wins
[i
], &wa
)
153 || wa
.override_redirect
154 || XGetTransientForHint(disp
, wins
[i
], &d1
))
156 if(wa
.map_state
== IsViewable
|| getstate(disp
, wins
[i
]) == IconicState
)
157 manage(disp
, drawcontext
, wins
[i
], &wa
, awesomeconf
);
159 /* now the transients */
160 for(i
= 0; i
< num
; i
++)
162 if(!XGetWindowAttributes(disp
, wins
[i
], &wa
))
164 if(XGetTransientForHint(disp
, wins
[i
], &d1
)
165 && (wa
.map_state
== IsViewable
|| getstate(disp
, wins
[i
]) == IconicState
))
166 manage(disp
, drawcontext
, wins
[i
], &wa
, awesomeconf
);
173 /** Setup everything before running
174 * \param disp Display ref
175 * \param screen Screen number
176 * \param awesomeconf awesome config ref
177 * \todo clean things...
180 setup(Display
*disp
, int screen
, DC
*drawcontext
, awesome_config
*awesomeconf
)
182 XSetWindowAttributes wa
;
185 drawcontext
->cursor
[CurNormal
] = XCreateFontCursor(disp
, XC_left_ptr
);
186 drawcontext
->cursor
[CurResize
] = XCreateFontCursor(disp
, XC_sizing
);
187 drawcontext
->cursor
[CurMove
] = XCreateFontCursor(disp
, XC_fleur
);
189 /* select for events */
190 wa
.event_mask
= SubstructureRedirectMask
| SubstructureNotifyMask
191 | EnterWindowMask
| LeaveWindowMask
| StructureNotifyMask
;
192 wa
.cursor
= drawcontext
->cursor
[CurNormal
];
194 XChangeWindowAttributes(disp
, RootWindow(disp
, screen
), CWEventMask
| CWCursor
, &wa
);
196 XSelectInput(disp
, RootWindow(disp
, screen
), wa
.event_mask
);
198 grabkeys(disp
, screen
, awesomeconf
);
200 compileregs(awesomeconf
->rules
, awesomeconf
->nrules
);
203 drawcontext
->h
= awesomeconf
->statusbar
.height
= drawcontext
->font
.height
+ 2;
204 initstatusbar(disp
, screen
, drawcontext
, &awesomeconf
->statusbar
);
205 drawcontext
->gc
= XCreateGC(disp
, RootWindow(disp
, screen
), 0, 0);
206 XSetLineAttributes(disp
, drawcontext
->gc
, 1, LineSolid
, CapButt
, JoinMiter
);
208 if(!drawcontext
->font
.set
)
209 XSetFont(disp
, drawcontext
->gc
, drawcontext
->font
.xfont
->fid
);
211 loadawesomeprops(disp
, awesomeconf
);
214 /** Startup Error handler to check if another window manager
215 * is already running.
216 * \param disp Display ref
217 * \param ee Error event
219 static int __attribute__ ((noreturn
))
220 xerrorstart(Display
* disp
__attribute__ ((unused
)), XErrorEvent
* ee
__attribute__ ((unused
)))
222 eprint("awesome: another window manager is already running\n");
226 * \param disp Display ref
227 * \param screen Screen number
228 * \param drawcontext Drawcontext ref
229 * \param awesomeconf awesome config
231 * \ingroup ui_callback
234 uicb_quit(Display
*disp
__attribute__ ((unused
)),
235 DC
*drawcontext
__attribute__ ((unused
)),
236 awesome_config
*awesomeconf
__attribute__((unused
)),
237 const char *arg
__attribute__ ((unused
)))
239 readin
= running
= False
;
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
272 main(int argc
, char *argv
[])
279 awesome_config
*awesomeconf
;
280 int shape_event
, randr_event_base
;
282 enum { NetSupported
, NetWMName
, NetLast
}; /* EWMH atoms */
283 Atom netatom
[NetLast
];
285 if(argc
== 2 && !strcmp("-v", argv
[1]))
287 printf("awesome-" VERSION
" © 2007 Julien Danjou\n");
291 eprint("usage: awesome [-v]\n");
293 /* Tag won't be printed otherwised */
294 setlocale(LC_CTYPE
, "");
296 if(!(dpy
= XOpenDisplay(NULL
)))
297 eprint("awesome: cannot open display\n");
299 xfd
= ConnectionNumber(dpy
);
301 XSetErrorHandler(xerrorstart
);
302 for(screen
= 0; screen
< ScreenCount(dpy
); screen
++)
303 /* this causes an error if some other window manager is running */
304 XSelectInput(dpy
, RootWindow(dpy
, screen
), SubstructureRedirectMask
);
306 /* need to XSync to validate errorhandler */
308 XSetErrorHandler(NULL
);
309 xerrorxlib
= XSetErrorHandler(xerror
);
312 netatom
[NetSupported
] = XInternAtom(dpy
, "_NET_SUPPORTED", False
);
313 netatom
[NetWMName
] = XInternAtom(dpy
, "_NET_WM_NAME", False
);
316 dc
= p_new(DC
, ScreenCount(dpy
));
317 awesomeconf
= p_new(awesome_config
, ScreenCount(dpy
));
319 for(screen
= 0; screen
< ScreenCount(dpy
); screen
++)
321 parse_config(dpy
, screen
, &dc
[screen
], &awesomeconf
[screen
]);
322 setup(dpy
, screen
, &dc
[screen
], &awesomeconf
[screen
]);
323 XChangeProperty(dpy
, RootWindow(dpy
, screen
), netatom
[NetSupported
],
324 XA_ATOM
, 32, PropModeReplace
, (unsigned char *) netatom
, NetLast
);
325 drawstatusbar(dpy
, screen
, &dc
[screen
], &awesomeconf
[screen
]);
328 void (*handler
[LASTEvent
]) (XEvent
*, awesome_config
*) =
330 [ButtonPress
] = handle_event_buttonpress
,
331 [ConfigureRequest
] = handle_event_configurerequest
,
332 [ConfigureNotify
] = handle_event_configurenotify
,
333 [DestroyNotify
] = handle_event_destroynotify
,
334 [EnterNotify
] = handle_event_enternotify
,
335 [LeaveNotify
] = handle_event_leavenotify
,
336 [Expose
] = handle_event_expose
,
337 [KeyPress
] = handle_event_keypress
,
338 [MappingNotify
] = handle_event_mappingnotify
,
339 [MapRequest
] = handle_event_maprequest
,
340 [PropertyNotify
] = handle_event_propertynotify
,
341 [UnmapNotify
] = handle_event_unmapnotify
,
344 /* check for shape extension */
345 if((awesomeconf
[0].have_shape
= XShapeQueryExtension(dpy
, &shape_event
, &e_dummy
)))
346 handler
[shape_event
] = handle_event_shape
;
348 /* check for randr extension */
349 if((awesomeconf
[0].have_randr
= XRRQueryExtension(dpy
, &randr_event_base
, &e_dummy
)))
350 handler
[randr_event_base
+ RRScreenChangeNotify
] = handle_event_randr_screen_change_notify
;
352 for(screen
= 0; screen
< ScreenCount(dpy
); screen
++)
354 awesomeconf
[screen
].have_shape
= awesomeconf
[0].have_shape
;
355 awesomeconf
[screen
].have_randr
= awesomeconf
[0].have_randr
;
356 scan(dpy
, screen
, &dc
[screen
], &awesomeconf
[screen
]);
361 /* main event loop, also reads status text from stdin */
366 FD_SET(STDIN_FILENO
, &rd
);
368 if(select(xfd
+ 1, &rd
, NULL
, NULL
, NULL
) == -1)
372 eprint("select failed\n");
374 if(FD_ISSET(STDIN_FILENO
, &rd
))
376 switch (r
= read(STDIN_FILENO
, awesomeconf
[0].statustext
, sizeof(awesomeconf
[0].statustext
) - 1))
379 strncpy(awesomeconf
[0].statustext
, strerror(errno
), sizeof(awesomeconf
[0].statustext
) - 1);
380 awesomeconf
[0].statustext
[sizeof(awesomeconf
[0].statustext
) - 1] = '\0';
384 strncpy(awesomeconf
[0].statustext
, "EOF", 4);
388 for(awesomeconf
[0].statustext
[r
] = '\0', p
= awesomeconf
[0].statustext
+ a_strlen(awesomeconf
[0].statustext
) - 1;
389 p
>= awesomeconf
[0].statustext
&& *p
== '\n'; *p
-- = '\0');
390 for(; p
>= awesomeconf
[0].statustext
&& *p
!= '\n'; --p
);
391 if(p
> awesomeconf
[0].statustext
)
392 strncpy(awesomeconf
[0].statustext
, p
+ 1, sizeof(awesomeconf
[0].statustext
));
394 drawstatusbar(dpy
, 0, &dc
[0], &awesomeconf
[0]);
399 XNextEvent(dpy
, &ev
);
401 handler
[ev
.type
](&ev
, awesomeconf
); /* call handler */
404 cleanup(dpy
, dc
, awesomeconf
);