aboutsummaryrefslogtreecommitdiff
path: root/main.go
blob: ed9e2ee7ec2a92601616e5fd328b9761f59f9af6 (plain)
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package main

import (
	"flag"
	"fmt"
	"log"
	"os"
	"strconv"

	"mrogalski.eu/go/pulseaudio"
)

func main() {
	flag.Parse()

	c, e := pulseaudio.NewClient()
	if e != nil {
		log.Println(e)
		os.Exit(1)
	}

	if os.Args[0] == "puvol-cont" {
		cont(c)
	} else {
		single(c)
	}
}

func printvol(c *pulseaudio.Client) float32 {
	muted, e := c.Mute()
	if muted {
		fmt.Println("🔇")
		return 0
	}

	v, e := c.Volume()
	if e != nil {
		fmt.Println(e)
	} else {
		var c rune
		if v < 0.1 {
			c = ''
		} else if v < 0.5 {
			c = ''
		} else if v < 0.8 {
			c = ''
		} else {
			c = ''
		}
		fmt.Printf("%c % 2d%%\n", c, int(100*v))
	}
	return v
}

func cont(c *pulseaudio.Client) {

	ch, e := c.Updates()
	if e != nil {
		log.Println(e)
		os.Exit(2)
	}

	for {
		printvol(c)
		_ = <-ch
	}
}

func single(c *pulseaudio.Client) {
	if len(os.Args) == 1 {
		printvol(c)
		return
	}

	switch a := os.Args[1]; a {
	case "toggle":
		c.ToggleMute()
		printvol(c)
	case "mute", "unmute":
		c.SetMute(a == "mute")
		printvol(c)
	case "inc", "dec":
		v, e := c.Volume()
		if e != nil {
			log.Println(e)
			os.Exit(2)
		}

		if a == "inc" {
			v += 0.1
		} else if a == "dec" {
			v -= 0.1
		}

		if v <= 0 {
			v = 0
		}

		c.SetVolume(float32(v))
		printvol(c)

	case "set":
		if len(os.Args) < 3 {
			log.Println("missing volume")
			os.Exit(3)
		}
		v, e := strconv.ParseFloat(os.Args[2], 32)
		if e != nil {
			log.Println(e)
			os.Exit(4)
		}
		c.SetVolume(float32(v))
		printvol(c)
	default:
		printvol(c)
	}
}