2007-11-27 19:40:25 +00:00
|
|
|
/********************************************************************
|
2007-04-29 17:35:43 +00:00
|
|
|
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>
|
|
|
|
|
2007-11-27 19:40:25 +00:00
|
|
|
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/>.
|
|
|
|
*********************************************************************/
|
2007-04-29 17:35:43 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
This file is for (very) small utility functions/classes.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "utils.h"
|
|
|
|
|
2013-07-23 09:57:35 +00:00
|
|
|
#include <QWidget>
|
2007-04-29 17:35:43 +00:00
|
|
|
|
|
|
|
#ifndef KCMRULES
|
|
|
|
#include <assert.h>
|
2013-09-10 11:18:55 +00:00
|
|
|
#include <QApplication>
|
2013-09-02 11:14:39 +00:00
|
|
|
#include <QDebug>
|
2007-04-29 17:35:43 +00:00
|
|
|
#include <kkeyserver.h>
|
|
|
|
|
|
|
|
#include <X11/Xlib.h>
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#include "atoms.h"
|
2013-02-19 10:25:46 +00:00
|
|
|
#include "cursor.h"
|
2007-04-29 17:35:43 +00:00
|
|
|
#include "workspace.h"
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
namespace KWin
|
|
|
|
{
|
|
|
|
|
|
|
|
#ifndef KCMRULES
|
|
|
|
|
2009-02-17 15:50:00 +00:00
|
|
|
//************************************
|
|
|
|
// StrutRect
|
|
|
|
//************************************
|
|
|
|
|
2011-01-30 14:34:42 +00:00
|
|
|
StrutRect::StrutRect(QRect rect, StrutArea area)
|
|
|
|
: QRect(rect)
|
|
|
|
, m_area(area)
|
|
|
|
{
|
|
|
|
}
|
2009-02-17 15:50:00 +00:00
|
|
|
|
2011-01-30 14:34:42 +00:00
|
|
|
StrutRect::StrutRect(const StrutRect& other)
|
|
|
|
: QRect(other)
|
|
|
|
, m_area(other.area())
|
|
|
|
{
|
|
|
|
}
|
2009-02-17 15:50:00 +00:00
|
|
|
|
|
|
|
//************************************
|
|
|
|
// Motif
|
|
|
|
//************************************
|
|
|
|
|
2013-05-02 10:49:06 +00:00
|
|
|
void Motif::readFlags(xcb_window_t w, bool& got_noborder, bool& noborder,
|
2011-01-30 14:34:42 +00:00
|
|
|
bool& resize, bool& move, bool& minimize, bool& maximize, bool& close)
|
|
|
|
{
|
2007-04-29 17:35:43 +00:00
|
|
|
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)
|
2007-04-29 17:35:43 +00:00
|
|
|
hints = (MwmHints*) data;
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2009-10-03 14:32:24 +00:00
|
|
|
got_noborder = false;
|
2007-04-29 17:35:43 +00:00
|
|
|
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) {
|
2007-04-29 17:35:43 +00:00
|
|
|
// 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);
|
2007-04-29 17:35:43 +00:00
|
|
|
resize = move = minimize = maximize = close = !set_value;
|
2011-01-30 14:34:42 +00:00
|
|
|
if (hints->functions & MWM_FUNC_RESIZE)
|
2007-04-29 17:35:43 +00:00
|
|
|
resize = set_value;
|
2011-01-30 14:34:42 +00:00
|
|
|
if (hints->functions & MWM_FUNC_MOVE)
|
2007-04-29 17:35:43 +00:00
|
|
|
move = set_value;
|
2011-01-30 14:34:42 +00:00
|
|
|
if (hints->functions & MWM_FUNC_MINIMIZE)
|
2007-04-29 17:35:43 +00:00
|
|
|
minimize = set_value;
|
2011-01-30 14:34:42 +00:00
|
|
|
if (hints->functions & MWM_FUNC_MAXIMIZE)
|
2007-04-29 17:35:43 +00:00
|
|
|
maximize = set_value;
|
2011-01-30 14:34:42 +00:00
|
|
|
if (hints->functions & MWM_FUNC_CLOSE)
|
2007-04-29 17:35:43 +00:00
|
|
|
close = set_value;
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
|
|
|
if (hints->flags & MWM_HINTS_DECORATIONS) {
|
2009-10-03 14:32:24 +00:00
|
|
|
got_noborder = true;
|
|
|
|
noborder = !hints->decorations;
|
2007-04-29 17:35:43 +00:00
|
|
|
}
|
2011-01-30 14:34:42 +00:00
|
|
|
XFree(data);
|
2007-04-29 17:35:43 +00:00
|
|
|
}
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2007-04-29 17:35:43 +00:00
|
|
|
|
2013-01-07 07:07:27 +00:00
|
|
|
#endif
|
|
|
|
|
2013-05-02 09:14:40 +00:00
|
|
|
QByteArray getStringProperty(xcb_window_t w, xcb_atom_t prop, char separator)
|
2011-01-30 14:34:42 +00:00
|
|
|
{
|
2013-05-02 09:14:40 +00:00
|
|
|
const xcb_get_property_cookie_t c = xcb_get_property_unchecked(connection(), false, w, prop,
|
|
|
|
XCB_ATOM_STRING, 0, 10000);
|
|
|
|
ScopedCPointer<xcb_get_property_reply_t> property(xcb_get_property_reply(connection(), c, NULL));
|
|
|
|
if (property.isNull() || property->type == XCB_ATOM_NONE) {
|
|
|
|
return QByteArray();
|
|
|
|
}
|
|
|
|
char *data = static_cast<char*>(xcb_get_property_value(property.data()));
|
|
|
|
if (data && separator) {
|
|
|
|
for (uint32_t i = 0; i < property->value_len; ++i) {
|
|
|
|
if (!data[i] && i + 1 < property->value_len) {
|
|
|
|
data[i] = separator;
|
|
|
|
}
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2007-04-29 17:35:43 +00:00
|
|
|
}
|
2013-05-02 09:14:40 +00:00
|
|
|
return QByteArray(data, property->value_len);
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2007-04-29 17:35:43 +00:00
|
|
|
|
2013-01-07 07:07:27 +00:00
|
|
|
#ifndef KCMRULES
|
2007-04-29 17:35:43 +00:00
|
|
|
/*
|
|
|
|
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
|
|
|
{
|
2013-08-13 08:07:13 +00:00
|
|
|
// 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.
|
|
|
|
QX11Info::setAppTime(QX11Info::getTimestamp());
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2007-04-29 17:35:43 +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
|
|
|
}
|
2007-04-29 17:35:43 +00:00
|
|
|
|
|
|
|
void ungrabXServer()
|
2011-01-30 14:34:42 +00:00
|
|
|
{
|
|
|
|
assert(server_grab_count > 0);
|
|
|
|
if (--server_grab_count == 0) {
|
2013-05-02 07:20:13 +00:00
|
|
|
xcb_ungrab_server(connection());
|
|
|
|
xcb_flush(connection());
|
2007-04-29 17:35:43 +00:00
|
|
|
}
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2007-04-29 17:35:43 +00:00
|
|
|
|
|
|
|
bool grabbedXServer()
|
2011-01-30 14:34:42 +00:00
|
|
|
{
|
2007-04-29 17:35:43 +00:00
|
|
|
return server_grab_count > 0;
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2007-04-29 17:35:43 +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() != NULL)
|
2007-04-29 17:35:43 +00:00
|
|
|
return false;
|
2011-01-30 14:34:42 +00:00
|
|
|
if (keyboard_grabbed)
|
2007-04-29 17:35:43 +00:00
|
|
|
return false;
|
2011-01-30 14:34:42 +00:00
|
|
|
if (qApp->activePopupWidget() != NULL)
|
2007-04-29 17:35:43 +00:00
|
|
|
return false;
|
2013-05-02 07:13:25 +00:00
|
|
|
if (w == XCB_WINDOW_NONE)
|
2007-04-29 17:35:43 +00:00
|
|
|
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, NULL));
|
|
|
|
if (grab.isNull()) {
|
2007-04-29 17:35:43 +00:00
|
|
|
return false;
|
2013-05-02 07:13:25 +00:00
|
|
|
}
|
|
|
|
if (grab->status != XCB_GRAB_STATUS_SUCCESS) {
|
|
|
|
return false;
|
|
|
|
}
|
2007-04-29 17:35:43 +00:00
|
|
|
keyboard_grabbed = true;
|
|
|
|
return true;
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2007-04-29 17:35:43 +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
|
2013-09-02 11:14:39 +00:00
|
|
|
qDebug() << "ungrabXKeyboard() called but keyboard not grabbed!";
|
2007-04-29 17:35:43 +00:00
|
|
|
}
|
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
|
|
|
}
|
2007-04-29 17:35:43 +00:00
|
|
|
|
|
|
|
QPoint cursorPos()
|
2011-01-30 14:34:42 +00:00
|
|
|
{
|
2013-02-19 10:25:46 +00:00
|
|
|
return Cursor::self()->pos();
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2007-04-29 17:35:43 +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)
|
2013-05-02 07:42:39 +00:00
|
|
|
return XCB_BUTTON_INDEX_1;
|
2011-01-30 14:34:42 +00:00
|
|
|
else if (button == Qt::MidButton)
|
2013-05-02 07:42:39 +00:00
|
|
|
return XCB_BUTTON_INDEX_2;
|
2011-01-30 14:34:42 +00:00
|
|
|
else if (button == Qt::RightButton)
|
2013-05-02 07:42:39 +00:00
|
|
|
return XCB_BUTTON_INDEX_3;
|
|
|
|
return XCB_BUTTON_INDEX_ANY; // 0
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2007-04-29 17:35:43 +00:00
|
|
|
|
2011-01-30 14:34:42 +00:00
|
|
|
Qt::MouseButton x11ToQtMouseButton(int button)
|
|
|
|
{
|
2013-05-02 07:42:39 +00:00
|
|
|
if (button == XCB_BUTTON_INDEX_1)
|
2007-04-29 17:35:43 +00:00
|
|
|
return Qt::LeftButton;
|
2013-05-02 07:42:39 +00:00
|
|
|
if (button == XCB_BUTTON_INDEX_2)
|
2007-04-29 17:35:43 +00:00
|
|
|
return Qt::MidButton;
|
2013-05-02 07:42:39 +00:00
|
|
|
if (button == XCB_BUTTON_INDEX_3)
|
2007-04-29 17:35:43 +00:00
|
|
|
return Qt::RightButton;
|
2013-05-02 07:42:39 +00:00
|
|
|
if (button == XCB_BUTTON_INDEX_4)
|
2009-02-06 09:45:34 +00:00
|
|
|
return Qt::XButton1;
|
2013-05-02 07:42:39 +00:00
|
|
|
if (button == XCB_BUTTON_INDEX_5)
|
2009-02-06 09:45:34 +00:00
|
|
|
return Qt::XButton2;
|
2007-04-29 17:35:43 +00:00
|
|
|
return Qt::NoButton;
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2007-04-29 17:35:43 +00:00
|
|
|
|
2011-01-30 14:34:42 +00:00
|
|
|
int qtToX11State(Qt::MouseButtons buttons, Qt::KeyboardModifiers modifiers)
|
|
|
|
{
|
2007-04-29 17:35:43 +00:00
|
|
|
int ret = 0;
|
2011-01-30 14:34:42 +00:00
|
|
|
if (buttons & Qt::LeftButton)
|
2013-05-02 07:42:39 +00:00
|
|
|
ret |= XCB_KEY_BUT_MASK_BUTTON_1;
|
2011-01-30 14:34:42 +00:00
|
|
|
if (buttons & Qt::MidButton)
|
2013-05-02 07:42:39 +00:00
|
|
|
ret |= XCB_KEY_BUT_MASK_BUTTON_2;
|
2011-01-30 14:34:42 +00:00
|
|
|
if (buttons & Qt::RightButton)
|
2013-05-02 07:42:39 +00:00
|
|
|
ret |= XCB_KEY_BUT_MASK_BUTTON_3;
|
2011-01-30 14:34:42 +00:00
|
|
|
if (modifiers & Qt::ShiftModifier)
|
2013-05-02 07:42:39 +00:00
|
|
|
ret |= XCB_KEY_BUT_MASK_SHIFT;
|
2011-01-30 14:34:42 +00:00
|
|
|
if (modifiers & Qt::ControlModifier)
|
2013-05-02 07:42:39 +00:00
|
|
|
ret |= XCB_KEY_BUT_MASK_CONTROL;
|
2011-01-30 14:34:42 +00:00
|
|
|
if (modifiers & Qt::AltModifier)
|
2007-04-29 17:35:43 +00:00
|
|
|
ret |= KKeyServer::modXAlt();
|
2011-01-30 14:34:42 +00:00
|
|
|
if (modifiers & Qt::MetaModifier)
|
2007-04-29 17:35:43 +00:00
|
|
|
ret |= KKeyServer::modXMeta();
|
|
|
|
return ret;
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2007-04-29 17:35:43 +00:00
|
|
|
|
2011-01-30 14:34:42 +00:00
|
|
|
Qt::MouseButtons x11ToQtMouseButtons(int state)
|
|
|
|
{
|
2007-04-29 17:35:43 +00:00
|
|
|
Qt::MouseButtons ret = 0;
|
2013-05-02 07:42:39 +00:00
|
|
|
if (state & XCB_KEY_BUT_MASK_BUTTON_1)
|
2007-04-29 17:35:43 +00:00
|
|
|
ret |= Qt::LeftButton;
|
2013-05-02 07:42:39 +00:00
|
|
|
if (state & XCB_KEY_BUT_MASK_BUTTON_2)
|
2007-04-29 17:35:43 +00:00
|
|
|
ret |= Qt::MidButton;
|
2013-05-02 07:42:39 +00:00
|
|
|
if (state & XCB_KEY_BUT_MASK_BUTTON_3)
|
2007-04-29 17:35:43 +00:00
|
|
|
ret |= Qt::RightButton;
|
2013-05-02 07:42:39 +00:00
|
|
|
if (state & XCB_KEY_BUT_MASK_BUTTON_4)
|
2009-02-06 09:45:34 +00:00
|
|
|
ret |= Qt::XButton1;
|
2013-05-02 07:42:39 +00:00
|
|
|
if (state & XCB_KEY_BUT_MASK_BUTTON_5)
|
2009-02-06 09:45:34 +00:00
|
|
|
ret |= Qt::XButton2;
|
2007-04-29 17:35:43 +00:00
|
|
|
return ret;
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2007-04-29 17:35:43 +00:00
|
|
|
|
2011-01-30 14:34:42 +00:00
|
|
|
Qt::KeyboardModifiers x11ToQtKeyboardModifiers(int state)
|
|
|
|
{
|
2007-04-29 17:35:43 +00:00
|
|
|
Qt::KeyboardModifiers ret = 0;
|
2013-05-02 07:42:39 +00:00
|
|
|
if (state & XCB_KEY_BUT_MASK_SHIFT)
|
2007-04-29 17:35:43 +00:00
|
|
|
ret |= Qt::ShiftModifier;
|
2013-05-02 07:42:39 +00:00
|
|
|
if (state & XCB_KEY_BUT_MASK_CONTROL)
|
2007-04-29 17:35:43 +00:00
|
|
|
ret |= Qt::ControlModifier;
|
2011-01-30 14:34:42 +00:00
|
|
|
if (state & KKeyServer::modXAlt())
|
2007-04-29 17:35:43 +00:00
|
|
|
ret |= Qt::AltModifier;
|
2011-01-30 14:34:42 +00:00
|
|
|
if (state & KKeyServer::modXMeta())
|
2007-04-29 17:35:43 +00:00
|
|
|
ret |= Qt::MetaModifier;
|
|
|
|
return ret;
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2007-04-29 17:35:43 +00:00
|
|
|
|
|
|
|
#endif
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
#ifndef KCMRULES
|
|
|
|
#include "utils.moc"
|
|
|
|
#endif
|