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.db"
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->dispatchPropertyChangeEvent(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->dispatchPropertyChangeEvent(layer, layer_prop, mask);
59 LayerControl::LayerControl(const std::string& root)
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->loadAreaDb(area_path);
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];
105 rc = ilm_getPropertiesOfScreen(this->screenID, &this->screen_prop);
107 if(rc != ILM_SUCCESS) goto lc_init_error;
109 // Register Callback from ILM
111 ilm_registerNotification(createCallback_static, this);
113 return WMError::SUCCESS;
116 HMI_ERROR("Failed to initialize. Terminate WM");
118 return WMError::FAIL;
121 void LayerControl::createNewLayer(unsigned id)
123 HMI_INFO("create new ID :%d", id);
124 struct rect rct = this->area2size[LC_DEFAULT_AREA];
125 ilm_layerCreateWithDimension(&id, rct.w, rct.h);
126 //ilm_layerSetSourceRectangle(id, rct.x, rct.y, rct.w, rct.h);
127 //ilm_layerSetDestinationRectangle(id, rct.x, rct.y, rct.w, rct.h);
128 ilm_layerSetOpacity(id, 1.0);
129 ilm_layerSetVisibility(id, ILM_FALSE);
131 auto wm_layer = getWMLayer(id);
132 wm_layer->addLayerToState(id);
133 this->renderLayers();
136 unsigned LayerControl::getNewLayerID(const string& role, string* layer_name)
139 for(const auto& l: this->wm_layers)
141 ret = l->getNewLayerID(role);
144 *layer_name = l->layerName();
145 unsigned uid = l->getUuid();
146 this->lid2wmlid[ret] = uid;
153 shared_ptr<WMLayer> LayerControl::getWMLayer(unsigned layer)
155 unsigned uuid = this->lid2wmlid[layer];
156 return this->wm_layers[uuid];
159 std::shared_ptr<WMLayer> LayerControl::getWMLayer(std::string layer_name)
161 for(auto &l : this->wm_layers)
163 if(l->layerName() == layer_name)
171 struct rect LayerControl::getAreaSize(const std::string& area)
173 return area2size[area];
176 void LayerControl::setupArea(const rectangle& base_rct, double scaling)
179 this->scaling = scaling;
181 rct = this->area2size["normal.full"];
182 this->area2size["normalfull"] = rct;
183 this->area2size["normal"] = rct;
185 for (auto &i : this->area2size)
187 i.second.x = base_rct.left() + static_cast<int>(scaling * i.second.x + 0.5);
188 i.second.y = base_rct.top() + static_cast<int>(scaling * i.second.y + 0.5);
189 i.second.w = static_cast<int>(scaling * i.second.w + 0.5);
190 i.second.h = static_cast<int>(scaling * i.second.h + 0.5);
192 HMI_DEBUG("area:%s size(after) : x:%d y:%d w:%d h:%d",
193 i.first.c_str(), i.second.x, i.second.y, i.second.w, i.second.h);
197 Screen LayerControl::getScreenInfo()
199 return Screen(this->screen_prop.screenWidth, this->screen_prop.screenHeight);
202 double LayerControl::scale()
204 return this->scaling;
207 WMError LayerControl::updateLayer(LayerState& layer_state)
209 return WMError::SUCCESS;
212 WMError LayerControl::renderLayers()
214 HMI_INFO("Commit change");
215 WMError rc = WMError::SUCCESS;
217 // Check the number of layers
218 vector<unsigned> ivi_l_ids;
219 for(auto& l : this->wm_layers)
221 auto state = l->getLayerState();
222 HMI_DEBUG("layer %s", l->layerName().c_str());
223 for(const auto& id : state.getIviIdList())
225 HMI_DEBUG("Add %d", id);
226 ivi_l_ids.push_back(id);
230 // Create render order
231 t_ilm_layer* id_array = new t_ilm_layer[ivi_l_ids.size()];
232 if(id_array == nullptr)
234 HMI_WARNING("short memory");
236 return WMError::FAIL;
239 for(const auto& i : ivi_l_ids)
246 ilmErrorTypes ret = ilm_displaySetRenderOrder(this->screenID, id_array, ivi_l_ids.size());
247 if(ret != ILM_SUCCESS)
254 for(auto& l : this->wm_layers)
264 void LayerControl::undoUpdate()
266 for(auto& l : this->wm_layers)
272 WMError LayerControl::loadLayerSetting(const string &path)
274 HMI_DEBUG("loading WMLayer(Application Containers) Setting from %s", path);
276 json_object *json_obj, *json_cfg;
277 int ret = jh::inputJsonFilie(path.c_str(), &json_obj);
280 HMI_ERROR("Could not open %s", path.c_str());
281 return WMError::FAIL;
283 HMI_INFO("json_obj dump:%s", json_object_get_string(json_obj));
285 if (!json_object_object_get_ex(json_obj, "mappings", &json_cfg))
287 HMI_ERROR("Parse Error!!");
288 return WMError::FAIL;
291 int len = json_object_array_length(json_cfg);
292 HMI_DEBUG("json_cfg len:%d", len);
294 for (int i = 0; i < len; i++)
296 json_object *json_tmp = json_object_array_get_idx(json_cfg, i);
297 HMI_DEBUG("> json_tmp dump:%s", json_object_get_string(json_tmp));
299 this->wm_layers.emplace_back(std::make_shared<WMLayer>(json_tmp, i));
301 json_object_put(json_obj);
303 return WMError::SUCCESS;
306 WMError LayerControl::loadAreaDb(const std::string& path)
309 json_object *json_obj;
310 int ret = jh::inputJsonFilie(path.c_str(), &json_obj);
313 HMI_ERROR("Could not open %s", path.c_str());
314 return WMError::FAIL;
316 HMI_INFO("json_obj dump:%s", json_object_get_string(json_obj));
319 json_object *json_cfg;
320 if (!json_object_object_get_ex(json_obj, "areas", &json_cfg))
322 HMI_ERROR("Parse Error!!");
323 return WMError::FAIL;
326 int len = json_object_array_length(json_cfg);
327 HMI_DEBUG("json_cfg len:%d", len);
330 for (int i = 0; i < len; i++)
332 json_object *json_tmp = json_object_array_get_idx(json_cfg, i);
333 HMI_DEBUG("> json_tmp dump:%s", json_object_get_string(json_tmp));
335 area = jh::getStringFromJson(json_tmp, "name");
338 HMI_ERROR("Parse Error!!");
339 return WMError::FAIL;
341 HMI_DEBUG("> area:%s", area);
343 json_object *json_rect;
344 if (!json_object_object_get_ex(json_tmp, "rect", &json_rect))
346 HMI_ERROR("Parse Error!!");
347 return WMError::FAIL;
349 HMI_DEBUG("> json_rect dump:%s", json_object_get_string(json_rect));
351 struct rect area_size;
352 area_size.x = jh::getIntFromJson(json_rect, "x");
353 area_size.y = jh::getIntFromJson(json_rect, "y");
354 area_size.w = jh::getIntFromJson(json_rect, "w");
355 area_size.h = jh::getIntFromJson(json_rect, "h");
357 this->area2size[area] = area_size;
361 for (const auto& itr : this->area2size)
363 HMI_DEBUG("area:%s x:%d y:%d w:%d h:%d",
364 itr.first.c_str(), itr.second.x, itr.second.y,
365 itr.second.w, itr.second.h);
368 // Release json_object
369 json_object_put(json_obj);
371 return WMError::SUCCESS;
374 WMError LayerControl::layoutChange(const WMAction& action)
376 if (action.visible == TaskVisible::INVISIBLE)
378 // Visibility is not change -> no redraw is required
379 return WMError::SUCCESS;
381 if(action.client == nullptr)
383 HMI_SEQ_ERROR(action.req_num, "client may vanish");
384 return WMError::NOT_REGISTERED;
386 unsigned layer = action.client->layerID();
389 // WMError ret = this->setLayerSize(layer, action.area);
390 auto rect = this->getAreaSize(action.area);
391 HMI_DEBUG("Set layout %d, %d, %d, %d",rect.x, rect.y, rect.w, rect.h);
392 ilm_layerSetSourceRectangle(layer, 0, 0, rect.w, rect.h);
394 ilm_layerSetDestinationRectangle(layer, rect.x, rect.y, rect.w, rect.h);
396 for(auto &wm_layer: this->wm_layers)
398 if(wm_layer->hasLayerID(layer))
400 wm_layer->setAreaToState(action.client->appID(), action.area);
401 /* TODO: manipulate state directly
402 LayerState ls = wm_layer->getLayerState();
403 ls.setArea(action.client->appID(), action.area);
408 return WMError::SUCCESS;
411 WMError LayerControl::visibilityChange(const WMAction& action)
413 WMError ret = WMError::FAIL;
414 if(action.client == nullptr)
416 HMI_SEQ_ERROR(action.req_num, "client may vanish");
417 return WMError::NOT_REGISTERED;
420 if (action.visible == TaskVisible::VISIBLE)
422 ret = this->makeVisible(action.client);
424 else if (action.visible == TaskVisible::INVISIBLE)
426 ret = this->makeInvisible(action.client);
431 void LayerControl::terminateApp(const shared_ptr<WMClient> client)
433 for(auto& l : this->wm_layers)
435 l->terminateApp(client->layerID());
439 void LayerControl::dispatchCreateEvent(ilmObjectType object, unsigned id, bool created)
441 if (ILM_SURFACE == object)
445 ilmSurfaceProperties sp;
447 rc = ilm_getPropertiesOfSurface(id, &sp);
448 if(rc != ILM_SUCCESS)
450 HMI_ERROR("Failed to get surface %d property due to %d", id, ilm_getError());
453 this->cb.surfaceCreated(sp.creatorPid, id);
454 ilm_surfaceAddNotification(id, surfaceCallback_static);
455 ilm_surfaceSetVisibility(id, ILM_TRUE);
459 this->cb.surfaceDestroyed(id);
462 if (ILM_LAYER == object)
466 ilm_layerAddNotification(id, layerCallback_static);
470 // Ignore here. Nothing to do currently.
471 // Process of application dead is handled by Window Manager
472 // from binder notification
477 void LayerControl::dispatchPropertyChangeEvent(unsigned id,
478 struct ilmSurfaceProperties* sprop,
479 t_ilm_notification_mask mask)
482 ILM_NOTIFICATION_CONTENT_AVAILABLE & ILM_NOTIFICATION_CONTENT_REMOVED
483 are not handled here, handled in create/destroy event
485 if (ILM_NOTIFICATION_VISIBILITY & mask)
487 HMI_DEBUG("surface %d turns visibility %d", id, sprop->visibility);
489 if (ILM_NOTIFICATION_OPACITY & mask)
491 HMI_DEBUG("surface %d turns opacity %f", id, sprop->opacity);
493 if (ILM_NOTIFICATION_SOURCE_RECT & mask)
495 HMI_DEBUG("surface %d source rect changes", id);
497 if (ILM_NOTIFICATION_DEST_RECT & mask)
499 HMI_DEBUG("surface %d dest rect changes", id);
501 if (ILM_NOTIFICATION_CONFIGURED & mask)
503 HMI_DEBUG("surface %d size %d, %d, %d, %d", id,
504 sprop->sourceX, sprop->sourceY, sprop->origSourceWidth, sprop->origSourceHeight);
505 ilm_surfaceSetSourceRectangle(id, 0, 0, sprop->origSourceWidth, sprop->origSourceHeight);
506 ilm_surfaceSetDestinationRectangle(id, 0, 0, sprop->origSourceWidth, sprop->origSourceHeight);
510 void LayerControl::dispatchPropertyChangeEvent(unsigned id,
511 struct ilmLayerProperties* lprop,
512 t_ilm_notification_mask mask)
515 ILM_NOTIFICATION_CONTENT_AVAILABLE & ILM_NOTIFICATION_CONTENT_REMOVED
516 are not handled here, handled in create/destroy event
518 if (ILM_NOTIFICATION_VISIBILITY & mask)
520 HMI_DEBUG("layer %d turns visibility %d", id, lprop->visibility);
522 if (ILM_NOTIFICATION_OPACITY & mask)
524 HMI_DEBUG("layer %d turns opacity %f", id, lprop->opacity);
526 if (ILM_NOTIFICATION_SOURCE_RECT & mask)
528 HMI_DEBUG("layer %d source rect changes", id);
530 if (ILM_NOTIFICATION_DEST_RECT & mask)
532 HMI_DEBUG("layer %d dest rect changes", id);
536 WMError LayerControl::makeVisible(const shared_ptr<WMClient> client)
538 WMError ret = WMError::SUCCESS;
539 // Don't check here the client is not nullptr
540 unsigned layer = client->layerID();
542 this->moveForeGround(client);
544 ilm_layerSetVisibility(layer, ILM_TRUE);
546 /* for(auto& wm_layer : this->wm_layers)
548 if(wm_layer->hasLayerID(layer))
550 LayerState ls = wm_layer->getLayerState();
555 // Move foreground from back ground layer
556 /* for(auto& wm_layer : this->wm_layers)
558 if(wm_layer->layerName() == "BackGroundLayer")
560 if(wm_layer->hasRole(client->role()))
562 LayerState ls = wm_layer->getLayerState();
563 ls.removeLayer(layer);
572 WMError LayerControl::makeInvisible(const shared_ptr<WMClient> client)
574 WMError ret = WMError::SUCCESS;
575 unsigned layer = client->layerID(); // Don't check here the client is not nullptr
577 bool mv_ok = this->moveBackGround(client);
581 HMI_INFO("make invisible client %s", client->appID().c_str());
582 ilm_layerSetVisibility(layer, ILM_FALSE);
585 //ilm_layerSetDestinationRectangle(layer, 0, 0, 0, 0);
587 /* for(auto& wm_layer : this->wm_layers)
589 if(wm_layer->hasLayerID(layer))
591 LayerState ls = wm_layer->getLayerState();
592 ls.removeLayer(layer);;
601 bool LayerControl::moveBackGround(const shared_ptr<WMClient> client)
605 // Move background from foreground layer
606 auto bg = this->getWMLayer(BACK_GROUND_LAYER);
609 HMI_DEBUG("client %s role %s", client->appID().c_str(), client->role().c_str());
610 unsigned layer = client->layerID();
611 if(bg->hasRole(client->role()))
613 HMI_INFO("%s go to background", client->appID().c_str());
614 bg->addLayerToState(layer);
615 auto wm_layer = this->getWMLayer(layer);
616 wm_layer->removeLayerFromState(layer);
617 /* TODO: manipulate state directly
618 LayerState bg_ls = bg->getLayerState();
619 bg_ls.addLayer(layer);
620 LayerState ls = wm_layer->getLayerState();
621 ls.removeLayer(layer); */
630 bool LayerControl::moveForeGround(const shared_ptr<WMClient> client)
634 // Move foreground from foreground layer
635 auto bg = this->getWMLayer(BACK_GROUND_LAYER);
638 if(bg->hasRole(client->role()))
640 unsigned layer = client->layerID();
641 HMI_INFO("%s go to foreground", client->appID().c_str());
642 bg->removeLayerFromState(layer);
643 auto wm_layer = this->getWMLayer(layer);
644 wm_layer->addLayerToState(layer);
645 /* TODO: manipulate state directly
646 LayerState bg_ls = bg->getLayerState();
647 bg_ls.removeLayer(layer);
648 LayerState ls = wm_layer->getLayerState();
649 ls.addLayer(layer); */