pointing_device.c 16 KB

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