-
Notifications
You must be signed in to change notification settings - Fork 93
/
lid_switch_common.go
200 lines (169 loc) · 3.93 KB
/
lid_switch_common.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
// SPDX-FileCopyrightText: 2018 - 2022 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: GPL-3.0-or-later
package power
/*
#include <linux/input.h>
*/
import "C"
import (
"bytes"
"encoding/binary"
"fmt"
"os"
"strconv"
"strings"
"syscall"
"unsafe"
)
const (
SW_LID = C.SW_LID
SW_CNT = C.SW_CNT
SW_MAX = C.SW_MAX
EV_SW = C.EV_SW
bitsPerLong = int(unsafe.Sizeof(C.long(0))) * 8
)
// NBITS(x) ((((x)-1)/BITS_PER_LONG)+1)
func NBITS(x int) int {
return (((x) - 1) / bitsPerLong) + 1
}
// #define LONG(x) ((x)/BITS_PER_LONG)
func LONG(x int) int {
return x / bitsPerLong
}
// #define OFF(x) ((x)%BITS_PER_LONG)
func OFF(x int) int {
return x % bitsPerLong
}
//#define test_bit(bit, array) ((array[LONG(bit)] >> OFF(bit)) & 1)
func testBit(bit int, array []int) bool {
v := (array[LONG(bit)] >> uint(OFF(bit))) & 1
return v != 0
}
func upInputStrToBitmask(s string, bitmask []int) int {
var numBitsSet int
maxSize := len(bitmask)
v := strings.SplitN(s, " ", maxSize)
j := 0
for i := len(v) - 1; i >= 0; i-- {
val, _ := strconv.ParseUint(v[i], 16, 64)
bitmask[j] = int(val)
for val != 0 {
numBitsSet++
val &= val - 1
}
j++
}
return numBitsSet
}
type InputEvent struct {
Time syscall.Timeval // time in seconds since epoch at which event occurred
Type uint16 // event type - one of ecodes.EV_*
Code uint16 // event code related to the event type
Value int32 // event value related to the event type
}
// Get a useful description for an input event. Example:
// event at 1347905437.435795, code 01, type 02, val 02
func (ev *InputEvent) String() string {
return fmt.Sprintf("event at %d.%d, code %02d, type %02d, val %02d",
ev.Time.Sec, ev.Time.Usec, ev.Code, ev.Type, ev.Value)
}
const eventSize = int(unsafe.Sizeof(InputEvent{}))
func (m *Manager) findLidSwitch() string {
devices := m.gudevClient.QueryBySubsystem("input")
defer func() {
// free devices
for _, device := range devices {
device.Unref()
}
}()
for _, device := range devices {
name := device.GetName()
if !strings.HasPrefix(name, "event") {
continue
}
capSw := device.GetSysfsAttr("../capabilities/sw")
bitmask := make([]int, NBITS(SW_LID))
numBits := upInputStrToBitmask(capSw, bitmask)
if numBits == 0 || numBits >= SW_CNT {
// invalid bitmask entry
continue
}
if !testBit(SW_LID, bitmask) {
// not a lid
continue
}
deviceFile := device.GetDeviceFile()
if deviceFile != "" {
return deviceFile
}
}
return ""
}
func (m *Manager) initLidSwitchCommon() {
devFile := m.findLidSwitch()
if devFile == "" {
logger.Info("Not found lid switch")
return
}
logger.Debugf("find dev file %q", devFile)
// open
f, err := os.Open(devFile)
if err != nil {
logger.Debug("err:", err)
return
}
m.HasLidSwitch = true
go func() {
for {
events, err := readLidSwitchEvent(f)
if err != nil {
logger.Warning(err)
continue
}
for _, ev := range events {
logger.Debugf("%v", &ev)
if ev.Type == EV_SW && ev.Code == SW_LID {
logger.Debugf("lid switch event value: %v", ev.Value)
var closed bool
switch ev.Value {
case 1:
closed = true
case 0:
closed = false
default:
logger.Warningf("unknown lid switch event value %v", ev.Value)
continue
}
m.handleLidSwitchEvent(closed)
}
}
}
}()
}
func readLidSwitchEvent(f *os.File) ([]InputEvent, error) {
// read
count := 16
events := make([]InputEvent, count)
buffer := make([]byte, eventSize*count)
_, err := f.Read(buffer)
if err != nil {
logger.Debug("f read err:", err)
return nil, err
}
b := bytes.NewBuffer(buffer)
err = binary.Read(b, binary.LittleEndian, &events)
if err != nil {
logger.Debug("binary read err:", err)
return nil, err
}
// remove trailing structures
for i := range events {
logger.Debug("i", i)
if events[i].Time.Sec == 0 {
events = events[:i]
break
}
}
return events, nil
}