src/SimulatorSocketServer.cpp
author František Kučera <franta-hg@frantovo.cz>
Fri, 15 Mar 2019 00:14:25 +0100
branchv_0
changeset 7 b355abd4b887
parent 5 980a27d138f7
child 10 da93f3667a52
permissions -rw-r--r--
symlink hint
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;
7
b355abd4b887 symlink hint
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    38
		std::wcout << L"To allow client connections do:" << std::endl;
b355abd4b887 symlink hint
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    39
		std::wcout << L"ln -s " << path.toStdWString() << " /var/run/spnav.sock" << std::endl;
5
980a27d138f7 publish events to all clients connected over unix domain socket
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    40
980a27d138f7 publish events to all clients connected over unix domain socket
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    41
		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
    42
			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
    43
			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
    44
		});
980a27d138f7 publish events to all clients connected over unix domain socket
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    45
980a27d138f7 publish events to all clients connected over unix domain socket
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    46
	} else {
980a27d138f7 publish events to all clients connected over unix domain socket
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    47
		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
    48
	}
2
a27850958a67 emit button and motion events
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    49
}
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
SimulatorSocketServer::SimulatorSocketServer(const SimulatorSocketServer& orig) {
a27850958a67 emit button and motion events
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    52
}
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
SimulatorSocketServer::~SimulatorSocketServer() {
a27850958a67 emit button and motion events
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    55
}
a27850958a67 emit button and motion events
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    56
3
42d64bd73232 connect slots, print events to stdout
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    57
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
    58
	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
    59
	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
    60
	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
    61
	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
    62
980a27d138f7 publish events to all clients connected over unix domain socket
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    63
3
42d64bd73232 connect slots, print events to stdout
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    64
}
42d64bd73232 connect slots, print events to stdout
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    65
42d64bd73232 connect slots, print events to stdout
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    66
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
    67
	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
    68
	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
    69
	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
    70
	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
    71
	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
    72
	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
    73
	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
    74
	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
    75
	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
    76
	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
    77
	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
    78
	// 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
    79
}
3
42d64bd73232 connect slots, print events to stdout
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    80
5
980a27d138f7 publish events to all clients connected over unix domain socket
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    81
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
    82
	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
    83
		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
    84
			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
    85
			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
    86
		} else {
980a27d138f7 publish events to all clients connected over unix domain socket
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    87
			// 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
    88
		}
980a27d138f7 publish events to all clients connected over unix domain socket
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    89
	}
3
42d64bd73232 connect slots, print events to stdout
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    90
}