From 23c2bc20bb8ba503fd9466eb6d259c438f21673f Mon Sep 17 00:00:00 2001 From: MysterD Date: Mon, 2 Aug 2021 18:57:19 -0700 Subject: [PATCH] Prevent segfault when hovering over a DJUI element --- src/pc/djui/djui_base.c | 5 +++++ src/pc/djui/djui_interactable.c | 27 +++++++++++++-------------- src/pc/djui/djui_interactable.h | 1 + 3 files changed, 19 insertions(+), 14 deletions(-) diff --git a/src/pc/djui/djui_base.c b/src/pc/djui/djui_base.c index 535fafd0..ed1f575f 100644 --- a/src/pc/djui/djui_base.c +++ b/src/pc/djui/djui_base.c @@ -319,6 +319,11 @@ bool djui_base_render(struct DjuiBase* base) { } void djui_base_destroy(struct DjuiBase* base) { + // remove hovered status + if (gDjuiHovered == base) { + gDjuiHovered = NULL; + } + // remove myself from parent's linked list if (base->parent != NULL) { struct DjuiBaseChild* child = base->parent->child; diff --git a/src/pc/djui/djui_interactable.c b/src/pc/djui/djui_interactable.c index 2fc78949..6f737137 100644 --- a/src/pc/djui/djui_interactable.c +++ b/src/pc/djui/djui_interactable.c @@ -15,9 +15,9 @@ static u16 sKeyboardButtons = 0; static bool sIgnoreInteractableUntilCursorReleased = false; +struct DjuiBase* gDjuiHovered = NULL; static struct DjuiBase* sInteractableFocus = NULL; static struct DjuiBase* sInteractableBinding = NULL; -static struct DjuiBase* sHovered = NULL; static struct DjuiBase* sMouseDown = NULL; bool gInteractableOverridePad = false; OSContPad gInteractablePad = { 0 }; @@ -50,9 +50,9 @@ static void djui_interactable_on_cursor_down_begin(struct DjuiBase* base, bool i if (base->interactable == NULL) { return; } if (base->interactable->on_cursor_down_begin == NULL) { return; } - if (sHovered != NULL) { - djui_interactable_on_hover_end(sHovered); - sHovered = NULL; + if (gDjuiHovered != NULL) { + djui_interactable_on_hover_end(gDjuiHovered); + gDjuiHovered = NULL; } base->interactable->on_cursor_down_begin(base, inputCursor); @@ -121,10 +121,10 @@ static void djui_interactable_cursor_update_active(struct DjuiBase* base) { if (!djui_cursor_inside_base(base)) { return; } if (base->interactable != NULL) { - sHovered = base; + gDjuiHovered = base; insideParent = base; } else if (insideParent == NULL) { - sHovered = NULL; + gDjuiHovered = NULL; } // check all children @@ -330,9 +330,9 @@ void djui_interactable_update(void) { djui_interactable_on_bind(sInteractableBinding); } else if ((padButtons & PAD_BUTTON_A) || (mouseButtons & MOUSE_BUTTON_1)) { // cursor down events - if (sHovered != NULL) { - sMouseDown = sHovered; - sHovered = NULL; + if (gDjuiHovered != NULL) { + sMouseDown = gDjuiHovered; + gDjuiHovered = NULL; djui_interactable_on_cursor_down_begin(sMouseDown, !mouseButtons); } else { djui_interactable_on_cursor_down(sMouseDown); @@ -343,15 +343,14 @@ void djui_interactable_update(void) { djui_interactable_on_cursor_down_end(sMouseDown); sMouseDown = NULL; } - struct DjuiBase* lastHovered = sHovered; - sHovered = NULL; + struct DjuiBase* lastHovered = gDjuiHovered; + gDjuiHovered = NULL; djui_interactable_cursor_update_active(&gDjuiRoot->base); - if (lastHovered != sHovered) { - // FIXME: THIS CAN CAUSE A SEGFAULT! + if (lastHovered != gDjuiHovered) { djui_interactable_on_hover_end(lastHovered); play_sound(SOUND_MENU_MESSAGE_NEXT_PAGE, gDefaultSoundArgs); } - djui_interactable_on_hover(sHovered); + djui_interactable_on_hover(gDjuiHovered); } sLastInteractablePad = gInteractablePad; diff --git a/src/pc/djui/djui_interactable.h b/src/pc/djui/djui_interactable.h index 2fba0528..36e7c620 100644 --- a/src/pc/djui/djui_interactable.h +++ b/src/pc/djui/djui_interactable.h @@ -42,6 +42,7 @@ struct DjuiInteractable { extern bool gInteractableOverridePad; extern OSContPad gInteractablePad; +extern struct DjuiBase* gDjuiHovered; bool djui_interactable_is_binding(void); void djui_interactable_set_binding(struct DjuiBase* base);