From e763abe91f0eb3d6e4d71c1e5088800ff49adfa1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Gr=C3=A4=C3=9Flin?= Date: Wed, 6 Jul 2016 13:32:38 +0200 Subject: [PATCH] [tests] Tool to read the X11 shadow on a window Summary: This new helper tool allows to read the X11 shadow from a window and visualize the individual parts. Reviewers: #kwin Subscribers: kwin Tags: #kwin Differential Revision: https://phabricator.kde.org/D2101 --- tests/CMakeLists.txt | 3 + tests/x11shadowreader.cpp | 129 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 132 insertions(+) create mode 100644 tests/x11shadowreader.cpp diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 353514f9cb..f6f915850d 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -33,3 +33,6 @@ if (HAVE_INPUT) add_definitions(-DKWIN_BUILD_TESTING) target_link_libraries(libinputtest Qt5::Core Qt5::DBus Libinput::Libinput ${UDEV_LIBS} KF5::ConfigCore KF5::GlobalAccel KF5::WindowSystem) endif() + +add_executable(x11shadowreader x11shadowreader.cpp) +target_link_libraries(x11shadowreader XCB::XCB Qt5::Widgets Qt5::X11Extras KF5::ConfigCore KF5::WindowSystem) diff --git a/tests/x11shadowreader.cpp b/tests/x11shadowreader.cpp new file mode 100644 index 0000000000..7af037f3ea --- /dev/null +++ b/tests/x11shadowreader.cpp @@ -0,0 +1,129 @@ +/* + * Copyright 2016 Martin Gräßlin + * + * 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) version 3 or any later version + * accepted by the membership of KDE e.V. (or its successor approved + * by the membership of KDE e.V.), which shall act as a proxy + * defined in Section 14 of version 3 of the license. + * + * 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 . +*/ +#include "../xcbutils.h" +#include +#include +#include +#include +#include +#include +#include + +static QVector readShadow(quint32 windowId) +{ + KWin::Xcb::Atom atom(QByteArrayLiteral("_KDE_NET_WM_SHADOW"), false, QX11Info::connection()); + QVector ret; + if (windowId != XCB_WINDOW) { + KWin::Xcb::Property property(false, windowId, atom, XCB_ATOM_CARDINAL, 0, 12); + uint32_t *shadow = property.value(); + if (shadow) { + ret.reserve(12); + for (int i=0; i<12; ++i) { + ret << shadow[i]; + } + } else { + qDebug() << "!!!! no shadow"; + } + } else { + qDebug() << "!!!! no window"; + } + return ret; +} + +static QVector getPixmaps(const QVector &data) +{ + QVector ret; + static const int ShadowElementsCount = 8; + QVector pixmapGeometries(ShadowElementsCount); + QVector getImageCookies(ShadowElementsCount); + auto *c = KWin::connection(); + for (int i = 0; i < ShadowElementsCount; ++i) { + pixmapGeometries[i] = KWin::Xcb::WindowGeometry(data[i]); + } + auto discardReplies = [&getImageCookies](int start) { + for (int i = start; i < getImageCookies.size(); ++i) { + xcb_discard_reply(KWin::connection(), getImageCookies.at(i).sequence); + } + }; + for (int i = 0; i < ShadowElementsCount; ++i) { + auto &geo = pixmapGeometries[i]; + if (geo.isNull()) { + discardReplies(0); + return QVector(); + } + getImageCookies[i] = xcb_get_image_unchecked(c, XCB_IMAGE_FORMAT_Z_PIXMAP, data[i], + 0, 0, geo->width, geo->height, ~0); + } + for (int i = 0; i < ShadowElementsCount; ++i) { + auto *reply = xcb_get_image_reply(c, getImageCookies.at(i), nullptr); + if (!reply) { + discardReplies(i+1); + return QVector(); + } + auto &geo = pixmapGeometries[i]; + QImage image(xcb_get_image_data(reply), geo->width, geo->height, QImage::Format_ARGB32); + ret << QPixmap::fromImage(image); + free(reply); + } + return ret; +} + +int main(int argc, char **argv) +{ + qputenv("QT_QPA_PLATFORM", "xcb"); + QApplication app(argc, argv); + app.setProperty("x11Connection", QVariant::fromValue(QX11Info::connection())); + + QCommandLineParser parser; + parser.addPositionalArgument(QStringLiteral("windowId"), QStringLiteral("The X11 windowId from which to read the shadow")); + parser.addHelpOption(); + parser.process(app); + + if (parser.positionalArguments().count() != 1) { + parser.showHelp(1); + } + + bool ok = false; + const auto shadow = readShadow(parser.positionalArguments().first().toULongLong(&ok, 16)); + if (!ok) { + qDebug() << "!!! Failed to read window id"; + return 1; + } + if (shadow.isEmpty()) { + qDebug() << "!!!! Read shadow failed"; + return 1; + } + const auto pixmaps = getPixmaps(shadow); + if (pixmaps.isEmpty()) { + qDebug() << "!!!! Read pixmap failed"; + return 1; + } + + QScopedPointer widget(new QWidget()); + QFormLayout *layout = new QFormLayout(widget.data()); + for (const auto &pix : pixmaps) { + QLabel *l = new QLabel(widget.data()); + l->setPixmap(pix); + layout->addRow(QStringLiteral("%1x%2:").arg(pix.width()).arg(pix.height()), l); + } + widget->setLayout(layout); + widget->show(); + return app.exec(); +}