kwin/utils.cpp

196 lines
4.4 KiB
C++
Raw Normal View History

2020-08-02 22:22:19 +00:00
/*
KWin - the KDE window manager
This file is part of the KDE project.
2020-08-02 22:22:19 +00:00
SPDX-FileCopyrightText: 1999, 2000 Matthias Ettrich <ettrich@kde.org>
SPDX-FileCopyrightText: 2003 Lubos Lunak <l.lunak@kde.org>
2020-08-02 22:22:19 +00:00
SPDX-License-Identifier: GPL-2.0-or-later
*/
/*
This file is for (very) small utility functions/classes.
*/
#include "utils.h"
#include <QWidget>
#include <kkeyserver.h>
#ifndef KCMRULES
#include <QApplication>
#include <QDebug>
#include "atoms.h"
#include "platform.h"
#include "workspace.h"
#include <csignal>
#include <cstdio>
#endif
Q_LOGGING_CATEGORY(KWIN_CORE, "kwin_core", QtCriticalMsg)
Q_LOGGING_CATEGORY(KWIN_VIRTUALKEYBOARD, "kwin_virtualkeyboard", QtCriticalMsg)
namespace KWin
{
#ifndef KCMRULES
//************************************
// StrutRect
//************************************
2011-01-30 14:34:42 +00:00
StrutRect::StrutRect(QRect rect, StrutArea area)
: QRect(rect)
, m_area(area)
{
}
2011-01-30 14:34:42 +00:00
StrutRect::StrutRect(const StrutRect& other)
: QRect(other)
, m_area(other.area())
{
}
StrutRect &StrutRect::operator=(const StrutRect &other)
{
if (this != &other) {
QRect::operator=(other);
m_area = other.area();
}
return *this;
}
Improved resolving whether a window is on local machine Most windows use the hostname in WM_CLIENT_MACHINE, but there are windows using the FQDN (for example libreoffice). So instead of "foo" it is "foo.local.net" or similar. The logic so far has been unable to properly determine whether windows with FQDN are on the local system. In order to solve this problem the handling is split out into an own class which stores the information of hostname and whether it is a local machine. This is to not query multiple times. To determine whether the Client is on the local system getaddrinfo is used for the own hostname and the FQDN provided in WM_CLIENT_MACHINE. If one of the queried names matches, we know that it is on the local machine. The old logic to compare the hostname is still used and getaddrinfo is only a fallback in case hostname does not match. The problem with getaddrinfo is, that it accesses the network and by that could block. To circumvent this problem the calls are moved into threads by using QtConcurrent::run. Obviously this brings disadvantages. When trying to resolve whether a Client is on the local machine and a FQDN is used, the information is initially wrong. The new ClientMachine class emits a signal when the information that the system is local becomes available, but for some things this is just too late: * window rules are already gathered * Session Management has already taken place In both cases this is an acceptable loss. For window rules it just needs a proper matching of the machine in case of localhost (remote hosts are not affected). And the case of session management is very academic as it is unlikely that a restoring session contains remote windows. BUG: 308391 FIXED-IN: 4.11 REVIEW: 108235
2013-01-07 07:07:27 +00:00
#endif
Process::Process(QObject *parent)
: QProcess(parent)
{
}
Process::~Process() = default;
Improved resolving whether a window is on local machine Most windows use the hostname in WM_CLIENT_MACHINE, but there are windows using the FQDN (for example libreoffice). So instead of "foo" it is "foo.local.net" or similar. The logic so far has been unable to properly determine whether windows with FQDN are on the local system. In order to solve this problem the handling is split out into an own class which stores the information of hostname and whether it is a local machine. This is to not query multiple times. To determine whether the Client is on the local system getaddrinfo is used for the own hostname and the FQDN provided in WM_CLIENT_MACHINE. If one of the queried names matches, we know that it is on the local machine. The old logic to compare the hostname is still used and getaddrinfo is only a fallback in case hostname does not match. The problem with getaddrinfo is, that it accesses the network and by that could block. To circumvent this problem the calls are moved into threads by using QtConcurrent::run. Obviously this brings disadvantages. When trying to resolve whether a Client is on the local machine and a FQDN is used, the information is initially wrong. The new ClientMachine class emits a signal when the information that the system is local becomes available, but for some things this is just too late: * window rules are already gathered * Session Management has already taken place In both cases this is an acceptable loss. For window rules it just needs a proper matching of the machine in case of localhost (remote hosts are not affected). And the case of session management is very academic as it is unlikely that a restoring session contains remote windows. BUG: 308391 FIXED-IN: 4.11 REVIEW: 108235
2013-01-07 07:07:27 +00:00
#ifndef KCMRULES
void updateXTime()
2011-01-30 14:34:42 +00:00
{
kwinApp()->platform()->updateXTime();
2011-01-30 14:34:42 +00:00
}
static int server_grab_count = 0;
void grabXServer()
2011-01-30 14:34:42 +00:00
{
if (++server_grab_count == 1)
2013-05-02 07:20:13 +00:00
xcb_grab_server(connection());
2011-01-30 14:34:42 +00:00
}
void ungrabXServer()
2011-01-30 14:34:42 +00:00
{
Q_ASSERT(server_grab_count > 0);
2011-01-30 14:34:42 +00:00
if (--server_grab_count == 0) {
2013-05-02 07:20:13 +00:00
xcb_ungrab_server(connection());
xcb_flush(connection());
}
2011-01-30 14:34:42 +00:00
}
static bool keyboard_grabbed = false;
2013-05-02 07:13:25 +00:00
bool grabXKeyboard(xcb_window_t w)
2011-01-30 14:34:42 +00:00
{
if (QWidget::keyboardGrabber() != nullptr)
return false;
2011-01-30 14:34:42 +00:00
if (keyboard_grabbed)
return false;
if (qApp->activePopupWidget() != nullptr)
return false;
2013-05-02 07:13:25 +00:00
if (w == XCB_WINDOW_NONE)
w = rootWindow();
2013-05-02 07:13:25 +00:00
const xcb_grab_keyboard_cookie_t c = xcb_grab_keyboard_unchecked(connection(), false, w, xTime(),
XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC);
ScopedCPointer<xcb_grab_keyboard_reply_t> grab(xcb_grab_keyboard_reply(connection(), c, nullptr));
2013-05-02 07:13:25 +00:00
if (grab.isNull()) {
return false;
2013-05-02 07:13:25 +00:00
}
if (grab->status != XCB_GRAB_STATUS_SUCCESS) {
return false;
}
keyboard_grabbed = true;
return true;
2011-01-30 14:34:42 +00:00
}
void ungrabXKeyboard()
2011-01-30 14:34:42 +00:00
{
if (!keyboard_grabbed) {
// grabXKeyboard() may fail sometimes, so don't fail, but at least warn anyway
qCDebug(KWIN_CORE) << "ungrabXKeyboard() called but keyboard not grabbed!";
}
2011-01-30 14:34:42 +00:00
keyboard_grabbed = false;
2013-05-02 07:13:25 +00:00
xcb_ungrab_keyboard(connection(), XCB_TIME_CURRENT_TIME);
2011-01-30 14:34:42 +00:00
}
void Process::setupChildProcess()
{
sigset_t userSiganls;
sigemptyset(&userSiganls);
sigaddset(&userSiganls, SIGUSR1);
sigaddset(&userSiganls, SIGUSR2);
pthread_sigmask(SIG_UNBLOCK, &userSiganls, nullptr);
}
#endif
// converting between X11 mouse/keyboard state mask and Qt button/keyboard states
2011-01-30 14:34:42 +00:00
Qt::MouseButton x11ToQtMouseButton(int button)
{
if (button == XCB_BUTTON_INDEX_1)
return Qt::LeftButton;
if (button == XCB_BUTTON_INDEX_2)
return Qt::MidButton;
if (button == XCB_BUTTON_INDEX_3)
return Qt::RightButton;
if (button == XCB_BUTTON_INDEX_4)
return Qt::XButton1;
if (button == XCB_BUTTON_INDEX_5)
return Qt::XButton2;
return Qt::NoButton;
2011-01-30 14:34:42 +00:00
}
2011-01-30 14:34:42 +00:00
Qt::MouseButtons x11ToQtMouseButtons(int state)
{
Qt::MouseButtons ret = {};
if (state & XCB_KEY_BUT_MASK_BUTTON_1)
ret |= Qt::LeftButton;
if (state & XCB_KEY_BUT_MASK_BUTTON_2)
ret |= Qt::MidButton;
if (state & XCB_KEY_BUT_MASK_BUTTON_3)
ret |= Qt::RightButton;
if (state & XCB_KEY_BUT_MASK_BUTTON_4)
ret |= Qt::XButton1;
if (state & XCB_KEY_BUT_MASK_BUTTON_5)
ret |= Qt::XButton2;
return ret;
2011-01-30 14:34:42 +00:00
}
2011-01-30 14:34:42 +00:00
Qt::KeyboardModifiers x11ToQtKeyboardModifiers(int state)
{
Qt::KeyboardModifiers ret = {};
if (state & XCB_KEY_BUT_MASK_SHIFT)
ret |= Qt::ShiftModifier;
if (state & XCB_KEY_BUT_MASK_CONTROL)
ret |= Qt::ControlModifier;
2011-01-30 14:34:42 +00:00
if (state & KKeyServer::modXAlt())
ret |= Qt::AltModifier;
2011-01-30 14:34:42 +00:00
if (state & KKeyServer::modXMeta())
ret |= Qt::MetaModifier;
return ret;
2011-01-30 14:34:42 +00:00
}
} // namespace
#ifndef KCMRULES
#endif