-
Notifications
You must be signed in to change notification settings - Fork 13
/
usb_hid_autofire.c
127 lines (104 loc) · 3.92 KB
/
usb_hid_autofire.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include <string.h>
#include <furi.h>
#include <furi_hal.h>
#include <gui/gui.h>
#include <input/input.h>
#include "version.h"
#include "tools.h"
// Uncomment to be able to make a screenshot
//#define USB_HID_AUTOFIRE_SCREENSHOT
typedef enum {
EventTypeInput,
} EventType;
typedef struct {
union {
InputEvent input;
};
EventType type;
} UsbMouseEvent;
bool btn_left_autofire = false;
uint32_t autofire_delay = 10;
static void usb_hid_autofire_render_callback(Canvas* canvas, void* ctx) {
UNUSED(ctx);
char autofire_delay_str[12];
//std::string pi = "pi is " std::to_string(3.1415926);
itoa(autofire_delay, autofire_delay_str, 10);
//sprintf(autofire_delay_str, "%lu", autofire_delay);
canvas_clear(canvas);
canvas_set_font(canvas, FontPrimary);
canvas_draw_str(canvas, 0, 10, "USB HID Autofire");
canvas_draw_str(canvas, 0, 34, btn_left_autofire ? "<active>" : "<inactive>");
canvas_set_font(canvas, FontSecondary);
canvas_draw_str(canvas, 90, 10, "v");
canvas_draw_str(canvas, 96, 10, VERSION);
canvas_draw_str(canvas, 0, 22, "Press [ok] for auto left clicking");
canvas_draw_str(canvas, 0, 46, "delay [ms]:");
canvas_draw_str(canvas, 50, 46, autofire_delay_str);
canvas_draw_str(canvas, 0, 63, "Press [back] to exit");
}
static void usb_hid_autofire_input_callback(InputEvent* input_event, void* ctx) {
FuriMessageQueue* event_queue = ctx;
UsbMouseEvent event;
event.type = EventTypeInput;
event.input = *input_event;
furi_message_queue_put(event_queue, &event, FuriWaitForever);
}
int32_t usb_hid_autofire_app(void* p) {
UNUSED(p);
FuriMessageQueue* event_queue = furi_message_queue_alloc(8, sizeof(UsbMouseEvent));
furi_check(event_queue);
ViewPort* view_port = view_port_alloc();
FuriHalUsbInterface* usb_mode_prev = furi_hal_usb_get_config();
#ifndef USB_HID_AUTOFIRE_SCREENSHOT
furi_hal_usb_unlock();
furi_check(furi_hal_usb_set_config(&usb_hid, NULL) == true);
#endif
view_port_draw_callback_set(view_port, usb_hid_autofire_render_callback, NULL);
view_port_input_callback_set(view_port, usb_hid_autofire_input_callback, event_queue);
// Open GUI and register view_port
Gui* gui = furi_record_open(RECORD_GUI);
gui_add_view_port(gui, view_port, GuiLayerFullscreen);
UsbMouseEvent event;
while(1) {
FuriStatus event_status = furi_message_queue_get(event_queue, &event, 50);
if(event_status == FuriStatusOk) {
if(event.type == EventTypeInput) {
if(event.input.key == InputKeyBack) {
break;
}
if(event.input.type != InputTypeRelease) {
continue;
}
switch(event.input.key) {
case InputKeyOk:
btn_left_autofire = !btn_left_autofire;
break;
case InputKeyLeft:
if(autofire_delay > 0) {
autofire_delay -= 10;
}
break;
case InputKeyRight:
autofire_delay = 10;
break;
default:
break;
}
}
}
if(btn_left_autofire) {
furi_hal_hid_mouse_press(HID_MOUSE_BTN_LEFT);
// TODO: Don't wait, but use the timer directly to just don't send the release event (see furi_hal_cortex_delay_us)
furi_delay_us(autofire_delay * 500);
furi_hal_hid_mouse_release(HID_MOUSE_BTN_LEFT);
furi_delay_us(autofire_delay * 500);
}
view_port_update(view_port);
}
furi_hal_usb_set_config(usb_mode_prev, NULL);
// remove & free all stuff created by app
gui_remove_view_port(gui, view_port);
view_port_free(view_port);
furi_message_queue_free(event_queue);
return 0;
}