From d0c878fe0e496083a1e892795c23a1ac939714a9 Mon Sep 17 00:00:00 2001 From: David Robillard Date: Wed, 27 Aug 2014 23:32:26 +0000 Subject: Event-based dispatch. --- pugl/pugl_internal.h | 115 +++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 85 insertions(+), 30 deletions(-) (limited to 'pugl/pugl_internal.h') diff --git a/pugl/pugl_internal.h b/pugl/pugl_internal.h index 3db08f7..32d31bf 100644 --- a/pugl/pugl_internal.h +++ b/pugl/pugl_internal.h @@ -24,27 +24,18 @@ If you are copying the pugl code into your source tree, the following symbols can be defined to tweak pugl behaviour: - PUGL_GRAB_FOCUS: Work around reparent keyboard issues by grabbing focus. - PUGL_VERBOSE: Print graphics information to console. PUGL_HAVE_CAIRO: Include Cairo support code. PUGL_HAVE_GL: Include OpenGL support code. */ -#include "pugl.h" - -#ifdef PUGL_VERBOSE -# include -# define PUGL_LOG(str) fprintf(stderr, "pugl: " str) -# define PUGL_LOGF(fmt, ...) fprintf(stderr, "pugl: " fmt, __VA_ARGS__) -#else -# define PUGL_LOG(str) -# define PUGL_LOGF(fmt, ...) -#endif +#include "pugl/pugl.h" +#include "pugl/event.h" typedef struct PuglInternalsImpl PuglInternals; struct PuglViewImpl { PuglHandle handle; + PuglEventFunc eventFunc; PuglCloseFunc closeFunc; PuglDisplayFunc displayFunc; PuglKeyboardFunc keyboardFunc; @@ -71,8 +62,6 @@ struct PuglViewImpl { PuglInternals* puglInitInternals(); -void puglDefaultReshape(PuglView* view, int width, int height); - PuglView* puglInit(int* pargc, char** argv) { @@ -143,27 +132,15 @@ puglGetModifiers(PuglView* view) } void -puglDefaultReshape(PuglView* view, int width, int height) +puglIgnoreKeyRepeat(PuglView* view, bool ignore) { -#ifdef PUGL_HAVE_GL - glMatrixMode(GL_PROJECTION); - glLoadIdentity(); - glOrtho(0, width, height, 0, 0, 1); - glViewport(0, 0, width, height); - - glMatrixMode(GL_MODELVIEW); - glLoadIdentity(); -#endif - return; - - // unused - (void)view; + view->ignoreKeyRepeat = ignore; } void -puglIgnoreKeyRepeat(PuglView* view, bool ignore) +puglSetEventFunc(PuglView* view, PuglEventFunc eventFunc) { - view->ignoreKeyRepeat = ignore; + view->eventFunc = eventFunc; } void @@ -213,3 +190,81 @@ puglSetSpecialFunc(PuglView* view, PuglSpecialFunc specialFunc) { view->specialFunc = specialFunc; } + +void +puglEnterContext(PuglView* view); + +void +puglLeaveContext(PuglView* view, bool flush); + +static void +puglDispatchEvent(PuglView* view, const PuglEvent* event) +{ + if (view->eventFunc) { + view->eventFunc(view, event); + } + + switch (event->type) { + case PUGL_CONFIGURE: + puglEnterContext(view); + view->width = event->configure.width; + view->height = event->configure.height; + if (view->reshapeFunc) { + view->reshapeFunc(view, view->width, view->height); + } + puglLeaveContext(view, false); + break; + case PUGL_EXPOSE: + if (event->expose.count == 0) { + puglEnterContext(view); + if (view->displayFunc) { + view->displayFunc(view); + } + view->redisplay = false; + puglLeaveContext(view, true); + } + break; + case PUGL_MOTION_NOTIFY: + view->event_timestamp_ms = event->motion.time; + view->mods = event->motion.state; + if (view->motionFunc) { + view->motionFunc(view, event->motion.x, event->motion.y); + } + break; + case PUGL_SCROLL: + if (view->scrollFunc) { + view->scrollFunc(view, + event->scroll.x, event->scroll.y, + event->scroll.dx, event->scroll.dy); + } + break; + case PUGL_BUTTON_PRESS: + case PUGL_BUTTON_RELEASE: + view->event_timestamp_ms = event->button.time; + view->mods = event->button.state; + if (view->mouseFunc) { + view->mouseFunc(view, + event->button.button, + event->type == PUGL_BUTTON_PRESS, + event->button.x, + event->button.y); + } + break; + case PUGL_KEY_PRESS: + case PUGL_KEY_RELEASE: + view->event_timestamp_ms = event->key.time; + view->mods = event->key.state; + if (event->key.special && view->specialFunc) { + view->specialFunc(view, + event->type == PUGL_KEY_PRESS, + event->key.special); + } else if (event->key.character && view->keyboardFunc) { + view->keyboardFunc(view, + event->type == PUGL_KEY_PRESS, + event->key.character); + } + break; + default: + break; + } +} -- cgit v1.2.3