Remove libc function from Windows tray

This commit is contained in:
Semphris
2024-12-29 15:32:09 -05:00
committed by Ozkan Sezer
parent 78c1dc3e2c
commit 2d91f096ca
2 changed files with 18 additions and 24 deletions

View File

@@ -102,8 +102,8 @@ typedef void (SDLCALL *SDL_TrayCallback)(void *userdata, SDL_TrayEntry *entry);
* Using tray icons require the video subsystem. * Using tray icons require the video subsystem.
* *
* \param icon a surface to be used as icon. May be NULL. * \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. * \param tooltip a tooltip to be displayed when the mouse hovers the icon in
* Not supported on all platforms. May be NULL. * UTF-8 encoding. Not supported on all platforms. May be NULL.
* \returns The newly created system tray icon. * \returns The newly created system tray icon.
* *
* \since This function is available since SDL 3.2.0. * \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. * Updates the system tray icon's tooltip.
* *
* \param tray the tray icon to be updated. * \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. * \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 menu the menu to append the entry to.
* \param pos the desired position for the new entry. Entries at or following * \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. * 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 * \param label the text to be displayed on the entry, in UTF-8 encoding, or
* separator. * NULL for a separator.
* \param flags a combination of flags, some of which are mandatory. * \param flags a combination of flags, some of which are mandatory.
* \returns the newly created entry, or NULL if pos is out of bounds. * \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. * label. The function will silently fail if that happens.
* *
* \param entry the entry to be updated. * \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. * \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. * If the returned value is NULL, the entry is a separator.
* *
* \param entry the entry to be read. * \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. * \since This function is available since SDL 3.2.0.
* *

View File

@@ -26,7 +26,6 @@
#include <windowsx.h> #include <windowsx.h>
#include <shellapi.h> #include <shellapi.h>
#include <stdlib.h> /* FIXME: for mbstowcs_s, wcslen */
#include "../../video/windows/SDL_surface_utils.h" #include "../../video/windows/SDL_surface_utils.h"
@@ -150,7 +149,7 @@ static void DestroySDLMenu(SDL_TrayMenu *menu)
SDL_free(menu); SDL_free(menu);
} }
static wchar_t *convert_label(const char *in) static wchar_t *escape_label(const char *in)
{ {
const char *c; const char *c;
char *c2; char *c2;
@@ -176,16 +175,7 @@ static wchar_t *convert_label(const char *in)
*c2 = '\0'; *c2 = '\0';
int len_w = MultiByteToWideChar(CP_UTF8, 0, escaped, len + 1, NULL, 0); wchar_t *out = WIN_UTF8ToStringW(escaped);
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);
SDL_free(escaped); SDL_free(escaped);
return out; 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.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP | NIF_SHOWTIP;
tray->nid.uCallbackMessage = WM_TRAYICON; tray->nid.uCallbackMessage = WM_TRAYICON;
tray->nid.uVersion = NOTIFYICON_VERSION_4; 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) { if (icon) {
tray->nid.hIcon = CreateIconFromSurface(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) void SDL_SetTrayTooltip(SDL_Tray *tray, const char *tooltip)
{ {
if (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 { } else {
tray->nid.szTip[0] = '\0'; 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; wchar_t *label_w = NULL;
if (label && !(label_w = convert_label(label))) { if (label && !(label_w = escape_label(label))) {
SDL_free(entry); SDL_free(entry);
return NULL; 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); 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) { if (!label_w) {
return; return;
@@ -464,7 +458,7 @@ void SDL_SetTrayEntryLabel(SDL_TrayEntry *entry, const char *label)
mii.fMask = MIIM_STRING; mii.fMask = MIIM_STRING;
mii.dwTypeData = label_w; 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)) { if (!SetMenuItemInfoW(entry->parent->hMenu, (UINT) entry->id, TRUE, &mii)) {
SDL_SetError("Couldn't update tray entry label"); SDL_SetError("Couldn't update tray entry label");