Fix tray messages with different code page

This commit is contained in:
Yukino Song
2024-08-17 01:49:24 +08:00
parent 0826abf607
commit 89677241f8
2 changed files with 53 additions and 4 deletions

32
src/misc.h Normal file
View File

@@ -0,0 +1,32 @@
#pragma once
#include <string>
#ifdef _WIN32
#include <windows.h>
std::string convertUtf8ToCurrentCodepage(const std::string& utf8Str) {
if (GetACP() == CP_UTF8) {
return std::string(utf8Str);
}
// Step 1: Convert UTF-8 to UTF-16
int utf16Len = MultiByteToWideChar(CP_UTF8, 0, utf8Str.c_str(), -1, NULL, 0);
if (utf16Len == 0) {
throw std::runtime_error("Failed to convert UTF-8 to UTF-16");
}
std::wstring utf16Str(utf16Len, L'\0');
MultiByteToWideChar(CP_UTF8, 0, utf8Str.c_str(), -1, &utf16Str[0], utf16Len);
// Step 2: Convert UTF-16 to the current Windows codepage
int codepageLen = WideCharToMultiByte(GetACP(), 0, utf16Str.c_str(), -1, NULL, 0, NULL, NULL);
if (codepageLen == 0) {
throw std::runtime_error("Failed to convert UTF-16 to current Windows codepage");
}
std::string codepageStr(codepageLen, '\0');
WideCharToMultiByte(GetACP(), 0, utf16Str.c_str(), -1, &codepageStr[0], codepageLen, NULL, NULL);
return codepageStr;
}
#endif

View File

@@ -46,6 +46,7 @@
#include "network.h"
#include "src/entry_handler.h"
#include "version.h"
#include "misc.h"
using namespace std::literals;
@@ -222,8 +223,12 @@ namespace system_tray {
#else // Windows, Linux
// create tray in separate thread
#ifdef _WIN32
std::string tmp_str = "Open Apollo (" + config::nvhttp.sunshine_name + ":" + std::to_string(net::map_port(confighttp::PORT_HTTPS)) + ")";
static const std::string title_str = convertUtf8ToCurrentCodepage(tmp_str);
#else
static const std::string title_str = "Open Apollo (" + config::nvhttp.sunshine_name + ":" + std::to_string(net::map_port(confighttp::PORT_HTTPS)) + ")";
#endif
tray.menu[0].text = title_str.c_str();
std::thread tray_thread(system_tray);
@@ -244,6 +249,8 @@ namespace system_tray {
return;
}
printf("Tray playing: %s\n", app_name.c_str());
tray.notification_title = NULL;
tray.notification_text = NULL;
tray.notification_cb = NULL;
@@ -254,12 +261,16 @@ namespace system_tray {
tray.icon = TRAY_ICON_PLAYING;
tray.notification_title = "Stream Started";
char msg[256];
static char force_close_msg[256];
snprintf(msg, std::size(msg), "Streaming started for %s", app_name.c_str());
snprintf(force_close_msg, std::size(force_close_msg), "Force close [%s]", app_name.c_str());
#ifdef _WIN32
strcpy(msg, convertUtf8ToCurrentCodepage(msg).c_str());
strcpy(force_close_msg, convertUtf8ToCurrentCodepage(force_close_msg).c_str());
#endif
tray.notification_text = msg;
tray.tooltip = msg;
tray.notification_icon = TRAY_ICON_PLAYING;
static char force_close_msg[256];
snprintf(force_close_msg, std::size(force_close_msg), "Force close [%s]", app_name.c_str());
tray.menu[1].text = force_close_msg;
tray_update(&tray);
}
@@ -278,6 +289,9 @@ namespace system_tray {
tray_update(&tray);
char msg[256];
snprintf(msg, std::size(msg), "Streaming paused for %s", app_name.c_str());
#ifdef _WIN32
strcpy(msg, convertUtf8ToCurrentCodepage(msg).c_str());
#endif
tray.icon = TRAY_ICON_PAUSING;
tray.notification_title = "Stream Paused";
tray.notification_text = msg;
@@ -299,7 +313,10 @@ namespace system_tray {
tray.icon = TRAY_ICON;
tray_update(&tray);
char msg[256];
snprintf(msg, std::size(msg), "Application %s successfully stopped", app_name.c_str());
snprintf(msg, std::size(msg), "Streaming paused for %s", app_name.c_str());
#ifdef _WIN32
strcpy(msg, convertUtf8ToCurrentCodepage(msg).c_str());
#endif
tray.icon = TRAY_ICON;
tray.notification_icon = TRAY_ICON;
tray.notification_title = "Application Stopped";