-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
430 lines (366 loc) · 8.41 KB
/
main.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
package main
import (
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"os/exec"
"path/filepath"
"regexp"
"strconv"
"strings"
"time"
"github.com/BurntSushi/xgb/xproto"
"github.com/BurntSushi/xgbutil"
"github.com/BurntSushi/xgbutil/ewmh"
"github.com/awused/awconf"
"github.com/gen2brain/beeep"
)
type override struct {
Name string
Regex string
Format string
Yearly bool
Monthly bool
Delegate string
Callback string
}
type config struct {
ScreenshotDir string
Fallback string
YearlyApplications *[]string
Overrides []override
IgnoredParents []string
CheckWindowID bool
Compression int
SlopShaders []string
Callback string
MouseKeys bool
}
type application struct {
dir string
yearly bool
monthly bool
callback string
}
const (
no int = iota
yes
maybe
)
var stderr = log.New(os.Stderr, "", 0)
// If _any_ have the right window ID then we must only match against those
var hasWindowID = maybe
var xu *xgbutil.XUtil
var c *config
var appChan = make(chan application)
var errorChan = make(chan error)
var delegateEnvironment = make(map[string]string)
var mode = ""
var debug = false
// TODO -- urfave/cli instead of this mess
func main() {
flag.BoolVar(&debug, "debug", false, "Enable debugging output")
flag.Parse()
if flag.NArg() == 0 {
log.Fatal("Specify mode in [window, region, desktop]")
}
err := awconf.LoadConfig("screenshotter", &c)
if err != nil {
log.Fatal(err)
}
if c.YearlyApplications != nil {
_ = beeep.Notify(
"Screenshotter Error",
"YearlyApplications has been removed in favour of more flexible "+
"Override settings. Update your config.", "")
os.Exit(1)
}
if !c.CheckWindowID {
hasWindowID = no
}
// It'd be slightly faster to connect maim and imagemagick using pipes but
// not worth the complexity.
tmpBMP, tmpPNG := mkTemp()
screenshotArgs := []string{
"--capturebackground",
"--hidecursor",
"--quality", "1", // Slow, but as good as we can manage
}
defer os.Remove(tmpBMP)
defer os.Remove(tmpPNG)
initXConn()
mode = flag.Arg(0)
delegateEnvironment["SCREENSHOTTER_MODE"] = mode
switch mode {
case "window":
wid, err := ewmh.ActiveWindowGet(xu)
if err != nil {
errorChan <- err
return
}
go getActiveWindowApplication(wid)
screenshotArgs = append(
screenshotArgs,
"--window", strconv.Itoa(int(wid)),
)
case "desktop":
go getDesktopApplicationName()
case "region":
geometry := selectRegion()
if geometry == "" {
// Slop was cancelled
return
}
delegateEnvironment["SCREENSHOTTER_GEOMETRY"] = geometry
go getMouseWindowApplication()
screenshotArgs = append(
screenshotArgs,
"--geometry", geometry,
)
case "name":
debug = true // name implies debug
// TODO -- get window name at mousedown and compare to mouseup
geometry := selectRegion()
if geometry == "" {
// Slop was cancelled
return
}
delegateEnvironment["SCREENSHOTTER_GEOMETRY"] = geometry
go getMouseWindowApplication()
select {
case app := <-appChan:
fname := getFileName(app)
stderr.Println("Application directory: " + app.dir)
stderr.Println("File name: " + fname)
if c.Callback != "" {
stderr.Printf("Would call callback [%s] with environment:\n", c.Callback)
for k, v := range delegateEnvironment {
stderr.Println(k + "=" + v)
}
}
err = beeep.Notify("Application Directory", app.dir, "")
if err != nil {
panic(err)
}
fmt.Println(fname)
case err = <-errorChan:
panic(err)
}
return
case "window-name":
debug = true // name implies debug
wid, err := strconv.ParseUint(flag.Arg(1), 10, 32)
if err != nil {
panic(err)
}
go getActiveWindowApplication(xproto.Window(uint32(wid)))
select {
case app := <-appChan:
fname := getFileName(app)
stderr.Println("Application directory: " + app.dir)
stderr.Println("File name: " + fname)
if c.Callback != "" {
stderr.Printf("Would call callback [%s] with environment:\n", c.Callback)
for k, v := range delegateEnvironment {
stderr.Println(k + "=" + v)
}
}
err = beeep.Notify("Application Directory", app.dir, "")
if err != nil {
panic(err)
}
fmt.Println(fname)
case err = <-errorChan:
panic(err)
}
return
default:
stderr.Println("Specify mode in [window, region, desktop, name]")
return
}
screenshotArgs = append(screenshotArgs, tmpBMP)
err = exec.Command("maim", screenshotArgs...).Run()
if err != nil {
panic(err)
}
convertArgs := []string{
tmpBMP,
"-background", "black",
"-alpha", "off",
"-define", "png:compression-level=" + strconv.Itoa(c.Compression),
tmpPNG,
}
err = exec.Command("convert", convertArgs...).Run()
if err != nil {
panic(err)
}
var app application
select {
case app = <-appChan:
case err = <-errorChan:
panic(err)
}
if app.dir == "" {
panic("Application name can't be empty, check your settings and overrides")
}
outFile := getFileName(app)
if debug {
stderr.Println("Application directory: " + app.dir)
stderr.Println("File name: " + outFile)
}
err = os.MkdirAll(filepath.Dir(outFile), 0777)
if err != nil {
panic(err)
}
err = moveFile(tmpPNG, outFile)
if err != nil {
panic(err)
}
if app.callback != "" {
if debug {
stderr.Printf("Calling override callback [%s] with environment:\n",
app.callback)
}
cmd := exec.Command(app.callback, outFile)
cmd.Env = os.Environ()
for k, v := range delegateEnvironment {
if debug {
stderr.Println(k + "=" + v)
}
cmd.Env = append(cmd.Env, k+"="+v)
}
err = cmd.Run()
if err != nil {
stderr.Printf("Override callback [%s] failed: %s\n", app.callback, err)
}
}
if c.Callback != "" {
if debug {
stderr.Printf("Calling callback [%s] with environment:\n", c.Callback)
}
cmd := exec.Command(c.Callback, outFile)
cmd.Env = os.Environ()
for k, v := range delegateEnvironment {
if debug {
stderr.Println(k + "=" + v)
}
cmd.Env = append(cmd.Env, k+"="+v)
}
err = cmd.Run()
if err != nil {
stderr.Printf("Callback [%s] failed: %s\n", c.Callback, err)
}
}
partPath := strings.TrimPrefix(outFile, c.ScreenshotDir)
err = beeep.Notify("Screenshot Taken", partPath, outFile)
if err != nil {
panic(err)
}
}
func getFileName(app application) string {
path := filepath.Join(c.ScreenshotDir, app.dir)
d := time.Now()
if app.monthly {
if app.yearly {
return filepath.Join(
path,
d.Format("2006"),
d.Format("01"),
d.Format("02_15-04-05")+".png")
}
return filepath.Join(
path,
d.Format("2006-01"),
d.Format("02_15-04-05")+".png")
} else if app.yearly {
return filepath.Join(
path,
d.Format("2006"),
d.Format("01-02_15-04-05")+".png")
}
return filepath.Join(
path,
d.Format("2006-01-02_15-04-05")+".png")
}
func initXConn() {
if xu == nil {
X, err := xgbutil.NewConn()
if err != nil {
panic(err)
}
xu = X
}
}
func mkTemp() (string, string) {
f, err := ioutil.TempFile("", "screenshotter*.bmp")
if err != nil {
panic(err)
}
err = f.Close()
if err != nil {
panic(err)
}
tmpBMP := f.Name()
// Remove in Go 1.11
if filepath.Ext(tmpBMP) != ".bmp" {
panic("Screenshotter requires Go 1.11 or higher")
}
f, err = ioutil.TempFile("", "screenshotter*.png")
if err != nil {
panic(err)
}
err = f.Close()
if err != nil {
panic(err)
}
tmpPNG := f.Name()
return tmpBMP, tmpPNG
}
// Slop can give us window IDs but Slop will always give the root window for
// area selections, which is undesirable
func selectRegion() string {
if c.MouseKeys {
xsetOut, err := exec.Command("xset", "q").Output()
if err != nil {
return ""
}
disabled, err := regexp.Match("Mouse Keys: +off", xsetOut)
if disabled {
err = exec.Command("xset", "led", "named", "Mouse Keys").Run()
if err != nil {
return ""
}
defer exec.Command("xset", "-led", "named", "Mouse Keys").Run()
}
}
slopArgs := []string{
"-n",
"-f", "%g",
"-l",
"-c", "0,1,1,0.3",
}
if len(c.SlopShaders) > 0 {
slopArgs = append(
slopArgs,
"-r", strings.Join(c.SlopShaders, ","))
}
geometry, err := exec.Command("slop", slopArgs...).Output()
if err != nil {
return ""
}
return string(geometry)
}
func moveFile(inFile string, outFile string) error {
err := os.Rename(inFile, outFile)
if err != nil {
// Probably a cross-device link or other error from os.Rename
err = exec.Command("mv", inFile, outFile).Run()
}
if err != nil {
return err
}
// Set some sane permissions
return os.Chmod(outFile, 0664)
}