bt_mouse.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. #include "bt_mouse.h"
  2. #include "../tracking/main_loop.h"
  3. #include <furi.h>
  4. #include <furi_hal_bt.h>
  5. #include <furi_hal_bt_hid.h>
  6. #include <furi_hal_usb_hid.h>
  7. #include <bt/bt_service/bt.h>
  8. #include <gui/elements.h>
  9. #include <notification/notification.h>
  10. #include <notification/notification_messages.h>
  11. typedef struct ButtonEvent {
  12. int8_t button;
  13. bool state;
  14. } ButtonEvent;
  15. #define BTN_EVT_QUEUE_SIZE 32
  16. struct BtMouse {
  17. View* view;
  18. ViewDispatcher* view_dispatcher;
  19. Bt* bt;
  20. NotificationApp* notifications;
  21. FuriMutex* mutex;
  22. FuriThread* thread;
  23. bool connected;
  24. // Current mouse state
  25. uint8_t btn;
  26. int dx;
  27. int dy;
  28. int wheel;
  29. // Circular buffer;
  30. // (qhead == qtail) means either empty or overflow.
  31. // We'll ignore overflow and treat it as empty.
  32. int qhead;
  33. int qtail;
  34. ButtonEvent queue[BTN_EVT_QUEUE_SIZE];
  35. };
  36. #define BT_MOUSE_FLAG_INPUT_EVENT (1UL << 0)
  37. #define BT_MOUSE_FLAG_KILL_THREAD (1UL << 1)
  38. #define BT_MOUSE_FLAG_ALL (BT_MOUSE_FLAG_INPUT_EVENT | BT_MOUSE_FLAG_KILL_THREAD)
  39. #define MOUSE_MOVE_SHORT 5
  40. #define MOUSE_MOVE_LONG 20
  41. static void bt_mouse_notify_event(BtMouse* bt_mouse) {
  42. FuriThreadId thread_id = furi_thread_get_id(bt_mouse->thread);
  43. furi_assert(thread_id);
  44. furi_thread_flags_set(thread_id, BT_MOUSE_FLAG_INPUT_EVENT);
  45. }
  46. static void bt_mouse_draw_callback(Canvas* canvas, void* context) {
  47. UNUSED(context);
  48. canvas_clear(canvas);
  49. canvas_set_font(canvas, FontPrimary);
  50. canvas_draw_str(canvas, 0, 10, "Bluetooth Mouse mode");
  51. canvas_set_font(canvas, FontSecondary);
  52. canvas_draw_str(canvas, 0, 63, "Hold [back] to exit");
  53. }
  54. static void bt_mouse_button_state(BtMouse* bt_mouse, int8_t button, bool state) {
  55. ButtonEvent event;
  56. event.button = button;
  57. event.state = state;
  58. if (bt_mouse->connected) {
  59. furi_mutex_acquire(bt_mouse->mutex, FuriWaitForever);
  60. bt_mouse->queue[bt_mouse->qtail++] = event;
  61. bt_mouse->qtail %= BTN_EVT_QUEUE_SIZE;
  62. furi_mutex_release(bt_mouse->mutex);
  63. bt_mouse_notify_event(bt_mouse);
  64. }
  65. }
  66. static void bt_mouse_process(BtMouse* bt_mouse, InputEvent* event) {
  67. with_view_model(
  68. bt_mouse->view,
  69. void* model,
  70. {
  71. UNUSED(model);
  72. if(event->key == InputKeyUp) {
  73. if(event->type == InputTypePress) {
  74. bt_mouse_button_state(bt_mouse, HID_MOUSE_BTN_LEFT, true);
  75. } else if(event->type == InputTypeRelease) {
  76. bt_mouse_button_state(bt_mouse, HID_MOUSE_BTN_LEFT, false);
  77. }
  78. } else if(event->key == InputKeyDown) {
  79. if(event->type == InputTypePress) {
  80. bt_mouse_button_state(bt_mouse, HID_MOUSE_BTN_RIGHT, true);
  81. } else if(event->type == InputTypeRelease) {
  82. bt_mouse_button_state(bt_mouse, HID_MOUSE_BTN_RIGHT, false);
  83. }
  84. } else if(event->key == InputKeyOk) {
  85. if(event->type == InputTypePress) {
  86. bt_mouse_button_state(bt_mouse, HID_MOUSE_BTN_WHEEL, true);
  87. } else if(event->type == InputTypeRelease) {
  88. bt_mouse_button_state(bt_mouse, HID_MOUSE_BTN_WHEEL, false);
  89. }
  90. }
  91. },
  92. true);
  93. }
  94. static bool bt_mouse_input_callback(InputEvent* event, void* context) {
  95. furi_assert(context);
  96. BtMouse* bt_mouse = context;
  97. bool consumed = false;
  98. if(event->type == InputTypeLong && event->key == InputKeyBack) {
  99. furi_hal_bt_hid_mouse_release_all();
  100. } else {
  101. bt_mouse_process(bt_mouse, event);
  102. consumed = true;
  103. }
  104. return consumed;
  105. }
  106. void bt_mouse_connection_status_changed_callback(BtStatus status, void* context) {
  107. furi_assert(context);
  108. BtMouse* bt_mouse = context;
  109. bt_mouse->connected = (status == BtStatusConnected);
  110. if(bt_mouse->connected) {
  111. notification_internal_message(bt_mouse->notifications, &sequence_set_blue_255);
  112. tracking_begin();
  113. view_dispatcher_send_custom_event(bt_mouse->view_dispatcher, 0);
  114. } else {
  115. tracking_end();
  116. notification_internal_message(bt_mouse->notifications, &sequence_reset_blue);
  117. }
  118. //with_view_model(
  119. // bt_mouse->view, void * model, { model->connected = connected; }, true);
  120. }
  121. bool bt_mouse_move(int8_t dx, int8_t dy, void *context) {
  122. furi_assert(context);
  123. BtMouse* bt_mouse = context;
  124. if (bt_mouse->connected) {
  125. furi_mutex_acquire(bt_mouse->mutex, FuriWaitForever);
  126. bt_mouse->dx += dx;
  127. bt_mouse->dy += dy;
  128. furi_mutex_release(bt_mouse->mutex);
  129. bt_mouse_notify_event(bt_mouse);
  130. }
  131. return true;
  132. }
  133. void bt_mouse_enter_callback(void* context) {
  134. furi_assert(context);
  135. BtMouse* bt_mouse = context;
  136. bt_mouse->bt = furi_record_open(RECORD_BT);
  137. bt_mouse->notifications = furi_record_open(RECORD_NOTIFICATION);
  138. bt_set_status_changed_callback(
  139. bt_mouse->bt, bt_mouse_connection_status_changed_callback, bt_mouse);
  140. furi_assert(bt_set_profile(bt_mouse->bt, BtProfileHidKeyboard));
  141. furi_hal_bt_start_advertising();
  142. }
  143. bool bt_mouse_custom_callback(uint32_t event, void* context) {
  144. UNUSED(event);
  145. furi_assert(context);
  146. BtMouse* bt_mouse = context;
  147. tracking_step(bt_mouse_move, context);
  148. furi_delay_ms(3); // Magic! Removing this will break the buttons
  149. view_dispatcher_send_custom_event(bt_mouse->view_dispatcher, 0);
  150. return true;
  151. }
  152. void bt_mouse_exit_callback(void* context) {
  153. furi_assert(context);
  154. BtMouse* bt_mouse = context;
  155. tracking_end();
  156. notification_internal_message(bt_mouse->notifications, &sequence_reset_blue);
  157. furi_hal_bt_stop_advertising();
  158. bt_set_profile(bt_mouse->bt, BtProfileSerial);
  159. furi_record_close(RECORD_NOTIFICATION);
  160. bt_mouse->notifications = NULL;
  161. furi_record_close(RECORD_BT);
  162. bt_mouse->bt = NULL;
  163. }
  164. static int8_t clamp(int t) {
  165. if (t < -128) {
  166. return -128;
  167. }
  168. else if (t > 127) {
  169. return 127;
  170. }
  171. return t;
  172. }
  173. static int32_t bt_mouse_thread_callback(void* context) {
  174. furi_assert(context);
  175. BtMouse* bt_mouse = (BtMouse*)context;
  176. while(1) {
  177. uint32_t flags = furi_thread_flags_wait(BT_MOUSE_FLAG_ALL, FuriFlagWaitAny, FuriWaitForever);
  178. if(flags & BT_MOUSE_FLAG_KILL_THREAD) {
  179. break;
  180. }
  181. if(flags & BT_MOUSE_FLAG_INPUT_EVENT) {
  182. furi_mutex_acquire(bt_mouse->mutex, FuriWaitForever);
  183. ButtonEvent event;
  184. bool send_buttons = false;
  185. if (bt_mouse->qhead != bt_mouse->qtail) {
  186. event = bt_mouse->queue[bt_mouse->qhead++];
  187. bt_mouse->qhead %= BTN_EVT_QUEUE_SIZE;
  188. send_buttons = true;
  189. }
  190. int8_t dx = clamp(bt_mouse->dx);
  191. bt_mouse->dx -= dx;
  192. int8_t dy = clamp(bt_mouse->dy);
  193. bt_mouse->dy -= dy;
  194. int8_t wheel = clamp(bt_mouse->wheel);
  195. bt_mouse->wheel -= wheel;
  196. furi_mutex_release(bt_mouse->mutex);
  197. if (bt_mouse->connected && send_buttons) {
  198. if (event.state) {
  199. furi_hal_bt_hid_mouse_press(event.button);
  200. } else {
  201. furi_hal_bt_hid_mouse_release(event.button);
  202. }
  203. }
  204. if (bt_mouse->connected && (dx != 0 || dy != 0)) {
  205. furi_hal_bt_hid_mouse_move(dx, dy);
  206. }
  207. if (bt_mouse->connected && wheel != 0) {
  208. furi_hal_bt_hid_mouse_scroll(wheel);
  209. }
  210. }
  211. }
  212. return 0;
  213. }
  214. void bt_mouse_thread_start(BtMouse* bt_mouse) {
  215. furi_assert(bt_mouse);
  216. bt_mouse->mutex = furi_mutex_alloc(FuriMutexTypeNormal);
  217. bt_mouse->thread = furi_thread_alloc();
  218. furi_thread_set_name(bt_mouse->thread, "BtSender");
  219. furi_thread_set_stack_size(bt_mouse->thread, 1024);
  220. furi_thread_set_context(bt_mouse->thread, bt_mouse);
  221. furi_thread_set_callback(bt_mouse->thread, bt_mouse_thread_callback);
  222. furi_thread_start(bt_mouse->thread);
  223. }
  224. void bt_mouse_thread_stop(BtMouse* bt_mouse) {
  225. furi_assert(bt_mouse);
  226. FuriThreadId thread_id = furi_thread_get_id(bt_mouse->thread);
  227. furi_assert(thread_id);
  228. furi_thread_flags_set(thread_id, BT_MOUSE_FLAG_KILL_THREAD);
  229. furi_thread_join(bt_mouse->thread);
  230. furi_thread_free(bt_mouse->thread);
  231. furi_mutex_free(bt_mouse->mutex);
  232. }
  233. BtMouse* bt_mouse_alloc(ViewDispatcher* view_dispatcher) {
  234. BtMouse* bt_mouse = malloc(sizeof(BtMouse));
  235. memset(bt_mouse, 0, sizeof(BtMouse));
  236. bt_mouse->view = view_alloc();
  237. bt_mouse->view_dispatcher = view_dispatcher;
  238. view_set_context(bt_mouse->view, bt_mouse);
  239. view_set_draw_callback(bt_mouse->view, bt_mouse_draw_callback);
  240. view_set_input_callback(bt_mouse->view, bt_mouse_input_callback);
  241. view_set_enter_callback(bt_mouse->view, bt_mouse_enter_callback);
  242. view_set_custom_callback(bt_mouse->view, bt_mouse_custom_callback);
  243. view_set_exit_callback(bt_mouse->view, bt_mouse_exit_callback);
  244. bt_mouse_thread_start(bt_mouse);
  245. return bt_mouse;
  246. }
  247. void bt_mouse_free(BtMouse* bt_mouse) {
  248. furi_assert(bt_mouse);
  249. bt_mouse_thread_stop(bt_mouse);
  250. view_free(bt_mouse->view);
  251. free(bt_mouse);
  252. }
  253. View* bt_mouse_get_view(BtMouse* bt_mouse) {
  254. furi_assert(bt_mouse);
  255. return bt_mouse->view;
  256. }