| | |
| | | /* |
| | | Simple DirectMedia Layer |
| | | Copyright (C) 1997-2018 Sam Lantinga <slouken@libsdl.org> |
| | | Copyright (C) 1997-2020 Sam Lantinga <slouken@libsdl.org> |
| | | |
| | | This software is provided 'as-is', without any express or implied |
| | | warranty. In no event will the authors be held liable for any damages |
| | |
| | | static int SDL_num_touch = 0; |
| | | static SDL_Touch **SDL_touchDevices = NULL; |
| | | |
| | | /* for mapping touch events to mice */ |
| | | |
| | | #define SYNTHESIZE_TOUCH_TO_MOUSE 1 |
| | | |
| | | #if SYNTHESIZE_TOUCH_TO_MOUSE |
| | | static SDL_bool finger_touching = SDL_FALSE; |
| | | static SDL_FingerID track_fingerid; |
| | | static SDL_TouchID track_touchid; |
| | | #endif |
| | | |
| | | /* Public functions */ |
| | | int |
| | |
| | | return SDL_touchDevices[index]; |
| | | } |
| | | |
| | | SDL_TouchDeviceType |
| | | SDL_GetTouchDeviceType(SDL_TouchID id) |
| | | { |
| | | SDL_Touch *touch = SDL_GetTouch(id); |
| | | if (touch) { |
| | | return touch->type; |
| | | } |
| | | return SDL_TOUCH_DEVICE_INVALID; |
| | | } |
| | | |
| | | static int |
| | | SDL_GetFingerIndex(const SDL_Touch * touch, SDL_FingerID fingerid) |
| | | { |
| | |
| | | } |
| | | |
| | | int |
| | | SDL_AddTouch(SDL_TouchID touchID, const char *name) |
| | | SDL_AddTouch(SDL_TouchID touchID, SDL_TouchDeviceType type, const char *name) |
| | | { |
| | | SDL_Touch **touchDevices; |
| | | int index; |
| | |
| | | |
| | | /* we're setting the touch properties */ |
| | | SDL_touchDevices[index]->id = touchID; |
| | | SDL_touchDevices[index]->type = type; |
| | | SDL_touchDevices[index]->num_fingers = 0; |
| | | SDL_touchDevices[index]->max_fingers = 0; |
| | | SDL_touchDevices[index]->fingers = NULL; |
| | |
| | | } |
| | | |
| | | int |
| | | SDL_SendTouch(SDL_TouchID id, SDL_FingerID fingerid, |
| | | SDL_SendTouch(SDL_TouchID id, SDL_FingerID fingerid, SDL_Window * window, |
| | | SDL_bool down, float x, float y, float pressure) |
| | | { |
| | | int posted; |
| | | SDL_Finger *finger; |
| | | SDL_Mouse *mouse; |
| | | |
| | | SDL_Touch* touch = SDL_GetTouch(id); |
| | | if (!touch) { |
| | | return -1; |
| | | } |
| | | |
| | | mouse = SDL_GetMouse(); |
| | | |
| | | #if SYNTHESIZE_TOUCH_TO_MOUSE |
| | | /* SDL_HINT_TOUCH_MOUSE_EVENTS: controlling whether touch events should generate synthetic mouse events */ |
| | | { |
| | | if (mouse->touch_mouse_events) { |
| | | /* FIXME: maybe we should only restrict to a few SDL_TouchDeviceType */ |
| | | if (id != SDL_MOUSE_TOUCHID) { |
| | | if (window) { |
| | | if (down) { |
| | | if (finger_touching == SDL_FALSE) { |
| | | int pos_x = (int)(x * (float)window->w); |
| | | int pos_y = (int)(y * (float)window->h); |
| | | if (pos_x < 0) pos_x = 0; |
| | | if (pos_x > window->w - 1) pos_x = window->w - 1; |
| | | if (pos_y < 0) pos_y = 0; |
| | | if (pos_y > window->h - 1) pos_y = window->h - 1; |
| | | SDL_SendMouseMotion(window, SDL_TOUCH_MOUSEID, 0, pos_x, pos_y); |
| | | SDL_SendMouseButton(window, SDL_TOUCH_MOUSEID, SDL_PRESSED, SDL_BUTTON_LEFT); |
| | | } |
| | | } else { |
| | | if (finger_touching == SDL_TRUE && track_touchid == id && track_fingerid == fingerid) { |
| | | SDL_SendMouseButton(window, SDL_TOUCH_MOUSEID, SDL_RELEASED, SDL_BUTTON_LEFT); |
| | | } |
| | | } |
| | | } |
| | | if (down) { |
| | | if (finger_touching == SDL_FALSE) { |
| | | finger_touching = SDL_TRUE; |
| | | track_touchid = id; |
| | | track_fingerid = fingerid; |
| | | } |
| | | } else { |
| | | if (finger_touching == SDL_TRUE && track_touchid == id && track_fingerid == fingerid) { |
| | | finger_touching = SDL_FALSE; |
| | | } |
| | | } |
| | | } |
| | | } |
| | | } |
| | | #endif |
| | | |
| | | /* SDL_HINT_MOUSE_TOUCH_EVENTS: if not set, discard synthetic touch events coming from platform layer */ |
| | | if (mouse->mouse_touch_events == 0) { |
| | | if (id == SDL_MOUSE_TOUCHID) { |
| | | return 0; |
| | | } |
| | | } |
| | | |
| | | finger = SDL_GetFinger(touch, fingerid); |
| | |
| | | event.tfinger.dx = 0; |
| | | event.tfinger.dy = 0; |
| | | event.tfinger.pressure = pressure; |
| | | event.tfinger.windowID = window ? SDL_GetWindowID(window) : 0; |
| | | posted = (SDL_PushEvent(&event) > 0); |
| | | } |
| | | } else { |
| | |
| | | if (SDL_GetEventState(SDL_FINGERUP) == SDL_ENABLE) { |
| | | SDL_Event event; |
| | | event.tfinger.type = SDL_FINGERUP; |
| | | event.tfinger.touchId = id; |
| | | event.tfinger.touchId = id; |
| | | event.tfinger.fingerId = fingerid; |
| | | /* I don't trust the coordinates passed on fingerUp */ |
| | | event.tfinger.x = finger->x; |
| | |
| | | event.tfinger.dx = 0; |
| | | event.tfinger.dy = 0; |
| | | event.tfinger.pressure = pressure; |
| | | event.tfinger.windowID = window ? SDL_GetWindowID(window) : 0; |
| | | posted = (SDL_PushEvent(&event) > 0); |
| | | } |
| | | |
| | |
| | | } |
| | | |
| | | int |
| | | SDL_SendTouchMotion(SDL_TouchID id, SDL_FingerID fingerid, |
| | | SDL_SendTouchMotion(SDL_TouchID id, SDL_FingerID fingerid, SDL_Window * window, |
| | | float x, float y, float pressure) |
| | | { |
| | | SDL_Touch *touch; |
| | | SDL_Finger *finger; |
| | | SDL_Mouse *mouse; |
| | | int posted; |
| | | float xrel, yrel, prel; |
| | | |
| | |
| | | return -1; |
| | | } |
| | | |
| | | mouse = SDL_GetMouse(); |
| | | |
| | | #if SYNTHESIZE_TOUCH_TO_MOUSE |
| | | /* SDL_HINT_TOUCH_MOUSE_EVENTS: controlling whether touch events should generate synthetic mouse events */ |
| | | { |
| | | if (mouse->touch_mouse_events) { |
| | | if (id != SDL_MOUSE_TOUCHID) { |
| | | if (window) { |
| | | if (finger_touching == SDL_TRUE && track_touchid == id && track_fingerid == fingerid) { |
| | | int pos_x = (int)(x * (float)window->w); |
| | | int pos_y = (int)(y * (float)window->h); |
| | | if (pos_x < 0) pos_x = 0; |
| | | if (pos_x > window->w - 1) pos_x = window->w - 1; |
| | | if (pos_y < 0) pos_y = 0; |
| | | if (pos_y > window->h - 1) pos_y = window->h - 1; |
| | | SDL_SendMouseMotion(window, SDL_TOUCH_MOUSEID, 0, pos_x, pos_y); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | } |
| | | #endif |
| | | |
| | | /* SDL_HINT_MOUSE_TOUCH_EVENTS: if not set, discard synthetic touch events coming from platform layer */ |
| | | if (mouse->mouse_touch_events == 0) { |
| | | if (id == SDL_MOUSE_TOUCHID) { |
| | | return 0; |
| | | } |
| | | } |
| | | |
| | | finger = SDL_GetFinger(touch,fingerid); |
| | | if (!finger) { |
| | | return SDL_SendTouch(id, fingerid, SDL_TRUE, x, y, pressure); |
| | | return SDL_SendTouch(id, fingerid, window, SDL_TRUE, x, y, pressure); |
| | | } |
| | | |
| | | xrel = x - finger->x; |
| | |
| | | prel = pressure - finger->pressure; |
| | | |
| | | /* Drop events that don't change state */ |
| | | if (!xrel && !yrel && !prel) { |
| | | if (xrel == 0.0f && yrel == 0.0f && prel == 0.0f) { |
| | | #if 0 |
| | | printf("Touch event didn't change state - dropped!\n"); |
| | | #endif |
| | |
| | | event.tfinger.dx = xrel; |
| | | event.tfinger.dy = yrel; |
| | | event.tfinger.pressure = pressure; |
| | | event.tfinger.windowID = window ? SDL_GetWindowID(window) : 0; |
| | | posted = (SDL_PushEvent(&event) > 0); |
| | | } |
| | | return posted; |