summaryrefslogtreecommitdiffhomepage
path: root/patch/floatpos.c
blob: 25207b489df1b87310f2d1c00ed5c90ed8bb30d9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
void
floatpos(const Arg *arg)
{
	Client *c = selmon->sel;

	if (!c || !c->isfloating)
		return;

	setfloatpos(c, (char *)arg->v);
	resizeclient(c, c->x, c->y, c->w, c->h);

	#if !FOCUSONCLICK_PATCH
	XRaiseWindow(dpy, c->win);
	XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w/2, c->h/2);
	#endif // FOCUSONCLICK_PATCH
}

void
setfloatpos(Client *c, const char *floatpos)
{
	char xCh, yCh, wCh, hCh;
	int x, y, w, h, cx, cy, cw, ch, wx, ww, wy, wh, bw;
	unsigned int i;
	#if FLOATPOS_RESPECT_GAPS_PATCH
	int oh, ov, ih, iv;
	unsigned int n;
	#endif // FLOATPOS_RESPECT_GAPS_PATCH
	int absx, absy, absw, absh, delta, rest;

	if (!c || !floatpos)
		return;
	if (selmon->lt[selmon->sellt]->arrange && !c->isfloating)
		return;
	switch(sscanf(floatpos, "%d%c %d%c %d%c %d%c", &x, &xCh, &y, &yCh, &w, &wCh, &h, &hCh)) {
		case 4:
			if (xCh == 'w' || xCh == 'W') {
				w = x; wCh = xCh;
				h = y; hCh = yCh;
				x = -1; xCh = 'C';
				y = -1; yCh = 'C';
			} else if (xCh == 'p' || xCh == 'P') {
				w = x; wCh = xCh;
				h = y; hCh = yCh;
				x = 0; xCh = 'G';
				y = 0; yCh = 'G';
			} else {
				w = 0; wCh = 0;
				h = 0; hCh = 0;
			}
			break;
		case 8:
			break;
		default:
			return;
	}

	absx = xCh == 'A' || xCh == 'a';
	absy = yCh == 'A' || yCh == 'a';
	absw = wCh == 'A' || wCh == 'a';
	absh = hCh == 'A' || hCh == 'a';

	#if FLOATPOS_RESPECT_GAPS_PATCH
	getgaps(c->mon, &oh, &ov, &ih, &iv, &n);
	wx = c->mon->wx + ov;
	wy = c->mon->wy + oh;
	ww = c->mon->ww - 2*ov;
	wh = c->mon->wh - 2*oh;
	#else
	wx = c->mon->wx;
	wy = c->mon->wy;
	ww = c->mon->ww;
	wh = c->mon->wh;
	#endif // FLOATPOS_RESPECT_GAPS_PATCH

	cx = (!absx && c->x < wx ? wx : c->x);
	cy = (!absy && c->y < wy ? wy : c->y);
	cw = (!absw && c->w > ww ? ww : c->w);
	ch = (!absh && c->h > wh ? wh : c->h);

	switch(xCh) {
	case 'A': // absolute position
		cx = x;
		break;
	case 'a': // absolute relative position
		cx += x;
		break;
	case 'x': // client relative position
		if (cx + x > ww)
			cx = ww;
		else
			cx += x;
		break;
	case 'X': // client position relative to monitor
		if (x > ww)
			x = ww;
		cx = wx + x;
		break;
	case 'S': // fixed client position (sticky)
	case 'C': // fixed client position (center)
	case 'Z': // fixed client right-hand position (position + width)
		if (x == -1)
			break;
		if (x > ww)
			x = ww;
		if (x < 0)
			x = 0;
		if (xCh == 'Z')
			cw = abs((cx + cw) - (wx + x));
		else if (xCh == 'C')
			cw = abs((cx + cw / 2) - (wx + x));
		else
			cw = abs(cx - (wx + x));
		cx = wx + x;
		wCh = 0; // size determined by position, override defined width
		break;
	case 'G': // grid
		if (x <= 0)
			x = floatposgrid_x; // default configurable
		if (w == 0 || x < 2 || (wCh != 'p' && wCh != 'P'))
			break;
		delta = (ww - cw - 2*c->bw) / (x - 1);
		rest = ww - cw - 2*c->bw - delta * (x - 1);
		if (wCh == 'P') {
			if (w < 1 || w > x)
				break;
			cx = wx + delta * (w - 1);
		} else {
			for (i = 0; i < x -1 && cx >= wx + delta * i + (i > x - rest ? i + rest - x + 1 : 0); i++);
			cx = wx + delta * (MAX(MIN(i + w, x), 1) - 1) + (i > x - rest ? i + rest - x + 1 : 0);
		}
		break;
	}

	switch(yCh) {
	case 'A': // absolute position
		cy = y;
		break;
	case 'a': // absolute relative position
		cy += y;
		break;
	case 'y': // client relative position
		if (cy + y > wh)
			cy = wh;
		else
			cy += y;
		break;
	case 'Y': // client position relative to monitor
		if (y > wh)
			y = wh;
		cy = wy + y;
		break;
	case 'S': // fixed client y position (sticky)
	case 'C': // fixed client position (center)
	case 'Z': // fixed client right-hand position (position + height)
		if (y == -1)
			break;
		if (y > wh)
			y = wh;
		if (y < 0)
			y = 0;
		if (yCh == 'Z')
			ch = abs((cy + ch) - (wy + y));
		else if (yCh == 'C')
			ch = abs((cy + ch / 2) - (wy + y));
		else
			ch = abs(cy - (wy + y));
		cy = wy + y;
		hCh = 0; // size determined by position, override defined width
		break;
	case 'G': // grid
		if (y <= 0)
			y = floatposgrid_y; // default configurable
		if (h == 0 || y < 1 || (hCh != 'p' && hCh != 'P'))
			break;
		delta = (wh - ch - 2*c->bw) / (y - 1);
		rest = wh - ch - 2*c->bw - delta * (y - 1);
		if (hCh == 'P') {
			if (h < 1 || h > y)
				break;
			cy = wy + delta * (h - 1);
		} else {
			for (i = 0; i < y - 1 && cy >= wy + delta * i + (i > y - rest ? i + rest - y + 1: 0); i++);
			cy = wy + delta * (MAX(MIN(i + h, y), 1) - 1) + (i > y - rest ? i + rest - y + 1: 0);
		}
		hCh = 0; // size determined by position, override defined width
		break;
	}

	bw = 2*c->bw;
	switch(wCh) {
	case 'A': // absolute width
		cw = w - 2*c->bw;
		break;
	case 'a': // absolute relative width
		if (cw + w < 1)
			cw = 1;
		else
			cw += w;
		break;
	case '%': // client width percentage in relation to monitor window area width
		if (w > 100)
			w = 100;
		if (w <= 0)
			break;
		w = ww * w / 100;
		/* falls through */
	case 'w': // width relative to client
		if (wCh == 'w') {
			if (w == 0)
				break;
			else {
				w += cw;
				bw = 0;
			}
		}
		/* falls through */
	case 'W': // normal width, position takes precedence
		if (xCh == 'S' && cx + w > wx + ww)
			w = wx + ww - cx;
		else if (xCh == 'C' && cx + cw / 2 + w / 2 > wx + ww)
			w = wx + ww - cx + cw / 2;
		else if (xCh == 'Z' && w > cx - ww)
			w = cx - wx;
		else if (w > ww)
			w = ww;

		if (xCh == 'C') { // fixed client center, expand or contract client
			delta = w - bw - cw;
			if (delta < 0 || (cx - delta / 2 + w + 2*c->bw <= wx + ww))
				cx -= delta / 2;
			else if (cx - delta / 2 < wx)
				cx = wx;
			else if (delta)
				cx = wx + ww - cw - bw;
		}

		cw = w - bw;
		break;
	}

	bw = 2*c->bw;
	switch(hCh) {
	case 'A': // absolute height
		ch = h - 2*c->bw;
		break;
	case 'a': // absolute relative height
		if (ch + h < 1)
			ch = 1;
		else
			ch += h;
		break;
	case '%': // client height percentage in relation to monitor window area height
		if (h > 100)
			h = 100;
		if (h <= 0)
			break;
		h = wh * h / 100;
		/* falls through */
	case 'h': // height relative to client
		if (hCh == 'h') {
			if (h == 0)
				break;
			else {
				h += ch;
				bw = 0;
			}
		}
		/* falls through */
	case 'H': /* normal height, position takes precedence */
		if (yCh == 'S' && cy + h > wy + wh)
			h = wy + wh - cy;
		else if (yCh == 'C' && cy + ch / 2 + h / 2 > wy + wh)
			h = wy + wh - cy + ch / 2;
		else if (yCh == 'Z' && h > cy - wh)
			h = cy - wy;
		else if (h > wh)
			h = wh;

		if (yCh == 'C') { // fixed client center, expand or contract client
			delta = h - bw - ch;
			if (delta < 0 || (cy - delta / 2 + ch + 2*c->bw <= wy + wh))
				cy -= delta / 2;
			else if (cy - delta / 2 < wy)
				cy = wy;
			else if (delta)
				cy = wy + wh - ch - bw;
		}

		ch = h - bw;
		break;
	}

	if (xCh == '%') { // client mid-point position in relation to monitor window area width
		if (x > 100)
			x = 100;
		if (x < 0)
			x = 0;
		cx = wx + ww * x / 100 - (cw + 2*c->bw) / 2;
	}

	if (yCh == '%') { // client mid-point position in relation to monitor window area height
		if (y > 100)
			y = 100;
		if (y < 0)
			y = 0;
		cy = wy + wh * y / 100 - (ch + 2*c->bw) / 2;
	}

	if (!absx && cx < wx)
		cx = wx;
	if (cx + cw + 2*c->bw > wx + ww && !(absx && absw)) {
		if (absx || cx == wx)
			cw = ww - cx + wx - 2*c->bw;
		else
			cx = wx + ww - cw - 2*c->bw;
	}

	if (!absy && cy < wy)
		cy = wy;
	if (cy + ch + 2*c->bw > wy + wh && !(absy && absh)) {
		if (absy || cy == wy)
			ch = wh - cy + wy - 2*c->bw;
		else
			cy = wy + wh - ch - 2*c->bw;
	}

	c->x = cx;
	c->y = cy;
	c->w = MAX(cw, 1);
	c->h = MAX(ch, 1);
}