forked from flipperdevices/flipperzero-firmware
-
-
Notifications
You must be signed in to change notification settings - Fork 514
/
loader_menu.c
247 lines (208 loc) · 7.48 KB
/
loader_menu.c
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
#include <gui/gui.h>
#include <gui/view_dispatcher.h>
#include <gui/modules/menu.h>
#include <gui/modules/submenu.h>
#include <assets_icons.h>
#include <applications.h>
#include <cfw.h>
#include "loader.h"
#include "loader_menu.h"
#define TAG "LoaderMenu"
struct LoaderMenu {
FuriThread* thread;
void (*closed_cb)(void*);
void* context;
};
static int32_t loader_menu_thread(void* p);
LoaderMenu* loader_menu_alloc(void (*closed_cb)(void*), void* context) {
LoaderMenu* loader_menu = malloc(sizeof(LoaderMenu));
loader_menu->closed_cb = closed_cb;
loader_menu->context = context;
loader_menu->thread = furi_thread_alloc_ex(TAG, 1024, loader_menu_thread, loader_menu);
furi_thread_start(loader_menu->thread);
return loader_menu;
}
void loader_menu_free(LoaderMenu* loader_menu) {
furi_assert(loader_menu);
furi_thread_join(loader_menu->thread);
furi_thread_free(loader_menu->thread);
free(loader_menu);
}
typedef enum {
LoaderMenuViewPrimary,
LoaderMenuViewSettings,
} LoaderMenuView;
typedef struct {
Gui* gui;
ViewDispatcher* view_dispatcher;
Menu* primary_menu;
Submenu* settings_menu;
} LoaderMenuApp;
static void loader_menu_start(const char* name) {
Loader* loader = furi_record_open(RECORD_LOADER);
loader_start_with_gui_error(loader, name, NULL);
furi_record_close(RECORD_LOADER);
}
static bool loader_menu_check_appid(uint32_t index, bool settings) {
if(settings) {
if(strstr(FLIPPER_SETTINGS_APPS[index].appid, ".fap")) {
return true;
}
} else {
if(strstr(FLIPPER_APPS[index].appid, ".fap")) {
return true;
}
}
return false;
}
static void loader_menu_callback(void* context, uint32_t index) {
UNUSED(context);
if(loader_menu_check_appid(index, false)) {
const char* name = FLIPPER_APPS[index].appid;
loader_menu_start(name);
} else {
const char* name = FLIPPER_APPS[index].name;
loader_menu_start(name);
}
}
static void loader_menu_applications_callback(void* context, uint32_t index) {
UNUSED(index);
UNUSED(context);
const char* name = LOADER_APPLICATIONS_NAME;
loader_menu_start(name);
}
static void loader_menu_external_apps_callback(void* context, uint32_t index) {
UNUSED(context);
const char* path = (const char*)index;
loader_menu_start(path);
}
static void loader_menu_settings_menu_callback(void* context, uint32_t index) {
UNUSED(context);
if(loader_menu_check_appid(index, true)) {
const char* name = FLIPPER_SETTINGS_APPS[index].appid;
loader_menu_start(name);
} else {
const char* name = FLIPPER_SETTINGS_APPS[index].name;
loader_menu_start(name);
}
}
static void loader_menu_switch_to_settings(void* context, uint32_t index) {
UNUSED(index);
LoaderMenuApp* app = context;
view_dispatcher_switch_to_view(app->view_dispatcher, LoaderMenuViewSettings);
}
static uint32_t loader_menu_switch_to_primary(void* context) {
UNUSED(context);
return LoaderMenuViewPrimary;
}
static uint32_t loader_menu_exit(void* context) {
UNUSED(context);
return VIEW_NONE;
}
static void loader_menu_build_menu(LoaderMenuApp* app, LoaderMenu* menu) {
Loader* loader = furi_record_open(RECORD_LOADER);
size_t i;
size_t manual = 0;
size_t ext = 0;
size_t ext_apps_size = loader_get_ext_main_app_list_size(loader);
size_t total_items = FLIPPER_APPS_COUNT + ext_apps_size + MANUALLY_ADDED_ITEMS_COUNT;
for(i = 0; i < total_items; i++) {
if(i < FLIPPER_APPS_COUNT) {
menu_add_item(
app->primary_menu,
FLIPPER_APPS[i].name,
FLIPPER_APPS[i].icon,
i,
loader_menu_callback,
(void*)menu);
} else if(i >= FLIPPER_APPS_COUNT) {
if(i < (FLIPPER_APPS_COUNT + ext_apps_size)) {
const ExtMainApp* ext_app = loader_get_ext_main_app_item(loader, ext);
menu_add_item(
app->primary_menu,
ext_app->name,
ext_app->icon,
(uint32_t)ext_app->path,
loader_menu_external_apps_callback,
(void*)menu);
ext++;
} else if(i >= (FLIPPER_APPS_COUNT + ext_apps_size)) {
if(manual == 0) {
menu_add_item(
app->primary_menu,
"Settings",
&A_Settings_14,
i,
loader_menu_switch_to_settings,
app);
manual++;
} else {
menu_add_item(
app->primary_menu,
LOADER_APPLICATIONS_NAME,
&A_Plugins_14,
i,
loader_menu_applications_callback,
(void*)menu);
}
}
}
}
furi_record_close(RECORD_LOADER);
};
static void loader_menu_build_submenu(LoaderMenuApp* app, LoaderMenu* loader_menu) {
size_t i;
for(i = 0; i < FLIPPER_SETTINGS_APPS_COUNT; i++) {
submenu_add_item(
app->settings_menu,
FLIPPER_SETTINGS_APPS[i].name,
i,
loader_menu_settings_menu_callback,
loader_menu);
}
}
static LoaderMenuApp* loader_menu_app_alloc(LoaderMenu* loader_menu) {
LoaderMenuApp* app = malloc(sizeof(LoaderMenuApp));
app->gui = furi_record_open(RECORD_GUI);
app->view_dispatcher = view_dispatcher_alloc();
size_t my_start_point = (size_t)CFW_SETTINGS()->start_point;
if(my_start_point < 0 || my_start_point > 14) my_start_point = 0;
app->primary_menu = menu_pos_alloc(my_start_point);
app->settings_menu = submenu_alloc();
loader_menu_build_menu(app, loader_menu);
loader_menu_build_submenu(app, loader_menu);
// Primary menu
View* primary_view = menu_get_view(app->primary_menu);
view_set_context(primary_view, app->primary_menu);
view_set_previous_callback(primary_view, loader_menu_exit);
view_dispatcher_add_view(app->view_dispatcher, LoaderMenuViewPrimary, primary_view);
// Settings menu
View* settings_view = submenu_get_view(app->settings_menu);
view_set_context(settings_view, app->settings_menu);
view_set_previous_callback(settings_view, loader_menu_switch_to_primary);
view_dispatcher_add_view(app->view_dispatcher, LoaderMenuViewSettings, settings_view);
view_dispatcher_enable_queue(app->view_dispatcher);
view_dispatcher_switch_to_view(app->view_dispatcher, LoaderMenuViewPrimary);
return app;
}
static void loader_menu_app_free(LoaderMenuApp* app) {
view_dispatcher_remove_view(app->view_dispatcher, LoaderMenuViewPrimary);
view_dispatcher_remove_view(app->view_dispatcher, LoaderMenuViewSettings);
view_dispatcher_free(app->view_dispatcher);
menu_free(app->primary_menu);
submenu_free(app->settings_menu);
furi_record_close(RECORD_GUI);
free(app);
}
static int32_t loader_menu_thread(void* p) {
LoaderMenu* loader_menu = p;
furi_assert(loader_menu);
LoaderMenuApp* app = loader_menu_app_alloc(loader_menu);
view_dispatcher_attach_to_gui(app->view_dispatcher, app->gui, ViewDispatcherTypeFullscreen);
view_dispatcher_run(app->view_dispatcher);
if(loader_menu->closed_cb) {
loader_menu->closed_cb(loader_menu->context);
}
loader_menu_app_free(app);
return 0;
}