Skip to content

Commit 1282660

Browse files
author
Sebi
committed
Adding SocketInput
1 parent dfa80c8 commit 1282660

File tree

5 files changed

+158
-1
lines changed

5 files changed

+158
-1
lines changed

CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ ADD_Executable(DasherUI
6666
${PROJECT_SOURCE_DIR}/src/MainWindow.h
6767
${PROJECT_SOURCE_DIR}/src/DasherUIScreen.cpp
6868
${PROJECT_SOURCE_DIR}/src/DasherUIScreen.h
69+
${PROJECT_SOURCE_DIR}/src/SocketInput.cpp
70+
${PROJECT_SOURCE_DIR}/src/SocketInput.h
6971
${PROJECT_SOURCE_DIR}/src/main.cpp
7072
)
7173
target_include_directories(DasherUI PUBLIC ${CMAKE_CURRENT_LIST_DIR}/Thirdparty/)

src/DasherController.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,12 @@ void DasherController::CreateModules()
8888
CDashIntfScreenMsgs::CreateModules();
8989

9090
RegisterModule(ScreenModule.get());
91-
SetDefaultInputDevice(ScreenModule.get());
91+
92+
SocketInputModule = std::make_shared<SocketInput>(static_cast<CSettingsUser*>(this), this, m_pFramerate);
93+
RegisterModule(static_cast<Dasher::CInputFilter*>(SocketInputModule.get()));
94+
SocketInputModule->startListen();
95+
96+
SetDefaultInputDevice(SocketInputModule.get());
9297
}
9398

9499
void DasherController::CopyToClipboard(const std::string& text)

src/DasherController.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "DasherUIScreen.h"
55
#include "imgui.h"
66
#include "imgui_internal.h"
7+
#include "SocketInput.h"
78

89

910
class DasherController : public Dasher::CDashIntfScreenMsgs
@@ -43,4 +44,5 @@ class DasherController : public Dasher::CDashIntfScreenMsgs
4344

4445
//Modules
4546
std::shared_ptr<DasherUIScreen> ScreenModule;
47+
std::shared_ptr<SocketInput> SocketInputModule;
4648
};

