-
Notifications
You must be signed in to change notification settings - Fork 122
Expand file tree
/
Copy pathapp.cpp
More file actions
303 lines (243 loc) · 8.94 KB
/
Copy pathapp.cpp
File metadata and controls
303 lines (243 loc) · 8.94 KB
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
#include <QElapsedTimer>
#include <unistd.h>
#include <typeinfo>
#include "app/config.hpp"
#include "app/arbiter.hpp"
#include "app/session.hpp"
#include "app.hpp"
XWorker::WindowProp::WindowProp(char *prop, unsigned long size)
{
this->size = size;
this->prop = new char[this->size + 1];
std::copy(prop, prop + size, (char *)this->prop);
((char *)this->prop)[size] = '\0';
}
XWorker::WindowProp::~WindowProp()
{
if (this->prop != nullptr) {
delete (char *)this->prop;
this->prop = nullptr;
}
}
XWorker::XWorker(QObject *parent) : QObject(parent)
{
this->display = XOpenDisplay(nullptr);
this->root_window = DefaultRootWindow(this->display);
}
int XWorker::get_window(uint64_t pid)
{
int retries = 0;
while (retries < MAX_RETRIES) {
WindowProp client_list = this->get_window_prop(this->root_window, XA_WINDOW, "_NET_CLIENT_LIST");
Window *windows = (Window *)client_list.prop;
for (unsigned long i = 0; i < (client_list.size / sizeof(Window)); i++) {
if (pid == *(uint64_t *)this->get_window_prop(windows[i], XA_CARDINAL, "_NET_WM_PID").prop)
return windows[i];
}
usleep(500000);
retries++;
}
return -1;
}
XWorker::WindowProp XWorker::get_window_prop(Window window, Atom type, const char *name)
{
Atom prop = XInternAtom(this->display, name, false);
Atom actual_type_return;
int actual_format_return;
unsigned long nitems_return;
unsigned long bytes_after_return;
unsigned char *prop_return;
XGetWindowProperty(this->display, window, prop, 0, 1024, false, type, &actual_type_return, &actual_format_return,
&nitems_return, &bytes_after_return, &prop_return);
unsigned long size = (actual_format_return / 8) * nitems_return;
if (actual_format_return == 32) size *= sizeof(long) / 4;
WindowProp window_prop((char *)prop_return, size);
XFree(prop_return);
return window_prop;
}
EmbeddedApp::EmbeddedApp(QWidget *parent) : QWidget(parent), process()
{
this->worker = new XWorker(this);
this->process.setStandardOutputFile(QProcess::nullDevice());
this->process.setStandardErrorFile(QProcess::nullDevice());
connect(&this->process, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished),
[this](int, QProcess::ExitStatus) { this->end(); });
this->container = new QVBoxLayout(this);
this->container->setContentsMargins(0, 0, 0, 0);
}
EmbeddedApp::~EmbeddedApp()
{
this->process.kill();
this->process.waitForFinished();
delete this->container;
delete this->worker;
}
void EmbeddedApp::start(QString app)
{
this->process.setProgram(app);
this->process.start();
this->process.waitForStarted();
QWindow *window = QWindow::fromWinId(worker->get_window(this->process.processId()));
window->setFlags(Qt::FramelessWindowHint);
usleep(500000);
this->container->addWidget(QWidget::createWindowContainer(window, this));
emit opened();
}
void EmbeddedApp::end()
{
this->process.terminate();
delete this->container->takeAt(0);
emit closed();
}
Launcher::Launcher(Arbiter &arbiter, QSettings &settings, int idx, QWidget *parent)
: QWidget(parent)
, arbiter(arbiter)
, settings(settings)
, idx(idx)
{
this->setObjectName("App");
QStackedLayout *layout = new QStackedLayout(this);
layout->setContentsMargins(0, 0, 0, 0);
this->app = new EmbeddedApp(this);
connect(this->app, &EmbeddedApp::opened, [layout]() { layout->setCurrentIndex(1); });
connect(this->app, &EmbeddedApp::closed, [layout]() { layout->setCurrentIndex(0); });
auto launcher_app = this->settings.value(this->app_key()).toString();
this->auto_launch = !launcher_app.isEmpty();
layout->addWidget(this->launcher_widget());
layout->addWidget(this->app);
if (this->auto_launch)
this->app->start(launcher_app);
}
Launcher::~Launcher()
{
delete this->app;
}
void Launcher::update_idx(int idx)
{
if (idx == this->idx)
return;
QString home;
QString app;
if (this->settings.contains(this->home_key()))
home = this->settings.value(this->home_key(), this->DEFAULT_DIR).toString();
if (this->settings.contains(this->app_key()))
app = this->settings.value(this->app_key()).toString();
this->settings.remove(QString::number(this->idx));
this->idx = idx;
if (!home.isNull())
this->settings.setValue(this->home_key(), home);
if (!app.isNull())
this->settings.setValue(this->app_key(), app);
}
QWidget *Launcher::launcher_widget()
{
QWidget *widget = new QWidget(this);
QVBoxLayout *layout = new QVBoxLayout(widget);
this->path_label = new QLabel(this->settings.value(this->home_key(), this->DEFAULT_DIR).toString(), this);
layout->addStretch(1);
layout->addWidget(this->path_label, 1);
layout->addWidget(this->app_select_widget(), 6);
layout->addWidget(this->config_widget(), 1, Qt::AlignRight);
layout->addStretch(1);
return widget;
}
QWidget *Launcher::app_select_widget()
{
QWidget *widget = new QWidget(this);
QHBoxLayout *layout = new QHBoxLayout(widget);
QString root_path(this->path_label->text());
QPushButton *home_button = new QPushButton(widget);
home_button->setFlat(true);
home_button->setCheckable(true);
home_button->setChecked(true);
connect(home_button, &QPushButton::clicked, [this](bool checked = false) {
if (checked)
this->settings.setValue(this->home_key(), this->path_label->text());
else
this->settings.remove(this->home_key());
});
this->arbiter.forge().iconize("playlist_add", "playlist_add_check", home_button, 32, true);
layout->addWidget(home_button, 0, Qt::AlignTop);
this->folders = new QListWidget(widget);
Session::Forge::to_touch_scroller(this->folders);
this->populate_dirs(root_path);
layout->addWidget(this->folders, 4);
this->apps = new QListWidget(widget);
Session::Forge::to_touch_scroller(this->apps);
this->populate_apps(root_path);
connect(this->apps, &QListWidget::itemClicked, [this](QListWidgetItem *item) {
QString app_path = this->path_label->text() + '/' + item->text();
if (this->auto_launch)
this->settings.setValue(this->app_key(), app_path);
this->app->start(app_path);
});
connect(this->folders, &QListWidget::itemClicked, [this, home_button](QListWidgetItem *item) {
if (!item->isSelected())
return;
this->apps->clear();
QString current_path(item->data(Qt::UserRole).toString());
this->path_label->setText(current_path);
this->populate_apps(current_path);
this->populate_dirs(current_path);
home_button->setChecked(this->settings.value(this->home_key(), this->DEFAULT_DIR).toString() == current_path);
});
layout->addWidget(this->apps, 5);
return widget;
}
QWidget *Launcher::config_widget()
{
QWidget *widget = new QWidget(this);
QVBoxLayout *layout = new QVBoxLayout(widget);
QCheckBox *checkbox = new QCheckBox("launch at startup", widget);
checkbox->setChecked(this->auto_launch);
connect(checkbox, &QCheckBox::toggled, [this, checkbox](bool checked) {
this->auto_launch = checked;
if (this->auto_launch && this->apps->currentItem())
this->settings.setValue(this->app_key(), this->path_label->text() + '/' + this->apps->currentItem()->text());
else
this->settings.remove(this->app_key());
});
layout->addWidget(checkbox);
return widget;
}
void Launcher::populate_dirs(QString path)
{
this->folders->clear();
QDir current_dir(path);
for (QFileInfo dir : current_dir.entryInfoList(QDir::AllDirs | QDir::Readable)) {
if (dir.fileName() == ".")
continue;
QListWidgetItem *item = new QListWidgetItem(dir.fileName(), this->folders);
if (dir.fileName() == "..") {
item->setText("↲");
if (current_dir.isRoot())
item->setFlags(Qt::NoItemFlags);
}
else {
item->setText(dir.fileName());
}
item->setData(Qt::UserRole, QVariant(dir.absoluteFilePath()));
}
}
void Launcher::populate_apps(QString path)
{
for (QString app : QDir(path).entryList(QDir::Files | QDir::Executable))
new QListWidgetItem(app, this->apps);
}
QList<QWidget *> App::widgets()
{
int size = this->loaded_widgets.size();
this->loaded_widgets.append(new Launcher(*this->arbiter, this->settings, size));
return this->loaded_widgets.mid(size);
}
void App::remove_widget(int idx)
{
LauncherPlugin::remove_widget(idx);
this->settings.remove(QString::number(idx));
for (int i = 0; i < this->loaded_widgets.size(); i++) {
auto typeStr(typeid(this->loaded_widgets[i]).name());
qDebug() << typeStr;
if (Launcher *launcher = qobject_cast<Launcher *>(this->loaded_widgets[i]))
launcher->update_idx(i);
}
}