e67e74bf0d0bb6ab0918c0fa93b2e45f8b9b6dcd
[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
8 #define UNUSED(x) (void)(x)
9
10 const char * AFBClient::wmURI = "ws://localhost:1700/api?wm";
11 const char * AFBClient::wmAPI = "winman";
12
13 AFBClient::AFBClient() : itf()
14 {
15     ///* itinializing the callback interface for wsj1 */
16     itf.on_hangup = AFBClient::onHangup;
17     itf.on_call = AFBClient::onCall;
18     itf.on_event = AFBClient::onEvent;
19 }
20
21 AFBClient& AFBClient::operator =(const AFBClient &that)
22 {
23     itf = that.itf;
24     return *this;
25 }
26
27 AFBClient::~AFBClient()
28 {
29 }
30
31 bool AFBClient::init()
32 {
33     /* get the default event loop */
34     int rc = sd_event_default(&loop);
35     if (rc < 0) {
36         fprintf(stderr, "Connection to default event loop failed: %s\n", strerror(-rc));
37         return false;
38     }
39
40 #ifdef AFB
41     /* connect the websocket wsj1 to the uri given by the first argument */
42     wsj1 = afb_ws_client_connect_wsj1(loop, wmURI, &itf, NULL);
43     if (wsj1 == NULL) {
44         fprintf(stderr, "Connection to %s failed: %m\n", wmURI);
45         return false;
46     }
47 #endif
48
49     return true;
50 }
51
52 void AFBClient::requestSurface(const char *label)
53 {
54     const char functionParamName[] = "{\"drawing_name\":\"";
55     char *parameter = (char *)malloc(strlen(functionParamName) + strlen(label) + 3);
56     strcpy(parameter, functionParamName);
57     strcat(parameter, label);
58     strcat(parameter, "\"}");
59     printf("requestSurface(%s): %s\n", label, parameter);
60     call(AFBClient::wmAPI, "request_surface", parameter);
61 }
62
63 void AFBClient::activateSurface(const char *label)
64 {
65     const char functionParamName[] = "{\"drawing_name\":\"";
66     char *parameter = (char *)malloc(strlen(functionParamName) + strlen(label) + 3);
67     strcpy(parameter, functionParamName);
68     strcat(parameter, label);
69     strcat(parameter, "\"}");
70     printf("activateSurface(%s): %s\n", label, parameter);
71     call(AFBClient::wmAPI, "activate_surface", parameter);
72 }
73
74 void AFBClient::deactivateSurface(const char *label)
75 {
76     UNUSED(label);
77 }
78
79 void AFBClient::endDraw(const char *label)
80 {
81     UNUSED(label);
82 }
83
84 /* called when wsj1 receives a method invocation */
85 void AFBClient::onCall(void *closure, const char *api, const char *verb, struct afb_wsj1_msg *msg)
86 {
87 #ifdef AFB
88     UNUSED(closure);
89     int rc;
90     printf("ON-CALL %s/%s:\n%s\n", api, verb,
91            json_object_to_json_string_ext(afb_wsj1_msg_object_j(msg),
92                                           JSON_C_TO_STRING_PRETTY));
93     fflush(stdout);
94     rc = afb_wsj1_reply_error_s(msg, "\"unimplemented\"", NULL);
95     if (rc < 0)
96         fprintf(stderr, "replying failed: %m\n");
97 #endif
98 }
99
100 /* called when wsj1 receives an event */
101 void AFBClient::onEvent(void *closure, const char *event, afb_wsj1_msg *msg)
102 {
103 #ifdef AFB
104     UNUSED(closure);
105     printf("ON-EVENT %s:\n%s\n", event,
106            json_object_to_json_string_ext(afb_wsj1_msg_object_j(msg),
107                                           JSON_C_TO_STRING_PRETTY));
108     fflush(stdout);
109 #endif
110 }
111
112 /* called when wsj1 hangsup */
113 void AFBClient::onHangup(void *closure, afb_wsj1 *wsj1)
114 {
115     UNUSED(closure);
116     UNUSED(wsj1);
117     printf("ON-HANGUP\n");
118     fflush(stdout);
119     exit(0);
120 }
121
122 /* called when wsj1 receives a reply */
123 void AFBClient::onReply(void *closure, afb_wsj1_msg *msg)
124 {
125 #ifdef AFB
126     printf("ON-REPLY %s: %s\n%s\n", (char*)closure,
127            afb_wsj1_msg_is_reply_ok(msg) ? "OK" : "ERROR",
128            json_object_to_json_string_ext(afb_wsj1_msg_object_j(msg),
129                                           JSON_C_TO_STRING_PRETTY));
130     fflush(stdout);
131     free(closure);
132 #endif
133 }
134
135 /* makes a call */
136 void AFBClient::call(const char *api, const char *verb, const char *object)
137 {
138 #ifdef AFB
139     static int num = 0;
140     char *key;
141     int rc;
142
143     /* allocates an id for the request */
144     rc = asprintf(&key, "%d:%s/%s", ++num, api, verb);
145
146     /* send the request */
147     rc = afb_wsj1_call_s(wsj1, api, verb, object, AFBClient::onReply, key);
148     if (rc < 0)
149         fprintf(stderr, "calling %s/%s(%s) failed: %m\n", api, verb, object);
150 #endif
151 }
152
153 /* sends an event */
154 void AFBClient::event(const char *event, const char *object)
155 {
156 #ifdef AFB
157     int rc;
158
159     rc = afb_wsj1_send_event_s(wsj1, event, object);
160     if (rc < 0)
161         fprintf(stderr, "sending !%s(%s) failed: %m\n", event, object);
162 #endif
163 }
164
165 void AFBClient::emitSignalOrCall(const char *api, const char *verb, const char *object)
166 {
167     if (object == NULL || object[0] == 0)
168         object = "null";
169     if (api[0] == '!' && api[1] == 0)
170         event(verb, object);
171     else
172         call(api, verb, object);
173 }