diff options
Diffstat (limited to 'contrib/SDL-3.2.8/src/video/vita/SDL_vitatouch.c')
| -rw-r--r-- | contrib/SDL-3.2.8/src/video/vita/SDL_vitatouch.c | 186 |
1 files changed, 186 insertions, 0 deletions
diff --git a/contrib/SDL-3.2.8/src/video/vita/SDL_vitatouch.c b/contrib/SDL-3.2.8/src/video/vita/SDL_vitatouch.c new file mode 100644 index 0000000..ac6f764 --- /dev/null +++ b/contrib/SDL-3.2.8/src/video/vita/SDL_vitatouch.c | |||
| @@ -0,0 +1,186 @@ | |||
| 1 | /* | ||
| 2 | Simple DirectMedia Layer | ||
| 3 | Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org> | ||
| 4 | |||
| 5 | This software is provided 'as-is', without any express or implied | ||
| 6 | warranty. In no event will the authors be held liable for any damages | ||
| 7 | arising from the use of this software. | ||
| 8 | |||
| 9 | Permission is granted to anyone to use this software for any purpose, | ||
| 10 | including commercial applications, and to alter it and redistribute it | ||
| 11 | freely, subject to the following restrictions: | ||
| 12 | |||
| 13 | 1. The origin of this software must not be misrepresented; you must not | ||
| 14 | claim that you wrote the original software. If you use this software | ||
| 15 | in a product, an acknowledgment in the product documentation would be | ||
| 16 | appreciated but is not required. | ||
| 17 | 2. Altered source versions must be plainly marked as such, and must not be | ||
| 18 | misrepresented as being the original software. | ||
| 19 | 3. This notice may not be removed or altered from any source distribution. | ||
| 20 | */ | ||
| 21 | #include "SDL_internal.h" | ||
| 22 | |||
| 23 | #ifdef SDL_VIDEO_DRIVER_VITA | ||
| 24 | |||
| 25 | #include <psp2/kernel/processmgr.h> | ||
| 26 | #include <psp2/touch.h> | ||
| 27 | |||
| 28 | #include "SDL_vitavideo.h" | ||
| 29 | #include "SDL_vitatouch.h" | ||
| 30 | #include "../../events/SDL_mouse_c.h" | ||
| 31 | #include "../../events/SDL_touch_c.h" | ||
| 32 | |||
| 33 | SceTouchData touch_old[SCE_TOUCH_PORT_MAX_NUM]; | ||
| 34 | SceTouchData touch[SCE_TOUCH_PORT_MAX_NUM]; | ||
| 35 | |||
| 36 | SDL_FRect area_info[SCE_TOUCH_PORT_MAX_NUM]; | ||
| 37 | |||
| 38 | struct | ||
| 39 | { | ||
| 40 | float min; | ||
| 41 | float range; | ||
| 42 | } force_info[SCE_TOUCH_PORT_MAX_NUM]; | ||
| 43 | |||
| 44 | static bool disableFrontPoll; | ||
| 45 | static bool disableBackPoll; | ||
| 46 | |||
| 47 | void VITA_InitTouch(void) | ||
| 48 | { | ||
| 49 | disableFrontPoll = !SDL_GetHintBoolean(SDL_HINT_VITA_ENABLE_FRONT_TOUCH, true); | ||
| 50 | disableBackPoll = !SDL_GetHintBoolean(SDL_HINT_VITA_ENABLE_BACK_TOUCH, true); | ||
| 51 | |||
| 52 | sceTouchSetSamplingState(SCE_TOUCH_PORT_FRONT, SCE_TOUCH_SAMPLING_STATE_START); | ||
| 53 | sceTouchSetSamplingState(SCE_TOUCH_PORT_BACK, SCE_TOUCH_SAMPLING_STATE_START); | ||
| 54 | sceTouchEnableTouchForce(SCE_TOUCH_PORT_FRONT); | ||
| 55 | sceTouchEnableTouchForce(SCE_TOUCH_PORT_BACK); | ||
| 56 | |||
| 57 | for (int port = 0; port < SCE_TOUCH_PORT_MAX_NUM; port++) { | ||
| 58 | SceTouchPanelInfo panelinfo; | ||
| 59 | sceTouchGetPanelInfo(port, &panelinfo); | ||
| 60 | |||
| 61 | area_info[port].x = (float)panelinfo.minAaX; | ||
| 62 | area_info[port].y = (float)panelinfo.minAaY; | ||
| 63 | area_info[port].w = (float)(panelinfo.maxAaX - panelinfo.minAaX); | ||
| 64 | area_info[port].h = (float)(panelinfo.maxAaY - panelinfo.minAaY); | ||
| 65 | |||
| 66 | force_info[port].min = (float)panelinfo.minForce; | ||
| 67 | force_info[port].range = (float)(panelinfo.maxForce - panelinfo.minForce); | ||
| 68 | } | ||
| 69 | |||
| 70 | // Support passing both front and back touch devices in events | ||
| 71 | SDL_AddTouch(1, SDL_TOUCH_DEVICE_DIRECT, "Front"); | ||
| 72 | SDL_AddTouch(2, SDL_TOUCH_DEVICE_INDIRECT_ABSOLUTE, "Back"); | ||
| 73 | } | ||
| 74 | |||
| 75 | void VITA_QuitTouch(void) | ||
| 76 | { | ||
| 77 | sceTouchDisableTouchForce(SCE_TOUCH_PORT_FRONT); | ||
| 78 | sceTouchDisableTouchForce(SCE_TOUCH_PORT_BACK); | ||
| 79 | } | ||
| 80 | |||
| 81 | void VITA_PollTouch(void) | ||
| 82 | { | ||
| 83 | SDL_TouchID touch_id; | ||
| 84 | SDL_FingerID finger_id; | ||
| 85 | int port; | ||
| 86 | |||
| 87 | // We skip polling touch if no window is created | ||
| 88 | if (!Vita_Window) { | ||
| 89 | return; | ||
| 90 | } | ||
| 91 | |||
| 92 | SDL_memcpy(touch_old, touch, sizeof(touch_old)); | ||
| 93 | |||
| 94 | for (port = 0; port < SCE_TOUCH_PORT_MAX_NUM; port++) { | ||
| 95 | /** Skip polling of Touch Device if hint is set **/ | ||
| 96 | if (((port == 0) && disableFrontPoll) || ((port == 1) && disableBackPoll)) { | ||
| 97 | continue; | ||
| 98 | } | ||
| 99 | sceTouchPeek(port, &touch[port], 1); | ||
| 100 | |||
| 101 | touch_id = (SDL_TouchID)(port + 1); | ||
| 102 | |||
| 103 | if (touch[port].reportNum > 0) { | ||
| 104 | for (int i = 0; i < touch[port].reportNum; i++) { | ||
| 105 | // adjust coordinates and forces to return normalized values | ||
| 106 | // for the front, screen area is used as a reference (for direct touch) | ||
| 107 | // e.g. touch_x = 1.0 corresponds to screen_x = 960 | ||
| 108 | // for the back panel, the active touch area is used as reference | ||
| 109 | float x = 0; | ||
| 110 | float y = 0; | ||
| 111 | float force = (touch[port].report[i].force - force_info[port].min) / force_info[port].range; | ||
| 112 | int finger_down = 0; | ||
| 113 | |||
| 114 | if (touch_old[port].reportNum > 0) { | ||
| 115 | for (int j = 0; j < touch_old[port].reportNum; j++) { | ||
| 116 | if (touch[port].report[i].id == touch_old[port].report[j].id) { | ||
| 117 | finger_down = 1; | ||
| 118 | } | ||
| 119 | } | ||
| 120 | } | ||
| 121 | |||
| 122 | VITA_ConvertTouchXYToSDLXY(&x, &y, touch[port].report[i].x, touch[port].report[i].y, port); | ||
| 123 | finger_id = (SDL_FingerID)(touch[port].report[i].id + 1); | ||
| 124 | |||
| 125 | // Skip if finger was already previously down | ||
| 126 | if (!finger_down) { | ||
| 127 | // Send an initial touch | ||
| 128 | SDL_SendTouch(0, touch_id, finger_id, Vita_Window, SDL_EVENT_FINGER_DOWN, x, y, force); | ||
| 129 | } | ||
| 130 | |||
| 131 | // Always send the motion | ||
| 132 | SDL_SendTouchMotion(0, touch_id, finger_id, Vita_Window, x, y, force); | ||
| 133 | } | ||
| 134 | } | ||
| 135 | |||
| 136 | // some fingers might have been let go | ||
| 137 | if (touch_old[port].reportNum > 0) { | ||
| 138 | for (int i = 0; i < touch_old[port].reportNum; i++) { | ||
| 139 | int finger_up = 1; | ||
| 140 | if (touch[port].reportNum > 0) { | ||
| 141 | for (int j = 0; j < touch[port].reportNum; j++) { | ||
| 142 | if (touch[port].report[j].id == touch_old[port].report[i].id) { | ||
| 143 | finger_up = 0; | ||
| 144 | } | ||
| 145 | } | ||
| 146 | } | ||
| 147 | if (finger_up == 1) { | ||
| 148 | float x = 0; | ||
| 149 | float y = 0; | ||
| 150 | float force = (touch_old[port].report[i].force - force_info[port].min) / force_info[port].range; | ||
| 151 | VITA_ConvertTouchXYToSDLXY(&x, &y, touch_old[port].report[i].x, touch_old[port].report[i].y, port); | ||
| 152 | finger_id = (SDL_FingerID)(touch_old[port].report[i].id + 1); | ||
| 153 | // Finger released from screen | ||
| 154 | SDL_SendTouch(0, touch_id, finger_id, Vita_Window, SDL_EVENT_FINGER_UP, x, y, force); | ||
| 155 | } | ||
| 156 | } | ||
| 157 | } | ||
| 158 | } | ||
| 159 | } | ||
| 160 | |||
| 161 | void VITA_ConvertTouchXYToSDLXY(float *sdl_x, float *sdl_y, int vita_x, int vita_y, int port) | ||
| 162 | { | ||
| 163 | float x, y; | ||
| 164 | |||
| 165 | if (area_info[port].w <= 1) { | ||
| 166 | x = 0.5f; | ||
| 167 | } else { | ||
| 168 | x = (vita_x - area_info[port].x) / (area_info[port].w - 1); | ||
| 169 | } | ||
| 170 | if (area_info[port].h <= 1) { | ||
| 171 | y = 0.5f; | ||
| 172 | } else { | ||
| 173 | y = (vita_y - area_info[port].y) / (area_info[port].h - 1); | ||
| 174 | } | ||
| 175 | |||
| 176 | x = SDL_max(x, 0.0f); | ||
| 177 | x = SDL_min(x, 1.0f); | ||
| 178 | |||
| 179 | y = SDL_max(y, 0.0f); | ||
| 180 | y = SDL_min(y, 1.0f); | ||
| 181 | |||
| 182 | *sdl_x = x; | ||
| 183 | *sdl_y = y; | ||
| 184 | } | ||
| 185 | |||
| 186 | #endif // SDL_VIDEO_DRIVER_VITA | ||
