| 1 |
/* BEGIN_COMMON_COPYRIGHT_HEADER (c)LGPL2+ |
| 2 |
* |
| 3 |
* LXQt - a lightweight, Qt based, desktop toolset https://lxqt.org |
| 4 |
* |
| 5 |
* Copyright: 2023 LXQt team Authors: |
| 6 |
* Filippo Gentile <[email protected]> |
| 7 |
* |
| 8 |
* This program or library is free software; you can redistribute it and/or modify it under the terms of the |
| 9 |
* GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the |
| 10 |
* License, or (at your option) any later version. |
| 11 |
* |
| 12 |
* This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the |
| 13 |
* implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public |
| 14 |
* License for more details. |
| 15 |
* |
| 16 |
* You should have received a copy of the GNU Lesser General Public License along with this library; if not, |
| 17 |
* write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 |
* |
| 19 |
* END_COMMON_COPYRIGHT_HEADER */ |
| 20 |
|
| 21 |
#include "wayfire-common.h" |
| 22 |
|
| 23 |
// Socket related |
| 24 |
#include <sys/socket.h> |
| 25 |
#include <sys/un.h> |
| 26 |
|
| 27 |
// Other headers |
| 28 |
#include <unistd.h> |
| 29 |
#include <errno.h> |
| 30 |
|
| 31 |
|
| 32 |
#define QSL QStringLiteral |
| 33 |
|
| 34 |
LXQt::Panel::WayfireImpl::WayfireImpl(QObject *parent) : QThread(parent) |
| 35 |
{ |
| 36 |
wfSock.fd = -1; |
| 37 |
} |
| 38 |
|
| 39 |
LXQt::Panel::WayfireImpl::~WayfireImpl() |
| 40 |
{ |
| 41 |
stop(); |
| 42 |
wait(1000); |
| 43 |
} |
| 44 |
|
| 45 |
void LXQt::Panel::WayfireImpl::stop() |
| 46 |
{ |
| 47 |
mTerminate = true; |
| 48 |
|
| 49 |
if (wfSock.fd != -1) |
| 50 |
{ |
| 51 |
close(wfSock.fd); |
| 52 |
wfSock.fd = -1; |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
uint32_t LXQt::Panel::WayfireImpl::request(QJsonDocument req) |
| 57 |
{ |
| 58 |
QMutexLocker locker(&mutex); |
| 59 |
|
| 60 |
if (!mConnected) |
| 61 |
{ |
| 62 |
return 0; |
| 63 |
} |
| 64 |
|
| 65 |
writeJson(req); |
| 66 |
|
| 67 |
// Generate a unique request ID |
| 68 |
static std::atomic<uint32_t> requestCounter(0); |
| 69 |
uint32_t reqId = ++requestCounter; |
| 70 |
|
| 71 |
if (requestCounter == UINT32_MAX) |
| 72 |
{ |
| 73 |
requestCounter = 1; |
| 74 |
} |
| 75 |
|
| 76 |
mPendingRequests << reqId; |
| 77 |
return reqId; |
| 78 |
} |
| 79 |
|
| 80 |
void LXQt::Panel::WayfireImpl::run() |
| 81 |
{ |
| 82 |
emit started(); |
| 83 |
|
| 84 |
while (true) |
| 85 |
{ |
| 86 |
int nready = poll(&wfSock, 1, 10); |
| 87 |
|
| 88 |
/** Something went wrong while polling */ |
| 89 |
if (nready < 0) |
| 90 |
{ |
| 91 |
qWarning() << "[Error]:" << strerror(errno); |
| 92 |
} |
| 93 |
|
| 94 |
/** Bye bye..! */ |
| 95 |
if (mTerminate) |
| 96 |
{ |
| 97 |
return; |
| 98 |
} |
| 99 |
|
| 100 |
/** Nothing to read */ |
| 101 |
if (nready == 0) |
| 102 |
{ |
| 103 |
continue; |
| 104 |
} |
| 105 |
|
| 106 |
/** We have something to read. Let's see what it is. */ |
| 107 |
if (wfSock.revents & (POLLRDNORM | POLLERR)) |
| 108 |
{ |
| 109 |
QJsonDocument resp = readJson(); |
| 110 |
|
| 111 |
/** This is an event */ |
| 112 |
if (resp.isObject() && resp.object().contains(QSL("event"))) |
| 113 |
{ |
| 114 |
emit wayfireEvent(resp); |
| 115 |
} |
| 116 |
/** This is the response to a request */ |
| 117 |
else |
| 118 |
{ |
| 119 |
// Lock the mutex |
| 120 |
QMutexLocker locker(&mutex); |
| 121 |
|
| 122 |
uint32_t reqId = mPendingRequests.takeFirst(); |
| 123 |
|
| 124 |
// Emit signal asynchronously |
| 125 |
QMetaObject::invokeMethod(this, [this, reqId, resp] () |
| 126 |
{ |
| 127 |
emit response(reqId, resp); |
| 128 |
}, Qt::QueuedConnection); |
| 129 |
|
| 130 |
emit response(reqId, resp); |
| 131 |
} |
| 132 |
} |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
bool LXQt::Panel::WayfireImpl::writeJson(QJsonDocument j) |
| 137 |
{ |
| 138 |
QByteArray str = j.toJson(QJsonDocument::Compact); |
| 139 |
uint32_t size = str.size(); |
| 140 |
|
| 141 |
// Write the size of the JSON data |
| 142 |
ssize_t ret = write(wfSock.fd, &size, sizeof(size)); |
| 143 |
|
| 144 |
if (ret == -1) |
| 145 |
{ |
| 146 |
// Handle write error |
| 147 |
qWarning() << "Failed to write size to socket:" << strerror(errno); |
| 148 |
// throw std::runtime_error("Failed to write size to socket"); |
| 149 |
} else if (ret != sizeof(size)) |
| 150 |
{ |
| 151 |
// Handle partial write |
| 152 |
qWarning() << "Partial write of size to socket:" << ret << "bytes written, expected" << sizeof(size); |
| 153 |
// throw std::runtime_error("Partial write of size to socket"); |
| 154 |
} |
| 155 |
|
| 156 |
// Write the JSON data |
| 157 |
const char *data = str.constData(); |
| 158 |
ssize_t bytesWritten = 0; |
| 159 |
|
| 160 |
while (bytesWritten < str.size()) |
| 161 |
{ |
| 162 |
ret = write(wfSock.fd, data + bytesWritten, str.size() - bytesWritten); |
| 163 |
|
| 164 |
if (ret == -1) |
| 165 |
{ |
| 166 |
// Handle write error |
| 167 |
qWarning() << "Failed to write JSON data to socket:" << strerror(errno); |
| 168 |
return false; |
| 169 |
} else if (ret == 0) |
| 170 |
{ |
| 171 |
// Handle socket closed by peer |
| 172 |
qWarning() << "Socket closed by peer while writing JSON data"; |
| 173 |
return false; |
| 174 |
} |
| 175 |
|
| 176 |
bytesWritten += ret; |
| 177 |
} |
| 178 |
|
| 179 |
/** We succeeded in writing the JSON data successfully */ |
| 180 |
return true; |
| 181 |
} |
| 182 |
|
| 183 |
QJsonDocument LXQt::Panel::WayfireImpl::readJson() |
| 184 |
{ |
| 185 |
uint32_t msgSize; |
| 186 |
|
| 187 |
if (!readExact(reinterpret_cast<char*>(&msgSize), sizeof(msgSize))) |
| 188 |
{ |
| 189 |
return QJsonDocument(); |
| 190 |
} |
| 191 |
|
| 192 |
QByteArray buffer(msgSize, Qt::Uninitialized); |
| 193 |
if (!readExact(buffer.data(), msgSize)) |
| 194 |
{ |
| 195 |
return QJsonDocument(); |
| 196 |
} |
| 197 |
|
| 198 |
return QJsonDocument::fromJson(buffer); |
| 199 |
} |
| 200 |
|
| 201 |
bool LXQt::Panel::WayfireImpl::readExact(char *buf, uint size) |
| 202 |
{ |
| 203 |
while (size > 0) |
| 204 |
{ |
| 205 |
int ret = read(wfSock.fd, buf, size); |
| 206 |
|
| 207 |
if (ret == -1) |
| 208 |
{ |
| 209 |
qCritical() << "Failed to read from socket: " << strerror(errno); |
| 210 |
return false; |
| 211 |
} |
| 212 |
|
| 213 |
buf += ret; |
| 214 |
size -= ret; |
| 215 |
} |
| 216 |
|
| 217 |
return true; |
| 218 |
} |
| 219 |
|
| 220 |
// Helper function to create a QJsonDocument from an initializer list |
| 221 |
QJsonDocument createJsonObject(std::initializer_list<std::pair<QString, QJsonValue>> initList) |
| 222 |
{ |
| 223 |
QJsonObject obj; |
| 224 |
|
| 225 |
for (const auto& pair : initList) |
| 226 |
{ |
| 227 |
obj.insert(pair.first, pair.second); |
| 228 |
} |
| 229 |
|
| 230 |
return QJsonDocument(obj); |
| 231 |
} |
| 232 |
|
| 233 |
LXQt::Panel::Wayfire::Wayfire(const QString &wfSock) : QObject() |
| 234 |
{ |
| 235 |
impl = new WayfireImpl(this); |
| 236 |
connect(impl, &QThread::finished, impl, &QObject::deleteLater); |
| 237 |
impl->wfSockPath = (wfSock.isEmpty() ? qEnvironmentVariable("WAYFIRE_SOCKET") : wfSock); |
| 238 |
|
| 239 |
/** Always emit this for any wayfire event */ |
| 240 |
connect(impl, &LXQt::Panel::WayfireImpl::wayfireEvent, this, &LXQt::Panel::Wayfire::genericEvent); |
| 241 |
|
| 242 |
/** Parse the events and emit the correct signal */ |
| 243 |
connect(impl, &LXQt::Panel::WayfireImpl::wayfireEvent, this, &LXQt::Panel::Wayfire::parseEvents); |
| 244 |
} |
| 245 |
|
| 246 |
bool LXQt::Panel::Wayfire::connectToServer() const |
| 247 |
{ |
| 248 |
if (!impl) |
| 249 |
return false; |
| 250 |
|
| 251 |
impl->wfSock.fd = socket(AF_UNIX, SOCK_STREAM, 0); |
| 252 |
impl->wfSock.events = POLLRDNORM; |
| 253 |
|
| 254 |
if (impl->wfSock.fd == -1) |
| 255 |
{ |
| 256 |
qCritical() << "Failed to create socket: " << strerror(errno); |
| 257 |
return false; |
| 258 |
} |
| 259 |
|
| 260 |
struct sockaddr_un addr; |
| 261 |
addr.sun_family = AF_UNIX, |
| 262 |
|
| 263 |
strncpy(addr.sun_path, impl->wfSockPath.toUtf8().data(), sizeof(addr.sun_path) - 1); |
| 264 |
addr.sun_path[sizeof(addr.sun_path) - 1] = '\0'; |
| 265 |
|
| 266 |
if (::connect(impl->wfSock.fd, (struct sockaddr*)&addr, sizeof(addr)) == -1) |
| 267 |
{ |
| 268 |
qCritical() << "Failed to connect to socket: " << strerror(errno); |
| 269 |
qCritical() << "Ensure that ipc and ipc-rules plugins are enabled."; |
| 270 |
qCritical() << |
| 271 |
"If these plugins were enabled after starting this session, please restart the session."; |
| 272 |
close(impl->wfSock.fd); |
| 273 |
return false; |
| 274 |
} |
| 275 |
|
| 276 |
impl->mConnected = true; |
| 277 |
|
| 278 |
/** Run the impl in a separate thread */ |
| 279 |
impl->start(); |
| 280 |
|
| 281 |
/** Make a request and forget about it. */ |
| 282 |
QJsonObject request; |
| 283 |
request[QSL("method")] = QSL("window-rules/events/watch"); |
| 284 |
genericRequest(QJsonDocument(request)); |
| 285 |
|
| 286 |
return true; |
| 287 |
} |
| 288 |
|
| 289 |
QJsonArray LXQt::Panel::Wayfire::listOutputs() const |
| 290 |
{ |
| 291 |
QJsonObject request; |
| 292 |
|
| 293 |
request[QSL("method")] = QSL("window-rules/list-outputs"); |
| 294 |
|
| 295 |
QJsonDocument response = genericRequest(QJsonDocument(request)); |
| 296 |
|
| 297 |
if (response.isArray()) |
| 298 |
{ |
| 299 |
return response.array(); |
| 300 |
} |
| 301 |
|
| 302 |
return QJsonArray(); |
| 303 |
} |
| 304 |
|
| 305 |
QJsonObject LXQt::Panel::Wayfire::getOutputInfo(WaylandId opId) const |
| 306 |
{ |
| 307 |
QJsonObject request; |
| 308 |
|
| 309 |
request[QSL("method")] = QSL("window-rules/output-info"); |
| 310 |
request[QSL("data")] = QJsonObject({ |
| 311 |
{QSL("id"), QJsonValue::fromVariant((quint64)opId)}, |
| 312 |
}); |
| 313 |
|
| 314 |
return genericRequest(QJsonDocument(request)).object(); |
| 315 |
} |
| 316 |
|
| 317 |
WaylandId LXQt::Panel::Wayfire::getActiveOutput() const |
| 318 |
{ |
| 319 |
QJsonObject request; |
| 320 |
|
| 321 |
request[QSL("method")] = QSL("window-rules/get-focused-output"); |
| 322 |
|
| 323 |
QJsonObject reply = genericRequest(QJsonDocument(request)).object(); |
| 324 |
|
| 325 |
if (reply[QSL("result")].toString() == QSL("ok")) |
| 326 |
{ |
| 327 |
QJsonObject opInfo = reply[QSL("info")].toObject(); |
| 328 |
uint32_t opId = opInfo[QSL("id")].toInt(); |
| 329 |
|
| 330 |
return WaylandId(opId); |
| 331 |
} |
| 332 |
|
| 333 |
return WaylandId(); |
| 334 |
} |
| 335 |
|
| 336 |
bool LXQt::Panel::Wayfire::focusOutput(WaylandId opId) const |
| 337 |
{ |
| 338 |
QJsonObject request; |
| 339 |
request[QSL("method")] = QSL("oswitch/switch-output"); |
| 340 |
request[QSL("data")] = QJsonObject({ |
| 341 |
{QSL("output-id"), QJsonValue::fromVariant((quint64)opId)}, |
| 342 |
}); |
| 343 |
|
| 344 |
QJsonDocument reply = genericRequest(QJsonDocument(request)); |
| 345 |
|
| 346 |
if (reply[QSL("result")].toString() == QSL("ok")) |
| 347 |
{ |
| 348 |
return true; |
| 349 |
} |
| 350 |
|
| 351 |
return false; |
| 352 |
} |
| 353 |
|
| 354 |
bool LXQt::Panel::Wayfire::showDesktop(WaylandId opId) const |
| 355 |
{ |
| 356 |
QJsonObject request; |
| 357 |
|
| 358 |
request[QSL("method")] = QSL("wm-actions/toggle_showdesktop"); |
| 359 |
request[QSL("data")] = QJsonObject({ |
| 360 |
{QSL("output_id"), QJsonValue::fromVariant((quint64)opId)}, |
| 361 |
}); |
| 362 |
|
| 363 |
QJsonDocument reply = genericRequest(QJsonDocument(request)); |
| 364 |
|
| 365 |
return (reply[QSL("result")].toString() == QSL("ok")); |
| 366 |
} |
| 367 |
|
| 368 |
QJsonArray LXQt::Panel::Wayfire::getWorkspaceSetsInfo() const |
| 369 |
{ |
| 370 |
QJsonObject request; |
| 371 |
request[QSL("method")] = QSL("window-rules/list-wsets"); |
| 372 |
|
| 373 |
return genericRequest(QJsonDocument(request)).array(); |
| 374 |
} |
| 375 |
|
| 376 |
QString LXQt::Panel::Wayfire::getWorkspaceName(int x, const QString &outputName) const |
| 377 |
{ |
| 378 |
QString targetKey = QString::fromUtf8("%1_workspace_%2").arg(outputName).arg(x); |
| 379 |
|
| 380 |
QJsonObject request; |
| 381 |
request[QSL("method")] = QSL("wayfire/get-config-option"); |
| 382 |
request[QSL("data")] = QJsonObject({ |
| 383 |
{QSL("option"), QSL("workspace-names/names")}, |
| 384 |
}); |
| 385 |
|
| 386 |
QJsonObject wsNamesObj = genericRequest(QJsonDocument(request)).object(); |
| 387 |
|
| 388 |
QJsonArray wsNameList = wsNamesObj[QSL("value")].toArray(); |
| 389 |
|
| 390 |
for ( int i = 0; i < wsNameList.size(); i++ ) |
| 391 |
{ |
| 392 |
QJsonArray wsNamePair = wsNameList[i].toArray(); |
| 393 |
|
| 394 |
if (wsNamePair.size() != 2) |
| 395 |
{ |
| 396 |
continue; |
| 397 |
} |
| 398 |
|
| 399 |
QString key = wsNamePair[0].toString(); |
| 400 |
if (key == targetKey) |
| 401 |
{ |
| 402 |
QString name = wsNamePair[1].toString(); |
| 403 |
return name; |
| 404 |
} |
| 405 |
} |
| 406 |
|
| 407 |
return QString(); // the front-end sets the name |
| 408 |
} |
| 409 |
|
| 410 |
bool LXQt::Panel::Wayfire::setWorkspaceName(int, const QString&) const |
| 411 |
{ |
| 412 |
return false; |
| 413 |
} |
| 414 |
|
| 415 |
bool LXQt::Panel::Wayfire::switchToWorkspace(WaylandId opId, int64_t nth, WaylandId viewId) const |
| 416 |
{ |
| 417 |
QJsonObject wsetsInfo = getWorkspaceSetsInfo().at(0).toObject(); |
| 418 |
QJsonObject workspace = wsetsInfo[QSL("workspace")].toObject(); |
| 419 |
int64_t nCols = workspace[QSL("grid_width")].toInt(); |
| 420 |
|
| 421 |
int64_t row = floor((nth - 1) / nCols); |
| 422 |
int64_t col = (nth - 1) % nCols; |
| 423 |
|
| 424 |
QJsonObject request; |
| 425 |
request[QSL("method")] = QSL("vswitch/set-workspace"); |
| 426 |
if (viewId) |
| 427 |
{ |
| 428 |
request[QSL("data")] = QJsonObject({ |
| 429 |
{QSL("output-id"), QJsonValue::fromVariant((quint64)opId)}, |
| 430 |
{QSL("x"), QJsonValue::fromVariant((quint64)col)}, |
| 431 |
{QSL("y"), QJsonValue::fromVariant((quint64)row)}, |
| 432 |
{QSL("view-id"), QJsonValue::fromVariant((quint64)viewId)}, |
| 433 |
}); |
| 434 |
} else |
| 435 |
{ |
| 436 |
request[QSL("data")] = QJsonObject({ |
| 437 |
{QSL("output-id"), QJsonValue::fromVariant((quint64)opId)}, |
| 438 |
{QSL("x"), QJsonValue::fromVariant((quint64)col)}, |
| 439 |
|
| 440 |
{QSL("y"), QJsonValue::fromVariant((quint64)row)}, |
| 441 |
}); |
| 442 |
} |
| 443 |
|
| 444 |
QJsonDocument reply = genericRequest(QJsonDocument(request)); |
| 445 |
|
| 446 |
return (reply[QSL("result")].toString() == QSL("ok")); |
| 447 |
} |
| 448 |
|
| 449 |
QJsonArray LXQt::Panel::Wayfire::listViews() const |
| 450 |
{ |
| 451 |
QJsonObject request; |
| 452 |
request[QSL("method")] = QSL("window-rules/list-views"); |
| 453 |
|
| 454 |
QJsonArray response = genericRequest(QJsonDocument(request)).array(); |
| 455 |
|
| 456 |
QJsonArray views; |
| 457 |
for ( QJsonValue viewVal : response ) |
| 458 |
{ |
| 459 |
if (viewVal.isObject()) |
| 460 |
{ |
| 461 |
QJsonObject view = viewVal.toObject(); |
| 462 |
// Ghost windows of Xwayland |
| 463 |
if (view[QSL("pid")].toInt() == -1) |
| 464 |
{ |
| 465 |
continue; |
| 466 |
} |
| 467 |
|
| 468 |
// Proper toplevel and mapped views |
| 469 |
if (view[QSL("role")].toString() == QSL("toplevel") && view[QSL("mapped")].toBool()) |
| 470 |
{ |
| 471 |
views << view; |
| 472 |
} |
| 473 |
} |
| 474 |
} |
| 475 |
|
| 476 |
return views; |
| 477 |
} |
| 478 |
|
| 479 |
WaylandId LXQt::Panel::Wayfire::getActiveView() const |
| 480 |
{ |
| 481 |
QJsonObject request; |
| 482 |
|
| 483 |
request[QSL("method")] = QSL("window-rules/get-focused-view"); |
| 484 |
|
| 485 |
QJsonObject reply = genericRequest(QJsonDocument(request)).object(); |
| 486 |
|
| 487 |
if (reply[QSL("result")].toString() == QSL("ok")) |
| 488 |
{ |
| 489 |
QJsonObject viewInfo = reply[QSL("info")].toObject(); |
| 490 |
uint32_t viewId = viewInfo[QSL("id")].toInt(); |
| 491 |
|
| 492 |
return WaylandId(viewId); |
| 493 |
} |
| 494 |
|
| 495 |
return WaylandId(); |
| 496 |
} |
| 497 |
|
| 498 |
QJsonObject LXQt::Panel::Wayfire::getViewInfo(WaylandId viewId) const |
| 499 |
{ |
| 500 |
QJsonObject request; |
| 501 |
|
| 502 |
request[QSL("method")] = QSL("window-rules/view-info"); |
| 503 |
request[QSL("data")] = QJsonObject({ |
| 504 |
{QSL("id"), QJsonValue::fromVariant((quint64)viewId)} |
| 505 |
}); |
| 506 |
|
| 507 |
QJsonObject reply = genericRequest(QJsonDocument(request)).object(); |
| 508 |
|
| 509 |
if (reply[QSL("result")].toString() == QSL("ok")) |
| 510 |
{ |
| 511 |
return reply[QSL("info")].toObject(); |
| 512 |
} |
| 513 |
|
| 514 |
return QJsonObject(); |
| 515 |
} |
| 516 |
|
| 517 |
bool LXQt::Panel::Wayfire::focusView(WaylandId viewId) const |
| 518 |
{ |
| 519 |
QJsonObject viewInfo = getViewInfo(viewId); |
| 520 |
|
| 521 |
if ((viewInfo.isEmpty() == false) && (viewInfo[QSL("minimized")].toBool() == true)) |
| 522 |
{ |
| 523 |
minimizeView(viewId, false); |
| 524 |
} |
| 525 |
|
| 526 |
QJsonObject request; |
| 527 |
request[QSL("method")] = QSL("window-rules/focus-view"); |
| 528 |
request[QSL("data")] = QJsonObject({ |
| 529 |
{QSL("id"), QJsonValue::fromVariant((quint64)viewId)} |
| 530 |
}); |
| 531 |
|
| 532 |
QJsonObject reply = genericRequest(QJsonDocument(request)).object(); |
| 533 |
|
| 534 |
return (reply[QSL("result")].toString() == QSL("ok")); |
| 535 |
} |
| 536 |
|
| 537 |
bool LXQt::Panel::Wayfire::minimizeView(WaylandId viewId, bool yes) const |
| 538 |
{ |
| 539 |
QJsonObject request; |
| 540 |
|
| 541 |
request[QSL("method")] = QSL("wm-actions/set-minimized"); |
| 542 |
request[QSL("data")] = QJsonObject({ |
| 543 |
{QSL("view_id"), QJsonValue::fromVariant((quint64)viewId)}, |
| 544 |
{QSL("state"), yes} |
| 545 |
}); |
| 546 |
|
| 547 |
QJsonDocument reply = genericRequest(QJsonDocument(request)); |
| 548 |
|
| 549 |
if (reply[QSL("result")] != QSL("ok")) |
| 550 |
{ |
| 551 |
qWarning() << QJsonDocument(reply).toJson().data() << "\n"; |
| 552 |
} |
| 553 |
|
| 554 |
return (reply[QSL("result")].toString() == QSL("ok")); |
| 555 |
} |
| 556 |
|
| 557 |
bool LXQt::Panel::Wayfire::maximizeView(WaylandId viewId, int edges) const |
| 558 |
{ |
| 559 |
QJsonObject request; |
| 560 |
|
| 561 |
/** |
| 562 |
* Support for this does not yet exist in Wayfire. Pending PR from Marcus Britanicus. |
| 563 |
*/ |
| 564 |
|
| 565 |
// request[QSL("method")] = QSL("wm-actions/set-tiled"); |
| 566 |
// request[QSL("data")] = QJsonObject({ |
| 567 |
// {QSL("view_id"), QJsonValue::fromVariant((quint64)viewId)}, |
| 568 |
// {QSL("view-id"), QJsonValue::fromVariant((quint64)viewId)}, |
| 569 |
// {QSL("id"), QJsonValue::fromVariant((quint64)viewId)}, |
| 570 |
// {QSL("edges"), edges} |
| 571 |
// }); |
| 572 |
|
| 573 |
request[QSL("method")] = (edges ? QSL("grid/slot_c") : QSL("grid/restore")); |
| 574 |
request[QSL("data")] = QJsonObject({ |
| 575 |
{QSL("view_id"), QJsonValue::fromVariant((quint64)viewId)}, |
| 576 |
}); |
| 577 |
|
| 578 |
QJsonDocument reply = genericRequest(QJsonDocument(request)); |
| 579 |
|
| 580 |
if (reply[QSL("result")] != QSL("ok")) |
| 581 |
{ |
| 582 |
qWarning() << QJsonDocument(reply).toJson().data() << "\n"; |
| 583 |
} |
| 584 |
|
| 585 |
return (reply[QSL("result")].toString() == QSL("ok")); |
| 586 |
} |
| 587 |
|
| 588 |
bool LXQt::Panel::Wayfire::fullscreenView(WaylandId viewId, bool yes) const |
| 589 |
{ |
| 590 |
QJsonObject request; |
| 591 |
|
| 592 |
request[QSL("method")] = QSL("wm-actions/set-fullscreen"); |
| 593 |
request[QSL("data")] = QJsonObject({ |
| 594 |
{QSL("view_id"), QJsonValue::fromVariant((quint64)viewId)}, |
| 595 |
{QSL("state"), yes} |
| 596 |
}); |
| 597 |
|
| 598 |
QJsonDocument reply = genericRequest(QJsonDocument(request)); |
| 599 |
|
| 600 |
return (reply[QSL("result")].toString() == QSL("ok")); |
| 601 |
} |
| 602 |
|
| 603 |
bool LXQt::Panel::Wayfire::restoreView(WaylandId viewId) const |
| 604 |
{ |
| 605 |
QJsonObject viewInfo = getViewInfo(viewId); |
| 606 |
|
| 607 |
if (viewInfo.isEmpty()) |
| 608 |
{ |
| 609 |
return false; |
| 610 |
} |
| 611 |
|
| 612 |
/** If it's minimized, unminimize it */ |
| 613 |
if (viewInfo[QSL("minimized")] == true) |
| 614 |
{ |
| 615 |
return minimizeView(viewId, false); |
| 616 |
} |
| 617 |
/** The view is fullscreened, un-fullscreen it */ |
| 618 |
else if (viewInfo[QSL("fullscreen")] == true) |
| 619 |
{ |
| 620 |
return fullscreenView(viewId, false); |
| 621 |
} |
| 622 |
/** The view is maximized, unmaximize it */ |
| 623 |
else if (viewInfo[QSL("tiled-edges")] != 0) |
| 624 |
{ |
| 625 |
return maximizeView(viewId, false); |
| 626 |
} |
| 627 |
|
| 628 |
return false; |
| 629 |
} |
| 630 |
|
| 631 |
bool LXQt::Panel::Wayfire::sendViewToWorkspace(WaylandId viewId, int nth) const |
| 632 |
{ |
| 633 |
QJsonObject request; |
| 634 |
|
| 635 |
/** Set view sticky */ |
| 636 |
if ( nth == 0 ) { |
| 637 |
QJsonObject viewInfo = getViewInfo( viewId ); |
| 638 |
request[QSL("method")] = QSL("wm-actions/set-sticky"); |
| 639 |
request[QSL("data")] = QJsonObject({ |
| 640 |
{QSL("view-id"), QJsonValue::fromVariant((quint64)viewId)}, |
| 641 |
{QSL("view_id"), QJsonValue::fromVariant((quint64)viewId)}, |
| 642 |
{QSL("state"), !viewInfo[QSL("sticky")].toBool()}, |
| 643 |
}); |
| 644 |
} |
| 645 |
|
| 646 |
/** Send view to a specific desktop */ |
| 647 |
else { |
| 648 |
QJsonObject wsetsInfo = getWorkspaceSetsInfo().at(0).toObject(); |
| 649 |
QJsonObject workspace = wsetsInfo[QSL("workspace")].toObject(); |
| 650 |
QJsonObject viewInfo = getViewInfo(viewId); |
| 651 |
|
| 652 |
int64_t nCols = workspace[QSL("grid_width")].toInt(); |
| 653 |
|
| 654 |
int64_t row = floor((nth - 1) / nCols); |
| 655 |
int64_t col = (nth - 1) % nCols; |
| 656 |
|
| 657 |
quint64 opId = viewInfo[QSL("output-id")].toInt(); |
| 658 |
|
| 659 |
request[QSL("method")] = QSL("vswitch/send-view"); |
| 660 |
request[QSL("data")] = QJsonObject({ |
| 661 |
{QSL("output-id"), QJsonValue::fromVariant((quint64)opId)}, |
| 662 |
{QSL("x"), QJsonValue::fromVariant((quint64)col)}, |
| 663 |
{QSL("y"), QJsonValue::fromVariant((quint64)row)}, |
| 664 |
{QSL("view-id"), QJsonValue::fromVariant((quint64)viewId)}, |
| 665 |
{QSL("view_id"), QJsonValue::fromVariant((quint64)viewId)}, |
| 666 |
}); |
| 667 |
} |
| 668 |
|
| 669 |
QJsonDocument reply = genericRequest(QJsonDocument(request)); |
| 670 |
|
| 671 |
return (reply[QSL("result")].toString() == QSL("ok")); |
| 672 |
} |
| 673 |
|
| 674 |
bool LXQt::Panel::Wayfire::closeView(WaylandId viewId) const |
| 675 |
{ |
| 676 |
QJsonObject request; |
| 677 |
|
| 678 |
request[QSL("method")] = QSL("window-rules/close-view"); |
| 679 |
request[QSL("data")] = QJsonObject({ |
| 680 |
{QSL("id"), QJsonValue::fromVariant((quint64)viewId)} |
| 681 |
}); |
| 682 |
|
| 683 |
QJsonDocument reply = genericRequest(QJsonDocument(request)); |
| 684 |
|
| 685 |
return (reply[QSL("result")].toString() == QSL("ok")); |
| 686 |
} |
| 687 |
|
| 688 |
QJsonDocument LXQt::Panel::Wayfire::genericRequest(QJsonDocument request) const |
| 689 |
{ |
| 690 |
if (!impl || !impl->mConnected) |
| 691 |
{ |
| 692 |
QJsonDocument reply{ |
| 693 |
{QSL("result"), QSL("failed")} |
| 694 |
}; |
| 695 |
return reply; |
| 696 |
} |
| 697 |
|
| 698 |
uint32_t reqId = impl->request(request); |
| 699 |
std::shared_ptr<QJsonDocument> reply = std::make_shared<QJsonDocument>(); |
| 700 |
|
| 701 |
QEventLoop loop; |
| 702 |
|
| 703 |
auto connection = connect( |
| 704 |
impl, &LXQt::Panel::WayfireImpl::response, &loop, |
| 705 |
[&reply, reqId, &loop] (uint32_t id, QJsonDocument response) |
| 706 |
{ |
| 707 |
if (id == reqId) |
| 708 |
{ |
| 709 |
*reply = response; // Update the content of the shared_ptr |
| 710 |
loop.quit(); |
| 711 |
} |
| 712 |
}); |
| 713 |
|
| 714 |
loop.exec(); |
| 715 |
|
| 716 |
// Disconnect the signal-slot connection to avoid any potential issues |
| 717 |
disconnect(connection); |
| 718 |
|
| 719 |
// Return the QJsonDocument, not the shared_ptr |
| 720 |
return *reply; |
| 721 |
} |
| 722 |
|
| 723 |
void LXQt::Panel::Wayfire::parseEvents(QJsonDocument response) |
| 724 |
{ |
| 725 |
QString event = response[QSL("event")].toString(); |
| 726 |
|
| 727 |
if (event == QSL("view-mapped")) |
| 728 |
{ |
| 729 |
emit viewMapped(response); |
| 730 |
} else if (event == QSL("view-focused")) |
| 731 |
{ |
| 732 |
emit viewFocused(response); |
| 733 |
} else if (event == QSL("view-title-changed")) |
| 734 |
{ |
| 735 |
emit viewTitleChanged(response); |
| 736 |
} else if (event == QSL("view-app-id-changed")) |
| 737 |
{ |
| 738 |
emit viewAppIdChanged(response); |
| 739 |
} else if (event == QSL("view-geometry-changed")) |
| 740 |
{ |
| 741 |
emit viewGeometryChanged(response); |
| 742 |
} else if (event == QSL("view-tiled")) |
| 743 |
{ |
| 744 |
emit viewTiled(response); |
| 745 |
} else if (event == QSL("view-minimized")) |
| 746 |
{ |
| 747 |
emit viewMinimized(response); |
| 748 |
} else if (event == QSL("view-set-output")) |
| 749 |
{ |
| 750 |
emit viewOutputChanged(response); |
| 751 |
} else if (event == QSL("view-workspace-changed")) |
| 752 |
{ |
| 753 |
emit viewWorkspaceChanged(response); |
| 754 |
} else if (event == QSL("view-unmapped")) |
| 755 |
{ |
| 756 |
emit viewUnmapped(response); |
| 757 |
} else if (event == QSL("output-added")) |
| 758 |
{ |
| 759 |
emit outputAdded(response); |
| 760 |
} else if (event == QSL("output-removed")) |
| 761 |
{ |
| 762 |
emit outputRemoved(response); |
| 763 |
} else if (event == QSL("output-gain-focus")) |
| 764 |
{ |
| 765 |
emit outputFocused(response); |
| 766 |
} else if (event == QSL("output-wset-changed")) |
| 767 |
{ |
| 768 |
emit workspaceSetChanged(response); |
| 769 |
} else if (event == QSL("wset-workspace-changed")) |
| 770 |
{ |
| 771 |
emit workspaceChanged(response); |
| 772 |
} else |
| 773 |
{ |
| 774 |
emit genericEvent(response); |
| 775 |
} |
| 776 |
} |