// Copyright (C) 2016 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only #include "qfbvthandler_p.h" #include #include #if defined(Q_OS_LINUX) && !defined(Q_OS_ANDROID) && (QT_CONFIG(evdev) || QT_CONFIG(libinput)) #define VTH_ENABLED #include #include #include #include #include #include #include #ifndef KDSKBMUTE #define KDSKBMUTE 0x4B51 #endif #ifdef K_OFF #define KBD_OFF_MODE K_OFF #else #define KBD_OFF_MODE K_RAW #endif #endif QT_BEGIN_NAMESPACE #ifdef VTH_ENABLED static void setTTYCursor(bool enable) { static bool ignore = qEnvironmentVariableIntValue("QT_QPA_PRESERVE_CONSOLE_STATE"); if (ignore) return; const char * const devs[] = { "/dev/tty0", "/dev/tty", "/dev/console", 0 }; int fd = -1; for (const char * const *dev = devs; *dev; ++dev) { fd = QT_OPEN(*dev, O_RDWR); if (fd != -1) { // Enable/disable screen blanking and the blinking cursor. const char *termctl = enable ? "\033[9;15]\033[?33h\033[?25h\033[?0c" : "\033[9;0]\033[?33l\033[?25l\033[?1c"; QT_WRITE(fd, termctl, strlen(termctl) + 1); QT_CLOSE(fd); return; } } } #endif #ifdef VTH_ENABLED static QFbVtHandler *vth; void QFbVtHandler::signalHandler(int sigNo) { char a = sigNo; QT_WRITE(vth->m_sigFd[0], &a, sizeof(a)); } #endif QFbVtHandler::QFbVtHandler(QObject *parent) : QObject(parent), m_tty(-1), m_signalNotifier(0) { #ifdef VTH_ENABLED if (isatty(0)) m_tty = 0; if (::socketpair(AF_UNIX, SOCK_STREAM, 0, m_sigFd)) { qErrnoWarning(errno, "QFbVtHandler: socketpair() failed"); return; } vth = this; setTTYCursor(false); setKeyboardEnabled(false); m_signalNotifier = new QSocketNotifier(m_sigFd[1], QSocketNotifier::Read, this); connect(m_signalNotifier, &QSocketNotifier::activated, this, &QFbVtHandler::handleSignal); if (!qEnvironmentVariableIntValue("QT_QPA_NO_SIGNAL_HANDLER")) { struct sigaction sa; sa.sa_flags = 0; sa.sa_handler = signalHandler; sigemptyset(&sa.sa_mask); sigaction(SIGINT, &sa, 0); // Ctrl+C sigaction(SIGTSTP, &sa, 0); // Ctrl+Z sigaction(SIGCONT, &sa, 0); sigaction(SIGTERM, &sa, 0); // default signal used by kill } #endif } QFbVtHandler::~QFbVtHandler() { #ifdef VTH_ENABLED setKeyboardEnabled(true); setTTYCursor(true); if (m_signalNotifier) { close(m_sigFd[0]); close(m_sigFd[1]); } #endif } void QFbVtHandler::setKeyboardEnabled(bool enable) { #ifdef VTH_ENABLED if (m_tty == -1) return; if (enable) { ::ioctl(m_tty, KDSKBMUTE, 0); ::ioctl(m_tty, KDSKBMODE, m_oldKbdMode); } else { ::ioctl(m_tty, KDGKBMODE, &m_oldKbdMode); if (!qEnvironmentVariableIntValue("QT_QPA_ENABLE_TERMINAL_KEYBOARD")) { ::ioctl(m_tty, KDSKBMUTE, 1); ::ioctl(m_tty, KDSKBMODE, KBD_OFF_MODE); } } #else Q_UNUSED(enable); #endif } void QFbVtHandler::handleSignal() { #ifdef VTH_ENABLED m_signalNotifier->setEnabled(false); char sigNo; if (QT_READ(m_sigFd[1], &sigNo, sizeof(sigNo)) == sizeof(sigNo)) { switch (sigNo) { case SIGINT: case SIGTERM: handleInt(); break; case SIGTSTP: emit aboutToSuspend(); setKeyboardEnabled(true); setTTYCursor(true); ::kill(getpid(), SIGSTOP); break; case SIGCONT: setTTYCursor(false); setKeyboardEnabled(false); emit resumed(); break; default: break; } } m_signalNotifier->setEnabled(true); #endif } void QFbVtHandler::handleInt() { #ifdef VTH_ENABLED emit interrupted(); setKeyboardEnabled(true); setTTYCursor(true); _exit(1); #endif } QT_END_NAMESPACE #include "moc_qfbvthandler_p.cpp"