/* Copyright 2012-2020 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 pugl.hpp @brief Pugl C++ API wrapper. */ #ifndef PUGL_PUGL_HPP #define PUGL_PUGL_HPP #include "pugl/pugl.h" #include #include namespace pugl { /** @defgroup puglxx Pugl C++ API C++ API wrapper. @ingroup pugldoc @{ */ namespace detail { /// Free function for a C object template using FreeFunc = void (*)(T*); /// Generic C++ wrapper for a C object template Free> class Wrapper { public: Wrapper(const Wrapper&) = delete; Wrapper& operator=(const Wrapper&) = delete; Wrapper(Wrapper&& wrapper) noexcept : _ptr{wrapper._ptr} { wrapper._ptr = nullptr; } Wrapper& operator=(Wrapper&& wrapper) noexcept { _ptr = wrapper._ptr; wrapper._ptr = nullptr; } ~Wrapper() noexcept { Free(_ptr); } T* cobj() noexcept { return _ptr; } const T* cobj() const noexcept { return _ptr; } protected: explicit Wrapper(T* ptr) noexcept : _ptr{ptr} {} private: T* _ptr; }; } // namespace detail using Rect = PuglRect; ///< @copydoc PuglRect /** @name Events @{ */ /** A strongly-typed analogue of PuglEvent. This is bit-for-bit identical to the corresponding PuglEvent, so events are simply cast to this type to avoid any copying overhead. @tparam t The `type` field of the corresponding PuglEvent. @tparam Base The specific struct type of the corresponding PuglEvent. */ template struct Event final : Base { using BaseEvent = Base; static constexpr const PuglEventType type = t; }; using Mod = PuglMod; ///< @copydoc PuglMod using Mods = PuglMods; ///< @copydoc PuglMods using Key = PuglKey; ///< @copydoc PuglKey using EventType = PuglEventType; ///< @copydoc PuglEventType using EventFlag = PuglEventFlag; ///< @copydoc PuglEventFlag using EventFlags = PuglEventFlags; ///< @copydoc PuglEventFlags using CrossingMode = PuglCrossingMode; ///< @copydoc PuglCrossingMode /// @copydoc PuglEventCreate using CreateEvent = Event; /// @copydoc PuglEventDestroy using DestroyEvent = Event; /// @copydoc PuglEventConfigure using ConfigureEvent = Event; /// @copydoc PuglEventMap using MapEvent = Event; /// @copydoc PuglEventUnmap using UnmapEvent = Event; /// @copydoc PuglEventUpdate using UpdateEvent = Event; /// @copydoc PuglEventExpose using ExposeEvent = Event; /// @copydoc PuglEventClose using CloseEvent = Event; /// @copydoc PuglEventFocus using FocusInEvent = Event; /// @copydoc PuglEventFocus using FocusOutEvent = Event; /// @copydoc PuglEventKey using KeyPressEvent = Event; /// @copydoc PuglEventKey using KeyReleaseEvent = Event; /// @copydoc PuglEventText using TextEvent = Event; /// @copydoc PuglEventCrossing using PointerInEvent = Event; /// @copydoc PuglEventCrossing using PointerOutEvent = Event; /// @copydoc PuglEventButton using ButtonPressEvent = Event; /// @copydoc PuglEventButton using ButtonReleaseEvent = Event; /// @copydoc PuglEventMotion using MotionEvent = Event; /// @copydoc PuglEventScroll using ScrollEvent = Event; /// @copydoc PuglEventClient using ClientEvent = Event; /// @copydoc PuglEventTimer using TimerEvent = Event; /// @copydoc PuglEventLoopEnter using LoopEnterEvent = Event; /// @copydoc PuglEventLoopLeave using LoopLeaveEvent = Event; /** @} @name Status @{ */ /// @copydoc PuglStatus enum class Status { success, ///< @copydoc PUGL_SUCCESS failure, ///< @copydoc PUGL_FAILURE unknownError, ///< @copydoc PUGL_UNKNOWN_ERROR badBackend, ///< @copydoc PUGL_BAD_BACKEND badConfiguration, ///< @copydoc PUGL_BAD_CONFIGURATION badParameter, ///< @copydoc PUGL_BAD_PARAMETER backendFailed, ///< @copydoc PUGL_BACKEND_FAILED registrationFailed, ///< @copydoc PUGL_REGISTRATION_FAILED realizeFailed, ///< @copydoc PUGL_REALIZE_FAILED setFormatFailed, ///< @copydoc PUGL_SET_FORMAT_FAILED createContextFailed, ///< @copydoc PUGL_CREATE_CONTEXT_FAILED unsupportedType, ///< @copydoc PUGL_UNSUPPORTED_TYPE }; static_assert(Status(PUGL_UNSUPPORTED_TYPE) == Status::unsupportedType, ""); /// @copydoc puglStrerror static inline const char* strerror(const Status status) noexcept { return puglStrerror(static_cast(status)); } /** @} @name World @{ */ /// @copydoc PuglWorldType enum class WorldType { program, ///< @copydoc PUGL_PROGRAM module, ///< @copydoc PUGL_MODULE }; static_assert(WorldType(PUGL_MODULE) == WorldType::module, ""); /// @copydoc PuglWorldFlag enum class WorldFlag { threads = PUGL_WORLD_THREADS, ///< @copydoc PUGL_WORLD_THREADS }; static_assert(WorldFlag(PUGL_WORLD_THREADS) == WorldFlag::threads, ""); using WorldFlags = PuglWorldFlags; ///< @copydoc PuglWorldFlags /// An exception thrown when construction fails class FailedConstructionError : public std::exception { public: FailedConstructionError(const char* const msg) noexcept : _msg{msg} {} virtual const char* what() const noexcept override; private: const char* _msg; }; /// @copydoc PuglWorld class World : public detail::Wrapper { public: World(const World&) = delete; World& operator=(const World&) = delete; World(World&&) = delete; World& operator=(World&&) = delete; explicit World(WorldType type, WorldFlags flags) : Wrapper{puglNewWorld(static_cast(type), flags)} { if (!cobj()) { throw FailedConstructionError("Failed to create pugl::World"); } } explicit World(WorldType type) : World{type, {}} { if (!cobj()) { throw FailedConstructionError("Failed to create pugl::World"); } } /// @copydoc puglGetNativeWorld void* nativeWorld() { return puglGetNativeWorld(cobj()); } /// @copydoc puglSetClassName Status setClassName(const char* const name) { return static_cast(puglSetClassName(cobj(), name)); } /// @copydoc puglGetTime double time() const { return puglGetTime(cobj()); } /// @copydoc puglUpdate Status update(const double timeout) { return static_cast(puglUpdate(cobj(), timeout)); } }; /** @} @name View @{ */ using Backend = PuglBackend; ///< @copydoc PuglBackend using NativeView = PuglNativeView; ///< @copydoc PuglNativeView /// @copydoc PuglViewHint enum class ViewHint { useCompatProfile, ///< @copydoc PUGL_USE_COMPAT_PROFILE useDebugContext, ///< @copydoc PUGL_USE_DEBUG_CONTEXT contextVersionMajor, ///< @copydoc PUGL_CONTEXT_VERSION_MAJOR contextVersionMinor, ///< @copydoc PUGL_CONTEXT_VERSION_MINOR redBits, ///< @copydoc PUGL_RED_BITS greenBits, ///< @copydoc PUGL_GREEN_BITS blueBits, ///< @copydoc PUGL_BLUE_BITS alphaBits, ///< @copydoc PUGL_ALPHA_BITS depthBits, ///< @copydoc PUGL_DEPTH_BITS stencilBits, ///< @copydoc PUGL_STENCIL_BITS samples, ///< @copydoc PUGL_SAMPLES doubleBuffer, ///< @copydoc PUGL_DOUBLE_BUFFER swapInterval, ///< @copydoc PUGL_SWAP_INTERVAL resizable, ///< @copydoc PUGL_RESIZABLE ignoreKeyRepeat, ///< @copydoc PUGL_IGNORE_KEY_REPEAT refreshRate, ///< @copydoc PUGL_REFRESH_RATE }; static_assert(ViewHint(PUGL_REFRESH_RATE) == ViewHint::refreshRate, ""); using ViewHintValue = PuglViewHintValue; ///< @copydoc PuglViewHintValue /// @copydoc PuglCursor enum class Cursor { arrow, ///< @copydoc PUGL_CURSOR_ARROW caret, ///< @copydoc PUGL_CURSOR_CARET crosshair, ///< @copydoc PUGL_CURSOR_CROSSHAIR hand, ///< @copydoc PUGL_CURSOR_HAND no, ///< @copydoc PUGL_CURSOR_NO leftRight, ///< @copydoc PUGL_CURSOR_LEFT_RIGHT upDown, ///< @copydoc PUGL_CURSOR_UP_DOWN }; static_assert(Cursor(PUGL_CURSOR_UP_DOWN) == Cursor::upDown, ""); /// @copydoc PuglView class View : protected detail::Wrapper { public: /** @name Setup Methods for creating and destroying a view. @{ */ explicit View(World& world) : Wrapper{puglNewView(world.cobj())} , _world(world) { if (!cobj()) { throw FailedConstructionError("Failed to create pugl::View"); } puglSetHandle(cobj(), this); puglSetEventFunc(cobj(), dispatchEvent); } virtual ~View() = default; View(const View&) = delete; View& operator=(const View&) = delete; View(View&&) = delete; View&& operator=(View&&) = delete; const World& world() const { return _world; } World& world() { return _world; } /// @copydoc puglSetViewHint Status setHint(ViewHint hint, int value) { return static_cast( puglSetViewHint(cobj(), static_cast(hint), value)); } /// @copydoc puglGetViewHint int getHint(ViewHint hint) { return puglGetViewHint(cobj(), static_cast(hint)); } /** @} @name Frame Methods for working with the position and size of a view. @{ */ /// @copydoc puglGetFrame Rect frame() const { return puglGetFrame(cobj()); } /// @copydoc puglSetFrame Status setFrame(Rect frame) { return static_cast(puglSetFrame(cobj(), frame)); } /// @copydoc puglSetDefaultSize Status setDefaultSize(int width, int height) { return static_cast(puglSetDefaultSize(cobj(), width, height)); } /// @copydoc puglSetMinSize Status setMinSize(int width, int height) { return static_cast(puglSetMinSize(cobj(), width, height)); } /// @copydoc puglSetMaxSize Status setMaxSize(int width, int height) { return static_cast(puglSetMaxSize(cobj(), width, height)); } /// @copydoc puglSetAspectRatio Status setAspectRatio(int minX, int minY, int maxX, int maxY) { return static_cast( puglSetAspectRatio(cobj(), minX, minY, maxX, maxY)); } /** @} @name Windows Methods for working with top-level windows. @{ */ /// @copydoc puglSetWindowTitle Status setWindowTitle(const char* title) { return static_cast(puglSetWindowTitle(cobj(), title)); } /// @copydoc puglSetParentWindow Status setParentWindow(NativeView parent) { return static_cast(puglSetParentWindow(cobj(), parent)); } /// @copydoc puglSetTransientFor Status setTransientFor(NativeView parent) { return static_cast(puglSetTransientFor(cobj(), parent)); } /// @copydoc puglRealize Status realize() { return static_cast(puglRealize(cobj())); } /// @copydoc puglShowWindow Status showWindow() { return static_cast(puglShowWindow(cobj())); } /// @copydoc puglHideWindow Status hideWindow() { return static_cast(puglHideWindow(cobj())); } /// @copydoc puglGetVisible bool visible() const { return puglGetVisible(cobj()); } /// @copydoc puglGetNativeWindow NativeView nativeWindow() { return puglGetNativeWindow(cobj()); } /** @} @name Graphics Methods for working with the graphics context and scheduling redisplays. @{ */ /// @copydoc puglGetContext void* context() { return puglGetContext(cobj()); } /// @copydoc puglPostRedisplay Status postRedisplay() { return static_cast(puglPostRedisplay(cobj())); } /// @copydoc puglPostRedisplayRect Status postRedisplayRect(const Rect rect) { return static_cast(puglPostRedisplayRect(cobj(), rect)); } /** @} @name Interaction Methods for interacting with the user and window system. @{ */ /// @copydoc puglGrabFocus Status grabFocus() { return static_cast(puglGrabFocus(cobj())); } /// @copydoc puglHasFocus bool hasFocus() const { return puglHasFocus(cobj()); } /// @copydoc puglSetBackend Status setBackend(const PuglBackend* backend) { return static_cast(puglSetBackend(cobj(), backend)); } /// @copydoc puglSetCursor Status setCursor(const Cursor cursor) { return static_cast( puglSetCursor(cobj(), static_cast(cursor))); } /// @copydoc puglRequestAttention Status requestAttention() { return static_cast(puglRequestAttention(cobj())); } /// @copydoc puglStartTimer Status startTimer(const uintptr_t id, const double timeout) { return static_cast(puglStartTimer(cobj(), id, timeout)); } /// @copydoc puglStopTimer Status stopTimer(const uintptr_t id) { return static_cast(puglStopTimer(cobj(), id)); } /** @} @name Event Handlers Methods called when events are dispatched to the view. @{ */ virtual Status onCreate(const CreateEvent&) PUGL_CONST_FUNC; virtual Status onDestroy(const DestroyEvent&) PUGL_CONST_FUNC; virtual Status onConfigure(const ConfigureEvent&) PUGL_CONST_FUNC; virtual Status onMap(const MapEvent&) PUGL_CONST_FUNC; virtual Status onUnmap(const UnmapEvent&) PUGL_CONST_FUNC; virtual Status onUpdate(const UpdateEvent&) PUGL_CONST_FUNC; virtual Status onExpose(const ExposeEvent&) PUGL_CONST_FUNC; virtual Status onClose(const CloseEvent&) PUGL_CONST_FUNC; virtual Status onFocusIn(const FocusInEvent&) PUGL_CONST_FUNC; virtual Status onFocusOut(const FocusOutEvent&) PUGL_CONST_FUNC; virtual Status onKeyPress(const KeyPressEvent&) PUGL_CONST_FUNC; virtual Status onKeyRelease(const KeyReleaseEvent&) PUGL_CONST_FUNC; virtual Status onText(const TextEvent&) PUGL_CONST_FUNC; virtual Status onPointerIn(const PointerInEvent&) PUGL_CONST_FUNC; virtual Status onPointerOut(const PointerOutEvent&) PUGL_CONST_FUNC; virtual Status onButtonPress(const ButtonPressEvent&) PUGL_CONST_FUNC; virtual Status onButtonRelease(const ButtonReleaseEvent&) PUGL_CONST_FUNC; virtual Status onMotion(const MotionEvent&) PUGL_CONST_FUNC; virtual Status onScroll(const ScrollEvent&) PUGL_CONST_FUNC; virtual Status onClient(const ClientEvent&) PUGL_CONST_FUNC; virtual Status onTimer(const TimerEvent&) PUGL_CONST_FUNC; virtual Status onLoopEnter(const LoopEnterEvent&) PUGL_CONST_FUNC; virtual Status onLoopLeave(const LoopLeaveEvent&) PUGL_CONST_FUNC; /** @} */ PuglView* cobj() { return Wrapper::cobj(); } const PuglView* cobj() const { return Wrapper::cobj(); } private: static PuglStatus dispatchEvent(PuglView* view, const PuglEvent* event) noexcept { try { View* self = static_cast(puglGetHandle(view)); return self->dispatch(event); } catch (...) { return PUGL_UNKNOWN_ERROR; } } PuglStatus dispatch(const PuglEvent* event) { switch (event->type) { case PUGL_NOTHING: return PUGL_SUCCESS; case PUGL_CREATE: return static_cast( onCreate(static_cast(event->any))); case PUGL_DESTROY: return static_cast( onDestroy(static_cast(event->any))); case PUGL_CONFIGURE: return static_cast(onConfigure( static_cast(event->configure))); case PUGL_MAP: return static_cast( onMap(static_cast(event->any))); case PUGL_UNMAP: return static_cast( onUnmap(static_cast(event->any))); case PUGL_UPDATE: return static_cast( onUpdate(static_cast(event->any))); case PUGL_EXPOSE: return static_cast( onExpose(static_cast(event->expose))); case PUGL_CLOSE: return static_cast( onClose(static_cast(event->any))); case PUGL_FOCUS_IN: return static_cast( onFocusIn(static_cast(event->focus))); case PUGL_FOCUS_OUT: return static_cast( onFocusOut(static_cast(event->focus))); case PUGL_KEY_PRESS: return static_cast( onKeyPress(static_cast(event->key))); case PUGL_KEY_RELEASE: return static_cast( onKeyRelease(static_cast(event->key))); case PUGL_TEXT: return static_cast( onText(static_cast(event->text))); case PUGL_POINTER_IN: return static_cast(onPointerIn( static_cast(event->crossing))); case PUGL_POINTER_OUT: return static_cast(onPointerOut( static_cast(event->crossing))); case PUGL_BUTTON_PRESS: return static_cast(onButtonPress( static_cast(event->button))); case PUGL_BUTTON_RELEASE: return static_cast(onButtonRelease( static_cast(event->button))); case PUGL_MOTION: return static_cast( onMotion(static_cast(event->motion))); case PUGL_SCROLL: return static_cast( onScroll(static_cast(event->scroll))); case PUGL_CLIENT: return static_cast( onClient(static_cast(event->client))); case PUGL_TIMER: return static_cast( onTimer(static_cast(event->timer))); case PUGL_LOOP_ENTER: return static_cast( onLoopEnter(static_cast(event->any))); case PUGL_LOOP_LEAVE: return static_cast( onLoopLeave(static_cast(event->any))); } return PUGL_FAILURE; } World& _world; }; /** @} @} */ } // namespace pugl #endif // PUGL_PUGL_HPP