dwm-meillo

annotate main.c @ 52:d18f6dd0cf23

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