├── screenshot.png ├── README.md ├── LICENSE └── main.go /screenshot.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andlabs/wakeup/HEAD/screenshot.png -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # wakeup 2 | 3 | ![wakeup](https://raw.githubusercontent.com/andlabs/wakeup/master/screenshot.png) 4 | 5 | This is just a simple alarm clock program in Go; it demonstrates http://github.com/andlabs/ui. The source should be self-explanatory; feel free to make suggestions. 6 | 7 | To install: 8 | 9 | 1. Make sure you are using Go >= 1.3. See http://golang.org/dl/#go1.3rc1. 10 | 2. Run: 11 | 12 | ```sh 13 | $ go get github.com/andlabs/wakeup 14 | ``` 15 | 16 | If your `$GOPATH/bin` directory is in your path, you should be able to just run: 17 | 18 | ```sh 19 | $ wakeup 20 | ``` 21 | 22 | from the command line. 23 | 24 | 25 | ## TODO 26 | - switch to a proper date control when one is added to package ui 27 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2014 Pietro Gagliardi 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 4 | 5 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 6 | 7 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 8 | 9 | (this is called the MIT License or Expat License; see http://www.opensource.org/licenses/MIT) 10 | -------------------------------------------------------------------------------- /main.go: -------------------------------------------------------------------------------- 1 | // 4 march 2014 2 | package main 3 | 4 | import ( 5 | "fmt" 6 | "os" 7 | "os/exec" 8 | "time" 9 | 10 | "github.com/andlabs/ui" 11 | ) 12 | 13 | const ( 14 | defCmdLine = "mpv -loop inf ~/ring.wav" 15 | defTime = "10:30 AM" 16 | timeFmt = "3:04 PM" 17 | ) 18 | 19 | // If later hasn't happened yet, make it happen on the day of now; if not, the day after. 20 | func bestTime(now time.Time, later time.Time) time.Time { 21 | now = now.Local() // use local time to make things make sense 22 | nowh, nowm, nows := now.Clock() 23 | laterh, laterm, laters := later.Clock() 24 | add := false 25 | if nowh > laterh { 26 | add = true 27 | } else if (nowh == laterh) && (nowm > laterm) { 28 | add = true 29 | } else if (nowh == laterh) && (nowm == laterm) && (nows >= laters) { 30 | // >= in the case we're on the exact second; add a day because the alarm should have gone off by now otherwise! 31 | add = true 32 | } 33 | if add { 34 | now = now.AddDate(0, 0, 1) 35 | } 36 | return time.Date(now.Year(), now.Month(), now.Day(), 37 | laterh, laterm, laters, 0, 38 | now.Location()) 39 | } 40 | 41 | type MainWindow struct { 42 | cmd *exec.Cmd 43 | stopChan chan struct{} 44 | 45 | win *ui.Window 46 | cmdbox *ui.LineEdit 47 | timebox *ui.LineEdit 48 | bStart *ui.Button 49 | bStop *ui.Button 50 | status *ui.Label 51 | } 52 | 53 | // this is run as a separate goroutine 54 | // mw.stopChan must be valid before this function starts and must be closed after this function returns 55 | func (mw *MainWindow) timer(t time.Duration) { 56 | timer := time.NewTimer(t) 57 | for { 58 | select { 59 | case <-timer.C: 60 | // send a signal to the main window that we're ready to run the command it has 61 | ui.Post(mw.win, nil) // no data needed; just a signal 62 | return 63 | case <-mw.stopChan: 64 | timer.Stop() 65 | return 66 | } 67 | } 68 | panic("unreachable") // just in case 69 | } 70 | 71 | // this is called by mw.Event() when we need to stop the alarm 72 | // it must run on the same OS thread as mw.Event() 73 | func (mw *MainWindow) stop() { 74 | if mw.cmd != nil { // stop the command if it's running 75 | err := mw.cmd.Process.Kill() 76 | if err != nil { 77 | mw.win.MsgBoxError( 78 | fmt.Sprintf("Error killing process: %v", err), 79 | "You may need to kill it manually.") 80 | } 81 | err = mw.cmd.Process.Release() 82 | if err != nil { 83 | mw.win.MsgBoxError( 84 | fmt.Sprintf("Error releasing process: %v", err), 85 | "") 86 | } 87 | mw.cmd = nil 88 | } 89 | if mw.stopChan != nil { // stop the timer if it's still running 90 | mw.stopChan <- struct{}{} 91 | close(mw.stopChan) 92 | mw.stopChan = nil 93 | } 94 | mw.status.SetText("") 95 | } 96 | 97 | func NewMainWindow() (mw *MainWindow) { 98 | mw = new(MainWindow) 99 | 100 | mw.win = ui.NewWindow("wakeup", 400, 100) 101 | mw.win.Closing = mw.Closing 102 | mw.win.Posted = mw.Fire 103 | mw.cmdbox = ui.NewLineEdit(defCmdLine) 104 | mw.timebox = ui.NewLineEdit(defTime) 105 | mw.bStart = ui.NewButton("Start") 106 | mw.bStart.Clicked = mw.StartClicked 107 | mw.bStop = ui.NewButton("Stop") 108 | mw.bStop.Clicked = mw.stop 109 | mw.status = ui.NewLabel("") 110 | 111 | // a Stack to keep both buttons at the same size 112 | btnbox := ui.NewHorizontalStack(mw.bStart, mw.bStop) 113 | btnbox.SetStretchy(0) 114 | btnbox.SetStretchy(1) 115 | // and a Stack around that Stack to keep them at a reasonable size, with space to their right 116 | btnbox = ui.NewHorizontalStack(btnbox, mw.status) 117 | 118 | // the main layout 119 | grid := ui.NewGrid(2, 120 | ui.NewLabel("Command"), mw.cmdbox, 121 | ui.NewLabel("Time"), mw.timebox, 122 | ui.Space(), ui.Space(), // the Space on the right will consume the window blank space 123 | ui.Space(), btnbox) 124 | grid.SetStretchy(2, 1) // make the Space noted above consume 125 | grid.SetFilling(0, 1) // make the two textboxes grow horizontally 126 | grid.SetFilling(1, 1) 127 | grid.SetFilling(0, 0) // make the two Labels fill their cells so alignment works right on GTK+ (TODO) 128 | grid.SetFilling(1, 0) 129 | 130 | mw.win.Open(grid) 131 | 132 | return mw 133 | } 134 | 135 | func (mw *MainWindow) Closing() bool { 136 | mw.stop() 137 | ui.Stop <- struct{}{} 138 | return true 139 | } 140 | 141 | func (mw *MainWindow) StartClicked() { 142 | mw.stop() // only one alarm at a time 143 | alarmTime, err := time.Parse(timeFmt, mw.timebox.Text()) 144 | if err != nil { 145 | mw.win.MsgBoxError( 146 | fmt.Sprintf("Error parsing time %q: %v", mw.timebox.Text(), err), 147 | fmt.Sprintf("Make sure your time is in the form %q (without quotes).", timeFmt)) 148 | return 149 | } 150 | now := time.Now() 151 | later := bestTime(now, alarmTime) 152 | mw.stopChan = make(chan struct{}) 153 | go mw.timer(later.Sub(now)) 154 | mw.status.SetText("Started") 155 | } 156 | 157 | func (mw *MainWindow) Fire(data interface{}) { 158 | mw.cmd = exec.Command("/bin/sh", "-c", "exec "+mw.cmdbox.Text()) 159 | // 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?) 160 | // keep stdout /dev/null to avoid stty mucking 161 | mw.cmd.Stderr = os.Stderr 162 | err := mw.cmd.Start() 163 | mw.status.SetText("Firing") 164 | if err != nil { 165 | mw.win.MsgBoxError( 166 | fmt.Sprintf("Error running program: %v", err), 167 | "") 168 | mw.cmd = nil 169 | mw.status.SetText("") 170 | } 171 | // we're done with the timer, but the goroutine that handles it has returned (or will after we do) 172 | // so close the stopChan now so that the next call to mw.stop() doesn't hang or crash 173 | close(mw.stopChan) 174 | mw.stopChan = nil 175 | } 176 | 177 | func main() { 178 | err := ui.Go(func() { // start 179 | NewMainWindow() 180 | }) 181 | if err != nil { 182 | panic(fmt.Errorf("error initializing UI library: %v", err)) 183 | } 184 | } 185 | --------------------------------------------------------------------------------