JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
implemented tagging a client
[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 <stdarg.h>
7 #include <stdio.h>
8 #include <stdlib.h>
9
10 #include <X11/cursorfont.h>
11 #include <X11/Xatom.h>
12 #include <X11/Xproto.h>
13
14 #include "dwm.h"
15
16 /********** CUSTOMIZE **********/
17
18 char *tags[TLast] = {
19         [Tscratch] = "scratch",
20         [Tdev] = "dev",
21         [Tirc] = "irc",
22         [Twww] = "www",
23         [Twork] = "work",
24 };
25
26 /********** CUSTOMIZE **********/
27
28 /* X structs */
29 Display *dpy;
30 Window root, barwin;
31 Atom wm_atom[WMLast], net_atom[NetLast];
32 Cursor cursor[CurLast];
33 Bool running = True;
34 Bool issel;
35
36 char stext[1024];
37 int tsel = Tdev; /* default tag */
38 int screen, sx, sy, sw, sh, th;
39
40 DC dc = {0};
41 Client *cstart = NULL;
42 Client *cend = NULL;
43 Client *csel = NULL;
44
45 static Bool other_wm_running;
46 static const char version[] =
47         "dwm - " VERSION ", (C)opyright MMVI Anselm R. Garbe\n";
48 static int (*x_error_handler) (Display *, XErrorEvent *);
49
50 static void
51 usage() {       error("usage: dwm [-v]\n"); }
52
53 static void
54 scan_wins()
55 {
56         unsigned int i, num;
57         Window *wins;
58         XWindowAttributes wa;
59         Window d1, d2;
60
61         if(XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
62                 for(i = 0; i < num; i++) {
63                         if(!XGetWindowAttributes(dpy, wins[i], &wa))
64                                 continue;
65                         if(wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1))
66                                 continue;
67                         if(wa.map_state == IsViewable)
68                                 manage(wins[i], &wa);
69                 }
70         }
71         if(wins)
72                 XFree(wins);
73 }
74
75 static int
76 win_property(Window w, Atom a, Atom t, long l, unsigned char **prop)
77 {
78         Atom real;
79         int format;
80         unsigned long res, extra;
81         int status;
82
83         status = XGetWindowProperty(dpy, w, a, 0L, l, False, t, &real, &format,
84                         &res, &extra, prop);
85
86         if(status != Success || *prop == 0) {
87                 return 0;
88         }
89         if(res == 0) {
90                 free((void *) *prop);
91         }
92         return res;
93 }
94
95 int
96 win_proto(Window w)
97 {
98         unsigned char *protocols;
99         long res;
100         int protos = 0;
101         int i;
102
103         res = win_property(w, wm_atom[WMProtocols], XA_ATOM, 20L, &protocols);
104         if(res <= 0) {
105                 return protos;
106         }
107         for(i = 0; i < res; i++) {
108                 if(protocols[i] == wm_atom[WMDelete])
109                         protos |= WM_PROTOCOL_DELWIN;
110         }
111         free((char *) protocols);
112         return protos;
113 }
114
115 void
116 send_message(Window w, Atom a, long value)
117 {
118         XEvent e;
119
120         e.type = ClientMessage;
121         e.xclient.window = w;
122         e.xclient.message_type = a;
123         e.xclient.format = 32;
124         e.xclient.data.l[0] = value;
125         e.xclient.data.l[1] = CurrentTime;
126         XSendEvent(dpy, w, False, NoEventMask, &e);
127         XFlush(dpy);
128 }
129
130 /*
131  * There's no way to check accesses to destroyed windows, thus
132  * those cases are ignored (especially on UnmapNotify's).
133  * Other types of errors call Xlib's default error handler, which
134  * calls exit().
135  */
136 int
137 error_handler(Display *dpy, XErrorEvent *error)
138 {
139         if(error->error_code == BadWindow
140                         || (error->request_code == X_SetInputFocus
141                                 && error->error_code == BadMatch)
142                         || (error->request_code == X_PolyText8
143                                 && error->error_code == BadDrawable)
144                         || (error->request_code == X_PolyFillRectangle
145                                 && error->error_code == BadDrawable)
146                         || (error->request_code == X_PolySegment
147                                 && error->error_code == BadDrawable)
148                         || (error->request_code == X_ConfigureWindow
149                                 && error->error_code == BadMatch)
150                         || (error->request_code == X_GrabKey
151                                 && error->error_code == BadAccess))
152                 return 0;
153         fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
154                         error->request_code, error->error_code);
155         return x_error_handler(dpy, error); /* may call exit() */
156 }
157
158 /*
159  * Startup Error handler to check if another window manager
160  * is already running.
161  */
162 static int
163 startup_error_handler(Display *dpy, XErrorEvent *error)
164 {
165         other_wm_running = True;
166         return -1;
167 }
168
169 static void
170 cleanup()
171 {
172         while(csel)
173                 unmanage(csel);
174         XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime);
175 }
176
177 void
178 quit(Arg *arg)
179 {
180         running = False;
181 }
182
183 int
184 main(int argc, char *argv[])
185 {
186         int i;
187         XSetWindowAttributes wa;
188         unsigned int mask;
189         Window w;
190         XEvent ev;
191
192         /* command line args */
193         for(i = 1; (i < argc) && (argv[i][0] == '-'); i++) {
194                 switch (argv[i][1]) {
195                 case 'v':
196                         fprintf(stdout, "%s", version);
197                         exit(0);
198                         break;
199                 default:
200                         usage();
201                         break;
202                 }
203         }
204
205         dpy = XOpenDisplay(0);
206         if(!dpy)
207                 error("dwm: cannot connect X server\n");
208
209         screen = DefaultScreen(dpy);
210         root = RootWindow(dpy, screen);
211
212         /* check if another WM is already running */
213         other_wm_running = False;
214         XSetErrorHandler(startup_error_handler);
215         /* this causes an error if some other WM is running */
216         XSelectInput(dpy, root, SubstructureRedirectMask);
217         XFlush(dpy);
218
219         if(other_wm_running)
220                 error("dwm: another window manager is already running\n");
221
222         sx = sy = 0;
223         sw = DisplayWidth(dpy, screen);
224         sh = DisplayHeight(dpy, screen);
225         issel = XQueryPointer(dpy, root, &w, &w, &i, &i, &i, &i, &mask);
226
227         XSetErrorHandler(0);
228         x_error_handler = XSetErrorHandler(error_handler);
229
230         /* init atoms */
231         wm_atom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
232         wm_atom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
233         net_atom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
234         net_atom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
235
236         XChangeProperty(dpy, root, net_atom[NetSupported], XA_ATOM, 32,
237                         PropModeReplace, (unsigned char *) net_atom, NetLast);
238
239
240         /* init cursors */
241         cursor[CurNormal] = XCreateFontCursor(dpy, XC_left_ptr);
242         cursor[CurResize] = XCreateFontCursor(dpy, XC_sizing);
243         cursor[CurMove] = XCreateFontCursor(dpy, XC_fleur);
244
245         update_keys();
246
247         /* style */
248         dc.bg = initcolor(BGCOLOR);
249         dc.fg = initcolor(FGCOLOR);
250         dc.border = initcolor(BORDERCOLOR);
251         initfont(FONT);
252
253         th = dc.font.height + 4;
254
255         dc.drawable = XCreatePixmap(dpy, root, sw, th, DefaultDepth(dpy, screen));
256         dc.gc = XCreateGC(dpy, root, 0, 0);
257
258         wa.event_mask = SubstructureRedirectMask | EnterWindowMask \
259                                         | LeaveWindowMask;
260         wa.cursor = cursor[CurNormal];
261         XChangeWindowAttributes(dpy, root, CWEventMask | CWCursor, &wa);
262
263         scan_wins();
264
265         while(running) {
266                 XNextEvent(dpy, &ev);
267                 if(handler[ev.type])
268                         (handler[ev.type])(&ev); /* call handler */
269         }
270
271         cleanup();
272         XCloseDisplay(dpy);
273
274         return 0;
275 }