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 drawcontext Drawcontext ref
53 * \param awesomeconf awesome config
56 cleanup(DC
*drawcontext
, awesome_config
*awesomeconf
)
65 unmanage(stack
, drawcontext
, NormalState
, awesomeconf
);
68 for(screen
= 0; screen
< get_screen_count(awesomeconf
->display
); screen
++)
70 XftFontClose(awesomeconf
->display
, drawcontext
->font
);
72 XUngrabKey(awesomeconf
->display
, AnyKey
, AnyModifier
, RootWindow(awesomeconf
->display
, screen
));
74 XFreePixmap(awesomeconf
->display
, awesomeconf
[screen
].statusbar
.drawable
);
75 XFreeGC(awesomeconf
->display
, drawcontext
[screen
].gc
);
76 XDestroyWindow(awesomeconf
->display
, awesomeconf
[screen
].statusbar
.window
);
77 XFreeCursor(awesomeconf
->display
, drawcontext
[screen
].cursor
[CurNormal
]);
78 XFreeCursor(awesomeconf
->display
, drawcontext
[screen
].cursor
[CurResize
]);
79 XFreeCursor(awesomeconf
->display
, drawcontext
[screen
].cursor
[CurMove
]);
81 for(i
= 0; i
< awesomeconf
[screen
].ntags
; i
++)
82 p_delete(&awesomeconf
[screen
].tags
[i
].name
);
83 for(i
= 0; i
< awesomeconf
[screen
].nkeys
; i
++)
84 p_delete(&awesomeconf
[screen
].keys
[i
].arg
);
85 for(i
= 0; i
< awesomeconf
[screen
].nlayouts
; i
++)
86 p_delete(&awesomeconf
[screen
].layouts
[i
].symbol
);
87 for(i
= 0; i
< awesomeconf
[screen
].nrules
; i
++)
89 p_delete(&awesomeconf
[screen
].rules
[i
].prop
);
90 p_delete(&awesomeconf
[screen
].rules
[i
].tags
);
92 p_delete(&awesomeconf
[screen
].tags
);
93 p_delete(&awesomeconf
[screen
].layouts
);
94 p_delete(&awesomeconf
[screen
].rules
);
95 p_delete(&awesomeconf
[screen
].keys
);
97 XSetInputFocus(awesomeconf
->display
, PointerRoot
, RevertToPointerRoot
, CurrentTime
);
98 XSync(awesomeconf
->display
, False
);
99 p_delete(&awesomeconf
);
103 /** Get a window state (WM_STATE)
104 * \param disp Display ref
105 * \param w Client window
109 getstate(Display
*disp
, Window w
)
113 unsigned char *p
= NULL
;
114 unsigned long n
, extra
;
116 status
= XGetWindowProperty(disp
, w
, XInternAtom(disp
, "WM_STATE", False
),
117 0L, 2L, False
, XInternAtom(disp
, "WM_STATE", False
),
118 &real
, &format
, &n
, &extra
, (unsigned char **) &p
);
119 if(status
!= Success
)
127 /** Scan X to find windows to manage
128 * \param screen Screen number
129 * \param drawcontext Drawcontext ref
130 * \param awesomeconf awesome config
133 scan(DC
*drawcontext
, awesome_config
*awesomeconf
)
136 int screen
, real_screen
;
137 Window
*wins
= NULL
, d1
, d2
;
138 XWindowAttributes wa
;
140 for(screen
= 0; screen
< ScreenCount(awesomeconf
->display
); screen
++)
142 if(XQueryTree(awesomeconf
->display
, RootWindow(awesomeconf
->display
, screen
), &d1
, &d2
, &wins
, &num
))
144 real_screen
= screen
;
145 for(i
= 0; i
< num
; i
++)
147 if(!XGetWindowAttributes(awesomeconf
->display
, wins
[i
], &wa
)
148 || wa
.override_redirect
149 || XGetTransientForHint(awesomeconf
->display
, wins
[i
], &d1
))
151 if(wa
.map_state
== IsViewable
|| getstate(awesomeconf
->display
, wins
[i
]) == IconicState
)
154 real_screen
= get_screen_bycoord(awesomeconf
->display
, wa
.x
, wa
.y
);
155 manage(awesomeconf
->display
, &drawcontext
[real_screen
], wins
[i
], &wa
, &awesomeconf
[real_screen
]);
158 /* now the transients */
159 for(i
= 0; i
< num
; i
++)
161 if(!XGetWindowAttributes(awesomeconf
->display
, wins
[i
], &wa
))
163 if(XGetTransientForHint(awesomeconf
->display
, wins
[i
], &d1
)
164 && (wa
.map_state
== IsViewable
|| getstate(awesomeconf
->display
, wins
[i
]) == IconicState
))
167 real_screen
= get_screen_bycoord(awesomeconf
->display
, wa
.x
, wa
.y
);
168 manage(awesomeconf
->display
, &drawcontext
[real_screen
], wins
[i
], &wa
, &awesomeconf
[real_screen
]);
177 /** Setup everything before running
178 * \param screen Screen number
179 * \param awesomeconf awesome config ref
180 * \todo clean things...
183 setup(DC
*drawcontext
, awesome_config
*awesomeconf
)
185 XSetWindowAttributes wa
;
188 drawcontext
->cursor
[CurNormal
] = XCreateFontCursor(awesomeconf
->display
, XC_left_ptr
);
189 drawcontext
->cursor
[CurResize
] = XCreateFontCursor(awesomeconf
->display
, XC_sizing
);
190 drawcontext
->cursor
[CurMove
] = XCreateFontCursor(awesomeconf
->display
, XC_fleur
);
192 /* select for events */
193 wa
.event_mask
= SubstructureRedirectMask
| SubstructureNotifyMask
194 | EnterWindowMask
| LeaveWindowMask
| StructureNotifyMask
;
195 wa
.cursor
= drawcontext
->cursor
[CurNormal
];
197 XChangeWindowAttributes(awesomeconf
->display
, RootWindow(awesomeconf
->display
, awesomeconf
->phys_screen
), CWEventMask
| CWCursor
, &wa
);
199 XSelectInput(awesomeconf
->display
, RootWindow(awesomeconf
->display
, awesomeconf
->phys_screen
), wa
.event_mask
);
201 grabkeys(awesomeconf
->display
, awesomeconf
->phys_screen
, awesomeconf
);
203 compileregs(awesomeconf
->rules
, awesomeconf
->nrules
);
206 drawcontext
->h
= awesomeconf
->statusbar
.height
= drawcontext
->font
->height
+ 2;
207 initstatusbar(awesomeconf
->display
, awesomeconf
->screen
, drawcontext
, &awesomeconf
->statusbar
);
208 drawcontext
->gc
= XCreateGC(awesomeconf
->display
, RootWindow(awesomeconf
->display
, awesomeconf
->phys_screen
), 0, 0);
209 XSetLineAttributes(awesomeconf
->display
, drawcontext
->gc
, 1, LineSolid
, CapButt
, JoinMiter
);
211 loadawesomeprops(awesomeconf
->display
, 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 drawcontext Drawcontext ref
228 * \param awesomeconf awesome config
230 * \ingroup ui_callback
233 uicb_quit(Display
*disp
__attribute__ ((unused
)),
234 DC
*drawcontext
__attribute__ ((unused
)),
235 awesome_config
*awesomeconf
__attribute__((unused
)),
236 const char *arg
__attribute__ ((unused
)))
238 readin
= running
= False
;
241 /* There's no way to check accesses to destroyed windows, thus those cases are
242 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
243 * default error handler, which may call exit.
246 xerror(Display
* edpy
, XErrorEvent
* ee
)
248 if(ee
->error_code
== BadWindow
249 || (ee
->request_code
== X_SetInputFocus
&& ee
->error_code
== BadMatch
)
250 || (ee
->request_code
== X_PolyText8
&& ee
->error_code
== BadDrawable
)
251 || (ee
->request_code
== X_PolyFillRectangle
252 && ee
->error_code
== BadDrawable
)
253 || (ee
->request_code
== X_PolySegment
&& ee
->error_code
== BadDrawable
)
254 || (ee
->request_code
== X_ConfigureWindow
255 && ee
->error_code
== BadMatch
) || (ee
->request_code
== X_GrabKey
256 && ee
->error_code
== BadAccess
)
257 || (ee
->request_code
== X_CopyArea
&& ee
->error_code
== BadDrawable
))
259 fprintf(stderr
, "awesome: fatal error: request code=%d, error code=%d\n",
260 ee
->request_code
, ee
->error_code
);
262 return xerrorxlib(edpy
, ee
); /* may call exit */
265 /** Hello, this is main
266 * \param argc who knows
267 * \param argv who knows
268 * \return EXIT_SUCCESS I hope
270 typedef void event_handler (XEvent
*, awesome_config
*);
272 main(int argc
, char *argv
[])
275 const char *confpath
= NULL
;
280 awesome_config
*awesomeconf
;
281 int shape_event
, randr_event_base
;
283 enum { NetSupported
, NetWMName
, NetLast
}; /* EWMH atoms */
284 Atom netatom
[NetLast
];
285 event_handler
**handler
;
289 if(!a_strcmp("-v", argv
[1]))
291 printf("awesome-" VERSION
" © 2007 Julien Danjou\n");
294 else if(!a_strcmp("-c", argv
[1]))
296 if(a_strlen(argv
[2]))
299 eprint("awesome: -c require a file\n");
302 eprint("usage: awesome [-v | -c configfile]\n");
305 /* Tag won't be printed otherwised */
306 setlocale(LC_CTYPE
, "");
308 if(!(dpy
= XOpenDisplay(NULL
)))
309 eprint("awesome: cannot open display\n");
311 xfd
= ConnectionNumber(dpy
);
313 XSetErrorHandler(xerrorstart
);
314 for(screen
= 0; screen
< ScreenCount(dpy
); screen
++)
315 /* this causes an error if some other window manager is running */
316 XSelectInput(dpy
, RootWindow(dpy
, screen
), SubstructureRedirectMask
);
318 /* need to XSync to validate errorhandler */
320 XSetErrorHandler(NULL
);
321 xerrorxlib
= XSetErrorHandler(xerror
);
324 netatom
[NetSupported
] = XInternAtom(dpy
, "_NET_SUPPORTED", False
);
325 netatom
[NetWMName
] = XInternAtom(dpy
, "_NET_WM_NAME", False
);
328 dc
= p_new(DC
, get_screen_count(dpy
));
329 awesomeconf
= p_new(awesome_config
, get_screen_count(dpy
));
331 for(screen
= 0; screen
< get_screen_count(dpy
); screen
++)
333 parse_config(dpy
, screen
, &dc
[screen
], confpath
, &awesomeconf
[screen
]);
334 setup(&dc
[screen
], &awesomeconf
[screen
]);
335 XChangeProperty(dpy
, RootWindow(dpy
, screen
), netatom
[NetSupported
],
336 XA_ATOM
, 32, PropModeReplace
, (unsigned char *) netatom
, NetLast
);
337 drawstatusbar(dpy
, &dc
[screen
], &awesomeconf
[screen
]);
340 handler
= p_new(event_handler
*, LASTEvent
);
341 handler
[ButtonPress
] = handle_event_buttonpress
;
342 handler
[ConfigureRequest
] = handle_event_configurerequest
;
343 handler
[ConfigureNotify
] = handle_event_configurenotify
;
344 handler
[DestroyNotify
] = handle_event_destroynotify
;
345 handler
[EnterNotify
] = handle_event_enternotify
;
346 handler
[LeaveNotify
] = handle_event_leavenotify
;
347 handler
[Expose
] = handle_event_expose
;
348 handler
[KeyPress
] = handle_event_keypress
;
349 handler
[MappingNotify
] = handle_event_mappingnotify
;
350 handler
[MapRequest
] = handle_event_maprequest
;
351 handler
[PropertyNotify
] = handle_event_propertynotify
;
352 handler
[UnmapNotify
] = handle_event_unmapnotify
;
354 /* check for shape extension */
355 if((awesomeconf
[0].have_shape
= XShapeQueryExtension(dpy
, &shape_event
, &e_dummy
)))
357 p_realloc(&handler
, shape_event
+ 1);
358 handler
[shape_event
] = handle_event_shape
;
361 /* check for randr extension */
362 if((awesomeconf
[0].have_randr
= XRRQueryExtension(dpy
, &randr_event_base
, &e_dummy
)))
364 p_realloc(&handler
, randr_event_base
+ RRScreenChangeNotify
+ 1);
365 handler
[randr_event_base
+ RRScreenChangeNotify
] = handle_event_randr_screen_change_notify
;
368 for(screen
= 0; screen
< get_screen_count(dpy
); screen
++)
370 awesomeconf
[screen
].have_shape
= awesomeconf
[0].have_shape
;
371 awesomeconf
[screen
].have_randr
= awesomeconf
[0].have_randr
;
374 scan(dc
, awesomeconf
);
378 /* main event loop, also reads status text from stdin */
383 FD_SET(STDIN_FILENO
, &rd
);
385 if(select(xfd
+ 1, &rd
, NULL
, NULL
, NULL
) == -1)
389 eprint("select failed\n");
391 if(FD_ISSET(STDIN_FILENO
, &rd
))
393 switch (r
= read(STDIN_FILENO
, awesomeconf
[0].statustext
, sizeof(awesomeconf
[0].statustext
) - 1))
396 a_strncpy(awesomeconf
[0].statustext
, sizeof(awesomeconf
[0].statustext
),
397 strerror(errno
), sizeof(awesomeconf
[0].statustext
) - 1);
398 awesomeconf
[0].statustext
[sizeof(awesomeconf
[0].statustext
) - 1] = '\0';
402 a_strncpy(awesomeconf
[0].statustext
, sizeof(awesomeconf
[0].statustext
),
407 for(awesomeconf
[0].statustext
[r
] = '\0', p
= awesomeconf
[0].statustext
+ a_strlen(awesomeconf
[0].statustext
) - 1;
408 p
>= awesomeconf
[0].statustext
&& *p
== '\n'; *p
-- = '\0');
409 for(; p
>= awesomeconf
[0].statustext
&& *p
!= '\n'; --p
);
410 if(p
> awesomeconf
[0].statustext
)
411 a_strncpy(awesomeconf
[0].statustext
, sizeof(awesomeconf
[0].statustext
),
412 p
+ 1, sizeof(awesomeconf
[0].statustext
));
414 drawstatusbar(dpy
, &dc
[0], &awesomeconf
[0]);
419 XNextEvent(dpy
, &ev
);
421 handler
[ev
.type
](&ev
, awesomeconf
); /* call handler */
424 cleanup(dc
, awesomeconf
);