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));
45 /* connect the websocket wsj1 to the uri given by the first argument */
46 wsj1 = afb_ws_client_connect_wsj1(loop, wmURI, &itf, NULL);
48 fprintf(stderr, "Connection to %s failed: %m\n", wmURI);
53 printf("init() <--\n");
57 void AFBClient::requestSurface(const char *label)
59 printf("requestSurface(%s) -->\n", label);
65 const char begin[] = "{\"drawing_name\":\"";
66 const char end[] = "\"}";
67 const char verb[] = "request_surface";
68 char *parameter = (char *)malloc(strlen(begin) +
71 strcpy(parameter, begin);
72 strcat(parameter, label);
73 strcat(parameter, end);
75 /* allocates an id for the request */
76 rc = asprintf(&key, "%d:%s/%s", ++num, AFBClient::wmAPI, verb);
78 /* send the request */
79 rc = afb_wsj1_call_s(wsj1, AFBClient::wmAPI, verb, parameter, AFBClient::onRequestSurfaceReply, key);
81 fprintf(stderr, "calling %s/%s(%s) failed: %m\n", AFBClient::wmAPI, verb, parameter);
83 printf("requestSurface(%s) <--\n", label);
87 void AFBClient::activateSurface(const char *label)
89 printf("activateSurface(%s) -->\n", label);
92 const char begin[] = "{\"drawing_name\":\"";
93 const char end[] = "\"}";
94 const char verb[] = "activate_surface";
95 char *parameter = (char *)malloc(strlen(begin) +
98 strcpy(parameter, begin);
99 strcat(parameter, label);
100 strcat(parameter, end);
101 call(AFBClient::wmAPI, verb, parameter);
103 printf("activateSurface(%s) <--\n", label);
107 int AFBClient::dispatch() {
108 return sd_event_run(loop, -1);
111 void AFBClient::deactivateSurface(const char *label)
116 void AFBClient::endDraw(const char *label)
121 /* called when wsj1 receives a method invocation */
122 void AFBClient::onCall(void *closure, const char *api, const char *verb, struct afb_wsj1_msg *msg)
127 printf("ON-CALL %s/%s:\n%s\n", api, verb,
128 json_object_to_json_string_ext(afb_wsj1_msg_object_j(msg),
129 JSON_C_TO_STRING_PRETTY));
131 rc = afb_wsj1_reply_error_s(msg, "\"unimplemented\"", NULL);
133 fprintf(stderr, "replying failed: %m\n");
137 /* called when wsj1 receives an event */
138 void AFBClient::onEvent(void *closure, const char *event, afb_wsj1_msg *msg)
142 printf("ON-EVENT %s:\n%s\n", event,
143 json_object_to_json_string_ext(afb_wsj1_msg_object_j(msg),
144 JSON_C_TO_STRING_PRETTY));
149 /* called when wsj1 hangsup */
150 void AFBClient::onHangup(void *closure, afb_wsj1 *wsj1)
154 printf("ON-HANGUP\n");
159 /* called when wsj1 receives a reply */
160 void AFBClient::onReply(void *closure, afb_wsj1_msg *msg)
163 printf("ON-REPLY %s: %s\n%s\n", (char*)closure,
164 afb_wsj1_msg_is_reply_ok(msg) ? "OK" : "ERROR",
165 json_object_to_json_string_ext(afb_wsj1_msg_object_j(msg),
166 JSON_C_TO_STRING_PRETTY));
172 void AFBClient::onRequestSurfaceReply(void *closure, afb_wsj1_msg *msg)
175 printf("onRequestSurfaceReply %s: %s\n%s\n", (char*)closure,
176 afb_wsj1_msg_is_reply_ok(msg) ? "OK" : "ERROR",
177 json_object_to_json_string_ext(afb_wsj1_msg_object_j(msg),
178 JSON_C_TO_STRING_PRETTY));
179 printf("\n\n===>RETURN STR: %s\n\n", afb_wsj1_msg_object_s(msg));
180 // putenv("QT_IVI_SURFACE_ID=16778219");
187 void AFBClient::call(const char *api, const char *verb, const char *object)
194 printf("call(%s, %s, %s) -->\n", api, verb, object);
197 /* allocates an id for the request */
198 rc = asprintf(&key, "%d:%s/%s", ++num, api, verb);
200 /* send the request */
201 rc = afb_wsj1_call_s(wsj1, api, verb, object, AFBClient::onReply, key);
203 fprintf(stderr, "calling %s/%s(%s) failed: %m\n", api, verb, object);
205 printf("call(%s, %s, %s) <--\n", api, verb, object);
211 void AFBClient::event(const char *event, const char *object)
216 rc = afb_wsj1_send_event_s(wsj1, event, object);
218 fprintf(stderr, "sending !%s(%s) failed: %m\n", event, object);