You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

457 lines
16 KiB

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