sdl3-src 3.4.4

Source code of the SDL 3 library
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
/*
  Simple DirectMedia Layer
  Copyright (C) 1997-2026 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
  arising from the use of this software.

  Permission is granted to anyone to use this software for any purpose,
  including commercial applications, and to alter it and redistribute it
  freely, subject to the following restrictions:

  1. The origin of this software must not be misrepresented; you must not
     claim that you wrote the original software. If you use this software
     in a product, an acknowledgment in the product documentation would be
     appreciated but is not required.
  2. Altered source versions must be plainly marked as such, and must not be
     misrepresented as being the original software.
  3. This notice may not be removed or altered from any source distribution.
*/
#include "SDL_internal.h"

// General touch handling code for SDL

#include "SDL_events_c.h"
#include "../video/SDL_sysvideo.h"

static SDL_Mutex *SDL_touch_lock = NULL; // This needs to support recursive locks
static int SDL_touch_locked = 0;

struct SDL_Touch
{
    SDL_TouchID id SDL_GUARDED_BY(SDL_touch_lock);
    SDL_TouchDeviceType type SDL_GUARDED_BY(SDL_touch_lock);
    int num_fingers SDL_GUARDED_BY(SDL_touch_lock);
    int max_fingers SDL_GUARDED_BY(SDL_touch_lock);
    SDL_Finger **fingers SDL_GUARDED_BY(SDL_touch_lock);
    char *name SDL_GUARDED_BY(SDL_touch_lock);
};

static int SDL_num_touch SDL_GUARDED_BY(SDL_touch_lock) = 0;
static SDL_Touch **SDL_touchDevices SDL_GUARDED_BY(SDL_touch_lock) = NULL;

// for mapping touch events to mice
static bool finger_touching = false;
static SDL_FingerID track_fingerid;
static SDL_TouchID track_touchid;

// Public functions
bool SDL_InitTouch(void)
{
    SDL_touch_lock = SDL_CreateMutex();
    return true;
}

static void SDL_LockTouch(void) SDL_ACQUIRE(SDL_touch_lock)
{
    SDL_LockMutex(SDL_touch_lock);
    ++SDL_touch_locked;
}

static void SDL_UnlockTouch(void) SDL_RELEASE(SDL_touch_lock)
{
    --SDL_touch_locked;
    SDL_UnlockMutex(SDL_touch_lock);
}

static void SDL_AssertTouchLocked(void) SDL_ASSERT_CAPABILITY(SDL_touch_lock)
{
    SDL_assert(SDL_touch_locked > 0);
}

bool SDL_TouchDevicesAvailable(void)
{
    bool available;

    SDL_LockTouch();
    {
        available = (SDL_num_touch > 0);
    }
    SDL_UnlockTouch();

    return available;
}

SDL_TouchID *SDL_GetTouchDevices(int *count)
{
    SDL_TouchID *result;

    if (count) {
        *count = 0;
    }

    SDL_LockTouch();
    {
        const int total = SDL_num_touch;
        result = (SDL_TouchID *)SDL_malloc(sizeof (SDL_TouchID) * (total + 1));
        if (result) {
            for (int i = 0; i < total; i++) {
                result[i] = SDL_touchDevices[i]->id;
            }
            result[total] = 0;
            if (count) {
                *count = SDL_num_touch;
            }
        }
    }
    SDL_UnlockTouch();

    return result;
}

static int SDL_GetTouchIndex(SDL_TouchID id)
{
    int index;
    SDL_Touch *touch;

    SDL_AssertTouchLocked();

    for (index = 0; index < SDL_num_touch; ++index) {
        touch = SDL_touchDevices[index];
        if (touch->id == id) {
            return index;
        }
    }
    return -1;
}

