File: joystick_test.py

package info (click to toggle)
pysdl2 0.9.17%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,328 kB
  • sloc: python: 24,685; makefile: 36; sh: 8
file content (547 lines) | stat: -rw-r--r-- 20,576 bytes parent folder | download
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
import sys
import pytest
from ctypes import create_string_buffer, byref, c_int, c_int16, c_uint16, CFUNCTYPE
import sdl2
from sdl2 import SDL_Init, SDL_Quit, SDL_INIT_JOYSTICK
from sdl2.events import SDL_QUERY, SDL_ENABLE, SDL_IGNORE
from sdl2.stdinc import SDL_TRUE, SDL_FALSE
from sdl2.error import SDL_GetError, SDL_ClearError
from .conftest import _check_error_msg

# Get status of joystick support/availability before running tests
any_joysticks = False
SDL_ClearError()
ret = SDL_Init(SDL_INIT_JOYSTICK)
joystick_works = ret == 0
if joystick_works:
    devices = sdl2.SDL_NumJoysticks()
    if sdl2.dll.version >= 2014:
        # On 2.0.14 and above, we can test with a virtual joystick
        devices += 1
    any_joysticks = devices > 0
SDL_Quit()

pytestmark = pytest.mark.skipif(not joystick_works, reason="system unsupported")

joystick_types = [
    sdl2.SDL_JOYSTICK_TYPE_UNKNOWN,
    sdl2.SDL_JOYSTICK_TYPE_GAMECONTROLLER,
    sdl2.SDL_JOYSTICK_TYPE_WHEEL,
    sdl2.SDL_JOYSTICK_TYPE_ARCADE_STICK,
    sdl2.SDL_JOYSTICK_TYPE_FLIGHT_STICK,
    sdl2.SDL_JOYSTICK_TYPE_DANCE_PAD,
    sdl2.SDL_JOYSTICK_TYPE_GUITAR,
    sdl2.SDL_JOYSTICK_TYPE_DRUM_KIT,
    sdl2.SDL_JOYSTICK_TYPE_ARCADE_PAD,
    sdl2.SDL_JOYSTICK_TYPE_THROTTLE,
]


# Overrides global fixture with one that initializes the joystick system
@pytest.fixture(scope="module")
def with_sdl():
    sdl2.SDL_ClearError()
    sdl2.SDL_SetHint(b"SDL_JOYSTICK_ALLOW_BACKGROUND_EVENTS", b"1")
    ret = sdl2.SDL_Init(sdl2.SDL_INIT_VIDEO | sdl2.SDL_INIT_JOYSTICK)
    assert ret == 0, _check_error_msg()
    # Also initialize a virtual joystick (if supported)
    if sdl2.dll.version >= 2014:
        virt_type = sdl2.SDL_JOYSTICK_TYPE_GAMECONTROLLER
        virt_index = sdl2.SDL_JoystickAttachVirtual(virt_type, 2, 4, 1)
    yield
    sdl2.SDL_Quit()

@pytest.fixture()
def joysticks(with_sdl):
    devices = []
    count = sdl2.SDL_NumJoysticks()
    for i in range(count):
        stick = sdl2.SDL_JoystickOpen(i)
        assert stick, _check_error_msg()
        assert isinstance(stick.contents, sdl2.SDL_Joystick)
        devices.append(stick)
    yield devices
    for stick in devices:
        sdl2.SDL_JoystickClose(stick)

def is_virtual(stick):
    virtual = False
    if isinstance(stick, int):
        if sdl2.dll.version >= 2014:
            virtual = sdl2.SDL_JoystickIsVirtual(stick) == SDL_TRUE
    elif isinstance(stick.contents, sdl2.SDL_Joystick):
        name = sdl2.SDL_JoystickName(stick)
        virtual = b"Virtual " in name
    return virtual


# TODO: Make one of the tests gather/print out current joystick info

def test_SDL_NumJoysticks():
    if SDL_Init(SDL_INIT_JOYSTICK) != 0:
        pytest.skip("joystick subsystem not supported")
    retval = sdl2.SDL_NumJoysticks()
    SDL_Quit()
    assert retval >= 0

@pytest.mark.skipif(sdl2.dll.version < 2007, reason="not available")
def test_SDL_LockUnlockJoysticks(with_sdl):
    # NOTE: Not sure how to test these more comprehensively
    sdl2.SDL_LockJoysticks()
    sdl2.SDL_UnlockJoysticks()

