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->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)
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 to Window Manager and 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, this->offset_x, this->offset_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)
178 this->scaling = scaling;
179 this->offset_x = base_rct.left();
180 this->offset_y = base_rct.top();
182 for (auto &i : this->area2size)
184 i.second.x = static_cast<int>(scaling * i.second.x + 0.5);
185 i.second.y = static_cast<int>(scaling * i.second.y + 0.5);
186 i.second.w = static_cast<int>(scaling * i.second.w + 0.5);
187 i.second.h = static_cast<int>(scaling * i.second.h + 0.5);
189 HMI_DEBUG("area:%s size(after) : x:%d y:%d w:%d h:%d",
190 i.first.c_str(), i.second.x, i.second.y, i.second.w, i.second.h);
194 Screen LayerControl::getScreenInfo()
196 return Screen(this->screen_prop.screenWidth, this->screen_prop.screenHeight);
199 double LayerControl::scale()
201 return this->scaling;
204 WMError LayerControl::updateLayer(LayerState& layer_state)
206 return WMError::SUCCESS;
209 WMError LayerControl::renderLayers()
211 HMI_INFO("Commit change");
212 WMError rc = WMError::SUCCESS;
214 // Check the number of layers
215 vector<unsigned> ivi_l_ids;
216 for(auto& l : this->wm_layers)
218 auto state = l->getLayerState();
219 HMI_DEBUG("layer %s", l->layerName().c_str());
220 for(const auto& id : state.getIviIdList())
222 HMI_DEBUG("Add %d", id);
223 ivi_l_ids.push_back(id);
227 // Create render order
228 t_ilm_layer* id_array = new t_ilm_layer[ivi_l_ids.size()];
229 if(id_array == nullptr)
231 HMI_WARNING("short memory");
233 return WMError::FAIL;
236 for(const auto& i : ivi_l_ids)
243 ilmErrorTypes ret = ilm_displaySetRenderOrder(this->screenID, id_array, ivi_l_ids.size());
244 if(ret != ILM_SUCCESS)
251 for(auto& l : this->wm_layers)
261 void LayerControl::undoUpdate()
263 for(auto& l : this->wm_layers)
269 WMError LayerControl::loadLayerSetting(const string &path)
271 HMI_DEBUG("loading WMLayer(Application Containers) Setting from %s", path);
273 json_object *json_obj, *json_cfg;
274 int ret = jh::inputJsonFilie(path.c_str(), &json_obj);
277 HMI_ERROR("Could not open %s", path.c_str());
278 return WMError::FAIL;
280 HMI_INFO("json_obj dump:%s", json_object_get_string(json_obj));
282 if (!json_object_object_get_ex(json_obj, "mappings", &json_cfg))
284 HMI_ERROR("Parse Error!!");
285 return WMError::FAIL;
288 int len = json_object_array_length(json_cfg);
289 HMI_DEBUG("json_cfg len:%d", len);
291 for (int i = 0; i < len; i++)
293 json_object *json_tmp = json_object_array_get_idx(json_cfg, i);
294 HMI_DEBUG("> json_tmp dump:%s", json_object_get_string(json_tmp));
296 this->wm_layers.emplace_back(std::make_shared<WMLayer>(json_tmp, i));
298 json_object_put(json_obj);
300 return WMError::SUCCESS;
303 WMError LayerControl::loadAreaDb(const std::string& path)
306 json_object *json_obj;
307 int ret = jh::inputJsonFilie(path.c_str(), &json_obj);
310 HMI_ERROR("Could not open %s", path.c_str());
311 return WMError::FAIL;
313 HMI_INFO("json_obj dump:%s", json_object_get_string(json_obj));
316 json_object *json_cfg;
317 if (!json_object_object_get_ex(json_obj, "areas", &json_cfg))
319 HMI_ERROR("Parse Error!!");
320 return WMError::FAIL;
323 int len = json_object_array_length(json_cfg);
324 HMI_DEBUG("json_cfg len:%d", len);
327 for (int i = 0; i < len; i++)
329 json_object *json_tmp = json_object_array_get_idx(json_cfg, i);
330 HMI_DEBUG("> json_tmp dump:%s", json_object_get_string(json_tmp));
332 area = jh::getStringFromJson(json_tmp, "name");
335 HMI_ERROR("Parse Error!!");
336 return WMError::FAIL;
338 HMI_DEBUG("> area:%s", area);
340 json_object *json_rect;
341 if (!json_object_object_get_ex(json_tmp, "rect", &json_rect))
343 HMI_ERROR("Parse Error!!");
344 return WMError::FAIL;
346 HMI_DEBUG("> json_rect dump:%s", json_object_get_string(json_rect));
348 struct rect area_size;
349 area_size.x = jh::getIntFromJson(json_rect, "x");
350 area_size.y = jh::getIntFromJson(json_rect, "y");
351 area_size.w = jh::getIntFromJson(json_rect, "w");
352 area_size.h = jh::getIntFromJson(json_rect, "h");
354 this->area2size[area] = area_size;
358 for (const auto& itr : this->area2size)
360 HMI_DEBUG("area:%s x:%d y:%d w:%d h:%d",
361 itr.first.c_str(), itr.second.x, itr.second.y,
362 itr.second.w, itr.second.h);
365 // Release json_object
366 json_object_put(json_obj);
368 return WMError::SUCCESS;
371 WMError LayerControl::layoutChange(const WMAction& action)
373 if (action.visible == TaskVisible::INVISIBLE)
375 // Visibility is not change -> no redraw is required
376 return WMError::SUCCESS;
378 if(action.client == nullptr)
380 HMI_SEQ_ERROR(action.req_num, "client may vanish");
381 return WMError::NOT_REGISTERED;
383 unsigned layer = action.client->layerID();
384 unsigned surface = action.client->surfaceID();
386 auto rect = this->getAreaSize(action.area);
387 HMI_DEBUG("Set layout %d, %d, %d, %d",rect.x, rect.y, rect.w, rect.h);
389 ilm_surfaceSetDestinationRectangle(surface, rect.x, rect.y, rect.w, rect.h);
391 for(auto &wm_layer: this->wm_layers)
393 // Store the state who is assigned to the area
394 if(wm_layer->hasLayerID(layer))
396 wm_layer->setAreaToState(action.client->appID(), action.area);
397 /* TODO: manipulate state directly
398 LayerState ls = wm_layer->getLayerState();
399 ls.setArea(action.client->appID(), action.area);
404 return WMError::SUCCESS;
407 WMError LayerControl::visibilityChange(const WMAction& action)
409 WMError ret = WMError::FAIL;
410 if(action.client == nullptr)
412 HMI_SEQ_ERROR(action.req_num, "client may vanish");
413 return WMError::NOT_REGISTERED;
416 if (action.visible == TaskVisible::VISIBLE)
418 ret = this->makeVisible(action.client);
420 else if (action.visible == TaskVisible::INVISIBLE)
422 ret = this->makeInvisible(action.client);
427 void LayerControl::terminateApp(const shared_ptr<WMClient> client)
429 for(auto& l : this->wm_layers)
431 l->terminateApp(client->layerID());
435 void LayerControl::dispatchCreateEvent(ilmObjectType object, unsigned id, bool created)
437 if (ILM_SURFACE == object)
441 ilmSurfaceProperties sp;
443 rc = ilm_getPropertiesOfSurface(id, &sp);
444 if(rc != ILM_SUCCESS)
446 HMI_ERROR("Failed to get surface %d property due to %d", id, ilm_getError());
449 this->cb.surfaceCreated(sp.creatorPid, id);
450 ilm_surfaceAddNotification(id, surfaceCallback_static);
451 ilm_surfaceSetVisibility(id, ILM_TRUE);
455 this->cb.surfaceDestroyed(id);
458 if (ILM_LAYER == object)
462 ilm_layerAddNotification(id, layerCallback_static);
466 // Ignore here. Nothing to do currently.
467 // Process of application dead is handled by Window Manager
468 // from binder notification
473 void LayerControl::dispatchSurfacePropChangeEvent(unsigned id,
474 struct ilmSurfaceProperties* sprop,
475 t_ilm_notification_mask mask)
478 ILM_NOTIFICATION_CONTENT_AVAILABLE & ILM_NOTIFICATION_CONTENT_REMOVED
479 are not handled here, handled in create/destroy event
481 if (ILM_NOTIFICATION_VISIBILITY & mask)
483 HMI_DEBUG("surface %d turns visibility %d", id, sprop->visibility);
485 if (ILM_NOTIFICATION_OPACITY & mask)
487 HMI_DEBUG("surface %d turns opacity %f", id, sprop->opacity);
489 if (ILM_NOTIFICATION_SOURCE_RECT & mask)
491 HMI_DEBUG("surface %d source rect changes", id);
493 if (ILM_NOTIFICATION_DEST_RECT & mask)
495 HMI_DEBUG("surface %d dest rect changes", id);
497 if (ILM_NOTIFICATION_CONFIGURED & mask)
499 HMI_DEBUG("surface %d size %d, %d, %d, %d", id,
500 sprop->sourceX, sprop->sourceY, sprop->origSourceWidth, sprop->origSourceHeight);
501 ilm_surfaceSetSourceRectangle(id, 0, 0, sprop->origSourceWidth, sprop->origSourceHeight);
505 void LayerControl::dispatchLayerPropChangeEvent(unsigned id,
506 struct ilmLayerProperties* lprop,
507 t_ilm_notification_mask mask)
509 if (ILM_NOTIFICATION_VISIBILITY & mask)
511 HMI_DEBUG("layer %d turns visibility %d", id, lprop->visibility);
513 if (ILM_NOTIFICATION_OPACITY & mask)
515 HMI_DEBUG("layer %d turns opacity %f", id, lprop->opacity);
517 if (ILM_NOTIFICATION_SOURCE_RECT & mask)
519 HMI_DEBUG("layer %d source rect changes", id);
521 if (ILM_NOTIFICATION_DEST_RECT & mask)
523 HMI_DEBUG("layer %d dest rect changes", id);
527 WMError LayerControl::makeVisible(const shared_ptr<WMClient> client)
529 WMError ret = WMError::SUCCESS;
530 // Don't check here the client is not nullptr
531 unsigned layer = client->layerID();
533 this->moveForeGround(client);
535 ilm_layerSetVisibility(layer, ILM_TRUE);
537 /* for(auto& wm_layer : this->wm_layers)
539 if(wm_layer->hasLayerID(layer))
541 LayerState ls = wm_layer->getLayerState();
546 // Move foreground from back ground layer
547 /* for(auto& wm_layer : this->wm_layers)
549 if(wm_layer->layerName() == "BackGroundLayer")
551 if(wm_layer->hasRole(client->role()))
553 LayerState ls = wm_layer->getLayerState();
554 ls.removeLayer(layer);
563 WMError LayerControl::makeInvisible(const shared_ptr<WMClient> client)
565 WMError ret = WMError::SUCCESS;
566 unsigned layer = client->layerID(); // Don't check here the client is not nullptr
568 bool mv_ok = this->moveBackGround(client);
572 HMI_INFO("make invisible client %s", client->appID().c_str());
573 ilm_layerSetVisibility(layer, ILM_FALSE);
576 //ilm_layerSetDestinationRectangle(layer, 0, 0, 0, 0);
578 /* for(auto& wm_layer : this->wm_layers)
580 if(wm_layer->hasLayerID(layer))
582 LayerState ls = wm_layer->getLayerState();
583 ls.removeLayer(layer);;
592 bool LayerControl::moveBackGround(const shared_ptr<WMClient> client)
596 // Move background from foreground layer
597 auto bg = this->getWMLayer(BACK_GROUND_LAYER);
600 HMI_DEBUG("client %s role %s", client->appID().c_str(), client->role().c_str());
601 unsigned layer = client->layerID();
602 if(bg->hasRole(client->role()))
604 HMI_INFO("%s go to background", client->appID().c_str());
605 bg->addLayerToState(layer);
606 auto wm_layer = this->getWMLayer(layer);
607 wm_layer->removeLayerFromState(layer);
608 /* TODO: manipulate state directly
609 LayerState bg_ls = bg->getLayerState();
610 bg_ls.addLayer(layer);
611 LayerState ls = wm_layer->getLayerState();
612 ls.removeLayer(layer); */
621 bool LayerControl::moveForeGround(const shared_ptr<WMClient> client)
625 // Move foreground from foreground layer
626 auto bg = this->getWMLayer(BACK_GROUND_LAYER);
629 if(bg->hasRole(client->role()))
631 unsigned layer = client->layerID();
632 HMI_INFO("%s go to foreground", client->appID().c_str());
633 bg->removeLayerFromState(layer);
634 auto wm_layer = this->getWMLayer(layer);
635 wm_layer->addLayerToState(layer);
636 /* TODO: manipulate state directly
637 LayerState bg_ls = bg->getLayerState();
638 bg_ls.removeLayer(layer);
639 LayerState ls = wm_layer->getLayerState();
640 ls.addLayer(layer); */