SDL_Touch *SDL_GetTouch(SDL_TouchID id)
{
    SDL_AssertTouchLocked();

    int index = SDL_GetTouchIndex(id);
    if (index < 0 || index >= SDL_num_touch) {
        if ((id == SDL_MOUSE_TOUCHID) || (id == SDL_PEN_TOUCHID)) {
            // this is a virtual touch device, but for some reason they aren't added to the system. Just ignore it.
        } else if ( SDL_GetVideoDevice()->ResetTouch) {
            SDL_SetError("Unknown touch id %d, resetting", (int)id);
            SDL_GetVideoDevice()->ResetTouch(SDL_GetVideoDevice());
        } else {
            SDL_SetError("Unknown touch device id %d, cannot reset", (int)id);
        }
        return NULL;
    }
    return SDL_touchDevices[index];
}

const char *SDL_GetTouchDeviceName(SDL_TouchID id)
{
    const char *name = NULL;

    SDL_LockTouch();
    {
        SDL_Touch *touch = SDL_GetTouch(id);
        if (touch) {
            name = SDL_GetPersistentString(touch->name);
        }
    }
    SDL_UnlockTouch();

    return name;
}

SDL_TouchDeviceType SDL_GetTouchDeviceType(SDL_TouchID id)
{
    SDL_TouchDeviceType type = SDL_TOUCH_DEVICE_INVALID;

    SDL_LockTouch();
    {
        SDL_Touch *touch = SDL_GetTouch(id);
        if (touch) {
            type = touch->type;
        }
    }
    SDL_UnlockTouch();

    return type;
}

static int SDL_GetFingerIndex(const SDL_Touch *touch, SDL_FingerID fingerid)
{
    SDL_AssertTouchLocked();

    int index;
    for (index = 0; index < touch->num_fingers; ++index) {
        if (touch->fingers[index]->id == fingerid) {
            return index;
        }
    }
    return -1;
}

static SDL_Finger *SDL_GetFinger(const SDL_Touch *touch, SDL_FingerID id)
{
    SDL_AssertTouchLocked();

    int index = SDL_GetFingerIndex(touch, id);
    if (index < 0 || index >= touch->num_fingers) {
        return NULL;
    }
    return touch->fingers[index];
}

SDL_Finger **SDL_GetTouchFingers(SDL_TouchID touchID, int *count)
{
    SDL_Finger **fingers;
    SDL_Finger *finger_data;

    if (count) {
        *count = 0;
    }

    SDL_LockTouch();
    {
        SDL_Touch *touch = SDL_GetTouch(touchID);
        if (!touch) {
            SDL_UnlockTouch();
            return NULL;
        }

        // Create a snapshot of the current finger state
        fingers = (SDL_Finger **)SDL_malloc((touch->num_fingers + 1) * sizeof(*fingers) + touch->num_fingers * sizeof(**fingers));
        if (!fingers) {
            SDL_UnlockTouch();
            return NULL;
        }
        finger_data = (SDL_Finger *)(fingers + (touch->num_fingers + 1));

        for (int i = 0; i < touch->num_fingers; ++i) {
            fingers[i] = &finger_data[i];
            SDL_copyp(fingers[i], touch->fingers[i]);
        }
        fingers[touch->num_fingers] = NULL;

        if (count) {
            *count = touch->num_fingers;
        }
    }
    SDL_UnlockTouch();

    return fingers;
}

int SDL_AddTouch(SDL_TouchID touchID, SDL_TouchDeviceType type, const char *name)
{
    SDL_Touch **touchDevices;
    int index;

    SDL_assert(touchID != 0);

    SDL_LockTouch();
    {
        index = SDL_GetTouchIndex(touchID);
        if (index >= 0) {
            SDL_UnlockTouch();
            return index;
        }

        // Add the touch to the list of touch
        touchDevices = (SDL_Touch **)SDL_realloc(SDL_touchDevices,
                                                 (SDL_num_touch + 1) * sizeof(*touchDevices));
        if (!touchDevices) {
            SDL_UnlockTouch();
            return -1;
        }

        SDL_touchDevices = touchDevices;
        index = SDL_num_touch;

        SDL_touchDevices[index] = (SDL_Touch *)SDL_malloc(sizeof(*SDL_touchDevices[index]));
        if (!SDL_touchDevices[index]) {
            SDL_UnlockTouch();
            return -1;
        }

        // Added touch to list
        ++SDL_num_touch;

        // 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;
        SDL_touchDevices[index]->name = SDL_strdup(name ? name : "");
    }
    SDL_UnlockTouch();

    return index;
}

