-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathgesture_parser.cpy
168 lines (138 loc) · 4.67 KB
/
gesture_parser.cpy
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
using input::Gesture
using input::SwipeGesture
using input::TapGesture
namespace genie:
struct GestureConfigData:
string command = ""
string gesture = ""
string fingers = ""
string zone = ""
string direction = ""
string duration = ""
string min_events = ""
string distance = ""
;
void run_command(string command):
debug "RUNNING COMMAND", command
string cmd = command + "&"
c_str := cmd.c_str()
_ := system(c_str)
ui::TaskQueue::add_task([=]() {
usleep(1e3 * 50)
ui::MainLoop::reset_gestures()
})
input::SwipeGesture* build_swipe_gesture(GestureConfigData gcd):
fb := framebuffer::get()
fw, fh := fb->get_display_size()
g := new input::SwipeGesture()
if gcd.fingers != "":
g->fingers = atoi(gcd.fingers.c_str())
if gcd.zone != "":
tokens := str_utils::split(gcd.zone, ' ')
if tokens.size() != 4:
debug "ZONE MUST BE 4 FLOATS", gcd.zone
else:
x1 := int(atof((const char*) tokens[0].c_str()) * fw)
y1 := int(atof((const char*) tokens[1].c_str()) * fh)
x2 := int(atof((const char*) tokens[2].c_str()) * fw)
y2 := int(atof((const char*) tokens[3].c_str()) * fh)
g->set_coordinates(x1, y1, x2, y2)
if gcd.direction == "left":
g->direction = {-1, 0}
if gcd.direction == "right":
g->direction = {1, 0}
if gcd.direction == "up":
g->direction = {0, -1}
if gcd.direction == "down":
g->direction = {0, 1}
if gcd.distance != "":
g->distance = atof(gcd.distance.c_str())
if gcd.min_events != "":
g->min_events = atof(gcd.min_events.c_str())
g->events.activate += PLS_LAMBDA(auto d) {
if gcd.command == "":
return
run_command(gcd.command)
}
debug "ADDED SWIPE GESTURE:"
debug " command:", gcd.command
debug " gesture:", gcd.gesture
debug " fingers:", gcd.fingers
debug " min_events:", gcd.min_events
debug " zone:", g->zone.x1, g->zone.y1, g->zone.x2, g->zone.y2
debug " direction:", gcd.direction
debug " distance:", gcd.distance
return g
input::TapGesture* build_tap_gesture(GestureConfigData gcd):
fb := framebuffer::get()
fw, fh := fb->get_display_size()
g := new input::TapGesture()
if gcd.fingers != "":
g->fingers = atoi(gcd.fingers.c_str())
if gcd.duration != "":
g->duration = atof(gcd.duration.c_str())
if gcd.zone != "":
tokens := str_utils::split(gcd.zone, ' ')
if tokens.size() != 4:
debug "ZONE MUST BE 4 FLOATS", gcd.zone
else:
x1 := int(atof((const char*) tokens[0].c_str()) * fw)
y1 := int(atof((const char*) tokens[1].c_str()) * fh)
x2 := int(atof((const char*) tokens[2].c_str()) * fw)
y2 := int(atof((const char*) tokens[3].c_str()) * fh)
g->set_coordinates(x1, y1, x2, y2)
g->events.activate += PLS_LAMBDA(auto d) {
if gcd.command == "":
return
run_command(gcd.command)
}
debug "ADDED TAP GESTURE:"
debug " command:", gcd.command
debug " gesture:", gcd.gesture
debug " fingers:", gcd.fingers
debug " zone:", g->zone.x1, g->zone.y1, g->zone.x2, g->zone.y2
debug " duration:", g->duration
return g
void create_gesture(GestureConfigData gcd, vector<Gesture*> &gestures):
fb := framebuffer::get()
fw, fh := fb->get_display_size()
if gcd.gesture != "":
if gcd.gesture == "swipe":
gestures.push_back(build_swipe_gesture(gcd))
else if gcd.gesture == "tap":
gestures.push_back(build_tap_gesture(gcd))
else:
debug "Unknown gesture type:", gcd.gesture
vector<Gesture*> parse_config(vector<string> &lines):
GestureConfigData gcd
vector<Gesture*> gestures;
for auto line : lines:
str_utils::trim(line)
if line == "":
create_gesture(gcd, gestures)
gcd = {}
else if line[0] == '#':
continue
else:
tokens := str_utils::split(line, '=')
while tokens.size() > 2:
tokens[tokens.size()-2] += "=" + tokens[tokens.size()-1]
tokens.pop_back()
if tokens[0] == "gesture":
gcd.gesture = tokens[1]
if tokens[0] == "command":
gcd.command = tokens[1]
if tokens[0] == "count":
gcd.min_events = tokens[1]
if tokens[0] == "fingers":
gcd.fingers = tokens[1]
if tokens[0] == "zone":
gcd.zone = tokens[1]
if tokens[0] == "distance":
gcd.distance = tokens[1]
if tokens[0] == "duration":
gcd.duration = tokens[1]
if tokens[0] == "direction":
gcd.direction = tokens[1]
create_gesture(gcd, gestures)
return gestures