unleashed-firmware/applications/plugins/subbrute/views/subbrute_main_view.c

241 lines
7.3 KiB
C
Raw Normal View History

#include "subbrute_main_view.h"
#include "../subbrute_i.h"
#include <input/input.h>
#include <gui/elements.h>
#include <gui/icon.h>
2022-09-24 20:46:43 +00:00
#define STATUS_BAR_Y_SHIFT 14
#define TAG "SubBruteMainView"
struct SubBruteMainView {
View* view;
SubBruteMainViewCallback callback;
void* context;
};
typedef struct {
uint8_t index;
2022-09-24 20:46:43 +00:00
uint8_t window_position;
} SubBruteMainViewModel;
void subbrute_main_view_set_callback(
SubBruteMainView* instance,
SubBruteMainViewCallback callback,
void* context) {
furi_assert(instance);
furi_assert(callback);
instance->callback = callback;
instance->context = context;
}
void subbrute_main_view_draw(Canvas* canvas, SubBruteMainViewModel* model) {
SubBruteMainViewModel* m = model;
2022-09-24 20:46:43 +00:00
// Title
canvas_set_font(canvas, FontPrimary);
canvas_draw_str_aligned(canvas, 64, 2, AlignCenter, AlignTop, "Sub-GHz Bruteforcer");
// Menu
canvas_set_color(canvas, ColorBlack);
canvas_set_font(canvas, FontSecondary);
2022-09-24 20:46:43 +00:00
uint8_t items_on_screen = 3;
const uint8_t item_height = 16;
#ifdef FURI_DEBUG
FURI_LOG_D(TAG, "window_position: %d, index: %d", model->window_position, m->index);
#endif
for(uint8_t position = 0; position < SubBruteAttackTotalCount; ++position) {
uint8_t item_position = position - model->window_position;
if(item_position < items_on_screen) {
const char* str = subbrute_get_menu_name(position);
if(m->index == position) {
canvas_draw_str_aligned(
canvas, 64, 9 + (item_position * item_height) + STATUS_BAR_Y_SHIFT, AlignCenter, AlignCenter, str);
elements_frame(canvas, 1, 2 + (item_position * item_height) + STATUS_BAR_Y_SHIFT, 125, 15);
} else {
canvas_draw_str_aligned(
canvas, 64, 9 + (item_position * item_height) + STATUS_BAR_Y_SHIFT, AlignCenter, AlignCenter, str);
}
}
}
}
bool subbrute_main_view_input(InputEvent* event, void* context) {
furi_assert(event);
furi_assert(context);
2022-09-24 20:46:43 +00:00
#ifdef FURI_DEBUG
FURI_LOG_D(TAG, "InputKey: %d", event->key);
#endif
if(event->key == InputKeyBack && event->type == InputTypeShort) {
return false;
}
SubBruteMainView* instance = context;
2022-09-24 20:46:43 +00:00
const uint8_t min_value = 0;
const uint8_t correct_total = SubBruteAttackTotalCount - 1;
2022-09-24 20:46:43 +00:00
uint8_t index = 0;
bool consumed = false;
if((event->type == InputTypeShort) || (event->type == InputTypeRepeat)) {
with_view_model(
instance->view, (SubBruteMainViewModel * model) {
bool ret = false;
2022-09-24 20:46:43 +00:00
uint8_t items_on_screen = 3;
if(event->key == InputKeyUp) {
if(model->index == min_value) {
model->index = correct_total;
} else {
model->index = CLAMP(model->index - 1, correct_total, min_value);
}
ret = true;
consumed = true;
} else if(event->key == InputKeyDown) {
if(model->index == correct_total) {
model->index = min_value;
} else {
model->index = CLAMP(model->index + 1, correct_total, min_value);
}
ret = true;
consumed = true;
}
2022-09-24 20:46:43 +00:00
if (ret) {
model->window_position = model->index;
if(model->window_position > 0) {
model->window_position -= 1;
}
if(SubBruteAttackTotalCount <= items_on_screen) {
model->window_position = 0;
} else {
if(model->window_position >=
(SubBruteAttackTotalCount - items_on_screen)) {
model->window_position =
(SubBruteAttackTotalCount - items_on_screen);
}
}
}
2022-09-24 20:46:43 +00:00
index = model->index;
return ret;
});
}
2022-09-24 20:46:43 +00:00
#ifdef FURI_DEBUG
with_view_model(
instance->view, (SubBruteMainViewModel * model) {
index = model->index;
return false;
});
FURI_LOG_I(TAG, "Index: %d", index);
#endif
if(event->key == InputKeyOk && event->type == InputTypeShort) {
2022-09-24 20:46:43 +00:00
if(index == SubBruteAttackLoadFile) {
instance->callback(SubBruteCustomEventTypeLoadFile, instance->context);
2022-09-24 20:46:43 +00:00
} else {
instance->callback(SubBruteCustomEventTypeMenuSelected, instance->context);
}
consumed = true;
}
return consumed;
}
void subbrute_main_view_enter(void* context) {
furi_assert(context);
2022-09-24 20:46:43 +00:00
#ifdef FURI_DEBUG
FURI_LOG_D(TAG, "subbrute_main_view_enter");
#endif
}
void subbrute_main_view_exit(void* context) {
furi_assert(context);
2022-09-24 20:46:43 +00:00
#ifdef FURI_DEBUG
FURI_LOG_D(TAG, "subbrute_main_view_exit");
#endif
}
SubBruteMainView* subbrute_main_view_alloc() {
SubBruteMainView* instance = malloc(sizeof(SubBruteMainView));
instance->view = view_alloc();
view_allocate_model(instance->view, ViewModelTypeLocking, sizeof(SubBruteMainViewModel));
view_set_context(instance->view, instance);
view_set_draw_callback(instance->view, (ViewDrawCallback)subbrute_main_view_draw);
view_set_input_callback(instance->view, subbrute_main_view_input);
view_set_enter_callback(instance->view, subbrute_main_view_enter);
view_set_exit_callback(instance->view, subbrute_main_view_exit);
2022-09-24 20:46:43 +00:00
with_view_model(
instance->view, (SubBruteMainViewModel * model) {
model->index = 0;
model->window_position = 0;
return true;
});
return instance;
}
void subbrute_main_view_free(SubBruteMainView* instance) {
furi_assert(instance);
view_free(instance->view);
free(instance);
}
View* subbrute_main_view_get_view(SubBruteMainView* instance) {
furi_assert(instance);
return instance->view;
}
void subbrute_main_view_set_index(SubBruteMainView* instance, uint8_t idx) {
furi_assert(instance);
2022-09-24 20:46:43 +00:00
furi_assert(idx < SubBruteAttackTotalCount - 1);
#ifdef FURI_DEBUG
FURI_LOG_I(TAG, "Set index: %d", idx);
#endif
with_view_model(
instance->view, (SubBruteMainViewModel * model) {
2022-09-24 20:46:43 +00:00
model->index = idx;
model->window_position = idx;
uint8_t items_on_screen = 3;
if(model->window_position > 0) {
model->window_position -= 1;
}
if(SubBruteAttackTotalCount <= items_on_screen) {
model->window_position = 0;
} else {
if(model->window_position >=
(SubBruteAttackTotalCount - items_on_screen)) {
model->window_position =
(SubBruteAttackTotalCount - items_on_screen);
}
}
return true;
});
}
SubBruteAttacks subbrute_main_view_get_index(SubBruteMainView* instance) {
furi_assert(instance);
2022-09-24 20:46:43 +00:00
uint8_t idx = 0;
with_view_model(
instance->view, (SubBruteMainViewModel * model) {
2022-09-24 20:46:43 +00:00
idx = model->index;
return false;
});
2022-09-24 20:46:43 +00:00
#ifdef FURI_DEBUG
FURI_LOG_D(TAG, "Get index: %d", idx);
#endif
return idx;
}