Removing some test calls
[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("AFBClient::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     /* connect the websocket wsj1 to the uri given by the first argument */
45     wsj1 = afb_ws_client_connect_wsj1(loop, wmURI, &itf, NULL);
46     if (wsj1 == NULL) {
47         fprintf(stderr, "Connection to %s failed: %m\n", wmURI);
48         return false;
49     }
50
51     printf("AFBClient::init() <--\n");
52     return true;
53 }
54
55 int AFBClient::requestSurface(const char *label)
56 {
57    printf("AFBClient::requestSurface(%s) -->\n", label);
58    constexpr char const *verb = "request_surface";
59    int ret = -1;
60
61    json_object *jp = json_object_new_object();
62    json_object_object_add(jp, "drawing_name", json_object_new_string(label));
63
64    // std::experimental::optional look-alike
65    struct optional {
66       int value;
67       bool is_not_set;
68    };
69
70    constexpr struct optional const nullopt = {0, true};
71    auto id = nullopt;
72
73    /* send the request */
74    int rc = afb_wsj1_call_j(
75       wsj1, AFBClient::wmAPI, verb, jp,
76       [](void *closure, afb_wsj1_msg *msg) {
77          if (afb_wsj1_msg_is_reply_ok(msg)) {
78             int id = json_object_get_int(
79                json_object_object_get(afb_wsj1_msg_object_j(msg), "response"));
80             auto oid = (optional *)closure;
81             *oid = optional{id};
82          } else
83             fprintf(stderr, "wrong request surface reply received!\n");
84       },
85       (void *)&id);
86
87    if (rc < 0) {
88       fprintf(stderr, "calling %s/%s(%s) failed: %m\n", AFBClient::wmAPI, verb,
89               json_object_to_json_string(jp));
90    } else {
91       // Lets make this call sync here...
92       dispatch(-1);
93
94       if (! id.is_not_set) {
95           char *buf;
96           asprintf(&buf, "%d", id.value);
97           printf("setenv(\"QT_IVI_SURFACE_ID\", %s, 1)\n", buf);
98           if (setenv("QT_IVI_SURFACE_ID", buf, 1) != 0) {
99               fprintf(stderr, "putenv failed: %m\n");
100           } else {
101               ret = 0; // Single point of success
102           }
103       } else {
104           fprintf(stderr, "Could not get surface ID from WM\n");
105       }
106    }
107
108    printf("AFBClient::requestSurface(%s) = %d <--\n", label, ret);
109
110    return ret;
111 }
112
113 void AFBClient::activateSurface(const char *label)
114 {
115     printf("AFBClient::activateSurface(%s) -->\n", label);
116     fflush(stdout);
117
118     const char begin[] = "{\"drawing_name\":\"";
119     const char end[] = "\"}";
120     const char verb[] = "activate_surface";
121     char *parameter = (char *)malloc(strlen(begin) +
122                                      strlen(label) +
123                                      strlen(end) + 1);
124     strcpy(parameter, begin);
125     strcat(parameter, label);
126     strcat(parameter, end);
127     call(AFBClient::wmAPI, verb, parameter);
128
129     // Sync this one too
130     dispatch(-1);
131
132     printf("AFBClient::activateSurface(%s) <--\n", label);
133     fflush(stdout);
134 }
135
136 int AFBClient::dispatch(uint64_t timeout) {
137     return sd_event_run(loop, timeout);
138 }
139
140 void AFBClient::deactivateSurface(const char *label)
141 {
142     printf("AFBClient::deactivateSurface(%s) -->\n", label);
143     fflush(stdout);
144     json_object *j = json_object_new_object();
145     json_object_object_add(j, "drawing_name", json_object_new_string(label));
146     call(AFBClient::wmAPI, "deactivate_surface", json_object_to_json_string(j));
147     json_object_put(j);
148     dispatch(-1);
149     printf("AFBClient::deactivateSurface(%s) <--\n", label);
150     fflush(stdout);
151 }
152
153 void AFBClient::endDraw(const char *label)
154 {
155     printf("AFBClient::endDraw(%s) -->\n", label);
156     fflush(stdout);
157     json_object *j = json_object_new_object();
158     json_object_object_add(j, "drawing_name", json_object_new_string(label));
159     call(AFBClient::wmAPI, "enddraw", json_object_to_json_string(j));
160     json_object_put(j);
161     dispatch(-1);
162     printf("AFBClient::endDraw(%s) <--\n", label);
163     fflush(stdout);
164 }
165
166 /* called when wsj1 receives a method invocation */
167 void AFBClient::onCall(void *closure, const char *api, const char *verb, struct afb_wsj1_msg *msg)
168 {
169     UNUSED(closure);
170     int rc;
171     printf("ON-CALL %s/%s:\n%s\n", api, verb,
172            json_object_to_json_string_ext(afb_wsj1_msg_object_j(msg),
173                                           JSON_C_TO_STRING_PRETTY));
174     fflush(stdout);
175     rc = afb_wsj1_reply_error_s(msg, "\"unimplemented\"", NULL);
176     if (rc < 0)
177         fprintf(stderr, "replying failed: %m\n");
178 }
179
180 /* called when wsj1 receives an event */
181 void AFBClient::onEvent(void *closure, const char *event, afb_wsj1_msg *msg)
182 {
183     UNUSED(closure);
184     printf("ON-EVENT %s:\n%s\n", event,
185            json_object_to_json_string_ext(afb_wsj1_msg_object_j(msg),
186                                           JSON_C_TO_STRING_PRETTY));
187     fflush(stdout);
188 }
189
190 /* called when wsj1 hangsup */
191 void AFBClient::onHangup(void *closure, afb_wsj1 *wsj1)
192 {
193     UNUSED(closure);
194     UNUSED(wsj1);
195     printf("ON-HANGUP\n");
196     fflush(stdout);
197     exit(0);
198 }
199
200 /* called when wsj1 receives a reply */
201 void AFBClient::onReply(void *closure, afb_wsj1_msg *msg)
202 {
203     printf("ON-REPLY %s: %s\n%s\n", (char*)closure,
204            afb_wsj1_msg_is_reply_ok(msg) ? "OK" : "ERROR",
205            json_object_to_json_string_ext(afb_wsj1_msg_object_j(msg),
206                                           JSON_C_TO_STRING_PRETTY));
207     fflush(stdout);
208     free(closure);
209 }
210
211 /* makes a call */
212 void AFBClient::call(const char *api, const char *verb, const char *object)
213 {
214     static int num = 0;
215     char *key;
216     int rc;
217
218     printf("call(%s, %s, %s) -->\n", api, verb, object);
219     fflush(stdout);
220
221     /* allocates an id for the request */
222     rc = asprintf(&key, "%d:%s/%s", ++num, api, verb);
223
224     /* send the request */
225     rc = afb_wsj1_call_s(wsj1, api, verb, object, AFBClient::onReply, key);
226     if (rc < 0)
227         fprintf(stderr, "calling %s/%s(%s) failed: %m\n", api, verb, object);
228
229     printf("call(%s, %s, %s) <--\n", api, verb, object);
230     fflush(stdout);
231 }
232
233 /* sends an event */
234 void AFBClient::event(const char *event, const char *object)
235 {
236     int rc;
237
238     rc = afb_wsj1_send_event_s(wsj1, event, object);
239     if (rc < 0)
240         fprintf(stderr, "sending !%s(%s) failed: %m\n", event, object);
241 }