Skip to content

Commit d906325

Browse files
committed
In-client protocol debugger
Optional (of course). Thanks to an idea @Amrsatrio had.
1 parent 469f94e commit d906325

22 files changed

Lines changed: 59091 additions & 14 deletions

CMakeLists.txt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,12 +87,18 @@ if (MDR_BUILD_WITH_ASAN)
8787
endif ()
8888

8989
option(MDR_DEBUG "Build with debug flags enabled (also always on for Debug builds)" OFF)
90+
option(MDR_DEBUG_TRAPS "Break into the debugger when MDR operations fail in Debug builds" OFF)
91+
option(MDR_CLIENT_DEBUGGER "Build the ImGui packet debugger in all configurations" OFF)
92+
set(MDR_CLIENT_DEBUGGER_TARGET OFF)
93+
if (MDR_CLIENT_DEBUGGER OR CMAKE_BUILD_TYPE STREQUAL "Debug" OR CMAKE_CONFIGURATION_TYPES)
94+
set(MDR_CLIENT_DEBUGGER_TARGET ON)
95+
endif ()
9096
if (CMAKE_BUILD_TYPE STREQUAL "Debug")
9197
set(MDR_DEBUG ON)
9298
endif ()
9399
add_compile_definitions(
94100
$<$<OR:$<BOOL:${MDR_DEBUG}>,$<CONFIG:Debug>>:MDR_DEBUG>
95-
$<$<CONFIG:Debug>:MDR_DEBUG_TRAPS>
101+
$<$<AND:$<BOOL:${MDR_DEBUG_TRAPS}>,$<CONFIG:Debug>>:MDR_DEBUG_TRAPS>
96102
)
97103
if (MDR_DEBUG)
98104
message(STATUS "Debugging features *ON*. Enjoy the logs.")

client/CMakeLists.txt

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,35 @@
11
set(CLIENT_VERSION "2.0.0")
22

3+
set(CLIENT_DEBUGGER_GENERATED_SOURCES
4+
"${CMAKE_CURRENT_SOURCE_DIR}/Generated/ProtocolV1T1Debugger.cpp"
5+
"${CMAKE_CURRENT_SOURCE_DIR}/Generated/ProtocolV1T2Debugger.cpp"
6+
"${CMAKE_CURRENT_SOURCE_DIR}/Generated/ProtocolV2T1Debugger.cpp"
7+
"${CMAKE_CURRENT_SOURCE_DIR}/Generated/ProtocolV2T2Debugger.cpp"
8+
)
9+
10+
if (MDR_CLIENT_DEBUGGER_TARGET AND COMMAND mdr_add_codegen)
11+
mdr_add_codegen(OUTPUT "${CMAKE_CURRENT_SOURCE_DIR}/Generated/ProtocolV1T1Debugger.cpp"
12+
TOOL tooling_DebuggerCodegen
13+
INPUT "${CMAKE_SOURCE_DIR}/libmdr/include/mdr/ProtocolV1T1.hpp"
14+
NAMESPACE "mdr::v1::t1"
15+
INCLUDE "mdr/ProtocolV1T1.hpp")
16+
mdr_add_codegen(OUTPUT "${CMAKE_CURRENT_SOURCE_DIR}/Generated/ProtocolV1T2Debugger.cpp"
17+
TOOL tooling_DebuggerCodegen
18+
INPUT "${CMAKE_SOURCE_DIR}/libmdr/include/mdr/ProtocolV1T2.hpp"
19+
NAMESPACE "mdr::v1::t2"
20+
INCLUDE "mdr/ProtocolV1T2.hpp")
21+
mdr_add_codegen(OUTPUT "${CMAKE_CURRENT_SOURCE_DIR}/Generated/ProtocolV2T1Debugger.cpp"
22+
TOOL tooling_DebuggerCodegen
23+
INPUT "${CMAKE_SOURCE_DIR}/libmdr/include/mdr/ProtocolV2T1.hpp"
24+
NAMESPACE "mdr::v2::t1"
25+
INCLUDE "mdr/ProtocolV2T1.hpp")
26+
mdr_add_codegen(OUTPUT "${CMAKE_CURRENT_SOURCE_DIR}/Generated/ProtocolV2T2Debugger.cpp"
27+
TOOL tooling_DebuggerCodegen
28+
INPUT "${CMAKE_SOURCE_DIR}/libmdr/include/mdr/ProtocolV2T2.hpp"
29+
NAMESPACE "mdr::v2::t2"
30+
INCLUDE "mdr/ProtocolV2T2.hpp")
31+
endif ()
32+
333
source_group(TREE ${CMAKE_CURRENT_SOURCE_DIR} FILES ${SOURCES} ${INCLUDES})
434

