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,
36 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) {
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));
59 /* called when wsj1 hangsup */
60 void onHangup(void *closure, afb_wsj1 *wsj1) {
64 printf("ON-HANGUP\n");
69 /* called when wsj1 receives a reply */
70 void onReply(void *closure, afb_wsj1_msg *msg) {
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));
82 AFBClient &AFBClient::instance() {
88 AFBClient::AFBClient() : wsj1{}, itf{}, loop{} {
90 ///* itinializing the callback interface for wsj1 */
91 itf.on_hangup = onHangup;
93 itf.on_event = onEvent;
96 AFBClient::~AFBClient() {
102 int AFBClient::init(int port, char const *token) {
104 char *uribuf = nullptr;
107 if (!token || strlen(token) > token_maxlen) {
108 fprintf(stderr, "Token is invalid\n");
113 for (char const *p = token; *p; p++) {
115 fprintf(stderr, "Token is invalid\n");
121 if (port < 1 && port > 0xffff) {
122 fprintf(stderr, "Port is invalid\n");
127 /* get the default event loop */
128 rc = sd_event_default(&loop);
130 fprintf(stderr, "Connection to default event loop failed: %s\n",
135 asprintf(&uribuf, "ws://localhost:%d/api?token=%s", port, token);
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);
152 int AFBClient::dispatch(uint64_t timeout) {
154 return sd_event_run(loop, timeout);
157 int AFBClient::requestSurface(const char *label) {
159 constexpr char const *verb = "request_surface";
161 json_object *jp = json_object_new_object();
162 json_object_object_add(jp, "drawing_name", json_object_new_string(label));
164 // std::experimental::optional look-alike
170 constexpr struct optional const nullopt = {0, true};
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;
183 fprintf(stderr, "wrong request surface reply received!\n");
188 fprintf(stderr, "calling %s/%s(%s) failed: %m\n", wmAPI, verb,
189 json_object_to_json_string(jp));
191 // Lets make this call sync here...
194 if (!id.is_not_set) {
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");
201 rc = 0; // Single point of success
204 fprintf(stderr, "Could not get surface ID from WM\n");
212 int AFBClient::activateSurface(const char *label) {
215 const char begin[] = "{\"drawing_name\":\"";
216 const char end[] = "\"}";
217 const char verb[] = "activate_surface";
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);
231 int AFBClient::deactivateSurface(const char *label) {
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));
241 int AFBClient::endDraw(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, "enddraw", json_object_to_json_string(j));
252 void AFBClient::call(const char *api, const char *verb, const char *object) {
260 /* allocates an id for the request */
261 rc = asprintf(&key, "%d:%s/%s", ++num, api, verb);
263 /* send the request */
264 rc = afb_wsj1_call_s(wsj1, api, verb, object, onReply, key);
266 fprintf(stderr, "calling %s/%s(%s) failed: %m\n", api, verb, object);
271 void AFBClient::set_event_handler(enum EventType at,
272 std::function<void(char const *)> func) {