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
68QT_BEGIN_NAMESPACE
69
70#ifdef VTH_ENABLED
71static void setTTYCursor(bool enable)
72{
73 static bool ignore = qEnvironmentVariableIntValue("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(*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, termctl, strlen(termctl) + 1);
85 QT_CLOSE(fd);
86 return;
87 }
88 }
89}
90#endif
91
92#ifdef VTH_ENABLED
93static QFbVtHandler *vth;
94
95void QFbVtHandler::signalHandler(int sigNo)
96{
97 char a = sigNo;
98 QT_WRITE(vth->m_sigFd[0], &a, sizeof(a));
99}
100#endif
101
102QFbVtHandler::QFbVtHandler(QObject *parent)
103 : QObject(parent),
104 m_tty(-1),
105 m_signalNotifier(0)
106{
107#ifdef VTH_ENABLED
108 if (isatty(0))
109 m_tty = 0;
110
111 if (::socketpair(AF_UNIX, SOCK_STREAM, 0, m_sigFd)) {
112 qErrnoWarning(errno, "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(m_signalNotifier, &QSocketNotifier::activated, this, &QFbVtHandler::handleSignal);
122
123 if (!qEnvironmentVariableIntValue("QT_QPA_NO_SIGNAL_HANDLER")) {
124 struct sigaction sa;
125 sa.sa_flags = 0;
126 sa.sa_handler = signalHandler;
127 sigemptyset(&sa.sa_mask);
128 sigaction(SIGINT, &sa, 0); // Ctrl+C
129 sigaction(SIGTSTP, &sa, 0); // Ctrl+Z
130 sigaction(SIGCONT, &sa, 0);
131 sigaction(SIGTERM, &sa, 0); // default signal used by kill
132 }
133#endif
134}
135
136QFbVtHandler::~QFbVtHandler()
137{
138#ifdef VTH_ENABLED
139 setKeyboardEnabled(true);
140 setTTYCursor(true);
141
142 if (m_signalNotifier) {
143 close(m_sigFd[0]);
144 close(m_sigFd[1]);
145 }
146#endif
147}
148
149void QFbVtHandler::setKeyboardEnabled(bool enable)
150{
151#ifdef VTH_ENABLED
152 if (m_tty == -1)
153 return;
154
155 if (enable) {
156 ::ioctl(m_tty, KDSKBMUTE, 0);
157 ::ioctl(m_tty, KDSKBMODE, m_oldKbdMode);
158 } else {
159 ::ioctl(m_tty, KDGKBMODE, &m_oldKbdMode);
160 if (!qEnvironmentVariableIntValue("QT_QPA_ENABLE_TERMINAL_KEYBOARD")) {
161 ::ioctl(m_tty, KDSKBMUTE, 1);
162 ::ioctl(m_tty, KDSKBMODE, KBD_OFF_MODE);
163 }
164 }
165#else
166 Q_UNUSED(enable);
167#endif
168}
169
170void QFbVtHandler::handleSignal()
171{
172#ifdef VTH_ENABLED
173 m_signalNotifier->setEnabled(false);
174
175 char sigNo;
176 if (QT_READ(m_sigFd[1], &sigNo, 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(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
202void QFbVtHandler::handleInt()
203{
204#ifdef VTH_ENABLED
205 emit interrupted();
206 setKeyboardEnabled(true);
207 setTTYCursor(true);
208 _exit(1);
209#endif
210}
211
212QT_END_NAMESPACE
213