~rhialto/ctwm/smallbeer

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
/*
 * Copyright notice...
 */

#include "ctwm.h"

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>

#include "r_area_list.h"
#include "r_area.h"


/*
 * Prototype internal funcs
 */
static RAreaList *RAreaListCopy(const RAreaList *self);
static void RAreaListDelete(RAreaList *self, int index);
static void RAreaListAddList(RAreaList *self, const RAreaList *other);
static RAreaList *RAreaListIntersectCrop(const RAreaList *self,
                const RArea *area);

/* Sorts and their internal comparison routines */
static int _cmpX(const void *av, const void *bv);
static void RAreaListSortX(const RAreaList *self);
static int _cmpY(const void *av, const void *bv);
static void RAreaListSortY(const RAreaList *self);



/**
 * Create an RAreaList from a set of RArea's.
 * \param cap Hint as to the number of RArea's being passed
 * \param ... Sequence of RArea * to put in it.  Don't forget a trailing
 *            NULL.
 */
RAreaList *
RAreaListNew(int cap, ...)
{
	va_list ap;
	RAreaList *list;
	RArea *area;

	list = malloc(sizeof(RAreaList));
	if(list == NULL) {
		abort();
	}
	list->len = 0;

	if(cap <= 0) {
		cap = 1;
	}
	list->cap = cap;
	list->areas = malloc(cap * sizeof(RArea));
	if(list->areas == NULL) {
		abort();
	}

	va_start(ap, cap);

	while((area = va_arg(ap, RArea *)) != NULL) {
		RAreaListAdd(list, area);
	}

	va_end(ap);

	return list;
}


/**
 * Create a copy of a given RAreaList.
 */
static RAreaList *
RAreaListCopy(const RAreaList *self)
{
	RAreaList *new = RAreaListNew(self->cap, NULL);

	RAreaListAddList(new, self);

	return new;
}


/**
 * Create a copy of an RAreaList with given amounts cropped off the
 * sides.  This is used principally during startup, to handle the
 * BorderBottom/Top/Left/Right config params.
 */
RAreaList *
RAreaListCopyCropped(const RAreaList *self, int left_margin,
                     int right_margin,
                     int top_margin, int bottom_margin)
{
	if(left_margin > 0 || right_margin > 0
	                || top_margin > 0 || bottom_margin > 0) {
		// Start with a big spanning square
		RArea big_area = RAreaListBigArea(self);

		// Guard against negative margins
		if(left_margin < 0) {
			left_margin = 0;
		}
		if(right_margin < 0) {
			right_margin = 0;
		}
		if(top_margin < 0) {
			top_margin = 0;
		}
		if(bottom_margin < 0) {
			bottom_margin = 0;
		}

		// Squeeze down the big square by the asked for amounts
		big_area.x += left_margin;
		big_area.width -= left_margin + right_margin;
		big_area.y += top_margin;
		big_area.height -= top_margin + bottom_margin;

		// If we cropped down to nothing, that's a RAreaList with nothing
		// in it, so give back that.
		if(big_area.width <= 0 || big_area.height <= 0) {
			return RAreaListNew(0, NULL); // empty list
		}

		// Make a new RAreaList cropped down to that size.
		return RAreaListIntersectCrop(self, &big_area);
	}

	// Nothing to do; our callers expect getting nothing back.
	return NULL;
}


/**
 * Clean up and free an RAreaList.
 */
void
RAreaListFree(RAreaList *self)
{
	if(self == NULL) {
		return;
	}
	free(self->areas);
	free(self);
}


/**
 * Delete an RArea from inside an RAreaList.
 */
static void
RAreaListDelete(RAreaList *self, int index)
{
	if(index >= self->len) {
		return;
	}

	self->len--;

	if(index == self->len) {
		return;
	}

	memcpy(&self->areas[index], &self->areas[index + 1],
	       (self->len - index) * sizeof(RArea));
}


/**
 * Add an RArea onto an RAreaList.
 */
void
RAreaListAdd(RAreaList *self, const RArea *area)
{
	if(self->cap == self->len) {
		RArea *new_list = realloc(self->areas, (self->cap + 1) * sizeof(RArea));
		if(new_list == NULL) {
			abort();
		}

		self->cap++;
		self->areas = new_list;
	}

	self->areas[self->len++] = *area;
}


/**
 * Add the RArea's from one RAreaList onto another.
 */
static void
RAreaListAddList(RAreaList *self, const RAreaList *other)
{
	if(self->cap - self->len < other->len) {
		RArea *new_list = realloc(self->areas,
		                          (self->len + other->len) * sizeof(RArea));
		if(new_list == NULL) {
			abort();
		}

		self->cap = self->len + other->len;
		self->areas = new_list;
	}

	memcpy(&self->areas[self->len], other->areas, other->len * sizeof(RArea));

	self->len += other->len;
}


