From 2e8e543725e7e3b375321b93c0f5a2e684b60911 Mon Sep 17 00:00:00 2001 From: David Robillard Date: Wed, 4 Jan 2023 11:02:32 -0500 Subject: Add puglUnrealize() --- src/common.c | 6 +----- src/mac.m | 39 ++++++++++++++++++++++++++++++++++- src/win.c | 26 +++++++++++++++++++++++- src/x11.c | 66 +++++++++++++++++++++++++++++++++++++++++++++--------------- 4 files changed, 114 insertions(+), 23 deletions(-) (limited to 'src') diff --git a/src/common.c b/src/common.c index 9ea3594..c3f0818 100644 --- a/src/common.c +++ b/src/common.c @@ -1,4 +1,4 @@ -// Copyright 2012-2022 David Robillard +// Copyright 2012-2023 David Robillard // SPDX-License-Identifier: ISC // Common implementations of public API functions in the core library @@ -136,10 +136,6 @@ puglNewView(PuglWorld* const world) void puglFreeView(PuglView* view) { - if (view->eventFunc && view->backend) { - puglDispatchSimpleEvent(view, PUGL_DESTROY); - } - // Remove from world view list PuglWorld* world = view->world; for (size_t i = 0; i < world->numViews; ++i) { diff --git a/src/mac.m b/src/mac.m index 0ba95ca..658c807 100644 --- a/src/mac.m +++ b/src/mac.m @@ -1,4 +1,4 @@ -// Copyright 2012-2022 David Robillard +// Copyright 2012-2023 David Robillard // Copyright 2017 Hanspeter Portner // SPDX-License-Identifier: ISC @@ -1170,6 +1170,43 @@ puglRealize(PuglView* view) return PUGL_SUCCESS; } +PuglStatus +puglUnrealize(PuglView* const view) +{ + PuglInternals* const impl = view->impl; + if (!impl || !impl->wrapperView) { + return PUGL_FAILURE; + } + + puglDispatchSimpleEvent(view, PUGL_DESTROY); + + if (view->backend) { + view->backend->destroy(view); + } + + if (impl->wrapperView) { + [impl->wrapperView removeFromSuperview]; + impl->wrapperView->puglview = NULL; + } + + if (impl->window) { + [impl->window close]; + } + + if (impl->wrapperView) { + [impl->wrapperView release]; + impl->wrapperView = NULL; + } + + if (impl->window) { + [impl->window release]; + impl->window = NULL; + } + + memset(&view->lastConfigure, 0, sizeof(PuglConfigureEvent)); + return PUGL_SUCCESS; +} + PuglStatus puglShow(PuglView* view) { diff --git a/src/win.c b/src/win.c index e41222b..13ad090 100644 --- a/src/win.c +++ b/src/win.c @@ -1,4 +1,4 @@ -// Copyright 2012-2022 David Robillard +// Copyright 2012-2023 David Robillard // SPDX-License-Identifier: ISC #include "win.h" @@ -274,6 +274,30 @@ puglRealize(PuglView* view) return PUGL_SUCCESS; } +PuglStatus +puglUnrealize(PuglView* const view) +{ + PuglInternals* const impl = view->impl; + if (!impl || !impl->hwnd) { + return PUGL_FAILURE; + } + + puglDispatchSimpleEvent(view, PUGL_DESTROY); + + if (view->backend) { + view->backend->destroy(view); + } + + ReleaseDC(view->impl->hwnd, view->impl->hdc); + view->impl->hdc = NULL; + + DestroyWindow(view->impl->hwnd); + view->impl->hwnd = NULL; + + memset(&view->lastConfigure, 0, sizeof(PuglConfigureEvent)); + return PUGL_SUCCESS; +} + PuglStatus puglShow(PuglView* view) { diff --git a/src/x11.c b/src/x11.c index 271a0fb..1dc4f9d 100644 --- a/src/x11.c +++ b/src/x11.c @@ -1,4 +1,4 @@ -// Copyright 2012-2022 David Robillard +// Copyright 2012-2023 David Robillard // Copyright 2013 Robin Gareus // Copyright 2011-2012 Ben Loftis, Harrison Consoles // SPDX-License-Identifier: ISC @@ -344,6 +344,21 @@ defineCursorName(PuglView* const view, const char* const name) } #endif +static void +clearX11Clipboard(PuglX11Clipboard* const board) +{ + for (unsigned long i = 0; i < board->numFormats; ++i) { + free(board->formatStrings[i]); + board->formatStrings[i] = NULL; + } + + board->source = None; + board->numFormats = 0; + board->acceptedFormatIndex = UINT32_MAX; + board->acceptedFormat = None; + board->data.len = 0; +} + PuglStatus puglRealize(PuglView* const view) { @@ -473,6 +488,40 @@ puglRealize(PuglView* const view) return PUGL_SUCCESS; } +PuglStatus +puglUnrealize(PuglView* const view) +{ + PuglInternals* const impl = view->impl; + if (!impl || !impl->win) { + return PUGL_FAILURE; + } + + puglDispatchSimpleEvent(view, PUGL_DESTROY); + clearX11Clipboard(&impl->clipboard); + + if (impl->xic) { + XDestroyIC(impl->xic); + impl->xic = None; + } + + if (view->backend) { + view->backend->destroy(view); + } + + if (view->world->impl->display && impl->win) { + XDestroyWindow(view->world->impl->display, impl->win); + impl->win = None; + } + + XFree(impl->vi); + impl->vi = NULL; + + memset(&view->lastConfigure, 0, sizeof(PuglConfigureEvent)); + memset(&view->impl->pendingConfigure, 0, sizeof(PuglEvent)); + memset(&view->impl->pendingExpose, 0, sizeof(PuglEvent)); + return PUGL_SUCCESS; +} + PuglStatus puglShow(PuglView* const view) { @@ -493,21 +542,6 @@ puglHide(PuglView* const view) return PUGL_SUCCESS; } -static void -clearX11Clipboard(PuglX11Clipboard* const board) -{ - for (unsigned long i = 0; i < board->numFormats; ++i) { - free(board->formatStrings[i]); - board->formatStrings[i] = NULL; - } - - board->source = None; - board->numFormats = 0; - board->acceptedFormatIndex = UINT32_MAX; - board->acceptedFormat = None; - board->data.len = 0; -} - void puglFreeViewInternals(PuglView* const view) { -- cgit v1.2.1