aewl
diff main.c @ 333:827f8f6c9e97
separated setup stuff into main.c:setup() - this makes main() more readable
author | Anselm R. Garbe <arg@10kloc.org> |
---|---|
date | Wed, 23 Aug 2006 10:21:57 +0200 |
parents | cea0c98495bc |
children | 5c874c619287 |
line diff
1.1 --- a/main.c Tue Aug 22 19:56:29 2006 +0200 1.2 +++ b/main.c Wed Aug 23 10:21:57 2006 +0200 1.3 @@ -15,6 +15,22 @@ 1.4 #include <X11/Xatom.h> 1.5 #include <X11/Xproto.h> 1.6 1.7 +/* extern */ 1.8 + 1.9 +char stext[1024]; 1.10 +Bool *seltag; 1.11 +int screen, sx, sy, sw, sh, bx, by, bw, bh, mw; 1.12 +unsigned int ntags, numlockmask; 1.13 +Atom wmatom[WMLast], netatom[NetLast]; 1.14 +Bool running = True; 1.15 +Bool issel = True; 1.16 +Client *clients = NULL; 1.17 +Client *sel = NULL; 1.18 +Cursor cursor[CurLast]; 1.19 +Display *dpy; 1.20 +DC dc = {0}; 1.21 +Window root, barwin; 1.22 + 1.23 /* static */ 1.24 1.25 static int (*xerrorxlib)(Display *, XErrorEvent *); 1.26 @@ -62,6 +78,79 @@ 1.27 XFree(wins); 1.28 } 1.29 1.30 +static void 1.31 +setup() 1.32 +{ 1.33 + int i, j; 1.34 + unsigned int mask; 1.35 + Window w; 1.36 + XModifierKeymap *modmap; 1.37 + XSetWindowAttributes wa; 1.38 + 1.39 + /* init atoms */ 1.40 + wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False); 1.41 + wmatom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False); 1.42 + netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False); 1.43 + netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False); 1.44 + XChangeProperty(dpy, root, netatom[NetSupported], XA_ATOM, 32, 1.45 + PropModeReplace, (unsigned char *) netatom, NetLast); 1.46 + 1.47 + /* init cursors */ 1.48 + cursor[CurNormal] = XCreateFontCursor(dpy, XC_left_ptr); 1.49 + cursor[CurResize] = XCreateFontCursor(dpy, XC_sizing); 1.50 + cursor[CurMove] = XCreateFontCursor(dpy, XC_fleur); 1.51 + 1.52 + modmap = XGetModifierMapping(dpy); 1.53 + for (i = 0; i < 8; i++) { 1.54 + for (j = 0; j < modmap->max_keypermod; j++) { 1.55 + if(modmap->modifiermap[i * modmap->max_keypermod + j] == XKeysymToKeycode(dpy, XK_Num_Lock)) 1.56 + numlockmask = (1 << i); 1.57 + } 1.58 + } 1.59 + XFree(modmap); 1.60 + 1.61 + wa.event_mask = SubstructureRedirectMask | SubstructureNotifyMask | EnterWindowMask | LeaveWindowMask; 1.62 + wa.cursor = cursor[CurNormal]; 1.63 + XChangeWindowAttributes(dpy, root, CWEventMask | CWCursor, &wa); 1.64 + 1.65 + grabkeys(); 1.66 + initrregs(); 1.67 + 1.68 + for(ntags = 0; tags[ntags]; ntags++); 1.69 + seltag = emallocz(sizeof(Bool) * ntags); 1.70 + seltag[0] = True; 1.71 + 1.72 + /* style */ 1.73 + dc.bg = getcolor(BGCOLOR); 1.74 + dc.fg = getcolor(FGCOLOR); 1.75 + dc.border = getcolor(BORDERCOLOR); 1.76 + setfont(FONT); 1.77 + 1.78 + sx = sy = 0; 1.79 + sw = DisplayWidth(dpy, screen); 1.80 + sh = DisplayHeight(dpy, screen); 1.81 + mw = (sw * MASTERW) / 100; 1.82 + 1.83 + bx = by = 0; 1.84 + bw = sw; 1.85 + dc.h = bh = dc.font.height + 4; 1.86 + wa.override_redirect = 1; 1.87 + wa.background_pixmap = ParentRelative; 1.88 + wa.event_mask = ButtonPressMask | ExposureMask; 1.89 + barwin = XCreateWindow(dpy, root, bx, by, bw, bh, 0, DefaultDepth(dpy, screen), 1.90 + CopyFromParent, DefaultVisual(dpy, screen), 1.91 + CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa); 1.92 + XDefineCursor(dpy, barwin, cursor[CurNormal]); 1.93 + XMapRaised(dpy, barwin); 1.94 + 1.95 + dc.drawable = XCreatePixmap(dpy, root, sw, bh, DefaultDepth(dpy, screen)); 1.96 + dc.gc = XCreateGC(dpy, root, 0, 0); 1.97 + 1.98 + issel = XQueryPointer(dpy, root, &w, &w, &i, &i, &i, &i, &mask); 1.99 + 1.100 + strcpy(stext, "dwm-"VERSION); 1.101 +} 1.102 + 1.103 /* 1.104 * Startup Error handler to check if another window manager 1.105 * is already running. 1.106 @@ -75,20 +164,6 @@ 1.107 1.108 /* extern */ 1.109 1.110 -char stext[1024]; 1.111 -Bool *seltag; 1.112 -int screen, sx, sy, sw, sh, bx, by, bw, bh, mw; 1.113 -unsigned int ntags, numlockmask; 1.114 -Atom wmatom[WMLast], netatom[NetLast]; 1.115 -Bool running = True; 1.116 -Bool issel = True; 1.117 -Client *clients = NULL; 1.118 -Client *sel = NULL; 1.119 -Cursor cursor[CurLast]; 1.120 -Display *dpy; 1.121 -DC dc = {0}; 1.122 -Window root, barwin; 1.123 - 1.124 int 1.125 getproto(Window w) 1.126 { 1.127 @@ -153,12 +228,8 @@ 1.128 int 1.129 main(int argc, char *argv[]) 1.130 { 1.131 - int i, j, xfd; 1.132 - unsigned int mask; 1.133 + int r, xfd; 1.134 fd_set rd; 1.135 - Window w; 1.136 - XModifierKeymap *modmap; 1.137 - XSetWindowAttributes wa; 1.138 1.139 if(argc == 2 && !strncmp("-v", argv[1], 3)) { 1.140 fputs("dwm-"VERSION", (C)opyright MMVI Anselm R. Garbe\n", stdout); 1.141 @@ -189,70 +260,8 @@ 1.142 xerrorxlib = XSetErrorHandler(xerror); 1.143 XSync(dpy, False); 1.144 1.145 - /* init atoms */ 1.146 - wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False); 1.147 - wmatom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False); 1.148 - netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False); 1.149 - netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False); 1.150 - XChangeProperty(dpy, root, netatom[NetSupported], XA_ATOM, 32, 1.151 - PropModeReplace, (unsigned char *) netatom, NetLast); 1.152 - 1.153 - /* init cursors */ 1.154 - cursor[CurNormal] = XCreateFontCursor(dpy, XC_left_ptr); 1.155 - cursor[CurResize] = XCreateFontCursor(dpy, XC_sizing); 1.156 - cursor[CurMove] = XCreateFontCursor(dpy, XC_fleur); 1.157 - 1.158 - modmap = XGetModifierMapping(dpy); 1.159 - for (i = 0; i < 8; i++) { 1.160 - for (j = 0; j < modmap->max_keypermod; j++) { 1.161 - if(modmap->modifiermap[i * modmap->max_keypermod + j] == XKeysymToKeycode(dpy, XK_Num_Lock)) 1.162 - numlockmask = (1 << i); 1.163 - } 1.164 - } 1.165 - XFree(modmap); 1.166 - 1.167 - wa.event_mask = SubstructureRedirectMask | SubstructureNotifyMask | EnterWindowMask | LeaveWindowMask; 1.168 - wa.cursor = cursor[CurNormal]; 1.169 - XChangeWindowAttributes(dpy, root, CWEventMask | CWCursor, &wa); 1.170 - 1.171 - grabkeys(); 1.172 - initrregs(); 1.173 - 1.174 - for(ntags = 0; tags[ntags]; ntags++); 1.175 - seltag = emallocz(sizeof(Bool) * ntags); 1.176 - seltag[0] = True; 1.177 - 1.178 - /* style */ 1.179 - dc.bg = getcolor(BGCOLOR); 1.180 - dc.fg = getcolor(FGCOLOR); 1.181 - dc.border = getcolor(BORDERCOLOR); 1.182 - setfont(FONT); 1.183 - 1.184 - sx = sy = 0; 1.185 - sw = DisplayWidth(dpy, screen); 1.186 - sh = DisplayHeight(dpy, screen); 1.187 - mw = (sw * MASTERW) / 100; 1.188 - 1.189 - bx = by = 0; 1.190 - bw = sw; 1.191 - dc.h = bh = dc.font.height + 4; 1.192 - wa.override_redirect = 1; 1.193 - wa.background_pixmap = ParentRelative; 1.194 - wa.event_mask = ButtonPressMask | ExposureMask; 1.195 - barwin = XCreateWindow(dpy, root, bx, by, bw, bh, 0, DefaultDepth(dpy, screen), 1.196 - CopyFromParent, DefaultVisual(dpy, screen), 1.197 - CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa); 1.198 - XDefineCursor(dpy, barwin, cursor[CurNormal]); 1.199 - XMapRaised(dpy, barwin); 1.200 - 1.201 - dc.drawable = XCreatePixmap(dpy, root, sw, bh, DefaultDepth(dpy, screen)); 1.202 - dc.gc = XCreateGC(dpy, root, 0, 0); 1.203 - 1.204 - strcpy(stext, "dwm-"VERSION); 1.205 + setup(); 1.206 drawstatus(); 1.207 - 1.208 - issel = XQueryPointer(dpy, root, &w, &w, &i, &i, &i, &i, &mask); 1.209 - 1.210 scan(); 1.211 1.212 /* main event loop, also reads status text from stdin */ 1.213 @@ -264,10 +273,10 @@ 1.214 if(readin) 1.215 FD_SET(STDIN_FILENO, &rd); 1.216 FD_SET(xfd, &rd); 1.217 - i = select(xfd + 1, &rd, NULL, NULL, NULL); 1.218 - if((i == -1) && (errno == EINTR)) 1.219 + r = select(xfd + 1, &rd, NULL, NULL, NULL); 1.220 + if((r == -1) && (errno == EINTR)) 1.221 continue; 1.222 - if(i > 0) { 1.223 + if(r > 0) { 1.224 if(readin && FD_ISSET(STDIN_FILENO, &rd)) { 1.225 readin = NULL != fgets(stext, sizeof(stext), stdin); 1.226 if(readin) 1.227 @@ -277,7 +286,7 @@ 1.228 drawstatus(); 1.229 } 1.230 } 1.231 - else if(i < 0) 1.232 + else if(r < 0) 1.233 eprint("select failed\n"); 1.234 procevent(); 1.235 }