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
< get_screen_count(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
, DC
*drawcontext
, awesome_config
*awesomeconf
)
141 int screen
, real_screen
;
142 Window
*wins
= NULL
, d1
, d2
;
143 XWindowAttributes wa
;
145 for(screen
= 0; screen
< ScreenCount(disp
); screen
++)
147 if(XQueryTree(disp
, RootWindow(disp
, screen
), &d1
, &d2
, &wins
, &num
))
149 real_screen
= screen
;
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
)
159 real_screen
= get_screen_bycoord(disp
, wa
.x
, wa
.y
);
160 manage(disp
, &drawcontext
[real_screen
], wins
[i
], &wa
, &awesomeconf
[real_screen
]);
163 /* now the transients */
164 for(i
= 0; i
< num
; i
++)
166 if(!XGetWindowAttributes(disp
, wins
[i
], &wa
))
168 if(XGetTransientForHint(disp
, wins
[i
], &d1
)
169 && (wa
.map_state
== IsViewable
|| getstate(disp
, wins
[i
]) == IconicState
))
172 real_screen
= get_screen_bycoord(disp
, wa
.x
, wa
.y
);
173 manage(disp
, &drawcontext
[real_screen
], wins
[i
], &wa
, &awesomeconf
[real_screen
]);
182 /** Setup everything before running
183 * \param disp Display ref
184 * \param screen Screen number
185 * \param awesomeconf awesome config ref
186 * \todo clean things...
189 setup(Display
*disp
, int screen
, DC
*drawcontext
, awesome_config
*awesomeconf
)
191 XSetWindowAttributes wa
;
194 if(XineramaIsActive(disp
))
195 real_screen
= DefaultScreen(disp
);
197 real_screen
= screen
;
200 drawcontext
->cursor
[CurNormal
] = XCreateFontCursor(disp
, XC_left_ptr
);
201 drawcontext
->cursor
[CurResize
] = XCreateFontCursor(disp
, XC_sizing
);
202 drawcontext
->cursor
[CurMove
] = XCreateFontCursor(disp
, XC_fleur
);
204 /* select for events */
205 wa
.event_mask
= SubstructureRedirectMask
| SubstructureNotifyMask
206 | EnterWindowMask
| LeaveWindowMask
| StructureNotifyMask
;
207 wa
.cursor
= drawcontext
->cursor
[CurNormal
];
209 XChangeWindowAttributes(disp
, RootWindow(disp
, real_screen
), CWEventMask
| CWCursor
, &wa
);
211 XSelectInput(disp
, RootWindow(disp
, real_screen
), wa
.event_mask
);
213 grabkeys(disp
, real_screen
, awesomeconf
);
215 compileregs(awesomeconf
->rules
, awesomeconf
->nrules
);
218 drawcontext
->h
= awesomeconf
->statusbar
.height
= drawcontext
->font
.height
+ 2;
219 initstatusbar(disp
, screen
, drawcontext
, &awesomeconf
->statusbar
);
220 drawcontext
->gc
= XCreateGC(disp
, RootWindow(disp
, real_screen
), 0, 0);
221 XSetLineAttributes(disp
, drawcontext
->gc
, 1, LineSolid
, CapButt
, JoinMiter
);
223 if(!drawcontext
->font
.set
)
224 XSetFont(disp
, drawcontext
->gc
, drawcontext
->font
.xfont
->fid
);
226 loadawesomeprops(disp
, awesomeconf
);
229 /** Startup Error handler to check if another window manager
230 * is already running.
231 * \param disp Display ref
232 * \param ee Error event
234 static int __attribute__ ((noreturn
))
235 xerrorstart(Display
* disp
__attribute__ ((unused
)), XErrorEvent
* ee
__attribute__ ((unused
)))
237 eprint("awesome: another window manager is already running\n");
241 * \param disp Display ref
242 * \param screen Screen number
243 * \param drawcontext Drawcontext ref
244 * \param awesomeconf awesome config
246 * \ingroup ui_callback
249 uicb_quit(Display
*disp
__attribute__ ((unused
)),
250 DC
*drawcontext
__attribute__ ((unused
)),
251 awesome_config
*awesomeconf
__attribute__((unused
)),
252 const char *arg
__attribute__ ((unused
)))
254 readin
= running
= False
;
257 /* There's no way to check accesses to destroyed windows, thus those cases are
258 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
259 * default error handler, which may call exit.
262 xerror(Display
* edpy
, XErrorEvent
* ee
)
264 if(ee
->error_code
== BadWindow
265 || (ee
->request_code
== X_SetInputFocus
&& ee
->error_code
== BadMatch
)
266 || (ee
->request_code
== X_PolyText8
&& ee
->error_code
== BadDrawable
)
267 || (ee
->request_code
== X_PolyFillRectangle
268 && ee
->error_code
== BadDrawable
)
269 || (ee
->request_code
== X_PolySegment
&& ee
->error_code
== BadDrawable
)
270 || (ee
->request_code
== X_ConfigureWindow
271 && ee
->error_code
== BadMatch
) || (ee
->request_code
== X_GrabKey
272 && ee
->error_code
== BadAccess
)
273 || (ee
->request_code
== X_CopyArea
&& ee
->error_code
== BadDrawable
))
275 fprintf(stderr
, "awesome: fatal error: request code=%d, error code=%d\n",
276 ee
->request_code
, ee
->error_code
);
278 return xerrorxlib(edpy
, ee
); /* may call exit */
281 /** Hello, this is main
282 * \param argc who knows
283 * \param argv who knows
284 * \return EXIT_SUCCESS I hope
286 typedef void event_handler (XEvent
*, awesome_config
*);
288 main(int argc
, char *argv
[])
291 const char *confpath
= NULL
;
296 awesome_config
*awesomeconf
;
297 int shape_event
, randr_event_base
;
299 enum { NetSupported
, NetWMName
, NetLast
}; /* EWMH atoms */
300 Atom netatom
[NetLast
];
301 event_handler
**handler
;
305 if(!a_strcmp("-v", argv
[1]))
307 printf("awesome-" VERSION
" © 2007 Julien Danjou\n");
310 else if(!a_strcmp("-c", argv
[1]))
312 if(a_strlen(argv
[2]))
315 eprint("awesome: -c require a file\n");
318 eprint("usage: awesome [-v | -c configfile]\n");
321 /* Tag won't be printed otherwised */
322 setlocale(LC_CTYPE
, "");
324 if(!(dpy
= XOpenDisplay(NULL
)))
325 eprint("awesome: cannot open display\n");
327 xfd
= ConnectionNumber(dpy
);
329 XSetErrorHandler(xerrorstart
);
330 for(screen
= 0; screen
< ScreenCount(dpy
); screen
++)
331 /* this causes an error if some other window manager is running */
332 XSelectInput(dpy
, RootWindow(dpy
, screen
), SubstructureRedirectMask
);
334 /* need to XSync to validate errorhandler */
336 XSetErrorHandler(NULL
);
337 xerrorxlib
= XSetErrorHandler(xerror
);
340 netatom
[NetSupported
] = XInternAtom(dpy
, "_NET_SUPPORTED", False
);
341 netatom
[NetWMName
] = XInternAtom(dpy
, "_NET_WM_NAME", False
);
344 dc
= p_new(DC
, get_screen_count(dpy
));
345 awesomeconf
= p_new(awesome_config
, get_screen_count(dpy
));
347 for(screen
= 0; screen
< get_screen_count(dpy
); screen
++)
349 parse_config(dpy
, screen
, &dc
[screen
], confpath
, &awesomeconf
[screen
]);
350 setup(dpy
, screen
, &dc
[screen
], &awesomeconf
[screen
]);
351 XChangeProperty(dpy
, RootWindow(dpy
, screen
), netatom
[NetSupported
],
352 XA_ATOM
, 32, PropModeReplace
, (unsigned char *) netatom
, NetLast
);
353 drawstatusbar(dpy
, &dc
[screen
], &awesomeconf
[screen
]);
356 handler
= p_new(event_handler
*, LASTEvent
);
357 handler
[ButtonPress
] = handle_event_buttonpress
;
358 handler
[ConfigureRequest
] = handle_event_configurerequest
;
359 handler
[ConfigureNotify
] = handle_event_configurenotify
;
360 handler
[DestroyNotify
] = handle_event_destroynotify
;
361 handler
[EnterNotify
] = handle_event_enternotify
;
362 handler
[LeaveNotify
] = handle_event_leavenotify
;
363 handler
[Expose
] = handle_event_expose
;
364 handler
[KeyPress
] = handle_event_keypress
;
365 handler
[MappingNotify
] = handle_event_mappingnotify
;
366 handler
[MapRequest
] = handle_event_maprequest
;
367 handler
[PropertyNotify
] = handle_event_propertynotify
;
368 handler
[UnmapNotify
] = handle_event_unmapnotify
;
370 /* check for shape extension */
371 if((awesomeconf
[0].have_shape
= XShapeQueryExtension(dpy
, &shape_event
, &e_dummy
)))
373 p_realloc(&handler
, shape_event
+ 1);
374 handler
[shape_event
] = handle_event_shape
;
377 /* check for randr extension */
378 if((awesomeconf
[0].have_randr
= XRRQueryExtension(dpy
, &randr_event_base
, &e_dummy
)))
380 p_realloc(&handler
, randr_event_base
+ RRScreenChangeNotify
+ 1);
381 handler
[randr_event_base
+ RRScreenChangeNotify
] = handle_event_randr_screen_change_notify
;
384 for(screen
= 0; screen
< get_screen_count(dpy
); screen
++)
386 awesomeconf
[screen
].have_shape
= awesomeconf
[0].have_shape
;
387 awesomeconf
[screen
].have_randr
= awesomeconf
[0].have_randr
;
390 scan(dpy
, dc
, awesomeconf
);
394 /* main event loop, also reads status text from stdin */
399 FD_SET(STDIN_FILENO
, &rd
);
401 if(select(xfd
+ 1, &rd
, NULL
, NULL
, NULL
) == -1)
405 eprint("select failed\n");
407 if(FD_ISSET(STDIN_FILENO
, &rd
))
409 switch (r
= read(STDIN_FILENO
, awesomeconf
[0].statustext
, sizeof(awesomeconf
[0].statustext
) - 1))
412 a_strncpy(awesomeconf
[0].statustext
, sizeof(awesomeconf
[0].statustext
),
413 strerror(errno
), sizeof(awesomeconf
[0].statustext
) - 1);
414 awesomeconf
[0].statustext
[sizeof(awesomeconf
[0].statustext
) - 1] = '\0';
418 a_strncpy(awesomeconf
[0].statustext
, sizeof(awesomeconf
[0].statustext
),
423 for(awesomeconf
[0].statustext
[r
] = '\0', p
= awesomeconf
[0].statustext
+ a_strlen(awesomeconf
[0].statustext
) - 1;
424 p
>= awesomeconf
[0].statustext
&& *p
== '\n'; *p
-- = '\0');
425 for(; p
>= awesomeconf
[0].statustext
&& *p
!= '\n'; --p
);
426 if(p
> awesomeconf
[0].statustext
)
427 a_strncpy(awesomeconf
[0].statustext
, sizeof(awesomeconf
[0].statustext
),
428 p
+ 1, sizeof(awesomeconf
[0].statustext
));
430 drawstatusbar(dpy
, &dc
[0], &awesomeconf
[0]);
435 XNextEvent(dpy
, &ev
);
437 handler
[ev
.type
](&ev
, awesomeconf
); /* call handler */
440 cleanup(dpy
, dc
, awesomeconf
);