File indexing completed on 2024-04-21 15:05:42

0001 /*
0002     This file is part of the KDE libraries
0003     SPDX-FileCopyrightText: 2016 Martin Gräßlin <mgraesslin@kde.org>
0004 
0005     SPDX-License-Identifier: LGPL-2.1-or-later
0006 */
0007 #include "../src/platforms/xcb/netwm.h"
0008 #include <QApplication>
0009 #include <QCommandLineParser>
0010 #include <QHBoxLayout>
0011 #include <QIcon>
0012 #include <QLabel>
0013 #include <QVBoxLayout>
0014 #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
0015 #include <private/qtx11extras_p.h>
0016 #else
0017 #include <QX11Info>
0018 #endif
0019 
0020 #include <kwindowsystem.h>
0021 #include <kx11extras.h>
0022 
0023 int main(int argc, char **argv)
0024 {
0025     QApplication app(argc, argv);
0026     QCommandLineParser parser;
0027     parser.addPositionalArgument(QStringLiteral("WId"), QStringLiteral("window id for the window to take the icon from"), QStringLiteral("[WId]"));
0028     parser.addHelpOption();
0029     parser.process(app);
0030 
0031     QWidget window;
0032     QVBoxLayout *vbox = new QVBoxLayout(&window);
0033 
0034     bool ok = false;
0035     qulonglong id = parser.positionalArguments().first().toULongLong(&ok);
0036     if (!ok) {
0037         // try hex
0038         id = parser.positionalArguments().first().toULongLong(&ok, 16);
0039     }
0040     if (!ok) {
0041         return 1;
0042     }
0043     NETWinInfo info(QX11Info::connection(), id, QX11Info::appRootWindow(), NET::WMIcon, NET::WM2WindowClass | NET::WM2IconPixmap);
0044     auto addIcons = [&window, vbox, &id, &info](const QString &name, int flag) {
0045         QLabel *title = new QLabel(name, &window);
0046         vbox->addWidget(title);
0047         QIcon icons;
0048         if (flag & KX11Extras::NETWM) {
0049             const int *iconSizes = info.iconSizes();
0050             int index = 0;
0051             while (iconSizes[index] != 0 && iconSizes[index + 1] != 0) {
0052                 const int width = iconSizes[index++];
0053                 const int height = iconSizes[index++];
0054                 NETIcon ni = info.icon(width, height);
0055                 if (ni.data) {
0056                     QImage img((uchar *)ni.data, (int)ni.size.width, (int)ni.size.height, QImage::Format_ARGB32);
0057                     if (!img.isNull()) {
0058                         icons.addPixmap(QPixmap::fromImage(img));
0059                     }
0060                 }
0061             }
0062         }
0063 
0064         if (flag & KX11Extras::WMHints) {
0065             icons.addPixmap(KX11Extras::icon(id, 0, 0, false, KX11Extras::WMHints, &info));
0066         }
0067 
0068         if (flag & KX11Extras::ClassHint) {
0069             icons = QIcon::fromTheme(QString::fromUtf8(info.windowClassClass()).toLower());
0070         }
0071         if (flag & KX11Extras::XApp) {
0072             icons = QIcon::fromTheme(QLatin1String("xorg"));
0073         }
0074         if (icons.isNull()) {
0075             return;
0076         }
0077         QHBoxLayout *layout = new QHBoxLayout();
0078         const auto sizes = icons.availableSizes();
0079         for (auto it = sizes.begin(); it != sizes.end(); ++it) {
0080             const QSize &s = *it;
0081             QVBoxLayout *v = new QVBoxLayout();
0082             QLabel *l = new QLabel(QStringLiteral("%1/%2").arg(s.width()).arg(s.height()), &window);
0083             v->addWidget(l);
0084             QLabel *p = new QLabel(&window);
0085             p->setPixmap(icons.pixmap(s));
0086             v->addWidget(p);
0087             layout->addLayout(v);
0088         }
0089         vbox->addLayout(layout);
0090     };
0091     addIcons(QStringLiteral("NetWM"), KX11Extras::NETWM);
0092     addIcons(QStringLiteral("WMHints"), KX11Extras::WMHints);
0093     addIcons(QStringLiteral("ClassHint"), KX11Extras::ClassHint);
0094     addIcons(QStringLiteral("XApp"), KX11Extras::XApp);
0095 
0096     window.show();
0097 
0098     return app.exec();
0099 }