kwin/utils.cpp
Martin Gräßlin 0bec9ad733 Improve the x11 timestamp handling
Summary:
So far KWin only updated the x11 timestamp if the new timestamp is larger
than the existing one. While this is a useful thing it creates problems
when the 32 bit msec based time stamp wraps around which happens after
running an X server for 49 days. After the timestamp wrapped around KWin
would not update the timestamp any more and thus some calls might fail.
Most prominent victims are keyboard and pointer grab which fails as the
timestamp is either larger than the server timestamp or smaller than the
last grab timestamp.

Another problem related to timestamp handling is KWin getting broken by
wrong timestamps sent by applications. A prominent example is clusterssh
which used to send a timestamp as unix time which is larger than the
x timestamp and thus our timestamp gets too large.

This change addresses these problems by allowing to reset the timestamp.
This is only used from updateXTime (which is normally invoked before we
do things like grabKeyboard). Thus we make QX11Info::getTimestamp the
ultimate trusted source for timestamps.

BUG: 377901
BUG: 348569
FIXED-IN: 5.8.7

Test Plan: As I cannot wait 50 days: unit tests for the two conditions added.

Reviewers: #kwin, #plasma

Subscribers: plasma-devel, kwin

Tags: #kwin

Differential Revision: https://phabricator.kde.org/D5704
2017-05-03 21:36:34 +02:00

246 lines
6.5 KiB
C++

/********************************************************************
KWin - the KDE window manager
This file is part of the KDE project.
Copyright (C) 1999, 2000 Matthias Ettrich <ettrich@kde.org>
Copyright (C) 2003 Lubos Lunak <l.lunak@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/>.
*********************************************************************/
/*
This file is for (very) small utility functions/classes.
*/
#include "utils.h"
#include <QWidget>
#include <kkeyserver.h>
#ifndef KCMRULES
#include <assert.h>
#include <QApplication>
#include <QDebug>
#include <X11/Xlib.h>
#include <stdio.h>
#include "atoms.h"
#include "workspace.h"
#include <signal.h>
#endif
Q_LOGGING_CATEGORY(KWIN_CORE, "kwin_core", QtCriticalMsg)
namespace KWin
{
#ifndef KCMRULES
//************************************
// StrutRect
//************************************
StrutRect::StrutRect(QRect rect, StrutArea area)
: QRect(rect)
, m_area(area)
{
}
StrutRect::StrutRect(const StrutRect& other)
: QRect(other)
, m_area(other.area())
{
}
#endif
#ifndef KCMRULES
/*
Updates xTime(). This used to simply fetch current timestamp from the server,
but that can cause xTime() to be newer than timestamp of events that are
still in our events queue, thus e.g. making XSetInputFocus() caused by such
event to be ignored. Therefore events queue is searched for first
event with timestamp, and extra PropertyNotify is generated in order to make
sure such event is found.
*/
void updateXTime()
{
// NOTE: QX11Info::getTimestamp does not yet search the event queue as the old
// solution did. This means there might be regressions currently. See the
// documentation above on how it should be done properly.
kwinApp()->setX11Time(QX11Info::getTimestamp(), Application::TimestampUpdate::Always);
}
static int server_grab_count = 0;
void grabXServer()
{
if (++server_grab_count == 1)
xcb_grab_server(connection());
}
void ungrabXServer()
{
assert(server_grab_count > 0);
if (--server_grab_count == 0) {
xcb_ungrab_server(connection());
xcb_flush(connection());
}
}
bool grabbedXServer()
{
return server_grab_count > 0;
}
static bool keyboard_grabbed = false;
bool grabXKeyboard(xcb_window_t w)
{
if (QWidget::keyboardGrabber() != NULL)
return false;
if (keyboard_grabbed)
return false;
if (qApp->activePopupWidget() != NULL)
return false;
if (w == XCB_WINDOW_NONE)
w = rootWindow();
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, NULL));
if (grab.isNull()) {
return false;
}
if (grab->status != XCB_GRAB_STATUS_SUCCESS) {
return false;
}
keyboard_grabbed = true;
return true;
}
void ungrabXKeyboard()
{
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!";
}
keyboard_grabbed = false;
xcb_ungrab_keyboard(connection(), XCB_TIME_CURRENT_TIME);
}
Process::Process(QObject *parent)
: QProcess(parent)
{
}
Process::~Process() = default;
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
int qtToX11Button(Qt::MouseButton button)
{
if (button == Qt::LeftButton)
return XCB_BUTTON_INDEX_1;
else if (button == Qt::MidButton)
return XCB_BUTTON_INDEX_2;
else if (button == Qt::RightButton)
return XCB_BUTTON_INDEX_3;
return XCB_BUTTON_INDEX_ANY; // 0
}
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;
}
int qtToX11State(Qt::MouseButtons buttons, Qt::KeyboardModifiers modifiers)
{
int ret = 0;
if (buttons & Qt::LeftButton)
ret |= XCB_KEY_BUT_MASK_BUTTON_1;
if (buttons & Qt::MidButton)
ret |= XCB_KEY_BUT_MASK_BUTTON_2;
if (buttons & Qt::RightButton)
ret |= XCB_KEY_BUT_MASK_BUTTON_3;
if (modifiers & Qt::ShiftModifier)
ret |= XCB_KEY_BUT_MASK_SHIFT;
if (modifiers & Qt::ControlModifier)
ret |= XCB_KEY_BUT_MASK_CONTROL;
if (modifiers & Qt::AltModifier)
ret |= KKeyServer::modXAlt();
if (modifiers & Qt::MetaModifier)
ret |= KKeyServer::modXMeta();
return ret;
}
Qt::MouseButtons x11ToQtMouseButtons(int state)
{
Qt::MouseButtons ret = 0;
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;
}
Qt::KeyboardModifiers x11ToQtKeyboardModifiers(int state)
{
Qt::KeyboardModifiers ret = 0;
if (state & XCB_KEY_BUT_MASK_SHIFT)
ret |= Qt::ShiftModifier;
if (state & XCB_KEY_BUT_MASK_CONTROL)
ret |= Qt::ControlModifier;
if (state & KKeyServer::modXAlt())
ret |= Qt::AltModifier;
if (state & KKeyServer::modXMeta())
ret |= Qt::MetaModifier;
return ret;
}
} // namespace
#ifndef KCMRULES
#endif