summary refs log tree commit diff
path: root/.config/waybar/modules/redshift.go
blob: 43b623cf4a51c3ab40a38e390a556bd13960edbc (plain) (blame)
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// usage:
// go build -o waybar-redshift redshift.go
package main

import (
	"encoding/json"
	"fmt"
	"os"
	"os/exec"
	"os/signal"
	"strings"

	. "syscall"
)

const (
	ICON = ""
)

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 {
		os.Exit(1)
	}
	// if err := Prctl(PR_SET_PDEATHSIG,
	// 		SIGTERM,
	// 		0, 0, 0); err != nil {
	// 	fmt.Fprintln(os.Stderr, err)
	// 	os.Exit(1)
	// }

	lookup()
	for {
		select {
		case sig := <-update:
			switch sig {
			case SIGUSR1:
				// redshift off
				printJson("day", "day", ICON)
			case SIGUSR2:
				// redshift on
				printJson("night", "night", ICON)
			case SIGHUP:
				lookup()
			}
		case <-cancel:
			fmt.Fprintln(os.Stderr, "bye")
			os.Exit(0)
		}
	}
}

func printJson(class, tooltip, text string) {
	_ = json.NewEncoder(os.Stdout).Encode(map[string]string{
		"class":   class,
		"tooltip": "redshift: " + tooltip,
		"text":    text,
	})
}

func lookup() {
	b, err := exec.Command("redshift", "-p").Output()
	if err != nil {
		return
	}
	fds := strings.Fields(string(b))
	for idx, f := range fds {
		if f == "Period:" {
			fds = fds[idx+1:]
			break
		}
	}
	switch fds[0] {
	case "Night":
		printJson("night", "night", ICON)
	case "Transition":
		printJson("night", "transition", ICON)
	case "Day":
		printJson("day", "day", ICON)
	}
}