-
Notifications
You must be signed in to change notification settings - Fork 2
/
VirtualTouchScreen.cpp
275 lines (247 loc) · 9.5 KB
/
VirtualTouchScreen.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#include <QDebug>
#include <QDesktopWidget>
#include <QBitmap>
#include <QMessageBox>
#include <QApplication>
#include <QAction>
#include <QSettings>
#include <QPainter>
#include "GestureThread.h"
#include "GestureAlgos.h"
#include "VirtualTouchScreen.h"
#include "TouchInputEmulator.h"
#include "ConfigDialog.h"
#define APPLICATION_NAME "Virtual Touch Screen"
#define FINGER_DEFAULT_ICON ":/icons/fingerprint.png"
VirtualTouchScreen::VirtualTouchScreen(QWidget *parent)
: QMainWindow(parent),
gestureThread(NULL),
offset(OFFSET_X,OFFSET_Y),
scaleFactor(SCALE_FACTOR_x100/100.0),
thumbPointer(new QWidget()),
config(NULL),
handSkeletonPoints_(Hand::POINTS),
touch_(new TouchInputEmulator()),
virtualScreenThreshold_(VIRTUAL_SCREEN_THRESHOLD_CM/100.0),
fingerIcon_(FINGER_DEFAULT_ICON),
fingerIconSize_(FINGER_ICON_SIZE),
hideThumb_(false)
{
loadSettings();
setWindowFlags(Qt::WindowStaysOnTopHint | Qt::FramelessWindowHint);
thumbPointer->setWindowFlags(Qt::WindowStaysOnTopHint | Qt::FramelessWindowHint | Qt::Tool);
loadFingerIcons(fingerIcon_, fingerIconSize_);
if (!hideThumb_) thumbPointer->show();
//get screen size
QDesktopWidget *desktop = QApplication::desktop();
QRect geom = desktop->availableGeometry(0);//first screen
//init gesture algorithms
gestureAlgos = GestureAlgos::instance();
gestureAlgos->setScreenSize(QSize(geom.width(), geom.height()));
gestureAlgos->setCorrectionFactors(scaleFactor, offset);
gestureAlgos->setMainWindow(this);
setupActions();
config = new ConfigDialog(this);
qDebug() << QThread::currentThreadId() << "starting gesture thread";
gestureThread = new GestureThread(this);
gestureThread->start();
if (TRUE != SetWindowPos((HWND)winId(), HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE)) {
qDebug() << "Cannot set topmost flag on main (index) window";
}
if (TRUE != SetWindowPos((HWND)thumbPointer->winId(), HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE)) {
qDebug() << "Cannot set topmost flag on secondary (thumb) window";
}
}
VirtualTouchScreen::~VirtualTouchScreen()
{
gestureThread->terminate();
gestureThread->wait();
delete gestureThread;
delete touch_;
delete thumbPointer;
delete config;
saveSettings();
}
void VirtualTouchScreen::setFingerPointer(QWidget *target,
const QString &iconPath,
int iconSize, bool rotate)
{
QPixmap pix(iconPath);
if ((iconSize != pix.size().width()) && (0 < iconSize)) {
pix = pix.scaled(iconSize, iconSize, Qt::KeepAspectRatio, Qt::SmoothTransformation);
}
if (rotate) {
QImage orig = pix.toImage();
pix = QPixmap::fromImage(rotate270(orig));
}
if (!pix.isNull()) {
QPalette p = palette();
p.setBrush(QPalette::Background, pix);
target->setPalette(p);
target->resize(pix.size());
target->setMask(pix.mask());
}
else {
qDebug() << "Cannot load cursor pixmap";
}
}
void VirtualTouchScreen::loadFingerIcons(const QString &iconPath, int iconSize)
{
setFingerPointer(this, iconPath, iconSize);
setFingerPointer(thumbPointer, iconPath, iconSize, true);
}
QImage VirtualTouchScreen::rotate270(const QImage &src)
{
QImage dst(src.height(), src.width(), src.format());
for (int y=0;y<src.height();++y) {
const uint *srcLine = reinterpret_cast< const uint * >(src.scanLine(y));
for (int x=0;x<src.width();++x) {
dst.setPixel(y, src.width()-x-1, srcLine[x]);
}
}
return dst;
}
void VirtualTouchScreen::setupActions()
{
QAction *menuAction = new QAction(this);
menuAction->setShortcut(QKeySequence("F2"));
connect(menuAction, SIGNAL(triggered(bool)), this, SLOT(showMenu()));
addAction(menuAction);
QAction *quitAction = new QAction(this);
quitAction->setShortcut(QKeySequence("Ctrl+q"));
connect(quitAction, SIGNAL(triggered(bool)), qApp, SLOT(quit()));
addAction(quitAction);
QAction *helpAction = new QAction(this);
helpAction->setShortcut(QKeySequence("F1"));
connect(helpAction, SIGNAL(triggered(bool)), this, SLOT(showHelp()));
addAction(helpAction);
}
void VirtualTouchScreen::showMenu()
{
if (NULL != config)
{
thumbPointer->hide();
config->show();
}
}
void VirtualTouchScreen::showHelp()
{
QMessageBox::about(NULL, APPLICATION_NAME,
" Virtual Touch Screen\n\n"
" author: Bogdan Cristea\n"
" e-mail: [email protected]\n\n"
" This application aims at improving user experience by providing a touch screen "
"experience with gestures. Currently touch screen enabled-applications have become "
"increasingly popular. However, classical PCs cannot benefit from this new type of "
"input. This application tries to fill this gap by providing touch screen experience "
"on any type of PC/device using a Creative Interactive Gesture Camera and perceptual "
"computing.\n"
" The virtual touch screen is represented by the sphere centered on the camera "
"and of radius equal with a virtual touch screen threshold. Once the fingers are "
"on the sphere or inside a touch event is generated. Since both the thumb and the "
"index fingers are tracked, in theory, all eight gestures supported in Windows 8 "
"are available:\n"
" - Press and Hold\n"
" - Tap\n"
" - Slide\n"
" - Swipe\n"
" - Pinch\n"
" - Stretch\n"
" - Swipe from Edge\n"
" - Turn\n"
"However, in practice, with a single camera, the distance estimation has fluctuations "
"that might have a negative impact on virtual touch screen accuracy.\n"
" The virtual touch screen threshold is configurable through the configuration "
"dialog. Finger icon (*.jpg and *.png image formats are supported) and "
"its size can also be changed. Both the thumb and the index fingers use the same "
"icon, but the thumb finger uses the icon rotated with 90 degrees counterclock wise. "
"If needed, the thumb finger icon can be hidden while the touch events from the thumb "
"are still received. The coordinates of the upper-left corner of the area covered "
"by the fingers can be changed, also the scaling factor used to make sure that "
"the entire screen area is covered.\n"
" The following shortcut keys are available:\n"
"- F1: shows this help\n"
"- F2: shows the configuration dialog\n"
"- Ctrl+q: quits the application\n"
"In order to receive the shortcut keys the application needs to have "
"the focus. In order to have the focus either click on "
"the application icon on the main toolbar or click the pointer.");
}
void VirtualTouchScreen::onMoveHand()
{
QMutexLocker lock(&skeletonPointMutex_);
QSize size = this->size();
move(handSkeletonPoints_[INDEX].x()-size.width()/2,
handSkeletonPoints_[INDEX].y()-size.height()/2);
if ((NULL != thumbPointer) && (!hideThumb_)) {
size = thumbPointer->size();
thumbPointer->move(handSkeletonPoints_[THUMB].x()-size.width()/2,
handSkeletonPoints_[THUMB].y()-size.height()/2);
}
}
void VirtualTouchScreen::onShowThumb(bool visible)
{
if (NULL != thumbPointer) {
thumbPointer->setVisible(visible);
}
}
void VirtualTouchScreen::onSwipe(BYTE code)
{
keybd_event(code & 0xff, 0, 0, 0);
keybd_event(code & 0xff, 0, KEYEVENTF_KEYUP, 0);
}
void VirtualTouchScreen::onTouchDown(const QPoint &ptThumb, const QPoint &ptIndex)
{
if ((NULL != touch_) && (EXIT_FAILURE == touch_->touchDown(ptThumb, ptIndex))) {
qDebug() << "Cannot send double touch down";
}
}
void VirtualTouchScreen::onTouchDown(const QPoint &ptIndex)
{
if ((NULL != touch_) && (EXIT_FAILURE == touch_->touchDown(ptIndex))) {
qDebug() << "Cannot send single touch down";
}
}
void VirtualTouchScreen::onTouchUp(const QPoint &ptThumb, const QPoint &ptIndex)
{
if ((NULL != touch_) && (EXIT_FAILURE == touch_->touchUp(ptThumb, ptIndex))) {
qDebug() << "Cannot send double touch up";
}
}
void VirtualTouchScreen::onTouchUp(const QPoint &ptIndex)
{
if ((NULL != touch_) && (EXIT_FAILURE == touch_->touchUp(ptIndex))) {
qDebug() << "Cannot send single touch up";
}
}
#define COMPANY_NAME "Bogdan Cristea"
#define KEY_VIRTUAL_SCREEN_THRESHOLD "VirtualScreenThreshold"
#define KEY_FINGER_ICON "FingerIcon"
#define KEY_FINGER_ICON_SIZE "FingerIconSize"
#define KEY_OFFSET_X "OffsetX"
#define KEY_OFFSET_Y "OffsetY"
#define SCALE_FACTOR "ScaleFactor"
#define KEY_HIDE_THUMB "HideThumb"
void VirtualTouchScreen::loadSettings()
{
QSettings settings(COMPANY_NAME, APPLICATION_NAME);
virtualScreenThreshold_ = settings.value(KEY_VIRTUAL_SCREEN_THRESHOLD,
VIRTUAL_SCREEN_THRESHOLD_CM/100.0).toDouble();
fingerIcon_ = settings.value(KEY_FINGER_ICON, FINGER_DEFAULT_ICON).toString();
fingerIconSize_ = settings.value(KEY_FINGER_ICON_SIZE, FINGER_ICON_SIZE).toInt();
hideThumb_ = settings.value(KEY_HIDE_THUMB, false).toBool();
offset.setX(settings.value(KEY_OFFSET_X, OFFSET_X).toInt());
offset.setY(settings.value(KEY_OFFSET_Y, OFFSET_Y).toInt());
scaleFactor = settings.value(SCALE_FACTOR, SCALE_FACTOR_x100/100.0).toDouble();
}
void VirtualTouchScreen::saveSettings()
{
QSettings settings(COMPANY_NAME, APPLICATION_NAME);
settings.setValue(KEY_VIRTUAL_SCREEN_THRESHOLD, virtualScreenThreshold_);
settings.setValue(KEY_FINGER_ICON, fingerIcon_);
settings.setValue(KEY_FINGER_ICON_SIZE, fingerIconSize_);
settings.setValue(KEY_HIDE_THUMB, hideThumb_);
settings.setValue(KEY_OFFSET_X, offset.x());
settings.setValue(KEY_OFFSET_Y, offset.y());
settings.setValue(SCALE_FACTOR, scaleFactor);
}