File indexing completed on 2024-11-10 04:57:21

0001 /*
0002     KWin - the KDE window manager
0003     This file is part of the KDE project.
0004 
0005     SPDX-FileCopyrightText: 2017 Martin Flöser <mgraesslin@kde.org>
0006 
0007     SPDX-License-Identifier: GPL-2.0-or-later
0008 */
0009 #include "x11_filter.h"
0010 
0011 #include "effect/effecthandler.h"
0012 #include "screenedge.h"
0013 #include "tabbox/tabbox.h"
0014 #include "utils/xcbutils.h"
0015 #include "workspace.h"
0016 
0017 #include <KKeyServer>
0018 
0019 #include <xcb/xcb.h>
0020 
0021 namespace KWin
0022 {
0023 namespace TabBox
0024 {
0025 
0026 X11Filter::X11Filter()
0027     : X11EventFilter(QList<int>{XCB_KEY_PRESS, XCB_KEY_RELEASE, XCB_MOTION_NOTIFY, XCB_BUTTON_PRESS, XCB_BUTTON_RELEASE})
0028 {
0029 }
0030 
0031 bool X11Filter::event(xcb_generic_event_t *event)
0032 {
0033     const auto tab = workspace()->tabbox();
0034     if (!tab->isGrabbed()) {
0035         return false;
0036     }
0037     const uint8_t eventType = event->response_type & ~0x80;
0038     switch (eventType) {
0039     case XCB_BUTTON_PRESS:
0040     case XCB_BUTTON_RELEASE: {
0041         auto e = reinterpret_cast<xcb_button_press_event_t *>(event);
0042         xcb_allow_events(connection(), XCB_ALLOW_ASYNC_POINTER, XCB_CURRENT_TIME);
0043         if (!tab->isShown() && tab->isDisplayed()) {
0044             if (effects && effects->isMouseInterception()) {
0045                 // pass on to effects, effects will filter out the event
0046                 return false;
0047             }
0048         }
0049         if (eventType == XCB_BUTTON_PRESS) {
0050             return buttonPress(e);
0051         }
0052         return false;
0053     }
0054     case XCB_MOTION_NOTIFY: {
0055         motion(event);
0056         break;
0057     }
0058     case XCB_KEY_PRESS: {
0059         keyPress(event);
0060         return true;
0061     }
0062     case XCB_KEY_RELEASE:
0063         keyRelease(event);
0064         return true;
0065     }
0066     return false;
0067 }
0068 bool X11Filter::buttonPress(xcb_button_press_event_t *event)
0069 {
0070     // press outside Tabbox?
0071     const auto tab = workspace()->tabbox();
0072     QPoint pos(event->root_x, event->root_y);
0073     if ((!tab->isShown() && tab->isDisplayed())
0074         || (!tabBox->containsPos(pos) && (event->detail == XCB_BUTTON_INDEX_1 || event->detail == XCB_BUTTON_INDEX_2 || event->detail == XCB_BUTTON_INDEX_3))) {
0075         tab->close(); // click outside closes tab
0076         return true;
0077     }
0078     if (event->detail == XCB_BUTTON_INDEX_5 || event->detail == XCB_BUTTON_INDEX_4) {
0079         // mouse wheel event
0080         const QModelIndex index = tabBox->nextPrev(event->detail == XCB_BUTTON_INDEX_5);
0081         if (index.isValid()) {
0082             tab->setCurrentIndex(index);
0083         }
0084         return true;
0085     }
0086     return false;
0087 }
0088 
0089 void X11Filter::motion(xcb_generic_event_t *event)
0090 {
0091     auto *mouseEvent = reinterpret_cast<xcb_motion_notify_event_t *>(event);
0092     const QPoint rootPos(mouseEvent->root_x, mouseEvent->root_y);
0093     // TODO: this should be in ScreenEdges directly
0094     workspace()->screenEdges()->check(rootPos, QDateTime::fromMSecsSinceEpoch(xTime(), Qt::UTC), true);
0095     xcb_allow_events(connection(), XCB_ALLOW_ASYNC_POINTER, XCB_CURRENT_TIME);
0096 }
0097 
0098 void X11Filter::keyPress(xcb_generic_event_t *event)
0099 {
0100     int keyQt;
0101     xcb_key_press_event_t *keyEvent = reinterpret_cast<xcb_key_press_event_t *>(event);
0102     KKeyServer::xcbKeyPressEventToQt(keyEvent, &keyQt);
0103     workspace()->tabbox()->keyPress(keyQt);
0104 }
0105 
0106 void X11Filter::keyRelease(xcb_generic_event_t *event)
0107 {
0108     const auto ev = reinterpret_cast<xcb_key_release_event_t *>(event);
0109     unsigned int mk = ev->state & (KKeyServer::modXShift() | KKeyServer::modXCtrl() | KKeyServer::modXAlt() | KKeyServer::modXMeta());
0110     // ev.state is state before the key release, so just checking mk being 0 isn't enough
0111     // using XQueryPointer() also doesn't seem to work well, so the check that all
0112     // modifiers are released: only one modifier is active and the currently released
0113     // key is this modifier - if yes, release the grab
0114     int mod_index = -1;
0115     for (int i = XCB_MAP_INDEX_SHIFT;
0116          i <= XCB_MAP_INDEX_5;
0117          ++i) {
0118         if ((mk & (1 << i)) != 0) {
0119             if (mod_index >= 0) {
0120                 return;
0121             }
0122             mod_index = i;
0123         }
0124     }
0125     bool release = false;
0126     if (mod_index == -1) {
0127         release = true;
0128     } else {
0129         Xcb::ModifierMapping xmk;
0130         if (xmk) {
0131             xcb_keycode_t *keycodes = xmk.keycodes();
0132             const int maxIndex = xmk.size();
0133             for (int i = 0; i < xmk->keycodes_per_modifier; ++i) {
0134                 const int index = xmk->keycodes_per_modifier * mod_index + i;
0135                 if (index >= maxIndex) {
0136                     continue;
0137                 }
0138                 if (keycodes[index] == ev->detail) {
0139                     release = true;
0140                 }
0141             }
0142         }
0143     }
0144     if (release) {
0145         workspace()->tabbox()->modifiersReleased();
0146     }
0147 }
0148 
0149 }
0150 }