Bad BT plugin, Submenu locked elements, API updates, etc.

Thanks to WillyJL, ClaraCrazy, and XFW contributors
This commit is contained in:
MX
2023-05-13 00:14:22 +03:00
parent a7691b2d3b
commit 849f14e480
42 changed files with 3211 additions and 44 deletions

View File

@@ -0,0 +1,30 @@
#include "bad_bt_scene.h"
// Generate scene on_enter handlers array
#define ADD_SCENE(prefix, name, id) prefix##_scene_##name##_on_enter,
void (*const bad_bt_scene_on_enter_handlers[])(void*) = {
#include "bad_bt_scene_config.h"
};
#undef ADD_SCENE
// Generate scene on_event handlers array
#define ADD_SCENE(prefix, name, id) prefix##_scene_##name##_on_event,
bool (*const bad_bt_scene_on_event_handlers[])(void* context, SceneManagerEvent event) = {
#include "bad_bt_scene_config.h"
};
#undef ADD_SCENE
// Generate scene on_exit handlers array
#define ADD_SCENE(prefix, name, id) prefix##_scene_##name##_on_exit,
void (*const bad_bt_scene_on_exit_handlers[])(void* context) = {
#include "bad_bt_scene_config.h"
};
#undef ADD_SCENE
// Initialize scene handlers configuration structure
const SceneManagerHandlers bad_bt_scene_handlers = {
.on_enter_handlers = bad_bt_scene_on_enter_handlers,
.on_event_handlers = bad_bt_scene_on_event_handlers,
.on_exit_handlers = bad_bt_scene_on_exit_handlers,
.scene_num = BadBtSceneNum,
};

View File

@@ -0,0 +1,29 @@
#pragma once
#include <gui/scene_manager.h>
// Generate scene id and total number
#define ADD_SCENE(prefix, name, id) BadBtScene##id,
typedef enum {
#include "bad_bt_scene_config.h"
BadBtSceneNum,
} BadBtScene;
#undef ADD_SCENE
extern const SceneManagerHandlers bad_bt_scene_handlers;
// Generate scene on_enter handlers declaration
#define ADD_SCENE(prefix, name, id) void prefix##_scene_##name##_on_enter(void*);
#include "bad_bt_scene_config.h"
#undef ADD_SCENE
// Generate scene on_event handlers declaration
#define ADD_SCENE(prefix, name, id) \
bool prefix##_scene_##name##_on_event(void* context, SceneManagerEvent event);
#include "bad_bt_scene_config.h"
#undef ADD_SCENE
// Generate scene on_exit handlers declaration
#define ADD_SCENE(prefix, name, id) void prefix##_scene_##name##_on_exit(void* context);
#include "bad_bt_scene_config.h"
#undef ADD_SCENE

View File

