4#include "qi3pc_global.h"
5#include <QtNetwork/qlocalsocket.h>
8#include <QJsonDocument>
10#include <QMutexLocker>
11#include <QProcessEnvironment>
26class QI3PCSHARED_EXPORT
qi3pc :
public QObject
32 static constexpr auto IpcMagicString = std::string(
"i3-ipc");
33 static constexpr auto IpcMagicLength = IpcMagicString.length();
41 Workspace = I3_IPC_EVENT_WORKSPACE,
42 Output = I3_IPC_EVENT_OUTPUT,
43 Mode = I3_IPC_EVENT_MODE,
44 Window = I3_IPC_EVENT_WINDOW,
45 BarUpdate = I3_IPC_EVENT_BARCONFIG_UPDATE,
46 Binding = I3_IPC_EVENT_BINDING,
47 Shutdown = I3_IPC_EVENT_SHUTDOWN,
48 Tick = I3_IPC_EVENT_TICK
57 Command = I3_IPC_REPLY_TYPE_COMMAND,
58 Workspaces = I3_IPC_REPLY_TYPE_WORKSPACES,
59 Subscribe = I3_IPC_REPLY_TYPE_SUBSCRIBE,
60 Outputs = I3_IPC_REPLY_TYPE_OUTPUTS,
61 Tree = I3_IPC_REPLY_TYPE_TREE,
62 Marks = I3_IPC_REPLY_TYPE_MARKS,
63 BarConfig = I3_IPC_REPLY_TYPE_BAR_CONFIG,
64 Version = I3_IPC_REPLY_TYPE_VERSION,
65 BindingModes = I3_IPC_REPLY_TYPE_BINDING_MODES,
66 Config = I3_IPC_REPLY_TYPE_CONFIG,
67 Tick = I3_IPC_REPLY_TYPE_TICK,
68 Sync = I3_IPC_REPLY_TYPE_SYNC,
69 BindingState = I3_IPC_REPLY_TYPE_GET_BINDING_STATE
160 using DataObject = std::optional<std::pair<QJsonObject, qint64>>;
165 using DataArray = std::optional<std::pair<QJsonArray, qint64>>;
170 using String = std::optional<std::pair<QString, qint64>>;
171 using Error = std::optional<QString>;
172 using Message = std::optional<std::pair<QJsonDocument, quint32>>;
178 explicit qi3pc(QObject* parent =
nullptr);
224 void subscribe(
const QStringList& events);
581 void writeMagicString(QLocalSocket& socket)
const;
583 void writePayload(QLocalSocket& socket,
const QByteArray& payload,
IpcType type)
const;
647 const QJsonObject& current,
648 const QJsonObject& old);
ShutdownChange
Enum holding the types of change a shutdown event can have.
Definition qi3pc.h:137
@ Restart
Definition qi3pc.h:138
@ Exit
Definition qi3pc.h:139
void configUpdated(const qi3pc::DataObject &config)
Signal emitted when the (cached) config have been updated.
DataArray m_workspaces
Definition qi3pc.h:592
void treeUpdated(const qi3pc::DataObject &tree)
Signal emitted when the layout tree cache have been updated.
WindowChange
Definition qi3pc.h:115
@ New
Definition qi3pc.h:116
@ Fullscreen
Definition qi3pc.h:120
@ Title
Definition qi3pc.h:119
@ Mark
Definition qi3pc.h:124
@ Close
Definition qi3pc.h:117
void processOutputEvent(const QJsonDocument &doc)
Handle data received from an output event.
Definition qi3pc.cpp:246
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.
Definition qi3pc.cpp:623
void processBindingEvent(const QJsonDocument &doc)
Handle data received from a binding event.
Definition qi3pc.cpp:282
void fetchBarConfigs()
Signal to emit to update the list of bar configurations.
Definition qi3pc.cpp:629
DataObject m_version
Definition qi3pc.h:596
void fetchBindingModes()
Signal to emit to trigger an update of the (cached) list of modes.
Definition qi3pc.cpp:617
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:589
void workspacesUpdated(const qi3pc::DataArray &workspaces)
Signal emitted when the (cached) list of workspaces have been updated.
std::optional< std::pair< QString, qint64 > > String
Definition qi3pc.h:170
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.
void processBindingModesReply(const QJsonDocument &doc)
Handle data received in a binding mode reply.
Definition qi3pc.cpp:196
QString socketPath() const
Get the path to the i3 ipc local unix socket.
Definition qi3pc.cpp:424
DataObject m_config
Definition qi3pc.h:599
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 processSyncReply(const QJsonDocument &doc)
Handle data received from a sync reply.
Definition qi3pc.cpp:218
String m_bindingState
Definition qi3pc.h:598
void fetchBarConfig(const QString &id)
Signal to emit to update the (cached) configuration of a certain bar.
Definition qi3pc.cpp:635
void processBarUpdateEvent(const QJsonDocument &doc)
Handle data received from an update event.
Definition qi3pc.cpp:276
void outputEvent(qi3pc::OutputChange change)
Signal emitted when the output(s) change.
void processBarConfigReply(const QJsonDocument &doc)
Handle data received from a bar config reply.
Definition qi3pc.cpp:168
void fetchTree()
Signal to emit to trigger an update of the (cached) layout tree.
Definition qi3pc.cpp:593
void processReply()
Read data from the message socket.
Definition qi3pc.cpp:72
DataArray m_marks
Definition qi3pc.h:594
DataObject tree() const
Get the (cached) i3 layout tree.
Definition qi3pc.cpp:449
void fetchOutputs()
Signal to emit to trigger an update of the (cached) outputs.
Definition qi3pc.cpp:599
void versionUpdated(const qi3pc::DataObject &version)
Signal emitted when the (cached) i3 version have been updated.
DataObject config() const
Get the (cached) data read from the config file.
Definition qi3pc.cpp:484
void outputsUpdated(const qi3pc::DataArray &outputs)
Signal emitted when (cached) outputs have been updated.
bool connect()
Connect to the unix socket.
Definition qi3pc.cpp:496
void modeEvent(QString change, bool pango)
Signal emitted when the binding mode changes.
void processMarkReply(const QJsonDocument &doc)
Handle data received from a mark reply.
Definition qi3pc.cpp:160
void fetchBindingState()
Request update of the (cached) binding state.
Definition qi3pc.cpp:643
ShutdownChange shutdownChangeFromString(const QString &s) const
Convert a string into a shutdown change object.
Definition qi3pc.cpp:362
void windowEvent(qi3pc::WindowChange change, const QJsonObject &container)
Signal emitted when a window changes.
QLocalSocket m_eventSocket
Definition qi3pc.h:588
void processWindowEvent(const QJsonDocument &doc)
Handle data received from a window event.
Definition qi3pc.cpp:264
void subscribe(const QStringList &events)
Subscribe to a list of events.
Definition qi3pc.cpp:556
void fetchMarks()
Signal to emit to trigger an update of the (cached) list of marks.
Definition qi3pc.cpp:605
DataObject barConfigs() const
Get the (cached) list of all bar configurations.
Definition qi3pc.cpp:467
void processVersionReply(const QJsonDocument &doc)
Handle data received from a version reply.
Definition qi3pc.cpp:188
void newBarConfig(const QString &id)
Signal emitted when a new bar config have been added to the cache.
void processWorkspaceReply(const QJsonDocument &doc)
Handle data received from a workspace reply.
Definition qi3pc.cpp:138
String bindingState() const
Get the (cached) binding state.
Definition qi3pc.cpp:490
OutputChange outputChangeFromString(const QString &s) const
Convert a string into an output change object.
Definition qi3pc.cpp:374
void fetchVersion()
Signal to emit to trigger a cache update for the i3wm version.
Definition qi3pc.cpp:611
bool disconnect()
Disconnect from the ipc.
Definition qi3pc.cpp:517
BindingChange bindingChangeFromString(const QString &s) const
Convert a string into a binding change object.
Definition qi3pc.cpp:384
void processConfigReply(const QJsonDocument &doc)
Handle data received from a config reply.
Definition qi3pc.cpp:204
DataArray outputs() const
Get the (cached) list of outputs.
Definition qi3pc.cpp:455
void processTickReply(const QJsonDocument &doc)
Handle data received from a tick reply.
Definition qi3pc.cpp:212
WorkspaceChange workspaceChangeFromString(const QString &s) const
Convert a string into a workspace change object.
Definition qi3pc.cpp:312
void processTickEvent(const QJsonDocument &doc)
Handle data received from a tick event.
Definition qi3pc.cpp:306
void processBindingStateReply(const QJsonDocument &doc)
Handle data received from a binding state event reply.
Definition qi3pc.cpp:224
void commandRan(bool success, qi3pc::Error error)
Signal emitted when a command have been ran by i3.
DataArray workspaces() const
Get the list of (cached) worksaces.
Definition qi3pc.cpp:443
IpcType
Enum holding the types of messages the API expect to send and receive replies for from i3.
Definition qi3pc.h:56
void bindingStateUpdated(const qi3pc::String &state)
Signal emitted when the (cached) current binding state have been updated.
void processTreeReply(const QJsonDocument &doc)
Handle data received from a tree reply.
Definition qi3pc.cpp:153
WindowChange windowChangeFromString(const QString &s) const
Convert a string into a window change object.
Definition qi3pc.cpp:336
void processShutdownEvent(const QJsonDocument &doc)
Handle data received from a shutdowm event.
Definition qi3pc.cpp:294
DataArray marks() const
Get the (cached) list of set marks.
Definition qi3pc.cpp:461
DataObject m_barConfigs
Definition qi3pc.h:595
DataArray bindingModes() const
Get the (cached) list of binding modes.
Definition qi3pc.cpp:478
BindingChange
Definition qi3pc.h:152
@ Run
Definition qi3pc.h:153
void processModeEvent(const QJsonDocument &doc)
Handle data received from a mode event.
Definition qi3pc.cpp:258
Message processMessage(QLocalSocket &socket)
Read one message using the socket parameter.
Definition qi3pc.cpp:394
void marksUpdated(const qi3pc::DataArray &marks)
Signal emitted when the (cached) list of marks have been updated.
void processEvent()
Read data from the event socket.
Definition qi3pc.cpp:24
OutputChange
Enum holding the types of change an output event can have.
Definition qi3pc.h:101
@ Unspecified
Definition qi3pc.h:102
std::optional< std::pair< QJsonObject, qint64 > > DataObject
Definition qi3pc.h:160
WorkspaceChange
Enum holding the types of change a workspace event can have.
Definition qi3pc.h:80
void processCommandReply(const QJsonDocument &doc)
Handle data received from a run command reply.
Definition qi3pc.cpp:127
DataArray m_bindingModes
Definition qi3pc.h:597
qi3pc(QObject *parent=nullptr)
Construct an qi3pc object.
Definition qi3pc.cpp:3
void sendMessage(IpcType type, const QByteArray &payload=QByteArray())
Send a message with the specified type and payload to i3.
Definition qi3pc.cpp:550
void fetchWorkspaces()
Signal to emit to trigger an update of the list of workspace cache.
Definition qi3pc.cpp:587
void processOutputReply(const QJsonDocument &doc)
Handle data received from an output reply.
Definition qi3pc.cpp:145
DataArray m_outputs
Definition qi3pc.h:593
void shutdownEvent(qi3pc::ShutdownChange change)
Signal emitted when the ipc socket i about to shutdown.
void workspaceEvent(qi3pc::WorkspaceChange change, const QJsonObject ¤t, const QJsonObject &old)
Signal emitted with a workspace event's data preprocessed.
void bindingModesUpdated(const qi3pc::DataArray &modes)
Signal emitted when the (cached) list of modes have been updated.
DataObject version() const
Get the (cached) i3 version object.
Definition qi3pc.cpp:473
std::optional< std::pair< QJsonArray, qint64 > > DataArray
Definition qi3pc.h:165
bool isConnected()
Check if the connection to the ipc socket is established.
Definition qi3pc.cpp:537
DataObject m_tree
Definition qi3pc.h:591
QString m_socketPath
Definition qi3pc.h:586
void processWorkspaceEvent(const QJsonDocument &doc)
Handle data received from a workspace event.
Definition qi3pc.cpp:232
IpcEvent
Enum holding the types of events offered by the i3wm's IPC API.
Definition qi3pc.h:40