dwm-meillo

annotate main.c @ 50:148f25ed0ad7

several other additions/fixes, dwm is quite usable already
author Anselm R. Garbe <garbeam@wmii.de>
date Thu, 13 Jul 2006 18:21:38 +0200
parents 466591c2f967
children 035617ee18d1
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 char stext[1024];
garbeam@31 37 int tsel = Tdev; /* default tag */
garbeam@32 38 int screen, sx, sy, sw, sh, th;
garbeam@3 39
garbeam@34 40 DC dc = {0};
garbeam@50 41 Client *clients = NULL;
garbeam@50 42 Client *sel = NULL;
garbeam@0 43
garbeam@0 44 static Bool other_wm_running;
garbeam@31 45 static const char version[] =
garbeam@34 46 "dwm - " VERSION ", (C)opyright MMVI Anselm R. Garbe\n";
garbeam@0 47 static int (*x_error_handler) (Display *, XErrorEvent *);
garbeam@0 48
garbeam@0 49 static void
garbeam@34 50 usage() { error("usage: dwm [-v]\n"); }
garbeam@0 51
garbeam@0 52 static void
garbeam@0 53 scan_wins()
garbeam@0 54 {
garbeam@0 55 unsigned int i, num;
garbeam@0 56 Window *wins;
garbeam@0 57 XWindowAttributes wa;
garbeam@0 58 Window d1, d2;
garbeam@0 59
garbeam@0 60 if(XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
garbeam@0 61 for(i = 0; i < num; i++) {
garbeam@0 62 if(!XGetWindowAttributes(dpy, wins[i], &wa))
garbeam@0 63 continue;
garbeam@0 64 if(wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1))
garbeam@0 65 continue;
garbeam@0 66 if(wa.map_state == IsViewable)
garbeam@10 67 manage(wins[i], &wa);
garbeam@0 68 }
garbeam@0 69 }
garbeam@0 70 if(wins)
garbeam@0 71 XFree(wins);
garbeam@0 72 }
garbeam@0 73
garbeam@13 74 static int
garbeam@13 75 win_property(Window w, Atom a, Atom t, long l, unsigned char **prop)
garbeam@13 76 {
garbeam@13 77 Atom real;
garbeam@13 78 int format;
garbeam@13 79 unsigned long res, extra;
garbeam@13 80 int status;
garbeam@13 81
garbeam@13 82 status = XGetWindowProperty(dpy, w, a, 0L, l, False, t, &real, &format,
garbeam@13 83 &res, &extra, prop);
garbeam@13 84
garbeam@13 85 if(status != Success || *prop == 0) {
garbeam@13 86 return 0;
garbeam@13 87 }
garbeam@13 88 if(res == 0) {
garbeam@13 89 free((void *) *prop);
garbeam@13 90 }
garbeam@13 91 return res;
garbeam@13 92 }
garbeam@13 93
garbeam@13 94 int
garbeam@13 95 win_proto(Window w)
garbeam@13 96 {
garbeam@30 97 unsigned char *protocols;
garbeam@13 98 long res;
garbeam@13 99 int protos = 0;
garbeam@13 100 int i;
garbeam@13 101
garbeam@30 102 res = win_property(w, wm_atom[WMProtocols], XA_ATOM, 20L, &protocols);
garbeam@13 103 if(res <= 0) {
garbeam@13 104 return protos;
garbeam@13 105 }
garbeam@13 106 for(i = 0; i < res; i++) {
garbeam@13 107 if(protocols[i] == wm_atom[WMDelete])
garbeam@13 108 protos |= WM_PROTOCOL_DELWIN;
garbeam@13 109 }
garbeam@13 110 free((char *) protocols);
garbeam@13 111 return protos;
garbeam@13 112 }
garbeam@13 113
garbeam@13 114 void
garbeam@13 115 send_message(Window w, Atom a, long value)
garbeam@13 116 {
garbeam@13 117 XEvent e;
garbeam@13 118
garbeam@13 119 e.type = ClientMessage;
garbeam@13 120 e.xclient.window = w;
garbeam@13 121 e.xclient.message_type = a;
garbeam@13 122 e.xclient.format = 32;
garbeam@13 123 e.xclient.data.l[0] = value;
garbeam@13 124 e.xclient.data.l[1] = CurrentTime;
garbeam@13 125 XSendEvent(dpy, w, False, NoEventMask, &e);
garbeam@13 126 XFlush(dpy);
garbeam@13 127 }
garbeam@13 128
garbeam@0 129 /*
garbeam@0 130 * There's no way to check accesses to destroyed windows, thus
garbeam@0 131 * those cases are ignored (especially on UnmapNotify's).
garbeam@0 132 * Other types of errors call Xlib's default error handler, which
garbeam@0 133 * calls exit().
garbeam@0 134 */
garbeam@10 135 int
garbeam@0 136 error_handler(Display *dpy, XErrorEvent *error)
garbeam@0 137 {
garbeam@0 138 if(error->error_code == BadWindow
garbeam@0 139 || (error->request_code == X_SetInputFocus
garbeam@0 140 && error->error_code == BadMatch)
garbeam@0 141 || (error->request_code == X_PolyText8
garbeam@0 142 && error->error_code == BadDrawable)
garbeam@0 143 || (error->request_code == X_PolyFillRectangle
garbeam@0 144 && error->error_code == BadDrawable)
garbeam@0 145 || (error->request_code == X_PolySegment
garbeam@0 146 && error->error_code == BadDrawable)
garbeam@0 147 || (error->request_code == X_ConfigureWindow
garbeam@0 148 && error->error_code == BadMatch)
garbeam@0 149 || (error->request_code == X_GrabKey
garbeam@0 150 && error->error_code == BadAccess))
garbeam@0 151 return 0;
garbeam@34 152 fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
garbeam@0 153 error->request_code, error->error_code);
garbeam@0 154 return x_error_handler(dpy, error); /* may call exit() */
garbeam@0 155 }
garbeam@0 156
garbeam@0 157 /*
garbeam@0 158 * Startup Error handler to check if another window manager
garbeam@0 159 * is already running.
garbeam@0 160 */
garbeam@0 161 static int
garbeam@0 162 startup_error_handler(Display *dpy, XErrorEvent *error)
garbeam@0 163 {
garbeam@0 164 other_wm_running = True;
garbeam@0 165 return -1;
garbeam@0 166 }
garbeam@0 167
garbeam@0 168 static void
garbeam@0 169 cleanup()
garbeam@0 170 {
garbeam@50 171 while(sel) {
garbeam@50 172 resize(sel);
garbeam@50 173 unmanage(sel);
garbeam@50 174 }
garbeam@0 175 XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime);
garbeam@0 176 }
garbeam@0 177
garbeam@27 178 void
garbeam@49 179 quit(Arg *arg)
garbeam@27 180 {
garbeam@27 181 running = False;
garbeam@27 182 }
garbeam@27 183
garbeam@0 184 int
garbeam@0 185 main(int argc, char *argv[])
garbeam@0 186 {
garbeam@0 187 int i;
garbeam@0 188 XSetWindowAttributes wa;
garbeam@0 189 unsigned int mask;
garbeam@0 190 Window w;
garbeam@5 191 XEvent ev;
garbeam@0 192
garbeam@0 193 /* command line args */
garbeam@0 194 for(i = 1; (i < argc) && (argv[i][0] == '-'); i++) {
garbeam@0 195 switch (argv[i][1]) {
garbeam@0 196 case 'v':
garbeam@0 197 fprintf(stdout, "%s", version);
garbeam@0 198 exit(0);
garbeam@0 199 break;
garbeam@0 200 default:
garbeam@0 201 usage();
garbeam@0 202 break;
garbeam@0 203 }
garbeam@0 204 }
garbeam@0 205
garbeam@0 206 dpy = XOpenDisplay(0);
garbeam@0 207 if(!dpy)
garbeam@34 208 error("dwm: cannot connect X server\n");
garbeam@0 209
garbeam@0 210 screen = DefaultScreen(dpy);
garbeam@0 211 root = RootWindow(dpy, screen);
garbeam@0 212
garbeam@0 213 /* check if another WM is already running */
garbeam@0 214 other_wm_running = False;
garbeam@0 215 XSetErrorHandler(startup_error_handler);
garbeam@0 216 /* this causes an error if some other WM is running */
garbeam@0 217 XSelectInput(dpy, root, SubstructureRedirectMask);
garbeam@3 218 XFlush(dpy);
garbeam@0 219
garbeam@0 220 if(other_wm_running)
garbeam@34 221 error("dwm: another window manager is already running\n");
garbeam@0 222
garbeam@26 223 sx = sy = 0;
garbeam@26 224 sw = DisplayWidth(dpy, screen);
garbeam@26 225 sh = DisplayHeight(dpy, screen);
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 }