WIP: make requestSurface/activateSurace() syncronous
[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     /* 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("init() <--\n");
52     return true;
53 }
54
55 int AFBClient::requestSurface(const char *label)
56 {
57    printf("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 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("requestSurface(%s) = %d <--\n", label, ret);
109
110    return ret;
111 }
112
113 void AFBClient::activateSurface(const char *label)
114 {
115     printf("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("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     UNUSED(label);
143 }
144
145 void AFBClient::endDraw(const char *label)
146 {
147     UNUSED(label);
148 }
149
150 /* called when wsj1 receives a method invocation */
151 void AFBClient::onCall(void *closure, const char *api, const char *verb, struct afb_wsj1_msg *msg)
152 {
153     UNUSED(closure);
154     int rc;
155     printf("ON-CALL %s/%s:\n%s\n", api, verb,
156            json_object_to_json_string_ext(afb_wsj1_msg_object_j(msg),
157                                           JSON_C_TO_STRING_PRETTY));
158     fflush(stdout);
159     rc = afb_wsj1_reply_error_s(msg, "\"unimplemented\"", NULL);
160     if (rc < 0)
161         fprintf(stderr, "replying failed: %m\n");
162 }
163
164 /* called when wsj1 receives an event */
165 void AFBClient::onEvent(void *closure, const char *event, afb_wsj1_msg *msg)
166 {
167     UNUSED(closure);
168     printf("ON-EVENT %s:\n%s\n", event,
169            json_object_to_json_string_ext(afb_wsj1_msg_object_j(msg),
170                                           JSON_C_TO_STRING_PRETTY));
171     fflush(stdout);
172 }
173
174 /* called when wsj1 hangsup */
175 void AFBClient::onHangup(void *closure, afb_wsj1 *wsj1)
176 {
177     UNUSED(closure);
178     UNUSED(wsj1);
179     printf("ON-HANGUP\n");
180     fflush(stdout);
181     exit(0);
182 }
183
184 /* called when wsj1 receives a reply */
185 void AFBClient::onReply(void *closure, afb_wsj1_msg *msg)
186 {
187     printf("ON-REPLY %s: %s\n%s\n", (char*)closure,
188            afb_wsj1_msg_is_reply_ok(msg) ? "OK" : "ERROR",
189            json_object_to_json_string_ext(afb_wsj1_msg_object_j(msg),
190                                           JSON_C_TO_STRING_PRETTY));
191     fflush(stdout);
192     free(closure);
193 }
194
195 /* makes a call */
196 void AFBClient::call(const char *api, const char *verb, const char *object)
197 {
198     static int num = 0;
199     char *key;
200     int rc;
201
202     printf("call(%s, %s, %s) -->\n", api, verb, object);
203     fflush(stdout);
204
205     /* allocates an id for the request */
206     rc = asprintf(&key, "%d:%s/%s", ++num, api, verb);
207
208     /* send the request */
209     rc = afb_wsj1_call_s(wsj1, api, verb, object, AFBClient::onReply, key);
210     if (rc < 0)
211         fprintf(stderr, "calling %s/%s(%s) failed: %m\n", api, verb, object);
212
213     printf("call(%s, %s, %s) <--\n", api, verb, object);
214     fflush(stdout);
215 }
216
217 /* sends an event */
218 void AFBClient::event(const char *event, const char *object)
219 {
220     int rc;
221
222     rc = afb_wsj1_send_event_s(wsj1, event, object);
223     if (rc < 0)
224         fprintf(stderr, "sending !%s(%s) failed: %m\n", event, object);
225 }