328445c7bf709b505bffd9c095cb253d713ed586
[staging/windowmanager.git] / AFBClient.cpp
1 #include "AFBClient.h"
2
3 #include <cctype>
4 #include <stdlib.h>
5 #include <stdio.h>
6 #include <errno.h>
7 #include <string.h>
8 #include <unistd.h>
9
10 #define UNUSED(x) (void)(x)
11
12 namespace {
13
14 constexpr const int token_maxlen = 20;
15 constexpr const char *const wmAPI = "winman";
16
17 struct ScopeTrace {
18    static int indent;
19    char const *f{};
20    ScopeTrace(char const *func) : f(func) {
21       fprintf(stderr, "%*s%s -->\n", 2 * indent++, "", this->f);
22    }
23    ~ScopeTrace() {
24       fprintf(stderr, "%*s%s <--\n", 2 * --indent, "", this->f);
25    }
26 };
27 int ScopeTrace::indent = 0;
28
29 #ifndef NDEBUG
30 #define TRACE()
31 #else
32 #define TRACE() ScopeTrace __attribute__((unused)) trace_scope_here__(__PRETTY_FUNCTION__)
33 #endif
34
35 /* called when wsj1 receives a method invocation */
36 void onCall(void *closure, const char *api, const char *verb, struct afb_wsj1_msg *msg)
37 {
38    TRACE();
39     UNUSED(closure);
40     int rc;
41     printf("ON-CALL %s/%s:\n%s\n", api, verb,
42            json_object_to_json_string_ext(afb_wsj1_msg_object_j(msg),
43                                           JSON_C_TO_STRING_PRETTY));
44     fflush(stdout);
45     rc = afb_wsj1_reply_error_s(msg, "\"unimplemented\"", nullptr);
46     if (rc < 0)
47         fprintf(stderr, "replying failed: %m\n");
48 }
49
50 /* called when wsj1 receives an event */
51 void onEvent(void *closure, const char *event, afb_wsj1_msg *msg)
52 {
53    TRACE();
54     UNUSED(closure);
55     printf("ON-EVENT %s:\n%s\n", event,
56            json_object_to_json_string_ext(afb_wsj1_msg_object_j(msg),
57                                           JSON_C_TO_STRING_PRETTY));
58     fflush(stdout);
59 }
60
61 /* called when wsj1 hangsup */
62 void onHangup(void *closure, afb_wsj1 *wsj1)
63 {
64    TRACE();
65     UNUSED(closure);
66     UNUSED(wsj1);
67     printf("ON-HANGUP\n");
68     fflush(stdout);
69     exit(0);
70 }
71
72 /* called when wsj1 receives a reply */
73 void onReply(void *closure, afb_wsj1_msg *msg)
74 {
75    TRACE();
76     printf("ON-REPLY %s: %s\n%s\n", (char*)closure,
77            afb_wsj1_msg_is_reply_ok(msg) ? "OK" : "ERROR",
78            json_object_to_json_string_ext(afb_wsj1_msg_object_j(msg),
79                                           JSON_C_TO_STRING_PRETTY));
80     fflush(stdout);
81     free(closure);
82 }
83
84 }  // namespace
85
86 AFBClient &AFBClient::instance()
87 {
88    TRACE();
89    static AFBClient obj;
90    return obj;
91 }
92
93 AFBClient::AFBClient() : wsj1{}, itf{}, loop{}
94 {
95    TRACE();
96     ///* itinializing the callback interface for wsj1 */
97     itf.on_hangup = onHangup;
98     itf.on_call = onCall;
99     itf.on_event = onEvent;
100 }
101
102 AFBClient::~AFBClient()
103 {
104    TRACE();
105    sd_event_unref(loop);
106    loop = nullptr;
107 }
108
109 int AFBClient::init(int port, char const *token)
110 {
111    TRACE();
112     char *uribuf = nullptr;
113     int rc = -1;
114
115     if (!token || strlen(token) > token_maxlen) {
116        fprintf(stderr, "Token is invalid\n");
117        rc = -EINVAL;
118        goto fail;
119     }
120
121     for (char const *p = token; *p; p++) {
122        if (!isalnum(*p)) {
123           fprintf(stderr, "Token is invalid\n");
124           rc = -EINVAL;
125           goto fail;
126        }
127     }
128
129     if (port < 1 && port > 0xffff) {
130        fprintf(stderr, "Port is invalid\n");
131        rc = -EINVAL;
132        goto fail;
133     }
134
135     /* get the default event loop */
136     rc = sd_event_default(&loop);
137     if (rc < 0) {
138         fprintf(stderr, "Connection to default event loop failed: %s\n", strerror(-rc));
139         goto fail;
140     }
141
142     asprintf(&uribuf, "ws://localhost:%d/api?token=%s", port, token);
143
144     /* connect the websocket wsj1 to the uri given by the first argument */
145     wsj1 = afb_ws_client_connect_wsj1(loop, uribuf, &itf, nullptr);
146     if (wsj1 == nullptr) {
147         sd_event_unref(loop);
148         fprintf(stderr, "Connection to %s failed: %m\n", uribuf);
149         rc = -errno;
150         goto fail;
151     }
152
153     return 0;
154
155 fail:
156     return rc;
157 }
158
159 int AFBClient::dispatch(uint64_t timeout) {
160    TRACE();
161     return sd_event_run(loop, timeout);
162 }
163
164 int AFBClient::requestSurface(const char *label)
165 {
166    TRACE();
167    constexpr char const *verb = "request_surface";
168
169    json_object *jp = json_object_new_object();
170    json_object_object_add(jp, "drawing_name", json_object_new_string(label));
171
172    // std::experimental::optional look-alike
173    struct optional {
174       int value;
175       bool is_not_set;
176    };
177
178    constexpr struct optional const nullopt = {0, true};
179    auto id = nullopt;
180
181    /* send the request */
182    int rc = afb_wsj1_call_j(
183       wsj1, wmAPI, verb, jp,
184       [](void *closure, afb_wsj1_msg *msg) {
185          if (afb_wsj1_msg_is_reply_ok(msg)) {
186             int id = json_object_get_int(
187                json_object_object_get(afb_wsj1_msg_object_j(msg), "response"));
188             auto oid = (optional *)closure;
189             *oid = optional{id};
190          } else
191             fprintf(stderr, "wrong request surface reply received!\n");
192       },
193       (void *)&id);
194
195    if (rc < 0) {
196       fprintf(stderr, "calling %s/%s(%s) failed: %m\n", wmAPI, verb,
197               json_object_to_json_string(jp));
198    } else {
199       // Lets make this call sync here...
200       dispatch(-1);
201
202       if (! id.is_not_set) {
203           char *buf;
204           asprintf(&buf, "%d", id.value);
205           printf("setenv(\"QT_IVI_SURFACE_ID\", %s, 1)\n", buf);
206           if (setenv("QT_IVI_SURFACE_ID", buf, 1) != 0) {
207               fprintf(stderr, "putenv failed: %m\n");
208           } else {
209               rc = 0; // Single point of success
210           }
211       } else {
212           fprintf(stderr, "Could not get surface ID from WM\n");
213           rc = -EINVAL;
214       }
215    }
216
217    return rc;
218 }
219
220 int AFBClient::activateSurface(const char *label)
221 {
222    TRACE();
223
224     const char begin[] = "{\"drawing_name\":\"";
225     const char end[] = "\"}";
226     const char verb[] = "activate_surface";
227     char *parameter = (char *)malloc(strlen(begin) +
228                                      strlen(label) +
229                                      strlen(end) + 1);
230     strcpy(parameter, begin);
231     strcat(parameter, label);
232     strcat(parameter, end);
233     call(wmAPI, verb, parameter);
234
235     // Sync this one too
236     dispatch(-1);
237
238     return 0;
239 }
240
241 int AFBClient::deactivateSurface(const char *label)
242 {
243    TRACE();
244     json_object *j = json_object_new_object();
245     json_object_object_add(j, "drawing_name", json_object_new_string(label));
246     call(wmAPI, "deactivate_surface", json_object_to_json_string(j));
247     json_object_put(j);
248     dispatch(-1);
249     return 0;
250 }
251
252 int AFBClient::endDraw(const char *label)
253 {
254    TRACE();
255     json_object *j = json_object_new_object();
256     json_object_object_add(j, "drawing_name", json_object_new_string(label));
257     call(wmAPI, "enddraw", json_object_to_json_string(j));
258     json_object_put(j);
259     dispatch(-1);
260     return 0;
261 }
262
263 /* makes a call */
264 void AFBClient::call(const char *api, const char *verb, const char *object)
265 {
266    TRACE();
267     static int num = 0;
268     char *key;
269     int rc;
270
271     fflush(stdout);
272
273     /* allocates an id for the request */
274     rc = asprintf(&key, "%d:%s/%s", ++num, api, verb);
275
276     /* send the request */
277     rc = afb_wsj1_call_s(wsj1, api, verb, object, onReply, key);
278     if (rc < 0)
279         fprintf(stderr, "calling %s/%s(%s) failed: %m\n", api, verb, object);
280
281     fflush(stdout);
282 }
283
284 void AFBClient::set_event_handler(enum EventType at, std::function<void(char const *)> func) {
285    TRACE();
286    // XXX todo
287 }