forked from iberianpig/xSwipe
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMainController.rb
137 lines (110 loc) · 3.91 KB
/
MainController.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
# Controls the modules in order to perform all the actions
class MainController
def main(runDaemon=false)
@config = ConfigHandler.new
$DEBUG = @config.get :debug
@time_tolerance = @config.get :timeTolerance
@magnitude_tolerance = @config.get :magnitudeTolerance
@angle_tolerance = @config.get :angleTolerance
@polling_interval = @config.get :pollingInterval
@repetition_time = @config.get :repTime
@move_fingers = @config.get :move, :fingers
@move_key = @config.get :move, :key
@action = ActionHandler.new
@controller = InputController.new runDaemon, @config.get(:inputDevice)
@input_interpreter = InputHandler.new @controller
@input_interpreter.time_tolerance = @time_tolerance
@input_interpreter.magnitude_tolerance = @magnitude_tolerance
@input_interpreter.angle_tolerance = @angle_tolerance
@input_interpreter.repetition_time = @repetition_time
@input_interpreter.polling_interval = @polling_interval
@input_interpreter.move_fingers = @move_fingers
@input_interpreter.reset
@input_interpreter.mainloop self
end
def onSwipe(fingers)
angle = @input_interpreter.direction.angle
if angle <= @angle_tolerance || angle >= 360-@angle_tolerance
edge = @input_interpreter.edge.include? InputHandler::EDGE_LEFT
return swipe fingers, :right, edge
elsif angle.between?(90-@angle_tolerance, 90+@angle_tolerance)
edge = @input_interpreter.edge.include? InputHandler::EDGE_TOP
return swipe fingers, :down, edge
elsif angle.between?(180-@angle_tolerance, 180+@angle_tolerance)
edge = @input_interpreter.edge.include? InputHandler::EDGE_RIGHT
return swipe fingers, :left, edge
elsif angle.between?(270-@angle_tolerance, 270+@angle_tolerance)
edge = @input_interpreter.edge.include? InputHandler::EDGE_BOTTOM
return swipe fingers, :up, edge
end
end
def onStartMoving
@is_moving = true
@action.pressKey(@move_key)
@action.pressMouseButton(ActionHandler::MOUSE_LEFT)
debug "MoveStart"
end
def onEndMoving
@action.releaseMouseButton(ActionHandler::MOUSE_LEFT)
@action.releaseKey(@move_key)
@is_moving = false
debug "MoveStop"
end
def onPinchIn
@action.sendKeys(@config.get(:pinch, :in))
end
def onPinchOut
@action.sendKeys(@config.get(:pinch, :out))
end
def swipe(fingers, direction, edge)
action, magnitude, onetime, hold = nil
if !@is_moving
folder = (edge ? :edgeSwipe : :swipe)
action, magnitude, onetime = *get_swipe_data(folder, fingers, direction)
@action.sendKeys(action) if check_magnitude magnitude
else
action, hold, onetime = *get_move_swipe_data(:move, :swipe, fingers, direction)
self.onEndMoving
@action.sendKeys(action) if check_magnitude magnitude
self.onStartMoving
pdebug hold
self.onEndMoving unless hold
magnitude = 0
end
debug "Simulating #{action}"
return onetime
end
def get_swipe_data(*path)
debug "Getting #{path.join(', ')}"
acp = path + [:action]
mgp = path + [:magnitude]
otp = path + [:onetime]
pdebug action = @config.get(*acp)
magnitude = @config.get(*mgp)
onetime = @config.get(*otp)
onetime = false if onetime == "default"
return action, magnitude, onetime
end
def get_move_swipe_data(*path)
debug "Getting #{path.join(', ')}"
acp = path + [:action]
hlp = path + [:hold]
otp = path + [:onetime]
pdebug action = @config.get(*acp)
pdebug hold = @config.get(*hlp)
onetime = @config.get(*otp)
onetime = false if onetime == "default"
hold = true if hold == "default"
return action, hold, onetime
end
def check_magnitude(magnitude)
return true unless magnitude.is_a? Numeric
return @input_interpreter.direction.magnitude >= magnitude
end
def debug info
puts info if $debug
end
def pdebug info
p info if $debug
end
end