+void WindowManager::api_enddraw(char const *appid, char const *drawing_name)
+{
+ std::string id = appid;
+ std::string role = drawing_name;
+ unsigned current_req = g_app_list.currentRequestNumber();
+ bool result = g_app_list.setEndDrawFinished(current_req, id, role);
+
+ if (!result)
+ {
+ HMI_ERROR("wm", "%s is not in transition state", id.c_str());
+ return;
+ }
+
+ if (g_app_list.endDrawFullfilled(current_req))
+ {
+ // do task for endDraw
+ this->stopTimer();
+ WMError ret = this->doEndDraw(current_req);
+
+ if(ret != WMError::SUCCESS)
+ {
+ //this->emit_error();
+ }
+ HMI_SEQ_INFO(current_req, "Finish request status: %s", errorDescription(ret));
+
+ g_app_list.removeRequest(current_req);
+
+ this->processNextRequest();
+ }
+ else
+ {
+ HMI_SEQ_INFO(current_req, "Wait other App call endDraw");
+ return;
+ }
+}
+
+result<json_object *> WindowManager::api_get_display_info()
+{
+ // Check controller
+ if (!this->controller)
+ {
+ return Err<json_object *>("ivi_controller global not available");
+ }
+
+ // Set display info
+ compositor::size o_size = this->controller->output_size;
+ compositor::size p_size = this->controller->physical_size;
+
+ json_object *object = json_object_new_object();
+ json_object_object_add(object, kKeyWidthPixel, json_object_new_int(o_size.w));
+ json_object_object_add(object, kKeyHeightPixel, json_object_new_int(o_size.h));
+ json_object_object_add(object, kKeyWidthMm, json_object_new_int(p_size.w));
+ json_object_object_add(object, kKeyHeightMm, json_object_new_int(p_size.h));
+
+ return Ok<json_object *>(object);
+}
+
+result<json_object *> WindowManager::api_get_area_info(char const *drawing_name)
+{
+ HMI_DEBUG("wm", "called");
+
+ // Check drawing name, surface/layer id
+ auto const &surface_id = this->lookup_id(drawing_name);
+ if (!surface_id)
+ {
+ return Err<json_object *>("Surface does not exist");
+ }
+
+ if (!this->controller->surface_exists(*surface_id))
+ {
+ return Err<json_object *>("Surface does not exist in controller!");
+ }
+
+ auto layer_id = this->layers.get_layer_id(*surface_id);
+ if (!layer_id)
+ {
+ return Err<json_object *>("Surface is not on any layer!");
+ }
+
+ auto o_state = *this->layers.get_layout_state(*surface_id);
+ if (o_state == nullptr)
+ {
+ return Err<json_object *>("Could not find layer for surface");
+ }
+
+ struct LayoutState &state = *o_state;
+ if ((state.main != *surface_id) && (state.sub != *surface_id))
+ {
+ return Err<json_object *>("Surface is inactive");
+ }
+
+ // Set area rectangle
+ compositor::rect area_info = this->area_info[*surface_id];
+ json_object *object = json_object_new_object();
+ json_object_object_add(object, kKeyX, json_object_new_int(area_info.x));
+ json_object_object_add(object, kKeyY, json_object_new_int(area_info.y));
+ json_object_object_add(object, kKeyWidth, json_object_new_int(area_info.w));
+ json_object_object_add(object, kKeyHeight, json_object_new_int(area_info.h));
+
+ return Ok<json_object *>(object);
+}
+
+void WindowManager::api_ping() { this->dispatch_pending_events(); }
+
+void WindowManager::send_event(char const *evname, char const *label)
+{
+ HMI_DEBUG("wm", "%s: %s(%s)", __func__, evname, label);
+
+ json_object *j = json_object_new_object();
+ json_object_object_add(j, kKeyDrawingName, json_object_new_string(label));
+
+ int ret = afb_event_push(this->map_afb_event[evname], j);
+ if (ret != 0)
+ {
+ HMI_DEBUG("wm", "afb_event_push failed: %m");
+ }
+}
+
+void WindowManager::send_event(char const *evname, char const *label, char const *area,
+ int x, int y, int w, int h)
+{
+ HMI_DEBUG("wm", "%s: %s(%s, %s) x:%d y:%d w:%d h:%d",
+ __func__, evname, label, area, x, y, w, h);
+
+ json_object *j_rect = json_object_new_object();
+ json_object_object_add(j_rect, kKeyX, json_object_new_int(x));
+ json_object_object_add(j_rect, kKeyY, json_object_new_int(y));
+ json_object_object_add(j_rect, kKeyWidth, json_object_new_int(w));
+ json_object_object_add(j_rect, kKeyHeight, json_object_new_int(h));
+
+ json_object *j = json_object_new_object();
+ json_object_object_add(j, kKeyDrawingName, json_object_new_string(label));
+ json_object_object_add(j, kKeyDrawingArea, json_object_new_string(area));
+ json_object_object_add(j, kKeyDrawingRect, j_rect);
+
+ int ret = afb_event_push(this->map_afb_event[evname], j);
+ if (ret != 0)
+ {
+ HMI_DEBUG("wm", "afb_event_push failed: %m");
+ }
+}
+
+/**
+ * proxied events
+ */
+void WindowManager::surface_created(uint32_t surface_id)
+{
+ auto layer_id = this->layers.get_layer_id(surface_id);
+ if (!layer_id)
+ {
+ HMI_DEBUG("wm", "Newly created surfce %d is not associated with any layer!",
+ surface_id);
+ return;
+ }
+
+ HMI_DEBUG("wm", "surface_id is %u, layer_id is %u", surface_id, *layer_id);
+
+ this->controller->layers[*layer_id]->add_surface(surface_id);
+ this->layout_commit();
+}
+
+void WindowManager::surface_removed(uint32_t surface_id)
+{
+ HMI_DEBUG("wm", "surface_id is %u", surface_id);
+
+ g_app_list.removeSurface(surface_id);
+}
+
+void WindowManager::removeClient(const std::string &appid)
+{
+ HMI_DEBUG("wm", "Remove clinet %s from list", appid.c_str());
+ g_app_list.removeClient(appid);
+}
+
+void WindowManager::exceptionProcessForTransition()
+{
+ unsigned req_num = g_app_list.currentRequestNumber();
+ HMI_SEQ_NOTICE(req_num, "Process exception handling for request. Remove current request %d", req_num);
+ g_app_list.removeRequest(req_num);
+ HMI_SEQ_NOTICE(g_app_list.currentRequestNumber(), "Process next request if exists");
+ this->processNextRequest();
+}
+
+bool WindowManager::subscribeEventForApp(const std::string &appid, afb_req req, const std::string &evname)
+{
+ if(!g_app_list.contains(appid)){
+ HMI_DEBUG("wm", "Client %s is not registered", appid.c_str());
+ return false;
+ }
+ auto client = g_app_list.lookUpClient(appid);
+ return client->subscribe(req, evname);
+}
+
+void WindowManager::timerHandler()
+{
+ unsigned req_num = g_app_list.currentRequestNumber();
+ HMI_SEQ_DEBUG(req_num, "Timer expired remove Request");
+ g_app_list.reqDump();
+ g_app_list.removeRequest(req_num);
+ this->processNextRequest();
+}
+
+/*
+ ******* Private Functions *******
+ */
+
+bool WindowManager::pop_pending_events()
+{
+ bool x{true};
+ return this->pending_events.compare_exchange_strong(
+ x, false, std::memory_order_consume);
+}
+
+optional<int> WindowManager::lookup_id(char const *name)
+{
+ return this->id_alloc.lookup(std::string(name));
+}
+optional<std::string> WindowManager::lookup_name(int id)
+{
+ return this->id_alloc.lookup(id);
+}
+
+/**
+ * init_layers()
+ */
+int WindowManager::init_layers()
+{
+ if (!this->controller)
+ {
+ HMI_ERROR("wm", "ivi_controller global not available");
+ return -1;
+ }
+
+ if (this->outputs.empty())
+ {
+ HMI_ERROR("wm", "no output was set up!");
+ return -1;
+ }
+
+ auto &c = this->controller;
+
+ auto &o = this->outputs.front();
+ auto &s = c->screens.begin()->second;
+ auto &layers = c->layers;
+
+ // Write output dimensions to ivi controller...
+ c->output_size = compositor::size{uint32_t(o->width), uint32_t(o->height)};
+ c->physical_size = compositor::size{uint32_t(o->physical_width),
+ uint32_t(o->physical_height)};
+
+ // Clear scene
+ layers.clear();
+
+ // Clear screen
+ s->clear();
+
+ // Quick and dirty setup of layers
+ for (auto const &i : this->layers.mapping)
+ {
+ c->layer_create(i.second.layer_id, o->width, o->height);
+ auto &l = layers[i.second.layer_id];
+ l->set_destination_rectangle(0, 0, o->width, o->height);
+ l->set_visibility(1);
+ HMI_DEBUG("wm", "Setting up layer %s (%d) for surface role match \"%s\"",
+ i.second.name.c_str(), i.second.layer_id, i.second.role.c_str());
+ }
+
+ // Add layers to screen
+ s->set_render_order(this->layers.layers);
+
+ this->layout_commit();
+
+ this->layers.setupArea(o->width, o->height);
+
+ return 0;
+}
+
+void WindowManager::surface_set_layout(int surface_id, const std::string& area)
+{
+ if (!this->controller->surface_exists(surface_id))
+ {
+ HMI_ERROR("wm", "Surface %d does not exist", surface_id);
+ return;
+ }
+
+ auto o_layer_id = this->layers.get_layer_id(surface_id);
+
+ if (!o_layer_id)
+ {
+ HMI_ERROR("wm", "Surface %d is not associated with any layer!", surface_id);
+ return;
+ }
+
+ uint32_t layer_id = *o_layer_id;
+
+ auto const &layer = this->layers.get_layer(layer_id);
+ auto rect = this->layers.getAreaSize(area);
+ HMI_SEQ_DEBUG(g_app_list.currentRequestNumber(), "%s : x:%d y:%d w:%d h:%d", area.c_str(),
+ rect.x, rect.y, rect.w, rect.h);
+ auto &s = this->controller->surfaces[surface_id];
+
+ int x = rect.x;
+ int y = rect.y;
+ int w = rect.w;
+ int h = rect.h;
+
+ // less-than-0 values refer to MAX + 1 - $VALUE
+ // e.g. MAX is either screen width or height
+ if (w < 0)
+ {
+ w = this->controller->output_size.w + 1 + w;
+ }
+ if (h < 0)
+ {
+ h = this->controller->output_size.h + 1 + h;
+ }
+
+ HMI_DEBUG("wm", "surface_set_layout for surface %u on layer %u", surface_id,
+ layer_id);
+
+ // set destination to the display rectangle
+ s->set_destination_rectangle(x, y, w, h);
+
+ // update area information
+ this->area_info[surface_id].x = x;
+ this->area_info[surface_id].y = y;
+ this->area_info[surface_id].w = w;
+ this->area_info[surface_id].h = h;
+
+ HMI_DEBUG("wm", "Surface %u now on layer %u with rect { %d, %d, %d, %d }",
+ surface_id, layer_id, x, y, w, h);
+}
+
+void WindowManager::layout_commit()