File: virtual_terminal.cpp

package info (click to toggle)
kwin 4%3A5.14.5-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 45,616 kB
  • sloc: cpp: 155,470; xml: 634; sh: 97; makefile: 11
file content (215 lines) | stat: -rw-r--r-- 5,330 bytes parent folder | download | duplicates (2)
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/********************************************************************
 KWin - the KDE window manager
 This file is part of the KDE project.

Copyright (C) 2015 Martin Gräßlin <mgraesslin@kde.org>

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*********************************************************************/
#include "virtual_terminal.h"
// kwin
#include "logind.h"
#include "main.h"
#include "utils.h"
// Qt
#include <QDebug>
#include <QSocketNotifier>
// linux
#include <linux/major.h>
#include <linux/kd.h>
#include <linux/vt.h>
// system
#include <fcntl.h>
#include <unistd.h>
#include <signal.h>
#include <sys/ioctl.h>
#include <sys/signalfd.h>
#include <sys/stat.h>
#include <sys/sysmacros.h>

#define RELEASE_SIGNAL SIGUSR1
#define ACQUISITION_SIGNAL SIGUSR2

namespace KWin
{

KWIN_SINGLETON_FACTORY(VirtualTerminal)

VirtualTerminal::VirtualTerminal(QObject *parent)
    : QObject(parent)
{
}

void VirtualTerminal::init()
{
    auto logind = LogindIntegration::self();
    if (logind->vt() != -1) {
        setup(logind->vt());
    }
    connect(logind, &LogindIntegration::virtualTerminalChanged, this, &VirtualTerminal::setup);
    if (logind->isConnected()) {
        logind->takeControl();
    } else {
        connect(logind, &LogindIntegration::connectedChanged, logind, &LogindIntegration::takeControl);
    }
}

VirtualTerminal::~VirtualTerminal()
{
    s_self = nullptr;
    closeFd();
}

static bool isTty(int fd)
{
    if (fd < 0) {
        return false;
    }
    struct stat st;
    if (fstat(fd, &st) == -1) {
        return false;
    }
    if (major(st.st_rdev) != TTY_MAJOR || minor (st.st_rdev) <= 0 || minor(st.st_rdev) >= 64) {
        return false;
    }
    return true;
}

void VirtualTerminal::setup(int vtNr)
{
    if (m_vt != -1) {
        return;
    }
    if (vtNr == -1) {
        // error condition
        return;
    }
    QString ttyName = QStringLiteral("/dev/tty%1").arg(vtNr);

    m_vt = open(ttyName.toUtf8().constData(), O_RDWR|O_CLOEXEC|O_NONBLOCK);
    if (m_vt < 0) {
        qCWarning(KWIN_CORE) << "Failed to open tty" << vtNr;
        return;
    }
    if (!isTty(m_vt)) {
        qCWarning(KWIN_CORE) << vtNr << " is no tty";
        closeFd();
        return;
    }
    if (ioctl(m_vt, KDSETMODE, KD_GRAPHICS) < 0) {
        qCWarning(KWIN_CORE()) << "Failed to set tty " << vtNr << " in graphics mode";
        closeFd();
        return;
    }
    if (!createSignalHandler()) {
        qCWarning(KWIN_CORE) << "Failed to create signalfd";
        closeFd();
        return;
    }
    vt_mode mode = {
        VT_PROCESS,
        0,
        RELEASE_SIGNAL,
        ACQUISITION_SIGNAL,
        0
    };
    if (ioctl(m_vt, VT_SETMODE, &mode) < 0) {
        qCWarning(KWIN_CORE) << "Failed to take over virtual terminal";
        closeFd();
        return;
    }
    m_vtNumber = vtNr;
    setActive(true);
    emit kwinApp()->virtualTerminalCreated();
}

void VirtualTerminal::closeFd()
{
    if (m_vt < 0) {
        return;
    }
    if (m_notifier) {
        const int sfd = m_notifier->socket();
        delete m_notifier;
        m_notifier = nullptr;
        close(sfd);
    }
    close(m_vt);
    m_vt = -1;
}

bool VirtualTerminal::createSignalHandler()
{
    if (m_notifier) {
        return false;
    }
    sigset_t mask;
    sigemptyset(&mask);
    sigaddset(&mask, RELEASE_SIGNAL);
    sigaddset(&mask, ACQUISITION_SIGNAL);
    pthread_sigmask(SIG_BLOCK, &mask, nullptr);

    const int fd = signalfd(-1, &mask, SFD_NONBLOCK | SFD_CLOEXEC);
    if (fd < 0) {
        return false;
    }
    m_notifier = new QSocketNotifier(fd, QSocketNotifier::Read, this);
    connect(m_notifier, &QSocketNotifier::activated, this,
        [this] {
            if (m_vt < 0) {
                return;
            }
            while (true) {
                signalfd_siginfo sigInfo;
                if (read(m_notifier->socket(), &sigInfo, sizeof(sigInfo)) != sizeof(sigInfo)) {
                    break;
                }
                switch (sigInfo.ssi_signo) {
                case RELEASE_SIGNAL:
                    setActive(false);
                    ioctl(m_vt, VT_RELDISP, 1);
                    break;
                case ACQUISITION_SIGNAL:
                    ioctl(m_vt, VT_RELDISP, VT_ACKACQ);
                    setActive(true);
                    break;
                }
            }
        }
    );
    return true;
}

void VirtualTerminal::activate(int vt)
{
    if (m_vt < 0) {
        return;
    }
    if (vt == m_vtNumber) {
        return;
    }
    ioctl(m_vt, VT_ACTIVATE, vt);
    setActive(false);
}

void VirtualTerminal::setActive(bool active)
{
    if (m_active == active) {
        return;
    }
    m_active = active;
    emit activeChanged(m_active);
}

}