1 /* ***** BEGIN LICENSE BLOCK *****
5 * Copyright (c) 2008 BBC Research
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 * ***** END LICENSE BLOCK ***** */
28 #include <QApplication>
38 #include "QConsoleInput.h"
40 QConsoleInput::QConsoleInput(QObject
* in_event_handler
)
42 event_handler
= in_event_handler
;
57 Qt::KeyboardModifiers mod
= Qt::NoModifier
;
59 mod
|= Qt::ShiftModifier
;
66 if (!isatty(STDIN_FILENO
))
67 /* we can only do this if we are on a tty */
70 /* Protect against reading while backgrounded
71 * (causes read to return -EIO) */
72 signal(SIGTTIN
, SIG_IGN
);
73 /* hack: flag set when we get -EIO on read */
74 /* No signal is generated to inform a process if it suddenly
75 * becomes the foreground process, so some hack is required */
78 /* put the terminal into cbreak mode, aka character mode, so that read()
79 * doesn't block waiting for a newline before returning */
81 tio_orig
= new termios
;
82 tcgetattr(STDIN_FILENO
, tio_orig
);
83 tcgetattr(STDIN_FILENO
, &tio
);
84 tio
.c_lflag
&= ~(ICANON
| ECHO
);
85 tcsetattr(STDIN_FILENO
, TCSANOW
, &tio
);
88 if (hack_got_eio
&& tcgetpgrp(STDIN_FILENO
) == getpid()) {
89 tcgetattr(STDIN_FILENO
, &tio
);
90 tio
.c_lflag
&= ~(ICANON
| ECHO
);
91 tcsetattr(STDIN_FILENO
, TCSANOW
, &tio
);
96 if (read(STDIN_FILENO
, &c
, 1) <= 0) {
102 Qt::KeyboardModifiers mods
= asciiToMod(c
);
103 int key
= asciiToKey(c
);
105 QKeyEvent
*ev
= new QKeyEvent(QEvent::KeyPress
, key
, mods
);
106 #if QT_VERSION >= 0x040400
107 if (event_handler
->isWidgetType())
108 QApplication::setActiveWindow(reinterpret_cast<QWidget
*>(event_handler
));
110 QCoreApplication::postEvent(event_handler
, ev
);
114 QConsoleInput::~QConsoleInput()
119 signal(SIGTTIN
, SIG_DFL
);
121 /* restore tty mode at exit */
122 if (!isatty(STDIN_FILENO
))
125 tcsetattr(STDIN_FILENO
, TCSANOW
, tio_orig
);