pointing_device.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  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. #ifdef MOUSEKEY_ENABLE
  22. # include "mousekey.h"
  23. #endif
  24. #if (defined(POINTING_DEVICE_ROTATION_90) + defined(POINTING_DEVICE_ROTATION_180) + defined(POINTING_DEVICE_ROTATION_270)) > 1
  25. # error More than one rotation selected. This is not supported.
  26. #endif
  27. #if defined(POINTING_DEVICE_LEFT) || defined(POINTING_DEVICE_RIGHT) || defined(POINTING_DEVICE_COMBINED)
  28. # ifndef SPLIT_POINTING_ENABLE
  29. # error "Using POINTING_DEVICE_LEFT or POINTING_DEVICE_RIGHT or POINTING_DEVICE_COMBINED, then SPLIT_POINTING_ENABLE is required but has not been defined"
  30. # endif
  31. #endif
  32. #if defined(SPLIT_POINTING_ENABLE)
  33. # include "transactions.h"
  34. # include "keyboard.h"
  35. report_mouse_t shared_mouse_report = {};
  36. uint16_t shared_cpi = 0;
  37. /**
  38. * @brief Sets the shared mouse report used be pointing device task
  39. *
  40. * NOTE : Only available when using SPLIT_POINTING_ENABLE
  41. *
  42. * @param[in] new_mouse_report report_mouse_t
  43. */
  44. void pointing_device_set_shared_report(report_mouse_t new_mouse_report) {
  45. shared_mouse_report = new_mouse_report;
  46. }
  47. /**
  48. * @brief Gets current pointing device CPI if supported
  49. *
  50. * Gets current cpi of the shared report and returns it as uint16_t
  51. *
  52. * NOTE : Only available when using SPLIT_POINTING_ENABLE
  53. *
  54. * @return cpi value as uint16_t
  55. */
  56. uint16_t pointing_device_get_shared_cpi(void) {
  57. return shared_cpi;
  58. }
  59. # if defined(POINTING_DEVICE_LEFT)
  60. # define POINTING_DEVICE_THIS_SIDE is_keyboard_left()
  61. # elif defined(POINTING_DEVICE_RIGHT)
  62. # define POINTING_DEVICE_THIS_SIDE !is_keyboard_left()
  63. # elif defined(POINTING_DEVICE_COMBINED)
  64. # define POINTING_DEVICE_THIS_SIDE true
  65. # endif
  66. #endif // defined(SPLIT_POINTING_ENABLE)
  67. static report_mouse_t local_mouse_report = {};
  68. extern const pointing_device_driver_t pointing_device_driver;
  69. /**
  70. * @brief Keyboard level code pointing device initialisation
  71. *
  72. */
  73. __attribute__((weak)) void pointing_device_init_kb(void) {}
  74. /**
  75. * @brief User level code pointing device initialisation
  76. *
  77. */
  78. __attribute__((weak)) void pointing_device_init_user(void) {}
  79. /**
  80. * @brief Weak function allowing for keyboard level mouse report modification
  81. *
  82. * Takes report_mouse_t struct allowing modification at keyboard level then returns report_mouse_t.
  83. *
  84. * @param[in] mouse_report report_mouse_t
  85. * @return report_mouse_t
  86. */
  87. __attribute__((weak)) report_mouse_t pointing_device_task_kb(report_mouse_t mouse_report) {
  88. return pointing_device_task_user(mouse_report);
  89. }
  90. /**
  91. * @brief Weak function allowing for user level mouse report modification
  92. *
  93. * Takes report_mouse_t struct allowing modification at user level then returns report_mouse_t.
  94. *
  95. * @param[in] mouse_report report_mouse_t
  96. * @return report_mouse_t
  97. */
  98. __attribute__((weak)) report_mouse_t pointing_device_task_user(report_mouse_t mouse_report) {
  99. return mouse_report;
  100. }
  101. /**
  102. * @brief Handles pointing device buttons
  103. *
  104. * Returns modified button bitmask using bool pressed and selected pointing_device_buttons_t button in uint8_t buttons bitmask.
  105. *
  106. * @param buttons[in] uint8_t bitmask
  107. * @param pressed[in] bool
  108. * @param button[in] pointing_device_buttons_t value
  109. * @return Modified uint8_t bitmask buttons
  110. */
  111. __attribute__((weak)) uint8_t pointing_device_handle_buttons(uint8_t buttons, bool pressed, pointing_device_buttons_t button) {
  112. if (pressed) {
  113. buttons |= 1 << (button);
  114. } else {
  115. buttons &= ~(1 << (button));
  116. }
  117. return buttons;
  118. }
  119. /**
  120. * @brief Initialises pointing device
  121. *
  122. * Initialises pointing device, perform driver init and optional keyboard/user level code.
  123. */
  124. __attribute__((weak)) void pointing_device_init(void) {
  125. #if defined(SPLIT_POINTING_ENABLE)
  126. if (!(POINTING_DEVICE_THIS_SIDE)) {
  127. return;
  128. }
  129. #endif
  130. pointing_device_driver.init();
  131. #ifdef POINTING_DEVICE_MOTION_PIN
  132. setPinInputHigh(POINTING_DEVICE_MOTION_PIN);
  133. #endif
  134. pointing_device_init_kb();
  135. pointing_device_init_user();
  136. }
  137. /**
  138. * @brief Sends processed mouse report to host
  139. *
  140. * This sends the mouse report generated by pointing_device_task if changed since the last report. Once send zeros mouse report except buttons.
  141. *
  142. */
  143. __attribute__((weak)) void pointing_device_send(void) {
  144. static report_mouse_t old_report = {};
  145. // If you need to do other things, like debugging, this is the place to do it.
  146. if (has_mouse_report_changed(&local_mouse_report, &old_report)) {
  147. host_mouse_send(&local_mouse_report);
  148. }
  149. // send it and 0 it out except for buttons, so those stay until they are explicity over-ridden using update_pointing_device
  150. local_mouse_report.x = 0;
  151. local_mouse_report.y = 0;
  152. local_mouse_report.v = 0;
  153. local_mouse_report.h = 0;
  154. memcpy(&old_report, &local_mouse_report, sizeof(local_mouse_report));
  155. }
  156. /**
  157. * @brief Adjust mouse report by any optional common pointing configuration defines
  158. *
  159. * This applies rotation or inversion to the mouse report as selected by the pointing device common configuration defines.
  160. *
  161. * @param mouse_report[in] takes a report_mouse_t to be adjusted
  162. * @return report_mouse_t with adjusted values
  163. */
  164. report_mouse_t pointing_device_adjust_by_defines(report_mouse_t mouse_report) {
  165. // Support rotation of the sensor data
  166. #if defined(POINTING_DEVICE_ROTATION_90) || defined(POINTING_DEVICE_ROTATION_180) || defined(POINTING_DEVICE_ROTATION_270)
  167. mouse_xy_report_t x = mouse_report.x;
  168. mouse_xy_report_t y = mouse_report.y;
  169. # if defined(POINTING_DEVICE_ROTATION_90)
  170. mouse_report.x = y;
  171. mouse_report.y = -x;
  172. # elif defined(POINTING_DEVICE_ROTATION_180)
  173. mouse_report.x = -x;
  174. mouse_report.y = -y;
  175. # elif defined(POINTING_DEVICE_ROTATION_270)
  176. mouse_report.x = -y;
  177. mouse_report.y = x;
  178. # else
  179. # error "How the heck did you get here?!"
  180. # endif
  181. #endif
  182. // Support Inverting the X and Y Axises
  183. #if defined(POINTING_DEVICE_INVERT_X)
  184. mouse_report.x = -mouse_report.x;
  185. #endif
  186. #if defined(POINTING_DEVICE_INVERT_Y)
  187. mouse_report.y = -mouse_report.y;
  188. #endif
  189. return mouse_report;
  190. }
  191. /**
  192. * @brief Retrieves and processes pointing device data.
  193. *
  194. * This function is part of the keyboard loop and retrieves the mouse report from the pointing device driver.
  195. * It applies any optional configuration e.g. rotation or axis inversion and then initiates a send.
  196. *
  197. */
  198. __attribute__((weak)) void pointing_device_task(void) {
  199. #if defined(SPLIT_POINTING_ENABLE)
  200. // Don't poll the target side pointing device.
  201. if (!is_keyboard_master()) {
  202. return;
  203. };
  204. #endif
  205. #if (POINTING_DEVICE_TASK_THROTTLE_MS > 0)
  206. static uint32_t last_exec = 0;
  207. if (timer_elapsed32(last_exec) < POINTING_DEVICE_TASK_THROTTLE_MS) {
  208. return;
  209. }
  210. last_exec = timer_read32();
  211. #endif
  212. // Gather report info
  213. #ifdef POINTING_DEVICE_MOTION_PIN
  214. # if defined(SPLIT_POINTING_ENABLE)
  215. # error POINTING_DEVICE_MOTION_PIN not supported when sharing the pointing device report between sides.
  216. # endif
  217. if (!readPin(POINTING_DEVICE_MOTION_PIN))
  218. #endif
  219. #if defined(SPLIT_POINTING_ENABLE)
  220. # if defined(POINTING_DEVICE_COMBINED)
  221. static uint8_t old_buttons = 0;
  222. local_mouse_report.buttons = old_buttons;
  223. local_mouse_report = pointing_device_driver.get_report(local_mouse_report);
  224. old_buttons = local_mouse_report.buttons;
  225. # elif defined(POINTING_DEVICE_LEFT) || defined(POINTING_DEVICE_RIGHT)
  226. local_mouse_report = POINTING_DEVICE_THIS_SIDE ? pointing_device_driver.get_report(local_mouse_report) : shared_mouse_report;
  227. # else
  228. # error "You need to define the side(s) the pointing device is on. POINTING_DEVICE_COMBINED / POINTING_DEVICE_LEFT / POINTING_DEVICE_RIGHT"
  229. # endif
  230. #else
  231. local_mouse_report = pointing_device_driver.get_report(local_mouse_report);
  232. #endif // defined(SPLIT_POINTING_ENABLE)
  233. // allow kb to intercept and modify report
  234. #if defined(SPLIT_POINTING_ENABLE) && defined(POINTING_DEVICE_COMBINED)
  235. if (is_keyboard_left()) {
  236. local_mouse_report = pointing_device_adjust_by_defines(local_mouse_report);
  237. shared_mouse_report = pointing_device_adjust_by_defines_right(shared_mouse_report);
  238. } else {
  239. local_mouse_report = pointing_device_adjust_by_defines_right(local_mouse_report);
  240. shared_mouse_report = pointing_device_adjust_by_defines(shared_mouse_report);
  241. }
  242. 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);
  243. #else
  244. local_mouse_report = pointing_device_adjust_by_defines(local_mouse_report);
  245. local_mouse_report = pointing_device_task_kb(local_mouse_report);
  246. #endif
  247. // combine with mouse report to ensure that the combined is sent correctly
  248. #ifdef MOUSEKEY_ENABLE
  249. report_mouse_t mousekey_report = mousekey_get_report();
  250. local_mouse_report.buttons = local_mouse_report.buttons | mousekey_report.buttons;
  251. #endif
  252. pointing_device_send();
  253. }
  254. /**
  255. * @brief Gets current mouse report used by pointing device task
  256. *
  257. * @return report_mouse_t
  258. */
  259. report_mouse_t pointing_device_get_report(void) {
  260. return local_mouse_report;
  261. }
  262. /**
  263. * @brief Sets mouse report used be pointing device task
  264. *
  265. * @param[in] mouse_report
  266. */
  267. void pointing_device_set_report(report_mouse_t mouse_report) {
  268. local_mouse_report = mouse_report;
  269. }
  270. /**
  271. * @brief Gets current pointing device CPI if supported
  272. *
  273. * Gets current cpi from pointing device driver if supported and returns it as uint16_t
  274. *
  275. * @return cpi value as uint16_t
  276. */
  277. uint16_t pointing_device_get_cpi(void) {
  278. #if defined(SPLIT_POINTING_ENABLE)
  279. return POINTING_DEVICE_THIS_SIDE ? pointing_device_driver.get_cpi() : shared_cpi;
  280. #else
  281. return pointing_device_driver.get_cpi();
  282. #endif
  283. }
  284. /**
  285. * @brief Set pointing device CPI if supported
  286. *
  287. * Takes a uint16_t value to set pointing device cpi if supported by driver.
  288. *
  289. * @param[in] cpi uint16_t value.
  290. */
  291. void pointing_device_set_cpi(uint16_t cpi) {
  292. #if defined(SPLIT_POINTING_ENABLE)
  293. if (POINTING_DEVICE_THIS_SIDE) {
  294. pointing_device_driver.set_cpi(cpi);
  295. } else {
  296. shared_cpi = cpi;
  297. }
  298. #else
  299. pointing_device_driver.set_cpi(cpi);
  300. #endif
  301. }
  302. #if defined(SPLIT_POINTING_ENABLE) && defined(POINTING_DEVICE_COMBINED)
  303. /**
  304. * @brief Set pointing device CPI if supported
  305. *
  306. * Takes a bool and uint16_t and allows setting cpi for a single side when using 2 pointing devices with a split keyboard.
  307. *
  308. * NOTE: Only available when using SPLIT_POINTING_ENABLE and POINTING_DEVICE_COMBINED
  309. *
  310. * @param[in] left true = left, false = right.
  311. * @param[in] cpi uint16_t value.
  312. */
  313. void pointing_device_set_cpi_on_side(bool left, uint16_t cpi) {
  314. bool local = (is_keyboard_left() & left) ? true : false;
  315. if (local) {
  316. pointing_device_driver.set_cpi(cpi);
  317. } else {
  318. shared_cpi = cpi;
  319. }
  320. }
  321. /**
  322. * @brief clamps int16_t to int8_t
  323. *
  324. * @param[in] int16_t value
  325. * @return int8_t clamped value
  326. */
  327. static inline int8_t pointing_device_hv_clamp(int16_t value) {
  328. if (value < INT8_MIN) {
  329. return INT8_MIN;
  330. } else if (value > INT8_MAX) {
  331. return INT8_MAX;
  332. } else {
  333. return value;
  334. }
  335. }
  336. /**
  337. * @brief clamps int16_t to int8_t
  338. *
  339. * @param[in] clamp_range_t value
  340. * @return mouse_xy_report_t clamped value
  341. */
  342. static inline mouse_xy_report_t pointing_device_xy_clamp(clamp_range_t value) {
  343. if (value < XY_REPORT_MIN) {
  344. return XY_REPORT_MIN;
  345. } else if (value > XY_REPORT_MAX) {
  346. return XY_REPORT_MAX;
  347. } else {
  348. return value;
  349. }
  350. }
  351. /**
  352. * @brief combines 2 mouse reports and returns 2
  353. *
  354. * Combines 2 report_mouse_t structs, clamping movement values to int8_t and ignores report_id then returns the resulting report_mouse_t struct.
  355. *
  356. * NOTE: Only available when using SPLIT_POINTING_ENABLE and POINTING_DEVICE_COMBINED
  357. *
  358. * @param[in] left_report left report_mouse_t
  359. * @param[in] right_report right report_mouse_t
  360. * @return combined report_mouse_t of left_report and right_report
  361. */
  362. report_mouse_t pointing_device_combine_reports(report_mouse_t left_report, report_mouse_t right_report) {
  363. left_report.x = pointing_device_xy_clamp((clamp_range_t)left_report.x + right_report.x);
  364. left_report.y = pointing_device_xy_clamp((clamp_range_t)left_report.y + right_report.y);
  365. left_report.h = pointing_device_hv_clamp((int16_t)left_report.h + right_report.h);
  366. left_report.v = pointing_device_hv_clamp((int16_t)left_report.v + right_report.v);
  367. left_report.buttons |= right_report.buttons;
  368. return left_report;
  369. }
  370. /**
  371. * @brief Adjust mouse report by any optional right pointing configuration defines
  372. *
  373. * This applies rotation or inversion to the mouse report as selected by the pointing device common configuration defines.
  374. *
  375. * NOTE: Only available when using SPLIT_POINTING_ENABLE and POINTING_DEVICE_COMBINED
  376. *
  377. * @param[in] mouse_report report_mouse_t to be adjusted
  378. * @return report_mouse_t with adjusted values
  379. */
  380. report_mouse_t pointing_device_adjust_by_defines_right(report_mouse_t mouse_report) {
  381. // Support rotation of the sensor data
  382. # if defined(POINTING_DEVICE_ROTATION_90_RIGHT) || defined(POINTING_DEVICE_ROTATION_RIGHT) || defined(POINTING_DEVICE_ROTATION_RIGHT)
  383. mouse_xy_report_t x = mouse_report.x;
  384. mouse_xy_report_t y = mouse_report.y;
  385. # if defined(POINTING_DEVICE_ROTATION_90_RIGHT)
  386. mouse_report.x = y;
  387. mouse_report.y = -x;
  388. # elif defined(POINTING_DEVICE_ROTATION_180_RIGHT)
  389. mouse_report.x = -x;
  390. mouse_report.y = -y;
  391. # elif defined(POINTING_DEVICE_ROTATION_270_RIGHT)
  392. mouse_report.x = -y;
  393. mouse_report.y = x;
  394. # else
  395. # error "How the heck did you get here?!"
  396. # endif
  397. # endif
  398. // Support Inverting the X and Y Axises
  399. # if defined(POINTING_DEVICE_INVERT_X_RIGHT)
  400. mouse_report.x = -mouse_report.x;
  401. # endif
  402. # if defined(POINTING_DEVICE_INVERT_Y_RIGHT)
  403. mouse_report.y = -mouse_report.y;
  404. # endif
  405. return mouse_report;
  406. }
  407. /**
  408. * @brief Weak function allowing for keyboard level mouse report modification
  409. *
  410. * Takes 2 report_mouse_t structs allowing individual modification of sides at keyboard level then returns pointing_device_task_combined_user.
  411. *
  412. * NOTE: Only available when using SPLIT_POINTING_ENABLE and POINTING_DEVICE_COMBINED
  413. *
  414. * @param[in] left_report report_mouse_t
  415. * @param[in] right_report report_mouse_t
  416. * @return pointing_device_task_combined_user(left_report, right_report) by default
  417. */
  418. __attribute__((weak)) report_mouse_t pointing_device_task_combined_kb(report_mouse_t left_report, report_mouse_t right_report) {
  419. return pointing_device_task_combined_user(left_report, right_report);
  420. }
  421. /**
  422. * @brief Weak function allowing for user level mouse report modification
  423. *
  424. * Takes 2 report_mouse_t structs allowing individual modification of sides at user level then returns pointing_device_combine_reports.
  425. *
  426. * NOTE: Only available when using SPLIT_POINTING_ENABLE and POINTING_DEVICE_COMBINED
  427. *
  428. * @param[in] left_report report_mouse_t
  429. * @param[in] right_report report_mouse_t
  430. * @return pointing_device_combine_reports(left_report, right_report) by default
  431. */
  432. __attribute__((weak)) report_mouse_t pointing_device_task_combined_user(report_mouse_t left_report, report_mouse_t right_report) {
  433. return pointing_device_combine_reports(left_report, right_report);
  434. }
  435. #endif