def test_SDL_JoystickNameForIndex(with_sdl):
    count = sdl2.SDL_NumJoysticks()
    for index in range(count):
        name = sdl2.SDL_JoystickNameForIndex(index)
        assert type(name) in (str, bytes)

@pytest.mark.skipif(sdl2.dll.version < 2231, reason="not available")
def test_SDL_JoystickPathForIndex(with_sdl):
    count = sdl2.SDL_NumJoysticks()
    for index in range(count):
        path = sdl2.SDL_JoystickPathForIndex(index)
        assert path == None or type(path) in (str, bytes)

@pytest.mark.skipif(sdl2.dll.version < 2009, reason="not available")
def test_SDL_JoystickGetDevicePlayerIndex(with_sdl):
    count = sdl2.SDL_NumJoysticks()
    for index in range(count):
        player = sdl2.SDL_JoystickGetDevicePlayerIndex(index)
        assert player in [-1, 0, 1, 2, 3]

def test_SDL_JoystickGetDeviceGUID(with_sdl):
    count = sdl2.SDL_NumJoysticks()
    for index in range(count):
        guid = sdl2.SDL_JoystickGetDeviceGUID(index)
        assert isinstance(guid, sdl2.SDL_JoystickGUID)
        guidlist = list(guid.data)
        assert isinstance(guidlist[0], int)

def test_SDL_JoystickGetDeviceVendor(with_sdl):
    count = sdl2.SDL_NumJoysticks()
    for index in range(count):
        sdl2.SDL_ClearError()
        vid = sdl2.SDL_JoystickGetDeviceVendor(index)
        if not is_virtual(index):
            assert vid > 0, _check_error_msg()

def test_SDL_JoystickGetDeviceProduct(with_sdl):
    count = sdl2.SDL_NumJoysticks()
    for index in range(count):
        sdl2.SDL_ClearError()
        pid = sdl2.SDL_JoystickGetDeviceProduct(index)
        if not is_virtual(index):
            assert pid > 0, _check_error_msg()

def test_SDL_JoystickGetDeviceProductVersion(with_sdl):
    count = sdl2.SDL_NumJoysticks()
    for index in range(count):
        sdl2.SDL_ClearError()
        pver = sdl2.SDL_JoystickGetDeviceProductVersion(index)
        assert pver >= 0, _check_error_msg()

def test_SDL_JoystickGetDeviceType(with_sdl):
    count = sdl2.SDL_NumJoysticks()
    for index in range(count):
        jtype = sdl2.SDL_JoystickGetDeviceType(index)
        assert jtype in joystick_types
        if is_virtual(index):
            assert jtype == sdl2.SDL_JOYSTICK_TYPE_GAMECONTROLLER

@pytest.mark.skipif(sdl2.dll.version < 2006, reason="not available")
def test_SDL_JoystickGetDeviceInstanceID(joysticks):
    for index in range(len(joysticks)):
        sdl2.SDL_ClearError()
        iid = sdl2.SDL_JoystickGetDeviceInstanceID(index)
        stick = joysticks[index]
        assert iid == sdl2.SDL_JoystickInstanceID(stick), _check_error_msg()

def test_SDL_JoystickOpenClose(with_sdl):
    count = sdl2.SDL_NumJoysticks()
    for index in range(count):
        sdl2.SDL_ClearError()
        stick = sdl2.SDL_JoystickOpen(index)
        assert stick, _check_error_msg()
        assert isinstance(stick.contents, sdl2.SDL_Joystick)
        sdl2.SDL_JoystickClose(stick)

def test_SDL_JoystickFromInstanceID(joysticks):
    for stick in joysticks:
        iid = sdl2.SDL_JoystickInstanceID(stick)
        assert iid >= 0
        stick2 = sdl2.SDL_JoystickFromInstanceID(iid)
        name = sdl2.SDL_JoystickName(stick)
        assert sdl2.SDL_JoystickName(stick2) == name

@pytest.mark.skipif(sdl2.dll.version < 2012, reason="not available")
def test_SDL_JoystickFromPlayerIndex(joysticks):
    i = 0
    for stick in joysticks:
        sdl2.SDL_JoystickSetPlayerIndex(stick, i)
        stick2 = sdl2.SDL_JoystickFromPlayerIndex(i)
        name = sdl2.SDL_JoystickName(stick)
        assert sdl2.SDL_JoystickName(stick2) == name
        i += 1