src/SocketInput.cpp

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
#include "SocketInput.h"
2+
3+
#include <I18n.h>
4+
#include <thread>
5+
6+
#include "DasherInterfaceBase.h"
7+
#include "DasherModel.h"
8+
9+
SocketInput::SocketInput(CSettingsUser* Creator, Dasher::CDashIntfSettings* Controller, Dasher::CFrameRate* pFramerate): CScreenCoordInput(0, _("Socket Input")),
10+
CDefaultFilter(Creator, Controller, pFramerate, 2, "Socket Mode")
11+
{
12+
xLabel = Controller->GetStringParameter(Dasher::Parameter::SP_SOCKET_INPUT_X_LABEL);
13+
yLabel = Controller->GetStringParameter(Dasher::Parameter::SP_SOCKET_INPUT_Y_LABEL);
14+
}
15+
16+
SocketInput::~SocketInput()
17+
{
18+
stopListen();
19+
}
20+
21+
bool SocketInput::GetScreenCoords(Dasher::screenint& iX, Dasher::screenint& iY, Dasher::CDasherView* pView)
22+
{
23+
const Dasher::CDasherView::ScreenRegion screenRegion = pView->VisibleRegion();
24+
25+
const double vectorLength = std::min(Dasher::CDasherModel::ORIGIN_Y - screenRegion.minY, screenRegion.maxY - Dasher::CDasherModel::ORIGIN_Y);
26+
const double normalization = sqrt(lastRelativeX * lastRelativeX + lastRelativeY * lastRelativeY);
27+
28+
pView->Dasher2Screen(
29+
Dasher::CDasherModel::ORIGIN_X - static_cast<Dasher::myint>(lastRelativeX * (normalization > 1.0 ? vectorLength / normalization : vectorLength)),
30+
Dasher::CDasherModel::ORIGIN_Y - static_cast<Dasher::myint>(lastRelativeY * (normalization > 1.0 ? vectorLength / normalization : vectorLength)),
31+
iX, iY);
32+
33+
return true;
34+
}
35+
36+
void SocketInput::startListen()
37+
{
38+
session_acceptor = std::make_unique<asio::ip::tcp::acceptor>(io_service, asio::ip::tcp::endpoint(asio::ip::tcp::v4(), static_cast<short>(Controller->GetLongParameter(Dasher::LP_SOCKET_PORT))));
39+
40+
receiveThread = std::thread([this](){
41+
42+
asio::error_code ec;
43+
keepListenServerAlive = true;
44+
while(keepListenServerAlive)
45+
{
46+
open_socket = std::make_unique<asio::ip::tcp::socket>(session_acceptor->accept(ec));
47+
48+
if(ec) continue; //error while accepting occured
49+
50+
keepReadThreadAlive = true;
51+
size_t bytes_read = 0;
52+
while(keepReadThreadAlive)
53+
{
54+
if((bytes_read = read_until(*open_socket, stream_buffer, '\n', ec)))
55+
{
56+
std::string_view data{
57+
static_cast<const char*>(stream_buffer.data().data()),
58+
stream_buffer.size()
59+
};
60+
61+
try {
62+
std::string_view cmd = data.substr(0, data.find(' '));
63+
const double num = std::stod(data.data() + cmd.size()); //skip first two characters <cmd><whitespace>
64+
65+
if(cmd == xLabel)
66+
{
67+
lastRelativeX = num;
68+
relativeMode = true;
69+
}else if(cmd == yLabel)
70+
{
71+
lastRelativeY = num;
72+
relativeMode = true;
73+
}else if(cmd == startStopLabel)
74+
{
75+
if(num >= 0.5)
76+
{
77+
run(0);
78+
}else
79+
{
80+
stop();
81+
}
82+
}
83+
} catch (std::exception& e)
84+
{
85+
std::cout << "Wrong format for socket input" << std::endl;
86+
}
87+
88+
stream_buffer.consume(bytes_read);
89+
} else if(ec) break;
90+
}
91+
open_socket->close();
92+
}
93+
});
94+
}
95+
96+
void SocketInput::stopListen()
97+
{
98+
keepListenServerAlive = false;
99+
keepReadThreadAlive = false;
100+
session_acceptor->close();
101+
open_socket->close();
102+
receiveThread.join();
103+
}

src/SocketInput.h

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#pragma once
2+
3+
#include "DasherInput.h"
4+
#include "asio.hpp"
5+
#include "DashIntfSettings.h"
6+
#include "PressFilter.h"
7+
8+
class SocketInput : public Dasher::CScreenCoordInput, public Dasher::CDefaultFilter
9+
{
10+
public:
11+
SocketInput(CSettingsUser* Creator, Dasher::CDashIntfSettings* Controller, Dasher::CFrameRate* pFramerate);
12+
virtual ~SocketInput();
13+
14+
bool GetScreenCoords(Dasher::screenint& iX, Dasher::screenint& iY, Dasher::CDasherView* pView) override;
15+
16+
void startListen();
17+
void stopListen();
18+
19+
bool GetSettings(SModuleSettings** sets, int* iCount) override;
20+
private:
21+
bool relativeMode;
22+
double lastRelativeX = 0.5;
23+
double lastRelativeY = 0.5;
24+
Dasher::screenint lastX = 0;
25+
Dasher::screenint lastY = 0;
26+
27+
//Dasher Stuff
28+
Dasher::CDashIntfSettings* Controller;
29+
30+
//Commands
31+
std::string xLabel;
32+
std::string yLabel;
33+
std::string startStopLabel = "s";
34+
35+
// ASIO Stuff
36+
asio::io_service io_service;
37+
std::unique_ptr<asio::ip::tcp::acceptor> session_acceptor;
38+
std::unique_ptr<asio::ip::tcp::socket> open_socket;
39+
asio::streambuf stream_buffer;
40+
41+
// Async Thread
42+
std::thread receiveThread;
43+
bool keepReadThreadAlive = true;
44+
bool keepListenServerAlive = true;
45+
};

0 commit comments

Comments
 (0)