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

Simplify c-side of events sub-module. (Part 1) #3000

Open
wants to merge 19 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion buildconfig/Setup.Android.SDL2.in
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ base src_c/base.c $(SDL) $(DEBUG)
color src_c/color.c $(SDL) $(DEBUG)
constants src_c/constants.c $(SDL) $(DEBUG)
display src_c/display.c $(SDL) $(DEBUG)
event src_c/event.c $(SDL) $(DEBUG)
_event src_c/_event.c $(SDL) $(DEBUG)
key src_c/key.c $(SDL) $(DEBUG)
mouse src_c/mouse.c $(SDL) $(DEBUG)
rect src_c/rect.c src_c/pgcompat_rect.c $(SDL) $(DEBUG)
Expand Down
2 changes: 1 addition & 1 deletion buildconfig/Setup.Emscripten.SDL2.in
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ controller src_c/void.c
controller_old src_c/void.c
display src_c/void.c
draw src_c/void.c
event src_c/void.c
_event src_c/void.c
font src_c/void.c
gfxdraw src_c/void.c
joystick src_c/void.c
Expand Down
2 changes: 1 addition & 1 deletion buildconfig/Setup.SDL2.in
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ base src_c/base.c $(SDL) $(DEBUG)
color src_c/color.c $(SDL) $(DEBUG)
constants src_c/constants.c $(SDL) $(DEBUG)
display src_c/display.c $(SDL) $(DEBUG)
event src_c/event.c $(SDL) $(DEBUG)
_event src_c/_event.c $(SDL) $(DEBUG)
key src_c/key.c $(SDL) $(DEBUG)
mouse src_c/mouse.c $(SDL) $(DEBUG)
rect src_c/rect.c src_c/pgcompat_rect.c $(SDL) $(DEBUG)
Expand Down
29 changes: 29 additions & 0 deletions buildconfig/stubs/pygame/_event.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
from typing import (
Any,
List,
Optional,
Union,
Type,
)

from .typing import SequenceLike, EventLike

_EventTypes = Union[int, SequenceLike[int]]

def pump() -> None: ...
def get(
eventtype: Optional[_EventTypes] = None,
pump: Any = True,
exclude: Optional[_EventTypes] = None,
) -> List[EventLike]: ...
def poll() -> EventLike: ...
def wait(timeout: int = 0) -> EventLike: ...
def peek(eventtype: Optional[_EventTypes] = None, pump: Any = True) -> bool: ...
def clear(eventtype: Optional[_EventTypes] = None, pump: Any = True) -> None: ...
def set_blocked(type: Optional[_EventTypes], /) -> None: ...
def set_allowed(type: Optional[_EventTypes], /) -> None: ...
def get_blocked(type: _EventTypes, /) -> bool: ...
def set_grab(grab: bool, /) -> None: ...
def get_grab() -> bool: ...
def post(event: EventLike, /) -> bool: ...
def register_event_class(cls: Type[EventLike]): ...
22 changes: 6 additions & 16 deletions buildconfig/stubs/pygame/event.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,12 @@ from typing import (
List,
Optional,
Union,
final,
)

from pygame.typing import SequenceLike
from pygame.typing import SequenceLike, EventLike

@final
class Event:
type: int
dict: Dict[str, Any]
__dict__: Dict[str, Any]
__hash__: None # type: ignore
def __init__(
self, type: int, dict: Dict[str, Any] = ..., **kwargs: Any
) -> None: ...
def __getattribute__(self, name: str) -> Any: ...
def __setattr__(self, name: str, value: Any) -> None: ...
def __delattr__(self, name: str) -> None: ...
def __bool__(self) -> bool: ...
class Event(EventLike):
...

_EventTypes = Union[int, SequenceLike[int]]

