src/SimulatorSocketServer.cpp
author František Kučera <franta-hg@frantovo.cz>
Thu, 14 Mar 2019 21:37:43 +0100
branchv_0
changeset 6 f81d43b74209
parent 5 980a27d138f7
child 7 b355abd4b887
permissions -rw-r--r--
Option: Periodic updates (each 20 ms)
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
a27850958a67 emit button and motion events
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     1
/**
a27850958a67 emit button and motion events
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     2
 * Spacenav Simulator Qt
a27850958a67 emit button and motion events
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     3
 * Copyright © 2019 František Kučera (Frantovo.cz, GlobalCode.info)
a27850958a67 emit button and motion events
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     4
 *
a27850958a67 emit button and motion events
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     5
 * This program is free software: you can redistribute it and/or modify
a27850958a67 emit button and motion events
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     6
 * it under the terms of the GNU General Public License as published by
a27850958a67 emit button and motion events
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     7
 * the Free Software Foundation, either version 3 of the License, or
a27850958a67 emit button and motion events
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     8
 * (at your option) any later version.
a27850958a67 emit button and motion events
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     9
 *
a27850958a67 emit button and motion events
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    10
 * This program is distributed in the hope that it will be useful,
a27850958a67 emit button and motion events
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
a27850958a67 emit button and motion events
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
a27850958a67 emit button and motion events
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    13
 * GNU General Public License for more details.
a27850958a67 emit button and motion events
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    14
 *
a27850958a67 emit button and motion events
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License
a27850958a67 emit button and motion events
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    16
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
a27850958a67 emit button and motion events
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    17
 */
