2 * Copyright (c) 2017 TOYOTA MOTOR CORPORATION
3 * Copyright (c) 2018,2019 Konsulko Group
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
19 #include "wm_layer_control.hpp"
20 #include "wm_layer.hpp"
21 #include "wm_client.hpp"
22 #include "request.hpp"
23 #include "json_helper.hpp"
25 #define LC_AREA_FILE "areas.json"
26 #define LC_LAYER_SETTING_FILE "layers.json"
27 #define LC_DEFAULT_AREA "fullscreen"
28 #define LC_REMOTE_DEFAULT_AREA "remote.fullscreen"
29 #define BACK_GROUND_LAYER "BackGroundLayer"
33 using std::shared_ptr;
37 LayerControl* g_lc_ctxt;
39 static void createCallback_static(ilmObjectType object,
44 static_cast<LayerControl*>(data)->dispatchCreateEvent(object, id, created);
47 static void surfaceCallback_static(t_ilm_surface surface,
48 struct ilmSurfaceProperties* surface_prop,
49 t_ilm_notification_mask mask)
51 g_lc_ctxt->dispatchSurfacePropChangeEvent(surface, surface_prop, mask);
54 static void layerCallback_static(t_ilm_layer layer,
55 struct ilmLayerProperties* layer_prop,
56 t_ilm_notification_mask mask)
58 g_lc_ctxt->dispatchLayerPropChangeEvent(layer, layer_prop, mask);
61 LayerControl::LayerControl(const std::string& root)
63 string area_path(get_file_path(LC_AREA_FILE, root.c_str()));
64 string layer_path(get_file_path(LC_LAYER_SETTING_FILE, root.c_str()));
65 // load layers.setting.json
66 WMError ret = this->loadLayerSetting(layer_path);
67 assert(ret == WMError::SUCCESS);
69 ret = this->loadAreaDb(area_path);
70 assert(ret == WMError::SUCCESS);
73 WMError LayerControl::init(const LayerControlCallbacks& cb)
75 HMI_DEBUG("Initialize of ilm library and display");
79 ilmErrorTypes rc = ilm_init();
81 while (rc != ILM_SUCCESS)
86 HMI_ERROR("Could not connect to compositor");
89 HMI_ERROR("Wait to start weston ...");
93 if(rc != ILM_SUCCESS) goto lc_init_error;
95 // Get current screen setting
96 rc = ilm_getScreenIDs(&num, &ids);
98 if(rc != ILM_SUCCESS) goto lc_init_error;
100 for(unsigned i = 0; i < num; i++)
102 HMI_INFO("get screen: %d", ids[i]);
104 // Currently, 0 is only available
105 this->screenID = ids[0];
109 // TODO: set remote screen id
110 HMI_INFO("There is remote screen (id:%d)", ids[1]);
111 this->remoteScreenID = ids[1];
115 HMI_INFO("There is no remote screen");
116 this->remoteScreenID = -1;
119 rc = ilm_getPropertiesOfScreen(this->screenID, &this->screen_prop);
121 if(rc != ILM_SUCCESS) goto lc_init_error;
123 // Register Callback to Window Manager and from ILM
125 ilm_registerNotification(createCallback_static, this);
127 return WMError::SUCCESS;
130 HMI_ERROR("Failed to initialize. Terminate WM");
132 return WMError::FAIL;
135 void LayerControl::createNewLayer(unsigned id, bool remote)
137 HMI_INFO("create new ID :%d%s", id, remote ? " (For remote layer)" : "");
138 struct rect rct = this->area2size[remote ? LC_REMOTE_DEFAULT_AREA : LC_DEFAULT_AREA];
139 ilm_layerCreateWithDimension(&id, rct.w, rct.h);
140 //ilm_layerSetSourceRectangle(id, rct.x, rct.y, rct.w, rct.h);
141 ilm_layerSetDestinationRectangle(id, this->offset_x, this->offset_y, rct.w, rct.h);
142 ilm_layerSetOpacity(id, 1.0);
143 ilm_layerSetVisibility(id, ILM_FALSE);
145 auto wm_layer = getWMLayer(id);
146 wm_layer->addLayerToState(id);
147 wm_layer->setRemote(remote);
148 this->renderLayers();
151 unsigned LayerControl::getNewLayerID(const string& role, std::string* layer_name)
154 for(const auto& l: this->wm_layers)
156 ret = l->getNewLayerID(role);
159 unsigned wmlid = l->getWMLayerID();
160 this->lid2wmlid[ret] = wmlid;
163 *layer_name = l->layerName();
171 shared_ptr<WMLayer> LayerControl::getWMLayer(unsigned layer)
173 unsigned wm_lid = this->lid2wmlid[layer];
174 return this->wm_layers[wm_lid];
177 std::shared_ptr<WMLayer> LayerControl::getWMLayer(std::string layer_name)
179 for(auto &l : this->wm_layers)
181 if(l->layerName() == layer_name)
189 struct rect LayerControl::getAreaSize(const std::string& area)
191 return area2size[area];
194 void LayerControl::setupArea(const rectangle& base_rct, double scaling)
196 this->scaling = scaling;
197 this->offset_x = base_rct.left();
198 this->offset_y = base_rct.top();
200 for (auto &i : this->area2size)
202 i.second.x = static_cast<int>(scaling * i.second.x + 0.5);
203 i.second.y = static_cast<int>(scaling * i.second.y + 0.5);
204 i.second.w = static_cast<int>(scaling * i.second.w + 0.5);
205 i.second.h = static_cast<int>(scaling * i.second.h + 0.5);
207 HMI_DEBUG("area:%s size(after) : x:%d y:%d w:%d h:%d",
208 i.first.c_str(), i.second.x, i.second.y, i.second.w, i.second.h);
212 Screen LayerControl::getScreenInfo()
214 return Screen(this->screen_prop.screenWidth, this->screen_prop.screenHeight);
217 double LayerControl::scale()
219 return this->scaling;
222 WMError LayerControl::renderLayers()
224 HMI_INFO("Commit change");
225 WMError rc = WMError::SUCCESS;
227 bool haveRemoteDisplay = remoteScreenID > 0;
229 // Check the number of layers
230 vector<unsigned> ivi_l_ids;
231 vector<unsigned> ivi_remote_l_ids;
232 for(auto& l : this->wm_layers)
234 auto state = l->getLayerState();
235 HMI_DEBUG("layer %s", l->layerName().c_str());
238 for(const auto& id : state.getIviIdList())
240 HMI_DEBUG("Add %d", id);
241 ivi_l_ids.push_back(id);
243 } else if (haveRemoteDisplay) {
244 for(const auto& id : state.getIviIdList())
246 HMI_DEBUG("Add remote %d", id);
247 ivi_remote_l_ids.push_back(id);
252 // Create render order
253 t_ilm_layer* id_array = new t_ilm_layer[ivi_l_ids.size()];
254 if(id_array == nullptr)
256 HMI_WARNING("short memory");
258 return WMError::FAIL;
261 for(const auto& i : ivi_l_ids)
263 id_array[count++] = i;
266 t_ilm_layer* remote_id_array = nullptr;
267 if(haveRemoteDisplay && ivi_remote_l_ids.size() != 0) {
268 remote_id_array = new t_ilm_layer[ivi_remote_l_ids.size()];
269 if(remote_id_array == nullptr)
271 HMI_WARNING("short memory");
273 return WMError::FAIL;
276 for(const auto& i : ivi_remote_l_ids)
278 remote_id_array[count++] = i;
283 ilmErrorTypes ret = ilm_displaySetRenderOrder(this->screenID, id_array, ivi_l_ids.size());
284 if(ret == ILM_SUCCESS && haveRemoteDisplay)
286 ret = ilm_displaySetRenderOrder(this->remoteScreenID, remote_id_array, ivi_remote_l_ids.size());
288 if(ret != ILM_SUCCESS)
295 for(auto& l : this->wm_layers)
302 delete remote_id_array;
306 WMError LayerControl::setXDGSurfaceOriginSize(unsigned surface)
308 WMError ret = WMError::NOT_REGISTERED;
309 ilmSurfaceProperties prop;
310 ilmErrorTypes rc = ilm_getPropertiesOfSurface(surface, &prop);
311 if(rc == ILM_SUCCESS)
313 HMI_INFO("xdg surface info %d, %d", prop.origSourceWidth, prop.origSourceHeight);
314 ilm_surfaceSetSourceRectangle(surface, 0, 0, prop.origSourceWidth, prop.origSourceHeight);
315 ret = WMError::SUCCESS;
321 void LayerControl::undoUpdate()
323 for(auto& l : this->wm_layers)
329 WMError LayerControl::loadLayerSetting(const string &path)
331 HMI_DEBUG("loading WMLayer(Application Containers) Setting from %s", path.c_str());
333 json_object *json_obj, *json_cfg;
334 int ret = jh::inputJsonFilie(path.c_str(), &json_obj);
337 HMI_ERROR("Could not open %s", path.c_str());
338 return WMError::FAIL;
340 HMI_INFO("json_obj dump:%s", json_object_get_string(json_obj));
342 if (!json_object_object_get_ex(json_obj, "mappings", &json_cfg))
344 HMI_ERROR("Parse Error!!");
345 return WMError::FAIL;
348 int len = json_object_array_length(json_cfg);
349 HMI_DEBUG("json_cfg len:%d", len);
351 for (int i = 0; i < len; i++)
353 json_object *json_tmp = json_object_array_get_idx(json_cfg, i);
354 HMI_DEBUG("> json_tmp dump:%s", json_object_get_string(json_tmp));
356 this->wm_layers.emplace_back(std::make_shared<WMLayer>(json_tmp, i));
358 json_object_put(json_obj);
360 return WMError::SUCCESS;
363 WMError LayerControl::loadAreaDb(const std::string& path)
366 json_object *json_obj;
367 int ret = jh::inputJsonFilie(path.c_str(), &json_obj);
370 HMI_ERROR("Could not open %s", path.c_str());
371 return WMError::FAIL;
373 HMI_INFO("json_obj dump:%s", json_object_get_string(json_obj));
376 json_object *json_cfg;
377 if (!json_object_object_get_ex(json_obj, "areas", &json_cfg))
379 HMI_ERROR("Parse Error!!");
380 return WMError::FAIL;
383 int len = json_object_array_length(json_cfg);
384 HMI_DEBUG("json_cfg len:%d", len);
387 for (int i = 0; i < len; i++)
389 json_object *json_tmp = json_object_array_get_idx(json_cfg, i);
390 HMI_DEBUG("> json_tmp dump:%s", json_object_get_string(json_tmp));
392 area = jh::getStringFromJson(json_tmp, "name");
395 HMI_ERROR("Parse Error!!");
396 return WMError::FAIL;
398 HMI_DEBUG("> area:%s", area);
400 json_object *json_rect;
401 if (!json_object_object_get_ex(json_tmp, "rect", &json_rect))
403 HMI_ERROR("Parse Error!!");
404 return WMError::FAIL;
406 HMI_DEBUG("> json_rect dump:%s", json_object_get_string(json_rect));
408 struct rect area_size;
409 area_size.x = jh::getIntFromJson(json_rect, "x");
410 area_size.y = jh::getIntFromJson(json_rect, "y");
411 area_size.w = jh::getIntFromJson(json_rect, "w");
412 area_size.h = jh::getIntFromJson(json_rect, "h");
414 this->area2size[area] = area_size;
418 for (const auto& itr : this->area2size)
420 HMI_DEBUG("area:%s x:%d y:%d w:%d h:%d",
421 itr.first.c_str(), itr.second.x, itr.second.y,
422 itr.second.w, itr.second.h);
425 // Release json_object
426 json_object_put(json_obj);
428 return WMError::SUCCESS;
431 WMError LayerControl::layoutChange(const WMAction& action)
433 if (action.visible == TaskVisible::INVISIBLE)
435 // Visibility is not change -> no redraw is required
436 return WMError::SUCCESS;
438 if(action.client == nullptr)
440 HMI_SEQ_ERROR(action.req_num, "client may vanish");
441 return WMError::NOT_REGISTERED;
443 unsigned layer = action.client->layerID();
444 unsigned surface = action.client->surfaceID();
446 auto rect = this->getAreaSize(action.area);
447 HMI_SEQ_INFO(action.req_num, "Set layout %d, %d, %d, %d",rect.x, rect.y, rect.w, rect.h);
449 // Sometimes, ivi_wm_surface_size signal doesn't reach window manager,
450 // then, Window Manager set set source size = 0.
451 if(!action.client->isSourceSizeSet())
453 ilmSurfaceProperties sp;
454 if (ILM_SUCCESS != ilm_getPropertiesOfSurface(surface, &sp))
455 return WMError::FAIL;
457 if (sp.origSourceHeight != sp.sourceHeight ||
458 sp.origSourceWidth != sp.sourceWidth)
460 HMI_SEQ_NOTICE(action.req_num, "set source size w:%d h%d", sp.origSourceWidth, sp.origSourceHeight);
461 ilm_surfaceSetSourceRectangle(surface, 0, 0, sp.origSourceWidth, sp.origSourceHeight);
463 action.client->setSurfaceSizeCorrectly();
467 ilm_surfaceSetDestinationRectangle(surface, rect.x, rect.y, rect.w, rect.h);
469 action.client->setArea(action.area);
470 for(auto &wm_layer: this->wm_layers)
472 // Store the state who is assigned to the area
473 if(wm_layer->hasLayerID(layer))
475 wm_layer->attachAppToArea(action.client->appID(), action.area);
476 /* TODO: manipulate state directly
477 LayerState ls = wm_layer->getLayerState();
478 ls.seattachAppToAreatArea(action.client->appID(), action.area);
483 return WMError::SUCCESS;
486 WMError LayerControl::visibilityChange(const WMAction& action)
488 WMError ret = WMError::FAIL;
489 if(action.client == nullptr)
491 HMI_SEQ_ERROR(action.req_num, "client may vanish");
492 return WMError::NOT_REGISTERED;
495 if (action.visible == TaskVisible::VISIBLE)
497 ret = this->makeVisible(action.client);
499 else if (action.visible == TaskVisible::INVISIBLE)
501 ret = this->makeInvisible(action.client);
507 WMError LayerControl::updateAreaList(const ChangeAreaReq& req)
510 for(const auto& elem : req.area_req)
512 if(this->area2size.find(elem.first) == this->area2size.end())
514 HMI_ERROR("requested area %s is not registered in area list", elem.first.c_str());
515 return WMError::NOT_REGISTERED;
519 for(const auto& elem : req.area_req)
521 this->area2size[elem.first] = elem.second;
525 HMI_NOTICE("Not implemented");
528 return WMError::SUCCESS;
531 WMError LayerControl::getUpdateAreaList(ChangeAreaReq *req)
533 for(const auto& wm_layer : this->wm_layers)
535 // get area name and compare it with elem
536 for(const auto& area : req->area_req)
538 string app = wm_layer->attachedApp(area.first);
541 HMI_INFO("app %s changes area %s", app.c_str(), area.first.c_str());
542 req->update_app2area[app] = area.first;
546 return WMError::SUCCESS;
549 void LayerControl::appTerminated(const shared_ptr<WMClient> client)
551 for(auto& l : this->wm_layers)
553 if(l->hasLayerID(client->layerID()))
555 l->appTerminated(client->layerID());
560 void LayerControl::dispatchCreateEvent(ilmObjectType object, unsigned id, bool created)
562 if (ILM_SURFACE == object)
566 ilmSurfaceProperties sp;
568 rc = ilm_getPropertiesOfSurface(id, &sp);
569 if(rc != ILM_SUCCESS)
571 HMI_ERROR("Failed to get surface %d property due to %d", id, ilm_getError());
574 this->cb.surfaceCreated(sp.creatorPid, id);
575 ilm_surfaceAddNotification(id, surfaceCallback_static);
576 ilm_surfaceSetVisibility(id, ILM_TRUE);
577 ilm_surfaceSetType(id, ILM_SURFACETYPE_DESKTOP);
581 this->cb.surfaceDestroyed(id);
584 if (ILM_LAYER == object)
588 ilm_layerAddNotification(id, layerCallback_static);
592 // Ignore here. Nothing to do currently.
593 // Process of application dead is handled by Window Manager
594 // from binder notification
599 void LayerControl::dispatchSurfacePropChangeEvent(unsigned id,
600 struct ilmSurfaceProperties* sprop,
601 t_ilm_notification_mask mask)
604 ILM_NOTIFICATION_CONTENT_AVAILABLE & ILM_NOTIFICATION_CONTENT_REMOVED
605 are not handled here, handled in create/destroy event
607 if (ILM_NOTIFICATION_VISIBILITY & mask)
609 HMI_DEBUG("surface %d turns visibility %d", id, sprop->visibility);
611 if (ILM_NOTIFICATION_OPACITY & mask)
613 HMI_DEBUG("surface %d turns opacity %f", id, sprop->opacity);
615 if (ILM_NOTIFICATION_SOURCE_RECT & mask)
617 HMI_DEBUG("surface %d source rect changes", id);
619 if (ILM_NOTIFICATION_DEST_RECT & mask)
621 HMI_DEBUG("surface %d dest rect changes", id);
623 if (ILM_NOTIFICATION_CONFIGURED & mask)
625 HMI_DEBUG("surface %d size %d, %d, %d, %d", id,
626 sprop->sourceX, sprop->sourceY, sprop->origSourceWidth, sprop->origSourceHeight);
627 ilm_surfaceSetSourceRectangle(id, 0, 0, sprop->origSourceWidth, sprop->origSourceHeight);
631 void LayerControl::dispatchLayerPropChangeEvent(unsigned id,
632 struct ilmLayerProperties* lprop,
633 t_ilm_notification_mask mask)
635 if (ILM_NOTIFICATION_VISIBILITY & mask)
637 HMI_DEBUG("layer %d turns visibility %d", id, lprop->visibility);
639 if (ILM_NOTIFICATION_OPACITY & mask)
641 HMI_DEBUG("layer %d turns opacity %f", id, lprop->opacity);
643 if (ILM_NOTIFICATION_SOURCE_RECT & mask)
645 HMI_DEBUG("layer %d source rect changes", id);
647 if (ILM_NOTIFICATION_DEST_RECT & mask)
649 HMI_DEBUG("layer %d dest rect changes", id);
653 WMError LayerControl::makeVisible(const shared_ptr<WMClient> client)
655 WMError ret = WMError::SUCCESS;
656 // Don't check here wheher client is nullptr or not
657 unsigned layer_id = client->layerID();
658 auto layer = getWMLayer(layer_id);
660 if (!layer->isRemote())
662 this->moveForeGround(client);
664 else if (0 > this->remoteScreenID) {
668 ilm_layerSetVisibility(layer_id, ILM_TRUE);
673 WMError LayerControl::makeInvisible(const shared_ptr<WMClient> client)
675 WMError ret = WMError::SUCCESS;
676 // Don't check here the client is not nullptr
677 unsigned layer_id = client->layerID();
678 auto layer = getWMLayer(layer_id);
680 bool set_invisible = true;
681 if (!layer->isRemote())
683 if(this->moveBackGround(client))
685 set_invisible = false;
689 HMI_INFO("make invisible client %s", client->appID().c_str());
692 else if (0 > this->remoteScreenID) {
698 ilm_layerSetVisibility(layer_id, ILM_FALSE);
704 bool LayerControl::moveBackGround(const shared_ptr<WMClient> client)
708 // Move background from foreground layer
709 auto bg = this->getWMLayer(BACK_GROUND_LAYER);
712 HMI_DEBUG("client %s role %s", client->appID().c_str(), client->role().c_str());
713 unsigned layer = client->layerID();
714 if(bg->hasRole(client->role()))
716 HMI_INFO("%s go to background", client->appID().c_str());
717 bg->addLayerToState(layer);
718 auto wm_layer = this->getWMLayer(layer);
719 wm_layer->removeLayerFromState(layer);
720 /* TODO: manipulate state directly
721 LayerState bg_ls = bg->getLayerState();
722 bg_ls.addLayer(layer);
723 LayerState ls = wm_layer->getLayerState();
724 ls.removeLayer(layer); */
733 bool LayerControl::moveForeGround(const shared_ptr<WMClient> client)
737 // Move foreground from foreground layer
738 auto bg = this->getWMLayer(BACK_GROUND_LAYER);
741 if(bg->hasRole(client->role()))
743 unsigned layer = client->layerID();
744 HMI_INFO("%s go to foreground", client->appID().c_str());
745 bg->removeLayerFromState(layer);
746 auto wm_layer = this->getWMLayer(layer);
747 wm_layer->addLayerToState(layer);
748 /* TODO: manipulate state directly
749 LayerState bg_ls = bg->getLayerState();
750 bg_ls.removeLayer(layer);
751 LayerState ls = wm_layer->getLayerState();
752 ls.addLayer(layer); */