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.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)
139 for(const auto& l: this->wm_layers)
141 ret = l->getNewLayerID(role);
144 unsigned wmlid = l->getWMLayerID();
145 this->lid2wmlid[ret] = wmlid;
152 shared_ptr<WMLayer> LayerControl::getWMLayer(unsigned layer)
154 unsigned wm_lid = this->lid2wmlid[layer];
155 return this->wm_layers[wm_lid];
158 std::shared_ptr<WMLayer> LayerControl::getWMLayer(std::string layer_name)
160 for(auto &l : this->wm_layers)
162 if(l->layerName() == layer_name)
170 struct rect LayerControl::getAreaSize(const std::string& area)
172 return area2size[area];
175 void LayerControl::setupArea(const rectangle& base_rct, double scaling)
177 this->scaling = scaling;
178 this->offset_x = base_rct.left();
179 this->offset_y = base_rct.top();
181 for (auto &i : this->area2size)
183 i.second.x = static_cast<int>(scaling * i.second.x + 0.5);
184 i.second.y = static_cast<int>(scaling * i.second.y + 0.5);
185 i.second.w = static_cast<int>(scaling * i.second.w + 0.5);
186 i.second.h = static_cast<int>(scaling * i.second.h + 0.5);
188 HMI_DEBUG("area:%s size(after) : x:%d y:%d w:%d h:%d",
189 i.first.c_str(), i.second.x, i.second.y, i.second.w, i.second.h);
193 Screen LayerControl::getScreenInfo()
195 return Screen(this->screen_prop.screenWidth, this->screen_prop.screenHeight);
198 double LayerControl::scale()
200 return this->scaling;
203 WMError LayerControl::renderLayers()
205 HMI_INFO("Commit change");
206 WMError rc = WMError::SUCCESS;
208 // Check the number of layers
209 vector<unsigned> ivi_l_ids;
210 for(auto& l : this->wm_layers)
212 auto state = l->getLayerState();
213 HMI_DEBUG("layer %s", l->layerName().c_str());
214 for(const auto& id : state.getIviIdList())
216 HMI_DEBUG("Add %d", id);
217 ivi_l_ids.push_back(id);
221 // Create render order
222 t_ilm_layer* id_array = new t_ilm_layer[ivi_l_ids.size()];
223 if(id_array == nullptr)
225 HMI_WARNING("short memory");
227 return WMError::FAIL;
230 for(const auto& i : ivi_l_ids)
237 ilmErrorTypes ret = ilm_displaySetRenderOrder(this->screenID, id_array, ivi_l_ids.size());
238 if(ret != ILM_SUCCESS)
245 for(auto& l : this->wm_layers)
255 WMError LayerControl::setXDGSurfaceOriginSize(unsigned surface)
257 WMError ret = WMError::NOT_REGISTERED;
258 ilmSurfaceProperties prop;
259 ilmErrorTypes rc = ilm_getPropertiesOfSurface(surface, &prop);
260 if(rc == ILM_SUCCESS)
262 HMI_INFO("xdg surface info %d, %d", prop.origSourceWidth, prop.origSourceHeight);
263 ilm_surfaceSetSourceRectangle(surface, 0, 0, prop.origSourceWidth, prop.origSourceHeight);
264 ret = WMError::SUCCESS;
270 void LayerControl::undoUpdate()
272 for(auto& l : this->wm_layers)
278 WMError LayerControl::loadLayerSetting(const string &path)
280 HMI_DEBUG("loading WMLayer(Application Containers) Setting from %s", path);
282 json_object *json_obj, *json_cfg;
283 int ret = jh::inputJsonFilie(path.c_str(), &json_obj);
286 HMI_ERROR("Could not open %s", path.c_str());
287 return WMError::FAIL;
289 HMI_INFO("json_obj dump:%s", json_object_get_string(json_obj));
291 if (!json_object_object_get_ex(json_obj, "mappings", &json_cfg))
293 HMI_ERROR("Parse Error!!");
294 return WMError::FAIL;
297 int len = json_object_array_length(json_cfg);
298 HMI_DEBUG("json_cfg len:%d", len);
300 for (int i = 0; i < len; i++)
302 json_object *json_tmp = json_object_array_get_idx(json_cfg, i);
303 HMI_DEBUG("> json_tmp dump:%s", json_object_get_string(json_tmp));
305 this->wm_layers.emplace_back(std::make_shared<WMLayer>(json_tmp, i));
307 json_object_put(json_obj);
309 return WMError::SUCCESS;
312 WMError LayerControl::loadAreaDb(const std::string& path)
315 json_object *json_obj;
316 int ret = jh::inputJsonFilie(path.c_str(), &json_obj);
319 HMI_ERROR("Could not open %s", path.c_str());
320 return WMError::FAIL;
322 HMI_INFO("json_obj dump:%s", json_object_get_string(json_obj));
325 json_object *json_cfg;
326 if (!json_object_object_get_ex(json_obj, "areas", &json_cfg))
328 HMI_ERROR("Parse Error!!");
329 return WMError::FAIL;
332 int len = json_object_array_length(json_cfg);
333 HMI_DEBUG("json_cfg len:%d", len);
336 for (int i = 0; i < len; i++)
338 json_object *json_tmp = json_object_array_get_idx(json_cfg, i);
339 HMI_DEBUG("> json_tmp dump:%s", json_object_get_string(json_tmp));
341 area = jh::getStringFromJson(json_tmp, "name");
344 HMI_ERROR("Parse Error!!");
345 return WMError::FAIL;
347 HMI_DEBUG("> area:%s", area);
349 json_object *json_rect;
350 if (!json_object_object_get_ex(json_tmp, "rect", &json_rect))
352 HMI_ERROR("Parse Error!!");
353 return WMError::FAIL;
355 HMI_DEBUG("> json_rect dump:%s", json_object_get_string(json_rect));
357 struct rect area_size;
358 area_size.x = jh::getIntFromJson(json_rect, "x");
359 area_size.y = jh::getIntFromJson(json_rect, "y");
360 area_size.w = jh::getIntFromJson(json_rect, "w");
361 area_size.h = jh::getIntFromJson(json_rect, "h");
363 this->area2size[area] = area_size;
367 for (const auto& itr : this->area2size)
369 HMI_DEBUG("area:%s x:%d y:%d w:%d h:%d",
370 itr.first.c_str(), itr.second.x, itr.second.y,
371 itr.second.w, itr.second.h);
374 // Release json_object
375 json_object_put(json_obj);
377 return WMError::SUCCESS;
380 WMError LayerControl::layoutChange(const WMAction& action)
382 if (action.visible == TaskVisible::INVISIBLE)
384 // Visibility is not change -> no redraw is required
385 return WMError::SUCCESS;
387 if(action.client == nullptr)
389 HMI_SEQ_ERROR(action.req_num, "client may vanish");
390 return WMError::NOT_REGISTERED;
392 unsigned layer = action.client->layerID();
393 unsigned surface = action.client->surfaceID();
395 auto rect = this->getAreaSize(action.area);
396 HMI_DEBUG("Set layout %d, %d, %d, %d",rect.x, rect.y, rect.w, rect.h);
398 ilm_surfaceSetDestinationRectangle(surface, rect.x, rect.y, rect.w, rect.h);
400 for(auto &wm_layer: this->wm_layers)
402 // Store the state who is assigned to the area
403 if(wm_layer->hasLayerID(layer))
405 wm_layer->attachAppToArea(action.client->appID(), action.area);
406 /* TODO: manipulate state directly
407 LayerState ls = wm_layer->getLayerState();
408 ls.seattachAppToAreatArea(action.client->appID(), action.area);
413 return WMError::SUCCESS;
416 WMError LayerControl::visibilityChange(const WMAction& action)
418 WMError ret = WMError::FAIL;
419 if(action.client == nullptr)
421 HMI_SEQ_ERROR(action.req_num, "client may vanish");
422 return WMError::NOT_REGISTERED;
425 if (action.visible == TaskVisible::VISIBLE)
427 ret = this->makeVisible(action.client);
429 else if (action.visible == TaskVisible::INVISIBLE)
431 ret = this->makeInvisible(action.client);
437 void LayerControl::appTerminated(const shared_ptr<WMClient> client)
439 for(auto& l : this->wm_layers)
441 if(l->hasLayerID(client->layerID()))
443 l->appTerminated(client->layerID());
448 void LayerControl::dispatchCreateEvent(ilmObjectType object, unsigned id, bool created)
450 if (ILM_SURFACE == object)
454 ilmSurfaceProperties sp;
456 rc = ilm_getPropertiesOfSurface(id, &sp);
457 if(rc != ILM_SUCCESS)
459 HMI_ERROR("Failed to get surface %d property due to %d", id, ilm_getError());
462 this->cb.surfaceCreated(sp.creatorPid, id);
463 ilm_surfaceAddNotification(id, surfaceCallback_static);
464 ilm_surfaceSetVisibility(id, ILM_TRUE);
465 ilm_surfaceSetType(id, ILM_SURFACETYPE_DESKTOP);
469 this->cb.surfaceDestroyed(id);
472 if (ILM_LAYER == object)
476 ilm_layerAddNotification(id, layerCallback_static);
480 // Ignore here. Nothing to do currently.
481 // Process of application dead is handled by Window Manager
482 // from binder notification
487 void LayerControl::dispatchSurfacePropChangeEvent(unsigned id,
488 struct ilmSurfaceProperties* sprop,
489 t_ilm_notification_mask mask)
492 ILM_NOTIFICATION_CONTENT_AVAILABLE & ILM_NOTIFICATION_CONTENT_REMOVED
493 are not handled here, handled in create/destroy event
495 if (ILM_NOTIFICATION_VISIBILITY & mask)
497 HMI_DEBUG("surface %d turns visibility %d", id, sprop->visibility);
499 if (ILM_NOTIFICATION_OPACITY & mask)
501 HMI_DEBUG("surface %d turns opacity %f", id, sprop->opacity);
503 if (ILM_NOTIFICATION_SOURCE_RECT & mask)
505 HMI_DEBUG("surface %d source rect changes", id);
507 if (ILM_NOTIFICATION_DEST_RECT & mask)
509 HMI_DEBUG("surface %d dest rect changes", id);
511 if (ILM_NOTIFICATION_CONFIGURED & mask)
513 HMI_DEBUG("surface %d size %d, %d, %d, %d", id,
514 sprop->sourceX, sprop->sourceY, sprop->origSourceWidth, sprop->origSourceHeight);
515 ilm_surfaceSetSourceRectangle(id, 0, 0, sprop->origSourceWidth, sprop->origSourceHeight);
519 void LayerControl::dispatchLayerPropChangeEvent(unsigned id,
520 struct ilmLayerProperties* lprop,
521 t_ilm_notification_mask mask)
523 if (ILM_NOTIFICATION_VISIBILITY & mask)
525 HMI_DEBUG("layer %d turns visibility %d", id, lprop->visibility);
527 if (ILM_NOTIFICATION_OPACITY & mask)
529 HMI_DEBUG("layer %d turns opacity %f", id, lprop->opacity);
531 if (ILM_NOTIFICATION_SOURCE_RECT & mask)
533 HMI_DEBUG("layer %d source rect changes", id);
535 if (ILM_NOTIFICATION_DEST_RECT & mask)
537 HMI_DEBUG("layer %d dest rect changes", id);
541 WMError LayerControl::makeVisible(const shared_ptr<WMClient> client)
543 WMError ret = WMError::SUCCESS;
544 // Don't check here wheher client is nullptr or not
545 unsigned layer = client->layerID();
547 this->moveForeGround(client);
549 ilm_layerSetVisibility(layer, ILM_TRUE);
554 WMError LayerControl::makeInvisible(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 bool mv_ok = this->moveBackGround(client);
564 HMI_INFO("make invisible client %s", client->appID().c_str());
565 ilm_layerSetVisibility(layer, ILM_FALSE);
571 bool LayerControl::moveBackGround(const shared_ptr<WMClient> client)
575 // Move background from foreground layer
576 auto bg = this->getWMLayer(BACK_GROUND_LAYER);
579 HMI_DEBUG("client %s role %s", client->appID().c_str(), client->role().c_str());
580 unsigned layer = client->layerID();
581 if(bg->hasRole(client->role()))
583 HMI_INFO("%s go to background", client->appID().c_str());
584 bg->addLayerToState(layer);
585 auto wm_layer = this->getWMLayer(layer);
586 wm_layer->removeLayerFromState(layer);
587 /* TODO: manipulate state directly
588 LayerState bg_ls = bg->getLayerState();
589 bg_ls.addLayer(layer);
590 LayerState ls = wm_layer->getLayerState();
591 ls.removeLayer(layer); */
600 bool LayerControl::moveForeGround(const shared_ptr<WMClient> client)
604 // Move foreground from foreground layer
605 auto bg = this->getWMLayer(BACK_GROUND_LAYER);
608 if(bg->hasRole(client->role()))
610 unsigned layer = client->layerID();
611 HMI_INFO("%s go to foreground", client->appID().c_str());
612 bg->removeLayerFromState(layer);
613 auto wm_layer = this->getWMLayer(layer);
614 wm_layer->addLayerToState(layer);
615 /* TODO: manipulate state directly
616 LayerState bg_ls = bg->getLayerState();
617 bg_ls.removeLayer(layer);
618 LayerState ls = wm_layer->getLayerState();
619 ls.addLayer(layer); */