Advertisement
Guest User

Untitled

a guest
Nov 30th, 2013
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.14 KB | None | 0 0
  1. /* size of touchpad area, it will be evenly divided for the buttons */
  2. #define TOUCHPAD_WIDTH  3010
  3. #define TOUCHPAD_HEIGHT 1975
  4.  
  5. #define DEADZONE_XMUL 50 /* x deadzone multiplier */
  6. #define DEADZONE_YMUL 40 /* y deadzone multiplier */
  7.  
  8. /* we emulate a 3x3 grid, this gives the button mapping
  9.  * WARNING: the y-axis is reversed ! */
  10. int button_mapping[3][3] =
  11. {
  12.     {BUTTON_BOTTOMLEFT, BUTTON_DOWN, BUTTON_BOTTOMRIGHT},
  13.     {BUTTON_LEFT, BUTTON_SELECT, BUTTON_RIGHT},
  14.     {BUTTON_TOPLEFT, BUTTON_UP, BUTTON_TOPRIGHT},
  15. };
  16.  
  17. int x_deadzone, y_deadzone;
  18.  
  19. /* Ignore deadzone function */
  20. static int find_button_no_deadzone(int x, int y)
  21. {
  22.     /* compute grid coordinate */
  23.     int gx = x * 3 / TOUCHPAD_WIDTH;
  24.     int gy = y * 3 / TOUCHPAD_HEIGHT;
  25.     if(gx < 0 || gx >= 3 || gy < 0 || gy >= 3)
  26.         return 0; /* something went wrong, these coordinates are useless */
  27.     return button_mapping[gx][gy];
  28. }
  29.  
  30. /* Ignore deadzone function */
  31. static int find_button(int x, int y)
  32. {
  33.     /* compute grid coordinate */
  34.     int gx = x * 3 / TOUCHPAD_WIDTH;
  35.     int gy = y * 3 / TOUCHPAD_HEIGHT;
  36.     /* find button ignoring deadzones */
  37.     int btn = find_button_no_deadzone(x, y);
  38.     if(btn == 0)
  39.         return 0;
  40.     /* to see if we are in a deadzone, we try to shift the coordinate
  41.      * and see if we get the same button, however we do not want the
  42.      * deadzone to apply on the borders, only between buttons ! */
  43.     /* right deadzone: only if not in the last column */
  44.     if(gx != 2 && find_button_no_deadzone(x + x_deadzone, y) != btn)
  45.         return 0;
  46.     /* left deadzone: only if not in the first column */
  47.     if(gx != 0 && find_button_no_deadzone(x - x_deadzone, y) != btn)
  48.         return 0;
  49.     /* top deadzone: only if not in the first row */
  50.     if(gy != 2 && find_button_no_deadzone(x, y + y_deadzone) != btn)
  51.         return 0;
  52.     /* bottom deadzone: only if not in the last row */
  53.     if(gy != 0 && find_button_no_deadzone(x, y - y_deadzone) != btn)
  54.         return 0;
  55.     return btn;
  56. }
  57.  
  58. void touchpad_set_deadzone(int deadzone)
  59. {
  60.     x_deadzone = deadzone * DEADZONE_XMUL;
  61.     y_deadzone = deadzone * DEADZONE_YMUL;
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement