summaryrefslogtreecommitdiffstats
path: root/src/platformsupport/fbconvenience/qfbvthandler.cpp
blob: 0d835331d6d99e3db3723331de653eebf6388724 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
// 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 <QtCore/QSocketNotifier>
#include <QtCore/private/qglobal_p.h>

#if defined(Q_OS_LINUX) && !defined(Q_OS_ANDROID) && (QT_CONFIG(evdev) || QT_CONFIG(libinput))

#define VTH_ENABLED

#include <private/qcore_unix_p.h>
#include <unistd.h>
#include <signal.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/kd.h>

#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"