-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwuake_tab_page.cpp
208 lines (173 loc) · 4.98 KB
/
wuake_tab_page.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#include <QProcessEnvironment>
#include <QProcess>
#include <QThread>
#include <QVBoxLayout>
#include <QFileInfo>
#include <QDir>
#include <QtDebug>
#include <QMessageBox>
#include <Windows.h>
#include "wuake_tab_page.h"
WuakeTabPage::WuakeTabPage(QWidget *parent) :
QWidget(parent),
mEnFocus(true)
{
mProcess = new QProcess(this);
connect(mProcess, SIGNAL(started()), this, SLOT(onStarted()));
connect(mProcess, SIGNAL(finished(int,QProcess::ExitStatus)), this, SLOT(onFinished(int,QProcess::ExitStatus)));
connect(mProcess, SIGNAL(errorOccurred(QProcess::ProcessError)), this, SLOT(onError(QProcess::ProcessError)));
}
WuakeTabPage::~WuakeTabPage()
{
delete mWindow;
}
void WuakeTabPage::requestFocus()
{
if (mEnFocus) {
qDebug() << titleName() << " requestFocus";
::SetFocus(mHwnd);
}
}
void WuakeTabPage::close()
{
::SendMessage(mHwnd, WM_CLOSE, 0, 0);
}
void WuakeTabPage::startProcess()
{
}
bool WuakeTabPage::processRunning()
{
if (mProcess->state() == QProcess::NotRunning) {
return false;
}
return true;
}
void WuakeTabPage::enableFocus(bool enable)
{
mEnFocus = enable;
}
bool WuakeTabPage::isEnableFocus()
{
return mEnFocus;
}
bool WuakeTabPage::isFocused()
{
return ::GetFocus() == mHwnd;
}
void WuakeTabPage::onError(QProcess::ProcessError error)
{
qWarning() << "Process error:" << error;
QMessageBox::critical(this, tr("Error"), tr("Open program [") + mProcess->program() + tr("] failed!"));
emit stateChanged(PAGE_STATE_ERROR);
}
void WuakeTabPage::onFinished(int exitCode, QProcess::ExitStatus exitStatus)
{
qDebug() << "exitCode:" << exitCode << ", exitStatus:" << exitStatus;
emit stateChanged(PAGE_STATE_CLOSE);
}
HWND findWindow(const QString& className, const QString& titleName)
{
HWND hwnd;
#ifdef UNICODE
hwnd = ::FindWindow(className.toStdWString().data(), titleName.toStdWString().data());
#else
hwnd = ::FindWindow(className.toLocal8Bit().data(), titleName.toLocal8Bit().data());
#endif
return hwnd;
}
void WuakeTabPage::onStarted()
{
qDebug() << "onStarted";
mHwnd = findWindow(className(), titleName());
for (int i=0; i<100 && 0 == mHwnd; ++i) {
QThread::msleep(5);
mHwnd = findWindow(className(), titleName());
}
WId wid = (WId)mHwnd;
mWindow = QWindow::fromWinId(wid);
mWidget = QWidget::createWindowContainer(mWindow);
mWidget->setFocusPolicy(Qt::StrongFocus);
mWidget->setFixedSize(mWindow->size());
QVBoxLayout* layout = new QVBoxLayout(this);
layout->setContentsMargins(0, 0, 0, 0);
layout->addWidget(mWidget);
emit stateChanged(PAGE_STATE_START);
}
bool WuakeTabPage::event(QEvent *event)
{
QEvent::Type eventType = event->type();
if (QEvent::Paint != eventType) {
//qDebug() << " Event:" << eventType;
}
if (QEvent::WindowActivate == eventType) {
requestFocus();
}
return QWidget::event(event);
}
quint64 MinttyTabPage::sCounter = 0;
QString MinttyTabPage::sMinttyPath;
MinttyTabPage::MinttyTabPage(QWidget *parent) :
WuakeTabPage(parent)
{
mTitleName = QString("%1%2").arg(className()).arg(++sCounter);
findMintty();
}
MinttyTabPage::~MinttyTabPage()
{
}
void MinttyTabPage::startProcess()
{
QProcess* process = mProcess;
QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
env.insert("ConEmuPID", "-1");
process->setProcessEnvironment(env);
QString homeDir = env.value("HOME");
if (homeDir.isEmpty()) {
homeDir = env.value("USERPROFILE");
}
if (!homeDir.isEmpty()) {
process->setWorkingDirectory(homeDir);
}
QStringList params;
//params << "-s" << "100,30";
params << "-B" << "void";
params << "-t" << titleName();
params << "--nopin";
params << "-";
process->start(sMinttyPath, params);
}
QString MinttyTabPage::className()
{
return "mintty";
}
QString MinttyTabPage::titleName()
{
return mTitleName;
}
QString findByCmdWhere(const QString& cmd)
{
QProcess process;
QList<QString> params;
params << cmd;
process.start("where", params);
process.waitForFinished();
if (process.exitCode() != 0) return nullptr;
QByteArray output = process.readAllStandardOutput();
int pos = output.indexOf("\n");
if (pos < 0) return nullptr;
QByteArray path = output.left(pos - 1);
return QString(path);
}
void MinttyTabPage::findMintty()
{
if (!sMinttyPath.isEmpty()) return;
sMinttyPath = findByCmdWhere("mintty.exe");
if (!sMinttyPath.isEmpty()) return;
QString gitPath = findByCmdWhere("git.exe");
if (gitPath.isEmpty()) return;
QFileInfo gitInfo(gitPath);
QDir tmp = gitInfo.absoluteDir();
tmp.cdUp();
sMinttyPath = tmp.absolutePath() + "/usr/bin/mintty.exe";
//sMinttyPath = "mintty.exe";
}