forked from linuxdeepin/dde-daemon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
manager.go
206 lines (173 loc) · 4.49 KB
/
manager.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
/*
* Copyright (C) 2014 ~ 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 inputdevices
import (
"bufio"
"fmt"
"os"
"os/exec"
"path/filepath"
"pkg.deepin.io/dde/daemon/common/dsync"
"pkg.deepin.io/gir/gio-2.0"
"pkg.deepin.io/lib/dbusutil"
"pkg.deepin.io/lib/dbusutil/gsprop"
"pkg.deepin.io/lib/xdg/basedir"
)
const (
gsSchemaInputDevices = "com.deepin.dde.inputdevices"
gsKeyWheelSpeed = "wheel-speed"
imWheelBin = "imwheel"
)
type devicePathInfo struct {
Path string
Type string
}
type devicePathInfos []*devicePathInfo
type Manager struct {
Infos devicePathInfos // readonly
WheelSpeed gsprop.Uint `prop:"access:rw"`
settings *gio.Settings
imWheelConfigFile string
kbd *Keyboard
mouse *Mouse
trackPoint *TrackPoint
tpad *Touchpad
wacom *Wacom
sessionSigLoop *dbusutil.SignalLoop
syncConfig *dsync.Config
}
func NewManager(service *dbusutil.Service) *Manager {
var m = new(Manager)
m.imWheelConfigFile = filepath.Join(basedir.GetUserHomeDir(), ".imwheelrc")
m.Infos = devicePathInfos{
&devicePathInfo{
Path: kbdDBusInterface,
Type: "keyboard",
},
&devicePathInfo{
Path: mouseDBusInterface,
Type: "mouse",
},
&devicePathInfo{
Path: trackPointDBusInterface,
Type: "trackpoint",
},
&devicePathInfo{
Path: touchPadDBusInterface,
Type: "touchpad",
},
}
m.settings = gio.NewSettings(gsSchemaInputDevices)
m.WheelSpeed.Bind(m.settings, gsKeyWheelSpeed)
m.kbd = newKeyboard(service)
m.wacom = newWacom(service)
m.tpad = newTouchpad(service)
m.mouse = newMouse(service, m.tpad)
m.trackPoint = newTrackPoint(service)
m.sessionSigLoop = dbusutil.NewSignalLoop(service.Conn(), 10)
m.syncConfig = dsync.NewConfig("peripherals", &syncConfig{m: m},
m.sessionSigLoop, dbusPath, logger)
return m
}
func (m *Manager) setWheelSpeed(inInit bool) {
speed := m.settings.GetUint(gsKeyWheelSpeed)
// speed range is [1,100]
logger.Debug("setWheelSpeed", speed)
var err error
shouldWrite := true
if inInit {
if _, err := os.Stat(m.imWheelConfigFile); err == nil {
shouldWrite = false
}
}
if shouldWrite {
err = writeImWheelConfig(m.imWheelConfigFile, speed)
if err != nil {
logger.Warning("failed to write imwheel config file:", err)
return
}
}
err = controlImWheel(speed)
if err != nil {
logger.Warning("failed to control imwheel:", err)
return
}
}
func controlImWheel(speed uint32) error {
if speed == 1 {
// quit
return exec.Command(imWheelBin, "-k", "-q").Run()
}
// restart
return exec.Command(imWheelBin, "-k", "-b", "4 5").Run()
}
func writeImWheelConfig(file string, speed uint32) error {
logger.Debugf("writeImWheelConfig file:%q, speed: %d", file, speed)
const header = `# written by ` + dbusServiceName + `
".*"
Control_L,Up,Control_L|Button4
Control_R,Up,Control_R|Button4
Control_L,Down,Control_L|Button5
Control_R,Down,Control_R|Button5
Shift_L,Up,Shift_L|Button4
Shift_R,Up,Shift_R|Button4
Shift_L,Down,Shift_L|Button5
Shift_R,Down,Shift_R|Button5
`
fh, err := os.Create(file)
if err != nil {
return err
}
defer fh.Close()
writer := bufio.NewWriter(fh)
_, err = writer.Write([]byte(header))
if err != nil {
return err
}
// Delay Before Next KeyPress Event
delay := 240000 / speed
_, err = fmt.Fprintf(writer, "None,Up,Button4,%d,0,%d\n", speed, delay)
if err != nil {
return err
}
_, err = fmt.Fprintf(writer, "None,Down,Button5,%d,0,%d\n", speed, delay)
if err != nil {
return err
}
err = writer.Flush()
if err != nil {
return err
}
return fh.Sync()
}
func (m *Manager) init() {
m.kbd.init()
m.kbd.handleGSettings()
m.wacom.init()
m.wacom.handleGSettings()
m.tpad.init()
m.tpad.handleGSettings()
m.mouse.init()
m.mouse.handleGSettings()
m.trackPoint.init()
m.trackPoint.handleGSettings()
m.setWheelSpeed(true)
m.handleGSettings()
m.sessionSigLoop.Start()
}