File: mouse.c

package info (click to toggle)
wmii2 2.5.2-11
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 608 kB
  • ctags: 925
  • sloc: ansic: 9,195; makefile: 184; sh: 11
file content (631 lines) | stat: -rw-r--r-- 14,109 bytes parent folder | download | duplicates (3)
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
618
619
620
621
622
623
624
625
626
627
628
629
630
631
/*
 * (C)opyright MMIV-MMV Anselm R. Garbe <garbeam at gmail dot com>
 * See LICENSE file for license details.
 */

#include <stdlib.h>

#include "wmiiwm.h"

Cursor 
cursor_for_motion(Frame * f, int x, int y)
{
	int             n, e, w, s, tn, te, tw, ts;
	int             tabh, bw;

	bw = _strtonum(f->files[F_BORDER_W]->content, 0, 10);
	tabh = _strtonum(f->files[F_TAB_H]->content, 0, 30);

	if (!f || !bw)
		return normal_cursor;

	/* rectangle attributes of client are used */
	w = x < bw;
	e = x >= rect_of_frame(f)->width - bw;
	n = y < bw;
	s = y >= rect_of_frame(f)->height - bw;

	tw = x < (tabh ? tabh : 2 * bw);
	te = x > rect_of_frame(f)->width - (tabh ? tabh : 2 * bw);
	tn = y < (tabh ? tabh : 2 * bw);
	ts = s > rect_of_frame(f)->height - (tabh ? tabh : 2 * bw);

	if ((w && n) || (w && tn) || (n && tw))
		return nw_cursor;
	else if ((e && n) || (e && tn) || (n && te))
		return ne_cursor;
	else if ((w && s) || (w && ts) || (s && tw))
		return sw_cursor;
	else if ((e && s) || (e && ts) || (s && te))
		return se_cursor;
	else if (w)
		return w_cursor;
	else if (e)
		return e_cursor;
	else if (n)
		return n_cursor;
	else if (s)
		return s_cursor;

	return normal_cursor;
}

Align 
xy_to_align(XRectangle * rect, int x, int y)
{

	int             w = x <= rect->x + rect->width / 2;
	int             n = y <= rect->y + rect->height / 2;
	int             e = x > rect->x + rect->width / 2;
	int             s = y > rect->y + rect->height / 2;
	int             nw = w && n;
	int             ne = e && n;
	int             sw = w && s;
	int             se = e && s;

	if (nw)
		return NWEST;
	else if (ne)
		return NEAST;
	else if (se)
		return SEAST;
	else if (sw)
		return SWEST;
	else if (w)
		return WEST;
	else if (e)
		return EAST;
	else if (n)
		return NORTH;
	else if (s)
		return SOUTH;
	return CENTER;
}

Align 
cursor_to_align(Cursor cursor)
{
	if (cursor == w_cursor)
		return WEST;
	else if (cursor == nw_cursor)
		return NWEST;
	else if (cursor == n_cursor)
		return NORTH;
	else if (cursor == ne_cursor)
		return NEAST;
	else if (cursor == e_cursor)
		return EAST;
	else if (cursor == se_cursor)
		return SEAST;
	else if (cursor == s_cursor)
		return SOUTH;
	else if (cursor == sw_cursor)
		return SWEST;
	return CENTER;		/* should not happen */
}

static int 
check_vert_match(XRectangle * r, XRectangle * neighbor)
{
	/* check if neighbor matches edge */
	return (((neighbor->y <= r->y)
		 && (neighbor->y + neighbor->height >= r->y))
		|| ((neighbor->y >= r->y)
		    && (r->y + r->height >= neighbor->y)));
}

static int 
check_horiz_match(XRectangle * r, XRectangle * neighbor)
{
	/* check if neighbor matches edge */
	return (((neighbor->x <= r->x)
		 && (neighbor->x + neighbor->width >= r->x))
		|| ((neighbor->x >= r->x)
		    && (r->x + r->width >= neighbor->x)));
}

