Debian Codegraph @

src/pygame_sdl2/controller.pyx

raw
1 # Copyright 2015 Tom Rothamel <[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 from pygame_sdl2.rwobject cimport to_rwops
24
25 @pygame_sdl2.register_init
26 def init():
27 """
28 Initializes game controller support.
29 """
30
31 pygame_sdl2.display.sdl_main_init()
32
33 if SDL_InitSubSystem(SDL_INIT_GAMECONTROLLER):
34 raise error()
35
36 @pygame_sdl2.register_quit
37 def quit(): # @ReservedAssignment
38 """
39 Shuts down game controller support.
40 """
41
42 SDL_QuitSubSystem(SDL_INIT_GAMECONTROLLER)
43
44 def get_init():
45 """
46 Returns true if game controller support has been initialized.
47 """
48
49 return SDL_WasInit(SDL_INIT_GAMECONTROLLER) != 0
50
51 def get_count():
52 """
53 Returns the number of joysticks and game controllers connected to the
54 system. This is one more than the maximum index that can be passed to
55 Controller. While every game controller is a joystick, not every joystick
56 is a game controller. To check that index `i` corresponds to a game
57 controller, use code such as::
58
59 if pygame_sdl2.controller.Controller(i).is_controller():
60 ...
61 """
62
63 return SDL_NumJoysticks()
64
65
66 def add_mapping(mapping):
67 """
68 Adds a game controller mapping from the string in `mapping`.
69 """
70
71 if SDL_GameControllerAddMapping(mapping) == -1:
72 raise error()
73
74 def add_mappings(mapping_file):
75 """
76 Adds game controller mappings from `mapping_file`, which can be a string
77 filename or a file-like object. This file should be gamecontrollerdb.txt,
78 downloaded from:
79
80 https://raw.githubusercontent.com/gabomdq/SDL_GameControllerDB/master/gamecontrollerdb.txt
81
82 This should be called before creating a Controller object. It can be called
83 multiple times to add multiple database files.
84 """
85
86 cdef SDL_RWops *rwops = to_rwops(mapping_file)
87
88 if SDL_GameControllerAddMappingsFromRW(rwops, 1) == -1:
89 raise error()
90
91 def get_axis_from_string(name):
92 """
93 Returns the axis number of the controller axis with `name`, or
94 pygame.CONTROLLER_AXIS_INVALID if `name` is not known.
95 """
96
97 if not isinstance(name, bytes):
98 name = name.encode("utf-8")
99
100 return SDL_GameControllerGetAxisFromString(name)
101
102 def get_button_from_string(name):
103 """
104 Returns the button number of the controller button with `name`, or
105 pygame.CONTROLLER_BUTTON_INVALID if `name` is not known.
106 """
107
108 if not isinstance(name, bytes):
109 name = name.encode("utf-8")
110
111 return SDL_GameControllerGetButtonFromString(name)
112
113 def get_string_for_axis(axis):
114 """
115 Returns a string describing the controller axis `axis`, which must be
116 an integer. Returns None if the axis is not known.
117 """
118
119 cdef const char *rv = SDL_GameControllerGetStringForAxis(axis)
120
121 if rv != NULL:
122 return rv.decode("utf-8")
123 else:
124 return None
125
126 def get_string_for_button(button):
127 """
128 Returns a string describing the controller button `button`, which must be
129 an integer. Returns None if the button is not known.
130 """
131
132 cdef const char *rv = SDL_GameControllerGetStringForButton(button)
133
134 if rv != NULL:
135 return rv.decode("utf-8")
136 else:
137 return None
138
139
140 cdef class Controller:
141 # Allow weak references.
142 cdef object __weakref__
143
144 cdef SDL_GameController *controller
145 cdef int index
146 cdef public int instance_id
147
148 def __cinit__(self):
149 self.controller = NULL
150
151 def __init__(self, index):
152 """
153 Represents a game controller object corresponding to the joystick
154 with `index`.
155 """
156
157 self.index = index
158
159 def init(self):
160 """
161 Opens the game controller, causing it to begin sending events.
162 """
163
164 self.instance_id = SDL_JoystickGetDeviceInstanceID(self.index)
165
166 if self.controller == NULL:
167 self.controller = SDL_GameControllerOpen(self.index)
168 if self.controller == NULL:
169 raise error()
170
171 def quit(self): # @ReservedAssignment
172 """
173 Closes the game controller, preventing it from sending events.
174 """
175
176 if self.controller and SDL_GameControllerGetAttached(self.controller):
177 SDL_GameControllerClose(self.controller)
178 self.controller = NULL
179
180 def get_init(self):
181 """
182 Returns true if the controller has been initialized, false otherwise.
183 """
184
185 if self.controller:
186 return True
187 else:
188 return False
189
190 def get_axis(self, axis):
191 """
192 Returns the value of `axis`, which is one of the pygame.CONTROLLER_AXIS
193 constants.
194
195 Axes return values from -32768 to 32678, triggers return 0 to 32768. 0 is
196 returned on failure.
197 """
198
199 if self.controller == NULL:
200 raise error("controller not initialized.")
201
202 return SDL_GameControllerGetAxis(self.controller, axis)
203
204
205 def get_button(self, button):
206 """
207 Returns the value of `button`, which must be one of the pygame.CONTROLLER_BUTTON
208 constants.
209
210 Returns 1 if the button is pressed, 0 if not or the button does not exist.
211 """
212
213 if self.controller == NULL:
214 raise error("controller not initialized.")
215
216 return SDL_GameControllerGetButton(self.controller, button)
217
218 def get_name(self):
219 """
220 Returns an implementation-dependent name for this game controller,
221 or None if no name could be found.
222 """
223
224 cdef const char *rv = SDL_GameControllerNameForIndex(self.index)
225
226 if rv == NULL:
227 return None
228
229 return rv.decode("utf-8")
230
231 def is_controller(self):
232 """
233 Returns true if this Controller object corresponds to a supported
234 game controller, or False otherwise.
235 """
236
237 return SDL_IsGameController(self.index)
238
239 def get_guid_string(self):
240 """
241 Returns the guid string corresponding to this controller.
242 """
243
244 cdef SDL_JoystickGUID guid
245 cdef char s[33]
246
247 guid = SDL_JoystickGetDeviceGUID(self.index)
248 SDL_JoystickGetGUIDString(guid, s, 33)
249
250 return s.decode("utf-8")