unleashed-firmware/applications/external/multi_fuzzer/views/main_menu.c

235 lines
6.5 KiB
C
Raw Normal View History

2023-06-02 16:42:58 +03:00
#include "main_menu.h"
#include "../fuzzer_i.h"
#include <input/input.h>
2023-06-05 14:10:51 +03:00
#include "../lib/worker/protocol.h"
2023-06-02 17:48:21 +03:00
2023-06-05 14:35:15 +03:00
#define PROTOCOL_NAME_Y 12
// #define PROTOCOL_CAROUSEL
2023-06-02 16:42:58 +03:00
struct FuzzerViewMain {
View* view;
FuzzerViewMainCallback callback;
void* context;
};
typedef struct {
uint8_t proto_index;
uint8_t menu_index;
2023-06-05 14:10:51 +03:00
uint8_t proto_max;
uint8_t menu_max;
2023-06-02 16:42:58 +03:00
} FuzzerViewMainModel;
2023-06-02 20:33:28 +03:00
void fuzzer_view_main_update_data(FuzzerViewMain* view, FuzzerState state) {
furi_assert(view);
with_view_model(
view->view,
FuzzerViewMainModel * model,
{
model->proto_index = state.proto_index;
model->menu_index = state.menu_index;
},
true);
}
void fuzzer_view_main_get_state(FuzzerViewMain* view, FuzzerState* state) {
furi_assert(view);
with_view_model(
view->view,
FuzzerViewMainModel * model,
{
state->proto_index = model->proto_index;
state->menu_index = model->menu_index;
},
true);
}
2023-06-02 16:42:58 +03:00
void fuzzer_view_main_set_callback(
2023-06-02 20:33:28 +03:00
FuzzerViewMain* view,
2023-06-02 16:42:58 +03:00
FuzzerViewMainCallback callback,
void* context) {
2023-06-02 20:33:28 +03:00
furi_assert(view);
2023-06-02 16:42:58 +03:00
2023-06-02 20:33:28 +03:00
view->callback = callback;
view->context = context;
2023-06-02 16:42:58 +03:00
}
void fuzzer_view_main_draw(Canvas* canvas, FuzzerViewMainModel* model) {
2023-06-02 17:48:21 +03:00
canvas_clear(canvas);
canvas_set_color(canvas, ColorBlack);
if(model->menu_index > 0) {
canvas_set_font(canvas, FontSecondary);
canvas_draw_str_aligned(
2023-06-05 14:10:51 +03:00
canvas,
64,
24,
AlignCenter,
AlignTop,
fuzzer_proto_get_menu_label(model->menu_index - 1));
2023-06-02 17:48:21 +03:00
}
canvas_set_font(canvas, FontPrimary);
canvas_draw_str_aligned(
2023-06-05 14:10:51 +03:00
canvas, 64, 36, AlignCenter, AlignTop, fuzzer_proto_get_menu_label(model->menu_index));
2023-06-02 17:48:21 +03:00
if(model->menu_index < (model->menu_max - 1)) {
2023-06-02 17:48:21 +03:00
canvas_set_font(canvas, FontSecondary);
canvas_draw_str_aligned(
2023-06-05 14:10:51 +03:00
canvas,
64,
48,
AlignCenter,
AlignTop,
fuzzer_proto_get_menu_label(model->menu_index + 1));
2023-06-02 17:48:21 +03:00
}
canvas_set_font(canvas, FontPrimary);
2023-06-05 14:35:15 +03:00
canvas_draw_str_aligned(canvas, 27, PROTOCOL_NAME_Y, AlignCenter, AlignBottom, "<");
canvas_draw_str_aligned(
canvas,
64,
PROTOCOL_NAME_Y,
AlignCenter,
AlignBottom,
fuzzer_proto_get_name(model->proto_index));
canvas_draw_str_aligned(canvas, 101, PROTOCOL_NAME_Y, AlignCenter, AlignBottom, ">");
#ifdef PROTOCOL_CAROUSEL
canvas_set_font(canvas, FontSecondary);
canvas_draw_str_aligned(
canvas,
20,
PROTOCOL_NAME_Y,
AlignRight,
AlignBottom,
(model->proto_index > 0) ? fuzzer_proto_get_name(model->proto_index - 1) :
fuzzer_proto_get_name((model->proto_max - 1)));
2023-06-02 17:48:21 +03:00
canvas_draw_str_aligned(
2023-06-05 14:35:15 +03:00
canvas,
108,
PROTOCOL_NAME_Y,
AlignLeft,
AlignBottom,
(model->proto_index < (model->proto_max - 1)) ?
fuzzer_proto_get_name(model->proto_index + 1) :
fuzzer_proto_get_name(0));
#endif
2023-06-02 16:42:58 +03:00
}
bool fuzzer_view_main_input(InputEvent* event, void* context) {
furi_assert(context);
2023-06-02 20:33:28 +03:00
FuzzerViewMain* view = context;
2023-06-02 16:42:58 +03:00
if(event->key == InputKeyBack &&
(event->type == InputTypeLong || event->type == InputTypeShort)) {
2023-06-02 20:33:28 +03:00
view->callback(FuzzerCustomEventViewMainBack, view->context);
return true;
} else if(event->key == InputKeyOk && event->type == InputTypeShort) {
view->callback(FuzzerCustomEventViewMainOk, view->context);
2023-06-02 16:42:58 +03:00
return true;
2023-06-02 17:48:21 +03:00
} else if(event->key == InputKeyDown && event->type == InputTypeShort) {
with_view_model(
2023-06-02 20:33:28 +03:00
view->view,
2023-06-02 17:48:21 +03:00
FuzzerViewMainModel * model,
{
2023-06-05 14:10:51 +03:00
if(model->menu_index < (model->menu_max - 1)) {
2023-06-02 17:48:21 +03:00
model->menu_index++;
}
},
true);
return true;
} else if(event->key == InputKeyUp && event->type == InputTypeShort) {
with_view_model(
2023-06-02 20:33:28 +03:00
view->view,
2023-06-02 17:48:21 +03:00
FuzzerViewMainModel * model,
{
if(model->menu_index != 0) {
model->menu_index--;
}
},
true);
return true;
} else if(event->key == InputKeyLeft && event->type == InputTypeShort) {
with_view_model(
2023-06-02 20:33:28 +03:00
view->view,
2023-06-02 17:48:21 +03:00
FuzzerViewMainModel * model,
{
if(model->proto_index != 0) {
model->proto_index--;
} else {
2023-06-05 14:10:51 +03:00
model->proto_index = (model->proto_max - 1);
2023-06-02 17:48:21 +03:00
}
},
true);
return true;
} else if(event->key == InputKeyRight && event->type == InputTypeShort) {
with_view_model(
2023-06-02 20:33:28 +03:00
view->view,
2023-06-02 17:48:21 +03:00
FuzzerViewMainModel * model,
{
2023-06-05 14:10:51 +03:00
if(model->proto_index == (model->proto_max - 1)) {
2023-06-02 17:48:21 +03:00
model->proto_index = 0;
} else {
model->proto_index++;
}
},
true);
return true;
2023-06-02 16:42:58 +03:00
}
return true;
}
void fuzzer_view_main_enter(void* context) {
furi_assert(context);
}
void fuzzer_view_main_exit(void* context) {
furi_assert(context);
}
FuzzerViewMain* fuzzer_view_main_alloc() {
2023-06-02 20:33:28 +03:00
FuzzerViewMain* view = malloc(sizeof(FuzzerViewMain));
2023-06-02 16:42:58 +03:00
// View allocation and configuration
2023-06-02 20:33:28 +03:00
view->view = view_alloc();
view_allocate_model(view->view, ViewModelTypeLocking, sizeof(FuzzerViewMainModel));
view_set_context(view->view, view);
view_set_draw_callback(view->view, (ViewDrawCallback)fuzzer_view_main_draw);
view_set_input_callback(view->view, fuzzer_view_main_input);
view_set_enter_callback(view->view, fuzzer_view_main_enter);
view_set_exit_callback(view->view, fuzzer_view_main_exit);
2023-06-02 16:42:58 +03:00
with_view_model(
2023-06-02 20:33:28 +03:00
view->view,
2023-06-02 16:42:58 +03:00
FuzzerViewMainModel * model,
{
model->proto_index = 0;
2023-06-05 14:10:51 +03:00
model->proto_max = fuzzer_proto_get_count_of_protocols();
2023-06-02 16:42:58 +03:00
model->menu_index = 0;
2023-06-05 14:10:51 +03:00
model->menu_max = fuzzer_proto_get_count_of_menu_items();
2023-06-02 16:42:58 +03:00
},
true);
2023-06-02 20:33:28 +03:00
return view;
2023-06-02 16:42:58 +03:00
}
2023-06-02 20:33:28 +03:00
void fuzzer_view_main_free(FuzzerViewMain* view) {
furi_assert(view);
2023-06-02 16:42:58 +03:00
// with_view_model(
2023-06-02 20:33:28 +03:00
// view->view,
2023-06-02 16:42:58 +03:00
// FuzzerViewMainModel * model,
// {
// },
// true);
2023-06-02 20:33:28 +03:00
view_free(view->view);
free(view);
2023-06-02 16:42:58 +03:00
}
2023-06-02 20:33:28 +03:00
View* fuzzer_view_main_get_view(FuzzerViewMain* view) {
furi_assert(view);
return view->view;
2023-06-02 16:42:58 +03:00
}