Skip to content

Commit e5c9386

Browse files
committed
i3/ipc: expose subscribe method to receive arbitrary events
1 parent fc704e6 commit e5c9386

File tree

4 files changed

+18
-6
lines changed

4 files changed

+18
-6
lines changed

src/x11/i3/ipc/connection.cpp

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ I3Ipc::I3Ipc() {
8585
QObject::connect(&this->liveEventSocket, &QLocalSocket::errorOccurred, this, &I3Ipc::eventSocketError);
8686
QObject::connect(&this->liveEventSocket, &QLocalSocket::stateChanged, this, &I3Ipc::eventSocketStateChanged);
8787
QObject::connect(&this->liveEventSocket, &QLocalSocket::readyRead, this, &I3Ipc::eventSocketReady);
88-
QObject::connect(&this->liveEventSocket, &QLocalSocket::connected, this, &I3Ipc::subscribe);
88+
QObject::connect(&this->liveEventSocket, &QLocalSocket::connected, this, &I3Ipc::eventSocketConnected);
8989
// clang-format on
9090

9191
this->liveEventSocketDs.setDevice(&this->liveEventSocket);
@@ -94,18 +94,23 @@ I3Ipc::I3Ipc() {
9494
this->liveEventSocket.connectToServer(this->mSocketPath);
9595
}
9696

97-
void I3Ipc::subscribe() {
97+
void I3Ipc::eventSocketConnected() {
9898
auto payload = QByteArray(R"(["workspace","output"])");
99-
auto message = I3Ipc::buildRequestMessage(EventCode::Subscribe, payload);
100-
101-
this->makeRequest(message);
99+
this->subscribeEvent(payload);
102100

103101
// Workspaces must be refreshed before monitors or no focus will be
104102
// detected on launch.
105103
this->refreshWorkspaces();
106104
this->refreshMonitors();
107105
}
108106

107+
void I3Ipc::subscribe(const QString& payload) { this->subscribeEvent(payload.toLocal8Bit()); }
108+
109+
void I3Ipc::subscribeEvent(const QByteArray& payload) {
110+
auto message = I3Ipc::buildRequestMessage(EventCode::Subscribe, payload);
111+
this->makeRequest(message);
112+
}
113+
109114
void I3Ipc::eventSocketReady() {
110115
for (auto& [type, data]: this->parseResponse()) {
111116
this->event.mCode = type;

src/x11/i3/ipc/connection.hpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ class I3Ipc: public QObject {
8585

8686
void makeRequest(const QByteArray& request);
8787
void dispatch(const QString& payload);
88+
void subscribe(const QString& payload);
8889

8990
static QByteArray buildRequestMessage(EventCode cmd, const QByteArray& payload = QByteArray());
9091

@@ -120,7 +121,7 @@ private slots:
120121
void eventSocketError(QLocalSocket::LocalSocketError error) const;
121122
void eventSocketStateChanged(QLocalSocket::LocalSocketState state);
122123
void eventSocketReady();
123-
void subscribe();
124+
void eventSocketConnected();
124125

125126
void onFocusedMonitorDestroyed();
126127

@@ -135,6 +136,8 @@ private slots:
135136
static void handleRunCommand(I3IpcEvent* event);
136137
static bool compareWorkspaces(I3Workspace* a, I3Workspace* b);
137138

139+
void subscribeEvent(const QByteArray& payload);
140+
138141
void reconnectIPC();
139142

140143
QVector<std::tuple<EventCode, QJsonDocument>> parseResponse();

src/x11/i3/ipc/qml.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ I3IpcQml::I3IpcQml() {
2323
}
2424

2525
void I3IpcQml::dispatch(const QString& request) { I3Ipc::instance()->dispatch(request); }
26+
void I3IpcQml::subscribe(const QString& request) { I3Ipc::instance()->subscribe(request); }
2627
void I3IpcQml::refreshMonitors() { I3Ipc::instance()->refreshMonitors(); }
2728
void I3IpcQml::refreshWorkspaces() { I3Ipc::instance()->refreshWorkspaces(); }
2829
QString I3IpcQml::socketPath() { return I3Ipc::instance()->socketPath(); }

src/x11/i3/ipc/qml.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ class I3IpcQml: public QObject {
3535
/// Execute an [I3/Sway command](https://i3wm.org/docs/userguide.html#list_of_commands)
3636
Q_INVOKABLE static void dispatch(const QString& request);
3737

38+
/// Subscribe to an I3/Sway event, allows reception of subscribed events through rawEvent
39+
Q_INVOKABLE static void subscribe(const QString& request);
40+
3841
/// Refresh monitor information.
3942
Q_INVOKABLE static void refreshMonitors();
4043

0 commit comments

Comments
 (0)