@@ -0,0 +1,104 @@
#include "../bad_bt_app.h"
#include "../helpers/ducky_script.h"
#include "furi_hal_power.h"
enum VarItemListIndex {
VarItemListIndexKeyboardLayout,
VarItemListIndexBtRemember,
VarItemListIndexBtDeviceName,
VarItemListIndexBtMacAddress,
VarItemListIndexRandomizeBtMac,
};
void bad_bt_scene_config_bt_remember_callback(VariableItem* item) {
BadBtApp* bad_bt = variable_item_get_context(item);
bad_bt->bt_remember = variable_item_get_current_value_index(item);
variable_item_set_current_value_text(item, bad_bt->bt_remember ? "ON" : "OFF");
view_dispatcher_send_custom_event(bad_bt->view_dispatcher, VarItemListIndexBtRemember);
}
void bad_bt_scene_config_var_item_list_callback(void* context, uint32_t index) {
BadBtApp* bad_bt = context;
view_dispatcher_send_custom_event(bad_bt->view_dispatcher, index);
}
void bad_bt_scene_config_on_enter(void* context) {
BadBtApp* bad_bt = context;
VariableItemList* var_item_list = bad_bt->var_item_list;
VariableItem* item;
item = variable_item_list_add(var_item_list, "Keyboard layout", 0, NULL, bad_bt);
item = variable_item_list_add(
var_item_list, "BT Remember", 2, bad_bt_scene_config_bt_remember_callback, bad_bt);
variable_item_set_current_value_index(item, bad_bt->bt_remember);
variable_item_set_current_value_text(item, bad_bt->bt_remember ? "ON" : "OFF");
item = variable_item_list_add(var_item_list, "BT Device Name", 0, NULL, bad_bt);
if(bad_bt->bad_bt_script->set_bt_id) {
variable_item_set_locked(item, true, "Script has\nBT_ID cmd!\nLocked to\nset Name!");
}
item = variable_item_list_add(var_item_list, "BT MAC Address", 0, NULL, bad_bt);
if(bad_bt->bt_remember) {
variable_item_set_locked(item, true, "Remember\nmust be Off!");
} else if(bad_bt->bad_bt_script->set_bt_id) {
variable_item_set_locked(item, true, "Script has\nBT_ID cmd!\nLocked to\nset MAC!");
}
item = variable_item_list_add(var_item_list, "Randomize BT MAC", 0, NULL, bad_bt);
if(bad_bt->bt_remember) {
variable_item_set_locked(item, true, "Remember\nmust be Off!");
} else if(bad_bt->bad_bt_script->set_bt_id) {
variable_item_set_locked(item, true, "Script has\nBT_ID cmd!\nLocked to\nset MAC!");
}
variable_item_list_set_enter_callback(
var_item_list, bad_bt_scene_config_var_item_list_callback, bad_bt);
variable_item_list_set_selected_item(
var_item_list, scene_manager_get_scene_state(bad_bt->scene_manager, BadBtSceneConfig));
view_dispatcher_switch_to_view(bad_bt->view_dispatcher, BadBtAppViewConfig);
}
bool bad_bt_scene_config_on_event(void* context, SceneManagerEvent event) {
BadBtApp* bad_bt = context;
bool consumed = false;
if(event.type == SceneManagerEventTypeCustom) {
scene_manager_set_scene_state(bad_bt->scene_manager, BadBtSceneConfig, event.event);
consumed = true;
switch(event.event) {
case VarItemListIndexKeyboardLayout:
scene_manager_next_scene(bad_bt->scene_manager, BadBtSceneConfigLayout);
break;
case VarItemListIndexBtRemember:
bad_bt_config_switch_remember_mode(bad_bt);
scene_manager_previous_scene(bad_bt->scene_manager);
scene_manager_next_scene(bad_bt->scene_manager, BadBtSceneConfig);
break;
case VarItemListIndexBtDeviceName:
scene_manager_next_scene(bad_bt->scene_manager, BadBtSceneConfigName);
break;
case VarItemListIndexBtMacAddress:
scene_manager_next_scene(bad_bt->scene_manager, BadBtSceneConfigMac);
break;
case VarItemListIndexRandomizeBtMac:
furi_hal_random_fill_buf(bad_bt->config.bt_mac, BAD_BT_MAC_ADDRESS_LEN);
bt_set_profile_mac_address(bad_bt->bt, bad_bt->config.bt_mac);
break;
default:
break;
}
}
return consumed;
}
void bad_bt_scene_config_on_exit(void* context) {
BadBtApp* bad_bt = context;
VariableItemList* var_item_list = bad_bt->var_item_list;
variable_item_list_reset(var_item_list);
}

View File

@@ -0,0 +1,7 @@
ADD_SCENE(bad_bt, file_select, FileSelect)
ADD_SCENE(bad_bt, work, Work)
ADD_SCENE(bad_bt, error, Error)
ADD_SCENE(bad_bt, config, Config)
ADD_SCENE(bad_bt, config_layout, ConfigLayout)
ADD_SCENE(bad_bt, config_name, ConfigName)
ADD_SCENE(bad_bt, config_mac, ConfigMac)

View File

@@ -0,0 +1,47 @@
#include "../bad_bt_app.h"
#include "furi_hal_power.h"
#include <storage/storage.h>
static bool bad_bt_layout_select(BadBtApp* bad_bt) {
furi_assert(bad_bt);
FuriString* predefined_path;
predefined_path = furi_string_alloc();
if(!furi_string_empty(bad_bt->keyboard_layout)) {
furi_string_set(predefined_path, bad_bt->keyboard_layout);
} else {
furi_string_set(predefined_path, BAD_BT_APP_PATH_LAYOUT_FOLDER);
}
DialogsFileBrowserOptions browser_options;
dialog_file_browser_set_basic_options(
&browser_options, BAD_BT_APP_LAYOUT_EXTENSION, &I_keyboard_10px);
browser_options.base_path = BAD_BT_APP_PATH_LAYOUT_FOLDER;
browser_options.skip_assets = false;
// Input events and views are managed by file_browser
bool res = dialog_file_browser_show(
bad_bt->dialogs, bad_bt->keyboard_layout, predefined_path, &browser_options);
furi_string_free(predefined_path);
return res;
}
void bad_bt_scene_config_layout_on_enter(void* context) {
BadBtApp* bad_bt = context;
if(bad_bt_layout_select(bad_bt)) {
bad_bt_script_set_keyboard_layout(bad_bt->bad_bt_script, bad_bt->keyboard_layout);
}
scene_manager_previous_scene(bad_bt->scene_manager);
}
bool bad_bt_scene_config_layout_on_event(void* context, SceneManagerEvent event) {
UNUSED(context);
UNUSED(event);
return false;
}
void bad_bt_scene_config_layout_on_exit(void* context) {
UNUSED(context);
}

