-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.go
184 lines (166 loc) · 4.92 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
// 4 march 2014
package main
import (
"fmt"
"os"
"os/exec"
"time"
"github.com/andlabs/ui"
)
const (
defCmdLine = "mpv -loop inf ~/ring.wav"
defTime = "10:30 AM"
timeFmt = "3:04 PM"
)
// If later hasn't happened yet, make it happen on the day of now; if not, the day after.
func bestTime(now time.Time, later time.Time) time.Time {
now = now.Local() // use local time to make things make sense
nowh, nowm, nows := now.Clock()
laterh, laterm, laters := later.Clock()
add := false
if nowh > laterh {
add = true
} else if (nowh == laterh) && (nowm > laterm) {
add = true
} else if (nowh == laterh) && (nowm == laterm) && (nows >= laters) {
// >= in the case we're on the exact second; add a day because the alarm should have gone off by now otherwise!
add = true
}
if add {
now = now.AddDate(0, 0, 1)
}
return time.Date(now.Year(), now.Month(), now.Day(),
laterh, laterm, laters, 0,
now.Location())
}
type MainWindow struct {
cmd *exec.Cmd
stopChan chan struct{}
win *ui.Window
cmdbox *ui.LineEdit
timebox *ui.LineEdit
bStart *ui.Button
bStop *ui.Button
status *ui.Label
}
// this is run as a separate goroutine
// mw.stopChan must be valid before this function starts and must be closed after this function returns
func (mw *MainWindow) timer(t time.Duration) {
timer := time.NewTimer(t)
for {
select {
case <-timer.C:
// send a signal to the main window that we're ready to run the command it has
ui.Post(mw.win, nil) // no data needed; just a signal
return
case <-mw.stopChan:
timer.Stop()
return
}
}
panic("unreachable") // just in case
}
// this is called by mw.Event() when we need to stop the alarm
// it must run on the same OS thread as mw.Event()
func (mw *MainWindow) stop() {
if mw.cmd != nil { // stop the command if it's running
err := mw.cmd.Process.Kill()
if err != nil {
mw.win.MsgBoxError(
fmt.Sprintf("Error killing process: %v", err),
"You may need to kill it manually.")
}
err = mw.cmd.Process.Release()
if err != nil {
mw.win.MsgBoxError(
fmt.Sprintf("Error releasing process: %v", err),
"")
}
mw.cmd = nil
}
if mw.stopChan != nil { // stop the timer if it's still running
mw.stopChan <- struct{}{}
close(mw.stopChan)
mw.stopChan = nil
}
mw.status.SetText("")
}
func NewMainWindow() (mw *MainWindow) {
mw = new(MainWindow)
mw.win = ui.NewWindow("wakeup", 400, 100)
mw.win.Closing = mw.Closing
mw.win.Posted = mw.Fire
mw.cmdbox = ui.NewLineEdit(defCmdLine)
mw.timebox = ui.NewLineEdit(defTime)
mw.bStart = ui.NewButton("Start")
mw.bStart.Clicked = mw.StartClicked
mw.bStop = ui.NewButton("Stop")
mw.bStop.Clicked = mw.stop
mw.status = ui.NewLabel("")
// a Stack to keep both buttons at the same size
btnbox := ui.NewHorizontalStack(mw.bStart, mw.bStop)
btnbox.SetStretchy(0)
btnbox.SetStretchy(1)
// and a Stack around that Stack to keep them at a reasonable size, with space to their right
btnbox = ui.NewHorizontalStack(btnbox, mw.status)
// the main layout
grid := ui.NewGrid(2,
ui.NewLabel("Command"), mw.cmdbox,
ui.NewLabel("Time"), mw.timebox,
ui.Space(), ui.Space(), // the Space on the right will consume the window blank space
ui.Space(), btnbox)
grid.SetStretchy(2, 1) // make the Space noted above consume
grid.SetFilling(0, 1) // make the two textboxes grow horizontally
grid.SetFilling(1, 1)
grid.SetFilling(0, 0) // make the two Labels fill their cells so alignment works right on GTK+ (TODO)
grid.SetFilling(1, 0)
mw.win.Open(grid)
return mw
}
func (mw *MainWindow) Closing() bool {
mw.stop()
ui.Stop <- struct{}{}
return true
}
func (mw *MainWindow) StartClicked() {
mw.stop() // only one alarm at a time
alarmTime, err := time.Parse(timeFmt, mw.timebox.Text())
if err != nil {
mw.win.MsgBoxError(
fmt.Sprintf("Error parsing time %q: %v", mw.timebox.Text(), err),
fmt.Sprintf("Make sure your time is in the form %q (without quotes).", timeFmt))
return
}
now := time.Now()
later := bestTime(now, alarmTime)
mw.stopChan = make(chan struct{})
go mw.timer(later.Sub(now))
mw.status.SetText("Started")
}
func (mw *MainWindow) Fire(data interface{}) {
mw.cmd = exec.Command("/bin/sh", "-c", "exec "+mw.cmdbox.Text())
// keep stdin /dev/null in case user wants to run multiple alarms on one instance (TODO should I allow this program to act as a pipe?)
// keep stdout /dev/null to avoid stty mucking
mw.cmd.Stderr = os.Stderr
err := mw.cmd.Start()
mw.status.SetText("Firing")
if err != nil {
mw.win.MsgBoxError(
fmt.Sprintf("Error running program: %v", err),
"")
mw.cmd = nil
mw.status.SetText("")
}
// we're done with the timer, but the goroutine that handles it has returned (or will after we do)
// so close the stopChan now so that the next call to mw.stop() doesn't hang or crash
close(mw.stopChan)
mw.stopChan = nil
}
func main() {
err := ui.Go(func() { // start
NewMainWindow()
})
if err != nil {
panic(fmt.Errorf("error initializing UI library: %v", err))
}
}