-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
56 lines (46 loc) · 1.36 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
package main
import (
"fmt"
"os"
"strconv"
"time"
"github.com/godbus/dbus/v5"
)
const (
dbusDest = "org.gnome.SessionManager"
dbusPath = "/org/gnome/SessionManager"
dbusInterface = "org.gnome.SessionManager"
inhibitReason = "Preventing screen blank"
inhibitFlags = 0x8 // inhibit flag for idle
)
func main() {
if len(os.Args) != 2 {
fmt.Printf("Usage: %s <minutes>\n", os.Args[0])
os.Exit(1)
}
minutes, err := strconv.Atoi(os.Args[1])
if err != nil || minutes < 1 {
fmt.Println("Invalid number of minutes. Please enter a positive integer.")
os.Exit(1)
}
conn, err := dbus.SessionBus()
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to connect to session bus: %s\n", err)
os.Exit(1)
}
obj := conn.Object(dbusDest, dbus.ObjectPath(dbusPath))
var inhibitCookie uint32
err = obj.Call("org.gnome.SessionManager.Inhibit", 0, os.Args[0], uint32(0), inhibitReason, uint32(inhibitFlags)).Store(&inhibitCookie)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to call Inhibit: %s\n", err)
os.Exit(1)
}
fmt.Printf("Inhibiting screen blank for %d minutes...\n", minutes)
time.Sleep(time.Duration(minutes) * time.Minute)
err = obj.Call("org.gnome.SessionManager.Uninhibit", 0, inhibitCookie).Store()
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to call Uninhibit: %s\n", err)
os.Exit(1)
}
fmt.Println("Screen blank inhibition ended.")
}