From ebd3e0efad2f537e4180bf6e13dfb88b2a6b9d7a Mon Sep 17 00:00:00 2001 From: falkTX Date: Mon, 17 Nov 2014 23:40:33 +0000 Subject: [PATCH] Start updating pugl --- dgl/Makefile | 4 +- dgl/src/Window.cpp | 1 + dgl/src/pugl/common.h | 121 +++++++++++++++++++ dgl/src/pugl/event.h | 217 +++++++++++++++++++++++++++++++++++ dgl/src/pugl/gl.h | 32 ++++++ dgl/src/pugl/glu.h | 32 ++++++ dgl/src/pugl/pugl.h | 153 +++++++++++------------- dgl/src/pugl/pugl_internal.h | 107 +++++++++++++++-- dgl/src/pugl/pugl_osx.m | 4 +- dgl/src/pugl/pugl_win.cpp | 2 +- dgl/src/pugl/pugl_x11.c | 22 +++- 11 files changed, 591 insertions(+), 104 deletions(-) create mode 100644 dgl/src/pugl/common.h create mode 100644 dgl/src/pugl/event.h create mode 100644 dgl/src/pugl/gl.h create mode 100644 dgl/src/pugl/glu.h diff --git a/dgl/Makefile b/dgl/Makefile index ba3f7ce4..443bcc68 100644 --- a/dgl/Makefile +++ b/dgl/Makefile @@ -8,8 +8,8 @@ include Makefile.mk # -------------------------------------------------------------- -BUILD_C_FLAGS += $(DGL_FLAGS) -I. -BUILD_CXX_FLAGS += $(DGL_FLAGS) -I. +BUILD_C_FLAGS += $(DGL_FLAGS) -I. -Isrc +BUILD_CXX_FLAGS += $(DGL_FLAGS) -I. -Isrc LINK_FLAGS += $(DGL_LIBS) # -------------------------------------------------------------- diff --git a/dgl/src/Window.cpp b/dgl/src/Window.cpp index f2183d1f..d64a95ed 100644 --- a/dgl/src/Window.cpp +++ b/dgl/src/Window.cpp @@ -15,6 +15,7 @@ */ // we need this for now +#define PUGL_HAVE_GL 1 #define PUGL_GRAB_FOCUS 1 #include "AppPrivateData.hpp" diff --git a/dgl/src/pugl/common.h b/dgl/src/pugl/common.h new file mode 100644 index 00000000..4255de69 --- /dev/null +++ b/dgl/src/pugl/common.h @@ -0,0 +1,121 @@ +/* + Copyright 2014 David Robillard + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ + +#ifndef PUGL_COMMON_H_INCLUDED +#define PUGL_COMMON_H_INCLUDED + +#ifdef __cplusplus +extern "C" { +#endif + +/** + @addtogroup pugl + @{ +*/ + +/** + A Pugl view. +*/ +typedef struct PuglViewImpl PuglView; + +/** + A native window handle. + + On X11, this is a Window. + On OSX, this is an NSView*. + On Windows, this is a HWND. +*/ +typedef intptr_t PuglNativeWindow; + +/** + Handle for opaque user data. +*/ +typedef void* PuglHandle; + +/** + Return status code. +*/ +typedef enum { + PUGL_SUCCESS = 0 +} PuglStatus; + +/** + Drawing context type. +*/ +typedef enum { + PUGL_GL, + PUGL_CAIRO +} PuglContextType; + +/** + Convenience symbols for ASCII control characters. +*/ +typedef enum { + PUGL_CHAR_BACKSPACE = 0x08, + PUGL_CHAR_ESCAPE = 0x1B, + PUGL_CHAR_DELETE = 0x7F +} PuglChar; + +/** + Keyboard modifier flags. +*/ +typedef enum { + PUGL_MOD_SHIFT = 1, /**< Shift key */ + PUGL_MOD_CTRL = 1 << 1, /**< Control key */ + PUGL_MOD_ALT = 1 << 2, /**< Alt/Option key */ + PUGL_MOD_SUPER = 1 << 3 /**< Mod4/Command/Windows key */ +} PuglMod; + +/** + Special (non-Unicode) keyboard keys. +*/ +typedef enum { + PUGL_KEY_F1 = 1, + PUGL_KEY_F2, + PUGL_KEY_F3, + PUGL_KEY_F4, + PUGL_KEY_F5, + PUGL_KEY_F6, + PUGL_KEY_F7, + PUGL_KEY_F8, + PUGL_KEY_F9, + PUGL_KEY_F10, + PUGL_KEY_F11, + PUGL_KEY_F12, + PUGL_KEY_LEFT, + PUGL_KEY_UP, + PUGL_KEY_RIGHT, + PUGL_KEY_DOWN, + PUGL_KEY_PAGE_UP, + PUGL_KEY_PAGE_DOWN, + PUGL_KEY_HOME, + PUGL_KEY_END, + PUGL_KEY_INSERT, + PUGL_KEY_SHIFT, + PUGL_KEY_CTRL, + PUGL_KEY_ALT, + PUGL_KEY_SUPER +} PuglKey; + +/** + @} +*/ + +#ifdef __cplusplus +} /* extern "C" */ +#endif + +#endif /* PUGL_COMMON_H_INCLUDED */ diff --git a/dgl/src/pugl/event.h b/dgl/src/pugl/event.h new file mode 100644 index 00000000..6fc80cf4 --- /dev/null +++ b/dgl/src/pugl/event.h @@ -0,0 +1,217 @@ +/* + Copyright 2014 David Robillard + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ + +#ifndef PUGL_EVENT_H_INCLUDED +#define PUGL_EVENT_H_INCLUDED + +#ifdef __cplusplus +extern "C" { +#else +# include +#endif + +#include "pugl/common.h" + +/** + @addtogroup pugl + @{ +*/ + +/** + The type of a PuglEvent. +*/ +typedef enum { + PUGL_BUTTON_PRESS, + PUGL_BUTTON_RELEASE, + PUGL_CONFIGURE, + PUGL_EXPOSE, + PUGL_KEY_PRESS, + PUGL_KEY_RELEASE, + PUGL_ENTER_NOTIFY, + PUGL_LEAVE_NOTIFY, + PUGL_MOTION_NOTIFY, + PUGL_NOTHING, + PUGL_SCROLL +} PuglEventType; + +/** + Reason for a PuglEventCrossing. +*/ +typedef enum { + PUGL_CROSSING_NORMAL, /**< Crossing due to pointer motion. */ + PUGL_CROSSING_GRAB, /**< Crossing due to a grab. */ + PUGL_CROSSING_UNGRAB /**< Crossing due to a grab release. */ +} PuglCrossingMode; + +/** + Common header for all event structs. +*/ +typedef struct { + PuglEventType type; /**< Event type. */ + PuglView* view; /**< View that received this event. */ + bool send_event; /**< True iff event was sent explicitly. */ +} PuglEventAny; + +/** + Button press or release event. + + For event types PUGL_BUTTON_PRESS and PUGL_BUTTON_RELEASE. +*/ +typedef struct { + PuglEventType type; /**< PUGL_BUTTON_PRESS or PUGL_BUTTON_RELEASE. */ + PuglView* view; /**< View that received this event. */ + bool send_event; /**< True iff event was sent explicitly. */ + uint32_t time; /**< Time in milliseconds. */ + double x; /**< View-relative X coordinate. */ + double y; /**< View-relative Y coordinate. */ + double x_root; /**< Root-relative X coordinate. */ + double y_root; /**< Root-relative Y coordinate. */ + unsigned state; /**< Bitwise OR of PuglMod flags. */ + unsigned button; /**< 1-relative button number. */ +} PuglEventButton; + +/** + Configure event for when window size or position has changed. +*/ +typedef struct { + PuglEventType type; /**< PUGL_CONFIGURE. */ + PuglView* view; /**< View that received this event. */ + bool send_event; /**< True iff event was sent explicitly. */ + double x; /**< New parent-relative X coordinate. */ + double y; /**< New parent-relative Y coordinate. */ + double width; /**< New width. */ + double height; /**< New height. */ +} PuglEventConfigure; + +/** + Expose event for when a region must be redrawn. +*/ +typedef struct { + PuglEventType type; /**< PUGL_EXPOSE. */ + PuglView* view; /**< View that received this event. */ + bool send_event; /**< True iff event was sent explicitly. */ + double x; /**< View-relative X coordinate. */ + double y; /**< View-relative Y coordinate. */ + double width; /**< Width of exposed region. */ + double height; /**< Height of exposed region. */ + int count; /**< Number of expose events to follow. */ +} PuglEventExpose; + +/** + Key press event. + + Keys that correspond to a Unicode character are expressed as a character + code. For other keys, `character` will be 0 and `special` indicates the key + pressed. +*/ +typedef struct { + PuglEventType type; /**< PUGL_KEY_PRESS or PUGL_KEY_RELEASE. */ + PuglView* view; /**< View that received this event. */ + bool send_event; /**< True iff event was sent explicitly. */ + uint32_t time; /**< Time in milliseconds. */ + double x; /**< View-relative X coordinate. */ + double y; /**< View-relative Y coordinate. */ + double x_root; /**< Root-relative X coordinate. */ + double y_root; /**< Root-relative Y coordinate. */ + unsigned state; /**< Bitwise OR of PuglMod flags. */ + uint32_t character; /**< Unicode character code, or 0. */ + PuglKey special; /**< Special key, if character is 0. */ +} PuglEventKey; + +/** + Pointer crossing event (enter and leave). +*/ +typedef struct { + PuglEventType type; /**< PUGL_ENTER_NOTIFY or PUGL_LEAVE_NOTIFY. */ + PuglView* view; /**< View that received this event. */ + bool send_event; /**< True iff event was sent explicitly. */ + uint32_t time; /**< Time in milliseconds. */ + double x; /**< View-relative X coordinate. */ + double y; /**< View-relative Y coordinate. */ + double x_root; /**< Root-relative X coordinate. */ + double y_root; /**< Root-relative Y coordinate. */ + unsigned state; /**< Bitwise OR of PuglMod flags. */ + PuglCrossingMode mode; /**< Reason for crossing. */ +} PuglEventCrossing; + +/** + Pointer motion event. +*/ +typedef struct { + PuglEventType type; /**< PUGL_MOTION_NOTIFY. */ + PuglView* view; /**< View that received this event. */ + bool send_event; /**< True iff event was sent explicitly. */ + uint32_t time; /**< Time in milliseconds. */ + double x; /**< View-relative X coordinate. */ + double y; /**< View-relative Y coordinate. */ + double x_root; /**< Root-relative X coordinate. */ + double y_root; /**< Root-relative Y coordinate. */ + unsigned state; /**< Bitwise OR of PuglMod flags. */ + bool is_hint; /**< True iff this event is a motion hint. */ + bool focus; /**< True iff this is the focused window. */ +} PuglEventMotion; + +/** + Scroll event. + + The scroll distance is expressed in "lines", an arbitrary unit that + corresponds to a single tick of a detented mouse wheel. For example, `dy` = + 1.0 scrolls 1 line up. Some systems and devices support finer resolution + and/or higher values for fast scrolls, so programs should handle any value + gracefully. + */ +typedef struct { + PuglEventType type; /**< PUGL_SCROLL. */ + PuglView* view; /**< View that received this event. */ + bool send_event; /**< True iff event was sent explicitly. */ + uint32_t time; /**< Time in milliseconds. */ + double x; /**< View-relative X coordinate. */ + double y; /**< View-relative Y coordinate. */ + double x_root; /**< Root-relative X coordinate. */ + double y_root; /**< Root-relative Y coordinate. */ + unsigned state; /**< Bitwise OR of PuglMod flags. */ + double dx; /**< Scroll X distance in lines. */ + double dy; /**< Scroll Y distance in lines. */ +} PuglEventScroll; + +/** + Interface event. + + This is a union of all event structs. The `type` must be checked to + determine which fields are safe to access. A pointer to PuglEvent can + either be cast to the appropriate type, or the union members used. +*/ +typedef union { + PuglEventType type; /**< Event type. */ + PuglEventAny any; /**< Valid for all event types. */ + PuglEventButton button; /**< PUGL_BUTTON_PRESS, PUGL_BUTTON_RELEASE. */ + PuglEventConfigure configure; /**< PUGL_CONFIGURE. */ + PuglEventCrossing crossing; /**< PUGL_ENTER_NOTIFY, PUGL_LEAVE_NOTIFY. */ + PuglEventExpose expose; /**< PUGL_EXPOSE. */ + PuglEventKey key; /**< PUGL_KEY_PRESS, PUGL_KEY_RELEASE. */ + PuglEventMotion motion; /**< PUGL_MOTION_NOTIFY. */ + PuglEventScroll scroll; /**< PUGL_SCROLL. */ +} PuglEvent; + +/** + @} +*/ + +#ifdef __cplusplus +} /* extern "C" */ +#endif + +#endif /* PUGL_EVENT_H_INCLUDED */ diff --git a/dgl/src/pugl/gl.h b/dgl/src/pugl/gl.h new file mode 100644 index 00000000..9a6aeefe --- /dev/null +++ b/dgl/src/pugl/gl.h @@ -0,0 +1,32 @@ +/* + Copyright 2012-2014 David Robillard + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ + +/** + @file gl.h Portable header wrapper for gl.h. + + Unfortunately, GL includes vary across platforms so this header allows for + pure portable programs. +*/ + +#ifdef __APPLE__ +# include "OpenGL/gl.h" +#else +# ifdef _WIN32 +# include /* Broken Windows GL headers require this */ +# endif +# include "GL/gl.h" +#endif + diff --git a/dgl/src/pugl/glu.h b/dgl/src/pugl/glu.h new file mode 100644 index 00000000..0ed0055d --- /dev/null +++ b/dgl/src/pugl/glu.h @@ -0,0 +1,32 @@ +/* + Copyright 2012-2014 David Robillard + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ + +/** + @file gl.h Portable header wrapper for glu.h. + + Unfortunately, GL includes vary across platforms so this header allows for + pure portable programs. +*/ + +#ifdef __APPLE__ +# include "OpenGL/glu.h" +#else +# ifdef _WIN32 +# include /* Broken Windows GL headers require this */ +# endif +# include "GL/glu.h" +#endif + diff --git a/dgl/src/pugl/pugl.h b/dgl/src/pugl/pugl.h index 360f8e8b..68ecc195 100644 --- a/dgl/src/pugl/pugl.h +++ b/dgl/src/pugl/pugl.h @@ -23,19 +23,8 @@ #include -/* - This API is pure portable C and contains no platform specific elements, or - even a GL dependency. However, unfortunately GL includes vary across - platforms so they are included here to allow for pure portable programs. -*/ -#ifdef __APPLE__ -# include "OpenGL/gl.h" -#else -# ifdef _WIN32 -# include /* Broken Windows GL headers require this */ -# endif -# include "GL/gl.h" -#endif +#include "pugl/common.h" +#include "pugl/event.h" #ifdef PUGL_SHARED # ifdef _WIN32 @@ -67,80 +56,9 @@ extern "C" { */ /** - An OpenGL view. -*/ -typedef struct PuglViewImpl PuglView; - -/** - A native window handle. - - On X11, this is a Window. - On OSX, this is an NSView*. - On Windows, this is a HWND. -*/ -typedef intptr_t PuglNativeWindow; - -/** - Return status code. -*/ -typedef enum { - PUGL_SUCCESS = 0 -} PuglStatus; - -/** - Convenience symbols for ASCII control characters. -*/ -typedef enum { - PUGL_CHAR_BACKSPACE = 0x08, - PUGL_CHAR_ESCAPE = 0x1B, - PUGL_CHAR_DELETE = 0x7F -} PuglChar; - -/** - Special (non-Unicode) keyboard keys. + A function called when an event occurs. */ -typedef enum { - PUGL_KEY_F1 = 1, - PUGL_KEY_F2, - PUGL_KEY_F3, - PUGL_KEY_F4, - PUGL_KEY_F5, - PUGL_KEY_F6, - PUGL_KEY_F7, - PUGL_KEY_F8, - PUGL_KEY_F9, - PUGL_KEY_F10, - PUGL_KEY_F11, - PUGL_KEY_F12, - PUGL_KEY_LEFT, - PUGL_KEY_UP, - PUGL_KEY_RIGHT, - PUGL_KEY_DOWN, - PUGL_KEY_PAGE_UP, - PUGL_KEY_PAGE_DOWN, - PUGL_KEY_HOME, - PUGL_KEY_END, - PUGL_KEY_INSERT, - PUGL_KEY_SHIFT, - PUGL_KEY_CTRL, - PUGL_KEY_ALT, - PUGL_KEY_SUPER -} PuglKey; - -/** - Keyboard modifier flags. -*/ -typedef enum { - PUGL_MOD_SHIFT = 1, /**< Shift key */ - PUGL_MOD_CTRL = 1 << 1, /**< Control key */ - PUGL_MOD_ALT = 1 << 2, /**< Alt/Option key */ - PUGL_MOD_SUPER = 1 << 3 /**< Mod4/Command/Windows key */ -} PuglMod; - -/** - Handle for opaque user data. -*/ -typedef void* PuglHandle; +typedef void (*PuglEventFunc)(PuglView* view, const PuglEvent* event); /** A function called when the window is closed. @@ -216,6 +134,12 @@ typedef void (*PuglScrollFunc)(PuglView* view, */ typedef void (*PuglSpecialFunc)(PuglView* view, bool press, PuglKey key); +/** + @name Initialization + Configuration functions which must be called before creating a window. + @{ +*/ + /** Create a Pugl context. @@ -246,6 +170,22 @@ puglInitWindowSize(PuglView* view, int width, int height); PUGL_API void puglInitResizable(PuglView* view, bool resizable); +/** + Set the context type before creating a window. +*/ +PUGL_API void +puglInitContextType(PuglView* view, PuglContextType type); + +/** + @} +*/ + +/** + @name Windows + Window management functions. + @{ +*/ + /** Create a window with the settings given by the various puglInit functions. @@ -266,6 +206,16 @@ puglShowWindow(PuglView* view); PUGL_API void puglHideWindow(PuglView* view); +/** + Return the native window handle. +*/ +PUGL_API PuglNativeWindow +puglGetNativeWindow(PuglView* view); + +/** + @} +*/ + /** Set the handle to be passed to all callbacks. @@ -284,6 +234,15 @@ puglSetHandle(PuglView* view, PuglHandle handle); PUGL_API PuglHandle puglGetHandle(PuglView* view); +/** + Get the drawing context. + + For PUGL_GL contexts, this is unused and returns NULL. + For PUGL_CAIRO contexts, this returns a pointer to a cairo_t. +*/ +PUGL_API void* +puglGetContext(PuglView* view); + /** Return the timestamp (if any) of the currently-processing event. */ @@ -304,6 +263,18 @@ puglGetModifiers(PuglView* view); PUGL_API void puglIgnoreKeyRepeat(PuglView* view, bool ignore); +/** + @name Event Callbacks + Functions to set event callbacks for handling user input. + @{ +*/ + +/** + Set the function to call when an event occurs. +*/ +PUGL_API void +puglSetEventFunc(PuglView* view, PuglEventFunc eventFunc); + /** Set the function to call when the window is closed. */ @@ -353,10 +324,14 @@ PUGL_API void puglSetReshapeFunc(PuglView* view, PuglReshapeFunc reshapeFunc); /** - Return the native window handle. + @} */ -PUGL_API PuglNativeWindow -puglGetNativeWindow(PuglView* view); + +/** + Grab the input focus. +*/ +PUGL_API void +puglGrabFocus(PuglView* view); /** Process all pending window events. diff --git a/dgl/src/pugl/pugl_internal.h b/dgl/src/pugl/pugl_internal.h index 45a3a2d9..5b05c573 100644 --- a/dgl/src/pugl/pugl_internal.h +++ b/dgl/src/pugl/pugl_internal.h @@ -1,5 +1,5 @@ /* - Copyright 2012 David Robillard + Copyright 2012-2014 David Robillard Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above @@ -24,11 +24,14 @@ If you are copying the pugl code into your source tree, the following symbols can be defined to tweak pugl behaviour: + PUGL_HAVE_CAIRO: Include Cairo support code. + PUGL_HAVE_GL: Include OpenGL support code. PUGL_GRAB_FOCUS: Work around reparent keyboard issues by grabbing focus. PUGL_VERBOSE: Print GL information to console. */ -#include "pugl.h" +#include "pugl/pugl.h" +#include "pugl/event.h" #ifdef PUGL_VERBOSE # include @@ -43,6 +46,7 @@ typedef struct PuglInternalsImpl PuglInternals; struct PuglViewImpl { PuglHandle handle; + PuglEventFunc eventFunc; PuglCloseFunc closeFunc; PuglDisplayFunc displayFunc; PuglKeyboardFunc keyboardFunc; @@ -55,6 +59,7 @@ struct PuglViewImpl { PuglInternals* impl; PuglNativeWindow parent; + PuglContextType ctx_type; int width; int height; @@ -68,8 +73,6 @@ struct PuglViewImpl { PuglInternals* puglInitInternals(); -void puglDefaultReshape(PuglView* view, int width, int height); - PuglView* puglInit(int* pargc, char** argv) { @@ -80,7 +83,7 @@ puglInit(int* pargc, char** argv) PuglInternals* impl = puglInitInternals(); if (!impl) { - free(view); + free(view); return NULL; } @@ -113,6 +116,12 @@ puglInitResizable(PuglView* view, bool resizable) view->resizable = resizable; } +void +puglInitContextType(PuglView* view, PuglContextType type) +{ + view->ctx_type = type; +} + void puglSetHandle(PuglView* view, PuglHandle handle) { @@ -138,6 +147,12 @@ puglGetModifiers(PuglView* view) } void +puglIgnoreKeyRepeat(PuglView* view, bool ignore) +{ + view->ignoreKeyRepeat = ignore; +} + +static void puglDefaultReshape(PuglView* view, int width, int height) { glMatrixMode(GL_PROJECTION); @@ -154,9 +169,9 @@ puglDefaultReshape(PuglView* view, int width, int height) } void -puglIgnoreKeyRepeat(PuglView* view, bool ignore) +puglSetEventFunc(PuglView* view, PuglEventFunc eventFunc) { - view->ignoreKeyRepeat = ignore; + view->eventFunc = eventFunc; } void @@ -206,3 +221,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; + } +} diff --git a/dgl/src/pugl/pugl_osx.m b/dgl/src/pugl/pugl_osx.m index e98a9364..5fdcdc0b 100644 --- a/dgl/src/pugl/pugl_osx.m +++ b/dgl/src/pugl/pugl_osx.m @@ -1,5 +1,5 @@ /* - Copyright 2012 David Robillard + Copyright 2012-2014 David Robillard Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above @@ -22,7 +22,7 @@ #import -#include "pugl_internal.h" +#include "pugl/pugl_internal.h" @interface PuglWindow : NSWindow { diff --git a/dgl/src/pugl/pugl_win.cpp b/dgl/src/pugl/pugl_win.cpp index 87b71d2b..aee37a7e 100644 --- a/dgl/src/pugl/pugl_win.cpp +++ b/dgl/src/pugl/pugl_win.cpp @@ -26,7 +26,7 @@ #include #include -#include "pugl_internal.h" +#include "pugl/pugl_internal.h" #ifndef WM_MOUSEWHEEL # define WM_MOUSEWHEEL 0x020A diff --git a/dgl/src/pugl/pugl_x11.c b/dgl/src/pugl/pugl_x11.c index fdba1b64..81e7db7d 100644 --- a/dgl/src/pugl/pugl_x11.c +++ b/dgl/src/pugl/pugl_x11.c @@ -1,5 +1,6 @@ /* Copyright 2012-2014 David Robillard + Copyright 2013 Robin Gareus Copyright 2011-2012 Ben Loftis, Harrison Consoles Permission to use, copy, modify, and/or distribute this software for any @@ -23,20 +24,35 @@ #include #include -#include -#include #include #include +#include #include -#include "pugl_internal.h" +#ifdef PUGL_HAVE_GL +#include +#include +#endif + +#ifdef PUGL_HAVE_CAIRO +#include +#include +#endif + +#include "pugl/event.h" +#include "pugl/pugl_internal.h" struct PuglInternalsImpl { Display* display; int screen; Window win; +#ifdef PUGL_HAVE_CAIRO + cairo_t* cr; +#endif +#ifdef PUGL_HAVE_GL GLXContext ctx; Bool doubleBuffered; +#endif }; /**