-
Notifications
You must be signed in to change notification settings - Fork 0
/
ldtoypad.c
158 lines (125 loc) · 5.06 KB
/
ldtoypad.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
#include "ldtoypad.h"
#include <furi.h>
#include <furi_hal.h>
// #include <notification/notification_messages.h>
/* generated by fbt from .png files in images folder */
#include <ldtoypad_icons.h>
// #include "LDToyPad_app.h"
// #include <gui/gui.h>
// #include <gui/view.h>
// #include <notification/notification.h>
// #include <gui/view_dispatcher.h>
// #include <gui/modules/submenu.h>
// #include <gui/modules/dialog_ex.h>
enum UsbDebugSubmenuIndex {
UsbHidSubmenuIndexEmulateToyPad,
UsbHidSubmenuIndexSettings,
};
void ldtoypad_submenu_callback(void* context, uint32_t index) {
furi_assert(context);
LDToyPadApp* app = context;
if(index == UsbHidSubmenuIndexEmulateToyPad) {
app->view_id = LDToyPadView_EmulateToyPad;
view_dispatcher_switch_to_view(app->view_dispatcher, LDToyPadView_EmulateToyPad);
} else if(index == UsbHidSubmenuIndexSettings) {
app->view_id = LDToyPadView_Settings;
view_dispatcher_switch_to_view(app->view_dispatcher, LDToyPadView_Settings);
}
}
uint32_t ldtoypad_exit_confirm_view(void* context) {
UNUSED(context);
return LDToyPadView_ExitConfirm;
}
uint32_t ldtoypad_exit(void* context) {
UNUSED(context);
return VIEW_NONE;
}
void ldtoypad_dialog_callback(DialogExResult result, void* context) {
furi_assert(context);
LDToyPadApp* app = context;
if(result == DialogExResultLeft) {
view_dispatcher_stop(
app->view_dispatcher); // Stop the view dispatcher when Result is DialogExResultLeft
} else if(result == DialogExResultRight) {
view_dispatcher_switch_to_view(app->view_dispatcher, app->view_id); // Show the last view
} else if(result == DialogExResultCenter) {
view_dispatcher_switch_to_view(
app->view_dispatcher, LDToyPadView_Submenu); // Show the submenu
}
}
LDToyPadApp* ldtoypad_app_alloc() {
LDToyPadApp* app = malloc(sizeof(LDToyPadApp));
// Gui
app->gui = furi_record_open(RECORD_GUI);
// Notifications
// app->notifications = furi_record_open(RECORD_NOTIFICATION);
// View dispatcher
app->view_dispatcher = view_dispatcher_alloc();
view_dispatcher_enable_queue(app->view_dispatcher);
view_dispatcher_attach_to_gui(app->view_dispatcher, app->gui, ViewDispatcherTypeFullscreen);
// Submenu view
app->submenu = submenu_alloc();
// Add items to the submenu
submenu_add_item(
app->submenu,
"Emulate Toy Pad",
UsbHidSubmenuIndexEmulateToyPad,
ldtoypad_submenu_callback,
app);
submenu_add_item(
app->submenu, "Settings", UsbHidSubmenuIndexSettings, ldtoypad_submenu_callback, app);
view_set_previous_callback(submenu_get_view(app->submenu), ldtoypad_exit);
// Add the submenu view to the view dispatcher
view_dispatcher_add_view(
app->view_dispatcher, LDToyPadView_Submenu, submenu_get_view(app->submenu));
// Dialog view for exit confirmation
app->dialog = dialog_ex_alloc();
dialog_ex_set_result_callback(app->dialog, ldtoypad_dialog_callback);
dialog_ex_set_context(app->dialog, app);
dialog_ex_set_left_button_text(app->dialog, "Exit");
dialog_ex_set_right_button_text(app->dialog, "Stay");
dialog_ex_set_center_button_text(app->dialog, "Menu");
dialog_ex_set_header(app->dialog, "Close Current App?", 16, 12, AlignLeft, AlignTop);
view_dispatcher_add_view(
app->view_dispatcher, LDToyPadView_ExitConfirm, dialog_ex_get_view(app->dialog));
// Emulate ToyPad view
app->ldtoypad_emulate_view = ldtoypad_emulate_alloc(app->view_dispatcher);
view_set_previous_callback(
ldtoypad_emulate_get_view(app->ldtoypad_emulate_view), ldtoypad_exit_confirm_view);
view_dispatcher_add_view(
app->view_dispatcher,
LDToyPadView_EmulateToyPad,
ldtoypad_emulate_get_view(app->ldtoypad_emulate_view));
app->view_id = LDToyPadView_Submenu;
view_dispatcher_switch_to_view(app->view_dispatcher, app->view_id);
return app;
}
void ldtoypad_app_free(LDToyPadApp* app) {
furi_assert(app);
// Notification turn LED blue
// notification_internal_message(app->notifications, &sequence_reset_blue);
// Free all the views
view_dispatcher_remove_view(app->view_dispatcher, LDToyPadView_Submenu);
submenu_free(app->submenu);
view_dispatcher_remove_view(app->view_dispatcher, LDToyPadView_ExitConfirm);
dialog_ex_free(app->dialog);
view_dispatcher_remove_view(app->view_dispatcher, LDToyPadView_EmulateToyPad);
ldtoypad_emulate_free(app->ldtoypad_emulate_view);
// view_dispatcher_remove_view(app->view_dispatcher, LDToyPadView_Settings);
// ldtoypad_settings_free(app->ldtoypad_settings_view);
view_dispatcher_free(app->view_dispatcher);
// // Close records
furi_record_close(RECORD_GUI);
app->gui = NULL;
// furi_record_close(RECORD_NOTIFICATION);
// app->notifications = NULL;
// Free rest
free(app);
}
int32_t ldtoypad_app(void* p) {
UNUSED(p);
LDToyPadApp* app = ldtoypad_app_alloc();
view_dispatcher_run(app->view_dispatcher);
ldtoypad_app_free(app);
return 0;
}