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.

446 lines
15 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 Keyboard level code pointing device initialisation
  66. *
  67. */
  68. __attribute__((weak)) void pointing_device_init_kb(void) {}
  69. /**
  70. * @brief User level code pointing device initialisation
  71. *
  72. */
  73. __attribute__((weak)) void pointing_device_init_user(void) {}
  74. /**
  75. * @brief Weak function allowing for keyboard level mouse report modification
  76. *
  77. * Takes report_mouse_t struct allowing modification at keyboard level then returns report_mouse_t.
  78. *
  79. * @param[in] mouse_report report_mouse_t
  80. * @return report_mouse_t
  81. */
  82. __attribute__((weak)) report_mouse_t pointing_device_task_kb(report_mouse_t mouse_report) {
  83. return pointing_device_task_user(mouse_report);
  84. }
  85. /**
  86. * @brief Weak function allowing for user level mouse report modification
  87. *
  88. * Takes report_mouse_t struct allowing modification at user level then returns report_mouse_t.
  89. *
  90. * @param[in] mouse_report report_mouse_t
  91. * @return report_mouse_t
  92. */
  93. __attribute__((weak)) report_mouse_t pointing_device_task_user(report_mouse_t mouse_report) {
  94. return mouse_report;
  95. }
  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) {
  254. return local_mouse_report;
  255. }
  256. /**
  257. * @brief Sets mouse report used be pointing device task
  258. *
  259. * @param[in] mouse_report
  260. */
  261. void pointing_device_set_report(report_mouse_t mouse_report) {
  262. local_mouse_report = mouse_report;
  263. }
  264. /**
  265. * @brief Gets current pointing device CPI if supported
  266. *
  267. * Gets current cpi from pointing device driver if supported and returns it as uint16_t
  268. *
  269. * @return cpi value as uint16_t
  270. */
  271. uint16_t pointing_device_get_cpi(void) {
  272. #if defined(SPLIT_POINTING_ENABLE)
  273. return POINTING_DEVICE_THIS_SIDE ? pointing_device_driver.get_cpi() : shared_cpi;
  274. #else
  275. return pointing_device_driver.get_cpi();
  276. #endif
  277. }
  278. /**
  279. * @brief Set pointing device CPI if supported
  280. *
  281. * Takes a uint16_t value to set pointing device cpi if supported by driver.
  282. *
  283. * @param[in] cpi uint16_t value.
  284. */
  285. void pointing_device_set_cpi(uint16_t cpi) {
  286. #if defined(SPLIT_POINTING_ENABLE)
  287. if (POINTING_DEVICE_THIS_SIDE) {
  288. pointing_device_driver.set_cpi(cpi);
  289. } else {
  290. shared_cpi = cpi;
  291. }
  292. #else
  293. pointing_device_driver.set_cpi(cpi);
  294. #endif
  295. }
  296. #if defined(SPLIT_POINTING_ENABLE) && defined(POINTING_DEVICE_COMBINED)
  297. /**
  298. * @brief Set pointing device CPI if supported
  299. *
  300. * Takes a bool and uint16_t and allows setting cpi for a single side when using 2 pointing devices with a split keyboard.
  301. *
  302. * NOTE: Only available when using SPLIT_POINTING_ENABLE and POINTING_DEVICE_COMBINED
  303. *
  304. * @param[in] left true = left, false = right.
  305. * @param[in] cpi uint16_t value.
  306. */
  307. void pointing_device_set_cpi_on_side(bool left, uint16_t cpi) {
  308. bool local = (is_keyboard_left() & left) ? true : false;
  309. if (local) {
  310. pointing_device_driver.set_cpi(cpi);
  311. } else {
  312. shared_cpi = cpi;
  313. }
  314. }
  315. /**
  316. * @brief clamps int16_t to int8_t
  317. *
  318. * @param[in] int16_t value
  319. * @return int8_t clamped value
  320. */
  321. static inline int8_t pointing_device_movement_clamp(int16_t value) {
  322. if (value < INT8_MIN) {
  323. return INT8_MIN;
  324. } else if (value > INT8_MAX) {
  325. return INT8_MAX;
  326. } else {
  327. return value;
  328. }
  329. }
  330. /**
  331. * @brief combines 2 mouse reports and returns 2
  332. *
  333. * Combines 2 report_mouse_t structs, clamping movement values to int8_t and ignores report_id then returns the resulting report_mouse_t struct.
  334. *
  335. * NOTE: Only available when using SPLIT_POINTING_ENABLE and POINTING_DEVICE_COMBINED
  336. *
  337. * @param[in] left_report left report_mouse_t
  338. * @param[in] right_report right report_mouse_t
  339. * @return combined report_mouse_t of left_report and right_report
  340. */
  341. report_mouse_t pointing_device_combine_reports(report_mouse_t left_report, report_mouse_t right_report) {
  342. left_report.x = pointing_device_movement_clamp((int16_t)left_report.x + right_report.x);
  343. left_report.y = pointing_device_movement_clamp((int16_t)left_report.y + right_report.y);
  344. left_report.h = pointing_device_movement_clamp((int16_t)left_report.h + right_report.h);
  345. left_report.v = pointing_device_movement_clamp((int16_t)left_report.v + right_report.v);
  346. left_report.buttons |= right_report.buttons;
  347. return left_report;
  348. }
  349. /**
  350. * @brief Adjust mouse report by any optional right pointing configuration defines
  351. *
  352. * This applies rotation or inversion to the mouse report as selected by the pointing device common configuration defines.
  353. *
  354. * NOTE: Only available when using SPLIT_POINTING_ENABLE and POINTING_DEVICE_COMBINED
  355. *
  356. * @param[in] mouse_report report_mouse_t to be adjusted
  357. * @return report_mouse_t with adjusted values
  358. */
  359. report_mouse_t pointing_device_adjust_by_defines_right(report_mouse_t mouse_report) {
  360. // Support rotation of the sensor data
  361. # if defined(POINTING_DEVICE_ROTATION_90_RIGHT) || defined(POINTING_DEVICE_ROTATION_RIGHT) || defined(POINTING_DEVICE_ROTATION_RIGHT)
  362. int8_t x = mouse_report.x, y = mouse_report.y;
  363. # if defined(POINTING_DEVICE_ROTATION_90_RIGHT)
  364. mouse_report.x = y;
  365. mouse_report.y = -x;
  366. # elif defined(POINTING_DEVICE_ROTATION_180_RIGHT)
  367. mouse_report.x = -x;
  368. mouse_report.y = -y;
  369. # elif defined(POINTING_DEVICE_ROTATION_270_RIGHT)
  370. mouse_report.x = -y;
  371. mouse_report.y = x;
  372. # else
  373. # error "How the heck did you get here?!"
  374. # endif
  375. # endif
  376. // Support Inverting the X and Y Axises
  377. # if defined(POINTING_DEVICE_INVERT_X_RIGHT)
  378. mouse_report.x = -mouse_report.x;
  379. # endif
  380. # if defined(POINTING_DEVICE_INVERT_Y_RIGHT)
  381. mouse_report.y = -mouse_report.y;
  382. # endif
  383. return mouse_report;
  384. }
  385. /**
  386. * @brief Weak function allowing for keyboard level mouse report modification
  387. *
  388. * Takes 2 report_mouse_t structs allowing individual modification of sides at keyboard level then returns pointing_device_task_combined_user.
  389. *
  390. * NOTE: Only available when using SPLIT_POINTING_ENABLE and POINTING_DEVICE_COMBINED
  391. *
  392. * @param[in] left_report report_mouse_t
  393. * @param[in] right_report report_mouse_t
  394. * @return pointing_device_task_combined_user(left_report, right_report) by default
  395. */
  396. __attribute__((weak)) report_mouse_t pointing_device_task_combined_kb(report_mouse_t left_report, report_mouse_t right_report) {
  397. return pointing_device_task_combined_user(left_report, right_report);
  398. }
  399. /**
  400. * @brief Weak function allowing for user level mouse report modification
  401. *
  402. * Takes 2 report_mouse_t structs allowing individual modification of sides at user level then returns pointing_device_combine_reports.
  403. *
  404. * NOTE: Only available when using SPLIT_POINTING_ENABLE and POINTING_DEVICE_COMBINED
  405. *
  406. * @param[in] left_report report_mouse_t
  407. * @param[in] right_report report_mouse_t
  408. * @return pointing_device_combine_reports(left_report, right_report) by default
  409. */
  410. __attribute__((weak)) report_mouse_t pointing_device_task_combined_user(report_mouse_t left_report, report_mouse_t right_report) {
  411. return pointing_device_combine_reports(left_report, right_report);
  412. }
  413. #endif