a27850958a67 emit button and motion events
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    18
3
42d64bd73232 connect slots, print events to stdout
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    19
#include <iostream>
42d64bd73232 connect slots, print events to stdout
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    20
2
a27850958a67 emit button and motion events
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    21
#include "SimulatorSocketServer.h"
3
42d64bd73232 connect slots, print events to stdout
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    22
#include "ButtonEvent.h"
42d64bd73232 connect slots, print events to stdout
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    23
#include "MotionEvent.h"
2
a27850958a67 emit button and motion events
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    24
5
980a27d138f7 publish events to all clients connected over unix domain socket
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    25
enum SocketEventType : int32_t {
980a27d138f7 publish events to all clients connected over unix domain socket
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    26
	Motion,
980a27d138f7 publish events to all clients connected over unix domain socket
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    27
	ButtonPress,
980a27d138f7 publish events to all clients connected over unix domain socket
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    28
	ButtonRelease
980a27d138f7 publish events to all clients connected over unix domain socket
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    29
};
980a27d138f7 publish events to all clients connected over unix domain socket
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    30
2
a27850958a67 emit button and motion events
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    31
SimulatorSocketServer::SimulatorSocketServer() {
5
980a27d138f7 publish events to all clients connected over unix domain socket
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    32
	QString path = "/tmp/spacenav-simulator.sock";
980a27d138f7 publish events to all clients connected over unix domain socket
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    33
980a27d138f7 publish events to all clients connected over unix domain socket
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    34
	bool listnening = server->listen(path);
980a27d138f7 publish events to all clients connected over unix domain socket
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    35
980a27d138f7 publish events to all clients connected over unix domain socket
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    36
	if (listnening) {
980a27d138f7 publish events to all clients connected over unix domain socket
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    37
		std::wcout << L"Listening on: " << path.toStdWString() << std::endl;
980a27d138f7 publish events to all clients connected over unix domain socket
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    38
980a27d138f7 publish events to all clients connected over unix domain socket
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    39
		connect(server, &QLocalServer::newConnection, [path, this]() {
980a27d138f7 publish events to all clients connected over unix domain socket
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    40
			std::wcout << L"New connection on: " << path.toStdWString() << std::endl;
980a27d138f7 publish events to all clients connected over unix domain socket
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    41
			connections.push_back(server->nextPendingConnection());
980a27d138f7 publish events to all clients connected over unix domain socket
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    42
		});
980a27d138f7 publish events to all clients connected over unix domain socket
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    43
980a27d138f7 publish events to all clients connected over unix domain socket
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    44
	} else {
980a27d138f7 publish events to all clients connected over unix domain socket
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    45
		std::wcout << L"Unable to listen on: " << path.toStdWString() << std::endl; // TODO: throw exception
980a27d138f7 publish events to all clients connected over unix domain socket
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    46
	}
2
a27850958a67 emit button and motion events
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    47
}
a27850958a67 emit button and motion events
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    48
a27850958a67 emit button and motion events
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    49
SimulatorSocketServer::SimulatorSocketServer(const SimulatorSocketServer& orig) {
a27850958a67 emit button and motion events
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    50
}
a27850958a67 emit button and motion events
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    51
a27850958a67 emit button and motion events
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    52
SimulatorSocketServer::~SimulatorSocketServer() {
a27850958a67 emit button and motion events
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    53
}
a27850958a67 emit button and motion events
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    54
3
42d64bd73232 connect slots, print events to stdout
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    55
void SimulatorSocketServer::publishButtonEvent(const ButtonEvent e) {
5
980a27d138f7 publish events to all clients connected over unix domain socket
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    56
	std::array<int32_t, 8> data = {0};
980a27d138f7 publish events to all clients connected over unix domain socket
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    57
	data[0] = e.pressed ? SocketEventType::ButtonPress : SocketEventType::ButtonRelease;
980a27d138f7 publish events to all clients connected over unix domain socket
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    58
	data[1] = e.number;
980a27d138f7 publish events to all clients connected over unix domain socket
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    59
	publish(data);
980a27d138f7 publish events to all clients connected over unix domain socket
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    60
980a27d138f7 publish events to all clients connected over unix domain socket
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    61
3
42d64bd73232 connect slots, print events to stdout
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    62
}
42d64bd73232 connect slots, print events to stdout
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    63
42d64bd73232 connect slots, print events to stdout
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    64
void SimulatorSocketServer::publishMotionEvent(const MotionEvent e) {
5
980a27d138f7 publish events to all clients connected over unix domain socket
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    65
	std::array<int32_t, 8> data = {0};
980a27d138f7 publish events to all clients connected over unix domain socket
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    66
	int i = 0;
980a27d138f7 publish events to all clients connected over unix domain socket
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    67
	data[i++] = SocketEventType::Motion;
980a27d138f7 publish events to all clients connected over unix domain socket
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    68
	data[i++] = e.x;
980a27d138f7 publish events to all clients connected over unix domain socket
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    69
	data[i++] = e.y;
980a27d138f7 publish events to all clients connected over unix domain socket
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    70
	data[i++] = e.z;
980a27d138f7 publish events to all clients connected over unix domain socket
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    71
	data[i++] = e.rx;
980a27d138f7 publish events to all clients connected over unix domain socket
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    72
	data[i++] = e.ry;
980a27d138f7 publish events to all clients connected over unix domain socket
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    73
	data[i++] = e.rz;
980a27d138f7 publish events to all clients connected over unix domain socket
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    74
	data[i++] = e.period;
980a27d138f7 publish events to all clients connected over unix domain socket
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    75
	publish(data);
980a27d138f7 publish events to all clients connected over unix domain socket
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    76
	// TODO: custom sensitivity for each connection
980a27d138f7 publish events to all clients connected over unix domain socket
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    77
}
3
42d64bd73232 connect slots, print events to stdout
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    78
5
980a27d138f7 publish events to all clients connected over unix domain socket
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    79
void SimulatorSocketServer::publish(const std::array<int32_t, 8> &data) {
980a27d138f7 publish events to all clients connected over unix domain socket
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    80
	for (QLocalSocket* socket : connections) {
980a27d138f7 publish events to all clients connected over unix domain socket
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    81
		if (socket->isValid()) {
980a27d138f7 publish events to all clients connected over unix domain socket
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    82
			socket->write((char*) &data, sizeof data);
980a27d138f7 publish events to all clients connected over unix domain socket
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    83
			socket->flush();
980a27d138f7 publish events to all clients connected over unix domain socket
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    84
		} else {
980a27d138f7 publish events to all clients connected over unix domain socket
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    85
			// TODO: remove invalid socket
980a27d138f7 publish events to all clients connected over unix domain socket
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    86
		}
980a27d138f7 publish events to all clients connected over unix domain socket
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    87
	}
3
42d64bd73232 connect slots, print events to stdout
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    88
}