diff options
Diffstat (limited to '')
-rw-r--r-- | src/common/cityhash.h | 1 | ||||
-rw-r--r-- | src/common/common_funcs.h | 6 | ||||
-rw-r--r-- | src/common/misc.cpp | 44 | ||||
-rw-r--r-- | src/core/core.cpp | 13 | ||||
-rw-r--r-- | src/core/hle/service/hid/controllers/gesture.cpp | 159 | ||||
-rw-r--r-- | src/core/hle/service/hid/controllers/gesture.h | 92 | ||||
-rw-r--r-- | src/core/hle/service/ldn/ldn.cpp | 2 | ||||
-rw-r--r-- | src/core/network/network.cpp | 173 | ||||
-rw-r--r-- | src/core/network/network.h | 6 | ||||
-rw-r--r-- | src/input_common/udp/client.cpp | 26 | ||||
-rw-r--r-- | src/input_common/udp/client.h | 6 | ||||
-rw-r--r-- | src/tests/CMakeLists.txt | 1 | ||||
-rw-r--r-- | src/tests/core/network/network.cpp | 28 | ||||
-rw-r--r-- | src/video_core/gpu.cpp | 2 | ||||
-rw-r--r-- | src/video_core/gpu_thread.cpp | 13 | ||||
-rw-r--r-- | src/video_core/gpu_thread.h | 19 | ||||
-rw-r--r-- | src/video_core/renderer_vulkan/vk_command_pool.cpp | 2 | ||||
-rw-r--r-- | src/video_core/shader_notify.cpp | 1 | ||||
-rw-r--r-- | src/yuzu/configuration/config.cpp | 4 | ||||
-rw-r--r-- | src/yuzu/configuration/configure_motion_touch.cpp | 8 | ||||
-rw-r--r-- | src/yuzu/configuration/configure_motion_touch.h | 2 | ||||
-rw-r--r-- | src/yuzu_cmd/config.cpp | 2 |
22 files changed, 426 insertions, 184 deletions
diff --git a/src/common/cityhash.h b/src/common/cityhash.h index 022d0f7cb..d74fc7639 100644 --- a/src/common/cityhash.h +++ b/src/common/cityhash.h @@ -61,6 +61,7 @@ #pragma once +#include <cstddef> #include "common/common_types.h" namespace Common { diff --git a/src/common/common_funcs.h b/src/common/common_funcs.h index 71b64e32a..4ace2cd33 100644 --- a/src/common/common_funcs.h +++ b/src/common/common_funcs.h @@ -52,9 +52,13 @@ __declspec(dllimport) void __stdcall DebugBreak(void); // Generic function to get last error message. // Call directly after the command or use the error num. // This function might change the error code. -// Defined in Misc.cpp. +// Defined in misc.cpp. [[nodiscard]] std::string GetLastErrorMsg(); +// Like GetLastErrorMsg(), but passing an explicit error code. +// Defined in misc.cpp. +[[nodiscard]] std::string NativeErrorToString(int e); + #define DECLARE_ENUM_FLAG_OPERATORS(type) \ [[nodiscard]] constexpr type operator|(type a, type b) noexcept { \ using T = std::underlying_type_t<type>; \ diff --git a/src/common/misc.cpp b/src/common/misc.cpp index 1d5393597..495385b9e 100644 --- a/src/common/misc.cpp +++ b/src/common/misc.cpp @@ -12,27 +12,41 @@ #include "common/common_funcs.h" -// Generic function to get last error message. -// Call directly after the command or use the error num. -// This function might change the error code. -std::string GetLastErrorMsg() { - static constexpr std::size_t buff_size = 255; - char err_str[buff_size]; - +std::string NativeErrorToString(int e) { #ifdef _WIN32 - FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM, nullptr, GetLastError(), - MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), err_str, buff_size, nullptr); - return std::string(err_str, buff_size); -#elif defined(__GLIBC__) && (_GNU_SOURCE || (_POSIX_C_SOURCE < 200112L && _XOPEN_SOURCE < 600)) + LPSTR err_str; + + DWORD res = FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER | + FORMAT_MESSAGE_IGNORE_INSERTS, + nullptr, e, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), + reinterpret_cast<LPSTR>(&err_str), 1, nullptr); + if (!res) { + return "(FormatMessageA failed to format error)"; + } + std::string ret(err_str); + LocalFree(err_str); + return ret; +#else + char err_str[255]; +#if defined(__GLIBC__) && (_GNU_SOURCE || (_POSIX_C_SOURCE < 200112L && _XOPEN_SOURCE < 600)) // Thread safe (GNU-specific) - const char* str = strerror_r(errno, err_str, buff_size); + const char* str = strerror_r(e, err_str, sizeof(err_str)); return std::string(str); #else // Thread safe (XSI-compliant) - const int success = strerror_r(errno, err_str, buff_size); - if (success != 0) { - return {}; + int second_err = strerror_r(e, err_str, sizeof(err_str)); + if (second_err != 0) { + return "(strerror_r failed to format error)"; } return std::string(err_str); +#endif // GLIBC etc. +#endif // _WIN32 +} + +std::string GetLastErrorMsg() { +#ifdef _WIN32 + return NativeErrorToString(GetLastError()); +#else + return NativeErrorToString(errno); #endif } diff --git a/src/core/core.cpp b/src/core/core.cpp index de6305e2a..305f56ff1 100644 --- a/src/core/core.cpp +++ b/src/core/core.cpp @@ -299,28 +299,17 @@ struct System::Impl { gpu_core->WaitIdle(); } - // Shutdown emulation session services.reset(); service_manager.reset(); cheat_engine.reset(); telemetry_session.reset(); - - // Close all CPU/threading state cpu_manager.Shutdown(); - - // Release the Time Manager's resources time_manager.Shutdown(); - - // Shutdown kernel and core timing core_timing.Shutdown(); - kernel.Shutdown(); - - // Close app loader app_loader.reset(); gpu_core.reset(); perf_stats.reset(); - - // Clear all applets + kernel.Shutdown(); applet_manager.ClearAll(); LOG_DEBUG(Core, "Shutdown OK"); diff --git a/src/core/hle/service/hid/controllers/gesture.cpp b/src/core/hle/service/hid/controllers/gesture.cpp index b7b7bfeae..e7063f8ef 100644 --- a/src/core/hle/service/hid/controllers/gesture.cpp +++ b/src/core/hle/service/hid/controllers/gesture.cpp @@ -5,15 +5,25 @@ #include <cstring> #include "common/common_types.h" #include "core/core_timing.h" +#include "core/frontend/emu_window.h" #include "core/hle/service/hid/controllers/gesture.h" +#include "core/settings.h" namespace Service::HID { constexpr std::size_t SHARED_MEMORY_OFFSET = 0x3BA00; +constexpr f32 angle_threshold = 0.08f; +constexpr f32 pinch_threshold = 100.0f; Controller_Gesture::Controller_Gesture(Core::System& system) : ControllerBase(system) {} Controller_Gesture::~Controller_Gesture() = default; -void Controller_Gesture::OnInit() {} +void Controller_Gesture::OnInit() { + for (std::size_t id = 0; id < MAX_FINGERS; ++id) { + mouse_finger_id[id] = MAX_FINGERS; + keyboard_finger_id[id] = MAX_FINGERS; + udp_finger_id[id] = MAX_FINGERS; + } +} void Controller_Gesture::OnRelease() {} @@ -35,10 +45,153 @@ void Controller_Gesture::OnUpdate(const Core::Timing::CoreTiming& core_timing, u cur_entry.sampling_number = last_entry.sampling_number + 1; cur_entry.sampling_number2 = cur_entry.sampling_number; - // TODO(ogniK): Update gesture states + + // TODO(german77): Implement all gesture types + + const Input::TouchStatus& mouse_status = touch_mouse_device->GetStatus(); + const Input::TouchStatus& udp_status = touch_udp_device->GetStatus(); + for (std::size_t id = 0; id < mouse_status.size(); ++id) { + mouse_finger_id[id] = UpdateTouchInputEvent(mouse_status[id], mouse_finger_id[id]); + udp_finger_id[id] = UpdateTouchInputEvent(udp_status[id], udp_finger_id[id]); + } + + if (Settings::values.use_touch_from_button) { + const Input::TouchStatus& keyboard_status = touch_btn_device->GetStatus(); + for (std::size_t id = 0; id < mouse_status.size(); ++id) { + keyboard_finger_id[id] = + UpdateTouchInputEvent(keyboard_status[id], keyboard_finger_id[id]); + } + } + + TouchType type = TouchType::Idle; + Attribute attributes{}; + GestureProperties gesture = GetGestureProperties(); + if (last_gesture.active_points != gesture.active_points) { + ++last_gesture.detection_count; + } + if (gesture.active_points > 0) { + if (last_gesture.active_points == 0) { + attributes.is_new_touch.Assign(true); + last_gesture.average_distance = gesture.average_distance; + last_gesture.angle = gesture.angle; + } + + type = TouchType::Touch; + if (gesture.mid_point.x != last_entry.x || gesture.mid_point.y != last_entry.y) { + type = TouchType::Pan; + } + if (std::abs(gesture.average_distance - last_gesture.average_distance) > pinch_threshold) { + type = TouchType::Pinch; + } + if (std::abs(gesture.angle - last_gesture.angle) > angle_threshold) { + type = TouchType::Rotate; + } + + cur_entry.delta_x = gesture.mid_point.x - last_entry.x; + cur_entry.delta_y = gesture.mid_point.y - last_entry.y; + // TODO: Find how velocities are calculated + cur_entry.vel_x = static_cast<float>(cur_entry.delta_x) * 150.1f; + cur_entry.vel_y = static_cast<float>(cur_entry.delta_y) * 150.1f; + + // Slowdown the rate of change for less flapping + last_gesture.average_distance = + (last_gesture.average_distance * 0.9f) + (gesture.average_distance * 0.1f); + last_gesture.angle = (last_gesture.angle * 0.9f) + (gesture.angle * 0.1f); + + } else { + cur_entry.delta_x = 0; + cur_entry.delta_y = 0; + cur_entry.vel_x = 0; + cur_entry.vel_y = 0; + } + last_gesture.active_points = gesture.active_points; + cur_entry.detection_count = last_gesture.detection_count; + cur_entry.type = type; + cur_entry.attributes = attributes; + cur_entry.x = gesture.mid_point.x; + cur_entry.y = gesture.mid_point.y; + cur_entry.point_count = static_cast<s32>(gesture.active_points); + for (size_t id = 0; id < MAX_POINTS; id++) { + cur_entry.points[id].x = gesture.points[id].x; + cur_entry.points[id].y = gesture.points[id].y; + } + cur_entry.rotation_angle = 0; + cur_entry.scale = 0; std::memcpy(data + SHARED_MEMORY_OFFSET, &shared_memory, sizeof(SharedMemory)); } -void Controller_Gesture::OnLoadInputDevices() {} +void Controller_Gesture::OnLoadInputDevices() { + touch_mouse_device = Input::CreateDevice<Input::TouchDevice>("engine:emu_window"); + touch_udp_device = Input::CreateDevice<Input::TouchDevice>("engine:cemuhookudp"); + touch_btn_device = Input::CreateDevice<Input::TouchDevice>("engine:touch_from_button"); +} + +std::optional<std::size_t> Controller_Gesture::GetUnusedFingerID() const { + std::size_t first_free_id = 0; + while (first_free_id < MAX_POINTS) { + if (!fingers[first_free_id].pressed) { + return first_free_id; + } else { + first_free_id++; + } + } + return std::nullopt; +} + +std::size_t Controller_Gesture::UpdateTouchInputEvent( + const std::tuple<float, float, bool>& touch_input, std::size_t finger_id) { + const auto& [x, y, pressed] = touch_input; + if (pressed) { + if (finger_id == MAX_POINTS) { + const auto first_free_id = GetUnusedFingerID(); + if (!first_free_id) { + // Invalid finger id do nothing + return MAX_POINTS; + } + finger_id = first_free_id.value(); + fingers[finger_id].pressed = true; + } + fingers[finger_id].x = x; + fingers[finger_id].y = y; + return finger_id; + } + + if (finger_id != MAX_POINTS) { + fingers[finger_id].pressed = false; + } + + return MAX_POINTS; +} + +Controller_Gesture::GestureProperties Controller_Gesture::GetGestureProperties() { + GestureProperties gesture; + std::array<Finger, MAX_POINTS> active_fingers; + const auto end_iter = std::copy_if(fingers.begin(), fingers.end(), active_fingers.begin(), + [](const auto& finger) { return finger.pressed; }); + gesture.active_points = + static_cast<std::size_t>(std::distance(active_fingers.begin(), end_iter)); + + for (size_t id = 0; id < gesture.active_points; ++id) { + gesture.points[id].x = + static_cast<int>(active_fingers[id].x * Layout::ScreenUndocked::Width); + gesture.points[id].y = + static_cast<int>(active_fingers[id].y * Layout::ScreenUndocked::Height); + gesture.mid_point.x += static_cast<int>(gesture.points[id].x / gesture.active_points); + gesture.mid_point.y += static_cast<int>(gesture.points[id].y / gesture.active_points); + } + + for (size_t id = 0; id < gesture.active_points; ++id) { + const double distance = + std::pow(static_cast<float>(gesture.mid_point.x - gesture.points[id].x), 2) + + std::pow(static_cast<float>(gesture.mid_point.y - gesture.points[id].y), 2); + gesture.average_distance += + static_cast<float>(distance) / static_cast<float>(gesture.active_points); + } + + gesture.angle = std::atan2(static_cast<float>(gesture.mid_point.y - gesture.points[0].y), + static_cast<float>(gesture.mid_point.x - gesture.points[0].x)); + return gesture; +} + } // namespace Service::HID diff --git a/src/core/hle/service/hid/controllers/gesture.h b/src/core/hle/service/hid/controllers/gesture.h index f650b8338..60ecc7822 100644 --- a/src/core/hle/service/hid/controllers/gesture.h +++ b/src/core/hle/service/hid/controllers/gesture.h @@ -5,8 +5,10 @@ #pragma once #include <array> +#include "common/bit_field.h" #include "common/common_types.h" #include "common/swap.h" +#include "core/frontend/input.h" #include "core/hle/service/hid/controllers/controller_base.h" namespace Service::HID { @@ -28,29 +30,64 @@ public: void OnLoadInputDevices() override; private: - struct Locations { + static constexpr size_t MAX_FINGERS = 16; + static constexpr size_t MAX_POINTS = 4; + + enum class TouchType : u32 { + Idle, // Nothing touching the screen + Complete, // Unknown. End of touch? + Cancel, // Never triggered + Touch, // Pressing without movement + Press, // Never triggered + Tap, // Fast press then release + Pan, // All points moving together across the screen + Swipe, // Fast press movement and release of a single point + Pinch, // All points moving away/closer to the midpoint + Rotate, // All points rotating from the midpoint + }; + + enum class Direction : u32 { + None, + Left, + Up, + Right, + Down, + }; + + struct Attribute { + union { + u32_le raw{}; + + BitField<0, 1, u32> is_new_touch; + BitField<1, 1, u32> is_double_tap; + }; + }; + static_assert(sizeof(Attribute) == 4, "Attribute is an invalid size"); + + struct Points { s32_le x; s32_le y; }; + static_assert(sizeof(Points) == 8, "Points is an invalid size"); struct GestureState { s64_le sampling_number; s64_le sampling_number2; s64_le detection_count; - s32_le type; - s32_le dir; + TouchType type; + Direction dir; s32_le x; s32_le y; s32_le delta_x; s32_le delta_y; f32 vel_x; f32 vel_y; - s32_le attributes; - f32 scale; - f32 rotation; - s32_le location_count; - std::array<Locations, 4> locations; + Attribute attributes; + u32 scale; + u32 rotation_angle; + s32_le point_count; + std::array<Points, 4> points; }; static_assert(sizeof(GestureState) == 0x68, "GestureState is an invalid size"); @@ -58,6 +95,45 @@ private: CommonHeader header; std::array<GestureState, 17> gesture_states; }; + static_assert(sizeof(SharedMemory) == 0x708, "SharedMemory is an invalid size"); + + struct Finger { + f32 x{}; + f32 y{}; + bool pressed{}; + }; + + struct GestureProperties { + std::array<Points, MAX_POINTS> points{}; + std::size_t active_points{}; + Points mid_point{}; + s64_le detection_count{}; + u64_le delta_time{}; + float average_distance{}; + float angle{}; + }; + + // Returns an unused finger id, if there is no fingers avaliable MAX_FINGERS will be returned + std::optional<size_t> GetUnusedFingerID() const; + + /** If the touch is new it tries to assing a new finger id, if there is no fingers avaliable no + * changes will be made. Updates the coordinates if the finger id it's already set. If the touch + * ends delays the output by one frame to set the end_touch flag before finally freeing the + * finger id */ + size_t UpdateTouchInputEvent(const std::tuple<float, float, bool>& touch_input, + size_t finger_id); + + // Returns the average distance, angle and middle point of the active fingers + GestureProperties GetGestureProperties(); + SharedMemory shared_memory{}; + std::unique_ptr<Input::TouchDevice> touch_mouse_device; + std::unique_ptr<Input::TouchDevice> touch_udp_device; + std::unique_ptr<Input::TouchDevice> touch_btn_device; + std::array<size_t, MAX_FINGERS> mouse_finger_id; + std::array<size_t, MAX_FINGERS> keyboard_finger_id; + std::array<size_t, MAX_FINGERS> udp_finger_id; + std::array<Finger, MAX_POINTS> fingers; + GestureProperties last_gesture; }; } // namespace Service::HID diff --git a/src/core/hle/service/ldn/ldn.cpp b/src/core/hle/service/ldn/ldn.cpp index c630d93cd..d160ffe87 100644 --- a/src/core/hle/service/ldn/ldn.cpp +++ b/src/core/hle/service/ldn/ldn.cpp @@ -156,7 +156,7 @@ public: is_initialized = true; IPC::ResponseBuilder rb{ctx, 2}; - rb.Push(RESULT_SUCCESS); + rb.Push(ERROR_DISABLED); } private: diff --git a/src/core/network/network.cpp b/src/core/network/network.cpp index 681e93468..526bfa110 100644 --- a/src/core/network/network.cpp +++ b/src/core/network/network.cpp @@ -7,6 +7,7 @@ #include <limits> #include <utility> #include <vector> +#include "common/common_funcs.h" #ifdef _WIN32 #define _WINSOCK_DEPRECATED_NO_WARNINGS // gethostname @@ -90,15 +91,36 @@ LINGER MakeLinger(bool enable, u32 linger_value) { return value; } -int LastError() { - return WSAGetLastError(); -} - bool EnableNonBlock(SOCKET fd, bool enable) { u_long value = enable ? 1 : 0; return ioctlsocket(fd, FIONBIO, &value) != SOCKET_ERROR; } +Errno TranslateNativeError(int e) { + switch (e) { + case WSAEBADF: + return Errno::BADF; + case WSAEINVAL: + return Errno::INVAL; + case WSAEMFILE: + return Errno::MFILE; + case WSAENOTCONN: + return Errno::NOTCONN; + case WSAEWOULDBLOCK: + return Errno::AGAIN; + case WSAECONNREFUSED: + return Errno::CONNREFUSED; + case WSAEHOSTUNREACH: + return Errno::HOSTUNREACH; + case WSAENETDOWN: + return Errno::NETDOWN; + case WSAENETUNREACH: + return Errno::NETUNREACH; + default: + return Errno::OTHER; + } +} + #elif YUZU_UNIX // ^ _WIN32 v YUZU_UNIX using SOCKET = int; @@ -108,9 +130,6 @@ using ULONG = u64; constexpr SOCKET INVALID_SOCKET = -1; constexpr SOCKET SOCKET_ERROR = -1; -constexpr int WSAEWOULDBLOCK = EAGAIN; -constexpr int WSAENOTCONN = ENOTCONN; - constexpr int SD_RECEIVE = SHUT_RD; constexpr int SD_SEND = SHUT_WR; constexpr int SD_BOTH = SHUT_RDWR; @@ -162,10 +181,6 @@ linger MakeLinger(bool enable, u32 linger_value) { return value; } -int LastError() { - return errno; -} - bool EnableNonBlock(int fd, bool enable) { int flags = fcntl(fd, F_GETFD); if (flags == -1) { @@ -179,8 +194,43 @@ bool EnableNonBlock(int fd, bool enable) { return fcntl(fd, F_SETFD, flags) == 0; } +Errno TranslateNativeError(int e) { + switch (e) { + case EBADF: + return Errno::BADF; + case EINVAL: + return Errno::INVAL; + case EMFILE: + return Errno::MFILE; + case ENOTCONN: + return Errno::NOTCONN; + case EAGAIN: + return Errno::AGAIN; + case ECONNREFUSED: + return Errno::CONNREFUSED; + case EHOSTUNREACH: + return Errno::HOSTUNREACH; + case ENETDOWN: + return Errno::NETDOWN; + case ENETUNREACH: + return Errno::NETUNREACH; + default: + return Errno::OTHER; + } +} + #endif +Errno GetAndLogLastError() { +#ifdef _WIN32 + int e = WSAGetLastError(); +#else + int e = errno; +#endif + LOG_ERROR(Network, "Socket operation error: {}", NativeErrorToString(e)); + return TranslateNativeError(e); +} + int TranslateDomain(Domain domain) { switch (domain) { case Domain::INET: @@ -290,9 +340,7 @@ Errno SetSockOpt(SOCKET fd, int option, T value) { if (result != SOCKET_ERROR) { return Errno::SUCCESS; } - const int ec = LastError(); - UNREACHABLE_MSG("Unhandled host socket error={}", ec); - return Errno::SUCCESS; + return GetAndLogLastError(); } } // Anonymous namespace @@ -308,14 +356,12 @@ NetworkInstance::~NetworkInstance() { std::pair<IPv4Address, Errno> GetHostIPv4Address() { std::array<char, 256> name{}; if (gethostname(name.data(), static_cast<int>(name.size()) - 1) == SOCKET_ERROR) { - UNIMPLEMENTED_MSG("Unhandled gethostname error"); - return {IPv4Address{}, Errno::SUCCESS}; + return {IPv4Address{}, GetAndLogLastError()}; } hostent* const ent = gethostbyname(name.data()); if (!ent) { - UNIMPLEMENTED_MSG("Unhandled gethostbyname error"); - return {IPv4Address{}, Errno::SUCCESS}; + return {IPv4Address{}, GetAndLogLastError()}; } if (ent->h_addr_list == nullptr) { UNIMPLEMENTED_MSG("No addr provided in hostent->h_addr_list"); @@ -359,9 +405,7 @@ std::pair<s32, Errno> Poll(std::vector<PollFD>& pollfds, s32 timeout) { ASSERT(result == SOCKET_ERROR); - const int ec = LastError(); - UNREACHABLE_MSG("Unhandled host socket error={}", ec); - return {-1, Errno::SUCCESS}; + return {-1, GetAndLogLastError()}; } Socket::~Socket() { @@ -380,9 +424,7 @@ Errno Socket::Initialize(Domain domain, Type type, Protocol protocol) { return Errno::SUCCESS; } - const int ec = LastError(); - UNREACHABLE_MSG("Unhandled host socket error={}", ec); - return Errno::SUCCESS; + return GetAndLogLastError(); } std::pair<Socket::AcceptResult, Errno> Socket::Accept() { @@ -391,9 +433,7 @@ std::pair<Socket::AcceptResult, Errno> Socket::Accept() { const SOCKET new_socket = accept(fd, &addr, &addrlen); if (new_socket == INVALID_SOCKET) { - const int ec = LastError(); - UNREACHABLE_MSG("Unhandled host socket error={}", ec); - return {AcceptResult{}, Errno::SUCCESS}; + return {AcceptResult{}, GetAndLogLastError()}; } AcceptResult result; @@ -412,23 +452,14 @@ Errno Socket::Connect(SockAddrIn addr_in) { return Errno::SUCCESS; } - switch (const int ec = LastError()) { - case WSAEWOULDBLOCK: - LOG_DEBUG(Service, "EAGAIN generated"); - return Errno::AGAIN; - default: - UNREACHABLE_MSG("Unhandled host socket error={}", ec); - return Errno::SUCCESS; - } + return GetAndLogLastError(); } std::pair<SockAddrIn, Errno> Socket::GetPeerName() { sockaddr addr; socklen_t addrlen = sizeof(addr); if (getpeername(fd, &addr, &addrlen) == SOCKET_ERROR) { - const int ec = LastError(); - UNREACHABLE_MSG("Unhandled host socket error={}", ec); - return {SockAddrIn{}, Errno::SUCCESS}; + return {SockAddrIn{}, GetAndLogLastError()}; } ASSERT(addrlen == sizeof(sockaddr_in)); @@ -439,9 +470,7 @@ std::pair<SockAddrIn, Errno> Socket::GetSockName() { sockaddr addr; socklen_t addrlen = sizeof(addr); if (getsockname(fd, &addr, &addrlen) == SOCKET_ERROR) { - const int ec = LastError(); - UNREACHABLE_MSG("Unhandled host socket error={}", ec); - return {SockAddrIn{}, Errno::SUCCESS}; + return {SockAddrIn{}, GetAndLogLastError()}; } ASSERT(addrlen == sizeof(sockaddr_in)); @@ -454,9 +483,7 @@ Errno Socket::Bind(SockAddrIn addr) { return Errno::SUCCESS; } - const int ec = LastError(); - UNREACHABLE_MSG("Unhandled host socket error={}", ec); - return Errno::SUCCESS; + return GetAndLogLastError(); } Errno Socket::Listen(s32 backlog) { @@ -464,9 +491,7 @@ Errno Socket::Listen(s32 backlog) { return Errno::SUCCESS; } - const int ec = LastError(); - UNREACHABLE_MSG("Unhandled host socket error={}", ec); - return Errno::SUCCESS; + return GetAndLogLastError(); } Errno Socket::Shutdown(ShutdownHow how) { @@ -489,14 +514,7 @@ Errno Socket::Shutdown(ShutdownHow how) { return Errno::SUCCESS; } - switch (const int ec = LastError()) { - case WSAENOTCONN: - LOG_ERROR(Service, "ENOTCONN generated"); - return Errno::NOTCONN; - default: - UNREACHABLE_MSG("Unhandled host socket error={}", ec); - return Errno::SUCCESS; - } + return GetAndLogLastError(); } std::pair<s32, Errno> Socket::Recv(int flags, std::vector<u8>& message) { @@ -509,17 +527,7 @@ std::pair<s32, Errno> Socket::Recv(int flags, std::vector<u8>& message) { return {static_cast<s32>(result), Errno::SUCCESS}; } - switch (const int ec = LastError()) { - case WSAEWOULDBLOCK: - LOG_DEBUG(Service, "EAGAIN generated"); - return {-1, Errno::AGAIN}; - case WSAENOTCONN: - LOG_ERROR(Service, "ENOTCONN generated"); - return {-1, Errno::NOTCONN}; - default: - UNREACHABLE_MSG("Unhandled host socket error={}", ec); - return {0, Errno::SUCCESS}; - } + return {-1, GetAndLogLastError()}; } std::pair<s32, Errno> Socket::RecvFrom(int flags, std::vector<u8>& message, SockAddrIn* addr) { @@ -541,17 +549,7 @@ std::pair<s32, Errno> Socket::RecvFrom(int flags, std::vector<u8>& message, Sock return {static_cast<s32>(result), Errno::SUCCESS}; } - switch (const int ec = LastError()) { - case WSAEWOULDBLOCK: - LOG_DEBUG(Service, "EAGAIN generated"); - return {-1, Errno::AGAIN}; - case WSAENOTCONN: - LOG_ERROR(Service, "ENOTCONN generated"); - return {-1, Errno::NOTCONN}; - default: - UNREACHABLE_MSG("Unhandled host socket error={}", ec); - return {-1, Errno::SUCCESS}; - } + return {-1, GetAndLogLastError()}; } std::pair<s32, Errno> Socket::Send(const std::vector<u8>& message, int flags) { @@ -564,18 +562,7 @@ std::pair<s32, Errno> Socket::Send(const std::vector<u8>& message, int flags) { return {static_cast<s32>(result), Errno::SUCCESS}; } - const int ec = LastError(); - switch (ec) { - case WSAEWOULDBLOCK: - LOG_DEBUG(Service, "EAGAIN generated"); - return {-1, Errno::AGAIN}; - case WSAENOTCONN: - LOG_ERROR(Service, "ENOTCONN generated"); - return {-1, Errno::NOTCONN}; - default: - UNREACHABLE_MSG("Unhandled host socket error={}", ec); - return {-1, Errno::SUCCESS}; - } + return {-1, GetAndLogLastError()}; } std::pair<s32, Errno> Socket::SendTo(u32 flags, const std::vector<u8>& message, @@ -597,9 +584,7 @@ std::pair<s32, Errno> Socket::SendTo(u32 flags, const std::vector<u8>& message, return {static_cast<s32>(result), Errno::SUCCESS}; } - const int ec = LastError(); - UNREACHABLE_MSG("Unhandled host socket error={}", ec); - return {-1, Errno::SUCCESS}; + return {-1, GetAndLogLastError()}; } Errno Socket::Close() { @@ -642,9 +627,7 @@ Errno Socket::SetNonBlock(bool enable) { if (EnableNonBlock(fd, enable)) { return Errno::SUCCESS; } - const int ec = LastError(); - UNREACHABLE_MSG("Unhandled host socket error={}", ec); - return Errno::SUCCESS; + return GetAndLogLastError(); } bool Socket::IsOpened() const { diff --git a/src/core/network/network.h b/src/core/network/network.h index 76b2821f2..bd30f1899 100644 --- a/src/core/network/network.h +++ b/src/core/network/network.h @@ -7,6 +7,7 @@ #include <array> #include <utility> +#include "common/common_funcs.h" #include "common/common_types.h" namespace Network { @@ -21,6 +22,11 @@ enum class Errno { MFILE, NOTCONN, AGAIN, + CONNREFUSED, + HOSTUNREACH, + NETDOWN, + NETUNREACH, + OTHER, }; /// Address families diff --git a/src/input_common/udp/client.cpp b/src/input_common/udp/client.cpp index c4afa4174..df73f9ff7 100644 --- a/src/input_common/udp/client.cpp +++ b/src/input_common/udp/client.cpp @@ -5,6 +5,7 @@ #include <chrono> #include <cstring> #include <functional> +#include <random> #include <thread> #include <boost/asio.hpp> #include "common/logging/log.h" @@ -26,10 +27,10 @@ class Socket { public: using clock = std::chrono::system_clock; - explicit Socket(const std::string& host, u16 port, std::size_t pad_index_, u32 client_id_, + explicit Socket(const std::string& host, u16 port, std::size_t pad_index_, SocketCallback callback_) : callback(std::move(callback_)), timer(io_service), - socket(io_service, udp::endpoint(udp::v4(), 0)), client_id(client_id_), + socket(io_service, udp::endpoint(udp::v4(), 0)), client_id(GenerateRandomClientId()), pad_index(pad_index_) { boost::system::error_code ec{}; auto ipv4 = boost::asio::ip::make_address_v4(host, ec); @@ -63,6 +64,11 @@ public: } private: + u32 GenerateRandomClientId() const { + std::random_device device; + return device(); + } + void HandleReceive(const boost::system::error_code&, std::size_t bytes_transferred) { if (auto type = Response::Validate(receive_buffer.data(), bytes_transferred)) { switch (*type) { @@ -115,7 +121,7 @@ private: boost::asio::basic_waitable_timer<clock> timer; udp::socket socket; - u32 client_id{}; + const u32 client_id; std::size_t pad_index{}; static constexpr std::size_t PORT_INFO_SIZE = sizeof(Message<Request::PortInfo>); @@ -203,7 +209,7 @@ void Client::ReloadSockets() { LOG_ERROR(Input, "Duplicated UDP servers found"); continue; } - StartCommunication(client++, udp_input_address, udp_input_port, pad, 24872); + StartCommunication(client++, udp_input_address, udp_input_port, pad); } } } @@ -277,7 +283,7 @@ void Client::OnPadData(Response::PadData data, std::size_t client) { } void Client::StartCommunication(std::size_t client, const std::string& host, u16 port, - std::size_t pad_index, u32 client_id) { + std::size_t pad_index) { SocketCallback callback{[this](Response::Version version) { OnVersion(version); }, [this](Response::PortInfo info) { OnPortInfo(info); }, [this, client](Response::PadData data) { OnPadData(data, client); }}; @@ -287,7 +293,7 @@ void Client::StartCommunication(std::size_t client, const std::string& host, u16 clients[client].port = port; clients[client].pad_index = pad_index; clients[client].active = 0; - clients[client].socket = std::make_unique<Socket>(host, port, pad_index, client_id, callback); + clients[client].socket = std::make_unique<Socket>(host, port, pad_index, callback); clients[client].thread = std::thread{SocketLoop, clients[client].socket.get()}; // Set motion parameters // SetGyroThreshold value should be dependent on GyroscopeZeroDriftMode @@ -416,7 +422,7 @@ const Common::SPSCQueue<UDPPadStatus>& Client::GetPadQueue() const { return pad_queue; } -void TestCommunication(const std::string& host, u16 port, std::size_t pad_index, u32 client_id, +void TestCommunication(const std::string& host, u16 port, std::size_t pad_index, const std::function<void()>& success_callback, const std::function<void()>& failure_callback) { std::thread([=] { @@ -426,7 +432,7 @@ void TestCommunication(const std::string& host, u16 port, std::size_t pad_index, .port_info = [](Response::PortInfo) {}, .pad_data = [&](Response::PadData) { success_event.Set(); }, }; - Socket socket{host, port, pad_index, client_id, std::move(callback)}; + Socket socket{host, port, pad_index, std::move(callback)}; std::thread worker_thread{SocketLoop, &socket}; const bool result = success_event.WaitFor(std::chrono::seconds(5)); socket.Stop(); @@ -440,7 +446,7 @@ void TestCommunication(const std::string& host, u16 port, std::size_t pad_index, } CalibrationConfigurationJob::CalibrationConfigurationJob( - const std::string& host, u16 port, std::size_t pad_index, u32 client_id, + const std::string& host, u16 port, std::size_t pad_index, std::function<void(Status)> status_callback, std::function<void(u16, u16, u16, u16)> data_callback) { @@ -485,7 +491,7 @@ CalibrationConfigurationJob::CalibrationConfigurationJob( complete_event.Set(); } }}; - Socket socket{host, port, pad_index, client_id, std::move(callback)}; + Socket socket{host, port, pad_index, std::move(callback)}; std::thread worker_thread{SocketLoop, &socket}; complete_event.Wait(); socket.Stop(); diff --git a/src/input_common/udp/client.h b/src/input_common/udp/client.h index a523f6124..e9e438e88 100644 --- a/src/input_common/udp/client.h +++ b/src/input_common/udp/client.h @@ -126,7 +126,7 @@ private: void OnPortInfo(Response::PortInfo); void OnPadData(Response::PadData, std::size_t client); void StartCommunication(std::size_t client, const std::string& host, u16 port, - std::size_t pad_index, u32 client_id); + std::size_t pad_index); void UpdateYuzuSettings(std::size_t client, const Common::Vec3<float>& acc, const Common::Vec3<float>& gyro); @@ -165,7 +165,7 @@ public: * @param data_callback Called when calibration data is ready */ explicit CalibrationConfigurationJob(const std::string& host, u16 port, std::size_t pad_index, - u32 client_id, std::function<void(Status)> status_callback, + std::function<void(Status)> status_callback, std::function<void(u16, u16, u16, u16)> data_callback); ~CalibrationConfigurationJob(); void Stop(); @@ -174,7 +174,7 @@ private: Common::Event complete_event; }; -void TestCommunication(const std::string& host, u16 port, std::size_t pad_index, u32 client_id, +void TestCommunication(const std::string& host, u16 port, std::size_t pad_index, const std::function<void()>& success_callback, const std::function<void()>& failure_callback); diff --git a/src/tests/CMakeLists.txt b/src/tests/CMakeLists.txt index 4ea0076e9..d875c4fee 100644 --- a/src/tests/CMakeLists.txt +++ b/src/tests/CMakeLists.txt @@ -5,6 +5,7 @@ add_executable(tests common/param_package.cpp common/ring_buffer.cpp core/core_timing.cpp + core/network/network.cpp tests.cpp video_core/buffer_base.cpp ) diff --git a/src/tests/core/network/network.cpp b/src/tests/core/network/network.cpp new file mode 100644 index 000000000..b21ad8911 --- /dev/null +++ b/src/tests/core/network/network.cpp @@ -0,0 +1,28 @@ +// Copyright 2021 yuzu Emulator Project +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#include <catch2/catch.hpp> + +#include "core/network/network.h" +#include "core/network/sockets.h" + +TEST_CASE("Network::Errors", "[core]") { + Network::NetworkInstance network_instance; // initialize network + + Network::Socket socks[2]; + for (Network::Socket& sock : socks) { + REQUIRE(sock.Initialize(Network::Domain::INET, Network::Type::STREAM, + Network::Protocol::TCP) == Network::Errno::SUCCESS); + } + + Network::SockAddrIn addr{ + Network::Domain::INET, + {127, 0, 0, 1}, + 1, // hopefully nobody running this test has something listening on port 1 + }; + REQUIRE(socks[0].Connect(addr) == Network::Errno::CONNREFUSED); + + std::vector<u8> message{1, 2, 3, 4}; + REQUIRE(socks[1].Recv(0, message).second == Network::Errno::NOTCONN); +} diff --git a/src/video_core/gpu.cpp b/src/video_core/gpu.cpp index 51c63af4a..c61f44619 100644 --- a/src/video_core/gpu.cpp +++ b/src/video_core/gpu.cpp @@ -459,7 +459,7 @@ void GPU::ProcessSemaphoreAcquire() { } void GPU::Start() { - gpu_thread.StartThread(*renderer, renderer->Context(), *dma_pusher, *cdma_pusher); + gpu_thread.StartThread(*renderer, renderer->Context(), *dma_pusher); cpu_context = renderer->GetRenderWindow().CreateSharedContext(); cpu_context->MakeCurrent(); } diff --git a/src/video_core/gpu_thread.cpp b/src/video_core/gpu_thread.cpp index eb0e43c0c..99353f15f 100644 --- a/src/video_core/gpu_thread.cpp +++ b/src/video_core/gpu_thread.cpp @@ -19,7 +19,7 @@ namespace VideoCommon::GPUThread { /// Runs the GPU thread static void RunThread(Core::System& system, VideoCore::RendererBase& renderer, Core::Frontend::GraphicsContext& context, Tegra::DmaPusher& dma_pusher, - SynchState& state, Tegra::CDmaPusher& cdma_pusher) { + SynchState& state) { std::string name = "yuzu:GPU"; MicroProfileOnThreadCreate(name.c_str()); SCOPE_EXIT({ MicroProfileOnThreadExit(); }); @@ -46,9 +46,6 @@ static void RunThread(Core::System& system, VideoCore::RendererBase& renderer, if (auto* submit_list = std::get_if<SubmitListCommand>(&next.data)) { dma_pusher.Push(std::move(submit_list->entries)); dma_pusher.DispatchCalls(); - } else if (auto* command_list = std::get_if<SubmitChCommandEntries>(&next.data)) { - // NVDEC - cdma_pusher.ProcessEntries(std::move(command_list->entries)); } else if (const auto* data = std::get_if<SwapBuffersCommand>(&next.data)) { renderer.SwapBuffers(data->framebuffer ? &*data->framebuffer : nullptr); } else if (std::holds_alternative<OnCommandListEndCommand>(next.data)) { @@ -83,20 +80,16 @@ ThreadManager::~ThreadManager() { void ThreadManager::StartThread(VideoCore::RendererBase& renderer, Core::Frontend::GraphicsContext& context, - Tegra::DmaPusher& dma_pusher, Tegra::CDmaPusher& cdma_pusher) { + Tegra::DmaPusher& dma_pusher) { rasterizer = renderer.ReadRasterizer(); thread = std::thread(RunThread, std::ref(system), std::ref(renderer), std::ref(context), - std::ref(dma_pusher), std::ref(state), std::ref(cdma_pusher)); + std::ref(dma_pusher), std::ref(state)); } void ThreadManager::SubmitList(Tegra::CommandList&& entries) { PushCommand(SubmitListCommand(std::move(entries))); } -void ThreadManager::SubmitCommandBuffer(Tegra::ChCommandHeaderList&& entries) { - PushCommand(SubmitChCommandEntries(std::move(entries))); -} - void ThreadManager::SwapBuffers(const Tegra::FramebufferConfig* framebuffer) { PushCommand(SwapBuffersCommand(framebuffer ? std::make_optional(*framebuffer) : std::nullopt)); } diff --git a/src/video_core/gpu_thread.h b/src/video_core/gpu_thread.h index 4cd951169..18269e51c 100644 --- a/src/video_core/gpu_thread.h +++ b/src/video_core/gpu_thread.h @@ -43,14 +43,6 @@ struct SubmitListCommand final { Tegra::CommandList entries; }; -/// Command to signal to the GPU thread that a cdma command list is ready for processing -struct SubmitChCommandEntries final { - explicit SubmitChCommandEntries(Tegra::ChCommandHeaderList&& entries_) - : entries{std::move(entries_)} {} - - Tegra::ChCommandHeaderList entries; -}; - /// Command to signal to the GPU thread that a swap buffers is pending struct SwapBuffersCommand final { explicit SwapBuffersCommand(std::optional<const Tegra::FramebufferConfig> framebuffer_) @@ -91,9 +83,9 @@ struct OnCommandListEndCommand final {}; struct GPUTickCommand final {}; using CommandData = - std::variant<EndProcessingCommand, SubmitListCommand, SubmitChCommandEntries, - SwapBuffersCommand, FlushRegionCommand, InvalidateRegionCommand, - FlushAndInvalidateRegionCommand, OnCommandListEndCommand, GPUTickCommand>; + std::variant<EndProcessingCommand, SubmitListCommand, SwapBuffersCommand, FlushRegionCommand, + InvalidateRegionCommand, FlushAndInvalidateRegionCommand, OnCommandListEndCommand, + GPUTickCommand>; struct CommandDataContainer { CommandDataContainer() = default; @@ -123,14 +115,11 @@ public: /// Creates and starts the GPU thread. void StartThread(VideoCore::RendererBase& renderer, Core::Frontend::GraphicsContext& context, - Tegra::DmaPusher& dma_pusher, Tegra::CDmaPusher& cdma_pusher); + Tegra::DmaPusher& dma_pusher); /// Push GPU command entries to be processed void SubmitList(Tegra::CommandList&& entries); - /// Push GPU CDMA command buffer entries to be processed - void SubmitCommandBuffer(Tegra::ChCommandHeaderList&& entries); - /// Swap buffers (render frame) void SwapBuffers(const Tegra::FramebufferConfig* framebuffer); diff --git a/src/video_core/renderer_vulkan/vk_command_pool.cpp b/src/video_core/renderer_vulkan/vk_command_pool.cpp index a99df9323..d8e92ac0e 100644 --- a/src/video_core/renderer_vulkan/vk_command_pool.cpp +++ b/src/video_core/renderer_vulkan/vk_command_pool.cpp @@ -10,7 +10,7 @@ namespace Vulkan { -constexpr size_t COMMAND_BUFFER_POOL_SIZE = 0x1000; +constexpr size_t COMMAND_BUFFER_POOL_SIZE = 4; struct CommandPool::Pool { vk::CommandPool handle; diff --git a/src/video_core/shader_notify.cpp b/src/video_core/shader_notify.cpp index c3c71657d..693e47158 100644 --- a/src/video_core/shader_notify.cpp +++ b/src/video_core/shader_notify.cpp @@ -2,6 +2,7 @@ // Licensed under GPLv2 or any later version // Refer to the license.txt file included. +#include <mutex> #include "video_core/shader_notify.h" using namespace std::chrono_literals; diff --git a/src/yuzu/configuration/config.cpp b/src/yuzu/configuration/config.cpp index 3d6f64300..b319d69fc 100644 --- a/src/yuzu/configuration/config.cpp +++ b/src/yuzu/configuration/config.cpp @@ -648,7 +648,7 @@ void Config::ReadDebuggingValues() { void Config::ReadServiceValues() { qt_config->beginGroup(QStringLiteral("Services")); Settings::values.bcat_backend = - ReadSetting(QStringLiteral("bcat_backend"), QStringLiteral("null")) + ReadSetting(QStringLiteral("bcat_backend"), QStringLiteral("none")) .toString() .toStdString(); Settings::values.bcat_boxcat_local = @@ -1239,7 +1239,7 @@ void Config::SaveDebuggingValues() { void Config::SaveServiceValues() { qt_config->beginGroup(QStringLiteral("Services")); WriteSetting(QStringLiteral("bcat_backend"), - QString::fromStdString(Settings::values.bcat_backend), QStringLiteral("null")); + QString::fromStdString(Settings::values.bcat_backend), QStringLiteral("none")); WriteSetting(QStringLiteral("bcat_boxcat_local"), Settings::values.bcat_boxcat_local, false); qt_config->endGroup(); } diff --git a/src/yuzu/configuration/configure_motion_touch.cpp b/src/yuzu/configuration/configure_motion_touch.cpp index 1f2b792e4..52fdf7265 100644 --- a/src/yuzu/configuration/configure_motion_touch.cpp +++ b/src/yuzu/configuration/configure_motion_touch.cpp @@ -24,7 +24,7 @@ CalibrationConfigurationDialog::CalibrationConfigurationDialog(QWidget* parent, const std::string& host, u16 port, - u8 pad_index, u16 client_id) + u8 pad_index) : QDialog(parent) { layout = new QVBoxLayout; status_label = new QLabel(tr("Communicating with the server...")); @@ -41,7 +41,7 @@ CalibrationConfigurationDialog::CalibrationConfigurationDialog(QWidget* parent, using namespace InputCommon::CemuhookUDP; job = std::make_unique<CalibrationConfigurationJob>( - host, port, pad_index, client_id, + host, port, pad_index, [this](CalibrationConfigurationJob::Status status) { QString text; switch (status) { @@ -218,7 +218,6 @@ void ConfigureMotionTouch::OnCemuhookUDPTest() { udp_test_in_progress = true; InputCommon::CemuhookUDP::TestCommunication( ui->udp_server->text().toStdString(), static_cast<u16>(ui->udp_port->text().toInt()), 0, - 24872, [this] { LOG_INFO(Frontend, "UDP input test success"); QMetaObject::invokeMethod(this, "ShowUDPTestResult", Q_ARG(bool, true)); @@ -233,8 +232,7 @@ void ConfigureMotionTouch::OnConfigureTouchCalibration() { ui->touch_calibration_config->setEnabled(false); ui->touch_calibration_config->setText(tr("Configuring")); CalibrationConfigurationDialog dialog(this, ui->udp_server->text().toStdString(), - static_cast<u16>(ui->udp_port->text().toUInt()), 0, - 24872); + static_cast<u16>(ui->udp_port->text().toUInt()), 0); dialog.exec(); if (dialog.completed) { min_x = dialog.min_x; diff --git a/src/yuzu/configuration/configure_motion_touch.h b/src/yuzu/configuration/configure_motion_touch.h index 15d61e8ba..d76bc8154 100644 --- a/src/yuzu/configuration/configure_motion_touch.h +++ b/src/yuzu/configuration/configure_motion_touch.h @@ -30,7 +30,7 @@ class CalibrationConfigurationDialog : public QDialog { Q_OBJECT public: explicit CalibrationConfigurationDialog(QWidget* parent, const std::string& host, u16 port, - u8 pad_index, u16 client_id); + u8 pad_index); ~CalibrationConfigurationDialog() override; private: diff --git a/src/yuzu_cmd/config.cpp b/src/yuzu_cmd/config.cpp index 6d8bc5509..43877fc98 100644 --- a/src/yuzu_cmd/config.cpp +++ b/src/yuzu_cmd/config.cpp @@ -457,7 +457,7 @@ void Config::ReadValues() { Settings::values.yuzu_token = sdl2_config->Get("WebService", "yuzu_token", ""); // Services - Settings::values.bcat_backend = sdl2_config->Get("Services", "bcat_backend", "null"); + Settings::values.bcat_backend = sdl2_config->Get("Services", "bcat_backend", "none"); Settings::values.bcat_boxcat_local = sdl2_config->GetBoolean("Services", "bcat_boxcat_local", false); } |