summaryrefslogtreecommitdiff
path: root/lib/sway/sway.vala
blob: 2ef10a27cc6f05fc8c925f788c3d57a73b064a57 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
namespace AstalSway {
    public Sway get_default() {
        return Sway.get_default();
    }


  public class Sway : Object {
    private static Sway _instance;
    private Ipc ipc;
    private SocketConnection subscribe_socket;

    public static Sway? get_default() {
      if (_instance != null) {
        return _instance;
      }
     
      var s = new Sway();
      var ipc = new Ipc();
      
s.notify.connect((_, p) => {
    stdout.printf("Property '%s' has changed!\n", p.name);
});
      try {
        ipc.init();
        s.ipc = ipc;
        s.subscribe.begin();
        _instance = s;
        return s;
      } catch (Error err) {
        critical(err.message);
        return null;
      }

    }

    public Workspace focused_workspace;
  
    private static HashTable<int, Node> _nodes =
        new HashTable<int, Node>(i => i, (a,b) => a==b);
    
    public List<weak Node> nodes { owned get { return _nodes.get_values(); } }
    
    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 {
              subscribe_socket.close(null);
          } catch (Error err) {
              critical(err.message);
          }
        }
    }

    public string message(PayloadType type, string payload) {
      return ipc.message(type, payload);
    }
    
    public async string message_async(PayloadType type, string payload) {
      return yield ipc.message_async(type, payload);
    }

    public void run_command(string command) {
      message_async.begin(PayloadType.MESSAGE_RUN_COMMAND, command, 
        (_, res) => {
          var obj = Json.from_string(message_async.end(res)).get_object();
          if (!obj.get_boolean_member("success")) {
            critical("Command error: %s", obj.get_string_member("error"));
          }
        }
      );
    }

    public async void sync() {
      yield Node.sync_tree();
      _nodes = Node._all_nodes;
      
      var new_workspaces = new HashTable<string, Workspace>(str_hash, str_equal);
      foreach (var node in nodes) {
        switch (node.node_type) {
          case NodeType.WORKSPACE:
            var ws = node as Workspace;
            new_workspaces.insert(ws.name, ws);
            if (ws.focused) {
              focused_workspace = ws;
            }
            break;
        }
      }
    }

    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);
    public signal void workspace_focus(Workspace new_ws, Workspace old_ws);

    private async void subscribe() {
      if (subscribe_socket != null) {
        return;
      }

      subscribe_socket = ipc.connection();
      ipc.send(subscribe_socket.output_stream, PayloadType.MESSAGE_SUBSCRIBE, "[ \"workspace\", \"window\" ]");
      while (true) {
        var result = yield ipc.receive_async(subscribe_socket.input_stream);
        handle_event(result);
      }
    }

    private async void handle_event(IpcReponse reply) {
      yield sync();
      if (focused_workspace != null) {
        print(focused_workspace.representation);
        print(focused_workspace.name);
        print(focused_workspace.id.to_string());
        print("\n");
      }
      switch (reply.type) {
        case PayloadType.MESSAGE_SUBSCRIBE:
          return;
        
        case PayloadType.EVENT_WORKSPACE:
          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 void handle_workspace_event(string args) {
      notify_property("workspaces");
      
      var obj = Json.from_string(args).get_object();

      switch (obj.get_string_member("change")) {
        case "init": 
          var name = obj.get_object_member("current").get_string_member("name");
          workspace_added(get_workspace(name));
          break;

        case "empty":
          var name = obj.get_object_member("current").get_string_member("name");
          workspace_removed(name);
          break;

        case "focus":
          var new_name = obj.get_object_member("current").get_string_member("name");
          var old_name = obj.get_object_member("old").get_string_member("name");
          workspace_focus(get_workspace(new_name), get_workspace(old_name));
        
        default:
          break;
      }
    }

    private async void handle_client_event(string args) {

    }
  }
}