CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
Path: blob/master/Core/Debugger/WebSocket/InputBroadcaster.cpp
Views: 1401
// Copyright (c) 2018- PPSSPP Project.12// This program is free software: you can redistribute it and/or modify3// it under the terms of the GNU General Public License as published by4// the Free Software Foundation, version 2.0 or later versions.56// This program is distributed in the hope that it will be useful,7// but WITHOUT ANY WARRANTY; without even the implied warranty of8// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the9// GNU General Public License 2.0 for more details.1011// A copy of the GPL 2.0 should have been included with the program.12// If not, see http://www.gnu.org/licenses/1314// Official git repository and contact information can be found at15// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.1617#include <unordered_map>18#include "Core/Debugger/WebSocket/InputBroadcaster.h"19#include "Core/Debugger/WebSocket/InputSubscriber.h"20#include "Core/Debugger/WebSocket/WebSocketUtils.h"21#include "Core/HLE/sceCtrl.h"22#include "Core/HW/Display.h"2324// Button press state change (input.buttons)25//26// Sent unexpectedly with these properties:27// - buttons: an object with button names as keys and bool press state as values.28// - changed: same as buttons, but only including changed states.29//30// See input.buttons.send in InputSubscriber for button names.3132// Analog position change (input.analog)33//34// Sent unexpectedly with these properties:35// - stick: "left" or "right".36// - x: number between -1.0 and 1.0, representing horizontal position in a square.37// - y: number between -1.0 and 1.0, representing vertical position in a square.3839std::string InputBroadcaster::Analog::Event(const char *stick) {40JsonWriter j;41j.begin();42j.writeString("event", "input.analog");43j.writeString("stick", stick);44j.writeFloat("x", x);45j.writeFloat("y", y);46j.end();47return j.str();48}4950static std::string ButtonsEvent(uint32_t lastButtons, uint32_t newButtons) {51uint32_t pressed = newButtons & ~lastButtons;52uint32_t released = ~newButtons & lastButtons;5354JsonWriter j;55j.begin();56j.writeString("event", "input.buttons");57j.pushDict("buttons");58for (auto it : WebSocketInputButtonLookup()) {59j.writeBool(it.first, (newButtons & it.second) != 0);60}61j.pop();62j.pushDict("changed");63for (auto it : WebSocketInputButtonLookup()) {64if (pressed & it.second) {65j.writeBool(it.first, true);66} else if (released & it.second) {67j.writeBool(it.first, false);68}69}70j.pop();71j.end();72return j.str();73}7475void InputBroadcaster::Broadcast(net::WebSocketServer *ws) {76int counter = __DisplayGetNumVblanks();77if (lastCounter_ == counter)78return;79lastCounter_ = counter;8081uint32_t newButtons = __CtrlPeekButtons();82if (newButtons != lastButtons_) {83ws->Send(ButtonsEvent(lastButtons_, newButtons));84lastButtons_ = newButtons;85}8687Analog newAnalog;88__CtrlPeekAnalog(CTRL_STICK_LEFT, &newAnalog.x, &newAnalog.y);89if (!lastAnalog_[0].Equals(newAnalog)) {90ws->Send(newAnalog.Event("left"));91lastAnalog_[0].x = newAnalog.x;92lastAnalog_[0].y = newAnalog.y;93}9495__CtrlPeekAnalog(CTRL_STICK_RIGHT, &newAnalog.x, &newAnalog.y);96if (!lastAnalog_[1].Equals(newAnalog)) {97ws->Send(newAnalog.Event("right"));98lastAnalog_[1].x = newAnalog.x;99lastAnalog_[1].y = newAnalog.y;100}101}102103104