10 #define UNUSED(x) (void)(x)
12 constexpr const int token_maxlen = 20;
13 const char * AFBClient::wmAPI = "winman";
15 AFBClient::AFBClient() : itf()
17 ///* itinializing the callback interface for wsj1 */
18 itf.on_hangup = AFBClient::onHangup;
19 itf.on_call = AFBClient::onCall;
20 itf.on_event = AFBClient::onEvent;
23 AFBClient::~AFBClient()
27 int AFBClient::init(int port, char const *token)
32 printf("AFBClient::init() -->\n");
34 if (!token || strlen(token) > token_maxlen) {
35 fprintf(stderr, "Token is invalid\n");
40 for (char const *p = token; *p; p++) {
42 fprintf(stderr, "Token is invalid\n");
48 if (port < 1 && port > 0xffff) {
49 fprintf(stderr, "Port is invalid\n");
54 /* get the default event loop */
55 rc = sd_event_default(&loop);
57 fprintf(stderr, "Connection to default event loop failed: %s\n", strerror(-rc));
61 asprintf(&uribuf, "ws://localhost:%d/api?token=%s", port, token);
63 /* connect the websocket wsj1 to the uri given by the first argument */
64 wsj1 = afb_ws_client_connect_wsj1(loop, uribuf, &itf, NULL);
67 fprintf(stderr, "Connection to %s failed: %m\n", uribuf);
72 printf("AFBClient::init() <--\n");
76 printf("AFBClient::init() <--\n");
80 int AFBClient::dispatch(uint64_t timeout) {
81 return sd_event_run(loop, timeout);
84 int AFBClient::requestSurface(const char *label)
86 printf("AFBClient::requestSurface(%s) -->\n", label);
87 constexpr char const *verb = "request_surface";
89 json_object *jp = json_object_new_object();
90 json_object_object_add(jp, "drawing_name", json_object_new_string(label));
92 // std::experimental::optional look-alike
98 constexpr struct optional const nullopt = {0, true};
101 /* send the request */
102 int rc = afb_wsj1_call_j(
103 wsj1, AFBClient::wmAPI, verb, jp,
104 [](void *closure, afb_wsj1_msg *msg) {
105 if (afb_wsj1_msg_is_reply_ok(msg)) {
106 int id = json_object_get_int(
107 json_object_object_get(afb_wsj1_msg_object_j(msg), "response"));
108 auto oid = (optional *)closure;
111 fprintf(stderr, "wrong request surface reply received!\n");
116 fprintf(stderr, "calling %s/%s(%s) failed: %m\n", AFBClient::wmAPI, verb,
117 json_object_to_json_string(jp));
119 // Lets make this call sync here...
122 if (! id.is_not_set) {
124 asprintf(&buf, "%d", id.value);
125 printf("setenv(\"QT_IVI_SURFACE_ID\", %s, 1)\n", buf);
126 if (setenv("QT_IVI_SURFACE_ID", buf, 1) != 0) {
127 fprintf(stderr, "putenv failed: %m\n");
129 rc = 0; // Single point of success
132 fprintf(stderr, "Could not get surface ID from WM\n");
137 printf("AFBClient::requestSurface(%s) = %d <--\n", label, rc);
142 int AFBClient::activateSurface(const char *label)
144 printf("AFBClient::activateSurface(%s) -->\n", label);
147 const char begin[] = "{\"drawing_name\":\"";
148 const char end[] = "\"}";
149 const char verb[] = "activate_surface";
150 char *parameter = (char *)malloc(strlen(begin) +
153 strcpy(parameter, begin);
154 strcat(parameter, label);
155 strcat(parameter, end);
156 call(AFBClient::wmAPI, verb, parameter);
161 printf("AFBClient::activateSurface(%s) <--\n", label);
166 int AFBClient::deactivateSurface(const char *label)
168 printf("AFBClient::deactivateSurface(%s) -->\n", label);
170 json_object *j = json_object_new_object();
171 json_object_object_add(j, "drawing_name", json_object_new_string(label));
172 call(AFBClient::wmAPI, "deactivate_surface", json_object_to_json_string(j));
175 printf("AFBClient::deactivateSurface(%s) <--\n", label);
180 int AFBClient::endDraw(const char *label)
182 printf("AFBClient::endDraw(%s) -->\n", label);
184 json_object *j = json_object_new_object();
185 json_object_object_add(j, "drawing_name", json_object_new_string(label));
186 call(AFBClient::wmAPI, "enddraw", json_object_to_json_string(j));
189 printf("AFBClient::endDraw(%s) <--\n", label);
194 /* called when wsj1 receives a method invocation */
195 void AFBClient::onCall(void *closure, const char *api, const char *verb, struct afb_wsj1_msg *msg)
199 printf("ON-CALL %s/%s:\n%s\n", api, verb,
200 json_object_to_json_string_ext(afb_wsj1_msg_object_j(msg),
201 JSON_C_TO_STRING_PRETTY));
203 rc = afb_wsj1_reply_error_s(msg, "\"unimplemented\"", NULL);
205 fprintf(stderr, "replying failed: %m\n");
208 /* called when wsj1 receives an event */
209 void AFBClient::onEvent(void *closure, const char *event, afb_wsj1_msg *msg)
212 printf("ON-EVENT %s:\n%s\n", event,
213 json_object_to_json_string_ext(afb_wsj1_msg_object_j(msg),
214 JSON_C_TO_STRING_PRETTY));
218 /* called when wsj1 hangsup */
219 void AFBClient::onHangup(void *closure, afb_wsj1 *wsj1)
223 printf("ON-HANGUP\n");
228 /* called when wsj1 receives a reply */
229 void AFBClient::onReply(void *closure, afb_wsj1_msg *msg)
231 printf("ON-REPLY %s: %s\n%s\n", (char*)closure,
232 afb_wsj1_msg_is_reply_ok(msg) ? "OK" : "ERROR",
233 json_object_to_json_string_ext(afb_wsj1_msg_object_j(msg),
234 JSON_C_TO_STRING_PRETTY));
240 void AFBClient::call(const char *api, const char *verb, const char *object)
246 printf("call(%s, %s, %s) -->\n", api, verb, object);
249 /* allocates an id for the request */
250 rc = asprintf(&key, "%d:%s/%s", ++num, api, verb);
252 /* send the request */
253 rc = afb_wsj1_call_s(wsj1, api, verb, object, AFBClient::onReply, key);
255 fprintf(stderr, "calling %s/%s(%s) failed: %m\n", api, verb, object);
257 printf("call(%s, %s, %s) <--\n", api, verb, object);
262 void AFBClient::event(const char *event, const char *object)
266 rc = afb_wsj1_send_event_s(wsj1, event, object);
268 fprintf(stderr, "sending !%s(%s) failed: %m\n", event, object);
271 void AFBClient::set_event_handler(enum EventType at, std::function<void(char const *)> func) {