Skip to content

Commit

Permalink
Merge pull request #2056 from zoldalma999/controller-c
Browse files Browse the repository at this point in the history
Port the controller module to C.
  • Loading branch information
MyreMylar authored Aug 30, 2024
2 parents e01a44e + 4eff7e1 commit 11317c6
Show file tree
Hide file tree
Showing 14 changed files with 653 additions and 96 deletions.
2 changes: 2 additions & 0 deletions buildconfig/Setup.Emscripten.SDL2.in
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,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 @@ -66,6 +67,7 @@ window src_c/void.c
geometry 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 @@ -35,6 +35,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 @@ -27,7 +27,3 @@ pygame\.pypm
pygame\._sdl2\.mixer
pygame\.sysfont.*
pygame\.docs.*

# 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) -> int: ...
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.

.. 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 @@ -152,9 +151,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

0 comments on commit 11317c6

Please sign in to comment.