author | František Kučera <franta-hg@frantovo.cz> |
Wed, 06 Mar 2019 21:08:34 +0100 | |
branch | v_0 |
changeset 2 | 21b0b2b0547e |
parent 1 | 2a3e9f07c128 |
child 3 | 6baa91ac3199 |
permissions | -rw-r--r-- |
1
2a3e9f07c128
receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
1 |
/** |
2a3e9f07c128
receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
2 |
* Spacenav Demo Qt |
2a3e9f07c128
receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
3 |
* Copyright © 2018 František Kučera (Frantovo.cz, GlobalCode.info) |
2a3e9f07c128
receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
4 |
* |
2a3e9f07c128
receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
5 |
* This program is free software: you can redistribute it and/or modify |
2a3e9f07c128
receive spacenav events (in separate thread) and print them on stderr
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 |
2a3e9f07c128
receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
7 |
* the Free Software Foundation, either version 3 of the License, or |
2a3e9f07c128
receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
8 |
* (at your option) any later version. |
2a3e9f07c128
receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
9 |
* |
2a3e9f07c128
receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
10 |
* This program is distributed in the hope that it will be useful, |
2a3e9f07c128
receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
2a3e9f07c128
receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
2a3e9f07c128
receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
13 |
* GNU General Public License for more details. |
2a3e9f07c128
receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
14 |
* |
2a3e9f07c128
receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
15 |
* You should have received a copy of the GNU General Public License |
2a3e9f07c128
receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
16 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
2a3e9f07c128
receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
17 |
*/ |
2a3e9f07c128
receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
18 |
#pragma once |
2a3e9f07c128
receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
19 |
|
2a3e9f07c128
receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
20 |
#include <iostream> |
2a3e9f07c128
receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
21 |
#include <QObject> |
2a3e9f07c128
receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
22 |
#include <QThread> |
2a3e9f07c128
receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
23 |
#include <spnav.h> |
2a3e9f07c128
receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
24 |
|
2a3e9f07c128
receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
25 |
Q_DECLARE_METATYPE(spnav_event) |
2a3e9f07c128
receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
26 |
|
2a3e9f07c128
receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
27 |
class SpacenavReceiver : public QThread { |
2a3e9f07c128
receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
28 |
|
2a3e9f07c128
receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
29 |
Q_OBJECT |
2a3e9f07c128
receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
30 |
public: |
2a3e9f07c128
receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
31 |
|
2a3e9f07c128
receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
32 |
|
2a3e9f07c128
receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
33 |
SpacenavReceiver() : QThread() { |
2a3e9f07c128
receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
34 |
} |
2a3e9f07c128
receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
35 |
|
2a3e9f07c128
receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
36 |
virtual ~SpacenavReceiver() { |
2
21b0b2b0547e
both buttons pressed → stop receiving events
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
37 |
std::wcout << L"calling spnav_close() in ~SpacenavReceiver()" << std::endl; |
1
2a3e9f07c128
receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
38 |
spnav_close(); |
2a3e9f07c128
receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
39 |
} |
2a3e9f07c128
receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
40 |
|
2a3e9f07c128
receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
41 |
void run() { |
2a3e9f07c128
receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
42 |
Display* display; |
2a3e9f07c128
receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
43 |
Window window; |
2a3e9f07c128
receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
44 |
unsigned long blackPixel; |
2a3e9f07c128
receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
45 |
|
2a3e9f07c128
receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
46 |
spnav_event event; |
2a3e9f07c128
receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
47 |
|
2a3e9f07c128
receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
48 |
if (!(display = XOpenDisplay(0))) { |
2a3e9f07c128
receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
49 |
std::wcout << L"unable to connect to the X server" << std::endl; |
2a3e9f07c128
receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
50 |
return; // TODO: throw exception |
2a3e9f07c128
receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
51 |
} |
2a3e9f07c128
receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
52 |
|
2a3e9f07c128
receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
53 |
blackPixel = BlackPixel(display, DefaultScreen(display)); |
2a3e9f07c128
receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
54 |
window = XCreateSimpleWindow(display, DefaultRootWindow(display), 0, 0, 1, 1, 0, blackPixel, blackPixel); |
2a3e9f07c128
receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
55 |
|
2a3e9f07c128
receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
56 |
if (spnav_x11_open(display, window) == -1) { |
2a3e9f07c128
receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
57 |
std::wcout << L"unable to connect to the space navigator daemon" << std::endl; |
2a3e9f07c128
receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
58 |
return; // TODO: throw exception |
2a3e9f07c128
receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
59 |
|
2a3e9f07c128
receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
60 |
/** |
2a3e9f07c128
receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
61 |
* TODO: try also unix socket |
2a3e9f07c128
receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
62 |
* |
2a3e9f07c128
receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
63 |
* if(spnav_open()==-1) { |
2a3e9f07c128
receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
64 |
* fprintf(stderr, "failed to connect to the space navigator daemon\n"); |
2a3e9f07c128
receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
65 |
* return 1; |
2a3e9f07c128
receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
66 |
* } |
2a3e9f07c128
receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
67 |
*/ |
2a3e9f07c128
receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
68 |
} |
2a3e9f07c128
receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
69 |
|
2a3e9f07c128
receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
70 |
// For stopping this thread by pressing both buttons: |
2
21b0b2b0547e
both buttons pressed → stop receiving events
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
71 |
bool pressed0 = false; |
21b0b2b0547e
both buttons pressed → stop receiving events
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
72 |
bool pressed1 = false; |
1
2a3e9f07c128
receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
73 |
|
2a3e9f07c128
receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
74 |
while (spnav_wait_event(&event)) { |
2a3e9f07c128
receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
75 |
if (event.type == SPNAV_EVENT_MOTION) { |
2a3e9f07c128
receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
76 |
std::wcout << L"motion event: t(" << event.motion.x << L", " << event.motion.y << L", " << event.motion.z << L") "; |
2a3e9f07c128
receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
77 |
std::wcout << L"r(" << event.motion.rx << L", " << event.motion.ry << L", " << event.motion.rz << L")" << std::endl; |
2a3e9f07c128
receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
78 |
|
2a3e9f07c128
receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
79 |
} else { /* SPNAV_EVENT_BUTTON */ |
2a3e9f07c128
receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
80 |
std::wcout << L"button " << (event.button.press ? "press" : "release") << L" event b(" << event.button.bnum << L")" << std::endl; |
2a3e9f07c128
receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
81 |
|
2
21b0b2b0547e
both buttons pressed → stop receiving events
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
82 |
if (event.button.bnum == 0) pressed0 = event.button.press; |
21b0b2b0547e
both buttons pressed → stop receiving events
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
83 |
if (event.button.bnum == 1) pressed1 = event.button.press; |
21b0b2b0547e
both buttons pressed → stop receiving events
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
84 |
if (pressed0 && pressed1) { |
21b0b2b0547e
both buttons pressed → stop receiving events
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
85 |
std::wcout << L"both buttons pressed → stop receiving events" << std::endl; |
21b0b2b0547e
both buttons pressed → stop receiving events
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
86 |
break; |
21b0b2b0547e
both buttons pressed → stop receiving events
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
87 |
} |
1
2a3e9f07c128
receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
88 |
} |
2a3e9f07c128
receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
89 |
emit spacenavEvent(event); |
2a3e9f07c128
receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
90 |
} |
2a3e9f07c128
receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
91 |
|
2a3e9f07c128
receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
92 |
} |
2a3e9f07c128
receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
93 |
|
2a3e9f07c128
receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
94 |
signals: |
2a3e9f07c128
receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
95 |
void spacenavEvent(spnav_event sev); |
2a3e9f07c128
receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
96 |
}; |