2022-11-28 19:27:16 +03:00
|
|
|
#include "hid_mouse.h"
|
2022-07-08 15:36:34 +03:00
|
|
|
#include <gui/elements.h>
|
2022-11-28 19:27:16 +03:00
|
|
|
#include "../hid.h"
|
2022-07-08 15:36:34 +03:00
|
|
|
|
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 "HidMouse"
|
|
|
|
|
|
|
|
struct HidMouse {
|
2022-07-08 15:36:34 +03:00
|
|
|
View* view;
|
2022-11-28 19:27:16 +03:00
|
|
|
Hid* hid;
|
2022-07-08 15:36:34 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
bool left_pressed;
|
|
|
|
bool up_pressed;
|
|
|
|
bool right_pressed;
|
|
|
|
bool down_pressed;
|
|
|
|
bool left_mouse_pressed;
|
|
|
|
bool left_mouse_held;
|
|
|
|
bool right_mouse_pressed;
|
|
|
|
bool connected;
|
2022-12-27 11:03:56 +03:00
|
|
|
HidTransport transport;
|
2022-11-28 19:27:16 +03:00
|
|
|
} HidMouseModel;
|
2022-07-08 15:36:34 +03:00
|
|
|
|
2022-11-28 19:27:16 +03:00
|
|
|
static void hid_mouse_draw_callback(Canvas* canvas, void* context) {
|
2022-07-08 15:36:34 +03:00
|
|
|
furi_assert(context);
|
2022-11-28 19:27:16 +03:00
|
|
|
HidMouseModel* model = context;
|
2022-07-08 15:36:34 +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-07-08 15:36:34 +03:00
|
|
|
}
|
2022-12-27 11:03:56 +03:00
|
|
|
|
2022-07-08 15:36:34 +03:00
|
|
|
canvas_set_font(canvas, FontPrimary);
|
|
|
|
elements_multiline_text_aligned(canvas, 17, 3, AlignLeft, AlignTop, "Mouse");
|
|
|
|
canvas_set_font(canvas, FontSecondary);
|
|
|
|
|
|
|
|
if(model->left_mouse_held == true) {
|
2022-08-22 19:41:41 +03:00
|
|
|
elements_multiline_text_aligned(canvas, 0, 62, AlignLeft, AlignBottom, "Selecting...");
|
|
|
|
} else {
|
|
|
|
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-07-08 15:36:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Keypad circles
|
2023-04-29 18:16:53 +03:00
|
|
|
canvas_draw_icon(canvas, 58, 3, &I_OutCircles);
|
2022-07-08 15:36:34 +03:00
|
|
|
|
|
|
|
// Up
|
|
|
|
if(model->up_pressed) {
|
2022-08-22 19:41:41 +03:00
|
|
|
canvas_set_bitmap_mode(canvas, 1);
|
2023-04-29 18:16:53 +03:00
|
|
|
canvas_draw_icon(canvas, 68, 6, &I_S_UP);
|
2022-08-22 19:41:41 +03:00
|
|
|
canvas_set_bitmap_mode(canvas, 0);
|
2022-07-08 15:36:34 +03:00
|
|
|
canvas_set_color(canvas, ColorWhite);
|
|
|
|
}
|
2023-04-29 18:16:53 +03:00
|
|
|
canvas_draw_icon(canvas, 80, 8, &I_Pin_arrow_up_7x9);
|
2022-07-08 15:36:34 +03:00
|
|
|
canvas_set_color(canvas, ColorBlack);
|
|
|
|
|
|
|
|
// Down
|
|
|
|
if(model->down_pressed) {
|
2022-08-22 19:41:41 +03:00
|
|
|
canvas_set_bitmap_mode(canvas, 1);
|
2023-04-29 18:16:53 +03:00
|
|
|
canvas_draw_icon(canvas, 68, 36, &I_S_DOWN);
|
2022-08-22 19:41:41 +03:00
|
|
|
canvas_set_bitmap_mode(canvas, 0);
|
2022-07-08 15:36:34 +03:00
|
|
|
canvas_set_color(canvas, ColorWhite);
|
|
|
|
}
|
2023-04-29 18:16:53 +03:00
|
|
|
canvas_draw_icon(canvas, 80, 40, &I_Pin_arrow_down_7x9);
|
2022-07-08 15:36:34 +03:00
|
|
|
canvas_set_color(canvas, ColorBlack);
|
|
|
|
|
|
|
|
// Left
|
|
|
|
if(model->left_pressed) {
|
2022-08-22 19:41:41 +03:00
|
|
|
canvas_set_bitmap_mode(canvas, 1);
|
2023-04-29 18:16:53 +03:00
|
|
|
canvas_draw_icon(canvas, 61, 13, &I_S_LEFT);
|
2022-08-22 19:41:41 +03:00
|
|
|
canvas_set_bitmap_mode(canvas, 0);
|
2022-07-08 15:36:34 +03:00
|
|
|
canvas_set_color(canvas, ColorWhite);
|
|
|
|
}
|
2023-04-29 18:16:53 +03:00
|
|
|
canvas_draw_icon(canvas, 63, 25, &I_Pin_arrow_left_9x7);
|
2022-07-08 15:36:34 +03:00
|
|
|
canvas_set_color(canvas, ColorBlack);
|
|
|
|
|
|
|
|
// Right
|
|
|
|
if(model->right_pressed) {
|
2022-08-22 19:41:41 +03:00
|
|
|
canvas_set_bitmap_mode(canvas, 1);
|
2023-04-29 18:16:53 +03:00
|
|
|
canvas_draw_icon(canvas, 91, 13, &I_S_RIGHT);
|
2022-08-22 19:41:41 +03:00
|
|
|
canvas_set_bitmap_mode(canvas, 0);
|
2022-07-08 15:36:34 +03:00
|
|
|
canvas_set_color(canvas, ColorWhite);
|
|
|
|
}
|
2023-04-29 18:16:53 +03:00
|
|
|
canvas_draw_icon(canvas, 95, 25, &I_Pin_arrow_right_9x7);
|
2022-07-08 15:36:34 +03:00
|
|
|
canvas_set_color(canvas, ColorBlack);
|
|
|
|
|
|
|
|
// Ok
|
|
|
|
if(model->left_mouse_pressed) {
|
2023-04-14 01:59:16 +03:00
|
|
|
canvas_set_bitmap_mode(canvas, 1);
|
2023-04-29 18:16:53 +03:00
|
|
|
canvas_draw_icon(canvas, 74, 19, &I_Pressed_Button_19x19);
|
2023-04-14 01:59:16 +03:00
|
|
|
canvas_set_bitmap_mode(canvas, 0);
|
|
|
|
canvas_set_color(canvas, ColorWhite);
|
2022-07-08 15:36:34 +03:00
|
|
|
}
|
2023-04-29 18:16:53 +03:00
|
|
|
canvas_draw_icon(canvas, 79, 24, &I_Left_mouse_icon_9x9);
|
2023-04-14 01:59:16 +03:00
|
|
|
canvas_set_color(canvas, ColorBlack);
|
2022-07-08 15:36:34 +03:00
|
|
|
|
|
|
|
// Back
|
|
|
|
if(model->right_mouse_pressed) {
|
2023-04-14 01:59:16 +03:00
|
|
|
canvas_set_bitmap_mode(canvas, 1);
|
2023-04-29 18:16:53 +03:00
|
|
|
canvas_draw_icon(canvas, 107, 33, &I_Pressed_Button_19x19);
|
2023-04-14 01:59:16 +03:00
|
|
|
canvas_set_bitmap_mode(canvas, 0);
|
|
|
|
canvas_set_color(canvas, ColorWhite);
|
2022-07-08 15:36:34 +03:00
|
|
|
}
|
2023-04-29 18:16:53 +03:00
|
|
|
canvas_draw_icon(canvas, 112, 38, &I_Right_mouse_icon_9x9);
|
2023-04-14 01:59:16 +03:00
|
|
|
canvas_set_color(canvas, ColorBlack);
|
2022-07-08 15:36:34 +03:00
|
|
|
}
|
|
|
|
|
2022-11-28 19:27:16 +03:00
|
|
|
static void hid_mouse_process(HidMouse* hid_mouse, InputEvent* event) {
|
2022-07-08 15:36:34 +03:00
|
|
|
with_view_model(
|
2022-11-28 19:27:16 +03:00
|
|
|
hid_mouse->view,
|
|
|
|
HidMouseModel * model,
|
2022-10-08 20:38:29 +03:00
|
|
|
{
|
2022-07-08 15:36:34 +03:00
|
|
|
if(event->key == InputKeyBack) {
|
|
|
|
if(event->type == InputTypeShort) {
|
2022-11-28 19:27:16 +03:00
|
|
|
hid_hal_mouse_press(hid_mouse->hid, HID_MOUSE_BTN_RIGHT);
|
|
|
|
hid_hal_mouse_release(hid_mouse->hid, HID_MOUSE_BTN_RIGHT);
|
2022-07-08 15:36:34 +03:00
|
|
|
} else if(event->type == InputTypePress) {
|
|
|
|
model->right_mouse_pressed = true;
|
|
|
|
} else if(event->type == InputTypeRelease) {
|
|
|
|
model->right_mouse_pressed = false;
|
|
|
|
}
|
|
|
|
} else if(event->key == InputKeyOk) {
|
|
|
|
if(event->type == InputTypeShort) {
|
|
|
|
// Just release if it was being held before
|
2022-11-28 19:27:16 +03:00
|
|
|
if(!model->left_mouse_held)
|
|
|
|
hid_hal_mouse_press(hid_mouse->hid, HID_MOUSE_BTN_LEFT);
|
|
|
|
hid_hal_mouse_release(hid_mouse->hid, HID_MOUSE_BTN_LEFT);
|
2022-07-08 15:36:34 +03:00
|
|
|
model->left_mouse_held = false;
|
|
|
|
} else if(event->type == InputTypeLong) {
|
2022-11-28 19:27:16 +03:00
|
|
|
hid_hal_mouse_press(hid_mouse->hid, HID_MOUSE_BTN_LEFT);
|
2022-07-08 15:36:34 +03:00
|
|
|
model->left_mouse_held = true;
|
|
|
|
model->left_mouse_pressed = true;
|
|
|
|
} else if(event->type == InputTypePress) {
|
|
|
|
model->left_mouse_pressed = true;
|
|
|
|
} else if(event->type == InputTypeRelease) {
|
|
|
|
// Only release if it wasn't a long press
|
|
|
|
if(!model->left_mouse_held) model->left_mouse_pressed = false;
|
|
|
|
}
|
|
|
|
} else if(event->key == InputKeyRight) {
|
|
|
|
if(event->type == InputTypePress) {
|
|
|
|
model->right_pressed = true;
|
2022-11-28 19:27:16 +03:00
|
|
|
hid_hal_mouse_move(hid_mouse->hid, MOUSE_MOVE_SHORT, 0);
|
2022-07-08 15:36:34 +03:00
|
|
|
} else if(event->type == InputTypeRepeat) {
|
2022-11-28 19:27:16 +03:00
|
|
|
hid_hal_mouse_move(hid_mouse->hid, MOUSE_MOVE_LONG, 0);
|
2022-07-08 15:36:34 +03:00
|
|
|
} else if(event->type == InputTypeRelease) {
|
|
|
|
model->right_pressed = false;
|
|
|
|
}
|
|
|
|
} else if(event->key == InputKeyLeft) {
|
|
|
|
if(event->type == InputTypePress) {
|
|
|
|
model->left_pressed = true;
|
2022-11-28 19:27:16 +03:00
|
|
|
hid_hal_mouse_move(hid_mouse->hid, -MOUSE_MOVE_SHORT, 0);
|
2022-07-08 15:36:34 +03:00
|
|
|
} else if(event->type == InputTypeRepeat) {
|
2022-11-28 19:27:16 +03:00
|
|
|
hid_hal_mouse_move(hid_mouse->hid, -MOUSE_MOVE_LONG, 0);
|
2022-07-08 15:36:34 +03:00
|
|
|
} else if(event->type == InputTypeRelease) {
|
|
|
|
model->left_pressed = false;
|
|
|
|
}
|
|
|
|
} else if(event->key == InputKeyDown) {
|
|
|
|
if(event->type == InputTypePress) {
|
|
|
|
model->down_pressed = true;
|
2022-11-28 19:27:16 +03:00
|
|
|
hid_hal_mouse_move(hid_mouse->hid, 0, MOUSE_MOVE_SHORT);
|
2022-07-08 15:36:34 +03:00
|
|
|
} else if(event->type == InputTypeRepeat) {
|
2022-11-28 19:27:16 +03:00
|
|
|
hid_hal_mouse_move(hid_mouse->hid, 0, MOUSE_MOVE_LONG);
|
2022-07-08 15:36:34 +03:00
|
|
|
} else if(event->type == InputTypeRelease) {
|
|
|
|
model->down_pressed = false;
|
|
|
|
}
|
|
|
|
} else if(event->key == InputKeyUp) {
|
|
|
|
if(event->type == InputTypePress) {
|
|
|
|
model->up_pressed = true;
|
2022-11-28 19:27:16 +03:00
|
|
|
hid_hal_mouse_move(hid_mouse->hid, 0, -MOUSE_MOVE_SHORT);
|
2022-07-08 15:36:34 +03:00
|
|
|
} else if(event->type == InputTypeRepeat) {
|
2022-11-28 19:27:16 +03:00
|
|
|
hid_hal_mouse_move(hid_mouse->hid, 0, -MOUSE_MOVE_LONG);
|
2022-07-08 15:36:34 +03:00
|
|
|
} else if(event->type == InputTypeRelease) {
|
|
|
|
model->up_pressed = false;
|
|
|
|
}
|
|
|
|
}
|
2022-10-08 20:38:29 +03:00
|
|
|
},
|
|
|
|
true);
|
2022-07-08 15:36:34 +03:00
|
|
|
}
|
|
|
|
|
2022-11-28 19:27:16 +03:00
|
|
|
static bool hid_mouse_input_callback(InputEvent* event, void* context) {
|
2022-07-08 15:36:34 +03:00
|
|
|
furi_assert(context);
|
2022-11-28 19:27:16 +03:00
|
|
|
HidMouse* hid_mouse = context;
|
2022-07-08 15:36:34 +03:00
|
|
|
bool consumed = false;
|
|
|
|
|
|
|
|
if(event->type == InputTypeLong && event->key == InputKeyBack) {
|
2022-11-28 19:27:16 +03:00
|
|
|
hid_hal_mouse_release_all(hid_mouse->hid);
|
2022-07-08 15:36:34 +03:00
|
|
|
} else {
|
2022-11-28 19:27:16 +03:00
|
|
|
hid_mouse_process(hid_mouse, event);
|
2022-07-08 15:36:34 +03:00
|
|
|
consumed = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return consumed;
|
|
|
|
}
|
|
|
|
|
2022-11-28 19:27:16 +03:00
|
|
|
HidMouse* hid_mouse_alloc(Hid* hid) {
|
|
|
|
HidMouse* hid_mouse = malloc(sizeof(HidMouse));
|
|
|
|
hid_mouse->view = view_alloc();
|
|
|
|
hid_mouse->hid = hid;
|
|
|
|
view_set_context(hid_mouse->view, hid_mouse);
|
|
|
|
view_allocate_model(hid_mouse->view, ViewModelTypeLocking, sizeof(HidMouseModel));
|
|
|
|
view_set_draw_callback(hid_mouse->view, hid_mouse_draw_callback);
|
|
|
|
view_set_input_callback(hid_mouse->view, hid_mouse_input_callback);
|
2022-07-08 15:36:34 +03:00
|
|
|
|
2022-12-27 11:03:56 +03:00
|
|
|
with_view_model(
|
|
|
|
hid_mouse->view, HidMouseModel * model, { model->transport = hid->transport; }, true);
|
|
|
|
|
2022-11-28 19:27:16 +03:00
|
|
|
return hid_mouse;
|
2022-07-08 15:36:34 +03:00
|
|
|
}
|
|
|
|
|
2022-11-28 19:27:16 +03:00
|
|
|
void hid_mouse_free(HidMouse* hid_mouse) {
|
|
|
|
furi_assert(hid_mouse);
|
|
|
|
view_free(hid_mouse->view);
|
|
|
|
free(hid_mouse);
|
2022-07-08 15:36:34 +03:00
|
|
|
}
|
|
|
|
|
2022-11-28 19:27:16 +03:00
|
|
|
View* hid_mouse_get_view(HidMouse* hid_mouse) {
|
|
|
|
furi_assert(hid_mouse);
|
|
|
|
return hid_mouse->view;
|
2022-07-08 15:36:34 +03:00
|
|
|
}
|
|
|
|
|
2022-11-28 19:27:16 +03:00
|
|
|
void hid_mouse_set_connected_status(HidMouse* hid_mouse, bool connected) {
|
|
|
|
furi_assert(hid_mouse);
|
2022-07-08 15:36:34 +03:00
|
|
|
with_view_model(
|
2022-11-28 19:27:16 +03:00
|
|
|
hid_mouse->view, HidMouseModel * model, { model->connected = connected; }, true);
|
2022-07-08 15:36:34 +03:00
|
|
|
}
|