qi3pc 0.1.2
Qt based library to communicate with i3wm via its IPC API
Loading...
Searching...
No Matches
qi3pc.h
1#ifndef QI3PC_H
2#define QI3PC_H
3
4#include "QtNetwork/qlocalsocket.h"
5#include "qi3pc_global.h"
6#include <QDebug>
7#include <QJsonArray>
8#include <QJsonDocument>
9#include <QJsonObject>
10#include <QLocalSocket>
11#include <QMutexLocker>
12#include <QProcessEnvironment>
13#include <QTime>
14#include <i3/ipc.h>
15#include <optional>
16
24class QI3PCSHARED_EXPORT qi3pc : public QObject
25{
26 Q_OBJECT
27
28public:
29 // magic string used by the current API
30 static constexpr auto IpcMagicString = I3_IPC_MAGIC;
31 static constexpr auto IpcMagicLength = 6;
32
37 enum class IpcEvent : quint32
38 {
39 Workspace = I3_IPC_EVENT_WORKSPACE,
40 Output = I3_IPC_EVENT_OUTPUT,
41 Mode = I3_IPC_EVENT_MODE,
42 Window = I3_IPC_EVENT_WINDOW,
43 BarUpdate = I3_IPC_EVENT_BARCONFIG_UPDATE,
44 Binding = I3_IPC_EVENT_BINDING,
45 Shutdown = I3_IPC_EVENT_SHUTDOWN,
46 Tick = I3_IPC_EVENT_TICK
47 };
48
53 enum class IpcType : quint32
54 {
55 Command = I3_IPC_REPLY_TYPE_COMMAND,
56 Workspaces = I3_IPC_REPLY_TYPE_WORKSPACES,
57 Subscribe = I3_IPC_REPLY_TYPE_SUBSCRIBE,
58 Outputs = I3_IPC_REPLY_TYPE_OUTPUTS,
59 Tree = I3_IPC_REPLY_TYPE_TREE,
60 Marks = I3_IPC_REPLY_TYPE_MARKS,
61 BarConfig = I3_IPC_REPLY_TYPE_BAR_CONFIG,
62 Version = I3_IPC_REPLY_TYPE_VERSION,
63 BindingModes = I3_IPC_REPLY_TYPE_BINDING_MODES,
64 Config = I3_IPC_REPLY_TYPE_CONFIG,
65 Tick = I3_IPC_REPLY_TYPE_TICK,
66 Sync = I3_IPC_REPLY_TYPE_SYNC
67 };
68
76 enum class WorkspaceChange
77 {
78 Focus,
79 Init,
80 Empty,
81 Urgent,
82 Reload,
83 Rename,
84 Restored,
85 Move,
86 Unknown
87 };
88
97 enum class OutputChange
98 {
99 Unspecified,
100 Unknown
101 };
102
111 enum class WindowChange
112 {
113 New,
114 Close,
115 Focus,
116 Title,
117 Fullscreen,
118 Move,
119 Floating,
120 Urgent,
121 Mark,
122 Unknown
123 };
124
133 enum class ShutdownChange
134 {
135 Restart,
136 Exit,
137 Unknown
138 };
139
148 enum class BindingChange
149 {
150 Run,
151 Unknown
152 };
153
157 using DataObject = std::optional<std::pair<QJsonObject, qint64>>;
158
162 using DataArray = std::optional<std::pair<QJsonArray, qint64>>;
163 using Error = std::optional<QString>;
164 using Message = std::optional<std::pair<QJsonDocument, quint32>>;
165
170 explicit qi3pc(QObject* parent = nullptr);
171
178 virtual ~qi3pc();
179
191 bool connect();
192
200 bool connected();
201
208 bool disconnect();
209
216 void subscribe(const QStringList& events);
217
222 QString socketPath() const;
223
236 void sendMessage(IpcType type, const QByteArray& payload = QByteArray());
237
244 DataArray workspaces() const;
245
252 DataObject tree() const;
253
260 DataArray outputs() const;
261
268 DataArray marks() const;
269
278 DataObject barConfigs() const;
279
287 DataObject version() const;
288
295 DataArray modes() const;
296
303 DataObject config() const;
304
305private:
311 Message processMessage(QLocalSocket& socket);
312
316 void processEvent();
317
321 void processReply();
322
327 void processWorkspaceEvent(const QJsonDocument& doc);
328
333 void processOutputEvent(const QJsonDocument& doc);
334
339 void processModeEvent(const QJsonDocument& doc);
340
345 void processWindowEvent(const QJsonDocument& doc);
346
351 void processBarUpdateEvent(const QJsonDocument& doc);
352
357 void processBindingEvent(const QJsonDocument& doc);
358
363 void processShutdownEvent(const QJsonDocument& doc);
364
369 void processTickEvent(const QJsonDocument& doc);
370
375 void processCommandReply(const QJsonDocument& doc);
376
381 void processWorkspaceReply(const QJsonDocument& doc);
382
387 void processOutputReply(const QJsonDocument& doc);
388
393 void processTreeReply(const QJsonDocument& doc);
394
399 void processMarkReply(const QJsonDocument& doc);
400
405 void processBarConfigReply(const QJsonDocument& doc);
406
411 void processVersionReply(const QJsonDocument& doc);
412
417 void processModeReply(const QJsonDocument& doc);
418
423 void processConfigReply(const QJsonDocument& doc);
424
429 void processTickReply(const QJsonDocument& doc);
430
435 void processSyncReply(const QJsonDocument& doc);
436
442 WorkspaceChange workspaceChangeFromString(const QString& s) const;
443
449 WindowChange windowChangeFromString(const QString& s) const;
450
456 ShutdownChange shutdownChangeFromString(const QString& s) const;
457
463 OutputChange outputChangeFromString(const QString& s) const;
464
470 BindingChange bindingChangeFromString(const QString& s) const;
471
472private:
473 QString m_socketPath;
475 QLocalSocket m_eventSocket;
476 QLocalSocket m_messageSocket;
487signals:
496 void commandRan(bool success, qi3pc::Error error);
497
505 void tickSent(bool success);
506
515 void synced(bool success);
516
524 void subscribed(bool success);
525
533 const QJsonObject& current,
534 const QJsonObject& old);
540
547 void modeEvent(QString change, bool pango);
548
554 void windowEvent(qi3pc::WindowChange change, const QJsonObject& container);
555
561 void barUpdateEvent(const QJsonObject& doc);
562
569 void bindingEvent(qi3pc::BindingChange change, const QJsonObject& binding);
570
576
584 void tickEvent(bool first, const QJsonObject& payload);
585
593
601 void workspacesUpdated(const qi3pc::DataArray& workspaces);
602
609 void fetchTree();
610
619
628
634 void outputsUpdated(const qi3pc::DataArray& outputs);
635
643
651 void marksUpdated(const qi3pc::DataArray& marks);
652
662 void fetchBarConfig(const QString& id);
663
671
679 void barConfigUpdated(const QJsonObject& config);
680
689 void newBarConfig(const QString& id);
690
699
708 void versionUpdated(const qi3pc::DataObject& version);
709
718
727 void modesUpdated(const qi3pc::DataArray& modes);
728
736
743 void configUpdated(const qi3pc::DataObject& config);
744};
745
746#endif // QI3PC_H
Class mapping to the to the i3wm IPC interface.
Definition qi3pc.h:25
ShutdownChange
Enum holding the types of change a shutdown event can have.
Definition qi3pc.h:134
void configUpdated(const qi3pc::DataObject &config)
Signal emitted when the (cached) config have been updated.
DataArray m_workspaces
Definition qi3pc.h:479
void treeUpdated(const qi3pc::DataObject &tree)
Signal emitted when the layout tree cache have been updated.
WindowChange
Definition qi3pc.h:112
void barConfigUpdated(const QJsonObject &config)
Signal emitted when a specific bar's (cached) config have been updated.
void fetchConfig()
Signal to emit to trigger an update of the (cached) config.
DataArray m_modes
Definition qi3pc.h:484
void fetchBarConfigs()
Signal to emit to update the list of bar configurations.
DataObject m_version
Definition qi3pc.h:483
void bindingEvent(qi3pc::BindingChange change, const QJsonObject &binding)
Signal emitteed when a binding have been triggered to run a command.
void barUpdateEvent(const QJsonObject &doc)
Signal emitted when a bar's configuration have been updated.
QLocalSocket m_messageSocket
Definition qi3pc.h:476
void workspacesUpdated(const qi3pc::DataArray &workspaces)
Signal emitted when the (cached) list of workspaces have been updated.
void synced(bool success)
Signal emitted when a sync message have been replied to by i3.
void tickSent(bool success)
Signal emitted when a tick have been processed by i3.
DataObject m_config
Definition qi3pc.h:485
void tickEvent(bool first, const QJsonObject &payload)
Signal emitted when subscribing to tick events or when a tick message have been sent to the ipc conne...
void subscribed(bool success)
Signal emitted when a subscribe message have been replied to.
void fetchBarConfig(const QString &id)
Signal to emit to update the (cached) configuration of a certain bar.
void outputEvent(qi3pc::OutputChange change)
Signal emitted when the output(s) change.
void fetchTree()
Signal to emit to trigger an update of the (cached) layout tree.
void modesUpdated(const qi3pc::DataArray &modes)
Signal emitted when the (cached) list of modes have been updated.
DataArray m_marks
Definition qi3pc.h:481
void fetchOutputs()
Signal to emit to trigger an update of the (cached) outputs.
void versionUpdated(const qi3pc::DataObject &version)
Signal emitted when the (cached) i3 version have been updated.
void outputsUpdated(const qi3pc::DataArray &outputs)
Signal emitted when (cached) outputs have been updated.
void modeEvent(QString change, bool pango)
Signal emitted when the binding mode changes.
void windowEvent(qi3pc::WindowChange change, const QJsonObject &container)
Signal emitted when a window changes.
QLocalSocket m_eventSocket
Definition qi3pc.h:475
void fetchMarks()
Signal to emit to trigger an update of the (cached) list of marks.
void newBarConfig(const QString &id)
Signal emitted when a new bar config have been added to the cache.
void fetchVersion()
Signal to emit to trigger a cache update for the i3wm version.
void commandRan(bool success, qi3pc::Error error)
Signal emitted when a command have been ran by i3.
IpcType
Enum holding the types of messages the API expect to send and receive replies for from i3.
Definition qi3pc.h:54
DataObject m_barConfigs
Definition qi3pc.h:482
BindingChange
Definition qi3pc.h:149
void marksUpdated(const qi3pc::DataArray &marks)
Signal emitted when the (cached) list of marks have been updated.
OutputChange
Enum holding the types of change an output event can have.
Definition qi3pc.h:98
std::optional< std::pair< QJsonObject, qint64 > > DataObject
Definition qi3pc.h:157
WorkspaceChange
Enum holding the types of change a workspace event can have.
Definition qi3pc.h:77
void fetchWorkspaces()
Signal to emit to trigger an update of the list of workspace cache.
DataArray m_outputs
Definition qi3pc.h:480
void shutdownEvent(qi3pc::ShutdownChange change)
Signal emitted when the ipc socket i about to shutdown.
void workspaceEvent(qi3pc::WorkspaceChange change, const QJsonObject &current, const QJsonObject &old)
Signal emitted with a workspace event's data preprocessed.
void fetchModes()
Signal to emit to trigger an update of the (cached) list of modes.
std::optional< std::pair< QJsonArray, qint64 > > DataArray
Definition qi3pc.h:162
DataObject m_tree
Definition qi3pc.h:478
QString m_socketPath
Definition qi3pc.h:473
IpcEvent
Enum holding the types of events offered by the i3wm's IPC API.
Definition qi3pc.h:38