1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2016 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the plugins of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:LGPL$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and The Qt Company. For licensing terms |
14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/contact-us. |
16 | ** |
17 | ** GNU Lesser General Public License Usage |
18 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
19 | ** General Public License version 3 as published by the Free Software |
20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
21 | ** packaging of this file. Please review the following information to |
22 | ** ensure the GNU Lesser General Public License version 3 requirements |
23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
24 | ** |
25 | ** GNU General Public License Usage |
26 | ** Alternatively, this file may be used under the terms of the GNU |
27 | ** General Public License version 2.0 or (at your option) the GNU General |
28 | ** Public license version 3 or any later version approved by the KDE Free |
29 | ** Qt Foundation. The licenses are as published by the Free Software |
30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
31 | ** included in the packaging of this file. Please review the following |
32 | ** information to ensure the GNU General Public License requirements will |
33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
35 | ** |
36 | ** $QT_END_LICENSE$ |
37 | ** |
38 | ****************************************************************************/ |
39 | |
40 | #include "qfbvthandler_p.h" |
41 | #include <QtCore/QSocketNotifier> |
42 | #include <QtCore/private/qglobal_p.h> |
43 | |
44 | #if defined(Q_OS_LINUX) && !defined(Q_OS_ANDROID) && (QT_CONFIG(evdev) || QT_CONFIG(libinput)) |
45 | |
46 | #define VTH_ENABLED |
47 | |
48 | #include <private/qcore_unix_p.h> |
49 | #include <unistd.h> |
50 | #include <signal.h> |
51 | #include <errno.h> |
52 | #include <fcntl.h> |
53 | #include <sys/ioctl.h> |
54 | #include <linux/kd.h> |
55 | |
56 | #ifndef KDSKBMUTE |
57 | #define KDSKBMUTE 0x4B51 |
58 | #endif |
59 | |
60 | #ifdef K_OFF |
61 | #define KBD_OFF_MODE K_OFF |
62 | #else |
63 | #define KBD_OFF_MODE K_RAW |
64 | #endif |
65 | |
66 | #endif |
67 | |
68 | QT_BEGIN_NAMESPACE |
69 | |
70 | #ifdef VTH_ENABLED |
71 | static void setTTYCursor(bool enable) |
72 | { |
73 | static bool ignore = qEnvironmentVariableIntValue(varName: "QT_QPA_PRESERVE_CONSOLE_STATE" ); |
74 | if (ignore) |
75 | return; |
76 | |
77 | const char * const devs[] = { "/dev/tty0" , "/dev/tty" , "/dev/console" , 0 }; |
78 | int fd = -1; |
79 | for (const char * const *dev = devs; *dev; ++dev) { |
80 | fd = QT_OPEN(pathname: *dev, O_RDWR); |
81 | if (fd != -1) { |
82 | // Enable/disable screen blanking and the blinking cursor. |
83 | const char *termctl = enable ? "\033[9;15]\033[?33h\033[?25h\033[?0c" : "\033[9;0]\033[?33l\033[?25l\033[?1c" ; |
84 | QT_WRITE(fd, data: termctl, len: strlen(s: termctl) + 1); |
85 | QT_CLOSE(fd); |
86 | return; |
87 | } |
88 | } |
89 | } |
90 | #endif |
91 | |
92 | #ifdef VTH_ENABLED |
93 | static QFbVtHandler *vth; |
94 | |
95 | void QFbVtHandler::signalHandler(int sigNo) |
96 | { |
97 | char a = sigNo; |
98 | QT_WRITE(fd: vth->m_sigFd[0], data: &a, len: sizeof(a)); |
99 | } |
100 | #endif |
101 | |
102 | QFbVtHandler::QFbVtHandler(QObject *parent) |
103 | : QObject(parent), |
104 | m_tty(-1), |
105 | m_signalNotifier(0) |
106 | { |
107 | #ifdef VTH_ENABLED |
108 | if (isatty(fd: 0)) |
109 | m_tty = 0; |
110 | |
111 | if (::socketpair(AF_UNIX, SOCK_STREAM, protocol: 0, fds: m_sigFd)) { |
112 | qErrnoWarning(errno, msg: "QFbVtHandler: socketpair() failed" ); |
113 | return; |
114 | } |
115 | |
116 | vth = this; |
117 | setTTYCursor(false); |
118 | setKeyboardEnabled(false); |
119 | |
120 | m_signalNotifier = new QSocketNotifier(m_sigFd[1], QSocketNotifier::Read, this); |
121 | connect(sender: m_signalNotifier, signal: &QSocketNotifier::activated, receiver: this, slot: &QFbVtHandler::handleSignal); |
122 | |
123 | if (!qEnvironmentVariableIntValue(varName: "QT_QPA_NO_SIGNAL_HANDLER" )) { |
124 | struct sigaction sa; |
125 | sa.sa_flags = 0; |
126 | sa.sa_handler = signalHandler; |
127 | sigemptyset(set: &sa.sa_mask); |
128 | sigaction(SIGINT, act: &sa, oact: 0); // Ctrl+C |
129 | sigaction(SIGTSTP, act: &sa, oact: 0); // Ctrl+Z |
130 | sigaction(SIGCONT, act: &sa, oact: 0); |
131 | sigaction(SIGTERM, act: &sa, oact: 0); // default signal used by kill |
132 | } |
133 | #endif |
134 | } |
135 | |
136 | QFbVtHandler::~QFbVtHandler() |
137 | { |
138 | #ifdef VTH_ENABLED |
139 | setKeyboardEnabled(true); |
140 | setTTYCursor(true); |
141 | |
142 | if (m_signalNotifier) { |
143 | close(fd: m_sigFd[0]); |
144 | close(fd: m_sigFd[1]); |
145 | } |
146 | #endif |
147 | } |
148 | |
149 | void QFbVtHandler::setKeyboardEnabled(bool enable) |
150 | { |
151 | #ifdef VTH_ENABLED |
152 | if (m_tty == -1) |
153 | return; |
154 | |
155 | if (enable) { |
156 | ::ioctl(fd: m_tty, KDSKBMUTE, 0); |
157 | ::ioctl(fd: m_tty, KDSKBMODE, m_oldKbdMode); |
158 | } else { |
159 | ::ioctl(fd: m_tty, KDGKBMODE, &m_oldKbdMode); |
160 | if (!qEnvironmentVariableIntValue(varName: "QT_QPA_ENABLE_TERMINAL_KEYBOARD" )) { |
161 | ::ioctl(fd: m_tty, KDSKBMUTE, 1); |
162 | ::ioctl(fd: m_tty, KDSKBMODE, KBD_OFF_MODE); |
163 | } |
164 | } |
165 | #else |
166 | Q_UNUSED(enable); |
167 | #endif |
168 | } |
169 | |
170 | void QFbVtHandler::handleSignal() |
171 | { |
172 | #ifdef VTH_ENABLED |
173 | m_signalNotifier->setEnabled(false); |
174 | |
175 | char sigNo; |
176 | if (QT_READ(fd: m_sigFd[1], data: &sigNo, maxlen: sizeof(sigNo)) == sizeof(sigNo)) { |
177 | switch (sigNo) { |
178 | case SIGINT: |
179 | case SIGTERM: |
180 | handleInt(); |
181 | break; |
182 | case SIGTSTP: |
183 | emit aboutToSuspend(); |
184 | setKeyboardEnabled(true); |
185 | setTTYCursor(true); |
186 | ::kill(pid: getpid(), SIGSTOP); |
187 | break; |
188 | case SIGCONT: |
189 | setTTYCursor(false); |
190 | setKeyboardEnabled(false); |
191 | emit resumed(); |
192 | break; |
193 | default: |
194 | break; |
195 | } |
196 | } |
197 | |
198 | m_signalNotifier->setEnabled(true); |
199 | #endif |
200 | } |
201 | |
202 | void QFbVtHandler::handleInt() |
203 | { |
204 | #ifdef VTH_ENABLED |
205 | emit interrupted(); |
206 | setKeyboardEnabled(true); |
207 | setTTYCursor(true); |
208 | _exit(status: 1); |
209 | #endif |
210 | } |
211 | |
212 | QT_END_NAMESPACE |
213 | |