From 4f31fc0ddc8a165546fe15a0938b0a1676473b29 Mon Sep 17 00:00:00 2001 From: James Turk Date: Sun, 27 Mar 2011 01:46:06 -0400 Subject: [PATCH] events\! --- TODO | 6 - csdl/__init__.py | 15 +- csdl/events.py | 311 +++++++++++++++++++++++++++++++++++++- csdl/internal.py | 14 ++ demo/random_rectangles.py | 8 + 5 files changed, 332 insertions(+), 22 deletions(-) diff --git a/TODO b/TODO index 0581067..9d00064 100644 --- a/TODO +++ b/TODO @@ -1,9 +1,4 @@ -Soon - * flesh out Window - * flesh out Renderer - Someday - * Point/Rect * Event Support * have Window pass-through to Renderer @@ -15,7 +10,6 @@ Maybe * Haptic * Audio * Timers - * SDL_GetPlatform, CPU detection, Byte Order, SDL_GetPowerInfo Probably Not * Threads diff --git a/csdl/__init__.py b/csdl/__init__.py index d80fd10..23c8ff1 100644 --- a/csdl/__init__.py +++ b/csdl/__init__.py @@ -1,5 +1,5 @@ import ctypes -from .internal import _SDL, errcheck +from .internal import _SDL, errcheck, Version # Constants class INIT(object): @@ -42,19 +42,6 @@ def quit(): ##### Version Info -class Version(ctypes.Structure): - _fields_ = ( - ("major", ctypes.c_uint8), - ("minor", ctypes.c_uint8), - ("patch", ctypes.c_uint8) - ) - - def __str__(self): - return '{0}.{1}.{2}'.format(self.major, self.minor, self.patch) - - def __repr__(self): - return 'Version({0},{1},{2})'.format(self.major, self.minor, - self.patch) def get_version(): v = Version() diff --git a/csdl/events.py b/csdl/events.py index 16c329e..8be263c 100644 --- a/csdl/events.py +++ b/csdl/events.py @@ -1,7 +1,7 @@ import ctypes -from .internal import _SDL +from .internal import _SDL, Version -class Event(object): +class EventType(int): FIRSTEVENT = 0 QUIT = 0x100 @@ -47,4 +47,311 @@ class Event(object): USEREVENT = 0x8000 LASTEVENT = 0xFFFF +# a few typedefs +Keycode = ctypes.c_int32 +Scancode = ctypes.c_int32 # enum, not sure about this one +GestureID = ctypes.c_int64 +TouchID = ctypes.c_int64 +FingerID = ctypes.c_int64 +class SysWMType(int): + UNKNOWN = 0 + WINDOWS = 1 + X11 = 2 + DIRECTFB = 2 + COCOA = 3 + UIKIT = 4 + +# TODO: see what can be done to make SysWMmsg work +class _SysWMUnion(ctypes.Union): + _fields_ = ( + ('dummy', ctypes.c_int), + ) + +class SysWMmsg(ctypes.Structure): + _fields_ = ( + ('version', Version), + ('subsystem', ctypes.c_int), + ('msg', _SysWMUnion), + ) + +class Keysym(ctypes.Structure): + _fields_ = ( + ('scancode', Scancode), + ('keycode', Keycode), + ('mod', ctypes.c_uint16), + ('unicode', ctypes.c_uint32), + ) + +class WindowEvent(ctypes.Structure): + _fields_ = ( + ('type', ctypes.c_uint32), + ('window_id', ctypes.c_uint32), + ('event', ctypes.c_uint8), + ('_padding1', ctypes.c_uint8), + ('_padding2', ctypes.c_uint8), + ('_padding3', ctypes.c_uint8), + ('data1', ctypes.c_int), + ('data2', ctypes.c_int), + ) + +class KeyboardEvent(ctypes.Structure): + _fields_ = ( + ('type', ctypes.c_uint32), + ('window_id', ctypes.c_uint32), + ('state', ctypes.c_uint8), + ('repeat', ctypes.c_uint8), + ('_padding2', ctypes.c_uint8), + ('_padding3', ctypes.c_uint8), + ('keysym', Keysym), + ) + +class TextEditingEvent(ctypes.Structure): + _fields_ = ( + ('type', ctypes.c_uint32), + ('window_id', ctypes.c_uint32), + ('text', ctypes.c_char*32), + ('start', ctypes.c_int), + ('length', ctypes.c_int), + ) + +class TextInputEvent(ctypes.Structure): + _fields_ = ( + ('type', ctypes.c_uint32), + ('window_id', ctypes.c_uint32), + ('text', ctypes.c_char*32), + ) + +class MouseMotionEvent(ctypes.Structure): + _fields_ = ( + ('type', ctypes.c_uint32), + ('window_id', ctypes.c_uint32), + ('state', ctypes.c_uint8), + ('_padding1', ctypes.c_uint8), + ('_padding2', ctypes.c_uint8), + ('_padding3', ctypes.c_uint8), + ('x', ctypes.c_int), + ('y', ctypes.c_int), + ('x_rel', ctypes.c_int), + ('y_rel', ctypes.c_int), + ) + +class MouseButtonEvent(ctypes.Structure): + _fields_ = ( + ('type', ctypes.c_uint32), + ('window_id', ctypes.c_uint32), + ('button', ctypes.c_uint8), + ('istate', ctypes.c_uint8), + ('_padding1', ctypes.c_uint8), + ('_padding2', ctypes.c_uint8), + ('x', ctypes.c_int), + ('y', ctypes.c_int), + ) + +class MouseWheelEvent(ctypes.Structure): + _fields_ = ( + ('type', ctypes.c_uint32), + ('window_id', ctypes.c_uint32), + ('x', ctypes.c_int), + ('y', ctypes.c_int), + ) + +class JoyAxisEvent(ctypes.Structure): + _fields_ = ( + ('type', ctypes.c_uint32), + ('which', ctypes.c_uint8), + ('axis', ctypes.c_uint8), + ('_padding1', ctypes.c_uint8), + ('_padding2', ctypes.c_uint8), + ('value', ctypes.c_int), + ) + +class JoyBallEvent(ctypes.Structure): + _fields_ = ( + ('type', ctypes.c_uint32), + ('which', ctypes.c_uint8), + ('ball', ctypes.c_uint8), + ('_padding1', ctypes.c_uint8), + ('_padding2', ctypes.c_uint8), + ('xrel', ctypes.c_int), + ('yrel', ctypes.c_int), + ) + +class JoyHatEvent(ctypes.Structure): + _fields_ = ( + ('type', ctypes.c_uint32), + ('which', ctypes.c_uint8), + ('hat', ctypes.c_uint8), + ('value', ctypes.c_uint8), + ('_padding1', ctypes.c_uint8), + ) + +class JoyButtonEvent(ctypes.Structure): + _fields_ = ( + ('type', ctypes.c_uint32), + ('which', ctypes.c_uint8), + ('button', ctypes.c_uint8), + ('state', ctypes.c_uint8), + ('_padding1', ctypes.c_uint8), + ) + +class TouchFingerEvent(ctypes.Structure): + _fields_ = ( + ('type', ctypes.c_uint32), + ('window_id', ctypes.c_uint32), + ('touch_id', TouchID), + ('finger_id', FingerID), + ('state', ctypes.c_uint8), + ('_padding1', ctypes.c_uint8), + ('_padding2', ctypes.c_uint8), + ('_padding3', ctypes.c_uint8), + ('x', ctypes.c_uint16), + ('y', ctypes.c_uint16), + ('dx', ctypes.c_int16), + ('dy', ctypes.c_int16), + ('pressure', ctypes.c_uint16), + ) + +class TouchFingerEvent(ctypes.Structure): + _fields_ = ( + ('type', ctypes.c_uint32), + ('window_id', ctypes.c_uint32), + ('touch_id', TouchID), + ('state', ctypes.c_uint8), + ('button', ctypes.c_uint8), + ('_padding1', ctypes.c_uint8), + ('_padding2', ctypes.c_uint8), + ) + +class MultiGestureEvent(ctypes.Structure): + _fields_ = ( + ('type', ctypes.c_uint32), + ('window_id', ctypes.c_uint32), + ('touch_id', TouchID), + ('d_theta', ctypes.c_float), + ('d_dist', ctypes.c_float), + ('x', ctypes.c_float), + ('y', ctypes.c_float), + ('num_fingers', ctypes.c_uint16), + ('padding', ctypes.c_uint16), + ) + +class DollarGestureEvent(ctypes.Structure): + _fields_ = ( + ('type', ctypes.c_uint32), + ('window_id', ctypes.c_uint32), + ('touch_id', TouchID), + ('gesture_id', GestureID), + ('num_fingers', ctypes.c_uint32), + ('error', ctypes.c_float), + # these are commented out in the header, maybe coming soon + #('x', ctypes.c_float), + #('y', ctypes.c_float), + ) + +class QuitEvent(ctypes.Structure): + _fields_ = ( + ('type', ctypes.c_uint32), + ) + +class UserEvent(ctypes.Structure): + _fields_ = ( + ('type', ctypes.c_uint32), + ('window_id', ctypes.c_uint32), + ('code', ctypes.c_int), + ('data1', ctypes.c_void_p), + ('data2', ctypes.c_void_p), + ) + +class SysWMEvent(ctypes.Structure): + _fields_ = ( + ('type', ctypes.c_uint32), + ('msg', ctypes.POINTER(SysWMmsg)), + ) + +class ActiveEvent(ctypes.Structure): + _fields_ = ( + ('type', ctypes.c_uint32), + ('gain', ctypes.c_uint8), + ('state', ctypes.c_uint8), + ) + +class ResizeEvent(ctypes.Structure): + _fields_ = ( + ('types', ctypes.c_uint32), + ('w', ctypes.c_int), + ('h', ctypes.c_int), + ) + +class Event(ctypes.Union): + _fields_ = ( + ('type', ctypes.c_uint32), + ('window', WindowEvent), + ('key', KeyboardEvent), + ('edit', TextEditingEvent), + ('text', TextInputEvent), + ('motion', MouseMotionEvent), + ('button', MouseButtonEvent), + ('wheel', MouseWheelEvent), + ('jaxis', JoyAxisEvent), + ('jball', JoyBallEvent), + ('jbutton', JoyButtonEvent), + ('quit', QuitEvent), + ('user', UserEvent), + ('syswm', SysWMEvent), + ('tfinger', TouchFingerEvent), + #('tbutton', TouchButtonEvent), + ('mgesture', MultiGestureEvent), + ('dgesture', DollarGestureEvent), + ('active', ActiveEvent), + ('resize', ResizeEvent), + ) + +# not implemented: +# SDL_AddEventWatch,SDL_DelEventWatch - void* +# SDL_FilterEvents,SDL_SetEventFilter,SDL_GetEventFilter - void* + +def pump_events(): + _SDL.SDL_PumpEvents() + +def peep_events(events, num_events, action, min_type, max_type): + return errcheck(_SDL>SDL_PeepEvents(events, numevents, action, + min_type, max_type)) + +def has_event(type): + return _SDL.SDL_HasEvent(type) == 1 + +def has_events(min_type, max_type): + return _SDL.SDL_HasEvents(min_type, max_type) == 1 + +def flush_event(type): + _SDL.SDL_FlushEvent(type) + +def flush_events(min_type, max_type): + _SDL.SDL_FlushEvents(min_type, max_type) + +def poll_event(): + event = Event() + retval = _SDL.SDL_PollEvent(ctypes.byref(event)) + if retval: + return event + +def wait_event(timeout=0): + event = Event() + + if timeout: + retval = _SDL.SDL_WaitEvent(ctypes.byref(event)) + else: + retval = _SDL.SDL_WaitEventTimeout(ctypes.byref(event), timeout) + + if retval: + return event + +def push_event(event): + return errcheck(_SDL.SDL_PushEvent(ctypes.byref(event))) + +def event_state(type, state): + errcheck(_SDL.SDL_EventState(type, state)) + +def register_events(num_events): + return _SDL.SDL_RegisterEvents(num_events) diff --git a/csdl/internal.py b/csdl/internal.py index 56e2e5e..1d19062 100644 --- a/csdl/internal.py +++ b/csdl/internal.py @@ -11,3 +11,17 @@ def errcheck(result): raise SDLError(msg) return result +# moved here so events.SysWMmsg can get at it +class Version(ctypes.Structure): + _fields_ = ( + ("major", ctypes.c_uint8), + ("minor", ctypes.c_uint8), + ("patch", ctypes.c_uint8) + ) + + def __str__(self): + return '{0}.{1}.{2}'.format(self.major, self.minor, self.patch) + + def __repr__(self): + return 'Version({0},{1},{2})'.format(self.major, self.minor, + self.patch) diff --git a/demo/random_rectangles.py b/demo/random_rectangles.py index 49ff636..d935c63 100644 --- a/demo/random_rectangles.py +++ b/demo/random_rectangles.py @@ -1,5 +1,6 @@ from csdl import init, INIT from csdl.video import Window, Rect +from csdl.events import EventType, poll_event import random import time @@ -8,6 +9,13 @@ def main(): window = Window('test', 100, 100, 512, 512, 0) while True: + while True: + event = poll_event() + if not event: + break + else: + print event.type + window.renderer.set_draw_color(0,0,0,255) window.renderer.clear() window.renderer.set_draw_color(255,0,0,255)