static void
snap_move(XRectangle * r, XRectangle * rects,
	  unsigned int num, int snapw, int snaph)
{

	int             i, j, w = 0, n = 0, e = 0, s = 0;

	/* snap to other windows */
	for (i = 0; i <= snapw && !(w && e); i++) {

		for (j = 0; j < num && !(w && e); j++) {

			/* check west neighbors leftwards */
			if (!w) {
				if (r->x - i == (rects[j].x + rects[j].width)) {
					/*
					 * west edge of neighbor found, check
					 * vert match
					 */
					w = check_vert_match(r, &rects[j]);
					if (w)
						r->x = rects[j].x + rects[j].width;
				}
			}
			/* check west neighbors rightwards */
			if (!w) {
				if (r->x + i == (rects[j].x + rects[j].width)) {
					/*
					 * west edge of neighbor found, check
					 * vert match
					 */
					w = check_vert_match(r, &rects[j]);
					if (w)
						r->x = rects[j].x + rects[j].width;
				}
			}
			/* check east neighbors leftwards */
			if (!e) {
				if (r->x + r->width - i == rects[j].x) {
					/*
					 * east edge of neighbor found, check
					 * vert match
					 */
					e = check_vert_match(r, &rects[j]);
					if (e)
						r->x = rects[j].x - r->width;
				}
			}
			/* check east neighbors rightwards */
			if (!e) {
				if (r->x + r->width + i == rects[j].x) {
					/*
					 * east edge of neighbor found, check
					 * vert match
					 */
					e = check_vert_match(r, &rects[j]);
					if (e)
						r->x = rects[j].x - r->width;
				}
			}
		}

		/* snap to west screen border */
		if (!w && (r->x - i == rect.x)) {
			w = 1;
			r->x = rect.x;
		}
		/* snap to west screen border */
		if (!w && (r->x + i == rect.x)) {
			w = 1;
			r->x = rect.x;
		}
		/* snap to east screen border */
		if (!e && (r->x + r->width - i == rect.width)) {
			e = 1;
			r->x = rect.x + rect.width - r->width;
		}
		if (!e && (r->x + r->width + i == rect.width)) {
			e = 1;
			r->x = rect.x + rect.width - r->width;
		}
	}

	for (i = 0; i <= snaph && !(n && s); i++) {

		for (j = 0; j < num && !(n && s); j++) {
			/* check north neighbors upwards */
			if (!n) {
				if (r->y - i == (rects[j].y + rects[j].height)) {
					/*
					 * north edge of neighbor found,
					 * check horiz match
					 */
					n = check_horiz_match(r, &rects[j]);
					if (n)
						r->y = rects[j].y + rects[j].height;
				}
			}
			/* check north neighbors downwards */
			if (!n) {
				if (r->y + i == (rects[j].y + rects[j].height)) {
					/*
					 * north edge of neighbor found,
					 * check horiz match
					 */
					n = check_horiz_match(r, &rects[j]);
					if (n)
						r->y = rects[j].y + rects[j].height;
				}
			}
			/* check south neighbors upwards */
			if (!s) {
				if (r->y + r->height - i == rects[j].y) {
					/*
					 * south edge of neighbor found,
					 * check horiz match
					 */
					s = check_horiz_match(r, &rects[j]);
					if (s)
						r->y = rects[j].y - r->height;
				}
			}
			/* check south neighbors downwards */
			if (!s) {
				if (r->y + r->height + i == rects[j].y) {
					/*
					 * south edge of neighbor found,
					 * check horiz match
					 */
					s = check_horiz_match(r, &rects[j]);
					if (s)
						r->y = rects[j].y - r->height;
				}
			}
		}

		/* snap to north screen border */
		if (!n && (r->y - i == rect.y)) {
			n = 1;
			r->y = rect.y;
		}
		if (!n && (r->y + i == rect.y)) {
			n = 1;
			r->y = rect.y;
		}
		/* snap to south screen border */
		if (!s && (r->y + r->height - i == rect.height)) {
			s = 1;
			r->y = rect.y + rect.height - r->height;
		}
		if (!s && (r->y + r->height + i == rect.height)) {
			s = 1;
			r->y = rect.y + rect.height - r->height;
		}
	}
}

static void 
draw_pseudo_border(XRectangle * r)
{
	XRectangle      pseudo = *r;

	pseudo.x += 2;
	pseudo.y += 2;
	pseudo.width -= 4;
	pseudo.height -= 4;
	XDrawRectangles(dpy, root, xorgc, &pseudo, 1);
	XSync(dpy, False);
}


