/
SleepTImerPauseShow.go
177 lines (155 loc) · 3.86 KB
/
SleepTImerPauseShow.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
package main
import (
"bufio"
"fmt"
"os"
"strconv"
"strings"
"syscall"
"time"
"unsafe"
"github.com/gen2brain/beeep"
"github.com/micmonay/keybd_event"
)
var pauseAfter = 30 //Default Pause After Time in minutes
var startAlerts = 5 //Default Start Alert Time in minutes
var beepAlert = true
var lastInput = getLastInput()
var lastInputInfo struct {
cbSize uint32
dwTime uint32
}
func main() {
getTimeForTimer()
getAlertTime()
getBeepAlert()
timer(pauseAfter) // in minutes
}
func pauseShow() {
kb, err := keybd_event.NewKeyBonding()
if err != nil {
panic(err)
}
kb.SetKeys(keybd_event.VK_SPACE)
err = kb.Launching()
if err != nil {
panic(err)
}
}
func wasThereInput() bool {
if getLastInput() != lastInput {
lastInput = getLastInput()
return true
}
return false
}
//timer waits t number of minutes
func timer(t int) {
i := 0
for {
isInput := wasThereInput()
if !isInput {
fmt.Printf("Been inactive for %v mins\n", i)
alert(i)
time.Sleep(1 * time.Minute)
i++
} else if isInput {
i = 0 //restart timer
}
if i >= t {
//pause show
fmt.Println("pause")
pauseShow()
return
}
}
}
func alert(idleSince int) {
if pauseAfter-idleSince <= startAlerts {
notifyTimeBefore(pauseAfter - idleSince)
}
}
// gets the last time user input in ms since system started
func getLastInput() uint32 {
//From https://stackoverflow.com/questions/22949444/using-golang-to-get-windows-idle-time-getlastinputinfo-or-similar
lastInputInfo.cbSize = uint32(unsafe.Sizeof(lastInputInfo))
user32 := syscall.MustLoadDLL("user32.dll") // or NewLazyDLL() to defer loading
getLastInputInfo := user32.MustFindProc("GetLastInputInfo") // or NewProc() if you used NewLazyDLL()
// or you can handle the errors in the above if you want to provide some alternative
r1, _, err := getLastInputInfo.Call(
uintptr(unsafe.Pointer(&lastInputInfo)))
// err will always be non-nil; you need to check r1 (the return value)
if r1 == 0 { // in this case
panic("error getting last input info: " + err.Error())
}
return lastInputInfo.dwTime
}
func notifyTimeBefore(t int) {
fmt.Println("Notify")
err := beeep.Notify("Sleep Timer", "Pausing your show in "+strconv.Itoa(t)+" mins\nMove your mouse or press a button to reset timer", "assets/information.png")
if err != nil {
panic(err)
}
if t == 1 && beepAlert {
err := beeep.Beep(beeep.DefaultFreq, beeep.DefaultDuration)
if err != nil {
panic(err)
}
}
}
func getTimeForTimer() {
reader := bufio.NewReader(os.Stdin)
fmt.Printf("How long do you want to set the timer in minutes? (default: %v) ", pauseAfter)
text, _ := reader.ReadString('\n')
text = strings.Trim(text, "\r\n")
if text != "" {
i, err := strconv.Atoi(text)
if err != nil || i <= 0 {
fmt.Println("Invalid Number: ", err)
getTimeForTimer()
return
}
pauseAfter = i
}
fmt.Printf("Will pause after %v mins of inactivity\n", pauseAfter)
}
func getAlertTime() {
reader := bufio.NewReader(os.Stdin)
fmt.Printf("At what time do you want alerts to start in mins? (default: %v) ", startAlerts)
text, _ := reader.ReadString('\n')
text = strings.Trim(text, "\r\n")
if text != "" {
i, err := strconv.Atoi(text)
if err != nil || i < 0 {
fmt.Println("Invalid Number: ", err)
getTimeForTimer()
return
}
startAlerts = i
}
fmt.Printf("Will start alerts %v mins before pause\n", startAlerts)
}
func getBeepAlert() {
reader := bufio.NewReader(os.Stdin)
fmt.Printf("Do you wish to have a beep alert on last notification? y/n (default: y) ")
text, _ := reader.ReadString('\n')
text = strings.Trim(text, "\r\n")
text = strings.ToLower(text)
switch text {
case "y":
beepAlert = true
case "n":
beepAlert = false
case "":
beepAlert = true
default:
fmt.Println("Invalid y/n")
getBeepAlert()
return
}
if beepAlert {
fmt.Println("Will beep on last notification")
} else {
fmt.Println("Won't beep")
}
}