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.

81 lines
2.4 KiB

3 years ago
3 years ago
3 years ago
  1. /* copyright Constantin Fürst 2021, licensed under GPLv3, a copy is provided in LICENSE */
  2. /* non-major portions of the code were taken from DWM (https://dwm.suckless.org) */
  3. /* inspired and named after keym by Chris Willcocks (https://github.com/cwkx/keym) */
  4. #include "typedefs.h"
  5. #include <stdint.h>
  6. #include <stdio.h>
  7. #include <X11/Xlib.h>
  8. #include <X11/extensions/XTest.h>
  9. extern keym_state state;
  10. /* internally-linked helpers */
  11. extern void set_x_update(uint32_t tv_usec);
  12. static inline int enum_to_mouse_button(unsigned int val) {
  13. switch (val) {
  14. case MB_Left: return 1;
  15. case MB_Right: return 3;
  16. case MB_Middle: return 2;
  17. case MB_Back: return 8;
  18. case MB_Forward: return 9;
  19. default: return 0;
  20. }
  21. }
  22. static inline uint32_t state_released(const uint32_t* a) {
  23. return (*a & KEY_RELEASE_MASK) >> 31;
  24. }
  25. static inline uint32_t state_demasker(const uint32_t* a) {
  26. return *a & (~KEY_RELEASE_MASK);
  27. }
  28. static inline int enum_to_scroll_button(uint32_t val) {
  29. switch(val) {
  30. case SD_Up: return 4;
  31. case SD_Down: return 5;
  32. default: return 0;
  33. }
  34. }
  35. /* actual key-actions following */
  36. void mouse_click(const uint32_t* a) {
  37. XTestFakeButtonEvent(state.display, enum_to_mouse_button(state_demasker(a)), !state_released(a), CurrentTime);
  38. }
  39. void mouse_move(const uint32_t* a) {
  40. const unsigned int released = state_released(a);
  41. switch (state_demasker(a)) {
  42. case MM_Up: state.y = released ? 0 : -1; break;
  43. case MM_Down: state.y = released ? 0 : 1; break;
  44. case MM_Left: state.x = released ? 0 : -1; break;
  45. case MM_Right: state.x = released ? 0 : 1; break;
  46. default: break;
  47. }
  48. }
  49. void mouse_scroll(const uint32_t* a) {
  50. if (!state_released(a)) {
  51. set_x_update(500 * speeds[state.speed]);
  52. XTestFakeButtonEvent(state.display, enum_to_scroll_button(state_demasker(a)), True, 1);
  53. XTestFakeButtonEvent(state.display, enum_to_scroll_button(state_demasker(a)), False, 1);
  54. set_x_update(speeds[state.speed]);
  55. }
  56. }
  57. void control_action(const uint32_t* a) {
  58. const unsigned int released = state_released(a);
  59. switch (state_demasker(a)) {
  60. case CA_QUIT_APPLICATION: state.quit = True;
  61. case CA_SPEED_HIGH: state.speed = released ? 1 : 0; break;
  62. case CA_SPEED_LOW: state.speed = released ? 1 : 2; break;
  63. default: break;
  64. }
  65. }