File: gravity.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 (617 lines) | stat: -rw-r--r-- 12,093 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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
/* -*-c-*- */
/* 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/>
 */

/* ---------------------------- included header files ---------------------- */

#include "config.h"

#include <stdio.h>

#include <X11/Xlib.h>
#include "fvwmlib.h"
#include "Parse.h"
#include "Strings.h"
#include "gravity.h"

/* ---------------------------- local definitions -------------------------- */

/* ---------------------------- local macros ------------------------------- */

/* ---------------------------- imports ------------------------------------ */

/* ---------------------------- included code files ------------------------ */

/* ---------------------------- local types -------------------------------- */

struct _gravity_offset
{
	int x, y;
};

/* ---------------------------- forward declarations ----------------------- */

/* ---------------------------- local variables ---------------------------- */

#define STRINGS_PER_DIR 7
static char *gravity_dir_optlist[] = {
	"-", "N",  "North",     "Top",         "t", "Up",         "u",
	"]", "E",  "East",      "Right",       "r", "Right",      "r",
	"_", "S",  "South",     "Bottom",      "b", "Down",       "d",
	"[", "W",  "West",      "Left",        "l", "Left",       "l",
	"^", "NE", "NorthEast", "TopRight",    "tr", "UpRight",   "ur",
	">", "SE", "SouthEast", "BottomRight", "br", "DownRight", "dr",
	"v", "SW", "SouthWest", "BottomLeft",  "bl", "DownLeft",  "dl",
	"<", "NW", "NorthWest", "TopLeft",     "tl", "UpLeft",    "ul",
	".", "C",  "Center",    "Centre",      NULL, NULL,        NULL,
	NULL
};

/* ---------------------------- exported variables (globals) --------------- */

/* ---------------------------- local functions ---------------------------- */

/* ---------------------------- interface functions ------------------------ */

/* map gravity to (x,y) offset signs for adding to x and y when window is
 * mapped to get proper placement. */
void gravity_get_offsets(int grav, int *xp,int *yp)
{
	static struct _gravity_offset gravity_offsets[11] = {
		{  0,  0 },     /* ForgetGravity */
		{ -1, -1 },     /* NorthWestGravity */
		{  0, -1 },     /* NorthGravity */
		{  1, -1 },     /* NorthEastGravity */
		{ -1,  0 },     /* WestGravity */
		{  0,  0 },     /* CenterGravity */
		{  1,  0 },     /* EastGravity */
		{ -1,  1 },     /* SouthWestGravity */
		{  0,  1 },     /* SouthGravity */
		{  1,  1 },     /* SouthEastGravity */
		{  0,  0 },     /* StaticGravity */
	};

	if (grav < ForgetGravity || grav > StaticGravity)
	{
		*xp = *yp = 0;
	}
	else
	{
		*xp = (int)gravity_offsets[grav].x;
		*yp = (int)gravity_offsets[grav].y;
	}

	return;
}

/* Move a rectangle while taking gravity into account. */
void gravity_move(int gravity, rectangle *rect, int xdiff, int ydiff)
{
	int xoff;
	int yoff;

	gravity_get_offsets(gravity, &xoff, &yoff);
	rect->x -= xoff * xdiff;
	rect->y -= yoff * ydiff;

	return;
}

/* Resize rectangle while taking gravity into account. */
void gravity_resize(int gravity, rectangle *rect, int wdiff, int hdiff)
{
	int xoff;
	int yoff;

	gravity_get_offsets(gravity, &xoff, &yoff);
	rect->x -= (wdiff * (xoff + 1)) / 2;
	rect->width += wdiff;
	rect->y -= (hdiff * (yoff + 1)) / 2;
	rect->height += hdiff;

	return;
}

/* Moves a child rectangle taking its gravity into accout as if the parent
 * rectangle was moved and resized. */
void gravity_move_resize_parent_child(
	int child_gravity, rectangle *parent_diff_r, rectangle *child_r)
{
	int xoff;
	int yoff;

	gravity_get_offsets(child_gravity, &xoff, &yoff);
	child_r->x -= xoff * parent_diff_r->x;
	child_r->y -= yoff * parent_diff_r->y;
	child_r->x += ((xoff + 1) * parent_diff_r->width) / 2;
	child_r->y += ((yoff + 1) * parent_diff_r->height) / 2;

	return;
}

direction_t gravity_grav_to_dir(
	int grav)
{
	switch (grav)
	{
	case NorthWestGravity:
		return DIR_NW;
	case NorthGravity:
		return DIR_N;
	case NorthEastGravity:
		return DIR_NE;
	case WestGravity:
		return DIR_W;
	case CenterGravity:
		return DIR_NONE;
	case EastGravity:
		return DIR_E;
	case SouthWestGravity:
		return DIR_SW;
	case SouthGravity:
		return DIR_S;
	case SouthEastGravity:
		return DIR_SE;
	case ForgetGravity:
	case StaticGravity:
	default:
		return DIR_NONE;
	}
}

