2021-10-13 16:38:24 +00:00
|
|
|
#include <furi.h>
|
2022-01-05 16:10:18 +00:00
|
|
|
#include <furi_hal.h>
|
2021-10-13 16:38:24 +00:00
|
|
|
#include <gui/gui.h>
|
|
|
|
#include <input/input.h>
|
|
|
|
|
|
|
|
#define MOUSE_MOVE_SHORT 5
|
2024-07-15 17:02:45 +00:00
|
|
|
#define MOUSE_MOVE_LONG 20
|
2021-10-13 16:38:24 +00:00
|
|
|
|
|
|
|
typedef enum {
|
|
|
|
EventTypeInput,
|
|
|
|
} EventType;
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
union {
|
|
|
|
InputEvent input;
|
|
|
|
};
|
|
|
|
EventType type;
|
|
|
|
} UsbMouseEvent;
|
|
|
|
|
|
|
|
static void usb_mouse_render_callback(Canvas* canvas, void* ctx) {
|
2022-05-06 13:37:10 +00:00
|
|
|
UNUSED(ctx);
|
2021-10-13 16:38:24 +00:00
|
|
|
canvas_clear(canvas);
|
|
|
|
|
|
|
|
canvas_set_font(canvas, FontPrimary);
|
2022-04-11 11:54:38 +00:00
|
|
|
canvas_draw_str(canvas, 0, 10, "USB Mouse Demo");
|
2021-10-13 16:38:24 +00:00
|
|
|
|
|
|
|
canvas_set_font(canvas, FontSecondary);
|
|
|
|
canvas_draw_str(canvas, 0, 63, "Hold [back] to exit");
|
|
|
|
}
|
|
|
|
|
|
|
|
static void usb_mouse_input_callback(InputEvent* input_event, void* ctx) {
|
2022-07-20 10:56:33 +00:00
|
|
|
FuriMessageQueue* event_queue = ctx;
|
2021-10-13 16:38:24 +00:00
|
|
|
|
|
|
|
UsbMouseEvent event;
|
|
|
|
event.type = EventTypeInput;
|
|
|
|
event.input = *input_event;
|
2022-07-20 10:56:33 +00:00
|
|
|
furi_message_queue_put(event_queue, &event, FuriWaitForever);
|
2021-10-13 16:38:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int32_t usb_mouse_app(void* p) {
|
2022-05-06 13:37:10 +00:00
|
|
|
UNUSED(p);
|
2022-07-20 10:56:33 +00:00
|
|
|
FuriMessageQueue* event_queue = furi_message_queue_alloc(8, sizeof(UsbMouseEvent));
|
2021-10-13 16:38:24 +00:00
|
|
|
ViewPort* view_port = view_port_alloc();
|
|
|
|
|
2022-01-21 16:55:44 +00:00
|
|
|
FuriHalUsbInterface* usb_mode_prev = furi_hal_usb_get_config();
|
2022-03-24 15:45:03 +00:00
|
|
|
furi_hal_usb_unlock();
|
|
|
|
furi_check(furi_hal_usb_set_config(&usb_hid, NULL) == true);
|
2021-10-13 16:38:24 +00:00
|
|
|
|
|
|
|
view_port_draw_callback_set(view_port, usb_mouse_render_callback, NULL);
|
|
|
|
view_port_input_callback_set(view_port, usb_mouse_input_callback, event_queue);
|
|
|
|
|
|
|
|
// Open GUI and register view_port
|
2022-07-26 12:21:51 +00:00
|
|
|
Gui* gui = furi_record_open(RECORD_GUI);
|
2021-10-13 16:38:24 +00:00
|
|
|
gui_add_view_port(gui, view_port, GuiLayerFullscreen);
|
|
|
|
|
|
|
|
UsbMouseEvent event;
|
|
|
|
while(1) {
|
2022-07-20 10:56:33 +00:00
|
|
|
FuriStatus event_status = furi_message_queue_get(event_queue, &event, FuriWaitForever);
|
2021-10-13 16:38:24 +00:00
|
|
|
|
2022-07-20 10:56:33 +00:00
|
|
|
if(event_status == FuriStatusOk) {
|
2021-10-13 16:38:24 +00:00
|
|
|
if(event.type == EventTypeInput) {
|
|
|
|
if(event.input.type == InputTypeLong && event.input.key == InputKeyBack) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(event.input.type == InputTypeShort && event.input.key == InputKeyBack) {
|
|
|
|
furi_hal_hid_mouse_press(HID_MOUSE_BTN_RIGHT);
|
|
|
|
furi_hal_hid_mouse_release(HID_MOUSE_BTN_RIGHT);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(event.input.key == InputKeyOk) {
|
|
|
|
if(event.input.type == InputTypePress) {
|
|
|
|
furi_hal_hid_mouse_press(HID_MOUSE_BTN_LEFT);
|
|
|
|
} else if(event.input.type == InputTypeRelease) {
|
|
|
|
furi_hal_hid_mouse_release(HID_MOUSE_BTN_LEFT);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(event.input.key == InputKeyRight) {
|
|
|
|
if(event.input.type == InputTypePress) {
|
|
|
|
furi_hal_hid_mouse_move(MOUSE_MOVE_SHORT, 0);
|
|
|
|
} else if(event.input.type == InputTypeRepeat) {
|
|
|
|
furi_hal_hid_mouse_move(MOUSE_MOVE_LONG, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(event.input.key == InputKeyLeft) {
|
|
|
|
if(event.input.type == InputTypePress) {
|
|
|
|
furi_hal_hid_mouse_move(-MOUSE_MOVE_SHORT, 0);
|
|
|
|
} else if(event.input.type == InputTypeRepeat) {
|
|
|
|
furi_hal_hid_mouse_move(-MOUSE_MOVE_LONG, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(event.input.key == InputKeyDown) {
|
|
|
|
if(event.input.type == InputTypePress) {
|
|
|
|
furi_hal_hid_mouse_move(0, MOUSE_MOVE_SHORT);
|
|
|
|
} else if(event.input.type == InputTypeRepeat) {
|
|
|
|
furi_hal_hid_mouse_move(0, MOUSE_MOVE_LONG);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(event.input.key == InputKeyUp) {
|
|
|
|
if(event.input.type == InputTypePress) {
|
|
|
|
furi_hal_hid_mouse_move(0, -MOUSE_MOVE_SHORT);
|
|
|
|
} else if(event.input.type == InputTypeRepeat) {
|
|
|
|
furi_hal_hid_mouse_move(0, -MOUSE_MOVE_LONG);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
view_port_update(view_port);
|
|
|
|
}
|
|
|
|
|
2022-03-23 13:35:25 +00:00
|
|
|
furi_hal_usb_set_config(usb_mode_prev, NULL);
|
2021-10-13 16:38:24 +00:00
|
|
|
|
|
|
|
// remove & free all stuff created by app
|
|
|
|
gui_remove_view_port(gui, view_port);
|
|
|
|
view_port_free(view_port);
|
2022-07-20 10:56:33 +00:00
|
|
|
furi_message_queue_free(event_queue);
|
2021-10-13 16:38:24 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|