File indexing completed on 2024-04-14 03:57:04

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 #include <private/qtx11extras_p.h>
0015 
0016 #include <kwindowsystem.h>
0017 #include <kx11extras.h>
0018 
0019 int main(int argc, char **argv)
0020 {
0021     QApplication app(argc, argv);
0022     QCommandLineParser parser;
0023     parser.addPositionalArgument(QStringLiteral("WId"), QStringLiteral("window id for the window to take the icon from"), QStringLiteral("[WId]"));
0024     parser.addHelpOption();
0025     parser.process(app);
0026 
0027     QWidget window;
0028     QVBoxLayout *vbox = new QVBoxLayout(&window);
0029 
0030     bool ok = false;
0031     qulonglong id = parser.positionalArguments().first().toULongLong(&ok);
0032     if (!ok) {
0033         // try hex
0034         id = parser.positionalArguments().first().toULongLong(&ok, 16);
0035     }
0036     if (!ok) {
0037         return 1;
0038     }
0039     NETWinInfo info(QX11Info::connection(), id, QX11Info::appRootWindow(), NET::WMIcon, NET::WM2WindowClass | NET::WM2IconPixmap);
0040     auto addIcons = [&window, vbox, &id, &info](const QString &name, int flag) {
0041         QLabel *title = new QLabel(name, &window);
0042         vbox->addWidget(title);
0043         QIcon icons;
0044         if (flag & KX11Extras::NETWM) {
0045             const int *iconSizes = info.iconSizes();
0046             int index = 0;
0047             while (iconSizes[index] != 0 && iconSizes[index + 1] != 0) {
0048                 const int width = iconSizes[index++];
0049                 const int height = iconSizes[index++];
0050                 NETIcon ni = info.icon(width, height);
0051                 if (ni.data) {
0052                     QImage img((uchar *)ni.data, (int)ni.size.width, (int)ni.size.height, QImage::Format_ARGB32);
0053                     if (!img.isNull()) {
0054                         icons.addPixmap(QPixmap::fromImage(img));
0055                     }
0056                 }
0057             }
0058         }
0059 
0060         if (flag & KX11Extras::WMHints) {
0061             icons.addPixmap(KX11Extras::icon(id, 0, 0, false, KX11Extras::WMHints, &info));
0062         }
0063 
0064         if (flag & KX11Extras::ClassHint) {
0065             icons = QIcon::fromTheme(QString::fromUtf8(info.windowClassClass()).toLower());
0066         }
0067         if (flag & KX11Extras::XApp) {
0068             icons = QIcon::fromTheme(QLatin1String("xorg"));
0069         }
0070         if (icons.isNull()) {
0071             return;
0072         }
0073         QHBoxLayout *layout = new QHBoxLayout();
0074         const auto sizes = icons.availableSizes();
0075         for (auto it = sizes.begin(); it != sizes.end(); ++it) {
0076             const QSize &s = *it;
0077             QVBoxLayout *v = new QVBoxLayout();
0078             QLabel *l = new QLabel(QStringLiteral("%1/%2").arg(s.width()).arg(s.height()), &window);
0079             v->addWidget(l);
0080             QLabel *p = new QLabel(&window);
0081             p->setPixmap(icons.pixmap(s));
0082             v->addWidget(p);
0083             layout->addLayout(v);
0084         }
0085         vbox->addLayout(layout);
0086     };
0087     addIcons(QStringLiteral("NetWM"), KX11Extras::NETWM);
0088     addIcons(QStringLiteral("WMHints"), KX11Extras::WMHints);
0089     addIcons(QStringLiteral("ClassHint"), KX11Extras::ClassHint);
0090     addIcons(QStringLiteral("XApp"), KX11Extras::XApp);
0091 
0092     window.show();
0093 
0094     return app.exec();
0095 }