int gravity_dir_to_grav(
	direction_t dir)
{
	switch (dir)
	{
	case DIR_N:
		return NorthGravity;
	case DIR_E:
		return EastGravity;
	case DIR_S:
		return SouthGravity;
	case DIR_W:
		return WestGravity;
	case DIR_NE:
		return NorthEastGravity;
	case DIR_SE:
		return SouthEastGravity;
	case DIR_SW:
		return SouthWestGravity;
	case DIR_NW:
		return NorthWestGravity;
	case DIR_NONE:
	default:
		return ForgetGravity;
	}
}

int gravity_combine_xy_grav(
	int grav_x, int grav_y)
{
	switch (grav_x)
	{
	case NorthWestGravity:
	case WestGravity:
	case SouthWestGravity:
		grav_x = WestGravity;
		break;
	case NorthEastGravity:
	case EastGravity:
	case SouthEastGravity:
		grav_x = EastGravity;
		break;
	default:
		grav_x = CenterGravity;
		break;
	}
	switch (grav_y)
	{
	case NorthWestGravity:
	case NorthGravity:
	case NorthEastGravity:
		grav_y = NorthGravity;
		break;
	case SouthWestGravity:
	case SouthGravity:
	case SouthEastGravity:
		grav_y = SouthGravity;
		break;
	default:
		grav_y = CenterGravity;
		break;
	}
	if (grav_x == CenterGravity)
	{
		return grav_y;
	}
	switch (grav_y)
	{
	case NorthGravity:
		return (grav_x == WestGravity) ?
			NorthWestGravity : NorthEastGravity;
	case SouthGravity:
		return (grav_x == WestGravity) ?
			SouthWestGravity : SouthEastGravity;
	case CenterGravity:
	default:
		return grav_x;
	}

	return 0;
}

void gravity_split_xy_grav(
	int *ret_grav_x, int *ret_grav_y, int in_grav)
{
	switch (in_grav)
	{
	case NorthWestGravity:
	case WestGravity:
	case SouthWestGravity:
		*ret_grav_x = WestGravity;
		break;
	case NorthEastGravity:
	case EastGravity:
	case SouthEastGravity:
		*ret_grav_x = EastGravity;
		break;
	case NorthGravity:
	case CenterGravity:
	case SouthGravity:
	case ForgetGravity:
	case StaticGravity:
	default:
		*ret_grav_x = CenterGravity;
		break;
	}
	switch (in_grav)
	{
	case NorthWestGravity:
	case NorthGravity:
	case NorthEastGravity:
		*ret_grav_y = NorthGravity;
		break;
	case SouthWestGravity:
	case SouthGravity:
	case SouthEastGravity:
		*ret_grav_y = SouthGravity;
		break;
	case WestGravity:
	case CenterGravity:
	case EastGravity:
	case ForgetGravity:
	case StaticGravity:
	default:
		*ret_grav_y = CenterGravity;
		break;
	}
}

int gravity_combine_xy_dir(
	int dir_x, int dir_y)
{
	switch (dir_x)
	{
	case DIR_W:
	case DIR_NW:
	case DIR_SW:
		dir_x = DIR_W;
		break;
	case DIR_E:
	case DIR_NE:
	case DIR_SE:
		dir_x = DIR_E;
		break;
	default:
		dir_x = DIR_NONE;
		break;
	}
	switch (dir_y)
	{
	case DIR_N:
	case DIR_NW:
	case DIR_NE:
		dir_y = DIR_N;
		break;
	case DIR_S:
	case DIR_SW:
	case DIR_SE:
		dir_y = DIR_S;
		break;
	default:
		dir_y = DIR_NONE;
		break;
	}
	if (dir_x == DIR_NONE)
	{
		return dir_y;
	}
	switch (dir_y)
	{
	case DIR_N:
		return (dir_x == DIR_W) ? DIR_NW : DIR_NE;
	case DIR_S:
		return (dir_x == DIR_W) ? DIR_SW : DIR_SE;
	case DIR_NONE:
	default:
		return dir_x;
	}
}

void gravity_split_xy_dir(
	int *ret_dir_x, int *ret_dir_y, int in_dir)
{
	switch (in_dir)
	{
	case DIR_W:
	case DIR_SW:
	case DIR_NW:
		*ret_dir_x = DIR_W;
		break;
	case DIR_E:
	case DIR_NE:
	case DIR_SE:
		*ret_dir_x = DIR_E;
		break;
	case DIR_N:
	case DIR_S:
	case DIR_NONE:
	default:
		*ret_dir_x = DIR_NONE;
		break;
	}
	switch (in_dir)
	{
	case DIR_N:
	case DIR_NW:
	case DIR_NE:
		*ret_dir_y = DIR_N;
		break;
	case DIR_S:
	case DIR_SW:
	case DIR_SE:
		*ret_dir_y = DIR_S;
		break;
	case DIR_W:
	case DIR_E:
	case DIR_NONE:
	default:
		*ret_dir_y = DIR_NONE;
		break;
	}
}

