summaryrefslogtreecommitdiff
path: root/patch/stacker.c
blob: ed8b76b7c7b2789d9094a5c92b6fdcbe0250bda1 (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
focusstack(const Arg *arg)
{
	int i = stackpos(arg);
	Client *c, *p;

	if (i < 0)
 		return;

	#if LOSEFULLSCREEN_PATCH
	if (!selmon->sel)
		return;
	#elif FAKEFULLSCREEN_CLIENT_PATCH
	if (!selmon->sel || (selmon->sel->isfullscreen && !selmon->sel->fakefullscreen))
		return;
	#else
	if (!selmon->sel || selmon->sel->isfullscreen)
		return;
	#endif // LOSEFULLSCREEN_PATCH

	#if BAR_WINTITLEACTIONS_PATCH
	for (p = NULL, c = selmon->clients; c && (i || !ISVISIBLE(c) || HIDDEN(c));
		i -= (ISVISIBLE(c) && !HIDDEN(c) ? 1 : 0), p = c, c = c->next);
	#else
	for (p = NULL, c = selmon->clients; c && (i || !ISVISIBLE(c));
		i -= (ISVISIBLE(c) ? 1 : 0), p = c, c = c->next);
	#endif // BAR_WINTITLEACTIONS_PATCH
	focus(c ? c : p);
	restack(selmon);
}

void
pushstack(const Arg *arg)
{
	int i = stackpos(arg);
	Client *sel = selmon->sel, *c, *p;

	if (i < 0)
		return;
	else if (i == 0) {
		detach(sel);
		attach(sel);
	}
	else {
		for (p = NULL, c = selmon->clients; c; p = c, c = c->next)
			#if BAR_WINTITLEACTIONS_PATCH
			if (!(i -= (ISVISIBLE(c) && !HIDDEN(c) && c != sel)))
			#else
			if (!(i -= (ISVISIBLE(c) && c != sel)))
			#endif // BAR_WINTITLEACTIONS_PATCH
				break;
		c = c ? c : p;
		detach(sel);
		sel->next = c->next;
		c->next = sel;
	}
	arrange(selmon);
}

int
stackpos(const Arg *arg)
{
	int n, i;
	Client *c, *l;

	if (!selmon->clients)
		return -1;

	#if BAR_WINTITLEACTIONS_PATCH
	if (arg->i == PREVSEL) {
		for (l = selmon->stack; l && (!ISVISIBLE(l) || HIDDEN(l) || l == selmon->sel); l = l->snext);
		if (!l)
			return -1;
		for (i = 0, c = selmon->clients; c != l; i += (ISVISIBLE(c) && !HIDDEN(c) ? 1 : 0), c = c->next);
		return i;
	}
	else if (ISINC(arg->i)) {
		if (!selmon->sel)
			return -1;
		for (i = 0, c = selmon->clients; c != selmon->sel; i += (ISVISIBLE(c) && !HIDDEN(c) ? 1 : 0), c = c->next);
		for (n = i; c; n += (ISVISIBLE(c) && !HIDDEN(c) ? 1 : 0), c = c->next);
		return MOD(i + GETINC(arg->i), n);
	}
	else if (arg->i < 0) {
		for (i = 0, c = selmon->clients; c; i += (ISVISIBLE(c) && !HIDDEN(c) ? 1 : 0), c = c->next);
		return MAX(i + arg->i, 0);
	}
	else
		return arg->i;
	#else // !BAR_WINTITLEACTIONS_PATCH
	if (arg->i == PREVSEL) {
		for (l = selmon->stack; l && (!ISVISIBLE(l) || l == selmon->sel); l = l->snext);
		if (!l)
			return -1;
		for (i = 0, c = selmon->clients; c != l; i += (ISVISIBLE(c) ? 1 : 0), c = c->next);
		return i;
	}
	else if (ISINC(arg->i)) {
		if (!selmon->sel)
			return -1;
		for (i = 0, c = selmon->clients; c != selmon->sel; i += (ISVISIBLE(c) ? 1 : 0), c = c->next);
		for (n = i; c; n += (ISVISIBLE(c) ? 1 : 0), c = c->next);
		return MOD(i + GETINC(arg->i), n);
	}
	else if (arg->i < 0) {
		for (i = 0, c = selmon->clients; c; i += (ISVISIBLE(c) ? 1 : 0), c = c->next);
		return MAX(i + arg->i, 0);
	}
	else
		return arg->i;
	#endif // BAR_WINTITLEACTIONS_PATCH
}