i3/sway: add support for the I3 and Sway IPC

sway: add urgent and focused dispatchers to workspaces

flake: add sway toggle

WIP sway: add monitor status

sway: handle multiple ipc events in one line

sway: reuse socket connection for dispatches & better command type handling

WIP sway: add associated monitor to a workspace

i3/sway: update to allow for i3 compatibility

i3/sway: manage setting the focused monitors

i3/sway: fix multi monitor crash

i3/sway: fix linting errors

i3/sway: update nix package flag naming to i3

i3/sway: add documentation, fix module.md and impl monitorFor

i3/sway: handle more workspace ipc events

i3/sway: fix review

i3/sway: fix crash due to newline breaking up an IPC message

i3/sway: handle broken messages by forwarding to the next magic sequence

i3/sway: break loop when buffer is empty

i3/sway: fix monitor focus & focused monitor signal not being emitted

i3/sway: use datastreams instead of qbytearrays for socket reading

i3/sway: fix lint issues

i3/sway: drop second socket connection, remove dispatch return value, recreate IPC connection on fatal error

i3/sway: handle run_command responses

i3/sway: remove reconnection on unknown event

i3/sway: fix formatting, lint & avoid writing to socket if connection is not open
This commit is contained in:
Nydragon 2024-11-02 03:52:27 +01:00
parent 84ce47b6d3
commit 31adcaac76
No known key found for this signature in database
15 changed files with 1252 additions and 1 deletions

View file

@ -0,0 +1,151 @@
#pragma once
#include <qbytearrayview.h>
#include <qjsondocument.h>
#include <qjsonobject.h>
#include <qlocalsocket.h>
#include <qobject.h>
#include <qqml.h>
#include <qqmlintegration.h>
#include <qtmetamacros.h>
#include <qtypes.h>
#include "../../../core/model.hpp"
#include "../../../core/qmlscreen.hpp"
namespace qs::i3::ipc {
class I3Workspace;
class I3Monitor;
} // namespace qs::i3::ipc
Q_DECLARE_OPAQUE_POINTER(qs::i3::ipc::I3Workspace*);
Q_DECLARE_OPAQUE_POINTER(qs::i3::ipc::I3Monitor*);
namespace qs::i3::ipc {
constexpr std::string MAGIC = "i3-ipc";
enum EventCode {
RunCommand = 0,
GetWorkspaces = 1,
Subscribe = 2,
GetOutputs = 3,
GetTree = 4,
Workspace = 0x80000000,
Output = 0x80000001,
Mode = 0x80000002,
Window = 0x80000003,
BarconfigUpdate = 0x80000004,
Binding = 0x80000005,
Shutdown = 0x80000006,
Tick = 0x80000007,
BarStateUpdate = 0x80000014,
Input = 0x80000015,
Unknown = 999,
};
using Event = std::tuple<EventCode, QJsonDocument>;
///! I3/Sway IPC Events
/// Emitted by @@I3.rawEvent(s)
class I3IpcEvent: public QObject {
Q_OBJECT;
/// The name of the event
Q_PROPERTY(QString type READ type CONSTANT);
/// The payload of the event in JSON format.
Q_PROPERTY(QString data READ data CONSTANT);
QML_NAMED_ELEMENT(I3Event);
QML_UNCREATABLE("I3IpcEvents cannot be created.");
public:
I3IpcEvent(QObject* parent): QObject(parent) {}
[[nodiscard]] QString type() const;
[[nodiscard]] QString data() const;
EventCode mCode = EventCode::Unknown;
QJsonDocument mData;
static EventCode intToEvent(uint32_t raw);
static QString eventToString(EventCode event);
};
class I3Ipc: public QObject {
Q_OBJECT;
public:
static I3Ipc* instance();
[[nodiscard]] QString socketPath() const;
void makeRequest(const QByteArray& request);
void dispatch(const QString& payload);
static QByteArray buildRequestMessage(EventCode cmd, const QByteArray& payload = QByteArray());
I3Workspace* findWorkspaceByName(const QString& name);
I3Monitor* findMonitorByName(const QString& name);
I3Workspace* findWorkspaceByID(qint32 id);
void setFocusedWorkspace(I3Workspace* workspace);
void setFocusedMonitor(I3Monitor* monitor);
void refreshWorkspaces();
void refreshMonitors();
I3Monitor* monitorFor(QuickshellScreenInfo* screen);
[[nodiscard]] I3Monitor* focusedMonitor() const;
[[nodiscard]] I3Workspace* focusedWorkspace() const;
[[nodiscard]] ObjectModel<I3Monitor>* monitors();
[[nodiscard]] ObjectModel<I3Workspace>* workspaces();
signals:
void connected();
void rawEvent(I3IpcEvent* event);
void focusedWorkspaceChanged();
void focusedMonitorChanged();
private slots:
void eventSocketError(QLocalSocket::LocalSocketError error) const;
void eventSocketStateChanged(QLocalSocket::LocalSocketState state);
void eventSocketReady();
void subscribe();
void onFocusedWorkspaceDestroyed();
void onFocusedMonitorDestroyed();
private:
explicit I3Ipc();
void onEvent(I3IpcEvent* event);
void handleWorkspaceEvent(I3IpcEvent* event);
void handleGetWorkspacesEvent(I3IpcEvent* event);
void handleGetOutputsEvent(I3IpcEvent* event);
static void handleRunCommand(I3IpcEvent* event);
void reconnectIPC();
QVector<std::tuple<EventCode, QJsonDocument>> parseResponse();
QLocalSocket liveEventSocket;
QDataStream liveEventSocketDs;
QString mSocketPath;
bool valid = false;
ObjectModel<I3Monitor> mMonitors {this};
ObjectModel<I3Workspace> mWorkspaces {this};
I3IpcEvent event {this};
I3Workspace* mFocusedWorkspace = nullptr;
I3Monitor* mFocusedMonitor = nullptr;
};
} // namespace qs::i3::ipc