-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathwayland-hyprland.go
83 lines (72 loc) · 1.63 KB
/
wayland-hyprland.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package hyprland
import (
"fmt"
"log"
"os"
"regexp"
"strconv"
"sync"
"time"
"github.com/thiagokokada/hyprland-go"
"github.com/thiagokokada/hyprland-go/helpers"
)
type Hyprland struct {
client *hyprland.RequestClient
re *regexp.Regexp
sleep time.Duration
once bool
debug bool
m sync.RWMutex
keyboards []string
}
func New(re *regexp.Regexp, scanPeriod time.Duration, scanOnce bool, debug bool) (*Hyprland, error) {
s, err := helpers.GetSocket(helpers.RequestSocket)
if err != nil {
return nil, err
}
return &Hyprland{client: hyprland.NewClient(s), re: re, sleep: scanPeriod, once: scanOnce, debug: debug}, nil
}
func (h *Hyprland) Init() error {
go h.matchKeyboards(h.debug)
return nil
}
func (h *Hyprland) Switch(id int) {
h.m.RLock()
for _, kbd := range h.keyboards {
if h.debug {
log.Printf("switch hyprland kbd \"%s\" to group %d", kbd, id-1)
}
resp, err := h.client.SwitchXkbLayout(kbd, strconv.Itoa(id-1))
if err != nil {
fmt.Fprintf(os.Stderr, "response: %v, error: %s", resp, err)
}
}
h.m.RUnlock()
}
func (h *Hyprland) Name() string {
return "Hyprland"
}
func (h *Hyprland) Close() {}
func (h *Hyprland) matchKeyboards(debug bool) {
for {
inputs, err := getDevices(h.client)
if err != nil {
fmt.Fprintf(os.Stderr, "can't get input devices from Hyprland: %s", err)
}
h.m.Lock()
h.keyboards = nil
for _, in := range inputs {
if h.re.MatchString(in.Name) && in.Main {
if debug {
log.Printf("Hyprland keyboard matched %s", in.Name)
}
h.keyboards = append(h.keyboards, in.Name)
}
}
h.m.Unlock()
if h.once {
return
}
time.Sleep(h.sleep)
}
}