qi3pc.h
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* \author Hantz Vius | ||
| 2 | * | ||
| 3 | * \copyright Copyright (C) 2019-2025 Hantz Vius | ||
| 4 | * | ||
| 5 | * \license{ | ||
| 6 | * This file is part of qi3pc. | ||
| 7 | * | ||
| 8 | * qi3pc is free software: you can redistribute it and/or modify | ||
| 9 | * it under the terms of the GNU Affero General Public License as published by | ||
| 10 | * the Free Software Foundation, either version 3 of the License, or | ||
| 11 | * (at your option) any later version. | ||
| 12 | * | ||
| 13 | * This program is distributed in the hope that it will be useful, | ||
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 16 | * GNU Affero General Public License for more details. | ||
| 17 | * | ||
| 18 | * You should have received a copy of the GNU Affero General Public License | ||
| 19 | * along with this program. If not, see <https://www.gnu.org/licenses/>.} | ||
| 20 | */ | ||
| 21 | |||
| 22 | |||
| 23 | |||
| 24 | #ifndef QI3PC_H | ||
| 25 | #define QI3PC_H | ||
| 26 | |||
| 27 | #include "qi3pc_global.h" | ||
| 28 | #include <QtNetwork/qlocalsocket.h> | ||
| 29 | #include <QDebug> | ||
| 30 | #include <QJsonArray> | ||
| 31 | #include <QJsonDocument> | ||
| 32 | #include <QJsonObject> | ||
| 33 | #include <QMutexLocker> | ||
| 34 | #include <QProcessEnvironment> | ||
| 35 | #include <QTime> | ||
| 36 | |||
| 37 | #include <i3/ipc.h> | ||
| 38 | |||
| 39 | #include <optional> | ||
| 40 | #include <string> | ||
| 41 | #include <QLoggingCategory> | ||
| 42 | |||
| 43 | Q_DECLARE_LOGGING_CATEGORY(Qi3pcLogger); | ||
| 44 | /*! | ||
| 45 | * \class qi3pc | ||
| 46 | * \brief Qt bindings for i3wm's IPC interface. | ||
| 47 | * | ||
| 48 | * | ||
| 49 | * qi3pc is a modern C++20 library providing idiomatics Qt bindings to i3wm's | ||
| 50 | * IPC interface. | ||
| 51 | */ | ||
| 52 | class QI3PCSHARED_EXPORT qi3pc : public QObject | ||
| 53 | { | ||
| 54 | Q_OBJECT | ||
| 55 | |||
| 56 | public: | ||
| 57 | static constexpr auto IpcMagicString = std::string("i3-ipc"); /*!< Magic string used by the current API */ | ||
| 58 | static constexpr auto IpcMagicLength = IpcMagicString.length(); /*!< Length of the current magic string */ | ||
| 59 | |||
| 60 | /*! | ||
| 61 | * \enum IpcEvent | ||
| 62 | * \brief Types of events offered by i3wm's IPC API. | ||
| 63 | * | ||
| 64 | * See <a href="https://i3wm.org/docs/ipc.html">i3 docs</a>. | ||
| 65 | */ | ||
| 66 | enum class IpcEvent : quint32 | ||
| 67 | { | ||
| 68 | /*!< The workspace list changed */ | ||
| 69 | Workspace = I3_IPC_EVENT_WORKSPACE, | ||
| 70 | /*!< The output list changed */ | ||
| 71 | Output = I3_IPC_EVENT_OUTPUT, | ||
| 72 | /*!< The mode changed */ | ||
| 73 | Mode = I3_IPC_EVENT_MODE, | ||
| 74 | /*!< The windows changed */ | ||
| 75 | Window = I3_IPC_EVENT_WINDOW, | ||
| 76 | /*!< The bar config changed */ | ||
| 77 | BarUpdate = I3_IPC_EVENT_BARCONFIG_UPDATE, | ||
| 78 | /*!< A binding was ran */ | ||
| 79 | Binding = I3_IPC_EVENT_BINDING, | ||
| 80 | /*!< The IPC shutdown */ | ||
| 81 | Shutdown = I3_IPC_EVENT_SHUTDOWN, | ||
| 82 | /*!< A tick message was requested */ | ||
| 83 | Tick = I3_IPC_EVENT_TICK | ||
| 84 | }; | ||
| 85 | |||
| 86 | /*! | ||
| 87 | * \enum IpcType | ||
| 88 | * \brief Types of message/replies the API send/expect to/from i3wm. | ||
| 89 | * | ||
| 90 | * See <a href="https://i3wm.org/docs/ipc.html">i3 docs</a>. | ||
| 91 | */ | ||
| 92 | enum class IpcType : quint32 | ||
| 93 | { | ||
| 94 | Command = I3_IPC_REPLY_TYPE_COMMAND, /*!< (Run) Command */ | ||
| 95 | Workspaces = I3_IPC_REPLY_TYPE_WORKSPACES, /*!< Current workspaces */ | ||
| 96 | Subscribe = I3_IPC_REPLY_TYPE_SUBSCRIBE, /*!< Subscription */ | ||
| 97 | Outputs = I3_IPC_REPLY_TYPE_OUTPUTS, /*!< Current outputs */ | ||
| 98 | Tree = I3_IPC_REPLY_TYPE_TREE, /*!< Tree layout */ | ||
| 99 | Marks = I3_IPC_REPLY_TYPE_MARKS, /*!< Current defined marks */ | ||
| 100 | BarConfig = I3_IPC_REPLY_TYPE_BAR_CONFIG, /*!< Specific bar configuration */ | ||
| 101 | Version = I3_IPC_REPLY_TYPE_VERSION, /*!< i3 version */ | ||
| 102 | BindingModes = I3_IPC_REPLY_TYPE_BINDING_MODES, /*!< Configured binding modes */ | ||
| 103 | Config = I3_IPC_REPLY_TYPE_CONFIG, /*!< Last loaded i3 config (raw) */ | ||
| 104 | Tick = I3_IPC_REPLY_TYPE_TICK, /*!< Trigger a tick event */ | ||
| 105 | Sync = I3_IPC_REPLY_TYPE_SYNC, /*!< Trigger a sync protocol message */ | ||
| 106 | BindingState = I3_IPC_REPLY_TYPE_GET_BINDING_STATE /*!< Current binding state */ | ||
| 107 | }; | ||
| 108 | |||
| 109 | /*! | ||
| 110 | * \enum WorkspaceChange | ||
| 111 | * \brief Types of change a workspace event | ||
| 112 | * can have. | ||
| 113 | * | ||
| 114 | * Mapping of the change property received in a workspace event. | ||
| 115 | */ | ||
| 116 | enum class WorkspaceChange | ||
| 117 | { | ||
| 118 | Empty, /*!< A workspace became empty. */ | ||
| 119 | Focus, /*!< A workspace received input focus. */ | ||
| 120 | Init, /*!< A workspace was created. */ | ||
| 121 | Move, /*!< A workspace was moved to a different output. */ | ||
| 122 | Reload, /*!< The window manager's config was reloaded. */ | ||
| 123 | Rename, /*!< A workspace was renamed. */ | ||
| 124 | Restored, /*!< A workspace's layout was restored to a saved layout. */ | ||
| 125 | Urgent, /*!< A workspace gained/lost urgent status. */ | ||
| 126 | Unknown /*!< Unsupported change. */ | ||
| 127 | }; | ||
| 128 | |||
| 129 | /*! | ||
| 130 | * \enum OutputChange | ||
| 131 | * \brief Types of change an output event can have. | ||
| 132 | * | ||
| 133 | * Mapping of the change property received in a output event. | ||
| 134 | */ | ||
| 135 | enum class OutputChange | ||
| 136 | { | ||
| 137 | Unspecified, /*!< See <a href="https://i3wm.org/docs/ipc.html">i3 docs</a>. */ | ||
| 138 | Unknown /*!< Unsupported change. */ | ||
| 139 | }; | ||
| 140 | |||
| 141 | /*! | ||
| 142 | * \enum WindowChange | ||
| 143 | * \brief Types of change a window event can have. | ||
| 144 | * | ||
| 145 | * Mapping of the change property received in a window event. | ||
| 146 | */ | ||
| 147 | enum class WindowChange | ||
| 148 | { | ||
| 149 | New, /*!< A new window is created. */ | ||
| 150 | Close, /*!< A window is closed. */ | ||
| 151 | Focus, /*!< A window gained focus. */ | ||
| 152 | Title, /*!< A window's title changed. */ | ||
| 153 | Fullscreen, /*!< A window entered/left fullscreen mode. */ | ||
| 154 | Move, /*!< A window have been moved. */ | ||
| 155 | Floating, /*!< A window entered/left floating mode. */ | ||
| 156 | Urgent, /*!< A window gained/lost urgent status. */ | ||
| 157 | Mark, /*!< A mark was added/remvoed to/from a window. */ | ||
| 158 | Unknown /*!< Unsupported change. */ | ||
| 159 | }; | ||
| 160 | |||
| 161 | /*! | ||
| 162 | * \enum ShutdownChange | ||
| 163 | * \brief Types of change a shutdown event can have. | ||
| 164 | * | ||
| 165 | * Mapping of the change property received in a shutdown event. | ||
| 166 | */ | ||
| 167 | enum class ShutdownChange | ||
| 168 | { | ||
| 169 | Restart, /*!< The window mananger is restarted. */ | ||
| 170 | Exit, /*!< The window manager is exiting. */ | ||
| 171 | Unknown /*!< Unsupported change. */ | ||
| 172 | }; | ||
| 173 | |||
| 174 | /*! | ||
| 175 | * \enum BindingChange | ||
| 176 | * \brief Types of change a binding event can have. | ||
| 177 | * | ||
| 178 | * Mapping of the change property received in a binding event. | ||
| 179 | */ | ||
| 180 | enum class BindingChange | ||
| 181 | { | ||
| 182 | Run, /*!< A binding was run. */ | ||
| 183 | Unknown /*!< Unsupported change. */ | ||
| 184 | }; | ||
| 185 | |||
| 186 | //! \brief Optional pair of a JSON object with its last update time. | ||
| 187 | using DataObject = std::optional<std::pair<QJsonObject, qint64>>; | ||
| 188 | |||
| 189 | //! \brief Optional pair of a JSON array with its last update time. | ||
| 190 | using DataArray = std::optional<std::pair<QJsonArray, qint64>>; | ||
| 191 | |||
| 192 | //! \brief Optional pair of a string with its last update time. | ||
| 193 | using DataString = std::optional<std::pair<QString, qint64>>; | ||
| 194 | |||
| 195 | /*! | ||
| 196 | * \brief Container for the attributes of a parsing error | ||
| 197 | * from i3wm when trying to run an unparsable command. | ||
| 198 | * | ||
| 199 | * This is the processed reply for a \link qi3pc::IpcType qi3pc::IpcType::Command \endlink reply. | ||
| 200 | */ | ||
| 201 | struct ParseError { | ||
| 202 | //! \brief Human readble error message. | ||
| 203 | QString error; | ||
| 204 | |||
| 205 | //! \brief The command that failed to run. | ||
| 206 | QString input; | ||
| 207 | |||
| 208 | /*! | ||
| 209 | * \brief The position where the error was detected in the input. | ||
| 210 | * | ||
| 211 | * This is a string of the same length as the input. | ||
| 212 | * Spaces ' ' represent the parsable part of the input. | ||
| 213 | * Starting from where the error was detected, | ||
| 214 | * the rest is represented with carets '^'. | ||
| 215 | */ | ||
| 216 | QString errorPosition; | ||
| 217 | |||
| 218 | /*! | ||
| 219 | * \brief Convert to a json like string | ||
| 220 | * \return QString representation of the ParseError | ||
| 221 | * | ||
| 222 | * The resulting string is in the form "{member:'member'}{member2:'member2'}". | ||
| 223 | * \note This is NOT valid JSON. Notice the lack of commas between elements. | ||
| 224 | */ | ||
| 225 | QString toString() const; | ||
| 226 | |||
| 227 | /*! | ||
| 228 | * \brief Memberwise comparison of this ParseError and another. | ||
| 229 | * \param other The second object to compare. | ||
| 230 | * \return whether all members are equal | ||
| 231 | */ | ||
| 232 | bool operator==(const ParseError& other) const; | ||
| 233 | |||
| 234 | /*! | ||
| 235 | * \brief Build an optional ParseError from a QJsonObject. | ||
| 236 | * \param json JSON object with structure matching \ref ParseError::_MEMBERS. | ||
| 237 | * \return A ParseError or a nullopt. | ||
| 238 | * | ||
| 239 | * At least "parse_error" should be in the JSON. | ||
| 240 | * If it's not present, a nullopt is returned. | ||
| 241 | * If only that key and none of the other checked ones | ||
| 242 | * exist, a default initialized ParseError is returned. | ||
| 243 | * Otherwise for each of the checked keys found, the corresponding member is initialized. | ||
| 244 | * Values in the JSON are expected to be of string type. | ||
| 245 | */ | ||
| 246 | static std::optional<ParseError> FromJSON(const QJsonObject& json); | ||
| 247 | |||
| 248 | private: | ||
| 249 | //! \brief QString member pointer of ParseError. | ||
| 250 | using QStringMemberPtr = QString ParseError::*; | ||
| 251 | |||
| 252 | //! \brief Mapping of i3wm's parse error reply's attributes to members of ParseError | ||
| 253 | inline static constexpr auto _MEMBERS = std::to_array<std::pair<const char*, QStringMemberPtr>> | ||
| 254 | ({ | ||
| 255 | {"error", &ParseError::error}, | ||
| 256 | {"input", &ParseError::input}, | ||
| 257 | {"errorposition", &ParseError::errorPosition} | ||
| 258 | }); | ||
| 259 | }; | ||
| 260 | |||
| 261 | //! \brief Optional \ref qi3pc::ParseError. The optional is empty when the error could not be parsed. | ||
| 262 | using Error = std::optional<ParseError>; | ||
| 263 | |||
| 264 | /*! | ||
| 265 | * \brief Pairs of \ref qi3pc::Error and boolean. | ||
| 266 | * | ||
| 267 | * For each element, if the boolean is false, the command was ran successfully. | ||
| 268 | * Otherwise information about the parse error encountered is in the second element. | ||
| 269 | */ | ||
| 270 | using CommandResults = std::vector<std::pair<bool, Error>>; | ||
| 271 | |||
| 272 | /*! | ||
| 273 | * \brief Optional pair of a JSON document with a \ref qi3pc::IpcType received | ||
| 274 | * with a message or an event before it has been processed. | ||
| 275 | * \see qi3pc::IpcType | ||
| 276 | * \see qi3pc::IpcEvent | ||
| 277 | */ | ||
| 278 | using Message = std::optional<std::pair<QJsonDocument, quint32>>; | ||
| 279 | |||
| 280 | /*! | ||
| 281 | * \brief Construct a qi3pc object. | ||
| 282 | * \param parent Parent of the qi3pc object to create (matching Qt conventions). | ||
| 283 | */ | ||
| 284 | explicit qi3pc(QObject* parent = nullptr); | ||
| 285 | |||
| 286 | /*! | ||
| 287 | * \brief Construct a qi3pc object connected to the provided socket. | ||
| 288 | * \param socketPath The path to the server to use (e.g. /tmp/some-service.socket) | ||
| 289 | * \param parent Parent of the qi3pc object to create (matching Qt conventions). | ||
| 290 | */ | ||
| 291 | explicit qi3pc(const QString& socketPath, QObject* parent = nullptr); | ||
| 292 | |||
| 293 | /*! | ||
| 294 | * \brief Simple destructor for the qi3pc class. | ||
| 295 | * | ||
| 296 | * Disconnects the used sockets. | ||
| 297 | * \see qi3pc::disconnect | ||
| 298 | */ | ||
| 299 | virtual ~qi3pc(); | ||
| 300 | |||
| 301 | /*! | ||
| 302 | * \brief Start listening to messages and events from the window manager. | ||
| 303 | * \param msecs A duration in milliseconds. This method may wait up to twice | ||
| 304 | * that duraction. | ||
| 305 | * \return Whether the connection succeeded within the alloted time | ||
| 306 | * | ||
| 307 | * This method may wait up to msecs milliseconds for the connection to be | ||
| 308 | * established. If it takes more than msecs milliseconds to connect, the | ||
| 309 | * connection might be established later, but the returned value | ||
| 310 | * will not reflect that. | ||
| 311 | * | ||
| 312 | * \see qi3pc::connected | ||
| 313 | */ | ||
| 314 | bool connect(int msecs = 1500); | ||
| 315 | |||
| 316 | /*! | ||
| 317 | * \brief Check if the connection to the ipc socket is established. | ||
| 318 | * \return Return if the connection to the two sockets (message/events) were established. | ||
| 319 | * | ||
| 320 | * \see qi3pc::connect | ||
| 321 | */ | ||
| 322 | bool isConnected(); | ||
| 323 | |||
| 324 | /*! | ||
| 325 | * \brief Stop listening to messages and events from the window manager. | ||
| 326 | * \param msecs A duration in milliseconds. This method may wait up to twice | ||
| 327 | * that duraction. | ||
| 328 | * \return Whether the connection succeeded within the alloted time | ||
| 329 | * | ||
| 330 | * \see qi3pc::isConnected | ||
| 331 | */ | ||
| 332 | bool disconnect(int msecs = 1500); | ||
| 333 | |||
| 334 | /*! | ||
| 335 | * \brief Subscribe to a list of events. | ||
| 336 | * \param events A list of strings with the | ||
| 337 | * <a href="https://i3wm.org/docs/ipc.html#_available_events">events</a> | ||
| 338 | * to subscribe to. | ||
| 339 | */ | ||
| 340 | void subscribe(const QStringList& events); | ||
| 341 | |||
| 342 | /*! | ||
| 343 | * \brief Find the path to the i3 ipc local unix socket. | ||
| 344 | * | ||
| 345 | * First tries to read the @c I3SOCK environment variable. | ||
| 346 | * If I3SOCK is empty or not set, calls @code i3 --get-socketpath @endcode | ||
| 347 | * \return The path to the unix socket. | ||
| 348 | */ | ||
| 349 | static QString FindSocketPath(); | ||
| 350 | |||
| 351 | /*! | ||
| 352 | * \brief Find the path to the i3 ipc local unix socket using the i3 binary. | ||
| 353 | * | ||
| 354 | * Calls and returns the output of @code i3 --get-socketpath @endcode | ||
| 355 | * \return The path to the unix socket. | ||
| 356 | */ | ||
| 357 | static QString FindSocketPathFromI3Binary(); | ||
| 358 | |||
| 359 | /*! | ||
| 360 | * \brief Get the socket path selected at construction. | ||
| 361 | * \return The i3 socket path from when the object was created. | ||
| 362 | */ | ||
| 363 | QString socketPath() const; | ||
| 364 | |||
| 365 | /*! | ||
| 366 | * \brief Send a message with the specified type and payload to i3. | ||
| 367 | * \tparam Type The type of the message to send. | ||
| 368 | * \param payload The content of the message. | ||
| 369 | * | ||
| 370 | * Available <a | ||
| 371 | * href="https://i3wm.org/docs/ipc.html#_sending_messages_to_i3">message | ||
| 372 | * types</a>. | ||
| 373 | */ | ||
| 374 | template<IpcType Type> | ||
| 375 | 75 | void sendMessage(const QByteArray& payload = QByteArray()) { | |
| 376 | if constexpr(Type == IpcType::Subscribe || Type == IpcType::Tick) { | ||
| 377 | 10 | WritePayload(m_eventSocket, payload, Type); | |
| 378 | if constexpr(Type == IpcType::Subscribe) { | ||
| 379 | m_eventSocket.flush(); | ||
| 380 | } | ||
| 381 | } else { | ||
| 382 | 65 | WritePayload(m_messageSocket, payload, Type); | |
| 383 | } | ||
| 384 | 75 | } | |
| 385 | |||
| 386 | /*! | ||
| 387 | * \brief Get the list of (cached) workspaces. | ||
| 388 | * \return The list of workspaces with its last update time. | ||
| 389 | * | ||
| 390 | * \see qi3pc::fetchWorkspaces | ||
| 391 | */ | ||
| 392 | const DataArray& workspaces() const; | ||
| 393 | |||
| 394 | /*! | ||
| 395 | * \brief Get the (cached) i3 layout tree. | ||
| 396 | * \return The layout tree with its last update time | ||
| 397 | * | ||
| 398 | * \see qi3pc::fetchTree | ||
| 399 | */ | ||
| 400 | const DataObject& tree() const; | ||
| 401 | |||
| 402 | /*! | ||
| 403 | * \brief Get the (cached) list of outputs. | ||
| 404 | * \return The list of outputs with its last update time. | ||
| 405 | * | ||
| 406 | * \see qi3pc::fetchOutputs | ||
| 407 | */ | ||
| 408 | const DataArray& outputs() const; | ||
| 409 | |||
| 410 | /*! | ||
| 411 | * \brief Get the (cached) list of set marks. | ||
| 412 | * \return The list of marks and its last update time. | ||
| 413 | * | ||
| 414 | * \see qi3pc::fetchMarks | ||
| 415 | */ | ||
| 416 | const DataArray& marks() const; | ||
| 417 | |||
| 418 | /*! | ||
| 419 | * \brief Get the (cached) list of all bar configurations. | ||
| 420 | * \return A map of bar configurations and its last update time. | ||
| 421 | * | ||
| 422 | * \see qi3pc::fetchBarConfig | ||
| 423 | * \see qi3pc::barConfigUpdated | ||
| 424 | * \see qi3pc::newBarConfig | ||
| 425 | */ | ||
| 426 | const DataObject& barConfigs() const; | ||
| 427 | |||
| 428 | /*! | ||
| 429 | * \brief Get the (cached) i3 version object. | ||
| 430 | * \return The i3 version and the last time it was updated. | ||
| 431 | * | ||
| 432 | * \see qi3pc::fetchVersion | ||
| 433 | * \see qi3pc::versionUpdated | ||
| 434 | */ | ||
| 435 | const DataObject& version() const; | ||
| 436 | |||
| 437 | /*! | ||
| 438 | * \brief Get the (cached) list of binding modes. | ||
| 439 | * \return The list of binding modes and its last update time. | ||
| 440 | * \see qi3pc::fetchBindingModes | ||
| 441 | * \see qi3pc::bindingModesUpdated | ||
| 442 | */ | ||
| 443 | const DataArray& bindingModes() const; | ||
| 444 | |||
| 445 | /*! | ||
| 446 | * \brief Get the (cached) data read from the config file. | ||
| 447 | * \return The config and the last time it was updated. | ||
| 448 | * \see qi3pc::fetchConfig | ||
| 449 | * \see qi3pc::configUpdated | ||
| 450 | */ | ||
| 451 | const DataObject& config() const; | ||
| 452 | |||
| 453 | /*! | ||
| 454 | * \brief Get the (cached) binding state. | ||
| 455 | * \return The binding state and the last time it was updated. | ||
| 456 | * \see qi3pc::fetchBindingState | ||
| 457 | * \see qi3pc::bindingStateUpdated | ||
| 458 | */ | ||
| 459 | const DataString& bindingState() const; | ||
| 460 | |||
| 461 | public slots: | ||
| 462 | /*! | ||
| 463 | * \brief Signal to emit to trigger an update of the list of | ||
| 464 | * workspace cache. | ||
| 465 | * \see qi3pc::workspaces | ||
| 466 | * \see qi3pc::workspacesUpdated | ||
| 467 | */ | ||
| 468 | void fetchWorkspaces(); | ||
| 469 | |||
| 470 | /*! | ||
| 471 | * \brief Signal to emit to trigger an update of the (cached) | ||
| 472 | * layout tree. | ||
| 473 | * \see qi3pc::tree | ||
| 474 | * \see qi3pc::treeUpdated | ||
| 475 | */ | ||
| 476 | void fetchTree(); | ||
| 477 | |||
| 478 | /*! | ||
| 479 | * \brief Signal to emit to trigger an update of | ||
| 480 | * the (cached) outputs. | ||
| 481 | * | ||
| 482 | * \see qi3pc::outputs | ||
| 483 | * \see qi3pc::outputsUpdated | ||
| 484 | */ | ||
| 485 | void fetchOutputs(); | ||
| 486 | |||
| 487 | /*! | ||
| 488 | * \brief Signal to emit to trigger an update of the (cached) | ||
| 489 | * list of marks. | ||
| 490 | * \see qi3pc::marks | ||
| 491 | * \see qi3pc::marksUpdated | ||
| 492 | */ | ||
| 493 | void fetchMarks(); | ||
| 494 | |||
| 495 | /*! | ||
| 496 | * \brief Signal to emit to update the (cached) configuration of a | ||
| 497 | * certain bar. | ||
| 498 | * \param id String identifying to bar to update. | ||
| 499 | * | ||
| 500 | * \see qi3pc::barConfigUpdated | ||
| 501 | * \see qi3pc::newBarConfig | ||
| 502 | * \see qi3pc::barConfigs | ||
| 503 | */ | ||
| 504 | void fetchBarConfig(const QString& id); | ||
| 505 | |||
| 506 | /*! | ||
| 507 | * \brief Signal to emit to update the list of bar configurations. | ||
| 508 | * \see qi3pc::barConfigUpdated | ||
| 509 | * \see qi3pc::newBarConfig | ||
| 510 | * \see qi3pc::barConfigs | ||
| 511 | */ | ||
| 512 | void fetchBarConfigs(); | ||
| 513 | |||
| 514 | /*! | ||
| 515 | * \brief Signal to emit to trigger a cache update for the | ||
| 516 | * i3wm version. | ||
| 517 | * | ||
| 518 | * \see qi3pc::version | ||
| 519 | * \see qi3pc::versionUpdated | ||
| 520 | */ | ||
| 521 | void fetchVersion(); | ||
| 522 | |||
| 523 | /*! | ||
| 524 | * \brief Signal to emit to trigger an update of the (cached) | ||
| 525 | * list of modes. | ||
| 526 | * | ||
| 527 | * \see qi3pc::bindingModes | ||
| 528 | * \see qi3pc::bindingModesUpdated | ||
| 529 | */ | ||
| 530 | void fetchBindingModes(); | ||
| 531 | |||
| 532 | /*! | ||
| 533 | * \brief Signal to emit to trigger an update of the (cached) config. | ||
| 534 | * | ||
| 535 | * \see qi3pc::config | ||
| 536 | * \see qi3pc::configUpdated | ||
| 537 | */ | ||
| 538 | void fetchConfig(); | ||
| 539 | |||
| 540 | /*! | ||
| 541 | * \brief Request update of the (cached) binding state. | ||
| 542 | * | ||
| 543 | * \see qi3pc::bindingState | ||
| 544 | * \see qi3pc::bindingStateUpdated | ||
| 545 | */ | ||
| 546 | void fetchBindingState(); | ||
| 547 | |||
| 548 | private: | ||
| 549 | /*! | ||
| 550 | * \brief Read one message using the socket parameter. | ||
| 551 | * \param socket Local unix socket from which the message is read | ||
| 552 | * \return An optional json doc and the type of the message read | ||
| 553 | */ | ||
| 554 | Message processMessage(QLocalSocket& socket); | ||
| 555 | |||
| 556 | /*! | ||
| 557 | * \brief Read data from the event socket. | ||
| 558 | */ | ||
| 559 | void processEvent(); | ||
| 560 | |||
| 561 | /*! | ||
| 562 | * \brief Read data from the message socket. | ||
| 563 | */ | ||
| 564 | void processReply(); | ||
| 565 | |||
| 566 | /*! | ||
| 567 | * \brief Handle data received from a workspace event. | ||
| 568 | * \param doc Document containing the data | ||
| 569 | */ | ||
| 570 | void processWorkspaceEvent(const QJsonDocument& doc); | ||
| 571 | |||
| 572 | /*! | ||
| 573 | * \brief Handle data received from an output event. | ||
| 574 | * \param doc Document containing the data | ||
| 575 | */ | ||
| 576 | void processOutputEvent(const QJsonDocument& doc); | ||
| 577 | |||
| 578 | /*! | ||
| 579 | * \brief Handle data received from a mode event. | ||
| 580 | * \param doc Document containing the data | ||
| 581 | */ | ||
| 582 | void processModeEvent(const QJsonDocument& doc); | ||
| 583 | |||
| 584 | /*! | ||
| 585 | * \brief Handle data received from a window event. | ||
| 586 | * \param doc Document containing the data | ||
| 587 | */ | ||
| 588 | void processWindowEvent(const QJsonDocument& doc); | ||
| 589 | |||
| 590 | /*! | ||
| 591 | * \brief Handle data received from a bar update event. | ||
| 592 | * \param doc Document containing the data | ||
| 593 | */ | ||
| 594 | void processBarUpdateEvent(const QJsonDocument& doc); | ||
| 595 | |||
| 596 | /*! | ||
| 597 | * \brief Handle data received from a binding event. | ||
| 598 | * \param doc Document containing the data | ||
| 599 | */ | ||
| 600 | void processBindingEvent(const QJsonDocument& doc); | ||
| 601 | |||
| 602 | /*! | ||
| 603 | * \brief Handle data received from a shutdowm event. | ||
| 604 | * \param doc Document containing the data | ||
| 605 | */ | ||
| 606 | void processShutdownEvent(const QJsonDocument& doc); | ||
| 607 | |||
| 608 | /*! | ||
| 609 | * \brief Handle data received from a tick event. | ||
| 610 | * \param doc Document containing the data | ||
| 611 | */ | ||
| 612 | void processTickEvent(const QJsonDocument& doc); | ||
| 613 | |||
| 614 | /*! | ||
| 615 | * \brief Handle data received from a run command reply. | ||
| 616 | * \param doc Document containing the data | ||
| 617 | */ | ||
| 618 | void processCommandReply(const QJsonDocument& doc); | ||
| 619 | |||
| 620 | /*! | ||
| 621 | * \brief Handle data received from a workspace reply. | ||
| 622 | * \param doc Document containing the data | ||
| 623 | */ | ||
| 624 | void processWorkspaceReply(const QJsonDocument& doc); | ||
| 625 | |||
| 626 | /*! | ||
| 627 | * \brief Handle data received from an output reply. | ||
| 628 | * \param doc Document containing the data | ||
| 629 | */ | ||
| 630 | void processOutputReply(const QJsonDocument& doc); | ||
| 631 | |||
| 632 | /*! | ||
| 633 | * \brief Handle data received from a tree reply. | ||
| 634 | * \param doc Document containing the data | ||
| 635 | */ | ||
| 636 | void processTreeReply(const QJsonDocument& doc); | ||
| 637 | |||
| 638 | /*! | ||
| 639 | * \brief Handle data received from a mark reply. | ||
| 640 | * \param doc Document containing the data | ||
| 641 | */ | ||
| 642 | void processMarkReply(const QJsonDocument& doc); | ||
| 643 | |||
| 644 | /*! | ||
| 645 | * \brief Handle data received from a bar config reply. | ||
| 646 | * \param doc Document containing the data | ||
| 647 | */ | ||
| 648 | void processBarConfigReply(const QJsonDocument& doc); | ||
| 649 | |||
| 650 | /*! | ||
| 651 | * \brief Handle data received from a version reply. | ||
| 652 | * \param doc Document containing the data | ||
| 653 | */ | ||
| 654 | void processVersionReply(const QJsonDocument& doc); | ||
| 655 | |||
| 656 | /*! | ||
| 657 | * \brief Handle data received in a binding mode reply. | ||
| 658 | * \param doc Document containing the data | ||
| 659 | */ | ||
| 660 | void processBindingModesReply(const QJsonDocument& doc); | ||
| 661 | |||
| 662 | /*! | ||
| 663 | * \brief Handle data received from a config reply. | ||
| 664 | * \param doc Document containing the data | ||
| 665 | */ | ||
| 666 | void processConfigReply(const QJsonDocument& doc); | ||
| 667 | |||
| 668 | /*! | ||
| 669 | * \brief Handle data received from a tick reply. | ||
| 670 | * \param doc Document containing the data | ||
| 671 | */ | ||
| 672 | void processTickReply(const QJsonDocument& doc); | ||
| 673 | |||
| 674 | /*! | ||
| 675 | * \brief Handle data received from a sync reply. | ||
| 676 | * \param doc Document containing the data | ||
| 677 | */ | ||
| 678 | void processSyncReply(const QJsonDocument& doc); | ||
| 679 | |||
| 680 | /*! | ||
| 681 | * \brief Handle data received from a binding state event reply. | ||
| 682 | * \param doc Document containing the data | ||
| 683 | */ | ||
| 684 | void processBindingStateReply(const QJsonDocument& doc); | ||
| 685 | |||
| 686 | /*! | ||
| 687 | * \brief Convert a string into a workspace change object. | ||
| 688 | * \param s The string to convert | ||
| 689 | * \return A qi3pc::WorkspaceChange value. | ||
| 690 | */ | ||
| 691 | static WorkspaceChange WorkspaceChangeFromString(const QString& s); | ||
| 692 | |||
| 693 | /*! | ||
| 694 | * \brief Convert a string into a window change object. | ||
| 695 | * \param s The string to convert | ||
| 696 | * \return A qi3pc::WindowChange value. | ||
| 697 | */ | ||
| 698 | static WindowChange WindowChangeFromString(const QString& s); | ||
| 699 | |||
| 700 | /*! | ||
| 701 | * \brief Convert a string into a shutdown change object. | ||
| 702 | * \param s The string to convert | ||
| 703 | * \return A qi3pc::ShutdownChange value. | ||
| 704 | */ | ||
| 705 | static ShutdownChange ShutdownChangeFromString(const QString& s); | ||
| 706 | |||
| 707 | /*! | ||
| 708 | * \brief Convert a string into an output change object. | ||
| 709 | * \param s The string to convert | ||
| 710 | * \return A qi3pc::OutputChange value. | ||
| 711 | */ | ||
| 712 | static OutputChange OutputChangeFromString(const QString& s); | ||
| 713 | |||
| 714 | /*! | ||
| 715 | * \brief Convert a string into a binding change object. | ||
| 716 | * \param s The string to convert | ||
| 717 | * \return A qi3pc::BindingChange value. | ||
| 718 | */ | ||
| 719 | static BindingChange BindingChangeFromString(const QString& s); | ||
| 720 | |||
| 721 | /*! | ||
| 722 | * \brief Send a message with the specified type and payload to i3 using the specified socket. | ||
| 723 | * | ||
| 724 | * \param socket The socket where the payload should be written. | ||
| 725 | * \param payload The content to be written on the socket. | ||
| 726 | * \param type The type of the payload. | ||
| 727 | */ | ||
| 728 | static void WritePayload(QLocalSocket& socket, const QByteArray& payload, IpcType type); | ||
| 729 | |||
| 730 | private: | ||
| 731 | QString m_socketPath; /*!< Path of the local unix socket used. */ | ||
| 732 | |||
| 733 | QLocalSocket m_eventSocket; /*!< Socket used to subscribe and handle events. */ | ||
| 734 | QLocalSocket m_messageSocket; /*!< Socket used to send messages and handle replies. */ | ||
| 735 | |||
| 736 | DataObject m_tree; /*!< Layout tree cache. */ | ||
| 737 | DataArray m_workspaces; /*!< Workspace list cache. */ | ||
| 738 | DataArray m_outputs; /*!< Output list cache. */ | ||
| 739 | DataArray m_marks; /*!< Mark list cache. */ | ||
| 740 | DataObject m_barConfigs; /*!< Bar configuration list cache. */ | ||
| 741 | DataObject m_version; /*!< Version cache. */ | ||
| 742 | DataArray m_bindingModes; /*!< Binding modes cache. */ | ||
| 743 | DataString m_bindingState; /*!< Current binding mode cache. */ | ||
| 744 | DataObject m_config; /*!< Configuration cache. */ | ||
| 745 | |||
| 746 | signals: | ||
| 747 | /*! | ||
| 748 | * \brief A command have been ran by i3. | ||
| 749 | * \param result Pairs of boolean and optional parse error. | ||
| 750 | * | ||
| 751 | * Use \ref qi3pc::sendMessage to run command with | ||
| 752 | * the \link qi3pc::IpcType qi3pc::IpcType::Command \endlink type. | ||
| 753 | */ | ||
| 754 | void commandRan(qi3pc::CommandResults result); | ||
| 755 | |||
| 756 | /*! | ||
| 757 | * \brief A tick message have been replied to by i3. | ||
| 758 | * \param success | ||
| 759 | * | ||
| 760 | * Use \ref qi3pc::sendMessage to send tick messages with | ||
| 761 | * the \link qi3pc::IpcType qi3pc::IpcType::Tick \endlink type. | ||
| 762 | */ | ||
| 763 | void tickSent(bool success); | ||
| 764 | |||
| 765 | /*! | ||
| 766 | * \brief A sync message have been replied to by i3. | ||
| 767 | * \param success | ||
| 768 | * | ||
| 769 | * Use \ref qi3pc::sendMessage to send sync messages with | ||
| 770 | * the \link qi3pc::IpcType qi3pc::IpcTypeSync \endlink type. | ||
| 771 | */ | ||
| 772 | void synced(bool success); | ||
| 773 | |||
| 774 | /*! | ||
| 775 | * \brief A subscribe message have been replied to. | ||
| 776 | * \param success Indicates whether the subscription was successfull | ||
| 777 | * | ||
| 778 | * \see qi3pc::subscribe | ||
| 779 | */ | ||
| 780 | void subscribed(bool success); | ||
| 781 | |||
| 782 | /*! | ||
| 783 | * \brief The workspaces changed. | ||
| 784 | * \param change The type of change | ||
| 785 | * \param current The current workspace | ||
| 786 | * \param old The old workspace | ||
| 787 | */ | ||
| 788 | void workspaceEvent(qi3pc::WorkspaceChange change, | ||
| 789 | const QJsonObject& current, | ||
| 790 | const QJsonObject& old); | ||
| 791 | /*! | ||
| 792 | * \brief Signal emitted when the output(s) change. | ||
| 793 | * \param change The type of change | ||
| 794 | */ | ||
| 795 | void outputEvent(qi3pc::OutputChange change); | ||
| 796 | |||
| 797 | /*! | ||
| 798 | * \brief The binding mode changes. | ||
| 799 | * \param change The name of the current mode. | ||
| 800 | * \param pango Boolean telling whether to display the mode | ||
| 801 | * with pango markup. | ||
| 802 | */ | ||
| 803 | void modeEvent(QString change, bool pango); | ||
| 804 | |||
| 805 | /*! | ||
| 806 | * \brief A window changed. | ||
| 807 | * \param change The type of change. | ||
| 808 | * \param container The parent of the changed window. | ||
| 809 | */ | ||
| 810 | void windowEvent(qi3pc::WindowChange change, const QJsonObject& container); | ||
| 811 | |||
| 812 | /*! | ||
| 813 | * \brief A bar's configuration have been updated. | ||
| 814 | * \param doc Json object containing the bar configuration. | ||
| 815 | */ | ||
| 816 | void barUpdateEvent(const QJsonObject& doc); | ||
| 817 | |||
| 818 | /*! | ||
| 819 | * \brief A binding have been triggered to run a command. | ||
| 820 | * \param change The type of change. | ||
| 821 | * \param binding The binding that was run. | ||
| 822 | * \param mode The name of the mode the binding was run in. | ||
| 823 | * | ||
| 824 | * \see qi3pc::bindingState | ||
| 825 | */ | ||
| 826 | void bindingEvent(qi3pc::BindingChange change, const QJsonObject& binding, const QString& mode); | ||
| 827 | |||
| 828 | /*! | ||
| 829 | * \brief The ipc socket is about to shutdown. | ||
| 830 | * \param change The type of change. | ||
| 831 | */ | ||
| 832 | void shutdownEvent(qi3pc::ShutdownChange change); | ||
| 833 | |||
| 834 | /*! | ||
| 835 | * \brief A tick event is received from i3. | ||
| 836 | * \param payload Arbitrary payload sent with the tick message. | ||
| 837 | */ | ||
| 838 | void tickEvent(const QString& payload); | ||
| 839 | |||
| 840 | /*! | ||
| 841 | * \brief The (cached) list of workspaces have been updated. | ||
| 842 | * \param workspaces The last list of workspaces with the update | ||
| 843 | * time. | ||
| 844 | * \see qi3pc::fetchWorkspaces | ||
| 845 | */ | ||
| 846 | void workspacesUpdated(const qi3pc::DataArray& workspaces); | ||
| 847 | |||
| 848 | /*! | ||
| 849 | * \brief The layout tree cache have been updated. | ||
| 850 | * \param tree The most recent layout tree cached and its | ||
| 851 | * update time. | ||
| 852 | * \see qi3pc::fetchTree | ||
| 853 | */ | ||
| 854 | void treeUpdated(const qi3pc::DataObject& tree); | ||
| 855 | |||
| 856 | /*! | ||
| 857 | * \brief The (cached) outputs have been updated. | ||
| 858 | * \param outputs The latest list of outputs and its update time. | ||
| 859 | * \see qi3pc::fetchOutputs | ||
| 860 | */ | ||
| 861 | void outputsUpdated(const qi3pc::DataArray& outputs); | ||
| 862 | |||
| 863 | /*! | ||
| 864 | * \brief The (cached) list of marks have been updated. | ||
| 865 | * \param marks The most recent list of marks and the time | ||
| 866 | * when it was updated. | ||
| 867 | * \see qi3pc::fetchMarks | ||
| 868 | */ | ||
| 869 | void marksUpdated(const qi3pc::DataArray& marks); | ||
| 870 | |||
| 871 | /*! | ||
| 872 | * \brief A specific bar's (cached) config have | ||
| 873 | * been updated. At this point the configuration for the bar has been cached. | ||
| 874 | * \param config The relevant bar's configuration. | ||
| 875 | * | ||
| 876 | * \see qi3pc::barConfigs | ||
| 877 | */ | ||
| 878 | void barConfigUpdated(const QJsonObject& config); | ||
| 879 | |||
| 880 | /*! | ||
| 881 | * \brief A new bar config have been added to the cache. | ||
| 882 | * \param id The id of the new bar. | ||
| 883 | * | ||
| 884 | * Only the id is stored at first. Call \ref qi3pc::fetchBarConfig with | ||
| 885 | * the appropriate id to update it. | ||
| 886 | */ | ||
| 887 | void newBarConfig(const QString& id); | ||
| 888 | |||
| 889 | /*! | ||
| 890 | * \brief The (cached) i3 version have been updated. | ||
| 891 | * \param version JSON object with the latest cached version and the | ||
| 892 | * time when it was updated. | ||
| 893 | * | ||
| 894 | * \see qi3pc::version | ||
| 895 | */ | ||
| 896 | void versionUpdated(const qi3pc::DataObject& version); | ||
| 897 | |||
| 898 | /*! | ||
| 899 | * \brief The (cached) list of modes have been updated. | ||
| 900 | * \param modes The most recent list of modes and the time | ||
| 901 | * when it was updated. | ||
| 902 | * | ||
| 903 | * \see qi3pc::modes | ||
| 904 | */ | ||
| 905 | void bindingModesUpdated(const qi3pc::DataArray& modes); | ||
| 906 | |||
| 907 | /*! | ||
| 908 | * \brief The (cached) config have been updated. | ||
| 909 | * \param config The most recent config and the time when it was updated. | ||
| 910 | * | ||
| 911 | * \see qi3pc::config | ||
| 912 | */ | ||
| 913 | void configUpdated(const qi3pc::DataObject& config); | ||
| 914 | |||
| 915 | /*! | ||
| 916 | * \brief The (cached) current binding state have been updated. | ||
| 917 | * \param state The most recent binding state and the time when it was updated. | ||
| 918 | * | ||
| 919 | * \see qi3pc::bindingState | ||
| 920 | */ | ||
| 921 | void bindingStateUpdated(const qi3pc::DataString& state); | ||
| 922 | }; | ||
| 923 | |||
| 924 | #endif // QI3PC_H | ||
| 925 |