forked from flipperdevices/flipperzero-catalog-sample-app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
120 lines (95 loc) · 2.82 KB
/
main.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
#include <gui/view.h>
#include <gui/modules/submenu.h>
#include <gui/modules/popup.h>
#include "../../main.h"
#include "main.h"
#define THIS_SCENE Home
AppHome* Home_alloc() {
AppHome* home = (AppHome*)malloc(sizeof(AppHome));
home->menu = submenu_alloc();
home->view = NULL;
return home;
}
void Home_free(void* ptr) {
AppHome* home = (AppHome*)ptr;
FURI_LOG_I(APP_NAME, "Freeing Home.");
if(home == NULL) {
FURI_LOG_I(APP_NAME, "Home is NULL.");
return;
}
if(home->view != NULL) {
FURI_LOG_I(APP_NAME, "Freeing View.");
view_free(home->view);
home->view = NULL;
}
// I don't know the reason why
// but this is causing NULL pointer exception
// if(home->menu != NULL) {
// FURI_LOG_I(APP_NAME, "Freeing Submenu.");
// submenu_free(home->menu);
// home->menu = NULL;
// }
free(home);
FURI_LOG_I(APP_NAME, "Home freed.");
}
View* Home_get_view(void* ptr) {
AppHome* home = (AppHome*)ptr;
if(home->view == NULL) {
home->view = submenu_get_view(home->menu);
furi_assert(home->view != NULL, "View is NULL");
}
return home->view;
}
void Home_on_submenu_item(void* context, uint32_t index) {
App* app = (App*)context;
AppHome* home = app->allocated_scenes[THIS_SCENE];
furi_assert(home != NULL, "Home is NULL");
switch(index) {
case 0:
FURI_LOG_I(APP_NAME, "Hello World");
break;
case 1:
FURI_LOG_I(APP_NAME, "About");
scene_manager_next_scene(app->scene_manager, About);
break;
case 99:
FURI_LOG_I(APP_NAME, "Exit");
Home_on_exit(app);
view_dispatcher_stop(app->view_dispatcher);
break;
default:
break;
}
}
void Home_on_enter(void* context) {
App* app = (App*)context;
AppHome* home = app->allocated_scenes[THIS_SCENE];
submenu_add_item(home->menu, "Hello World", 0, Home_on_submenu_item, app);
submenu_add_item(home->menu, "About", 1, Home_on_submenu_item, app);
submenu_add_item(home->menu, "Exit", 99, Home_on_submenu_item, app);
view_dispatcher_switch_to_view(app->view_dispatcher, THIS_SCENE);
}
bool Home_on_event(void* context, SceneManagerEvent event) {
UNUSED(context);
if(event.type == SceneManagerEventTypeBack) {
return false;
}
return true;
}
void Home_on_exit(void* context) {
App* app = (App*)context;
// if the on_exit has been ran, the app will be NULL
if(app == NULL || app->allocated_scenes == NULL) {
return;
}
AppHome* home = app->allocated_scenes[THIS_SCENE];
if(home == NULL) {
return;
}
FURI_LOG_I(APP_NAME, "Exiting Home.");
Submenu* menu = home->menu;
if(menu != NULL) {
submenu_reset(menu);
}
FURI_LOG_I(APP_NAME, "Reset submenu complete.");
}