forked from rmkit-dev/rmkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpy
165 lines (132 loc) · 4.02 KB
/
main.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
#include "../build/rmkit.h"
#include "../shared/string.h"
#include "shapes.h"
#include <sys/types.h>
#include <sys/wait.h>
pid_t _pid
class AppBackground: public ui::Widget:
public:
int byte_size
framebuffer::VirtualFB *vfb = NULL
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()
vfb = new framebuffer::VirtualFB(fw, fh)
vfb->clear_screen()
vfb->fbmem = (remarkable_color*) memcpy(vfb->fbmem, fb->fbmem, self.byte_size)
void render():
if rm2fb::IN_RM2FB_SHIM:
fb->waveform_mode = WAVEFORM_MODE_GC16
else:
fb->waveform_mode = WAVEFORM_MODE_AUTO
memcpy(fb->fbmem, vfb->fbmem, self.byte_size)
fb->perform_redraw(true)
fb->dirty = 1
class App:
public:
AppBackground* app_bg
shared_ptr<framebuffer::FB> fb
App():
fb = framebuffer::get()
w, h = fb->get_display_size()
fb->dither = framebuffer::DITHER::BAYER_2
fb->waveform_mode = WAVEFORM_MODE_DU
scene := ui::make_scene()
ui::MainLoop::set_scene(scene)
app_bg = new AppBackground(0, 0, w, h)
scene->add(app_bg)
style := ui::Stylesheet() \
.valign(ui::Style::VALIGN::MIDDLE) \
.justify(ui::Style::JUSTIFY::CENTER)
h_layout := ui::HorizontalLayout(0, h-60, w, 50, scene)
no_button := new ui::Button(0, 0, 200, 50, "cancel")
ok_button := new ui::Button(0, 0, 200, 50, "ok")
h_layout.pack_end(ok_button)
h_layout.pack_end(no_button)
shape_dropdown := new ui::TextDropdown(0, 0, 250, 50, "add")
shape_dropdown->dir = ui::DropdownButton::DIRECTION::UP
// TODO: pull these from the list of available shapes
shape_dropdown->add_section("add shape")->add_options(%{
"line",
"v. line",
"h. line",
"rect",
"circle",
"bezier"})
h_layout.pack_center(shape_dropdown)
no_button->set_style(style)
ok_button->set_style(style)
no_button->mouse.click += PLS_LAMBDA(auto &ev) {
self.cleanup()
exit(0)
}
ok_button->mouse.click += PLS_LAMBDA(auto &ev) {
self.cleanup()
ui::MainLoop::in.touch.lock()
shape_strs := vector<string>{}
for auto sh : shape::to_draw:
str := sh->to_lamp()
cmd := "echo '"+ str + "' | /opt/bin/lamp"
debug "RUNNING", cmd
_ := system("sleep 0.1")
_ = system(cmd.c_str())
_ := system("sleep 0.5")
ui::MainLoop::in.ungrab()
exit(0)
}
// TODO: this should use Shape to get the shape to use
shape_dropdown->events.selected += PLS_LAMBDA(int i) {
val := shape_dropdown->options[i]->name
r := 100
if val == "circle":
s := new shape::Circle(w/2-r/2, h/2-r/2, r, r, scene)
if val == "h. line":
s := new shape::HLine(w/2-r/2, h/2-r/2, r, r, scene)
if val == "v. line":
s := new shape::VLine(w/2-r/2, h/2-r/2, r, r, scene)
if val == "line":
s := new shape::Line(w/2-r/2, h/2-r/2, r, r, scene)
if val == "rect":
s := new shape::Rectangle(w/2-r/2, h/2-r/2, r, r, scene)
if val == "bezier":
r = 300
s := new shape::Bezier(w/2-r/2, h/2-r/2, r, r, scene)
}
void redraw(bool skip_shape=false):
app_bg->render()
void redraw(input::SynMotionEvent &ev):
redraw(false)
void cleanup():
debug "CLEANING UP", _pid
ui::MainLoop::in.ungrab()
app_bg->render()
void run():
ui::MainLoop::in.grab()
ui::MainLoop::refresh()
// just to kick off the app, we do a full redraw
ui::MainLoop::redraw()
while true:
ui::MainLoop::main()
ui::MainLoop::redraw()
ui::MainLoop::read_input()
App app
did_clean := false
void cleanup():
if did_clean:
return
did_clean = true
app.cleanup()
exit(0)
void catch_sigint(int):
debug "CAUGHT SIGINT", _pid
cleanup()
def main():
signal(SIGINT, catch_sigint);
// we fork just to be a little bit extra safe with
// unlocking the input file descriptors
_pid = fork()
if _pid:
wait(NULL)
ui::MainLoop::in.ungrab()
else:
app.run()