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.
20 #include "window_manager.hpp"
21 #include "json_helper.hpp"
22 #include "wm_config.hpp"
23 #include "applist.hpp"
27 #include <systemd/sd-event.h>
33 static const unsigned kTimeOut = 3000000UL; /* 3s */
35 /* DrawingArea name used by "{layout}.{area}" */
36 const char kNameLayoutNormal[] = "normal";
37 const char kNameLayoutSplit[] = "split";
38 const char kNameAreaFull[] = "full";
39 const char kNameAreaMain[] = "main";
40 const char kNameAreaSub[] = "sub";
42 /* Key for json obejct */
43 const char kKeyDrawingName[] = "drawing_name";
44 const char kKeyDrawingArea[] = "drawing_area";
45 const char kKeyDrawingRect[] = "drawing_rect";
46 const char kKeyX[] = "x";
47 const char kKeyY[] = "y";
48 const char kKeyWidth[] = "width";
49 const char kKeyHeight[] = "height";
50 const char kKeyWidthPixel[] = "width_pixel";
51 const char kKeyHeightPixel[] = "height_pixel";
52 const char kKeyWidthMm[] = "width_mm";
53 const char kKeyHeightMm[] = "height_mm";
54 const char kKeyIds[] = "ids";
56 static sd_event_source *g_timer_ev_src = nullptr;
57 static AppList g_app_list;
58 static WindowManager *g_context;
65 result<json> file_to_json(char const *filename)
68 std::ifstream i(filename);
71 HMI_DEBUG("wm", "Could not open config file, so use default layer information");
72 j = default_layers_json;
82 struct result<layer_map> load_layer_map(char const *filename)
84 HMI_DEBUG("wm", "loading IDs from %s", filename);
86 auto j = file_to_json(filename);
89 return Err<layer_map>(j.unwrap_err());
91 json jids = j.unwrap();
93 return to_layer_map(jids);
96 static int processTimerHandler(sd_event_source *s, uint64_t usec, void *userdata)
98 HMI_NOTICE("wm", "Time out occurs because the client replys endDraw slow, so revert the request");
99 reinterpret_cast<wm::WindowManager *>(userdata)->timerHandler();
103 static void onStateTransitioned(std::vector<WMAction> actions)
105 g_context->startTransitionWrapper(actions);
108 static void onError()
110 g_context->processError(WMError::LAYOUT_CHANGE_FAIL);
117 WindowManager::WindowManager(wl::display *d)
124 pending_events(false)
126 char const *path_layers_json = getenv("AFM_APP_INSTALL_DIR");
128 if (!path_layers_json)
130 HMI_ERROR("wm", "AFM_APP_INSTALL_DIR is not defined");
131 path = std::string(path_layers_json);
135 path = std::string(path_layers_json) + std::string("/etc/layers.json");
141 auto l = load_layer_map(path.c_str());
144 this->layers = l.unwrap();
148 HMI_ERROR("wm", "%s", l.err().value());
152 catch (std::exception &e)
154 HMI_ERROR("wm", "Loading of configuration failed: %s", e.what());
158 int WindowManager::init()
161 if (!this->display->ok())
166 if (this->layers.mapping.empty())
168 HMI_ERROR("wm", "No surface -> layer mapping loaded");
172 // TODO: application requests by old role,
173 // so create role map (old, new)
175 this->loadOldRoleDb();
177 // Store my context for calling callback from PolicyManager
180 // Initialize PMWrapper
181 this->pmw.initialize();
183 // Register callback to PolicyManager
184 this->pmw.registerCallback(onStateTransitioned, onError);
187 for (int i = Event_Val_Min; i <= Event_Val_Max; i++)
189 map_afb_event[kListEventName[i]] = afb_daemon_make_event(kListEventName[i]);
192 this->display->add_global_handler(
193 "wl_output", [this](wl_registry *r, uint32_t name, uint32_t v) {
194 this->outputs.emplace_back(std::make_unique<wl::output>(r, name, v));
197 this->display->add_global_handler(
198 "ivi_wm", [this](wl_registry *r, uint32_t name, uint32_t v) {
200 std::make_unique<struct compositor::controller>(r, name, v);
202 // Init controller hooks
203 this->controller->chooks = &this->chooks;
205 // This protocol needs the output, so lets just add our mapping here...
206 this->controller->add_proxy_to_id_mapping(
207 this->outputs.back()->proxy.get(),
208 wl_proxy_get_id(reinterpret_cast<struct wl_proxy *>(
209 this->outputs.back()->proxy.get())));
212 this->controller->create_screen(this->outputs.back()->proxy.get());
214 // Set display to controller
215 this->controller->display = this->display;
218 // First level objects
219 this->display->roundtrip();
220 // Second level objects
221 this->display->roundtrip();
222 // Third level objects
223 this->display->roundtrip();
229 int WindowManager::dispatch_pending_events()
231 if (this->pop_pending_events())
233 this->display->dispatch_pending();
239 void WindowManager::set_pending_events()
241 this->pending_events.store(true, std::memory_order_release);
244 result<int> WindowManager::api_request_surface(char const *appid, char const *drawing_name)
246 // TODO: application requests by old role,
247 // so convert role old to new
248 const char *role = this->convertRoleOldToNew(drawing_name);
250 auto lid = this->layers.get_layer_id(std::string(role));
254 * register drawing_name as fallback and make it displayed.
256 lid = this->layers.get_layer_id(std::string("fallback"));
257 HMI_DEBUG("wm", "%s is not registered in layers.json, then fallback as normal app", role);
260 return Err<int>("Drawing name does not match any role, Fallback is disabled");
264 auto rname = this->lookup_id(role);
267 // name does not exist yet, allocate surface id...
268 auto id = int(this->id_alloc.generate_id(role));
269 this->layers.add_surface(id, *lid);
271 // set the main_surface[_name] here and now
272 if (!this->layers.main_surface_name.empty() &&
273 this->layers.main_surface_name == drawing_name)
275 this->layers.main_surface = id;
276 HMI_DEBUG("wm", "Set main_surface id to %u", id);
279 // add client into the db
280 std::string appid_str(appid);
281 g_app_list.addClient(appid_str, *lid, id, std::string(role));
283 // Set role map of (new, old)
284 this->rolenew2old[role] = std::string(drawing_name);
289 // Check currently registered drawing names if it is already there.
290 return Err<int>("Surface already present");
293 char const *WindowManager::api_request_surface(char const *appid, char const *drawing_name,
298 // TODO: application requests by old role,
299 // so convert role old to new
300 const char *role = this->convertRoleOldToNew(drawing_name);
302 auto lid = this->layers.get_layer_id(std::string(role));
303 unsigned sid = std::stol(ivi_id);
308 * register drawing_name as fallback and make it displayed.
310 lid = this->layers.get_layer_id(std::string("fallback"));
311 HMI_DEBUG("wm", "%s is not registered in layers.json, then fallback as normal app", role);
314 return "Drawing name does not match any role, Fallback is disabled";
318 auto rname = this->lookup_id(role);
322 return "Surface already present";
325 // register pair drawing_name and ivi_id
326 this->id_alloc.register_name_id(role, sid);
327 this->layers.add_surface(sid, *lid);
329 // this surface is already created
330 HMI_DEBUG("wm", "surface_id is %u, layer_id is %u", sid, *lid);
332 this->controller->layers[*lid]->add_surface(sid);
333 this->layout_commit();
335 // add client into the db
336 std::string appid_str(appid);
337 g_app_list.addClient(appid_str, *lid, sid, std::string(role));
339 // Set role map of (new, old)
340 this->rolenew2old[role] = std::string(drawing_name);
346 * This function is substitute of requestSurface
347 * If surface creation is faster than application request of this function,
348 * WM will bind surfaceID with application and role.
349 * If surface creation is slower than application request of thie function,
350 * WM will put Client into pending list.
353 * Application can request with pid but this is temporary solution for now.
354 * This will be removed.
356 bool WindowManager::api_set_role(char const *appid, char const *drawing_name, unsigned pid){
357 std::string id = appid;
358 std::string role = drawing_name;
359 unsigned surface = 0;
360 WMError wm_err = WMError::UNKNOWN;
363 // get layer ID which role should be in
364 auto lid = this->layers.get_layer_id(role);
368 * register drawing_name as fallback and make it displayed.
370 lid = this->layers.get_layer_id(std::string("fallback"));
371 HMI_DEBUG("wm", "%s is not registered in layers.json, then fallback as normal app", role.c_str());
374 HMI_ERROR("wm", "Drawing name does not match any role, fallback is disabled");
380 // search floating surfaceID from pid if pid is designated.
381 // It is not good that application request with its pid
382 wm_err = g_app_list.popFloatingSurface(pid, &surface);
385 // get floating surface with appid. If WM queries appid from pid,
386 // WM can bind surface and role with appid(not implemented yet)
387 //wm_err = g_app_list.popFloatingSurface(id);
389 if(wm_err != WMError::SUCCESS){
390 HMI_ERROR("wm", "No floating surface for app: %s", id.c_str());
391 g_app_list.addFloatingClient(id, *lid, role);
392 HMI_NOTICE("wm", "%s : Waiting for surface creation", id.c_str());
397 if (g_app_list.contains(id))
399 HMI_INFO("wm", "Add role: %s with surface: %d. Client %s has multi surfaces.",
400 role.c_str(), surface, id.c_str());
401 wm_err = g_app_list.appendRole(id, role, surface);
402 if(wm_err != WMError::SUCCESS){
403 HMI_INFO("wm", errorDescription(wm_err));
407 HMI_INFO("wm", "Create new client: %s, surface: %d into layer: %d with role: %s",
408 id.c_str(), surface, *lid, role.c_str());
409 g_app_list.addClient(id, *lid, surface, role);
412 // register pair drawing_name and ivi_id
413 this->id_alloc.register_name_id(role.c_str(), surface);
414 this->layers.add_surface(surface, *lid);
416 // this surface is already created
417 HMI_DEBUG("wm", "surface_id is %u, layer_id is %u", surface, *lid);
419 const auto &o_layer = this->layers.get_layer(*lid);
420 auto rect = o_layer.value().rect;
423 rect.w = this->controller->output_size.w + 1 + rect.w;
427 rect.h = this->controller->output_size.h + 1 + rect.h;
429 this->controller->surfaces[surface]->set_source_rectangle(0, 0, rect.w, rect.h);
431 this->controller->layers[*lid]->add_surface(surface);
432 this->layout_commit();
437 void WindowManager::api_activate_surface(char const *appid, char const *drawing_name,
438 char const *drawing_area, const reply_func &reply)
442 // TODO: application requests by old role,
443 // so convert role old to new
444 const char *c_role = this->convertRoleOldToNew(drawing_name);
446 std::string id = appid;
447 std::string role = c_role;
448 std::string area = drawing_area;
449 Task task = Task::TASK_ALLOCATE;
450 unsigned req_num = 0;
451 WMError ret = WMError::UNKNOWN;
453 ret = this->setRequest(id, role, area, task, &req_num);
455 if(ret != WMError::SUCCESS)
457 HMI_ERROR("wm", errorDescription(ret));
458 reply("Failed to set request");
463 if (req_num != g_app_list.currentRequestNumber())
465 // Add request, then invoked after the previous task is finished
466 HMI_SEQ_DEBUG(req_num, "request is accepted");
473 ret = this->doTransition(req_num);
475 if (ret != WMError::SUCCESS)
478 HMI_SEQ_ERROR(req_num, errorDescription(ret));
479 g_app_list.removeRequest(req_num);
480 this->processNextRequest();
484 void WindowManager::api_deactivate_surface(char const *appid, char const *drawing_name,
485 const reply_func &reply)
489 // TODO: application requests by old role,
490 // so convert role old to new
491 const char *c_role = this->convertRoleOldToNew(drawing_name);
496 std::string id = appid;
497 std::string role = c_role;
498 std::string area = ""; //drawing_area;
499 Task task = Task::TASK_RELEASE;
500 unsigned req_num = 0;
501 WMError ret = WMError::UNKNOWN;
503 ret = this->setRequest(id, role, area, task, &req_num);
505 if (ret != WMError::SUCCESS)
507 HMI_ERROR("wm", errorDescription(ret));
508 reply("Failed to set request");
513 if (req_num != g_app_list.currentRequestNumber())
515 // Add request, then invoked after the previous task is finished
516 HMI_SEQ_DEBUG(req_num, "request is accepted");
523 ret = this->doTransition(req_num);
525 if (ret != WMError::SUCCESS)
528 HMI_SEQ_ERROR(req_num, errorDescription(ret));
529 g_app_list.removeRequest(req_num);
530 this->processNextRequest();
534 void WindowManager::api_enddraw(char const *appid, char const *drawing_name)
536 // TODO: application requests by old role,
537 // so convert role old to new
538 const char *c_role = this->convertRoleOldToNew(drawing_name);
540 std::string id = appid;
541 std::string role = c_role;
542 unsigned current_req = g_app_list.currentRequestNumber();
543 bool result = g_app_list.setEndDrawFinished(current_req, id, role);
547 HMI_ERROR("wm", "%s is not in transition state", id.c_str());
551 if (g_app_list.endDrawFullfilled(current_req))
553 // do task for endDraw
555 WMError ret = this->doEndDraw(current_req);
557 if(ret != WMError::SUCCESS)
559 //this->emit_error();
561 // Undo state of PolicyManager
562 this->pmw.undoState();
564 this->emitScreenUpdated(current_req);
565 HMI_SEQ_INFO(current_req, "Finish request status: %s", errorDescription(ret));
567 g_app_list.removeRequest(current_req);
569 this->processNextRequest();
573 HMI_SEQ_INFO(current_req, "Wait other App call endDraw");
578 result<json_object *> WindowManager::api_get_display_info()
581 if (!this->controller)
583 return Err<json_object *>("ivi_controller global not available");
587 compositor::size o_size = this->controller->output_size;
588 compositor::size p_size = this->controller->physical_size;
590 json_object *object = json_object_new_object();
591 json_object_object_add(object, kKeyWidthPixel, json_object_new_int(o_size.w));
592 json_object_object_add(object, kKeyHeightPixel, json_object_new_int(o_size.h));
593 json_object_object_add(object, kKeyWidthMm, json_object_new_int(p_size.w));
594 json_object_object_add(object, kKeyHeightMm, json_object_new_int(p_size.h));
596 return Ok<json_object *>(object);
599 result<json_object *> WindowManager::api_get_area_info(char const *drawing_name)
601 HMI_DEBUG("wm", "called");
603 // TODO: application requests by old role,
604 // so convert role old to new
605 const char *role = this->convertRoleOldToNew(drawing_name);
607 // Check drawing name, surface/layer id
608 auto const &surface_id = this->lookup_id(role);
611 return Err<json_object *>("Surface does not exist");
614 if (!this->controller->surface_exists(*surface_id))
616 return Err<json_object *>("Surface does not exist in controller!");
619 auto layer_id = this->layers.get_layer_id(*surface_id);
622 return Err<json_object *>("Surface is not on any layer!");
625 // Set area rectangle
626 compositor::rect area_info = this->area_info[*surface_id];
627 json_object *object = json_object_new_object();
628 json_object_object_add(object, kKeyX, json_object_new_int(area_info.x));
629 json_object_object_add(object, kKeyY, json_object_new_int(area_info.y));
630 json_object_object_add(object, kKeyWidth, json_object_new_int(area_info.w));
631 json_object_object_add(object, kKeyHeight, json_object_new_int(area_info.h));
633 return Ok<json_object *>(object);
636 void WindowManager::api_ping() { this->dispatch_pending_events(); }
638 void WindowManager::send_event(char const *evname, char const *label)
640 HMI_DEBUG("wm", "%s: %s(%s)", __func__, evname, label);
642 json_object *j = json_object_new_object();
643 json_object_object_add(j, kKeyDrawingName, json_object_new_string(label));
645 int ret = afb_event_push(this->map_afb_event[evname], j);
648 HMI_DEBUG("wm", "afb_event_push failed: %m");
652 void WindowManager::send_event(char const *evname, char const *label, char const *area,
653 int x, int y, int w, int h)
655 HMI_DEBUG("wm", "%s: %s(%s, %s) x:%d y:%d w:%d h:%d",
656 __func__, evname, label, area, x, y, w, h);
658 json_object *j_rect = json_object_new_object();
659 json_object_object_add(j_rect, kKeyX, json_object_new_int(x));
660 json_object_object_add(j_rect, kKeyY, json_object_new_int(y));
661 json_object_object_add(j_rect, kKeyWidth, json_object_new_int(w));
662 json_object_object_add(j_rect, kKeyHeight, json_object_new_int(h));
664 json_object *j = json_object_new_object();
665 json_object_object_add(j, kKeyDrawingName, json_object_new_string(label));
666 json_object_object_add(j, kKeyDrawingArea, json_object_new_string(area));
667 json_object_object_add(j, kKeyDrawingRect, j_rect);
669 int ret = afb_event_push(this->map_afb_event[evname], j);
672 HMI_DEBUG("wm", "afb_event_push failed: %m");
679 void WindowManager::surface_created(uint32_t surface_id)
681 auto layer_id = this->layers.get_layer_id(surface_id);
684 HMI_DEBUG("wm", "Newly created surfce %d is not associated with any layer!",
689 HMI_DEBUG("wm", "surface_id is %u, layer_id is %u", surface_id, *layer_id);
691 this->controller->layers[*layer_id]->add_surface(surface_id);
692 this->layout_commit();
695 void WindowManager::surface_removed(uint32_t surface_id)
697 HMI_DEBUG("wm", "surface_id is %u", surface_id);
698 g_app_list.removeSurface(surface_id);
701 void WindowManager::removeClient(const std::string &appid)
703 HMI_DEBUG("wm", "Remove clinet %s from list", appid.c_str());
704 g_app_list.removeClient(appid);
707 void WindowManager::exceptionProcessForTransition()
709 unsigned req_num = g_app_list.currentRequestNumber();
710 HMI_SEQ_NOTICE(req_num, "Process exception handling for request. Remove current request %d", req_num);
711 g_app_list.removeRequest(req_num);
712 HMI_SEQ_NOTICE(g_app_list.currentRequestNumber(), "Process next request if exists");
713 this->processNextRequest();
716 void WindowManager::timerHandler()
718 unsigned req_num = g_app_list.currentRequestNumber();
719 HMI_SEQ_DEBUG(req_num, "Timer expired remove Request");
720 g_app_list.reqDump();
721 g_app_list.removeRequest(req_num);
722 this->processNextRequest();
725 void WindowManager::startTransitionWrapper(std::vector<WMAction> &actions)
728 unsigned req_num = g_app_list.currentRequestNumber();
732 if (g_app_list.haveRequest())
734 HMI_SEQ_DEBUG(req_num, "There is no WMAction for this request");
735 goto proc_remove_request;
739 HMI_SEQ_DEBUG(req_num, "There is no request");
744 for (auto &act : actions)
749 auto const &surface_id = this->lookup_id(act.role.c_str());
750 std::string appid = g_app_list.getAppID(*surface_id, act.role, &found);
758 ret = g_app_list.setAction(req_num, act);
759 if (ret != WMError::SUCCESS)
765 HMI_SEQ_DEBUG(req_num, "Start transition.");
766 ret = this->startTransition(req_num);
767 if (ret != WMError::SUCCESS)
769 if (ret == WMError::NO_LAYOUT_CHANGE)
771 goto proc_remove_request;
783 HMI_SEQ_ERROR(req_num, errorDescription(ret));
784 this->pmw.undoState();
787 g_app_list.removeRequest(req_num);
788 this->processNextRequest();
791 void WindowManager::processError(WMError error)
793 unsigned req_num = g_app_list.currentRequestNumber();
796 HMI_SEQ_ERROR(req_num, errorDescription(error));
797 g_app_list.removeRequest(req_num);
798 this->processNextRequest();
802 ******* Private Functions *******
805 bool WindowManager::pop_pending_events()
808 return this->pending_events.compare_exchange_strong(
809 x, false, std::memory_order_consume);
812 optional<int> WindowManager::lookup_id(char const *name)
814 return this->id_alloc.lookup(std::string(name));
816 optional<std::string> WindowManager::lookup_name(int id)
818 return this->id_alloc.lookup(id);
824 int WindowManager::init_layers()
826 if (!this->controller)
828 HMI_ERROR("wm", "ivi_controller global not available");
832 if (this->outputs.empty())
834 HMI_ERROR("wm", "no output was set up!");
839 wm_config.loadConfigs();
841 auto &c = this->controller;
843 auto &o = this->outputs.front();
844 auto &s = c->screens.begin()->second;
845 auto &layers = c->layers;
847 this->layers.loadAreaDb();
848 const compositor::rect base = this->layers.getAreaSize("fullscreen");
850 const std::string aspect_setting = wm_config.getConfigAspect();
851 const compositor::rect scale_rect =
852 this->layers.getScaleDestRect(o->width, o->height, aspect_setting);
854 // Write output dimensions to ivi controller...
855 c->output_size = compositor::size{uint32_t(o->width), uint32_t(o->height)};
856 c->physical_size = compositor::size{uint32_t(o->physical_width),
857 uint32_t(o->physical_height)};
865 // Quick and dirty setup of layers
866 for (auto const &i : this->layers.mapping)
868 c->layer_create(i.second.layer_id, scale_rect.w, scale_rect.h);
869 auto &l = layers[i.second.layer_id];
870 l->set_source_rectangle(0, 0, base.w, base.h);
871 l->set_destination_rectangle(
872 scale_rect.x, scale_rect.y, scale_rect.w, scale_rect.h);
873 l->set_visibility(1);
874 HMI_DEBUG("wm", "Setting up layer %s (%d) for surface role match \"%s\"",
875 i.second.name.c_str(), i.second.layer_id, i.second.role.c_str());
878 // Add layers to screen
879 s->set_render_order(this->layers.layers);
881 this->layout_commit();
886 void WindowManager::surface_set_layout(int surface_id, const std::string& area)
888 if (!this->controller->surface_exists(surface_id))
890 HMI_ERROR("wm", "Surface %d does not exist", surface_id);
894 auto o_layer_id = this->layers.get_layer_id(surface_id);
898 HMI_ERROR("wm", "Surface %d is not associated with any layer!", surface_id);
902 uint32_t layer_id = *o_layer_id;
904 auto const &layer = this->layers.get_layer(layer_id);
905 auto rect = this->layers.getAreaSize(area);
906 HMI_SEQ_DEBUG(g_app_list.currentRequestNumber(), "%s : x:%d y:%d w:%d h:%d", area.c_str(),
907 rect.x, rect.y, rect.w, rect.h);
908 auto &s = this->controller->surfaces[surface_id];
915 HMI_DEBUG("wm", "surface_set_layout for surface %u on layer %u", surface_id,
918 // set destination to the display rectangle
919 s->set_destination_rectangle(x, y, w, h);
921 // update area information
922 this->area_info[surface_id].x = x;
923 this->area_info[surface_id].y = y;
924 this->area_info[surface_id].w = w;
925 this->area_info[surface_id].h = h;
927 HMI_DEBUG("wm", "Surface %u now on layer %u with rect { %d, %d, %d, %d }",
928 surface_id, layer_id, x, y, w, h);
931 void WindowManager::layout_commit()
933 this->controller->commit_changes();
934 this->display->flush();
937 void WindowManager::emit_activated(char const *label)
939 this->send_event(kListEventName[Event_Active], label);
942 void WindowManager::emit_deactivated(char const *label)
944 this->send_event(kListEventName[Event_Inactive], label);
947 void WindowManager::emit_syncdraw(char const *label, char const *area, int x, int y, int w, int h)
949 this->send_event(kListEventName[Event_SyncDraw], label, area, x, y, w, h);
952 void WindowManager::emit_syncdraw(const std::string &role, const std::string &area)
954 compositor::rect rect = this->layers.getAreaSize(area);
955 this->send_event(kListEventName[Event_SyncDraw],
956 role.c_str(), area.c_str(), rect.x, rect.y, rect.w, rect.h);
959 void WindowManager::emit_flushdraw(char const *label)
961 this->send_event(kListEventName[Event_FlushDraw], label);
964 void WindowManager::emit_visible(char const *label, bool is_visible)
966 this->send_event(is_visible ? kListEventName[Event_Visible] : kListEventName[Event_Invisible], label);
969 void WindowManager::emit_invisible(char const *label)
971 return emit_visible(label, false);
974 void WindowManager::emit_visible(char const *label) { return emit_visible(label, true); }
976 void WindowManager::activate(int id)
978 auto ip = this->controller->sprops.find(id);
979 if (ip != this->controller->sprops.end())
981 this->controller->surfaces[id]->set_visibility(1);
983 this->lookup_name(id).value_or("unknown-name").c_str();
986 if ((0 == strcmp(label, "radio")) ||
987 (0 == strcmp(label, "music")) ||
988 (0 == strcmp(label, "video")) ||
989 (0 == strcmp(label, "map")))
991 for (auto i = surface_bg.begin(); i != surface_bg.end(); ++i)
996 this->surface_bg.erase(i);
998 // Remove from BG layer (999)
999 HMI_DEBUG("wm", "Remove %s(%d) from BG layer", label, id);
1000 this->controller->layers[999]->remove_surface(id);
1002 // Add to FG layer (1001)
1003 HMI_DEBUG("wm", "Add %s(%d) to FG layer", label, id);
1004 this->controller->layers[1001]->add_surface(id);
1006 for (int j : this->surface_bg)
1008 HMI_DEBUG("wm", "Stored id:%d", j);
1016 this->layout_commit();
1018 // TODO: application requests by old role,
1019 // so convert role new to old for emitting event
1020 const char* old_role = this->rolenew2old[label].c_str();
1022 this->emit_visible(old_role);
1023 this->emit_activated(old_role);
1027 void WindowManager::deactivate(int id)
1029 auto ip = this->controller->sprops.find(id);
1030 if (ip != this->controller->sprops.end())
1033 this->lookup_name(id).value_or("unknown-name").c_str();
1036 if ((0 == strcmp(label, "radio")) ||
1037 (0 == strcmp(label, "music")) ||
1038 (0 == strcmp(label, "video")) ||
1039 (0 == strcmp(label, "map")))
1043 this->surface_bg.push_back(id);
1045 // Remove from FG layer (1001)
1046 HMI_DEBUG("wm", "Remove %s(%d) from FG layer", label, id);
1047 this->controller->layers[1001]->remove_surface(id);
1049 // Add to BG layer (999)
1050 HMI_DEBUG("wm", "Add %s(%d) to BG layer", label, id);
1051 this->controller->layers[999]->add_surface(id);
1053 for (int j : surface_bg)
1055 HMI_DEBUG("wm", "Stored id:%d", j);
1060 this->controller->surfaces[id]->set_visibility(0);
1064 this->layout_commit();
1066 // TODO: application requests by old role,
1067 // so convert role new to old for emitting event
1068 const char* old_role = this->rolenew2old[label].c_str();
1070 this->emit_deactivated(old_role);
1071 this->emit_invisible(old_role);
1075 WMError WindowManager::setRequest(const std::string& appid, const std::string &role, const std::string &area,
1076 Task task, unsigned* req_num)
1078 if (!g_app_list.contains(appid))
1080 return WMError::NOT_REGISTERED;
1083 auto client = g_app_list.lookUpClient(appid);
1088 unsigned current = g_app_list.currentRequestNumber();
1089 unsigned requested_num = g_app_list.getRequestNumber(appid);
1090 if (requested_num != 0)
1092 HMI_SEQ_INFO(requested_num,
1093 "%s %s %s request is already queued", appid.c_str(), role.c_str(), area.c_str());
1094 return REQ_REJECTED;
1097 WMRequest req = WMRequest(appid, role, area, task);
1098 unsigned new_req = g_app_list.addRequest(req);
1100 g_app_list.reqDump();
1102 HMI_SEQ_DEBUG(current, "%s start sequence with %s, %s", appid.c_str(), role.c_str(), area.c_str());
1104 return WMError::SUCCESS;
1107 WMError WindowManager::doTransition(unsigned req_num)
1109 HMI_SEQ_DEBUG(req_num, "check policy");
1110 WMError ret = this->checkPolicy(req_num);
1114 WMError WindowManager::checkPolicy(unsigned req_num)
1119 // get current trigger
1121 WMError ret = WMError::LAYOUT_CHANGE_FAIL;
1122 auto trigger = g_app_list.getRequest(req_num, &found);
1125 ret = WMError::NO_ENTRY;
1128 std::string req_area = trigger.area;
1130 if (trigger.task == Task::TASK_ALLOCATE)
1132 const char *msg = this->check_surface_exist(trigger.role.c_str());
1136 HMI_SEQ_ERROR(req_num, msg);
1141 // Input event data to PolicyManager
1142 if (0 > this->pmw.setInputEventData(trigger.task, trigger.role, trigger.area))
1144 HMI_SEQ_ERROR(req_num, "Failed to set input event data to PolicyManager");
1148 // Execute state transition of PolicyManager
1149 if (0 > this->pmw.executeStateTransition())
1151 HMI_SEQ_ERROR(req_num, "Failed to execute state transition of PolicyManager");
1155 ret = WMError::SUCCESS;
1157 g_app_list.reqDump();
1162 WMError WindowManager::startTransition(unsigned req_num)
1164 bool sync_draw_happen = false;
1166 WMError ret = WMError::SUCCESS;
1167 auto actions = g_app_list.getActions(req_num, &found);
1170 ret = WMError::NO_ENTRY;
1171 HMI_SEQ_ERROR(req_num,
1172 "Window Manager bug :%s : Action is not set", errorDescription(ret));
1176 for (const auto &action : actions)
1178 if (action.visible == TaskVisible::VISIBLE)
1180 sync_draw_happen = true;
1182 // TODO: application requests by old role,
1183 // so convert role new to old for emitting event
1184 std::string old_role = this->rolenew2old[action.role];
1186 this->emit_syncdraw(old_role, action.area);
1187 /* TODO: emit event for app not subscriber
1188 if(g_app_list.contains(y.appid))
1189 g_app_list.lookUpClient(y.appid)->emit_syncdraw(y.role, y.area); */
1193 if (sync_draw_happen)
1199 // deactivate only, no syncDraw
1200 // Make it deactivate here
1201 for (const auto &x : actions)
1203 if (g_app_list.contains(x.appid))
1205 auto client = g_app_list.lookUpClient(x.appid);
1206 this->deactivate(client->surfaceID(x.role));
1209 ret = WMError::NO_LAYOUT_CHANGE;
1214 WMError WindowManager::doEndDraw(unsigned req_num)
1218 auto actions = g_app_list.getActions(req_num, &found);
1219 WMError ret = WMError::SUCCESS;
1222 ret = WMError::NO_ENTRY;
1226 HMI_SEQ_INFO(req_num, "do endDraw");
1228 // layout change and make it visible
1229 for (const auto &act : actions)
1231 if(act.visible != TaskVisible::NO_CHANGE)
1234 if(!g_app_list.contains(act.appid)){
1235 ret = WMError::NOT_REGISTERED;
1237 ret = this->layoutChange(act);
1238 if(ret != WMError::SUCCESS)
1240 HMI_SEQ_WARNING(req_num,
1241 "Failed to manipulate surfaces while state change : %s", errorDescription(ret));
1244 ret = this->visibilityChange(act);
1245 if (ret != WMError::SUCCESS)
1247 HMI_SEQ_WARNING(req_num,
1248 "Failed to manipulate surfaces while state change : %s", errorDescription(ret));
1251 HMI_SEQ_DEBUG(req_num, "visible %s", act.role.c_str());
1252 //this->lm_enddraw(act.role.c_str());
1256 HMI_SEQ_INFO(req_num, "emit flushDraw");
1258 for(const auto &act_flush : actions)
1260 if(act_flush.visible == TaskVisible::VISIBLE)
1262 // TODO: application requests by old role,
1263 // so convert role new to old for emitting event
1264 std::string old_role = this->rolenew2old[act_flush.role];
1266 this->emit_flushdraw(old_role.c_str());
1273 WMError WindowManager::layoutChange(const WMAction &action)
1275 if (action.visible == TaskVisible::INVISIBLE)
1277 // Visibility is not change -> no redraw is required
1278 return WMError::SUCCESS;
1280 auto client = g_app_list.lookUpClient(action.appid);
1281 unsigned surface = client->surfaceID(action.role);
1284 HMI_SEQ_ERROR(g_app_list.currentRequestNumber(),
1285 "client doesn't have surface with role(%s)", action.role.c_str());
1286 return WMError::NOT_REGISTERED;
1289 WMError ret = this->setSurfaceSize(surface, action.area);
1293 WMError WindowManager::visibilityChange(const WMAction &action)
1295 HMI_SEQ_DEBUG(g_app_list.currentRequestNumber(), "Change visibility");
1296 if(!g_app_list.contains(action.appid)){
1297 return WMError::NOT_REGISTERED;
1299 auto client = g_app_list.lookUpClient(action.appid);
1300 unsigned surface = client->surfaceID(action.role);
1303 HMI_SEQ_ERROR(g_app_list.currentRequestNumber(),
1304 "client doesn't have surface with role(%s)", action.role.c_str());
1305 return WMError::NOT_REGISTERED;
1308 if (action.visible != TaskVisible::INVISIBLE)
1310 this->activate(surface); // Layout Manager task
1314 this->deactivate(surface); // Layout Manager task
1316 return WMError::SUCCESS;
1319 WMError WindowManager::setSurfaceSize(unsigned surface, const std::string &area)
1321 this->surface_set_layout(surface, area);
1323 return WMError::SUCCESS;
1326 void WindowManager::emitScreenUpdated(unsigned req_num)
1329 HMI_SEQ_DEBUG(req_num, "emit screen updated");
1331 auto actions = g_app_list.getActions(req_num, &found);
1333 // create json object
1334 json_object *j = json_object_new_object();
1335 json_object *jarray = json_object_new_array();
1337 for(const auto& action: actions)
1339 if(action.visible != TaskVisible::INVISIBLE)
1341 json_object_array_add(jarray, json_object_new_string(action.appid.c_str()));
1344 json_object_object_add(j, kKeyIds, jarray);
1345 HMI_SEQ_INFO(req_num, "Visible app: %s", json_object_get_string(j));
1347 int ret = afb_event_push(
1348 this->map_afb_event[kListEventName[Event_ScreenUpdated]], j);
1351 HMI_DEBUG("wm", "afb_event_push failed: %m");
1353 json_object_put(jarray);
1356 void WindowManager::setTimer()
1358 HMI_SEQ_DEBUG(g_app_list.currentRequestNumber(), "Timer set activate");
1359 if (g_timer_ev_src == nullptr)
1361 // firsttime set into sd_event
1362 int ret = sd_event_add_time(afb_daemon_get_event_loop(), &g_timer_ev_src,
1363 CLOCK_REALTIME, time(NULL) * (1000000UL) + kTimeOut, 1, processTimerHandler, this);
1366 HMI_ERROR("wm", "Could't set timer");
1371 // update timer limitation after second time
1372 sd_event_source_set_time(g_timer_ev_src, time(NULL) * (1000000UL) + kTimeOut);
1373 sd_event_source_set_enabled(g_timer_ev_src, SD_EVENT_ONESHOT);
1377 void WindowManager::stopTimer()
1379 unsigned req_num = g_app_list.currentRequestNumber();
1380 HMI_SEQ_DEBUG(req_num, "Timer stop");
1381 int rc = sd_event_source_set_enabled(g_timer_ev_src, SD_EVENT_OFF);
1384 HMI_SEQ_ERROR(req_num, "Timer stop failed");
1388 void WindowManager::processNextRequest()
1391 g_app_list.reqDump();
1392 unsigned req_num = g_app_list.currentRequestNumber();
1393 if (g_app_list.haveRequest())
1395 HMI_SEQ_DEBUG(req_num, "Process next request");
1396 WMError rc = doTransition(req_num);
1397 if (rc != WMError::SUCCESS)
1399 HMI_SEQ_ERROR(req_num, errorDescription(rc));
1404 HMI_SEQ_DEBUG(req_num, "Nothing Request. Waiting Request");
1408 const char* WindowManager::convertRoleOldToNew(char const *old_role)
1410 const char *new_role = nullptr;
1412 for (auto const &on : this->roleold2new)
1414 std::regex regex = std::regex(on.first);
1415 if (std::regex_match(old_role, regex))
1417 // role is old. So convert to new.
1418 new_role = on.second.c_str();
1423 if (nullptr == new_role)
1425 // role is new or fallback.
1426 new_role = old_role;
1429 HMI_DEBUG("wm", "old:%s -> new:%s", old_role, new_role);
1434 int WindowManager::loadOldRoleDb()
1436 // Get afm application installed dir
1437 char const *afm_app_install_dir = getenv("AFM_APP_INSTALL_DIR");
1438 HMI_DEBUG("wm", "afm_app_install_dir:%s", afm_app_install_dir);
1440 std::string file_name;
1441 if (!afm_app_install_dir)
1443 HMI_ERROR("wm", "AFM_APP_INSTALL_DIR is not defined");
1447 file_name = std::string(afm_app_install_dir) + std::string("/etc/old_roles.db");
1451 json_object* json_obj;
1452 int ret = jh::inputJsonFilie(file_name.c_str(), &json_obj);
1455 HMI_ERROR("wm", "Could not open old_role.db, so use default old_role information");
1456 json_obj = json_tokener_parse(kDefaultOldRoleDb);
1458 HMI_DEBUG("wm", "json_obj dump:%s", json_object_get_string(json_obj));
1461 json_object* json_cfg;
1462 if (!json_object_object_get_ex(json_obj, "old_roles", &json_cfg))
1464 HMI_ERROR("wm", "Parse Error!!");
1468 int len = json_object_array_length(json_cfg);
1469 HMI_DEBUG("wm", "json_cfg len:%d", len);
1470 HMI_DEBUG("wm", "json_cfg dump:%s", json_object_get_string(json_cfg));
1472 for (int i=0; i<len; i++)
1474 json_object* json_tmp = json_object_array_get_idx(json_cfg, i);
1476 const char* old_role = jh::getStringFromJson(json_tmp, "name");
1477 if (nullptr == old_role)
1479 HMI_ERROR("wm", "Parse Error!!");
1483 const char* new_role = jh::getStringFromJson(json_tmp, "new");
1484 if (nullptr == new_role)
1486 HMI_ERROR("wm", "Parse Error!!");
1490 this->roleold2new[old_role] = std::string(new_role);
1494 for(auto itr = this->roleold2new.begin();
1495 itr != this->roleold2new.end(); ++itr)
1497 HMI_DEBUG("wm", ">>> role old:%s new:%s",
1498 itr->first.c_str(), itr->second.c_str());
1501 // Release json_object
1502 json_object_put(json_obj);
1507 const char *WindowManager::check_surface_exist(const char *drawing_name)
1509 auto const &surface_id = this->lookup_id(drawing_name);
1512 return "Surface does not exist";
1515 if (!this->controller->surface_exists(*surface_id))
1517 return "Surface does not exist in controller!";
1520 auto layer_id = this->layers.get_layer_id(*surface_id);
1524 return "Surface is not on any layer!";
1527 HMI_DEBUG("wm", "surface %d is detected", *surface_id);
1531 const char* WindowManager::kDefaultOldRoleDb = "{ \
1534 \"name\": \"HomeScreen\", \
1535 \"new\": \"homescreen\" \
1538 \"name\": \"Music\", \
1539 \"new\": \"music\" \
1542 \"name\": \"MediaPlayer\", \
1543 \"new\": \"music\" \
1546 \"name\": \"Video\", \
1547 \"new\": \"video\" \
1550 \"name\": \"VideoPlayer\", \
1551 \"new\": \"video\" \
1554 \"name\": \"WebBrowser\", \
1555 \"new\": \"browser\" \
1558 \"name\": \"Radio\", \
1559 \"new\": \"radio\" \
1562 \"name\": \"Phone\", \
1563 \"new\": \"phone\" \
1566 \"name\": \"Navigation\", \
1570 \"name\": \"HVAC\", \
1574 \"name\": \"Settings\", \
1575 \"new\": \"settings\" \
1578 \"name\": \"Dashboard\", \
1579 \"new\": \"dashboard\" \
1582 \"name\": \"POI\", \
1586 \"name\": \"Mixer\", \
1587 \"new\": \"mixer\" \
1590 \"name\": \"Restriction\", \
1591 \"new\": \"restriction\" \
1594 \"name\": \"^OnScreen.*\", \
1595 \"new\": \"on_screen\" \
1603 void controller_hooks::surface_created(uint32_t surface_id)
1605 this->app->surface_created(surface_id);
1608 void controller_hooks::surface_removed(uint32_t surface_id)
1610 this->app->surface_removed(surface_id);
1613 void controller_hooks::surface_visibility(uint32_t /*surface_id*/,
1616 void controller_hooks::surface_destination_rectangle(uint32_t /*surface_id*/,