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