Expand All @@ -35,13 +23,15 @@ def poll() -> Event: ...
def wait(timeout: int = 0) -> Event: ...
def peek(eventtype: Optional[_EventTypes] = None, pump: Any = True) -> bool: ...
def clear(eventtype: Optional[_EventTypes] = None, pump: Any = True) -> None: ...
def event_name(type: int, /) -> str: ...
def event_name(type: int) -> str: ...
def set_blocked(type: Optional[_EventTypes], /) -> None: ...
def set_allowed(type: Optional[_EventTypes], /) -> None: ...
def get_blocked(type: _EventTypes, /) -> bool: ...
def set_grab(grab: bool, /) -> None: ...
def get_grab() -> bool: ...
def post(event: Event, /) -> bool: ...
def custom_type() -> int: ...
def init(): ...
def quit(): ...

EventType = Event
21 changes: 20 additions & 1 deletion buildconfig/stubs/pygame/typing.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ __all__ = [

import sys
from abc import abstractmethod
from typing import IO, Callable, Tuple, Union, TypeVar, Protocol
from typing import IO, Any, Callable, Dict, Optional, Tuple, Union, TypeVar, Protocol

from pygame.color import Color
from pygame.rect import Rect, FRect
Expand Down Expand Up @@ -72,6 +72,25 @@ RectLike = Union[
]


class EventLike(Protocol):
# __dict__: Dict[str, Any]
def __init__(
self, type: int, dict: Optional[Dict[str, Any]] = None, **kwargs: Any
) -> None: ...
def __new__(cls, *args, **kwargs) -> "EventLike": ...
def __getattribute__(self, name: str) -> Any: ...
def __setattr__(self, name: str, value: Any) -> None: ...
def __delattr__(self, name: str) -> None: ...
def __int__(self) -> int: ...
def __bool__(self) -> bool: ...
def __eq__(self, other) -> bool: ...

@property
def type(self) -> int: ...
@property
def dict(self) -> Dict[str, Any]: ...


# cleanup namespace
del (
sys,
Expand Down
38 changes: 32 additions & 6 deletions docs/reST/c_api/event.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,32 +13,58 @@ The extension module :py:mod:`pygame.event`.

Header file: src_c/include/pygame.h

.. c:type:: pgEventData

.. c:type:: pgEventObject

The :py:class:`pygame.event.EventType` object C struct.
Struct holding information about the event object.

.. c:member:: int type

The event type code.

.. c:type:: pgEvent_Type
.. c:member:: PyObject* dict

Dict object of the event, might be NULL.

.. c:function:: PyObject* pgEvent_GetType(void)

Return a python class that is currently set to be the event class

The pygame event object type :py:class:`pygame.event.EventType`.
If the class is not known at the time (called before ``pygame._event.register_event_class``)
this function will return NULL and set the error.

.. c:function:: int pgEvent_Check(PyObject *x)

Return true if *x* is a pygame event instance

Will return false if *x* is a subclass of event.
This is a macro. No check is made that *x* is not ``NULL``.
Will return -1 if python error is set while checking.
No check is made that *x* is not ``NULL``.

.. c:function:: PyObject* pgEvent_New(SDL_Event *event)

Return a new pygame event instance for the SDL *event*.
If *event* is ``NULL`` then create an empty event object.
On failure raise a Python exception and return ``NULL``.

.. c:function:: PyObject* pgEvent_FromEventData(pgEventData)

Return an event object constructed from pgEventData struct.

On error returns NULL and sets python exception.

.. c:function:: pgEventData pgEvent_GetEventData(PyObject *)

Return a pgEventData struct containing information about event extracted from the python object.

Beware: on error this doesn't retun any special sentiel value if error ocurred, only sets python exception, so use ``PyErr_Ocurred()`` for error checking.
Remember to call :c:func:`pgEvent_FreeEventData` after usage to avoid memory leaks.

.. c:function:: void pgEvent_FreeEventData(pgEventData)

Free resources held by pgEventData (decrefs dict).

.. ## pgEvent_FreeEventData ##

.. c:function:: char* pgEvent_GetKeyDownInfo(void)

Return an array of bools (using char) of length SDL_NUM_SCANCODES
Expand Down
Loading
Loading