Skip to content
This repository has been archived by the owner on Apr 29, 2023. It is now read-only.

Commit

Permalink
Support interactive window selection through the Platform
Browse files Browse the repository at this point in the history
Summary:
A new virtual method is added to Platform:
startInteractiveWindowSelection

The interactive window selection enters a mode where the user can select
a window through the pointer or keyboard device. The cursor is turned
into a crosshair cursor, unless another cursor name is provided (e.g.
pirate for kill window).

Once a window is selected the provided callback method is invoked with
the selected Toplevel as argument. In case the user cancelled the
selection a nullptr argument is passed in.

Currently it's only implemented by the X11 standalone platform using the
logic from KillWindow. Just instead of killing the window the callback
is invoked.

KillWindow loses the X11 implementation and interacts with the new
functionality in Platform by providing a lambda function for the
killing.

Test Plan: Killing of X11 windows is still possible

Reviewers: #kwin, #plasma

Subscribers: plasma-devel, kwin

Tags: #kwin

Differential Revision: https://phabricator.kde.org/D3363
  • Loading branch information
mgraesslin committed Nov 17, 2016
1 parent 327ccff commit 7b464cf
Show file tree
Hide file tree
Showing 10 changed files with 357 additions and 194 deletions.
185 changes: 15 additions & 170 deletions killwindow.cpp
Expand Up @@ -21,31 +21,14 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
*********************************************************************/
#include "killwindow.h"
#include "client.h"
#include "cursor.h"
#include "workspace.h"
#include "xcbutils.h"
// XLib
#include <X11/cursorfont.h>
#include <X11/Xutil.h>
#include <fixx11h.h>
// XCB
#include <xcb/xcb_keysyms.h>
#include "main.h"
#include "platform.h"
#include "unmanaged.h"

namespace KWin
{

KillWindow::KillWindow()
: X11EventFilter(QVector<int>{XCB_BUTTON_PRESS,
XCB_BUTTON_RELEASE,
XCB_MOTION_NOTIFY,
XCB_ENTER_NOTIFY,
XCB_LEAVE_NOTIFY,
XCB_KEY_PRESS,
XCB_KEY_RELEASE,
XCB_FOCUS_IN,
XCB_FOCUS_OUT
})
, m_active(false)
{
}

Expand All @@ -55,156 +38,18 @@ KillWindow::~KillWindow()

void KillWindow::start()
{
static xcb_cursor_t kill_cursor = XCB_CURSOR_NONE;
if (kill_cursor == XCB_CURSOR_NONE) {
kill_cursor = createCursor();
}
if (m_active) {
return;
}

xcb_connection_t *c = connection();
ScopedCPointer<xcb_grab_pointer_reply_t> grabPointer(xcb_grab_pointer_reply(c, xcb_grab_pointer_unchecked(c, false, rootWindow(),
XCB_EVENT_MASK_BUTTON_PRESS | XCB_EVENT_MASK_BUTTON_RELEASE |
XCB_EVENT_MASK_POINTER_MOTION |
XCB_EVENT_MASK_ENTER_WINDOW | XCB_EVENT_MASK_LEAVE_WINDOW,
XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC, XCB_WINDOW_NONE,
kill_cursor, XCB_TIME_CURRENT_TIME), NULL));
if (grabPointer.isNull() || grabPointer->status != XCB_GRAB_STATUS_SUCCESS) {
return;
}
m_active = grabXKeyboard();
if (!m_active) {
xcb_ungrab_pointer(connection(), XCB_TIME_CURRENT_TIME);
return;
}
grabXServer();
}

xcb_cursor_t KillWindow::createCursor()
{
xcb_cursor_t cursor = Cursor::x11Cursor(QByteArrayLiteral("pirate"));
if (cursor != XCB_CURSOR_NONE) {
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");
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;
}

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);
}
}

bool KillWindow::event(xcb_generic_event_t *event)
{
if (!m_active) {
return false;
}
processEvent(event);

return true;
}

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) {
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;
}
Cursor::setPos(Cursor::pos() + QPoint(mx, my));
if (returnPressed) {
performKill();
}
if (returnPressed || escapePressed) {
release();
}
xcb_key_symbols_free(symbols);
}

void KillWindow::performKill()
{
Xcb::Pointer pointer(rootWindow());
if (!pointer.isNull() && pointer->child != XCB_WINDOW_NONE) {
killWindowId(pointer->child);
}
}

