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.

73 lines
2.1 KiB

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. static inline int enum_to_mouse_button(unsigned int val) {
  12. switch (val) {
  13. case MB_Left: return 1;
  14. case MB_Right: return 3;
  15. case MB_Middle: return 2;
  16. case MB_Back: return 8;
  17. case MB_Forward: return 9;
  18. default: return 0;
  19. }
  20. }
  21. static inline uint32_t state_released(const uint32_t* a) {
  22. return (*a & KEY_RELEASE_MASK) >> 31;
  23. }
  24. static inline uint32_t state_demasker(const uint32_t* a) {
  25. return *a & (~KEY_RELEASE_MASK);
  26. }
  27. static inline int enum_to_scroll_button(uint32_t val) {
  28. switch(val) {
  29. case SD_Up: return 4;
  30. case SD_Down: return 5;
  31. default: return 0;
  32. }
  33. }
  34. /* actual key-actions following */
  35. void mouse_click(const uint32_t* a) {
  36. XTestFakeButtonEvent(state.display, enum_to_mouse_button(state_demasker(a)), !state_released(a), CurrentTime);
  37. }
  38. void mouse_move(const uint32_t* a) {
  39. const unsigned int released = state_released(a);
  40. switch (state_demasker(a)) {
  41. case MM_Up: state.y = released ? 0 : -1; break;
  42. case MM_Down: state.y = released ? 0 : 1; break;
  43. case MM_Left: state.x = released ? 0 : -1; break;
  44. case MM_Right: state.x = released ? 0 : 1; break;
  45. default: break;
  46. }
  47. }
  48. void mouse_scroll(const uint32_t* a) {
  49. XTestFakeButtonEvent(state.display, enum_to_scroll_button(state_demasker(a)), state_released(a), 1);
  50. }
  51. void control_action(const uint32_t* a) {
  52. const unsigned int released = state_released(a);
  53. switch (state_demasker(a)) {
  54. case CA_QUIT_APPLICATION: state.quit = True;
  55. case CA_SPEED_HIGH: state.speed = released ? 1 : 0; break;
  56. case CA_SPEED_LOW: state.speed = released ? 1 : 2; break;
  57. default: break;
  58. }
  59. }