+int App::allocateSurface() {
+ HMI_DEBUG("wm", "Call");
+
+ // Get current/previous layers
+ LayoutManager::TypeLayers crr_layers = this->lm_.getCurrentLayers();
+ LayoutManager::TypeLayers prv_layers = this->lm_.getPreviousLayers();
+
+ // Update resource of all layers
+ for (auto itr_layers = crr_layers.begin();
+ itr_layers != crr_layers.end(); ++itr_layers) {
+ // Get layer
+ std::string layer = itr_layers->first;
+ HMI_DEBUG("wm", "Try to update resource in %s layer", layer.c_str());
+
+ // If layout is changed, update resouce
+ if (this->lm_.isLayoutChanged(layer.c_str())) {
+ // Get current/previous layout
+ LayoutManager::TypeLayouts crr_layout = itr_layers->second;
+ LayoutManager::TypeLayouts prv_layout = prv_layers[layer];
+
+ // Get current/previous layout name
+ std::string crr_layout_name = crr_layout.begin()->first;
+ std::string prv_layout_name = prv_layout.begin()->first;
+ HMI_DEBUG("wm", "layout name crr:%s prv:%s",
+ crr_layout_name.c_str(), prv_layout_name.c_str());
+
+ // Get current/previous ares
+ LayoutManager::TypeAreas crr_areas = crr_layout[crr_layout_name];
+ LayoutManager::TypeAreas prv_areas = prv_layout[prv_layout_name];
+
+ // Create previous displayed role list
+ std::string prv_area_name;
+ std::vector<std::string> prv_role_list;
+ for (auto itr_areas = prv_areas.begin();
+ itr_areas != prv_areas.end(); ++itr_areas) {
+ prv_area_name = itr_areas->first;
+ prv_role_list.push_back(prv_areas[prv_area_name]["role"]);
+ HMI_DEBUG("wm", "previous displayed role:%s",
+ prv_areas[prv_area_name]["role"].c_str());
+ }
+
+ // Allocate surface for each area
+ std::string crr_area_name;
+ std::string crr_role_name;
+ LayoutManager::TypeRolCtg crr_rol_ctg;
+ for (auto itr_areas = crr_areas.begin();
+ itr_areas != crr_areas.end(); ++itr_areas) {
+ crr_area_name = itr_areas->first;
+ crr_rol_ctg = itr_areas->second;
+
+ // Get role of current area
+ if ("category" == crr_rol_ctg.begin()->first) {
+ // If current area have category
+ // Get category name
+ std::string crr_ctg = crr_rol_ctg.begin()->second;
+
+ // Serch relevant role from previous displayed role list
+ for (auto itr_role = prv_role_list.begin();
+ itr_role != prv_role_list.end(); ++itr_role) {
+ std::string prv_ctg = this->pm_.roleToCategory((*itr_role).c_str());
+ if (crr_ctg == prv_ctg) {
+ // First discovered role is set to current role
+ crr_role_name = *itr_role;
+
+ // Delete used role for other areas
+ // which have same category
+ prv_role_list.erase(itr_role);
+
+ break;
+ }
+ }
+ }
+ else {
+ crr_role_name = itr_areas->second["role"];
+ }
+ HMI_DEBUG("wm", "Allocate surface for area:%s role:%s",
+ crr_area_name.c_str(), crr_role_name.c_str());
+
+ // Deactivate non-displayed role
+ std::string prv_role_name;
+ if (crr_layout_name == prv_layout_name) {
+ HMI_DEBUG("wm", "Current layout is same with previous");
+
+ // Deactivate previous role in same area
+ // if it is different with current
+ prv_role_name = prv_areas[crr_area_name]["role"];
+ if (crr_role_name != prv_role_name) {
+ this->deactivate(prv_role_name);
+ }
+ }
+ else {
+ HMI_DEBUG("wm", "Current layout is different with previous");
+
+ if ("none" != prv_layout_name) {
+ // Deactivate previous role in all area in previous layout
+ // if it is different with current role
+ for(auto itr = prv_areas.begin(); itr != prv_areas.end(); ++itr) {
+ prv_role_name = itr->second["role"].c_str();
+ if (crr_role_name != prv_role_name) {
+ this->deactivate(prv_role_name);
+ }
+ }
+ }
+ }
+
+ // Set surface for displayed role
+ if ("none" != crr_layout_name) {
+ // If current layout is not "none",
+ // set surface for current role
+ this->setSurfaceSize(crr_role_name.c_str(), crr_area_name.c_str());
+
+ // TODO:
+ // This API is workaround.
+ // Resource manager should manage each resource infomations
+ // according to architecture document.
+ this->lm_.updateArea(layer.c_str(), crr_role_name.c_str(), crr_area_name.c_str());
+ }
+ }
+ }
+ }
+ return 0;
+}
+
+void App::setSurfaceSize(const char* role, const char* area) {
+ HMI_DEBUG("wm", "role:%s area:%s", role, area);
+
+ // Get size of current area
+ compositor::rect size = this->lm_.getAreaSize(area);
+
+ // Set destination to the display rectangle
+ int surface_id = this->role2surfaceid_[role];
+
+ if (!this->controller->surface_exists(surface_id)) {
+ // Block until all pending request are processed by wayland display server
+ // because waiting for the surface of new app is created
+ this->display->roundtrip();
+ }
+ auto &s = this->controller->surfaces[surface_id];
+ s->set_destination_rectangle(size.x, size.y, size.w, size.h);
+ this->layout_commit();
+
+ // Update area information
+ this->area_info[surface_id].x = size.x;
+ this->area_info[surface_id].y = size.y;
+ this->area_info[surface_id].w = size.w;
+ this->area_info[surface_id].h = size.h;
+ HMI_DEBUG("wm", "Surface rect { %d, %d, %d, %d }",
+ size.x, size.y, size.w, size.h);
+
+ // Emit syncDraw event
+ const char* app = this->role2app_[role].c_str();
+ this->emit_syncdraw(app, area,
+ size.x, size.y, size.w, size.h);
+
+ // Enqueue flushDraw event
+ this->enqueue_flushdraw(surface_id);
+}
+
+void App::setAccelPedalPos(double val) {
+ this->crr_car_info_.accel_pedal_pos = val;
+}
+
+extern const char* kDefaultAppDb;
+int App::loadAppDb() {
+ HMI_DEBUG("wm", "Call");
+
+ // Get afm application installed dir
+ char const *afm_app_install_dir = getenv("AFM_APP_INSTALL_DIR");
+ HMI_DEBUG("wm", "afm_app_install_dir:%s", afm_app_install_dir);
+
+ std::string file_name;
+ if (!afm_app_install_dir) {
+ HMI_ERROR("wm", "AFM_APP_INSTALL_DIR is not defined");
+ }
+ else {
+ file_name = std::string(afm_app_install_dir) + std::string("/etc/app.db");
+ }
+
+ // Load app.db
+ json_object* json_obj;
+ int ret = jh::inputJsonFilie(file_name.c_str(), &json_obj);
+ if (0 > ret) {
+ HMI_ERROR("wm", "Could not open app.db, so use default role information");
+ json_obj = json_tokener_parse(kDefaultAppDb);
+ }
+ HMI_DEBUG("wm", "json_obj dump:%s", json_object_get_string(json_obj));
+
+ // Perse apps
+ HMI_DEBUG("wm", "Perse apps");
+ json_object* json_cfg;
+ if (!json_object_object_get_ex(json_obj, "apps", &json_cfg)) {
+ HMI_ERROR("wm", "Parse Error!!");
+ return -1;
+ }
+
+ int len = json_object_array_length(json_cfg);
+ HMI_DEBUG("wm", "json_cfg len:%d", len);
+ HMI_DEBUG("wm", "json_cfg dump:%s", json_object_get_string(json_cfg));
+
+ for (int i=0; i<len; i++) {
+ json_object* json_tmp = json_object_array_get_idx(json_cfg, i);
+ HMI_DEBUG("wm", "> json_tmp dump:%s", json_object_get_string(json_tmp));
+
+ const char* app = jh::getStringFromJson(json_tmp, "name");
+ if (nullptr == app) {
+ HMI_ERROR("wm", "Parse Error!!");
+ return -1;
+ }
+ HMI_DEBUG("wm", "> app:%s", app);
+
+ const char* role = jh::getStringFromJson(json_tmp, "role");
+ if (nullptr == role) {
+ HMI_ERROR("wm", "Parse Error!!");
+ return -1;
+ }
+ HMI_DEBUG("wm", "> role:%s", role);
+
+ this->app2role_[app] = std::string(role);
+ }
+
+ // Check
+ for(auto itr = this->app2role_.begin();
+ itr != this->app2role_.end(); ++itr) {
+ HMI_DEBUG("wm", "app:%s role:%s",
+ itr->first.c_str(), itr->second.c_str());
+ }
+
+ // Release json_object
+ json_object_put(json_obj);
+
+ return 0;
+}
+
+
+const char* kDefaultAppDb = "{ \
+ \"apps\": [ \
+ { \
+ \"name\": \"HomeScreen\", \
+ \"role\": \"homescreen\" \
+ }, \
+ { \
+ \"name\": \"Music\", \
+ \"role\": \"music\" \
+ }, \
+ { \
+ \"name\": \"MediaPlayer\", \
+ \"role\": \"music\" \
+ }, \
+ { \
+ \"name\": \"Video\", \
+ \"role\": \"video\" \
+ }, \
+ { \
+ \"name\": \"VideoPlayer\", \
+ \"role\": \"video\" \
+ }, \
+ { \
+ \"name\": \"WebBrowser\", \
+ \"role\": \"browser\" \
+ }, \
+ { \
+ \"name\": \"Radio\", \
+ \"role\": \"radio\" \
+ }, \
+ { \
+ \"name\": \"Phone\", \
+ \"role\": \"phone\" \
+ }, \
+ { \
+ \"name\": \"Navigation\", \
+ \"role\": \"map\" \
+ }, \
+ { \
+ \"name\": \"HVAC\", \
+ \"role\": \"hvac\" \
+ }, \
+ { \
+ \"name\": \"Settings\", \
+ \"role\": \"settings\" \
+ }, \
+ { \
+ \"name\": \"Dashboard\", \
+ \"role\": \"dashboard\" \
+ }, \
+ { \
+ \"name\": \"POI\", \
+ \"role\": \"poi\" \
+ }, \
+ { \
+ \"name\": \"Mixer\", \
+ \"role\": \"mixer\" \
+ } \
+ ] \
+}";
+
+