diff options
author | David Robillard <d@drobilla.net> | 2023-01-02 17:04:04 -0500 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2023-01-02 17:04:04 -0500 |
commit | 2eb319fbef4c611387f421afab0f8ccefbc298cf (patch) | |
tree | d377a73acd4bc84c6321b044829a143be0aa0961 | |
parent | e1eaf1583025ed182b9a7226f3eb17e08e81bd34 (diff) | |
download | pugl-2eb319fbef4c611387f421afab0f8ccefbc298cf.tar.gz pugl-2eb319fbef4c611387f421afab0f8ccefbc298cf.tar.bz2 pugl-2eb319fbef4c611387f421afab0f8ccefbc298cf.zip |
Factor out common realize checks and initialization
-rw-r--r-- | src/internal.c | 28 | ||||
-rw-r--r-- | src/internal.h | 4 | ||||
-rw-r--r-- | src/mac.m | 19 | ||||
-rw-r--r-- | src/win.c | 24 | ||||
-rw-r--r-- | src/x11.c | 18 |
5 files changed, 49 insertions, 44 deletions
diff --git a/src/internal.c b/src/internal.c index dd1b7f1..bc5080f 100644 --- a/src/internal.c +++ b/src/internal.c @@ -95,6 +95,34 @@ puglDecodeUTF8(const uint8_t* buf) return 0xFFFD; } +static inline bool +isValidSize(const double width, const double height) +{ + return width > 0.0 && height > 0.0; +} + +PuglStatus +puglPreRealize(PuglView* const view) +{ + // Ensure that a backend with at least a configure method has been set + if (!view->backend || !view->backend->configure) { + return PUGL_BAD_BACKEND; + } + + // Set the size to the default if it hasn't already been set + if (!isValidSize(view->frame.width, view->frame.height)) { + const PuglViewSize defaultSize = view->sizeHints[PUGL_DEFAULT_SIZE]; + if (!isValidSize(defaultSize.width, defaultSize.height)) { + return PUGL_BAD_CONFIGURATION; + } + + view->frame.width = defaultSize.width; + view->frame.height = defaultSize.height; + } + + return PUGL_SUCCESS; +} + PuglStatus puglDispatchSimpleEvent(PuglView* view, const PuglEventType type) { diff --git a/src/internal.h b/src/internal.h index e3e7924..066dc7e 100644 --- a/src/internal.h +++ b/src/internal.h @@ -28,6 +28,10 @@ puglSetString(char** dest, const char* string); uint32_t puglDecodeUTF8(const uint8_t* buf); +/// Prepare a view to be realized by the platform implementation if possible +PuglStatus +puglPreRealize(PuglView* view); + /// Dispatch an event with a simple `type` to `view` PuglStatus puglDispatchSimpleEvent(PuglView* view, PuglEventType type); @@ -1007,13 +1007,16 @@ PuglStatus puglRealize(PuglView* view) { PuglInternals* impl = view->impl; + PuglStatus st = PUGL_SUCCESS; + // Ensure that we're unrealized if (impl->wrapperView) { return PUGL_FAILURE; } - if (!view->backend || !view->backend->configure) { - return PUGL_BAD_BACKEND; + // Check that the basic required configuration has been done + if ((st = puglPreRealize(view))) { + return st; } const NSScreen* const screen = [NSScreen mainScreen]; @@ -1053,17 +1056,6 @@ puglRealize(PuglView* view) CVDisplayLinkRelease(link); } - // Set the size to the default if it has not already been set - if (view->frame.width <= 0.0 || view->frame.height <= 0.0) { - const PuglViewSize defaultSize = view->sizeHints[PUGL_DEFAULT_SIZE]; - if (!defaultSize.width || !defaultSize.height) { - return PUGL_BAD_CONFIGURATION; - } - - view->frame.width = defaultSize.width; - view->frame.height = defaultSize.height; - } - // Center top-level windows if a position has not been set if (!view->parent && !view->frame.x && !view->frame.y) { const double screenWidthPx = [screen frame].size.width * scaleFactor; @@ -1115,7 +1107,6 @@ puglRealize(PuglView* view) } // Create draw view to be rendered to - PuglStatus st = PUGL_SUCCESS; if ((st = view->backend->configure(view)) || (st = view->backend->create(view))) { return st; @@ -213,10 +213,18 @@ PuglStatus puglRealize(PuglView* view) { PuglInternals* impl = view->impl; + PuglStatus st = PUGL_SUCCESS; + + // Ensure that we're unrealized if (impl->hwnd) { return PUGL_FAILURE; } + // Check that the basic required configuration has been done + if ((st = puglPreRealize(view))) { + return st; + } + // Getting depth from the display mode seems tedious, just set usual values if (view->hints[PUGL_RED_BITS] == PUGL_DONT_CARE) { view->hints[PUGL_RED_BITS] = 8; @@ -242,11 +250,6 @@ puglRealize(PuglView* view) return PUGL_REGISTRATION_FAILED; } - if (!view->backend || !view->backend->configure) { - return PUGL_BAD_BACKEND; - } - - PuglStatus st = PUGL_SUCCESS; if ((st = view->backend->configure(view)) || (st = view->backend->create(view))) { return st; @@ -1372,17 +1375,6 @@ puglWinCreateWindow(PuglView* const view, const unsigned winFlags = puglWinGetWindowFlags(view); const unsigned winExFlags = puglWinGetWindowExFlags(view); - // Set the size to the default if it has not already been set - if (view->frame.width <= 0.0 || view->frame.height <= 0.0) { - const PuglViewSize defaultSize = view->sizeHints[PUGL_DEFAULT_SIZE]; - if (!defaultSize.width || !defaultSize.height) { - return PUGL_BAD_CONFIGURATION; - } - - view->frame.width = defaultSize.width; - view->frame.height = defaultSize.height; - } - // Center top-level windows if a position has not been set if (!view->parent && !view->frame.x && !view->frame.y) { RECT desktopRect; @@ -357,24 +357,14 @@ puglRealize(PuglView* const view) XSetWindowAttributes attr = PUGL_INIT_STRUCT; PuglStatus st = PUGL_SUCCESS; - // Ensure that we're unrealized and that a reasonable backend has been set + // Ensure that we're unrealized if (impl->win) { return PUGL_FAILURE; } - if (!view->backend || !view->backend->configure) { - return PUGL_BAD_BACKEND; - } - - // Set the size to the default if it has not already been set - if (view->frame.width <= 0.0 || view->frame.height <= 0.0) { - const PuglViewSize defaultSize = view->sizeHints[PUGL_DEFAULT_SIZE]; - if (!defaultSize.width || !defaultSize.height) { - return PUGL_BAD_CONFIGURATION; - } - - view->frame.width = defaultSize.width; - view->frame.height = defaultSize.height; + // Check that the basic required configuration has been done + if ((st = puglPreRealize(view))) { + return st; } // Center top-level windows if a position has not been set |