Debian Codegraph @

panel/backends/wayland/wlroots/lxqttaskbarwlrwm.cpp

raw
1 #include "lxqttaskbarwlrwm.h"
2
3 #include <QString>
4 #include <QFuture>
5 #include <QtConcurrent>
6 #include <QGuiApplication>
7 #include <QMimeData>
8 #include <QSet>
9 #include <QUrl>
10 #include <QUuid>
11 #include <QWaylandClientExtension>
12 #include <QWindow>
13
14 #include <xdgicon.h>
15
16 #include <qpa/qplatformnativeinterface.h>
17
18 #include <fcntl.h>
19 #include <sys/poll.h>
20 #include <unistd.h>
21
22 #include <filesystem>
23
24 QString U8Str( const char *str ) {
25 return QString::fromUtf8( str );
26 }
27
28 static inline QString getPixmapIcon(QString name)
29 {
30 QStringList paths{
31 U8Str("/usr/local/share/pixmaps/"),
32 U8Str("/usr/share/pixmaps/"),
33 };
34
35 QStringList sfxs{
36 U8Str( ".svg" ), U8Str( ".png" ), U8Str( ".xpm" )
37 };
38
39 for (QString path: paths)
40 {
41 for (QString sfx: sfxs)
42 {
43 if (QFile::exists(path + name + sfx))
44 {
45 return path + name + sfx;
46 }
47 }
48 }
49
50 return QString();
51 }
52
53
54 QIcon getIconForAppId(QString mAppId)
55 {
56 if (mAppId.isEmpty() or (mAppId == U8Str("Unknown")))
57 {
58 return QIcon();
59 }
60
61 /** Wine apps */
62 if (mAppId.endsWith(U8Str(".exe")))
63 {
64 return QIcon::fromTheme(U8Str("wine"));
65 }
66
67 /** Check if a theme icon exists called @mAppId */
68 if (QIcon::hasThemeIcon(mAppId))
69 {
70 return QIcon::fromTheme(mAppId);
71 }
72
73 /** Check if the theme icon is @mAppId, but all lower-case letters */
74 else if (QIcon::hasThemeIcon(mAppId.toLower()))
75 {
76 return QIcon::fromTheme(mAppId.toLower());
77 }
78
79 QStringList appDirs = {
80 QDir::home().filePath(U8Str(".local/share/applications/")),
81 U8Str("/usr/local/share/applications/"),
82 U8Str("/usr/share/applications/"),
83 };
84
85 /**
86 * Assume mAppId == desktop-file-name (ideal situation)
87 * or mAppId.toLower() == desktop-file-name (cheap fallback)
88 */
89 QString iconName;
90
91 for (QString path: appDirs)
92 {
93 /** Get the icon name from desktop (mAppId: as it is) */
94 if (QFile::exists(path + mAppId + U8Str(".desktop")))
95 {
96 QSettings desktop(path + mAppId + U8Str(".desktop"), QSettings::IniFormat);
97 iconName = desktop.value(U8Str("Desktop Entry/Icon")).toString();
98 }
99
100 /** Get the icon name from desktop (mAppId: all lower-case letters) */
101 else if (QFile::exists(path + mAppId.toLower() + U8Str(".desktop")))
102 {
103 QSettings desktop(path + mAppId.toLower() + U8Str(".desktop"), QSettings::IniFormat);
104 iconName = desktop.value(U8Str("Desktop Entry/Icon")).toString();
105 }
106
107 /** No icon specified: try else-where */
108 if (iconName.isEmpty())
109 {
110 continue;
111 }
112
113 /** We got an iconName, and it's in the current theme */
114 if (QIcon::hasThemeIcon(iconName))
115 {
116 return QIcon::fromTheme(iconName);
117 }
118
119 /** Not a theme icon, but an absolute path */
120 else if (QFile::exists(iconName))
121 {
122 return QIcon(iconName);
123 }
124
125 /** Not theme icon or absolute path. So check /usr/share/pixmaps/ */
126 else
127 {
128 iconName = getPixmapIcon(iconName);
129
130 if (not iconName.isEmpty())
131 {
132 return QIcon(iconName);
133 }
134 }
135 }
136
137 /* Check all desktop files for @mAppId */
138 for (QString path: appDirs)
139 {
140 QStringList desktops = QDir(path).entryList({ U8Str("*.desktop") });
141 for (QString dskf: desktops)
142 {
143 QSettings desktop(path + dskf, QSettings::IniFormat);
144
145 QString exec = desktop.value(U8Str("Desktop Entry/Exec"), U8Str("abcd1234/-")).toString();
146 QString name = desktop.value(U8Str("Desktop Entry/Name"), U8Str("abcd1234/-")).toString();
147 QString cls = desktop.value(U8Str("Desktop Entry/StartupWMClass"), U8Str("abcd1234/-")).toString();
148
149 QString execPath = U8Str(std::filesystem::path(exec.toStdString()).filename().c_str());
150
151 if (mAppId.compare(execPath, Qt::CaseInsensitive) == 0)
152 {
153 iconName = desktop.value(U8Str("Desktop Entry/Icon")).toString();
154 }
155
156 else if (mAppId.compare(name, Qt::CaseInsensitive) == 0)
157 {
158 iconName = desktop.value(U8Str("Desktop Entry/Icon")).toString();
159 }
160
161 else if (mAppId.compare(cls, Qt::CaseInsensitive) == 0)
162 {
163 iconName = desktop.value(U8Str("Desktop Entry/Icon")).toString();
164 }
165
166 if (not iconName.isEmpty())
167 {
168 if (QIcon::hasThemeIcon(iconName))
169 {
170 return QIcon::fromTheme(iconName);
171 }
172
173 else if (QFile::exists(iconName))
174 {
175 return QIcon(iconName);
176 }
177
178 else
179 {
180 iconName = getPixmapIcon(iconName);
181
182 if (not iconName.isEmpty())
183 {
184 return QIcon(iconName);
185 }
186 }
187 }
188 }
189 }
190
191 iconName = getPixmapIcon(iconName);
192
193 if (not iconName.isEmpty())
194 {
195 return QIcon(iconName);
196 }
197
198 return QIcon();
199 }
200
201
202 static inline wl_seat *get_seat()
203 {
204 QPlatformNativeInterface *native = QGuiApplication::platformNativeInterface();
205
206 if (!native)
207 {
208 return nullptr;
209 }
210
211 struct wl_seat *seat = reinterpret_cast<wl_seat *>(native->nativeResourceForIntegration("wl_seat"));
212
213 return seat;
214 }
215
216
217 /*
218 * LXQtTaskbarWlrootsWindowManagment
219 */
220
221 LXQtTaskbarWlrootsWindowManagment::LXQtTaskbarWlrootsWindowManagment() : QWaylandClientExtensionTemplate(version)
222 {
223 /** Automatically destroy thie object */
224 connect(
225 this, &QWaylandClientExtension::activeChanged, this, [ this ] {
226 if (!isActive())
227 {
228 zwlr_foreign_toplevel_manager_v1_destroy(object());
229 }
230 });
231 }
232
233
234 LXQtTaskbarWlrootsWindowManagment::~LXQtTaskbarWlrootsWindowManagment()
235 {
236 if (isActive())
237 {
238 zwlr_foreign_toplevel_manager_v1_destroy(object());
239 }
240 }
241
242
243 void LXQtTaskbarWlrootsWindowManagment::zwlr_foreign_toplevel_manager_v1_toplevel(struct ::zwlr_foreign_toplevel_handle_v1 *toplevel)
244 {
245 /**
246 * A window was created.
247 * Wait for the window to become ready, i.e. wait for done() event to be sent by the compositor.
248 * Once we recieve done(), emit the windowReady() signal.
249 */
250
251 auto w = new LXQtTaskbarWlrootsWindow(toplevel);
252
253 connect(w, &LXQtTaskbarWlrootsWindow::windowReady, [w, this] () {
254 emit windowCreated(w->getWindowId());
255 });
256 }
257
258
259 /*
260 * LXQtTaskbarWlrootsWindow
261 */
262
263 LXQtTaskbarWlrootsWindow::LXQtTaskbarWlrootsWindow(::zwlr_foreign_toplevel_handle_v1 *id) : zwlr_foreign_toplevel_handle_v1(id)
264 {
265 ID = id;
266 }
267
268
269 LXQtTaskbarWlrootsWindow::~LXQtTaskbarWlrootsWindow()
270 {
271 destroy();
272 }
273
274
275 void LXQtTaskbarWlrootsWindow::activate()
276 {
277 /**
278 * Activate on default seat.
279 * TODO: Worry about multi-seat setups, when we have no other worries :P
280 */
281 zwlr_foreign_toplevel_handle_v1::activate(get_seat());
282 }
283
284
285 void LXQtTaskbarWlrootsWindow::zwlr_foreign_toplevel_handle_v1_title(const QString& title)
286 {
287 /** Store the incoming title in pending */
288 m_pendingState.title = title;
289 m_pendingState.titleChanged = true;
290 }
291
292
293 void LXQtTaskbarWlrootsWindow::zwlr_foreign_toplevel_handle_v1_app_id(const QString& app_id)
294 {
295 /** Store the incoming appId in pending */
296 m_pendingState.appId = app_id;
297 m_pendingState.appIdChanged = true;
298
299 /** Update the icon */
300 this->icon = getIconForAppId(app_id);
301
302 /** We did not get any icon from app-id. Let's use application-x-executable */
303 if (this->icon.pixmap(64).width() == 0)
304 {
305 this->icon = XdgIcon::fromTheme(QString::fromUtf8("application-x-executable"));
306 }
307 }
308
309
310 void LXQtTaskbarWlrootsWindow::zwlr_foreign_toplevel_handle_v1_output_enter(struct ::wl_output *output)
311 {
312 /** This view was added to an output */
313 m_pendingState.outputs << output;
314 m_pendingState.outputsChanged = true;
315 }
316
317
318 void LXQtTaskbarWlrootsWindow::zwlr_foreign_toplevel_handle_v1_output_leave(struct ::wl_output *output)
319 {
320 /** This view was removed from an output; store it in pending. */
321 m_pendingState.outputsLeft << output;
322
323 if (m_pendingState.outputs.contains(output))
324 {
325 m_pendingState.outputs.removeAll(output);
326 }
327
328 m_pendingState.outputsChanged = true;
329 }
330
331
332 void LXQtTaskbarWlrootsWindow::zwlr_foreign_toplevel_handle_v1_state(wl_array *state)
333 {
334 /** State of this window was changed; store it in pending. */
335 auto *states = static_cast<uint32_t *>(state->data);
336 int numStates = static_cast<int>(state->size / sizeof(uint32_t));
337
338 for (int i = 0; i < numStates; i++)
339 {
340 switch ((uint32_t)states[ i ])
341 {
342 case ZWLR_FOREIGN_TOPLEVEL_HANDLE_V1_STATE_MAXIMIZED: {
343 m_pendingState.maximized = true;
344 break;
345 }
346
347 case ZWLR_FOREIGN_TOPLEVEL_HANDLE_V1_STATE_MINIMIZED: {
348 m_pendingState.minimized = true;
349 m_pendingState.activated = false; // a minimized window isn't active
350 break;
351 }
352
353 case ZWLR_FOREIGN_TOPLEVEL_HANDLE_V1_STATE_ACTIVATED: {
354 m_pendingState.activated = true;
355 m_pendingState.minimized = false; // an active window isn't minimized
356 break;
357 }
358
359 case ZWLR_FOREIGN_TOPLEVEL_HANDLE_V1_STATE_FULLSCREEN: {
360 m_pendingState.fullscreen = true;
361 break;
362 }
363 }
364 }
365
366 /* WARNING:
367 These are needed with all states, also when "numStates" is zero.
368 Without them, states might be incorrect under some circumstances, e.g.,
369 an active task button might be deactivated if an overlay window is shown.
370 */
371 m_pendingState.activatedChanged = true;
372 m_pendingState.maximizedChanged = (windowState.maximized != m_pendingState.maximized);
373 m_pendingState.minimizedChanged = (windowState.minimized != m_pendingState.minimized);
374 m_pendingState.fullscreenChanged = (windowState.fullscreen != m_pendingState.fullscreen);
375 }
376
377
378 void LXQtTaskbarWlrootsWindow::zwlr_foreign_toplevel_handle_v1_done()
379 {
380 /**
381 * All the states/properties have been sent.
382 * We can now emit the signals and clear the pending state:
383 * 1. Update all the variables first.
384 * 2. Then clear the m_pendingState.<variable>
385 * 3. Emit the changed signals.
386 * 4. Finally, clean the m_pendingState.<variable>Changed flags.
387 */
388
389 // (1) title, if it changed
390 if (m_pendingState.titleChanged)
391 {
392 windowState.title = m_pendingState.title;
393 }
394
395 // (2) appId, if it changed
396 if (m_pendingState.appIdChanged)
397 {
398 windowState.appId = m_pendingState.appId;
399 }
400
401 // (3) outputs, if they changed
402 if (m_pendingState.outputsChanged)
403 {
404 for (::wl_output *op: m_pendingState.outputsLeft)
405 {
406 if (windowState.outputs.contains(op))
407 {
408 windowState.outputs.removeAll(op);
409 }
410 }
411
412 for (::wl_output *op: m_pendingState.outputs)
413 {
414 if (!windowState.outputs.contains(op))
415 {
416 windowState.outputs << op;
417 }
418 }
419 }
420
421 // (4) states, if they changed.
422 if (m_pendingState.maximizedChanged)
423 {
424 windowState.maximized = m_pendingState.maximized;
425 }
426
427 if (m_pendingState.minimizedChanged)
428 {
429 windowState.minimized = m_pendingState.minimized;
430 }
431
432 if (m_pendingState.activatedChanged)
433 {
434 windowState.activated = m_pendingState.activated;
435 }
436
437 if (m_pendingState.fullscreenChanged)
438 {
439 windowState.fullscreen = m_pendingState.fullscreen;
440 }
441
442 // (5) parent, if it changed.
443 if (m_pendingState.parentChanged)
444 {
445 if (m_pendingState.parent)
446 {
447 setParentWindow(new LXQtTaskbarWlrootsWindow(m_pendingState.parent));
448 }
449
450 else
451 {
452 setParentWindow(nullptr);
453 }
454 }
455
456 /** 2. Clear all m_pendingState.<variables> for next run */
457 m_pendingState.title = QString();
458 m_pendingState.appId = QString();
459 m_pendingState.outputs.clear();
460 m_pendingState.outputsLeft.clear();
461 m_pendingState.maximized = false;
462 m_pendingState.minimized = false;
463 m_pendingState.activated = false;
464 m_pendingState.fullscreen = false;
465 m_pendingState.parent = nullptr;
466
467 /**
468 * 3. Emit signals
469 * (a) First time done was emitted after the window was created.
470 * (b) Other times.
471 */
472
473 /** (a) First time done was emitted */
474 if (initDone == false)
475 {
476 /**
477 * All the states/properties are already set.
478 * Any query will give valid results.
479 */
480 initDone = true;
481 emit windowReady();
482 }
483
484 /** (b) All the subsequent times */
485 else
486 {
487 if (m_pendingState.titleChanged)
488 emit titleChanged();
489 if (m_pendingState.appIdChanged)
490 emit appIdChanged();
491 if (m_pendingState.outputsChanged)
492 emit outputsChanged();
493 if (m_pendingState.maximizedChanged)
494 emit maximizedChanged();
495 if (m_pendingState.minimizedChanged)
496 emit minimizedChanged();
497 if (m_pendingState.fullscreenChanged)
498 emit fullscreenChanged();
499 // NOTE: parentChanged is emitted before activatedChanged
500 // to guarantee correct activation states of child windows later
501 if (m_pendingState.parentChanged)
502 emit parentChanged();
503 if (m_pendingState.activatedChanged)
504 emit activatedChanged();
505
506 emit stateChanged();
507 }
508
509 /** 4. Clear m+m_pendingState.<variable>Changed flags */
510 m_pendingState.titleChanged = false;
511 m_pendingState.appIdChanged = false;
512 m_pendingState.outputsChanged = false;
513 m_pendingState.maximizedChanged = false;
514 m_pendingState.minimizedChanged = false;
515 m_pendingState.activatedChanged = false;
516 m_pendingState.fullscreenChanged = false;
517 m_pendingState.parentChanged = false;
518 }
519
520
521 void LXQtTaskbarWlrootsWindow::zwlr_foreign_toplevel_handle_v1_closed()
522 {
523 /** This window was closed */
524 emit closed();
525 }
526
527
528 void LXQtTaskbarWlrootsWindow::zwlr_foreign_toplevel_handle_v1_parent(struct ::zwlr_foreign_toplevel_handle_v1 *parent)
529 {
530 /** Parent of this window changed; store it in pending. */
531 m_pendingState.parent = parent;
532 m_pendingState.parentChanged = true;
533 }
534
535
536 void LXQtTaskbarWlrootsWindow::setParentWindow(LXQtTaskbarWlrootsWindow *parent)
537 {
538 QObject::disconnect(parentWindowUnmappedConnection);
539
540 if (parent)
541 {
542 parentWindow = parent->getWindowId();
543 parentWindowUnmappedConnection = QObject::connect(
544 parent, &LXQtTaskbarWlrootsWindow::closed, this, [ this ] {
545 setParentWindow(nullptr);
546 });
547 }
548 else
549 {
550 parentWindow = 0;
551 parentWindowUnmappedConnection = QMetaObject::Connection();
552 }
553 }