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