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