+ this->setTimer();
+}
+
+void WindowManager::api_deactivate_window(char const *appid, char const *drawing_name,
+ const reply_func &reply)
+{
+ // TODO: application requests by old role,
+ // so convert role old to new
+ const char *c_role = this->convertRoleOldToNew(drawing_name);
+
+ /*
+ * Check Phase
+ */
+ string id = appid;
+ string role = c_role;
+ string area = ""; //drawing_area;
+ Task task = Task::TASK_RELEASE;
+ unsigned req_num = 0;
+ WMError ret = WMError::UNKNOWN;
+
+ ret = this->setRequest(id, role, area, task, &req_num);
+
+ if (ret != WMError::SUCCESS)
+ {
+ HMI_ERROR(errorDescription(ret));
+ reply("Failed to set request");
+ return;
+ }
+
+ reply(nullptr);
+ if (req_num != g_app_list.currentRequestNumber())
+ {
+ // Add request, then invoked after the previous task is finished
+ HMI_SEQ_DEBUG(req_num, "request is accepted");
+ return;
+ }
+
+ /*
+ * Do allocate tasks
+ */
+ ret = this->checkPolicy(req_num);
+
+ if (ret != WMError::SUCCESS)
+ {
+ //this->emit_error()
+ HMI_SEQ_ERROR(req_num, errorDescription(ret));
+ g_app_list.removeRequest(req_num);
+ this->processNextRequest();
+ }
+}
+
+void WindowManager::api_deactivate_surface_for_slave(char const *appid, char const *drawing_name,
+ const reply_func &reply)
+{
+ // TODO: application requests by old role,
+ // so convert role old to new
+ const char *c_role = this->convertRoleOldToNew(drawing_name);
+
+ /*
+ * Check Phase
+ */
+ string id = appid;
+ string role = c_role;
+ string area = "";//drawing_area;
+ Task task = Task::TASK_RELEASE;
+ unsigned req_num = 0;
+ WMError ret = WMError::UNKNOWN;
+
+ ret = this->setRequest(id, role, area, task, &req_num);
+
+ if (ret != WMError::SUCCESS)
+ {
+ HMI_ERROR(errorDescription(ret));
+ reply("Failed to set request");
+ return;
+ }
+
+ reply(nullptr);
+ if (req_num != g_app_list.currentRequestNumber())
+ {
+ // Add request, then invoked after the previous task is finished
+ HMI_SEQ_DEBUG(req_num, "request is accepted");
+ return;
+ }
+
+ /*
+ * Do allocate tasks
+ */
+ ret = this->checkPolicyForSlave(req_num);
+
+ if (ret != WMError::SUCCESS)
+ {
+ //this->emit_error()
+ HMI_SEQ_ERROR(req_num, errorDescription(ret));
+ g_app_list.removeRequest(req_num);
+ this->processNextRequest();
+ }
+}
+
+void WindowManager::api_deactivate_surface_to_master(char const *appid, char const *drawing_name,
+ const reply_func &reply)
+{
+ // TODO: application requests by old role,
+ // so convert role old to new
+ const char *c_role = this->convertRoleOldToNew(drawing_name);
+
+ /*
+ * Check Phase
+ */
+ string id = appid;
+ string role = c_role;
+ string area = "";//drawing_area;
+ Task task = Task::TASK_RELEASE;
+ unsigned req_num = 0;
+ WMError ret = WMError::UNKNOWN;
+
+ ret = this->setRequest(id, role, area, task, &req_num);
+
+ if (ret != WMError::SUCCESS)
+ {
+ HMI_ERROR(errorDescription(ret));
+ reply("Failed to set request");
+ return;
+ }
+
+ reply(nullptr);
+ if (req_num != g_app_list.currentRequestNumber())
+ {
+ // Add request, then invoked after the previous task is finished
+ HMI_SEQ_DEBUG(req_num, "request is accepted");
+ return;
+ }
+
+ /*
+ * Do allocate tasks
+ */
+ int i_ret = this->wmcon.sendRequest("deactivateWindow", appid,
+ drawing_name, "");
+ if (0 > i_ret)
+ {
+ //this->emit_error()
+ HMI_SEQ_ERROR(req_num, errorDescription(ret));
+ g_app_list.removeRequest(req_num);
+ this->processNextRequest();
+ }
+
+ this->setTimer();
+}
+
+void WindowManager::api_enddraw(char const *appid, char const *drawing_name)
+{
+ // TODO: application requests by old role,
+ // so convert role old to new
+ const char *c_role = this->convertRoleOldToNew(drawing_name);
+
+ string id = appid;
+ string role = c_role;
+ unsigned current_req = g_app_list.currentRequestNumber();
+ bool result = g_app_list.setEndDrawFinished(current_req, id, role);
+
+ if (!result)
+ {
+ HMI_ERROR("%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();
+
+ // Undo state of PolicyManager
+ this->pmw.undoState();
+ this->lc->undoUpdate();
+ }
+ this->emitScreenUpdated(current_req);
+ HMI_SEQ_INFO(current_req, "Finish request status: %s", errorDescription(ret));
+
+ this->saveLastModeData(current_req);
+
+ g_app_list.removeRequest(current_req);
+
+ this->processNextRequest();
+ }
+ else
+ {
+ HMI_SEQ_INFO(current_req, "Wait other App call endDraw");
+ return;
+ }
+}
+
+int WindowManager::api_subscribe(afb_req req, int event_id)
+{
+ struct afb_event event = this->map_afb_event[kListEventName[event_id]];
+ return afb_req_subscribe(req, event);
+}
+
+void WindowManager::api_handshake()
+{
+ this->send_event(kListEventName[Event_Handshake]);
+}
+
+void WindowManager::api_enddraw_for_remote(char const *appid, char const *drawing_name)
+{
+ int ret = this->wmcon.sendRequest("endDraw", appid, drawing_name, "");
+ if (0 > ret)
+ {
+ //this->emit_error()
+
+ this->pmw.undoState();
+ this->lc->undoUpdate();
+
+ unsigned current_req = g_app_list.currentRequestNumber();
+ g_app_list.removeRequest(current_req);
+ this->processNextRequest();
+
+ return;
+ }
+
+ this->api_enddraw(appid, drawing_name);
+}
+
+bool WindowManager::api_client_set_render_order(char const* appid, const vector<string>& render_order)
+{
+ bool ret = false;
+ string id = appid;
+ auto client = g_app_list.lookUpClient(id);
+ if(client)
+ {
+ client->setRenderOrder(render_order);
+ }
+ return ret;
+}
+
+string WindowManager::api_client_attach_service_surface
+ (const char* appid, const char* dest, const char* service_surface)
+{
+ string uuid, s_dest = dest;
+ auto client = g_app_list.lookUpClient(s_dest);
+ if(!client)
+ {
+ HMI_ERROR("Failed to look up destination [%s]", dest);
+ return uuid;
+ }
+ uuid = client->attachTmpServiceSurface(appid, service_surface);
+ this->tmp_services.emplace_back(TmpService{appid, dest, service_surface, uuid});
+ return uuid;
+}
+
+json_object* WindowManager::api_get_area_list()
+{
+ json_object* ret = json_object_new_object();
+ json_object* jarray = json_object_new_array();
+ unordered_map<string, struct rect> area2size = this->lc->getAreaList();
+ for(const auto& area : area2size)
+ {
+ json_object* j = json_object_new_object();
+ json_object_object_add(j, "name", json_object_new_string(area.first.c_str()));
+ json_object* jrect = json_object_new_object();
+ json_object_object_add(jrect, "x", json_object_new_int(area.second.x));
+ json_object_object_add(jrect, "y", json_object_new_int(area.second.y));
+ json_object_object_add(jrect, "w", json_object_new_int(area.second.w));
+ json_object_object_add(jrect, "h", json_object_new_int(area.second.h));
+ json_object_object_add(j, "rect", jrect);
+ json_object_array_add(jarray, j);
+ }
+ json_object_object_add(ret, "areas", jarray);
+ HMI_DEBUG("area_list: %s", json_object_get_string(ret));
+ return ret;
+}
+
+void WindowManager::api_change_area_size(ChangeAreaReq &areas)
+{
+ // Error check
+ areas.dump();
+ auto client = g_app_list.lookUpClient(areas.appname);
+ WMError ret;
+ if(client == nullptr)
+ {
+ HMI_ERROR("Call register your role with setRole or requestSurface");
+ return;
+ }
+ if(std::find(white_list_area_size_change.begin(),
+ white_list_area_size_change.end(), client->role()) == white_list_area_size_change.end())
+ {
+ HMI_ERROR("Application %s which has the role %s is not allowed to change area size", client->appID().c_str(), client->role().c_str());
+ return;
+ }
+
+ // Update
+ ret = this->lc->updateAreaList(areas);
+ if(ret != WMError::SUCCESS)
+ {
+ HMI_ERROR("%d : %s", ret, errorDescription(ret));
+ return;
+ }
+ ret = this->lc->getUpdateAreaList(&areas);
+ areas.dump();
+ if(ret != WMError::SUCCESS)
+ {
+ HMI_ERROR("%d : %s", ret, errorDescription(ret));
+ return;
+ }
+
+ // Create Action
+ unsigned req_num;
+ bool found = false;
+ ret = this->setRequest(client->appID(), client->role(), "-", Task::TASK_CHANGE_AREA, &req_num); // area is null
+ if(ret != WMError::SUCCESS)
+ {
+ HMI_SEQ_ERROR(req_num, "%d : %s", ret, errorDescription(ret));
+ return;
+ }
+ for(const auto &update: areas.update_app2area)
+ {
+ // create action
+ auto client = g_app_list.lookUpClient(update.first);
+ if(client == nullptr)
+ {
+ HMI_SEQ_ERROR(req_num, "%s : %s", update.first.c_str(), errorDescription(ret));
+ g_app_list.removeRequest(req_num);
+ this->processNextRequest();
+ return;
+ }
+ ret = g_app_list.setAction(req_num, client, client->role(), update.second, TaskVisible::VISIBLE);
+ if(ret != WMError::SUCCESS)
+ {
+ HMI_SEQ_ERROR(req_num, "Failed to set request");
+ return;
+ }
+ }
+ HMI_SEQ_INFO(req_num, "Area change request");
+ g_app_list.reqDump();
+
+ // Request change size to applications
+ for(const auto &action : g_app_list.getActions(req_num, &found))
+ {
+ string old_role = this->rolenew2old[action.role];
+ this->emit_syncdraw(old_role, action.area);
+ }
+}
+
+result<json_object *> WindowManager::api_get_display_info()
+{
+ Screen screen = this->lc->getScreenInfo();
+
+ json_object *object = json_object_new_object();
+ json_object_object_add(object, kKeyWidthPixel, json_object_new_int(screen.width()));
+ json_object_object_add(object, kKeyHeightPixel, json_object_new_int(screen.height()));
+ // TODO: set size
+ json_object_object_add(object, kKeyWidthMm, json_object_new_int(0));
+ json_object_object_add(object, kKeyHeightMm, json_object_new_int(0));
+ json_object_object_add(object, kKeyScale, json_object_new_double(this->lc->scale()));
+
+ return Ok<json_object *>(object);
+}
+
+result<json_object *> WindowManager::api_get_area_info(char const *drawing_name)
+{
+ HMI_DEBUG("called");
+
+ // TODO: application requests by old role,
+ // so convert role old to new
+ const char *role = this->convertRoleOldToNew(drawing_name);
+
+ // Check drawing name, surface/layer id
+ auto const &surface_id = this->id_alloc.lookup(role);
+ if (!surface_id)
+ {
+ return Err<json_object *>("Surface does not exist");
+ }
+
+ // Set area rectangle
+ rect area_info = this->area_info[*surface_id];