author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Fri, 19 Feb 2010 23:40:16 +0200 | |
branch | RCL_3 |
changeset 4 | 3b1da2848fc7 |
parent 0 | 1918ee327afb |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3 |
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
0 | 4 |
** All rights reserved. |
5 |
** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 |
** |
|
7 |
** This file is part of the QtGui module of the Qt Toolkit. |
|
8 |
** |
|
9 |
** $QT_BEGIN_LICENSE:LGPL$ |
|
10 |
** No Commercial Usage |
|
11 |
** This file contains pre-release code and may not be distributed. |
|
12 |
** You may use this file in accordance with the terms and conditions |
|
13 |
** contained in the Technology Preview License Agreement accompanying |
|
14 |
** this package. |
|
15 |
** |
|
16 |
** GNU Lesser General Public License Usage |
|
17 |
** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 |
** General Public License version 2.1 as published by the Free Software |
|
19 |
** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 |
** packaging of this file. Please review the following information to |
|
21 |
** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 |
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 |
** |
|
24 |
** In addition, as a special exception, Nokia gives you certain additional |
|
25 |
** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 |
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 |
** |
|
28 |
** If you have questions regarding the use of this file, please contact |
|
29 |
** Nokia at qt-info@nokia.com. |
|
30 |
** |
|
31 |
** |
|
32 |
** |
|
33 |
** |
|
34 |
** |
|
35 |
** |
|
36 |
** |
|
37 |
** |
|
38 |
** $QT_END_LICENSE$ |
|
39 |
** |
|
40 |
****************************************************************************/ |
|
41 |
||
42 |
#include "qkbdtty_qws.h" |
|
43 |
||
44 |
#if !defined(QT_NO_QWS_KEYBOARD) && !defined(QT_NO_QWS_KBD_TTY) |
|
45 |
||
46 |
#include <QSocketNotifier> |
|
47 |
#include <QStringList> |
|
48 |
||
49 |
#include <qplatformdefs.h> |
|
50 |
#include <private/qcore_unix_p.h> // overrides QT_OPEN |
|
51 |
||
52 |
#include <errno.h> |
|
53 |
#include <termios.h> |
|
54 |
||
55 |
#if defined Q_OS_LINUX |
|
56 |
# include <linux/kd.h> |
|
57 |
# include <linux/vt.h> //TODO: move vt handling somewhere else (QLinuxFbScreen?) |
|
58 |
||
59 |
# include "qscreen_qws.h" |
|
60 |
# include "qwindowsystem_qws.h" |
|
61 |
# include "qapplication.h" |
|
62 |
# include "private/qwindowsurface_qws_p.h" |
|
63 |
# include "private/qwssignalhandler_p.h" |
|
64 |
||
65 |
# define VTACQSIG SIGUSR1 |
|
66 |
# define VTRELSIG SIGUSR2 |
|
67 |
#endif |
|
68 |
||
69 |
||
70 |
QT_BEGIN_NAMESPACE |
|
71 |
||
72 |
class QWSTtyKbPrivate : public QObject |
|
73 |
{ |
|
74 |
Q_OBJECT |
|
75 |
public: |
|
76 |
QWSTtyKbPrivate(QWSTtyKeyboardHandler *handler, const QString &device); |
|
77 |
~QWSTtyKbPrivate(); |
|
78 |
||
79 |
private: |
|
80 |
void switchLed(char, bool); |
|
81 |
void switchConsole(int vt); |
|
82 |
||
83 |
private Q_SLOTS: |
|
84 |
void readKeycode(); |
|
85 |
void handleConsoleSwitch(int sig); |
|
86 |
||
87 |
private: |
|
88 |
QWSTtyKeyboardHandler *m_handler; |
|
89 |
int m_tty_fd; |
|
90 |
struct termios m_tty_attr; |
|
91 |
char m_last_keycode; |
|
92 |
int m_vt_qws; |
|
93 |
int m_orig_kbmode; |
|
94 |
}; |
|
95 |
||
96 |
||
97 |
QWSTtyKeyboardHandler::QWSTtyKeyboardHandler(const QString &device) |
|
98 |
: QWSKeyboardHandler(device) |
|
99 |
{ |
|
100 |
d = new QWSTtyKbPrivate(this, device); |
|
101 |
} |
|
102 |
||
103 |
QWSTtyKeyboardHandler::~QWSTtyKeyboardHandler() |
|
104 |
{ |
|
105 |
delete d; |
|
106 |
} |
|
107 |
||
108 |
bool QWSTtyKeyboardHandler::filterKeycode(char &) |
|
109 |
{ |
|
110 |
return false; |
|
111 |
} |
|
112 |
||
113 |
QWSTtyKbPrivate::QWSTtyKbPrivate(QWSTtyKeyboardHandler *h, const QString &device) |
|
114 |
: m_handler(h), m_tty_fd(-1), m_last_keycode(0), m_vt_qws(0), m_orig_kbmode(K_XLATE) |
|
115 |
{ |
|
116 |
setObjectName(QLatin1String("TTY Keyboard Handler")); |
|
117 |
#ifndef QT_NO_QWS_SIGNALHANDLER |
|
118 |
QWSSignalHandler::instance()->addObject(this); |
|
119 |
#endif |
|
120 |
||
121 |
QString dev = QLatin1String("/dev/tty0"); |
|
122 |
int repeat_delay = -1; |
|
123 |
int repeat_rate = -1; |
|
124 |
||
125 |
QStringList args = device.split(QLatin1Char(':')); |
|
126 |
foreach (const QString &arg, args) { |
|
127 |
if (arg.startsWith(QLatin1String("repeat-delay="))) |
|
128 |
repeat_delay = arg.mid(13).toInt(); |
|
129 |
else if (arg.startsWith(QLatin1String("repeat-rate="))) |
|
130 |
repeat_rate = arg.mid(12).toInt(); |
|
131 |
else if (arg.startsWith(QLatin1String("/dev/"))) |
|
132 |
dev = arg; |
|
133 |
} |
|
134 |
||
135 |
m_tty_fd = QT_OPEN(dev.toLocal8Bit().constData(), O_RDWR, 0); |
|
136 |
if (m_tty_fd >= 0) { |
|
137 |
if (repeat_delay > 0 && repeat_rate > 0) { |
|
138 |
#if defined(Q_OS_LINUX) |
|
139 |
struct ::kbd_repeat kbdrep = { repeat_delay, repeat_rate }; |
|
140 |
::ioctl(m_tty_fd, KDKBDREP, &kbdrep); |
|
141 |
#endif |
|
142 |
} |
|
143 |
||
144 |
QSocketNotifier *notifier; |
|
145 |
notifier = new QSocketNotifier(m_tty_fd, QSocketNotifier::Read, this); |
|
146 |
connect(notifier, SIGNAL(activated(int)), this, SLOT(readKeycode())); |
|
147 |
||
148 |
// save tty config for restore. |
|
149 |
tcgetattr(m_tty_fd, &m_tty_attr); |
|
150 |
||
151 |
struct ::termios termdata; |
|
152 |
tcgetattr(m_tty_fd, &termdata); |
|
153 |
||
154 |
#if defined(Q_OS_LINUX) |
|
155 |
// record the original mode so we can restore it again in the destructor. |
|
156 |
::ioctl(m_tty_fd, KDGKBMODE, &m_orig_kbmode); |
|
157 |
||
158 |
// PLEASE NOTE: |
|
159 |
// the tty keycode interface can only report keycodes 0x01 .. 0x7f |
|
160 |
// KEY_MAX is however defined to 0x1ff. In practice this is sufficient |
|
161 |
// for a PC style keyboard though. |
|
162 |
// we don't support K_RAW anymore - if you need that, you have to add |
|
163 |
// a scan- to keycode converter yourself. |
|
164 |
::ioctl(m_tty_fd, KDSKBMODE, K_MEDIUMRAW); |
|
165 |
#endif |
|
166 |
||
167 |
// set the tty layer to pass-through |
|
168 |
termdata.c_iflag = (IGNPAR | IGNBRK) & (~PARMRK) & (~ISTRIP); |
|
169 |
termdata.c_oflag = 0; |
|
170 |
termdata.c_cflag = CREAD | CS8; |
|
171 |
termdata.c_lflag = 0; |
|
172 |
termdata.c_cc[VTIME]=0; |
|
173 |
termdata.c_cc[VMIN]=1; |
|
174 |
cfsetispeed(&termdata, 9600); |
|
175 |
cfsetospeed(&termdata, 9600); |
|
176 |
tcsetattr(m_tty_fd, TCSANOW, &termdata); |
|
177 |
||
178 |
#if defined(Q_OS_LINUX) |
|
179 |
// VT switching is handled via unix signals |
|
180 |
connect(QApplication::instance(), SIGNAL(unixSignal(int)), this, SLOT(handleConsoleSwitch(int))); |
|
181 |
QApplication::instance()->watchUnixSignal(VTACQSIG, true); |
|
182 |
QApplication::instance()->watchUnixSignal(VTRELSIG, true); |
|
183 |
||
184 |
struct ::vt_mode vtMode; |
|
185 |
if (::ioctl(m_tty_fd, VT_GETMODE, &vtMode) == 0) { |
|
186 |
vtMode.mode = VT_PROCESS; |
|
187 |
vtMode.relsig = VTRELSIG; |
|
188 |
vtMode.acqsig = VTACQSIG; |
|
189 |
||
190 |
if (::ioctl(m_tty_fd, VT_SETMODE, &vtMode) == 0) { |
|
191 |
struct ::vt_stat vtStat; |
|
192 |
::memset(&vtStat, 0, sizeof(vtStat)); |
|
193 |
||
194 |
if (::ioctl(m_tty_fd, VT_GETSTATE, &vtStat) == 0 ) { |
|
195 |
m_vt_qws = vtStat.v_active; |
|
196 |
} |
|
197 |
} |
|
198 |
} |
|
199 |
||
200 |
if (!m_vt_qws) |
|
201 |
qWarning("Could not initialize virtual console switching"); |
|
202 |
#endif |
|
203 |
} else { |
|
204 |
qWarning("Cannot open input device '%s': %s", qPrintable(dev), strerror(errno)); |
|
205 |
return; |
|
206 |
} |
|
207 |
||
208 |
} |
|
209 |
||
210 |
QWSTtyKbPrivate::~QWSTtyKbPrivate() |
|
211 |
{ |
|
212 |
if (m_tty_fd >= 0) { |
|
213 |
#if defined(Q_OS_LINUX) |
|
214 |
::ioctl(m_tty_fd, KDSKBMODE, m_orig_kbmode); |
|
215 |
#endif |
|
216 |
tcsetattr(m_tty_fd, TCSANOW, &m_tty_attr); |
|
217 |
QT_CLOSE(m_tty_fd); |
|
218 |
} |
|
219 |
} |
|
220 |
||
221 |
||
222 |
||
223 |
void QWSTtyKbPrivate::switchLed(char led, bool state) |
|
224 |
{ |
|
225 |
#if defined(Q_OS_LINUX) |
|
226 |
char ledstate; |
|
227 |
||
228 |
::ioctl(m_tty_fd, KDGETLED, &ledstate); |
|
229 |
if (state) |
|
230 |
ledstate |= led; |
|
231 |
else |
|
232 |
ledstate &= ~led; |
|
233 |
::ioctl(m_tty_fd, KDSETLED, ledstate); |
|
234 |
#endif |
|
235 |
} |
|
236 |
||
237 |
void QWSTtyKbPrivate::readKeycode() |
|
238 |
{ |
|
239 |
char buffer[32]; |
|
240 |
int n = 0; |
|
241 |
||
242 |
forever { |
|
243 |
n = QT_READ(m_tty_fd, buffer + n, 32 - n); |
|
244 |
||
245 |
if (n == 0) { |
|
246 |
qWarning("Got EOF from the input device."); |
|
247 |
return; |
|
248 |
} else if (n < 0 && (errno != EINTR && errno != EAGAIN)) { |
|
249 |
qWarning("Could not read from input device: %s", strerror(errno)); |
|
250 |
return; |
|
251 |
} else { |
|
252 |
break; |
|
253 |
} |
|
254 |
} |
|
255 |
||
256 |
for (int i = 0; i < n; ++i) { |
|
257 |
if (m_handler->filterKeycode(buffer[i])) |
|
258 |
continue; |
|
259 |
||
260 |
QWSKeyboardHandler::KeycodeAction ka; |
|
261 |
ka = m_handler->processKeycode(buffer[i] & 0x7f, (buffer[i] & 0x80) == 0x00, buffer[i] == m_last_keycode); |
|
262 |
m_last_keycode = buffer[i]; |
|
263 |
||
264 |
switch (ka) { |
|
265 |
case QWSKeyboardHandler::CapsLockOn: |
|
266 |
case QWSKeyboardHandler::CapsLockOff: |
|
267 |
switchLed(LED_CAP, ka == QWSKeyboardHandler::CapsLockOn); |
|
268 |
break; |
|
269 |
||
270 |
case QWSKeyboardHandler::NumLockOn: |
|
271 |
case QWSKeyboardHandler::NumLockOff: |
|
272 |
switchLed(LED_NUM, ka == QWSKeyboardHandler::NumLockOn); |
|
273 |
break; |
|
274 |
||
275 |
case QWSKeyboardHandler::ScrollLockOn: |
|
276 |
case QWSKeyboardHandler::ScrollLockOff: |
|
277 |
switchLed(LED_SCR, ka == QWSKeyboardHandler::ScrollLockOn); |
|
278 |
break; |
|
279 |
||
280 |
case QWSKeyboardHandler::PreviousConsole: |
|
281 |
switchConsole(qBound(1, m_vt_qws - 1, 10)); |
|
282 |
break; |
|
283 |
||
284 |
case QWSKeyboardHandler::NextConsole: |
|
285 |
switchConsole(qBound(1, m_vt_qws + 1, 10)); |
|
286 |
break; |
|
287 |
||
288 |
default: |
|
289 |
if (ka >= QWSKeyboardHandler::SwitchConsoleFirst && |
|
290 |
ka <= QWSKeyboardHandler::SwitchConsoleLast) { |
|
291 |
switchConsole(1 + (ka & QWSKeyboardHandler::SwitchConsoleMask)); |
|
292 |
} |
|
293 |
//ignore reboot |
|
294 |
break; |
|
295 |
} |
|
296 |
} |
|
297 |
} |
|
298 |
||
299 |
||
300 |
void QWSTtyKbPrivate::switchConsole(int vt) |
|
301 |
{ |
|
302 |
#if defined(Q_OS_LINUX) |
|
303 |
if (m_vt_qws && vt && (m_tty_fd >= 0 )) |
|
304 |
::ioctl(m_tty_fd, VT_ACTIVATE, vt); |
|
305 |
#endif |
|
306 |
} |
|
307 |
||
308 |
void QWSTtyKbPrivate::handleConsoleSwitch(int sig) |
|
309 |
{ |
|
310 |
#if defined(Q_OS_LINUX) |
|
311 |
// received a notification from the kernel that the current VT is |
|
312 |
// changing: either enable or disable QWS painting accordingly. |
|
313 |
||
314 |
if (sig == VTACQSIG) { |
|
315 |
if (::ioctl(m_tty_fd, VT_RELDISP, VT_ACKACQ) == 0) { |
|
316 |
qwsServer->enablePainting(true); |
|
317 |
qt_screen->restore(); |
|
318 |
qwsServer->resumeMouse(); |
|
319 |
qwsServer->refresh(); |
|
320 |
} |
|
321 |
} else if (sig == VTRELSIG) { |
|
322 |
qwsServer->enablePainting(false); |
|
323 |
||
324 |
// Check for reserved surfaces which might still do painting |
|
325 |
bool allWindowsHidden = true; |
|
326 |
const QList<QWSWindow*> windows = QWSServer::instance()->clientWindows(); |
|
327 |
for (int i = 0; i < windows.size(); ++i) { |
|
328 |
const QWSWindow *w = windows.at(i); |
|
329 |
QWSWindowSurface *s = w->windowSurface(); |
|
330 |
if (s && s->isRegionReserved() && !w->allocatedRegion().isEmpty()) { |
|
331 |
allWindowsHidden = false; |
|
332 |
break; |
|
333 |
} |
|
334 |
} |
|
335 |
||
336 |
if (!allWindowsHidden) { |
|
337 |
::ioctl(m_tty_fd, VT_RELDISP, 0); // abort console switch |
|
338 |
qwsServer->enablePainting(true); |
|
339 |
} else if (::ioctl(m_tty_fd, VT_RELDISP, 1) == 0) { |
|
340 |
qt_screen->save(); |
|
341 |
qwsServer->suspendMouse(); |
|
342 |
} else { |
|
343 |
qwsServer->enablePainting(true); |
|
344 |
} |
|
345 |
} |
|
346 |
#endif |
|
347 |
} |
|
348 |
||
349 |
QT_END_NAMESPACE |
|
350 |
||
351 |
#include "qkbdtty_qws.moc" |
|
352 |
||
353 |
#endif // QT_NO_QWS_KEYBOARD || QT_NO_QWS_KBD_TTY |