WIP: add enddraw() and deacrivate_surface() implementations.
[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 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("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     json_object *j = json_object_new_object();
143     json_object_object_add(j, "drawing_name", json_object_new_string(label));
144     call(AFBClient::wmAPI, "deactivate_surface", json_object_to_json_string(j));
145     json_object_put(j);
146     dispatch(-1);
147 }
148
149 void AFBClient::endDraw(const char *label)
150 {
151     json_object *j = json_object_new_object();
152     json_object_object_add(j, "drawing_name", json_object_new_string(label));
153     call(AFBClient::wmAPI, "enddraw", json_object_to_json_string(j));
154     json_object_put(j);
155     dispatch(-1);
156 }
157
158 /* called when wsj1 receives a method invocation */
159 void AFBClient::onCall(void *closure, const char *api, const char *verb, struct afb_wsj1_msg *msg)
160 {
161     UNUSED(closure);
162     int rc;
163     printf("ON-CALL %s/%s:\n%s\n", api, verb,
164            json_object_to_json_string_ext(afb_wsj1_msg_object_j(msg),
165                                           JSON_C_TO_STRING_PRETTY));
166     fflush(stdout);
167     rc = afb_wsj1_reply_error_s(msg, "\"unimplemented\"", NULL);
168     if (rc < 0)
169         fprintf(stderr, "replying failed: %m\n");
170 }
171
172 /* called when wsj1 receives an event */
173 void AFBClient::onEvent(void *closure, const char *event, afb_wsj1_msg *msg)
174 {
175     UNUSED(closure);
176     printf("ON-EVENT %s:\n%s\n", event,
177            json_object_to_json_string_ext(afb_wsj1_msg_object_j(msg),
178                                           JSON_C_TO_STRING_PRETTY));
179     fflush(stdout);
180 }
181
182 /* called when wsj1 hangsup */
183 void AFBClient::onHangup(void *closure, afb_wsj1 *wsj1)
184 {
185     UNUSED(closure);
186     UNUSED(wsj1);
187     printf("ON-HANGUP\n");
188     fflush(stdout);
189     exit(0);
190 }
191
192 /* called when wsj1 receives a reply */
193 void AFBClient::onReply(void *closure, afb_wsj1_msg *msg)
194 {
195     printf("ON-REPLY %s: %s\n%s\n", (char*)closure,
196            afb_wsj1_msg_is_reply_ok(msg) ? "OK" : "ERROR",
197            json_object_to_json_string_ext(afb_wsj1_msg_object_j(msg),
198                                           JSON_C_TO_STRING_PRETTY));
199     fflush(stdout);
200     free(closure);
201 }
202
203 /* makes a call */
204 void AFBClient::call(const char *api, const char *verb, const char *object)
205 {
206     static int num = 0;
207     char *key;
208     int rc;
209
210     printf("call(%s, %s, %s) -->\n", api, verb, object);
211     fflush(stdout);
212
213     /* allocates an id for the request */
214     rc = asprintf(&key, "%d:%s/%s", ++num, api, verb);
215
216     /* send the request */
217     rc = afb_wsj1_call_s(wsj1, api, verb, object, AFBClient::onReply, key);
218     if (rc < 0)
219         fprintf(stderr, "calling %s/%s(%s) failed: %m\n", api, verb, object);
220
221     printf("call(%s, %s, %s) <--\n", api, verb, object);
222     fflush(stdout);
223 }
224
225 /* sends an event */
226 void AFBClient::event(const char *event, const char *object)
227 {
228     int rc;
229
230     rc = afb_wsj1_send_event_s(wsj1, event, object);
231     if (rc < 0)
232         fprintf(stderr, "sending !%s(%s) failed: %m\n", event, object);
233 }