author | František Kučera <franta-hg@frantovo.cz> |
Thu, 07 Mar 2019 14:55:56 +0100 | |
branch | v_0 |
changeset 4 | 1383ad1c4f57 |
parent 3 | src/SpacenavReceiver.h@6baa91ac3199 |
child 6 | 49560660d230 |
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 |
4 | 3 |
* Copyright © 2019 František Kučera (Frantovo.cz, GlobalCode.info) |
1
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 |
|
2a3e9f07c128
receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
19 |
#include <spnav.h> |
4 | 20 |
#include "SpacenavWrapper.h" |
1
2a3e9f07c128
receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
21 |
|
4 | 22 |
SpacenavWrapper::SpacenavWrapper() { |
23 |
// TODO: remove logging |
|
24 |
if (spnav_open() == 0) { |
|
25 |
std::wcout << L"connected through: AF_UNIX socket" << std::endl; |
|
26 |
} else { |
|
1
2a3e9f07c128
receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
27 |
|
4 | 28 |
Display* display; |
29 |
Window window; |
|
30 |
unsigned long blackPixel; |
|
3
6baa91ac3199
support both: AF_UNIX and X11 connections
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
31 |
|
1
2a3e9f07c128
receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
32 |
|
4 | 33 |
if (!(display = XOpenDisplay(0))) { |
34 |
std::wcout << L"unable to connect to the X server" << std::endl; |
|
35 |
return; // TODO: throw exception |
|
1
2a3e9f07c128
receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
36 |
} |
2a3e9f07c128
receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
37 |
|
4 | 38 |
blackPixel = BlackPixel(display, DefaultScreen(display)); |
39 |
window = XCreateSimpleWindow(display, DefaultRootWindow(display), 0, 0, 1, 1, 0, blackPixel, blackPixel); |
|
1
2a3e9f07c128
receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
40 |
|
4 | 41 |
if (spnav_x11_open(display, window) == 0) { |
42 |
std::wcout << L"connected through: X11" << std::endl; |
|
43 |
} else { |
|
44 |
std::wcout << L"unable to connect to the space navigator daemon" << std::endl; |
|
45 |
return; // TODO: throw exception |
|
46 |
} |
|
47 |
} |
|
48 |
} |
|
1
2a3e9f07c128
receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
49 |
|
4 | 50 |
SpacenavWrapper::~SpacenavWrapper() { |
51 |
// TODO: remove logging |
|
52 |
std::wcout << L"calling spnav_close() in ~SpacenavReceiver()" << std::endl; |
|
53 |
spnav_close(); |
|
54 |
} |
|
55 |
||
56 |
SpacenavWrapper::Event SpacenavWrapper::waitEvent() { |
|
57 |
SpacenavWrapper::Event e; |
|
58 |
spnav_event event; |
|
1
2a3e9f07c128
receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
59 |
|
4 | 60 |
if (spnav_wait_event(&event)) { |
61 |
||
62 |
// TODO: copy fields |
|
63 |
||
64 |
||
65 |
// TODO: remove logging |
|
66 |
if (event.type == SPNAV_EVENT_MOTION) { |
|
67 |
std::wcout << L"xxx motion event: t(" << event.motion.x << L", " << event.motion.y << L", " << event.motion.z << L") "; |
|
68 |
std::wcout << L"r(" << event.motion.rx << L", " << event.motion.ry << L", " << event.motion.rz << L")" << std::endl; |
|
69 |
} else { /* SPNAV_EVENT_BUTTON */ |
|
70 |
std::wcout << L"xxx button " << (event.button.press ? "press" : "release") << L" event b(" << event.button.bnum << L")" << 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
|
71 |
} |
2a3e9f07c128
receive spacenav events (in separate thread) and print them on stderr
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
72 |
|
4 | 73 |
|
74 |
return e; |
|
75 |
} else { |
|
76 |
// TODO: throw exception |
|
77 |
return e; |
|
1
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 |
|
4 | 80 |
} |