static bool SDL_AddFinger(SDL_Touch *touch, SDL_FingerID fingerid, float x, float y, float pressure)
{
    SDL_Finger *finger;

    SDL_assert(fingerid != 0);

    SDL_AssertTouchLocked();

    if (touch->num_fingers == touch->max_fingers) {
        SDL_Finger **new_fingers;
        new_fingers = (SDL_Finger **)SDL_realloc(touch->fingers, (touch->max_fingers + 1) * sizeof(*touch->fingers));
        if (!new_fingers) {
            return false;
        }
        touch->fingers = new_fingers;
        touch->fingers[touch->max_fingers] = (SDL_Finger *)SDL_malloc(sizeof(*finger));
        if (!touch->fingers[touch->max_fingers]) {
            return false;
        }
        touch->max_fingers++;
    }

    finger = touch->fingers[touch->num_fingers++];
    finger->id = fingerid;
    finger->x = x;
    finger->y = y;
    finger->pressure = pressure;
    return true;
}

static void SDL_DelFinger(SDL_Touch *touch, SDL_FingerID fingerid)
{
    SDL_AssertTouchLocked();

    int index = SDL_GetFingerIndex(touch, fingerid);
    if (index < 0) {
        return;
    }

    --touch->num_fingers;
    if (index < (touch->num_fingers)) {
        // Move the deleted finger to just past the end of the active fingers array and shift the active fingers by one.
        // This ensures that the descriptor for the now-deleted finger is located at `touch->fingers[touch->num_fingers]`
        // and is ready for use in SDL_AddFinger.
        SDL_Finger *deleted_finger = touch->fingers[index];
        SDL_memmove(&touch->fingers[index], &touch->fingers[index + 1], (touch->num_fingers - index) * sizeof(touch->fingers[index]));
        touch->fingers[touch->num_fingers] = deleted_finger;
    }
}

