[mmap] partial revert of 8cef8db4 to disable using mmap file reader
[videoplayer.git] / QConsoleInput.cpp
bloba6da099b6fac203edf617fe639eeb34719fbb531
1 /* ***** BEGIN LICENSE BLOCK *****
3 * The MIT License
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
23 * THE SOFTWARE.
25 * ***** END LICENSE BLOCK ***** */
27 #include <QtGlobal>
28 #include <QApplication>
29 #include <QObject>
30 #include <QKeyEvent>
32 #ifdef Q_OS_UNIX
33 #include <signal.h>
34 #include <termios.h>
35 #include <unistd.h>
36 #endif
38 #include "QConsoleInput.h"
40 QConsoleInput::QConsoleInput(QObject* in_event_handler)
42 event_handler = in_event_handler;
43 start();
46 int
47 asciiToKey(char c)
49 if (!isprint(c))
50 return 0;
51 return toupper(c);
54 Qt::KeyboardModifiers
55 asciiToMod(char c)
57 Qt::KeyboardModifiers mod = Qt::NoModifier;
58 if (isupper(c))
59 mod |= Qt::ShiftModifier;
60 return mod;
63 void
64 QConsoleInput::run()
66 if (!isatty(STDIN_FILENO))
67 /* we can only do this if we are on a tty */
68 return;
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 */
76 bool hack_got_eio;
78 /* put the terminal into cbreak mode, aka character mode, so that read()
79 * doesn't block waiting for a newline before returning */
80 struct termios tio;
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);
87 while (1) {
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);
92 hack_got_eio = 0;
95 char c = 'a';
96 if (read(STDIN_FILENO, &c, 1) <= 0) {
97 sleep(1);
98 hack_got_eio = 1;
99 continue;
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));
109 #endif
110 QCoreApplication::postEvent(event_handler, ev);
114 QConsoleInput::~QConsoleInput()
116 terminate();
117 wait();
119 signal(SIGTTIN, SIG_DFL);
121 /* restore tty mode at exit */
122 if (!isatty(STDIN_FILENO))
123 return;
125 tcsetattr(STDIN_FILENO, TCSANOW, tio_orig);
127 delete tio_orig;