def __del__(self): if self.game_controller is not None: if sdl2.SDL_GameControllerGetAttached(self.game_controller): sdl2.SDL_GameControllerClose(self.game_controller) else: if sdl2.SDL_JoystickGetAttached(self.joystick): sdl2.SDL_JoystickClose(self.joystick)
def close(self): """Close the joystick.""" try: sdl2.SDL_GameControllerClose(self.gamecontroller) except: pass try: sdl2.SDL_JoystickClose(self.joystick) except: pass
def ev_controllerdeviceremoved( self, event: ControllerDeviceRemoved) -> Optional[Action]: # `which` contains the joystick index of the removed controller i = event.which controller = self.controllers.get(i) if controller: print("Removed controller {} {}".format(i, controller, i)) sdl2.SDL_GameControllerClose(controller) self.controllers.pop(i) return None
def sdl2_event_pump(events): global _sdlcontroller # Feed events into the loop for event in sdl2.ext.get_events(): if event.type == sdl2.SDL_QUIT: events.append(WindowEvent(WindowEvent.QUIT)) elif event.type == sdl2.SDL_KEYDOWN: events.append( WindowEvent( KEY_DOWN.get(event.key.keysym.sym, WindowEvent.PASS))) elif event.type == sdl2.SDL_KEYUP: events.append( WindowEvent(KEY_UP.get(event.key.keysym.sym, WindowEvent.PASS))) elif event.type == sdl2.SDL_WINDOWEVENT: if event.window.windowID == 1: if event.window.event == sdl2.SDL_WINDOWEVENT_FOCUS_LOST: events.append(WindowEvent(WindowEvent.WINDOW_UNFOCUS)) elif event.window.event == sdl2.SDL_WINDOWEVENT_FOCUS_GAINED: events.append(WindowEvent(WindowEvent.WINDOW_FOCUS)) elif event.type == sdl2.SDL_MOUSEWHEEL: events.append( WindowEventMouse(WindowEvent._INTERNAL_MOUSE, window_id=event.motion.windowID, mouse_scroll_x=event.wheel.x, mouse_scroll_y=event.wheel.y)) elif event.type == sdl2.SDL_MOUSEMOTION or event.type == sdl2.SDL_MOUSEBUTTONUP: mouse_button = -1 if event.type == sdl2.SDL_MOUSEBUTTONUP: if event.button.button == sdl2.SDL_BUTTON_LEFT: mouse_button = 0 elif event.button.button == sdl2.SDL_BUTTON_RIGHT: mouse_button = 1 events.append( WindowEventMouse(WindowEvent._INTERNAL_MOUSE, window_id=event.motion.windowID, mouse_x=event.motion.x, mouse_y=event.motion.y, mouse_button=mouse_button)) elif event.type == sdl2.SDL_CONTROLLERDEVICEADDED: _sdlcontroller = sdl2.SDL_GameControllerOpen(event.cdevice.which) elif event.type == sdl2.SDL_CONTROLLERDEVICEREMOVED: sdl2.SDL_GameControllerClose(_sdlcontroller) elif event.type == sdl2.SDL_CONTROLLERBUTTONDOWN: events.append( WindowEvent( CONTROLLER_DOWN.get(event.cbutton.button, WindowEvent.PASS))) elif event.type == sdl2.SDL_CONTROLLERBUTTONUP: events.append( WindowEvent( CONTROLLER_UP.get(event.cbutton.button, WindowEvent.PASS))) return events
def close(self): sdl2.SDL_GameControllerClose(self._sdl_controller) self._sdl_controller = None self._sdl_joystick = None self._sdl_joystick_id = None self._connected = False
def close(self): sdl2.SDL_GameControllerClose(self.sdl_controller) self.on_controller_disconnected()