-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtouchpad.go
81 lines (66 loc) · 1.54 KB
/
touchpad.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
package main
import (
"errors"
"fmt"
"os/exec"
"regexp"
"strconv"
"strings"
)
var (
idRegexp1 = regexp.MustCompile("TouchPad\\s*id\\=[0-9]{1,2}")
idRegexp2 = regexp.MustCompile("[0-9]{1,2}")
stateRegexp1 = regexp.MustCompile("(?m)Device Enabled.*$")
stateRegexp2 = regexp.MustCompile("(?m)\\d+$")
)
func getTouchpadID() (int, error) {
out, err := exec.Command("xinput", "list").Output()
if err != nil {
return 0, err
}
res1 := idRegexp1.Find(out)
if res1 == nil {
return 0, errors.New("Not found touchpad ID")
}
res2 := idRegexp2.Find(res1)
if res2 == nil {
return 0, errors.New("Not found touchpad ID")
}
return strconv.Atoi(strings.TrimSpace(string(res2)))
}
func getTouchpadState(id int) (bool, error) {
out, err := exec.Command("xinput", "list-props", fmt.Sprint(id)).Output()
if err != nil {
return false, err
}
res1 := stateRegexp1.Find(out)
if res1 == nil {
return false, errors.New("Not found touchpad state")
}
res2 := stateRegexp2.Find(res1)
if res2 == nil {
return false, errors.New("Not found touchpad state")
}
return "1" == strings.TrimSpace(string(res2)), nil
}
func enableTouchpad(id int) error {
return exec.Command("xinput", "enable", fmt.Sprint(id)).Run()
}
func disableTouchpad(id int) error {
return exec.Command("xinput", "disable", fmt.Sprint(id)).Run()
}
func toggleTouchpad() error {
id, err := getTouchpadID()
if err != nil {
return err
}
s, err := getTouchpadState(id)
if err != nil {
return err
}
if s {
return disableTouchpad(id)
} else {
return enableTouchpad(id)
}
}