SDL: Remove libc function from Windows tray

From 2d91f096cae00ad06627339519d876ab2df8248e Mon Sep 17 00:00:00 2001
From: Semphris <[EMAIL REDACTED]>
Date: Sun, 29 Dec 2024 15:32:09 -0500
Subject: [PATCH] Remove libc function from Windows tray

---
 include/SDL3/SDL_tray.h     | 14 +++++++-------
 src/tray/windows/SDL_tray.c | 28 +++++++++++-----------------
 2 files changed, 18 insertions(+), 24 deletions(-)

diff --git a/include/SDL3/SDL_tray.h b/include/SDL3/SDL_tray.h
index 8e81a0c38596f..9b31aad78d6c1 100644
--- a/include/SDL3/SDL_tray.h
+++ b/include/SDL3/SDL_tray.h
@@ -102,8 +102,8 @@ typedef void (SDLCALL *SDL_TrayCallback)(void *userdata, SDL_TrayEntry *entry);
  * Using tray icons require the video subsystem.
  *
  * \param icon a surface to be used as icon. May be NULL.
- * \param tooltip a tooltip to be displayed when the mouse hovers the icon.
- *                Not supported on all platforms. May be NULL.
+ * \param tooltip a tooltip to be displayed when the mouse hovers the icon in
+ *                UTF-8 encoding. Not supported on all platforms. May be NULL.
  * \returns The newly created system tray icon.
  *
  * \since This function is available since SDL 3.2.0.
