-
Notifications
You must be signed in to change notification settings - Fork 0
/
sser.go
237 lines (207 loc) · 5.39 KB
/
sser.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
package main
import (
"fmt"
"image/png"
"log"
"os"
"os/user"
"time"
"xgbutil/ewmh"
"xgbutil/icccm"
"xgbutil/keybind"
"xgbutil/xcursor"
"xgbutil/xwindow"
"github.com/BurntSushi/xgb/xproto"
"github.com/BurntSushi/xgbutil"
"github.com/BurntSushi/xgbutil/mousebind"
"github.com/BurntSushi/xgbutil/xevent"
"github.com/kbinani/screenshot"
)
func main() {
// Connect to the X server using the DISPLAY environment variable.
X, err := xgbutil.NewConn()
check(err)
mousebind.Initialize(X)
keybind.Initialize(X)
initOverlay(X)
//Hotkey for fullscreen
err = keybind.KeyPressFun(
func(X *xgbutil.XUtil, ev xevent.KeyPressEvent) {
if isActive {
return
}
width := int(X.Screen().WidthInPixels)
height := int(X.Screen().HeightInPixels)
var filename string
filename, err = saveImage(0, 0, width, height)
check(err)
log.Println("Saved", filename)
}).Connect(X, X.RootWin(), "Control-Shift-F1", true)
check(err)
//Hotkey for window
err = keybind.KeyPressFun(
func(X *xgbutil.XUtil, ev xevent.KeyPressEvent) {
if isActive {
return
}
changeCursor(X, cameraOV)
fmt.Println("Choose a window")
}).Connect(X, X.RootWin(), "Control-Shift-F2", true)
check(err)
//Setup the hotkey for area
err = keybind.KeyPressFun(
func(X *xgbutil.XUtil, ev xevent.KeyPressEvent) {
if isActive {
return
}
changeCursor(X, crossOV)
log.Println("Ready for region")
}).Connect(X, X.RootWin(), "Control-Shift-F3", true)
check(err)
log.Println("Press ctrl+shift+F4 to take screenshot")
xevent.Main(X)
}
var x0, y0 int
func begin(X *xgbutil.XUtil, rootX, rootY, eventX, eventY int) (bool, xproto.Cursor) {
x0 = eventX
y0 = eventY
log.Println("Starting screenshot")
return true, cross
}
func step(X *xgbutil.XUtil, rootX, rootY, eventX, eventY int) {
//TODO: This is not working :/ Wanted to make a square where the mouse is marking
// startX := x0
// width := eventX - startX
// if eventX < x0 {
// width = x0 - eventX
// startX = eventX
// }
// startY := y0
// height := eventY - startY
// if eventY < y0 {
// height = y0 - eventY
// startY = eventY
// }
// region := ewmh.WmOpaqueRegion{X: startX, Y: startY, Width: uint(width), Height: uint(height)}
// err := ewmh.WmOpaqueRegionSet(X, crossOV.Id, []ewmh.WmOpaqueRegion{region})
// check(err)
}
func end(X *xgbutil.XUtil, rootX, rootY, eventX, eventY int) {
width := eventX - x0
height := eventY - y0
closeCursor()
if width < 10 || height < 10 {
log.Println("Too small...")
} else {
filename, err := saveImage(x0, y0, width, height)
check(err)
log.Println("Saved as ", filename)
}
}
func saveImage(x0, y0, width, height int) (filename string, err error) {
img, err := screenshot.Capture(x0, y0, width, height)
if err != nil {
return
}
t := time.Now()
filename = desktop() + "Screenshot " + t.Format("2006-01-02 15:04:05") + ".png"
file, err := os.Create(filename)
if err != nil {
return
}
defer file.Close()
png.Encode(file, img)
return
}
var crossOV, cameraOV *xwindow.Window
var cross, camera xproto.Cursor
var isActive bool
func initOverlay(X *xgbutil.XUtil) {
var err error
camera, err = xcursor.CreateCursor(X, xcursor.Circle)
check(err)
cross, err = xcursor.CreateCursor(X, xcursor.Crosshair)
check(err)
crossOV, err = xwindow.Generate(X)
check(err)
err = crossOV.CreateChecked(X.RootWin(), 0, 0, 100, 100,
xproto.CwBackPixel|xproto.CwCursor,
0xffffffff, uint32(cross))
check(err)
ewmh.WmNameSet(X, crossOV.Id, "Screenshot")
ewmh.WmWindowOpacitySet(X, crossOV.Id, 0)
//Drag mouse
mousebind.Drag(X, crossOV.Id, crossOV.Id, "1", true, begin, step, end)
//Abort
cb := keybind.KeyPressFun(func(X *xgbutil.XUtil, ev xevent.KeyPressEvent) {
closeCursor()
})
err = cb.Connect(X, crossOV.Id, "Escape", true)
check(err)
//Window shot
cameraOV, err = xwindow.Generate(X)
check(err)
err = cameraOV.CreateChecked(X.RootWin(), 0, 0, 1, 1,
xproto.CwCursor, uint32(camera))
check(err)
ewmh.WmWindowOpacitySet(X, cameraOV.Id, 0)
//CLick binding
err = mousebind.ButtonPressFun(
func(X *xgbutil.XUtil, e xevent.ButtonPressEvent) {
closeCursor()
time.Sleep(time.Millisecond * 100)
xproto.AllowEvents(X.Conn(), xproto.AllowReplayPointer, 0)
time.Sleep(time.Millisecond * 500)
win, _ := ewmh.ActiveWindowGet(X)
fmt.Println(getName(X, win))
g, err2 := xwindow.New(X, win).DecorGeometry()
check(err2)
fmt.Println(g)
}).Connect(X, X.RootWin(), "1", true, true)
cameraOV.Listen(xproto.EventMaskFocusChange)
check(err)
//Abort
cb.Connect(X, cameraOV.Id, "Escape", true)
}
func changeCursor(X *xgbutil.XUtil, win *xwindow.Window) {
if !isActive {
win.Map()
ewmh.WmStateReq(X, win.Id, ewmh.StateToggle, "_NET_WM_STATE_FULLSCREEN")
}
isActive = !isActive
}
func closeCursor() {
if crossOV != nil {
crossOV.Unmap()
}
if cameraOV != nil {
cameraOV.Unmap()
}
isActive = !isActive
}
func desktop() string {
usr, err := user.Current()
if err != nil {
log.Println(err)
}
return usr.HomeDir + "/Desktop/"
}
func getName(X *xgbutil.XUtil, id xproto.Window) string {
name, err := ewmh.WmNameGet(X, id)
// If there was a problem getting _NET_WM_NAME or if its empty,
// try the old-school version.
if err != nil || len(name) == 0 {
name, err = icccm.WmNameGet(X, id)
// If we still can't find anything, give up.
if err != nil || len(name) == 0 {
name = "N/A"
}
}
return name
}
func check(err error) {
if err != nil {
log.Println(err)
closeCursor()
}
}