Skip to content

Commit

Permalink
upd badkb
Browse files Browse the repository at this point in the history
  • Loading branch information
xMasterX committed Apr 4, 2024
1 parent fb49d09 commit 2b9d505
Show file tree
Hide file tree
Showing 10 changed files with 150 additions and 32 deletions.
6 changes: 3 additions & 3 deletions base_pack/bad_kb/bad_kb_app.c
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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);
Expand Down
6 changes: 5 additions & 1 deletion base_pack/bad_kb/helpers/ducky_script.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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)) {
Expand Down
61 changes: 60 additions & 1 deletion base_pack/bad_kb/helpers/ducky_script_commands.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,26 @@ 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);
if(param == 1) {
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);
Expand Down Expand Up @@ -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);
Expand All @@ -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},
Expand All @@ -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"
Expand Down
3 changes: 3 additions & 0 deletions base_pack/bad_kb/helpers/ducky_script_i.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ struct BadKbScript {

uint32_t defdelay;
uint32_t stringdelay;
uint32_t defstringdelay;
uint16_t layout[128];

FuriString* line;
Expand All @@ -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);
Expand Down
56 changes: 55 additions & 1 deletion base_pack/bad_kb/helpers/ducky_script_keycodes.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include <furi_hal.h>
#include <furi_hal_usb_hid.h>
#include "ducky_script_i.h"

typedef struct {
Expand Down Expand Up @@ -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) {
Expand All @@ -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;
}
1 change: 0 additions & 1 deletion base_pack/bad_kb/scenes/bad_kb_scene_config.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include "../bad_kb_app_i.h"
#include "furi_hal_power.h"
#include "furi_hal_usb.h"

enum VarItemListIndex {
VarItemListIndexKeyboardLayout,
Expand Down
1 change: 0 additions & 1 deletion base_pack/bad_kb/scenes/bad_kb_scene_config_layout.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include "../bad_kb_app_i.h"
#include "furi_hal_power.h"
#include "furi_hal_usb.h"
#include <storage/storage.h>

static bool bad_kb_layout_select(BadKbApp* bad_kb) {
Expand Down
12 changes: 6 additions & 6 deletions base_pack/bad_kb/scenes/bad_kb_scene_work.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
Expand All @@ -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);
}

Expand Down
20 changes: 10 additions & 10 deletions base_pack/bad_kb/views/bad_kb_view.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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();
Expand All @@ -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(
Expand All @@ -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) {
Expand All @@ -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,
Expand Down
16 changes: 8 additions & 8 deletions base_pack/bad_kb/views/bad_kb_view.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

0 comments on commit 2b9d505

Please sign in to comment.