forked from linuxdeepin/dde-daemon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lid_switch_common.go
215 lines (184 loc) · 4.53 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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/*
* Copyright (C) 2016 ~ 2018 Deepin Technology Co., Ltd.
*
* Author: jouyouyun <jouyouwen717@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
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
}