@pytest.mark.skipif(sdl2.dll.version < 2014, reason="not available")
def test_SDL_JoystickVirtual(with_sdl):
    jcount = sdl2.SDL_NumJoysticks()
    jtype = sdl2.SDL_JOYSTICK_TYPE_GAMECONTROLLER
    index = sdl2.SDL_JoystickAttachVirtual(jtype, 1, 2, 1)
    assert index >= 0
    assert sdl2.SDL_JoystickIsVirtual(index) == SDL_TRUE
    assert sdl2.SDL_NumJoysticks() == (jcount + 1)
    stick = sdl2.SDL_JoystickOpen(index)

    # Test joystick configuration
    assert sdl2.SDL_JoystickNumAxes(stick) == 1
    assert sdl2.SDL_JoystickNumButtons(stick) == 2
    assert sdl2.SDL_JoystickNumHats(stick) == 1

    # Try setting and checking for some virtual values
    assert sdl2.SDL_JoystickSetVirtualAxis(stick, 0, 512) == 0
    assert sdl2.SDL_JoystickSetVirtualButton(stick, 0, 1) == 0
    assert sdl2.SDL_JoystickSetVirtualButton(stick, 1, 1) == 0
    assert sdl2.SDL_JoystickSetVirtualHat(stick, 0, sdl2.SDL_HAT_UP) == 0
    sdl2.SDL_JoystickUpdate()
    assert sdl2.SDL_JoystickGetAxis(stick, 0) == 512
    assert sdl2.SDL_JoystickGetButton(stick, 0) == 1
    assert sdl2.SDL_JoystickGetButton(stick, 1) == 1
    assert sdl2.SDL_JoystickGetHat(stick, 0) == sdl2.SDL_HAT_UP

    # Check that removing the virtual joystick works properly
    sdl2.SDL_JoystickClose(stick)
    jcount = sdl2.SDL_NumJoysticks()
    assert sdl2.SDL_JoystickDetachVirtual(index) == 0
    assert sdl2.SDL_NumJoysticks() == (jcount - 1)

@pytest.mark.skipif(sdl2.dll.version < 2231, reason="not available")
def test_SDL_JoystickAttachVirtualEx(with_sdl):
    # Initialize the custom virtual controller description
    virt = sdl2.SDL_VirtualJoystickDesc()
    virt.version = sdl2.SDL_VIRTUAL_JOYSTICK_DESC_VERSION
    virt.type = sdl2.SDL_JOYSTICK_TYPE_GAMECONTROLLER
    virt.naxes = 4
    virt.nbuttons = 8
    virt.nhats = 1
    virt.name = b"PySDL2 Virtual Controller"

    # Define some custom functions for the virtual controller
    led_value = [0, 0, 0]
    rumble = [0, 0]

    def set_led(userdata, r, g, b):
        led_value[0] = r
        led_value[1] = g
        led_value[2] = b
        return 0

    def set_rumble(userdata, a, b):
        rumble[0] = a
        rumble[1] = b
        return 0

    virt.SetLED = sdl2.joystick.CFUNC_SetLED(set_led)
    virt.Rumble = sdl2.joystick.CFUNC_Rumble(set_rumble)
    virt.RumbleTriggers = sdl2.joystick.CFUNC_RumbleTriggers(set_rumble)

    # Open the custom joystick
    jcount = sdl2.SDL_NumJoysticks()
    index = sdl2.SDL_JoystickAttachVirtualEx(byref(virt))
    assert index >= 0
    assert sdl2.SDL_JoystickIsVirtual(index) == SDL_TRUE
    assert sdl2.SDL_NumJoysticks() == (jcount + 1)
    stick = sdl2.SDL_JoystickOpen(index)

    # Test joystick configuration
    assert sdl2.SDL_JoystickNumAxes(stick) == 4
    assert sdl2.SDL_JoystickNumButtons(stick) == 8
    assert sdl2.SDL_JoystickNumHats(stick) == 1
    assert sdl2.SDL_JoystickName(stick) == b"PySDL2 Virtual Controller"

    # Test callback functions
    sdl2.SDL_JoystickSetLED(stick, 32, 64, 128)
    assert led_value == [32, 64, 128]
    sdl2.SDL_JoystickRumble(stick, 1000, 2000, 500)
    assert rumble == [1000, 2000]
    sdl2.SDL_JoystickRumbleTriggers(stick, 1234, 4321, 500)
    assert rumble == [1234, 4321]

    # Check that removing the virtual joystick works properly
    sdl2.SDL_JoystickClose(stick)
    jcount = sdl2.SDL_NumJoysticks()
    assert sdl2.SDL_JoystickDetachVirtual(index) == 0
    assert sdl2.SDL_NumJoysticks() == (jcount - 1)