@@ -130,7 +130,7 @@ extern SDL_DECLSPEC void SDLCALL SDL_SetTrayIcon(SDL_Tray *tray, SDL_Surface *ic
  * Updates the system tray icon's tooltip.
  *
  * \param tray the tray icon to be updated.
- * \param tooltip the new tooltip. May be NULL.
+ * \param tooltip the new tooltip in UTF-8 encoding. May be NULL.
  *
  * \since This function is available since SDL 3.2.0.
  *
@@ -262,8 +262,8 @@ extern SDL_DECLSPEC void SDLCALL SDL_RemoveTrayEntry(SDL_TrayEntry *entry);
  * \param menu the menu to append the entry to.
  * \param pos the desired position for the new entry. Entries at or following
  *            this place will be moved. If pos is -1, the entry is appended.
- * \param label the text to be displayed on the entry, or NULL for a
- *              separator.
+ * \param label the text to be displayed on the entry, in UTF-8 encoding, or
+ *              NULL for a separator.
  * \param flags a combination of flags, some of which are mandatory.
  * \returns the newly created entry, or NULL if pos is out of bounds.
  *
@@ -285,7 +285,7 @@ extern SDL_DECLSPEC SDL_TrayEntry *SDLCALL SDL_InsertTrayEntryAt(SDL_TrayMenu *m
  * label. The function will silently fail if that happens.
  *
  * \param entry the entry to be updated.
- * \param label the new label for the entry.
+ * \param label the new label for the entry in UTF-8 encoding.
  *
  * \since This function is available since SDL 3.2.0.
  *
@@ -301,7 +301,7 @@ extern SDL_DECLSPEC void SDLCALL SDL_SetTrayEntryLabel(SDL_TrayEntry *entry, con
  * If the returned value is NULL, the entry is a separator.
  *
  * \param entry the entry to be read.
- * \returns the label of the entry.
+ * \returns the label of the entry in UTF-8 encoding.
  *
  * \since This function is available since SDL 3.2.0.
  *
diff --git a/src/tray/windows/SDL_tray.c b/src/tray/windows/SDL_tray.c
index ba1a560e4972d..03b8119f97577 100644
--- a/src/tray/windows/SDL_tray.c
+++ b/src/tray/windows/SDL_tray.c
@@ -26,7 +26,6 @@
 
 #include <windowsx.h>
 #include <shellapi.h>
-#include <stdlib.h> /* FIXME: for mbstowcs_s, wcslen */
 
 #include "../../video/windows/SDL_surface_utils.h"
 
@@ -150,7 +149,7 @@ static void DestroySDLMenu(SDL_TrayMenu *menu)
     SDL_free(menu);
 }
 
-static wchar_t *convert_label(const char *in)
+static wchar_t *escape_label(const char *in)
 {
     const char *c;
     char *c2;
@@ -176,16 +175,7 @@ static wchar_t *convert_label(const char *in)
 
     *c2 = '\0';
 
-    int len_w = MultiByteToWideChar(CP_UTF8, 0, escaped, len + 1, NULL, 0);
-    wchar_t *out = (wchar_t *)SDL_malloc(len_w * sizeof(wchar_t));
-
-    if (!out) {
-        SDL_free(escaped);
-        return NULL;
-    }
-
-    MultiByteToWideChar(CP_UTF8, 0, escaped, -1, out, len_w);
-
+    wchar_t *out = WIN_UTF8ToStringW(escaped);
     SDL_free(escaped);
 
     return out;
@@ -210,7 +200,9 @@ SDL_Tray *SDL_CreateTray(SDL_Surface *icon, const char *tooltip)
     tray->nid.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP | NIF_SHOWTIP;
     tray->nid.uCallbackMessage = WM_TRAYICON;
     tray->nid.uVersion = NOTIFYICON_VERSION_4;
-    mbstowcs_s(NULL, tray->nid.szTip, sizeof(tray->nid.szTip) / sizeof(*tray->nid.szTip), tooltip, _TRUNCATE);
+    wchar_t *tooltipw = WIN_UTF8ToStringW(tooltip);
+    SDL_wcslcpy(tray->nid.szTip, tooltipw, sizeof(tray->nid.szTip) / sizeof(*tray->nid.szTip));
+    SDL_free(tooltipw);
 
     if (icon) {
         tray->nid.hIcon = CreateIconFromSurface(icon);
@@ -260,7 +252,9 @@ void SDL_SetTrayIcon(SDL_Tray *tray, SDL_Surface *icon)
 void SDL_SetTrayTooltip(SDL_Tray *tray, const char *tooltip)
 {
     if (tooltip) {
-        mbstowcs_s(NULL, tray->nid.szTip, sizeof(tray->nid.szTip) / sizeof(*tray->nid.szTip), tooltip, _TRUNCATE);
+        wchar_t *tooltipw = WIN_UTF8ToStringW(tooltip);
+        SDL_wcslcpy(tray->nid.szTip, tooltipw, sizeof(tray->nid.szTip) / sizeof(*tray->nid.szTip));
+        SDL_free(tooltipw);
     } else {
         tray->nid.szTip[0] = '\0';
     }
@@ -374,7 +368,7 @@ SDL_TrayEntry *SDL_InsertTrayEntryAt(SDL_TrayMenu *menu, int pos, const char *la
 
     wchar_t *label_w = NULL;
 
-    if (label && !(label_w = convert_label(label))) {
+    if (label && !(label_w = escape_label(label))) {
         SDL_free(entry);
         return NULL;
     }
@@ -453,7 +447,7 @@ void SDL_SetTrayEntryLabel(SDL_TrayEntry *entry, const char *label)
 {
     SDL_snprintf(entry->label_cache, sizeof(entry->label_cache), "%s", label);
 
-    wchar_t *label_w = convert_label(label);
+    wchar_t *label_w = escape_label(label);
 
     if (!label_w) {
         return;
@@ -464,7 +458,7 @@ void SDL_SetTrayEntryLabel(SDL_TrayEntry *entry, const char *label)
     mii.fMask = MIIM_STRING;
 
     mii.dwTypeData = label_w;
-    mii.cch = (UINT) wcslen(label_w);
+    mii.cch = (UINT) SDL_wcslen(label_w);
 
     if (!SetMenuItemInfoW(entry->parent->hMenu, (UINT) entry->id, TRUE, &mii)) {
         SDL_SetError("Couldn't update tray entry label");