Remove the AFB definition
[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 void AFBClient::requestSurface(const char *label)
56 {
57     printf("requestSurface(%s) -->\n", label);
58     fflush(stdout);
59
60     static int num = 0;
61     char *key;
62     int rc;
63     const char begin[] = "{\"drawing_name\":\"";
64     const char end[] = "\"}";
65     const char verb[] = "request_surface";
66     char *parameter = (char *)malloc(strlen(begin) +
67                                      strlen(label) +
68                                      strlen(end) + 1);
69     strcpy(parameter, begin);
70     strcat(parameter, label);
71     strcat(parameter, end);
72
73     /* allocates an id for the request */
74     rc = asprintf(&key, "%d:%s/%s", ++num, AFBClient::wmAPI, verb);
75
76     /* send the request */
77     rc = afb_wsj1_call_s(wsj1, AFBClient::wmAPI, verb, parameter, AFBClient::onRequestSurfaceReply, key);
78     if (rc < 0)
79         fprintf(stderr, "calling %s/%s(%s) failed: %m\n", AFBClient::wmAPI, verb, parameter);
80
81     printf("requestSurface(%s) <--\n", label);
82     fflush(stdout);
83 }
84
85 void AFBClient::activateSurface(const char *label)
86 {
87     printf("activateSurface(%s) -->\n", label);
88     fflush(stdout);
89
90     const char begin[] = "{\"drawing_name\":\"";
91     const char end[] = "\"}";
92     const char verb[] = "activate_surface";
93     char *parameter = (char *)malloc(strlen(begin) +
94                                      strlen(label) +
95                                      strlen(end) + 1);
96     strcpy(parameter, begin);
97     strcat(parameter, label);
98     strcat(parameter, end);
99     call(AFBClient::wmAPI, verb, parameter);
100
101     printf("activateSurface(%s) <--\n", label);
102     fflush(stdout);
103 }
104
105 int AFBClient::dispatch() {
106     return sd_event_run(loop, -1);
107 }
108
109 void AFBClient::deactivateSurface(const char *label)
110 {
111     UNUSED(label);
112 }
113
114 void AFBClient::endDraw(const char *label)
115 {
116     UNUSED(label);
117 }
118
119 /* called when wsj1 receives a method invocation */
120 void AFBClient::onCall(void *closure, const char *api, const char *verb, struct afb_wsj1_msg *msg)
121 {
122     UNUSED(closure);
123     int rc;
124     printf("ON-CALL %s/%s:\n%s\n", api, verb,
125            json_object_to_json_string_ext(afb_wsj1_msg_object_j(msg),
126                                           JSON_C_TO_STRING_PRETTY));
127     fflush(stdout);
128     rc = afb_wsj1_reply_error_s(msg, "\"unimplemented\"", NULL);
129     if (rc < 0)
130         fprintf(stderr, "replying failed: %m\n");
131 }
132
133 /* called when wsj1 receives an event */
134 void AFBClient::onEvent(void *closure, const char *event, afb_wsj1_msg *msg)
135 {
136     UNUSED(closure);
137     printf("ON-EVENT %s:\n%s\n", event,
138            json_object_to_json_string_ext(afb_wsj1_msg_object_j(msg),
139                                           JSON_C_TO_STRING_PRETTY));
140     fflush(stdout);
141 }
142
143 /* called when wsj1 hangsup */
144 void AFBClient::onHangup(void *closure, afb_wsj1 *wsj1)
145 {
146     UNUSED(closure);
147     UNUSED(wsj1);
148     printf("ON-HANGUP\n");
149     fflush(stdout);
150     exit(0);
151 }
152
153 /* called when wsj1 receives a reply */
154 void AFBClient::onReply(void *closure, afb_wsj1_msg *msg)
155 {
156     printf("ON-REPLY %s: %s\n%s\n", (char*)closure,
157            afb_wsj1_msg_is_reply_ok(msg) ? "OK" : "ERROR",
158            json_object_to_json_string_ext(afb_wsj1_msg_object_j(msg),
159                                           JSON_C_TO_STRING_PRETTY));
160     fflush(stdout);
161     free(closure);
162 }
163
164 void AFBClient::onRequestSurfaceReply(void *closure, afb_wsj1_msg *msg)
165 {
166     printf("onRequestSurfaceReply %s: %s\n%s\n", (char*)closure,
167            afb_wsj1_msg_is_reply_ok(msg) ? "OK" : "ERROR",
168            json_object_to_json_string_ext(afb_wsj1_msg_object_j(msg),
169                                           JSON_C_TO_STRING_PRETTY));
170     printf("\n\n===>RETURN STR: %s\n\n", afb_wsj1_msg_object_s(msg));
171     //    putenv("QT_IVI_SURFACE_ID=16778219");
172     fflush(stdout);
173     free(closure);
174 }
175
176 /* makes a call */
177 void AFBClient::call(const char *api, const char *verb, const char *object)
178 {
179     static int num = 0;
180     char *key;
181     int rc;
182
183     printf("call(%s, %s, %s) -->\n", api, verb, object);
184     fflush(stdout);
185
186     /* allocates an id for the request */
187     rc = asprintf(&key, "%d:%s/%s", ++num, api, verb);
188
189     /* send the request */
190     rc = afb_wsj1_call_s(wsj1, api, verb, object, AFBClient::onReply, key);
191     if (rc < 0)
192         fprintf(stderr, "calling %s/%s(%s) failed: %m\n", api, verb, object);
193
194     printf("call(%s, %s, %s) <--\n", api, verb, object);
195     fflush(stdout);
196 }
197
198 /* sends an event */
199 void AFBClient::event(const char *event, const char *object)
200 {
201     int rc;
202
203     rc = afb_wsj1_send_event_s(wsj1, event, object);
204     if (rc < 0)
205         fprintf(stderr, "sending !%s(%s) failed: %m\n", event, object);
206 }