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