-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
i3-ws.cpp
303 lines (251 loc) · 8.56 KB
/
i3-ws.cpp
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
#define MAX_WORKSPACES_PER_OUTPUT 10
#include <i3ipc++/ipc.hpp>
#include <algorithm>
#include <cstring>
#include <iostream>
#include <sstream>
using namespace std;
using i3ipc::output_t;
using i3ipc::workspace_t;
using i3ipc::container_t;
using i3ipc::rect_t;
using con_ptr = shared_ptr<container_t>;
using out_ptr = shared_ptr<output_t>;
using ws_ptr = shared_ptr<workspace_t>;
bool compare_rect(rect_t a, rect_t b) {
if (a.x < b.x)
return true;
else if (a.x > b.x)
return false;
else if (a.width < b.width) // a.x == b.x at this point onwards
return true;
else if (a.width > b.width)
return false;
else if (a.y < b.y) // a.width == b.width
return true;
else if (a.y > b.y)
return false;
else if (a.height < b.height) // a.y == b.y
return true;
else if (a.height > b.height)
return false;
else // a.height == a.width (exactly the same)
return true;
}
bool isPosInt(string input) {
string chars = "0123456789";
for (auto i : input) {
bool digit = false;
for (auto c : chars)
if (i == c)
digit = true;
if (!digit)
return false;
}
return true;
}
int strToInt(string input) {
stringstream ss(input);
int a;
ss >> a;
return a;
}
int main(int argc, char* argv[]) {
// handle argv
vector<string> args(argv + 1, argv + argc);
bool valid = true;
bool loop = false;
bool create = false;
bool init = false;
bool shift = false;
int pos = 0; // -2 = next, -1 = prev, 0 = unset; 1-based index onwards
string mode = "out";
for (auto it = args.begin(); it != args.end() && valid; ++it) {
if (*it == "prev" && !pos) {
pos = -1;
} else if (*it == "next" && !pos)
pos = -2;
else if (isPosInt(*it) && !pos) {
stringstream sstream(*it);
sstream >> pos;
} else if (*it == "--ws")
mode = "ws";
else if (*it == "--loop")
loop = true;
else if (*it == "--create")
create = true;
else if (*it == "--init")
init = true;
else if (*it == "--shift")
shift = true;
else
valid = false;
}
if (!init && (!pos || pos > MAX_WORKSPACES_PER_OUTPUT) || init && pos)
valid = false;
// if no direction specified or invalid arguments
if (!valid) {
cout << "Usage: i3-ws [--init]" << endl;
cout << "Usage: i3-ws [--ws] [--shift] {NUMBER}" << endl;
cout << "Usage: i3-ws [--ws] [--shift] [--loop] [--create] (prev|next)";
cout << endl;
return -1;
}
// connect to i3
i3ipc::connection i3;
// get active outputs
auto outputs = i3.get_outputs();
vector<out_ptr> active(outputs.size());
auto it = copy_if(
outputs.begin(),
outputs.end(),
active.begin(),
[](auto output) {
return output->active; });
active.resize(distance(active.begin(), it));
sort(
active.begin(),
active.end(),
[](out_ptr a, out_ptr b) {
return compare_rect(a->rect, b->rect); });
// get focused workspace
auto workspaces = i3.get_workspaces();
auto ws = find_if(
workspaces.begin(),
workspaces.end(),
[](auto workspace) {
return workspace->focused; });
// get focused output
auto current_output = find_if(
active.begin(),
active.end(),
[ws](auto output) {
return output->name == (*ws)->output; });
vector<ws_ptr> current(workspaces.size());
auto end = copy_if(
workspaces.begin(),
workspaces.end(),
current.begin(),
[current_output](auto workspace) {
return workspace->output == (*current_output)->name; });
if (init) {
for (auto it = active.begin(); it != active.end(); ++it) {
string ws_name;
stringstream ss;
ss << (distance(active.begin(), it) + 1) * 100 + 1;
ss >> ws_name;
// get focused workspace
workspaces = i3.get_workspaces();
ws = find_if(
workspaces.begin(),
workspaces.end(),
[](auto workspace) {
return workspace->focused; });
cout << ws_name << " " << (*it)->name << endl;
if ((*it)->current_workspace != ws_name) {
if ((*it)->current_workspace != (*ws)->name) {
cout << "Switching from " << (*ws)->name << endl;
cout << "Switching to " << (*it)->current_workspace << endl;
i3.send_command("workspace " + (*it)->current_workspace);
}
cout << "Switching to " << ws_name << endl;
i3.send_command("workspace " + ws_name);
}
}
return 0;
}
current.resize(distance(current.begin(), end));
string command = "workspace ";
if (shift)
command = "move window to workspace ";
// switch outputs
if (active.size() > 1 && mode == "out") {
auto current_output = find_if(
active.begin(),
active.end(),
[ws](auto output) {
return output->name == (*ws)->output; });
auto active_index = distance(active.begin(), current_output);
if (pos > 0 && pos - 1 != active_index)
active_index = pos - 1;
else if (pos == -1)
active_index = active_index - 1;
else if (pos == -2)
active_index = active_index + 1;
else
return 0;
if (loop) {
active_index = (active_index + active.size()) % active.size();
} else if (active_index < 0 || active_index >= active.size()) {
return 0;
}
cout << active_index << endl;
auto n = active[active_index];
cout << n->name << " " << n->current_workspace << endl;
i3.send_command(command + n->current_workspace);
// if moving a window to a new output, this will be on a visible
// workspace, so focus that workspace after moving the window there
if (shift)
i3.send_command("workspace " + n->current_workspace);
} else if (mode == "ws") {
// switch (or create) workspasces :)
auto cur = find(current.begin(), current.end(), *ws);
cout << (*cur)->name << endl;
int ws_num = strToInt((*cur)->name) % 100;
vector<int> ws_nums;
vector<int>::iterator new_ws;
// ws_nums = {1, 2, 3 .. MAX_WORKSPACES_PER_OUTPUT}
for (int i = 1; i <= MAX_WORKSPACES_PER_OUTPUT; i++)
ws_nums.push_back(i);
// -2 = next, -1 = prev, 0 = unset; 1-based index onwards
if (pos == -2 || pos == -1) {
// loop should only affect the edges (if you're on 1 or 10)
// create should affect if you're going to jump to the next ws_num
if (create) {
new_ws = ws_nums.begin() + strToInt((*ws)->name) % 100 - 1;
} else {
ws_nums = {};
for (auto &ws : current)
ws_nums.push_back(strToInt(ws->name) % 100);
new_ws = ws_nums.begin() + distance(current.begin(), cur);
}
}
// -2 = next, -1 = prev, 0 = unset; 1-based index onwards
if (pos > 0) {
// pos starts at 1, but items start from iter+0
new_ws = ws_nums.begin() + pos - 1;
}
else if (pos == -1) {
// if not first!
if (new_ws != ws_nums.begin()) {
new_ws -= 1;
} else if (loop) {
// this implies *new_ws == ws_nums[0]
new_ws = ws_nums.end() - 1;
} else {
cout << "At the first workspace, use --loop" << endl;
return 0;
}
}
else if (pos == -2) {
// if not last!
if (new_ws != ws_nums.end() - 1) {
new_ws += 1;
} else if (loop) {
// this implies *new_ws == ws_nums[-1]
new_ws = ws_nums.begin();
} else {
cout << "At the last workspace, use --loop" << endl;
return 0;
}
}
string new_ws_name;
stringstream new_wss;
new_wss << ((distance(active.begin(), current_output) + 1) * 100
+ *new_ws);
new_wss >> new_ws_name;
if (new_ws_name != (*cur)->name)
i3.send_command(command + new_ws_name);
}
return 0;
}