-
Notifications
You must be signed in to change notification settings - Fork 8
/
xremote_app.c
608 lines (477 loc) · 22.3 KB
/
xremote_app.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
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
/*!
* @file flipper-xremote/xremote_app.c
@license This project is released under the GNU GPLv3 License
* @copyright (c) 2023 Sandro Kalatozishvili (s.kalatoz@gmail.com)
*
* @brief Shared functionality and data types between the apps.
*/
#include "xremote_app.h"
//////////////////////////////////////////////////////////////////////////////
// XRemote generic functions and definitions
//////////////////////////////////////////////////////////////////////////////
#define XREMOTE_APP_SETTINGS APP_DATA_PATH("xremote.cfg")
#define XREMOTE_ORIENTATION_TEXT_HORIZONTAL "Horizontal"
#define XREMOTE_ORIENTATION_TEXT_VERTICAL "Vertical"
#define XREMOTE_ORIENTATION_INDEX_HORIZONTAL 0
#define XREMOTE_ORIENTATION_INDEX_VERTICAL 1
#define XREMOTE_EXIT_BEHAVIOR_TEXT_PRESS "Press"
#define XREMOTE_EXIT_BEHAVIOR_TEXT_HOLD "Hold"
#define XREMOTE_EXIT_BEHAVIOR_INDEX_PRESS 0
#define XREMOTE_EXIT_BEHAVIOR_INDEX_HOLD 1
const NotificationSequence g_sequence_blink_purple_50 = {
&message_red_255,
&message_blue_255,
&message_delay_50,
NULL,
};
XRemoteAppExit xremote_app_get_exit_behavior(uint8_t exit_index) {
return exit_index ? XRemoteAppExitHold : XRemoteAppExitPress;
}
ViewOrientation xremote_app_get_orientation(uint8_t orientation_index) {
return orientation_index ? ViewOrientationVertical : ViewOrientationHorizontal;
}
const char* xremote_app_get_exit_str(XRemoteAppExit exit_behavior) {
return exit_behavior == XRemoteAppExitPress ? "Press" : "Hold";
}
const char* xremote_app_get_alt_names_str(uint8_t alt_names_index) {
return alt_names_index ? "On" : "Off";
}
const char* xremote_app_get_orientation_str(ViewOrientation view_orientation) {
return view_orientation == ViewOrientationHorizontal ? "Horizontal" : "Vertical";
}
uint32_t xremote_app_get_orientation_index(ViewOrientation view_orientation) {
return view_orientation == ViewOrientationHorizontal ? 0 : 1;
}
uint32_t xremote_app_get_exit_index(XRemoteAppExit exit_behavior) {
return exit_behavior == XRemoteAppExitPress ? 0 : 1;
}
void xremote_app_notification_blink(NotificationApp* notifications) {
xremote_app_assert_void(notifications);
notification_message(notifications, &g_sequence_blink_purple_50);
}
//////////////////////////////////////////////////////////////////////////////
// XRemote buttons and custom button pairs
//////////////////////////////////////////////////////////////////////////////
bool xremote_app_extension_load(XRemoteAppButtons* buttons, FuriString* path) {
Storage* storage = furi_record_open(RECORD_STORAGE);
FlipperFormat* ff = flipper_format_buffered_file_alloc(storage);
FuriString* tmp = furi_string_alloc();
bool success = false;
do {
/* Open file and read the header */
if(!flipper_format_buffered_file_open_existing(ff, furi_string_get_cstr(path))) break;
if(!flipper_format_read_string(ff, "custom_ok", tmp)) break;
furi_string_set(buttons->custom_ok, tmp);
if(!flipper_format_read_string(ff, "custom_up", tmp)) break;
furi_string_set(buttons->custom_up, tmp);
if(!flipper_format_read_string(ff, "custom_down", tmp)) break;
furi_string_set(buttons->custom_down, tmp);
if(!flipper_format_read_string(ff, "custom_left", tmp)) break;
furi_string_set(buttons->custom_left, tmp);
if(!flipper_format_read_string(ff, "custom_right", tmp)) break;
furi_string_set(buttons->custom_right, tmp);
if(!flipper_format_read_string(ff, "custom_ok_hold", tmp)) break;
furi_string_set(buttons->custom_ok_hold, tmp);
if(!flipper_format_read_string(ff, "custom_up_hold", tmp)) break;
furi_string_set(buttons->custom_up_hold, tmp);
if(!flipper_format_read_string(ff, "custom_down_hold", tmp)) break;
furi_string_set(buttons->custom_down_hold, tmp);
if(!flipper_format_read_string(ff, "custom_left_hold", tmp)) break;
furi_string_set(buttons->custom_left_hold, tmp);
if(!flipper_format_read_string(ff, "custom_right_hold", tmp)) break;
furi_string_set(buttons->custom_right_hold, tmp);
success = true;
} while(false);
furi_record_close(RECORD_STORAGE);
flipper_format_free(ff);
furi_string_free(tmp);
return success;
}
bool xremote_app_extension_store(XRemoteAppButtons* buttons, FuriString* path) {
Storage* storage = furi_record_open(RECORD_STORAGE);
FlipperFormat* ff = flipper_format_file_alloc(storage);
bool success = false;
do {
if(!flipper_format_file_open_append(ff, furi_string_get_cstr(path))) break;
if(!flipper_format_write_comment_cstr(ff, "XRemote extension")) break;
if(!flipper_format_write_string(ff, "custom_ok", buttons->custom_ok)) break;
if(!flipper_format_write_string(ff, "custom_up", buttons->custom_up)) break;
if(!flipper_format_write_string(ff, "custom_down", buttons->custom_down)) break;
if(!flipper_format_write_string(ff, "custom_left", buttons->custom_left)) break;
if(!flipper_format_write_string(ff, "custom_right", buttons->custom_right)) break;
if(!flipper_format_write_string(ff, "custom_ok_hold", buttons->custom_ok_hold)) break;
if(!flipper_format_write_string(ff, "custom_up_hold", buttons->custom_up_hold)) break;
if(!flipper_format_write_string(ff, "custom_down_hold", buttons->custom_down_hold)) break;
if(!flipper_format_write_string(ff, "custom_left_hold", buttons->custom_left_hold)) break;
if(!flipper_format_write_string(ff, "custom_right_hold", buttons->custom_right_hold))
break;
success = true;
} while(false);
furi_record_close(RECORD_STORAGE);
flipper_format_free(ff);
return success;
}
bool xremote_app_alt_names_check_and_init() {
Storage* storage = furi_record_open(RECORD_STORAGE);
FlipperFormat* ff = flipper_format_file_alloc(storage);
bool success = false;
do {
if(!flipper_format_file_open_new(ff, XREMOTE_ALT_NAMES)) break;
if(!flipper_format_write_header_cstr(ff, "XRemote Alt-Names", 1)) break;
if(!flipper_format_write_comment_cstr(ff, "")) break;
if(!flipper_format_write_string_cstr(ff, "Power", "shutdown,off,on,standby")) break;
if(!flipper_format_write_string_cstr(ff, "Setup", "settings,config,cfg")) break;
if(!flipper_format_write_string_cstr(ff, "Input", "source,select")) break;
if(!flipper_format_write_string_cstr(ff, "Menu", "osd,gui")) break;
if(!flipper_format_write_string_cstr(ff, "List", "guide")) break;
if(!flipper_format_write_string_cstr(ff, "Info", "display")) break;
if(!flipper_format_write_string_cstr(ff, "Mode", "aspect,format")) break;
if(!flipper_format_write_string_cstr(ff, "Back", "return,exit")) break;
if(!flipper_format_write_string_cstr(ff, "Ok", "enter,select")) break;
if(!flipper_format_write_string_cstr(ff, "Up", "uparrow")) break;
if(!flipper_format_write_string_cstr(ff, "Down", "downarrow")) break;
if(!flipper_format_write_string_cstr(ff, "Left", "leftarrow")) break;
if(!flipper_format_write_string_cstr(ff, "Right", "rightarrow")) break;
if(!flipper_format_write_string_cstr(ff, "Mute", "silence,silent,unmute")) break;
if(!flipper_format_write_string_cstr(ff, "Vol_up", "vol+,volume+,volup,+")) break;
if(!flipper_format_write_string_cstr(ff, "Vol_dn", "vol-,volume-,voldown,-")) break;
if(!flipper_format_write_string_cstr(ff, "Ch_next", "ch+,channel+,chup")) break;
if(!flipper_format_write_string_cstr(ff, "Ch_prev", "ch-,channel-,chdown")) break;
if(!flipper_format_write_string_cstr(ff, "Next", "next,skip,ffwd")) break;
if(!flipper_format_write_string_cstr(ff, "Prev", "prev,back,rewind,rew")) break;
if(!flipper_format_write_string_cstr(ff, "Fast_fo", "fastfwd,fastforward,ff")) break;
if(!flipper_format_write_string_cstr(ff, "Fast_ba", "fastback,fastrewind,fb")) break;
if(!flipper_format_write_string_cstr(ff, "Play_pa", "playpause,play,pause")) break;
success = true;
} while(false);
furi_record_close(RECORD_STORAGE);
flipper_format_free(ff);
return success;
}
void xremote_app_buttons_free(XRemoteAppButtons* buttons) {
xremote_app_assert_void(buttons);
infrared_remote_free(buttons->remote);
furi_string_free(buttons->custom_up);
furi_string_free(buttons->custom_down);
furi_string_free(buttons->custom_left);
furi_string_free(buttons->custom_right);
furi_string_free(buttons->custom_ok);
furi_string_free(buttons->custom_up_hold);
furi_string_free(buttons->custom_down_hold);
furi_string_free(buttons->custom_left_hold);
furi_string_free(buttons->custom_right_hold);
furi_string_free(buttons->custom_ok_hold);
free(buttons);
}
XRemoteAppButtons* xremote_app_buttons_alloc() {
XRemoteAppButtons* buttons = malloc(sizeof(XRemoteAppButtons));
buttons->remote = infrared_remote_alloc();
buttons->app_ctx = NULL;
/* Setup default buttons for custom layout */
buttons->custom_up = furi_string_alloc_set_str(XREMOTE_COMMAND_UP);
buttons->custom_down = furi_string_alloc_set_str(XREMOTE_COMMAND_DOWN);
buttons->custom_left = furi_string_alloc_set_str(XREMOTE_COMMAND_LEFT);
buttons->custom_right = furi_string_alloc_set_str(XREMOTE_COMMAND_RIGHT);
buttons->custom_ok = furi_string_alloc_set_str(XREMOTE_COMMAND_OK);
buttons->custom_up_hold = furi_string_alloc_set_str(XREMOTE_COMMAND_INPUT);
buttons->custom_down_hold = furi_string_alloc_set_str(XREMOTE_COMMAND_SETUP);
buttons->custom_left_hold = furi_string_alloc_set_str(XREMOTE_COMMAND_MENU);
buttons->custom_right_hold = furi_string_alloc_set_str(XREMOTE_COMMAND_LIST);
buttons->custom_ok_hold = furi_string_alloc_set_str(XREMOTE_COMMAND_POWER);
return buttons;
}
XRemoteAppButtons* xremote_app_buttons_load(XRemoteAppContext* app_ctx) {
/* Show file selection dialog (returns selected file path with app_ctx->file_path) */
if(!xremote_app_context_select_file(app_ctx, XREMOTE_APP_EXTENSION)) return NULL;
XRemoteAppButtons* buttons = xremote_app_buttons_alloc();
buttons->app_ctx = app_ctx;
/* Load buttons from the selected path */
if(!infrared_remote_load(buttons->remote, app_ctx->file_path)) {
xremote_app_buttons_free(buttons);
return NULL;
}
/* Load custom buttons from the selected path */
xremote_app_extension_load(buttons, app_ctx->file_path);
return buttons;
}
//////////////////////////////////////////////////////////////////////////////
// XRemote application settings
//////////////////////////////////////////////////////////////////////////////
XRemoteAppSettings* xremote_app_settings_alloc() {
XRemoteAppSettings* settings = malloc(sizeof(XRemoteAppSettings));
settings->orientation = ViewOrientationHorizontal;
settings->exit_behavior = XRemoteAppExitPress;
settings->repeat_count = 2;
settings->alt_names = 1;
return settings;
}
void xremote_app_settings_free(XRemoteAppSettings* settings) {
xremote_app_assert_void(settings);
free(settings);
}
bool xremote_app_settings_store(XRemoteAppSettings* settings) {
Storage* storage = furi_record_open(RECORD_STORAGE);
FlipperFormat* ff = flipper_format_file_alloc(storage);
FURI_LOG_I(XREMOTE_APP_TAG, "store config file: \'%s\'", XREMOTE_APP_SETTINGS);
bool success = false;
do {
/* Write header in config file */
if(!flipper_format_file_open_always(ff, XREMOTE_APP_SETTINGS)) break;
if(!flipper_format_write_header_cstr(ff, "XRemote", 1)) break;
if(!flipper_format_write_comment_cstr(ff, "")) break;
/* Write actual configuration to the settings file */
uint32_t value = xremote_app_get_orientation_index(settings->orientation);
if(!flipper_format_write_uint32(ff, "orientation", &value, 1)) break;
value = xremote_app_get_exit_index(settings->exit_behavior);
if(!flipper_format_write_uint32(ff, "appexit", &value, 1)) break;
value = settings->repeat_count;
if(!flipper_format_write_uint32(ff, "repeat", &value, 1)) break;
value = settings->alt_names;
if(!flipper_format_write_uint32(ff, "altNames", &value, 1)) break;
success = true;
} while(false);
furi_record_close(RECORD_STORAGE);
flipper_format_free(ff);
return success;
}
bool xremote_app_settings_load(XRemoteAppSettings* settings) {
Storage* storage = furi_record_open(RECORD_STORAGE);
FlipperFormat* ff = flipper_format_buffered_file_alloc(storage);
FuriString* header = furi_string_alloc();
FURI_LOG_I(XREMOTE_APP_TAG, "load config file: \'%s\'", XREMOTE_APP_SETTINGS);
uint32_t version = 0;
uint32_t value = 0;
bool success = false;
do {
/* Open file and read the header */
if(!flipper_format_buffered_file_open_existing(ff, XREMOTE_APP_SETTINGS)) break;
if(!flipper_format_read_header(ff, header, &version)) break;
if(!furi_string_equal(header, "XRemote") || (version != 1)) break;
/* Parse config data from the buffer */
if(!flipper_format_read_uint32(ff, "orientation", &value, 1)) break;
settings->orientation = xremote_app_get_orientation(value);
if(!flipper_format_read_uint32(ff, "appexit", &value, 1)) break;
settings->exit_behavior = xremote_app_get_exit_behavior(value);
if(!flipper_format_read_uint32(ff, "repeat", &value, 1)) break;
settings->repeat_count = value;
if(!flipper_format_read_uint32(ff, "altNames", &value, 1)) break;
settings->alt_names = value;
success = true;
} while(false);
furi_record_close(RECORD_STORAGE);
furi_string_free(header);
flipper_format_free(ff);
return success;
}
//////////////////////////////////////////////////////////////////////////////
// XRemote gloal context shared between every child application
//////////////////////////////////////////////////////////////////////////////
XRemoteAppContext* xremote_app_context_alloc(void* arg) {
XRemoteAppContext* ctx = malloc(sizeof(XRemoteAppContext));
ctx->app_argument = arg;
ctx->file_path = NULL;
/* Open GUI and norification records */
ctx->gui = furi_record_open(RECORD_GUI);
ctx->notifications = furi_record_open(RECORD_NOTIFICATION);
/* Allocate and load global app settings */
ctx->app_settings = xremote_app_settings_alloc();
xremote_app_settings_load(ctx->app_settings);
/* Initialize alternative names */
if(ctx->app_settings->alt_names) xremote_app_alt_names_check_and_init();
/* Allocate and setup view dispatcher */
ctx->view_dispatcher = view_dispatcher_alloc();
view_dispatcher_enable_queue(ctx->view_dispatcher);
view_dispatcher_attach_to_gui(ctx->view_dispatcher, ctx->gui, ViewDispatcherTypeFullscreen);
return ctx;
}
void xremote_app_context_free(XRemoteAppContext* ctx) {
xremote_app_assert_void(ctx);
notification_internal_message(ctx->notifications, &sequence_reset_blue);
xremote_app_settings_free(ctx->app_settings);
view_dispatcher_free(ctx->view_dispatcher);
furi_record_close(RECORD_NOTIFICATION);
furi_record_close(RECORD_GUI);
if(ctx->file_path != NULL) {
furi_string_free(ctx->file_path);
ctx->file_path = NULL;
}
free(ctx);
}
bool xremote_app_browser_select_file(FuriString** file_path, const char* extension) {
DialogsApp* dialogs = furi_record_open(RECORD_DIALOGS);
Storage* storage = furi_record_open(RECORD_STORAGE);
storage_simply_mkdir(storage, XREMOTE_APP_FOLDER);
if(*file_path == NULL) {
*file_path = furi_string_alloc();
furi_string_set(*file_path, XREMOTE_APP_FOLDER);
}
/* Open file browser (view and dialogs are managed by the browser itself) */
DialogsFileBrowserOptions browser;
dialog_file_browser_set_basic_options(&browser, extension, &I_IR_Icon_10x10);
browser.base_path = XREMOTE_APP_FOLDER;
/* Show file selection dialog (returns selected file path with file_path) */
bool status = dialog_file_browser_show(dialogs, *file_path, *file_path, &browser);
/* Cleanup file loading context */
furi_record_close(RECORD_STORAGE);
furi_record_close(RECORD_DIALOGS);
return status;
}
bool xremote_app_context_select_file(XRemoteAppContext* app_ctx, const char* extension) {
if(app_ctx == NULL) return false;
return xremote_app_browser_select_file(&app_ctx->file_path, extension);
}
const char* xremote_app_context_get_exit_str(XRemoteAppContext* app_ctx) {
XRemoteAppExit exit_behavior = app_ctx->app_settings->exit_behavior;
return exit_behavior == XRemoteAppExitHold ? "Hold to exit" : "Press to exit";
}
void xremote_app_context_notify_led(XRemoteAppContext* app_ctx) {
xremote_app_assert_void(app_ctx);
xremote_app_notification_blink(app_ctx->notifications);
}
bool xremote_app_send_signal(XRemoteAppContext* app_ctx, InfraredSignal* signal) {
xremote_app_assert(signal, false);
XRemoteAppSettings* settings = app_ctx->app_settings;
infrared_signal_transmit_times(signal, settings->repeat_count);
xremote_app_context_notify_led(app_ctx);
return true;
}
//////////////////////////////////////////////////////////////////////////////
// XRemote application factory
//////////////////////////////////////////////////////////////////////////////
void xremote_app_view_alloc(XRemoteApp* app, uint32_t view_id, XRemoteViewAllocator allocator) {
furi_assert(app);
xremote_app_assert_void(app->app_ctx);
if(app->view_id == view_id && app->view_ctx != NULL) return;
xremote_app_view_free(app);
app->view_id = view_id;
app->view_ctx = allocator(app->app_ctx);
View* app_view = xremote_view_get_view(app->view_ctx);
ViewDispatcher* view_disp = app->app_ctx->view_dispatcher;
view_dispatcher_add_view(view_disp, app->view_id, app_view);
}
void xremote_app_view_alloc2(
XRemoteApp* app,
uint32_t view_id,
XRemoteViewAllocator2 allocator,
void* model_ctx) {
furi_assert(app);
xremote_app_assert_void(app->app_ctx);
if(app->view_id == view_id && app->view_ctx != NULL) return;
xremote_app_view_free(app);
app->view_id = view_id;
app->view_ctx = allocator(app->app_ctx, model_ctx);
View* app_view = xremote_view_get_view(app->view_ctx);
ViewDispatcher* view_disp = app->app_ctx->view_dispatcher;
view_dispatcher_add_view(view_disp, app->view_id, app_view);
}
void xremote_app_view_free(XRemoteApp* app) {
xremote_app_assert_void(app);
if(app->app_ctx != NULL && app->view_id != XRemoteViewNone) {
ViewDispatcher* view_disp = app->app_ctx->view_dispatcher;
view_dispatcher_remove_view(view_disp, app->view_id);
app->view_id = XRemoteViewNone;
}
if(app->view_ctx != NULL) {
xremote_view_free(app->view_ctx);
app->view_ctx = NULL;
}
}
bool xremote_app_has_view(XRemoteApp* app, uint32_t view_id) {
xremote_app_assert(app, false);
return (app->view_id == view_id || app->submenu_id == view_id);
}
void xremote_app_switch_to_view(XRemoteApp* app, uint32_t view_id) {
furi_assert(app);
xremote_app_assert_void(app->app_ctx);
ViewDispatcher* view_disp = app->app_ctx->view_dispatcher;
view_dispatcher_switch_to_view(view_disp, view_id);
}
void xremote_app_switch_to_submenu(XRemoteApp* app) {
furi_assert(app);
xremote_app_assert_void(app->app_ctx);
ViewDispatcher* view_disp = app->app_ctx->view_dispatcher;
view_dispatcher_switch_to_view(view_disp, app->submenu_id);
}
void xremote_app_submenu_add(
XRemoteApp* app,
const char* name,
uint32_t index,
SubmenuItemCallback callback) {
furi_assert(app);
xremote_app_assert_void(app->submenu);
submenu_add_item(app->submenu, name, index, callback, app);
}
void xremote_app_submenu_alloc(XRemoteApp* app, uint32_t index, ViewNavigationCallback prev_cb) {
furi_assert(app);
app->submenu = submenu_alloc();
app->submenu_id = index;
XRemoteAppSettings* settings = app->app_ctx->app_settings;
View* view = submenu_get_view(app->submenu);
view_set_previous_callback(view, prev_cb);
#if defined(FW_ORIGIN_Unleashed) || defined(FW_ORIGIN_RM)
submenu_set_orientation(app->submenu, settings->orientation);
#else
view_set_orientation(view, settings->orientation);
#endif
ViewDispatcher* view_disp = app->app_ctx->view_dispatcher;
view_dispatcher_add_view(view_disp, app->submenu_id, view);
}
void xremote_app_submenu_free(XRemoteApp* app) {
xremote_app_assert_void(app);
/* Remove submenu view from dispatcher */
if(app->submenu_id != XRemoteViewNone && app->app_ctx != NULL) {
ViewDispatcher* view_disp = app->app_ctx->view_dispatcher;
view_dispatcher_remove_view(view_disp, app->submenu_id);
app->submenu_id = XRemoteViewNone;
}
/* Free submenu */
if(app->submenu != NULL) {
submenu_free(app->submenu);
app->submenu = NULL;
}
}
void xremote_app_view_set_previous_callback(XRemoteApp* app, ViewNavigationCallback callback) {
furi_assert(app);
xremote_app_assert_void(app->view_ctx);
View* view = xremote_view_get_view(app->view_ctx);
view_set_previous_callback(view, callback);
}
void xremote_app_set_view_context(XRemoteApp* app, void* context, XRemoteClearCallback on_clear) {
furi_assert(app);
xremote_app_assert_void(app->view_ctx);
xremote_view_set_context(app->view_ctx, context, on_clear);
}
void xremote_app_set_user_context(XRemoteApp* app, void* context, XRemoteClearCallback on_clear) {
furi_assert(app);
app->on_clear = on_clear;
app->context = context;
}
void xremote_app_user_context_free(XRemoteApp* app) {
furi_assert(app);
xremote_app_assert_void(app->context);
xremote_app_assert_void(app->on_clear);
app->on_clear(app->context);
app->on_clear = NULL;
app->context = NULL;
}
XRemoteApp* xremote_app_alloc(XRemoteAppContext* ctx) {
furi_assert(ctx);
XRemoteApp* app = malloc(sizeof(XRemoteApp));
xremote_app_assert(app, NULL);
app->submenu_id = XRemoteViewNone;
app->view_id = XRemoteViewNone;
app->app_ctx = ctx;
app->submenu = NULL;
app->view_ctx = NULL;
app->on_clear = NULL;
app->context = NULL;
return app;
}
void xremote_app_free(XRemoteApp* app) {
xremote_app_assert_void(app);
xremote_app_submenu_free(app);
xremote_app_view_free(app);
/* Call clear callback if there is an user context attached */
if(app->on_clear != NULL) app->on_clear(app->context);
free(app);
}