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 const char * const devs[] = { "/dev/tty0", "/dev/tty", "/dev/console", 0 };
74 int fd = -1;
75 for (const char * const *dev = devs; *dev; ++dev) {
76 fd = QT_OPEN(*dev, O_RDWR);
77 if (fd != -1) {
78 // Enable/disable screen blanking and the blinking cursor.
79 const char *termctl = enable ? "\033[9;15]\033[?33h\033[?25h\033[?0c" : "\033[9;0]\033[?33l\033[?25l\033[?1c";
80 QT_WRITE(fd, termctl, strlen(termctl) + 1);
81 QT_CLOSE(fd);
82 return;
83 }
84 }
85}
86#endif
87
88#ifdef VTH_ENABLED
89static QFbVtHandler *vth;
90
91void QFbVtHandler::signalHandler(int sigNo)
92{
93 char a = sigNo;
94 QT_WRITE(vth->m_sigFd[0], &a, sizeof(a));
95}
96#endif
97
98QFbVtHandler::QFbVtHandler(QObject *parent)
99 : QObject(parent),
100 m_tty(-1),
101 m_signalNotifier(0)
102{
103#ifdef VTH_ENABLED
104 if (isatty(0))
105 m_tty = 0;
106
107 if (::socketpair(AF_UNIX, SOCK_STREAM, 0, m_sigFd)) {
108 qErrnoWarning(errno, "QFbVtHandler: socketpair() failed");
109 return;
110 }
111
112 vth = this;
113 setTTYCursor(false);
114 setKeyboardEnabled(false);
115
116 m_signalNotifier = new QSocketNotifier(m_sigFd[1], QSocketNotifier::Read, this);
117 connect(m_signalNotifier, &QSocketNotifier::activated, this, &QFbVtHandler::handleSignal);
118
119 if (!qEnvironmentVariableIntValue("QT_QPA_NO_SIGNAL_HANDLER")) {
120 struct sigaction sa;
121 sa.sa_flags = 0;
122 sa.sa_handler = signalHandler;
123 sigemptyset(&sa.sa_mask);
124 sigaction(SIGINT, &sa, 0); // Ctrl+C
125 sigaction(SIGTSTP, &sa, 0); // Ctrl+Z
126 sigaction(SIGCONT, &sa, 0);
127 sigaction(SIGTERM, &sa, 0); // default signal used by kill
128 }
129#endif
130}
131
132QFbVtHandler::~QFbVtHandler()
133{
134#ifdef VTH_ENABLED
135 setKeyboardEnabled(true);
136 setTTYCursor(true);
137
138 if (m_signalNotifier) {
139 close(m_sigFd[0]);
140 close(m_sigFd[1]);
141 }
142#endif
143}
144
145void QFbVtHandler::setKeyboardEnabled(bool enable)
146{
147#ifdef VTH_ENABLED
148 if (m_tty == -1)
149 return;
150
151 if (enable) {
152 ::ioctl(m_tty, KDSKBMUTE, 0);
153 ::ioctl(m_tty, KDSKBMODE, m_oldKbdMode);
154 } else {
155 ::ioctl(m_tty, KDGKBMODE, &m_oldKbdMode);
156 if (!qEnvironmentVariableIntValue("QT_QPA_ENABLE_TERMINAL_KEYBOARD")) {
157 ::ioctl(m_tty, KDSKBMUTE, 1);
158 ::ioctl(m_tty, KDSKBMODE, KBD_OFF_MODE);
159 }
160 }
161#else
162 Q_UNUSED(enable);
163#endif
164}
165
166void QFbVtHandler::handleSignal()
167{
168#ifdef VTH_ENABLED
169 m_signalNotifier->setEnabled(false);
170
171 char sigNo;
172 if (QT_READ(m_sigFd[1], &sigNo, sizeof(sigNo)) == sizeof(sigNo)) {
173 switch (sigNo) {
174 case SIGINT:
175 case SIGTERM:
176 handleInt();
177 break;
178 case SIGTSTP:
179 emit aboutToSuspend();
180 setKeyboardEnabled(true);
181 setTTYCursor(true);
182 ::kill(getpid(), SIGSTOP);
183 break;
184 case SIGCONT:
185 setTTYCursor(false);
186 setKeyboardEnabled(false);
187 emit resumed();
188 break;
189 default:
190 break;
191 }
192 }
193
194 m_signalNotifier->setEnabled(true);
195#endif
196}
197
198void QFbVtHandler::handleInt()
199{
200#ifdef VTH_ENABLED
201 emit interrupted();
202 setKeyboardEnabled(true);
203 setTTYCursor(true);
204 _exit(1);
205#endif
206}
207
208QT_END_NAMESPACE
209