From 475037f0e5ad461f9566696277018481fd6f55d6 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Fri, 6 Sep 2019 06:19:53 -0400 Subject: [PATCH 01/13] DolphinSmashAdapter: Use std::array where applicable --- include/boo/inputdev/DolphinSmashAdapter.hpp | 43 +++---- lib/inputdev/DolphinSmashAdapter.cpp | 114 ++++++++++--------- 2 files changed, 83 insertions(+), 74 deletions(-) diff --git a/include/boo/inputdev/DolphinSmashAdapter.hpp b/include/boo/inputdev/DolphinSmashAdapter.hpp index a3b06c6..d08d227 100644 --- a/include/boo/inputdev/DolphinSmashAdapter.hpp +++ b/include/boo/inputdev/DolphinSmashAdapter.hpp @@ -1,5 +1,7 @@ #pragma once +#include +#include #include #include "boo/System.hpp" @@ -31,17 +33,14 @@ enum class EDolphinControllerButtons { ENABLE_BITWISE_ENUM(EDolphinControllerButtons) struct DolphinControllerState { - int16_t m_leftStick[2] = {0}; - int16_t m_rightStick[2] = {0}; - int16_t m_analogTriggers[2] = {0}; + std::array m_leftStick{}; + std::array m_rightStick{}; + std::array m_analogTriggers{}; uint16_t m_btns = 0; void reset() { - m_leftStick[0] = 0; - m_leftStick[1] = 0; - m_rightStick[0] = 0; - m_rightStick[1] = 0; - m_analogTriggers[0] = 0; - m_analogTriggers[1] = 0; + m_leftStick = {}; + m_rightStick = {}; + m_analogTriggers = {}; m_btns = 0; } void clamp(); @@ -61,12 +60,12 @@ struct IDolphinSmashAdapterCallback { }; class DolphinSmashAdapter final : public TDeviceBase { - int16_t m_leftStickCal[2] = {0x7f}; - int16_t m_rightStickCal[2] = {0x7f}; - int16_t m_triggersCal[2] = {0x0}; + std::array m_leftStickCal{0x7f, 0}; + std::array m_rightStickCal{0x7f, 0}; + std::array m_triggersCal{}; uint8_t m_knownControllers = 0; uint8_t m_rumbleRequest = 0; - bool m_hardStop[4] = {false}; + std::array m_hardStop{}; uint8_t m_rumbleState = 0xf; /* Force initial send of stop-rumble command */ void deviceDisconnected() override; void initialCycle() override; @@ -81,15 +80,21 @@ public: TDeviceBase::setCallback(cb); m_knownControllers = 0; } - void startRumble(unsigned idx) { - if (idx >= 4) + + void startRumble(size_t idx) { + if (idx >= m_hardStop.size()) { return; - m_rumbleRequest |= 1 << idx; + } + + m_rumbleRequest |= 1U << idx; } - void stopRumble(unsigned idx, bool hard = false) { - if (idx >= 4) + + void stopRumble(size_t idx, bool hard = false) { + if (idx >= m_hardStop.size()) { return; - m_rumbleRequest &= ~(1 << idx); + } + + m_rumbleRequest &= ~(1U << idx); m_hardStop[idx] = hard; } }; diff --git a/lib/inputdev/DolphinSmashAdapter.cpp b/lib/inputdev/DolphinSmashAdapter.cpp index f4dcebd..9d75c1b 100644 --- a/lib/inputdev/DolphinSmashAdapter.cpp +++ b/lib/inputdev/DolphinSmashAdapter.cpp @@ -1,9 +1,6 @@ #include "boo/inputdev/DolphinSmashAdapter.hpp" #include "boo/inputdev/DeviceSignature.hpp" -#include -#include - namespace boo { /* * Reference: https://github.com/ToadKing/wii-u-gc-adapter/blob/master/wii-u-gc-adapter.c @@ -14,9 +11,10 @@ DolphinSmashAdapter::DolphinSmashAdapter(DeviceToken* token) DolphinSmashAdapter::~DolphinSmashAdapter() {} -constexpr EDolphinControllerType parseType(unsigned char status) { - EDolphinControllerType type = +static constexpr EDolphinControllerType parseType(unsigned char status) { + const auto type = EDolphinControllerType(status) & (EDolphinControllerType::Normal | EDolphinControllerType::Wavebird); + switch (type) { case EDolphinControllerType::Normal: case EDolphinControllerType::Wavebird: @@ -26,13 +24,13 @@ constexpr EDolphinControllerType parseType(unsigned char status) { } } -static EDolphinControllerType parseState(DolphinControllerState* stateOut, uint8_t* payload, bool& rumble) { - unsigned char status = payload[0]; - EDolphinControllerType type = parseType(status); +static EDolphinControllerType parseState(DolphinControllerState* stateOut, const uint8_t* payload, bool& rumble) { + const unsigned char status = payload[0]; + const EDolphinControllerType type = parseType(status); rumble = ((status & 0x04) != 0) ? true : false; - stateOut->m_btns = (uint16_t)payload[1] << 8 | (uint16_t)payload[2]; + stateOut->m_btns = static_cast(payload[1]) << 8 | static_cast(payload[2]); stateOut->m_leftStick[0] = payload[3]; stateOut->m_leftStick[1] = payload[4]; @@ -45,43 +43,44 @@ static EDolphinControllerType parseState(DolphinControllerState* stateOut, uint8 } void DolphinSmashAdapter::initialCycle() { - uint8_t handshakePayload[] = {0x13}; - sendUSBInterruptTransfer(handshakePayload, sizeof(handshakePayload)); + constexpr std::array handshakePayload{0x13}; + sendUSBInterruptTransfer(handshakePayload.data(), handshakePayload.size()); } void DolphinSmashAdapter::transferCycle() { - uint8_t payload[37]; - size_t recvSz = receiveUSBInterruptTransfer(payload, sizeof(payload)); - if (recvSz != 37 || payload[0] != 0x21) + std::array payload; + const size_t recvSz = receiveUSBInterruptTransfer(payload.data(), payload.size()); + if (recvSz != payload.size() || payload[0] != 0x21) { return; + } // fmt::print("RECEIVED DATA {} {:02X}\n", recvSz, payload[0]); std::lock_guard lk(m_callbackLock); - if (!m_callback) + if (!m_callback) { return; + } /* Parse controller states */ - uint8_t* controller = &payload[1]; + const uint8_t* controller = &payload[1]; uint8_t rumbleMask = 0; for (uint32_t i = 0; i < 4; i++, controller += 9) { DolphinControllerState state; bool rumble = false; - EDolphinControllerType type = parseState(&state, controller, rumble); - if (True(type) && !(m_knownControllers & 1 << i)) { - m_leftStickCal[0] = state.m_leftStick[0]; - m_leftStickCal[1] = state.m_leftStick[1]; - m_rightStickCal[0] = state.m_rightStick[0]; - m_rightStickCal[1] = state.m_rightStick[1]; - m_triggersCal[0] = state.m_analogTriggers[0]; - m_triggersCal[1] = state.m_analogTriggers[1]; - m_knownControllers |= 1 << i; + const EDolphinControllerType type = parseState(&state, controller, rumble); + + if (True(type) && (m_knownControllers & (1U << i)) == 0) { + m_leftStickCal = state.m_leftStick; + m_rightStickCal = state.m_rightStick; + m_triggersCal = state.m_analogTriggers; + m_knownControllers |= 1U << i; m_callback->controllerConnected(i, type); - } else if (False(type) && (m_knownControllers & 1 << i)) { - m_knownControllers &= ~(1 << i); + } else if (False(type) && (m_knownControllers & (1U << i)) != 0) { + m_knownControllers &= ~(1U << i); m_callback->controllerDisconnected(i); } - if (m_knownControllers & 1 << i) { + + if ((m_knownControllers & (1U << i)) != 0) { state.m_leftStick[0] = state.m_leftStick[0] - m_leftStickCal[0]; state.m_leftStick[1] = state.m_leftStick[1] - m_leftStickCal[1]; state.m_rightStick[0] = state.m_rightStick[0] - m_rightStickCal[0]; @@ -90,39 +89,42 @@ void DolphinSmashAdapter::transferCycle() { state.m_analogTriggers[1] = state.m_analogTriggers[1] - m_triggersCal[1]; m_callback->controllerUpdate(i, type, state); } - rumbleMask |= rumble ? 1 << i : 0; + + rumbleMask |= rumble ? 1U << i : 0; } /* Send rumble message (if needed) */ - uint8_t rumbleReq = m_rumbleRequest & rumbleMask; + const uint8_t rumbleReq = m_rumbleRequest & rumbleMask; if (rumbleReq != m_rumbleState) { - uint8_t rumbleMessage[5] = {0x11}; - for (int i = 0; i < 4; ++i) { - if (rumbleReq & 1 << i) + std::array rumbleMessage{0x11, 0, 0, 0, 0}; + for (size_t i = 0; i < 4; ++i) { + if ((rumbleReq & (1U << i)) != 0) { rumbleMessage[i + 1] = 1; - else if (m_hardStop[i]) + } else if (m_hardStop[i]) { rumbleMessage[i + 1] = 2; - else + } else { rumbleMessage[i + 1] = 0; + } } - sendUSBInterruptTransfer(rumbleMessage, sizeof(rumbleMessage)); + sendUSBInterruptTransfer(rumbleMessage.data(), rumbleMessage.size()); m_rumbleState = rumbleReq; } } void DolphinSmashAdapter::finalCycle() { - uint8_t rumbleMessage[5] = {0x11, 0, 0, 0, 0}; - sendUSBInterruptTransfer(rumbleMessage, sizeof(rumbleMessage)); + constexpr std::array rumbleMessage{0x11, 0, 0, 0, 0}; + sendUSBInterruptTransfer(rumbleMessage.data(), sizeof(rumbleMessage)); } void DolphinSmashAdapter::deviceDisconnected() { for (uint32_t i = 0; i < 4; i++) { - if (m_knownControllers & 1 << i) { - m_knownControllers &= ~(1 << i); + if ((m_knownControllers & (1U << i)) != 0) { + m_knownControllers &= ~(1U << i); std::lock_guard lk(m_callbackLock); - if (m_callback) + if (m_callback) { m_callback->controllerDisconnected(i); + } } } } @@ -151,15 +153,15 @@ void DolphinSmashAdapter::deviceDisconnected() { * distribution. */ -static int16_t pad_clampregion[8] = {30, 180, 15, 72, 40, 15, 59, 31}; +constexpr std::array pad_clampregion{ + 30, 180, 15, 72, 40, 15, 59, 31, +}; static void pad_clampstick(int16_t& px, int16_t& py, int16_t max, int16_t xy, int16_t min) { int x = px; int y = py; - int signX; - int signY; - int d; + int signX; if (x > 0) { signX = 1; } else { @@ -167,6 +169,7 @@ static void pad_clampstick(int16_t& px, int16_t& py, int16_t max, int16_t xy, in x = -x; } + int signY; if (y > 0) { signY = 1; } else { @@ -174,10 +177,11 @@ static void pad_clampstick(int16_t& px, int16_t& py, int16_t max, int16_t xy, in y = -y; } - if (x <= min) + if (x <= min) { x = 0; - else + } else { x -= min; + } if (y <= min) { y = 0; @@ -191,13 +195,13 @@ static void pad_clampstick(int16_t& px, int16_t& py, int16_t max, int16_t xy, in } if (xy * y <= xy * x) { - d = xy * x + (max - xy) * y; + const int d = xy * x + (max - xy) * y; if (xy * max < d) { x = int16_t(xy * max * x / d); y = int16_t(xy * max * y / d); } } else { - d = xy * y + (max - xy) * x; + const int d = xy * y + (max - xy) * x; if (xy * max < d) { x = int16_t(xy * max * x / d); y = int16_t(xy * max * y / d); @@ -209,15 +213,15 @@ static void pad_clampstick(int16_t& px, int16_t& py, int16_t max, int16_t xy, in } static void pad_clamptrigger(int16_t& trigger) { - int16_t min, max; + const int16_t min = pad_clampregion[0]; + const int16_t max = pad_clampregion[1]; - min = pad_clampregion[0]; - max = pad_clampregion[1]; - if (min > trigger) + if (min > trigger) { trigger = 0; - else { - if (max < trigger) + } else { + if (max < trigger) { trigger = max; + } trigger -= min; } } From b5c0c9e599f37ba0c23daa0e843db8243427aef5 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Fri, 6 Sep 2019 06:41:03 -0400 Subject: [PATCH 02/13] DolphinSmashAdapter: Make use of [[maybe_unused]] --- include/boo/inputdev/DolphinSmashAdapter.hpp | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/include/boo/inputdev/DolphinSmashAdapter.hpp b/include/boo/inputdev/DolphinSmashAdapter.hpp index d08d227..cae73c3 100644 --- a/include/boo/inputdev/DolphinSmashAdapter.hpp +++ b/include/boo/inputdev/DolphinSmashAdapter.hpp @@ -47,16 +47,10 @@ struct DolphinControllerState { }; struct IDolphinSmashAdapterCallback { - virtual void controllerConnected(unsigned idx, EDolphinControllerType type) { - (void)idx; - (void)type; - } - virtual void controllerDisconnected(unsigned idx) { (void)idx; } - virtual void controllerUpdate(unsigned idx, EDolphinControllerType type, const DolphinControllerState& state) { - (void)idx; - (void)type; - (void)state; - } + virtual void controllerConnected([[maybe_unused]] unsigned idx, [[maybe_unused]] EDolphinControllerType type) {} + virtual void controllerDisconnected([[maybe_unused]] unsigned idx) {} + virtual void controllerUpdate([[maybe_unused]] unsigned idx, [[maybe_unused]] EDolphinControllerType type, + [[maybe_unused]] const DolphinControllerState& state) {} }; class DolphinSmashAdapter final : public TDeviceBase { From bfd042f156fed2471146e9cf5461da92e1eb6858 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Fri, 6 Sep 2019 07:04:57 -0400 Subject: [PATCH 03/13] ApplicationWin32/WindowWin32: Get/SetWindowLong -> Get/SetWindowLongPtr Get/SetWindowLong is superseded on 64-bit Windows platform with Get/SetWindowLongPtr. These are trivial to migrate over and have been available since Windows 2000 Professional, so there's no need to worry about breaking meaningful platform support --- include/boo/graphicsdev/Vulkan.hpp | 4 ++-- lib/win/ApplicationWin32.cpp | 11 +++++----- lib/win/Win32Common.hpp | 4 ++-- lib/win/WindowWin32.cpp | 32 +++++++++++++++++++----------- 4 files changed, 30 insertions(+), 21 deletions(-) diff --git a/include/boo/graphicsdev/Vulkan.hpp b/include/boo/graphicsdev/Vulkan.hpp index b85999b..95f0455 100644 --- a/include/boo/graphicsdev/Vulkan.hpp +++ b/include/boo/graphicsdev/Vulkan.hpp @@ -85,8 +85,8 @@ struct VulkanContext { #if _WIN32 HWND m_hwnd = 0; bool m_fs = false; - LONG m_fsStyle; - LONG m_fsExStyle; + LONG_PTR m_fsStyle; + LONG_PTR m_fsExStyle; RECT m_fsRect; int m_fsCountDown = 0; #endif diff --git a/lib/win/ApplicationWin32.cpp b/lib/win/ApplicationWin32.cpp index 8bc4e0c..6f6cd21 100644 --- a/lib/win/ApplicationWin32.cpp +++ b/lib/win/ApplicationWin32.cpp @@ -339,9 +339,10 @@ public: win.m_fsExStyle = GetWindowLong(win.m_hwnd, GWL_EXSTYLE); GetWindowRect(win.m_hwnd, &win.m_fsRect); - SetWindowLong(win.m_hwnd, GWL_STYLE, win.m_fsStyle & ~(WS_CAPTION | WS_THICKFRAME)); - SetWindowLong(win.m_hwnd, GWL_EXSTYLE, - win.m_fsExStyle & ~(WS_EX_DLGMODALFRAME | WS_EX_WINDOWEDGE | WS_EX_CLIENTEDGE | WS_EX_STATICEDGE)); + SetWindowLongPtr(win.m_hwnd, GWL_STYLE, win.m_fsStyle & ~(WS_CAPTION | WS_THICKFRAME)); + SetWindowLongPtr(win.m_hwnd, GWL_EXSTYLE, + win.m_fsExStyle & + ~(WS_EX_DLGMODALFRAME | WS_EX_WINDOWEDGE | WS_EX_CLIENTEDGE | WS_EX_STATICEDGE)); MONITORINFO monitor_info; monitor_info.cbSize = sizeof(monitor_info); @@ -353,8 +354,8 @@ public: win.m_fs = true; } else { - SetWindowLong(win.m_hwnd, GWL_STYLE, win.m_fsStyle); - SetWindowLong(win.m_hwnd, GWL_EXSTYLE, win.m_fsExStyle); + SetWindowLongPtr(win.m_hwnd, GWL_STYLE, win.m_fsStyle); + SetWindowLongPtr(win.m_hwnd, GWL_EXSTYLE, win.m_fsExStyle); SetWindowPos(win.m_hwnd, nullptr, win.m_fsRect.left, win.m_fsRect.top, win.m_fsRect.right - win.m_fsRect.left, win.m_fsRect.bottom - win.m_fsRect.top, SWP_NOZORDER | SWP_NOACTIVATE | SWP_FRAMECHANGED); diff --git a/lib/win/Win32Common.hpp b/lib/win/Win32Common.hpp index 3ef3381..c77916a 100644 --- a/lib/win/Win32Common.hpp +++ b/lib/win/Win32Common.hpp @@ -39,8 +39,8 @@ struct OGLContext { size_t width, height; bool m_fs = false; - LONG m_fsStyle; - LONG m_fsExStyle; + LONG_PTR m_fsStyle; + LONG_PTR m_fsExStyle; RECT m_fsRect; int m_fsCountDown = 0; }; diff --git a/lib/win/WindowWin32.cpp b/lib/win/WindowWin32.cpp index 0fbc1fb..68fe485 100644 --- a/lib/win/WindowWin32.cpp +++ b/lib/win/WindowWin32.cpp @@ -1300,35 +1300,43 @@ public: ETouchType getTouchType() const override { return ETouchType::None; } void setStyle(EWindowStyle style) override { - LONG sty = GetWindowLong(m_hwnd, GWL_STYLE); + LONG_PTR sty = GetWindowLongPtr(m_hwnd, GWL_STYLE); - if ((style & EWindowStyle::Titlebar) != EWindowStyle::None) + if ((style & EWindowStyle::Titlebar) != EWindowStyle::None) { sty |= WS_CAPTION; - else + } else { sty &= ~WS_CAPTION; + } - if ((style & EWindowStyle::Resize) != EWindowStyle::None) + if ((style & EWindowStyle::Resize) != EWindowStyle::None) { sty |= WS_THICKFRAME; - else + } else { sty &= ~WS_THICKFRAME; + } - if ((style & EWindowStyle::Close) != EWindowStyle::None) + if ((style & EWindowStyle::Close) != EWindowStyle::None) { sty |= (WS_SYSMENU | WS_MINIMIZEBOX | WS_MAXIMIZEBOX); - else + } else { sty &= ~(WS_SYSMENU | WS_MINIMIZEBOX | WS_MAXIMIZEBOX); + } - SetWindowLong(m_hwnd, GWL_STYLE, sty); + SetWindowLongPtr(m_hwnd, GWL_STYLE, sty); } EWindowStyle getStyle() const override { - LONG sty = GetWindowLong(m_hwnd, GWL_STYLE); + const LONG_PTR sty = GetWindowLongPtr(m_hwnd, GWL_STYLE); + EWindowStyle retval = EWindowStyle::None; - if ((sty & WS_CAPTION) != 0) + if ((sty & WS_CAPTION) != 0) { retval |= EWindowStyle::Titlebar; - if ((sty & WS_THICKFRAME) != 0) + } + if ((sty & WS_THICKFRAME) != 0) { retval |= EWindowStyle::Resize; - if ((sty & WS_SYSMENU)) + } + if ((sty & WS_SYSMENU)) { retval |= EWindowStyle::Close; + } + return retval; } From 7496109ff69ad26a7a95cab2570e3b6d6b376043 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Fri, 6 Sep 2019 07:23:34 -0400 Subject: [PATCH 04/13] NintendoPowerA: Make constructor explicit While we're at it we can also ensure that all class members have deterministic initial state. --- include/boo/inputdev/NintendoPowerA.hpp | 4 ++-- lib/inputdev/NintendoPowerA.cpp | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/include/boo/inputdev/NintendoPowerA.hpp b/include/boo/inputdev/NintendoPowerA.hpp index 20096a1..a7373a8 100644 --- a/include/boo/inputdev/NintendoPowerA.hpp +++ b/include/boo/inputdev/NintendoPowerA.hpp @@ -34,7 +34,7 @@ struct INintendoPowerACallback { }; class NintendoPowerA final : public TDeviceBase { - NintendoPowerAState m_last; + NintendoPowerAState m_last{}; void deviceDisconnected() override; void initialCycle() override; void transferCycle() override; @@ -42,7 +42,7 @@ class NintendoPowerA final : public TDeviceBase { void receivedHIDReport(const uint8_t* data, size_t length, HIDReportType tp, uint32_t message) override; public: - NintendoPowerA(DeviceToken*); + explicit NintendoPowerA(DeviceToken*); ~NintendoPowerA() override; }; } // namespace boo diff --git a/lib/inputdev/NintendoPowerA.cpp b/lib/inputdev/NintendoPowerA.cpp index fbc46dd..7b477cb 100644 --- a/lib/inputdev/NintendoPowerA.cpp +++ b/lib/inputdev/NintendoPowerA.cpp @@ -8,7 +8,7 @@ namespace boo { NintendoPowerA::NintendoPowerA(DeviceToken* token) : TDeviceBase(dev_typeid(NintendoPowerA), token) {} -NintendoPowerA::~NintendoPowerA() {} +NintendoPowerA::~NintendoPowerA() = default; void NintendoPowerA::deviceDisconnected() { std::lock_guard lk(m_callbackLock); From 2e0c7dc973c7b24e1168facb8da0e7e90a94660e Mon Sep 17 00:00:00 2001 From: Lioncash Date: Fri, 6 Sep 2019 07:26:03 -0400 Subject: [PATCH 05/13] NintendoPowerA: Use deduction guides for locks Same behavior, but without the need to explicitly hardcode the mutex type. --- lib/inputdev/NintendoPowerA.cpp | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/lib/inputdev/NintendoPowerA.cpp b/lib/inputdev/NintendoPowerA.cpp index 7b477cb..9b78d11 100644 --- a/lib/inputdev/NintendoPowerA.cpp +++ b/lib/inputdev/NintendoPowerA.cpp @@ -11,9 +11,10 @@ NintendoPowerA::NintendoPowerA(DeviceToken* token) NintendoPowerA::~NintendoPowerA() = default; void NintendoPowerA::deviceDisconnected() { - std::lock_guard lk(m_callbackLock); - if (m_callback) + std::lock_guard lk{m_callbackLock}; + if (m_callback != nullptr) { m_callback->controllerDisconnected(); + } } void NintendoPowerA::initialCycle() {} @@ -21,14 +22,16 @@ void NintendoPowerA::initialCycle() {} void NintendoPowerA::transferCycle() { uint8_t payload[8]; size_t recvSz = receiveUSBInterruptTransfer(payload, sizeof(payload)); - if (recvSz != 8) + if (recvSz != 8) { return; + } NintendoPowerAState state = *reinterpret_cast(&payload); - std::lock_guard lk(m_callbackLock); - if (state != m_last && m_callback) + std::lock_guard lk{m_callbackLock}; + if (state != m_last && m_callback != nullptr) { m_callback->controllerUpdate(state); + } m_last = state; } From 4b82c6510fa443b994b53dc9e5d8b93a1ab32eb7 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Fri, 6 Sep 2019 07:28:10 -0400 Subject: [PATCH 06/13] NintendoPowerA: Use std::array where applicable --- lib/inputdev/NintendoPowerA.cpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/inputdev/NintendoPowerA.cpp b/lib/inputdev/NintendoPowerA.cpp index 9b78d11..2da33d2 100644 --- a/lib/inputdev/NintendoPowerA.cpp +++ b/lib/inputdev/NintendoPowerA.cpp @@ -1,5 +1,6 @@ #include "boo/inputdev/NintendoPowerA.hpp" +#include #include #include "boo/inputdev/DeviceSignature.hpp" @@ -20,9 +21,9 @@ void NintendoPowerA::deviceDisconnected() { void NintendoPowerA::initialCycle() {} void NintendoPowerA::transferCycle() { - uint8_t payload[8]; - size_t recvSz = receiveUSBInterruptTransfer(payload, sizeof(payload)); - if (recvSz != 8) { + std::array payload; + const size_t recvSz = receiveUSBInterruptTransfer(payload.data(), payload.size()); + if (recvSz != payload.size()) { return; } From f445df17018f1e462d30056b3b25e024c5706682 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Fri, 6 Sep 2019 07:30:10 -0400 Subject: [PATCH 07/13] NintendoPowerA: Use std::memcpy within transferCycle() Eliminates undefined behavior within the function. --- lib/inputdev/NintendoPowerA.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/inputdev/NintendoPowerA.cpp b/lib/inputdev/NintendoPowerA.cpp index 2da33d2..788cacd 100644 --- a/lib/inputdev/NintendoPowerA.cpp +++ b/lib/inputdev/NintendoPowerA.cpp @@ -27,7 +27,8 @@ void NintendoPowerA::transferCycle() { return; } - NintendoPowerAState state = *reinterpret_cast(&payload); + NintendoPowerAState state; + std::memcpy(&state, payload.data(), sizeof(state)); std::lock_guard lk{m_callbackLock}; if (state != m_last && m_callback != nullptr) { @@ -41,7 +42,7 @@ void NintendoPowerA::finalCycle() {} void NintendoPowerA::receivedHIDReport(const uint8_t* data, size_t length, HIDReportType tp, uint32_t message) {} bool NintendoPowerAState::operator==(const NintendoPowerAState& other) const { - return memcmp(this, &other, sizeof(NintendoPowerAState)) == 0; + return std::memcmp(this, &other, sizeof(NintendoPowerAState)) == 0; } bool NintendoPowerAState::operator!=(const NintendoPowerAState& other) const { return !operator==(other); } From 4f4250baf7cbcafc4fd78de8ebb2a456d219bfe0 Mon Sep 17 00:00:00 2001 From: Phillip Stephens Date: Fri, 6 Sep 2019 23:30:04 -0700 Subject: [PATCH 08/13] Update athena --- logvisor | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/logvisor b/logvisor index f623ace..8c2e711 160000 --- a/logvisor +++ b/logvisor @@ -1 +1 @@ -Subproject commit f623ace3b4620c56722a1460ff2d9db61621f659 +Subproject commit 8c2e7113629f49f4eb1185af92f770f0bffaf85e From 660df8f7e614fa32b54e9fe146a492ce159ded31 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Mon, 9 Sep 2019 21:00:41 -0400 Subject: [PATCH 09/13] System: Make enum functions noexcept Allows them to be used within noexcept contexts --- include/boo/System.hpp | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/include/boo/System.hpp b/include/boo/System.hpp index 509bec2..15cfe4f 100644 --- a/include/boo/System.hpp +++ b/include/boo/System.hpp @@ -29,33 +29,33 @@ static inline ComPtr* ReferenceComPtr(ComPtr& ptr) { #ifndef ENABLE_BITWISE_ENUM #define ENABLE_BITWISE_ENUM(type) \ - constexpr type operator|(type a, type b) { \ + constexpr type operator|(type a, type b) noexcept { \ using T = std::underlying_type_t; \ return type(static_cast(a) | static_cast(b)); \ } \ - constexpr type operator&(type a, type b) { \ + constexpr type operator&(type a, type b) noexcept { \ using T = std::underlying_type_t; \ return type(static_cast(a) & static_cast(b)); \ } \ - constexpr type& operator|=(type& a, type b) { \ + constexpr type& operator|=(type& a, type b) noexcept { \ using T = std::underlying_type_t; \ a = type(static_cast(a) | static_cast(b)); \ return a; \ } \ - constexpr type& operator&=(type& a, type b) { \ + constexpr type& operator&=(type& a, type b) noexcept { \ using T = std::underlying_type_t; \ a = type(static_cast(a) & static_cast(b)); \ return a; \ } \ - constexpr type operator~(type key) { \ + constexpr type operator~(type key) noexcept { \ using T = std::underlying_type_t; \ return type(~static_cast(key)); \ } \ - constexpr bool True(type key) { \ + constexpr bool True(type key) noexcept { \ using T = std::underlying_type_t; \ return static_cast(key) != 0; \ } \ - constexpr bool False(type key) { \ + constexpr bool False(type key) noexcept { \ using T = std::underlying_type_t; \ return static_cast(key) == 0; \ } From f2ab814ce1488314da8787fab8852d7f66c8f837 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Mon, 9 Sep 2019 21:02:28 -0400 Subject: [PATCH 10/13] System: Implement False() in terms of True() This is just a negation, so we can do this to place the logic in one spot. --- include/boo/System.hpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/include/boo/System.hpp b/include/boo/System.hpp index 15cfe4f..7e03e10 100644 --- a/include/boo/System.hpp +++ b/include/boo/System.hpp @@ -56,8 +56,7 @@ static inline ComPtr* ReferenceComPtr(ComPtr& ptr) { return static_cast(key) != 0; \ } \ constexpr bool False(type key) noexcept { \ - using T = std::underlying_type_t; \ - return static_cast(key) == 0; \ + return !True(key); \ } #endif From 794a680797688f4c556f759ceca6f41deff49280 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Mon, 9 Sep 2019 21:12:54 -0400 Subject: [PATCH 11/13] IWindow: Use std::array where applicable Allows for more flexible copying/manipulation within using code. While we're at it, we can make the interface of SWindowRect and SScrollData constexpr, given they only manipulate primitives. --- include/boo/IWindow.hpp | 51 +++++++++++++++++------------------------ 1 file changed, 21 insertions(+), 30 deletions(-) diff --git a/include/boo/IWindow.hpp b/include/boo/IWindow.hpp index f1c05c6..b2cc022 100644 --- a/include/boo/IWindow.hpp +++ b/include/boo/IWindow.hpp @@ -1,8 +1,8 @@ #pragma once #include +#include #include -#include #include "boo/System.hpp" @@ -17,36 +17,30 @@ struct IAudioVoiceEngine; enum class EMouseButton { None = 0, Primary = 1, Secondary = 2, Middle = 3, Aux1 = 4, Aux2 = 5 }; struct SWindowCoord { - int pixel[2]; - int virtualPixel[2]; - float norm[2]; + std::array pixel; + std::array virtualPixel; + std::array norm; }; struct SWindowRect { - int location[2]; - int size[2]; + std::array location{}; + std::array size{}; - SWindowRect() { std::memset(this, 0, sizeof(SWindowRect)); } + constexpr SWindowRect() noexcept = default; + constexpr SWindowRect(int x, int y, int w, int h) noexcept : location{x, y}, size{w, h} {} - SWindowRect(int x, int y, int w, int h) { - location[0] = x; - location[1] = y; - size[0] = w; - size[1] = h; + constexpr bool operator==(const SWindowRect& other) const noexcept { + return location[0] == other.location[0] && location[1] == other.location[1] && size[0] == other.size[0] && + size[1] == other.size[1]; } + constexpr bool operator!=(const SWindowRect& other) const noexcept { return !operator==(other); } - bool operator!=(const SWindowRect& other) const { - return location[0] != other.location[0] || location[1] != other.location[1] || size[0] != other.size[0] || - size[1] != other.size[1]; - } - bool operator==(const SWindowRect& other) const { return !(*this != other); } - - bool coordInRect(const SWindowCoord& coord) const { + constexpr bool coordInRect(const SWindowCoord& coord) const noexcept { return coord.pixel[0] >= location[0] && coord.pixel[0] < location[0] + size[0] && coord.pixel[1] >= location[1] && coord.pixel[1] < location[1] + size[1]; } - SWindowRect intersect(const SWindowRect& other) const { + constexpr SWindowRect intersect(const SWindowRect& other) const noexcept { if (location[0] < other.location[0] + other.size[0] && location[0] + size[0] > other.location[0] && location[1] < other.location[1] + other.size[1] && location[1] + size[1] > other.location[1]) { SWindowRect ret; @@ -61,15 +55,15 @@ struct SWindowRect { }; struct STouchCoord { - double coord[2]; + std::array coord; }; struct SScrollDelta { - double delta[2] = {}; + std::array delta{}; bool isFine = false; /* Use system-scale fine-scroll (for scrollable-trackpads) */ bool isAccelerated = false; /* System performs acceleration computation */ - SScrollDelta operator+(const SScrollDelta& other) const { + constexpr SScrollDelta operator+(const SScrollDelta& other) const noexcept { SScrollDelta ret; ret.delta[0] = delta[0] + other.delta[0]; ret.delta[1] = delta[1] + other.delta[1]; @@ -77,7 +71,7 @@ struct SScrollDelta { ret.isAccelerated = isAccelerated || other.isAccelerated; return ret; } - SScrollDelta operator-(const SScrollDelta& other) const { + constexpr SScrollDelta operator-(const SScrollDelta& other) const noexcept { SScrollDelta ret; ret.delta[0] = delta[0] - other.delta[0]; ret.delta[1] = delta[1] - other.delta[1]; @@ -85,18 +79,15 @@ struct SScrollDelta { ret.isAccelerated = isAccelerated || other.isAccelerated; return ret; } - SScrollDelta& operator+=(const SScrollDelta& other) { + constexpr SScrollDelta& operator+=(const SScrollDelta& other) noexcept { delta[0] += other.delta[0]; delta[1] += other.delta[1]; isFine |= other.isFine; isAccelerated |= other.isAccelerated; return *this; } - void zeroOut() { - delta[0] = 0.0; - delta[1] = 0.0; - } - bool isZero() const { return delta[0] == 0.0 && delta[1] == 0.0; } + constexpr void zeroOut() noexcept { delta = {}; } + constexpr bool isZero() const noexcept { return delta[0] == 0.0 && delta[1] == 0.0; } }; enum class ESpecialKey { From 23cdae9e977f4c1769041eabde7f0edfdef2df79 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Mon, 9 Sep 2019 21:42:36 -0400 Subject: [PATCH 12/13] General: Make use of [[maybe_unused]] Replaces uses of (void)variable with the attribute introduced in C++17. --- include/boo/IGraphicsContext.hpp | 2 +- include/boo/IWindow.hpp | 82 +++++++++----------------------- lib/nx/WindowNX.cpp | 5 +- lib/x11/ApplicationWayland.hpp | 2 +- 4 files changed, 26 insertions(+), 65 deletions(-) diff --git a/include/boo/IGraphicsContext.hpp b/include/boo/IGraphicsContext.hpp index 334822d..7920144 100644 --- a/include/boo/IGraphicsContext.hpp +++ b/include/boo/IGraphicsContext.hpp @@ -10,7 +10,7 @@ struct IGraphicsDataFactory; class IGraphicsContext { friend class WindowCocoa; friend class WindowXCB; - virtual void _setCallback(class IWindowCallback* cb) { (void)cb; } + virtual void _setCallback([[maybe_unused]] class IWindowCallback* cb) {} public: enum class EGraphicsAPI { diff --git a/include/boo/IWindow.hpp b/include/boo/IWindow.hpp index f1c05c6..36f346e 100644 --- a/include/boo/IWindow.hpp +++ b/include/boo/IWindow.hpp @@ -155,67 +155,34 @@ struct ITextInputCallback { class IWindowCallback { public: - virtual void resized(const SWindowRect& rect, bool sync) { (void)rect; } - virtual void mouseDown(const SWindowCoord& coord, EMouseButton button, EModifierKey mods) { - (void)coord; - (void)button; - (void)mods; - } - virtual void mouseUp(const SWindowCoord& coord, EMouseButton button, EModifierKey mods) { - (void)coord; - (void)button; - (void)mods; - } - virtual void mouseMove(const SWindowCoord& coord) { (void)coord; } - virtual void mouseEnter(const SWindowCoord& coord) { (void)coord; } - virtual void mouseLeave(const SWindowCoord& coord) { (void)coord; } - virtual void scroll(const SWindowCoord& coord, const SScrollDelta& scroll) { - (void)coord; - (void)scroll; - } + virtual void resized([[maybe_unused]] const SWindowRect& rect, [[maybe_unused]] bool sync) {} + virtual void mouseDown([[maybe_unused]] const SWindowCoord& coord, [[maybe_unused]] EMouseButton button, + [[maybe_unused]] EModifierKey mods) {} + virtual void mouseUp([[maybe_unused]] const SWindowCoord& coord, [[maybe_unused]] EMouseButton button, + [[maybe_unused]] EModifierKey mods) {} + virtual void mouseMove([[maybe_unused]] const SWindowCoord& coord) {} + virtual void mouseEnter([[maybe_unused]] const SWindowCoord& coord) {} + virtual void mouseLeave([[maybe_unused]] const SWindowCoord& coord) {} + virtual void scroll([[maybe_unused]] const SWindowCoord& coord, [[maybe_unused]] const SScrollDelta& scroll) {} - virtual void touchDown(const STouchCoord& coord, uintptr_t tid) { - (void)coord; - (void)tid; - } - virtual void touchUp(const STouchCoord& coord, uintptr_t tid) { - (void)coord; - (void)tid; - } - virtual void touchMove(const STouchCoord& coord, uintptr_t tid) { - (void)coord; - (void)tid; - } + virtual void touchDown([[maybe_unused]] const STouchCoord& coord, [[maybe_unused]] uintptr_t tid) {} + virtual void touchUp([[maybe_unused]] const STouchCoord& coord, [[maybe_unused]] uintptr_t tid) {} + virtual void touchMove([[maybe_unused]] const STouchCoord& coord, [[maybe_unused]] uintptr_t tid) {} - virtual void charKeyDown(unsigned long charCode, EModifierKey mods, bool isRepeat) { - (void)charCode; - (void)mods; - (void)isRepeat; - } - virtual void charKeyUp(unsigned long charCode, EModifierKey mods) { - (void)charCode; - (void)mods; - } - virtual void specialKeyDown(ESpecialKey key, EModifierKey mods, bool isRepeat) { - (void)key; - (void)mods; - (void)isRepeat; - } - virtual void specialKeyUp(ESpecialKey key, EModifierKey mods) { - (void)key; - (void)mods; - } - virtual void modKeyDown(EModifierKey mod, bool isRepeat) { - (void)mod; - (void)isRepeat; - } - virtual void modKeyUp(EModifierKey mod) { (void)mod; } + virtual void charKeyDown([[maybe_unused]] unsigned long charCode, [[maybe_unused]] EModifierKey mods, + [[maybe_unused]] bool isRepeat) {} + virtual void charKeyUp([[maybe_unused]] unsigned long charCode, [[maybe_unused]] EModifierKey mods) {} + virtual void specialKeyDown([[maybe_unused]] ESpecialKey key, [[maybe_unused]] EModifierKey mods, + [[maybe_unused]] bool isRepeat) {} + virtual void specialKeyUp([[maybe_unused]] ESpecialKey key, [[maybe_unused]] EModifierKey mods) {} + virtual void modKeyDown([[maybe_unused]] EModifierKey mod, [[maybe_unused]] bool isRepeat) {} + virtual void modKeyUp([[maybe_unused]] EModifierKey mod) {} virtual ITextInputCallback* getTextInputCallback() { return nullptr; } virtual void focusLost() {} virtual void focusGained() {} - virtual void windowMoved(const SWindowRect& rect) { (void)rect; } + virtual void windowMoved([[maybe_unused]] const SWindowRect& rect) {} virtual void destroyed() {} }; @@ -278,10 +245,7 @@ public: virtual int waitForRetrace() = 0; virtual uintptr_t getPlatformHandle() const = 0; - virtual bool _incomingEvent(void* event) { - (void)event; - return false; - } + virtual bool _incomingEvent([[maybe_unused]] void* event) { return false; } virtual void _cleanup() {} virtual ETouchType getTouchType() const = 0; @@ -289,7 +253,7 @@ public: virtual void setStyle(EWindowStyle style) = 0; virtual EWindowStyle getStyle() const = 0; - virtual void setTouchBarProvider(void*) {} + virtual void setTouchBarProvider([[maybe_unused]] void* provider) {} virtual IGraphicsCommandQueue* getCommandQueue() = 0; virtual IGraphicsDataFactory* getDataFactory() = 0; diff --git a/lib/nx/WindowNX.cpp b/lib/nx/WindowNX.cpp index d8ba12a..f20ae10 100644 --- a/lib/nx/WindowNX.cpp +++ b/lib/nx/WindowNX.cpp @@ -89,10 +89,7 @@ public: void waitForRetrace() override {} uintptr_t getPlatformHandle() const override { return 0; } - bool _incomingEvent(void* event) override { - (void)event; - return false; - } + bool _incomingEvent([[maybe_unused]] void* event) override { return false; } void _cleanup() override {} ETouchType getTouchType() const override { return ETouchType::Display; } diff --git a/lib/x11/ApplicationWayland.hpp b/lib/x11/ApplicationWayland.hpp index e4743b4..b993f3e 100644 --- a/lib/x11/ApplicationWayland.hpp +++ b/lib/x11/ApplicationWayland.hpp @@ -19,7 +19,7 @@ class ApplicationWayland final : public IApplication { const std::vector m_args; bool m_singleInstance; - void _deletedWindow(IWindow* window) override { (void)window; } + void _deletedWindow([[maybe_unused]] IWindow* window) override {} public: ApplicationWayland(IApplicationCallback& callback, std::string_view uniqueName, std::string_view friendlyName, From 0c5f0e0d2611db3eeea4ee29016372f4cca4b0b4 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Mon, 9 Sep 2019 21:48:17 -0400 Subject: [PATCH 13/13] WindowWin32: Move variable into ifdef Avoids the need to have a separate ifdef path to silence an unused variable warning. --- lib/win/WindowWin32.cpp | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/lib/win/WindowWin32.cpp b/lib/win/WindowWin32.cpp index 68fe485..2959b0d 100644 --- a/lib/win/WindowWin32.cpp +++ b/lib/win/WindowWin32.cpp @@ -859,18 +859,17 @@ public: m_hwnd = CreateWindowW(L"BooWindow", title.data(), WS_OVERLAPPEDWINDOW, r.left, r.top, r.right - r.left, r.bottom - r.top, nullptr, nullptr, nullptr, nullptr); - HINSTANCE wndInstance = HINSTANCE(GetWindowLongPtr(m_hwnd, GWLP_HINSTANCE)); m_imc = ImmGetContext(m_hwnd); #if BOO_HAS_VULKAN + HINSTANCE wndInstance = HINSTANCE(GetWindowLongPtr(m_hwnd, GWLP_HINSTANCE)); if (b3dCtx.m_vulkanDxFactory) { m_gfxCtx.reset(new GraphicsContextWin32Vulkan(this, wndInstance, m_hwnd, &g_VulkanContext, b3dCtx)); if (m_gfxCtx->initializeContext(nullptr)) return; } -#else - (void)wndInstance; #endif + IGraphicsContext::EGraphicsAPI api = IGraphicsContext::EGraphicsAPI::D3D11; if (b3dCtx.m_ctxOgl.m_dxFactory) { m_gfxCtx.reset(new GraphicsContextWin32GL(IGraphicsContext::EGraphicsAPI::OpenGL3_3, this, m_hwnd, b3dCtx));