Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Port the controller module to C. #2056

Merged
merged 11 commits into from
Aug 30, 2024
2 changes: 2 additions & 0 deletions buildconfig/Setup.Emscripten.SDL2.in
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ image src_c/void.c
base src_c/void.c
bufferproxy src_c/void.c
color src_c/void.c
controller src_c/void.c
controller_old src_c/void.c
display src_c/void.c
draw src_c/void.c
Expand All @@ -63,6 +64,7 @@ rwobject src_c/void.c
system src_c/void.c

#_sdl2.controller src_c/_sdl2/controller.c $(SDL) $(DEBUG) -Isrc_c
_sdl2.controller src_c/void.c
_sdl2.controller_old src_c/void.c

#_sdl2.touch src_c/_sdl2/touch.c $(SDL) $(DEBUG) -Isrc_c
Expand Down
1 change: 1 addition & 0 deletions buildconfig/Setup.SDL2.in
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ _sdl2.audio src_c/_sdl2/audio.c $(SDL) $(DEBUG) -Isrc_c
_sdl2.video src_c/_sdl2/video.c src_c/pgcompat.c $(SDL) $(DEBUG) -Isrc_c
_sdl2.mixer src_c/_sdl2/mixer.c $(SDL) $(MIXER) $(DEBUG) -Isrc_c
_sdl2.touch src_c/_sdl2/touch.c $(SDL) $(DEBUG) -Isrc_c
_sdl2.controller src_c/_sdl2/controller.c $(SDL) $(DEBUG) -Isrc_c
_sdl2.controller_old src_c/_sdl2/controller_old.c $(SDL) $(DEBUG) -Isrc_c

GFX = src_c/SDL_gfx/SDL_gfxPrimitives.c
Expand Down
4 changes: 0 additions & 4 deletions buildconfig/stubs/mypy_allow_list.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,3 @@ pygame\.pkgdata
pygame\.pypm
pygame\._sdl2\.mixer
pygame\.sysfont.*

# temporarily ignore the controller module while it is being converted
# to C
pygame\._sdl2\.controller
31 changes: 31 additions & 0 deletions buildconfig/stubs/pygame/_sdl2/controller.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from typing import final
from pygame.joystick import JoystickType

def init() -> None: ...
def get_init() -> bool: ...
def quit() -> None: ...
def is_controller(device_index: int) -> bool: ...
def get_count() -> int: ...
@final
class Controller:
def __new__(cls, device_index: int) -> Controller: ...
def __init__(self, device_index: int) -> None: ...
@classmethod
def from_joystick(cls, joystick: JoystickType) -> Controller: ...
def get_init(self) -> bool: ...
def init(self) -> None: ...
def quit(self) -> None: ...
@property
def id(self) -> int: ...
@property
def name(self) -> str: ...
def attached(self) -> bool: ...
def as_joystick(self) -> JoystickType: ...
def get_axis(self, axis: int) -> float: ...
def get_button(self, button: int) -> bool: ...
def get_mapping(self) -> dict: ...
def set_mapping(self, mapping: dict) -> int: ...
def rumble(self, ___) -> bool: ...
def stop_rumble(
self, low_frequency: float, high_frequency: float, duration: int
) -> bool: ...
8 changes: 8 additions & 0 deletions docs/reST/ref/sdl2_controller.rst
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,14 @@ events related to controllers.
``pygame._sdl2.controller.from_joystick``. Controllers are
initialized on creation.

.. method:: init

| :sl:`Initialize the Controller`
| :sg:`init() -> None`

Initialize a controller object. This should not be used much, since
Controllers are initialised on creation.

MyreMylar marked this conversation as resolved.
Show resolved Hide resolved
.. method:: quit

| :sl:`uninitialize the Controller`
Expand Down
4 changes: 0 additions & 4 deletions examples/eventlist.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@

- press keys up an down to see events.
- you can see joystick events if any are plugged in.
- press "c" to toggle events generated by controllers.
"""

import pygame
Expand Down Expand Up @@ -160,9 +159,6 @@ def main():
last_key = e.key
if e.key == pygame.K_h:
draw_usage_in_history(history, usage)
if SDL2 and e.key == pygame.K_c:
current_state = pygame._sdl2.controller.get_eventstate()
pygame._sdl2.controller.set_eventstate(not current_state)

if e.type == pygame.MOUSEBUTTONDOWN and e.button == 1:
pygame.event.set_grab(not pygame.event.get_grab())
Expand Down
Loading