9 #define UNUSED(x) (void)(x)
11 const char * AFBClient::wmURI = "ws://localhost:1700/api?token=wm";
12 const char * AFBClient::wmAPI = "winman";
14 AFBClient::AFBClient() : itf()
16 ///* itinializing the callback interface for wsj1 */
17 itf.on_hangup = AFBClient::onHangup;
18 itf.on_call = AFBClient::onCall;
19 itf.on_event = AFBClient::onEvent;
22 AFBClient::~AFBClient()
26 bool AFBClient::init()
28 printf("AFBClient::init() -->\n");
29 /* get the default event loop */
30 int rc = sd_event_default(&loop);
32 fprintf(stderr, "Connection to default event loop failed: %s\n", strerror(-rc));
36 /* connect the websocket wsj1 to the uri given by the first argument */
37 wsj1 = afb_ws_client_connect_wsj1(loop, wmURI, &itf, NULL);
39 fprintf(stderr, "Connection to %s failed: %m\n", wmURI);
43 printf("AFBClient::init() <--\n");
47 int AFBClient::dispatch(uint64_t timeout) {
48 return sd_event_run(loop, timeout);
51 int AFBClient::requestSurface(const char *label)
53 printf("AFBClient::requestSurface(%s) -->\n", label);
54 constexpr char const *verb = "request_surface";
57 json_object *jp = json_object_new_object();
58 json_object_object_add(jp, "drawing_name", json_object_new_string(label));
60 // std::experimental::optional look-alike
66 constexpr struct optional const nullopt = {0, true};
69 /* send the request */
70 int rc = afb_wsj1_call_j(
71 wsj1, AFBClient::wmAPI, verb, jp,
72 [](void *closure, afb_wsj1_msg *msg) {
73 if (afb_wsj1_msg_is_reply_ok(msg)) {
74 int id = json_object_get_int(
75 json_object_object_get(afb_wsj1_msg_object_j(msg), "response"));
76 auto oid = (optional *)closure;
79 fprintf(stderr, "wrong request surface reply received!\n");
84 fprintf(stderr, "calling %s/%s(%s) failed: %m\n", AFBClient::wmAPI, verb,
85 json_object_to_json_string(jp));
87 // Lets make this call sync here...
90 if (! id.is_not_set) {
92 asprintf(&buf, "%d", id.value);
93 printf("setenv(\"QT_IVI_SURFACE_ID\", %s, 1)\n", buf);
94 if (setenv("QT_IVI_SURFACE_ID", buf, 1) != 0) {
95 fprintf(stderr, "putenv failed: %m\n");
97 ret = 0; // Single point of success
100 fprintf(stderr, "Could not get surface ID from WM\n");
104 printf("AFBClient::requestSurface(%s) = %d <--\n", label, ret);
109 void AFBClient::activateSurface(const char *label)
111 printf("AFBClient::activateSurface(%s) -->\n", label);
114 const char begin[] = "{\"drawing_name\":\"";
115 const char end[] = "\"}";
116 const char verb[] = "activate_surface";
117 char *parameter = (char *)malloc(strlen(begin) +
120 strcpy(parameter, begin);
121 strcat(parameter, label);
122 strcat(parameter, end);
123 call(AFBClient::wmAPI, verb, parameter);
128 printf("AFBClient::activateSurface(%s) <--\n", label);
132 void AFBClient::deactivateSurface(const char *label)
134 printf("AFBClient::deactivateSurface(%s) -->\n", label);
136 json_object *j = json_object_new_object();
137 json_object_object_add(j, "drawing_name", json_object_new_string(label));
138 call(AFBClient::wmAPI, "deactivate_surface", json_object_to_json_string(j));
141 printf("AFBClient::deactivateSurface(%s) <--\n", label);
145 void AFBClient::endDraw(const char *label)
147 printf("AFBClient::endDraw(%s) -->\n", label);
149 json_object *j = json_object_new_object();
150 json_object_object_add(j, "drawing_name", json_object_new_string(label));
151 call(AFBClient::wmAPI, "enddraw", json_object_to_json_string(j));
154 printf("AFBClient::endDraw(%s) <--\n", label);
158 /* called when wsj1 receives a method invocation */
159 void AFBClient::onCall(void *closure, const char *api, const char *verb, struct afb_wsj1_msg *msg)
163 printf("ON-CALL %s/%s:\n%s\n", api, verb,
164 json_object_to_json_string_ext(afb_wsj1_msg_object_j(msg),
165 JSON_C_TO_STRING_PRETTY));
167 rc = afb_wsj1_reply_error_s(msg, "\"unimplemented\"", NULL);
169 fprintf(stderr, "replying failed: %m\n");
172 /* called when wsj1 receives an event */
173 void AFBClient::onEvent(void *closure, const char *event, afb_wsj1_msg *msg)
176 printf("ON-EVENT %s:\n%s\n", event,
177 json_object_to_json_string_ext(afb_wsj1_msg_object_j(msg),
178 JSON_C_TO_STRING_PRETTY));
182 /* called when wsj1 hangsup */
183 void AFBClient::onHangup(void *closure, afb_wsj1 *wsj1)
187 printf("ON-HANGUP\n");
192 /* called when wsj1 receives a reply */
193 void AFBClient::onReply(void *closure, afb_wsj1_msg *msg)
195 printf("ON-REPLY %s: %s\n%s\n", (char*)closure,
196 afb_wsj1_msg_is_reply_ok(msg) ? "OK" : "ERROR",
197 json_object_to_json_string_ext(afb_wsj1_msg_object_j(msg),
198 JSON_C_TO_STRING_PRETTY));
204 void AFBClient::call(const char *api, const char *verb, const char *object)
210 printf("call(%s, %s, %s) -->\n", api, verb, object);
213 /* allocates an id for the request */
214 rc = asprintf(&key, "%d:%s/%s", ++num, api, verb);
216 /* send the request */
217 rc = afb_wsj1_call_s(wsj1, api, verb, object, AFBClient::onReply, key);
219 fprintf(stderr, "calling %s/%s(%s) failed: %m\n", api, verb, object);
221 printf("call(%s, %s, %s) <--\n", api, verb, object);
226 void AFBClient::event(const char *event, const char *object)
230 rc = afb_wsj1_send_event_s(wsj1, event, object);
232 fprintf(stderr, "sending !%s(%s) failed: %m\n", event, object);