def test_SDL_JoystickName(joysticks):
    names = []
    for stick in joysticks:
        name = sdl2.SDL_JoystickName(stick)
        assert type(name) in (str, bytes)
        names.append(name.decode('utf-8'))
    print(names)

@pytest.mark.skipif(sdl2.dll.version < 2231, reason="not available")
def test_SDL_JoystickPath(joysticks):
    paths = []
    for stick in joysticks:
        path = sdl2.SDL_JoystickPath(stick)
        if not (is_virtual(stick) or sys.platform == "darwin"):
            assert type(path) in (str, bytes)
            paths.append(path.decode('utf-8'))
    if len(paths):
        print(paths)

@pytest.mark.skipif(sdl2.dll.version < 2009, reason="not available")
def test_SDL_JoystickGetPlayerIndex(joysticks):
    for stick in joysticks:
        player = sdl2.SDL_JoystickGetPlayerIndex(stick)
        assert player in [-1, 0, 1, 2, 3]

@pytest.mark.skipif(sdl2.dll.version < 2012, reason="not available")
def test_SDL_JoystickSetPlayerIndex(joysticks):
    i = 0
    for stick in joysticks:
        sdl2.SDL_JoystickSetPlayerIndex(stick, i)
        player = sdl2.SDL_JoystickGetPlayerIndex(stick)
        assert player == i
        i += 1

def test_SDL_JoystickGetGUID(joysticks):
    for stick in joysticks:
        guid = sdl2.SDL_JoystickGetGUID(stick)
        assert isinstance(guid, sdl2.SDL_JoystickGUID)
        guidlist = list(guid.data)
        assert isinstance(guidlist[0], int)

def test_SDL_JoystickGetVendor(joysticks):
    for stick in joysticks:
        sdl2.SDL_ClearError()
        vid = sdl2.SDL_JoystickGetVendor(stick)
        if not is_virtual(stick):
            assert vid > 0, _check_error_msg()

def test_SDL_JoystickGetProduct(joysticks):
    for stick in joysticks:
        sdl2.SDL_ClearError()
        pid = sdl2.SDL_JoystickGetProduct(stick)
        if not is_virtual(stick):
            assert pid > 0, _check_error_msg()

def test_SDL_JoystickGetProductVersion(joysticks):
    for stick in joysticks:
        sdl2.SDL_ClearError()
        pver = sdl2.SDL_JoystickGetProductVersion(stick)
        assert pver >= 0, _check_error_msg()

@pytest.mark.skipif(sdl2.dll.version < 2231, reason="not available")
def test_SDL_JoystickGetFirmwareVersion(joysticks):
    for stick in joysticks:
        sdl2.SDL_ClearError()
        fw_ver = sdl2.SDL_JoystickGetFirmwareVersion(stick)
        assert fw_ver >= 0, _check_error_msg()

@pytest.mark.skipif(sdl2.dll.version < 2014, reason="not available")
def test_SDL_JoystickGetSerial(joysticks):
    for stick in joysticks:
        serial = sdl2.SDL_JoystickGetSerial(stick)
        assert serial == None or type(serial) in (str, bytes)

def test_SDL_JoystickGetType(joysticks):
    for stick in joysticks:
        jtype = sdl2.SDL_JoystickGetType(stick)
        assert jtype in joystick_types
        if is_virtual(stick):
            assert jtype == sdl2.SDL_JOYSTICK_TYPE_GAMECONTROLLER

def test_SDL_JoystickGetGUIDString():
    guid_str = b'030000007e050000060300001c3a0000' # Wiimote on macOS
    guid = sdl2.SDL_JoystickGetGUIDFromString(guid_str)
    buff = create_string_buffer(33)
    sdl2.SDL_JoystickGetGUIDString(guid, buff, 33) # Get GUID string
    assert guid_str == buff.value

def test_SDL_JoystickGetGUIDFromString():
    guid_str = b'030000007e050000060300001c3a0000' # Wiimote on macOS
    expected = [3, 0, 0, 0, 126, 5, 0, 0, 6, 3, 0, 0, 28, 58, 0, 0]
    guid = sdl2.SDL_JoystickGetGUIDFromString(guid_str)
    assert list(guid.data) == expected

@pytest.mark.skipif(sdl2.dll.version < 2260, reason="not available")
@pytest.mark.xfail(sys.version_info < (3, 7, 0, 'final'), reason="ctypes bug")
def test_SDL_GetJoystickGUIDInfo():
    guid_str = b'030000007e050000060300001c3a0000' # Wiimote on macOS
    guid = sdl2.SDL_JoystickGetGUIDFromString(guid_str)
    # Try parsing field information from GUID
    vendor, product, version = c_uint16(0), c_uint16(0), c_uint16(0)
    crc16 = c_uint16(0)
    sdl2.SDL_GetJoystickGUIDInfo(
        guid, byref(vendor), byref(product), byref(version), byref(crc16)
    )
    assert vendor.value == 1406
    assert product.value > 0
    assert version.value > 0
    assert crc16.value == 0

def test_SDL_JoystickGetAttached(joysticks):
    for stick in joysticks:
        ret = sdl2.SDL_JoystickGetAttached(stick)
        assert ret in [SDL_FALSE, SDL_TRUE]

def test_SDL_JoystickInstanceID(joysticks):
    for stick in joysticks:
        ret = sdl2.SDL_JoystickInstanceID(stick)
        assert ret >= 0

def test_SDL_JoystickNumAxes(joysticks):
    for stick in joysticks:
        assert isinstance(stick.contents, sdl2.SDL_Joystick)
        axes = sdl2.SDL_JoystickNumAxes(stick)
        assert axes >= 0
        if is_virtual(stick):
            assert axes == 2

def test_SDL_JoystickNumBalls(joysticks):
    for stick in joysticks:
        assert isinstance(stick.contents, sdl2.SDL_Joystick)
        balls = sdl2.SDL_JoystickNumBalls(stick)
        assert balls >= 0

def test_SDL_JoystickNumHats(joysticks):
    for stick in joysticks:
        assert isinstance(stick.contents, sdl2.SDL_Joystick)
        hats = sdl2.SDL_JoystickNumHats(stick)
        assert hats >= 0
        if is_virtual(stick):
            assert hats == 1

def test_SDL_JoystickNumButtons(joysticks):
    for stick in joysticks:
        assert isinstance(stick.contents, sdl2.SDL_Joystick)
        buttons = sdl2.SDL_JoystickNumButtons(stick)
        assert buttons >= 0
        if is_virtual(stick):
            assert buttons == 4

def test_SDL_JoystickUpdate(with_sdl):
    # TODO: This is not 100% safe, but in SDL2, JoystickUpdate returns void
    # so we can't reliably detect error
    sdl2.SDL_ClearError()
    sdl2.SDL_JoystickUpdate()
    assert SDL_GetError() == b""

def test_SDL_JoystickEventState(with_sdl):
    for state in (SDL_IGNORE, SDL_ENABLE):
        news = sdl2.SDL_JoystickEventState(state)
        assert news == state
        query = sdl2.SDL_JoystickEventState(SDL_QUERY)
        assert query == state

def test_SDL_JoystickGetAxis(joysticks):
    for stick in joysticks:
        for axis in range(sdl2.SDL_JoystickNumAxes(stick)):
            val = sdl2.SDL_JoystickGetAxis(stick, axis)
            assert -32768 <= val <= 32767

def test_SDL_JoystickGetAxisInitialState(joysticks):
    init_state = c_int16(0)
    for stick in joysticks:
        for axis in range(sdl2.SDL_JoystickNumAxes(stick)):
            ret = sdl2.SDL_JoystickGetAxisInitialState(
                stick, axis, byref(init_state)
            )
            assert -32768 <= init_state.value <= 32767
            assert ret in [SDL_TRUE, SDL_FALSE]

def test_SDL_JoystickGetHat(joysticks):
    hatvals = [
        sdl2.SDL_HAT_UP, sdl2.SDL_HAT_DOWN, sdl2.SDL_HAT_LEFT,
        sdl2.SDL_HAT_RIGHT, sdl2.SDL_HAT_CENTERED,
        sdl2.SDL_HAT_LEFTUP, sdl2.SDL_HAT_LEFTDOWN,
        sdl2.SDL_HAT_RIGHTUP, sdl2.SDL_HAT_RIGHTDOWN
    ]
    for stick in joysticks:
        for hat in range(sdl2.SDL_JoystickNumHats(stick)):
            val = sdl2.SDL_JoystickGetHat(stick, hat)
            assert val in hatvals

def test_SDL_JoystickGetBall(joysticks):
    numball = [sdl2.SDL_JoystickNumBalls(s) for s in joysticks]
    if not any(numball):
        pytest.skip("no trackball on any connected controller")
    dx, dy = c_int(0), c_int(0)
    get_ball = sdl2.SDL_JoystickGetBall
    for stick in sticks:
        for ball in range(sdl2.SDL_JoystickNumBalls(stick)):
            ret = get_ball(stick, ball, byref(dx), byref(dy))
            assert ret == 0, _check_error_msg()

def test_SDL_JoystickGetButton(joysticks):
    for stick in joysticks:
        for button in range(sdl2.SDL_JoystickNumButtons(stick)):
            val = sdl2.SDL_JoystickGetButton(stick, button)
            assert val in [0, 1]

@pytest.mark.skipif(sdl2.dll.version < 2009, reason="not available")
def test_SDL_JoystickRumble(joysticks):
    # If we ever add an interactive test suite, this should be moved there
    for stick in joysticks:
        # 50% strength low-frequency, 25% high-frequency rumble for 500ms
        ret = sdl2.SDL_JoystickRumble(stick, 32767, 16384, 500)
        assert ret in [-1, 0]

@pytest.mark.skipif(sdl2.dll.version < 2014, reason="not available")
def test_SDL_JoystickRumbleTriggers(joysticks):
    # If we ever add an interactive test suite, this should be moved there
    for stick in joysticks:
        # 50% strength left trigger, 25% right trigger rumble for 500ms
        ret = sdl2.SDL_JoystickRumbleTriggers(stick, 32767, 16384, 500)
        assert ret in [-1, 0]

@pytest.mark.skipif(sdl2.dll.version < 2014, reason="not available")
def test_SDL_JoystickHasSetLED(joysticks):
    # If we ever add an interactive test suite, this should be moved there
    for stick in joysticks:
        has_led = sdl2.SDL_JoystickHasLED(stick)
        assert has_led in [SDL_FALSE, SDL_TRUE]
        expected = -1 if has_led == SDL_FALSE else 0
        cols = [(255, 0, 0), (0, 255, 0), (0, 0, 255)]
        for r, g, b in cols:
            ret = sdl2.SDL_JoystickSetLED(stick, r, g, b)
            assert ret == expected

@pytest.mark.skipif(sdl2.dll.version < 2018, reason="not available")
def test_SDL_JoystickHasRumble(joysticks):
    # If we ever add an interactive test suite, this should be moved there
    for stick in joysticks:
        has_rumble = sdl2.SDL_JoystickHasRumble(stick)
        assert has_rumble in [SDL_FALSE, SDL_TRUE]

@pytest.mark.skipif(sdl2.dll.version < 2018, reason="not available")
def test_SDL_JoystickHasRumbleTriggers(joysticks):
    # If we ever add an interactive test suite, this should be moved there
    for stick in joysticks:
        has_rumble_triggers = sdl2.SDL_JoystickHasRumbleTriggers(stick)
        assert has_rumble_triggers in [SDL_FALSE, SDL_TRUE]

@pytest.mark.skip("not implemented")
@pytest.mark.skipif(sdl2.dll.version < 2016, reason="not available")
def test_SDL_JoystickSendEffect(joysticks):
    # NOTE: Not supported on macOS or Linux, and effect data would be specific
    # to each controller type, so can't easily test this.
    pass

def test_SDL_JoystickCurrentPowerLevel(joysticks):
    levels = [
        sdl2.SDL_JOYSTICK_POWER_UNKNOWN,
        sdl2.SDL_JOYSTICK_POWER_EMPTY,
        sdl2.SDL_JOYSTICK_POWER_LOW,
        sdl2.SDL_JOYSTICK_POWER_MEDIUM,
        sdl2.SDL_JOYSTICK_POWER_FULL,
        sdl2.SDL_JOYSTICK_POWER_WIRED,
        sdl2.SDL_JOYSTICK_POWER_MAX,
    ]
    for stick in joysticks:
        pwr = sdl2.SDL_JoystickCurrentPowerLevel(stick)
        assert pwr in levels