Skip to content

Commit 71c725d

Browse files
author
okay
committedJan 14, 2021
[remux] refactor gesture parsing out of genie and use it in remux
1 parent 357bb19 commit 71c725d

File tree

5 files changed

+198
-162
lines changed

5 files changed

+198
-162
lines changed
 

‎src/genie/gesture_parser.cpy

+155
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
using input::Gesture
2+
using input::SwipeGesture
3+
using input::TapGesture
4+
5+
namespace genie:
6+
7+
struct GestureConfigData:
8+
string command = ""
9+
string gesture = ""
10+
string fingers = ""
11+
string zone = ""
12+
string direction = ""
13+
string duration = ""
14+
string min_events = ""
15+
;
16+
17+
input::SwipeGesture* build_swipe_gesture(GestureConfigData gcd):
18+
fb := framebuffer::get()
19+
fw, fh := fb->get_display_size()
20+
g := new input::SwipeGesture()
21+
if gcd.fingers != "":
22+
g->fingers = atoi(gcd.fingers.c_str())
23+
if gcd.zone != "":
24+
tokens := str_utils::split(gcd.zone, ' ')
25+
if tokens.size() != 4:
26+
debug "ZONE MUST BE 4 FLOATS", gcd.zone
27+
28+
else:
29+
x1 := int(atof((const char*) tokens[0].c_str()) * fw)
30+
y1 := int(atof((const char*) tokens[1].c_str()) * fh)
31+
x2 := int(atof((const char*) tokens[2].c_str()) * fw)
32+
y2 := int(atof((const char*) tokens[3].c_str()) * fh)
33+
34+
g->set_coordinates(x1, y1, x2, y2)
35+
36+
if gcd.direction == "left":
37+
g->direction = {-1, 0}
38+
if gcd.direction == "right":
39+
g->direction = {1, 0}
40+
if gcd.direction == "up":
41+
g->direction = {0, -1}
42+
if gcd.direction == "down":
43+
g->direction = {0, 1}
44+
45+
if gcd.min_events != "":
46+
g->min_events = atof(gcd.min_events.c_str())
47+
48+
g->events.activate += PLS_LAMBDA(auto d) {
49+
if gcd.command == "":
50+
return
51+
52+
debug "RUNNING COMMAND", gcd.command
53+
string cmd = gcd.command + string(" &")
54+
c_str := cmd.c_str()
55+
_ := system(c_str)
56+
}
57+
58+
debug "ADDED SWIPE GESTURE:"
59+
debug " command:", gcd.command
60+
debug " gesture:", gcd.gesture
61+
debug " fingers:", gcd.fingers
62+
debug " min_events:", gcd.min_events
63+
debug " zone:", g->zone.x1, g->zone.y1, g->zone.x2, g->zone.y2
64+
debug " direction:", gcd.direction
65+
return g
66+
67+
input::TapGesture* build_tap_gesture(GestureConfigData gcd):
68+
fb := framebuffer::get()
69+
fw, fh := fb->get_display_size()
70+
g := new input::TapGesture()
71+
if gcd.fingers != "":
72+
g->fingers = atoi(gcd.fingers.c_str())
73+
74+
if gcd.duration != "":
75+
g->duration = atof(gcd.duration.c_str())
76+
77+
if gcd.zone != "":
78+
tokens := str_utils::split(gcd.zone, ' ')
79+
if tokens.size() != 4:
80+
debug "ZONE MUST BE 4 FLOATS", gcd.zone
81+
82+
else:
83+
x1 := int(atof((const char*) tokens[0].c_str()) * fw)
84+
y1 := int(atof((const char*) tokens[1].c_str()) * fh)
85+
x2 := int(atof((const char*) tokens[2].c_str()) * fw)
86+
y2 := int(atof((const char*) tokens[3].c_str()) * fh)
87+
88+
g->set_coordinates(x1, y1, x2, y2)
89+
90+
g->events.activate += PLS_LAMBDA(auto d) {
91+
if gcd.command == "":
92+
return
93+
94+
debug "RUNNING COMMAND", gcd.command
95+
string cmd = gcd.command + string(" &")
96+
c_str := cmd.c_str()
97+
_ := system(c_str)
98+
}
99+
100+
debug "ADDED TAP GESTURE:"
101+
debug " command:", gcd.command
102+
debug " gesture:", gcd.gesture
103+
debug " fingers:", gcd.fingers
104+
debug " zone:", g->zone.x1, g->zone.y1, g->zone.x2, g->zone.y2
105+
debug " duration:", g->duration
106+
return g
107+
108+
void create_gesture(GestureConfigData gcd, vector<Gesture*> &gestures):
109+
fb := framebuffer::get()
110+
fw, fh := fb->get_display_size()
111+
if gcd.gesture != "":
112+
if gcd.gesture == "swipe":
113+
gestures.push_back(build_swipe_gesture(gcd))
114+
else if gcd.gesture == "tap":
115+
gestures.push_back(build_tap_gesture(gcd))
116+
else:
117+
debug "Unknown gesture type:", gcd.gesture
118+
119+
vector<Gesture*> parse_config(vector<string> &lines):
120+
GestureConfigData gcd
121+
122+
vector<Gesture*> gestures;
123+
124+
125+
for auto line : lines:
126+
str_utils::trim(line)
127+
if line == "":
128+
create_gesture(gcd, gestures)
129+
gcd = {}
130+
else if line[0] == '#':
131+
continue
132+
else:
133+
tokens := str_utils::split(line, '=')
134+
while tokens.size() > 2:
135+
tokens[tokens.size()-2] += "=" + tokens[tokens.size()-1]
136+
tokens.pop_back()
137+
138+
if tokens[0] == "gesture":
139+
gcd.gesture = tokens[1]
140+
if tokens[0] == "command":
141+
gcd.command = tokens[1]
142+
if tokens[0] == "count":
143+
gcd.min_events = tokens[1]
144+
if tokens[0] == "fingers":
145+
gcd.fingers = tokens[1]
146+
if tokens[0] == "zone":
147+
gcd.zone = tokens[1]
148+
if tokens[0] == "duration":
149+
gcd.duration = tokens[1]
150+
if tokens[0] == "direction":
151+
gcd.direction = tokens[1]
152+
153+
create_gesture(gcd, gestures)
154+
return gestures
155+

