9 #define UNUSED(x) (void)(x)
12 extern struct afb_wsj1 *afb_ws_client_connect_wsj1(struct sd_event *eloop, const char *uri, struct afb_wsj1_itf *itf, void *closure);
13 extern int afb_wsj1_call_s(struct afb_wsj1 *wsj1, const char *api, const char *verb, const char *object, void (*on_reply)(void *closure, struct afb_wsj1_msg *msg), void *closure);
14 extern int afb_wsj1_msg_is_reply_ok(struct afb_wsj1_msg *msg);
15 extern int afb_wsj1_send_event_s(struct afb_wsj1 *wsj1, const char *event, const char *object);
16 static inline int afb_wsj1_reply_error_s(struct afb_wsj1_msg *msg, const char *object, const char *token);
19 const char * AFBClient::wmURI = "ws://localhost:1700/api?token=wm";
20 const char * AFBClient::wmAPI = "winman";
22 AFBClient::AFBClient() : itf()
24 ///* itinializing the callback interface for wsj1 */
25 itf.on_hangup = AFBClient::onHangup;
26 itf.on_call = AFBClient::onCall;
27 itf.on_event = AFBClient::onEvent;
30 AFBClient::~AFBClient()
34 bool AFBClient::init()
36 printf("init() -->\n");
37 /* get the default event loop */
38 int rc = sd_event_default(&loop);
40 fprintf(stderr, "Connection to default event loop failed: %s\n", strerror(-rc));
44 /* connect the websocket wsj1 to the uri given by the first argument */
45 wsj1 = afb_ws_client_connect_wsj1(loop, wmURI, &itf, NULL);
47 fprintf(stderr, "Connection to %s failed: %m\n", wmURI);
51 printf("init() <--\n");
55 int AFBClient::requestSurface(const char *label)
57 printf("requestSurface(%s) -->\n", label);
58 constexpr char const *verb = "request_surface";
61 json_object *jp = json_object_new_object();
62 json_object_object_add(jp, "drawing_name", json_object_new_string(label));
64 // std::experimental::optional look-alike
70 constexpr struct optional nullopt = {0, true};
73 /* send the request */
74 int rc = afb_wsj1_call_j(
75 wsj1, AFBClient::wmAPI, verb, jp,
76 [](void *closure, afb_wsj1_msg *msg) {
77 if (afb_wsj1_msg_is_reply_ok(msg)) {
78 int id = json_object_get_int(
79 json_object_object_get(afb_wsj1_msg_object_j(msg), "response"));
80 auto oid = (optional *)closure;
83 fprintf(stderr, "wrong request surface reply received!\n");
88 fprintf(stderr, "calling %s/%s(%s) failed: %m\n", AFBClient::wmAPI, verb,
89 json_object_to_json_string(jp));
91 // Lets make this call sync here...
94 if (! id.is_not_set) {
96 asprintf(&buf, "%d", id.value);
97 printf("setenv(\"QT_IVI_SURFACE_ID\", %s, 1)\n", buf);
98 if (setenv("QT_IVI_SURFACE_ID", buf, 1) != 0) {
99 fprintf(stderr, "putenv failed: %m\n");
101 ret = 0; // Single point of success
104 fprintf(stderr, "Could not get surface ID from WM\n");
108 printf("requestSurface(%s) = %d <--\n", label, ret);
113 void AFBClient::activateSurface(const char *label)
115 printf("activateSurface(%s) -->\n", label);
118 const char begin[] = "{\"drawing_name\":\"";
119 const char end[] = "\"}";
120 const char verb[] = "activate_surface";
121 char *parameter = (char *)malloc(strlen(begin) +
124 strcpy(parameter, begin);
125 strcat(parameter, label);
126 strcat(parameter, end);
127 call(AFBClient::wmAPI, verb, parameter);
132 printf("activateSurface(%s) <--\n", label);
136 int AFBClient::dispatch(uint64_t timeout) {
137 return sd_event_run(loop, timeout);
140 void AFBClient::deactivateSurface(const char *label)
145 void AFBClient::endDraw(const char *label)
150 /* called when wsj1 receives a method invocation */
151 void AFBClient::onCall(void *closure, const char *api, const char *verb, struct afb_wsj1_msg *msg)
155 printf("ON-CALL %s/%s:\n%s\n", api, verb,
156 json_object_to_json_string_ext(afb_wsj1_msg_object_j(msg),
157 JSON_C_TO_STRING_PRETTY));
159 rc = afb_wsj1_reply_error_s(msg, "\"unimplemented\"", NULL);
161 fprintf(stderr, "replying failed: %m\n");
164 /* called when wsj1 receives an event */
165 void AFBClient::onEvent(void *closure, const char *event, afb_wsj1_msg *msg)
168 printf("ON-EVENT %s:\n%s\n", event,
169 json_object_to_json_string_ext(afb_wsj1_msg_object_j(msg),
170 JSON_C_TO_STRING_PRETTY));
174 /* called when wsj1 hangsup */
175 void AFBClient::onHangup(void *closure, afb_wsj1 *wsj1)
179 printf("ON-HANGUP\n");
184 /* called when wsj1 receives a reply */
185 void AFBClient::onReply(void *closure, afb_wsj1_msg *msg)
187 printf("ON-REPLY %s: %s\n%s\n", (char*)closure,
188 afb_wsj1_msg_is_reply_ok(msg) ? "OK" : "ERROR",
189 json_object_to_json_string_ext(afb_wsj1_msg_object_j(msg),
190 JSON_C_TO_STRING_PRETTY));
196 void AFBClient::call(const char *api, const char *verb, const char *object)
202 printf("call(%s, %s, %s) -->\n", api, verb, object);
205 /* allocates an id for the request */
206 rc = asprintf(&key, "%d:%s/%s", ++num, api, verb);
208 /* send the request */
209 rc = afb_wsj1_call_s(wsj1, api, verb, object, AFBClient::onReply, key);
211 fprintf(stderr, "calling %s/%s(%s) failed: %m\n", api, verb, object);
213 printf("call(%s, %s, %s) <--\n", api, verb, object);
218 void AFBClient::event(const char *event, const char *object)
222 rc = afb_wsj1_send_event_s(wsj1, event, object);
224 fprintf(stderr, "sending !%s(%s) failed: %m\n", event, object);