535
if (MDR_PLATFORM_OS STREQUAL "WIN32")
@@ -23,6 +53,7 @@ target_sources(
2353
PRIVATE
2454
SDLMain.cpp
2555
Client.cpp
56+
PacketObserver.cpp
2657
PayloadRecorder.cpp
2758
Fonts/PlexSansIcon.c
2859
)
@@ -79,6 +110,29 @@ target_include_directories(ImGuiSDL3 PUBLIC
79110
.
80111
)
81112

113+
if (MDR_CLIENT_DEBUGGER_TARGET)
114+
add_library(Client_Debugger STATIC
115+
Debugger.cpp
116+
Debugger.hpp
117+
DebuggerDetails.hpp
118+
${CLIENT_DEBUGGER_GENERATED_SOURCES}
119+
)
120+
target_link_libraries(Client_Debugger PUBLIC mdr ImGuiSDL3)
121+
target_include_directories(Client_Debugger PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}")
122+
endif ()
123+
124+
if (MDR_CLIENT_DEBUGGER_TARGET)
125+
set(CLIENT_DEBUGGER_ENABLED
126+
$<OR:$<BOOL:${MDR_CLIENT_DEBUGGER}>,$<CONFIG:Debug>>
127+
)
128+
target_link_libraries(SonyHeadphonesClient PRIVATE
129+
$<${CLIENT_DEBUGGER_ENABLED}:Client_Debugger>
130+
)
131+
target_compile_definitions(SonyHeadphonesClient PRIVATE
132+
$<${CLIENT_DEBUGGER_ENABLED}:MDR_CLIENT_DEBUGGER>
133+
)
134+
endif ()
135+
82136
set(CLIENT_BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}")
83137
add_subdirectory(Platform)
84138

client/Client.cpp

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,17 @@
1818
#include <mdr/Protocol.hpp>
1919
#include "Fonts/PlexSansIcon.h"
2020
#include "MaterialYouTheme.hpp"
21+
#include "PacketObserver.hpp"
2122
#include "Platform/Platform.hpp"
22-
#include "PayloadRecorder.hpp"
23+
#ifdef MDR_CLIENT_DEBUGGER
24+
#include "Debugger.hpp"
25+
#endif
2326

2427
MDRHeadphones* gDevice;
2528
mdr::String gHeadphonesError;
29+
#ifdef MDR_CLIENT_DEBUGGER
30+
bool gDebuggerOpen{};
31+
#endif
2632

2733
template <typename T>
2834
T MDRStruct()
@@ -373,7 +379,11 @@ void CloseDevice()
373379
{
374380
if (!gDevice)
375381
return;
382+
#ifdef MDR_CLIENT_DEBUGGER
383+
gDebuggerOpen = false;
384+
#endif
376385
gHeadphonesError = GetText(MDR_TEXT_LAST_ERROR);
386+
clientPacketObserverDetach();
377387
mdrHeadphonesDestroy(gDevice);
378388
gDevice = nullptr;
379389
}
@@ -893,7 +903,7 @@ void DrawDeviceConnecting()
893903
DisconnectWithModal();
894904
return;
895905
}
896-
clientPayloadRecorderAttach(gDevice);
906+
clientPacketObserverAttach(gDevice);
897907
if (mdrHeadphonesRequestInit(gDevice) != MDR_RESULT_OK)
898908
DisconnectWithModal();
899909

@@ -970,6 +980,10 @@ void DrawDeviceControlsHeader()
970980
}
971981
}
972982
}
983+
#ifdef MDR_CLIENT_DEBUGGER
984+
ImGui::Separator();
985+
ImGui::MenuItem("Protocol Debugger", nullptr, &gDebuggerOpen);
986+
#endif
973987
ImGui::EndMenu();
974988
}
975989
if (!gDevice)
@@ -1012,7 +1026,7 @@ void DrawDeviceControlsHeader()
10121026
ImVec2 size = ImGui::CalcTextSize(s);
10131027
badgeRegionX += size.x + padding.x * 2, badgeRegionY = std::max(badgeRegionY, size.y);
10141028
}
1015-
ImGui::SameLine(ImGui::GetCursorPosX() + ImGui::GetContentRegionAvail().x - badgeRegionX);
1029+
ImGui::SameLine(ImGui::GetCursorPosX() + ImGui::GetContentRegionAvail().x - badgeRegionX - padding.x * 2);
10161030
float rounding = style.FrameRounding;
10171031
float offsetY = padding.y / 2;
10181032
for (auto& [s, border, text] : badges)
@@ -1801,6 +1815,10 @@ void DrawApp()
18011815
}
18021816
}
18031817
ImGui::End();
1818+
#ifdef MDR_CLIENT_DEBUGGER
1819+
if (gDebuggerOpen || ImGui::IsPopupOpen("Command Playground"))
1820+
clientDebuggerDraw(&gDebuggerOpen);
1821+
#endif
18041822
}
18051823

18061824
bool clientShouldExit()

0 commit comments

Comments
 (0)