/
client.go
526 lines (468 loc) · 10.1 KB
/
client.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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
package clients
import (
"github.com/BurntSushi/xgb"
"github.com/BurntSushi/xgb/xproto"
"github.com/thrisp/scpwm/euclid/rules"
"github.com/thrisp/scpwm/euclid/window"
)
type Client interface {
Class() string
Instance() string
Rectangle() xproto.Rectangle
Adjacent(Client, string) bool
SideHandle(string) *xproto.Point
window.Window
Floatr
Tilr
State
Constraint
Bordr
Shiftr
Stackr
}
type client struct {
class string
instance string
window.Window
Stackr
Floatr
Tilr
State
Constraint
Bordr
Shiftr
}
func NewClient(c *xgb.Conn, x xproto.Window, r xproto.Window, csq *rules.Consequence) Client {
cl := &client{
class: csq.Class,
instance: csq.Instance,
Window: window.New(c, x, r),
Stackr: newStackr(),
Floatr: newFloatr(),
Tilr: newTilr(),
State: newState(csq),
Constraint: newConstraint(csq),
Bordr: newBordr(),
Shiftr: newShiftr(),
}
return cl
}
func (c *client) Class() string {
return c.class
}
func (c *client) Instance() string {
return c.instance
}
func (c *client) Rectangle() xproto.Rectangle {
if c.Tiled() {
return c.TRectangle()
}
return c.FRectangle()
}
func (c *client) Adjacent(o Client, direction string) bool {
cr := c.Rectangle()
or := o.Rectangle()
switch direction {
case "right":
return (cr.X + int16(cr.Width)) == or.X
case "down":
return (cr.Y + int16(cr.Height)) == or.Y
case "left":
return (or.X + int16(or.Width)) == cr.X
case "up":
return (or.Y + int16(or.Height)) == cr.Y
}
return false
}
func (c *client) SideHandle(direction string) *xproto.Point {
rect := c.Rectangle()
var p xproto.Point
switch direction {
case "right":
p.X = rect.X + int16(rect.Width)
p.Y = rect.Y + (int16(rect.Height) / 2)
case "down":
p.X = rect.X + (int16(rect.Width) / 2)
p.Y = rect.Y + int16(rect.Height)
case "left":
p.X = rect.X
p.Y = rect.Y + (int16(rect.Height) / 2)
case "up":
p.X = rect.X + (int16(rect.Width) / 2)
p.Y = rect.Y
}
return &p
}
/*
type clientType int
const (
ctAll clientType = iota
ctFloating
ctTiled
)
var stringClientType map[string]clientType = map[string]clientType{
"all": ctAll,
"floating": ctFloating,
"tiled": ctTiled,
}
type clientClass int
const (
ccAll clientClass = iota
ccEqual
ccDiffer
)
var stringClientClass map[string]clientClass = map[string]clientClass{
"all": ccAll,
"equal": ccEqual,
"differ": ccDiffer,
}
type clientMode int
const (
cmAll clientMode = iota
cmAutomatic
cmManual
)
var stringClientMode map[string]clientMode = map[string]clientMode{
"all": cmAll,
"automatic": cmAutomatic,
"manual": cmManual,
}
//func selectClient(e *Euclid, sel ...Selector) bool {
// return false
//}
/*
func (n *Node) fence(dir Direction) *Node {
/*if n != nil {
t := n.top
for t != nil {
if (dir == Up && t.splitT == Horizontal && t.Rectangle.Y < n.Rectangle.Y) ||
(dir == Left && t.splitT == Vertical && t.Rectangle.X < n.Rectangle.X) ||
(dir == Down && t.splitT == Horizontal && (t.Rectangle.Y+int16(t.Rectangle.Height)) > (n.Rectangle.Y+int16(n.Rectangle.Height))) ||
(dir == Right && t.splitT == Vertical && (t.Rectangle.X+int16(t.Rectangle.Width)) > (n.Rectangle.X+int16(n.Rectangle.Width))) {
return t
}
t = t.top
}
}
return nil
}
type Node struct {
idx int
loc coordinate
focus bool
next *Node
prev *Node
*xproto.Rectangle
*Client
*shift
}
func newNode(root *Node, idx int) *Node {
n := &Node{
idx: idx,
shift: &shift{},
}
loc := root.loc
loc.n = n
n.loc = loc
return n
}
func (n *Node) peek(cy Cycle) *Node {
var ret *Node
switch cy {
case Next:
ret = n.next
case Prev:
ret = n.prev
}
return ret
}
func (n *Node) pop(cy Cycle) *Node {
ret := n.peek(cy)
if ret.idx != 0 {
ret.detach()
return ret
}
return nil
}
func (n *Node) detach() {
if n.idx != 0 {
next = n.next
prev = n.prev
if next != nil {
next.prev = prev
}
if prev != nil {
prev.next = next
}
}
}
func (n *Node) push(o *Node, cy Cycle) {
switch cy {
case Next:
if n.next != nil {
nxt = n.next
o.prev = n
o.nxt = nxt
n.next = o
} else {
n.next = o
}
case Prev:
//
}
}
func (n *Node) closest(cy Cycle, sel ClientSelect) *Node {
/*curr := n.peek(cy)
if curr != nil {
ref := n.loc
for curr != n {
loc := n.loc
loc.n = curr
if MatchNode(&loc, &ref, sel) {
return curr
}
curr = curr.peek(cy)
}
}
return nil
}
func (n *Node) applyLayout(r, rr xproto.Rectangle) {}
func (n *Node) destruct() {
if n != nil {
t1 := n.right
t2 := n.left
if n.Client != nil {
n.Client = nil
NumClients--
}
n = nil
t1.destruct()
t2.destruct()
}
}
//func (n *Node) focus() {}
func (n *Node) pseudoFocus() {}
func (n *Node) neighbor(dir Direction, sel ClientSelect) *Node {
/*if n != nil || !n.Client.fullscreen || (n.loc.d.layout != monocle && !n.IsTiled()) {
var ret *Node
if n.loc.e.Bool("HistoryAwareFocus") {
ret = n.neighborHistory(dir, sel)
}
if ret == nil {
if n.loc.e.Bool("FocusByDistance") {
ret = n.neighborDistance(dir, sel)
} else {
ret = n.neighborTree(dir, sel)
}
}
return ret
}
return nil
}
func (n *Node) neighborTree(dir Direction, sel ClientSelect) *Node {
/*if n != nil {
fence := n.fence(dir)
if fence != nil {
var nearest *Node
if dir == Up || dir == Left {
nearest = fence.right.leftExtrema()
} else if dir == Down || dir == Right {
nearest = fence.left.rightExtrema()
}
ref, loc := n.loc, coordinates(n.loc.e, n.loc.m, n.loc.d, nearest)
if MatchNode(&loc, &ref, sel) {
return nearest
} else {
return nil
}
}
}
return nil
}
func (n *Node) neighborHistory(dir Direction, sel ClientSelect) *Node {
/*if n != nil || n.IsTiled() {
target := n.fence(dir)
if target != nil {
switch dir {
case Up, Left:
target = target.right
case Down, Right:
target = target.left
}
var nearest *Node
//int min_rank = INT_MAX;
ref := n.loc
a := target.rightExtrema()
for a != nil {
if !a.vacant || n.isAdjacent(a, dir) || a != n {
//loc := coordinates(n.e, m, d, a)
//if NodeMatches(loc, ref, sel) {
//int rank = history_rank(d, a);
//if (rank >= 0 && rank < min_rank) {
// nearest = a;
// min_rank = rank;
//}
}
a = nextLeaf(a, target)
}
return nearest
}
}
return nil
}
func (n *Node) neighborDistance(dir Direction, sel ClientSelect) *Node {
/*if n != nil {
var target *Node
if n.IsTiled() {
target = n.fence(dir)
if target == nil {
return nil
}
if dir == Up || dir == Left {
target = target.right
} else if dir == Down || dir == Right {
target = target.left
}
} else {
//target = d.root
}
var nearest *Node
var dir2 Direction
var p1, p2 xproto.Point
//n.getSideHandle(dir, &p1)
opposite(dir, dir2)
//n.getSideHandle(dir, &p1)
opposite(dir, dir2)
var ds float64 //double ds = DBL_MAX;
ref := n.loc
a := target.rightExtrema()
for a != nil {
l := ref
l.n = a
loc := l //coordinates(m, d, a)
if a != n || MatchNode(&loc, &ref, sel) || a.IsTiled() == n.IsTiled() || (a.IsTiled() && n.isAdjacent(a, dir)) {
// a.getSideHandle(dir2, &p2)
// ds2 := distance(p1, p2)
// if ds2 < ds {
// ds = ds2
// nearest = a
// }
}
a := nextLeaf(a, target)
}
return nearest
}
return nil
}
func setVacant(n *Node) {
curr := n
for curr != nil {
curr.vacant = (curr.right.vacant && curr.left.vacant)
curr = curr.top
}
}
func setPrivacy(n *Node, as bool) {
var v int
if as {
v = 1
} else {
v = -1
}
curr := n
for curr != nil {
curr.privacy += v
curr = curr.top
}
}
func NodeFromDescription(desc []string, ref, dst *coordinate) bool {
/*
sel := clientselectFromString(desc)
dst.m = ref.m
dst.d = ref.d
dst.n = nil
var dir Direction
var cy Cycle
var hdir Age
if directionFromString(desc, dir) {
//dst->node = nearest_neighbor(ref->Monitor, ref->Desktop, ref->node, dir, sel);
//if (dst->node == NULL && num_Monitors > 1) {
// Monitor_t *m = nearest_Monitor(ref->Monitor, dir, (Desktop_select_t) {DESKTOP_STATUS_ALL, false, false});
// if (m != NULL) {
// coordinates_t loc = {m, m->desk, m->desk->focus};
// if (node_matches(&loc, ref, sel)) {
// dst->Monitor = m;
// dst->Desktop = m->desk;
// dst->node = m->desk->focus;
// }
// }
//}
} else if cycleFromString(desc, cy) {
//dst->node = closest_node(ref->Monitor, ref->Desktop, ref->node, cy, sel);
} else if ageFromString(desc, hdir) {
//history_find_node(hdir, ref, dst, sel);
} else if strings.Contains(desc, "last") {
// history_find_node(HISTORY_OLDER, ref, dst, sel);
} else if strings.Contains(desc, "biggest") {
// dst->node = find_biggest(ref->Monitor, ref->Desktop, ref->node, sel);
} else if strings.Contains(desc, "focused") {
// coordinates_t loc = {mon, mon->desk, mon->desk->focus};
// if (node_matches(&loc, ref, sel)) {
// dst->Monitor = mon;
// dst->Desktop = mon->desk;
// dst->node = mon->desk->focus;
// }
} else {
// long int wid;
// if (parse_window_id(desc, &wid))
// locate_window(wid, dst);
}
return dst.n != nil
return false
}
func MatchNode(loc, ref *coordinate, sel ClientSelect) bool {
if loc.n != nil {
}
return false
}
/*
type Nodes struct {
count int
*Node
}
func NewNodes(e *Euclid, m *Monitor, d *Desktop) *Nodes {
n := &Node{idx: 0}
n.loc = coordinates(e, m, d, n)
return &Nodes{
Node: n,
}
}
func (ns *Nodes) New() *Node {
ns.count++
return newNode(ns.Node, ns.count)
}
func (ns *Nodes) Biggest(sel ClientSelect) *Node {
return nil
}
func (ns *Nodes) Circulate(cy Cycle) {}
//func (ns *Nodes) Swap(n1, n2 *Node) bool {
// return false
//}
//func SwapNodes(ns1, ns2 *Nodes) bool {
// return false
//}
*/
/*
func (c *coordinate) resetMode() {
if c.n != nil {
c.n.splitM = Automatic
//window_draw_border(loc->Node, loc->Desktop->focus == loc->Node, mon == loc->Monitor);
} else if c.d != nil {
a := c.d.root.rightExtrema()
for a != nil {
a.splitM = Automatic
//window_draw_border(a, loc->Desktop->focus == a, mon == loc->Monitor);
a = nextLeaf(a, c.d.root)
}
}
}
*/