2 * Copyright (c) 2017 TOYOTA MOTOR CORPORATION
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 #include "wm_layer_control.hpp"
19 #include "wm_layer.hpp"
20 #include "wm_client.hpp"
21 #include "request.hpp"
22 #include "json_helper.hpp"
24 #define LC_AREA_PATH "/etc/areas.json"
25 #define LC_LAYER_SETTING_PATH "/etc/layers_setting.json"
26 #define LC_DEFAULT_AREA "fullscreen"
27 #define BACK_GROUND_LAYER "BackGroundLayer"
31 using std::shared_ptr;
35 LayerControl* g_lc_ctxt;
37 static void createCallback_static(ilmObjectType object,
42 static_cast<LayerControl*>(data)->dispatchCreateEvent(object, id, created);
45 static void surfaceCallback_static(t_ilm_surface surface,
46 struct ilmSurfaceProperties* surface_prop,
47 t_ilm_notification_mask mask)
49 g_lc_ctxt->dispatchSurfacePropChangeEvent(surface, surface_prop, mask);
52 static void layerCallback_static(t_ilm_layer layer,
53 struct ilmLayerProperties* layer_prop,
54 t_ilm_notification_mask mask)
56 g_lc_ctxt->dispatchLayerPropChangeEvent(layer, layer_prop, mask);
59 LayerControl::LayerControl(const std::string& root, const std::string& ecu_name)
61 string area_path = root + LC_AREA_PATH;
62 string layer_path= root + LC_LAYER_SETTING_PATH;
63 // load layers.setting.json
64 WMError ret = this->loadLayerSetting(layer_path);
65 assert(ret == WMError::SUCCESS);
67 ret = this->loadAreasConfigFile(area_path, ecu_name);
68 assert(ret == WMError::SUCCESS);
71 WMError LayerControl::init(const LayerControlCallbacks& cb)
73 HMI_DEBUG("Initialize of ilm library and display");
77 ilmErrorTypes rc = ilm_init();
79 while (rc != ILM_SUCCESS)
84 HMI_ERROR("Could not connect to compositor");
87 HMI_ERROR("Wait to start weston ...");
91 if(rc != ILM_SUCCESS) goto lc_init_error;
93 // Get current screen setting
94 rc = ilm_getScreenIDs(&num, &ids);
96 if(rc != ILM_SUCCESS) goto lc_init_error;
98 for(unsigned i = 0; i < num; i++)
100 HMI_INFO("get screen: %d", ids[i]);
102 // Currently, 0 is only available
103 this->screenID = ids[0];
107 // TODO: set remote screen id
108 HMI_INFO("There is remote screen (id:%d)", ids[1]);
109 this->remoteScreenID = ids[1];
113 HMI_INFO("There is no remote screen");
114 this->remoteScreenID = -1;
117 rc = ilm_getPropertiesOfScreen(this->screenID, &this->screen_prop);
119 if(rc != ILM_SUCCESS) goto lc_init_error;
121 // Register Callback to Window Manager and from ILM
123 ilm_registerNotification(createCallback_static, this);
125 return WMError::SUCCESS;
128 HMI_ERROR("Failed to initialize. Terminate WM");
130 return WMError::FAIL;
133 void LayerControl::createNewLayer(unsigned id)
135 HMI_INFO("create new ID :%d", id);
136 struct rect rct = this->area2size[LC_DEFAULT_AREA];
137 ilm_layerCreateWithDimension(&id, rct.w, rct.h);
138 //ilm_layerSetSourceRectangle(id, rct.x, rct.y, rct.w, rct.h);
139 ilm_layerSetDestinationRectangle(id, this->offset_x, this->offset_y, rct.w, rct.h);
140 ilm_layerSetOpacity(id, 1.0);
141 ilm_layerSetVisibility(id, ILM_FALSE);
143 auto wm_layer = getWMLayer(id);
144 wm_layer->addLayerToState(id);
145 this->renderLayers();
146 this->renderLayersRemote();
149 void LayerControl::createNewRemoteLayer(unsigned id)
151 HMI_INFO("create new ID :%d (For remote layer)", id);
152 struct rect rct = {640, 720, 0, 0};
153 ilm_layerCreateWithDimension(&id, rct.w, rct.h);
154 //ilm_layerSetSourceRectangle(id, rct.x, rct.y, rct.w, rct.h);
155 ilm_layerSetDestinationRectangle(id, this->offset_x, this->offset_y, rct.w, rct.h);
156 ilm_layerSetOpacity(id, 1.0);
157 ilm_layerSetVisibility(id, ILM_FALSE);
159 auto wm_layer = getWMLayer(id);
160 wm_layer->addLayerToState(id);
161 this->renderLayers();
162 this->renderLayersRemote();
165 unsigned LayerControl::getNewLayerID(const string& role, string* layer_name)
168 for(const auto& l: this->wm_layers)
170 ret = l->getNewLayerID(role);
173 *layer_name = l->layerName();
174 unsigned uid = l->getUuid();
175 this->lid2wmlid[ret] = uid;
182 shared_ptr<WMLayer> LayerControl::getWMLayer(unsigned layer)
184 unsigned uuid = this->lid2wmlid[layer];
185 return this->wm_layers[uuid];
188 std::shared_ptr<WMLayer> LayerControl::getWMLayer(std::string layer_name)
190 for(auto &l : this->wm_layers)
192 if(l->layerName() == layer_name)
200 struct rect LayerControl::getAreaSize(const std::string& area)
202 return area2size[area];
205 void LayerControl::setupArea(const rectangle& base_rct, double scaling)
207 this->scaling = scaling;
208 this->offset_x = base_rct.left();
209 this->offset_y = base_rct.top();
211 for (auto &i : this->area2size)
213 i.second.x = static_cast<int>(scaling * i.second.x + 0.5);
214 i.second.y = static_cast<int>(scaling * i.second.y + 0.5);
215 i.second.w = static_cast<int>(scaling * i.second.w + 0.5);
216 i.second.h = static_cast<int>(scaling * i.second.h + 0.5);
218 HMI_DEBUG("area:%s size(after) : x:%d y:%d w:%d h:%d",
219 i.first.c_str(), i.second.x, i.second.y, i.second.w, i.second.h);
223 Screen LayerControl::getScreenInfo()
225 return Screen(this->screen_prop.screenWidth, this->screen_prop.screenHeight);
228 double LayerControl::scale()
230 return this->scaling;
233 WMError LayerControl::updateLayer(LayerState& layer_state)
235 return WMError::SUCCESS;
238 WMError LayerControl::renderLayers()
240 HMI_INFO("Commit change");
241 WMError rc = WMError::SUCCESS;
243 // Check the number of layers
244 vector<unsigned> ivi_l_ids;
245 for(auto& l : this->wm_layers)
247 auto state = l->getLayerState();
248 HMI_DEBUG("layer %s", l->layerName().c_str());
249 for(const auto& id : state.getIviIdList())
251 HMI_DEBUG("Add %d", id);
252 ivi_l_ids.push_back(id);
256 // Create render order
257 t_ilm_layer* id_array = new t_ilm_layer[ivi_l_ids.size()];
258 if(id_array == nullptr)
260 HMI_WARNING("short memory");
262 return WMError::FAIL;
265 for(const auto& i : ivi_l_ids)
272 ilmErrorTypes ret = ilm_displaySetRenderOrder(this->screenID, id_array, ivi_l_ids.size());
273 if(ret != ILM_SUCCESS)
280 for(auto& l : this->wm_layers)
290 WMError LayerControl::renderLayersRemote()
292 HMI_INFO("Commit change");
293 WMError rc = WMError::SUCCESS;
295 if (0 > this->remoteScreenID)
300 // Check the number of layers
301 vector<unsigned> ivi_l_ids;
302 for(auto& l : this->wm_remote_layers)
304 auto state = l->getLayerState();
305 HMI_DEBUG("layer %s", l->layerName().c_str());
306 for(const auto& id : state.getIviIdList())
308 HMI_DEBUG("Add %d", id);
309 ivi_l_ids.push_back(id);
313 if (0 == ivi_l_ids.size())
315 ilm_displaySetRenderOrder(this->remoteScreenID, nullptr, 0);
319 // Create render order
320 t_ilm_layer* id_array = new t_ilm_layer[ivi_l_ids.size()];
321 if(id_array == nullptr)
323 HMI_WARNING("short memory");
325 return WMError::FAIL;
328 for(const auto& i : ivi_l_ids)
335 ilmErrorTypes ret = ilm_displaySetRenderOrder(this->remoteScreenID, id_array, ivi_l_ids.size());
336 if(ret != ILM_SUCCESS)
343 for(auto& l : this->wm_remote_layers)
353 WMError LayerControl::setXDGSurfaceOriginSize(unsigned surface)
355 WMError ret = WMError::NOT_REGISTERED;
356 ilmSurfaceProperties prop;
357 ilmErrorTypes rc = ilm_getPropertiesOfSurface(surface, &prop);
358 if(rc == ILM_SUCCESS)
360 HMI_INFO("xdg surface info %d, %d", prop.origSourceWidth, prop.origSourceHeight);
361 ilm_surfaceSetSourceRectangle(surface, 0, 0, prop.origSourceWidth, prop.origSourceHeight);
362 ret = WMError::SUCCESS;
368 void LayerControl::undoUpdate()
370 for(auto& l : this->wm_layers)
376 WMError LayerControl::loadLayerSetting(const string &path)
378 HMI_DEBUG("loading WMLayer(Application Containers) Setting from %s", path);
380 json_object *json_obj, *json_cfg;
381 int ret = jh::inputJsonFilie(path.c_str(), &json_obj);
384 HMI_ERROR("Could not open %s", path.c_str());
385 return WMError::FAIL;
387 HMI_INFO("json_obj dump:%s", json_object_get_string(json_obj));
389 if (!json_object_object_get_ex(json_obj, "mappings", &json_cfg))
391 HMI_ERROR("Parse Error!!");
392 return WMError::FAIL;
395 int len = json_object_array_length(json_cfg);
396 HMI_DEBUG("json_cfg len:%d", len);
398 for (int i = 0; i < len; i++)
400 json_object *json_tmp = json_object_array_get_idx(json_cfg, i);
401 HMI_DEBUG("> json_tmp dump:%s", json_object_get_string(json_tmp));
403 this->wm_layers.emplace_back(std::make_shared<WMLayer>(json_tmp, i));
405 json_object_put(json_obj);
407 return WMError::SUCCESS;
410 WMError LayerControl::loadAreasConfigFile(const std::string& path, const std::string& ecu_name)
412 // Load areas config file
413 json_object *json_obj;
414 int ret = jh::inputJsonFilie(path.c_str(), &json_obj);
417 HMI_ERROR("Could not open %s", path.c_str());
418 return WMError::FAIL;
420 HMI_INFO("json_obj dump:%s", json_object_get_string(json_obj));
423 json_object *json_cfg;
424 if (!json_object_object_get_ex(json_obj, "ecus", &json_cfg))
426 HMI_ERROR("Parse Error!!");
427 return WMError::FAIL;
430 int num_ecu = json_object_array_length(json_cfg);
431 HMI_DEBUG("json_cfg(ecus) len:%d", num_ecu);
433 const char* c_ecu_name;
434 json_object *json_ecu;
435 for (int i = 0; i < num_ecu; i++)
437 json_ecu= json_object_array_get_idx(json_cfg, i);
439 c_ecu_name = jh::getStringFromJson(json_ecu, "name");
440 if (nullptr == c_ecu_name)
442 HMI_ERROR("Parse Error!!");
443 return WMError::FAIL;
446 if (ecu_name == string(c_ecu_name))
458 HMI_ERROR("Areas for ecu:%s is NOT exist!!", ecu_name.c_str());
459 return WMError::FAIL;
463 if (!json_object_object_get_ex(json_ecu, "screens", &json_cfg))
465 HMI_ERROR("Parse Error!!");
466 return WMError::FAIL;
469 int num_screen = json_object_array_length(json_cfg);
470 HMI_DEBUG("json_cfg(screens) len:%d", num_screen);
473 json_object *json_screen;
474 for (int i = 0; i < num_screen; i++)
476 json_screen = json_object_array_get_idx(json_cfg, i);
477 HMI_INFO("json_cfg dump:%s", json_object_get_string(json_cfg));
479 // TODO: Currently only one display is connected to a ECU.
481 screen_id = jh::getIntFromJson(json_screen, "id");
484 json_object *json_tmp;
485 if (!json_object_object_get_ex(json_screen, "areas", &json_tmp))
487 HMI_ERROR("Parse Error!!");
488 return WMError::FAIL;
491 int num_area = json_object_array_length(json_tmp);
492 HMI_DEBUG("json_tmp(areas) len:%d", num_area);
495 for (int j = 0; j < num_area; j++)
497 json_object *json_area = json_object_array_get_idx(json_tmp, j);
499 area = jh::getStringFromJson(json_area, "name");
502 HMI_ERROR("Parse Error!!");
503 return WMError::FAIL;
505 HMI_DEBUG("> area:%s", area);
507 json_object *json_rect;
508 if (!json_object_object_get_ex(json_area, "rect", &json_rect))
510 HMI_ERROR("Parse Error!!");
511 return WMError::FAIL;
514 struct rect area_size;
515 area_size.x = jh::getIntFromJson(json_rect, "x");
516 area_size.y = jh::getIntFromJson(json_rect, "y");
517 area_size.w = jh::getIntFromJson(json_rect, "w");
518 area_size.h = jh::getIntFromJson(json_rect, "h");
520 this->area2size[area] = area_size;
524 for (const auto& itr : this->area2size)
526 HMI_DEBUG("area:%s x:%d y:%d w:%d h:%d",
527 itr.first.c_str(), itr.second.x, itr.second.y,
528 itr.second.w, itr.second.h);
532 // Release json_object
533 json_object_put(json_obj);
535 return WMError::SUCCESS;
538 WMError LayerControl::layoutChange(const WMAction& action)
540 if ((action.visible == TaskVisible::INVISIBLE) ||
541 (action.visible == TaskVisible::REQ_REMOTE_VISIBLE) ||
542 (action.visible == TaskVisible::REQ_REMOTE_INVISIBLE))
544 // Visibility is not change -> no redraw is required
545 return WMError::SUCCESS;
547 if(action.client == nullptr)
549 HMI_SEQ_ERROR(action.req_num, "client may vanish");
550 return WMError::NOT_REGISTERED;
552 unsigned layer = action.client->layerID();
553 unsigned surface = action.client->surfaceID();
555 auto rect = this->getAreaSize(action.area);
556 HMI_DEBUG("Set layout %d, %d, %d, %d",rect.x, rect.y, rect.w, rect.h);
558 ilm_surfaceSetDestinationRectangle(surface, rect.x, rect.y, rect.w, rect.h);
560 for(auto &wm_layer: this->wm_layers)
562 // Store the state who is assigned to the area
563 if(wm_layer->hasLayerID(layer))
565 wm_layer->setAreaToState(action.client->appID(), action.area);
566 /* TODO: manipulate state directly
567 LayerState ls = wm_layer->getLayerState();
568 ls.setArea(action.client->appID(), action.area);
573 return WMError::SUCCESS;
576 WMError LayerControl::visibilityChange(const WMAction& action)
578 WMError ret = WMError::FAIL;
579 if(action.client == nullptr)
581 HMI_SEQ_ERROR(action.req_num, "client may vanish");
582 return WMError::NOT_REGISTERED;
585 if (action.visible == TaskVisible::VISIBLE)
587 ret = this->makeVisible(action.client);
589 else if (action.visible == TaskVisible::INVISIBLE)
591 ret = this->makeInvisible(action.client);
593 else if (action.visible == TaskVisible::REMOTE_VISIBLE)
595 ret = this->makeRemoteVisible(action.client);
597 else if (action.visible == TaskVisible::REMOTE_INVISIBLE)
599 ret = this->makeRemoteInvisible(action.client);
601 else // TaskVisible::REQ_REMOTE_VISIBLE || TaskVisible::REQ_REMOTE_INVISIBLE
603 // Visibility is not change
604 ret = WMError::SUCCESS;
610 void LayerControl::terminateApp(const shared_ptr<WMClient> client)
612 for(auto& l : this->wm_layers)
614 l->terminateApp(client->layerID());
618 void LayerControl::dispatchCreateEvent(ilmObjectType object, unsigned id, bool created)
620 if (ILM_SURFACE == object)
624 ilmSurfaceProperties sp;
626 rc = ilm_getPropertiesOfSurface(id, &sp);
627 if(rc != ILM_SUCCESS)
629 HMI_ERROR("Failed to get surface %d property due to %d", id, ilm_getError());
632 this->cb.surfaceCreated(sp.creatorPid, id);
633 ilm_surfaceSetSourceRectangle(id, 0, 0, sp.origSourceWidth, sp.origSourceHeight);
634 ilm_surfaceAddNotification(id, surfaceCallback_static);
635 ilm_surfaceSetVisibility(id, ILM_TRUE);
636 ilm_surfaceSetType(id, ILM_SURFACETYPE_DESKTOP);
640 this->cb.surfaceDestroyed(id);
643 if (ILM_LAYER == object)
647 ilm_layerAddNotification(id, layerCallback_static);
651 // Ignore here. Nothing to do currently.
652 // Process of application dead is handled by Window Manager
653 // from binder notification
658 void LayerControl::dispatchSurfacePropChangeEvent(unsigned id,
659 struct ilmSurfaceProperties* sprop,
660 t_ilm_notification_mask mask)
663 ILM_NOTIFICATION_CONTENT_AVAILABLE & ILM_NOTIFICATION_CONTENT_REMOVED
664 are not handled here, handled in create/destroy event
666 if (ILM_NOTIFICATION_VISIBILITY & mask)
668 HMI_DEBUG("surface %d turns visibility %d", id, sprop->visibility);
670 if (ILM_NOTIFICATION_OPACITY & mask)
672 HMI_DEBUG("surface %d turns opacity %f", id, sprop->opacity);
674 if (ILM_NOTIFICATION_SOURCE_RECT & mask)
676 HMI_DEBUG("surface %d source rect changes", id);
678 if (ILM_NOTIFICATION_DEST_RECT & mask)
680 HMI_DEBUG("surface %d dest rect changes", id);
682 if (ILM_NOTIFICATION_CONFIGURED & mask)
684 HMI_DEBUG("surface %d size %d, %d, %d, %d", id,
685 sprop->sourceX, sprop->sourceY, sprop->origSourceWidth, sprop->origSourceHeight);
686 ilm_surfaceSetSourceRectangle(id, 0, 0, sprop->origSourceWidth, sprop->origSourceHeight);
690 void LayerControl::dispatchLayerPropChangeEvent(unsigned id,
691 struct ilmLayerProperties* lprop,
692 t_ilm_notification_mask mask)
694 if (ILM_NOTIFICATION_VISIBILITY & mask)
696 HMI_DEBUG("layer %d turns visibility %d", id, lprop->visibility);
698 if (ILM_NOTIFICATION_OPACITY & mask)
700 HMI_DEBUG("layer %d turns opacity %f", id, lprop->opacity);
702 if (ILM_NOTIFICATION_SOURCE_RECT & mask)
704 HMI_DEBUG("layer %d source rect changes", id);
706 if (ILM_NOTIFICATION_DEST_RECT & mask)
708 HMI_DEBUG("layer %d dest rect changes", id);
712 WMError LayerControl::makeVisible(const shared_ptr<WMClient> client)
714 WMError ret = WMError::SUCCESS;
715 // Don't check here the client is not nullptr
716 unsigned layer = client->layerID();
718 this->moveForeGround(client);
720 ilm_layerSetVisibility(layer, ILM_TRUE);
722 /* for(auto& wm_layer : this->wm_layers)
724 if(wm_layer->hasLayerID(layer))
726 LayerState ls = wm_layer->getLayerState();
731 // Move foreground from back ground layer
732 /* for(auto& wm_layer : this->wm_layers)
734 if(wm_layer->layerName() == "BackGroundLayer")
736 if(wm_layer->hasRole(client->role()))
738 LayerState ls = wm_layer->getLayerState();
739 ls.removeLayer(layer);
748 WMError LayerControl::makeInvisible(const shared_ptr<WMClient> client)
750 WMError ret = WMError::SUCCESS;
751 unsigned layer = client->layerID(); // Don't check here the client is not nullptr
753 bool mv_ok = this->moveBackGround(client);
757 HMI_INFO("make invisible client %s", client->appID().c_str());
758 ilm_layerSetVisibility(layer, ILM_FALSE);
761 //ilm_layerSetDestinationRectangle(layer, 0, 0, 0, 0);
763 /* for(auto& wm_layer : this->wm_layers)
765 if(wm_layer->hasLayerID(layer))
767 LayerState ls = wm_layer->getLayerState();
768 ls.removeLayer(layer);;
777 WMError LayerControl::makeRemoteVisible(const shared_ptr<WMClient> client)
779 WMError ret = WMError::SUCCESS;
780 unsigned layer = client->layerID(); // Don't check here the client is not nullptr
782 if (0 > this->remoteScreenID)
787 // TODO: Currently there is only one remote screen.
788 for (auto itr = this->wm_layers.begin(); itr != this->wm_layers.end(); ++itr)
790 if((*itr)->hasLayerID(layer))
792 HMI_DEBUG("Add layer:%d to remote screen:%d", layer, this->remoteScreenID);
793 this->wm_remote_layers.push_back(*itr);
794 this->wm_layers.erase(itr);
797 if (this->wm_layers.end() == itr)
799 HMI_DEBUG("iteretor indicates end of vector of wm_layers");
804 ilm_layerSetVisibility(layer, ILM_TRUE);
809 WMError LayerControl::makeRemoteInvisible(const shared_ptr<WMClient> client)
811 WMError ret = WMError::SUCCESS;
812 unsigned layer = client->layerID(); // Don't check here the client is not nullptr
814 if (0 > this->remoteScreenID)
819 // TODO: Currently there is only one remote screen.
820 for (auto itr = this->wm_remote_layers.begin();
821 itr != this->wm_remote_layers.end(); ++itr)
823 if((*itr)->hasLayerID(layer))
825 HMI_DEBUG("Remove layer:%d from remote screen:%d", layer, this->remoteScreenID);
826 this->wm_layers.push_back(*itr);
827 this->wm_remote_layers.erase(itr);
830 if (this->wm_remote_layers.end() == itr)
832 HMI_DEBUG("iteretor indicates end of vector of wm_remote_layers");
837 ilm_layerSetVisibility(layer, ILM_FALSE);
842 bool LayerControl::moveBackGround(const shared_ptr<WMClient> client)
846 // Move background from foreground layer
847 auto bg = this->getWMLayer(BACK_GROUND_LAYER);
850 HMI_DEBUG("client %s role %s", client->appID().c_str(), client->role().c_str());
851 unsigned layer = client->layerID();
852 if(bg->hasRole(client->role()))
854 HMI_INFO("%s go to background", client->appID().c_str());
855 bg->addLayerToState(layer);
856 auto wm_layer = this->getWMLayer(layer);
857 wm_layer->removeLayerFromState(layer);
858 /* TODO: manipulate state directly
859 LayerState bg_ls = bg->getLayerState();
860 bg_ls.addLayer(layer);
861 LayerState ls = wm_layer->getLayerState();
862 ls.removeLayer(layer); */
871 bool LayerControl::moveForeGround(const shared_ptr<WMClient> client)
875 // Move foreground from foreground layer
876 auto bg = this->getWMLayer(BACK_GROUND_LAYER);
879 if(bg->hasRole(client->role()))
881 unsigned layer = client->layerID();
882 HMI_INFO("%s go to foreground", client->appID().c_str());
883 bg->removeLayerFromState(layer);
884 auto wm_layer = this->getWMLayer(layer);
885 wm_layer->addLayerToState(layer);
886 /* TODO: manipulate state directly
887 LayerState bg_ls = bg->getLayerState();
888 bg_ls.removeLayer(layer);
889 LayerState ls = wm_layer->getLayerState();
890 ls.addLayer(layer); */