void 
mouse_move(Frame * f)
{
	int             px = 0, py = 0, wex, wey, ex, ey, first = 1, i;
	Window          dummy;
	XEvent          ev;
	/* borders */
	int             snapw = rect.width *
	_strtonum(core_files[CORE_SNAP_VALUE]->content, 0, 1000) / 1000;
	int             snaph = rect.height *
	_strtonum(core_files[CORE_SNAP_VALUE]->content, 0, 1000) / 1000;
	unsigned int    num;
	unsigned int    dmask;
	XRectangle     *rects = rectangles(&num);
	XRectangle      frect = *rect_of_frame(f);
	XPoint          pt;

	XQueryPointer(dpy, f->win, &dummy, &dummy, &i, &i, &wex, &wey, &dmask);
	XTranslateCoordinates(dpy, f->win, root, wex, wey, &ex, &ey, &dummy);
	pt.x = ex;
	pt.y = ey;
	XSync(dpy, False);
	XGrabServer(dpy);
	XGrabPointer(dpy, root, False, ButtonMotionMask | ButtonReleaseMask,
		     GrabModeAsync, GrabModeAsync, None, move_cursor,
		     CurrentTime);
	for (;;) {
		while (!XCheckMaskEvent(dpy,
			       ButtonReleaseMask | ButtonMotionMask, &ev)) {
			usleep(20000);
			continue;
		}

		switch (ev.type) {
		case ButtonRelease:
			if (!first) {
				draw_pseudo_border(&frect);
				resize_frame(f, &frect, &pt, 0);
			}
			draw_page(pages[sel_page]);
			free(rects);
			XUngrabPointer(dpy, CurrentTime /* ev.xbutton.time */ );
			XUngrabServer(dpy);
			XSync(dpy, False);
			return;
			break;
		case MotionNotify:
			pt.x = ev.xmotion.x;
			pt.y = ev.xmotion.y;
			XTranslateCoordinates(dpy, f->win, root, ev.xmotion.x,
					    ev.xmotion.y, &px, &py, &dummy);
			if (first)
				first = 0;
			else
				draw_pseudo_border(&frect);
			frect.x = px - ex;
			frect.y = py - ey;
			snap_move(&frect, rects, num, snapw, snaph);
			draw_pseudo_border(&frect);
			break;
		}
	}
}

static void
snap_resize(XRectangle * r, XRectangle * o, Align align,
	    XRectangle * rects, unsigned int num, int px, int ox, int py,
	    int oy, int snapw, int snaph)
{
	int             i, j, pend = 0;
	int             w, h;

	/* x */
	switch (align) {
	case NEAST:
	case EAST:
	case SEAST:
		w = px - r->x + (o->width - ox);
		if (w < 10)
			break;
		r->width = w;
		if (w <= snapw)
			break;
		/* snap to border */
		for (i = 0; !pend && (i < snapw); i++) {
			if (r->x + r->width - i == rect.x + rect.width) {
				r->width -= i;
				break;
			}
			if (r->x + r->width + i == rect.x + rect.width) {
				r->width += i;
				break;
			}
			for (j = 0; j < num; j++) {
				if (r->x + r->width - i == rects[j].x) {
					pend = check_vert_match(r, &rects[j]);
					if (pend) {
						r->width -= i;
						break;
					}
				}
				if (r->x + r->width + i == rects[j].x) {
					pend = check_vert_match(r, &rects[j]);
					if (pend) {
						r->width += i;
						break;
					}
				}
			}
		}
		break;
	case NWEST:
	case WEST:
	case SWEST:
		w = r->width + r->x - px + ox;
		if (w < 10)
			break;
		r->width = w;
		r->x = px - ox;
		if (w <= snapw)
			break;
		/* snap to border */
		for (i = 0; !pend && (i < snapw); i++) {
			if (r->x - i == rect.x) {
				r->x -= i;
				r->width += i;
				break;
			}
			if (r->x + i == rect.x) {
				r->x += i;
				r->width -= i;
				break;
			}
			for (j = 0; j < num; j++) {
				if (r->x - i == rects[j].x + rects[j].width) {
					pend = check_vert_match(r, &rects[j]);
					if (pend) {
						r->x -= i;
						r->width += i;
						break;
					}
				}
				if (r->x + i == rects[j].x + rects[j].width) {
					pend = check_vert_match(r, &rects[j]);
					if (pend) {
						r->x += i;
						r->width -= i;
						break;
					}
				}
			}
		}
		break;
	default:
		break;
	}

	/* y */
	pend = 0;
	switch (align) {
	case SWEST:
	case SOUTH:
	case SEAST:
		h = py - r->y + (o->height - oy);
		if (h < 10)
			break;
		r->height = h;
		if (h <= snaph)
			break;
		/* snap to border */
		for (i = 0; !pend && (i < snaph); i++) {
			if (r->y + r->height - i == rect.y + rect.height) {
				r->height -= i;
				break;
			}
			if (r->y + r->height + i == rect.y + rect.height) {
				r->height += i;
				break;
			}
			for (j = 0; j < num; j++) {
				if (r->y + r->height - i == rects[j].y) {
					pend = check_horiz_match(r, &rects[j]);
					if (pend) {
						r->height -= i;
						break;
					}
				}
				if (r->y + r->height + i == rects[j].y) {
					pend = check_horiz_match(r, &rects[j]);
					if (pend) {
						r->height += i;
						break;
					}
				}
			}
		}
		break;
	case NWEST:
	case NORTH:
	case NEAST:
		h = r->height + r->y - py + oy;
		if (h < 10)
			break;
		r->height = h;
		r->y = py - oy;
		if (h <= snaph)
			break;
		/* snap to border */
		for (i = 0; !pend && (i < snaph); i++) {
			if (r->y - i == rect.y) {
				r->y -= i;
				r->height += i;
				break;
			}
			if (r->y + i == rect.y) {
				r->y += i;
				r->height -= i;
				break;
			}
			for (j = 0; j < num; j++) {
				if (r->y - i == rects[j].y + rects[j].height) {
					pend = check_horiz_match(r, &rects[j]);
					if (pend) {
						r->y -= i;
						r->height += i;
						break;
					}
				}
				if (r->y + i == rects[j].y + rects[j].height) {
					pend = check_horiz_match(r, &rects[j]);
					if (pend) {
						r->y += i;
						r->height -= i;
						break;
					}
				}
			}
		}
		break;
	default:
		break;
	}
}


