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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
|
/****************************************************************************
**
** Copyright (C) 2011 - 2012 Research In Motion
**
** Contact: Research In Motion <blackberry-qt@qnx.com>
** Contact: Klarälvdalens Datakonsult AB <info@kdab.com>
**
** This file is part of the QtCore module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** GNU Lesser General Public License Usage
** This file may be used under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation and
** appearing in the file LICENSE.LGPL included in the packaging of this
** file. Please review the following information to ensure the GNU Lesser
** General Public License version 2.1 requirements will be met:
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU General
** Public License version 3.0 as published by the Free Software Foundation
** and appearing in the file LICENSE.GPL included in the packaging of this
** file. Please review the following information to ensure the GNU General
** Public License version 3.0 requirements will be met:
** http://www.gnu.org/copyleft/gpl.html.
**
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/
//#define QBBVIRTUALKEYBOARD_DEBUG
#include "qbbvirtualkeyboard.h"
#include <QDebug>
#include <QtGui/QApplication>
#include <QtGui/QPlatformScreen>
#include <QtGui/QPlatformWindow>
#include <errno.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <sys/iomsg.h>
#include <sys/pps.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
const char *QBBVirtualKeyboard::sPPSPath = "/pps/services/input/control?wait";
const size_t QBBVirtualKeyboard::sBufferSize = 2048;
static QBBVirtualKeyboard* s_instance = NULL;
// Huge hack for keyboard shadow (see QNX PR 88400). Should be removed ASAP.
#define KEYBOARD_SHADOW_HEIGHT 8
QBBVirtualKeyboard::QBBVirtualKeyboard() :
mEncoder(NULL),
mDecoder(NULL),
mBuffer(NULL),
mHeight(0),
mFd(-1),
mKeyboardMode(Default),
mVisible(false),
mLanguageId(QString::fromLatin1("en")),
mCountryId(QString::fromLatin1("US"))
{
connect();
}
QBBVirtualKeyboard::~QBBVirtualKeyboard()
{
close();
}
/* static */
QBBVirtualKeyboard& QBBVirtualKeyboard::instance()
{
if (!s_instance) {
s_instance = new QBBVirtualKeyboard();
s_instance->start();
}
return *s_instance;
}
/* static */
void QBBVirtualKeyboard::destroy()
{
if (s_instance) {
delete s_instance;
s_instance = 0;
}
}
void QBBVirtualKeyboard::close()
{
if (mFd) {
// any reads will fail after we close the fd, which is basically what we want.
::close(mFd);
}
// Wait for the thread to die (should be immediate), then continue the cleanup.
wait();
if (mFd) {
mFd = -1;
}
if (mDecoder)
{
pps_decoder_cleanup(mDecoder);
delete mDecoder;
mDecoder = NULL;
}
if (mEncoder)
{
pps_encoder_cleanup(mEncoder);
delete mEncoder;
mEncoder = NULL;
}
delete [] mBuffer;
mBuffer = NULL;
}
bool QBBVirtualKeyboard::connect()
{
close();
mEncoder = new pps_encoder_t;
mDecoder = new pps_decoder_t;
pps_encoder_initialize(mEncoder, false);
pps_decoder_initialize(mDecoder, NULL);
errno = 0;
mFd = ::open(sPPSPath, O_RDWR);
if (mFd == -1)
{
qCritical("QBBVirtualKeyboard: Unable to open \"%s\" for keyboard: %s (%d).",
sPPSPath, strerror(errno), errno);
close();
return false;
}
mBuffer = new char[sBufferSize];
if (!mBuffer) {
qCritical("QBBVirtualKeyboard: Unable to allocate buffer of %d bytes. Size is unavailable.", sBufferSize);
return false;
}
if (!queryPPSInfo())
return false;
start();
return true;
}
bool QBBVirtualKeyboard::queryPPSInfo()
{
// Request info, requires id to regenerate res message.
pps_encoder_add_string(mEncoder, "msg", "info");
pps_encoder_add_string(mEncoder, "id", "libWebView");
if (::write(mFd, pps_encoder_buffer(mEncoder), pps_encoder_length(mEncoder)) == -1) {
close();
return false;
}
pps_encoder_reset(mEncoder);
return true;
}
void QBBVirtualKeyboard::notifyClientActiveStateChange(bool active)
{
if (!active)
hideKeyboard();
}
void QBBVirtualKeyboard::run()
{
ppsDataReady();
}
void QBBVirtualKeyboard::ppsDataReady()
{
forever {
ssize_t nread = read(mFd, mBuffer, sBufferSize - 1);
#ifdef QBBVIRTUALKEYBOARD_DEBUG
qDebug() << "QBB: keyboardMessage size: " << nread;
#endif
if (nread < 0)
break;
// nread is the real space necessary, not the amount read.
if (static_cast<size_t>(nread) > sBufferSize - 1) {
qCritical("QBBVirtualKeyboard: Keyboard buffer size too short; need %u.", nread + 1);
break;
}
mBuffer[nread] = 0;
pps_decoder_parse_pps_str(mDecoder, mBuffer);
pps_decoder_push(mDecoder, NULL);
#ifdef QBBVIRTUALKEYBOARD_DEBUG
pps_decoder_dump_tree(mDecoder, stderr);
#endif
const char* value;
if (pps_decoder_get_string(mDecoder, "error", &value) == PPS_DECODER_OK) {
qCritical("QBBVirtualKeyboard: Keyboard PPS decoder error: %s", value ? value : "[null]");
continue;
}
if (pps_decoder_get_string(mDecoder, "msg", &value) == PPS_DECODER_OK) {
if (strcmp(value, "show") == 0) {
mVisible = true;
handleKeyboardStateChangeMessage(true);
} else if (strcmp(value, "hide") == 0) {
mVisible = false;
handleKeyboardStateChangeMessage(false);
} else if (strcmp(value, "info") == 0)
handleKeyboardInfoMessage();
else if (strcmp(value, "connect") == 0) { }
else
qCritical("QBBVirtualKeyboard: Unexpected keyboard PPS msg value: %s", value ? value : "[null]");
} else if (pps_decoder_get_string(mDecoder, "res", &value) == PPS_DECODER_OK) {
if (strcmp(value, "info") == 0)
handleKeyboardInfoMessage();
else
qCritical("QBBVirtualKeyboard: Unexpected keyboard PPS res value: %s", value ? value : "[null]");
} else
qCritical("QBBVirtualKeyboard: Unexpected keyboard PPS message type");
}
#ifdef QBBVIRTUALKEYBOARD_DEBUG
qDebug() << "QBB: exiting keyboard thread";
#endif
if (mDecoder)
pps_decoder_cleanup(mDecoder);
}
void QBBVirtualKeyboard::handleKeyboardInfoMessage()
{
int newHeight = 0;
const char* value;
if (pps_decoder_push(mDecoder, "dat") != PPS_DECODER_OK) {
qCritical("QBBVirtualKeyboard: Keyboard PPS dat object not found");
return;
}
if (pps_decoder_get_int(mDecoder, "size", &newHeight) != PPS_DECODER_OK) {
qCritical("QBBVirtualKeyboard: Keyboard PPS size field not found");
return;
}
if (pps_decoder_push(mDecoder, "locale") != PPS_DECODER_OK) {
qCritical("QBBVirtualKeyboard: Keyboard PPS locale object not found");
return;
}
if (pps_decoder_get_string(mDecoder, "languageId", &value) != PPS_DECODER_OK) {
qCritical("QBBVirtualKeyboard: Keyboard PPS languageId field not found");
return;
}
mLanguageId = QString::fromLatin1(value);
if (pps_decoder_get_string(mDecoder, "countryId", &value) != PPS_DECODER_OK) {
qCritical("QBBVirtualKeyboard: Keyboard PPS size countryId not found");
return;
}
mCountryId = QString::fromLatin1(value);
// HUGE hack, should be removed ASAP.
newHeight -= KEYBOARD_SHADOW_HEIGHT; // We want to ignore the 8 pixel shadow above the keyboard. (PR 88400)
if (newHeight != mHeight) {
mHeight = newHeight;
handleKeyboardStateChangeMessage(true);
}
#ifdef QBBVIRTUALKEYBOARD_DEBUG
qDebug() << "QBB: handleKeyboardInfoMessage size=" << mHeight << "languageId=" << mLanguageId << " countryId=" << mCountryId;
#endif
}
void QBBVirtualKeyboard::handleKeyboardStateChangeMessage(bool visible)
{
#ifdef QBBVIRTUALKEYBOARD_DEBUG
qDebug() << "QBB: handleKeyboardStateChangeMessage " << visible;
#endif
// TODO: What screen index should be used? I assume 0 here because it works, and
// we do it for handleScreenGeometryChange elsewhere but since we have support
// for more than one screen, that's not going to always work.
QWindowSystemInterface::handleScreenAvailableGeometryChange(0);
}
bool QBBVirtualKeyboard::showKeyboard()
{
#ifdef QBBVIRTUALKEYBOARD_DEBUG
qDebug() << "QBB: showKeyboard()";
#endif
// Try to connect.
if (mFd == -1 && !connect())
return false;
// NOTE: This must be done everytime the keyboard is shown even if there is no change because
// hiding the keyboard wipes the setting.
applyKeyboardModeOptions();
pps_encoder_reset(mEncoder);
// Send the show message.
pps_encoder_add_string(mEncoder, "msg", "show");
if (::write(mFd, pps_encoder_buffer(mEncoder), pps_encoder_length(mEncoder)) == -1) {
close();
return false;
}
pps_encoder_reset(mEncoder);
// Return true if no error occurs. Sizing response will be triggered when confirmation of
// the change arrives.
return true;
}
bool QBBVirtualKeyboard::hideKeyboard()
{
#ifdef QBBVIRTUALKEYBOARD_DEBUG
qDebug() << "QBB: hideKeyboard()";
#endif
if (mFd == -1 && !connect())
return false;
pps_encoder_add_string(mEncoder, "msg", "hide");
if (::write(mFd, pps_encoder_buffer(mEncoder), pps_encoder_length(mEncoder)) == -1) {
close();
//Try again.
if (connect()) {
if (::write(mFd, pps_encoder_buffer(mEncoder), pps_encoder_length(mEncoder)) == -1) {
close();
return false;
}
}
else
return false;
}
pps_encoder_reset(mEncoder);
// Return true if no error occurs. Sizing response will be triggered when confirmation of
// the change arrives.
return true;
}
void QBBVirtualKeyboard::setKeyboardMode(KeyboardMode mode)
{
mKeyboardMode = mode;
}
void QBBVirtualKeyboard::applyKeyboardModeOptions()
{
// Try to connect.
if (mFd == -1 && !connect())
return;
// Send the options message.
pps_encoder_add_string(mEncoder, "msg", "options");
pps_encoder_start_object(mEncoder, "dat");
switch (mKeyboardMode) {
case Url:
addUrlModeOptions();
break;
case Email:
addEmailModeOptions();
break;
case Web:
addWebModeOptions();
break;
case NumPunc:
addNumPuncModeOptions();
break;
case Symbol:
addSymbolModeOptions();
break;
case Phone:
addPhoneModeOptions();
break;
case Pin:
addPinModeOptions();
break;
case Default:
default:
addDefaultModeOptions();
break;
}
pps_encoder_end_object(mEncoder);
if (::write(mFd, pps_encoder_buffer(mEncoder), pps_encoder_length(mEncoder)) == -1) {
close();
}
pps_encoder_reset(mEncoder);
}
void QBBVirtualKeyboard::addDefaultModeOptions()
{
pps_encoder_add_string(mEncoder, "enter", "enter.default");
pps_encoder_add_string(mEncoder, "type", "default");
}
void QBBVirtualKeyboard::addUrlModeOptions()
{
pps_encoder_add_string(mEncoder, "enter", "enter.default");
pps_encoder_add_string(mEncoder, "type", "url");
}
void QBBVirtualKeyboard::addEmailModeOptions()
{
pps_encoder_add_string(mEncoder, "enter", "enter.default");
pps_encoder_add_string(mEncoder, "type", "email");
}
void QBBVirtualKeyboard::addWebModeOptions()
{
pps_encoder_add_string(mEncoder, "enter", "enter.default");
pps_encoder_add_string(mEncoder, "type", "web");
}
void QBBVirtualKeyboard::addNumPuncModeOptions()
{
pps_encoder_add_string(mEncoder, "enter", "enter.default");
pps_encoder_add_string(mEncoder, "type", "numPunc");
}
void QBBVirtualKeyboard::addPhoneModeOptions()
{
pps_encoder_add_string(mEncoder, "enter", "enter.default");
pps_encoder_add_string(mEncoder, "type", "phone");
}
void QBBVirtualKeyboard::addPinModeOptions()
{
pps_encoder_add_string(mEncoder, "enter", "enter.default");
pps_encoder_add_string(mEncoder, "type", "pin");
}
void QBBVirtualKeyboard::addSymbolModeOptions()
{
pps_encoder_add_string(mEncoder, "enter", "enter.default");
pps_encoder_add_string(mEncoder, "type", "symbol");
}
|