-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmain.go
437 lines (393 loc) · 12.9 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
431
432
433
434
435
436
437
//
// #@@@@@@@@@/
// (@@%%%%%%%%%%%%%%&@%
// %@%%%%%%%%%%%%%%%%%%%%%&@(
// (@%%%%%%%%%%%%%%%%%%%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%%%@& &/
// (@&%%%%%%%%%%%%%%%%%@ @,
// /@%%%%%%%%%%%%%%%%%%&* *@*
// /@%%%%%%%%%%%%%%&@. %#
// #%@&%%%%%%@%. ,@*
// ,&@& /@#//&@/
// *#&@(*, .*#&%/////(##@/
// ........... @(*#&@@@@@@(/////#@@@@&@* ..,*/#%@@@%.,,,.
// .............%&#(/////////////////(##&&.,,,.,,.,...
// .,.. ,(&@@%/*,#@@@@@@@,,,,,.
// ..........,,,.,...
// daky's X11 hotkey daemon
package main
import (
"bytes"
"errors"
"fmt"
"io/ioutil"
"log"
"os"
"os/exec"
"os/signal"
"path/filepath"
"runtime"
"strconv"
"syscall"
"time"
"github.com/BurntSushi/xgbutil"
"github.com/BurntSushi/xgbutil/keybind"
"github.com/BurntSushi/xgbutil/mousebind"
"github.com/BurntSushi/xgbutil/xevent"
"github.com/dakyskye/dxhd/config"
"github.com/dakyskye/dxhd/listener"
"github.com/dakyskye/dxhd/logger"
"github.com/dakyskye/dxhd/options"
"github.com/dakyskye/dxhd/parser"
"github.com/sirupsen/logrus"
)
var usage = `NAME
dxhd - daky's X11 Hotkey Daemon
VERSION
%s
SYNOPSIS
dxhd [OPTIONS]
DESCRIPTION
dxhd is an easy-to-use X11 hotkey daemon, written in Go programming language, and inspired by sxhkd.
More can be read here - https://github.com/dakyskye/dxhd#readme
OPTIONS%s
EXAMPLE CONFIG
#!/usr/bin/bash
## restart i3
# super + shift + r
i3-msg -t command restart
## switch to workspace 1-10
# super + @{1-9,0}
i3-msg -t command workspace {1-9,10}
## switch to workspace 11-20
# super + ctrl + {1-9,0}
i3-msg -t command workspace {11-19,20}
## switch to next/prev workspace
# super + @mouse{4,5}
i3-msg -t command workspace {next,prev}
BUGS
report a bug here if you find one - https://github.com/dakyskye/dxhd/issues
AUTHOR
Lasha Kanteladze <[email protected]>`
var version = `master`
func main() {
if runtime.GOOS != "linux" {
logger.L().Fatalln("dxhd is only supported on linux")
}
stdin := new([]byte)
stat, err := os.Stdin.Stat()
if err != nil {
logger.L().WithError(err).Fatal("can not stat stdin")
}
if stat.Mode()&os.ModeCharDevice == 0 {
*stdin, err = ioutil.ReadAll(os.Stdin)
if err != nil {
logger.L().WithError(err).Fatal("can not read the stdin")
}
} else {
stdin = nil
}
opts, err := options.Parse()
if err != nil {
logger.L().Fatalln(err)
}
usage = fmt.Sprintf(usage, version, options.OptionsToPrint)
exit := false
if opts.Help {
fmt.Println(usage)
fmt.Println()
exit = true
} else if opts.Version {
fmt.Println("you are using dxhd, version " + version)
fmt.Println()
exit = true
}
runInBackground := func(data *[]byte) (err error) {
exc, err := os.Executable()
if err != nil {
logger.L().WithError(err).Fatal("can not get the executable")
}
cmd := exec.Command(exc, os.Args...)
if data != nil {
cmd.Stdin = bytes.NewReader(*data)
}
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.SysProcAttr = &syscall.SysProcAttr{
Foreground: false,
Setsid: true,
}
err = cmd.Start()
if err != nil {
logger.L().WithError(err).Fatal("can not run dxhd in the background")
}
time.Sleep(time.Microsecond * 10)
return
}
if opts.Background && !opts.Interactive {
err = runInBackground(stdin)
if err != nil {
logger.L().WithError(err).Fatal("can not run dxhd in the background")
}
os.Exit(0)
}
findEditor := func() (ed string, e error) {
editor := os.Getenv("EDITOR")
editors := [5]string{editor, "nano", "nvim", "vim", "vi"}
for _, ed = range editors {
ed, e = exec.LookPath(ed)
if e == nil {
break
}
}
if e != nil {
e = errors.New("no text editor was found installed")
}
return
}
if opts.Edit != nil {
editor, err := findEditor()
if err != nil {
logger.L().WithError(err).Fatal("can not find a suitable editor to use")
}
_, configDir, _ := config.GetDefaultConfigPath()
if *opts.Edit == "" {
*opts.Edit = "dxhd.sh"
}
path := filepath.Join(configDir, *opts.Edit)
cmd := exec.Command(editor, path)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Stdin = os.Stdin
err = cmd.Run()
if err != nil {
logger.L().WithError(err).WithFields(logrus.Fields{"editor": editor, "path": path}).Fatal("cannot invoke editor")
}
exit = true
}
var (
configFilePath string
validPath bool
)
if stdin == nil {
if opts.Interactive && opts.Config == nil {
editor, err := findEditor()
if err != nil {
logger.L().WithError(err).Fatal("can not find a suitable editor to use")
}
tmp, err := ioutil.TempFile("/tmp", "dxhd")
if err != nil {
logger.L().WithError(err).Fatal("can not create a temp file for interactive l")
}
_, err = tmp.WriteString("#!/bin/sh")
if err != nil {
logger.L().WithError(err).WithField("file", tmp.Name()).Warn("can not write default shebang to temp file")
}
cmd := exec.Command(editor, tmp.Name())
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Stdin = os.Stdin
err = cmd.Run()
if err != nil {
logger.L().WithError(err).WithFields(logrus.Fields{"editor": editor, "path": tmp.Name()}).Fatal("cannot invoke editor")
}
stdin = new([]byte)
*stdin, err = ioutil.ReadFile(tmp.Name()) // ioutil.ReadAll(tmp) omits the shebang
if err != nil {
logger.L().WithError(err).WithField("file", tmp).Fatal("can not read file")
}
err = os.Remove(tmp.Name())
if err != nil {
logger.L().WithError(err).Warn("can not delete temp file dxhd created")
}
if opts.Background {
err = runInBackground(stdin)
if err != nil {
logger.L().WithError(err).Fatal("can not run dxhd in the background")
}
os.Exit(0)
}
} else if opts.Config != nil {
if validPath, err = config.IsPathToConfigValid(*opts.Config); !(err == nil && validPath) {
logger.L().WithFields(logrus.Fields{
"path": *opts.Config,
"valid": validPath,
}).WithError(err).Fatal("path to the config is not valid")
}
configFilePath = *opts.Config
} else {
configFilePath, _, err = config.GetDefaultConfigPath()
if err != nil {
logger.L().WithError(err).Fatal("can not get config path")
}
if validPath, err = config.IsPathToConfigValid(configFilePath); !(err == nil && validPath) {
if os.IsNotExist(err) {
err = config.CreateDefaultConfig()
if err != nil {
logger.L().WithField("path", configFilePath).Fatal("can not create default config")
}
logger.L().WithField("path", configFilePath).Info("created the default config")
os.Exit(0)
} else {
logger.L().WithFields(logrus.Fields{"path": configFilePath, "valid": validPath}).WithError(err).Fatal("path to the config is not valid")
}
}
}
}
var (
data []parser.FileData
shell string
globals string
startTime time.Time
)
if opts.ParseTime {
startTime = time.Now()
}
if stdin != nil {
shell, globals, err = parser.Parse(*stdin, &data)
*stdin = []byte("")
} else {
shell, globals, err = parser.Parse(configFilePath, &data)
}
if err != nil {
logger.L().WithField("file", configFilePath).WithError(err).Fatal("failed to parse config")
}
if opts.ParseTime {
since := time.Since(startTime)
timeTaken := fmt.Sprintf("%.0fs%dms%dµs",
since.Seconds(),
since.Milliseconds(),
since.Microseconds(),
)
fmt.Printf("it took %s to parse the config\n", timeTaken)
fmt.Printf("%d parsed keybindins (including replicated variants and ranges)\n", len(data))
exit = true
}
if opts.DryRun {
fmt.Println("dxhd dry run")
for _, d := range data {
fmt.Println("binding: " + d.OriginalBinding)
fmt.Println("command:")
fmt.Println(d.Command.String())
}
fmt.Println()
exit = true
}
if opts.Kill || opts.Reload {
execName, err := os.Executable()
if err != nil {
logger.L().WithError(err).Fatal("can not get executable")
}
if opts.Kill {
err = exec.Command("pkill", "-INT", "-x", filepath.Base(execName)).Start()
} else {
err = exec.Command("pkill", "-USR1", "-x", filepath.Base(execName)).Start()
}
if err != nil {
if opts.Kill {
log.Println("can not kill dxhd instances:")
log.Fatalln(err)
} else {
log.Println("can not reload dxhd instances:")
log.Fatalln(err)
}
}
if opts.Kill {
fmt.Println("killing every running instances of dxhd")
} else {
fmt.Println("reloading every running instances of dxhd")
}
exit = true
}
if exit {
os.Exit(0)
}
logger.L().WithFields(logrus.Fields{"version": version, "path": configFilePath}).Debug("starting dxhd")
// catch these signals
signals := make(chan os.Signal, 1)
signal.Notify(signals, os.Interrupt, syscall.SIGINT, syscall.SIGTERM, syscall.SIGUSR1, syscall.SIGUSR2)
// errors channel
errs := make(chan error)
// infinite loop - if user sends USR signal, reload configration (so, continue loop), otherwise, exit
toplevel:
for {
if len(data) == 0 && stdin == nil {
shell, globals, err = parser.Parse(configFilePath, &data)
if err != nil {
logger.L().WithField("file", configFilePath).WithError(err).Fatal("failed to parse config")
}
}
X, err := xgbutil.NewConn()
if err != nil {
logger.L().WithError(err).Fatal("can not open connection to Xorg")
}
keybind.Initialize(X)
mousebind.Initialize(X)
for _, d := range data {
err = listener.ListenKeybinding(X, errs, d.EvtType, shell, globals, d.Binding.String(), d.Command.String())
if err != nil {
logger.L().WithField("keybinding", d.Binding.String()).WithError(err).Warn("can not register a keybinding")
}
}
data = nil
go xevent.Main(X)
for {
select {
case err = <-errs:
if err != nil {
logger.L().WithError(err).Warn("a command resulted into an error")
}
continue
case sig := <-signals:
if (sig == syscall.SIGUSR1 || sig == syscall.SIGUSR2) && stdin != nil {
logger.L().Debug("user defined signal received, but not reloading, as dxhd's using memory config")
continue
}
keybind.Detach(X, X.RootWin())
mousebind.Detach(X, X.RootWin())
xevent.Quit(X)
if (sig == syscall.SIGUSR1 || sig == syscall.SIGUSR2) && stdin == nil {
logger.L().Debug("user defined signal received, reloading")
continue toplevel
}
logger.L().WithField("signal", sig.String()).Info("signal received, shutting down")
if env, err := strconv.ParseBool(os.Getenv("STACKTRACE")); env && err == nil {
buf := make([]byte, 1<<20)
stackLen := runtime.Stack(buf, true)
log.Printf("\nPriting goroutine stack trace, because `STACKTRACE` was set.\n%s\n", buf[:stackLen])
}
os.Exit(0)
}
}
}
}