27#include "qi3pc_global.h"
28#include <QtNetwork/qlocalsocket.h>
31#include <QJsonDocument>
33#include <QMutexLocker>
34#include <QProcessEnvironment>
41#include <QLoggingCategory>
43Q_DECLARE_LOGGING_CATEGORY(Qi3pcLogger);
52class QI3PCSHARED_EXPORT
qi3pc :
public QObject
69 Workspace = I3_IPC_EVENT_WORKSPACE,
71 Output = I3_IPC_EVENT_OUTPUT,
73 Mode = I3_IPC_EVENT_MODE,
75 Window = I3_IPC_EVENT_WINDOW,
77 BarUpdate = I3_IPC_EVENT_BARCONFIG_UPDATE,
79 Binding = I3_IPC_EVENT_BINDING,
81 Shutdown = I3_IPC_EVENT_SHUTDOWN,
83 Tick = I3_IPC_EVENT_TICK
94 Command = I3_IPC_REPLY_TYPE_COMMAND,
95 Workspaces = I3_IPC_REPLY_TYPE_WORKSPACES,
96 Subscribe = I3_IPC_REPLY_TYPE_SUBSCRIBE,
97 Outputs = I3_IPC_REPLY_TYPE_OUTPUTS,
98 Tree = I3_IPC_REPLY_TYPE_TREE,
99 Marks = I3_IPC_REPLY_TYPE_MARKS,
100 BarConfig = I3_IPC_REPLY_TYPE_BAR_CONFIG,
101 Version = I3_IPC_REPLY_TYPE_VERSION,
102 BindingModes = I3_IPC_REPLY_TYPE_BINDING_MODES,
103 Config = I3_IPC_REPLY_TYPE_CONFIG,
104 Tick = I3_IPC_REPLY_TYPE_TICK,
105 Sync = I3_IPC_REPLY_TYPE_SYNC,
106 BindingState = I3_IPC_REPLY_TYPE_GET_BINDING_STATE
187 using DataObject = std::optional<std::pair<QJsonObject, qint64>>;
190 using DataArray = std::optional<std::pair<QJsonArray, qint64>>;
193 using DataString = std::optional<std::pair<QString, qint64>>;
246 static std::optional<ParseError>
FromJSON(
const QJsonObject& json);
253 inline static constexpr auto _MEMBERS = std::to_array<std::pair<const char*, QStringMemberPtr>>
262 using Error = std::optional<ParseError>;
278 using Message = std::optional<std::pair<QJsonDocument, quint32>>;
284 explicit qi3pc(QObject* parent =
nullptr);
314 bool connect(
int msecs = 1500);
340 void subscribe(
const QStringList& events);
374 template<IpcType Type>
392 const DataArray& workspaces()
const;
400 const DataObject& tree()
const;
408 const DataArray& outputs()
const;
416 const DataArray& marks()
const;
426 const DataObject& barConfigs()
const;
435 const DataObject& version()
const;
443 const DataArray& bindingModes()
const;
451 const DataObject& config()
const;
459 const DataString& bindingState()
const;
468 void fetchWorkspaces();
504 void fetchBarConfig(
const QString&
id);
512 void fetchBarConfigs();
530 void fetchBindingModes();
546 void fetchBindingState();
554 Message processMessage(QLocalSocket& socket);
570 void processWorkspaceEvent(
const QJsonDocument& doc);
576 void processOutputEvent(
const QJsonDocument& doc);
582 void processModeEvent(
const QJsonDocument& doc);
588 void processWindowEvent(
const QJsonDocument& doc);
594 void processBarUpdateEvent(
const QJsonDocument& doc);
600 void processBindingEvent(
const QJsonDocument& doc);
606 void processShutdownEvent(
const QJsonDocument& doc);
612 void processTickEvent(
const QJsonDocument& doc);
618 void processCommandReply(
const QJsonDocument& doc);
624 void processWorkspaceReply(
const QJsonDocument& doc);
630 void processOutputReply(
const QJsonDocument& doc);
636 void processTreeReply(
const QJsonDocument& doc);
642 void processMarkReply(
const QJsonDocument& doc);
648 void processBarConfigReply(
const QJsonDocument& doc);
654 void processVersionReply(
const QJsonDocument& doc);
660 void processBindingModesReply(
const QJsonDocument& doc);
666 void processConfigReply(
const QJsonDocument& doc);
672 void processTickReply(
const QJsonDocument& doc);
678 void processSyncReply(
const QJsonDocument& doc);
684 void processBindingStateReply(
const QJsonDocument& doc);
691 static WorkspaceChange WorkspaceChangeFromString(
const QString& s);
698 static WindowChange WindowChangeFromString(
const QString& s);
705 static ShutdownChange ShutdownChangeFromString(
const QString& s);
712 static OutputChange OutputChangeFromString(
const QString& s);
719 static BindingChange BindingChangeFromString(
const QString& s);
728 static void WritePayload(QLocalSocket& socket,
const QByteArray& payload, IpcType type);
789 const QJsonObject& current,
790 const QJsonObject& old);
ShutdownChange
Types of change a shutdown event can have.
void configUpdated(const qi3pc::DataObject &config)
The (cached) config have been updated.
void treeUpdated(const qi3pc::DataObject &tree)
The layout tree cache have been updated.
WindowChange
Types of change a window event can have.
void barConfigUpdated(const QJsonObject &config)
A specific bar's (cached) config have been updated. At this point the configuration for the bar has b...
std::vector< std::pair< bool, Error > > CommandResults
Pairs of qi3pc::Error and boolean.
std::optional< std::pair< QJsonDocument, quint32 > > Message
Optional pair of a JSON document with a qi3pc::IpcType received with a message or an event before it ...
void barUpdateEvent(const QJsonObject &doc)
A bar's configuration have been updated.
QLocalSocket m_messageSocket
void workspacesUpdated(const qi3pc::DataArray &workspaces)
The (cached) list of workspaces have been updated.
void synced(bool success)
A sync message have been replied to by i3.
std::optional< ParseError > Error
Optional qi3pc::ParseError. The optional is empty when the error could not be parsed.
void tickSent(bool success)
A tick message have been replied to by i3.
QString socketPath() const
Get the socket path selected at construction.
bool connect(int msecs=1500)
Start listening to messages and events from the window manager.
const DataArray & marks() const
Get the (cached) list of set marks.
void subscribed(bool success)
A subscribe message have been replied to.
DataString m_bindingState
void bindingStateUpdated(const qi3pc::DataString &state)
The (cached) current binding state have been updated.
void outputEvent(qi3pc::OutputChange change)
Signal emitted when the output(s) change.
void versionUpdated(const qi3pc::DataObject &version)
The (cached) i3 version have been updated.
void outputsUpdated(const qi3pc::DataArray &outputs)
The (cached) outputs have been updated.
void modeEvent(QString change, bool pango)
The binding mode changes.
static constexpr auto IpcMagicLength
void windowEvent(qi3pc::WindowChange change, const QJsonObject &container)
A window changed.
QLocalSocket m_eventSocket
void subscribe(const QStringList &events)
Subscribe to a list of events.
void sendMessage(const QByteArray &payload=QByteArray())
Send a message with the specified type and payload to i3.
const DataArray & workspaces() const
Get the list of (cached) workspaces.
void newBarConfig(const QString &id)
A new bar config have been added to the cache.
static QString FindSocketPath()
Find the path to the i3 ipc local unix socket.
static void WritePayload(QLocalSocket &socket, const QByteArray &payload, IpcType type)
Send a message with the specified type and payload to i3 using the specified socket.
IpcType
Types of message/replies the API send/expect to/from i3wm.
static constexpr auto IpcMagicString
void tickEvent(const QString &payload)
A tick event is received from i3.
BindingChange
Types of change a binding event can have.
void marksUpdated(const qi3pc::DataArray &marks)
The (cached) list of marks have been updated.
OutputChange
Types of change an output event can have.
std::optional< std::pair< QJsonObject, qint64 > > DataObject
Optional pair of a JSON object with its last update time.
WorkspaceChange
Types of change a workspace event can have.
bool disconnect(int msecs=1500)
Stop listening to messages and events from the window manager.
qi3pc(QObject *parent=nullptr)
Construct a qi3pc object.
void bindingEvent(qi3pc::BindingChange change, const QJsonObject &binding, const QString &mode)
A binding have been triggered to run a command.
const DataObject & tree() const
Get the (cached) i3 layout tree.
void commandRan(qi3pc::CommandResults result)
A command have been ran by i3.
void shutdownEvent(qi3pc::ShutdownChange change)
The ipc socket is about to shutdown.
const DataObject & config() const
Get the (cached) data read from the config file.
void workspaceEvent(qi3pc::WorkspaceChange change, const QJsonObject ¤t, const QJsonObject &old)
The workspaces changed.
std::optional< std::pair< QString, qint64 > > DataString
Optional pair of a string with its last update time.
static QString FindSocketPathFromI3Binary()
Find the path to the i3 ipc local unix socket using the i3 binary.
void bindingModesUpdated(const qi3pc::DataArray &modes)
The (cached) list of modes have been updated.
std::optional< std::pair< QJsonArray, qint64 > > DataArray
Optional pair of a JSON array with its last update time.
const DataObject & version() const
Get the (cached) i3 version object.
bool isConnected()
Check if the connection to the ipc socket is established.
const DataArray & outputs() const
Get the (cached) list of outputs.
IpcEvent
Types of events offered by i3wm's IPC API.
Container for the attributes of a parsing error from i3wm when trying to run an unparsable command.
static constexpr auto _MEMBERS
Mapping of i3wm's parse error reply's attributes to members of ParseError.
QString errorPosition
The position where the error was detected in the input.
QString ParseError::* QStringMemberPtr
QString member pointer of ParseError.
QString input
The command that failed to run.
QString toString() const
Convert to a json like string.
QString error
Human readble error message.
bool operator==(const ParseError &other) const
Memberwise comparison of this ParseError and another.
static std::optional< ParseError > FromJSON(const QJsonObject &json)
Build an optional ParseError from a QJsonObject.