This repository has been archived by the owner on May 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
main.cpp
252 lines (214 loc) · 9.57 KB
/
main.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
/* Copyright (C) 2008 e_k ([email protected])
Ported to Blackberry Playbook by BGmot <[email protected]>, 2012
This application is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This application is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#include <QtCore>
#include <QtGui>
#include <QtGui/QApplication>
#include <fcntl.h>
#include "qtermwidget.h"
#include "mymenu.h"
#include "mysystemmenu.h"
#include "mymainwindow.h"
#include <bps/virtualkeyboard.h>
#include "myvk.h"
#include "mydevicetype.h"
// Headers for trackpad event handling
#include <screen/screen.h>
#include <bps/bps.h>
#include <bps/screen.h>
#include <bps/event.h>
#include <bps/navigator.h>
int masterFdG = -1; // will be used in parent process
int slaveFdG = -1; // will be used in child process
int pidG_ = -1; // child's PID
int nKBHeight = 0; // current Virtual Keyboard height
int nMaxKBHeight = 0; // Maximum Keyboard Height in current mode (Landscape/Portrait)
QTermWidget *console; // our 'main' widget, let's make it global so it is available in Menu widget
CMyMainWindow *mainWindow;
CMyMenu *Menu; // Menu with soft buttons
CMySystemMenu *SystemMenu; // System menu from 'swipe down' event
QFont font;
bool bOrientationJustChanged = false; // If we just changed screen orientation then do not handle hide/show keyboard event (skip one ScreenAvailableGeometry event)
bool bKBhidden = false; // True if VK is not shown
bool bCtrlFlag = false; // A flag that we use to interpret key - whether it should be treated as Ctrl+ or not
CMyVirtualKeyboard *virtualKeyboard; // Used only for Q10 when you press SYM key
bool bSymFlag = false; // A flag that we use to check whether my VK is shown
bool bShiftFlag = false; // SHIFT is active (was pressed), we need it for BB Passport that doesn't handle Shift properly
uDeviceType dtDevice = DONTCARE; // We need to differentiate between BB Passport and other devices
static QAbstractEventDispatcher::EventFilter mainEventFilter = 0; // To store old EventFilter (Application's)
void handleTrackpadEvent(bps_event_t *event) {
//handle trackpad events from BB Classic
int screen_val, button;
int displacement[2];
int sens; //sensitivity of Trackpad
sens=5;
screen_event_t screen_event = screen_event_get_event(event);
screen_get_event_property_iv(screen_event, SCREEN_PROPERTY_TYPE, &screen_val);
switch (screen_val) {
case SCREEN_EVENT_JOYSTICK:
if (0 != screen_get_event_property_iv(screen_event, SCREEN_PROPERTY_DISPLACEMENT, displacement)) {
;
}
if (0 != screen_get_event_property_iv(screen_event, SCREEN_PROPERTY_BUTTONS, &button)) {
;
}
if(button)
fprintf(stderr, "Trackpad pressed");
if (abs(displacement[0])>=sens || abs(displacement[1])>=sens){
if(abs(displacement[0]) > abs(displacement[1])) {
if(displacement[0] > 0){
char c[] = {0x1B,0x5B,'C'};
if(bCtrlFlag)
c[2] = 'c';
write(masterFdG, c, 3);
} else {
char c[] = {0x1B,0x5B,'D'};
if(bCtrlFlag)
c[2] = 'd';
write(masterFdG, c, 3);
}
} else {
if(displacement[1] > 0){
char c[] = {0x1B,0x5B,'B'};
write(masterFdG, c, 3);
} else {
char c[] = {0x1B,0x5B,'A'};
write(masterFdG, c, 3);
}
}
fprintf(stderr, "The x:'%4d', y:'%4d'\n", displacement[0],displacement[1]);
}
break;
default:
break;
}
}
bool myEventFilter(void *message) {
// here we should have all requested bps events (no Qt events here)
bps_event_t *event = (bps_event_t*)message;
handleTrackpadEvent(event); //Handle Trackpad events on Classic
if (event) {
int domain = bps_event_get_domain(event);
if (domain == virtualkeyboard_get_domain()) {
// We get here only if device has virtual keyboard (like Z10, Passport)
unsigned int ec = bps_event_get_code(event);
if (!bKBhidden && ec == VIRTUALKEYBOARD_EVENT_INFO){
int nNewKBHeight = virtualkeyboard_event_get_height(event);
virtualkeyboard_get_height(&nNewKBHeight);
if ( nNewKBHeight != nKBHeight)
nKBHeight = nNewKBHeight;
}
QRect r = QApplication::desktop()->screenGeometry(0);
if (ec == VIRTUALKEYBOARD_EVENT_VISIBLE){
bKBhidden = false;
nKBHeight = nMaxKBHeight;
virtualkeyboard_change_options(VIRTUALKEYBOARD_LAYOUT_DEFAULT, VIRTUALKEYBOARD_ENTER_DEFAULT);
}else if (ec == VIRTUALKEYBOARD_EVENT_HIDDEN){
bKBhidden = true;
nKBHeight = 0;
}
console->setGeometry(0, 0, r.width()+1, r.height()-nKBHeight-103);
Menu->SetGeometryPortrait(nKBHeight);
//font = QFont(QString("Courier New"), 6);
font.setPixelSize(mainWindow->nFontSize);
font.setStyle(QFont::StyleNormal);
font.setWeight(QFont::Normal);
console->setTerminalFont(font);
}
}
mainEventFilter(message); // Call replaced event filter so we deliever everything to Qt that runs in background
return false;
}
int main(int argc, char *argv[])
{
// Let's search for programs first in our own 'app' folder
QString oldPath = qgetenv("PATH");
QString oldLDPath = qgetenv("LD_LIBRARY_PATH");
char cwd[1024];
if (getcwd(cwd, sizeof(cwd)) == NULL){
fprintf(stderr, "Can't determine my current folder, terminating");
exit(-1);
}
QString newPath = QString(cwd) + "/app/native:" + oldPath;
qputenv("PATH", newPath.toAscii().data());
newPath = QString(cwd) + "/app/native/lib:/usr/lib/qt4/lib:" + oldLDPath;
qputenv("LD_LIBRARY_PATH", newPath.toAscii().data());
// Init file descriptors opening proper devices
masterFdG = ::open("/dev/ptyp1", O_RDWR);
slaveFdG = ::open("/dev/ttyp1", O_RDWR | O_NOCTTY);
// And here is where magic begins
pidG_ = fork();
if (pidG_ == 0) {
dup2(slaveFdG, STDIN_FILENO);
dup2(slaveFdG, STDOUT_FILENO);
dup2(slaveFdG, STDERR_FILENO);
// reset all signal handlers
struct sigaction act;
sigemptyset(&act.sa_mask);
act.sa_handler = SIG_DFL;
act.sa_flags = 0;
for (int sig = 1; sig < NSIG; sig++)
sigaction(sig, &act, 0L);
// All we want in child process is to have sh running
const char *arglist[] = {"/bin/sh", "-l", NULL};
execvp(arglist[0], (char**)arglist);
} else if (pidG_ == -1) {
// fork() failed
qDebug() << "fork() failed:";
pidG_ = 0;
return false;
}
// Drop garbage that is shown when you start the app
char cGarbage[72];
read(masterFdG, cGarbage, 72);
// needed for correct QT initialization
//qputenv("QT_QPA_FONTDIR", "/usr/fonts/font_repository/dejavu-ttf-2.17"); // needed for correct QT initialization
qputenv("QT_QPA_PLATFORM_PLUGIN_PATH", "/usr/lib/qt4/plugins/platforms");
//QCoreApplication::addLibraryPath("app/native/lib"); // blackberry plugin does not load without this line
QApplication app(argc, argv);
mainWindow = new CMyMainWindow();
QRect r = QApplication::desktop()->screenGeometry(0);
mainWindow->resize(r.width()+1, r.height()+1);
if (r.width() == 1440)
dtDevice = BB_PASSPORT;
console = new QTermWidget(1, mainWindow);
font = QFont(QString("Courier New"), 6);
font.setPixelSize(mainWindow->nFontSize);
font.setStyle(QFont::StyleNormal);
font.setWeight(QFont::Normal);
console->setTerminalFont(font);
// We start the app with shown keyboard (it does not make any harm for devices with physical keyboard)
virtualkeyboard_change_options(VIRTUALKEYBOARD_LAYOUT_DEFAULT, VIRTUALKEYBOARD_ENTER_DEFAULT);
console->setGeometry(0, 0, r.width()+1, r.height()-nKBHeight-103);
console->setScrollBarPosition(QTermWidget::ScrollBarRight);
// Our 'soft buttons' menu
Menu = new CMyMenu(mainWindow);
Menu->MenuInit();
// Our system menu
SystemMenu = new CMySystemMenu(mainWindow);
SystemMenu->MenuInit();
QObject::connect(console, SIGNAL(finished()), mainWindow, SLOT(close()));
mainWindow->show();
// Install event filter
mainEventFilter = QAbstractEventDispatcher::instance()->setEventFilter(myEventFilter);
// Show virtual keyboard (does nothing for devices with physical keyboard)
QEvent event(QEvent::RequestSoftwareInputPanel);
QApplication::sendEvent(console, &event);
virtualkeyboard_get_height(&nMaxKBHeight); // Init maximum VK height
virtualkeyboard_request_events(0); // To catch show/hide VK events
// Special keyboard that is shown only for Q10 when you press SYM
virtualKeyboard = new CMyVirtualKeyboard(mainWindow);
return app.exec();
}