static inline int __gravity_override_one_axis(int dir_orig, int dir_mod)
{
	int ret_dir;

	if (dir_mod == DIR_NONE)
	{
		ret_dir = dir_orig;
	}
	else
	{
		ret_dir = dir_mod;
	}

	return ret_dir;
}

int gravity_override_dir(
	int dir_orig, int dir_mod)
{
	int ret_dir;
	int ret_x;
	int ret_y;
	int orig_x;
	int orig_y;
	int mod_x;
	int mod_y;

	gravity_split_xy_dir(&orig_x, &orig_y, dir_orig);
	gravity_split_xy_dir(&mod_x, &mod_y, dir_mod);
	ret_x = __gravity_override_one_axis(orig_x, mod_x);
	ret_y = __gravity_override_one_axis(orig_y, mod_y);
	ret_dir = gravity_combine_xy_dir(ret_x, ret_y);

	return ret_dir;
}

int gravity_dir_to_sign_one_axis(
	direction_t dir)
{
	switch (dir)
	{
	case DIR_N:
	case DIR_W:
		return -1;
	case DIR_S:
	case DIR_E:
		return 1;
	default:
		return 0;
	}
}

/* Parses the next token in action and returns
 *
 *   0 if it is N, North, Top or Up
 *   1 if it is E, East, Right or Right
 *   2 if it is S, South, Bottom or Down
 *   3 if it is E, West, Left or Left
 *   4 if it is NE, NorthEast, TopRight or UpRight
 *   5 if it is SE, SouthEast, BottomRight or DownRight
 *   6 if it is SW, SouthWest, BottomLeft or DownLeft
 *   7 if it is NW, NorthWest, TopLeft or UpLeft
 *   8 if it is C, Center or Centre
 *   default_ret if no string matches.
 *
 * A pointer to the first character in action behind the token is returned
 * through ret_action in this case. ret_action may be NULL. If the token
 * matches none of these strings the default_ret value is returned and the
 * action itself is passed back in ret_action. */
direction_t gravity_parse_dir_argument(
	char *action, char **ret_action, direction_t default_ret)
{
	int index;
	int rc;
	char *next;

	next = GetNextTokenIndex(action, gravity_dir_optlist, 0, &index);
	if (index == -1)
	{
		/* nothing selected, use default and don't modify action */
		rc = default_ret;
		next = action;
	}
	else
	{
		rc = index / STRINGS_PER_DIR;
	}
	if (ret_action)
	{
		*ret_action = next;
	}

	return (direction_t)rc;
}

char *gravity_dir_to_string(direction_t dir, char *default_str)
{
	char *str = NULL;
	int d = dir * STRINGS_PER_DIR;

	if (d >= sizeof(gravity_dir_optlist)/sizeof(gravity_dir_optlist[0]))
	{
		return default_str;
	}
	str = gravity_dir_optlist[d];

	if (str == NULL)
	{
		return default_str;
	}

	return str;
}

multi_direction_t gravity_parse_multi_dir_argument(
	char *action, char **ret_action)
{
	int rc = MULTI_DIR_NONE;
	char *token, *str;
	direction_t dir = gravity_parse_dir_argument(action, ret_action, -1);

	if (dir != -1)
	{
		rc = (1 << dir);
	}
	else
	{
		token = PeekToken(action, &str);
		if (StrEquals(token, "all"))
		{
			rc = MULTI_DIR_ALL;
			*ret_action = str;
		}
		else
		{
			rc = MULTI_DIR_NONE;
		}
	}

	return (multi_direction_t)rc;
}

void gravity_get_next_multi_dir(int dir_set, multi_direction_t *dir)
{
	if (*dir == MULTI_DIR_NONE)
	{
		*dir = MULTI_DIR_FIRST;
		if (dir_set & *dir)
		{
			return;
		}
	}
	while(*dir != MULTI_DIR_LAST)
	{
		*dir = (*dir << 1);
		if (dir_set & *dir)
		{
			return;
		}
	}
	*dir = MULTI_DIR_NONE;

	return;
}

direction_t gravity_multi_dir_to_dir(multi_direction_t mdir)
{
	direction_t dir = DIR_NONE;

	for ( ; mdir != 0; dir++)
	{
		mdir = (mdir >> 1);
	}
	if (dir > DIR_ALL_MASK)
	{
		dir = DIR_NONE;
	}

	return dir;
}

void gravity_rotate_xy(rotation_t rot, int x, int y, int *ret_x, int *ret_y)
{
	int tx;
	int ty;

	switch (rot)
	{
	case ROTATION_90:
		/* CW */
		tx = -y;
		ty = x;
		break;
	case ROTATION_180:
		tx = -x;
		ty = -y;
		break;
	case ROTATION_270:
		/* CCW */
		tx = y;
		ty = -x;
		break;
	default:
	case ROTATION_0:
		tx = x;
		ty = y;
		break;
	}
	*ret_x = tx;
	*ret_y = ty;

	return;
}

rotation_t gravity_add_rotations(rotation_t rot1, rotation_t rot2)
{
	rotation_t rot;

	rot = ((rot1 + rot2) & ROTATION_MASK);

	return rot;
}