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