Trying to fix the link with wsj1
[staging/windowmanager.git] / AFBClient.cpp
1 #include "AFBClient.h"
2
3 #include <stdlib.h>
4 #include <stdio.h>
5 #include <errno.h>
6 #include <string.h>
7 #include <unistd.h>
8
9 #define UNUSED(x) (void)(x)
10
11 extern "C" {
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);
17 }
18
19 const char * AFBClient::wmURI = "ws://localhost:1700/api?token=wm";
20 const char * AFBClient::wmAPI = "winman";
21
22 AFBClient::AFBClient() : itf()
23 {
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;
28 }
29
30 AFBClient::~AFBClient()
31 {
32 }
33
34 bool AFBClient::init()
35 {
36     printf("init() -->\n");
37     /* get the default event loop */
38     int rc = sd_event_default(&loop);
39     if (rc < 0) {
40         fprintf(stderr, "Connection to default event loop failed: %s\n", strerror(-rc));
41         return false;
42     }
43
44 #ifdef AFB
45     /* connect the websocket wsj1 to the uri given by the first argument */
46     wsj1 = afb_ws_client_connect_wsj1(loop, wmURI, &itf, NULL);
47     if (wsj1 == NULL) {
48         fprintf(stderr, "Connection to %s failed: %m\n", wmURI);
49         return false;
50     }
51 #endif
52
53     printf("init() <--\n");
54     return true;
55 }
56
57 void AFBClient::requestSurface(const char *label)
58 {
59     printf("requestSurface(%s) -->\n", label);
60     fflush(stdout);
61
62     static int num = 0;
63     char *key;
64     int rc;
65     const char begin[] = "{\"drawing_name\":\"";
66     const char end[] = "\"}";
67     const char verb[] = "request_surface";
68     char *parameter = (char *)malloc(strlen(begin) +
69                                      strlen(label) +
70                                      strlen(end) + 1);
71     strcpy(parameter, begin);
72     strcat(parameter, label);
73     strcat(parameter, end);
74
75     /* allocates an id for the request */
76     rc = asprintf(&key, "%d:%s/%s", ++num, AFBClient::wmAPI, verb);
77
78     /* send the request */
79     rc = afb_wsj1_call_s(wsj1, AFBClient::wmAPI, verb, parameter, AFBClient::onRequestSurfaceReply, key);
80     if (rc < 0)
81         fprintf(stderr, "calling %s/%s(%s) failed: %m\n", AFBClient::wmAPI, verb, parameter);
82
83     printf("requestSurface(%s) <--\n", label);
84     fflush(stdout);
85 }
86
87 void AFBClient::activateSurface(const char *label)
88 {
89     printf("activateSurface(%s) -->\n", label);
90     fflush(stdout);
91
92     const char begin[] = "{\"drawing_name\":\"";
93     const char end[] = "\"}";
94     const char verb[] = "activate_surface";
95     char *parameter = (char *)malloc(strlen(begin) +
96                                      strlen(label) +
97                                      strlen(end) + 1);
98     strcpy(parameter, begin);
99     strcat(parameter, label);
100     strcat(parameter, end);
101     call(AFBClient::wmAPI, verb, parameter);
102
103     printf("activateSurface(%s) <--\n", label);
104     fflush(stdout);
105 }
106
107 void AFBClient::deactivateSurface(const char *label)
108 {
109     UNUSED(label);
110 }
111
112 void AFBClient::endDraw(const char *label)
113 {
114     UNUSED(label);
115 }
116
117 /* called when wsj1 receives a method invocation */
118 void AFBClient::onCall(void *closure, const char *api, const char *verb, struct afb_wsj1_msg *msg)
119 {
120 #ifdef AFB
121     UNUSED(closure);
122     int rc;
123     printf("ON-CALL %s/%s:\n%s\n", api, verb,
124            json_object_to_json_string_ext(afb_wsj1_msg_object_j(msg),
125                                           JSON_C_TO_STRING_PRETTY));
126     fflush(stdout);
127     rc = afb_wsj1_reply_error_s(msg, "\"unimplemented\"", NULL);
128     if (rc < 0)
129         fprintf(stderr, "replying failed: %m\n");
130 #endif
131 }
132
133 /* called when wsj1 receives an event */
134 void AFBClient::onEvent(void *closure, const char *event, afb_wsj1_msg *msg)
135 {
136 #ifdef AFB
137     UNUSED(closure);
138     printf("ON-EVENT %s:\n%s\n", event,
139            json_object_to_json_string_ext(afb_wsj1_msg_object_j(msg),
140                                           JSON_C_TO_STRING_PRETTY));
141     fflush(stdout);
142 #endif
143 }
144
145 /* called when wsj1 hangsup */
146 void AFBClient::onHangup(void *closure, afb_wsj1 *wsj1)
147 {
148     UNUSED(closure);
149     UNUSED(wsj1);
150     printf("ON-HANGUP\n");
151     fflush(stdout);
152     exit(0);
153 }
154
155 /* called when wsj1 receives a reply */
156 void AFBClient::onReply(void *closure, afb_wsj1_msg *msg)
157 {
158 #ifdef AFB
159     printf("ON-REPLY %s: %s\n%s\n", (char*)closure,
160            afb_wsj1_msg_is_reply_ok(msg) ? "OK" : "ERROR",
161            json_object_to_json_string_ext(afb_wsj1_msg_object_j(msg),
162                                           JSON_C_TO_STRING_PRETTY));
163     fflush(stdout);
164     free(closure);
165 #endif
166 }
167
168 void AFBClient::onRequestSurfaceReply(void *closure, afb_wsj1_msg *msg)
169 {
170 #ifdef AFB
171     printf("onRequestSurfaceReply %s: %s\n%s\n", (char*)closure,
172            afb_wsj1_msg_is_reply_ok(msg) ? "OK" : "ERROR",
173            json_object_to_json_string_ext(afb_wsj1_msg_object_j(msg),
174                                           JSON_C_TO_STRING_PRETTY));
175     printf("\n\n===>RETURN STR: %s\n\n", afb_wsj1_msg_object_s(msg));
176     //    putenv("QT_IVI_SURFACE_ID=16778219");
177     fflush(stdout);
178     free(closure);
179 #endif
180 }
181
182 /* makes a call */
183 void AFBClient::call(const char *api, const char *verb, const char *object)
184 {
185 #ifdef AFB
186     static int num = 0;
187     char *key;
188     int rc;
189
190     printf("call(%s, %s, %s) -->\n", api, verb, object);
191     fflush(stdout);
192
193     /* allocates an id for the request */
194     rc = asprintf(&key, "%d:%s/%s", ++num, api, verb);
195
196     /* send the request */
197     rc = afb_wsj1_call_s(wsj1, api, verb, object, AFBClient::onReply, key);
198     if (rc < 0)
199         fprintf(stderr, "calling %s/%s(%s) failed: %m\n", api, verb, object);
200
201     printf("call(%s, %s, %s) <--\n", api, verb, object);
202     fflush(stdout);
203 #endif
204 }
205
206 /* sends an event */
207 void AFBClient::event(const char *event, const char *object)
208 {
209 #ifdef AFB
210     int rc;
211
212     rc = afb_wsj1_send_event_s(wsj1, event, object);
213     if (rc < 0)
214         fprintf(stderr, "sending !%s(%s) failed: %m\n", event, object);
215 #endif
216 }