src/SimulatorSocketServer.cpp
branchv_0
changeset 5 980a27d138f7
parent 3 42d64bd73232
child 7 b355abd4b887
equal deleted inserted replaced
4:a874deb6a536 5:980a27d138f7
    20 
    20 
    21 #include "SimulatorSocketServer.h"
    21 #include "SimulatorSocketServer.h"
    22 #include "ButtonEvent.h"
    22 #include "ButtonEvent.h"
    23 #include "MotionEvent.h"
    23 #include "MotionEvent.h"
    24 
    24 
       
    25 enum SocketEventType : int32_t {
       
    26 	Motion,
       
    27 	ButtonPress,
       
    28 	ButtonRelease
       
    29 };
       
    30 
    25 SimulatorSocketServer::SimulatorSocketServer() {
    31 SimulatorSocketServer::SimulatorSocketServer() {
       
    32 	QString path = "/tmp/spacenav-simulator.sock";
       
    33 
       
    34 	bool listnening = server->listen(path);
       
    35 
       
    36 	if (listnening) {
       
    37 		std::wcout << L"Listening on: " << path.toStdWString() << std::endl;
       
    38 
       
    39 		connect(server, &QLocalServer::newConnection, [path, this]() {
       
    40 			std::wcout << L"New connection on: " << path.toStdWString() << std::endl;
       
    41 			connections.push_back(server->nextPendingConnection());
       
    42 		});
       
    43 
       
    44 	} else {
       
    45 		std::wcout << L"Unable to listen on: " << path.toStdWString() << std::endl; // TODO: throw exception
       
    46 	}
    26 }
    47 }
    27 
    48 
    28 SimulatorSocketServer::SimulatorSocketServer(const SimulatorSocketServer& orig) {
    49 SimulatorSocketServer::SimulatorSocketServer(const SimulatorSocketServer& orig) {
    29 }
    50 }
    30 
    51 
    31 SimulatorSocketServer::~SimulatorSocketServer() {
    52 SimulatorSocketServer::~SimulatorSocketServer() {
    32 }
    53 }
    33 
    54 
    34 void SimulatorSocketServer::publishButtonEvent(const ButtonEvent e) {
    55 void SimulatorSocketServer::publishButtonEvent(const ButtonEvent e) {
    35 	std::wcout << L"buttonEvent(" << e.number << L", " << (e.pressed ? L"pressed" : L"released") << L")" << std::endl;
    56 	std::array<int32_t, 8> data = {0};
       
    57 	data[0] = e.pressed ? SocketEventType::ButtonPress : SocketEventType::ButtonRelease;
       
    58 	data[1] = e.number;
       
    59 	publish(data);
       
    60 
       
    61 
    36 }
    62 }
    37 
    63 
    38 void SimulatorSocketServer::publishMotionEvent(const MotionEvent e) {
    64 void SimulatorSocketServer::publishMotionEvent(const MotionEvent e) {
    39 	std::wcout << L"motionEvent( m = ["
    65 	std::array<int32_t, 8> data = {0};
    40 			<< e.x << L", "
    66 	int i = 0;
    41 			<< e.y << L", "
    67 	data[i++] = SocketEventType::Motion;
    42 			<< e.z << L"], "
    68 	data[i++] = e.x;
    43 			<< L"r = ["
    69 	data[i++] = e.y;
    44 			<< e.rx << L", "
    70 	data[i++] = e.z;
    45 			<< e.ry << L", "
    71 	data[i++] = e.rx;
    46 			<< e.rz << L"], "
    72 	data[i++] = e.ry;
    47 			<< L"period = " << e.period
    73 	data[i++] = e.rz;
    48 			<< L")" << std::endl;
    74 	data[i++] = e.period;
       
    75 	publish(data);
       
    76 	// TODO: custom sensitivity for each connection
       
    77 }
    49 
    78 
       
    79 void SimulatorSocketServer::publish(const std::array<int32_t, 8> &data) {
       
    80 	for (QLocalSocket* socket : connections) {
       
    81 		if (socket->isValid()) {
       
    82 			socket->write((char*) &data, sizeof data);
       
    83 			socket->flush();
       
    84 		} else {
       
    85 			// TODO: remove invalid socket
       
    86 		}
       
    87 	}
    50 }
    88 }