/**
 * qsort comparison function to sort by RArea.x
 */
static int
_cmpX(const void *av, const void *bv)
{
	const RArea *a = (const RArea *)av, *b = (const RArea *)bv;

	if(a->x < b->x) {
		return -1;
	}

	if(a->x > b->x) {
		return 1;
	}

	return (a->y > b->y) - (a->y < b->y);
}


/**
 * Sort the RArea's in an RAreaList by their x coordinate.
 */
static void
RAreaListSortX(const RAreaList *self)
{
	if(self->len <= 1) {
		return;
	}

	qsort(self->areas, self->len, sizeof(RArea), _cmpX);
}


/**
 * qsort comparison function to sort by RArea.t
 */
static int
_cmpY(const void *av, const void *bv)
{
	const RArea *a = (const RArea *)av, *b = (const RArea *)bv;

	if(a->y < b->y) {
		return -1;
	}

	if(a->y > b->y) {
		return 1;
	}

	return (a->x > b->x) - (a->x < b->x);
}


/**
 * Sort the RArea's in an RAreaList by their y coordinate.
 */
static void
RAreaListSortY(const RAreaList *self)
{
	if(self->len <= 1) {
		return;
	}

	qsort(self->areas, self->len, sizeof(RArea), _cmpY);
}


/**
 * Create an RAreaList whose RArea's are the horizontal union of our
 * RArea's.
 */
RAreaList *
RAreaListHorizontalUnion(const RAreaList *self)
{
	RAreaList *copy = RAreaListCopy(self);

refine:
	// Two areas can't form a horizontal stripe if there's any space
	// between them.  So start by putting them all in x-coord order to be
	// sure any gaps there are necessary.
	RAreaListSortX(copy);

	// Try HorizontalUnion'ing each area with the next one.  If we can
	// create a union, replace them with it, and hop back to the top of
	// the process to start over.
	for(int i = 0; i < copy->len - 1; i++) {
		for(int j = i + 1; j < copy->len; j++) {
			RAreaList *repl = RAreaHorizontalUnion(&copy->areas[i], &copy->areas[j]);
			if(repl != NULL) {
				if(repl->len) {
					RAreaListDelete(copy, j);
					RAreaListDelete(copy, i);
					RAreaListAddList(copy, repl);
					RAreaListFree(repl);
					goto refine;
				}
				RAreaListFree(repl);
			}
		}
	}

	return copy;
}


/**
 * Create an RAreaList whose RArea's are the vertical union of our
 * RArea's.
 */
RAreaList *
RAreaListVerticalUnion(const RAreaList *self)
{
	RAreaList *copy = RAreaListCopy(self);

refine:
	// X-ref logic above in RAreaListHorizontalUnion()
	RAreaListSortY(copy);

	for(int i = 0; i < copy->len - 1; i++) {
		for(int j = i + 1; j < copy->len; j++) {
			RAreaList *repl = RAreaVerticalUnion(&copy->areas[i], &copy->areas[j]);
			if(repl != NULL) {
				if(repl->len) {
					RAreaListDelete(copy, j);
					RAreaListDelete(copy, i);
					RAreaListAddList(copy, repl);
					RAreaListFree(repl);
					goto refine;
				}
				RAreaListFree(repl);
			}
		}
	}

	return copy;
}


/**
 * Create an RAreaList of all the areas in an RAreaList that a given
 * RArea intersects with.
 */
RAreaList *
RAreaListIntersect(const RAreaList *self, const RArea *area)
{
	RAreaList *new = RAreaListNew(self->len, NULL);

	for(int i = 0; i < self->len; i++) {
		if(RAreaIsIntersect(&self->areas[i], area)) {
			RAreaListAdd(new, &self->areas[i]);
		}
	}

	return new;
}


/**
 * Run a function over each RArea in an RAreaList until one returns true,
 * allowing them a place to stash other internal data.
 */
void
RAreaListForeach(const RAreaList *self,
                 bool (*func)(const RArea *cur_area, void *data),
                 void *data)
{
	for(int i = 0 ; i < self->len ; i++) {
		if(func(&(self->areas[i]), data) == true) {
			break;
		}
	}
}



/**
 * Create an RAreaList from another, cropped to a certain area defined by
 * an RArea.
 */
