Rewrite KillWindow on top of XCB

The code is rewritten in a way to replace the local event loop with event
filtering done in the normal way through events.cpp. Therefore instead of
creating a KillWindow whenever it's needed, there is one available in
Workspace which will be reused on next invocation.

The responsible events are passed from events.cpp to KillWindow for
processing.

In order to port the keycode to symbol to XCB, KWin now finds
xcb_keysyms and links it.

To get the right cursor KWin now links the XCursor library which is
unfortunatelly an XLib based library, but there seems to be no XCB
replacement.
This commit is contained in:
Martin Gräßlin 2012-12-27 14:26:46 +01:00
parent b8c6831a1f
commit 9655c7b3cb
6 changed files with 184 additions and 77 deletions

View file

@ -184,7 +184,7 @@ qt4_add_dbus_interface( kwin_KDEINIT_SRCS
qt4_add_resources( kwin_KDEINIT_SRCS resources.qrc ) qt4_add_resources( kwin_KDEINIT_SRCS resources.qrc )
set(kwinLibs ${KDE4_KDEUI_LIBS} ${KDE4_PLASMA_LIBS} ${QT_QTDECLARATIVE_LIBRARY} ${KDECLARATIVE_LIBRARIES} kdecorations kwineffects ${X11_LIBRARIES} ${X11_Xrandr_LIB} ${X11_Xdamage_LIB} ${X11_Xrender_LIB} ${X11_Xfixes_LIB} ${XCB_XCB_LIBRARIES} ${X11_XCB_LIBRARIES} ${XCB_XFIXES_LIBRARIES} ${XCB_DAMAGE_LIBRARIES} ${XCB_COMPOSITE_LIBRARIES} ${XCB_SHAPE_LIBRARIES} ${XCB_SYNC_LIBRARIES} ${XCB_RENDER_LIBRARIES} ${XCB_RANDR_LIBRARIES}) set(kwinLibs ${KDE4_KDEUI_LIBS} ${KDE4_PLASMA_LIBS} ${QT_QTDECLARATIVE_LIBRARY} ${KDECLARATIVE_LIBRARIES} kdecorations kwineffects ${X11_LIBRARIES} ${X11_Xcursor_LIB} ${X11_Xrandr_LIB} ${X11_Xdamage_LIB} ${X11_Xrender_LIB} ${X11_Xfixes_LIB} ${XCB_XCB_LIBRARIES} ${X11_XCB_LIBRARIES} ${XCB_XFIXES_LIBRARIES} ${XCB_DAMAGE_LIBRARIES} ${XCB_COMPOSITE_LIBRARIES} ${XCB_SHAPE_LIBRARIES} ${XCB_SYNC_LIBRARIES} ${XCB_RENDER_LIBRARIES} ${XCB_RANDR_LIBRARIES} ${XCB_KEYSYMS_LIBRARIES})
find_library(XF86VM_LIBRARY Xxf86vm) find_library(XF86VM_LIBRARY Xxf86vm)
if (XF86VM_LIBRARY) if (XF86VM_LIBRARY)

View file

@ -57,6 +57,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#include <QX11Info> #include <QX11Info>
#include "composite.h" #include "composite.h"
#include "killwindow.h"
namespace KWin namespace KWin
{ {
@ -233,6 +234,12 @@ bool Workspace::workspaceEvent(XEvent * e)
&& (e->type == KeyPress || e->type == KeyRelease)) && (e->type == KeyPress || e->type == KeyRelease))
return false; // let Qt process it, it'll be intercepted again in eventFilter() return false; // let Qt process it, it'll be intercepted again in eventFilter()
if (!m_windowKiller.isNull() && m_windowKiller->isActive() && m_windowKiller->isResponsibleForEvent(e->type)) {
m_windowKiller->processEvent(e);
// filter out the event
return true;
}
if (e->type == PropertyNotify || e->type == ClientMessage) { if (e->type == PropertyNotify || e->type == ClientMessage) {
unsigned long dirty[ NETRootInfo::PROPERTIES_SIZE ]; unsigned long dirty[ NETRootInfo::PROPERTIES_SIZE ];
rootInfo->event(e, dirty, NETRootInfo::PROPERTIES_SIZE); rootInfo->event(e, dirty, NETRootInfo::PROPERTIES_SIZE);

View file

@ -4,6 +4,7 @@
Copyright (C) 1999, 2000 Matthias Ettrich <ettrich@kde.org> Copyright (C) 1999, 2000 Matthias Ettrich <ettrich@kde.org>
Copyright (C) 2003 Lubos Lunak <l.lunak@kde.org> Copyright (C) 2003 Lubos Lunak <l.lunak@kde.org>
Copyright (C) 2012 Martin Gräßlin <mgraesslin@kde.org>
This program is free software; you can redistribute it and/or modify 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 it under the terms of the GNU General Public License as published by
@ -18,23 +19,21 @@ GNU General Public License for more details.
You should have received a copy of the GNU General Public License You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
*********************************************************************/ *********************************************************************/
//#ifndef QT_CLEAN_NAMESPACE
//#define QT_CLEAN_NAMESPACE
//#endif
#include "killwindow.h" #include "killwindow.h"
#include "workspace.h"
// Qt
#include <QCursor> #include <QCursor>
#include <X11/Xlib.h> // XLib
#include <X11/keysym.h>
#include <X11/keysymdef.h>
#include <X11/cursorfont.h> #include <X11/cursorfont.h>
#include <QX11Info> #include <X11/Xcursor/Xcursor.h>
// XCB
#include <xcb/xcb_keysyms.h>
namespace KWin namespace KWin
{ {
KillWindow::KillWindow(Workspace* ws) KillWindow::KillWindow()
: workspace(ws) : m_active(false)
{ {
} }
@ -44,71 +43,152 @@ KillWindow::~KillWindow()
void KillWindow::start() void KillWindow::start()
{ {
static Cursor kill_cursor = 0; static xcb_cursor_t kill_cursor = XCB_CURSOR_NONE;
if (!kill_cursor) if (kill_cursor == XCB_CURSOR_NONE) {
kill_cursor = XCreateFontCursor(display(), XC_pirate); kill_cursor = createCursor();
}
if (m_active) {
return;
}
m_active = true;
if (XGrabPointer(display(), rootWindow(), False, xcb_connection_t *c = connection();
ButtonPressMask | ButtonReleaseMask | ScopedCPointer<xcb_grab_pointer_reply_t> grabPointer(xcb_grab_pointer_reply(c, xcb_grab_pointer_unchecked(c, false, rootWindow(),
PointerMotionMask | XCB_EVENT_MASK_BUTTON_PRESS | XCB_EVENT_MASK_BUTTON_RELEASE |
EnterWindowMask | LeaveWindowMask, XCB_EVENT_MASK_POINTER_MOTION |
GrabModeAsync, GrabModeAsync, None, XCB_EVENT_MASK_ENTER_WINDOW | XCB_EVENT_MASK_LEAVE_WINDOW,
kill_cursor, CurrentTime) == GrabSuccess) { XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC, XCB_WINDOW_NONE,
grabXKeyboard(); kill_cursor, XCB_TIME_CURRENT_TIME), NULL));
if (grabPointer.isNull() || grabPointer->status != XCB_GRAB_STATUS_SUCCESS) {
return;
}
grabXKeyboard();
grabXServer();
}
XEvent ev; xcb_cursor_t KillWindow::createCursor()
int return_pressed = 0; {
int escape_pressed = 0; // XCursor is an XLib only lib
int button_released = 0; const char *theme = XcursorGetTheme(display());
const int size = XcursorGetDefaultSize(display());
XcursorImage *ximg = XcursorLibraryLoadImage("pirate", theme, size);
if (ximg) {
xcb_cursor_t cursor = XcursorImageLoadCursor(display(), ximg);
XcursorImageDestroy(ximg);
return cursor;
}
// fallback on font
xcb_connection_t *c = connection();
const xcb_font_t cursorFont = xcb_generate_id(c);
xcb_open_font(c, cursorFont, strlen ("cursor"), "cursor");
xcb_cursor_t cursor = xcb_generate_id(c);
xcb_create_glyph_cursor(c, cursor, cursorFont, cursorFont,
XC_pirate, /* source character glyph */
XC_pirate + 1, /* mask character glyph */
0, 0, 0, 0, 0, 0); /* r b g r b g */
return cursor;
}
grabXServer(); bool KillWindow::isResponsibleForEvent(int eventType) const
{
while (!return_pressed && !escape_pressed && !button_released) { switch (eventType) {
XMaskEvent(display(), KeyPressMask | ButtonPressMask | case XCB_BUTTON_PRESS:
ButtonReleaseMask | PointerMotionMask, &ev); case XCB_BUTTON_RELEASE:
case XCB_MOTION_NOTIFY:
if (ev.type == KeyPress) { case XCB_ENTER_NOTIFY:
int kc = XKeycodeToKeysym(display(), ev.xkey.keycode, 0); case XCB_LEAVE_NOTIFY:
int mx = 0; case XCB_KEY_PRESS:
int my = 0; case XCB_KEY_RELEASE:
return_pressed = (kc == XK_Return) || (kc == XK_space); case XCB_FOCUS_IN:
escape_pressed = (kc == XK_Escape); case XCB_FOCUS_OUT:
if (kc == XK_Left) mx = -10; return true;
if (kc == XK_Right) mx = 10; default:
if (kc == XK_Up) my = -10; return false;
if (kc == XK_Down) my = 10;
if (ev.xkey.state & ControlMask) {
mx /= 10;
my /= 10;
}
QCursor::setPos(cursorPos() + QPoint(mx, my));
}
if (ev.type == ButtonRelease) {
button_released = (ev.xbutton.button == Button1);
if (ev.xbutton.button == Button3) {
escape_pressed = true;
break;
}
if (ev.xbutton.button == Button1 || ev.xbutton.button == Button2)
workspace->killWindowId(ev.xbutton.subwindow);
}
continue;
}
if (return_pressed) {
Window root, child;
int dummy1, dummy2, dummy3, dummy4;
unsigned int dummy5;
if (XQueryPointer(display(), rootWindow(), &root, &child,
&dummy1, &dummy2, &dummy3, &dummy4, &dummy5) == true
&& child != None)
workspace->killWindowId(child);
}
ungrabXServer();
ungrabXKeyboard();
XUngrabPointer(display(), CurrentTime);
} }
} }
void KillWindow::processEvent(XEvent *event)
{
if (event->type == XCB_BUTTON_RELEASE) {
handleButtonRelease(event->xbutton.button, event->xbutton.subwindow);
} else if (event->type == XCB_KEY_PRESS) {
handleKeyPress(event->xkey.keycode, event->xkey.state);
}
}
void KillWindow::processEvent(xcb_generic_event_t *event)
{
if (event->response_type == XCB_BUTTON_RELEASE) {
xcb_button_release_event_t *buttonEvent = reinterpret_cast<xcb_button_release_event_t*>(event);
handleButtonRelease(buttonEvent->detail, buttonEvent->child);
} else if (event->response_type == XCB_KEY_PRESS) {
xcb_key_press_event_t *keyEvent = reinterpret_cast<xcb_key_press_event_t*>(event);
handleKeyPress(keyEvent->detail, keyEvent->state);
}
}
void KillWindow::handleButtonRelease(xcb_button_t button, xcb_window_t window)
{
if (button == XCB_BUTTON_INDEX_3) {
release();
return;
}
if (button == XCB_BUTTON_INDEX_1 || button == XCB_BUTTON_INDEX_2) {
Workspace::self()->killWindowId(window);
release();
return;
}
}
void KillWindow::handleKeyPress(xcb_keycode_t keycode, uint16_t state)
{
xcb_key_symbols_t *symbols = xcb_key_symbols_alloc(connection());
xcb_keysym_t kc = xcb_key_symbols_get_keysym(symbols, keycode, 0);
int mx = 0;
int my = 0;
const bool returnPressed = (kc == XK_Return) || (kc == XK_space);
const bool escapePressed = (kc == XK_Escape);
if (kc == XK_Left) {
mx = -10;
}
if (kc == XK_Right) {
mx = 10;
}
if (kc == XK_Up) {
my = -10;
}
if (kc == XK_Down) {
my = 10;
}
if (state & XCB_MOD_MASK_CONTROL) {
mx /= 10;
my /= 10;
}
QCursor::setPos(cursorPos() + QPoint(mx, my));
if (returnPressed) {
performKill();
}
if (returnPressed || escapePressed) {
release();
}
xcb_key_symbols_free(symbols);
}
void KillWindow::performKill()
{
xcb_connection_t *c = connection();
ScopedCPointer<xcb_query_pointer_reply_t> pointer(xcb_query_pointer_reply(c, xcb_query_pointer_unchecked(c, rootWindow()), NULL));
if (!pointer.isNull() && pointer->child != XCB_WINDOW_NONE) {
Workspace::self()->killWindowId(pointer->child);
}
}
void KillWindow::release()
{
ungrabXKeyboard();
xcb_ungrab_pointer(connection(), XCB_TIME_CURRENT_TIME);
ungrabXServer();
m_active = false;
}
} // namespace } // namespace

View file

@ -4,6 +4,7 @@
Copyright (C) 1999, 2000 Matthias Ettrich <ettrich@kde.org> Copyright (C) 1999, 2000 Matthias Ettrich <ettrich@kde.org>
Copyright (C) 2003 Lubos Lunak <l.lunak@kde.org> Copyright (C) 2003 Lubos Lunak <l.lunak@kde.org>
Copyright (C) 2012 Martin Gräßlin <mgraesslin@kde.org>
This program is free software; you can redistribute it and/or modify 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 it under the terms of the GNU General Public License as published by
@ -22,7 +23,9 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#ifndef KWIN_KILLWINDOW_H #ifndef KWIN_KILLWINDOW_H
#define KWIN_KILLWINDOW_H #define KWIN_KILLWINDOW_H
#include "workspace.h" #include <xcb/xcb.h>
typedef union _XEvent XEvent;
namespace KWin namespace KWin
{ {
@ -31,13 +34,25 @@ class KillWindow
{ {
public: public:
KillWindow(Workspace* ws); KillWindow();
~KillWindow(); ~KillWindow();
void start(); void start();
bool isActive() const {
return m_active;
}
bool isResponsibleForEvent(int eventType) const;
// TODO: remove once event handling is ported to XCB
void processEvent(XEvent *event);
void processEvent(xcb_generic_event_t *event);
private: private:
Workspace* workspace; xcb_cursor_t createCursor();
void release();
void performKill();
void handleKeyPress(xcb_keycode_t keycode, uint16_t state);
void handleButtonRelease(xcb_button_t button, xcb_window_t window);
bool m_active;
}; };
} // namespace } // namespace

View file

@ -1562,8 +1562,10 @@ void Workspace::slotUntab()
*/ */
void Workspace::slotKillWindow() void Workspace::slotKillWindow()
{ {
KillWindow kill(this); if (m_windowKiller.isNull()) {
kill.start(); m_windowKiller.reset(new KillWindow());
}
m_windowKiller->start();
} }
/*! /*!

View file

@ -42,6 +42,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#include "kdecoration.h" #include "kdecoration.h"
#include "kdecorationfactory.h" #include "kdecorationfactory.h"
#include "sm.h" #include "sm.h"
#include "killwindow.h"
#include <X11/Xlib.h> #include <X11/Xlib.h>
@ -726,6 +727,8 @@ private:
Scripting *m_scripting; Scripting *m_scripting;
QScopedPointer<KillWindow> m_windowKiller;
private: private:
friend bool performTransiencyCheck(); friend bool performTransiencyCheck();
}; };