diff options
Diffstat (limited to 'lib/sway/sway.vala')
-rw-r--r-- | lib/sway/sway.vala | 85 |
1 files changed, 80 insertions, 5 deletions
diff --git a/lib/sway/sway.vala b/lib/sway/sway.vala index 8f20952..670ef3e 100644 --- a/lib/sway/sway.vala +++ b/lib/sway/sway.vala @@ -8,9 +8,6 @@ namespace AstalSway { private static Sway _instance; private Ipc ipc; private SocketConnection subscribe_socket; - - public Workspace focused_workspace; - public static Sway? get_default() { if (_instance != null) { @@ -32,6 +29,15 @@ namespace AstalSway { } } + public Workspace focused_workspace; + + private HashTable<string, Workspace> _workspaces = + new HashTable<string, Workspace>(str_hash, str_equal); + + public List<weak Workspace> workspaces { owned get { return _workspaces.get_values(); } } + + public Workspace get_workspace(string name) { return _workspaces.get(name); } + ~Sway() { if (subscribe_socket != null) { try { @@ -50,7 +56,26 @@ namespace AstalSway { return yield ipc.message_async(type, payload); } - public async void subscribe() { + public async void sync() { + var str = yield message_async(PayloadType.MESSAGE_GET_WORKSPACES, ""); + var arr = Json.from_string(str).get_array(); + foreach (var obj in arr.get_elements()) { + var name = obj.get_object().get_string_member("name"); + var ws = get_workspace(name); + if (ws != null) + ws.sync(obj.get_object()); + + } + } + + public signal void event(PayloadType type, string args); + + // Work space signals + + public signal void workspace_added(Workspace ws); + public signal void workspace_removed(string name); + + private async void subscribe() { if (subscribe_socket != null) { return; } @@ -59,8 +84,58 @@ namespace AstalSway { ipc.send(subscribe_socket.output_stream, PayloadType.MESSAGE_SUBSCRIBE, "[ \"workspace\", \"window\" ]"); while (true) { var result = yield ipc.receive_async(subscribe_socket.input_stream); - print(result.payload); + handle_event(result); + } + } + + private async void handle_event(IpcReponse reply) { + switch (reply.type) { + case PayloadType.MESSAGE_SUBSCRIBE: + return; + + case PayloadType.EVENT_WORKSPACE: + yield handle_workspace_event(reply.payload); + break; + + case PayloadType.EVENT_WINDOW: + // yield handle_client_event(reply.payload); + break; + + default: + break; + } + + event(reply.type, reply.payload); + } + + private async void handle_workspace_event(string args) { + var data = Json.from_string(args).get_object(); + + switch (data.get_string_member("change")) { + case "init": + var obj = data.get_object_member("current"); + var ws = new Workspace(); + _workspaces.insert(obj.get_string_member("name"), ws); + yield sync(); + workspace_added(ws); + break; + + case "empty": + var obj = data.get_object_member("current"); + var name = obj.get_string_member("name"); + _workspaces.remove(name); + workspace_removed(name); + var stuff = yield message_async(PayloadType.MESSAGE_GET_TREE, ""); + Node.build(Json.from_string(stuff).get_object()); + break; + + default: + break; } } + + private async void handle_client_event(string args) { + + } } } |