static RAreaList *
RAreaListIntersectCrop(const RAreaList *self, const RArea *area)
{
	RAreaList *new = RAreaListNew(self->len, NULL);

	for(int i = 0; i < self->len; i++) {
		RArea it = RAreaIntersect(&self->areas[i], area);
		if(RAreaIsValid(&it)) {
			RAreaListAdd(new, &it);
		}
	}

	return new;
}


/**
 * Create a maximal RArea describing the union of an RAreaList.
 *
 * This is used to construct a giant square that contains all our
 * monitors (and the dead area necessary to cover them).  It winds up
 * being the equivalent of a spanning pseudo-Root window, and is used
 * when we need to figure some sort of "overall" positioning, like when
 * figuring "real" x/y coordinates.
 */
RArea
RAreaListBigArea(const RAreaList *self)
{
	int x, y, x2, y2;

	// Guard; probably impossible
	if(self->len < 1) {
		return RAreaInvalid();
	}

	for(int i = 0 ; i < self->len ; i++) {
		const RArea *area = &(self->areas[i]);
		if(i == 0 || area->x < x) {
			x = area->x;
		}

		if(i == 0 || area->y < y) {
			y = area->y;
		}

		if(i == 0 || RAreaX2(area) > x2) {
			x2 = RAreaX2(area);
		}

		if(i == 0 || RAreaY2(area) > y2) {
			y2 = RAreaY2(area);
		}
	}

	return RAreaNew(x, y, x2 - x + 1, y2 - y + 1);
}


/**
 * Find the RArea in an RAreaList that has the largest intersection with
 * a given RArea.  Colloquially, which area in an RAreaList does our
 * RArea mostly fit into?  This is used to resize a window to fill one
 * monitor, by finding which monitor it's on.
 */
RArea
RAreaListBestTarget(const RAreaList *self, const RArea *area)
{
	RArea full_area = RAreaInvalid();
	int max_area = -1;

	for(int i = 0; i < self->len; i++) {
		RArea it = RAreaIntersect(area, &self->areas[i]);
		if(RAreaIsValid(&it) && (max_area < 0 || RAreaArea(&it) > max_area)) {
			max_area = RAreaArea(&it);
			full_area = self->areas[i];
		}
	}

	return full_area;
}


/**
 * Find the x coordinate of the right-most RArea in an RAreaList.
 */
int
RAreaListMaxX(const RAreaList *self)
{
	RArea *cur_area = &self->areas[0], *area_end = &self->areas[self->len];
	int max_x = self->len ? cur_area->x : 0;

	// While a for(i=0 ; i<self->len ; i++) loop is generally nicer for
	// these iterations, it winds up being a little trickier here, so we
	// leave it as a pointer-stepper.
	while(++cur_area < area_end) {
		if(cur_area->x > max_x) {
			max_x = cur_area->x;
		}
	}

	return max_x;
}


/**
 * Find the y coordinate of the bottom-most RArea in an RAreaList.
 */
int
RAreaListMaxY(const RAreaList *self)
{
	RArea *cur_area = &self->areas[0], *area_end = &self->areas[self->len];
	int max_y = self->len ? cur_area->y : 0;

	while(++cur_area < area_end) {
		if(cur_area->y > max_y) {
			max_y = cur_area->y;
		}
	}

	return max_y;
}


/**
 * Find the x coordinate of the right edge of the left-most RArea in an
 * RAreaList.
 */
int
RAreaListMinX2(const RAreaList *self)
{
	RArea *cur_area = &self->areas[0], *area_end = &self->areas[self->len];
	int min_x = self->len ? RAreaX2(cur_area) : 0;

	while(++cur_area < area_end) {
		if(RAreaX2(cur_area) < min_x) {
			min_x = RAreaX2(cur_area);
		}
	}

	return min_x;
}


/**
 * Find the y coordinate of the bottom edge of the top-most RArea in an
 * RAreaList.
 */
int
RAreaListMinY2(const RAreaList *self)
{
	RArea *cur_area = &self->areas[0], *area_end = &self->areas[self->len];
	int min_y = self->len ? RAreaY2(cur_area) : 0;

	while(++cur_area < area_end) {
		if(RAreaY2(cur_area) < min_y) {
			min_y = RAreaY2(cur_area);
		}
	}

	return min_y;
}


/**
 * Pretty-print an RAreaList.
 *
 * Used for dev/debug.
 */
void
RAreaListPrint(const RAreaList *self)
{
	fprintf(stderr, "[len=%d cap=%d", self->len, self->cap);

	for(int i = 0 ; i < self->len ; i++) {
		RArea *area = &self->areas[i];
		fprintf(stderr, " ");
		RAreaPrint(area);
	}

	fprintf(stderr, "]");
}