File: WinMagic.c

package info (click to toggle)
fvwm 1:2.6.8-1
  • links: PTS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 15,804 kB
  • sloc: ansic: 145,770; xml: 17,093; perl: 7,302; sh: 4,921; makefile: 1,094; yacc: 688; lex: 187; sed: 11
file content (321 lines) | stat: -rw-r--r-- 7,766 bytes parent folder | download
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
/* -*-c-*- */
/* Copyright (C) 1999  Dominik Vogt */
/* This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see: <http://www.gnu.org/licenses/>
 */

/*
** WinMagic.c:
** This file supplies routines for moving and resizing windows in an animated
** fashion.
*/

#include "config.h"

#include <X11/Xlib.h>
#include <fvwmlib.h>
#include <stdio.h>
#include "WinMagic.h"

/* Continuously moves and resized window win from start geometry (s_?) to end
 * geometry (e_?). Waits for delay_ms milliseconds after each step except the
 * last (max. 10 seconds per step). The number of steps is determined by the
 * steps argument (min. 1 and max. 10000). If the pointer ppctMovement is NULL
 * the steps are all the same width, if it is given it is interpreted as a
 * pointer to an array of float percent values. These are used to determine the
 * distance of each step counted from the start position, i.e. 0.0 means the
 * start position itself, 50.0 is halfway between start and end position and
 * 100.0 is the end position. Values smaller than 0.0 or bigger than 100.0 are
 * allowed too. The do_flush flag determines of all requests are sent to the
 * X server immediately (True) or not (False). The use_hints detrmines if the
 * min size and resize hints are used */
void SlideWindow(
	Display *dpy, Window win,
	int s_x, int s_y, int s_w, int s_h,
	int e_x, int e_y, int e_w, int e_h,
	int steps, int delay_ms, float *ppctMovement,
	Bool do_sync, Bool use_hints)
{
	int x = 0;
	int y = 0;
	int w = 0;
	int h = 0;
	int g_w = 0;
	int g_h = 0;  /* -Wall fixes :o( */
	int min_w = 1;
	int min_h = 1;
	int inc_w = 1;
	int inc_h = 1;
	int i;
	unsigned int us;
	Bool is_mapped;
	Bool keep_x1 = False;
	Bool keep_x2 = False;
	Bool keep_y1 = False;
	Bool keep_y2 = False;
	XSizeHints hints;
	long dummy;

	/* check limits */
	if (delay_ms > 10000)
	{
		/* max. 10 seconds per step */
		us = 10000000;
	}
	else if (delay_ms < 0)
	{
		us = 0;
	}
	else
	{
		us = 1000 * delay_ms;
	}

	if (steps > 10000)
	{
		/* max. 10000 steps */
		steps = 10000;
	}
	if (steps <= 0)
	{
		/* no steps, no animation */
		if (e_w == 0 || e_h == 0)
		{
			XUnmapWindow(dpy, win);
		}
		else
		{
			XMoveResizeWindow(dpy, win, e_x, e_y, e_w, e_h);
			XMapWindow(dpy, win);
			XMapSubwindows(dpy, win);
		}
		if (do_sync)
			XSync(dpy, 0);
		return;
	}

	is_mapped = False;

	/* Get the mini (re)size hints and do some check consistency */
	if (use_hints && FGetWMNormalHints(dpy, win, &hints, &dummy))
	{
		if (hints.flags & PMinSize)
		{
			if (hints.min_width >= 1 && hints.min_width <=
			    max(e_w,s_w))
				min_w = hints.min_width;
			if (hints.min_height >= 1 && hints.min_height <=
			    max(e_h,s_h))
				min_h = hints.min_height;
		}
		if (hints.flags & PResizeInc)
		{
			if (hints.width_inc >= 1 && hints.width_inc <=
			    max(e_w,s_w))
				inc_w = hints.width_inc;
			if (hints.height_inc >= 1 && hints.width_inc <=
			    max(e_h,s_h))
				inc_h = hints.height_inc;
		}
	}

	if (s_x == e_x)
		keep_x1 = True;
	if (s_y == e_y)
		keep_y1 = True;
	if (s_x + s_w == e_x + e_w)
		keep_x2 = True;
	if (s_y + s_h == e_y + e_h)
		keep_y2 = True;

	/* animate the window */
	for (i = 0; i <= steps; i++)
	{
		if (i == steps)
		{
			x = e_x;
			y = e_y;
		}
		else
		{
			float f;

			if (ppctMovement == NULL)
			{
				f = (float)i / (float)steps;
			}
			else
			{
				f = ppctMovement[i] / (float)steps;
			}
			x = (int)((float)s_x + (float)(e_x - s_x) * f);
			y = (int)((float)s_y + (float)(e_y - s_y) * f);
		}
		w = s_w + (int)(e_w - s_w) * i / steps;
		h = s_h + (int)(e_h - s_h) * i / steps;

		/* take the resize inc in account */
		g_w = w - ((w - min_w) % inc_w);
		x += w-g_w;
		g_h = h - ((h - min_h) % inc_h);
		y += h-g_h;

		/* prevent annoying flickering */
		if (keep_x1)
			x = s_x;
		if (keep_y1)
			y = s_y;
		if (keep_x2)
			g_w = s_x + s_w - x;
		if (keep_y2)
			g_h = s_y + s_h - y;

		if (g_w < min_w || g_h < min_h)
		{
			/* don't show zero width/height windows */
			if (is_mapped)
			{
				XUnmapWindow(dpy, win);
				is_mapped = False;
			}
		}
		else
		{
			XMoveResizeWindow(dpy, win, x, y, g_w, g_h);
			if (!is_mapped)
			{
				XMapWindow(dpy, win);
				XMapSubwindows(dpy, win);
				is_mapped = True;
			}
		}
		/* make sure everything is updated */
		if (do_sync)
			XSync(dpy, 0);
		if (us && i < steps && is_mapped)
		{
			/* don't sleep after the last step */
			usleep(us);
		}
	} /* for */

	/* if hints and asked size do not agree try to respect the caller */
	if (e_w > 0 && e_h > 0 && (!is_mapped || g_w != w || g_h != w))
	{
		XMoveResizeWindow(dpy, win, x, y, w, h);
		if (!is_mapped)
		{
			XMapWindow(dpy, win);
			XMapSubwindows(dpy, win);
		}
		if (do_sync)
			XSync(dpy, 0);
	}
	return;
}

