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