bt_mouse.c 9.9 KB

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