JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
made fullscreen apps working fine in floating mode (there is no sane way to make...
[dwm.git] / main.c
1 /*
2  * (C)opyright MMVI Anselm R. Garbe <garbeam at gmail dot com>
3  * See LICENSE file for license details.
4  */
5
6 #include "dwm.h"
7 #include <errno.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <unistd.h>
12 #include <sys/select.h>
13 #include <X11/cursorfont.h>
14 #include <X11/Xatom.h>
15 #include <X11/Xproto.h>
16
17 /* static */
18
19 static int (*xerrorxlib)(Display *, XErrorEvent *);
20 static Bool otherwm;
21
22 static void
23 cleanup()
24 {
25         while(sel) {
26                 resize(sel, True, TopLeft);
27                 unmanage(sel);
28         }
29         XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime);
30 }
31
32 static void
33 scan()
34 {
35         unsigned int i, num;
36         Window *wins, d1, d2;
37         XWindowAttributes wa;
38
39         if(XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
40                 for(i = 0; i < num; i++) {
41                         if(!XGetWindowAttributes(dpy, wins[i], &wa))
42                                 continue;
43                         if(wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1))
44                                 continue;
45                         if(wa.map_state == IsViewable)
46                                 manage(wins[i], &wa);
47                 }
48         }
49         if(wins)
50                 XFree(wins);
51 }
52
53 static int
54 win_property(Window w, Atom a, Atom t, long l, unsigned char **prop)
55 {
56         int status, format;
57         unsigned long res, extra;
58         Atom real;
59
60         status = XGetWindowProperty(dpy, w, a, 0L, l, False, t, &real, &format,
61                         &res, &extra, prop);
62
63         if(status != Success || *prop == 0) {
64                 return 0;
65         }
66         if(res == 0) {
67                 free((void *) *prop);
68         }
69         return res;
70 }
71
72 /*
73  * Startup Error handler to check if another window manager
74  * is already running.
75  */
76 static int
77 xerrorstart(Display *dsply, XErrorEvent *ee)
78 {
79         otherwm = True;
80         return -1;
81 }
82
83 /* extern */
84
85 char stext[1024];
86 int tsel = DEFTAG;
87 int screen, sx, sy, sw, sh, bx, by, bw, bh, mw;
88 Atom wmatom[WMLast], netatom[NetLast];
89 Bool running = True;
90 Bool issel = True;
91 Client *clients = NULL;
92 Client *sel = NULL;
93 Cursor cursor[CurLast];
94 Display *dpy;
95 DC dc = {0};
96 Window root, barwin;
97
98 int
99 getproto(Window w)
100 {
101         int protos = 0;
102         int i;
103         long res;
104         unsigned char *protocols;
105
106         res = win_property(w, wmatom[WMProtocols], XA_ATOM, 20L, &protocols);
107         if(res <= 0) {
108                 return protos;
109         }
110         for(i = 0; i < res; i++) {
111                 if(protocols[i] == wmatom[WMDelete])
112                         protos |= PROTODELWIN;
113         }
114         free((char *) protocols);
115         return protos;
116 }
117
118 void
119 sendevent(Window w, Atom a, long value)
120 {
121         XEvent e;
122
123         e.type = ClientMessage;
124         e.xclient.window = w;
125         e.xclient.message_type = a;
126         e.xclient.format = 32;
127         e.xclient.data.l[0] = value;
128         e.xclient.data.l[1] = CurrentTime;
129         XSendEvent(dpy, w, False, NoEventMask, &e);
130         XSync(dpy, False);
131 }
132
133 void
134 quit(Arg *arg)
135 {
136         running = False;
137 }
138
139 /*
140  * There's no way to check accesses to destroyed windows, thus those cases are
141  * ignored (especially on UnmapNotify's).  Other types of errors call Xlibs
142  * default error handler, which calls exit().
143  */
144 int
145 xerror(Display *dpy, XErrorEvent *ee)
146 {
147         if(ee->error_code == BadWindow
148         || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
149         || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
150         || (ee->request_code == X_PolyFillRectangle && ee->error_code == BadDrawable)
151         || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
152         || (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch)
153         || (ee->request_code == X_GrabKey && ee->error_code == BadAccess))
154                 return 0;
155         fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
156                 ee->request_code, ee->error_code);
157         return xerrorxlib(dpy, ee); /* may call exit() */
158 }
159
160 int
161 main(int argc, char *argv[])
162 {
163         int i;
164         unsigned int mask;
165         fd_set rd;
166         Bool readin = True;
167         Window w;
168         XEvent ev;
169         XSetWindowAttributes wa;
170
171         if(argc == 2 && !strncmp("-v", argv[1], 3)) {
172                 fputs("dwm-"VERSION", (C)opyright MMVI Anselm R. Garbe\n", stdout);
173                 exit(EXIT_SUCCESS);
174         }
175         else if(argc != 1)
176                 eprint("usage: dwm [-v]\n");
177
178         dpy = XOpenDisplay(0);
179         if(!dpy)
180                 eprint("dwm: cannot connect X server\n");
181
182         screen = DefaultScreen(dpy);
183         root = RootWindow(dpy, screen);
184
185         /* check if another WM is already running */
186         otherwm = False;
187         XSetErrorHandler(xerrorstart);
188         /* this causes an error if some other WM is running */
189         XSelectInput(dpy, root, SubstructureRedirectMask);
190         XSync(dpy, False);
191
192         if(otherwm)
193                 eprint("dwm: another window manager is already running\n");
194
195         XSetErrorHandler(NULL);
196         xerrorxlib = XSetErrorHandler(xerror);
197
198         /* init atoms */
199         wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
200         wmatom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
201         netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
202         netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
203         XChangeProperty(dpy, root, netatom[NetSupported], XA_ATOM, 32,
204                         PropModeReplace, (unsigned char *) netatom, NetLast);
205
206         /* init cursors */
207         cursor[CurNormal] = XCreateFontCursor(dpy, XC_left_ptr);
208         cursor[CurResize] = XCreateFontCursor(dpy, XC_sizing);
209         cursor[CurMove] = XCreateFontCursor(dpy, XC_fleur);
210
211         grabkeys();
212
213         /* style */
214         dc.bg = getcolor(BGCOLOR);
215         dc.fg = getcolor(FGCOLOR);
216         dc.border = getcolor(BORDERCOLOR);
217         setfont(FONT);
218
219         sx = sy = 0;
220         sw = DisplayWidth(dpy, screen);
221         sh = DisplayHeight(dpy, screen);
222         mw = (sw * MASTERW) / 100;
223
224         wa.override_redirect = 1;
225         wa.background_pixmap = ParentRelative;
226         wa.event_mask = ButtonPressMask | ExposureMask;
227
228         bx = by = 0;
229         bw = sw;
230         dc.h = bh = dc.font.height + 4;
231         barwin = XCreateWindow(dpy, root, bx, by, bw, bh, 0, DefaultDepth(dpy, screen),
232                         CopyFromParent, DefaultVisual(dpy, screen),
233                         CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
234         XDefineCursor(dpy, barwin, cursor[CurNormal]);
235         XMapRaised(dpy, barwin);
236
237         dc.drawable = XCreatePixmap(dpy, root, sw, bh, DefaultDepth(dpy, screen));
238         dc.gc = XCreateGC(dpy, root, 0, 0);
239         drawstatus();
240
241         issel = XQueryPointer(dpy, root, &w, &w, &i, &i, &i, &i, &mask);
242
243         wa.event_mask = SubstructureRedirectMask | EnterWindowMask | LeaveWindowMask;
244         wa.cursor = cursor[CurNormal];
245         XChangeWindowAttributes(dpy, root, CWEventMask | CWCursor, &wa);
246
247         strcpy(stext, "dwm-"VERSION);
248         scan();
249
250         /* main event loop, reads status text from stdin as well */
251         while(running) {
252                 FD_ZERO(&rd);
253                 if(readin)
254                         FD_SET(STDIN_FILENO, &rd);
255                 FD_SET(ConnectionNumber(dpy), &rd);
256
257                 i = select(ConnectionNumber(dpy) + 1, &rd, 0, 0, 0);
258                 if(i == -1 && errno == EINTR)
259                         continue;
260                 if(i < 0)
261                         eprint("select failed\n");
262                 else if(i > 0) {
263                         if(FD_ISSET(ConnectionNumber(dpy), &rd)) {
264                                 while(XPending(dpy)) {
265                                         XNextEvent(dpy, &ev);
266                                         if(handler[ev.type])
267                                                 (handler[ev.type])(&ev); /* call handler */
268                                 }
269                         }
270                         if(readin && FD_ISSET(STDIN_FILENO, &rd)) {
271                                 readin = NULL != fgets(stext, sizeof(stext), stdin);
272                                 if(readin)
273                                         stext[strlen(stext) - 1] = 0;
274                                 else 
275                                         strcpy(stext, "broken pipe");
276                                 drawstatus();
277                         }
278                 }
279         }
280
281         cleanup();
282         XCloseDisplay(dpy);
283
284         return 0;
285 }