From 2b9d505283280f14bbf060570bf031142d3ba3ff Mon Sep 17 00:00:00 2001 From: MX <10697207+xMasterX@users.noreply.github.com> Date: Fri, 5 Apr 2024 02:59:38 +0300 Subject: [PATCH] upd badkb --- base_pack/bad_kb/bad_kb_app.c | 6 +- base_pack/bad_kb/helpers/ducky_script.c | 6 +- .../bad_kb/helpers/ducky_script_commands.c | 61 ++++++++++++++++++- base_pack/bad_kb/helpers/ducky_script_i.h | 3 + .../bad_kb/helpers/ducky_script_keycodes.c | 56 ++++++++++++++++- base_pack/bad_kb/scenes/bad_kb_scene_config.c | 1 - .../scenes/bad_kb_scene_config_layout.c | 1 - base_pack/bad_kb/scenes/bad_kb_scene_work.c | 12 ++-- base_pack/bad_kb/views/bad_kb_view.c | 20 +++--- base_pack/bad_kb/views/bad_kb_view.h | 16 ++--- 10 files changed, 150 insertions(+), 32 deletions(-) diff --git a/base_pack/bad_kb/bad_kb_app.c b/base_pack/bad_kb/bad_kb_app.c index 58dba4ee39b..3289fad49a1 100644 --- a/base_pack/bad_kb/bad_kb_app.c +++ b/base_pack/bad_kb/bad_kb_app.c @@ -345,9 +345,9 @@ BadKbApp* bad_kb_app_alloc(char* arg) { BadKbAppViewVarItemList, variable_item_list_get_view(app->var_item_list)); - app->bad_kb_view = bad_kb_alloc(); + app->bad_kb_view = bad_kb_view_alloc(); view_dispatcher_add_view( - app->view_dispatcher, BadKbAppViewWork, bad_kb_get_view(app->bad_kb_view)); + app->view_dispatcher, BadKbAppViewWork, bad_kb_view_get_view(app->bad_kb_view)); app->text_input = text_input_alloc(); view_dispatcher_add_view( @@ -389,7 +389,7 @@ void bad_kb_app_free(BadKbApp* app) { // Views view_dispatcher_remove_view(app->view_dispatcher, BadKbAppViewWork); - bad_kb_free(app->bad_kb_view); + bad_kb_view_free(app->bad_kb_view); // Custom Widget view_dispatcher_remove_view(app->view_dispatcher, BadKbAppViewWidget); diff --git a/base_pack/bad_kb/helpers/ducky_script.c b/base_pack/bad_kb/helpers/ducky_script.c index 2e7b8ae1102..4f3640051fc 100644 --- a/base_pack/bad_kb/helpers/ducky_script.c +++ b/base_pack/bad_kb/helpers/ducky_script.c @@ -576,6 +576,7 @@ static int32_t bad_kb_worker(void* context) { bad_kb->st.line_cur = 0; bad_kb->defdelay = 0; bad_kb->stringdelay = 0; + bad_kb->defstringdelay = 0; bad_kb->repeat_cnt = 0; bad_kb->key_hold_nb = 0; bad_kb->file_end = false; @@ -605,6 +606,7 @@ static int32_t bad_kb_worker(void* context) { bad_kb->st.line_cur = 0; bad_kb->defdelay = 0; bad_kb->stringdelay = 0; + bad_kb->defstringdelay = 0; bad_kb->repeat_cnt = 0; bad_kb->file_end = false; storage_file_seek(script_file, 0, true); @@ -780,10 +782,12 @@ static int32_t bad_kb_worker(void* context) { } } else if(worker_state == BadKbStateStringDelay) { // State: print string with delays FURI_LOG_D(WORKER_TAG, "delay wait"); + uint32_t delay = (bad_kb->stringdelay == 0) ? bad_kb->defstringdelay : + bad_kb->stringdelay; uint32_t flags = bad_kb_flags_get( WorkerEvtEnd | WorkerEvtStartStop | WorkerEvtPauseResume | WorkerEvtConnect | WorkerEvtDisconnect, - bad_kb->stringdelay); + delay); FURI_LOG_D(WORKER_TAG, "delay flags: %lu", flags); if(!(flags & FuriFlagError)) { diff --git a/base_pack/bad_kb/helpers/ducky_script_commands.c b/base_pack/bad_kb/helpers/ducky_script_commands.c index 0cb696c6ca9..13681f8a4f7 100644 --- a/base_pack/bad_kb/helpers/ducky_script_commands.c +++ b/base_pack/bad_kb/helpers/ducky_script_commands.c @@ -48,6 +48,17 @@ static int32_t ducky_fnc_strdelay(BadKbScript* bad_kb, const char* line, int32_t return 0; } +static int32_t ducky_fnc_defstrdelay(BadKbScript* bad_kb, const char* line, int32_t param) { + UNUSED(param); + + line = &line[ducky_get_command_len(line) + 1]; + bool state = ducky_get_number(line, &bad_kb->defstringdelay); + if(!state) { + return ducky_error(bad_kb, "Invalid number %s", line); + } + return 0; +} + static int32_t ducky_fnc_string(BadKbScript* bad_kb, const char* line, int32_t param) { line = &line[ducky_get_command_len(line) + 1]; furi_string_set_str(bad_kb->string_print, line); @@ -55,7 +66,8 @@ static int32_t ducky_fnc_string(BadKbScript* bad_kb, const char* line, int32_t p furi_string_cat(bad_kb->string_print, "\n"); } - if(bad_kb->stringdelay == 0) { // stringdelay not set - run command immediately + if(bad_kb->stringdelay == 0 && + bad_kb->defstringdelay == 0) { // stringdelay not set - run command immediately bool state = ducky_string(bad_kb, furi_string_get_cstr(bad_kb->string_print)); if(!state) { return ducky_error(bad_kb, "Invalid string %s", line); @@ -161,6 +173,49 @@ static int32_t ducky_fnc_release(BadKbScript* bad_kb, const char* line, int32_t return 0; } +static int32_t ducky_fnc_media(BadKbScript* bad_kb, const char* line, int32_t param) { + UNUSED(param); + + line = &line[ducky_get_command_len(line) + 1]; + uint16_t key = ducky_get_media_keycode_by_name(line); + if(key == HID_CONSUMER_UNASSIGNED) { + return ducky_error(bad_kb, "No keycode defined for %s", line); + } + if(bad_kb->bt) { + ble_profile_hid_kb_press(bad_kb->app->ble_hid, key); + furi_delay_ms(bt_timeout); + ble_profile_hid_kb_release(bad_kb->app->ble_hid, key); + } else { + furi_hal_hid_kb_press(key); + furi_hal_hid_kb_release(key); + } + return 0; +} + +static int32_t ducky_fnc_globe(BadKbScript* bad_kb, const char* line, int32_t param) { + UNUSED(param); + + line = &line[ducky_get_command_len(line) + 1]; + uint16_t key = ducky_get_keycode(bad_kb, line, true); + if(key == HID_KEYBOARD_NONE) { + return ducky_error(bad_kb, "No keycode defined for %s", line); + } + + if(bad_kb->bt) { + ble_profile_hid_consumer_key_press(bad_kb->app->ble_hid, HID_CONSUMER_FN_GLOBE); + ble_profile_hid_kb_press(bad_kb->app->ble_hid, key); + furi_delay_ms(bt_timeout); + ble_profile_hid_kb_release(bad_kb->app->ble_hid, key); + ble_profile_hid_consumer_key_release(bad_kb->app->ble_hid, HID_CONSUMER_FN_GLOBE); + } else { + furi_hal_hid_consumer_key_press(HID_CONSUMER_FN_GLOBE); + furi_hal_hid_kb_press(key); + furi_hal_hid_kb_release(key); + furi_hal_hid_consumer_key_release(HID_CONSUMER_FN_GLOBE); + } + return 0; +} + static int32_t ducky_fnc_waitforbutton(BadKbScript* bad_kb, const char* line, int32_t param) { UNUSED(param); UNUSED(bad_kb); @@ -180,6 +235,8 @@ static const DuckyCmd ducky_commands[] = { {"DEFAULTDELAY", ducky_fnc_defdelay, -1}, {"STRINGDELAY", ducky_fnc_strdelay, -1}, {"STRING_DELAY", ducky_fnc_strdelay, -1}, + {"DEFAULT_STRING_DELAY", ducky_fnc_defstrdelay, -1}, + {"DEFAULTSTRINGDELAY", ducky_fnc_defstrdelay, -1}, {"REPEAT", ducky_fnc_repeat, -1}, {"SYSRQ", ducky_fnc_sysrq, -1}, {"ALTCHAR", ducky_fnc_altchar, -1}, @@ -188,6 +245,8 @@ static const DuckyCmd ducky_commands[] = { {"HOLD", ducky_fnc_hold, -1}, {"RELEASE", ducky_fnc_release, -1}, {"WAIT_FOR_BUTTON_PRESS", ducky_fnc_waitforbutton, -1}, + {"MEDIA", ducky_fnc_media, -1}, + {"GLOBE", ducky_fnc_globe, -1}, }; #define TAG "BadKb" diff --git a/base_pack/bad_kb/helpers/ducky_script_i.h b/base_pack/bad_kb/helpers/ducky_script_i.h index fcc061de671..1ed2aae3f16 100644 --- a/base_pack/bad_kb/helpers/ducky_script_i.h +++ b/base_pack/bad_kb/helpers/ducky_script_i.h @@ -30,6 +30,7 @@ struct BadKbScript { uint32_t defdelay; uint32_t stringdelay; + uint32_t defstringdelay; uint16_t layout[128]; FuriString* line; @@ -52,6 +53,8 @@ bool ducky_is_line_end(const char chr); uint16_t ducky_get_keycode_by_name(const char* param); +uint16_t ducky_get_media_keycode_by_name(const char* param); + bool ducky_get_number(const char* param, uint32_t* val); void ducky_numlock_on(BadKbScript* bad_kb); diff --git a/base_pack/bad_kb/helpers/ducky_script_keycodes.c b/base_pack/bad_kb/helpers/ducky_script_keycodes.c index da2fc22f79f..290618c131b 100644 --- a/base_pack/bad_kb/helpers/ducky_script_keycodes.c +++ b/base_pack/bad_kb/helpers/ducky_script_keycodes.c @@ -1,5 +1,4 @@ #include -#include #include "ducky_script_i.h" typedef struct { @@ -64,6 +63,49 @@ static const DuckyKey ducky_keys[] = { {"F10", HID_KEYBOARD_F10}, {"F11", HID_KEYBOARD_F11}, {"F12", HID_KEYBOARD_F12}, + {"F13", HID_KEYBOARD_F13}, + {"F14", HID_KEYBOARD_F14}, + {"F15", HID_KEYBOARD_F15}, + {"F16", HID_KEYBOARD_F16}, + {"F17", HID_KEYBOARD_F17}, + {"F18", HID_KEYBOARD_F18}, + {"F19", HID_KEYBOARD_F19}, + {"F20", HID_KEYBOARD_F20}, + {"F21", HID_KEYBOARD_F21}, + {"F22", HID_KEYBOARD_F22}, + {"F23", HID_KEYBOARD_F23}, + {"F24", HID_KEYBOARD_F24}, +}; + +static const DuckyKey ducky_media_keys[] = { + {"POWER", HID_CONSUMER_POWER}, + {"REBOOT", HID_CONSUMER_RESET}, + {"SLEEP", HID_CONSUMER_SLEEP}, + {"LOGOFF", HID_CONSUMER_AL_LOGOFF}, + + {"EXIT", HID_CONSUMER_AC_EXIT}, + {"HOME", HID_CONSUMER_AC_HOME}, + {"BACK", HID_CONSUMER_AC_BACK}, + {"FORWARD", HID_CONSUMER_AC_FORWARD}, + {"REFRESH", HID_CONSUMER_AC_REFRESH}, + + {"SNAPSHOT", HID_CONSUMER_SNAPSHOT}, + + {"PLAY", HID_CONSUMER_PLAY}, + {"PAUSE", HID_CONSUMER_PAUSE}, + {"PLAY_PAUSE", HID_CONSUMER_PLAY_PAUSE}, + {"NEXT_TRACK", HID_CONSUMER_SCAN_NEXT_TRACK}, + {"PREV_TRACK", HID_CONSUMER_SCAN_PREVIOUS_TRACK}, + {"STOP", HID_CONSUMER_STOP}, + {"EJECT", HID_CONSUMER_EJECT}, + + {"MUTE", HID_CONSUMER_MUTE}, + {"VOLUME_UP", HID_CONSUMER_VOLUME_INCREMENT}, + {"VOLUME_DOWN", HID_CONSUMER_VOLUME_DECREMENT}, + + {"FN", HID_CONSUMER_FN_GLOBE}, + {"BRIGHT_UP", HID_CONSUMER_BRIGHTNESS_INCREMENT}, + {"BRIGHT_DOWN", HID_CONSUMER_BRIGHTNESS_DECREMENT}, }; uint16_t ducky_get_keycode_by_name(const char* param) { @@ -77,3 +119,15 @@ uint16_t ducky_get_keycode_by_name(const char* param) { return HID_KEYBOARD_NONE; } + +uint16_t ducky_get_media_keycode_by_name(const char* param) { + for(size_t i = 0; i < COUNT_OF(ducky_media_keys); i++) { + size_t key_cmd_len = strlen(ducky_media_keys[i].name); + if((strncmp(param, ducky_media_keys[i].name, key_cmd_len) == 0) && + (ducky_is_line_end(param[key_cmd_len]))) { + return ducky_media_keys[i].keycode; + } + } + + return HID_CONSUMER_UNASSIGNED; +} diff --git a/base_pack/bad_kb/scenes/bad_kb_scene_config.c b/base_pack/bad_kb/scenes/bad_kb_scene_config.c index 8845bd5d529..cc7cefdc02e 100644 --- a/base_pack/bad_kb/scenes/bad_kb_scene_config.c +++ b/base_pack/bad_kb/scenes/bad_kb_scene_config.c @@ -1,6 +1,5 @@ #include "../bad_kb_app_i.h" #include "furi_hal_power.h" -#include "furi_hal_usb.h" enum VarItemListIndex { VarItemListIndexKeyboardLayout, diff --git a/base_pack/bad_kb/scenes/bad_kb_scene_config_layout.c b/base_pack/bad_kb/scenes/bad_kb_scene_config_layout.c index 380bde30e63..79f574a246f 100644 --- a/base_pack/bad_kb/scenes/bad_kb_scene_config_layout.c +++ b/base_pack/bad_kb/scenes/bad_kb_scene_config_layout.c @@ -1,6 +1,5 @@ #include "../bad_kb_app_i.h" #include "furi_hal_power.h" -#include "furi_hal_usb.h" #include static bool bad_kb_layout_select(BadKbApp* bad_kb) { diff --git a/base_pack/bad_kb/scenes/bad_kb_scene_work.c b/base_pack/bad_kb/scenes/bad_kb_scene_work.c index bea285e5eea..f2e1fd2da0f 100644 --- a/base_pack/bad_kb/scenes/bad_kb_scene_work.c +++ b/base_pack/bad_kb/scenes/bad_kb_scene_work.c @@ -16,7 +16,7 @@ bool bad_kb_scene_work_on_event(void* context, SceneManagerEvent event) { if(event.type == SceneManagerEventTypeCustom) { if(event.event == InputKeyLeft) { - if(bad_kb_is_idle_state(app->bad_kb_view)) { + if(bad_kb_view_is_idle_state(app->bad_kb_view)) { scene_manager_next_scene(app->scene_manager, BadKbSceneConfig); } consumed = true; @@ -28,7 +28,7 @@ bool bad_kb_scene_work_on_event(void* context, SceneManagerEvent event) { consumed = true; } } else if(event.type == SceneManagerEventTypeTick) { - bad_kb_set_state(app->bad_kb_view, bad_kb_script_get_state(app->bad_kb_script)); + bad_kb_view_set_state(app->bad_kb_view, bad_kb_script_get_state(app->bad_kb_script)); } return consumed; } @@ -39,18 +39,18 @@ void bad_kb_scene_work_on_enter(void* context) { FuriString* file_name; file_name = furi_string_alloc(); path_extract_filename(app->file_path, file_name, true); - bad_kb_set_file_name(app->bad_kb_view, furi_string_get_cstr(file_name)); + bad_kb_view_set_file_name(app->bad_kb_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_kb_set_layout(app->bad_kb_view, furi_string_get_cstr(layout)); + bad_kb_view_set_layout(app->bad_kb_view, furi_string_get_cstr(layout)); furi_string_free(layout); - bad_kb_set_state(app->bad_kb_view, bad_kb_script_get_state(app->bad_kb_script)); + bad_kb_view_set_state(app->bad_kb_view, bad_kb_script_get_state(app->bad_kb_script)); - bad_kb_set_button_callback(app->bad_kb_view, bad_kb_scene_work_button_callback, app); + bad_kb_view_set_button_callback(app->bad_kb_view, bad_kb_scene_work_button_callback, app); view_dispatcher_switch_to_view(app->view_dispatcher, BadKbAppViewWork); } diff --git a/base_pack/bad_kb/views/bad_kb_view.c b/base_pack/bad_kb/views/bad_kb_view.c index a5dea1a19fe..72064190aa2 100644 --- a/base_pack/bad_kb/views/bad_kb_view.c +++ b/base_pack/bad_kb/views/bad_kb_view.c @@ -75,8 +75,8 @@ static void bad_kb_draw_callback(Canvas* canvas, void* _model) { if(state == BadKbStateNotConnected) { canvas_draw_icon(canvas, 4, 26, &I_Clock_18x18); canvas_set_font(canvas, FontPrimary); - canvas_draw_str_aligned(canvas, 127, 31, AlignRight, AlignBottom, "Connect to"); - canvas_draw_str_aligned(canvas, 127, 43, AlignRight, AlignBottom, "a device"); + canvas_draw_str_aligned(canvas, 127, 31, AlignRight, AlignBottom, "Connect"); + canvas_draw_str_aligned(canvas, 127, 43, AlignRight, AlignBottom, "to device"); } else if(state == BadKbStateWillRun) { canvas_draw_icon(canvas, 4, 26, &I_Clock_18x18); canvas_set_font(canvas, FontPrimary); @@ -211,7 +211,7 @@ static bool bad_kb_input_callback(InputEvent* event, void* context) { return consumed; } -BadKb* bad_kb_alloc() { +BadKb* bad_kb_view_alloc(void) { BadKb* bad_kb = malloc(sizeof(BadKb)); bad_kb->view = view_alloc(); @@ -223,18 +223,18 @@ BadKb* bad_kb_alloc() { return bad_kb; } -void bad_kb_free(BadKb* bad_kb) { +void bad_kb_view_free(BadKb* bad_kb) { furi_assert(bad_kb); view_free(bad_kb->view); free(bad_kb); } -View* bad_kb_get_view(BadKb* bad_kb) { +View* bad_kb_view_get_view(BadKb* bad_kb) { furi_assert(bad_kb); return bad_kb->view; } -void bad_kb_set_button_callback(BadKb* bad_kb, BadKbButtonCallback callback, void* context) { +void bad_kb_view_set_button_callback(BadKb* bad_kb, BadKbButtonCallback callback, void* context) { furi_assert(bad_kb); furi_assert(callback); with_view_model( @@ -248,19 +248,19 @@ void bad_kb_set_button_callback(BadKb* bad_kb, BadKbButtonCallback callback, voi true); } -void bad_kb_set_file_name(BadKb* bad_kb, const char* name) { +void bad_kb_view_set_file_name(BadKb* bad_kb, const char* name) { furi_assert(name); with_view_model( bad_kb->view, BadKbModel * model, { strlcpy(model->file_name, name, MAX_NAME_LEN); }, true); } -void bad_kb_set_layout(BadKb* bad_kb, const char* layout) { +void bad_kb_view_set_layout(BadKb* bad_kb, const char* layout) { furi_assert(layout); with_view_model( bad_kb->view, BadKbModel * model, { strlcpy(model->layout, layout, MAX_NAME_LEN); }, true); } -void bad_kb_set_state(BadKb* bad_kb, BadKbState* st) { +void bad_kb_view_set_state(BadKb* bad_kb, BadKbState* st) { furi_assert(st); uint32_t pin = 0; if(bad_kb->context != NULL) { @@ -283,7 +283,7 @@ void bad_kb_set_state(BadKb* bad_kb, BadKbState* st) { true); } -bool bad_kb_is_idle_state(BadKb* bad_kb) { +bool bad_kb_view_is_idle_state(BadKb* bad_kb) { bool is_idle = false; with_view_model( bad_kb->view, diff --git a/base_pack/bad_kb/views/bad_kb_view.h b/base_pack/bad_kb/views/bad_kb_view.h index d8f2559bb33..427862eb363 100644 --- a/base_pack/bad_kb/views/bad_kb_view.h +++ b/base_pack/bad_kb/views/bad_kb_view.h @@ -6,18 +6,18 @@ typedef struct BadKb BadKb; typedef void (*BadKbButtonCallback)(InputKey key, void* context); -BadKb* bad_kb_alloc(); +BadKb* bad_kb_view_alloc(void); -void bad_kb_free(BadKb* bad_kb); +void bad_kb_view_free(BadKb* bad_kb); -View* bad_kb_get_view(BadKb* bad_kb); +View* bad_kb_view_get_view(BadKb* bad_kb); -void bad_kb_set_button_callback(BadKb* bad_kb, BadKbButtonCallback callback, void* context); +void bad_kb_view_set_button_callback(BadKb* bad_kb, BadKbButtonCallback callback, void* context); -void bad_kb_set_file_name(BadKb* bad_kb, const char* name); +void bad_kb_view_set_file_name(BadKb* bad_kb, const char* name); -void bad_kb_set_layout(BadKb* bad_kb, const char* layout); +void bad_kb_view_set_layout(BadKb* bad_kb, const char* layout); -void bad_kb_set_state(BadKb* bad_kb, BadKbState* st); +void bad_kb_view_set_state(BadKb* bad_kb, BadKbState* st); -bool bad_kb_is_idle_state(BadKb* bad_kb); +bool bad_kb_view_is_idle_state(BadKb* bad_kb);