‎src/genie/main.cpy

+8-138
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33

44
#include "../build/rmkit.h"
55
#include "../shared/string.h"
6-
using namespace std
76

8-
INFILE := "genie.conf"
97

10-
using input::Gesture
11-
using input::SwipeGesture
12-
using input::TapGesture
8+
#include "gesture_parser.h"
9+
10+
INFILE := "genie.conf"
11+
using namespace std
12+
using namespace genie
1313

1414
class App:
1515
public:
@@ -38,139 +38,6 @@ class App:
3838
// fingers=2
3939
// command="/home/root/swipes/swipeup.sh"
4040

41-
struct GestureConfigData:
42-
string command = ""
43-
string gesture = ""
44-
string fingers = ""
45-
string zone = ""
46-
string direction = ""
47-
string duration = ""
48-
49-
input::SwipeGesture* build_swipe_gesture(GestureConfigData gcd):
50-
fb := framebuffer::get()
51-
fw, fh := fb->get_display_size()
52-
g := new input::SwipeGesture()
53-
if gcd.fingers != "":
54-
g->fingers = atoi(gcd.fingers.c_str())
55-
if gcd.zone != "":
56-
tokens := str_utils::split(gcd.zone, ' ')
57-
if tokens.size() != 4:
58-
debug "ZONE MUST BE 4 FLOATS", gcd.zone
59-
60-
else:
61-
x1 := int(atof((const char*) tokens[0].c_str()) * fw)
62-
y1 := int(atof((const char*) tokens[1].c_str()) * fh)
63-
x2 := int(atof((const char*) tokens[2].c_str()) * fw)
64-
y2 := int(atof((const char*) tokens[3].c_str()) * fh)
65-
66-
g->set_coordinates(x1, y1, x2, y2)
67-
68-
if gcd.direction == "left":
69-
g->direction = {-1, 0}
70-
if gcd.direction == "right":
71-
g->direction = {1, 0}
72-
if gcd.direction == "up":
73-
g->direction = {0, -1}
74-
if gcd.direction == "down":
75-
g->direction = {0, 1}
76-
77-
g->events.activate += PLS_LAMBDA(auto d) {
78-
debug "RUNNING COMMAND", gcd.command
79-
string cmd = gcd.command + string(" &")
80-
c_str := cmd.c_str()
81-
system(c_str)
82-
}
83-
84-
debug "ADDED SWIPE GESTURE:"
85-
debug " command:", gcd.command
86-
debug " gesture:", gcd.gesture
87-
debug " fingers:", gcd.fingers
88-
debug " zone:", g->zone.x1, g->zone.y1, g->zone.x2, g->zone.y2
89-
debug " direction:", gcd.direction
90-
return g
91-
92-
input::TapGesture* build_tap_gesture(GestureConfigData gcd):
93-
fb := framebuffer::get()
94-
fw, fh := fb->get_display_size()
95-
g := new input::TapGesture()
96-
if gcd.fingers != "":
97-
g->fingers = atoi(gcd.fingers.c_str())
98-
99-
if gcd.duration != "":
100-
g->duration = atof(gcd.duration.c_str())
101-
102-
if gcd.zone != "":
103-
tokens := str_utils::split(gcd.zone, ' ')
104-
if tokens.size() != 4:
105-
debug "ZONE MUST BE 4 FLOATS", gcd.zone
106-
107-
else:
108-
x1 := int(atof((const char*) tokens[0].c_str()) * fw)
109-
y1 := int(atof((const char*) tokens[1].c_str()) * fh)
110-
x2 := int(atof((const char*) tokens[2].c_str()) * fw)
111-
y2 := int(atof((const char*) tokens[3].c_str()) * fh)
112-
113-
g->set_coordinates(x1, y1, x2, y2)
114-
115-
g->events.activate += PLS_LAMBDA(auto d) {
116-
debug "RUNNING COMMAND", gcd.command
117-
string cmd = gcd.command + string(" &")
118-
c_str := cmd.c_str()
119-
system(c_str)
120-
}
121-
122-
debug "ADDED TAP GESTURE:"
123-
debug " command:", gcd.command
124-
debug " gesture:", gcd.gesture
125-
debug " fingers:", gcd.fingers
126-
debug " zone:", g->zone.x1, g->zone.y1, g->zone.x2, g->zone.y2
127-
debug " duration:", g->duration
128-
return g
129-
130-
void create_gesture(GestureConfigData gcd, vector<Gesture*> &gestures):
131-
fb := framebuffer::get()
132-
fw, fh := fb->get_display_size()
133-
if gcd.gesture != "" && gcd.command != "":
134-
if gcd.gesture == "swipe":
135-
gestures.push_back(build_swipe_gesture(gcd))
136-
else if gcd.gesture == "tap":
137-
gestures.push_back(build_tap_gesture(gcd))
138-
else:
139-
debug "Unknown gesture type:", gcd.gesture
140-
141-
vector<Gesture*> parse_config(vector<string> &lines):
142-
GestureConfigData gcd
143-
144-
145-
for auto line : lines:
146-
str_utils::trim(line)
147-
if line == "":
148-
create_gesture(gcd, ui::MainLoop::gestures)
149-
gcd = {}
150-
else if line[0] == '#':
151-
continue
152-
else:
153-
tokens := str_utils::split(line, '=')
154-
while tokens.size() > 2:
155-
tokens[tokens.size()-2] += "=" + tokens[tokens.size()-1]
156-
tokens.pop_back()
157-
158-
if tokens[0] == "gesture":
159-
gcd.gesture = tokens[1]
160-
if tokens[0] == "command":
161-
gcd.command = tokens[1]
162-
if tokens[0] == "fingers":
163-
gcd.fingers = tokens[1]
164-
if tokens[0] == "zone":
165-
gcd.zone = tokens[1]
166-
if tokens[0] == "duration":
167-
gcd.duration = tokens[1]
168-
if tokens[0] == "direction":
169-
gcd.direction = tokens[1]
170-
171-
create_gesture(gcd, ui::MainLoop::gestures)
172-
return ui::MainLoop::gestures
173-
17441
def setup_gestures(App &app):
17542
string line
17643
ifstream infile(INFILE)
@@ -184,6 +51,9 @@ def setup_gestures(App &app):
18451
debug "NO GESTURES"
18552
exit(0)
18653

