pointing_device.c 17 KB

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