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>
30 #include <X11/cursorfont.h>
31 #include <X11/keysym.h>
32 #include <X11/Xatom.h>
33 #include <X11/Xproto.h>
34 #include <X11/Xutil.h>
35 #include <X11/extensions/shape.h>
36 #include <X11/extensions/Xrandr.h>
44 #include "statusbar.h"
47 #define CONTROL_FIFO_PATH ".awesome_ctl"
49 static int (*xerrorxlib
) (Display
*, XErrorEvent
*);
50 static Bool running
= True
;
52 /** Cleanup everything on quit
53 * \param awesomeconf awesome config
56 cleanup(awesome_config
*awesomeconf
)
60 while(*awesomeconf
->clients
)
62 unban(*awesomeconf
->clients
);
63 unmanage(*awesomeconf
->clients
, NormalState
, awesomeconf
);
66 for(screen
= 0; screen
< get_screen_count(awesomeconf
->display
); screen
++)
68 XftFontClose(awesomeconf
->display
, awesomeconf
->font
);
70 XUngrabKey(awesomeconf
->display
, AnyKey
, AnyModifier
, RootWindow(awesomeconf
->display
, awesomeconf
[screen
].phys_screen
));
72 XFreePixmap(awesomeconf
->display
, awesomeconf
[screen
].statusbar
.drawable
);
73 XDestroyWindow(awesomeconf
->display
, awesomeconf
[screen
].statusbar
.window
);
74 XFreeCursor(awesomeconf
->display
, awesomeconf
[screen
].cursor
[CurNormal
]);
75 XFreeCursor(awesomeconf
->display
, awesomeconf
[screen
].cursor
[CurResize
]);
76 XFreeCursor(awesomeconf
->display
, awesomeconf
[screen
].cursor
[CurMove
]);
78 for(i
= 0; i
< awesomeconf
[screen
].ntags
; i
++)
79 p_delete(&awesomeconf
[screen
].tags
[i
].name
);
80 for(i
= 0; i
< awesomeconf
[screen
].nkeys
; i
++)
81 p_delete(&awesomeconf
[screen
].keys
[i
].arg
);
82 for(i
= 0; i
< awesomeconf
[screen
].nlayouts
; i
++)
83 p_delete(&awesomeconf
[screen
].layouts
[i
].symbol
);
84 for(i
= 0; i
< awesomeconf
[screen
].nrules
; i
++)
86 p_delete(&awesomeconf
[screen
].rules
[i
].prop
);
87 p_delete(&awesomeconf
[screen
].rules
[i
].tags
);
89 p_delete(&awesomeconf
[screen
].tags
);
90 p_delete(&awesomeconf
[screen
].layouts
);
91 p_delete(&awesomeconf
[screen
].rules
);
92 p_delete(&awesomeconf
[screen
].keys
);
94 XSetInputFocus(awesomeconf
->display
, PointerRoot
, RevertToPointerRoot
, CurrentTime
);
95 XSync(awesomeconf
->display
, False
);
96 p_delete(&awesomeconf
->clients
);
97 p_delete(&awesomeconf
->client_sel
);
98 p_delete(&awesomeconf
);
101 /** Get a window state (WM_STATE)
102 * \param disp Display ref
103 * \param w Client window
107 getstate(Display
*disp
, Window w
)
111 unsigned char *p
= NULL
;
112 unsigned long n
, extra
;
114 status
= XGetWindowProperty(disp
, w
, XInternAtom(disp
, "WM_STATE", False
),
115 0L, 2L, False
, XInternAtom(disp
, "WM_STATE", False
),
116 &real
, &format
, &n
, &extra
, (unsigned char **) &p
);
117 if(status
!= Success
)
125 /** Scan X to find windows to manage
126 * \param screen Screen number
127 * \param awesomeconf awesome config
130 scan(awesome_config
*awesomeconf
)
133 int screen
, real_screen
;
134 Window
*wins
= NULL
, d1
, d2
;
135 XWindowAttributes wa
;
137 for(screen
= 0; screen
< ScreenCount(awesomeconf
->display
); screen
++)
139 if(XQueryTree(awesomeconf
->display
, RootWindow(awesomeconf
->display
, screen
), &d1
, &d2
, &wins
, &num
))
141 real_screen
= screen
;
142 for(i
= 0; i
< num
; i
++)
144 if(!XGetWindowAttributes(awesomeconf
->display
, wins
[i
], &wa
)
145 || wa
.override_redirect
146 || XGetTransientForHint(awesomeconf
->display
, wins
[i
], &d1
))
148 if(wa
.map_state
== IsViewable
|| getstate(awesomeconf
->display
, wins
[i
]) == IconicState
)
151 real_screen
= get_screen_bycoord(awesomeconf
->display
, wa
.x
, wa
.y
);
152 manage(awesomeconf
->display
, wins
[i
], &wa
, &awesomeconf
[real_screen
]);
155 /* now the transients */
156 for(i
= 0; i
< num
; i
++)
158 if(!XGetWindowAttributes(awesomeconf
->display
, wins
[i
], &wa
))
160 if(XGetTransientForHint(awesomeconf
->display
, wins
[i
], &d1
)
161 && (wa
.map_state
== IsViewable
|| getstate(awesomeconf
->display
, wins
[i
]) == IconicState
))
164 real_screen
= get_screen_bycoord(awesomeconf
->display
, wa
.x
, wa
.y
);
165 manage(awesomeconf
->display
, wins
[i
], &wa
, &awesomeconf
[real_screen
]);
174 /** Setup everything before running
175 * \param screen Screen number
176 * \param awesomeconf awesome config ref
177 * \todo clean things...
180 setup(awesome_config
*awesomeconf
)
182 XSetWindowAttributes wa
;
185 awesomeconf
->cursor
[CurNormal
] = XCreateFontCursor(awesomeconf
->display
, XC_left_ptr
);
186 awesomeconf
->cursor
[CurResize
] = XCreateFontCursor(awesomeconf
->display
, XC_sizing
);
187 awesomeconf
->cursor
[CurMove
] = XCreateFontCursor(awesomeconf
->display
, XC_fleur
);
189 /* select for events */
190 wa
.event_mask
= SubstructureRedirectMask
| SubstructureNotifyMask
191 | EnterWindowMask
| LeaveWindowMask
| StructureNotifyMask
;
192 wa
.cursor
= awesomeconf
->cursor
[CurNormal
];
194 XChangeWindowAttributes(awesomeconf
->display
, RootWindow(awesomeconf
->display
, awesomeconf
->phys_screen
), CWEventMask
| CWCursor
, &wa
);
196 XSelectInput(awesomeconf
->display
, RootWindow(awesomeconf
->display
, awesomeconf
->phys_screen
), wa
.event_mask
);
198 grabkeys(awesomeconf
->display
, awesomeconf
->phys_screen
, awesomeconf
);
201 initstatusbar(awesomeconf
->display
, awesomeconf
->screen
, &awesomeconf
->statusbar
, awesomeconf
->cursor
[CurNormal
], awesomeconf
->font
, awesomeconf
->layouts
, awesomeconf
->nlayouts
);
204 /** Startup Error handler to check if another window manager
205 * is already running.
206 * \param disp Display ref
207 * \param ee Error event
209 static int __attribute__ ((noreturn
))
210 xerrorstart(Display
* disp
__attribute__ ((unused
)), XErrorEvent
* ee
__attribute__ ((unused
)))
212 eprint("awesome: another window manager is already running\n");
216 * \param awesomeconf awesome config
218 * \ingroup ui_callback
221 uicb_quit(awesome_config
*awesomeconf
__attribute__((unused
)),
222 const char *arg
__attribute__ ((unused
)))
227 /* There's no way to check accesses to destroyed windows, thus those cases are
228 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
229 * default error handler, which may call exit.
232 xerror(Display
* edpy
, XErrorEvent
* ee
)
234 if(ee
->error_code
== BadWindow
235 || (ee
->request_code
== X_SetInputFocus
&& ee
->error_code
== BadMatch
)
236 || (ee
->request_code
== X_PolyText8
&& ee
->error_code
== BadDrawable
)
237 || (ee
->request_code
== X_PolyFillRectangle
238 && ee
->error_code
== BadDrawable
)
239 || (ee
->request_code
== X_PolySegment
&& ee
->error_code
== BadDrawable
)
240 || (ee
->request_code
== X_ConfigureWindow
241 && ee
->error_code
== BadMatch
) || (ee
->request_code
== X_GrabKey
242 && ee
->error_code
== BadAccess
)
243 || (ee
->request_code
== X_CopyArea
&& ee
->error_code
== BadDrawable
))
245 fprintf(stderr
, "awesome: fatal error: request code=%d, error code=%d\n",
246 ee
->request_code
, ee
->error_code
);
248 return xerrorxlib(edpy
, ee
); /* may call exit */
251 /** Hello, this is main
252 * \param argc who knows
253 * \param argv who knows
254 * \return EXIT_SUCCESS I hope
256 typedef void event_handler (XEvent
*, awesome_config
*);
258 main(int argc
, char *argv
[])
260 char *fifopath
, buf
[1024];
261 const char *confpath
= NULL
, *homedir
;
262 int r
, cfd
, xfd
, e_dummy
;
266 awesome_config
*awesomeconf
;
267 int shape_event
, randr_event_base
;
269 enum { NetSupported
, NetWMName
, NetLast
}; /* EWMH atoms */
270 Atom netatom
[NetLast
];
271 event_handler
**handler
;
272 Client
**clients
, **sel
;
274 ssize_t fifopath_len
;
278 if(!a_strcmp("-v", argv
[1]))
280 printf("awesome-" VERSION
" © 2007 Julien Danjou\n");
283 else if(!a_strcmp("-c", argv
[1]))
285 if(a_strlen(argv
[2]))
288 eprint("awesome: -c require a file\n");
291 eprint("usage: awesome [-v | -c configfile]\n");
294 /* Tag won't be printed otherwised */
295 setlocale(LC_CTYPE
, "");
297 if(!(dpy
= XOpenDisplay(NULL
)))
298 eprint("awesome: cannot open display\n");
300 xfd
= ConnectionNumber(dpy
);
302 XSetErrorHandler(xerrorstart
);
303 for(screen
= 0; screen
< ScreenCount(dpy
); screen
++)
304 /* this causes an error if some other window manager is running */
305 XSelectInput(dpy
, RootWindow(dpy
, screen
), SubstructureRedirectMask
);
307 /* need to XSync to validate errorhandler */
309 XSetErrorHandler(NULL
);
310 xerrorxlib
= XSetErrorHandler(xerror
);
314 awesomeconf
= p_new(awesome_config
, get_screen_count(dpy
));
315 clients
= p_new(Client
*, 1);
316 sel
= p_new(Client
*, 1);
318 for(screen
= 0; screen
< get_screen_count(dpy
); screen
++)
320 parse_config(dpy
, screen
, confpath
, &awesomeconf
[screen
]);
321 setup(&awesomeconf
[screen
]);
322 awesomeconf
[screen
].clients
= clients
;
323 awesomeconf
[screen
].client_sel
= sel
;
324 drawstatusbar(dpy
, &awesomeconf
[screen
]);
327 netatom
[NetSupported
] = XInternAtom(dpy
, "_NET_SUPPORTED", False
);
328 netatom
[NetWMName
] = XInternAtom(dpy
, "_NET_WM_NAME", False
);
330 /* do this only for real screen */
331 for(screen
= 0; screen
< ScreenCount(dpy
); screen
++)
333 loadawesomeprops(dpy
, &awesomeconf
[screen
]);
334 XChangeProperty(dpy
, RootWindow(dpy
, screen
), netatom
[NetSupported
],
335 XA_ATOM
, 32, PropModeReplace
, (unsigned char *) netatom
, NetLast
);
338 handler
= p_new(event_handler
*, LASTEvent
);
339 handler
[ButtonPress
] = handle_event_buttonpress
;
340 handler
[ConfigureRequest
] = handle_event_configurerequest
;
341 handler
[ConfigureNotify
] = handle_event_configurenotify
;
342 handler
[DestroyNotify
] = handle_event_destroynotify
;
343 handler
[EnterNotify
] = handle_event_enternotify
;
344 handler
[LeaveNotify
] = handle_event_leavenotify
;
345 handler
[Expose
] = handle_event_expose
;
346 handler
[KeyPress
] = handle_event_keypress
;
347 handler
[MappingNotify
] = handle_event_mappingnotify
;
348 handler
[MapRequest
] = handle_event_maprequest
;
349 handler
[PropertyNotify
] = handle_event_propertynotify
;
350 handler
[UnmapNotify
] = handle_event_unmapnotify
;
352 /* check for shape extension */
353 if((awesomeconf
[0].have_shape
= XShapeQueryExtension(dpy
, &shape_event
, &e_dummy
)))
355 p_realloc(&handler
, shape_event
+ 1);
356 handler
[shape_event
] = handle_event_shape
;
359 /* check for randr extension */
360 if((awesomeconf
[0].have_randr
= XRRQueryExtension(dpy
, &randr_event_base
, &e_dummy
)))
362 p_realloc(&handler
, randr_event_base
+ RRScreenChangeNotify
+ 1);
363 handler
[randr_event_base
+ RRScreenChangeNotify
] = handle_event_randr_screen_change_notify
;
366 for(screen
= 0; screen
< get_screen_count(dpy
); screen
++)
368 awesomeconf
[screen
].have_shape
= awesomeconf
[0].have_shape
;
369 awesomeconf
[screen
].have_randr
= awesomeconf
[0].have_randr
;
376 /* construct fifo path */
377 homedir
= getenv("HOME");
378 fifopath_len
= a_strlen(homedir
) + a_strlen(CONTROL_FIFO_PATH
) + 2;
379 fifopath
= p_new(char, fifopath_len
);
380 a_strcpy(fifopath
, fifopath_len
, homedir
);
381 a_strcat(fifopath
, fifopath_len
, "/");
382 a_strcat(fifopath
, fifopath_len
, CONTROL_FIFO_PATH
);
384 if(lstat(fifopath
, &fifost
) == -1)
385 if(mkfifo(fifopath
, 0600) == -1)
386 perror("error creating control fifo");
388 cfd
= open(fifopath
, O_RDONLY
| O_NDELAY
);
390 /* main event loop, also reads status text from stdin */
397 if(select(MAX(xfd
, cfd
) + 1, &rd
, NULL
, NULL
, NULL
) == -1)
401 eprint("select failed\n");
403 if(cfd
>= 0 && FD_ISSET(cfd
, &rd
))
405 switch (r
= read(cfd
, buf
, sizeof(buf
)))
408 perror("awesome: error reading fifo");
409 a_strncpy(awesomeconf
[0].statustext
, sizeof(awesomeconf
[0].statustext
),
410 strerror(errno
), sizeof(awesomeconf
[0].statustext
) - 1);
411 awesomeconf
[0].statustext
[sizeof(awesomeconf
[0].statustext
) - 1] = '\0';
416 cfd
= open(fifopath
, O_RDONLY
| O_NDELAY
);
419 parse_control(buf
, awesomeconf
);
421 drawstatusbar(dpy
, &awesomeconf
[0]);
426 XNextEvent(dpy
, &ev
);
428 handler
[ev
.type
](&ev
, awesomeconf
); /* call handler */
434 cleanup(awesomeconf
);
439 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99