-
Notifications
You must be signed in to change notification settings - Fork 19
/
client.go
160 lines (129 loc) · 3.32 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
package main
import (
"strings"
"github.com/BurntSushi/xgb/xproto"
"github.com/BurntSushi/xgbutil/ewmh"
"github.com/BurntSushi/xgbutil/icccm"
"github.com/BurntSushi/xgbutil/motif"
"github.com/BurntSushi/xgbutil/xrect"
"github.com/BurntSushi/xgbutil/xwindow"
"github.com/blrsn/zentile/state"
log "github.com/sirupsen/logrus"
)
type Client struct {
window *xwindow.Window
Desk uint // Desktop the client is currently in.
savedProp Prop // Properties that the client had, before it was tiled.
}
type Prop struct {
Geom xrect.Rect
decoration bool
}
func newClient(w xproto.Window) (c Client) {
win := xwindow.New(state.X, w)
desk, err := ewmh.WmDesktopGet(state.X, w)
if err != nil {
desk = state.CurrentDesk
}
savedGeom, err := win.DecorGeometry()
if err != nil {
log.Info(err)
}
c = Client{
window: win,
Desk: desk,
savedProp: Prop{
Geom: savedGeom,
decoration: hasDecoration(w),
},
}
return c
}
func (c Client) name() string {
name, err := ewmh.WmNameGet(state.X, c.window.Id)
if err != nil {
return ""
}
return name
}
func (c Client) MoveResize(x, y, width, height int) {
c.Unmaximize()
dw, dh := c.DecorDimensions()
err := c.window.WMMoveResize(x, y, width-dw, height-dh)
if err != nil {
log.Info("Error when moving ", c.name(), " ", err)
}
}
// DecorDimensions returns the width and height occupied by window decorations
func (c Client) DecorDimensions() (width int, height int) {
cGeom, err1 := xwindow.RawGeometry(state.X, xproto.Drawable(c.window.Id))
pGeom, err2 := c.window.DecorGeometry()
if err1 != nil || err2 != nil {
return
}
width = pGeom.Width() - cGeom.Width()
height = pGeom.Height() - cGeom.Height()
return
}
func (c Client) Unmaximize() {
ewmh.WmStateReq(state.X, c.window.Id, 0, "_NET_WM_STATE_MAXIMIZED_VERT")
ewmh.WmStateReq(state.X, c.window.Id, 0, "_NET_WM_STATE_MAXIMIZED_HORZ")
}
func (c Client) UnDecorate() {
motif.WmHintsSet(state.X, c.window.Id,
&motif.Hints{
Flags: motif.HintDecorations,
Decoration: motif.DecorationNone,
})
}
func (c Client) Decorate() {
if !c.savedProp.decoration {
return
}
motif.WmHintsSet(state.X, c.window.Id,
&motif.Hints{
Flags: motif.HintDecorations,
Decoration: motif.DecorationAll,
})
}
// Restore resizes and decorates window to pre-tiling state.
func (c Client) Restore() {
c.Decorate()
geom := c.savedProp.Geom
log.Info("Restoring ", c.name(), ": ", "X: ", geom.X(), " Y: ", geom.Y())
c.MoveResize(geom.X(), geom.Y(), geom.Width(), geom.Height())
}
// Activate makes the client the currently active window
func (c Client) Activate() {
ewmh.ActiveWindowReq(state.X, c.window.Id)
}
// hasDecoration returns true if the window has client decorations.
func hasDecoration(wid xproto.Window) bool {
mh, err := motif.WmHintsGet(state.X, wid)
if err != nil {
return true
}
return motif.Decor(mh)
}
// isHidden returns true if the window has been minimized.
func isHidden(w xproto.Window) bool {
states, _ := ewmh.WmStateGet(state.X, w)
for _, state := range states {
if state == "_NET_WM_STATE_HIDDEN" {
return true
}
}
return false
}
func shouldIgnore(w xproto.Window) bool {
c, err := icccm.WmClassGet(state.X, w)
if err != nil {
log.Warn(err)
}
for _, s := range Config.WindowsToIgnore {
if strings.EqualFold(c.Class, s) {
return true
}
}
return false
}