Last active
June 14, 2020 22:15
-
-
Save solarkraft/fa1bee41f970eb3a3da23d7216e364e7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// build with env GOOS=linux GOARCH=arm GOARM=5 go build | |
package main | |
import ( | |
"fmt" | |
"os" | |
"os/exec" | |
"github.com/godbus/dbus" | |
) | |
func main() { | |
if len(os.Args) < 2 || os.Args[1] == "--help" { | |
fmt.Fprintln(os.Stderr, "Run a command whenever your Kindle wakes up. ") | |
fmt.Fprintf(os.Stderr, "Usage: %s [command to run]\n", os.Args[0]) | |
fmt.Fprintln(os.Stderr, "If you need special shell functionality like piping, you can use bash -c '[commands to run]'") | |
os.Exit(1) | |
} | |
conn, err := dbus.ConnectSystemBus() | |
if err != nil { | |
fmt.Fprintln(os.Stderr, "Failed to connect to system bus:", err) | |
os.Exit(1) | |
} | |
defer conn.Close() | |
err = conn.AddMatchSignal( | |
dbus.WithMatchObjectPath("/default"), | |
dbus.WithMatchInterface("com.lab126.powerd"), | |
dbus.WithMatchMember("outOfScreenSaver"), | |
//test locally with dbus-send --system /org/freedesktop/DBus/test org.freedesktop.DBus.test string:'this is a test' | |
//dbus.WithMatchObjectPath("/org/freedesktop/DBus/test"), | |
) | |
if err != nil { panic(err) } | |
c := make(chan *dbus.Signal, 10) | |
conn.Signal(c) | |
for range c { | |
fmt.Println("Running", os.Args[1:]) | |
err := exec.Command(os.Args[1], os.Args[2:]...).Run() | |
if err != nil { fmt.Fprintln(os.Stderr, "The command exited with an error:", err) } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment