2022-11-28 19:27:16 +03:00
|
|
|
#include "hid_tiktok.h"
|
|
|
|
#include "../hid.h"
|
2022-10-12 18:48:13 +03:00
|
|
|
#include <gui/elements.h>
|
|
|
|
|
2022-11-28 19:27:16 +03:00
|
|
|
#include "hid_icons.h"
|
2022-10-12 18:48:13 +03:00
|
|
|
|
2022-11-28 19:27:16 +03:00
|
|
|
#define TAG "HidTikTok"
|
|
|
|
|
|
|
|
struct HidTikTok {
|
2022-10-12 18:48:13 +03:00
|
|
|
View* view;
|
2022-11-28 19:27:16 +03:00
|
|
|
Hid* hid;
|
2022-10-12 18:48:13 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
bool left_pressed;
|
|
|
|
bool up_pressed;
|
|
|
|
bool right_pressed;
|
|
|
|
bool down_pressed;
|
|
|
|
bool ok_pressed;
|
|
|
|
bool connected;
|
2022-10-26 01:57:06 +03:00
|
|
|
bool is_cursor_set;
|
2022-12-27 11:03:56 +03:00
|
|
|
HidTransport transport;
|
2022-11-28 19:27:16 +03:00
|
|
|
} HidTikTokModel;
|
2022-10-12 18:48:13 +03:00
|
|
|
|
2022-11-28 19:27:16 +03:00
|
|
|
static void hid_tiktok_draw_callback(Canvas* canvas, void* context) {
|
2022-10-12 18:48:13 +03:00
|
|
|
furi_assert(context);
|
2022-11-28 19:27:16 +03:00
|
|
|
HidTikTokModel* model = context;
|
2022-10-12 18:48:13 +03:00
|
|
|
|
|
|
|
// Header
|
2022-12-27 11:03:56 +03:00
|
|
|
if(model->transport == HidTransportBle) {
|
|
|
|
if(model->connected) {
|
|
|
|
canvas_draw_icon(canvas, 0, 0, &I_Ble_connected_15x15);
|
|
|
|
} else {
|
|
|
|
canvas_draw_icon(canvas, 0, 0, &I_Ble_disconnected_15x15);
|
|
|
|
}
|
2022-10-12 18:48:13 +03:00
|
|
|
}
|
2022-12-27 11:03:56 +03:00
|
|
|
|
2022-10-12 18:48:13 +03:00
|
|
|
canvas_set_font(canvas, FontPrimary);
|
|
|
|
elements_multiline_text_aligned(canvas, 17, 3, AlignLeft, AlignTop, "TikTok");
|
|
|
|
canvas_set_font(canvas, FontSecondary);
|
|
|
|
|
|
|
|
// Keypad circles
|
|
|
|
canvas_draw_icon(canvas, 76, 8, &I_Circles_47x47);
|
|
|
|
|
|
|
|
// Up
|
|
|
|
if(model->up_pressed) {
|
|
|
|
canvas_set_bitmap_mode(canvas, 1);
|
|
|
|
canvas_draw_icon(canvas, 93, 9, &I_Pressed_Button_13x13);
|
|
|
|
canvas_set_bitmap_mode(canvas, 0);
|
|
|
|
canvas_set_color(canvas, ColorWhite);
|
|
|
|
}
|
|
|
|
canvas_draw_icon(canvas, 96, 11, &I_Arr_up_7x9);
|
|
|
|
canvas_set_color(canvas, ColorBlack);
|
|
|
|
|
|
|
|
// Down
|
|
|
|
if(model->down_pressed) {
|
|
|
|
canvas_set_bitmap_mode(canvas, 1);
|
|
|
|
canvas_draw_icon(canvas, 93, 41, &I_Pressed_Button_13x13);
|
|
|
|
canvas_set_bitmap_mode(canvas, 0);
|
|
|
|
canvas_set_color(canvas, ColorWhite);
|
|
|
|
}
|
|
|
|
canvas_draw_icon(canvas, 96, 44, &I_Arr_dwn_7x9);
|
|
|
|
canvas_set_color(canvas, ColorBlack);
|
|
|
|
|
|
|
|
// Left
|
|
|
|
if(model->left_pressed) {
|
|
|
|
canvas_set_bitmap_mode(canvas, 1);
|
|
|
|
canvas_draw_icon(canvas, 77, 25, &I_Pressed_Button_13x13);
|
|
|
|
canvas_set_bitmap_mode(canvas, 0);
|
|
|
|
canvas_set_color(canvas, ColorWhite);
|
|
|
|
}
|
|
|
|
canvas_draw_icon(canvas, 81, 29, &I_Voldwn_6x6);
|
|
|
|
canvas_set_color(canvas, ColorBlack);
|
|
|
|
|
|
|
|
// Right
|
|
|
|
if(model->right_pressed) {
|
|
|
|
canvas_set_bitmap_mode(canvas, 1);
|
|
|
|
canvas_draw_icon(canvas, 109, 25, &I_Pressed_Button_13x13);
|
|
|
|
canvas_set_bitmap_mode(canvas, 0);
|
|
|
|
canvas_set_color(canvas, ColorWhite);
|
|
|
|
}
|
|
|
|
canvas_draw_icon(canvas, 111, 29, &I_Volup_8x6);
|
|
|
|
canvas_set_color(canvas, ColorBlack);
|
|
|
|
|
|
|
|
// Ok
|
|
|
|
if(model->ok_pressed) {
|
|
|
|
canvas_draw_icon(canvas, 91, 23, &I_Like_pressed_17x17);
|
|
|
|
} else {
|
|
|
|
canvas_draw_icon(canvas, 94, 27, &I_Like_def_11x9);
|
|
|
|
}
|
|
|
|
// Exit
|
|
|
|
canvas_draw_icon(canvas, 0, 54, &I_Pin_back_arrow_10x8);
|
|
|
|
canvas_set_font(canvas, FontSecondary);
|
|
|
|
elements_multiline_text_aligned(canvas, 13, 62, AlignLeft, AlignBottom, "Hold to exit");
|
|
|
|
}
|
|
|
|
|
2022-11-28 19:27:16 +03:00
|
|
|
static void hid_tiktok_reset_cursor(HidTikTok* hid_tiktok) {
|
2022-10-26 01:57:06 +03:00
|
|
|
// Set cursor to the phone's left up corner
|
|
|
|
// Delays to guarantee one packet per connection interval
|
|
|
|
for(size_t i = 0; i < 8; i++) {
|
2022-11-28 19:27:16 +03:00
|
|
|
hid_hal_mouse_move(hid_tiktok->hid, -127, -127);
|
2022-10-26 01:57:06 +03:00
|
|
|
furi_delay_ms(50);
|
|
|
|
}
|
|
|
|
// Move cursor from the corner
|
2022-11-28 19:27:16 +03:00
|
|
|
hid_hal_mouse_move(hid_tiktok->hid, 20, 120);
|
2022-10-26 01:57:06 +03:00
|
|
|
furi_delay_ms(50);
|
2022-10-12 18:48:13 +03:00
|
|
|
}
|
|
|
|
|
2022-11-28 19:27:16 +03:00
|
|
|
static void
|
|
|
|
hid_tiktok_process_press(HidTikTok* hid_tiktok, HidTikTokModel* model, InputEvent* event) {
|
2022-10-26 01:57:06 +03:00
|
|
|
if(event->key == InputKeyUp) {
|
|
|
|
model->up_pressed = true;
|
|
|
|
} else if(event->key == InputKeyDown) {
|
|
|
|
model->down_pressed = true;
|
|
|
|
} else if(event->key == InputKeyLeft) {
|
|
|
|
model->left_pressed = true;
|
2022-11-28 19:27:16 +03:00
|
|
|
hid_hal_consumer_key_press(hid_tiktok->hid, HID_CONSUMER_VOLUME_DECREMENT);
|
2022-10-26 01:57:06 +03:00
|
|
|
} else if(event->key == InputKeyRight) {
|
|
|
|
model->right_pressed = true;
|
2022-11-28 19:27:16 +03:00
|
|
|
hid_hal_consumer_key_press(hid_tiktok->hid, HID_CONSUMER_VOLUME_INCREMENT);
|
2022-10-26 01:57:06 +03:00
|
|
|
} else if(event->key == InputKeyOk) {
|
|
|
|
model->ok_pressed = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-28 19:27:16 +03:00
|
|
|
static void
|
|
|
|
hid_tiktok_process_release(HidTikTok* hid_tiktok, HidTikTokModel* model, InputEvent* event) {
|
2022-10-26 01:57:06 +03:00
|
|
|
if(event->key == InputKeyUp) {
|
|
|
|
model->up_pressed = false;
|
|
|
|
} else if(event->key == InputKeyDown) {
|
|
|
|
model->down_pressed = false;
|
|
|
|
} else if(event->key == InputKeyLeft) {
|
|
|
|
model->left_pressed = false;
|
2022-11-28 19:27:16 +03:00
|
|
|
hid_hal_consumer_key_release(hid_tiktok->hid, HID_CONSUMER_VOLUME_DECREMENT);
|
2022-10-26 01:57:06 +03:00
|
|
|
} else if(event->key == InputKeyRight) {
|
|
|
|
model->right_pressed = false;
|
2022-11-28 19:27:16 +03:00
|
|
|
hid_hal_consumer_key_release(hid_tiktok->hid, HID_CONSUMER_VOLUME_INCREMENT);
|
2022-10-26 01:57:06 +03:00
|
|
|
} else if(event->key == InputKeyOk) {
|
|
|
|
model->ok_pressed = false;
|
|
|
|
}
|
2022-10-12 18:48:13 +03:00
|
|
|
}
|
|
|
|
|
2022-11-28 19:27:16 +03:00
|
|
|
static bool hid_tiktok_input_callback(InputEvent* event, void* context) {
|
2022-10-12 18:48:13 +03:00
|
|
|
furi_assert(context);
|
2022-11-28 19:27:16 +03:00
|
|
|
HidTikTok* hid_tiktok = context;
|
2022-10-12 18:48:13 +03:00
|
|
|
bool consumed = false;
|
|
|
|
|
2022-10-26 01:57:06 +03:00
|
|
|
with_view_model(
|
2022-11-28 19:27:16 +03:00
|
|
|
hid_tiktok->view,
|
|
|
|
HidTikTokModel * model,
|
2022-10-26 01:57:06 +03:00
|
|
|
{
|
|
|
|
if(event->type == InputTypePress) {
|
2022-11-28 19:27:16 +03:00
|
|
|
hid_tiktok_process_press(hid_tiktok, model, event);
|
2022-10-26 01:57:06 +03:00
|
|
|
if(model->connected && !model->is_cursor_set) {
|
2022-11-28 19:27:16 +03:00
|
|
|
hid_tiktok_reset_cursor(hid_tiktok);
|
2022-10-26 01:57:06 +03:00
|
|
|
model->is_cursor_set = true;
|
|
|
|
}
|
|
|
|
consumed = true;
|
|
|
|
} else if(event->type == InputTypeRelease) {
|
2022-11-28 19:27:16 +03:00
|
|
|
hid_tiktok_process_release(hid_tiktok, model, event);
|
2022-10-26 01:57:06 +03:00
|
|
|
consumed = true;
|
|
|
|
} else if(event->type == InputTypeShort) {
|
|
|
|
if(event->key == InputKeyOk) {
|
2022-11-28 19:27:16 +03:00
|
|
|
hid_hal_mouse_press(hid_tiktok->hid, HID_MOUSE_BTN_LEFT);
|
2022-10-26 01:57:06 +03:00
|
|
|
furi_delay_ms(50);
|
2022-11-28 19:27:16 +03:00
|
|
|
hid_hal_mouse_release(hid_tiktok->hid, HID_MOUSE_BTN_LEFT);
|
2022-10-26 01:57:06 +03:00
|
|
|
furi_delay_ms(50);
|
2022-11-28 19:27:16 +03:00
|
|
|
hid_hal_mouse_press(hid_tiktok->hid, HID_MOUSE_BTN_LEFT);
|
2022-10-26 01:57:06 +03:00
|
|
|
furi_delay_ms(50);
|
2022-11-28 19:27:16 +03:00
|
|
|
hid_hal_mouse_release(hid_tiktok->hid, HID_MOUSE_BTN_LEFT);
|
2022-10-26 01:57:06 +03:00
|
|
|
consumed = true;
|
|
|
|
} else if(event->key == InputKeyUp) {
|
|
|
|
// Emulate up swipe
|
2022-11-28 19:27:16 +03:00
|
|
|
hid_hal_mouse_scroll(hid_tiktok->hid, -6);
|
|
|
|
hid_hal_mouse_scroll(hid_tiktok->hid, -12);
|
|
|
|
hid_hal_mouse_scroll(hid_tiktok->hid, -19);
|
|
|
|
hid_hal_mouse_scroll(hid_tiktok->hid, -12);
|
|
|
|
hid_hal_mouse_scroll(hid_tiktok->hid, -6);
|
2022-10-26 01:57:06 +03:00
|
|
|
consumed = true;
|
|
|
|
} else if(event->key == InputKeyDown) {
|
|
|
|
// Emulate down swipe
|
2022-11-28 19:27:16 +03:00
|
|
|
hid_hal_mouse_scroll(hid_tiktok->hid, 6);
|
|
|
|
hid_hal_mouse_scroll(hid_tiktok->hid, 12);
|
|
|
|
hid_hal_mouse_scroll(hid_tiktok->hid, 19);
|
|
|
|
hid_hal_mouse_scroll(hid_tiktok->hid, 12);
|
|
|
|
hid_hal_mouse_scroll(hid_tiktok->hid, 6);
|
2022-10-26 01:57:06 +03:00
|
|
|
consumed = true;
|
|
|
|
} else if(event->key == InputKeyBack) {
|
2022-11-28 19:27:16 +03:00
|
|
|
hid_hal_consumer_key_release_all(hid_tiktok->hid);
|
2022-10-26 01:57:06 +03:00
|
|
|
consumed = true;
|
|
|
|
}
|
|
|
|
} else if(event->type == InputTypeLong) {
|
|
|
|
if(event->key == InputKeyBack) {
|
2022-11-28 19:27:16 +03:00
|
|
|
hid_hal_consumer_key_release_all(hid_tiktok->hid);
|
2022-10-26 01:57:06 +03:00
|
|
|
model->is_cursor_set = false;
|
|
|
|
consumed = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
true);
|
2022-10-12 18:48:13 +03:00
|
|
|
|
|
|
|
return consumed;
|
|
|
|
}
|
|
|
|
|
2022-11-28 19:27:16 +03:00
|
|
|
HidTikTok* hid_tiktok_alloc(Hid* bt_hid) {
|
|
|
|
HidTikTok* hid_tiktok = malloc(sizeof(HidTikTok));
|
|
|
|
hid_tiktok->hid = bt_hid;
|
|
|
|
hid_tiktok->view = view_alloc();
|
|
|
|
view_set_context(hid_tiktok->view, hid_tiktok);
|
|
|
|
view_allocate_model(hid_tiktok->view, ViewModelTypeLocking, sizeof(HidTikTokModel));
|
|
|
|
view_set_draw_callback(hid_tiktok->view, hid_tiktok_draw_callback);
|
|
|
|
view_set_input_callback(hid_tiktok->view, hid_tiktok_input_callback);
|
2022-10-12 18:48:13 +03:00
|
|
|
|
2022-12-27 11:03:56 +03:00
|
|
|
with_view_model(
|
|
|
|
hid_tiktok->view, HidTikTokModel * model, { model->transport = bt_hid->transport; }, true);
|
|
|
|
|
2022-11-28 19:27:16 +03:00
|
|
|
return hid_tiktok;
|
2022-10-12 18:48:13 +03:00
|
|
|
}
|
|
|
|
|
2022-11-28 19:27:16 +03:00
|
|
|
void hid_tiktok_free(HidTikTok* hid_tiktok) {
|
|
|
|
furi_assert(hid_tiktok);
|
|
|
|
view_free(hid_tiktok->view);
|
|
|
|
free(hid_tiktok);
|
2022-10-12 18:48:13 +03:00
|
|
|
}
|
|
|
|
|
2022-11-28 19:27:16 +03:00
|
|
|
View* hid_tiktok_get_view(HidTikTok* hid_tiktok) {
|
|
|
|
furi_assert(hid_tiktok);
|
|
|
|
return hid_tiktok->view;
|
2022-10-12 18:48:13 +03:00
|
|
|
}
|
|
|
|
|
2022-11-28 19:27:16 +03:00
|
|
|
void hid_tiktok_set_connected_status(HidTikTok* hid_tiktok, bool connected) {
|
|
|
|
furi_assert(hid_tiktok);
|
2022-10-12 18:48:13 +03:00
|
|
|
with_view_model(
|
2022-11-28 19:27:16 +03:00
|
|
|
hid_tiktok->view,
|
|
|
|
HidTikTokModel * model,
|
2022-10-26 01:57:06 +03:00
|
|
|
{
|
|
|
|
model->connected = connected;
|
|
|
|
model->is_cursor_set = false;
|
|
|
|
},
|
|
|
|
true);
|
2022-10-12 18:48:13 +03:00
|
|
|
}
|