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("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));
37 /* connect the websocket wsj1 to the uri given by the first argument */
38 wsj1 = afb_ws_client_connect_wsj1(loop, wmURI, &itf, NULL);
40 fprintf(stderr, "Connection to %s failed: %m\n", wmURI);
45 printf("init() <--\n");
49 void AFBClient::requestSurface(const char *label)
51 printf("requestSurface(%s) -->\n", label);
57 const char begin[] = "{\"drawing_name\":\"";
58 const char end[] = "\"}";
59 const char verb[] = "request_surface";
60 char *parameter = (char *)malloc(strlen(begin) +
63 strcpy(parameter, begin);
64 strcat(parameter, label);
65 strcat(parameter, end);
67 /* allocates an id for the request */
68 rc = asprintf(&key, "%d:%s/%s", ++num, AFBClient::wmAPI, verb);
70 /* send the request */
71 rc = afb_wsj1_call_s(wsj1, AFBClient::wmAPI, verb, parameter, AFBClient::onRequestSurfaceReply, key);
73 fprintf(stderr, "calling %s/%s(%s) failed: %m\n", AFBClient::wmAPI, verb, parameter);
75 printf("requestSurface(%s) <--\n", label);
79 void AFBClient::activateSurface(const char *label)
81 printf("activateSurface(%s) -->\n", label);
84 const char begin[] = "{\"drawing_name\":\"";
85 const char end[] = "\"}";
86 const char verb[] = "activate_surface";
87 strcpy(parameter, functionParamName);
88 strcat(parameter, label);
89 strcat(parameter, "\"}");
90 char *parameter = (char *)malloc(strlen(begin) +
93 strcpy(parameter, begin);
94 strcat(parameter, label);
95 strcat(parameter, end);
96 call(AFBClient::wmAPI, verb, parameter);
98 printf("activateSurface(%s) <--\n", label);
102 void AFBClient::deactivateSurface(const char *label)
107 void AFBClient::endDraw(const char *label)
112 /* called when wsj1 receives a method invocation */
113 void AFBClient::onCall(void *closure, const char *api, const char *verb, struct afb_wsj1_msg *msg)
118 printf("ON-CALL %s/%s:\n%s\n", api, verb,
119 json_object_to_json_string_ext(afb_wsj1_msg_object_j(msg),
120 JSON_C_TO_STRING_PRETTY));
122 rc = afb_wsj1_reply_error_s(msg, "\"unimplemented\"", NULL);
124 fprintf(stderr, "replying failed: %m\n");
128 /* called when wsj1 receives an event */
129 void AFBClient::onEvent(void *closure, const char *event, afb_wsj1_msg *msg)
133 printf("ON-EVENT %s:\n%s\n", event,
134 json_object_to_json_string_ext(afb_wsj1_msg_object_j(msg),
135 JSON_C_TO_STRING_PRETTY));
140 /* called when wsj1 hangsup */
141 void AFBClient::onHangup(void *closure, afb_wsj1 *wsj1)
145 printf("ON-HANGUP\n");
150 /* called when wsj1 receives a reply */
151 void AFBClient::onReply(void *closure, afb_wsj1_msg *msg)
154 printf("ON-REPLY %s: %s\n%s\n", (char*)closure,
155 afb_wsj1_msg_is_reply_ok(msg) ? "OK" : "ERROR",
156 json_object_to_json_string_ext(afb_wsj1_msg_object_j(msg),
157 JSON_C_TO_STRING_PRETTY));
163 void AFBClient::onRequestSurfaceReply(void *closure, afb_wsj1_msg *msg)
166 printf("onRequestSurfaceReply %s: %s\n%s\n", (char*)closure,
167 afb_wsj1_msg_is_reply_ok(msg) ? "OK" : "ERROR",
168 json_object_to_json_string_ext(afb_wsj1_msg_object_j(msg),
169 JSON_C_TO_STRING_PRETTY));
170 printf("\n\n===>RETURN STR: %s\n\n", afb_wsj1_msg_object_s(msg));
171 // putenv("QT_IVI_SURFACE_ID=16778219");
178 void AFBClient::call(const char *api, const char *verb, const char *object)
185 printf("call(%s, %s, %s) -->\n", api, verb, object);
188 /* allocates an id for the request */
189 rc = asprintf(&key, "%d:%s/%s", ++num, api, verb);
191 /* send the request */
192 rc = afb_wsj1_call_s(wsj1, api, verb, object, AFBClient::onReply, key);
194 fprintf(stderr, "calling %s/%s(%s) failed: %m\n", api, verb, object);
196 printf("call(%s, %s, %s) <--\n", api, verb, object);
202 void AFBClient::event(const char *event, const char *object)
207 rc = afb_wsj1_send_event_s(wsj1, event, object);
209 fprintf(stderr, "sending !%s(%s) failed: %m\n", event, object);