Skip to content

Fix event polling #242

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

Merged
merged 1 commit into from
Mar 10, 2022
Merged
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
11 changes: 11 additions & 0 deletions cbits/sdlhelper.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
#include <string.h>
#include <stdlib.h>
#include "sdlhelper.h"

int SDLHelper_GetEventBufferSize() { return 64; }
SDL_Event *SDLHelper_GetEventBuffer() {
static SDL_Event *buffer = NULL;
if(buffer == NULL) {
/* leak an inconsequental amount of memory */
buffer = calloc(SDLHelper_GetEventBufferSize(), sizeof(SDL_Event));
}
return buffer;
}

void SDLHelper_JoystickGetDeviceGUID (int device_index, SDL_JoystickGUID *guid)
{
SDL_JoystickGUID t = SDL_JoystickGetDeviceGUID (device_index);
Expand Down
2 changes: 2 additions & 0 deletions include/sdlhelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#include <stddef.h>
#include "SDL.h"

int SDLHelper_GetEventBufferSize(void);
SDL_Event *SDLHelper_GetEventBuffer(void);
void SDLHelper_JoystickGetDeviceGUID (int device_index, SDL_JoystickGUID *guid);
void SDLHelper_JoystickGetGUID (SDL_Joystick *joystick, SDL_JoystickGUID *guid);
void SDLHelper_JoystickGetGUIDFromString (const char *pchGUID, SDL_JoystickGUID *guid);
Expand Down
20 changes: 15 additions & 5 deletions src/SDL/Event.hs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ import Data.Text (Text)
import Data.Typeable
import Foreign hiding (throwIfNeg_)
import Foreign.C
import Foreign.Marshal.Array
import GHC.Generics (Generic)
import SDL.Vect
import SDL.Input.Joystick
Expand Down Expand Up @@ -765,11 +766,20 @@ pollEvent =
-- Like 'pollEvent' this function should only be called in the OS thread which
-- set the video mode.
pollEvents :: MonadIO m => m [Event]
pollEvents =
do e <- pollEvent
case e of
Nothing -> return []
Just e' -> (e' :) <$> pollEvents
pollEvents = liftIO $ do
Raw.pumpEvents
peepAllEvents >>= mapM convertRaw where
peepAllEvents = do
numPeeped <- Raw.peepEvents
Raw.eventBuffer
Raw.eventBufferSize
Raw.SDL_GETEVENT
Raw.SDL_FIRSTEVENT
Raw.SDL_LASTEVENT
peeped <- peekArray (fromIntegral numPeeped) Raw.eventBuffer
if numPeeped == Raw.eventBufferSize -- are there more events to peep?
Copy link
Contributor Author

@hanst99 hanst99 Feb 9, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note this is unlikely in a typical scenario because I picked the buffer size to be pretty generous, unless there's a high frequency input device involved or
pollEvents is called very infrequently.

then (peeped ++) <$> peepAllEvents
else return peeped

-- | Run a monadic computation, accumulating over all known 'Event's.
--
Expand Down
7 changes: 6 additions & 1 deletion src/SDL/Raw/Event.hs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,9 @@ module SDL.Raw.Event (
gameControllerNameForIndex,
gameControllerOpen,
gameControllerUpdate,
isGameController
isGameController,
eventBuffer,
eventBufferSize
) where

import Control.Monad.IO.Class
Expand Down Expand Up @@ -235,6 +237,9 @@ foreign import ccall "SDL.h SDL_GameControllerOpen" gameControllerOpenFFI :: CIn
foreign import ccall "SDL.h SDL_GameControllerUpdate" gameControllerUpdateFFI :: IO ()
foreign import ccall "SDL.h SDL_IsGameController" isGameControllerFFI :: CInt -> IO Bool

foreign import ccall "sdlhelper.c SDLHelper_GetEventBufferSize" eventBufferSize :: CInt
foreign import ccall "sdlhelper.c SDLHelper_GetEventBuffer" eventBuffer :: Ptr Event

addEventWatch :: MonadIO m => EventFilter -> Ptr () -> m ()
addEventWatch v1 v2 = liftIO $ addEventWatchFFI v1 v2
{-# INLINE addEventWatch #-}
Expand Down