2022-05-10 10:06:48 +00:00
|
|
|
// Copyright (C) 2017 The Qt Company Ltd.
|
|
|
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
|
2016-01-08 09:04:02 +00:00
|
|
|
|
2020-03-18 16:02:11 +00:00
|
|
|
#include <QtGui/QAction>
|
2016-01-08 09:04:02 +00:00
|
|
|
#include <QtWidgets/QApplication>
|
|
|
|
#include <QtWidgets/QMainWindow>
|
|
|
|
#include <QtWidgets/QMenu>
|
|
|
|
#include <QtWidgets/QMenuBar>
|
|
|
|
#include <QtWidgets/QToolBar>
|
|
|
|
|
|
|
|
#include <QtGui/QScreen>
|
|
|
|
#include <QtGui/QWindow>
|
|
|
|
|
|
|
|
#include <QtCore/QCommandLineOption>
|
|
|
|
#include <QtCore/QCommandLineParser>
|
|
|
|
#include <QtCore/QDebug>
|
|
|
|
#include <QtCore/QSharedPointer>
|
|
|
|
#include <QtCore/QStringList>
|
|
|
|
#include <QtCore/QTextStream>
|
|
|
|
#include <QtCore/QTimer>
|
|
|
|
|
|
|
|
#ifdef Q_OS_WIN
|
|
|
|
# include <QtCore/qt_windows.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <eventfilter.h> // diaglib
|
|
|
|
#include <nativewindowdump.h>
|
|
|
|
#include <qwidgetdump.h>
|
|
|
|
#include <qwindowdump.h>
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
QT_USE_NAMESPACE
|
|
|
|
|
2020-01-28 07:53:17 +00:00
|
|
|
using WidgetPtr = QSharedPointer<QWidget>;
|
2020-07-06 14:37:47 +00:00
|
|
|
using WidgetPtrList = QList<WidgetPtr>;
|
|
|
|
using WIdList = QList<WId>;
|
2016-01-08 09:04:02 +00:00
|
|
|
|
|
|
|
// Create some pre-defined Windows controls by class name
|
|
|
|
static WId createInternalWindow(const QString &name)
|
|
|
|
{
|
|
|
|
WId result = 0;
|
|
|
|
#ifdef Q_OS_WIN
|
|
|
|
if (name == QLatin1String("BUTTON") || name == QLatin1String("COMBOBOX")
|
|
|
|
|| name == QLatin1String("EDIT") || name.startsWith(QLatin1String("RICHEDIT"))) {
|
|
|
|
const HWND hwnd =
|
|
|
|
CreateWindowEx(0, reinterpret_cast<const wchar_t *>(name.utf16()),
|
|
|
|
L"NativeCtrl", WS_OVERLAPPEDWINDOW,
|
|
|
|
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
|
2020-01-28 07:53:17 +00:00
|
|
|
nullptr, nullptr, GetModuleHandle(nullptr), nullptr);
|
2016-01-08 09:04:02 +00:00
|
|
|
if (hwnd) {
|
|
|
|
SetWindowText(hwnd, L"Demo");
|
|
|
|
result = WId(hwnd);
|
|
|
|
} else {
|
|
|
|
qErrnoWarning("Cannot create window \"%s\"", qPrintable(name));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#else // Q_OS_WIN
|
2020-06-27 12:18:09 +00:00
|
|
|
Q_UNUSED(name);
|
2016-01-08 09:04:02 +00:00
|
|
|
#endif
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Embed a foreign window using createWindowContainer() providing
|
|
|
|
// menu actions to dump information.
|
|
|
|
class EmbeddingWindow : public QMainWindow
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
public:
|
|
|
|
explicit EmbeddingWindow(QWindow *window);
|
|
|
|
|
|
|
|
public slots:
|
|
|
|
void releaseForeignWindow();
|
|
|
|
|
|
|
|
private:
|
|
|
|
QWindow *m_window;
|
|
|
|
QAction *m_releaseAction;
|
|
|
|
};
|
|
|
|
|
|
|
|
EmbeddingWindow::EmbeddingWindow(QWindow *window) : m_window(window)
|
|
|
|
{
|
|
|
|
const QString title = QLatin1String("Qt ") + QLatin1String(QT_VERSION_STR)
|
|
|
|
+ QLatin1String(" 0x") + QString::number(window->winId(), 16);
|
|
|
|
setWindowTitle(title);
|
|
|
|
setObjectName("MainWindow");
|
2017-09-18 09:49:52 +00:00
|
|
|
QWidget *container = QWidget::createWindowContainer(window, nullptr, Qt::Widget);
|
2016-01-08 09:04:02 +00:00
|
|
|
container->setObjectName("Container");
|
|
|
|
setCentralWidget(container);
|
|
|
|
|
|
|
|
QMenu *fileMenu = menuBar()->addMenu("File");
|
|
|
|
fileMenu->setObjectName("FileMenu");
|
|
|
|
QToolBar *toolbar = new QToolBar;
|
|
|
|
addToolBar(Qt::TopToolBarArea, toolbar);
|
|
|
|
|
|
|
|
// Manipulation
|
|
|
|
QAction *action = fileMenu->addAction("Visible");
|
|
|
|
action->setCheckable(true);
|
|
|
|
action->setChecked(true);
|
|
|
|
connect(action, &QAction::toggled, m_window, &QWindow::setVisible);
|
|
|
|
toolbar->addAction(action);
|
|
|
|
|
|
|
|
m_releaseAction = fileMenu->addAction("Release", this, &EmbeddingWindow::releaseForeignWindow);
|
|
|
|
toolbar->addAction(m_releaseAction);
|
|
|
|
|
|
|
|
fileMenu->addSeparator(); // Diaglib actions
|
|
|
|
action = fileMenu->addAction("Dump Widgets",
|
|
|
|
this, [] () { QtDiag::dumpAllWidgets(); });
|
|
|
|
toolbar->addAction(action);
|
|
|
|
action = fileMenu->addAction("Dump Windows",
|
|
|
|
this, [] () { QtDiag::dumpAllWindows(); });
|
|
|
|
toolbar->addAction(action);
|
|
|
|
action = fileMenu->addAction("Dump Native Windows",
|
|
|
|
this, [this] () { QtDiag::dumpNativeWindows(winId()); });
|
|
|
|
toolbar->addAction(action);
|
|
|
|
|
|
|
|
fileMenu->addSeparator();
|
|
|
|
action = fileMenu->addAction("Quit", qApp, &QCoreApplication::quit);
|
|
|
|
toolbar->addAction(action);
|
2020-09-25 07:49:47 +00:00
|
|
|
action->setShortcut(Qt::CTRL | Qt::Key_Q);
|
2016-01-08 09:04:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void EmbeddingWindow::releaseForeignWindow()
|
|
|
|
{
|
|
|
|
if (m_window) {
|
2017-09-18 09:49:52 +00:00
|
|
|
m_window->setParent(nullptr);
|
|
|
|
m_window = nullptr;
|
2016-01-08 09:04:02 +00:00
|
|
|
m_releaseAction->setEnabled(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Dump information about foreign windows.
|
|
|
|
class WindowDumper : public QObject {
|
|
|
|
Q_OBJECT
|
|
|
|
public:
|
|
|
|
explicit WindowDumper(const QWindowList &watchedWindows)
|
|
|
|
: m_watchedWindows(watchedWindows) {}
|
|
|
|
|
|
|
|
public slots:
|
|
|
|
void dump() const;
|
|
|
|
|
|
|
|
private:
|
|
|
|
const QWindowList m_watchedWindows;
|
|
|
|
};
|
|
|
|
|
|
|
|
void WindowDumper::dump() const
|
|
|
|
{
|
|
|
|
static int n = 0;
|
|
|
|
QString s;
|
|
|
|
QDebug debug(&s);
|
|
|
|
debug.nospace();
|
|
|
|
debug.setVerbosity(3);
|
|
|
|
debug << '#' << n++;
|
|
|
|
if (m_watchedWindows.size() > 1)
|
|
|
|
debug << '\n';
|
2020-01-28 07:53:17 +00:00
|
|
|
for (const QWindow *w : m_watchedWindows) {
|
2016-01-08 09:04:02 +00:00
|
|
|
const QPoint globalPos = w->mapToGlobal(QPoint());
|
|
|
|
debug << " " << w << " pos=" << globalPos.x() << ',' << globalPos.y() << '\n';
|
|
|
|
}
|
|
|
|
|
|
|
|
std::cout << qPrintable(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
static QString description(const QString &appName)
|
|
|
|
{
|
|
|
|
QString result;
|
|
|
|
QTextStream(&result)
|
|
|
|
<< "\nDumps information about foreign windows passed on the command line or\n"
|
|
|
|
"tests embedding foreign windows into Qt.\n\nUse cases:\n\n"
|
|
|
|
<< appName << " -a Dump a list of all native window ids.\n"
|
|
|
|
<< appName << " <winid> Dump information on the window.\n"
|
2016-11-03 08:38:44 +00:00
|
|
|
<< appName << " -m <winid> Move window to top left corner\n"
|
|
|
|
<< QByteArray(appName.size(), ' ')
|
|
|
|
<< " (recover lost windows after changing monitor setups).\n"
|
2016-01-08 09:04:02 +00:00
|
|
|
<< appName << " -c <winid> Dump information on the window continuously.\n"
|
|
|
|
<< appName << " -e <winid> Embed window into a Qt widget.\n"
|
|
|
|
<< "\nOn Windows, class names of well known controls (EDIT, BUTTON...) can be\n"
|
|
|
|
"passed as <winid> along with -e, which will create the control.\n";
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct EventFilterOption
|
|
|
|
{
|
|
|
|
const char *name;
|
|
|
|
const char *description;
|
|
|
|
QtDiag::EventFilter::EventCategories categories;
|
|
|
|
};
|
|
|
|
|
2020-01-28 07:53:17 +00:00
|
|
|
static EventFilterOption eventFilterOptions[] = {
|
2016-01-08 09:04:02 +00:00
|
|
|
{"mouse-events", "Dump mouse events.", QtDiag::EventFilter::MouseEvents},
|
|
|
|
{"keyboard-events", "Dump keyboard events.", QtDiag::EventFilter::KeyEvents},
|
|
|
|
{"state-events", "Dump state/focus change events.", QtDiag::EventFilter::StateChangeEvents | QtDiag::EventFilter::FocusEvents}
|
|
|
|
};
|
|
|
|
|
|
|
|
static inline bool isOptionSet(int argc, char *argv[], const char *option)
|
|
|
|
{
|
|
|
|
return (argv + argc) !=
|
|
|
|
std::find_if(argv + 1, argv + argc,
|
|
|
|
[option] (const char *arg) { return !qstrcmp(arg, option); });
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
QCoreApplication::setApplicationVersion(QLatin1String(QT_VERSION_STR));
|
|
|
|
QGuiApplication::setApplicationDisplayName("Foreign window tester");
|
|
|
|
|
|
|
|
QApplication app(argc, argv);
|
|
|
|
|
|
|
|
QCommandLineParser parser;
|
|
|
|
parser.setSingleDashWordOptionMode(QCommandLineParser::ParseAsLongOptions);
|
|
|
|
parser.setApplicationDescription(description(QCoreApplication::applicationName()));
|
|
|
|
parser.addHelpOption();
|
|
|
|
parser.addVersionOption();
|
|
|
|
QCommandLineOption noScalingDummy(QStringLiteral("s"),
|
|
|
|
QStringLiteral("Disable High DPI scaling."));
|
|
|
|
parser.addOption(noScalingDummy);
|
|
|
|
QCommandLineOption outputAllOption(QStringList() << QStringLiteral("a") << QStringLiteral("all"),
|
|
|
|
QStringLiteral("Output all native window ids (requires diaglib)."));
|
|
|
|
parser.addOption(outputAllOption);
|
|
|
|
QCommandLineOption continuousOption(QStringList() << QStringLiteral("c") << QStringLiteral("continuous"),
|
|
|
|
QStringLiteral("Output continuously."));
|
2020-01-28 07:53:17 +00:00
|
|
|
parser.addOption(continuousOption);
|
2016-11-03 08:38:44 +00:00
|
|
|
QCommandLineOption moveOption(QStringList() << QStringLiteral("m") << QStringLiteral("move"),
|
|
|
|
QStringLiteral("Move window to top left corner."));
|
|
|
|
parser.addOption(moveOption);
|
2016-01-08 09:04:02 +00:00
|
|
|
QCommandLineOption embedOption(QStringList() << QStringLiteral("e") << QStringLiteral("embed"),
|
|
|
|
QStringLiteral("Embed a foreign window into a Qt widget."));
|
|
|
|
parser.addOption(embedOption);
|
|
|
|
const int eventFilterOptionCount = int(sizeof(eventFilterOptions) / sizeof(eventFilterOptions[0]));
|
|
|
|
for (int i = 0; i < eventFilterOptionCount; ++i) {
|
|
|
|
parser.addOption(QCommandLineOption(QLatin1String(eventFilterOptions[i].name),
|
|
|
|
QLatin1String(eventFilterOptions[i].description)));
|
|
|
|
}
|
|
|
|
parser.addPositionalArgument(QStringLiteral("[windows]"), QStringLiteral("Window IDs."));
|
|
|
|
|
|
|
|
parser.process(QCoreApplication::arguments());
|
|
|
|
|
|
|
|
if (parser.isSet(outputAllOption)) {
|
|
|
|
QtDiag::dumpNativeWindows();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
QWindowList windows;
|
2020-01-28 07:53:17 +00:00
|
|
|
for (const QString &argument : parser.positionalArguments()) {
|
2016-01-08 09:04:02 +00:00
|
|
|
bool ok = true;
|
|
|
|
WId wid = createInternalWindow(argument);
|
|
|
|
if (!wid)
|
|
|
|
wid = argument.toULongLong(&ok, 0);
|
|
|
|
if (!wid || !ok) {
|
|
|
|
std::cerr << "Invalid window id: \"" << qPrintable(argument) << "\"\n";
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
QWindow *foreignWindow = QWindow::fromWinId(wid);
|
2016-11-03 08:38:44 +00:00
|
|
|
if (!foreignWindow)
|
|
|
|
return -1;
|
2016-01-08 09:04:02 +00:00
|
|
|
foreignWindow->setObjectName("ForeignWindow" + QString::number(wid, 16));
|
|
|
|
windows.append(foreignWindow);
|
2016-11-03 08:38:44 +00:00
|
|
|
if (parser.isSet(moveOption))
|
|
|
|
foreignWindow->setFramePosition(QGuiApplication::primaryScreen()->availableGeometry().topLeft());
|
2016-01-08 09:04:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (windows.isEmpty())
|
|
|
|
parser.showHelp(0);
|
|
|
|
|
|
|
|
int exitCode = 0;
|
|
|
|
|
|
|
|
if (parser.isSet(embedOption)) {
|
2020-01-28 07:53:17 +00:00
|
|
|
QtDiag::EventFilter::EventCategories eventCategories;
|
2016-01-08 09:04:02 +00:00
|
|
|
for (int i = 0; i < eventFilterOptionCount; ++i) {
|
|
|
|
if (parser.isSet(QLatin1String(eventFilterOptions[i].name)))
|
|
|
|
eventCategories |= eventFilterOptions[i].categories;
|
|
|
|
}
|
|
|
|
if (eventCategories)
|
|
|
|
app.installEventFilter(new QtDiag::EventFilter(eventCategories, &app));
|
|
|
|
|
2020-01-28 07:53:17 +00:00
|
|
|
const QRect availableGeometry = QGuiApplication::primaryScreen()->availableGeometry();
|
2016-01-08 09:04:02 +00:00
|
|
|
QPoint pos = availableGeometry.topLeft() + QPoint(availableGeometry.width(), availableGeometry.height()) / 3;
|
|
|
|
|
|
|
|
WidgetPtrList mainWindows;
|
2020-01-28 07:53:17 +00:00
|
|
|
for (QWindow *window : qAsConst(windows)) {
|
2016-01-08 09:04:02 +00:00
|
|
|
WidgetPtr mainWindow(new EmbeddingWindow(window));
|
|
|
|
mainWindow->move(pos);
|
|
|
|
mainWindow->resize(availableGeometry.size() / 4);
|
|
|
|
mainWindow->show();
|
|
|
|
pos += QPoint(40, 40);
|
|
|
|
mainWindows.append(mainWindow);
|
|
|
|
}
|
|
|
|
exitCode = app.exec();
|
|
|
|
|
|
|
|
} else if (parser.isSet(continuousOption)) {
|
|
|
|
WindowDumper dumper(windows);
|
|
|
|
dumper.dump();
|
|
|
|
QTimer *timer = new QTimer(&dumper);
|
|
|
|
QObject::connect(timer, &QTimer::timeout, &dumper, &WindowDumper::dump);
|
|
|
|
timer->start(1000);
|
|
|
|
exitCode = app.exec();
|
|
|
|
} else {
|
|
|
|
WindowDumper(windows).dump();
|
|
|
|
}
|
|
|
|
|
|
|
|
return exitCode;
|
|
|
|
}
|
|
|
|
|
|
|
|
#include "main.moc"
|