/* This function returns the top level ancestor of the window 'child'. It
 * returns None if an error occurs or if the window is a top level window. */
Window GetTopAncestorWindow(Display *dpy, Window child)
{
	Window root = None;
	Window ancestor = child;
	Window last_child = child;
	Window *children;
	unsigned int nchildren;

	if (child == None)
		return None;

	while (ancestor != root)
	{
		last_child = ancestor;
		children = NULL;
		if (
			!XQueryTree(
				dpy, last_child, &root, &ancestor, &children,
				&nchildren))
		{
			return None;
		}
		if (children)
		{
			XFree(children);
		}
	}

	return (last_child == child) ? None : last_child;
}

/* Given a parent window this function returns a list of children of the
 * parent window that have the same size, depth, visual and colormap as the
 * parent window and that have position +0+0 within the parent. If the 'depth'
 * argument is non-zero it must match the depth of the window. The 'visualid'
 * and 'colormap' arguments work just the same. The number of matching
 * children is returned. The list of children is returned in *children. If this
 * list is non-NULL, it must be free'd with XFree. If an error occurs or the
 * parent window does not match the required depth, colormap or visualid, the
 * function returns -1 and NULL in *children. */
int GetEqualSizeChildren(
	Display *dpy, Window parent, int depth, VisualID visualid,
	Colormap colormap, Window **ret_children)
{
	XWindowAttributes pxwa;
	XWindowAttributes cxwa;
	Window JunkW;
	Window *children;
	unsigned int nchildren;
	int i;
	int j;

	if (!XGetWindowAttributes(dpy, parent, &pxwa))
		return -1;
	if (!XQueryTree(dpy, parent, &JunkW, &JunkW, &children, &nchildren))
		return -1;
	if (depth && pxwa.depth != depth)
		return -1;
	if (visualid && XVisualIDFromVisual(pxwa.visual) != visualid)
		return -1;
	if (colormap && pxwa.colormap != colormap)
		return -1;

	for (i = 0, j = 0; i < nchildren; i++)
	{
		if (XGetWindowAttributes(dpy, children[i], &cxwa) &&
		    cxwa.x == 0 &&
		    cxwa.y == 0 &&
		    cxwa.width == pxwa.width &&
		    cxwa.height == pxwa.height &&
		    (!depth || cxwa.depth == depth) &&
		    (
			    !visualid ||
			    XVisualIDFromVisual(cxwa.visual) == visualid) &&
		    cxwa.class == InputOutput &&
		    (!colormap || cxwa.colormap == colormap))
		{
			children[j++] = children[i];
		}
	} /* for */

	if (j == 0)
	{
		if (children)
		{
			XFree(children);
			children = NULL;
		}
	}
	*ret_children = children;

	return j;
}