kwin/utils.cpp

512 lines
14 KiB
C++
Raw Normal View History

/********************************************************************
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"
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
#include <kxerrorhandler.h>
#include <X11/Xatom.h>
#include <KDE/KLocalizedString>
#ifndef KCMRULES
#include <QLabel>
#include <QVBoxLayout>
#include <assert.h>
#include <kdebug.h>
#include <kglobalaccel.h>
#include <kshortcut.h>
#include <kkeyserver.h>
#include <KPushButton>
#include <X11/Xlib.h>
#include <X11/extensions/shape.h>
#include <QX11Info>
#include <QtGui/QKeySequence>
#include <stdio.h>
#include "atoms.h"
#include "cursor.h"
#include "notifications.h"
#include "workspace.h"
#endif
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())
{
}
//************************************
// Motif
//************************************
2011-01-30 14:34:42 +00:00
void Motif::readFlags(WId w, bool& got_noborder, bool& noborder,
bool& resize, bool& move, bool& minimize, bool& maximize, bool& close)
{
Atom type;
int format;
unsigned long length, after;
unsigned char* data;
MwmHints* hints = 0;
2011-01-30 14:34:42 +00:00
if (XGetWindowProperty(display(), w, atoms->motif_wm_hints, 0, 5,
false, atoms->motif_wm_hints, &type, &format,
&length, &after, &data) == Success) {
if (data)
hints = (MwmHints*) data;
2011-01-30 14:34:42 +00:00
}
got_noborder = false;
noborder = false;
resize = true;
move = true;
minimize = true;
maximize = true;
close = true;
2011-01-30 14:34:42 +00:00
if (hints) {
// To quote from Metacity 'We support those MWM hints deemed non-stupid'
if (hints->flags & MWM_HINTS_FUNCTIONS) {
// if MWM_FUNC_ALL is set, other flags say what to turn _off_
2011-01-30 14:34:42 +00:00
bool set_value = ((hints->functions & MWM_FUNC_ALL) == 0);
resize = move = minimize = maximize = close = !set_value;
2011-01-30 14:34:42 +00:00
if (hints->functions & MWM_FUNC_RESIZE)
resize = set_value;
2011-01-30 14:34:42 +00:00
if (hints->functions & MWM_FUNC_MOVE)
move = set_value;
2011-01-30 14:34:42 +00:00
if (hints->functions & MWM_FUNC_MINIMIZE)
minimize = set_value;
2011-01-30 14:34:42 +00:00
if (hints->functions & MWM_FUNC_MAXIMIZE)
maximize = set_value;
2011-01-30 14:34:42 +00:00
if (hints->functions & MWM_FUNC_CLOSE)
close = set_value;
2011-01-30 14:34:42 +00:00
}
if (hints->flags & MWM_HINTS_DECORATIONS) {
got_noborder = true;
noborder = !hints->decorations;
}
2011-01-30 14:34:42 +00:00
XFree(data);
}
2011-01-30 14:34:42 +00:00
}
//************************************
// KWinSelectionOwner
//************************************
2011-01-30 14:34:42 +00:00
KWinSelectionOwner::KWinSelectionOwner(int screen_P)
: KSelectionOwner(make_selection_atom(screen_P), screen_P)
{
}
2011-01-30 14:34:42 +00:00
Atom KWinSelectionOwner::make_selection_atom(int screen_P)
{
if (screen_P < 0)
screen_P = DefaultScreen(display());
char tmp[ 30 ];
2011-01-30 14:34:42 +00:00
sprintf(tmp, "WM_S%d", screen_P);
return XInternAtom(display(), tmp, False);
}
void KWinSelectionOwner::getAtoms()
2011-01-30 14:34:42 +00:00
{
KSelectionOwner::getAtoms();
2011-01-30 14:34:42 +00:00
if (xa_version == None) {
Atom atoms[ 1 ];
const char* const names[] =
2011-01-30 14:34:42 +00:00
{ "VERSION" };
XInternAtoms(display(), const_cast< char** >(names), 1, False, atoms);
xa_version = atoms[ 0 ];
}
2011-01-30 14:34:42 +00:00
}
2011-01-30 14:34:42 +00:00
void KWinSelectionOwner::replyTargets(Atom property_P, Window requestor_P)
{
KSelectionOwner::replyTargets(property_P, requestor_P);
Atom atoms[ 1 ] = { xa_version };
// PropModeAppend !
2011-01-30 14:34:42 +00:00
XChangeProperty(display(), requestor_P, property_P, XA_ATOM, 32, PropModeAppend,
reinterpret_cast< unsigned char* >(atoms), 1);
}
2011-01-30 14:34:42 +00:00
bool KWinSelectionOwner::genericReply(Atom target_P, Atom property_P, Window requestor_P)
{
if (target_P == xa_version) {
long version[] = { 2, 0 };
2011-01-30 14:34:42 +00:00
XChangeProperty(display(), requestor_P, property_P, XA_INTEGER, 32,
PropModeReplace, reinterpret_cast< unsigned char* >(&version), 2);
} else
return KSelectionOwner::genericReply(target_P, property_P, requestor_P);
return true;
}
Atom KWinSelectionOwner::xa_version = None;
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
QByteArray getStringProperty(WId w, Atom prop, char separator)
2011-01-30 14:34:42 +00:00
{
Atom type;
int format, status;
unsigned long nitems = 0;
unsigned long extra = 0;
unsigned char *data = 0;
QByteArray result = "";
KXErrorHandler handler; // ignore errors
2011-01-30 14:34:42 +00:00
status = XGetWindowProperty(display(), w, prop, 0, 10000,
false, XA_STRING, &type, &format,
&nitems, &extra, &data);
if (status == Success) {
if (data && separator) {
for (int i = 0; i < (int)nitems; i++)
if (!data[i] && i + 1 < (int)nitems)
data[i] = separator;
2011-01-30 14:34:42 +00:00
}
if (data)
result = (const char*) data;
XFree(data);
}
2011-01-30 14:34:42 +00:00
return result;
}
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
static Time next_x_time;
2011-01-30 14:34:42 +00:00
static Bool update_x_time_predicate(Display*, XEvent* event, XPointer)
{
2011-01-30 14:34:42 +00:00
if (next_x_time != CurrentTime)
return False;
// from qapplication_x11.cpp
2011-01-30 14:34:42 +00:00
switch(event->type) {
case ButtonPress:
2011-01-30 14:34:42 +00:00
// fallthrough intended
case ButtonRelease:
2011-01-30 14:34:42 +00:00
next_x_time = event->xbutton.time;
break;
case MotionNotify:
2011-01-30 14:34:42 +00:00
next_x_time = event->xmotion.time;
break;
case KeyPress:
2011-01-30 14:34:42 +00:00
// fallthrough intended
case KeyRelease:
2011-01-30 14:34:42 +00:00
next_x_time = event->xkey.time;
break;
case PropertyNotify:
2011-01-30 14:34:42 +00:00
next_x_time = event->xproperty.time;
break;
case EnterNotify:
case LeaveNotify:
2011-01-30 14:34:42 +00:00
next_x_time = event->xcrossing.time;
break;
case SelectionClear:
2011-01-30 14:34:42 +00:00
next_x_time = event->xselectionclear.time;
break;
default:
2011-01-30 14:34:42 +00:00
break;
}
return False;
}
/*
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()
2011-01-30 14:34:42 +00:00
{
static QWidget* w = 0;
2011-01-30 14:34:42 +00:00
if (!w)
w = new QWidget;
long data = 1;
XChangeProperty(display(), w->winId(), atoms->kwin_running, atoms->kwin_running, 32,
2011-01-30 14:34:42 +00:00
PropModeAppend, (unsigned char*) &data, 1);
next_x_time = CurrentTime;
XEvent dummy;
2011-01-30 14:34:42 +00:00
XCheckIfEvent(display(), &dummy, update_x_time_predicate, NULL);
if (next_x_time == CurrentTime) {
XSync(display(), False);
XCheckIfEvent(display(), &dummy, update_x_time_predicate, NULL);
}
2011-01-30 14:34:42 +00:00
assert(next_x_time != CurrentTime);
QX11Info::setAppTime(next_x_time);
XEvent ev; // remove the PropertyNotify event from the events queue
XWindowEvent(display(), w->winId(), PropertyChangeMask, &ev);
}
static int server_grab_count = 0;
void grabXServer()
2011-01-30 14:34:42 +00:00
{
if (++server_grab_count == 1)
XGrabServer(display());
}
void ungrabXServer()
2011-01-30 14:34:42 +00:00
{
assert(server_grab_count > 0);
if (--server_grab_count == 0) {
XUngrabServer(display());
XFlush(display());
Notify::sendPendingEvents();
}
2011-01-30 14:34:42 +00:00
}
bool grabbedXServer()
2011-01-30 14:34:42 +00:00
{
return server_grab_count > 0;
2011-01-30 14:34:42 +00:00
}
static bool keyboard_grabbed = false;
2011-01-30 14:34:42 +00:00
bool grabXKeyboard(Window w)
{
if (QWidget::keyboardGrabber() != NULL)
return false;
2011-01-30 14:34:42 +00:00
if (keyboard_grabbed)
return false;
2011-01-30 14:34:42 +00:00
if (qApp->activePopupWidget() != NULL)
return false;
2011-01-30 14:34:42 +00:00
if (w == None)
w = rootWindow();
2011-01-30 14:34:42 +00:00
if (XGrabKeyboard(display(), w, False,
GrabModeAsync, GrabModeAsync, xTime()) != GrabSuccess)
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
kDebug(1212) << "ungrabXKeyboard() called but keyboard not grabbed!";
}
2011-01-30 14:34:42 +00:00
keyboard_grabbed = false;
XUngrabKeyboard(display(), CurrentTime);
}
QPoint cursorPos()
2011-01-30 14:34:42 +00:00
{
return Cursor::self()->pos();
2011-01-30 14:34:42 +00:00
}
// converting between X11 mouse/keyboard state mask and Qt button/keyboard states
2011-01-30 14:34:42 +00:00
int qtToX11Button(Qt::MouseButton button)
{
if (button == Qt::LeftButton)
return Button1;
2011-01-30 14:34:42 +00:00
else if (button == Qt::MidButton)
return Button2;
2011-01-30 14:34:42 +00:00
else if (button == Qt::RightButton)
return Button3;
return AnyButton; // 0
2011-01-30 14:34:42 +00:00
}
2011-01-30 14:34:42 +00:00
Qt::MouseButton x11ToQtMouseButton(int button)
{
if (button == Button1)
return Qt::LeftButton;
2011-01-30 14:34:42 +00:00
if (button == Button2)
return Qt::MidButton;
2011-01-30 14:34:42 +00:00
if (button == Button3)
return Qt::RightButton;
2011-01-30 14:34:42 +00:00
if (button == Button4)
return Qt::XButton1;
2011-01-30 14:34:42 +00:00
if (button == Button5)
return Qt::XButton2;
return Qt::NoButton;
2011-01-30 14:34:42 +00:00
}
2011-01-30 14:34:42 +00:00
int qtToX11State(Qt::MouseButtons buttons, Qt::KeyboardModifiers modifiers)
{
int ret = 0;
2011-01-30 14:34:42 +00:00
if (buttons & Qt::LeftButton)
ret |= Button1Mask;
2011-01-30 14:34:42 +00:00
if (buttons & Qt::MidButton)
ret |= Button2Mask;
2011-01-30 14:34:42 +00:00
if (buttons & Qt::RightButton)
ret |= Button3Mask;
2011-01-30 14:34:42 +00:00
if (modifiers & Qt::ShiftModifier)
ret |= ShiftMask;
2011-01-30 14:34:42 +00:00
if (modifiers & Qt::ControlModifier)
ret |= ControlMask;
2011-01-30 14:34:42 +00:00
if (modifiers & Qt::AltModifier)
ret |= KKeyServer::modXAlt();
2011-01-30 14:34:42 +00:00
if (modifiers & Qt::MetaModifier)
ret |= KKeyServer::modXMeta();
return ret;
2011-01-30 14:34:42 +00:00
}
2011-01-30 14:34:42 +00:00
Qt::MouseButtons x11ToQtMouseButtons(int state)
{
Qt::MouseButtons ret = 0;
2011-01-30 14:34:42 +00:00
if (state & Button1Mask)
ret |= Qt::LeftButton;
2011-01-30 14:34:42 +00:00
if (state & Button2Mask)
ret |= Qt::MidButton;
2011-01-30 14:34:42 +00:00
if (state & Button3Mask)
ret |= Qt::RightButton;
2011-01-30 14:34:42 +00:00
if (state & Button4Mask)
ret |= Qt::XButton1;
2011-01-30 14:34:42 +00:00
if (state & Button5Mask)
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 = 0;
2011-01-30 14:34:42 +00:00
if (state & ShiftMask)
ret |= Qt::ShiftModifier;
2011-01-30 14:34:42 +00:00
if (state & ControlMask)
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
}
#endif
#ifndef KCMRULES
2011-01-30 14:34:42 +00:00
ShortcutDialog::ShortcutDialog(const QKeySequence& cut)
: _shortcut(cut)
2011-01-30 14:34:42 +00:00
{
QWidget *vBoxContainer = new QWidget(this);
vBoxContainer->setLayout(new QVBoxLayout(vBoxContainer));
vBoxContainer->layout()->addWidget(widget = new KKeySequenceWidget(vBoxContainer));
vBoxContainer->layout()->addWidget(warning = new QLabel(vBoxContainer));
warning->hide();
2011-01-30 14:34:42 +00:00
widget->setKeySequence(cut);
// To not check for conflicting shortcuts. The widget would use a message
// box which brings down kwin.
widget->setCheckForConflictsAgainst(KKeySequenceWidget::None);
// It's a global shortcut so don't allow multikey shortcuts
widget->setMultiKeyShortcutsAllowed(false);
// Listen to changed shortcuts
connect(
2011-08-17 21:51:55 +00:00
widget, SIGNAL(keySequenceChanged(QKeySequence)),
SLOT(keySequenceChanged(QKeySequence)));
2011-01-30 14:34:42 +00:00
setMainWidget(vBoxContainer);
widget->setFocus();
// make it a popup, so that it has the grab
XSetWindowAttributes attrs;
attrs.override_redirect = True;
2011-01-30 14:34:42 +00:00
XChangeWindowAttributes(display(), winId(), CWOverrideRedirect, &attrs);
setWindowFlags(Qt::Popup);
}
void ShortcutDialog::accept()
2011-01-30 14:34:42 +00:00
{
QKeySequence seq = shortcut();
2011-01-30 14:34:42 +00:00
if (!seq.isEmpty()) {
if (seq[0] == Qt::Key_Escape) {
reject();
return;
2011-01-30 14:34:42 +00:00
}
if (seq[0] == Qt::Key_Space
|| (seq[0] & Qt::KeyboardModifierMask) == 0) {
// clear
widget->clearKeySequence();
KDialog::accept();
return;
}
}
2011-01-30 14:34:42 +00:00
KDialog::accept();
}
2011-01-30 14:34:42 +00:00
void ShortcutDialog::done(int r)
{
KDialog::done(r);
emit dialogDone(r == Accepted);
}
void ShortcutDialog::keySequenceChanged(const QKeySequence &seq)
2011-01-30 14:34:42 +00:00
{
activateWindow(); // where is the kbd focus lost? cause of popup state?
if (_shortcut == seq)
return; // don't try to update the same
if (seq.isEmpty()) { // clear
_shortcut = seq;
return;
}
// Check if the key sequence is used currently
QString sc = seq.toString();
// NOTICE - seq.toString() & the entries in "conflicting" randomly get invalidated after the next call (if no sc has been set & conflicting isn't empty?!)
QList<KGlobalShortcutInfo> conflicting = KGlobalAccel::getGlobalShortcutsByKey(seq);
2011-01-30 14:34:42 +00:00
if (!conflicting.isEmpty()) {
const KGlobalShortcutInfo &conflict = conflicting.at(0);
warning->setText(i18nc("'%1' is a keyboard shortcut like 'ctrl+w'",
2011-01-30 14:34:42 +00:00
"<b>%1</b> is already in use", sc));
warning->setToolTip(i18nc("keyboard shortcut '%1' is used by action '%2' in application '%3'",
2011-01-30 14:34:42 +00:00
"<b>%1</b> is used by %2 in %3", sc, conflict.friendlyName(), conflict.componentFriendlyName()));
warning->show();
widget->setKeySequence(shortcut());
2011-01-30 14:34:42 +00:00
} else if (seq != _shortcut) {
warning->hide();
2011-01-30 14:34:42 +00:00
if (KPushButton *ok = button(KDialog::Ok))
ok->setFocus();
}
2011-01-30 14:34:42 +00:00
_shortcut = seq;
}
QKeySequence ShortcutDialog::shortcut() const
2011-01-30 14:34:42 +00:00
{
return _shortcut;
2011-01-30 14:34:42 +00:00
}
#endif //KCMRULES
} // namespace
#ifndef KCMRULES
#include "utils.moc"
#endif