-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathwindow.go
209 lines (189 loc) · 4.79 KB
/
window.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
package main
// THIS IS AN AUTOGENERATED FILE; DO NOT EDIT
import (
"fmt"
"github.com/BurntSushi/xgb/xinerama"
"github.com/BurntSushi/xgb/xproto"
"log"
"sync"
)
type ManagedWindow struct {
xproto.Window
SizeDelta int
}
type Column struct {
Windows []ManagedWindow
SizeDelta int
}
type Workspace struct {
Screen *xinerama.ScreenInfo
columns []Column
maximizedWindow *xproto.Window
mu *sync.Mutex
}
var workspaces map[string]*Workspace
var activeWindow *xproto.Window
func (w *Workspace) Add(win xproto.Window) error {
// Ensure that we can manage this window.
if err := xproto.ConfigureWindowChecked(
xc,
win,
xproto.ConfigWindowBorderWidth,
[]uint32{
2,
}).Check(); err != nil {
return err
}
// Get notifications when this window is deleted.
if err := xproto.ChangeWindowAttributesChecked(
xc,
win,
xproto.CwEventMask,
[]uint32{
xproto.EventMaskStructureNotify |
xproto.EventMaskEnterWindow,
},
).Check(); err != nil {
return err
}
w.mu.Lock()
defer w.mu.Unlock()
switch len(w.columns) {
case 0:
w.columns = []Column{
Column{Windows: []ManagedWindow{ManagedWindow{win, 0}}, SizeDelta: 0},
}
default:
// Add to the first empty column we can find, and shortcircuit out
// if applicable.
for i, c := range w.columns {
if len(c.Windows) == 0 {
w.columns[i].Windows = append(w.columns[i].Windows, ManagedWindow{win, 0})
return nil
}
}
// No empty columns, add to the last one.
i := len(w.columns) - 1
w.columns[i].Windows = append(w.columns[i].Windows, ManagedWindow{win, 0})
}
return nil
}
// TileWindows tiles all the windows of the workspace into the screen that
// the workspace is attached to.
func (w *Workspace) TileWindows() error {
if w.Screen == nil {
return fmt.Errorf("Workspace not attached to a screen.")
}
if w.maximizedWindow != nil {
return xproto.ConfigureWindowChecked(
xc,
*w.maximizedWindow,
xproto.ConfigWindowX|
xproto.ConfigWindowY|
xproto.ConfigWindowWidth|
xproto.ConfigWindowHeight|
xproto.ConfigWindowBorderWidth|
xproto.ConfigWindowStackMode,
[]uint32{
0,
0,
uint32(w.Screen.Width),
uint32(w.Screen.Height),
0,
xproto.StackModeAbove,
},
).Check()
}
n := uint32(len(w.columns))
if n == 0 {
return fmt.Errorf("No columns to tile")
}
var totalDeltas int
for _, c := range w.columns {
totalDeltas += c.SizeDelta
}
size := uint32(int(w.Screen.Width)-totalDeltas) / n
var err error
// Keep track of the already incorporated deltas, to add to xstart
// for the column.TileWindow call
usedDeltas := 0
prevWin := activeWindow
for i, c := range w.columns {
if err != nil {
// Don't overwrite err if there's an error, but still
// tile the rest of the columns instead of returning.
c.TileColumn(uint32((i*int(size))+usedDeltas), uint32(int(size)+c.SizeDelta), uint32(w.Screen.Height))
} else {
err = c.TileColumn(uint32((i*int(size))+usedDeltas), uint32(int(size)+c.SizeDelta), uint32(w.Screen.Height))
}
usedDeltas += c.SizeDelta
}
if prevWin != nil {
if err := xproto.WarpPointerChecked(xc, 0, *prevWin, 0, 0, 0, 0, 10, 10).Check(); err != nil {
log.Print(err)
}
}
return err
}
// TileColumn sends ConfigureWindow messages to tile the ManagedWindows
// Using the geometry of the parameters passed
func (c Column) TileColumn(xstart, colwidth, colheight uint32) error {
n := uint32(len(c.Windows))
if n == 0 {
return nil
}
var totalDeltas int
for _, win := range c.Windows {
totalDeltas += win.SizeDelta
}
heightBase := (int(colheight) - totalDeltas) / int(n)
usedDeltas := 0
var err error
for i, win := range c.Windows {
if werr := xproto.ConfigureWindowChecked(
xc,
win.Window,
xproto.ConfigWindowX|
xproto.ConfigWindowY|
xproto.ConfigWindowWidth|
xproto.ConfigWindowHeight,
[]uint32{
xstart,
uint32((i * heightBase) + usedDeltas),
colwidth,
uint32(heightBase + win.SizeDelta),
}).Check(); werr != nil {
err = werr
}
usedDeltas += win.SizeDelta
}
return err
}
// RemoveWindow removes a window from the workspace. It returns
// an error if the window is not being managed by w.
func (wp *Workspace) RemoveWindow(w xproto.Window) error {
wp.mu.Lock()
defer wp.mu.Unlock()
for colnum, column := range wp.columns {
idx := -1
for i, candwin := range column.Windows {
if w == candwin.Window {
idx = i
break
}
}
if idx != -1 {
// Found the window at at idx, so delete it and return.
// (I wish Go made it easier to delete from a slice.)
wp.columns[colnum].Windows = append(column.Windows[0:idx], column.Windows[idx+1:]...)
if wp.maximizedWindow != nil && w == *wp.maximizedWindow {
wp.maximizedWindow = nil
}
return nil
}
}
return fmt.Errorf("Window not managed by workspace")
}
func (w *ManagedWindow) Resize(delta int) {
w.SizeDelta += delta
}