From 542c4e315bdd1d11ced5cb6ec9e390aa327e11f1 Mon Sep 17 00:00:00 2001 From: Yukino Song Date: Mon, 26 May 2025 17:33:00 +0800 Subject: [PATCH] misc --- src/platform/windows/display_wgc.cpp | 10 ++--- src/platform/windows/misc.cpp | 4 +- src/platform/windows/misc.h | 4 +- src/platform/windows/virtual_display.cpp | 20 ++++----- tools/CMakeLists.txt | 4 +- tools/audio.cpp | 5 +-- tools/utils.cpp | 55 ++++++++++++++++++++++++ tools/utils.h | 15 +++++++ 8 files changed, 93 insertions(+), 24 deletions(-) create mode 100644 tools/utils.cpp create mode 100644 tools/utils.h diff --git a/src/platform/windows/display_wgc.cpp b/src/platform/windows/display_wgc.cpp index 5b31dcae..3e81902f 100644 --- a/src/platform/windows/display_wgc.cpp +++ b/src/platform/windows/display_wgc.cpp @@ -5,11 +5,6 @@ // platform includes #include -// local includes -#include "display.h" -#include "misc.h" -#include "src/logging.h" - // Gross hack to work around MINGW-packages#22160 #define ____FIReference_1_boolean_INTERFACE_DEFINED__ @@ -18,6 +13,11 @@ #include #include +// local includes +#include "display.h" +#include "misc.h" +#include "src/logging.h" + namespace platf { using namespace std::literals; } diff --git a/src/platform/windows/misc.cpp b/src/platform/windows/misc.cpp index b21287d3..cf04d0f2 100644 --- a/src/platform/windows/misc.cpp +++ b/src/platform/windows/misc.cpp @@ -1744,7 +1744,7 @@ namespace platf { return {}; } - std::wstring from_utf8(const std::string &string) { + std::wstring from_utf8(const std::string_view &string) { // No conversion needed if the string is empty if (string.empty()) { return {}; @@ -1770,7 +1770,7 @@ namespace platf { return output; } - std::string to_utf8(const std::wstring &string) { + std::string to_utf8(const std::wstring_view &string) { // No conversion needed if the string is empty if (string.empty()) { return {}; diff --git a/src/platform/windows/misc.h b/src/platform/windows/misc.h index 30d85376..c996e677 100644 --- a/src/platform/windows/misc.h +++ b/src/platform/windows/misc.h @@ -25,12 +25,12 @@ namespace platf { * @param string The UTF-8 string. * @return The converted UTF-16 wide string. */ - std::wstring from_utf8(const std::string &string); + std::wstring from_utf8(const std::string_view &string); /** * @brief Convert a UTF-16 wide string into a UTF-8 string. * @param string The UTF-16 wide string. * @return The converted UTF-8 string. */ - std::string to_utf8(const std::wstring &string); + std::string to_utf8(const std::wstring_view &string); } // namespace platf diff --git a/src/platform/windows/virtual_display.cpp b/src/platform/windows/virtual_display.cpp index 4594ad16..7c649838 100644 --- a/src/platform/windows/virtual_display.cpp +++ b/src/platform/windows/virtual_display.cpp @@ -18,7 +18,7 @@ using namespace SUDOVDA; namespace VDISPLAY { // {dff7fd29-5b75-41d1-9731-b32a17a17104} -static const GUID DEFAULT_DISPLAY_GUID = { 0xdff7fd29, 0x5b75, 0x41d1, { 0x97, 0x31, 0xb3, 0x2a, 0x17, 0xa1, 0x71, 0x04 } }; +// static const GUID DEFAULT_DISPLAY_GUID = { 0xdff7fd29, 0x5b75, 0x41d1, { 0x97, 0x31, 0xb3, 0x2a, 0x17, 0xa1, 0x71, 0x04 } }; HANDLE SUDOVDA_DRIVER_HANDLE = INVALID_HANDLE_VALUE; @@ -91,7 +91,7 @@ LONG changeDisplaySettings2(const wchar_t* deviceName, int width, int height, in sourceName.header.size = sizeof(sourceName); sourceName.header.adapterId = pathArray[i].sourceInfo.adapterId; sourceName.header.id = pathArray[i].sourceInfo.id; - bAtVirtualDisplay = false; + bAtVirtualDisplay = false; if (DisplayConfigGetDeviceInfo(&sourceName.header) != ERROR_SUCCESS) { continue; @@ -116,7 +116,7 @@ LONG changeDisplaySettings2(const wchar_t* deviceName, int width, int height, in auto* sourceMode = &modeArray[j].sourceMode; wprintf(L"[SUDOVDA] Current mode found: [%dx%dx%d]\n", sourceMode->width, sourceMode->height, targetInfo->refreshRate); - + pCurrentElement = new (struct positionwidthheight); pCurrentElement->position.x = modeArray[j].sourceMode.position.x; @@ -152,7 +152,7 @@ LONG changeDisplaySettings2(const wchar_t* deviceName, int width, int height, in int xdifference, ydifference = 0; for (iIndex = 0; iIndex < displayArray.size(); iIndex += 1) { - + // Find the primary display and get the offset to apply to all of the displays to keep the same primary if( modeArray[(displayArray[iIndex]->modeindex)].sourceMode.position.x == 0 && modeArray[(displayArray[iIndex]->modeindex)].sourceMode.position.y == 0 ) @@ -162,7 +162,7 @@ LONG changeDisplaySettings2(const wchar_t* deviceName, int width, int height, in break; } } - + // Set all of the OS Displays to their new locations; Do not change the primary // Update the real vector for the system call for (iIndex = 0; iIndex < displayArray.size(); iIndex += 1) @@ -263,7 +263,7 @@ LONG changeDisplaySettings2(const wchar_t* deviceName, int width, int height, in } LONG changeDisplaySettings(const wchar_t* deviceName, int width, int height, int refresh_rate) { - DEVMODEW devMode = {0}; + DEVMODEW devMode = {}; devMode.dmSize = sizeof(devMode); // Old method to set at least baseline refresh rate @@ -725,7 +725,7 @@ std::string printAllDisplays(std::vector< struct positionwidthheight*> displays) return sOutput; } -// Helper method for the rearrangeVirtualDisplayForLowerRight() method to move the unknown unconnected display to be connected to the +// Helper method for the rearrangeVirtualDisplayForLowerRight() method to move the unknown unconnected display to be connected to the // second display which is assumed to be already connected // // It will return the move that the unknown display would need to perform @@ -914,7 +914,7 @@ std::vector < struct coordinates > moveToBeConnected(std::vector < struct coordi std::vector< struct positionwidthheight*>rearrangeVirtualDisplayForLowerRight(std::vector< struct positionwidthheight*> displays) { // Make a temporary connected List based on the current Displays - // Here connected means that the displays are "touching" by either the + // Here connected means that the displays are "touching" by either the // vertical axis or a horizontal axis or a corner. int count = displays.size(); std::vector< int > vConnected(count, 0); @@ -969,7 +969,7 @@ std::vector< struct positionwidthheight*>rearrangeVirtualDisplayForLowerRight(st } } - // Get the location, width and height of the window that is moving + // Get the location, width and height of the window that is moving // Make sure the correct display is set to 0,0. for (int index = 0; index < count; index++) { if (index == changeIndex) { @@ -1068,7 +1068,7 @@ std::vector< struct positionwidthheight*>rearrangeVirtualDisplayForLowerRight(st // The result of this may not be used as there may be a closer display when we go through the list std::vector < struct coordinates > sToMove = moveToBeConnected(secondboxpoints, connectedboxpoints); - // No movement necessary + // No movement necessary if (sToMove[0].x == 0 && sToMove[0].y == 0) { vConnected[secondboxindex] = 1; diff --git a/tools/CMakeLists.txt b/tools/CMakeLists.txt index 8d4c08e3..70cd879f 100644 --- a/tools/CMakeLists.txt +++ b/tools/CMakeLists.txt @@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 3.20) project(sunshine_tools) -include_directories("${CMAKE_SOURCE_DIR}") +include_directories(${CMAKE_SOURCE_DIR}) add_executable(dxgi-info dxgi.cpp) set_target_properties(dxgi-info PROPERTIES CXX_STANDARD 20) @@ -12,7 +12,7 @@ target_link_libraries(dxgi-info ${PLATFORM_LIBRARIES}) target_compile_options(dxgi-info PRIVATE ${SUNSHINE_COMPILE_OPTIONS}) -add_executable(audio-info audio.cpp) +add_executable(audio-info audio.cpp utils.cpp) set_target_properties(audio-info PROPERTIES CXX_STANDARD 20) target_link_libraries(audio-info ${CMAKE_THREAD_LIBS_INIT} diff --git a/tools/audio.cpp b/tools/audio.cpp index 470b9796..cefeebd1 100644 --- a/tools/audio.cpp +++ b/tools/audio.cpp @@ -4,6 +4,7 @@ */ #define INITGUID #include "src/utility.h" +#include "utils.h" #include #include @@ -46,8 +47,6 @@ namespace audio { using handle_t = util::safe_ptr_v2; - static std::wstring_convert, wchar_t> converter; - class prop_var_t { public: prop_var_t() { @@ -206,7 +205,7 @@ namespace audio { // so we can take the first match as the current format to display. auto audio_client = make_audio_client(device, format); if (audio_client) { - current_format = converter.from_bytes(format.name.data()); + current_format = from_utf8(format.name); break; } } diff --git a/tools/utils.cpp b/tools/utils.cpp new file mode 100644 index 00000000..bc8a88e7 --- /dev/null +++ b/tools/utils.cpp @@ -0,0 +1,55 @@ +#include "utils.h" + +#include + +std::wstring from_utf8(const std::string_view &string) { + // No conversion needed if the string is empty + if (string.empty()) { + return {}; + } + + // Get the output size required to store the string + auto output_size = MultiByteToWideChar(CP_UTF8, 0, string.data(), string.size(), nullptr, 0); + if (output_size == 0) { + // auto winerr = GetLastError(); + // BOOST_LOG(error) << "Failed to get UTF-16 buffer size: "sv << winerr; + return {}; + } + + // Perform the conversion + std::wstring output(output_size, L'\0'); + output_size = MultiByteToWideChar(CP_UTF8, 0, string.data(), string.size(), output.data(), output.size()); + if (output_size == 0) { + // auto winerr = GetLastError(); + // BOOST_LOG(error) << "Failed to convert string to UTF-16: "sv << winerr; + return {}; + } + + return output; +} + +std::string to_utf8(const std::wstring_view &string) { + // No conversion needed if the string is empty + if (string.empty()) { + return {}; + } + + // Get the output size required to store the string + auto output_size = WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, string.data(), string.size(), nullptr, 0, nullptr, nullptr); + if (output_size == 0) { + // auto winerr = GetLastError(); + // BOOST_LOG(error) << "Failed to get UTF-8 buffer size: "sv << winerr; + return {}; + } + + // Perform the conversion + std::string output(output_size, '\0'); + output_size = WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, string.data(), string.size(), output.data(), output.size(), nullptr, nullptr); + if (output_size == 0) { + // auto winerr = GetLastError(); + // BOOST_LOG(error) << "Failed to convert string to UTF-8: "sv << winerr; + return {}; + } + + return output; +} \ No newline at end of file diff --git a/tools/utils.h b/tools/utils.h new file mode 100644 index 00000000..03b874d0 --- /dev/null +++ b/tools/utils.h @@ -0,0 +1,15 @@ +#include + +/** + * @brief Convert a UTF-8 string into a UTF-16 wide string. + * @param string The UTF-8 string. + * @return The converted UTF-16 wide string. + */ +std::wstring from_utf8(const std::string_view &string); + +/** + * @brief Convert a UTF-16 wide string into a UTF-8 string. + * @param string The UTF-16 wide string. + * @return The converted UTF-8 string. + */ +std::string to_utf8(const std::wstring_view &string); \ No newline at end of file