-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathqeyes.cpp
129 lines (113 loc) · 3.41 KB
/
qeyes.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#include "qeyes.h"
#include <QFile>
#include <QDebug>
#include <QResizeEvent>
#define MyPreferWidth 180
#define MyPreferHeight 140
#define XeyesFilePath "/usr/bin/xeyes"
QEyes::QEyes(QWidget *parent)
: QWidget(parent)
, m_mainVBoxLayout(new QVBoxLayout(this))
, m_xeyesProcess(new QProcess(this))
, m_container(nullptr)
, m_embedDelayTimer(new QTimer(this))
, m_preferSize(QSize(MyPreferWidth, MyPreferHeight))
{
m_embedDelayTimer->setSingleShot(true);
m_embedDelayTimer->setInterval(500);
initConnection();
QTimer::singleShot(0, this, &QEyes::startXeyesProcess);
}
QEyes::~QEyes()
{
m_xeyesProcess->kill();
}
QSize QEyes::sizeHint() const
{
qDebug() << ">>>>>>>>>>>>>>>>>>>>>>>sizehint" << m_preferSize;
return m_preferSize;
}
void QEyes::startXeyesProcess()
{
if (!xeyesCmdExists()) {
qDebug() << "The xeyes cmd is not exists";
return;
}
if (m_xeyesProcess->state() != QProcess::ProcessState::NotRunning) {
qDebug() << "The xeyes process is staring or running";
return;
}
QStringList args = {"-geometry", "1x2+3+4"};
m_xeyesProcess->setProgram(XeyesFilePath);
m_xeyesProcess->setArguments(args);
m_xeyesProcess->start();
}
void QEyes::onXeyesProcessError(QProcess::ProcessError err)
{
qDebug() << "Xeyes process error occurred: " << err;
removeContainer();
}
void QEyes::embedXeyesWindow()
{
// call timer's start function instead of call this directly
if (sender() != m_embedDelayTimer) {
qDebug() << "Do not call embed function directly";
return;
}
const WId id = getXeyesWID();
if (id == 0) {
qDebug() << "Not find the xeyes WId";
return;
}
removeContainer();
QWindow *xeyesWindow = QWindow::fromWinId(id);
m_container = QWidget::createWindowContainer(xeyesWindow, this);
if (!m_container) {
qDebug() << "Embed xeyes window failed";
}
m_mainVBoxLayout->addWidget(m_container);
}
void QEyes::initConnection()
{
connect(m_xeyesProcess, SIGNAL(started()), m_embedDelayTimer, SLOT(start()));
connect(m_xeyesProcess, SIGNAL(finished(int)), this, SLOT(startXeyesProcess()));
connect(m_xeyesProcess, SIGNAL(errorOccurred(QProcess::ProcessError)),
this, SLOT(onXeyesProcessError(QProcess::ProcessError)));
connect(m_embedDelayTimer, SIGNAL(timeout()), this, SLOT(embedXeyesWindow()));
}
bool QEyes::xeyesCmdExists()
{
return QFile::exists(XeyesFilePath);
}
WId QEyes::getXeyesWID()
{
QProcess *findExeysWIdProcess = new QProcess(this);
QStringList args = {"-root", "-tree"};
findExeysWIdProcess->setProgram("/usr/bin/xwininfo");
findExeysWIdProcess->setArguments(args);
findExeysWIdProcess->start();
findExeysWIdProcess->waitForFinished();
const QString &data = findExeysWIdProcess->readAll();
for (const QString &line : data.split("\n")) {
if (line.contains("xeyes") && line.contains("1x2+0+0") && line.contains("+3+4")) {
const QString widStr = line.trimmed().split(" ").first();
bool ok;
WId id = widStr.toUInt(&ok, 16);
if (ok) {
return id;
}
}
}
return 0;
}
void QEyes::removeContainer()
{
if (m_container) {
m_container->deleteLater();
m_mainVBoxLayout->removeWidget(m_container);
}
}
void QEyes::setPreferSize(const QSize &preferSize)
{
m_preferSize = preferSize;
}