summary refs log tree commit diff
path: root/.config/waybar/modules/gamma.go
diff options
context:
space:
mode:
Diffstat (limited to '.config/waybar/modules/gamma.go')
-rw-r--r--.config/waybar/modules/gamma.go76
1 files changed, 76 insertions, 0 deletions
diff --git a/.config/waybar/modules/gamma.go b/.config/waybar/modules/gamma.go
new file mode 100644
index 0000000..2a91cdb
--- /dev/null
+++ b/.config/waybar/modules/gamma.go
@@ -0,0 +1,76 @@
+// usage:
+// go build -o waybar-gammastep gammastep.go
+package main
+
+import (
+	"encoding/json"
+	"log"
+	"os"
+	"os/exec"
+	"os/signal"
+	"strings"
+
+	. "syscall"
+)
+
+func main() {
+	var (
+		update = make(chan os.Signal, 1)
+		cancel = make(chan os.Signal, 1)
+	)
+	signal.Notify(update, SIGUSR1, SIGUSR2, SIGHUP)
+	signal.Notify(cancel, SIGTERM, SIGINT)
+
+	// could use golang.org/x/sys/unix.Prctl here
+	// but I want to avoid the dependency
+	if _, _, e := Syscall6(SYS_PRCTL,
+		uintptr(PR_SET_PDEATHSIG),
+		uintptr(SIGTERM),
+		0, 0, 0, 0); e != 0 {
+		log.Printf("error: %v", e)
+		os.Exit(1)
+	}
+
+	lookup()
+	for {
+		select {
+		case sig := <-update:
+			switch sig {
+			case SIGUSR1:
+				printJson("day")
+			case SIGUSR2:
+				printJson("night")
+			case SIGHUP:
+				lookup()
+			}
+		case <-cancel:
+			log.Printf("%v exiting\n", os.Args[0])
+			os.Exit(0)
+		}
+	}
+}
+
+func printJson(phase string) {
+	_ = json.NewEncoder(os.Stdout).Encode(map[string]interface{}{
+		"class":   phase,
+		"alt":     phase,
+		"tooltip": "gamma: " + phase,
+		"text":    phase,
+	})
+}
+
+func lookup() {
+	cmdEnv, ok := os.LookupEnv("GAMMA_COMMAND")
+	if !ok {
+		printJson("unknown")
+		return
+	}
+	log.Println(cmdEnv)
+	b, err := exec.Command("bash", "-c", cmdEnv).Output()
+	if err != nil {
+		log.Printf("error: %v", err)
+		return
+	}
+	phase := strings.TrimSpace(string(b))
+	printJson(phase)
+}