| 1 | # Rules of thumb when writing a Wayland plugin |
| 2 | |
| 3 | ## Client extension object lifetime management |
| 4 | |
| 5 | When constructing a client extension with `QWaylandClientExtensionTemplate`, |
| 6 | use automated deletion routines provided by the generated code: |
| 7 | |
| 8 | Pass the autogenerated `destroy()` method into the second argument of the |
| 9 | template: |
| 10 | |
| 11 | ```cpp |
| 12 | class KisWaylandAPIColorManager |
| 13 | : public QWaylandClientExtensionTemplate<KisWaylandAPIColorManager, |
| 14 | // \|/ pass the destructor here! |
| 15 | &QtWayland::wp_color_manager_v1::destroy> |
| 16 | , public QtWayland::wp_color_manager_v1 |
| 17 | { |
| 18 | }; |
| 19 | ``` |
| 20 | |
| 21 | This will generate a code that performs two things for you: |
| 22 | |
| 23 | 1) Destruction of the Wayland's C-style object in the destructor of |
| 24 | the C++-object |
| 25 | |
| 26 | 2) Destruction of the Wayland's C-style object when the extension |
| 27 | gets inactive, i.e. when the connection is lost. |
| 28 | |
| 29 | ## Active and non-active states |
| 30 | |
| 31 | Wayland protocol is a "connection" to the compositor, which means that this |
| 32 | connection may be lost sometimes (e.g. when the compositor crashes). Theoretically, |
| 33 | we should expose this state on the API level, but we don't do that. Instead we |
| 34 | expose ready and non-ready states (see below). |
| 35 | |
| 36 | ## Ready and non-ready states |
| 37 | |
| 38 | The Wayland protocol is asynchronous. It means that when we request some |
| 39 | information from the compositor we do not get the reply immediately. Instead |
| 40 | we should wait untit the compositor sends us an event with the reply. |
| 41 | |
| 42 | For this reason we added two states to the plugin API. The interface may be |
| 43 | "ready" (i.e. all information is available right away) or "non-ready". The |
| 44 | "non-ready" state also covers the case when the connection is lost and |
| 45 | the API is in non-active state. |
| 46 | |
| 47 | ## Handling of the disconnection signal |
| 48 | |
| 49 | When the connection to the compositor is lost (i.e. it crashed and got restarted), |
| 50 | we should handle that and recreate all the objects related to our extension. |
| 51 | |
| 52 | Basically, you need to handle `QWaylandClientExtensionTemplate::activeChanged` |
| 53 | signal, destroy all the object and turn the object into "non-ready" mode. |
| 54 | |
| 55 | See an example in `KisWaylandAPIColorManager`: |
| 56 | |
| 57 | ```cpp |
| 58 | KisWaylandAPIColorManager::KisWaylandAPIColorManager() |
| 59 | : QWaylandClientExtensionTemplate(1) |
| 60 | { |
| 61 | connect(this, &QWaylandClientExtensionTemplate::activeChanged, this, [this] { |
| 62 | if (!isActive()) { |
| 63 | m_supportedFeatures.clear(); |
| 64 | m_supportedIntents.clear(); |
| 65 | m_supportedPrimariesNamed.clear(); |
| 66 | m_supportedTransferFunctionsNamed.clear(); |
| 67 | if (m_isReady) { |
| 68 | m_isReady = false; |
| 69 | Q_EMIT sigReadyChanged(m_isReady); |
| 70 | } |
| 71 | } |
| 72 | }); |
| 73 | } |
| 74 | ``` |
| 75 | |
| 76 | To test if your app handles the disconnection signal correctly just run: |
| 77 | |
| 78 | ```bash |
| 79 | # ensure Qt tries to reconnect on the server restart |
| 80 | export QT_WAYLAND_RECONNECT=1 |
| 81 | |
| 82 | # restart the compositor |
| 83 | kwin_wayland --replace |
| 84 | ``` |
| 85 | |
| 86 | Your app should not die :) |
| 87 | |
| 88 | **WARNING:** it will kill all your XWayland apps (and wayland-native apps |
| 89 | that handle this case incorrectly). |
| 90 | |
| 91 | ## How to debug Wayland protocol interactions |
| 92 | |
| 93 | You can set `WAYLAND_DEBUG` environment variable: |
| 94 | |
| 95 | ```bash |
| 96 | WAYLAND_DEBUG=1 krita | grep -E 'image|color' |
| 97 | ``` |
| 98 | |
| 99 | ## How to test client-side-decorations rendering code |
| 100 | |
| 101 | Gnome/Mutter refuse to implement server-side window decorations |
| 102 | (https://gitlab.gnome.org/GNOME/mutter/-/issues/217), so on Gnome |
| 103 | Krita uses the decorations provided by Qt. To test this codepath |
| 104 | on a normal compositor with SSD, one needs to disable the |
| 105 | corresponding protocol support in Qt and choose the CSD plugin |
| 106 | name: |
| 107 | |
| 108 | ```bash |
| 109 | QT_QPA_PLATFORM=wayland QT_WAYLAND_DISABLED_INTERFACES=zxdg_decoration_manager_v1 QT_WAYLAND_DECORATION=adwaita krita |
| 110 | |
| 111 | # or |
| 112 | |
| 113 | QT_QPA_PLATFORM=wayland QT_WAYLAND_DISABLED_INTERFACES=zxdg_decoration_manager_v1 QT_WAYLAND_DECORATION=bradient krita |
| 114 | ``` |
| 115 | |
| 116 | ## How to test an extension, unsupported in your compositor |
| 117 | |
| 118 | If you want to test code related to an extension that your compositor doesn't support, |
| 119 | you can bypass Krita's checks for the extension by passing the following environment variable: |
| 120 | |
| 121 | ```bash |
| 122 | KRITA_FORCE_WAYLAND_INTERFACES=*list of extension names* |
| 123 | ``` |
| 124 | |
| 125 | For example to force Krita to do wayland color management on an unsupported compositor, you can pass: |
| 126 | |
| 127 | ```bash |
| 128 | KRITA_FORCE_WAYLAND_INTERFACES=wp_color_manager_v1 |
| 129 | ``` |
| 130 | |
| 131 | Note: The extension will **NOT** function properly if you do this. |