+#include "json_helper.hpp"
#include "util.hpp"
#include "wayland.hpp"
#include <algorithm>
#include <json.h>
-#define AFB_BINDING_VERSION 2
-
extern "C" {
#include <afb/afb-binding.h>
#include <systemd/sd-event.h>
}
-#include <json.hpp>
-
namespace {
struct wayland {
std::unique_ptr<wl::display> display;
int display_event_callback(sd_event_source *evs, int fd, uint32_t events,
void *data) {
if ((events & EPOLLHUP) != 0) {
- AFB_ERROR("The compositor hung up, dying now.");
+ logerror("The compositor hung up, dying now.");
delete g_wayland;
g_wayland = nullptr;
goto error;
if (events & EPOLLIN) {
int ret = g_wayland->display->dispatch();
if (ret == -1) {
- AFB_ERROR("wl_display_dipatch() returned error %d",
+ logerror("wl_display_dipatch() returned error %d",
g_wayland->display->get_error());
goto error;
}
lognotice("WinMan ver. %s", WINMAN_VERSION_STRING);
if (g_wayland != nullptr) {
- AFB_ERROR("Wayland context already initialized?");
+ logerror("Wayland context already initialized?");
return 0;
}
if (getenv("XDG_RUNTIME_DIR") == nullptr) {
- AFB_ERROR("Environment variable XDG_RUNTIME_DIR not set");
+ logerror("Environment variable XDG_RUNTIME_DIR not set");
goto error;
}
g_wayland = new wayland;
if (g_wayland->init() == -1) {
- AFB_ERROR("Could not connect to compositor");
+ logerror("Could not connect to compositor");
goto error;
}
if (char const *e = init_layout()) {
- AFB_ERROR("Could not init layout: %s", e);
+ logerror("Could not init layout: %s", e);
goto error;
}
g_wayland->display->get_fd(), EPOLLIN,
display_event_callback, g_wayland);
if (ret < 0) {
- AFB_ERROR("Could not initialize wayland event handler: %s",
- std::strerror(-ret));
+ logerror("Could not initialize wayland event handler: %d", -ret);
goto error;
}
}
try {
return binding_init_();
} catch (std::exception &e) {
- AFB_ERROR("Uncaught exception in binding_init(): %s", e.what());
+ logerror("Uncaught exception in binding_init(): %s", e.what());
}
return -1;
}
-#define CHECK_WAYLAND() \
- do { \
- if (g_wayland == nullptr) { \
- afb_req_fail(req, "failed", \
- "Binding not initialized, did the compositor die?"); \
- return; \
- } \
- } while (0)
-
// _ _ _ _ ____
// __| | ___| |__ _ _ __ _ ___| |_ __ _| |_ _ _ ___ / /\ \
// / _` |/ _ \ '_ \| | | |/ _` | / __| __/ _` | __| | | / __| | | |
// | (_| | __/ |_) | |_| | (_| | \__ \ || (_| | |_| |_| \__ \ | | |
// \__,_|\___|_.__/ \__,_|\__, |___|___/\__\__,_|\__|\__,_|___/ | | |
// |___/_____| \_\/_/
-void debug_status(struct afb_req req) noexcept {
+void debug_status(struct afb_req req) {
// Quick and dirty, dump current surfaces and layers
AFB_REQ_DEBUG(req, "status");
- CHECK_WAYLAND();
-
- try {
- using json = nlohmann::json;
-
- json j;
-
- if (!g_wayland->controller->surfaces.empty()) {
- auto a = json::array();
- for (auto const &i : g_wayland->controller->surfaces) {
- auto const &r = i.second->dst_rect;
- auto const &s = i.second->size;
- a.push_back({{"id", i.first},
- {"size", {s.w, s.h}},
- {"dst_rect", {r.w, r.h, r.x, r.y}}});
- }
- j["surfaces"] = a;
- }
+ auto o = json_object_new_object();
+ json_object_object_add(o, "surfaces",
+ to_json(g_wayland->controller->sprops));
+ json_object_object_add(o, "layers", to_json(g_wayland->controller->lprops));
+// json_object_object_add(o, "screens",
+// to_json(g_wayland->controller->screens));
- if (!g_wayland->controller->layers.empty()) {
- auto a = json::array();
- for (auto const &i : g_wayland->controller->layers) {
- auto const &r = i.second->dst_rect;
- auto const &s = i.second->size;
- a.push_back({{"id", i.first},
- {"size", {s.w, s.h}},
- {"dst_rect", {r.w, r.h, r.x, r.y}}});
- }
- j["layers"] = a;
- }
+ afb_req_success(req, o, "status");
+}
- afb_req_success(req, json_tokener_parse(j.dump().c_str()), "status");
- } catch (std::exception &e) {
- afb_req_fail_f(req, "failed", "Uncaught exception: %s", e.what());
- }
+void debug_surfaces(afb_req req) {
+ afb_req_success(req, to_json(g_wayland->controller->sprops), "surfaces");
}
-void debug_surfaces(afb_req req) noexcept {
- CHECK_WAYLAND();
+void debug_layers(afb_req req) {
+ afb_req_success(req, to_json(g_wayland->controller->lprops), "layers");
+}
- auto a = json_object_new_array();
+// Dummy register_surface implementation
+void register_surface(afb_req req) {
+ AFB_DEBUG("register_surface");
- if (!g_wayland->controller->surfaces.empty()) {
- for (auto const &i : g_wayland->controller->surfaces) {
- json_object_array_add(a, json_object_new_int(i.first));
- }
+ auto jo = afb_req_json(req);
+ json_object *jappid;
+ if (! json_object_object_get_ex(jo, "appid", &jappid)) {
+ afb_req_fail(req, "failed", "register_surface needs 'appid' integer argument");
+ return;
}
- afb_req_success(req, a, "surfaces");
-}
+ json_object *jsurfid;
+ if (! json_object_object_get_ex(jo, "surfaceid", &jsurfid)) {
+ afb_req_fail(req, "failed", "register_surface needs 'surfaceid' integer argument");
+ return;
+ }
-void debug_layers(afb_req req) noexcept {
- CHECK_WAYLAND();
+ int32_t appid = json_object_get_int(jappid);
+ int32_t surfid = json_object_get_int(jsurfid);
- auto a = json_object_new_array();
+ if (appid < 0 || appid > 0xff) {
+ afb_req_fail(req, "failed", "invalid appid");
+ return;
+ }
- if (!g_wayland->controller->layers.empty()) {
- for (auto const &i : g_wayland->controller->layers) {
- json_object_array_add(a, json_object_new_int(i.first));
- }
+ if (surfid < 0 || surfid > 0xffff) {
+ afb_req_fail(req, "failed", "invalid surfaceid");
+ return;
}
- afb_req_success(req, a, "surfaces");
+ lognotice("register_surface, got appid %d and surfaceid %d", appid, surfid);
+
+ afb_req_success(req, json_object_new_int((appid << 16) + surfid), "success");
}
+#define WRAP(F) \
+ [](afb_req req) noexcept { \
+ if (g_wayland == nullptr) { \
+ afb_req_fail(req, "failed", \
+ "Binding not initialized, did the compositor die?"); \
+ return; \
+ } \
+ try { \
+ F(req); \
+ } catch (std::exception & e) { \
+ afb_req_fail_f(req, "failed", "Uncaught exception: %s", e.what()); \
+ } \
+ }
+
const struct afb_verb_v2 verbs[] = {
- {"status", debug_status, NULL, NULL, AFB_SESSION_NONE_V2},
- {"layers", debug_layers, NULL, NULL, AFB_SESSION_NONE_V2},
- {"surfaces", debug_surfaces, NULL, NULL, AFB_SESSION_NONE_V2},
- {},
+ {"debug::status", WRAP(debug_status), NULL, NULL, AFB_SESSION_NONE_V2},
+ {"debug::layers", WRAP(debug_layers), NULL, NULL, AFB_SESSION_NONE_V2},
+ {"debug::surfaces", WRAP(debug_surfaces), NULL, NULL, AFB_SESSION_NONE_V2},
+
+ {"register_surface", WRAP(register_surface), NULL, NULL, AFB_SESSION_NONE_V2},
+ {}
};
} // namespace