void 
mouse_resize(Frame * f, Align align)
{
	int             px = 0, py = 0, i, ox, oy, first = 1;
	Window          dummy;
	XEvent          ev;
	/* borders */
	int             snapw = rect.width *
	_strtonum(core_files[CORE_SNAP_VALUE]->content, 0, 1000) / 1000;
	int             snaph = rect.height *
	_strtonum(core_files[CORE_SNAP_VALUE]->content, 0, 1000) / 1000;
	unsigned int    dmask;
	unsigned int    num;
	XRectangle     *rects = rectangles(&num);
	XRectangle      frect = *rect_of_frame(f);
	XRectangle      origin = frect;

	XQueryPointer(dpy, f->win, &dummy, &dummy, &i, &i, &ox, &oy, &dmask);
	XSync(dpy, False);
	XGrabServer(dpy);
	XGrabPointer(dpy, f->win, False, ButtonMotionMask | ButtonReleaseMask,
		     GrabModeAsync, GrabModeAsync, None, resize_cursor,
		     CurrentTime);
	for (;;) {
		while (!XCheckMaskEvent(dpy,
			       ButtonReleaseMask | ButtonMotionMask, &ev)) {
			usleep(20000);
			continue;
		}

		switch (ev.type) {
		case ButtonRelease:
			if (!first) {
				XPoint          pt;
				draw_pseudo_border(&frect);
				pt.x = px;
				pt.y = py;
				resize_frame(f, &frect, &pt, 0);
			}
			XUngrabPointer(dpy, CurrentTime /* ev.xbutton.time */ );
			XUngrabServer(dpy);
			XSync(dpy, False);
			return;
			break;
		case MotionNotify:
			XTranslateCoordinates(dpy, f->win, root, ev.xmotion.x,
					    ev.xmotion.y, &px, &py, &dummy);

			if (first)
				first = 0;
			else
				draw_pseudo_border(&frect);

			snap_resize(&frect, &origin, align, rects, num, px,
				    ox, py, oy, snapw, snaph);
			draw_pseudo_border(&frect);
			break;
		}
	}
}


void 
drop_move(Frame * f, XRectangle * new, XPoint * pt)
{
	Page           *p = f->page;
	int             cx, cy;
	int             i, idx = index_item((void **) p->managed, f);

	if ((f->managed_rect.x == new->x) && (f->managed_rect.y == new->y))
		return;
	cx = (pt ? pt->x : new->x + new->width / 2);
	cy = (pt ? pt->y : new->y + new->height / 2);
	for (i = 0; p->managed && p->managed[i]; i++) {
		if ((p->managed[i] != f)
		    && blitz_ispointinrect(cx, cy, &p->managed[i]->managed_rect)) {
			swap((void **) &p->managed[i], (void **) &p->managed[idx]);
			p->managed_stack =
				(Frame **) attach_item_begin(detach_item((void **) p->
							      managed_stack,
							      p->managed[i],
							   sizeof(Frame *)),
							     p->managed[i],
							   sizeof(Frame *));
			p->layout->arrange(p);
			return;
		}
	}
}