-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
164 lines (134 loc) · 3.18 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
package main
import (
"flag"
"fmt"
"os"
"os/signal"
"path/filepath"
"syscall"
"time"
"github.com/ejv2/podbit/colors"
"github.com/ejv2/podbit/data"
ev "github.com/ejv2/podbit/event"
"github.com/ejv2/podbit/sound"
"github.com/ejv2/podbit/ui"
"github.com/juju/fslock"
goncurses "github.com/vit1251/go-ncursesw"
)
const (
homebase = "podbit"
pidfile = "podbit.lock"
)
var (
homedir string
confdir string
events *ev.Handler
keystroke = make(chan rune)
newMen = make(chan ui.Menu)
exit = make(chan struct{})
reload = make(chan int8)
)
// Flags.
var (
KeepPlayed = flag.Bool("nocleanup", false, "Disable cache cleanups and keep all finished items")
PurgeQueue = flag.Bool("purge", false, "Purge finished items from the queue file as well as disk")
)
func banner() {
fmt.Printf("Starting Podbit v%d.%d.%d...\n", verMaj, verMin, verPatch)
}
func initDirs() {
share := os.Getenv("XDG_DATA_HOME")
config, _ := os.UserConfigDir()
if share == "" {
share = ".local/share"
}
homedir = filepath.Join(share, homebase)
confdir = filepath.Join(config, homebase)
herr := os.MkdirAll(homedir, os.ModeDir|os.ModePerm)
cerr := os.MkdirAll(confdir, os.ModeDir|os.ModePerm)
if herr != nil || cerr != nil {
fmt.Println("Error: Failed to create required directory(s)")
os.Exit(1)
}
}
func alreadyRunning() (bool, *fslock.Lock) {
lockpath := filepath.Join(homedir, pidfile)
lock := fslock.New(lockpath)
err := lock.TryLock()
if err != nil {
return true, nil
}
return false, lock
}
func initColors() {
if goncurses.HasColors() {
goncurses.StartColor()
}
colors.CreateColors()
}
func initTTY() {
goncurses.Raw(true)
goncurses.Echo(false)
goncurses.Cursor(0)
}
func initSignals(exit chan struct{}) {
sigchan := make(chan os.Signal, 1)
signal.Notify(sigchan, os.Interrupt, syscall.SIGTERM)
go func() {
<-sigchan
exit <- struct{}{}
}()
}
func main() {
banner()
flag.Parse()
initDirs()
initSignals(exit)
running, lock := alreadyRunning()
if running {
fmt.Println("Error: Podbit is already running")
os.Exit(1)
}
defer lock.Unlock()
events = ev.NewHandler()
now := time.Now()
err := data.InitData(*events)
if err != nil {
fmt.Println("\n" + err.Error())
return
}
defer data.SaveData()
go data.ReloadLoop(reload)
fmt.Print("Initialising sound system...")
sound.Plr, err = sound.NewPlayer(events)
if err != nil {
fmt.Printf("\nError: Failed to initialise sound system: %s\n", err.Error())
os.Exit(1)
}
fmt.Println("done")
go sound.Mainloop()
defer sound.Plr.Destroy()
scr, err := goncurses.Init()
if err != nil {
fmt.Printf("Error: Failed to initialize UI: %s\n", err)
os.Exit(1)
}
initTTY()
initColors()
defer goncurses.End()
ui.InitUI(scr, ui.LibraryMenu, events, keystroke, newMen, reload)
go ui.RenderLoop()
// Welcome message
startup := time.Since(now)
go ui.StatusMessage(fmt.Sprintf("Podbit v%d.%d.%d -- %d episodes of %d podcasts loaded in %.2fs",
verMaj,
verMin,
verPatch,
len(data.Q.Items), len(data.Q.GetPodcasts()),
startup.Seconds()))
// Run events handler and kickstart listeners
go events.Run()
events.Post(ev.Keystroke)
// Initialisation is done; use this thread as the input loop
ui.InputLoop(exit)
}