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