Skip to content

Instantly share code, notes, and snippets.

@solarkraft
Last active June 14, 2020 22:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save solarkraft/fa1bee41f970eb3a3da23d7216e364e7 to your computer and use it in GitHub Desktop.
Save solarkraft/fa1bee41f970eb3a3da23d7216e364e7 to your computer and use it in GitHub Desktop.
// 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