dwm-meillo

changeset 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 c5bea7c0231b
children 5cfa63564a0f
files dwm.h main.c view.c
diffstat 3 files changed, 104 insertions(+), 99 deletions(-) [+]
line diff
     1.1 --- a/dwm.h	Tue Aug 22 19:56:29 2006 +0200
     1.2 +++ b/dwm.h	Wed Aug 23 10:21:57 2006 +0200
     1.3 @@ -11,15 +11,10 @@
     1.4  #define MOUSEMASK		(BUTTONMASK | PointerMotionMask)
     1.5  #define PROTODELWIN		1
     1.6  
     1.7 -typedef union Arg Arg;
     1.8 -typedef struct Client Client;
     1.9 -typedef struct DC DC;
    1.10 -typedef struct Fnt Fnt;
    1.11 -
    1.12 -union Arg {
    1.13 +typedef union {
    1.14  	const char *cmd;
    1.15  	int i;
    1.16 -};
    1.17 +} Arg;
    1.18  
    1.19  /* atoms */
    1.20  enum { NetSupported, NetWMName, NetLast };
    1.21 @@ -28,18 +23,18 @@
    1.22  /* cursor */
    1.23  enum { CurNormal, CurResize, CurMove, CurLast };
    1.24  
    1.25 -/* windowcorners */
    1.26 +/* window corners */
    1.27  typedef enum { TopLeft, TopRight, BotLeft, BotRight } Corner;
    1.28  
    1.29 -struct Fnt {
    1.30 +typedef struct {
    1.31  	int ascent;
    1.32  	int descent;
    1.33  	int height;
    1.34  	XFontSet set;
    1.35  	XFontStruct *xfont;
    1.36 -};
    1.37 +} Fnt;
    1.38  
    1.39 -struct DC { /* draw context */
    1.40 +typedef struct { /* draw context */
    1.41  	int x, y, w, h;
    1.42  	unsigned long bg;
    1.43  	unsigned long fg;
    1.44 @@ -47,8 +42,9 @@
    1.45  	Drawable drawable;
    1.46  	Fnt font;
    1.47  	GC gc;
    1.48 -};
    1.49 +} DC;
    1.50  
    1.51 +typedef struct Client Client;
    1.52  struct Client {
    1.53  	char name[256];
    1.54  	int proto;
     2.1 --- a/main.c	Tue Aug 22 19:56:29 2006 +0200
     2.2 +++ b/main.c	Wed Aug 23 10:21:57 2006 +0200
     2.3 @@ -15,6 +15,22 @@
     2.4  #include <X11/Xatom.h>
     2.5  #include <X11/Xproto.h>
     2.6  
     2.7 +/* extern */
     2.8 +
     2.9 +char stext[1024];
    2.10 +Bool *seltag;
    2.11 +int screen, sx, sy, sw, sh, bx, by, bw, bh, mw;
    2.12 +unsigned int ntags, numlockmask;
    2.13 +Atom wmatom[WMLast], netatom[NetLast];
    2.14 +Bool running = True;
    2.15 +Bool issel = True;
    2.16 +Client *clients = NULL;
    2.17 +Client *sel = NULL;
    2.18 +Cursor cursor[CurLast];
    2.19 +Display *dpy;
    2.20 +DC dc = {0};
    2.21 +Window root, barwin;
    2.22 +
    2.23  /* static */
    2.24  
    2.25  static int (*xerrorxlib)(Display *, XErrorEvent *);
    2.26 @@ -62,6 +78,79 @@
    2.27  		XFree(wins);
    2.28  }
    2.29  
    2.30 +static void
    2.31 +setup()
    2.32 +{
    2.33 +	int i, j;
    2.34 +	unsigned int mask;
    2.35 +	Window w;
    2.36 +	XModifierKeymap *modmap;
    2.37 +	XSetWindowAttributes wa;
    2.38 +
    2.39 +	/* init atoms */
    2.40 +	wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
    2.41 +	wmatom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
    2.42 +	netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
    2.43 +	netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
    2.44 +	XChangeProperty(dpy, root, netatom[NetSupported], XA_ATOM, 32,
    2.45 +			PropModeReplace, (unsigned char *) netatom, NetLast);
    2.46 +
    2.47 +	/* init cursors */
    2.48 +	cursor[CurNormal] = XCreateFontCursor(dpy, XC_left_ptr);
    2.49 +	cursor[CurResize] = XCreateFontCursor(dpy, XC_sizing);
    2.50 +	cursor[CurMove] = XCreateFontCursor(dpy, XC_fleur);
    2.51 +
    2.52 +	modmap = XGetModifierMapping(dpy);
    2.53 +	for (i = 0; i < 8; i++) {
    2.54 +		for (j = 0; j < modmap->max_keypermod; j++) {
    2.55 +			if(modmap->modifiermap[i * modmap->max_keypermod + j] == XKeysymToKeycode(dpy, XK_Num_Lock))
    2.56 +				numlockmask = (1 << i);
    2.57 +		}
    2.58 +	}
    2.59 +	XFree(modmap);
    2.60 +
    2.61 +	wa.event_mask = SubstructureRedirectMask | SubstructureNotifyMask | EnterWindowMask | LeaveWindowMask;
    2.62 +	wa.cursor = cursor[CurNormal];
    2.63 +	XChangeWindowAttributes(dpy, root, CWEventMask | CWCursor, &wa);
    2.64 +
    2.65 +	grabkeys();
    2.66 +	initrregs();
    2.67 +
    2.68 +	for(ntags = 0; tags[ntags]; ntags++);
    2.69 +	seltag = emallocz(sizeof(Bool) * ntags);
    2.70 +	seltag[0] = True;
    2.71 +
    2.72 +	/* style */
    2.73 +	dc.bg = getcolor(BGCOLOR);
    2.74 +	dc.fg = getcolor(FGCOLOR);
    2.75 +	dc.border = getcolor(BORDERCOLOR);
    2.76 +	setfont(FONT);
    2.77 +
    2.78 +	sx = sy = 0;
    2.79 +	sw = DisplayWidth(dpy, screen);
    2.80 +	sh = DisplayHeight(dpy, screen);
    2.81 +	mw = (sw * MASTERW) / 100;
    2.82 +
    2.83 +	bx = by = 0;
    2.84 +	bw = sw;
    2.85 +	dc.h = bh = dc.font.height + 4;
    2.86 +	wa.override_redirect = 1;
    2.87 +	wa.background_pixmap = ParentRelative;
    2.88 +	wa.event_mask = ButtonPressMask | ExposureMask;
    2.89 +	barwin = XCreateWindow(dpy, root, bx, by, bw, bh, 0, DefaultDepth(dpy, screen),
    2.90 +			CopyFromParent, DefaultVisual(dpy, screen),
    2.91 +			CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
    2.92 +	XDefineCursor(dpy, barwin, cursor[CurNormal]);
    2.93 +	XMapRaised(dpy, barwin);
    2.94 +
    2.95 +	dc.drawable = XCreatePixmap(dpy, root, sw, bh, DefaultDepth(dpy, screen));
    2.96 +	dc.gc = XCreateGC(dpy, root, 0, 0);
    2.97 +
    2.98 +	issel = XQueryPointer(dpy, root, &w, &w, &i, &i, &i, &i, &mask);
    2.99 +
   2.100 +	strcpy(stext, "dwm-"VERSION);
   2.101 +}
   2.102 +
   2.103  /*
   2.104   * Startup Error handler to check if another window manager
   2.105   * is already running.
   2.106 @@ -75,20 +164,6 @@
   2.107  
   2.108  /* extern */
   2.109  
   2.110 -char stext[1024];
   2.111 -Bool *seltag;
   2.112 -int screen, sx, sy, sw, sh, bx, by, bw, bh, mw;
   2.113 -unsigned int ntags, numlockmask;
   2.114 -Atom wmatom[WMLast], netatom[NetLast];
   2.115 -Bool running = True;
   2.116 -Bool issel = True;
   2.117 -Client *clients = NULL;
   2.118 -Client *sel = NULL;
   2.119 -Cursor cursor[CurLast];
   2.120 -Display *dpy;
   2.121 -DC dc = {0};
   2.122 -Window root, barwin;
   2.123 -
   2.124  int
   2.125  getproto(Window w)
   2.126  {
   2.127 @@ -153,12 +228,8 @@
   2.128  int
   2.129  main(int argc, char *argv[])
   2.130  {
   2.131 -	int i, j, xfd;
   2.132 -	unsigned int mask;
   2.133 +	int r, xfd;
   2.134  	fd_set rd;
   2.135 -	Window w;
   2.136 -	XModifierKeymap *modmap;
   2.137 -	XSetWindowAttributes wa;
   2.138  
   2.139  	if(argc == 2 && !strncmp("-v", argv[1], 3)) {
   2.140  		fputs("dwm-"VERSION", (C)opyright MMVI Anselm R. Garbe\n", stdout);
   2.141 @@ -189,70 +260,8 @@
   2.142  	xerrorxlib = XSetErrorHandler(xerror);
   2.143  	XSync(dpy, False);
   2.144  
   2.145 -	/* init atoms */
   2.146 -	wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
   2.147 -	wmatom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
   2.148 -	netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
   2.149 -	netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
   2.150 -	XChangeProperty(dpy, root, netatom[NetSupported], XA_ATOM, 32,
   2.151 -			PropModeReplace, (unsigned char *) netatom, NetLast);
   2.152 -
   2.153 -	/* init cursors */
   2.154 -	cursor[CurNormal] = XCreateFontCursor(dpy, XC_left_ptr);
   2.155 -	cursor[CurResize] = XCreateFontCursor(dpy, XC_sizing);
   2.156 -	cursor[CurMove] = XCreateFontCursor(dpy, XC_fleur);
   2.157 -
   2.158 -	modmap = XGetModifierMapping(dpy);
   2.159 -	for (i = 0; i < 8; i++) {
   2.160 -		for (j = 0; j < modmap->max_keypermod; j++) {
   2.161 -			if(modmap->modifiermap[i * modmap->max_keypermod + j] == XKeysymToKeycode(dpy, XK_Num_Lock))
   2.162 -				numlockmask = (1 << i);
   2.163 -		}
   2.164 -	}
   2.165 -	XFree(modmap);
   2.166 -
   2.167 -	wa.event_mask = SubstructureRedirectMask | SubstructureNotifyMask | EnterWindowMask | LeaveWindowMask;
   2.168 -	wa.cursor = cursor[CurNormal];
   2.169 -	XChangeWindowAttributes(dpy, root, CWEventMask | CWCursor, &wa);
   2.170 -
   2.171 -	grabkeys();
   2.172 -	initrregs();
   2.173 -
   2.174 -	for(ntags = 0; tags[ntags]; ntags++);
   2.175 -	seltag = emallocz(sizeof(Bool) * ntags);
   2.176 -	seltag[0] = True;
   2.177 -
   2.178 -	/* style */
   2.179 -	dc.bg = getcolor(BGCOLOR);
   2.180 -	dc.fg = getcolor(FGCOLOR);
   2.181 -	dc.border = getcolor(BORDERCOLOR);
   2.182 -	setfont(FONT);
   2.183 -
   2.184 -	sx = sy = 0;
   2.185 -	sw = DisplayWidth(dpy, screen);
   2.186 -	sh = DisplayHeight(dpy, screen);
   2.187 -	mw = (sw * MASTERW) / 100;
   2.188 -
   2.189 -	bx = by = 0;
   2.190 -	bw = sw;
   2.191 -	dc.h = bh = dc.font.height + 4;
   2.192 -	wa.override_redirect = 1;
   2.193 -	wa.background_pixmap = ParentRelative;
   2.194 -	wa.event_mask = ButtonPressMask | ExposureMask;
   2.195 -	barwin = XCreateWindow(dpy, root, bx, by, bw, bh, 0, DefaultDepth(dpy, screen),
   2.196 -			CopyFromParent, DefaultVisual(dpy, screen),
   2.197 -			CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
   2.198 -	XDefineCursor(dpy, barwin, cursor[CurNormal]);
   2.199 -	XMapRaised(dpy, barwin);
   2.200 -
   2.201 -	dc.drawable = XCreatePixmap(dpy, root, sw, bh, DefaultDepth(dpy, screen));
   2.202 -	dc.gc = XCreateGC(dpy, root, 0, 0);
   2.203 -
   2.204 -	strcpy(stext, "dwm-"VERSION);
   2.205 +	setup();
   2.206  	drawstatus();
   2.207 -
   2.208 -	issel = XQueryPointer(dpy, root, &w, &w, &i, &i, &i, &i, &mask);
   2.209 -
   2.210  	scan();
   2.211  
   2.212  	/* main event loop, also reads status text from stdin */
   2.213 @@ -264,10 +273,10 @@
   2.214  		if(readin)
   2.215  			FD_SET(STDIN_FILENO, &rd);
   2.216  		FD_SET(xfd, &rd);
   2.217 -		i = select(xfd + 1, &rd, NULL, NULL, NULL);
   2.218 -		if((i == -1) && (errno == EINTR))
   2.219 +		r = select(xfd + 1, &rd, NULL, NULL, NULL);
   2.220 +		if((r == -1) && (errno == EINTR))
   2.221  			continue;
   2.222 -		if(i > 0) {
   2.223 +		if(r > 0) {
   2.224  			if(readin && FD_ISSET(STDIN_FILENO, &rd)) {
   2.225  				readin = NULL != fgets(stext, sizeof(stext), stdin);
   2.226  				if(readin)
   2.227 @@ -277,7 +286,7 @@
   2.228  				drawstatus();
   2.229  			}
   2.230  		}
   2.231 -		else if(i < 0)
   2.232 +		else if(r < 0)
   2.233  			eprint("select failed\n");
   2.234  		procevent();
   2.235  	}
     3.1 --- a/view.c	Tue Aug 22 19:56:29 2006 +0200
     3.2 +++ b/view.c	Wed Aug 23 10:21:57 2006 +0200
     3.3 @@ -195,7 +195,7 @@
     3.4  void
     3.5  togglemode(Arg *arg)
     3.6  {
     3.7 -	arrange = arrange == dofloat ? dotile : dofloat;
     3.8 +	arrange = (arrange == dofloat) ? dotile : dofloat;
     3.9  	if(sel)
    3.10  		arrange(NULL);
    3.11  	else