Feature/integrated touchserver (#1015)

* WINDOWS: Touch server integrated into module

The touch server functionality has been integrated into the touch module
which is enabled by-default when the touch module is used on a windows
build.
The touch-hook checks for a connected digitizer (pen or touchscreen, but
I haven't tested pen)

This commit should also fix two potential low-risk race conditions in
the tuioear.cpp file.

* Added comment regarding which window we use

* Added copyright notice on the win32_touch files

Also changed from #pragma to #ifndef

* Fixes based on review

- Added anonymous namespace
- Put win32hook in openspace namespace
- Fixed indentations and linebreaks
- Fixed an issue regarding global state deinitialization
This commit is contained in:
Mikael Pettersson
2019-12-02 14:09:15 +01:00
committed by GitHub
parent c469dd78a0
commit a33057d830
9 changed files with 253 additions and 11 deletions

View File

@@ -29,6 +29,7 @@ set(HEADER_FILES
${CMAKE_CURRENT_SOURCE_DIR}/include/tuioear.h
${CMAKE_CURRENT_SOURCE_DIR}/include/touchinteraction.h
${CMAKE_CURRENT_SOURCE_DIR}/include/touchmarker.h
${CMAKE_CURRENT_SOURCE_DIR}/include/win32_touch.h
)
source_group("Header Files" FILES ${HEADER_FILES})
@@ -37,6 +38,7 @@ set(SOURCE_FILES
${CMAKE_CURRENT_SOURCE_DIR}/src/tuioear.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/touchinteraction.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/touchmarker.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/win32_touch.cpp
)
source_group("Source Files" FILES ${SOURCE_FILES})

View File

@@ -61,9 +61,7 @@ class TuioEar : public TUIO::TuioListener {
public:
TuioEar();
~TuioEar() {
_tuioClient->disconnect();
delete _tuioClient;
delete _oscReceiver;
_tuioClient.disconnect();
}
/**
@@ -108,8 +106,7 @@ class TuioEar : public TUIO::TuioListener {
TUIO::TuioCursor _tapCo = TUIO::TuioCursor(-1, -1, -1.0f, -1.0f);
std::mutex _mx;
TUIO::TuioClient *_tuioClient;
TUIO::OscReceiver *_oscReceiver;
TUIO::TuioClient _tuioClient;
std::vector<TUIO::TuioCursor> _list;

View File

@@ -0,0 +1,41 @@
/*****************************************************************************************
* *
* OpenSpace *
* *
* Copyright (c) 2014-2019 *
* *
* Permission is hereby granted, free of charge, to any person obtaining a copy of this *
* software and associated documentation files (the "Software"), to deal in the Software *
* without restriction, including without limitation the rights to use, copy, modify, *
* merge, publish, distribute, sublicense, and/or sell copies of the Software, and to *
* permit persons to whom the Software is furnished to do so, subject to the following *
* conditions: *
* *
* The above copyright notice and this permission notice shall be included in all copies *
* or substantial portions of the Software. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, *
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A *
* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT *
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF *
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE *
* OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
****************************************************************************************/
#ifndef __OPENSPACE_MODULE_TOUCH___WIN32_TOUCH___H__
#define __OPENSPACE_MODULE_TOUCH___WIN32_TOUCH___H__
#ifdef WIN32
namespace openspace {
class Win32TouchHook {
public:
Win32TouchHook(void* nativeWindow);
~Win32TouchHook();
};
} // namespace openspace
#endif // WIN32
#endif // __OPENSPACE_MODULE_TOUCH___WIN32_TOUCH___H__

View File

@@ -115,10 +115,12 @@ void TuioEar::removeTuioBlob(TuioBlob*) { }
void TuioEar::refresh(TuioTime) { } // about every 15ms
std::vector<TuioCursor> TuioEar::getInput() {
std::lock_guard lock(_mx);
return _list;
}
bool TuioEar::tap() {
std::lock_guard lock(_mx);
if (_tap) {
_tap = false;
return !_tap;
@@ -129,7 +131,7 @@ bool TuioEar::tap() {
}
TuioCursor TuioEar::getTap() {
std::lock_guard<std::mutex> lock(_mx);
std::lock_guard lock(_mx);
return _tapCo;
}
@@ -157,9 +159,9 @@ void TuioEar::clearInput() {
}
// Standard UDP IP connection to port 3333
TuioEar::TuioEar() {
_oscReceiver = new UdpReceiver(3333);
_tuioClient = new TuioClient(_oscReceiver);
_tuioClient->addTuioListener(this);
_tuioClient->connect();
TuioEar::TuioEar()
: _tuioClient(3333)
{
_tuioClient.addTuioListener(this);
_tuioClient.connect();
}

View File

@@ -0,0 +1,166 @@
/*****************************************************************************************
* *
* OpenSpace *
* *
* Copyright (c) 2014-2019 *
* *
* Permission is hereby granted, free of charge, to any person obtaining a copy of this *
* software and associated documentation files (the "Software"), to deal in the Software *
* without restriction, including without limitation the rights to use, copy, modify, *
* merge, publish, distribute, sublicense, and/or sell copies of the Software, and to *
* permit persons to whom the Software is furnished to do so, subject to the following *
* conditions: *
* *
* The above copyright notice and this permission notice shall be included in all copies *
* or substantial portions of the Software. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, *
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A *
* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT *
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF *
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE *
* OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
****************************************************************************************/
#ifdef WIN32
#include <modules/touch/include/win32_touch.h>
#include <openspace/engine/openspaceengine.h>
#include <openspace/engine/windowdelegate.h>
#include <ghoul/logging/logmanager.h>
#include <TUIO/TuioServer.h>
#include <tchar.h>
#include <tpcshrd.h>
namespace {
constexpr const char* _loggerCat = "win32_touch";
HHOOK gTouchHook{ nullptr };
bool gStarted{ false };
TUIO::TuioServer* gTuioServer{ nullptr };
std::unordered_map<UINT, TUIO::TuioCursor*> gCursorMap;
}
namespace openspace {
// This hook will only work for Win7+ Digitizers.
// - Once GLFW has native touch support, we can remove this windows-specific code
LRESULT CALLBACK HookCallback(int nCode, WPARAM wParam, LPARAM lParam) {
if (nCode < 0) {
return CallNextHookEx(0, nCode, wParam, lParam);
}
if (nCode == HC_ACTION) {
LPMSG pStruct = reinterpret_cast<LPMSG>(lParam);
const UINT message = pStruct->message;
switch (message) {
case WM_POINTERDOWN:
case WM_POINTERUPDATE:
case WM_POINTERUP:
{
POINTER_INFO pointerInfo = {};
if (GetPointerInfo(GET_POINTERID_WPARAM(pStruct->wParam), &pointerInfo)) {
RECT rect;
GetClientRect(pStruct->hwnd, reinterpret_cast<LPRECT>(&rect));
POINT p = pointerInfo.ptPixelLocation;
// native touch to screen conversion
ScreenToClient(pStruct->hwnd, reinterpret_cast<LPPOINT>(&p));
float xPos = (float)p.x / (float)(rect.right - rect.left);
float yPos = (float)p.y / (float)(rect.bottom - rect.top);
if (pointerInfo.pointerFlags & POINTER_FLAG_DOWN) {
// Handle new touchpoint
gTuioServer->initFrame(TUIO::TuioTime::getSessionTime());
gCursorMap[pointerInfo.pointerId] = gTuioServer->addTuioCursor(xPos, yPos);
gTuioServer->commitFrame();
}
else if (pointerInfo.pointerFlags & POINTER_FLAG_UPDATE) {
// Handle update of touchpoint
TUIO::TuioTime frameTime = TUIO::TuioTime::getSessionTime();
if (gCursorMap[pointerInfo.pointerId]->getTuioTime() == frameTime) {
break;
}
gTuioServer->initFrame(frameTime);
gTuioServer->updateTuioCursor(gCursorMap[pointerInfo.pointerId], xPos, yPos);
gTuioServer->commitFrame();
}
else if (pointerInfo.pointerFlags & POINTER_FLAG_UP) {
// Handle removed touchpoint
gTuioServer->initFrame(TUIO::TuioTime::getSessionTime());
gTuioServer->removeTuioCursor(gCursorMap[pointerInfo.pointerId]);
gTuioServer->commitFrame();
gCursorMap.erase(pointerInfo.pointerId);
}
}
break;
}
}
}
// Pass the hook along!
return CallNextHookEx(0, nCode, wParam, lParam);
}
Win32TouchHook::Win32TouchHook(void* nativeWindow)
{
HWND hWnd = reinterpret_cast<HWND>(nativeWindow);
if (hWnd == nullptr) {
LINFO("No windowhandle available for touch input.");
return;
}
// Test for touch:
int value = GetSystemMetrics(SM_DIGITIZER);
if ((value & NID_READY) == 0) {
// Don't bother setting up touch hooks?
return;
}
// stack ready, drivers installed and digitizer is ready for input
if (value & NID_MULTI_INPUT) {
// Digitizer is multitouch
LINFO("Found Multitouch input digitizer!");
}
if (value & NID_INTEGRATED_TOUCH) {
// Integrated touch
}
// This should be needed, but we seem to receive messages even without it,
// probably a Win7+ behaviour
// Also - RegisterTouchWindow enables Windows gestures, which we don't want
// since they produce visual feedback for "press-and-tap" etc.
// RegisterTouchWindow(hWnd, TWF_FINETOUCH | TWF_WANTPALM);
// TODO: Would be nice to find out if the gesture "press-and-tap" can be disabled
// basically we don't really care for windows gestures for now...
// this disables press and hold (right-click) gesture
const DWORD dwHwndTabletProperty = TABLET_DISABLE_PRESSANDHOLD;
ATOM atom = ::GlobalAddAtom(MICROSOFT_TABLETPENSERVICE_PROPERTY);
::SetProp(hWnd, MICROSOFT_TABLETPENSERVICE_PROPERTY, reinterpret_cast<HANDLE>(dwHwndTabletProperty));
::GlobalDeleteAtom(atom);
if (!gStarted) {
gStarted = true;
gTuioServer = new TUIO::TuioServer("localhost", 3333);
TUIO::TuioTime::initSession();
gTouchHook = SetWindowsHookExW(WH_GETMESSAGE, HookCallback, GetModuleHandleW(NULL), GetCurrentThreadId());
if (!gTouchHook) {
LINFO(fmt::format("Failed to setup WindowsHook for touch input redirection"));
delete gTuioServer;
gStarted = false;
}
}
}
Win32TouchHook::~Win32TouchHook() {
if (gStarted) {
UnhookWindowsHookEx(gTouchHook);
delete gTuioServer;
}
}
} // namespace openspace
#endif // WIN32

View File

@@ -23,6 +23,7 @@
****************************************************************************************/
#include <modules/touch/touchmodule.h>
#include <modules/touch/include/win32_touch.h>
#include <modules/webgui/webguimodule.h>
#include <openspace/engine/globals.h>
@@ -156,6 +157,14 @@ TouchModule::TouchModule()
global::callback::initializeGL.push_back([&]() {
LDEBUGC("TouchModule", "Initializing TouchMarker OpenGL");
_markers.initialize();
#ifdef WIN32
// We currently only support one window of touch input internally
// so here we grab the first window-handle and use it.
void* nativeWindowHandle = global::windowDelegate.getNativeWindowHandle(0);
if (nativeWindowHandle) {
_win32TouchHook.reset(new Win32TouchHook(nativeWindowHandle));
}
#endif //WIN32
});
global::callback::deinitializeGL.push_back([&]() {
@@ -190,4 +199,8 @@ TouchModule::TouchModule()
}
TouchModule::~TouchModule() {
//intentionally left empty
}
} // namespace openspace

View File

@@ -31,11 +31,15 @@
namespace openspace {
#ifdef WIN32
class Win32TouchHook;
#endif //WIN32
class TouchModule : public OpenSpaceModule {
using Point = std::pair<int, TUIO::TuioPoint>;
public:
TouchModule();
~TouchModule();
private:
/**
@@ -54,6 +58,9 @@ namespace openspace {
// contains an id and the TuioPoint that was processed last frame
std::vector<Point> _lastProcessed;
glm::ivec2 _webPositionCallback = glm::ivec2(0,0);
#ifdef WIN32
std::unique_ptr<Win32TouchHook> _win32TouchHook;
#endif //WIN32
};
} // namespace openspace