Application code clean-up
[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 const char * AFBClient::wmURI = "ws://localhost:1700/api?token=wm";
12 const char * AFBClient::wmAPI = "winman";
13
14 AFBClient::AFBClient() : itf()
15 {
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;
20 }
21
22 AFBClient::~AFBClient()
23 {
24 }
25
26 bool AFBClient::init()
27 {
28     printf("AFBClient::init() -->\n");
29     /* get the default event loop */
30     int rc = sd_event_default(&loop);
31     if (rc < 0) {
32         fprintf(stderr, "Connection to default event loop failed: %s\n", strerror(-rc));
33         return false;
34     }
35
36     /* connect the websocket wsj1 to the uri given by the first argument */
37     wsj1 = afb_ws_client_connect_wsj1(loop, wmURI, &itf, NULL);
38     if (wsj1 == NULL) {
39         fprintf(stderr, "Connection to %s failed: %m\n", wmURI);
40         return false;
41     }
42
43     printf("AFBClient::init() <--\n");
44     return true;
45 }
46
47 int AFBClient::dispatch(uint64_t timeout) {
48     return sd_event_run(loop, timeout);
49 }
50
51 int AFBClient::requestSurface(const char *label)
52 {
53    printf("AFBClient::requestSurface(%s) -->\n", label);
54    constexpr char const *verb = "request_surface";
55    int ret = -1;
56
57    json_object *jp = json_object_new_object();
58    json_object_object_add(jp, "drawing_name", json_object_new_string(label));
59
60    // std::experimental::optional look-alike
61    struct optional {
62       int value;
63       bool is_not_set;
64    };
65
66    constexpr struct optional const nullopt = {0, true};
67    auto id = nullopt;
68
69    /* send the request */
70    int rc = afb_wsj1_call_j(
71       wsj1, AFBClient::wmAPI, verb, jp,
72       [](void *closure, afb_wsj1_msg *msg) {
73          if (afb_wsj1_msg_is_reply_ok(msg)) {
74             int id = json_object_get_int(
75                json_object_object_get(afb_wsj1_msg_object_j(msg), "response"));
76             auto oid = (optional *)closure;
77             *oid = optional{id};
78          } else
79             fprintf(stderr, "wrong request surface reply received!\n");
80       },
81       (void *)&id);
82
83    if (rc < 0) {
84       fprintf(stderr, "calling %s/%s(%s) failed: %m\n", AFBClient::wmAPI, verb,
85               json_object_to_json_string(jp));
86    } else {
87       // Lets make this call sync here...
88       dispatch(-1);
89
90       if (! id.is_not_set) {
91           char *buf;
92           asprintf(&buf, "%d", id.value);
93           printf("setenv(\"QT_IVI_SURFACE_ID\", %s, 1)\n", buf);
94           if (setenv("QT_IVI_SURFACE_ID", buf, 1) != 0) {
95               fprintf(stderr, "putenv failed: %m\n");
96           } else {
97               ret = 0; // Single point of success
98           }
99       } else {
100           fprintf(stderr, "Could not get surface ID from WM\n");
101       }
102    }
103
104    printf("AFBClient::requestSurface(%s) = %d <--\n", label, ret);
105
106    return ret;
107 }
108
109 void AFBClient::activateSurface(const char *label)
110 {
111     printf("AFBClient::activateSurface(%s) -->\n", label);
112     fflush(stdout);
113
114     const char begin[] = "{\"drawing_name\":\"";
115     const char end[] = "\"}";
116     const char verb[] = "activate_surface";
117     char *parameter = (char *)malloc(strlen(begin) +
118                                      strlen(label) +
119                                      strlen(end) + 1);
120     strcpy(parameter, begin);
121     strcat(parameter, label);
122     strcat(parameter, end);
123     call(AFBClient::wmAPI, verb, parameter);
124
125     // Sync this one too
126     dispatch(-1);
127
128     printf("AFBClient::activateSurface(%s) <--\n", label);
129     fflush(stdout);
130 }
131
132 void AFBClient::deactivateSurface(const char *label)
133 {
134     printf("AFBClient::deactivateSurface(%s) -->\n", label);
135     fflush(stdout);
136     json_object *j = json_object_new_object();
137     json_object_object_add(j, "drawing_name", json_object_new_string(label));
138     call(AFBClient::wmAPI, "deactivate_surface", json_object_to_json_string(j));
139     json_object_put(j);
140     dispatch(-1);
141     printf("AFBClient::deactivateSurface(%s) <--\n", label);
142     fflush(stdout);
143 }
144
145 void AFBClient::endDraw(const char *label)
146 {
147     printf("AFBClient::endDraw(%s) -->\n", label);
148     fflush(stdout);
149     json_object *j = json_object_new_object();
150     json_object_object_add(j, "drawing_name", json_object_new_string(label));
151     call(AFBClient::wmAPI, "enddraw", json_object_to_json_string(j));
152     json_object_put(j);
153     dispatch(-1);
154     printf("AFBClient::endDraw(%s) <--\n", label);
155     fflush(stdout);
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 }