Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add debugger APIs for buttons and analogs #13946

Merged
merged 3 commits into from
Jan 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1492,6 +1492,10 @@ add_library(${CoreLibName} ${CoreLinkType}
Core/Debugger/WebSocket/GPURecordSubscriber.h
Core/Debugger/WebSocket/HLESubscriber.cpp
Core/Debugger/WebSocket/HLESubscriber.h
Core/Debugger/WebSocket/InputBroadcaster.cpp
Core/Debugger/WebSocket/InputBroadcaster.h
Core/Debugger/WebSocket/InputSubscriber.cpp
Core/Debugger/WebSocket/InputSubscriber.h
Core/Debugger/WebSocket/LogBroadcaster.cpp
Core/Debugger/WebSocket/LogBroadcaster.h
Core/Debugger/WebSocket/MemorySubscriber.cpp
Expand Down
4 changes: 4 additions & 0 deletions Core/Core.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,8 @@
<ClCompile Include="Debugger\WebSocket\GPUBufferSubscriber.cpp" />
<ClCompile Include="Debugger\WebSocket\GPURecordSubscriber.cpp" />
<ClCompile Include="Debugger\WebSocket\HLESubscriber.cpp" />
<ClCompile Include="Debugger\WebSocket\InputBroadcaster.cpp" />
<ClCompile Include="Debugger\WebSocket\InputSubscriber.cpp" />
<ClCompile Include="Debugger\WebSocket\LogBroadcaster.cpp" />
<ClCompile Include="Debugger\WebSocket\DisasmSubscriber.cpp" />
<ClCompile Include="Debugger\WebSocket\MemorySubscriber.cpp" />
Expand Down Expand Up @@ -981,6 +983,8 @@
<ClInclude Include="Debugger\WebSocket\GPUBufferSubscriber.h" />
<ClInclude Include="Debugger\WebSocket\GPURecordSubscriber.h" />
<ClInclude Include="Debugger\WebSocket\HLESubscriber.h" />
<ClInclude Include="Debugger\WebSocket\InputBroadcaster.h" />
<ClInclude Include="Debugger\WebSocket\InputSubscriber.h" />
<ClInclude Include="Debugger\WebSocket\SteppingSubscriber.h" />
<ClInclude Include="Debugger\WebSocket\WebSocketUtils.h" />
<ClInclude Include="Debugger\WebSocket\CPUCoreSubscriber.h" />
Expand Down
12 changes: 12 additions & 0 deletions Core/Core.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -965,6 +965,12 @@
<ClCompile Include="ThreadPools.cpp">
<Filter>Core</Filter>
</ClCompile>
<ClCompile Include="Debugger\WebSocket\InputSubscriber.cpp">
<Filter>Debugger\WebSocket</Filter>
</ClCompile>
<ClCompile Include="Debugger\WebSocket\InputBroadcaster.cpp">
<Filter>Debugger\WebSocket</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="ELF\ElfReader.h">
Expand Down Expand Up @@ -1649,6 +1655,12 @@
<ClInclude Include="ThreadPools.h">
<Filter>Core</Filter>
</ClInclude>
<ClInclude Include="Debugger\WebSocket\InputSubscriber.h">
<Filter>Debugger\WebSocket</Filter>
</ClInclude>
<ClInclude Include="Debugger\WebSocket\InputBroadcaster.h">
<Filter>Debugger\WebSocket</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="CMakeLists.txt" />
Expand Down
7 changes: 6 additions & 1 deletion Core/Debugger/WebSocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
// For other events, look inside Core/Debugger/WebSocket/ for details on each event.

#include "Core/Debugger/WebSocket/GameBroadcaster.h"
#include "Core/Debugger/WebSocket/InputBroadcaster.h"
#include "Core/Debugger/WebSocket/LogBroadcaster.h"
#include "Core/Debugger/WebSocket/SteppingBroadcaster.h"

Expand All @@ -55,6 +56,7 @@
#include "Core/Debugger/WebSocket/GPUBufferSubscriber.h"
#include "Core/Debugger/WebSocket/GPURecordSubscriber.h"
#include "Core/Debugger/WebSocket/HLESubscriber.h"
#include "Core/Debugger/WebSocket/InputSubscriber.h"
#include "Core/Debugger/WebSocket/MemorySubscriber.h"
#include "Core/Debugger/WebSocket/SteppingSubscriber.h"

Expand All @@ -67,6 +69,7 @@ static const std::vector<SubscriberInit> subscribers({
&WebSocketGPUBufferInit,
&WebSocketGPURecordInit,
&WebSocketHLEInit,
&WebSocketInputInit,
&WebSocketMemoryInit,
&WebSocketSteppingInit,
});
Expand Down Expand Up @@ -125,8 +128,9 @@ void HandleDebuggerRequest(const http::Request &request) {
UpdateConnected(1);
SetupDebuggerLock();

LogBroadcaster logger;
GameBroadcaster game;
LogBroadcaster logger;
InputBroadcaster input;
SteppingBroadcaster stepping;

std::unordered_map<std::string, DebuggerEventHandler> eventHandlers;
Expand Down Expand Up @@ -175,6 +179,7 @@ void HandleDebuggerRequest(const http::Request &request) {
logger.Broadcast(ws);
game.Broadcast(ws);
stepping.Broadcast(ws);
input.Broadcast(ws);

for (size_t i = 0; i < subscribers.size(); ++i) {
if (subscriberData[i]) {
Expand Down
102 changes: 102 additions & 0 deletions Core/Debugger/WebSocket/InputBroadcaster.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
// Copyright (c) 2018- PPSSPP Project.

// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, version 2.0 or later versions.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License 2.0 for more details.

// A copy of the GPL 2.0 should have been included with the program.
// If not, see http://www.gnu.org/licenses/

// Official git repository and contact information can be found at
// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.

#include <unordered_map>
#include "Core/Debugger/WebSocket/InputBroadcaster.h"
#include "Core/Debugger/WebSocket/InputSubscriber.h"
#include "Core/Debugger/WebSocket/WebSocketUtils.h"
#include "Core/HLE/sceCtrl.h"
#include "Core/HLE/sceDisplay.h"

// Button press state change (input.buttons)
//
// Sent unexpectedly with these properties:
// - buttons: an object with button names as keys and bool press state as values.
// - changed: same as buttons, but only including changed states.
//
// See input.buttons.send in InputSubscriber for button names.

// Analog position change (input.analog)
//
// Sent unexpectedly with these properties:
// - stick: "left" or "right".
// - x: number between -1.0 and 1.0, representing horizontal position in a square.
// - y: number between -1.0 and 1.0, representing vertical position in a square.

std::string InputBroadcaster::Analog::Event(const char *stick) {
JsonWriter j;
j.begin();
j.writeString("event", "input.analog");
j.writeString("stick", stick);
j.writeFloat("x", x);
j.writeFloat("y", y);
j.end();
return j.str();
}

static std::string ButtonsEvent(uint32_t lastButtons, uint32_t newButtons) {
uint32_t pressed = newButtons & ~lastButtons;
uint32_t released = ~newButtons & lastButtons;

JsonWriter j;
j.begin();
j.writeString("event", "input.buttons");
j.pushDict("buttons");
for (auto it : WebSocketInputButtonLookup()) {
j.writeBool(it.first, (newButtons & it.second) != 0);
}
j.pop();
j.pushDict("changed");
for (auto it : WebSocketInputButtonLookup()) {
if (pressed & it.second) {
j.writeBool(it.first, true);
} else if (released & it.second) {
j.writeBool(it.first, false);
}
}
j.pop();
j.end();
return j.str();
}

void InputBroadcaster::Broadcast(net::WebSocketServer *ws) {
int counter = __DisplayGetNumVblanks();
if (lastCounter_ == counter)
return;
lastCounter_ = counter;

uint32_t newButtons = __CtrlPeekButtons();
if (newButtons != lastButtons_) {
ws->Send(ButtonsEvent(lastButtons_, newButtons));
lastButtons_ = newButtons;
}

Analog newAnalog;
__CtrlPeekAnalog(CTRL_STICK_LEFT, &newAnalog.x, &newAnalog.y);
if (!lastAnalog_[0].Equals(newAnalog)) {
ws->Send(newAnalog.Event("left"));
lastAnalog_[0].x = newAnalog.x;
lastAnalog_[0].y = newAnalog.y;
}

__CtrlPeekAnalog(CTRL_STICK_RIGHT, &newAnalog.x, &newAnalog.y);
if (!lastAnalog_[1].Equals(newAnalog)) {
ws->Send(newAnalog.Event("right"));
lastAnalog_[1].x = newAnalog.x;
lastAnalog_[1].y = newAnalog.y;
}
}
48 changes: 48 additions & 0 deletions Core/Debugger/WebSocket/InputBroadcaster.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright (c) 2018- PPSSPP Project.

// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, version 2.0 or later versions.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License 2.0 for more details.

// A copy of the GPL 2.0 should have been included with the program.
// If not, see http://www.gnu.org/licenses/

// Official git repository and contact information can be found at
// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.

#pragma once

#include <cstdint>
#include <string>

namespace net {
class WebSocketServer;
}

struct InputBroadcaster {
public:
InputBroadcaster() {
}

void Broadcast(net::WebSocketServer *ws);

private:
struct Analog {
float x = 0.0f;
float y = 0.0f;

bool Equals(const Analog &other) const {
return x == other.x && y == other.y;
}
std::string Event(const char *stick);
};

int lastCounter_ = -1;
uint32_t lastButtons_ = 0;
Analog lastAnalog_[2];
};
Loading