pointing_device.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  1. /* Copyright 2017 Joshua Broekhuijsen <snipeye+qmk@gmail.com>
  2. * Copyright 2020 Christopher Courtney, aka Drashna Jael're (@drashna) <drashna@live.com>
  3. * Copyright 2021 Dasky (@daskygit)
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include "pointing_device.h"
  19. #include <string.h>
  20. #include "timer.h"
  21. #include "gpio.h"
  22. #ifdef MOUSEKEY_ENABLE
  23. # include "mousekey.h"
  24. #endif
  25. #if (defined(POINTING_DEVICE_ROTATION_90) + defined(POINTING_DEVICE_ROTATION_180) + defined(POINTING_DEVICE_ROTATION_270)) > 1
  26. # error More than one rotation selected. This is not supported.
  27. #endif
  28. #if defined(POINTING_DEVICE_LEFT) || defined(POINTING_DEVICE_RIGHT) || defined(POINTING_DEVICE_COMBINED)
  29. # ifndef SPLIT_POINTING_ENABLE
  30. # error "Using POINTING_DEVICE_LEFT or POINTING_DEVICE_RIGHT or POINTING_DEVICE_COMBINED, then SPLIT_POINTING_ENABLE is required but has not been defined"
  31. # endif
  32. #endif
  33. #if defined(SPLIT_POINTING_ENABLE)
  34. # include "transactions.h"
  35. # include "keyboard.h"
  36. report_mouse_t shared_mouse_report = {};
  37. uint16_t shared_cpi = 0;
  38. /**
  39. * @brief Sets the shared mouse report used be pointing device task
  40. *
  41. * NOTE : Only available when using SPLIT_POINTING_ENABLE
  42. *
  43. * @param[in] new_mouse_report report_mouse_t
  44. */
  45. void pointing_device_set_shared_report(report_mouse_t new_mouse_report) {
  46. shared_mouse_report = new_mouse_report;
  47. }
  48. /**
  49. * @brief Gets current pointing device CPI if supported
  50. *
  51. * Gets current cpi of the shared report and returns it as uint16_t
  52. *
  53. * NOTE : Only available when using SPLIT_POINTING_ENABLE
  54. *
  55. * @return cpi value as uint16_t
  56. */
  57. uint16_t pointing_device_get_shared_cpi(void) {
  58. return shared_cpi;
  59. }
  60. # if defined(POINTING_DEVICE_LEFT)
  61. # define POINTING_DEVICE_THIS_SIDE is_keyboard_left()
  62. # elif defined(POINTING_DEVICE_RIGHT)
  63. # define POINTING_DEVICE_THIS_SIDE !is_keyboard_left()
  64. # elif defined(POINTING_DEVICE_COMBINED)
  65. # define POINTING_DEVICE_THIS_SIDE true
  66. # endif
  67. #endif // defined(SPLIT_POINTING_ENABLE)
  68. static report_mouse_t local_mouse_report = {};
  69. static bool pointing_device_force_send = false;
  70. #define POINTING_DEVICE_DRIVER_CONCAT(name) name##_pointing_device_driver
  71. #define POINTING_DEVICE_DRIVER(name) POINTING_DEVICE_DRIVER_CONCAT(name)
  72. #ifdef POINTING_DEVICE_DRIVER_custom
  73. __attribute__((weak)) void pointing_device_driver_init(void) {}
  74. __attribute__((weak)) report_mouse_t pointing_device_driver_get_report(report_mouse_t mouse_report) {
  75. return mouse_report;
  76. }
  77. __attribute__((weak)) uint16_t pointing_device_driver_get_cpi(void) {
  78. return 0;
  79. }
  80. __attribute__((weak)) void pointing_device_driver_set_cpi(uint16_t cpi) {}
  81. const pointing_device_driver_t custom_pointing_device_driver = {
  82. .init = pointing_device_driver_init,
  83. .get_report = pointing_device_driver_get_report,
  84. .get_cpi = pointing_device_driver_get_cpi,
  85. .set_cpi = pointing_device_driver_set_cpi,
  86. };
  87. #endif
  88. const pointing_device_driver_t *pointing_device_driver = &POINTING_DEVICE_DRIVER(POINTING_DEVICE_DRIVER_NAME);
  89. /**
  90. * @brief Keyboard level code pointing device initialisation
  91. *
  92. */
  93. __attribute__((weak)) void pointing_device_init_kb(void) {}
  94. /**
  95. * @brief User level code pointing device initialisation
  96. *
  97. */
  98. __attribute__((weak)) void pointing_device_init_user(void) {}
  99. /**
  100. * @brief Weak function allowing for keyboard level mouse report modification
  101. *
  102. * Takes report_mouse_t struct allowing modification at keyboard level then returns report_mouse_t.
  103. *
  104. * @param[in] mouse_report report_mouse_t
  105. * @return report_mouse_t
  106. */
  107. __attribute__((weak)) report_mouse_t pointing_device_task_kb(report_mouse_t mouse_report) {
  108. return pointing_device_task_user(mouse_report);
  109. }
  110. /**
  111. * @brief Weak function allowing for user level mouse report modification
  112. *
  113. * Takes report_mouse_t struct allowing modification at user level then returns report_mouse_t.
  114. *
  115. * @param[in] mouse_report report_mouse_t
  116. * @return report_mouse_t
  117. */
  118. __attribute__((weak)) report_mouse_t pointing_device_task_user(report_mouse_t mouse_report) {
  119. return mouse_report;
  120. }
  121. /**
  122. * @brief Handles pointing device buttons
  123. *
  124. * Returns modified button bitmask using bool pressed and selected pointing_device_buttons_t button in uint8_t buttons bitmask.
  125. *
  126. * @param buttons[in] uint8_t bitmask
  127. * @param pressed[in] bool
  128. * @param button[in] pointing_device_buttons_t value
  129. * @return Modified uint8_t bitmask buttons
  130. */
  131. __attribute__((weak)) uint8_t pointing_device_handle_buttons(uint8_t buttons, bool pressed, pointing_device_buttons_t button) {
  132. if (pressed) {
  133. buttons |= 1 << (button);
  134. } else {
  135. buttons &= ~(1 << (button));
  136. }
  137. return buttons;
  138. }
  139. /**
  140. * @brief Initialises pointing device
  141. *
  142. * Initialises pointing device, perform driver init and optional keyboard/user level code.
  143. */
  144. __attribute__((weak)) void pointing_device_init(void) {
  145. #if defined(SPLIT_POINTING_ENABLE)
  146. if ((POINTING_DEVICE_THIS_SIDE))
  147. #endif
  148. {
  149. pointing_device_driver->init();
  150. #ifdef POINTING_DEVICE_MOTION_PIN
  151. # ifdef POINTING_DEVICE_MOTION_PIN_ACTIVE_LOW
  152. gpio_set_pin_input_high(POINTING_DEVICE_MOTION_PIN);
  153. # else
  154. gpio_set_pin_input(POINTING_DEVICE_MOTION_PIN);
  155. # endif
  156. #endif
  157. }
  158. pointing_device_init_kb();
  159. pointing_device_init_user();
  160. }
  161. /**
  162. * @brief Sends processed mouse report to host
  163. *
  164. * This sends the mouse report generated by pointing_device_task if changed since the last report. Once send zeros mouse report except buttons.
  165. *
  166. */
  167. __attribute__((weak)) bool pointing_device_send(void) {
  168. static report_mouse_t old_report = {};
  169. bool should_send_report = has_mouse_report_changed(&local_mouse_report, &old_report);
  170. if (should_send_report) {
  171. host_mouse_send(&local_mouse_report);
  172. }
  173. // send it and 0 it out except for buttons, so those stay until they are explicity over-ridden using update_pointing_device
  174. uint8_t buttons = local_mouse_report.buttons;
  175. memset(&local_mouse_report, 0, sizeof(local_mouse_report));
  176. local_mouse_report.buttons = buttons;
  177. memcpy(&old_report, &local_mouse_report, sizeof(local_mouse_report));
  178. return should_send_report || buttons;
  179. }
  180. /**
  181. * @brief Adjust mouse report by any optional common pointing configuration defines
  182. *
  183. * This applies rotation or inversion to the mouse report as selected by the pointing device common configuration defines.
  184. *
  185. * @param mouse_report[in] takes a report_mouse_t to be adjusted
  186. * @return report_mouse_t with adjusted values
  187. */
  188. report_mouse_t pointing_device_adjust_by_defines(report_mouse_t mouse_report) {
  189. // Support rotation of the sensor data
  190. #if defined(POINTING_DEVICE_ROTATION_90) || defined(POINTING_DEVICE_ROTATION_180) || defined(POINTING_DEVICE_ROTATION_270)
  191. mouse_xy_report_t x = mouse_report.x;
  192. mouse_xy_report_t y = mouse_report.y;
  193. # if defined(POINTING_DEVICE_ROTATION_90)
  194. mouse_report.x = y;
  195. mouse_report.y = -x;
  196. # elif defined(POINTING_DEVICE_ROTATION_180)
  197. mouse_report.x = -x;
  198. mouse_report.y = -y;
  199. # elif defined(POINTING_DEVICE_ROTATION_270)
  200. mouse_report.x = -y;
  201. mouse_report.y = x;
  202. # else
  203. # error "How the heck did you get here?!"
  204. # endif
  205. #endif
  206. // Support Inverting the X and Y Axises
  207. #if defined(POINTING_DEVICE_INVERT_X)
  208. mouse_report.x = -mouse_report.x;
  209. #endif
  210. #if defined(POINTING_DEVICE_INVERT_Y)
  211. mouse_report.y = -mouse_report.y;
  212. #endif
  213. return mouse_report;
  214. }
  215. /**
  216. * @brief Retrieves and processes pointing device data.
  217. *
  218. * This function is part of the keyboard loop and retrieves the mouse report from the pointing device driver.
  219. * It applies any optional configuration e.g. rotation or axis inversion and then initiates a send.
  220. *
  221. */
  222. __attribute__((weak)) bool pointing_device_task(void) {
  223. #if defined(SPLIT_POINTING_ENABLE)
  224. // Don't poll the target side pointing device.
  225. if (!is_keyboard_master()) {
  226. return false;
  227. };
  228. #endif
  229. #if (POINTING_DEVICE_TASK_THROTTLE_MS > 0)
  230. static uint32_t last_exec = 0;
  231. if (timer_elapsed32(last_exec) < POINTING_DEVICE_TASK_THROTTLE_MS) {
  232. return false;
  233. }
  234. last_exec = timer_read32();
  235. #endif
  236. // Gather report info
  237. #ifdef POINTING_DEVICE_MOTION_PIN
  238. # if defined(SPLIT_POINTING_ENABLE)
  239. # error POINTING_DEVICE_MOTION_PIN not supported when sharing the pointing device report between sides.
  240. # endif
  241. # ifdef POINTING_DEVICE_MOTION_PIN_ACTIVE_LOW
  242. if (!gpio_read_pin(POINTING_DEVICE_MOTION_PIN))
  243. # else
  244. if (gpio_read_pin(POINTING_DEVICE_MOTION_PIN))
  245. # endif
  246. {
  247. #endif
  248. #if defined(SPLIT_POINTING_ENABLE)
  249. # if defined(POINTING_DEVICE_COMBINED)
  250. static uint8_t old_buttons = 0;
  251. local_mouse_report.buttons = old_buttons;
  252. local_mouse_report = pointing_device_driver->get_report(local_mouse_report);
  253. old_buttons = local_mouse_report.buttons;
  254. # elif defined(POINTING_DEVICE_LEFT) || defined(POINTING_DEVICE_RIGHT)
  255. local_mouse_report = POINTING_DEVICE_THIS_SIDE ? pointing_device_driver->get_report(local_mouse_report) : shared_mouse_report;
  256. # else
  257. # error "You need to define the side(s) the pointing device is on. POINTING_DEVICE_COMBINED / POINTING_DEVICE_LEFT / POINTING_DEVICE_RIGHT"
  258. # endif
  259. #else
  260. local_mouse_report = pointing_device_driver->get_report(local_mouse_report);
  261. #endif // defined(SPLIT_POINTING_ENABLE)
  262. #ifdef POINTING_DEVICE_MOTION_PIN
  263. }
  264. #endif
  265. // allow kb to intercept and modify report
  266. #if defined(SPLIT_POINTING_ENABLE) && defined(POINTING_DEVICE_COMBINED)
  267. if (is_keyboard_left()) {
  268. local_mouse_report = pointing_device_adjust_by_defines(local_mouse_report);
  269. shared_mouse_report = pointing_device_adjust_by_defines_right(shared_mouse_report);
  270. } else {
  271. local_mouse_report = pointing_device_adjust_by_defines_right(local_mouse_report);
  272. shared_mouse_report = pointing_device_adjust_by_defines(shared_mouse_report);
  273. }
  274. local_mouse_report = is_keyboard_left() ? pointing_device_task_combined_kb(local_mouse_report, shared_mouse_report) : pointing_device_task_combined_kb(shared_mouse_report, local_mouse_report);
  275. #else
  276. local_mouse_report = pointing_device_adjust_by_defines(local_mouse_report);
  277. local_mouse_report = pointing_device_task_kb(local_mouse_report);
  278. #endif
  279. // automatic mouse layer function
  280. #ifdef POINTING_DEVICE_AUTO_MOUSE_ENABLE
  281. pointing_device_task_auto_mouse(local_mouse_report);
  282. #endif
  283. // combine with mouse report to ensure that the combined is sent correctly
  284. #ifdef MOUSEKEY_ENABLE
  285. report_mouse_t mousekey_report = mousekey_get_report();
  286. local_mouse_report.buttons = local_mouse_report.buttons | mousekey_report.buttons;
  287. #endif
  288. const bool send_report = pointing_device_send() || pointing_device_force_send;
  289. pointing_device_force_send = false;
  290. return send_report;
  291. }
  292. /**
  293. * @brief Gets current mouse report used by pointing device task
  294. *
  295. * @return report_mouse_t
  296. */
  297. report_mouse_t pointing_device_get_report(void) {
  298. return local_mouse_report;
  299. }
  300. /**
  301. * @brief Sets mouse report used be pointing device task
  302. *
  303. * @param[in] mouse_report
  304. */
  305. void pointing_device_set_report(report_mouse_t mouse_report) {
  306. pointing_device_force_send = has_mouse_report_changed(&local_mouse_report, &mouse_report);
  307. memcpy(&local_mouse_report, &mouse_report, sizeof(local_mouse_report));
  308. }
  309. /**
  310. * @brief Gets current pointing device CPI if supported
  311. *
  312. * Gets current cpi from pointing device driver if supported and returns it as uint16_t
  313. *
  314. * @return cpi value as uint16_t
  315. */
  316. uint16_t pointing_device_get_cpi(void) {
  317. #if defined(SPLIT_POINTING_ENABLE)
  318. return POINTING_DEVICE_THIS_SIDE ? pointing_device_driver->get_cpi() : shared_cpi;
  319. #else
  320. return pointing_device_driver->get_cpi();
  321. #endif
  322. }
  323. /**
  324. * @brief Set pointing device CPI if supported
  325. *
  326. * Takes a uint16_t value to set pointing device cpi if supported by driver.
  327. *
  328. * @param[in] cpi uint16_t value.
  329. */
  330. void pointing_device_set_cpi(uint16_t cpi) {
  331. #if defined(SPLIT_POINTING_ENABLE)
  332. if (POINTING_DEVICE_THIS_SIDE) {
  333. pointing_device_driver->set_cpi(cpi);
  334. } else {
  335. shared_cpi = cpi;
  336. }
  337. #else
  338. pointing_device_driver->set_cpi(cpi);
  339. #endif
  340. }
  341. #if defined(SPLIT_POINTING_ENABLE) && defined(POINTING_DEVICE_COMBINED)
  342. /**
  343. * @brief Set pointing device CPI if supported
  344. *
  345. * Takes a bool and uint16_t and allows setting cpi for a single side when using 2 pointing devices with a split keyboard.
  346. *
  347. * NOTE: Only available when using SPLIT_POINTING_ENABLE and POINTING_DEVICE_COMBINED
  348. *
  349. * @param[in] left true = left, false = right.
  350. * @param[in] cpi uint16_t value.
  351. */
  352. void pointing_device_set_cpi_on_side(bool left, uint16_t cpi) {
  353. bool local = (is_keyboard_left() == left);
  354. if (local) {
  355. pointing_device_driver->set_cpi(cpi);
  356. } else {
  357. shared_cpi = cpi;
  358. }
  359. }
  360. /**
  361. * @brief clamps int16_t to int8_t, or int32_t to int16_t
  362. *
  363. * @param[in] hv_clamp_range_t value
  364. * @return mouse_hv_report_t clamped value
  365. */
  366. static inline mouse_hv_report_t pointing_device_hv_clamp(hv_clamp_range_t value) {
  367. if (value < HV_REPORT_MIN) {
  368. return HV_REPORT_MIN;
  369. } else if (value > HV_REPORT_MAX) {
  370. return HV_REPORT_MAX;
  371. } else {
  372. return value;
  373. }
  374. }
  375. /**
  376. * @brief clamps int16_t to int8_t, or int32_t to int16_t
  377. *
  378. * @param[in] xy_clamp_range_t value
  379. * @return mouse_xy_report_t clamped value
  380. */
  381. static inline mouse_xy_report_t pointing_device_xy_clamp(xy_clamp_range_t value) {
  382. if (value < XY_REPORT_MIN) {
  383. return XY_REPORT_MIN;
  384. } else if (value > XY_REPORT_MAX) {
  385. return XY_REPORT_MAX;
  386. } else {
  387. return value;
  388. }
  389. }
  390. /**
  391. * @brief combines 2 mouse reports and returns 2
  392. *
  393. * Combines 2 report_mouse_t structs, clamping movement values to int8_t and ignores report_id then returns the resulting report_mouse_t struct.
  394. *
  395. * NOTE: Only available when using SPLIT_POINTING_ENABLE and POINTING_DEVICE_COMBINED
  396. *
  397. * @param[in] left_report left report_mouse_t
  398. * @param[in] right_report right report_mouse_t
  399. * @return combined report_mouse_t of left_report and right_report
  400. */
  401. report_mouse_t pointing_device_combine_reports(report_mouse_t left_report, report_mouse_t right_report) {
  402. left_report.x = pointing_device_xy_clamp((xy_clamp_range_t)left_report.x + right_report.x);
  403. left_report.y = pointing_device_xy_clamp((xy_clamp_range_t)left_report.y + right_report.y);
  404. left_report.h = pointing_device_hv_clamp((hv_clamp_range_t)left_report.h + right_report.h);
  405. left_report.v = pointing_device_hv_clamp((hv_clamp_range_t)left_report.v + right_report.v);
  406. left_report.buttons |= right_report.buttons;
  407. return left_report;
  408. }
  409. /**
  410. * @brief Adjust mouse report by any optional right pointing configuration defines
  411. *
  412. * This applies rotation or inversion to the mouse report as selected by the pointing device common configuration defines.
  413. *
  414. * NOTE: Only available when using SPLIT_POINTING_ENABLE and POINTING_DEVICE_COMBINED
  415. *
  416. * @param[in] mouse_report report_mouse_t to be adjusted
  417. * @return report_mouse_t with adjusted values
  418. */
  419. report_mouse_t pointing_device_adjust_by_defines_right(report_mouse_t mouse_report) {
  420. // Support rotation of the sensor data
  421. # if defined(POINTING_DEVICE_ROTATION_90_RIGHT) || defined(POINTING_DEVICE_ROTATION_180_RIGHT) || defined(POINTING_DEVICE_ROTATION_270_RIGHT)
  422. mouse_xy_report_t x = mouse_report.x;
  423. mouse_xy_report_t y = mouse_report.y;
  424. # if defined(POINTING_DEVICE_ROTATION_90_RIGHT)
  425. mouse_report.x = y;
  426. mouse_report.y = -x;
  427. # elif defined(POINTING_DEVICE_ROTATION_180_RIGHT)
  428. mouse_report.x = -x;
  429. mouse_report.y = -y;
  430. # elif defined(POINTING_DEVICE_ROTATION_270_RIGHT)
  431. mouse_report.x = -y;
  432. mouse_report.y = x;
  433. # else
  434. # error "How the heck did you get here?!"
  435. # endif
  436. # endif
  437. // Support Inverting the X and Y Axises
  438. # if defined(POINTING_DEVICE_INVERT_X_RIGHT)
  439. mouse_report.x = -mouse_report.x;
  440. # endif
  441. # if defined(POINTING_DEVICE_INVERT_Y_RIGHT)
  442. mouse_report.y = -mouse_report.y;
  443. # endif
  444. return mouse_report;
  445. }
  446. /**
  447. * @brief Weak function allowing for keyboard level mouse report modification
  448. *
  449. * Takes 2 report_mouse_t structs allowing individual modification of sides at keyboard level then returns pointing_device_task_combined_user.
  450. *
  451. * NOTE: Only available when using SPLIT_POINTING_ENABLE and POINTING_DEVICE_COMBINED
  452. *
  453. * @param[in] left_report report_mouse_t
  454. * @param[in] right_report report_mouse_t
  455. * @return pointing_device_task_combined_user(left_report, right_report) by default
  456. */
  457. __attribute__((weak)) report_mouse_t pointing_device_task_combined_kb(report_mouse_t left_report, report_mouse_t right_report) {
  458. return pointing_device_task_combined_user(left_report, right_report);
  459. }
  460. /**
  461. * @brief Weak function allowing for user level mouse report modification
  462. *
  463. * Takes 2 report_mouse_t structs allowing individual modification of sides at user level then returns pointing_device_combine_reports.
  464. *
  465. * NOTE: Only available when using SPLIT_POINTING_ENABLE and POINTING_DEVICE_COMBINED
  466. *
  467. * @param[in] left_report report_mouse_t
  468. * @param[in] right_report report_mouse_t
  469. * @return pointing_device_combine_reports(left_report, right_report) by default
  470. */
  471. __attribute__((weak)) report_mouse_t pointing_device_task_combined_user(report_mouse_t left_report, report_mouse_t right_report) {
  472. return pointing_device_combine_reports(left_report, right_report);
  473. }
  474. #endif
  475. __attribute__((weak)) void pointing_device_keycode_handler(uint16_t keycode, bool pressed) {
  476. if IS_MOUSEKEY_BUTTON (keycode) {
  477. local_mouse_report.buttons = pointing_device_handle_buttons(local_mouse_report.buttons, pressed, keycode - QK_MOUSE_BUTTON_1);
  478. pointing_device_send();
  479. }
  480. }