2 * awesome.c - awesome main functions
4 * Copyright © 2007-2008 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.
29 #include <sys/select.h>
30 #include <sys/types.h>
32 #include <sys/socket.h>
36 #include <X11/cursorfont.h>
37 #include <X11/keysym.h>
38 #include <X11/Xatom.h>
39 #include <X11/Xproto.h>
40 #include <X11/Xutil.h>
41 #include <X11/extensions/shape.h>
42 #include <X11/extensions/Xrandr.h>
49 #include "statusbar.h"
55 #include "common/awclient.h"
56 #include "common/util.h"
57 #include "common/awesome-version.h"
58 #include "common/configopts.h"
60 static int (*xerrorxlib
) (Display
*, XErrorEvent
*);
61 static Bool running
= True
;
63 AwesomeConf globalconf
;
65 /** Scan X to find windows to manage
71 int screen
, real_screen
;
72 Window
*wins
= NULL
, d1
, d2
;
75 for(screen
= 0; screen
< ScreenCount(globalconf
.display
); screen
++)
77 if(XQueryTree(globalconf
.display
, RootWindow(globalconf
.display
, screen
), &d1
, &d2
, &wins
, &num
))
80 for(i
= 0; i
< num
; i
++)
82 /* XGetWindowAttributes return 1 on success */
83 if(!XGetWindowAttributes(globalconf
.display
, wins
[i
], &wa
)
84 || wa
.override_redirect
85 || XGetTransientForHint(globalconf
.display
, wins
[i
], &d1
))
87 if(wa
.map_state
== IsViewable
|| window_getstate(wins
[i
]) == IconicState
)
90 real_screen
= screen_get_bycoord(wa
.x
, wa
.y
);
91 client_manage(wins
[i
], &wa
, real_screen
);
94 /* now the transients */
95 for(i
= 0; i
< num
; i
++)
97 if(!XGetWindowAttributes(globalconf
.display
, wins
[i
], &wa
))
99 if(XGetTransientForHint(globalconf
.display
, wins
[i
], &d1
)
100 && (wa
.map_state
== IsViewable
|| window_getstate(wins
[i
]) == IconicState
))
103 real_screen
= screen_get_bycoord(wa
.x
, wa
.y
);
104 client_manage(wins
[i
], &wa
, real_screen
);
113 /** Setup everything before running
114 * \param screen Screen number
115 * \todo clean things...
120 XSetWindowAttributes wa
;
121 Statusbar
*statusbar
;
122 int phys_screen
= get_phys_screen(screen
);
125 globalconf
.cursor
[CurNormal
] = XCreateFontCursor(globalconf
.display
, XC_left_ptr
);
126 globalconf
.cursor
[CurResize
] = XCreateFontCursor(globalconf
.display
, XC_sizing
);
127 globalconf
.cursor
[CurMove
] = XCreateFontCursor(globalconf
.display
, XC_fleur
);
129 /* select for events */
130 wa
.event_mask
= SubstructureRedirectMask
| SubstructureNotifyMask
131 | EnterWindowMask
| LeaveWindowMask
| StructureNotifyMask
;
132 wa
.cursor
= globalconf
.cursor
[CurNormal
];
134 XChangeWindowAttributes(globalconf
.display
,
135 RootWindow(globalconf
.display
, phys_screen
),
136 CWEventMask
| CWCursor
, &wa
);
138 XSelectInput(globalconf
.display
,
139 RootWindow(globalconf
.display
, phys_screen
),
142 grabkeys(phys_screen
);
144 for(statusbar
= globalconf
.screens
[screen
].statusbar
; statusbar
; statusbar
= statusbar
->next
)
145 statusbar_init(statusbar
);
148 /** Startup Error handler to check if another window manager
149 * is already running.
150 * \param disp Display
151 * \param ee Error event
153 static int __attribute__ ((noreturn
))
154 xerrorstart(Display
* disp
__attribute__ ((unused
)),
155 XErrorEvent
* ee
__attribute__ ((unused
)))
157 eprint("another window manager is already running\n");
161 * \param screen Screen ID
163 * \ingroup ui_callback
166 uicb_quit(int screen
__attribute__ ((unused
)), char *arg
__attribute__ ((unused
)))
172 exit_on_signal(int sig
__attribute__ ((unused
)))
177 /* There's no way to check accesses to destroyed windows, thus those cases are
178 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
179 * default error handler, which may call exit.
182 xerror(Display
*edpy
, XErrorEvent
*ee
)
184 if(ee
->error_code
== BadWindow
185 || (ee
->error_code
== BadMatch
&& ee
->request_code
== X_SetInputFocus
))
187 warn("fatal error: request code=%d, error code=%d\n", ee
->request_code
, ee
->error_code
);
188 return xerrorxlib(edpy
, ee
); /* may call exit */
191 /** Print help and exit(2) with given exit_code.
193 static void __attribute__ ((noreturn
))
194 exit_help(int exit_code
)
196 FILE *outfile
= (exit_code
== EXIT_SUCCESS
) ? stdout
: stderr
;
197 fprintf(outfile
, "Usage: awesome [ -v | -h | -c configfile | -k ]\n");
201 /** Hello, this is main
202 * \param argc who knows
203 * \param argv who knows
204 * \return EXIT_SUCCESS I hope
206 typedef void event_handler (XEvent
*);
208 main(int argc
, char *argv
[])
211 const char *confpath
= NULL
;
212 int r
, xfd
, e_dummy
, csfd
;
216 int shape_event
, randr_event_base
;
219 event_handler
**handler
;
220 struct sockaddr_un
*addr
;
222 static struct option long_options
[] = {
223 {"help", 0, NULL
, 'h'},
224 {"version", 0, NULL
, 'v'},
229 while((opt
= getopt_long(argc
, argv
, "vhkc:",
230 long_options
, NULL
)) != -1)
234 eprint_version("awesome");
237 exit_help(EXIT_SUCCESS
);
243 eprint("-c option requires a file name\n");
246 return config_check(confpath
);
250 /* Text won't be printed correctly otherwise */
251 setlocale(LC_CTYPE
, "");
254 if(!(dpy
= XOpenDisplay(NULL
)))
255 eprint("cannot open display\n");
257 for(cmdlen
= 0, i
= 0; i
< argc
; i
++)
258 cmdlen
+= a_strlen(argv
[i
] + 1);
259 globalconf
.argv
= p_new(char, cmdlen
);
260 a_strcpy(globalconf
.argv
, cmdlen
, argv
[0]);
261 for(i
= 1; i
< argc
; i
++)
263 a_strcat(globalconf
.argv
, cmdlen
, " ");
264 a_strcat(globalconf
.argv
, cmdlen
, argv
[i
]);
267 xfd
= ConnectionNumber(dpy
);
269 XSetErrorHandler(xerrorstart
);
270 for(screen
= 0; screen
< ScreenCount(dpy
); screen
++)
271 /* this causes an error if some other window manager is running */
272 XSelectInput(dpy
, RootWindow(dpy
, screen
), SubstructureRedirectMask
);
274 /* need to XSync to validate errorhandler */
276 XSetErrorHandler(NULL
);
277 xerrorxlib
= XSetErrorHandler(xerror
);
281 globalconf
.display
= dpy
;
283 /* init EWMH atoms */
286 /* init screens struct */
287 screen_build_screens();
288 focus_add_client(NULL
);
291 config_parse(confpath
);
295 /* for each virtual screen */
296 for(screen
= 0; screen
< globalconf
.nscreen
; screen
++)
299 /* do this only for real screen */
300 for(screen
= 0; screen
< ScreenCount(dpy
); screen
++)
302 loadawesomeprops(screen
);
303 ewmh_set_supported_hints(screen
);
304 /* call this to at least grab root window clicks */
305 window_root_grabbuttons(screen
);
308 handler
= p_new(event_handler
*, LASTEvent
);
309 handler
[ButtonPress
] = handle_event_buttonpress
;
310 handler
[ConfigureRequest
] = handle_event_configurerequest
;
311 handler
[ConfigureNotify
] = handle_event_configurenotify
;
312 handler
[DestroyNotify
] = handle_event_destroynotify
;
313 handler
[EnterNotify
] = handle_event_enternotify
;
314 handler
[LeaveNotify
] = handle_event_leavenotify
;
315 handler
[Expose
] = handle_event_expose
;
316 handler
[KeyPress
] = handle_event_keypress
;
317 handler
[MappingNotify
] = handle_event_mappingnotify
;
318 handler
[MapRequest
] = handle_event_maprequest
;
319 handler
[PropertyNotify
] = handle_event_propertynotify
;
320 handler
[UnmapNotify
] = handle_event_unmapnotify
;
321 handler
[ClientMessage
] = handle_event_clientmessage
;
323 /* check for shape extension */
324 if((globalconf
.have_shape
= XShapeQueryExtension(dpy
, &shape_event
, &e_dummy
)))
326 p_realloc(&handler
, shape_event
+ 1);
327 handler
[shape_event
] = handle_event_shape
;
330 /* check for randr extension */
331 if((globalconf
.have_randr
= XRRQueryExtension(dpy
, &randr_event_base
, &e_dummy
)))
333 p_realloc(&handler
, randr_event_base
+ RRScreenChangeNotify
+ 1);
334 handler
[randr_event_base
+ RRScreenChangeNotify
] = handle_event_randr_screen_change_notify
;
340 csfd
= get_client_socket();
341 addr
= get_client_addr(getenv("DISPLAY"));
343 if(bind(csfd
, (const struct sockaddr
*) addr
, SUN_LEN(addr
)))
345 if(errno
== EADDRINUSE
)
347 if(unlink(addr
->sun_path
))
348 perror("error unlinking existing file");
349 if(bind(csfd
, (const struct sockaddr
*) addr
, SUN_LEN(addr
)))
350 perror("error binding UNIX domain socket");
353 perror("error binding UNIX domain socket");
356 /* register function for signals */
357 signal(SIGINT
, &exit_on_signal
);
358 signal(SIGTERM
, &exit_on_signal
);
359 signal(SIGHUP
, &exit_on_signal
);
361 /* refresh everything before waiting events */
365 /* main event loop, also reads status text from socket */
372 if(select(MAX(xfd
, csfd
) + 1, &rd
, NULL
, NULL
, NULL
) == -1)
376 eprint("select failed\n");
378 if(csfd
>= 0 && FD_ISSET(csfd
, &rd
))
379 switch (r
= recv(csfd
, buf
, sizeof(buf
)-1, MSG_TRUNC
))
382 perror("awesome: error reading UNIX domain socket");
388 if(r
>= ssizeof(buf
))
395 /* two level XPending:
396 * we need to first check we have XEvent to handle
397 * and if so, we handle them all in a round.
398 * Then when we have refresh()'ed stuff so maybe new XEvent
399 * are available and select() won't tell us, so let's check
400 * with XPending() again.
406 XNextEvent(dpy
, &ev
);
408 handler
[ev
.type
](&ev
);
410 /* drop events requested to */
411 if(globalconf
.drop_events
)
415 while(XCheckMaskEvent(dpy
, globalconf
.drop_events
, &ev
));
416 globalconf
.drop_events
= NoEventMask
;
431 if(csfd
> 0 && close(csfd
))
432 perror("error closing UNIX domain socket");
433 if(unlink(addr
->sun_path
))
434 perror("error unlinking UNIX domain socket");
441 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80