-
Notifications
You must be signed in to change notification settings - Fork 1
/
regions.go
396 lines (326 loc) · 8.16 KB
/
regions.go
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
/***********************************************************************************
* This file is part of samurai-select
* https://github.com/Samudevv/samurai-select
***********************************************************************************
* Copyright (c) 2023 Jonas Pucher
*
* This software is provided ‘as-is’, without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
*
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
*
* 3. This notice may not be removed or altered from any source
* distribution.
************************************************************************************/
package main
import (
"bufio"
"encoding/json"
"errors"
"os"
"os/exec"
"strconv"
"strings"
samure "github.com/Samudevv/samurai-render-go"
)
type Region struct {
Geo samure.Rect
Name string
}
type Regions interface {
OutputRegions() []Region
CursorPos() (int, int, error)
}
func DetectRegions() Regions {
var stdout strings.Builder
ps := exec.Command("ps", "-e")
ps.Stderr = os.Stderr
ps.Stdout = &stdout
if err := ps.Run(); err != nil {
return nil
}
scanner := bufio.NewScanner(strings.NewReader(stdout.String()))
for scanner.Scan() {
line := scanner.Text()
if strings.HasSuffix(line, "Hyprland") {
return &HyprlandRegions{}
} else if strings.HasSuffix(line, "sway") {
return &SwayRegions{}
}
}
return nil
}
type HyprlandRegions struct {
}
type HyprWorkspace struct {
ID int
Name string
}
type HyprMonitor struct {
ActiveWorkspace HyprWorkspace
}
type HyprClient struct {
At [2]int
Size [2]int
Workspace HyprWorkspace
Floating bool
Title string
}
func (h HyprClient) IsOnScreen(monitors []HyprMonitor) bool {
if !(h.At[0] != 0 || h.At[1] != 0 || h.Size[0] != 0 || h.Size[1] != 0) {
return false
}
for _, m := range monitors {
if m.ActiveWorkspace.ID == h.Workspace.ID {
return true
}
}
return false
}
func (*HyprlandRegions) OutputRegions() (rs []Region) {
hyprctlPath, err := exec.LookPath("hyprctl")
if err != nil {
return
}
var stdout strings.Builder
hyprctl := exec.Command(hyprctlPath, "-j", "clients")
hyprctl.Stderr = os.Stderr
hyprctl.Stdout = &stdout
if err = hyprctl.Run(); err != nil {
return
}
var clients []HyprClient
decoder := json.NewDecoder(strings.NewReader(stdout.String()))
if err = decoder.Decode(&clients); err != nil {
return
}
stdout.Reset()
hyprctl = exec.Command(hyprctlPath, "-j", "monitors")
hyprctl.Stderr = os.Stderr
hyprctl.Stdout = &stdout
if err = hyprctl.Run(); err != nil {
return
}
var monitors []HyprMonitor
decoder = json.NewDecoder(strings.NewReader(stdout.String()))
if err = decoder.Decode(&monitors); err != nil {
return
}
var floatingClients []Region
for _, c := range clients {
if c.IsOnScreen(monitors) {
r := Region{
Geo: samure.Rect{
X: c.At[0],
Y: c.At[1],
W: c.Size[0],
H: c.Size[1],
},
Name: c.Title,
}
if c.Floating {
floatingClients = append(floatingClients, r)
} else {
rs = append(rs, r)
}
}
}
rs = append(floatingClients, rs...)
return
}
func (*HyprlandRegions) CursorPos() (int, int, error) {
hyprctlPath, err := exec.LookPath("hyprctl")
if err != nil {
return 0, 0, err
}
var stdout strings.Builder
hyprctl := exec.Command(hyprctlPath, "cursorpos")
hyprctl.Stderr = os.Stderr
hyprctl.Stdout = &stdout
if err = hyprctl.Run(); err != nil {
return 0, 0, err
}
words := strings.Split(stdout.String(), ",")
if len(words) != 2 {
return 0, 0, errors.New("Invalid output")
}
xStr := strings.TrimSpace(words[0])
yStr := strings.TrimSpace(words[1])
x, err := strconv.ParseUint(xStr, 10, 64)
if err != nil {
return 0, 0, err
}
y, err := strconv.ParseUint(yStr, 10, 64)
if err != nil {
return 0, 0, err
}
return int(x), int(y), nil
}
type SwayRegions struct {
}
type SwayRect struct {
X int
Y int
Width int
Height int
}
type SwayOutput struct {
CurrentWorkspace string `json:"current_workspace"`
}
type SwayNode struct {
Type string
Name string
Rect SwayRect
WindowRect SwayRect `json:"window_rect"`
Nodes []SwayNode
FloatingNodes []SwayNode `json:"floating_nodes"`
}
func (*SwayRegions) OutputRegions() (rs []Region) {
swaymsgPath, err := exec.LookPath("swaymsg")
if err != nil {
return
}
var stdout strings.Builder
swaymsg := exec.Command(swaymsgPath, "--raw", "-t", "get_outputs")
swaymsg.Stdout = &stdout
swaymsg.Stderr = os.Stderr
if err = swaymsg.Run(); err != nil {
return
}
var outputs []SwayOutput
decoder := json.NewDecoder(strings.NewReader(stdout.String()))
if err = decoder.Decode(&outputs); err != nil {
return
}
var currentWorkspaces []string
for _, o := range outputs {
currentWorkspaces = append(currentWorkspaces, o.CurrentWorkspace)
}
stdout.Reset()
swaymsg = exec.Command(swaymsgPath, "--raw", "-t", "get_tree")
swaymsg.Stdout = &stdout
swaymsg.Stderr = os.Stderr
if err = swaymsg.Run(); err != nil {
return
}
decoder = json.NewDecoder(strings.NewReader(stdout.String()))
var tree SwayNode
if err = decoder.Decode(&tree); err != nil {
return
}
swayTreeAddRegions(&rs, tree, currentWorkspaces)
return
}
func (*SwayRegions) CursorPos() (int, int, error) {
return 0, 0, errors.New("not implemented")
}
func swayTreeAddRegions(rs *[]Region, n SwayNode, currentWorkspaces []string) {
if n.Type == "con" || n.Type == "floating_con" {
*rs = append(*rs, Region{
Geo: samure.Rect{
X: n.Rect.X,
Y: n.Rect.Y,
W: n.Rect.Width,
H: n.Rect.Height,
},
Name: n.Name,
})
} else {
if n.Type == "workspace" {
var isIn bool
for _, c := range currentWorkspaces {
if c == n.Name {
isIn = true
break
}
}
if !isIn {
return
}
}
for _, child := range n.FloatingNodes {
swayTreeAddRegions(rs, child, currentWorkspaces)
}
for _, child := range n.Nodes {
swayTreeAddRegions(rs, child, currentWorkspaces)
}
}
}
type ArgumentRegions struct {
regions []Region
}
func (a *ArgumentRegions) OutputRegions() []Region {
if len(a.regions) != 0 {
return a.regions
}
var r Region
r.Name = "nil"
var this string
other := flags.RegionsArgument
for len(other) != 0 {
words := strings.SplitN(other, " ", 2)
if len(words) != 2 {
return a.regions
}
this, other = strings.TrimSpace(words[0]), strings.TrimSpace(words[1])
r.Name = this
words = strings.SplitN(other, ",", 2)
if len(words) != 2 {
return a.regions
}
this, other = strings.TrimSpace(words[0]), strings.TrimSpace(words[1])
x, err := strconv.ParseInt(this, 10, 64)
if err != nil {
return a.regions
}
r.Geo.X = int(x)
words = strings.SplitN(other, " ", 2)
if len(words) != 2 {
return a.regions
}
this, other = strings.TrimSpace(words[0]), strings.TrimSpace(words[1])
y, err := strconv.ParseInt(this, 10, 64)
if err != nil {
return a.regions
}
r.Geo.Y = int(y)
words = strings.SplitN(other, "x", 2)
if len(words) != 2 {
return a.regions
}
this, other = strings.TrimSpace(words[0]), strings.TrimSpace(words[1])
w, err := strconv.ParseUint(this, 10, 64)
if err != nil {
return a.regions
}
r.Geo.W = int(w)
words = strings.SplitN(other, " ", 2)
this = strings.TrimSpace(words[0])
h, err := strconv.ParseUint(this, 10, 64)
if err != nil {
return a.regions
}
r.Geo.H = int(h)
a.regions = append(a.regions, r)
if len(words) == 2 {
other = strings.TrimSpace(words[1])
} else {
return a.regions
}
}
return a.regions
}
func (*ArgumentRegions) CursorPos() (int, int, error) {
return 0, 0, errors.New("not implemented")
}