-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.go
117 lines (93 loc) · 2.35 KB
/
main.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
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 (
"errors"
"github.com/Depau/go-iio-sensor-proxy"
"github.com/godbus/dbus"
"log"
"time"
)
func SwayRotate(orientation string) (err error) {
log.Println("Rotating:", orientation)
var degrees string
switch orientation {
case "normal":
degrees = "0"
case "right-up":
degrees = "90"
case "bottom-up":
degrees = "180"
case "left-up":
degrees = "270"
default:
return errors.New("Unrecognized orientation: " + orientation)
}
var outputs []string
outputs, err = GetOutputNames()
if err != nil {
return
}
for _, output := range outputs {
err = SwayMsg(nil, "output", output, "transform", degrees)
if err != nil {
return
}
}
// Inputs can't be rotated yet
//var inputs []SwayInput
//
//for _, input := range inputs {
// if input.Type == "touchpad" || (input.Type == "pointer" && strings.Contains(input.Name, "TrackPoint")) {
// err = SwayMsg(nil, "input", "eDP-1", "transform", degrees)
// }
//}
return
}
func Claim(sensorProxy sensorproxy.SensorProxy) {
if err := sensorProxy.ClaimAccelerometer(); err != nil {
log.Fatal("Failed to claim accelerometer:", err)
}
}
func Release(sensorProxy sensorproxy.SensorProxy) {
if err := sensorProxy.ReleaseAccelerometer(); err != nil {
log.Fatal("Failed to claim accelerometer:", err)
}
}
func getOrientationAndRotate(sensorProxy sensorproxy.SensorProxy, previous string) (newOrientation string) {
var err error
newOrientation, err = sensorProxy.GetAccelerometerOrientation()
if err != nil {
log.Fatal("Failed to get orientation:", err)
}
if newOrientation != previous {
err = SwayRotate(newOrientation)
if err != nil {
log.Fatal("Unable to rotate:", err)
}
}
return
}
func main() {
conn, err := dbus.SystemBus()
if err != nil {
log.Fatal("Failed to connect to system bus:", err)
}
sensorProxy, err := sensorproxy.NewSensorProxyFromBus(conn)
if err != nil {
log.Fatal("Failed to get sensorProxy object from DBus:", err)
}
hasAccel, err := sensorProxy.HasAccelerometer()
if err != nil {
log.Fatal("Failed to check whether device has an accelerometer:", err)
}
if !hasAccel {
log.Fatal("No accelerometer found")
}
//noinspection GoInfiniteFor
currentOrientation := "undefined"
Claim(sensorProxy)
defer Release(sensorProxy)
for {
currentOrientation = getOrientationAndRotate(sensorProxy, currentOrientation)
time.Sleep(1e9)
}
}