forked from rmkit-dev/rmkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlauncher.cpy
351 lines (278 loc) · 8.78 KB
/
launcher.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
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
// LAUNCHER FOR REMARKABLE
#include <time.h>
#include <thread>
#include <chrono>
#include <sys/types.h>
#include <dirent.h>
#include <algorithm>
#include <unordered_set>
#include "../shared/proc.h"
#include "../build/rmkit.h"
#define TIMEOUT 2
#include "config.launcher.h"
DIALOG_WIDTH := 600
DIALOG_HEIGHT := 800
#ifdef REMARKABLE
#define BIN_DIR "/home/root/apps/"
#define DRAFT_DIR "/etc/draft/"
#else
#define BIN_DIR "./src/build/"
#define DRAFT_DIR "./src/remux/draft"
#endif
class IApp:
public:
virtual void selected(string) = 0;
virtual void on_suspend() = 0;
class SuspendButton: public ui::Button:
public:
IApp *app
SuspendButton(int x, int y, int w, int h, IApp *a): ui::Button(x, y, w, h, "Suspend"):
self.app = a
void on_mouse_click(input::SynMouseEvent &ev):
self.app->on_suspend()
class AppBackground: public ui::Widget:
public:
char *buf
int byte_size
bool snapped = false
framebuffer::VirtualFB *vfb
AppBackground(int x, y, w, h): ui::Widget(x, y, w, h):
self.byte_size = w*h*sizeof(remarkable_color)
fw, fh := fb->get_display_size()
self.vfb = new framebuffer::VirtualFB(fw, fh)
buf = (char*) self.vfb->fbmem
def load_from_file():
self.vfb->load_from_png("/usr/share/remarkable/suspended.png")
def snapshot():
fb := framebuffer::get()
snapped = true
memcpy(buf, fb->fbmem, self.byte_size)
void render():
if not snapped:
return
self.fb->waveform_mode = WAVEFORM_MODE_GC16
memcpy(fb->fbmem, buf, self.byte_size)
fb->dirty = 1
class AppDialog: public ui::Pager:
public:
vector<string> binaries
vector<RMApp> apps
IApp* app
AppDialog(int x, y, w, h, IApp* a): ui::Pager(x, y, w, h, self):
self.set_title("Select an app...")
self.app = a
self.apps = {}
void add_shortcuts():
_w, _h := fb->get_display_size()
h_layout := ui::HorizontalLayout(0, 0, _w, _h, self.scene)
v_layout := ui::VerticalLayout(0, 0, _w, _h, self.scene)
b1 := new SuspendButton(0, 0, 200, 50, self.app)
h_layout.pack_end(b1)
v_layout.pack_end(b1)
vector<RMApp> read_draft_from_dir(string bin_dir):
DIR *dir
struct dirent *ent
vector<RMApp> apps
char resolved_path[PATH_MAX];
if ((dir = opendir (bin_dir.c_str())) != NULL):
while ((ent = readdir (dir)) != NULL):
str_d_name := string(ent->d_name)
if str_d_name == "." or str_d_name == "..":
continue
path := string(bin_dir) + "/" + string(ent->d_name)
ifstream filein(path)
string line
RMApp rmapp
rmapp.bin = "";
while filein.good():
getline(filein, line)
tokens := split(line, '=')
if tokens.size() == 2:
arg := tokens[0]
val := tokens[1]
if arg == "call":
rmapp.bin = val
else if arg == "desc":
rmapp.desc = val
else if arg == "name":
rmapp.name = val
else if arg == "term":
rmapp.term = val
if rmapp.bin != "":
apps.push_back(rmapp)
closedir (dir)
else:
perror ("")
return apps
def read_apps_from_dir(string bin_dir):
DIR *dir
struct dirent *ent
vector<string> filenames
char resolved_path[PATH_MAX];
if ((dir = opendir (bin_dir.c_str())) != NULL):
while ((ent = readdir (dir)) != NULL):
str_d_name := string(ent->d_name)
if str_d_name != "." and str_d_name != ".." and ends_with(str_d_name, ".exe"):
path := string(bin_dir) + string(ent->d_name)
_ := realpath(path.c_str(), resolved_path);
str_d_name = string(resolved_path)
filenames.push_back(str_d_name)
closedir (dir)
else:
perror ("")
sort(filenames.begin(),filenames.end())
return filenames
void populate():
vector<string> skip_list = { "demo.exe", "remux.exe" }
vector<string> binaries
unordered_set<string> seen
self.apps = {}
for auto a : APPS:
if a.always_show || proc::exe_exists(a.bin):
self.apps.push_back(a)
draft_binaries := read_draft_from_dir(DRAFT_DIR)
for auto a : draft_binaries:
dont_add := false
for auto s : skip_list:
if s == a.bin:
dont_add = true
if dont_add:
continue
self.apps.push_back(a)
bin_binaries := read_apps_from_dir(BIN_DIR)
for auto a : bin_binaries:
bin_str := string(a)
app_str := a.c_str()
base := basename(app_str)
dont_add := false
for auto s : skip_list:
if s == base:
dont_add = true
if dont_add:
continue
app := (RMApp) { .bin=bin_str, .name=base, .term="killall " + string(base) }
self.apps.push_back(app)
for auto a : self.apps:
auto name = a.name
if seen.find(a.bin) != seen.end():
continue
seen.insert(a.bin)
if name == "":
name = a.bin
binaries.push_back(name)
self.options = binaries
void on_row_selected(string name):
app->selected(name)
ui::MainLoop::hide_overlay()
void render_row(ui::HorizontalLayout *row, string option):
d := new ui::DialogButton(20, 0, self.w-200, self.opt_h, self, option)
d->set_justification(ui::Text::JUSTIFY::LEFT)
self.layout->pack_start(row)
row->pack_start(d)
class App: public IApp:
int lastpress
int is_pressed = false
AppDialog *app_dialog
AppBackground *app_bg
shared_ptr<framebuffer::FB> fb
public:
App():
fb = framebuffer::get()
// on resize, we exit and trust our service to restart us
fb->resize += [=](auto &e):
exit(1)
;
w, h = fb->get_display_size()
if app_dialog != NULL:
delete app_dialog
if app_bg != NULL:
delete app_bg
app_dialog = new AppDialog(0, 0, DIALOG_WIDTH, DIALOG_HEIGHT, self)
app_bg = new AppBackground(0, 0, w, h)
notebook := ui::make_scene()
notebook->add(app_bg)
ui::MainLoop::set_scene(notebook)
def handle_key_event(input::SynKeyEvent ev):
static int lastpress = RAND_MAX
static int event_press_id = 0
ui::MainLoop::handle_key_event(ev)
if is_pressed && ev.is_pressed:
return
switch ev.key:
case KEY_HOME:
if ev.is_pressed:
lastpress = time(NULL)
event_press_id = ev.id
thread *th = new thread([=]() {
this_thread::sleep_for(chrono::seconds(TIMEOUT));
if is_pressed && event_press_id == ev.id
now := time(NULL)
if now - lastpress > 1:
ui::TaskQueue::add_task([=] {
app_bg->visible = true
app_bg->snapshot()
app_dialog->populate()
app_dialog->setup_for_render()
app_dialog->add_shortcuts()
app_dialog->show()
ui::MainLoop::in.grab()
app_dialog->scene->pinned = true
});
});
else:
event_press_id = 0
is_pressed = ev.is_pressed
last_ev := &ev
void term_apps(string name=""):
for auto a : app_dialog->apps:
if a.name != name && a.term != "":
proc::launch_process(a.term, false)
// TODO: power button will cause suspend screen, why not?
void on_suspend():
ui::MainLoop::in.ungrab()
ui::MainLoop::hide_overlay()
app_bg->render()
ui::MainLoop::redraw()
print "SUSPENDING"
_w, _h := fb->get_display_size()
text := ui::Text(0, _h-64, _w, 100, "Press any button to wake")
text.font_size = 64
text.justify = ui::Text::JUSTIFY::CENTER
text.undraw()
text.render()
fb->waveform_mode = WAVEFORM_MODE_AUTO
fb->redraw_screen()
_ := system("systemctl suspend")
sleep(1)
print "RESUMING FROM SUSPEND"
fb->clear_screen()
app_bg->render()
fb->redraw_screen(true)
void selected(string name):
print "LAUNCHING APP", name
string bin
string which
for auto a : app_dialog->apps:
if a.name == name:
bin = a.bin
which = a.which
term_apps(name)
ui::MainLoop::in.ungrab()
if !proc::check_process(which):
proc::launch_process(bin, true /* check running */, true /* background */)
ui::MainLoop::hide_overlay()
ui::MainLoop::redraw()
app_bg->render()
app_bg->visible = false
def run():
ui::Text::DEFAULT_FS = 32
ui::MainLoop::key_event += PLS_DELEGATE(self.handle_key_event)
while true:
ui::MainLoop::main()
ui::MainLoop::check_resize()
if app_bg->visible:
ui::MainLoop::redraw()
ui::MainLoop::read_input()
App app
def main():
app.run()