54+
for auto g : gestures:
55+
ui::MainLoop::gestures.push_back(g)
56+
18757
def main(int argc, char **argv):
18858
App app
18959

‎src/remux/launcher.cpy

+30-23
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515

1616
#include "../shared/proc.h"
1717
#include "../build/rmkit.h"
18+
#include "../rmkit/util/machine_id.h"
19+
#include "../genie/gesture_parser.h"
1820

1921
TIMEOUT := 1
2022
SUSPEND_TIMER := 10
@@ -695,6 +697,33 @@ class App: public IApp:
695697

696698
return default_cmd
697699

700+
void setup_gestures():
701+
// TODO: read gesture config from remux.conf file
702+
// since we want it to be backward compatible, what shall we do?
703+
// launch_gesture=
704+
// launch_gesture=
705+
// launch_gesture=
706+
// last_app_gesture=
707+
708+
debug "SETTING UP GESTURES"
709+
710+
DEFAULT_LAUNCH_GESTURES := vector<string> %{
711+
"gesture=swipe; direction=up; zone=0 0 0.1 1",
712+
"gesture=swipe; direction=up; zone=0.9 0 1 1",
713+
"gesture=tap; fingers=3"
714+
}
715+
716+
launch_gestures := DEFAULT_LAUNCH_GESTURES
717+
718+
for auto l : launch_gestures:
719+
lines := str_utils::split(l, ';')
720+
gestures := genie::parse_config(lines)
721+
for auto g : gestures:
722+
g->events.activate += PLS_LAMBDA(auto d):
723+
self.show_launcher()
724+
;
725+
ui::MainLoop::gestures.push_back(g)
726+
698727
def run():
699728
// for koreader
700729
putenv((char*) "KO_DONT_SET_DEPTH=1")
@@ -712,30 +741,8 @@ class App: public IApp:
712741
// launches a thread that suspends on idle
713742
self.suspend_on_idle()
714743
self.open_input_fifo()
744+
self.setup_gestures()
715745

