#include "AFBClient.h"
+#include <cctype>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#define UNUSED(x) (void)(x)
-const char * AFBClient::wmURI = "ws://localhost:1700/api?token=wm";
+constexpr const int token_maxlen = 20;
const char * AFBClient::wmAPI = "winman";
AFBClient::AFBClient() : itf()
{
}
-bool AFBClient::init()
+int AFBClient::init(int port, char const *token)
{
+ char *uribuf = NULL;
+ int rc = -1;
+
printf("AFBClient::init() -->\n");
+
+ if (!token || strlen(token) > token_maxlen) {
+ fprintf(stderr, "Token is invalid\n");
+ rc = -EINVAL;
+ goto fail;
+ }
+
+ for (char const *p = token; *p; p++) {
+ if (!isalnum(*p)) {
+ fprintf(stderr, "Token is invalid\n");
+ rc = -EINVAL;
+ goto fail;
+ }
+ }
+
+ if (port < 1 && port > 0xffff) {
+ fprintf(stderr, "Port is invalid\n");
+ rc = -EINVAL;
+ goto fail;
+ }
+
/* get the default event loop */
- int rc = sd_event_default(&loop);
+ rc = sd_event_default(&loop);
if (rc < 0) {
fprintf(stderr, "Connection to default event loop failed: %s\n", strerror(-rc));
- return false;
+ goto fail;
}
+ asprintf(&uribuf, "ws://localhost:%d/api?token=%s", port, token);
+
/* connect the websocket wsj1 to the uri given by the first argument */
- wsj1 = afb_ws_client_connect_wsj1(loop, wmURI, &itf, NULL);
+ wsj1 = afb_ws_client_connect_wsj1(loop, uribuf, &itf, NULL);
if (wsj1 == NULL) {
- fprintf(stderr, "Connection to %s failed: %m\n", wmURI);
- return false;
+ sd_event_unref(loop);
+ fprintf(stderr, "Connection to %s failed: %m\n", uribuf);
+ rc = -errno;
+ goto fail;
}
printf("AFBClient::init() <--\n");
- return true;
+ return 0;
+
+fail:
+ printf("AFBClient::init() <--\n");
+ return rc;
}
int AFBClient::dispatch(uint64_t timeout) {
{
printf("AFBClient::requestSurface(%s) -->\n", label);
constexpr char const *verb = "request_surface";
- int ret = -1;
json_object *jp = json_object_new_object();
json_object_object_add(jp, "drawing_name", json_object_new_string(label));
if (setenv("QT_IVI_SURFACE_ID", buf, 1) != 0) {
fprintf(stderr, "putenv failed: %m\n");
} else {
- ret = 0; // Single point of success
+ rc = 0; // Single point of success
}
} else {
fprintf(stderr, "Could not get surface ID from WM\n");
+ rc = -EINVAL;
}
}
- printf("AFBClient::requestSurface(%s) = %d <--\n", label, ret);
+ printf("AFBClient::requestSurface(%s) = %d <--\n", label, rc);
- return ret;
+ return rc;
}
-void AFBClient::activateSurface(const char *label)
+int AFBClient::activateSurface(const char *label)
{
printf("AFBClient::activateSurface(%s) -->\n", label);
fflush(stdout);
printf("AFBClient::activateSurface(%s) <--\n", label);
fflush(stdout);
+ return 0;
}
-void AFBClient::deactivateSurface(const char *label)
+int AFBClient::deactivateSurface(const char *label)
{
printf("AFBClient::deactivateSurface(%s) -->\n", label);
fflush(stdout);
dispatch(-1);
printf("AFBClient::deactivateSurface(%s) <--\n", label);
fflush(stdout);
+ return 0;
}
-void AFBClient::endDraw(const char *label)
+int AFBClient::endDraw(const char *label)
{
printf("AFBClient::endDraw(%s) -->\n", label);
fflush(stdout);
dispatch(-1);
printf("AFBClient::endDraw(%s) <--\n", label);
fflush(stdout);
+ return 0;
}
/* called when wsj1 receives a method invocation */
if (rc < 0)
fprintf(stderr, "sending !%s(%s) failed: %m\n", event, object);
}
+
+void AFBClient::set_event_handler(enum EventType at, std::function<void(char const *)> func) {
+ // XXX todo
+}
#include <systemd/sd-event.h>
+#include <functional>
+
extern "C"
{
#include <json-c/json.h>
virtual~AFBClient();
public:
+ enum EventType {
+ Event_Active = 1,
+ Event_Inactive,
+ Event_Visible,
+ Event_Invisible,
+ Event_SyncDraw,
+ Event_FlushDraw,
+ };
+
static AFBClient &instance()
{
static AFBClient obj;
return obj;
}
- bool init();
- int requestSurface(const char *label);
- void activateSurface(const char *label);
- void deactivateSurface(const char *label);
- void endDraw(const char *label);
+ int init(int port, char const *token);
int dispatch(uint64_t timeout);
+ // WM API
+ int requestSurface(const char *label);
+ int activateSurface(const char *label);
+ int deactivateSurface(const char *label);
+ int endDraw(const char *label);
+
+ void set_event_handler(enum EventType et, std::function<void(char const *label)> f);
+
static void onCall(void *closure, const char *api, const char *verb, struct afb_wsj1_msg *msg);
static void onEvent(void *closure, const char *event, struct afb_wsj1_msg *msg);
static void onHangup(void *closure, struct afb_wsj1 *wsj1);