-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevent.rb
219 lines (160 loc) · 4.44 KB
/
event.rb
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
216
217
218
# coding: utf-8
# Event
# TODO:
# 0. OK Start an X server.
# 1. OK Listen to all the event devices.
# 2. OK remove all of the event devices, except the keyboard maybe.
# 3. OK Create a custom uinput for a mouse / MT.
# 4. OK Remove this custom uinput from the main X server.
# 5. Wait / stream custom events to the new X server.
# 6. enable/disable the keyboard at will.
# 7. (optionnal - care about the keyboard map)
# ~~ Launch some apps on the X server ?
#
# Killing process
# 1. Destroy the uinput.
# 2. Disconnect the clients - send dying message.
# 3. Destroy the X server.
require "redis"
require 'json'
require_relative 'xinput'
require_relative 'xserver'
require_relative 'pointer'
@main_display = ":0"
@main_input = InputList.new(@main_display)
# Display
display = ":99"
## Create X server
server = XServer.new(display)
p = server.build
p.start
## Load dedicated input, disable existing devices
il = InputList.new(display)
il.load_all
il.disable_all
## Create new pointer
pointer = VirtualPointer.new(display)
device = pointer.create_devices
pointer.delete
## Attach new pointer
il.enable_pointers(pointer)
## Detach from main X server
@main_input.disable_pointers(pointer)
# @main_input.enable_pointers(pointer)
## disable main mouse ?
## enable sub-mouse ?
## move it...
pointer.move(10, 10)
pointer.move(20, 20)
## Move to absolute location.
pointer.press_abs(1920, 1080)
sleep(0.2)
pointer.move_abs(460, 535)
sleep(0.2)
pointer.release_abs
sleep(0.2)
# "Logitech M325"
pointer.press_abs_mt(200, 200, 10, 0)
sleep(0.2)
pointer.move_abs_mt(200, 300, 0)
sleep(0.2)
pointer.release_abs_mt(0)
## DISPLAY=:99 xrandr -s 800x600
## DISPLAY=:99 setxkbmap fr bepo
# @main_input = InputList.new(@main_display)
# @main_input.load_all
#@main_input.get_inputs("Logitech")[0].disable
# @main_input.get_inputs("Logitech")[0].enable
## Enable the keyboard
# il.get_inputs("SIGMACHIP USB Keyboard").each { |i| i.enable }
## enable mouse
# il.get_inputs("Logitech M325").each { |i| i.enable }
## WIP
redis = Redis.new
last_x = 0
last_y = 0
# redis.subscribe_with_timeout(5, "evt:10:mouse:x") do |on|
## Enable/disable keyboard and mouse
t = Thread.new do
redis.subscribe("evt:99") do |on|
on.message do |channel, message|
m = JSON.parse(message)
if m["name"] == "captureMouse"
if(m["pressed"])
il.get_inputs("Logitech").each { |i| i.enable }
puts "Capture mouse"
else
il.get_inputs("Logitech").each { |i| i.disable }
puts "release mouse"
end
end
if m["name"] == "captureKeyboard"
if(m["pressed"])
il.get_inputs("SIGMACHIP USB Keyboard").each { |i| i.enable }
puts "capture keyboard"
else
il.get_inputs("SIGMACHIP USB Keyboard").each { |i| i.disable }
puts "release keyboard"
end
end
end
end
end
t.kill
pointers = {}
slots = {} ## Hash: ID -> slot
## Thread for mouse events
redis2 = Redis.new
t2 = Thread.new do
redis2.subscribe("evt:99") do |on|
on.message do |channel, message|
m = JSON.parse(message)
if m["name"] == "pointer"
id = m["id"]
x = (m["x"].to_f * 2048).to_i
y = (m["y"].to_f * 2048).to_i
## update
known = pointers.include?(id)
if not known
slot_id = slots.size
slots[id] = slot_id
puts "creation of #{id}. #{slot_id}, #{x}, #{y}"
# pointer.press_abs_mt(x, y, id.to_i, slot_id)
pointer.press_abs(x, y)
pointers[id] = [x,y]
# sleep(0.2)
else
slot_id = slots[id]
slot_id = 0 if slot_id.nil?
#pointer.move_abs_mt(x, y, slot_id)
pointer.move_abs(x, y)
pointers[id] = [x,y]
# puts "update of #{id}, #{x} #{y}, slot #{slot_id} "
end
end
if m["name"] == "pointerDeath"
id = m["id"]
known = pointers.include? id
if not known
puts "Death of an unknown pointer"
else
##pointer.release_abs
slot_id = slots[id]
# pointer.release_abs_mt(slot_id)
pointer.release_abs
slots.delete id
pointers.delete id
puts "Death of #{id}, slot #{slot_id}"
end
end
end
end
end
t2.kill
## Clear the pointer
pointer.delete
# serv = XServer.new 10
# p = serv.build
# p.start
# il = InputList.new(":10")
# il.load