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 "normal.full"
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 ilmErrorTypes rc = ilm_init();
77 while (rc != ILM_SUCCESS)
82 HMI_ERROR("Could not connect to compositor");
85 HMI_ERROR("Wait to start weston ...");
89 if(rc != ILM_SUCCESS) goto lc_init_error;
91 // Get current screen setting
92 rc = ilm_getScreenIDs(&num, &ids);
94 if(rc != ILM_SUCCESS) goto lc_init_error;
96 for(unsigned i = 0; i < num; i++)
98 HMI_INFO("get screen: %d", ids[i]);
100 // Currently, 0 is only available
101 this->screenID = ids[0];
103 rc = ilm_getPropertiesOfScreen(this->screenID, &this->screen_prop);
105 if(rc != ILM_SUCCESS) goto lc_init_error;
107 // Register Callback from ILM
109 ilm_registerNotification(createCallback_static, this);
111 return WMError::SUCCESS;
114 HMI_ERROR("Failed to initialize. Terminate WM");
116 return WMError::FAIL;
119 void LayerControl::createNewLayer(unsigned id)
121 HMI_INFO("create new ID :%d", id);
122 struct rect rct = this->area2size[LC_DEFAULT_AREA];
123 ilm_layerCreateWithDimension(&id, rct.w, rct.h);
124 ilm_layerSetSourceRectangle(id, rct.x, rct.y, rct.w, rct.h);
125 //ilm_layerSetDestinationRectangle(id, rct.x, rct.y, rct.w, rct.h);
126 ilm_layerSetOpacity(id, 1.0);
127 ilm_layerSetVisibility(id, ILM_TRUE);
131 unsigned LayerControl::getNewLayerID(const string& role, string* layer_name)
134 for(const auto& l: this->wm_layers)
136 ret = l->getNewLayerID(role);
139 *layer_name = l->layerName();
146 struct rect LayerControl::getAreaSize(const std::string& area)
148 return area2size[area];
151 void LayerControl::setupArea(double scaling)
154 this->scaling = scaling;
156 rct = this->area2size["normal.full"];
157 this->area2size["normalfull"] = rct;
158 this->area2size["normal"] = rct;
160 for (auto &i : this->area2size)
162 i.second.x = static_cast<int>(scaling * i.second.x + 0.5);
163 i.second.y = static_cast<int>(scaling * i.second.y + 0.5);
164 i.second.w = static_cast<int>(scaling * i.second.w + 0.5);
165 i.second.h = static_cast<int>(scaling * i.second.h + 0.5);
167 HMI_DEBUG("wm:lm", "area:%s size(after) : x:%d y:%d w:%d h:%d",
168 i.first.c_str(), i.second.x, i.second.y, i.second.w, i.second.h);
172 Screen LayerControl::getScreenInfo()
174 return Screen(this->screen_prop.screenWidth, this->screen_prop.screenHeight);
177 double LayerControl::scale()
179 return this->scaling;
182 WMError LayerControl::updateLayer(LayerState& layer_state)
184 return WMError::SUCCESS;
187 WMError LayerControl::commitChange()
189 WMError rc = WMError::SUCCESS;
190 vector<unsigned> ivi_l_ids;
191 for(const auto& l : this->wm_layers)
193 auto state = l->getLayerState();
194 for(const auto& id : state.getIviIdList())
196 ivi_l_ids.push_back(id);
199 t_ilm_layer* id_array = new t_ilm_layer[ivi_l_ids.size()];
200 if(id_array == nullptr)
202 HMI_WARNING("short memory");
204 return WMError::FAIL;
207 for(const auto& i : ivi_l_ids)
213 ilmErrorTypes ret = ilm_displaySetRenderOrder(this->screenID, id_array, ivi_l_ids.size());
214 if(ret != ILM_SUCCESS)
223 void LayerControl::undoUpdate() {}
225 WMError LayerControl::loadLayerSetting(const string &path)
227 HMI_DEBUG("loading WMLayer(Application Containers) Setting from %s", path);
229 json_object *json_obj, *json_cfg;
230 int ret = jh::inputJsonFilie(path.c_str(), &json_obj);
233 HMI_DEBUG("Could not open %s, so use default area information", path.c_str());
234 return WMError::FAIL;
236 HMI_INFO("json_obj dump:%s", json_object_get_string(json_obj));
238 if (!json_object_object_get_ex(json_obj, "mappings", &json_cfg))
240 HMI_ERROR("Parse Error!!");
241 return WMError::FAIL;
244 int len = json_object_array_length(json_cfg);
245 HMI_DEBUG("json_cfg len:%d", len);
247 for (int i = 0; i < len; i++)
249 json_object *json_tmp = json_object_array_get_idx(json_cfg, i);
250 HMI_DEBUG("> json_tmp dump:%s", json_object_get_string(json_tmp));
252 this->wm_layers.emplace_back(std::make_shared<WMLayer>(json_tmp));
255 return WMError::SUCCESS;
258 WMError LayerControl::loadAreaDb(const std::string& path)
261 json_object *json_obj;
262 int ret = jh::inputJsonFilie(path.c_str(), &json_obj);
265 HMI_DEBUG("Could not open %s, so use default area information", path.c_str());
266 return WMError::FAIL;
268 HMI_INFO("json_obj dump:%s", json_object_get_string(json_obj));
271 json_object *json_cfg;
272 if (!json_object_object_get_ex(json_obj, "areas", &json_cfg))
274 HMI_ERROR("Parse Error!!");
275 return WMError::FAIL;
278 int len = json_object_array_length(json_cfg);
279 HMI_DEBUG("json_cfg len:%d", len);
282 for (int i = 0; i < len; i++)
284 json_object *json_tmp = json_object_array_get_idx(json_cfg, i);
285 HMI_DEBUG("> json_tmp dump:%s", json_object_get_string(json_tmp));
287 area = jh::getStringFromJson(json_tmp, "name");
290 HMI_ERROR("Parse Error!!");
291 return WMError::FAIL;
293 HMI_DEBUG("> area:%s", area);
295 json_object *json_rect;
296 if (!json_object_object_get_ex(json_tmp, "rect", &json_rect))
298 HMI_ERROR("Parse Error!!");
299 return WMError::FAIL;
301 HMI_DEBUG("> json_rect dump:%s", json_object_get_string(json_rect));
303 struct rect area_size;
304 area_size.x = jh::getIntFromJson(json_rect, "x");
305 area_size.y = jh::getIntFromJson(json_rect, "y");
306 area_size.w = jh::getIntFromJson(json_rect, "w");
307 area_size.h = jh::getIntFromJson(json_rect, "h");
309 this->area2size[area] = area_size;
313 for (const auto& itr : this->area2size)
315 HMI_DEBUG("area:%s x:%d y:%d w:%d h:%d",
316 itr.first.c_str(), itr.second.x, itr.second.y,
317 itr.second.w, itr.second.h);
320 // Release json_object
321 json_object_put(json_obj);
323 return WMError::SUCCESS;
326 WMError LayerControl::layoutChange(const WMAction& action)
328 WMError ret = WMError::FAIL;
329 if (action.visible == TaskVisible::INVISIBLE)
331 // Visibility is not change -> no redraw is required
332 return WMError::SUCCESS;
334 if(action.client == nullptr)
336 HMI_SEQ_ERROR(action.req_num, "client may vanish");
337 return WMError::NOT_REGISTERED;
339 unsigned layer = action.client->layerID();
342 // WMError ret = this->setLayerSize(layer, action.area);
343 auto rect = this->getAreaSize(action.area);
344 ilmErrorTypes err = ilm_layerSetDestinationRectangle(layer, rect.x, rect.y, rect.w, rect.h);
345 for(auto &wm_layer: this->wm_layers)
347 if(wm_layer->hasLayerID(layer))
349 LayerState ls = wm_layer->getLayerState();
350 ls.setArea(action.client->appID(), action.area);
353 if(err == ILM_SUCCESS)
355 ret = WMError::SUCCESS;
360 WMError LayerControl::visibilityChange(const WMAction& action)
362 WMError ret = WMError::FAIL;
363 if(action.client == nullptr)
365 HMI_SEQ_ERROR(action.req_num, "client may vanish");
366 return WMError::NOT_REGISTERED;
369 if (action.visible != TaskVisible::INVISIBLE)
371 ret = this->makeVisible(action.client);
375 ret = this->makeInvisible(action.client);
380 void LayerControl::dispatchCreateEvent(ilmObjectType object, unsigned id, bool created)
383 if (ILM_SURFACE == object)
387 ilmSurfaceProperties sp;
389 rc = ilm_getPropertiesOfSurface(id, &sp);
390 if(rc != ILM_SUCCESS)
392 // this->cb->surfaceCreated(pid, id);
393 ilm_surfaceAddNotification(id, surfaceCallback_static);
394 ilm_surfaceSetSourceRectangle(id, 0, 0, sp.origSourceWidth, sp.origSourceHeight);
398 // this->cb->surfaceDestroyed(id);
401 if (ILM_LAYER == object)
405 ilm_layerAddNotification(id, layerCallback_static);
406 // this->cb->layerCreated(id);
410 // this->cb->layerDestroyed(id); // Means Application is dead.
415 void LayerControl::dispatchPropertyChangeEvent(unsigned id,
416 struct ilmSurfaceProperties* sprop,
417 t_ilm_notification_mask mask)
419 pid_t pid = sprop->creatorPid;
420 HMI_DEBUG("pid : %d", pid);
422 if (ILM_NOTIFICATION_VISIBILITY & mask)
424 //this->cb->surfaceVisibilityChanged(id, sprop->visibility);
426 if (ILM_NOTIFICATION_OPACITY & mask)
429 if (ILM_NOTIFICATION_ORIENTATION & mask)
432 if (ILM_NOTIFICATION_SOURCE_RECT & mask)
434 // this->cb->surfaceSourceRectChanged(id, )
436 if (ILM_NOTIFICATION_DEST_RECT & mask)
438 // this->cb->surfaceSourceRectChanged(id, )
440 if (ILM_NOTIFICATION_CONTENT_AVAILABLE & mask)
443 if (ILM_NOTIFICATION_CONTENT_REMOVED & mask)
445 /* application being down */
446 // m_appLayers.remove(pid);
448 if (ILM_NOTIFICATION_CONFIGURED & mask)
450 /* qDebug("ILM_NOTIFICATION_CONFIGURED");
451 qDebug(" surfaceProperties %d", surface);
452 qDebug(" surfaceProperties.origSourceWidth: %d", surfaceProperties->origSourceWidth);
453 qDebug(" surfaceProperties.origSourceHeight: %d", surfaceProperties->origSourceHeight);
455 if (surface == WINDOWMANAGER_HOMESCREEN_MAIN_SURFACE_ID) {
456 addSurfaceToLayer(surface, WINDOWMANAGER_LAYER_HOMESCREEN);
457 configureHomeScreenMainSurface(surface, surfaceProperties->origSourceWidth, surfaceProperties->origSourceHeight);
459 ilmErrorTypes result;
460 t_ilm_layer layer = addSurfaceToAppLayer(pid, surface);
463 configureAppSurface(surface,
464 surfaceProperties->origSourceWidth,
465 surfaceProperties->origSourceHeight);
467 result = ilm_layerAddSurface(layer, surface);
468 if (result != ILM_SUCCESS) {
469 qDebug("ilm_layerAddSurface(%d,%d) failed.", layer, surface);
478 void LayerControl::dispatchPropertyChangeEvent(unsigned id,
479 struct ilmLayerProperties* lprop,
480 t_ilm_notification_mask mask)
482 if (ILM_NOTIFICATION_VISIBILITY & mask)
484 //this->cb->layerVisibilityChanged(id, sprop->visibility);
486 if (ILM_NOTIFICATION_OPACITY & mask)
489 if (ILM_NOTIFICATION_ORIENTATION & mask)
492 if (ILM_NOTIFICATION_SOURCE_RECT & mask)
494 // this->cb->surfaceSourceRectChanged(id, )
496 if (ILM_NOTIFICATION_DEST_RECT & mask)
498 // this->cb->surfaceSourceRectChanged(id, )
500 if (ILM_NOTIFICATION_CONTENT_AVAILABLE & mask)
503 if (ILM_NOTIFICATION_CONTENT_REMOVED & mask)
505 /* application being down */
506 // m_appLayers.remove(pid);
508 if (ILM_NOTIFICATION_CONFIGURED & mask)
510 /* qDebug("ILM_NOTIFICATION_CONFIGURED");
511 qDebug(" surfaceProperties %d", surface);
512 qDebug(" surfaceProperties.origSourceWidth: %d", surfaceProperties->origSourceWidth);
513 qDebug(" surfaceProperties.origSourceHeight: %d", surfaceProperties->origSourceHeight);
515 if (surface == WINDOWMANAGER_HOMESCREEN_MAIN_SURFACE_ID) {
516 addSurfaceToLayer(surface, WINDOWMANAGER_LAYER_HOMESCREEN);
517 configureHomeScreenMainSurface(surface, surfaceProperties->origSourceWidth, surfaceProperties->origSourceHeight);
519 ilmErrorTypes result;
520 t_ilm_layer layer = addSurfaceToAppLayer(pid, surface);
523 configureAppSurface(surface,
524 surfaceProperties->origSourceWidth,
525 surfaceProperties->origSourceHeight);
527 result = ilm_layerAddSurface(layer, surface);
528 if (result != ILM_SUCCESS) {
529 qDebug("ilm_layerAddSurface(%d,%d) failed.", layer, surface);
538 WMError LayerControl::makeVisible(const shared_ptr<WMClient> client)
540 WMError ret = WMError::SUCCESS;
541 // Don't check here the client is not nullptr
542 unsigned layer = client->layerID();
544 for(auto& wm_layer : this->wm_layers)
546 if(wm_layer->hasLayerID(layer))
548 LayerState ls = wm_layer->getLayerState();
553 // Move foreground from back ground layer
554 for(auto& wm_layer : this->wm_layers)
556 if(wm_layer->layerName() == "BackGroundLayer")
558 if(wm_layer->hasRole(client->role()))
560 LayerState ls = wm_layer->getLayerState();
561 ls.removeLayer(layer);
570 WMError LayerControl::makeInvisible(const shared_ptr<WMClient> client)
572 WMError ret = WMError::SUCCESS;
573 // Don't check here the client is not nullptr
574 unsigned layer = client->layerID();
576 for(auto& wm_layer : this->wm_layers)
578 if(wm_layer->hasLayerID(layer))
580 LayerState ls = wm_layer->getLayerState();
581 ls.removeLayer(layer);;
585 // Move foreground from back ground layer
586 for(auto& wm_layer : this->wm_layers)
588 if(wm_layer->layerName() == "BackGroundLayer")
590 if(wm_layer->hasRole(client->role()))
592 LayerState ls = wm_layer->getLayerState();