summaryrefslogtreecommitdiff
path: root/patch/inplacerotate.c
blob: b700e04a73adacadc87db9c3e9655dabdbfc1422 (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
void
insertclient(Client *item, Client *insertItem, int after)
{
	Client *c;
	if (item == NULL || insertItem == NULL || item == insertItem)
		return;
	detach(insertItem);
	if (!after && selmon->clients == item) {
		attach(insertItem);
		return;
	}
	if (after) {
		c = item;
	} else {
		for (c = selmon->clients; c; c = c->next) {
			if (c->next == item)
				break;
		}
	}
	insertItem->next = c->next;
	c->next = insertItem;
}

void
inplacerotate(const Arg *arg)
{
	if (!selmon->sel || (selmon->sel->isfloating && !arg->f))
		return;

	unsigned int selidx = 0, i = 0;
	Client *c = NULL, *stail = NULL, *mhead = NULL, *mtail = NULL, *shead = NULL;

	// Determine positionings for insertclient
	for (c = selmon->clients; c; c = c->next) {
		if (ISVISIBLE(c) && !(c->isfloating)) {
			if (selmon->sel == c)
				selidx = i;
			if (i == selmon->nmaster - 1)
				mtail = c;
			if (i == selmon->nmaster)
				shead = c;
			if (mhead == NULL)
				mhead = c;
			stail = c;
			i++;
		}
	}

	switch(arg->i) {
	case 1:
		if (selidx >= selmon->nmaster)
			insertclient(shead, stail, 0);
		else
			insertclient(mhead, mtail, 0);
		break;
	case -1:
		if (selidx >= selmon->nmaster)
			insertclient(stail, shead, 1);
		else
			insertclient(mtail, mhead, 1);
		break;
	case 2:
		insertclient(selmon->clients, stail, 0);
		break;
	case -2:
		insertclient(stail, selmon->clients, 1);
		break;
	}

	// Restore focus position
	i = 0;
	for (c = selmon->clients; c; c = c->next) {
		if (!ISVISIBLE(c) || (c->isfloating))
			continue;
		if (i == selidx) {
			focus(c);
			break;
		}
		i++;
	}
	arrange(selmon);
	focus(c);
}