Skip to content

Commit ca26d68

Browse files
committed
sync badkb
1 parent f37d0d5 commit ca26d68

File tree

8 files changed

+115
-60
lines changed

8 files changed

+115
-60
lines changed

base_pack/bad_kb/bad_kb_app.c

Lines changed: 34 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -28,49 +28,67 @@ static void bad_kb_app_tick_event_callback(void* context) {
2828
scene_manager_handle_tick_event(app->scene_manager);
2929
}
3030

31-
static void bad_kb_load_settings(BadKbApp* app) {
31+
void bad_kb_load_settings(BadKbApp* app) {
3232
furi_string_reset(app->keyboard_layout);
3333
BadKbConfig* cfg = &app->config;
3434

3535
Storage* storage = furi_record_open(RECORD_STORAGE);
3636
FlipperFormat* file = flipper_format_file_alloc(storage);
3737
if(flipper_format_file_open_existing(file, BAD_KB_SETTINGS_PATH)) {
3838
FuriString* tmp_str = furi_string_alloc();
39+
3940
if(!flipper_format_read_string(file, "Keyboard_Layout", app->keyboard_layout)) {
4041
furi_string_reset(app->keyboard_layout);
42+
flipper_format_rewind(file);
43+
}
44+
45+
if(!flipper_format_read_bool(file, "Is_Bt", &app->is_bt, 1)) {
46+
app->is_bt = false;
47+
flipper_format_rewind(file);
48+
}
49+
50+
if(!flipper_format_read_bool(file, "Bt_Remember", &app->bt_remember, 1)) {
51+
app->bt_remember = false;
52+
flipper_format_rewind(file);
4153
}
42-
if(flipper_format_read_string(file, "Bt_Name", tmp_str) && !furi_string_empty(tmp_str)) {
54+
55+
if(flipper_format_read_string(file, "Bt_Name", tmp_str)) {
4356
strlcpy(cfg->ble.name, furi_string_get_cstr(tmp_str), sizeof(cfg->ble.name));
4457
} else {
45-
strcpy(cfg->ble.name, "");
58+
cfg->ble.name[0] = '\0';
59+
flipper_format_rewind(file);
4660
}
61+
4762
if(!flipper_format_read_hex(
4863
file, "Bt_Mac", (uint8_t*)&cfg->ble.mac, sizeof(cfg->ble.mac))) {
4964
memset(cfg->ble.mac, 0, sizeof(cfg->ble.mac));
65+
flipper_format_rewind(file);
5066
}
51-
if(flipper_format_read_string(file, "Usb_Manuf", tmp_str) && !furi_string_empty(tmp_str)) {
67+
68+
if(flipper_format_read_string(file, "Usb_Manuf", tmp_str)) {
5269
strlcpy(cfg->usb.manuf, furi_string_get_cstr(tmp_str), sizeof(cfg->usb.manuf));
5370
} else {
54-
strcpy(cfg->usb.manuf, "");
71+
cfg->usb.manuf[0] = '\0';
72+
flipper_format_rewind(file);
5573
}
56-
if(flipper_format_read_string(file, "Usb_Product", tmp_str) &&
57-
!furi_string_empty(tmp_str)) {
74+
75+
if(flipper_format_read_string(file, "Usb_Product", tmp_str)) {
5876
strlcpy(cfg->usb.product, furi_string_get_cstr(tmp_str), sizeof(cfg->usb.product));
5977
} else {
60-
strcpy(cfg->usb.product, "");
78+
cfg->usb.product[0] = '\0';
79+
flipper_format_rewind(file);
6180
}
81+
6282
if(!flipper_format_read_uint32(file, "Usb_Vid", &cfg->usb.vid, 1)) {
6383
cfg->usb.vid = 0;
84+
flipper_format_rewind(file);
6485
}
86+
6587
if(!flipper_format_read_uint32(file, "Usb_Pid", &cfg->usb.pid, 1)) {
6688
cfg->usb.pid = 0;
89+
flipper_format_rewind(file);
6790
}
68-
if(!flipper_format_read_bool(file, "Bt_Remember", &cfg->bt_remember, 1)) {
69-
cfg->bt_remember = false;
70-
}
71-
if(!flipper_format_read_bool(file, "Is_Ble", &cfg->is_ble, 1)) {
72-
cfg->is_ble = false;
73-
}
91+
7492
furi_string_free(tmp_str);
7593
flipper_format_file_close(file);
7694
}
@@ -98,14 +116,14 @@ static void bad_kb_save_settings(BadKbApp* app) {
98116
FlipperFormat* file = flipper_format_file_alloc(storage);
99117
if(flipper_format_file_open_always(file, BAD_KB_SETTINGS_PATH)) {
100118
flipper_format_write_string(file, "Keyboard_Layout", app->keyboard_layout);
119+
flipper_format_write_bool(file, "Is_Bt", &app->is_bt, 1);
120+
flipper_format_write_bool(file, "Bt_Remember", &app->bt_remember, 1);
101121
flipper_format_write_string_cstr(file, "Bt_Name", cfg->ble.name);
102122
flipper_format_write_hex(file, "Bt_Mac", (uint8_t*)&cfg->ble.mac, sizeof(cfg->ble.mac));
103123
flipper_format_write_string_cstr(file, "Usb_Manuf", cfg->usb.manuf);
104124
flipper_format_write_string_cstr(file, "Usb_Product", cfg->usb.product);
105125
flipper_format_write_uint32(file, "Usb_Vid", &cfg->usb.vid, 1);
106126
flipper_format_write_uint32(file, "Usb_Pid", &cfg->usb.pid, 1);
107-
flipper_format_write_bool(file, "Bt_Remember", &cfg->bt_remember, 1);
108-
flipper_format_write_bool(file, "Is_Ble", &cfg->is_ble, 1);
109127
flipper_format_file_close(file);
110128
}
111129
flipper_format_free(file);
@@ -272,13 +290,6 @@ void bad_kb_config_refresh(BadKbApp* app) {
272290
// Reload config page
273291
scene_manager_next_scene(app->scene_manager, BadKbSceneConfig);
274292
scene_manager_previous_scene(app->scene_manager);
275-
276-
// Update settings
277-
if((app->config.bt_remember != app->bt_remember) || (app->config.is_ble != app->is_bt)) {
278-
app->config.bt_remember = app->bt_remember;
279-
app->config.is_ble = app->is_bt;
280-
bad_kb_save_settings(app);
281-
}
282293
}
283294

284295
BadKbApp* bad_kb_app_alloc(char* arg) {
@@ -314,8 +325,6 @@ BadKbApp* bad_kb_app_alloc(char* arg) {
314325
Bt* bt = furi_record_open(RECORD_BT);
315326
app->bt = bt;
316327
app->bt->suppress_pin_screen = true;
317-
app->is_bt = app->config.is_ble;
318-
app->bt_remember = app->config.bt_remember;
319328
bad_kb_config_adjust(&app->config);
320329

321330
// Save prev config

base_pack/bad_kb/bad_kb_app_i.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,6 @@ typedef enum {
3939
typedef struct {
4040
BleProfileHidParams ble;
4141
FuriHalUsbHidConfig usb;
42-
bool bt_remember;
43-
bool is_ble;
4442
} BadKbConfig;
4543

4644
typedef enum {
@@ -104,6 +102,8 @@ typedef enum {
104102

105103
void bad_kb_app_show_loading_popup(BadKbApp* app, bool show);
106104

105+
void bad_kb_load_settings(BadKbApp* app);
106+
107107
int32_t bad_kb_conn_apply(BadKbApp* app);
108108

109109
void bad_kb_conn_reset(BadKbApp* app);

base_pack/bad_kb/helpers/ducky_script.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -382,11 +382,12 @@ static void ducky_script_preload(BadKbScript* bad_kb, File* script_file) {
382382
app->has_usb_id = strncmp(line_tmp, ducky_cmd_id, strlen(ducky_cmd_id)) == 0;
383383
app->has_bt_id = strncmp(line_tmp, ducky_cmd_bt_id, strlen(ducky_cmd_bt_id)) == 0;
384384

385+
// Auto-switch to mode chosen with ID/BT_ID, can override manually in config screen
385386
if(app->has_usb_id) {
386-
//app->is_bt = false;
387+
app->is_bt = false;
387388
app->set_usb_id = ducky_set_usb_id(bad_kb, &line_tmp[strlen(ducky_cmd_id) + 1]);
388389
} else if(app->has_bt_id) {
389-
//app->is_bt = true;
390+
app->is_bt = true;
390391
app->set_bt_id = ducky_set_bt_id(bad_kb, &line_tmp[strlen(ducky_cmd_bt_id) + 1]);
391392
}
392393

base_pack/bad_kb/scenes/bad_kb_scene_config.c

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,6 @@ void bad_kb_scene_config_on_enter(void* context) {
5151
var_item_list, "Connection", 2, bad_kb_scene_config_connection_callback, bad_kb);
5252
variable_item_set_current_value_index(item, bad_kb->is_bt);
5353
variable_item_set_current_value_text(item, bad_kb->is_bt ? "BT" : "USB");
54-
/*if(bad_kb->has_usb_id) {
55-
variable_item_set_locked(item, true, "Script has\nID cmd!\nLocked to\nUSB Mode!");
56-
} else if(bad_kb->has_bt_id) {
57-
variable_item_set_locked(item, true, "Script has\nBT_ID cmd!\nLocked to\nBT Mode!");
58-
}*/
5954

6055
if(bad_kb->is_bt) {
6156
item = variable_item_list_add(
@@ -64,43 +59,24 @@ void bad_kb_scene_config_on_enter(void* context) {
6459
variable_item_set_current_value_text(item, bad_kb->bt_remember ? "ON" : "OFF");
6560

6661
item = variable_item_list_add(var_item_list, "BT Device Name", 0, NULL, bad_kb);
67-
if(bad_kb->set_bt_id) {
68-
variable_item_set_locked(item, true, "Script has\nBT_ID cmd!\nLocked to\nset Name!");
69-
}
7062

7163
item = variable_item_list_add(var_item_list, "BT MAC Address", 0, NULL, bad_kb);
7264
if(bad_kb->bt_remember) {
7365
variable_item_set_locked(item, true, "Remember\nmust be Off!");
74-
} else if(bad_kb->set_bt_id) {
75-
variable_item_set_locked(item, true, "Script has\nBT_ID cmd!\nLocked to\nset MAC!");
7666
}
7767

7868
item = variable_item_list_add(var_item_list, "Randomize BT MAC", 0, NULL, bad_kb);
7969
if(bad_kb->bt_remember) {
8070
variable_item_set_locked(item, true, "Remember\nmust be Off!");
81-
} else if(bad_kb->set_bt_id) {
82-
variable_item_set_locked(item, true, "Script has\nBT_ID cmd!\nLocked to\nset MAC!");
8371
}
8472
} else {
8573
item = variable_item_list_add(var_item_list, "USB Manufacturer", 0, NULL, bad_kb);
86-
if(bad_kb->set_usb_id) {
87-
variable_item_set_locked(item, true, "Script has\nID cmd!\nLocked to\nset Mname!");
88-
}
8974

9075
item = variable_item_list_add(var_item_list, "USB Product Name", 0, NULL, bad_kb);
91-
if(bad_kb->set_usb_id) {
92-
variable_item_set_locked(item, true, "Script has\nID cmd!\nLocked to\nset Pname!");
93-
}
9476

9577
item = variable_item_list_add(var_item_list, "USB VID and PID", 0, NULL, bad_kb);
96-
if(bad_kb->set_usb_id) {
97-
variable_item_set_locked(item, true, "Script has\nID cmd!\nLocked to\nset IDs!");
98-
}
9978

10079
item = variable_item_list_add(var_item_list, "Randomize USB VID:PID", 0, NULL, bad_kb);
101-
if(bad_kb->set_usb_id) {
102-
variable_item_set_locked(item, true, "Script has\nID cmd!\nLocked to\nset IDs!");
103-
}
10480
}
10581

10682
variable_item_list_set_enter_callback(
@@ -141,7 +117,15 @@ bool bad_kb_scene_config_on_event(void* context, SceneManagerEvent event) {
141117
scene_manager_next_scene(bad_kb->scene_manager, BadKbSceneConfigBtMac);
142118
break;
143119
case VarItemListIndexBtRandomizeMac:
120+
// Set user config and remember
144121
furi_hal_random_fill_buf(bad_kb->config.ble.mac, sizeof(bad_kb->config.ble.mac));
122+
// Apply to ID config so its temporarily overridden
123+
if(bad_kb->set_bt_id) {
124+
memcpy(
125+
bad_kb->id_config.ble.mac,
126+
bad_kb->config.ble.mac,
127+
sizeof(bad_kb->id_config.ble.mac));
128+
}
145129
bad_kb_config_refresh(bad_kb);
146130
break;
147131
default:
@@ -165,8 +149,14 @@ bool bad_kb_scene_config_on_event(void* context, SceneManagerEvent event) {
165149
case VarItemListIndexUsbRandomizeVidPid:
166150
furi_hal_random_fill_buf(
167151
(void*)bad_kb->usb_vidpid_buf, sizeof(bad_kb->usb_vidpid_buf));
152+
// Set user config and remember
168153
bad_kb->config.usb.vid = bad_kb->usb_vidpid_buf[0];
169154
bad_kb->config.usb.pid = bad_kb->usb_vidpid_buf[1];
155+
// Apply to ID config so its temporarily overridden
156+
if(bad_kb->set_usb_id) {
157+
bad_kb->id_config.usb.vid = bad_kb->config.usb.vid;
158+
bad_kb->id_config.usb.pid = bad_kb->config.usb.pid;
159+
}
170160
bad_kb_config_refresh(bad_kb);
171161
break;
172162
default:

base_pack/bad_kb/scenes/bad_kb_scene_config_bt_mac.c

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ void bad_kb_scene_config_bt_mac_on_enter(void* context) {
1010
BadKbApp* bad_kb = context;
1111
ByteInput* byte_input = bad_kb->byte_input;
1212

13-
memcpy(bad_kb->bt_mac_buf, bad_kb->config.ble.mac, sizeof(bad_kb->bt_mac_buf));
13+
memcpy(
14+
bad_kb->bt_mac_buf,
15+
bad_kb->set_bt_id ? bad_kb->id_config.ble.mac : bad_kb->config.ble.mac,
16+
sizeof(bad_kb->bt_mac_buf));
1417
furi_hal_bt_reverse_mac_addr(bad_kb->bt_mac_buf);
1518
byte_input_set_header_text(byte_input, "Set BT MAC address");
1619

@@ -33,7 +36,15 @@ bool bad_kb_scene_config_bt_mac_on_event(void* context, SceneManagerEvent event)
3336
consumed = true;
3437
if(event.event == BadKbAppCustomEventByteInputDone) {
3538
furi_hal_bt_reverse_mac_addr(bad_kb->bt_mac_buf);
39+
// Set user config and remember
3640
memcpy(bad_kb->config.ble.mac, bad_kb->bt_mac_buf, sizeof(bad_kb->config.ble.mac));
41+
// Apply to ID config so its temporarily overridden
42+
if(bad_kb->set_bt_id) {
43+
memcpy(
44+
bad_kb->id_config.ble.mac,
45+
bad_kb->bt_mac_buf,
46+
sizeof(bad_kb->id_config.ble.mac));
47+
}
3748
bad_kb_config_refresh(bad_kb);
3849
}
3950
scene_manager_previous_scene(bad_kb->scene_manager);

base_pack/bad_kb/scenes/bad_kb_scene_config_bt_name.c

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ void bad_kb_scene_config_bt_name_on_enter(void* context) {
1010
BadKbApp* bad_kb = context;
1111
TextInput* text_input = bad_kb->text_input;
1212

13-
strlcpy(bad_kb->bt_name_buf, bad_kb->config.ble.name, sizeof(bad_kb->bt_name_buf));
13+
strlcpy(
14+
bad_kb->bt_name_buf,
15+
bad_kb->set_bt_id ? bad_kb->id_config.ble.name : bad_kb->config.ble.name,
16+
sizeof(bad_kb->bt_name_buf));
1417
text_input_set_header_text(text_input, "Set BT device name");
1518

1619
text_input_set_result_callback(
@@ -31,7 +34,15 @@ bool bad_kb_scene_config_bt_name_on_event(void* context, SceneManagerEvent event
3134
if(event.type == SceneManagerEventTypeCustom) {
3235
consumed = true;
3336
if(event.event == BadKbAppCustomEventTextInputDone) {
37+
// Set user config and remember
3438
strlcpy(bad_kb->config.ble.name, bad_kb->bt_name_buf, sizeof(bad_kb->config.ble.name));
39+
// Apply to ID config so its temporarily overridden
40+
if(bad_kb->set_bt_id) {
41+
strlcpy(
42+
bad_kb->id_config.ble.name,
43+
bad_kb->bt_name_buf,
44+
sizeof(bad_kb->id_config.ble.name));
45+
}
3546
bad_kb_config_refresh(bad_kb);
3647
}
3748
scene_manager_previous_scene(bad_kb->scene_manager);

base_pack/bad_kb/scenes/bad_kb_scene_config_usb_name.c

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,16 @@ void bad_kb_scene_config_usb_name_on_enter(void* context) {
1111
TextInput* text_input = bad_kb->text_input;
1212

1313
if(scene_manager_get_scene_state(bad_kb->scene_manager, BadKbSceneConfigUsbName)) {
14-
strlcpy(bad_kb->usb_name_buf, bad_kb->config.usb.manuf, sizeof(bad_kb->usb_name_buf));
14+
strlcpy(
15+
bad_kb->usb_name_buf,
16+
bad_kb->set_usb_id ? bad_kb->id_config.usb.manuf : bad_kb->config.usb.manuf,
17+
sizeof(bad_kb->usb_name_buf));
1518
text_input_set_header_text(text_input, "Set USB manufacturer name");
1619
} else {
17-
strlcpy(bad_kb->usb_name_buf, bad_kb->config.usb.product, sizeof(bad_kb->usb_name_buf));
20+
strlcpy(
21+
bad_kb->usb_name_buf,
22+
bad_kb->set_usb_id ? bad_kb->id_config.usb.product : bad_kb->config.usb.product,
23+
sizeof(bad_kb->usb_name_buf));
1824
text_input_set_header_text(text_input, "Set USB product name");
1925
}
2026

@@ -37,15 +43,31 @@ bool bad_kb_scene_config_usb_name_on_event(void* context, SceneManagerEvent even
3743
consumed = true;
3844
if(event.event == BadKbAppCustomEventTextInputDone) {
3945
if(scene_manager_get_scene_state(bad_kb->scene_manager, BadKbSceneConfigUsbName)) {
46+
// Set user config and remember
4047
strlcpy(
4148
bad_kb->config.usb.manuf,
4249
bad_kb->usb_name_buf,
43-
sizeof(bad_kb->config.usb.product));
50+
sizeof(bad_kb->config.usb.manuf));
51+
// Apply to ID config so its temporarily overridden
52+
if(bad_kb->set_usb_id) {
53+
strlcpy(
54+
bad_kb->id_config.usb.manuf,
55+
bad_kb->usb_name_buf,
56+
sizeof(bad_kb->id_config.usb.manuf));
57+
}
4458
} else {
59+
// Set user config and remember
4560
strlcpy(
4661
bad_kb->config.usb.product,
4762
bad_kb->usb_name_buf,
4863
sizeof(bad_kb->config.usb.product));
64+
// Apply to ID config so its temporarily overridden
65+
if(bad_kb->set_usb_id) {
66+
strlcpy(
67+
bad_kb->id_config.usb.product,
68+
bad_kb->usb_name_buf,
69+
sizeof(bad_kb->id_config.usb.product));
70+
}
4971
}
5072
bad_kb_config_refresh(bad_kb);
5173
}

base_pack/bad_kb/scenes/bad_kb_scene_config_usb_vidpid.c

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,13 @@ void bad_kb_scene_config_usb_vidpid_on_enter(void* context) {
1010
BadKbApp* bad_kb = context;
1111
ByteInput* byte_input = bad_kb->byte_input;
1212

13-
bad_kb->usb_vidpid_buf[0] = __REVSH(bad_kb->config.usb.vid);
14-
bad_kb->usb_vidpid_buf[1] = __REVSH(bad_kb->config.usb.pid);
13+
if(bad_kb->set_usb_id) {
14+
bad_kb->usb_vidpid_buf[0] = __REVSH(bad_kb->id_config.usb.vid);
15+
bad_kb->usb_vidpid_buf[1] = __REVSH(bad_kb->id_config.usb.pid);
16+
} else {
17+
bad_kb->usb_vidpid_buf[0] = __REVSH(bad_kb->config.usb.vid);
18+
bad_kb->usb_vidpid_buf[1] = __REVSH(bad_kb->config.usb.pid);
19+
}
1520
byte_input_set_header_text(byte_input, "Set USB VID:PID");
1621

1722
byte_input_set_result_callback(
@@ -32,8 +37,14 @@ bool bad_kb_scene_config_usb_vidpid_on_event(void* context, SceneManagerEvent ev
3237
if(event.type == SceneManagerEventTypeCustom) {
3338
consumed = true;
3439
if(event.event == BadKbAppCustomEventByteInputDone) {
40+
// Set user config and remember
3541
bad_kb->config.usb.vid = __REVSH(bad_kb->usb_vidpid_buf[0]);
3642
bad_kb->config.usb.pid = __REVSH(bad_kb->usb_vidpid_buf[1]);
43+
// Apply to ID config so its temporarily overridden
44+
if(bad_kb->set_usb_id) {
45+
bad_kb->id_config.usb.vid = bad_kb->config.usb.vid;
46+
bad_kb->id_config.usb.pid = bad_kb->config.usb.pid;
47+
}
3748
bad_kb_config_refresh(bad_kb);
3849
}
3950
scene_manager_previous_scene(bad_kb->scene_manager);

0 commit comments

Comments
 (0)