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"
30 using std::shared_ptr;
34 LayerControl* g_lc_ctxt;
36 static void createCallback_static(ilmObjectType object,
41 static_cast<LayerControl*>(data)->dispatchCreateEvent(object, id, created);
44 static void surfaceCallback_static(t_ilm_surface surface,
45 struct ilmSurfaceProperties* surface_prop,
46 t_ilm_notification_mask mask)
48 g_lc_ctxt->dispatchPropertyChangeEvent(surface, surface_prop, mask);
51 static void layerCallback_static(t_ilm_layer layer,
52 struct ilmLayerProperties* layer_prop,
53 t_ilm_notification_mask mask)
55 g_lc_ctxt->dispatchPropertyChangeEvent(layer, layer_prop, mask);
58 LayerControl::LayerControl(const std::string& root)
60 string area_path = root + LC_AREA_PATH;
61 string layer_path= root + LC_LAYER_SETTING_PATH;
62 // load layers.setting.json
63 WMError ret = this->loadLayerSetting(layer_path);
64 assert(ret == WMError::SUCCESS);
66 ret = this->loadAreaDb(area_path);
67 assert(ret == WMError::SUCCESS);
70 WMError LayerControl::init(const LayerControlCallbacks& cb)
72 HMI_DEBUG("Initialize of ilm library and display");
76 ilmErrorTypes rc = ilm_init();
78 while (rc != ILM_SUCCESS)
83 HMI_ERROR("Could not connect to compositor");
86 HMI_ERROR("Wait to start weston ...");
90 if(rc != ILM_SUCCESS) goto lc_init_error;
92 // Get current screen setting
93 rc = ilm_getScreenIDs(&num, &ids);
95 if(rc != ILM_SUCCESS) goto lc_init_error;
97 for(unsigned i = 0; i < num; i++)
99 HMI_INFO("get screen: %d", ids[i]);
101 // Currently, 0 is only available
102 this->screenID = ids[0];
104 rc = ilm_getPropertiesOfScreen(this->screenID, &this->screen_prop);
106 if(rc != ILM_SUCCESS) goto lc_init_error;
108 // Register Callback from ILM
110 ilm_registerNotification(createCallback_static, this);
112 return WMError::SUCCESS;
115 HMI_ERROR("Failed to initialize. Terminate WM");
117 return WMError::FAIL;
120 void LayerControl::createNewLayer(unsigned id)
122 HMI_INFO("create new ID :%d", id);
123 struct rect rct = this->area2size[LC_DEFAULT_AREA];
124 ilm_layerCreateWithDimension(&id, rct.w, rct.h);
125 //ilm_layerSetSourceRectangle(id, rct.x, rct.y, rct.w, rct.h);
126 //ilm_layerSetDestinationRectangle(id, rct.x, rct.y, rct.w, rct.h);
127 ilm_layerSetOpacity(id, 1.0);
128 ilm_layerSetVisibility(id, ILM_FALSE);
130 auto wm_layer = getWMLayer(id);
131 wm_layer->addLayer(id);
132 this->commitChange();
135 unsigned LayerControl::getNewLayerID(const string& role, string* layer_name)
138 for(const auto& l: this->wm_layers)
140 ret = l->getNewLayerID(role);
143 *layer_name = l->layerName();
144 unsigned uid = l->getUuid();
145 this->lid2wmlid[ret] = uid;
152 shared_ptr<WMLayer> LayerControl::getWMLayer(unsigned layer)
154 unsigned uuid = this->lid2wmlid[layer];
155 return this->wm_layers[uuid];
158 struct rect LayerControl::getAreaSize(const std::string& area)
160 return area2size[area];
163 void LayerControl::setupArea(const rectangle& base_rct, double scaling)
166 this->scaling = scaling;
168 rct = this->area2size["normal.full"];
169 this->area2size["normalfull"] = rct;
170 this->area2size["normal"] = rct;
172 for (auto &i : this->area2size)
174 i.second.x = base_rct.left() + static_cast<int>(scaling * i.second.x + 0.5);
175 i.second.y = base_rct.top() + static_cast<int>(scaling * i.second.y + 0.5);
176 i.second.w = static_cast<int>(scaling * i.second.w + 0.5);
177 i.second.h = static_cast<int>(scaling * i.second.h + 0.5);
179 HMI_DEBUG("area:%s size(after) : x:%d y:%d w:%d h:%d",
180 i.first.c_str(), i.second.x, i.second.y, i.second.w, i.second.h);
184 Screen LayerControl::getScreenInfo()
186 return Screen(this->screen_prop.screenWidth, this->screen_prop.screenHeight);
189 double LayerControl::scale()
191 return this->scaling;
194 WMError LayerControl::updateLayer(LayerState& layer_state)
196 return WMError::SUCCESS;
199 WMError LayerControl::commitChange()
201 HMI_INFO("Commit change");
202 WMError rc = WMError::SUCCESS;
203 vector<unsigned> ivi_l_ids;
204 for(auto& l : this->wm_layers)
206 auto state = l->getLayerState();
207 for(const auto& id : state.getIviIdList())
209 ivi_l_ids.push_back(id);
212 t_ilm_layer* id_array = new t_ilm_layer[ivi_l_ids.size()];
213 if(id_array == nullptr)
215 HMI_WARNING("short memory");
217 return WMError::FAIL;
220 for(const auto& i : ivi_l_ids)
223 HMI_DEBUG("check render order %d", i);
227 ilmErrorTypes ret = ilm_displaySetRenderOrder(this->screenID, id_array, ivi_l_ids.size());
228 if(ret != ILM_SUCCESS)
238 void LayerControl::undoUpdate() {}
240 WMError LayerControl::loadLayerSetting(const string &path)
242 HMI_DEBUG("loading WMLayer(Application Containers) Setting from %s", path);
244 json_object *json_obj, *json_cfg;
245 int ret = jh::inputJsonFilie(path.c_str(), &json_obj);
248 HMI_DEBUG("Could not open %s, so use default area information", path.c_str());
249 return WMError::FAIL;
251 HMI_INFO("json_obj dump:%s", json_object_get_string(json_obj));
253 if (!json_object_object_get_ex(json_obj, "mappings", &json_cfg))
255 HMI_ERROR("Parse Error!!");
256 return WMError::FAIL;
259 int len = json_object_array_length(json_cfg);
260 HMI_DEBUG("json_cfg len:%d", len);
262 for (int i = 0; i < len; i++)
264 json_object *json_tmp = json_object_array_get_idx(json_cfg, i);
265 HMI_DEBUG("> json_tmp dump:%s", json_object_get_string(json_tmp));
267 this->wm_layers.emplace_back(std::make_shared<WMLayer>(json_tmp, i));
269 json_object_put(json_obj);
271 return WMError::SUCCESS;
274 WMError LayerControl::loadAreaDb(const std::string& path)
277 json_object *json_obj;
278 int ret = jh::inputJsonFilie(path.c_str(), &json_obj);
281 HMI_DEBUG("Could not open %s, so use default area information", path.c_str());
282 return WMError::FAIL;
284 HMI_INFO("json_obj dump:%s", json_object_get_string(json_obj));
287 json_object *json_cfg;
288 if (!json_object_object_get_ex(json_obj, "areas", &json_cfg))
290 HMI_ERROR("Parse Error!!");
291 return WMError::FAIL;
294 int len = json_object_array_length(json_cfg);
295 HMI_DEBUG("json_cfg len:%d", len);
298 for (int i = 0; i < len; i++)
300 json_object *json_tmp = json_object_array_get_idx(json_cfg, i);
301 HMI_DEBUG("> json_tmp dump:%s", json_object_get_string(json_tmp));
303 area = jh::getStringFromJson(json_tmp, "name");
306 HMI_ERROR("Parse Error!!");
307 return WMError::FAIL;
309 HMI_DEBUG("> area:%s", area);
311 json_object *json_rect;
312 if (!json_object_object_get_ex(json_tmp, "rect", &json_rect))
314 HMI_ERROR("Parse Error!!");
315 return WMError::FAIL;
317 HMI_DEBUG("> json_rect dump:%s", json_object_get_string(json_rect));
319 struct rect area_size;
320 area_size.x = jh::getIntFromJson(json_rect, "x");
321 area_size.y = jh::getIntFromJson(json_rect, "y");
322 area_size.w = jh::getIntFromJson(json_rect, "w");
323 area_size.h = jh::getIntFromJson(json_rect, "h");
325 this->area2size[area] = area_size;
329 for (const auto& itr : this->area2size)
331 HMI_DEBUG("area:%s x:%d y:%d w:%d h:%d",
332 itr.first.c_str(), itr.second.x, itr.second.y,
333 itr.second.w, itr.second.h);
336 // Release json_object
337 json_object_put(json_obj);
339 return WMError::SUCCESS;
342 WMError LayerControl::layoutChange(const WMAction& action)
344 if (action.visible == TaskVisible::INVISIBLE)
346 // Visibility is not change -> no redraw is required
347 return WMError::SUCCESS;
349 if(action.client == nullptr)
351 HMI_SEQ_ERROR(action.req_num, "client may vanish");
352 return WMError::NOT_REGISTERED;
354 unsigned layer = action.client->layerID();
357 // WMError ret = this->setLayerSize(layer, action.area);
358 auto rect = this->getAreaSize(action.area);
359 HMI_DEBUG("Set layout %d, %d, %d, %d",rect.x, rect.y, rect.w, rect.h);
360 ilm_layerSetSourceRectangle(layer, rect.x, rect.y, rect.w, rect.h);
361 ilm_layerSetDestinationRectangle(layer, rect.x, rect.y, rect.w, rect.h);
362 for(auto &wm_layer: this->wm_layers)
364 if(wm_layer->hasLayerID(layer))
366 LayerState ls = wm_layer->getLayerState();
367 ls.setArea(action.client->appID(), action.area);
371 return WMError::SUCCESS;
374 WMError LayerControl::visibilityChange(const WMAction& action)
376 WMError ret = WMError::FAIL;
377 if(action.client == nullptr)
379 HMI_SEQ_ERROR(action.req_num, "client may vanish");
380 return WMError::NOT_REGISTERED;
383 if (action.visible != TaskVisible::INVISIBLE)
385 ret = this->makeVisible(action.client);
389 ret = this->makeInvisible(action.client);
394 void LayerControl::dispatchCreateEvent(ilmObjectType object, unsigned id, bool created)
396 if (ILM_SURFACE == object)
400 ilmSurfaceProperties sp;
402 rc = ilm_getPropertiesOfSurface(id, &sp);
403 if(rc != ILM_SUCCESS)
405 this->cb.surfaceCreated(sp.creatorPid, id);
406 ilm_surfaceAddNotification(id, surfaceCallback_static);
407 ilm_surfaceSetVisibility(id, ILM_TRUE);
411 // this->cb->surfaceDestroyed(id);
414 if (ILM_LAYER == object)
418 ilm_layerAddNotification(id, layerCallback_static);
419 // this->cb->layerCreated(id);
423 // this->cb->layerDestroyed(id); // Means Application is dead.
428 void LayerControl::dispatchPropertyChangeEvent(unsigned id,
429 struct ilmSurfaceProperties* sprop,
430 t_ilm_notification_mask mask)
432 pid_t pid = sprop->creatorPid;
433 HMI_DEBUG("pid : %d", pid);
435 if (ILM_NOTIFICATION_VISIBILITY & mask)
437 //this->cb->surfaceVisibilityChanged(id, sprop->visibility);
439 if (ILM_NOTIFICATION_OPACITY & mask)
442 if (ILM_NOTIFICATION_ORIENTATION & mask)
445 if (ILM_NOTIFICATION_SOURCE_RECT & mask)
447 // this->cb->surfaceSourceRectChanged(id, )
449 if (ILM_NOTIFICATION_DEST_RECT & mask)
451 // this->cb->surfaceSourceRectChanged(id, )
453 if (ILM_NOTIFICATION_CONTENT_AVAILABLE & mask)
456 if (ILM_NOTIFICATION_CONTENT_REMOVED & mask)
458 /* application being down */
459 // m_appLayers.remove(pid);
461 if (ILM_NOTIFICATION_CONFIGURED & mask)
463 HMI_DEBUG("surface %d available", id);
464 ilm_surfaceSetSourceRectangle(id, 0, 0, sprop->origSourceWidth, sprop->origSourceHeight);
465 ilm_surfaceSetDestinationRectangle(id, 0, 0, sprop->origSourceWidth, sprop->origSourceHeight);
466 /* qDebug("ILM_NOTIFICATION_CONFIGURED");
467 qDebug(" surfaceProperties %d", surface);
468 qDebug(" surfaceProperties.origSourceWidth: %d", surfaceProperties->origSourceWidth);
469 qDebug(" surfaceProperties.origSourceHeight: %d", surfaceProperties->origSourceHeight);
471 if (surface == WINDOWMANAGER_HOMESCREEN_MAIN_SURFACE_ID) {
472 addSurfaceToLayer(surface, WINDOWMANAGER_LAYER_HOMESCREEN);
473 configureHomeScreenMainSurface(surface, surfaceProperties->origSourceWidth, surfaceProperties->origSourceHeight);
475 ilmErrorTypes result;
476 t_ilm_layer layer = addSurfaceToAppLayer(pid, surface);
479 configureAppSurface(surface,
480 surfaceProperties->origSourceWidth,
481 surfaceProperties->origSourceHeight);
483 result = ilm_layerAddSurface(layer, surface);
484 if (result != ILM_SUCCESS) {
485 qDebug("ilm_layerAddSurface(%d,%d) failed.", layer, surface);
494 void LayerControl::dispatchPropertyChangeEvent(unsigned id,
495 struct ilmLayerProperties* lprop,
496 t_ilm_notification_mask mask)
498 if (ILM_NOTIFICATION_VISIBILITY & mask)
500 //this->cb->layerVisibilityChanged(id, sprop->visibility);
502 if (ILM_NOTIFICATION_OPACITY & mask)
505 if (ILM_NOTIFICATION_ORIENTATION & mask)
508 if (ILM_NOTIFICATION_SOURCE_RECT & mask)
510 // this->cb->surfaceSourceRectChanged(id, )
512 if (ILM_NOTIFICATION_DEST_RECT & mask)
514 // this->cb->surfaceSourceRectChanged(id, )
516 if (ILM_NOTIFICATION_CONTENT_AVAILABLE & mask)
519 if (ILM_NOTIFICATION_CONTENT_REMOVED & mask)
521 /* application being down */
522 // m_appLayers.remove(pid);
524 if (ILM_NOTIFICATION_CONFIGURED & mask)
526 /* qDebug("ILM_NOTIFICATION_CONFIGURED");
527 qDebug(" surfaceProperties %d", surface);
528 qDebug(" surfaceProperties.origSourceWidth: %d", surfaceProperties->origSourceWidth);
529 qDebug(" surfaceProperties.origSourceHeight: %d", surfaceProperties->origSourceHeight);
531 if (surface == WINDOWMANAGER_HOMESCREEN_MAIN_SURFACE_ID) {
532 addSurfaceToLayer(surface, WINDOWMANAGER_LAYER_HOMESCREEN);
533 configureHomeScreenMainSurface(surface, surfaceProperties->origSourceWidth, surfaceProperties->origSourceHeight);
535 ilmErrorTypes result;
536 t_ilm_layer layer = addSurfaceToAppLayer(pid, surface);
539 configureAppSurface(surface,
540 surfaceProperties->origSourceWidth,
541 surfaceProperties->origSourceHeight);
543 result = ilm_layerAddSurface(layer, surface);
544 if (result != ILM_SUCCESS) {
545 qDebug("ilm_layerAddSurface(%d,%d) failed.", layer, surface);
554 WMError LayerControl::makeVisible(const shared_ptr<WMClient> client)
556 WMError ret = WMError::SUCCESS;
557 // Don't check here the client is not nullptr
558 unsigned layer = client->layerID();
560 ilm_layerSetVisibility(layer, ILM_TRUE);
562 /* for(auto& wm_layer : this->wm_layers)
564 if(wm_layer->hasLayerID(layer))
566 LayerState ls = wm_layer->getLayerState();
571 // Move foreground from back ground layer
572 /* for(auto& wm_layer : this->wm_layers)
574 if(wm_layer->layerName() == "BackGroundLayer")
576 if(wm_layer->hasRole(client->role()))
578 LayerState ls = wm_layer->getLayerState();
579 ls.removeLayer(layer);
588 WMError LayerControl::makeInvisible(const shared_ptr<WMClient> client)
590 WMError ret = WMError::SUCCESS;
591 unsigned layer = client->layerID(); // Don't check here the client is not nullptr
593 /* bool mv_ok = this->mvBackGround(client);
597 ilm_layerSetVisibility(layer, ILM_FALSE);
600 ilm_layerSetDestinationRectangle(layer, 0, 0, 0, 0);
602 /* for(auto& wm_layer : this->wm_layers)
604 if(wm_layer->hasLayerID(layer))
606 LayerState ls = wm_layer->getLayerState();
607 ls.removeLayer(layer);;
616 /* bool LayerControl::mvBackGround(const shared_ptr<WMClient> client)
620 // Move background from foreground layer
621 auto bg = this->getWMLayer("BackGroundLayer");
624 unsigned layer = client->layerID();
625 if(bg->hasRole(client->role()))
627 LayerState bg_ls = bg->getLayerState();
628 bg_ls.addLayer(layer);
629 auto wm_layer = this->getWMLayer(layer);
630 LayerState ls = wm_layer->getLayerState();
631 ls.removeLayer(layer);
638 bool LayerControl::mvForeGround(const shared_ptr<WMClient> client)
642 // Move foreground from foreground layer
643 auto bg = this->getWMLayer("BackGroundLayer");
646 unsigned layer = client->layerID();
647 if(bg->hasRole(client->role()))
649 LayerState bg_ls = bg->getLayerState();
650 bg_ls.removeLayer(layer);
651 auto wm_layer = this->getWMLayer(layer);
652 LayerState ls = wm_layer->getLayerState();