View File

@@ -0,0 +1,47 @@
#include "../bad_bt_app.h"
#define TAG "BadBtConfigMac"
void bad_bt_scene_config_mac_byte_input_callback(void* context) {
BadBtApp* bad_bt = context;
view_dispatcher_send_custom_event(bad_bt->view_dispatcher, BadBtAppCustomEventByteInputDone);
}
void bad_bt_scene_config_mac_on_enter(void* context) {
BadBtApp* bad_bt = context;
// Setup view
ByteInput* byte_input = bad_bt->byte_input;
byte_input_set_header_text(byte_input, "Set BT MAC address");
byte_input_set_result_callback(
byte_input,
bad_bt_scene_config_mac_byte_input_callback,
NULL,
bad_bt,
bad_bt->config.bt_mac,
GAP_MAC_ADDR_SIZE);
view_dispatcher_switch_to_view(bad_bt->view_dispatcher, BadBtAppViewConfigMac);
}
bool bad_bt_scene_config_mac_on_event(void* context, SceneManagerEvent event) {
BadBtApp* bad_bt = context;
bool consumed = false;
if(event.type == SceneManagerEventTypeCustom) {
if(event.event == BadBtAppCustomEventByteInputDone) {
bt_set_profile_mac_address(bad_bt->bt, bad_bt->config.bt_mac);
scene_manager_previous_scene(bad_bt->scene_manager);
consumed = true;
}
}
return consumed;
}
void bad_bt_scene_config_mac_on_exit(void* context) {
BadBtApp* bad_bt = context;
// Clear view
byte_input_set_result_callback(bad_bt->byte_input, NULL, NULL, NULL, NULL, 0);
byte_input_set_header_text(bad_bt->byte_input, "");
}

View File

@@ -0,0 +1,45 @@
#include "../bad_bt_app.h"
static void bad_bt_scene_config_name_text_input_callback(void* context) {
BadBtApp* bad_bt = context;
view_dispatcher_send_custom_event(bad_bt->view_dispatcher, BadBtAppCustomEventTextEditResult);
}
void bad_bt_scene_config_name_on_enter(void* context) {
BadBtApp* bad_bt = context;
TextInput* text_input = bad_bt->text_input;
text_input_set_header_text(text_input, "Set BT device name");
text_input_set_result_callback(
text_input,
bad_bt_scene_config_name_text_input_callback,
bad_bt,
bad_bt->config.bt_name,
BAD_BT_ADV_NAME_MAX_LEN,
true);
view_dispatcher_switch_to_view(bad_bt->view_dispatcher, BadBtAppViewConfigName);
}
bool bad_bt_scene_config_name_on_event(void* context, SceneManagerEvent event) {
BadBtApp* bad_bt = context;
bool consumed = false;
if(event.type == SceneManagerEventTypeCustom) {
consumed = true;
if(event.event == BadBtAppCustomEventTextEditResult) {
bt_set_profile_adv_name(bad_bt->bt, bad_bt->config.bt_name);
}
scene_manager_previous_scene(bad_bt->scene_manager);
}
return consumed;
}
void bad_bt_scene_config_name_on_exit(void* context) {
BadBtApp* bad_bt = context;
TextInput* text_input = bad_bt->text_input;
text_input_reset(text_input);
}

View File

@@ -0,0 +1,61 @@
#include "../bad_bt_app.h"
static void
bad_bt_scene_error_event_callback(GuiButtonType result, InputType type, void* context) {
furi_assert(context);
BadBtApp* app = context;
if((result == GuiButtonTypeLeft) && (type == InputTypeShort)) {
view_dispatcher_send_custom_event(app->view_dispatcher, BadBtCustomEventErrorBack);
}
}
void bad_bt_scene_error_on_enter(void* context) {
BadBtApp* app = context;
if(app->error == BadBtAppErrorNoFiles) {
widget_add_icon_element(app->widget, 0, 0, &I_SDQuestion_35x43);
widget_add_string_multiline_element(
app->widget,
81,
4,
AlignCenter,
AlignTop,
FontSecondary,
"No SD card or\napp data found.\nThis app will not\nwork without\nrequired files.");
widget_add_button_element(
app->widget, GuiButtonTypeLeft, "Back", bad_bt_scene_error_event_callback, app);
} else if(app->error == BadBtAppErrorCloseRpc) {
widget_add_icon_element(app->widget, 78, 0, &I_ActiveConnection_50x64);
widget_add_string_multiline_element(
app->widget, 3, 2, AlignLeft, AlignTop, FontPrimary, "Connection\nis active!");
widget_add_string_multiline_element(
app->widget,
3,
30,
AlignLeft,
AlignTop,
FontSecondary,
"Disconnect from\nPC or phone to\nuse this function.");
}
view_dispatcher_switch_to_view(app->view_dispatcher, BadBtAppViewError);
}
bool bad_bt_scene_error_on_event(void* context, SceneManagerEvent event) {
BadBtApp* app = context;
bool consumed = false;
if(event.type == SceneManagerEventTypeCustom) {
if(event.event == BadBtCustomEventErrorBack) {
view_dispatcher_stop(app->view_dispatcher);
consumed = true;
}
}
return consumed;
}
void bad_bt_scene_error_on_exit(void* context) {
BadBtApp* app = context;
widget_reset(app->widget);
}

