handleEvents :: AppController -> IO (Bool,AppController)
handleEvents app = do
  event <- pollEvent
  case event of
    NoEvent -> nochange
    Quit    -> quit
    KeyUp k -> case symKey k of
      SDLK_ESCAPE -> quit
      SDLK_SPACE  -> loopback $ app { appGUIVisible = toggle (appGUIVisible app) }
      SDLK_RETURN -> alter $ updateModel >=> updateModelView
      SDLK_MINUS  -> loopback $ changeMaxIter (\a -> a-50) app -- TODO: woah, pretty weird the - operator...
      SDLK_PLUS   -> loopback $ changeMaxIter (+50) app
      _           -> loopback app
    KeyDown k -> case symKey k of
      SDLK_LEFTPAREN  -> alter $ return . changeZoomWindowSize (/1.05) >=> updateGUIZoomWindow
      SDLK_RIGHTPAREN -> alter $ return . changeZoomWindowSize (*1.05) >=> updateGUIZoomWindow
      _               -> loopback app
    MouseButtonUp x y b -> case b of
      ButtonLeft -> alter $ (onMouseButtonLeft (fromIntegral x) (fromIntegral y) >=> updateModel >=> updateModelView)
      _          -> loopback app
    _ -> loopback app
 where quit     = return (False,app)
       nochange = return (True,app)
       loopback = handleEvents
       alter f  = f app >>= loopback
handleEvents :: AppController -> IO (Bool,AppController)
handleEvents app = do
  event <- pollEvent
  case event of
    NoEvent -> nochange
    Quit    -> quit
    KeyUp k -> case symKey k of
      SDLK_ESCAPE -> quit
      SDLK_SPACE  -> loopback $ app { appGUIVisible = toggle (appGUIVisible app) }
      SDLK_RETURN -> alter $ updateModel >=> updateModelView
      SDLK_MINUS  -> loopback $ changeMaxIter (\a -> a-50) app -- TODO: woah, pretty weird the - operator...
      SDLK_PLUS   -> loopback $ changeMaxIter (+50) app
      _           -> loopback app
    KeyDown k -> case symKey k of
      SDLK_LEFTPAREN  -> alter $ return . changeZoomWindowSize (/1.05) >=> updateGUIZoomWindow
      SDLK_RIGHTPAREN -> alter $ return . changeZoomWindowSize (*1.05) >=> updateGUIZoomWindow
      _               -> loopback app
    MouseButtonUp x y b -> case b of
      ButtonLeft -> alter $ (onMouseButtonLeft (fromIntegral x) (fromIntegral y) >=> updateModel >=> updateModelView)
      _          -> loopback app
    _ -> loopback app
 where quit     = return (False,app)
       nochange = return (True,app)
       loopback = handleEvents
       alter f  = f app >>= loopback
handleEvents app = do
  event <- pollEvent
  case event of
    NoEvent -> nochange
    Quit    -> quit
    KeyUp k -> case symKey k of
      SDLK_ESCAPE -> quit
      SDLK_SPACE  -> loopback $ app { appGUIVisible = toggle (appGUIVisible app) }
      SDLK_RETURN -> alter $ updateModel >=> updateModelView
      SDLK_MINUS  -> loopback $ changeMaxIter (\a -> a-50) app -- TODO: woah, pretty weird the - operator...
      SDLK_PLUS   -> loopback $ changeMaxIter (+50) app
      _           -> loopback app
    KeyDown k -> case symKey k of
      SDLK_LEFTPAREN  -> alter $ return . changeZoomWindowSize (/1.05) >=> updateGUIZoomWindow
      SDLK_RIGHTPAREN -> alter $ return . changeZoomWindowSize (*1.05) >=> updateGUIZoomWindow
      _               -> loopback app
    MouseButtonUp x y b -> case b of
      ButtonLeft -> alter $ (onMouseButtonLeft (fromIntegral x) (fromIntegral y) >=> updateModel >=> updateModelView)
      _          -> loopback app
    _ -> loopback app
 where quit     = return (False,app)
       nochange = return (True,app)
       loopback = handleEvents
       alter f  = f app >>= loopback