bt_mouse.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. #include "bt_mouse.h"
  2. #include "../tracking/main_loop.h"
  3. #include <furi.h>
  4. #include <furi_hal_bt.h>
  5. #include <extra_profiles/hid_profile.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. FuriHalBleProfileBase* ble_hid_profile;
  21. NotificationApp* notifications;
  22. FuriMutex* mutex;
  23. FuriThread* thread;
  24. bool connected;
  25. // Current mouse state
  26. uint8_t btn;
  27. int dx;
  28. int dy;
  29. int wheel;
  30. // Circular buffer;
  31. // (qhead == qtail) means either empty or overflow.
  32. // We'll ignore overflow and treat it as empty.
  33. int qhead;
  34. int qtail;
  35. ButtonEvent queue[BTN_EVT_QUEUE_SIZE];
  36. };
  37. #define BT_MOUSE_FLAG_INPUT_EVENT (1UL << 0)
  38. #define BT_MOUSE_FLAG_KILL_THREAD (1UL << 1)
  39. #define BT_MOUSE_FLAG_ALL (BT_MOUSE_FLAG_INPUT_EVENT | BT_MOUSE_FLAG_KILL_THREAD)
  40. #define MOUSE_SCROLL 2
  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. } else if(event->key == InputKeyRight) {
  91. if(event->type == InputTypePress || event->type == InputTypeRepeat) {
  92. bt_mouse->wheel = MOUSE_SCROLL;
  93. }
  94. } else if(event->key == InputKeyLeft) {
  95. if(event->type == InputTypePress || event->type == InputTypeRepeat) {
  96. bt_mouse->wheel = -MOUSE_SCROLL;
  97. }
  98. }
  99. },
  100. true);
  101. }
  102. static bool bt_mouse_input_callback(InputEvent* event, void* context) {
  103. furi_assert(context);
  104. BtMouse* bt_mouse = context;
  105. bool consumed = false;
  106. if(event->type == InputTypeLong && event->key == InputKeyBack) {
  107. ble_profile_hid_mouse_release_all(bt_mouse->ble_hid_profile);
  108. } else {
  109. bt_mouse_process(bt_mouse, event);
  110. consumed = true;
  111. }
  112. return consumed;
  113. }
  114. void bt_mouse_connection_status_changed_callback(BtStatus status, void* context) {
  115. furi_assert(context);
  116. BtMouse* bt_mouse = context;
  117. bt_mouse->connected = (status == BtStatusConnected);
  118. if(!bt_mouse->notifications) {
  119. tracking_end();
  120. return;
  121. }
  122. if(bt_mouse->connected) {
  123. notification_internal_message(bt_mouse->notifications, &sequence_set_blue_255);
  124. tracking_begin();
  125. view_dispatcher_send_custom_event(bt_mouse->view_dispatcher, 0);
  126. } else {
  127. tracking_end();
  128. notification_internal_message(bt_mouse->notifications, &sequence_reset_blue);
  129. }
  130. }
  131. bool bt_mouse_move(int8_t dx, int8_t dy, void* context) {
  132. furi_assert(context);
  133. BtMouse* bt_mouse = context;
  134. if(bt_mouse->connected) {
  135. furi_mutex_acquire(bt_mouse->mutex, FuriWaitForever);
  136. bt_mouse->dx += dx;
  137. bt_mouse->dy += dy;
  138. furi_mutex_release(bt_mouse->mutex);
  139. bt_mouse_notify_event(bt_mouse);
  140. }
  141. return true;
  142. }
  143. static int8_t clamp(int t) {
  144. if(t < -128) {
  145. return -128;
  146. } else if(t > 127) {
  147. return 127;
  148. }
  149. return t;
  150. }
  151. static int32_t bt_mouse_thread_callback(void* context) {
  152. furi_assert(context);
  153. BtMouse* bt_mouse = (BtMouse*)context;
  154. while(1) {
  155. uint32_t flags =
  156. furi_thread_flags_wait(BT_MOUSE_FLAG_ALL, FuriFlagWaitAny, FuriWaitForever);
  157. if(flags & BT_MOUSE_FLAG_KILL_THREAD) {
  158. break;
  159. }
  160. if(flags & BT_MOUSE_FLAG_INPUT_EVENT) {
  161. furi_mutex_acquire(bt_mouse->mutex, FuriWaitForever);
  162. ButtonEvent event;
  163. bool send_buttons = false;
  164. if(bt_mouse->qhead != bt_mouse->qtail) {
  165. event = bt_mouse->queue[bt_mouse->qhead++];
  166. bt_mouse->qhead %= BTN_EVT_QUEUE_SIZE;
  167. send_buttons = true;
  168. }
  169. int8_t dx = clamp(bt_mouse->dx);
  170. bt_mouse->dx -= dx;
  171. int8_t dy = clamp(bt_mouse->dy);
  172. bt_mouse->dy -= dy;
  173. int8_t wheel = clamp(bt_mouse->wheel);
  174. bt_mouse->wheel -= wheel;
  175. furi_mutex_release(bt_mouse->mutex);
  176. if(bt_mouse->connected && send_buttons) {
  177. if(event.state) {
  178. ble_profile_hid_mouse_press(bt_mouse->ble_hid_profile, event.button);
  179. } else {
  180. ble_profile_hid_mouse_release(bt_mouse->ble_hid_profile, event.button);
  181. }
  182. }
  183. if(bt_mouse->connected && (dx != 0 || dy != 0)) {
  184. ble_profile_hid_mouse_move(bt_mouse->ble_hid_profile, dx, dy);
  185. }
  186. if(bt_mouse->connected && wheel != 0) {
  187. ble_profile_hid_mouse_scroll(bt_mouse->ble_hid_profile, wheel);
  188. }
  189. }
  190. }
  191. return 0;
  192. }
  193. void bt_mouse_thread_start(BtMouse* bt_mouse) {
  194. furi_assert(bt_mouse);
  195. bt_mouse->mutex = furi_mutex_alloc(FuriMutexTypeNormal);
  196. bt_mouse->thread = furi_thread_alloc();
  197. furi_thread_set_name(bt_mouse->thread, "BtSender");
  198. furi_thread_set_stack_size(bt_mouse->thread, 1024);
  199. furi_thread_set_context(bt_mouse->thread, bt_mouse);
  200. furi_thread_set_callback(bt_mouse->thread, bt_mouse_thread_callback);
  201. furi_thread_start(bt_mouse->thread);
  202. }
  203. void bt_mouse_thread_stop(BtMouse* bt_mouse) {
  204. furi_assert(bt_mouse);
  205. FuriThreadId thread_id = furi_thread_get_id(bt_mouse->thread);
  206. furi_assert(thread_id);
  207. furi_thread_flags_set(thread_id, BT_MOUSE_FLAG_KILL_THREAD);
  208. furi_thread_join(bt_mouse->thread);
  209. furi_thread_free(bt_mouse->thread);
  210. furi_mutex_free(bt_mouse->mutex);
  211. bt_mouse->mutex = NULL;
  212. bt_mouse->thread = NULL;
  213. }
  214. void bt_mouse_enter_callback(void* context) {
  215. furi_assert(context);
  216. BtMouse* bt_mouse = context;
  217. bt_mouse->bt = furi_record_open(RECORD_BT);
  218. bt_mouse->notifications = furi_record_open(RECORD_NOTIFICATION);
  219. bt_set_status_changed_callback(
  220. bt_mouse->bt, bt_mouse_connection_status_changed_callback, bt_mouse);
  221. bt_mouse->ble_hid_profile = bt_profile_start(bt_mouse->bt, ble_profile_hid, NULL);
  222. furi_check(bt_mouse->ble_hid_profile);
  223. furi_hal_bt_start_advertising();
  224. bt_mouse_thread_start(bt_mouse);
  225. }
  226. bool bt_mouse_custom_callback(uint32_t event, void* context) {
  227. UNUSED(event);
  228. furi_assert(context);
  229. BtMouse* bt_mouse = context;
  230. tracking_step(bt_mouse_move, context);
  231. furi_delay_ms(3); // Magic! Removing this will break the buttons
  232. view_dispatcher_send_custom_event(bt_mouse->view_dispatcher, 0);
  233. return true;
  234. }
  235. void bt_mouse_exit_callback(void* context) {
  236. furi_assert(context);
  237. BtMouse* bt_mouse = context;
  238. bt_mouse_thread_stop(bt_mouse);
  239. tracking_end();
  240. notification_internal_message(bt_mouse->notifications, &sequence_reset_blue);
  241. bt_set_status_changed_callback(bt_mouse->bt, NULL, NULL);
  242. furi_check(bt_profile_restore_default(bt_mouse->bt));
  243. furi_record_close(RECORD_NOTIFICATION);
  244. bt_mouse->notifications = NULL;
  245. furi_record_close(RECORD_BT);
  246. bt_mouse->bt = NULL;
  247. }
  248. BtMouse* bt_mouse_alloc(ViewDispatcher* view_dispatcher) {
  249. BtMouse* bt_mouse = malloc(sizeof(BtMouse));
  250. memset(bt_mouse, 0, sizeof(BtMouse));
  251. bt_mouse->view = view_alloc();
  252. bt_mouse->view_dispatcher = view_dispatcher;
  253. view_set_context(bt_mouse->view, bt_mouse);
  254. view_set_draw_callback(bt_mouse->view, bt_mouse_draw_callback);
  255. view_set_input_callback(bt_mouse->view, bt_mouse_input_callback);
  256. view_set_enter_callback(bt_mouse->view, bt_mouse_enter_callback);
  257. view_set_custom_callback(bt_mouse->view, bt_mouse_custom_callback);
  258. view_set_exit_callback(bt_mouse->view, bt_mouse_exit_callback);
  259. return bt_mouse;
  260. }
  261. void bt_mouse_free(BtMouse* bt_mouse) {
  262. furi_assert(bt_mouse);
  263. view_free(bt_mouse->view);
  264. free(bt_mouse);
  265. }
  266. View* bt_mouse_get_view(BtMouse* bt_mouse) {
  267. furi_assert(bt_mouse);
  268. return bt_mouse->view;
  269. }