void KillWindow::release()
{
ungrabXKeyboard();
xcb_ungrab_pointer(connection(), XCB_TIME_CURRENT_TIME);
ungrabXServer();
m_active = false;
}

void KillWindow::killWindowId(xcb_window_t window_to_kill)
{
if (window_to_kill == XCB_WINDOW_NONE)
return;
xcb_window_t window = window_to_kill;
Client* client = NULL;
while (true) {
client = Workspace::self()->findClient(Predicate::FrameIdMatch, window);
if (client) {
break; // Found the client
}
Xcb::Tree tree(window);
if (window == tree->root) {
// We didn't find the client, probably an override-redirect window
break;
}
window = tree->parent; // Go up
}
if (client)
client->killWindow();
else
xcb_kill_client(connection(), window_to_kill);
kwinApp()->platform()->startInteractiveWindowSelection(
[] (KWin::Toplevel *t) {
if (!t) {
return;
}
if (Client *c = qobject_cast<Client*>(t)) {
c->killWindow();
} else if (Unmanaged *u = qobject_cast<Unmanaged*>(t)) {
xcb_kill_client(connection(), u->window());
}
}, QByteArrayLiteral("pirate")
);
}

} // namespace
21 changes: 1 addition & 20 deletions killwindow.h
Expand Up @@ -23,36 +23,17 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#ifndef KWIN_KILLWINDOW_H
#define KWIN_KILLWINDOW_H

#include "x11eventfilter.h"

#include <xcb/xcb.h>

namespace KWin
{

class KillWindow : public X11EventFilter
class KillWindow
{
public:

KillWindow();
~KillWindow();

void start();
bool isActive() const {
return m_active;
}
void processEvent(xcb_generic_event_t *event);

bool event(xcb_generic_event_t *event) override;

private:
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);
void killWindowId(xcb_window_t window_to_kill);
bool m_active;
};

} // namespace
Expand Down
7 changes: 7 additions & 0 deletions platform.cpp
Expand Up @@ -363,4 +363,11 @@ void Platform::createOpenGLSafePoint(OpenGLSafePoint safePoint)
Q_UNUSED(safePoint)
}

void Platform::startInteractiveWindowSelection(std::function<void(KWin::Toplevel*)> callback, const QByteArray &cursorName)
{
// TODO: provide a basic implementation for Wayland platforms
Q_UNUSED(cursorName)
callback(nullptr);
}

}
22 changes: 22 additions & 0 deletions platform.h
Expand Up @@ -26,6 +26,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#include <QImage>
#include <QObject>

#include <functional>

namespace KWayland {
namespace Server {
class OutputConfigurationInterface;
Expand All @@ -40,6 +42,7 @@ class OpenGLBackend;
class QPainterBackend;
class Screens;
class ScreenEdges;
class Toplevel;
class WaylandCursorTheme;

class KWIN_EXPORT Platform : public QObject
Expand Down Expand Up @@ -158,6 +161,25 @@ class KWIN_EXPORT Platform : public QObject
**/
virtual void createOpenGLSafePoint(OpenGLSafePoint safePoint);

/**
* Starts an interactive window selection process.
*
* Once the user selected a window the @p callback is invoked with the selected Toplevel as
* argument. In case the user cancels the interactive window selection or selecting a window is currently
* not possible (e.g. screen locked) the @p callback is invoked with a @c nullptr argument.
*
* During the interactive window selection the cursor is turned into a crosshair cursor unless
* @p cursorName is provided. The argument @p cursorName is a QByteArray instead of Qt::CursorShape
* to support the "pirate" cursor for kill window which is not wrapped by Qt::CursorShape.
*
* The default implementation does not support selecting windows yet and only invokes the
* @p callback with @c nullptr.
*
* @param callback The function to invoke once the interactive window selection ends
* @param cursorName The optional name of the cursor shape to use, default is crosshair
**/
virtual void startInteractiveWindowSelection(std::function<void(KWin::Toplevel*)> callback, const QByteArray &cursorName = QByteArray());

bool usesSoftwareCursor() const {
return m_softWareCursor;
}
Expand Down
1 change: 1 addition & 0 deletions plugins/platforms/x11/standalone/CMakeLists.txt
Expand Up @@ -4,6 +4,7 @@ set(X11PLATFORM_SOURCES
x11cursor.cpp
x11_platform.cpp
screens_xrandr.cpp
windowselector.cpp
)

if(X11_Xinput_FOUND)
Expand Down

0 comments on commit 7b464cf

Please sign in to comment.