Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ropery/aa2035d95a2a9ddefc65 to your computer and use it in GitHub Desktop.
Save ropery/aa2035d95a2a9ddefc65 to your computer and use it in GitHub Desktop.
From: lolilolicon <lolilolicon@gmail.com>
Date: Sun, 10 Aug 2014 15:40:18 +0000
Subject: [PATCH] win_open: refactor geometry & gravity calculation
When either x or y coordinate is not specified, default to place window
at the center of the screen in that dimension. Assign window gravity
according to the dimension and value of user specified coordinate(s).
It's worth noting that NorthGravity and SouthGravity are impossible to
be achieved via user specified geometry, since it's impossible to
specify y without also specifying x. To solve this limitation, we must
write a "smarter" version of XParseGeometry().
Note: issue #9 remains, but the quick fix in commit 0b83386 does not
solve the issue correctly anyway; e.g. if a negative value of x is
specified, the calculated win->x suffers the same problem. For that
reason, we here ignore issue #9 and keep on putting the window at the
center, pretending no one is using more than one monitors.
diff --git a/window.c b/window.c
index 5387564..de2763a 100644
--- a/window.c
+++ b/window.c
@@ -200,40 +200,60 @@ void win_open(win_t *win)
e = &win->env;
sizehints.flags = PWinGravity;
- sizehints.win_gravity = NorthWestGravity;
-
- /* determine window offsets, width & height */
if (options->geometry == NULL)
gmask = 0;
else
gmask = XParseGeometry(options->geometry, &win->x, &win->y,
&win->w, &win->h);
- if ((gmask & WidthValue) != 0)
- sizehints.flags |= USSize;
- else
+ /* determine window width & height */
+ if ((gmask & WidthValue) == 0)
win->w = WIN_WIDTH;
- if ((gmask & HeightValue) != 0)
+ if ((gmask & HeightValue) == 0)
+ win->h = WIN_HEIGHT;
+ if ((gmask & (WidthValue | HeightValue)) != 0)
sizehints.flags |= USSize;
+ /* determine window offsets */
+ if ((gmask & XValue) == 0)
+ win->x = (e->scrw - win->w) / 2; /* default: center on X */
+ else if ((gmask & XNegative) != 0)
+ win->x += e->scrw - win->w;
+ if ((gmask & YValue) == 0)
+ win->y = (e->scrh - win->h) / 2; /* default: center on Y */
+ else if ((gmask & YNegative) != 0)
+ win->y += e->scrh - win->h;
+ if ((gmask & (XValue | YValue)) != 0)
+ sizehints.flags |= USPosition;
else
- win->h = WIN_HEIGHT;
- if ((gmask & XValue) != 0) {
- if ((gmask & XNegative) != 0) {
- win->x += e->scrw - win->w;
+ sizehints.flags |= PPosition;
+ /* determine window gravity */
+ switch (gmask & (XValue | YValue | XNegative | YNegative)) {
+ case XValue | YValue:
+ sizehints.win_gravity = NorthWestGravity;
+ break;
+ case XValue | YValue | XNegative:
sizehints.win_gravity = NorthEastGravity;
- }
- sizehints.flags |= USPosition;
- } else {
- win->x = 0;
- }
- if ((gmask & YValue) != 0) {
- if ((gmask & YNegative) != 0) {
- win->y += e->scrh - win->h;
- sizehints.win_gravity = sizehints.win_gravity == NorthEastGravity
- ? SouthEastGravity : SouthWestGravity;
- }
- sizehints.flags |= USPosition;
- } else {
- win->y = 0;
+ break;
+ case XValue | YValue | YNegative:
+ sizehints.win_gravity = SouthWestGravity;
+ break;
+ case XValue | YValue | XNegative | YNegative:
+ sizehints.win_gravity = SouthEastGravity;
+ break;
+ case XValue:
+ sizehints.win_gravity = WestGravity;
+ break;
+ case XValue | XNegative:
+ sizehints.win_gravity = EastGravity;
+ break;
+ case YValue:
+ sizehints.win_gravity = NorthGravity;
+ break;
+ case YValue | YNegative:
+ sizehints.win_gravity = SouthGravity;
+ break;
+ default:
+ sizehints.win_gravity = CenterGravity;
+ break;
}
win->xwin = XCreateWindow(e->dpy, RootWindow(e->dpy, e->scr),
--
2.0.4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment