| 1 |
# Copyright 2014 Patrick Dawson <[email protected]> |
| 2 |
# |
| 3 |
# This software is provided 'as-is', without any express or implied |
| 4 |
# warranty. In no event will the authors be held liable for any damages |
| 5 |
# arising from the use of this software. |
| 6 |
# |
| 7 |
# Permission is granted to anyone to use this software for any purpose, |
| 8 |
# including commercial applications, and to alter it and redistribute it |
| 9 |
# freely, subject to the following restrictions: |
| 10 |
# |
| 11 |
# 1. The origin of this software must not be misrepresented; you must not |
| 12 |
# claim that you wrote the original software. If you use this software |
| 13 |
# in a product, an acknowledgment in the product documentation would be |
| 14 |
# appreciated but is not required. |
| 15 |
# 2. Altered source versions must be plainly marked as such, and must not be |
| 16 |
# misrepresented as being the original software. |
| 17 |
# 3. This notice may not be removed or altered from any source distribution. |
| 18 |
|
| 19 |
from sdl2 cimport * |
| 20 |
from pygame_sdl2.error import error |
| 21 |
import pygame_sdl2 |
| 22 |
|
| 23 |
@pygame_sdl2.register_init |
| 24 |
def init(): |
| 25 |
pygame_sdl2.display.sdl_main_init() |
| 26 |
|
| 27 |
if SDL_InitSubSystem(SDL_INIT_JOYSTICK): |
| 28 |
raise error() |
| 29 |
|
| 30 |
@pygame_sdl2.register_quit |
| 31 |
def quit(): # @ReservedAssignment |
| 32 |
SDL_QuitSubSystem(SDL_INIT_JOYSTICK) |
| 33 |
|
| 34 |
def get_init(): |
| 35 |
return SDL_WasInit(SDL_INIT_JOYSTICK) != 0 |
| 36 |
|
| 37 |
def get_count(): |
| 38 |
return SDL_NumJoysticks() |
| 39 |
|
| 40 |
|
| 41 |
cdef class Joystick: |
| 42 |
# Allow weak references. |
| 43 |
cdef object __weakref__ |
| 44 |
|
| 45 |
cdef SDL_Joystick *joystick |
| 46 |
cdef int joyid |
| 47 |
|
| 48 |
def __cinit__(self): |
| 49 |
self.joystick = NULL |
| 50 |
|
| 51 |
def __init__(self, id): |
| 52 |
self.joyid = id |
| 53 |
|
| 54 |
def init(self): |
| 55 |
if self.joystick == NULL: |
| 56 |
self.joystick = SDL_JoystickOpen(self.joyid) |
| 57 |
if self.joystick == NULL: |
| 58 |
raise error() |
| 59 |
|
| 60 |
def quit(self): # @ReservedAssignment |
| 61 |
if self.joystick != NULL: |
| 62 |
SDL_JoystickClose(self.joystick) |
| 63 |
self.joystick = NULL |
| 64 |
|
| 65 |
def get_init(self): |
| 66 |
if self.joystick: |
| 67 |
return True |
| 68 |
else: |
| 69 |
return False |
| 70 |
|
| 71 |
def get_id(self): |
| 72 |
if self.joystick == NULL: |
| 73 |
raise error("joystick not initialized") |
| 74 |
|
| 75 |
return SDL_JoystickInstanceID(self.joystick) |
| 76 |
|
| 77 |
def get_name(self): |
| 78 |
cdef char *rv |
| 79 |
|
| 80 |
if self.joystick == NULL: |
| 81 |
raise error("joystick not initialized") |
| 82 |
|
| 83 |
rv = SDL_JoystickName(self.joystick) |
| 84 |
|
| 85 |
if rv: |
| 86 |
return rv.decode("utf-8") |
| 87 |
else: |
| 88 |
return None |
| 89 |
|
| 90 |
def get_numaxes(self): |
| 91 |
if self.joystick == NULL: |
| 92 |
raise error("joystick not initialized") |
| 93 |
|
| 94 |
return SDL_JoystickNumAxes(self.joystick) |
| 95 |
|
| 96 |
def get_numballs(self): |
| 97 |
if self.joystick == NULL: |
| 98 |
raise error("joystick not initialized") |
| 99 |
|
| 100 |
return SDL_JoystickNumBalls(self.joystick) |
| 101 |
|
| 102 |
def get_numbuttons(self): |
| 103 |
if self.joystick == NULL: |
| 104 |
raise error("joystick not initialized") |
| 105 |
|
| 106 |
return SDL_JoystickNumButtons(self.joystick) |
| 107 |
|
| 108 |
def get_numhats(self): |
| 109 |
if self.joystick == NULL: |
| 110 |
raise error("joystick not initialized") |
| 111 |
|
| 112 |
return SDL_JoystickNumHats(self.joystick) |
| 113 |
|
| 114 |
def get_axis(self, axis_number): |
| 115 |
if self.joystick == NULL: |
| 116 |
raise error("joystick not initialized") |
| 117 |
|
| 118 |
return SDL_JoystickGetAxis(self.joystick, axis_number) / 32768.0 |
| 119 |
|
| 120 |
def get_ball(self, ball_number): |
| 121 |
cdef int dx, dy |
| 122 |
|
| 123 |
if self.joystick == NULL: |
| 124 |
raise error("joystick not initialized") |
| 125 |
|
| 126 |
if SDL_JoystickGetBall(self.joystick, ball_number, &dx, &dy) == 0: |
| 127 |
return (dx, dy) |
| 128 |
else: |
| 129 |
raise error() |
| 130 |
|
| 131 |
def get_button(self, button): |
| 132 |
if self.joystick == NULL: |
| 133 |
raise error("joystick not initialized") |
| 134 |
|
| 135 |
return SDL_JoystickGetButton(self.joystick, button) == 1 |
| 136 |
|
| 137 |
def get_hat(self, hat_number): |
| 138 |
if self.joystick == NULL: |
| 139 |
raise error("joystick not initialized") |
| 140 |
|
| 141 |
state = SDL_JoystickGetHat(self.joystick, hat_number) |
| 142 |
hx = hy = 0 |
| 143 |
if state & SDL_HAT_LEFT: |
| 144 |
hx = -1 |
| 145 |
elif state & SDL_HAT_RIGHT: |
| 146 |
hx = 1 |
| 147 |
|
| 148 |
if state & SDL_HAT_UP: |
| 149 |
hy = 1 |
| 150 |
elif state & SDL_HAT_DOWN: |
| 151 |
hy = -1 |
| 152 |
|
| 153 |
return (hx, hy) |