/
monitors.go
69 lines (62 loc) · 1.6 KB
/
monitors.go
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
package cmd
import (
"encoding/json"
"fmt"
"net"
ipc "github.com/Andrmist/dwm-polybar/utils"
"github.com/spf13/cobra"
)
var monitorsCmd = &cobra.Command{
Use: "monitors",
Short: "helper command to get information about monitors known by dwm",
Long: `since we can't predict what monitor you're trying to use, you can see what applications are present on each monitor, so you can see what "--monitor" value you should use`,
Run: func(cmd *cobra.Command, args []string) {
var buf []byte
c, err := net.Dial("unix", "/tmp/dwm.sock")
if err != nil {
fmt.Println(err)
return
}
defer c.Close()
buf = make([]byte, 10000)
_, err = c.Write(ipc.GenerateMessage(make([]byte, 0), ipc.IPC_TYPE_GET_MONITORS))
if err != nil {
fmt.Println(err)
return
}
bufSize, err := c.Read(buf)
rawB := buf[ipc.HEADER_LEN : bufSize-1]
var monitors []ipc.Monitor
err = json.Unmarshal(rawB, &monitors)
for _, mon := range monitors {
buf = make([]byte, 10000)
err = ipc.SendStruct(&c, ipc.IPCGetDWMClientPayload{Id: mon.Clients.Selected}, ipc.IPC_TYPE_GET_DWM_CLIENT)
if err != nil {
fmt.Println(err)
return
}
bufSize, err := c.Read(buf)
if err != nil {
fmt.Println(err)
return
}
rawB := buf[ipc.HEADER_LEN : bufSize-1]
var client ipc.Client
err = json.Unmarshal(rawB, &client)
if err != nil {
fmt.Println(err)
return
}
selected := ""
if mon.IsSelected {
selected = " (current)"
}
fmt.Printf(`Monitor %d%s:
Selected application name: %s
`, mon.Number, selected, client.Name)
}
},
}
func init() {
rootCmd.AddCommand(monitorsCmd)
}