forked from Unrud/remote-touchpad
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplugin_x11.go
209 lines (197 loc) · 5.62 KB
/
plugin_x11.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
// +build x11
/*
* Copyright (c) 2018 Unrud<[email protected]>
*
* This file is part of Remote-Touchpad.
*
* Foobar 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
* (at your option) any later version.
*
* Remote-Touchpad 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 Foobar. If not, see <http://www.gnu.org/licenses/>.
*/
package main
// #cgo LDFLAGS: -lX11 -lXtst
// #include <X11/Xlib.h>
// #include <X11/Intrinsic.h>
// #include <X11/extensions/XTest.h>
import "C"
import (
"errors"
"fmt"
"os"
"sync"
"time"
"unsafe"
)
const (
typingDelay time.Duration = 250 * time.Millisecond
scrollDiv int = 20
)
type x11Plugin struct {
display *C.Display
lock sync.Mutex
scrollHorizontal, scrollVertical int
}
func InitX11Plugin() (Plugin, error) {
sessionType := os.Getenv("XDG_SESSION_TYPE")
if sessionType != "" && sessionType != "x11" {
return nil, UnsupportedPlatformError{errors.New(fmt.Sprintf(
"unsupported session type '%v'", sessionType))}
}
display := C.XOpenDisplay(nil)
if display == nil {
return nil, UnsupportedPlatformError{
errors.New("failed to connect to X server")}
}
return &x11Plugin{display: display}, nil
}
func (p *x11Plugin) Close() error {
p.lock.Lock()
defer p.lock.Unlock()
if p.display == nil {
return errors.New("X server connection closed")
}
C.XCloseDisplay(p.display)
p.display = nil
return nil
}
func (p *x11Plugin) KeyboardText(text string) error {
p.lock.Lock()
defer p.lock.Unlock()
if p.display == nil {
return errors.New("X server connection closed")
}
if text == "" {
return nil
}
var minKeycodes, maxKeycodes C.int
C.XDisplayKeycodes(p.display, &minKeycodes, &maxKeycodes)
var keysymsPerKeycode C.int
keysyms := C.XGetKeyboardMapping(p.display, C.KeyCode(minKeycodes),
maxKeycodes-minKeycodes+1, &keysymsPerKeycode)
if keysyms == nil {
return errors.New("failed to get keyboard mapping")
}
defer C.XFree(unsafe.Pointer(keysyms))
emptyKeycode := C.KeyCode(0)
keycodes:
for keycode := C.KeyCode(minKeycodes); keycode <= C.KeyCode(maxKeycodes); keycode++ {
for i := 0; i < int(keysymsPerKeycode); i++ {
keysymsIndex := int(keycode-
C.KeyCode(minKeycodes))*int(keysymsPerKeycode) + i
keysym := *(*C.KeySym)(unsafe.Pointer(uintptr(unsafe.Pointer(keysyms)) +
uintptr(keysymsIndex)*unsafe.Sizeof(*keysyms)))
if keysym != 0 {
continue keycodes
}
}
emptyKeycode = keycode
break
}
if emptyKeycode == 0 {
return errors.New("no empty keycode found")
}
keycodeMapping := make([]C.KeySym, keysymsPerKeycode)
defer func() {
for i := range keycodeMapping {
keycodeMapping[i] = 0
}
C.XChangeKeyboardMapping(p.display, C.int(emptyKeycode), keysymsPerKeycode,
(*C.KeySym)(unsafe.Pointer(&keycodeMapping[0])), 1)
C.XFlush(p.display)
}()
for _, runeValue := range text {
keysym, err := runeToKeysym(runeValue)
if err != nil {
return err
}
for i := range keycodeMapping {
keycodeMapping[i] = C.KeySym(keysym)
}
C.XChangeKeyboardMapping(p.display, C.int(emptyKeycode), keysymsPerKeycode,
(*C.KeySym)(unsafe.Pointer(&keycodeMapping[0])), 1)
C.XTestFakeKeyEvent(p.display, C.uint(emptyKeycode), C.True, 0)
C.XTestFakeKeyEvent(p.display, C.uint(emptyKeycode), C.False, 0)
// race condition!
C.XFlush(p.display)
time.Sleep(typingDelay)
}
return nil
}
func (p *x11Plugin) PointerButton(button uint, press bool) error {
p.lock.Lock()
defer p.lock.Unlock()
if p.display == nil {
return errors.New("X server connection closed")
}
if button == 0 || button > 9 {
return errors.New("unsupported pointer button")
}
var pressC C.int = C.False
if press {
pressC = C.True
}
C.XTestFakeButtonEvent(p.display, C.uint(button), pressC, 0)
C.XFlush(p.display)
return nil
}
func (p *x11Plugin) PointerMove(deltaX, deltaY int) error {
p.lock.Lock()
defer p.lock.Unlock()
if p.display == nil {
return errors.New("X server connection closed")
}
C.XTestFakeRelativeMotionEvent(p.display, C.int(deltaX), C.int(deltaY), 0)
C.XFlush(p.display)
return nil
}
func (p *x11Plugin) PointerScroll(deltaHorizontal, deltaVertical int) error {
p.lock.Lock()
stepsHorizontal := (p.scrollHorizontal + deltaHorizontal) / scrollDiv
stepsVertical := (p.scrollVertical + deltaVertical) / scrollDiv
p.scrollHorizontal = (p.scrollHorizontal + deltaHorizontal) % scrollDiv
p.scrollVertical = (p.scrollVertical + deltaVertical) % scrollDiv
p.lock.Unlock()
var buttonHorizontal uint = 7
if stepsHorizontal < 0 {
buttonHorizontal = 6
stepsHorizontal = -stepsHorizontal
}
for i := 0; i < stepsHorizontal; i++ {
if err := p.PointerButton(buttonHorizontal, true); err != nil {
return err
}
if err := p.PointerButton(buttonHorizontal, false); err != nil {
return err
}
}
var buttonVertical uint = 5
if stepsVertical < 0 {
buttonVertical = 4
stepsVertical = -stepsVertical
}
for i := 0; i < stepsVertical; i++ {
if err := p.PointerButton(buttonVertical, true); err != nil {
return err
}
if err := p.PointerButton(buttonVertical, false); err != nil {
return err
}
}
return nil
}
func (p *x11Plugin) PointerScrollFinish() error {
p.lock.Lock()
defer p.lock.Unlock()
p.scrollHorizontal = 0
p.scrollVertical = 0
return nil
}