void SDL_SendTouch(Uint64 timestamp, SDL_TouchID id, SDL_FingerID fingerid, SDL_Window *window, SDL_EventType type, float x, float y, float pressure)
{
    SDL_Finger *finger;
    bool down = (type == SDL_EVENT_FINGER_DOWN);

    SDL_LockTouch();
    {
        SDL_Touch *touch = SDL_GetTouch(id);
        if (!touch) {
            SDL_UnlockTouch();
            return;
        }

        SDL_Mouse *mouse = SDL_GetMouse();

        // SDL_HINT_TOUCH_MOUSE_EVENTS: controlling whether touch events should generate synthetic mouse events
        // SDL_HINT_VITA_TOUCH_MOUSE_DEVICE: controlling which touchpad should generate synthetic mouse events, PSVita-only
        {
            // FIXME: maybe we should only restrict to a few SDL_TouchDeviceType
            if ((id != SDL_MOUSE_TOUCHID) && (id != SDL_PEN_TOUCHID)) {
    #ifdef SDL_PLATFORM_VITA
                if (mouse->touch_mouse_events && ((mouse->vita_touch_mouse_device == id) || (mouse->vita_touch_mouse_device == 3))) {
    #else
                if (mouse->touch_mouse_events) {
    #endif
                    if (window) {
                        if (down) {
                            if (finger_touching == false) {
                                float pos_x = (x * (float)window->w);
                                float pos_y = (y * (float)window->h);
                                if (pos_x < 0) {
                                    pos_x = 0;
                                }
                                if (pos_x > (float)(window->w - 1)) {
                                    pos_x = (float)(window->w - 1);
                                }
                                if (pos_y < 0.0f) {
                                    pos_y = 0.0f;
                                }
                                if (pos_y > (float)(window->h - 1)) {
                                    pos_y = (float)(window->h - 1);
                                }
                                SDL_SendMouseMotion(timestamp, window, SDL_TOUCH_MOUSEID, false, pos_x, pos_y);
                                SDL_SendMouseButton(timestamp, window, SDL_TOUCH_MOUSEID, SDL_BUTTON_LEFT, true);
                            }
                        } else {
                            if (finger_touching == true && track_touchid == id && track_fingerid == fingerid) {
                                SDL_SendMouseButton(timestamp, window, SDL_TOUCH_MOUSEID, SDL_BUTTON_LEFT, false);
                            }
                        }
                    }
                    if (down) {
                        if (finger_touching == false) {
                            finger_touching = true;
                            track_touchid = id;
                            track_fingerid = fingerid;
                        }
                    } else {
                        if (finger_touching == true && track_touchid == id && track_fingerid == fingerid) {
                            finger_touching = false;
                        }
                    }
                }
            }
        }

        // SDL_HINT_MOUSE_TOUCH_EVENTS: if not set, discard synthetic touch events coming from platform layer
        if (!mouse->mouse_touch_events && (id == SDL_MOUSE_TOUCHID)) {
            SDL_UnlockTouch();
            return;
        } else if (!mouse->pen_touch_events && (id == SDL_PEN_TOUCHID)) {
            SDL_UnlockTouch();
            return;
        }

        finger = SDL_GetFinger(touch, fingerid);
        if (down) {
            if (finger) {
                /* This finger is already down.
                   Assume the finger-up for the previous touch was lost, and send it. */
                SDL_SendTouch(timestamp, id, fingerid, window, SDL_EVENT_FINGER_CANCELED, x, y, pressure);
            }

            if (!SDL_AddFinger(touch, fingerid, x, y, pressure)) {
                SDL_UnlockTouch();
                return;
            }

            if (SDL_EventEnabled(type)) {
                SDL_Event event;
                event.type = type;
                event.common.timestamp = timestamp;
                event.tfinger.touchID = id;
                event.tfinger.fingerID = fingerid;
                event.tfinger.x = x;
                event.tfinger.y = y;
                event.tfinger.dx = 0;
                event.tfinger.dy = 0;
                event.tfinger.pressure = pressure;
                event.tfinger.windowID = window ? SDL_GetWindowID(window) : 0;
                SDL_PushEvent(&event);
            }
        } else {
            if (!finger) {
                // This finger is already up
                SDL_UnlockTouch();
                return;
            }

            if (SDL_EventEnabled(type)) {
                SDL_Event event;
                event.type = type;
                event.common.timestamp = timestamp;
                event.tfinger.touchID = id;
                event.tfinger.fingerID = fingerid;
                // I don't trust the coordinates passed on fingerUp
                event.tfinger.x = finger->x;
                event.tfinger.y = finger->y;
                event.tfinger.dx = 0;
                event.tfinger.dy = 0;
                event.tfinger.pressure = pressure;
                event.tfinger.windowID = window ? SDL_GetWindowID(window) : 0;
                SDL_PushEvent(&event);
            }

            SDL_DelFinger(touch, fingerid);
        }
    }
    SDL_UnlockTouch();
}

void SDL_SendTouchMotion(Uint64 timestamp, SDL_TouchID id, SDL_FingerID fingerid, SDL_Window *window,
                        float x, float y, float pressure)
{
    SDL_Touch *touch;
    SDL_Finger *finger;
    float xrel, yrel, prel;

    SDL_LockTouch();
    {
        touch = SDL_GetTouch(id);
        if (!touch) {
            SDL_UnlockTouch();
            return;
        }

        SDL_Mouse *mouse = SDL_GetMouse();

        // SDL_HINT_TOUCH_MOUSE_EVENTS: controlling whether touch events should generate synthetic mouse events
        {
            if ((id != SDL_MOUSE_TOUCHID) && (id != SDL_PEN_TOUCHID)) {
                if (mouse->touch_mouse_events) {
                    if (window) {
                        if (finger_touching == true && track_touchid == id && track_fingerid == fingerid) {
                            float pos_x = (x * (float)window->w);
                            float pos_y = (y * (float)window->h);
                            if (pos_x < 0.0f) {
                                pos_x = 0.0f;
                            }
                            if (pos_x > (float)(window->w - 1)) {
                                pos_x = (float)(window->w - 1);
                            }
                            if (pos_y < 0.0f) {
                                pos_y = 0.0f;
                            }
                            if (pos_y > (float)(window->h - 1)) {
                                pos_y = (float)(window->h - 1);
                            }
                            SDL_SendMouseMotion(timestamp, window, SDL_TOUCH_MOUSEID, false, pos_x, pos_y);
                        }
                    }
                }
            }
        }

        // SDL_HINT_MOUSE_TOUCH_EVENTS: if not set, discard synthetic touch events coming from platform layer
        if (!mouse->mouse_touch_events) {
            if (id == SDL_MOUSE_TOUCHID) {
                SDL_UnlockTouch();
                return;
            }
        }

        finger = SDL_GetFinger(touch, fingerid);
        if (!finger) {
            SDL_SendTouch(timestamp, id, fingerid, window, SDL_EVENT_FINGER_DOWN, x, y, pressure);
            SDL_UnlockTouch();
            return;
        }

        xrel = x - finger->x;
        yrel = y - finger->y;
        prel = pressure - finger->pressure;

        // Drop events that don't change state
        if (xrel == 0.0f && yrel == 0.0f && prel == 0.0f) {
    #if 0
            printf("Touch event didn't change state - dropped!\n");
    #endif
            SDL_UnlockTouch();
            return;
        }

        // Update internal touch coordinates
        finger->x = x;
        finger->y = y;
        finger->pressure = pressure;

        // Post the event, if desired
        if (SDL_EventEnabled(SDL_EVENT_FINGER_MOTION)) {
            SDL_Event event;
            event.type = SDL_EVENT_FINGER_MOTION;
            event.common.timestamp = timestamp;
            event.tfinger.touchID = id;
            event.tfinger.fingerID = fingerid;
            event.tfinger.x = x;
            event.tfinger.y = y;
            event.tfinger.dx = xrel;
            event.tfinger.dy = yrel;
            event.tfinger.pressure = pressure;
            event.tfinger.windowID = window ? SDL_GetWindowID(window) : 0;
            SDL_PushEvent(&event);
        }
    }
    SDL_UnlockTouch();
}

void SDL_DelTouch(SDL_TouchID id)
{
    int i, index;
    SDL_Touch *touch;

    SDL_LockTouch();
    {
        if (SDL_num_touch == 0) {
            // We've already cleaned up, we won't find this device
            SDL_UnlockTouch();
            return;
        }

        index = SDL_GetTouchIndex(id);
        touch = SDL_GetTouch(id);
        if (!touch) {
            SDL_UnlockTouch();
            return;
        }

        for (i = 0; i < touch->max_fingers; ++i) {
            SDL_free(touch->fingers[i]);
        }
        SDL_free(touch->fingers);
        SDL_free(touch->name);
        SDL_free(touch);

        SDL_num_touch--;
        SDL_touchDevices[index] = SDL_touchDevices[SDL_num_touch];
    }
    SDL_UnlockTouch();
}

void SDL_QuitTouch(void)
{
    int i;

    SDL_LockTouch();
    {
        for (i = SDL_num_touch; i--;) {
            SDL_DelTouch(SDL_touchDevices[i]->id);
        }
        SDL_assert(SDL_num_touch == 0);

        SDL_free(SDL_touchDevices);
        SDL_touchDevices = NULL;
    }
    SDL_UnlockTouch();

    SDL_DestroyMutex(SDL_touch_lock);
    SDL_touch_lock = NULL;
}

int SDL_SendPinch(SDL_EventType type, Uint64 timestamp, SDL_Window *window, float scale)
{
    /* Post the event, if desired */
    int posted = 0;
    if (SDL_EventEnabled(type)) {
        SDL_Event event;
        event.type = type;
        event.common.timestamp = timestamp;
        event.pinch.scale = scale;
        event.pinch.windowID = window ? SDL_GetWindowID(window) : 0;
        posted = (SDL_PushEvent(&event) > 0);
    }
    return posted;
}