[dev] [st PATCH] Honor X geometry specifications

From: <suckless_AT_dev97.com>
Date: Fri, 25 Apr 2014 21:52:16 +0300

From: Yuri Karaban <suckless_AT_dev97.com>

- Interpret width and height as rows and columns (common behavior
  for X applications with resize increment hints set)

- Obey the offset parameters
---
 st.c | 98 ++++++++++++++++++++++++++------------------------------------------
 1 file changed, 38 insertions(+), 60 deletions(-)
diff --git a/st.c b/st.c
index 60243a7..f5c9a69 100644
--- a/st.c
+++ b/st.c
_AT_@ -249,8 +249,7 @@ typedef struct {
 	Visual *vis;
 	XSetWindowAttributes attrs;
 	int scr;
-	bool isfixed; /* is fixed geometry? */
-	int fx, fy, fw, fh; /* fixed geometry */
+	int l, t, gm; /* geometry left, top, bitmask */
 	int tw, th; /* tty width and height */
 	int w, h; /* window width and height */
 	int ch; /* char height */
_AT_@ -406,6 +405,7 @@ static void xdrawcursor(void);
 static void xinit(void);
 static void xloadcols(void);
 static int xsetcolorname(int, const char *);
+static int xgeommasktogravity(int);
 static int xloadfont(Font *, FcPattern *);
 static void xloadfonts(char *, double);
 static int xloadfontset(Font *);
_AT_@ -2808,18 +2808,19 @@ xhints(void) {
 	XSizeHints *sizeh = NULL;
 
 	sizeh = XAllocSizeHints();
-	if(xw.isfixed == False) {
-		sizeh->flags = PSize | PResizeInc | PBaseSize;
-		sizeh->height = xw.h;
-		sizeh->width = xw.w;
-		sizeh->height_inc = xw.ch;
-		sizeh->width_inc = xw.cw;
-		sizeh->base_height = 2 * borderpx;
-		sizeh->base_width = 2 * borderpx;
-	} else {
-		sizeh->flags = PMaxSize | PMinSize;
-		sizeh->min_width = sizeh->max_width = xw.fw;
-		sizeh->min_height = sizeh->max_height = xw.fh;
+	sizeh->flags = PSize | PResizeInc | PBaseSize;
+	sizeh->height = xw.h;
+	sizeh->width = xw.w;
+	sizeh->height_inc = xw.ch;
+	sizeh->width_inc = xw.cw;
+	sizeh->base_height = 2 * borderpx;
+	sizeh->base_width = 2 * borderpx;
+
+	if (xw.gm & (XValue | YValue)) {
+		sizeh->flags |= USPosition | PWinGravity;
+		sizeh->x = xw.l;
+		sizeh->y = xw.t;
+		sizeh->win_gravity = xgeommasktogravity(xw.gm);
 	}
 
 	XSetWMProperties(xw.dpy, xw.win, NULL, NULL, NULL, 0, sizeh, &wm,
_AT_@ -2828,6 +2829,16 @@ xhints(void) {
 }
 
 int
+xgeommasktogravity(int mask) {
+	switch (mask & (XNegative | YNegative)) {
+	case 0: return NorthWestGravity;
+	case XNegative: return NorthEastGravity;
+	case YNegative: return SouthWestGravity;
+	default: return SouthEastGravity;
+	}
+}
+
+int
 xloadfont(Font *f, FcPattern *pattern) {
 	FcPattern *match;
 	FcResult result;
_AT_@ -2972,7 +2983,6 @@ xinit(void) {
 	XGCValues gcvalues;
 	Cursor cursor;
 	Window parent;
-	int sw, sh;
 	pid_t thispid = getpid();
 
 	if(!(xw.dpy = XOpenDisplay(NULL)))
_AT_@ -2991,24 +3001,13 @@ xinit(void) {
 	xw.cmap = XDefaultColormap(xw.dpy, xw.scr);
 	xloadcols();
 
-	/* adjust fixed window geometry */
-	if(xw.isfixed) {
-		sw = DisplayWidth(xw.dpy, xw.scr);
-		sh = DisplayHeight(xw.dpy, xw.scr);
-		if(xw.fx < 0)
-			xw.fx = sw + xw.fx - xw.fw - 1;
-		if(xw.fy < 0)
-			xw.fy = sh + xw.fy - xw.fh - 1;
-
-		xw.h = xw.fh;
-		xw.w = xw.fw;
-	} else {
-		/* window - default size */
-		xw.h = 2 * borderpx + term.row * xw.ch;
-		xw.w = 2 * borderpx + term.col * xw.cw;
-		xw.fx = 0;
-		xw.fy = 0;
-	}
+	/* setup window geometry */
+	xw.h = 2 * borderpx + term.row * xw.ch;
+	xw.w = 2 * borderpx + term.col * xw.cw;
+	if(xw.gm & XNegative)
+		xw.l += DisplayWidth(xw.dpy, xw.scr) - xw.w - 2;
+	if(xw.gm & YNegative)
+		xw.t += DisplayHeight(xw.dpy, xw.scr) - xw.h - 2;
 
 	/* Events */
 	xw.attrs.background_pixel = dc.col[defaultbg].pixel;
_AT_@ -3021,7 +3020,7 @@ xinit(void) {
 
 	parent = opt_embed ? strtol(opt_embed, NULL, 0) : \
 			XRootWindow(xw.dpy, xw.scr);
-	xw.win = XCreateWindow(xw.dpy, parent, xw.fx, xw.fy,
+	xw.win = XCreateWindow(xw.dpy, parent, xw.l, xw.t,
 			xw.w, xw.h, 0, XDefaultDepth(xw.dpy, xw.scr), InputOutput,
 			xw.vis, CWBackPixel | CWBorderPixel | CWBitGravity
 			| CWEventMask | CWColormap, &xw.attrs);
_AT_@ -3718,10 +3717,7 @@ run(void) {
 	}
 
 	ttynew();
-	if(!xw.isfixed)
-		cresize(w, h);
-	else
-		cresize(xw.fw, xw.fh);
+	cresize(w, h);
 
 	gettimeofday(&last, NULL);
 	lastblink = last;
_AT_@ -3811,13 +3807,9 @@ usage(void) {
 
 int
 main(int argc, char *argv[]) {
-	int bitm, xr, yr;
-	uint wr, hr;
+	uint cols = 80, rows = 24;
 	char *titles;
 
-	xw.fw = xw.fh = xw.fx = xw.fy = 0;
-	xw.isfixed = False;
-
 	ARGBEGIN {
 	case 'a':
 		allowaltscreen = false;
_AT_@ -3839,22 +3831,8 @@ main(int argc, char *argv[]) {
 		opt_font = EARGF(usage());
 		break;
 	case 'g':
-		bitm = XParseGeometry(EARGF(usage()), &xr, &yr, &wr, &hr);
-		if(bitm & XValue)
-			xw.fx = xr;
-		if(bitm & YValue)
-			xw.fy = yr;
-		if(bitm & WidthValue)
-			xw.fw = (int)wr;
-		if(bitm & HeightValue)
-			xw.fh = (int)hr;
-		if(bitm & XNegative && xw.fx == 0)
-			xw.fx = -1;
-		if(bitm & YNegative && xw.fy == 0)
-			xw.fy = -1;
-
-		if(xw.fh != 0 && xw.fw != 0)
-			xw.isfixed = True;
+		xw.gm = XParseGeometry(EARGF(usage()),
+				&xw.l, &xw.t, &cols, &rows);
 		break;
 	case 'o':
 		opt_io = EARGF(usage());
_AT_@ -3873,7 +3851,7 @@ main(int argc, char *argv[]) {
 run:
 	setlocale(LC_CTYPE, "");
 	XSetLocaleModifiers("");
-	tnew(80, 24);
+	tnew(cols ? cols : 1, rows ? rows : 1);
 	xinit();
 	selinit();
 	run();
-- 
1.9.2
Received on Fri Apr 25 2014 - 20:52:16 CEST

This archive was generated by hypermail 2.3.0 : Fri Apr 25 2014 - 21:00:12 CEST