10 #define UNUSED(x) (void)(x)
14 constexpr const int token_maxlen = 20;
15 constexpr const char *const wmAPI = "winman";
21 ScopeTrace __attribute__((unused)) trace_scope_here__(__PRETTY_FUNCTION__)
26 ScopeTrace(char const *func) : f(func) {
27 fprintf(stderr, "%*s%s -->\n", 2 * indent++, "", this->f);
29 ~ScopeTrace() { fprintf(stderr, "%*s%s <--\n", 2 * --indent, "", this->f); }
31 int ScopeTrace::indent = 0;
34 /* called when wsj1 receives a method invocation */
35 void onCall(void *closure, const char *api, const char *verb, struct afb_wsj1_msg *msg)
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));
44 rc = afb_wsj1_reply_error_s(msg, "\"unimplemented\"", nullptr);
46 fprintf(stderr, "replying failed: %m\n");
49 /* called when wsj1 receives an event */
50 void onEvent(void *closure, const char *event, afb_wsj1_msg *msg)
54 printf("ON-EVENT %s:\n%s\n", event,
55 json_object_to_json_string_ext(afb_wsj1_msg_object_j(msg),
56 JSON_C_TO_STRING_PRETTY));
60 /* called when wsj1 hangsup */
61 void onHangup(void *closure, afb_wsj1 *wsj1)
66 printf("ON-HANGUP\n");
71 /* called when wsj1 receives a reply */
72 void onReply(void *closure, afb_wsj1_msg *msg)
75 printf("ON-REPLY %s: %s\n%s\n", (char*)closure,
76 afb_wsj1_msg_is_reply_ok(msg) ? "OK" : "ERROR",
77 json_object_to_json_string_ext(afb_wsj1_msg_object_j(msg),
78 JSON_C_TO_STRING_PRETTY));
85 AFBClient &AFBClient::instance()
92 AFBClient::AFBClient() : wsj1{}, itf{}, loop{}
95 ///* itinializing the callback interface for wsj1 */
96 itf.on_hangup = onHangup;
98 itf.on_event = onEvent;
101 AFBClient::~AFBClient()
104 sd_event_unref(loop);
108 int AFBClient::init(int port, char const *token)
111 char *uribuf = nullptr;
114 if (!token || strlen(token) > token_maxlen) {
115 fprintf(stderr, "Token is invalid\n");
120 for (char const *p = token; *p; p++) {
122 fprintf(stderr, "Token is invalid\n");
128 if (port < 1 && port > 0xffff) {
129 fprintf(stderr, "Port is invalid\n");
134 /* get the default event loop */
135 rc = sd_event_default(&loop);
137 fprintf(stderr, "Connection to default event loop failed: %s\n", strerror(-rc));
141 asprintf(&uribuf, "ws://localhost:%d/api?token=%s", port, token);
143 /* connect the websocket wsj1 to the uri given by the first argument */
144 wsj1 = afb_ws_client_connect_wsj1(loop, uribuf, &itf, nullptr);
145 if (wsj1 == nullptr) {
146 sd_event_unref(loop);
147 fprintf(stderr, "Connection to %s failed: %m\n", uribuf);
158 int AFBClient::dispatch(uint64_t timeout) {
160 return sd_event_run(loop, timeout);
163 int AFBClient::requestSurface(const char *label)
166 constexpr char const *verb = "request_surface";
168 json_object *jp = json_object_new_object();
169 json_object_object_add(jp, "drawing_name", json_object_new_string(label));
171 // std::experimental::optional look-alike
177 constexpr struct optional const nullopt = {0, true};
180 /* send the request */
181 int rc = afb_wsj1_call_j(
182 wsj1, wmAPI, verb, jp,
183 [](void *closure, afb_wsj1_msg *msg) {
184 if (afb_wsj1_msg_is_reply_ok(msg)) {
185 int id = json_object_get_int(
186 json_object_object_get(afb_wsj1_msg_object_j(msg), "response"));
187 auto oid = (optional *)closure;
190 fprintf(stderr, "wrong request surface reply received!\n");
195 fprintf(stderr, "calling %s/%s(%s) failed: %m\n", wmAPI, verb,
196 json_object_to_json_string(jp));
198 // Lets make this call sync here...
201 if (! id.is_not_set) {
203 asprintf(&buf, "%d", id.value);
204 printf("setenv(\"QT_IVI_SURFACE_ID\", %s, 1)\n", buf);
205 if (setenv("QT_IVI_SURFACE_ID", buf, 1) != 0) {
206 fprintf(stderr, "putenv failed: %m\n");
208 rc = 0; // Single point of success
211 fprintf(stderr, "Could not get surface ID from WM\n");
219 int AFBClient::activateSurface(const char *label)
223 const char begin[] = "{\"drawing_name\":\"";
224 const char end[] = "\"}";
225 const char verb[] = "activate_surface";
226 char *parameter = (char *)malloc(strlen(begin) +
229 strcpy(parameter, begin);
230 strcat(parameter, label);
231 strcat(parameter, end);
232 call(wmAPI, verb, parameter);
240 int AFBClient::deactivateSurface(const char *label)
243 json_object *j = json_object_new_object();
244 json_object_object_add(j, "drawing_name", json_object_new_string(label));
245 call(wmAPI, "deactivate_surface", json_object_to_json_string(j));
251 int AFBClient::endDraw(const char *label)
254 json_object *j = json_object_new_object();
255 json_object_object_add(j, "drawing_name", json_object_new_string(label));
256 call(wmAPI, "enddraw", json_object_to_json_string(j));
263 void AFBClient::call(const char *api, const char *verb, const char *object)
272 /* allocates an id for the request */
273 rc = asprintf(&key, "%d:%s/%s", ++num, api, verb);
275 /* send the request */
276 rc = afb_wsj1_call_s(wsj1, api, verb, object, onReply, key);
278 fprintf(stderr, "calling %s/%s(%s) failed: %m\n", api, verb, object);
283 void AFBClient::set_event_handler(enum EventType at, std::function<void(char const *)> func) {