forked from jahnf/Projecteur
-
Notifications
You must be signed in to change notification settings - Fork 0
/
projecteurapp.cc
338 lines (291 loc) · 10.6 KB
/
projecteurapp.cc
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
// This file is part of Projecteur - https://github.com/jahnf/projecteur - See LICENSE.md and README.md
#include "projecteurapp.h"
#include "aboutdlg.h"
#include "preferencesdlg.h"
#include "qglobalshortcutx11.h"
#include "settings.h"
#include "spotlight.h"
#include <QDialog>
#include <QLocalServer>
#include <QLocalSocket>
#include <QMenu>
#include <QMessageBox>
#include <QPointer>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include <QScreen>
#include <QSystemTrayIcon>
#include <QTimer>
#include <QWindow>
#include <QDebug>
namespace {
QString localServerName() {
return QCoreApplication::applicationName() + "_local_socket";
}
}
ProjecteurApplication::ProjecteurApplication(int &argc, char **argv)
: QApplication(argc, argv)
, m_trayIcon(new QSystemTrayIcon())
, m_trayMenu(new QMenu())
, m_localServer(new QLocalServer(this))
{
if (screens().size() < 1)
{
QMessageBox::critical(nullptr, tr("No Screens"), tr("screens().size() returned a size < 1."));
QTimer::singleShot(0, [this](){ this->exit(2); });
return;
}
setQuitOnLastWindowClosed(false);
m_spotlight = new Spotlight(this);
const auto settings = new Settings(this);
m_dialog.reset(new PreferencesDialog(settings, m_spotlight));
m_dialog->updateAvailableScreens(screens());
connect(&*m_dialog, &PreferencesDialog::testButtonClicked, [this](){
emit m_spotlight->spotActiveChanged(true);
});
auto screen = screens().first();
if (settings->screen() < screens().size()) {
screen = screens().at(settings->screen());
}
const auto engine = new QQmlApplicationEngine(this);
engine->rootContext()->setContextProperty("Settings", settings);
engine->rootContext()->setContextProperty("PreferencesDialog", &*m_dialog);
engine->load(QUrl(QStringLiteral("qrc:/main.qml")));
const auto window = topLevelWindows().first();
const auto actionPref = m_trayMenu->addAction(tr("&Preferences..."));
connect(actionPref, &QAction::triggered, [this](){
this->showPreferences(true);
});
const auto actionAbout = m_trayMenu->addAction(tr("&About"));
connect(actionAbout, &QAction::triggered, [this]()
{
if (!m_aboutDialog)
m_aboutDialog.reset(new AboutDialog);
if (m_aboutDialog->isVisible()) {
m_aboutDialog->show();
m_aboutDialog->raise();
m_aboutDialog->activateWindow();
} else {
m_aboutDialog->exec();
}
});
m_trayMenu->addSeparator();
const auto actionQuit = m_trayMenu->addAction(tr("&Quit"));
connect(actionQuit, &QAction::triggered, [this](){ this->quit(); });
m_trayIcon->setContextMenu(&*m_trayMenu);
m_trayIcon->setIcon(QIcon(":/icons/projecteur-tray-64.png"));
m_trayIcon->show();
connect(&*m_trayIcon, &QSystemTrayIcon::activated, [this](QSystemTrayIcon::ActivationReason reason) {
if (reason == QSystemTrayIcon::Trigger)
{
//static const bool isKDE = (qgetenv("XDG_CURRENT_DESKTOP") == QByteArray("KDE"));
const auto trayGeometry = m_trayIcon->geometry();
// This usually won't give us a valid geometry, since Qt isn't drawing the tray icon itself
if (trayGeometry.isValid()) {
m_trayIcon->contextMenu()->popup(m_trayIcon->geometry().center());
} else {
// It's tricky to get the same behavior on all desktop environments. While on GNOME3
// it behaves as one (or most) would expect it behaves differently on other Desktop
// environments.
// QSystemTrayIcon is a wrapper around the StatusNotfierItem on modern (Linux) Desktops
// see: https://www.freedesktop.org/wiki/Specifications/StatusNotifierItem/
// Via the Qt API there is not much control over how e.g. KDE or GNOME show the icon
// and how it behaves.. e.g. setting something like
// org.freedesktop.StatusNotifierItem.ItemIsMenu to True would be good for KDE Plasma
// see: https://www.freedesktop.org/wiki/Specifications/StatusNotifierItem/StatusNotifierItem/
this->showPreferences(true);
}
}
});
window->setFlags(window->flags() | Qt::WindowTransparentForInput | Qt::Tool);
window->setScreen(screen);
window->setPosition(screen->geometry().topLeft());
window->setWidth(screen->geometry().width());
window->setHeight(screen->geometry().height());
connect(this, &ProjecteurApplication::aboutToQuit, [window](){ if (window) window->close(); });
// Example code for global shortcuts...
// const auto shortcut = new QGlobalShortcutX11(QKeySequence("Ctrl+F3"), this);
// connect(shortcut, &QGlobalShortcutX11::activated, [window](){
// qDebug() << "GlobalShortCut Ctrl+F3" << window;
// });
// Handling of spotlight window when input from spotlight device is detected
connect(m_spotlight, &Spotlight::spotActiveChanged, [window](bool active)
{
if (active)
{
window->setFlags(window->flags() | Qt::SplashScreen);
window->setFlags(window->flags() & ~Qt::WindowTransparentForInput);
window->setFlags(window->flags() | Qt::WindowStaysOnTopHint);
window->hide();
window->setFlags(window->flags() & ~Qt::SplashScreen);
window->setFlags(window->flags() | Qt::ToolTip);
if (window->screen())
{
const auto screenGeometry = window->screen()->geometry();
if (window->geometry() != screenGeometry) {
window->setGeometry(screenGeometry);
}
}
window->showFullScreen();
}
else
{
window->setFlags(window->flags() | Qt::SplashScreen | Qt::WindowStaysOnTopHint);
window->hide();
}
});
connect(window, &QWindow::visibleChanged, [this](bool v){
if (!v && m_dialog->isVisible()) {
m_dialog->raise();
m_dialog->activateWindow();
}
});
// Handling if the screen in the settings was changed
connect(settings, &Settings::screenChanged, [this, window](int screenIdx)
{
if (screenIdx >= screens().size())
return;
const auto screen = screens()[screenIdx];
const bool wasVisible = window->isVisible();
window->setFlags(window->flags() | Qt::SplashScreen | Qt::WindowStaysOnTopHint);
window->hide();
window->setGeometry(QRect(screen->geometry().topLeft(), QSize(300,200)));
window->setScreen(screen);
window->setGeometry(screen->geometry());
if (wasVisible) {
QTimer::singleShot(0, [this](){
emit m_spotlight->spotActiveChanged(true);
});
}
});
// Open local server for local IPC commands, e.g. from other command line instances
QLocalServer::removeServer(localServerName());
if (m_localServer->listen(localServerName()))
{
connect(m_localServer, &QLocalServer::newConnection, [this]()
{
while(QLocalSocket *clientConnection = m_localServer->nextPendingConnection())
{
connect(clientConnection, &QLocalSocket::readyRead, [this, clientConnection]() {
this->readCommand(clientConnection);
});
connect(clientConnection, &QLocalSocket::disconnected, [this, clientConnection]() {
const auto it = m_commandConnections.find(clientConnection);
if (it != m_commandConnections.end()) {
m_commandConnections.erase(it);
}
clientConnection->close();
clientConnection->deleteLater();
});
// Timeout timer - if after 5 seconds the connection is still open just disconnect...
const auto clientConnPtr = QPointer<QLocalSocket>(clientConnection);
QTimer::singleShot(5000, [clientConnPtr](){
if (clientConnPtr) {
// time out
clientConnPtr->disconnectFromServer();
}
});
m_commandConnections.emplace(clientConnection, 0);
}
});
}
else
{
qDebug() << "Error starting local socket for inter-process communication.";
}
}
ProjecteurApplication::~ProjecteurApplication()
{
if (m_localServer) m_localServer->close();
}
void ProjecteurApplication::readCommand(QLocalSocket* clientConnection)
{
auto it = m_commandConnections.find(clientConnection);
if (it == m_commandConnections.end()) {
return;
}
quint32& commandSize = it->second;
// Read size of command (always quint32) if not already done.
if (commandSize == 0) {
if (clientConnection->bytesAvailable() < static_cast<int>(sizeof(quint32)))
return;
QDataStream in(clientConnection);
in >> commandSize;
if (commandSize > 256)
{
clientConnection->disconnectFromServer();
return ;
}
}
if (clientConnection->bytesAvailable() < commandSize || clientConnection->atEnd()) {
return;
}
const auto command = QString::fromLocal8Bit(clientConnection->read(commandSize));
const QString cmdKey = command.section('=', 0, 0).trimmed();
const QString cmdValue = command.section('=', 1).trimmed();
if (cmdKey == "quit")
{
this->quit();
}
else if (cmdKey == "spot")
{
const bool active = (cmdValue == "on" || cmdValue == "1" || cmdValue == "true");
emit m_spotlight->spotActiveChanged(active);
}
else if (cmdKey == "settings" || cmdKey == "preferences")
{
const bool show = !(cmdValue == "hide" || cmdValue == "0");
showPreferences(show);
}
clientConnection->disconnectFromServer();
}
void ProjecteurApplication::showPreferences(bool show)
{
if (show)
{
m_dialog->show();
m_dialog->raise();
m_dialog->activateWindow();
}
else {
m_dialog->hide();
}
}
ProjecteurCommandClientApp::ProjecteurCommandClientApp(const QString& ipcCommand, int &argc, char **argv)
: QCoreApplication(argc, argv)
{
if (ipcCommand.isEmpty())
{
QMetaObject::invokeMethod(this, "quit", Qt::QueuedConnection);
return;
}
QLocalSocket* const localSocket = new QLocalSocket(this);
connect(localSocket,
static_cast<void (QLocalSocket::*)(QLocalSocket::LocalSocketError)>(&QLocalSocket::error),
[this, localSocket](QLocalSocket::LocalSocketError /*socketError*/) {
qDebug() << "Error sending command: " << localSocket->errorString();
localSocket->close();
QMetaObject::invokeMethod(this, "quit", Qt::QueuedConnection);
});
connect(localSocket, &QLocalSocket::connected, [localSocket, ipcCommand]()
{
const QByteArray commandBlock = [&ipcCommand](){
const QByteArray ipcBytes = ipcCommand.toLocal8Bit();
QByteArray block;
{
QDataStream out(&block, QIODevice::WriteOnly);
out << static_cast<quint32>(ipcBytes.size());
}
block.append(ipcBytes);
return block;
}();
localSocket->write(commandBlock);
localSocket->flush();
localSocket->disconnectFromServer();
});
connect(localSocket, &QLocalSocket::disconnected, [this, localSocket]() {
localSocket->close();
QMetaObject::invokeMethod(this, "quit", Qt::QueuedConnection);
});
localSocket->connectToServer(localServerName());
}