diff options
| author | Anselm R. Garbe <arg@10kloc.org> | 2006-08-23 10:21:57 +0200 |
|---|---|---|
| committer | Anselm R. Garbe <arg@10kloc.org> | 2006-08-23 10:21:57 +0200 |
| commit | b5159dfd2f552696b049b95e0d368a2c33a1002a (patch) | |
| tree | 111ed79d6fb06ad84b1930a27f555915fba29ed6 /main.c | |
| parent | updated man page (diff) | |
| download | dwm-b5159dfd2f552696b049b95e0d368a2c33a1002a.tar.gz dwm-b5159dfd2f552696b049b95e0d368a2c33a1002a.zip | |
separated setup stuff into main.c:setup() - this makes main() more readable
Diffstat (limited to 'main.c')
| -rw-r--r-- | main.c | 181 |
1 files changed, 95 insertions, 86 deletions
| @@ -15,6 +15,22 @@ | |||
| 15 | #include <X11/Xatom.h> | 15 | #include <X11/Xatom.h> |
| 16 | #include <X11/Xproto.h> | 16 | #include <X11/Xproto.h> |
| 17 | 17 | ||
| 18 | /* extern */ | ||
| 19 | |||
| 20 | char stext[1024]; | ||
| 21 | Bool *seltag; | ||
| 22 | int screen, sx, sy, sw, sh, bx, by, bw, bh, mw; | ||
| 23 | unsigned int ntags, numlockmask; | ||
| 24 | Atom wmatom[WMLast], netatom[NetLast]; | ||
| 25 | Bool running = True; | ||
| 26 | Bool issel = True; | ||
| 27 | Client *clients = NULL; | ||
| 28 | Client *sel = NULL; | ||
| 29 | Cursor cursor[CurLast]; | ||
| 30 | Display *dpy; | ||
| 31 | DC dc = {0}; | ||
| 32 | Window root, barwin; | ||
| 33 | |||
| 18 | /* static */ | 34 | /* static */ |
| 19 | 35 | ||
| 20 | static int (*xerrorxlib)(Display *, XErrorEvent *); | 36 | static int (*xerrorxlib)(Display *, XErrorEvent *); |
| @@ -62,6 +78,79 @@ scan() | |||
| 62 | XFree(wins); | 78 | XFree(wins); |
| 63 | } | 79 | } |
| 64 | 80 | ||
| 81 | static void | ||
| 82 | setup() | ||
| 83 | { | ||
| 84 | int i, j; | ||
| 85 | unsigned int mask; | ||
| 86 | Window w; | ||
| 87 | XModifierKeymap *modmap; | ||
| 88 | XSetWindowAttributes wa; | ||
| 89 | |||
| 90 | /* init atoms */ | ||
| 91 | wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False); | ||
| 92 | wmatom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False); | ||
| 93 | netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False); | ||
| 94 | netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False); | ||
| 95 | XChangeProperty(dpy, root, netatom[NetSupported], XA_ATOM, 32, | ||
| 96 | PropModeReplace, (unsigned char *) netatom, NetLast); | ||
| 97 | |||
| 98 | /* init cursors */ | ||
| 99 | cursor[CurNormal] = XCreateFontCursor(dpy, XC_left_ptr); | ||
| 100 | cursor[CurResize] = XCreateFontCursor(dpy, XC_sizing); | ||
| 101 | cursor[CurMove] = XCreateFontCursor(dpy, XC_fleur); | ||
| 102 | |||
| 103 | modmap = XGetModifierMapping(dpy); | ||
| 104 | for (i = 0; i < 8; i++) { | ||
| 105 | for (j = 0; j < modmap->max_keypermod; j++) { | ||
| 106 | if(modmap->modifiermap[i * modmap->max_keypermod + j] == XKeysymToKeycode(dpy, XK_Num_Lock)) | ||
| 107 | numlockmask = (1 << i); | ||
| 108 | } | ||
| 109 | } | ||
| 110 | XFree(modmap); | ||
| 111 | |||
| 112 | wa.event_mask = SubstructureRedirectMask | SubstructureNotifyMask | EnterWindowMask | LeaveWindowMask; | ||
| 113 | wa.cursor = cursor[CurNormal]; | ||
| 114 | XChangeWindowAttributes(dpy, root, CWEventMask | CWCursor, &wa); | ||
| 115 | |||
| 116 | grabkeys(); | ||
| 117 | initrregs(); | ||
| 118 | |||
| 119 | for(ntags = 0; tags[ntags]; ntags++); | ||
| 120 | seltag = emallocz(sizeof(Bool) * ntags); | ||
| 121 | seltag[0] = True; | ||
| 122 | |||
| 123 | /* style */ | ||
| 124 | dc.bg = getcolor(BGCOLOR); | ||
| 125 | dc.fg = getcolor(FGCOLOR); | ||
| 126 | dc.border = getcolor(BORDERCOLOR); | ||
| 127 | setfont(FONT); | ||
| 128 | |||
| 129 | sx = sy = 0; | ||
| 130 | sw = DisplayWidth(dpy, screen); | ||
| 131 | sh = DisplayHeight(dpy, screen); | ||
| 132 | mw = (sw * MASTERW) / 100; | ||
| 133 | |||
| 134 | bx = by = 0; | ||
| 135 | bw = sw; | ||
| 136 | dc.h = bh = dc.font.height + 4; | ||
| 137 | wa.override_redirect = 1; | ||
| 138 | wa.background_pixmap = ParentRelative; | ||
| 139 | wa.event_mask = ButtonPressMask | ExposureMask; | ||
| 140 | barwin = XCreateWindow(dpy, root, bx, by, bw, bh, 0, DefaultDepth(dpy, screen), | ||
| 141 | CopyFromParent, DefaultVisual(dpy, screen), | ||
| 142 | CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa); | ||
| 143 | XDefineCursor(dpy, barwin, cursor[CurNormal]); | ||
| 144 | XMapRaised(dpy, barwin); | ||
| 145 | |||
| 146 | dc.drawable = XCreatePixmap(dpy, root, sw, bh, DefaultDepth(dpy, screen)); | ||
| 147 | dc.gc = XCreateGC(dpy, root, 0, 0); | ||
| 148 | |||
| 149 | issel = XQueryPointer(dpy, root, &w, &w, &i, &i, &i, &i, &mask); | ||
| 150 | |||
| 151 | strcpy(stext, "dwm-"VERSION); | ||
| 152 | } | ||
| 153 | |||
| 65 | /* | 154 | /* |
| 66 | * Startup Error handler to check if another window manager | 155 | * Startup Error handler to check if another window manager |
| 67 | * is already running. | 156 | * is already running. |
| @@ -75,20 +164,6 @@ xerrorstart(Display *dsply, XErrorEvent *ee) | |||
| 75 | 164 | ||
| 76 | /* extern */ | 165 | /* extern */ |
| 77 | 166 | ||
| 78 | char stext[1024]; | ||
| 79 | Bool *seltag; | ||
| 80 | int screen, sx, sy, sw, sh, bx, by, bw, bh, mw; | ||
| 81 | unsigned int ntags, numlockmask; | ||
| 82 | Atom wmatom[WMLast], netatom[NetLast]; | ||
| 83 | Bool running = True; | ||
| 84 | Bool issel = True; | ||
| 85 | Client *clients = NULL; | ||
| 86 | Client *sel = NULL; | ||
| 87 | Cursor cursor[CurLast]; | ||
| 88 | Display *dpy; | ||
| 89 | DC dc = {0}; | ||
| 90 | Window root, barwin; | ||
| 91 | |||
| 92 | int | 167 | int |
| 93 | getproto(Window w) | 168 | getproto(Window w) |
| 94 | { | 169 | { |
| @@ -153,12 +228,8 @@ xerror(Display *dpy, XErrorEvent *ee) | |||
| 153 | int | 228 | int |
| 154 | main(int argc, char *argv[]) | 229 | main(int argc, char *argv[]) |
| 155 | { | 230 | { |
| 156 | int i, j, xfd; | 231 | int r, xfd; |
| 157 | unsigned int mask; | ||
| 158 | fd_set rd; | 232 | fd_set rd; |
| 159 | Window w; | ||
| 160 | XModifierKeymap *modmap; | ||
| 161 | XSetWindowAttributes wa; | ||
| 162 | 233 | ||
| 163 | if(argc == 2 && !strncmp("-v", argv[1], 3)) { | 234 | if(argc == 2 && !strncmp("-v", argv[1], 3)) { |
| 164 | fputs("dwm-"VERSION", (C)opyright MMVI Anselm R. Garbe\n", stdout); | 235 | fputs("dwm-"VERSION", (C)opyright MMVI Anselm R. Garbe\n", stdout); |
| @@ -189,70 +260,8 @@ main(int argc, char *argv[]) | |||
| 189 | xerrorxlib = XSetErrorHandler(xerror); | 260 | xerrorxlib = XSetErrorHandler(xerror); |
| 190 | XSync(dpy, False); | 261 | XSync(dpy, False); |
| 191 | 262 | ||
| 192 | /* init atoms */ | 263 | setup(); |
| 193 | wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False); | ||
| 194 | wmatom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False); | ||
| 195 | netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False); | ||
| 196 | netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False); | ||
| 197 | XChangeProperty(dpy, root, netatom[NetSupported], XA_ATOM, 32, | ||
| 198 | PropModeReplace, (unsigned char *) netatom, NetLast); | ||
| 199 | |||
| 200 | /* init cursors */ | ||
| 201 | cursor[CurNormal] = XCreateFontCursor(dpy, XC_left_ptr); | ||
| 202 | cursor[CurResize] = XCreateFontCursor(dpy, XC_sizing); | ||
| 203 | cursor[CurMove] = XCreateFontCursor(dpy, XC_fleur); | ||
| 204 | |||
| 205 | modmap = XGetModifierMapping(dpy); | ||
| 206 | for (i = 0; i < 8; i++) { | ||
| 207 | for (j = 0; j < modmap->max_keypermod; j++) { | ||
| 208 | if(modmap->modifiermap[i * modmap->max_keypermod + j] == XKeysymToKeycode(dpy, XK_Num_Lock)) | ||
| 209 | numlockmask = (1 << i); | ||
| 210 | } | ||
| 211 | } | ||
| 212 | XFree(modmap); | ||
| 213 | |||
| 214 | wa.event_mask = SubstructureRedirectMask | SubstructureNotifyMask | EnterWindowMask | LeaveWindowMask; | ||
| 215 | wa.cursor = cursor[CurNormal]; | ||
| 216 | XChangeWindowAttributes(dpy, root, CWEventMask | CWCursor, &wa); | ||
| 217 | |||
| 218 | grabkeys(); | ||
| 219 | initrregs(); | ||
| 220 | |||
| 221 | for(ntags = 0; tags[ntags]; ntags++); | ||
| 222 | seltag = emallocz(sizeof(Bool) * ntags); | ||
| 223 | seltag[0] = True; | ||
| 224 | |||
| 225 | /* style */ | ||
| 226 | dc.bg = getcolor(BGCOLOR); | ||
| 227 | dc.fg = getcolor(FGCOLOR); | ||
| 228 | dc.border = getcolor(BORDERCOLOR); | ||
| 229 | setfont(FONT); | ||
| 230 | |||
| 231 | sx = sy = 0; | ||
| 232 | sw = DisplayWidth(dpy, screen); | ||
| 233 | sh = DisplayHeight(dpy, screen); | ||
| 234 | mw = (sw * MASTERW) / 100; | ||
| 235 | |||
| 236 | bx = by = 0; | ||
| 237 | bw = sw; | ||
| 238 | dc.h = bh = dc.font.height + 4; | ||
| 239 | wa.override_redirect = 1; | ||
| 240 | wa.background_pixmap = ParentRelative; | ||
| 241 | wa.event_mask = ButtonPressMask | ExposureMask; | ||
| 242 | barwin = XCreateWindow(dpy, root, bx, by, bw, bh, 0, DefaultDepth(dpy, screen), | ||
| 243 | CopyFromParent, DefaultVisual(dpy, screen), | ||
| 244 | CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa); | ||
| 245 | XDefineCursor(dpy, barwin, cursor[CurNormal]); | ||
| 246 | XMapRaised(dpy, barwin); | ||
| 247 | |||
| 248 | dc.drawable = XCreatePixmap(dpy, root, sw, bh, DefaultDepth(dpy, screen)); | ||
| 249 | dc.gc = XCreateGC(dpy, root, 0, 0); | ||
| 250 | |||
| 251 | strcpy(stext, "dwm-"VERSION); | ||
| 252 | drawstatus(); | 264 | drawstatus(); |
| 253 | |||
| 254 | issel = XQueryPointer(dpy, root, &w, &w, &i, &i, &i, &i, &mask); | ||
| 255 | |||
| 256 | scan(); | 265 | scan(); |
| 257 | 266 | ||
| 258 | /* main event loop, also reads status text from stdin */ | 267 | /* main event loop, also reads status text from stdin */ |
| @@ -264,10 +273,10 @@ main(int argc, char *argv[]) | |||
| 264 | if(readin) | 273 | if(readin) |
| 265 | FD_SET(STDIN_FILENO, &rd); | 274 | FD_SET(STDIN_FILENO, &rd); |
| 266 | FD_SET(xfd, &rd); | 275 | FD_SET(xfd, &rd); |
| 267 | i = select(xfd + 1, &rd, NULL, NULL, NULL); | 276 | r = select(xfd + 1, &rd, NULL, NULL, NULL); |
| 268 | if((i == -1) && (errno == EINTR)) | 277 | if((r == -1) && (errno == EINTR)) |
| 269 | continue; | 278 | continue; |
| 270 | if(i > 0) { | 279 | if(r > 0) { |
| 271 | if(readin && FD_ISSET(STDIN_FILENO, &rd)) { | 280 | if(readin && FD_ISSET(STDIN_FILENO, &rd)) { |
| 272 | readin = NULL != fgets(stext, sizeof(stext), stdin); | 281 | readin = NULL != fgets(stext, sizeof(stext), stdin); |
| 273 | if(readin) | 282 | if(readin) |
| @@ -277,7 +286,7 @@ main(int argc, char *argv[]) | |||
| 277 | drawstatus(); | 286 | drawstatus(); |
| 278 | } | 287 | } |
| 279 | } | 288 | } |
| 280 | else if(i < 0) | 289 | else if(r < 0) |
| 281 | eprint("select failed\n"); | 290 | eprint("select failed\n"); |
| 282 | procevent(); | 291 | procevent(); |
| 283 | } | 292 | } |