View File

@@ -0,0 +1,49 @@
#include "../bad_bt_app.h"
#include <furi_hal_power.h>
#include <storage/storage.h>
static bool bad_bt_file_select(BadBtApp* bad_bt) {
furi_assert(bad_bt);
DialogsFileBrowserOptions browser_options;
dialog_file_browser_set_basic_options(
&browser_options, BAD_BT_APP_SCRIPT_EXTENSION, &I_badbt_10px);
browser_options.base_path = BAD_BT_APP_BASE_FOLDER;
browser_options.skip_assets = true;
// Input events and views are managed by file_browser
bool res = dialog_file_browser_show(
bad_bt->dialogs, bad_bt->file_path, bad_bt->file_path, &browser_options);
return res;
}
void bad_bt_scene_file_select_on_enter(void* context) {
BadBtApp* bad_bt = context;
if(bad_bt->bad_bt_script) {
bad_bt_script_close(bad_bt->bad_bt_script);
bad_bt->bad_bt_script = NULL;
}
if(bad_bt_file_select(bad_bt)) {
bad_bt->bad_bt_script = bad_bt_script_open(bad_bt->file_path, bad_bt->bt, bad_bt);
bad_bt_script_set_keyboard_layout(bad_bt->bad_bt_script, bad_bt->keyboard_layout);
scene_manager_next_scene(bad_bt->scene_manager, BadBtSceneWork);
} else {
view_dispatcher_stop(bad_bt->view_dispatcher);
}
}
bool bad_bt_scene_file_select_on_event(void* context, SceneManagerEvent event) {
UNUSED(context);
UNUSED(event);
// BadBtApp* bad_bt = context;
return false;
}
void bad_bt_scene_file_select_on_exit(void* context) {
UNUSED(context);
// BadBtApp* bad_bt = context;
}

View File

@@ -0,0 +1,56 @@
#include "../helpers/ducky_script.h"
#include "../bad_bt_app.h"
#include "../views/bad_bt_view.h"
#include <furi_hal.h>
#include "toolbox/path.h"
void bad_bt_scene_work_button_callback(InputKey key, void* context) {
furi_assert(context);
BadBtApp* app = context;
view_dispatcher_send_custom_event(app->view_dispatcher, key);
}
bool bad_bt_scene_work_on_event(void* context, SceneManagerEvent event) {
BadBtApp* app = context;
bool consumed = false;
if(event.type == SceneManagerEventTypeCustom) {
if(event.event == InputKeyLeft) {
if(bad_bt_is_idle_state(app->bad_bt_view)) {
scene_manager_next_scene(app->scene_manager, BadBtSceneConfig);
}
consumed = true;
} else if(event.event == InputKeyOk) {
bad_bt_script_toggle(app->bad_bt_script);
consumed = true;
}
} else if(event.type == SceneManagerEventTypeTick) {
bad_bt_set_state(app->bad_bt_view, bad_bt_script_get_state(app->bad_bt_script));
}
return consumed;
}
void bad_bt_scene_work_on_enter(void* context) {
BadBtApp* app = context;
FuriString* file_name;
file_name = furi_string_alloc();
path_extract_filename(app->file_path, file_name, true);
bad_bt_set_file_name(app->bad_bt_view, furi_string_get_cstr(file_name));
furi_string_free(file_name);
FuriString* layout;
layout = furi_string_alloc();
path_extract_filename(app->keyboard_layout, layout, true);
bad_bt_set_layout(app->bad_bt_view, furi_string_get_cstr(layout));
furi_string_free(layout);
bad_bt_set_state(app->bad_bt_view, bad_bt_script_get_state(app->bad_bt_script));
bad_bt_set_button_callback(app->bad_bt_view, bad_bt_scene_work_button_callback, app);
view_dispatcher_switch_to_view(app->view_dispatcher, BadBtAppViewWork);
}
void bad_bt_scene_work_on_exit(void* context) {
UNUSED(context);
}