src/events/SDL_mouse.c
changeset 12688 cc45bcb16ef2
parent 12687 712d84dd14c9
child 12692 69f24f4ef99e
     1.1 --- a/src/events/SDL_mouse.c	Thu Apr 04 15:19:00 2019 +0200
     1.2 +++ b/src/events/SDL_mouse.c	Thu Apr 04 16:51:50 2019 +0200
     1.3 @@ -37,6 +37,9 @@
     1.4  /* The mouse state */
     1.5  static SDL_Mouse SDL_mouse;
     1.6  
     1.7 +/* for mapping mouse events to touch */
     1.8 +static SDL_bool track_mouse_down = SDL_FALSE;
     1.9 +
    1.10  static int
    1.11  SDL_PrivateSendMouseMotion(SDL_Window * window, SDL_MouseID mouseID, int relative, int x, int y);
    1.12  
    1.13 @@ -104,6 +107,21 @@
    1.14      }
    1.15  }
    1.16  
    1.17 +static void SDLCALL
    1.18 +SDL_MouseTouchEventsChanged(void *userdata, const char *name, const char *oldValue, const char *hint)
    1.19 +{
    1.20 +    SDL_Mouse *mouse = (SDL_Mouse *)userdata;
    1.21 +
    1.22 +    if (hint && (*hint == '1' || SDL_strcasecmp(hint, "true") == 0)) {
    1.23 +
    1.24 +        SDL_AddTouch(SDL_MOUSE_TOUCHID, SDL_TOUCH_DEVICE_DIRECT, "mouse_input");
    1.25 +
    1.26 +        mouse->mouse_touch_events = SDL_TRUE;
    1.27 +    } else {
    1.28 +        mouse->mouse_touch_events = SDL_FALSE;
    1.29 +    }
    1.30 +}
    1.31 +
    1.32  /* Public functions */
    1.33  int
    1.34  SDL_MouseInit(void)
    1.35 @@ -127,6 +145,9 @@
    1.36      SDL_AddHintCallback(SDL_HINT_TOUCH_MOUSE_EVENTS,
    1.37                          SDL_TouchMouseEventsChanged, mouse);
    1.38  
    1.39 +    SDL_AddHintCallback(SDL_HINT_MOUSE_TOUCH_EVENTS,
    1.40 +                        SDL_MouseTouchEventsChanged, mouse);
    1.41 +
    1.42      mouse->cursor_shown = SDL_TRUE;
    1.43  
    1.44      return (0);
    1.45 @@ -298,6 +319,17 @@
    1.46      int xrel;
    1.47      int yrel;
    1.48  
    1.49 +    /* SDL_HINT_MOUSE_TOUCH_EVENTS: controlling whether mouse events should generate synthetic touch events */
    1.50 +    if (mouse->mouse_touch_events) {
    1.51 +        if (mouseID != SDL_TOUCH_MOUSEID && !relative && track_mouse_down) {
    1.52 +            if (window) {
    1.53 +                float fx = (float)x / (float)window->w;
    1.54 +                float fy = (float)y / (float)window->h;
    1.55 +                SDL_SendTouchMotion(SDL_MOUSE_TOUCHID, 0, fx, fy, 1.0f);
    1.56 +            }
    1.57 +        }
    1.58 +    }
    1.59 +
    1.60      if (mouseID != SDL_TOUCH_MOUSEID && mouse->relative_mode_warp) {
    1.61          int center_x = 0, center_y = 0;
    1.62          SDL_GetWindowSize(window, &center_x, &center_y);
    1.63 @@ -443,6 +475,22 @@
    1.64      Uint32 type;
    1.65      Uint32 buttonstate = mouse->buttonstate;
    1.66  
    1.67 +    /* SDL_HINT_MOUSE_TOUCH_EVENTS: controlling whether mouse events should generate synthetic touch events */
    1.68 +    if (mouse->mouse_touch_events) {
    1.69 +        if (mouseID != SDL_TOUCH_MOUSEID && button == SDL_BUTTON_LEFT) {
    1.70 +            if (window) {
    1.71 +                float fx = (float)mouse->x / (float)window->w;
    1.72 +                float fy = (float)mouse->y / (float)window->h;
    1.73 +                if (state == SDL_PRESSED) {
    1.74 +                    track_mouse_down = SDL_TRUE;
    1.75 +                } else {
    1.76 +                    track_mouse_down = SDL_FALSE;
    1.77 +                }
    1.78 +                SDL_SendTouch(SDL_MOUSE_TOUCHID, 0, track_mouse_down, fx, fy, 1.0f);
    1.79 +            }
    1.80 +        }
    1.81 +    }
    1.82 +
    1.83      /* Figure out which event to perform */
    1.84      switch (state) {
    1.85      case SDL_PRESSED: