-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
120 lines (99 loc) · 2.33 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
// +build linux
package main
import (
"flag"
"fmt"
"log"
"os"
"os/exec"
"runtime/pprof"
"time"
"github.com/BurntSushi/xgbutil"
"github.com/BurntSushi/xgbutil/keybind"
"github.com/BurntSushi/xgbutil/xevent"
)
var (
HOME = os.Getenv("HOME")
AppDir = HOME + "/.projektor"
SIFlag = "inst"
SingleInstance bool
CPUProfile string
Version string = "v0.1+"
ShowVersion bool
DryRun bool
)
func init() {
flag.BoolVar(&SingleInstance, SIFlag, false, "Run an instance of projektor, not a daemon")
flag.StringVar(&CPUProfile, "cpuprofile", "", "Run CPU profiling and output results to the `file`")
flag.BoolVar(&ShowVersion, "V", false, "Display version of current projektor build.\n\tPlus sign means that the build includes several more commits over the release.")
flag.BoolVar(&DryRun, "dry", false, "Prepare to run projektor instance but do not run (useful to force kernel caching).")
}
func RunInstance(dry bool) {
logf("Running single instance of projektor\n")
logf("Loading history...\n")
LoadHistory()
logf("Indexing desktop entries...\n")
IndexDesktopEntries()
logf("Set up the UI.\n")
SetupUi(dry)
}
func RunDaemon() {
logf("Running key binding daemon of projektor\n")
fci := Config.ForceCacheInterval
if fci > 0 {
go func() {
for {
logf("Running dry instance...\n")
cmd := exec.Command(os.Args[0], "-"+SIFlag, "-dry")
err := cmd.Start()
if err != nil {
errduring("dry instance creation", err, "")
}
go cmd.Wait()
time.Sleep(time.Duration(fci) * time.Second)
}
}()
}
xu, err := xgbutil.NewConn()
if err != nil {
log.Fatal(err)
}
keybind.Initialize(xu)
cb := func(xu *xgbutil.XUtil, e xevent.KeyPressEvent) {
if FindAndKill() {
return
}
cmd := exec.Command(os.Args[0], "-"+SIFlag)
err := cmd.Start()
if err != nil {
errduring("instance creation", err, "")
}
go cmd.Wait()
}
err = keybind.KeyPressFun(cb).Connect(xu, xu.RootWin(), Config.KeyBind, true)
if err != nil {
log.Fatal(err)
}
xevent.Main(xu)
}
func main() {
flag.Parse()
if ShowVersion {
fmt.Printf("Projektor %v\n", Version)
os.Exit(0)
}
if CPUProfile != "" {
f, err := os.Create(CPUProfile)
if err != nil {
panic(err)
}
defer f.Close()
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
}
if SingleInstance {
RunInstance(DryRun)
} else {
RunDaemon()
}
}