def set_cursor(size, hotspot, xormasks, andmasks): """ set_cursor(size, hotspot, xormasks, andmasks) -> None set the image for the system mouse cursor """ check_video() spotx, spoty = int(hotspot[0]), int(hotspot[1]) w, h = int(size[0]), int(size[1]) if w % 8 != 0: raise ValueError("Cursor width must be divisible by 8") if not hasattr(xormasks, '__iter__') or not hasattr(andmasks, '__iter__'): raise TypeError("xormask and andmask must be sequences") if len(xormasks) != w * h / 8.0 or len(andmasks) != w * h / 8.0: raise ValueError("bitmasks must be sized width*height/8") try: xordata = ffi.new('uint8_t[]', [int(m) for m in xormasks]) anddata = ffi.new('uint8_t[]', [int(andmasks[i]) for i in range(len(xormasks))]) except (ValueError, TypeError): raise TypeError("Invalid number in mask array") except OverflowError: raise TypeError("Number in mask array is larger than 8 bits") cursor = sdl.SDL_CreateCursor(xordata, anddata, w, h, spotx, spoty) if not cursor: raise SDLError.from_sdl_error() lastcursor = sdl.SDL_GetCursor() sdl.SDL_SetCursor(cursor) sdl.SDL_FreeCursor(lastcursor)
def get_repeat(): """ get_repeat() -> (delay, interval) see how held keys are repeated """ check_video() delay, interval = ffi.new('int*'), ffi.new('int*') sdl.SDL_GetKeyRepeat(delay, interval) return (delay[0], interval[0])
def get_rel(): """ get_rel() -> (x, y) get the amount of mouse movement """ check_video() x, y = ffi.new('int*'), ffi.new('int*') sdl.SDL_GetRelativeMouseState(x, y) return x[0], y[0]
def get_pressed(): """ get_pressed() -> (button1, button2, button3) get the state of the mouse buttons """ check_video() state = sdl.SDL_GetMouseState(ffi.NULL, ffi.NULL) return (int((state & sdl._pygame_SDL_BUTTON(1)) != 0), int((state & sdl._pygame_SDL_BUTTON(2)) != 0), int((state & sdl._pygame_SDL_BUTTON(3)) != 0))
def set_repeat(delay=0, interval=0): """ set_repeat() -> None control how held keys are repeated """ check_video() if delay and not interval: interval = delay if sdl.SDL_EnableKeyRepeat(delay, interval) == -1: raise SDLError.from_sdl_error()
def set_pos((x, y)): """ set_pos([x, y]) -> None set the mouse cursor position """ check_video() try: x = int(x) y = int(y) sdl.SDL_WarpMouse(x, y) except (ValueError, TypeError): raise "invalid position argument for set_pos"
def get_pressed(): """ get_pressed() -> bools get the state of all keyboard buttons """ check_video() num_keys = ffi.new('int*') key_state = sdl.SDL_GetKeyState(num_keys) num_keys = num_keys[0] if not key_state or not num_keys: return None return [key_state[i] for i in range(num_keys)]
def get_cursor(): """ get_cursor() -> (size, hotspot, xormasks, andmasks) get the image for the system mouse cursor """ check_video() cursor = sdl.SDL_GetCursor() if not cursor: raise SDLError.from_sdl_error() w = cursor.area.w h = cursor.area.h size = w * h / 8 xordata = [int(cursor.data[i]) for i in range(size)] anddata = [int(cursor.mask[i]) for i in range(size)] return (w, h), (cursor.hot_x, cursor.hot_y), xordata, anddata
def post(event): """post(Event): return None place a new event on the queue""" # SDL requires video to be initialised before PushEvent does the right thing check_video() is_blocked = sdl.SDL_EventState(event.type, sdl.SDL_QUERY) == sdl.SDL_IGNORE if is_blocked: raise RuntimeError("event post blocked for %s" % event_name(event.type)) sdl_event = ffi.new("SDL_Event *") sdl_event.type = event.type sdl_event.user.code = _USEROBJECT_CHECK1 sdl_event.user.data1 = _USEROBJECT_CHECK2 sdl_event.user.data2 = ffi.cast("void*", sdl_event) _user_events[sdl_event] = event if sdl.SDL_PushEvent(sdl_event) == -1: raise SDLError.from_sdl_error()
def post(event): """post(Event): return None place a new event on the queue""" # SDL requires video to be initialised before PushEvent does the right thing check_video() is_blocked = sdl.SDL_EventState(event.type, sdl.SDL_QUERY) == sdl.SDL_IGNORE if is_blocked: # Silently drop blocked events, since that's what pygame does # (maybe worth logging somehow?) return None sdl_event = ffi.new("SDL_Event *") sdl_event.type = event.type sdl_event.user.code = _USEROBJECT_CHECK1 sdl_event.user.data1 = _USEROBJECT_CHECK2 sdl_event.user.data2 = ffi.cast("void*", sdl_event) _user_events[sdl_event] = event if sdl.SDL_PushEvent(sdl_event) == -1: raise SDLError.from_sdl_error()
def get_pos(): check_video() x = ffi.new("int[1]") y = ffi.new("int[1]") sdl.SDL_GetMouseState(x, y) return x[0], y[0]
def get_focused(): """ get_focused() -> bool true if the display is receiving keyboard input from the system """ check_video() return (sdl.SDL_GetAppState() & sdl.SDL_APPINPUTFOCUS) != 0
def get_focused(): """ get_focused() -> bool check if the display is receiving mouse input """ check_video() return int(sdl.SDL_GetAppState() & sdl.SDL_APPMOUSEFOCUS != 0)
def get_mods(): """ get_mods() -> int determine which modifier keys are being held """ check_video() return sdl.SDL_GetModState()
def set_mods(mod_int): """ set_mods(int) -> None temporarily set which modifier keys are pressed """ check_video() sdl.SDL_SetModState(int(mod_int))
def set_visible(toggle): check_video() if not isinstance(toggle, int): raise TypeError("expected int, got %s" % (toggle,)) return sdl.SDL_ShowCursor(toggle)
def set_visible(toggle): check_video() if not isinstance(toggle, int): raise TypeError("expected int, got %s" % (toggle, )) return sdl.SDL_ShowCursor(toggle)