bt_mouse.c 9.7 KB

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