-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathkeep_awake.go
More file actions
45 lines (40 loc) · 911 Bytes
/
Copy pathkeep_awake.go
File metadata and controls
45 lines (40 loc) · 911 Bytes
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
package main
import (
"fmt"
"os/exec"
"runtime"
)
// sleepInhibitor keeps the operating system awake while Pulse runs. It
// deliberately does not prevent the display from turning off.
type sleepInhibitor struct {
cmd *exec.Cmd
}
func startSleepInhibitor() (*sleepInhibitor, error) {
var cmd *exec.Cmd
switch runtime.GOOS {
case "darwin":
cmd = exec.Command("caffeinate", "-i")
case "linux":
cmd = exec.Command(
"systemd-inhibit",
"--what=sleep",
"--mode=block",
"--why=Pulse is running",
"sleep",
"infinity",
)
default:
return nil, fmt.Errorf("automatic sleep prevention is not supported on %s", runtime.GOOS)
}
if err := cmd.Start(); err != nil {
return nil, err
}
return &sleepInhibitor{cmd: cmd}, nil
}
func (i *sleepInhibitor) stop() {
if i == nil || i.cmd == nil || i.cmd.Process == nil {
return
}
_ = i.cmd.Process.Kill()
_, _ = i.cmd.Process.Wait()
}