716-
left := new input::SwipeGesture()
717-
left->direction = {0, -1}
718-
left->zone = {0, 0, int(DISPLAYWIDTH*0.1), int(DISPLAYHEIGHT)}
719-
left->events.activate += PLS_LAMBDA(auto d) {
720-
self.show_launcher()
721-
}
722-
723-
right := new input::SwipeGesture()
724-
right->direction = {0, -1}
725-
right->zone = {int(DISPLAYWIDTH * 0.9), 0, int(DISPLAYWIDTH), int(DISPLAYHEIGHT)}
726-
right->events.activate += PLS_LAMBDA(auto d) {
727-
self.show_launcher()
728-
}
729-
730-
tap := new input::TapGesture()
731-
tap->fingers = 3
732-
tap->events.activate += PLS_LAMBDA(auto d) {
733-
self.show_launcher()
734-
}
735-
736-
ui::MainLoop::gestures.push_back(left)
737-
ui::MainLoop::gestures.push_back(right)
738-
ui::MainLoop::gestures.push_back(tap)
739746

740747
ui::MainLoop::key_event += PLS_DELEGATE(self.handle_key_event)
741748
ui::MainLoop::motion_event += PLS_DELEGATE(self.handle_motion_event)

0 commit comments

Comments
 (0)
Please sign in to comment.