aboutsummaryrefslogtreecommitdiffstats
path: root/modules/directionalfocus/directionalfocus.c
blob: 5d1ebcb067b8f1e587fa4bdcce37d2367867b761 (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
void
focusdir(const Arg *arg)
{
	Client *s = selmon->sel, *f = NULL, *c, *next;

	if (!s)
		return;

	unsigned int score = -1;
	unsigned int client_score;
	int isfloating = s->isfloating;

	next = s->next;
	if (!next)
		next = s->mon->clients;
	for (c = next; c != s; c = next) {

		next = c->next;
		if (!next)
			next = s->mon->clients;

		if (!ISVISIBLE(c) || c->isfloating != isfloating)
			continue;

#if INFINITE_TAGS
		if (selmon->lt[selmon->sellt]->arrange == NULL) {

			int s_cx = s->x + s->w / 2;
			int s_cy = s->y + s->h / 2;
			int c_cx = c->x + c->w / 2;
			int c_cy = c->y + c->h / 2;

			int dx = c_cx - s_cx;  /* positive = c is to the right */
			int dy = c_cy - s_cy;  /* positive = c is below */

			int axial, lateral;

			switch (arg->i) {
			case 0: /* left */
				if (dx >= 0) continue;
				axial   = -dx;
				lateral = abs(dy);
				break;
			case 1: /* right */
				if (dx <= 0) continue;
				axial   = dx;
				lateral = abs(dy);
				break;
			case 2: /* up */
				if (dy >= 0) continue;
				axial   = -dy;
				lateral = abs(dx);
				break;
			default:
			case 3: /* down */
				if (dy <= 0) continue;
				axial   = dy;
				lateral = abs(dx);
				break;
			}

			client_score = axial + (unsigned int)((long)lateral * lateral) / (axial + 1);
		} else
#endif
		{
			int dist;
			int dirweight = 20;
			switch (arg->i) {
        case 0:
          dist = s->x - c->x - c->w;
          client_score =
            dirweight * abs(dist) +
            abs(s->y - c->y);
          break;
        case 1:
          dist = c->x - s->x - s->w;
          client_score =
            dirweight * abs(dist) +
            abs(c->y - s->y);
          break;
        case 2:
          dist = s->y - c->y - c->h;
          client_score =
            dirweight * abs(dist) +
            abs(s->x - c->x);
          break;
        default:
        case 3:
          dist = c->y - s->y - s->h;
          client_score =
            dirweight * abs(dist) +
            abs(c->x - s->x);
        break;
			}
		}

		if (client_score < score) {
			score = client_score;
			f = c;
		}
	}

	if (f && f != s) {
		focus(f);
#if INFINITE_TAGS
		centerwindow(NULL);
#endif
#if WARP_TO_CLIENT && WARP_TO_CENTER_OF_WINDOW_AFFECTED_BY_FOCUSSTACK
		warptoclient(f);
#endif
		restack(f->mon);
	}
}