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);
345 void WindowManager::api_activate_surface(char const *appid, char const *drawing_name,
346 char const *drawing_area, const reply_func &reply)
350 // TODO: application requests by old role,
351 // so convert role old to new
352 const char *c_role = this->convertRoleOldToNew(drawing_name);
354 std::string id = appid;
355 std::string role = c_role;
356 std::string area = drawing_area;
357 Task task = Task::TASK_ALLOCATE;
358 unsigned req_num = 0;
359 WMError ret = WMError::UNKNOWN;
361 ret = this->setRequest(id, role, area, task, &req_num);
363 if(ret != WMError::SUCCESS)
365 HMI_ERROR("wm", errorDescription(ret));
366 reply("Failed to set request");
371 if (req_num != g_app_list.currentRequestNumber())
373 // Add request, then invoked after the previous task is finished
374 HMI_SEQ_DEBUG(req_num, "request is accepted");
381 ret = this->doTransition(req_num);
383 if (ret != WMError::SUCCESS)
386 HMI_SEQ_ERROR(req_num, errorDescription(ret));
387 g_app_list.removeRequest(req_num);
388 this->processNextRequest();
392 void WindowManager::api_deactivate_surface(char const *appid, char const *drawing_name,
393 const reply_func &reply)
397 // TODO: application requests by old role,
398 // so convert role old to new
399 const char *c_role = this->convertRoleOldToNew(drawing_name);
404 std::string id = appid;
405 std::string role = c_role;
406 std::string area = ""; //drawing_area;
407 Task task = Task::TASK_RELEASE;
408 unsigned req_num = 0;
409 WMError ret = WMError::UNKNOWN;
411 ret = this->setRequest(id, role, area, task, &req_num);
413 if (ret != WMError::SUCCESS)
415 HMI_ERROR("wm", errorDescription(ret));
416 reply("Failed to set request");
421 if (req_num != g_app_list.currentRequestNumber())
423 // Add request, then invoked after the previous task is finished
424 HMI_SEQ_DEBUG(req_num, "request is accepted");
431 ret = this->doTransition(req_num);
433 if (ret != WMError::SUCCESS)
436 HMI_SEQ_ERROR(req_num, errorDescription(ret));
437 g_app_list.removeRequest(req_num);
438 this->processNextRequest();
442 void WindowManager::api_enddraw(char const *appid, char const *drawing_name)
444 // TODO: application requests by old role,
445 // so convert role old to new
446 const char *c_role = this->convertRoleOldToNew(drawing_name);
448 std::string id = appid;
449 std::string role = c_role;
450 unsigned current_req = g_app_list.currentRequestNumber();
451 bool result = g_app_list.setEndDrawFinished(current_req, id, role);
455 HMI_ERROR("wm", "%s is not in transition state", id.c_str());
459 if (g_app_list.endDrawFullfilled(current_req))
461 // do task for endDraw
463 WMError ret = this->doEndDraw(current_req);
465 if(ret != WMError::SUCCESS)
467 //this->emit_error();
469 // Undo state of PolicyManager
470 this->pmw.undoState();
472 this->emitScreenUpdated(current_req);
473 HMI_SEQ_INFO(current_req, "Finish request status: %s", errorDescription(ret));
475 g_app_list.removeRequest(current_req);
477 this->processNextRequest();
481 HMI_SEQ_INFO(current_req, "Wait other App call endDraw");
486 result<json_object *> WindowManager::api_get_display_info()
489 if (!this->controller)
491 return Err<json_object *>("ivi_controller global not available");
495 compositor::size o_size = this->controller->output_size;
496 compositor::size p_size = this->controller->physical_size;
498 json_object *object = json_object_new_object();
499 json_object_object_add(object, kKeyWidthPixel, json_object_new_int(o_size.w));
500 json_object_object_add(object, kKeyHeightPixel, json_object_new_int(o_size.h));
501 json_object_object_add(object, kKeyWidthMm, json_object_new_int(p_size.w));
502 json_object_object_add(object, kKeyHeightMm, json_object_new_int(p_size.h));
504 return Ok<json_object *>(object);
507 result<json_object *> WindowManager::api_get_area_info(char const *drawing_name)
509 HMI_DEBUG("wm", "called");
511 // TODO: application requests by old role,
512 // so convert role old to new
513 const char *role = this->convertRoleOldToNew(drawing_name);
515 // Check drawing name, surface/layer id
516 auto const &surface_id = this->lookup_id(role);
519 return Err<json_object *>("Surface does not exist");
522 if (!this->controller->surface_exists(*surface_id))
524 return Err<json_object *>("Surface does not exist in controller!");
527 auto layer_id = this->layers.get_layer_id(*surface_id);
530 return Err<json_object *>("Surface is not on any layer!");
533 // Set area rectangle
534 compositor::rect area_info = this->area_info[*surface_id];
535 json_object *object = json_object_new_object();
536 json_object_object_add(object, kKeyX, json_object_new_int(area_info.x));
537 json_object_object_add(object, kKeyY, json_object_new_int(area_info.y));
538 json_object_object_add(object, kKeyWidth, json_object_new_int(area_info.w));
539 json_object_object_add(object, kKeyHeight, json_object_new_int(area_info.h));
541 return Ok<json_object *>(object);
544 void WindowManager::api_ping() { this->dispatch_pending_events(); }
546 void WindowManager::send_event(char const *evname, char const *label)
548 HMI_DEBUG("wm", "%s: %s(%s)", __func__, evname, label);
550 json_object *j = json_object_new_object();
551 json_object_object_add(j, kKeyDrawingName, json_object_new_string(label));
553 int ret = afb_event_push(this->map_afb_event[evname], j);
556 HMI_DEBUG("wm", "afb_event_push failed: %m");
560 void WindowManager::send_event(char const *evname, char const *label, char const *area,
561 int x, int y, int w, int h)
563 HMI_DEBUG("wm", "%s: %s(%s, %s) x:%d y:%d w:%d h:%d",
564 __func__, evname, label, area, x, y, w, h);
566 json_object *j_rect = json_object_new_object();
567 json_object_object_add(j_rect, kKeyX, json_object_new_int(x));
568 json_object_object_add(j_rect, kKeyY, json_object_new_int(y));
569 json_object_object_add(j_rect, kKeyWidth, json_object_new_int(w));
570 json_object_object_add(j_rect, kKeyHeight, json_object_new_int(h));
572 json_object *j = json_object_new_object();
573 json_object_object_add(j, kKeyDrawingName, json_object_new_string(label));
574 json_object_object_add(j, kKeyDrawingArea, json_object_new_string(area));
575 json_object_object_add(j, kKeyDrawingRect, j_rect);
577 int ret = afb_event_push(this->map_afb_event[evname], j);
580 HMI_DEBUG("wm", "afb_event_push failed: %m");
587 void WindowManager::surface_created(uint32_t surface_id)
589 auto layer_id = this->layers.get_layer_id(surface_id);
592 HMI_DEBUG("wm", "Newly created surfce %d is not associated with any layer!",
597 HMI_DEBUG("wm", "surface_id is %u, layer_id is %u", surface_id, *layer_id);
599 this->controller->layers[*layer_id]->add_surface(surface_id);
600 this->layout_commit();
603 void WindowManager::surface_removed(uint32_t surface_id)
605 HMI_DEBUG("wm", "surface_id is %u", surface_id);
606 g_app_list.removeSurface(surface_id);
609 void WindowManager::removeClient(const std::string &appid)
611 HMI_DEBUG("wm", "Remove clinet %s from list", appid.c_str());
612 g_app_list.removeClient(appid);
615 void WindowManager::exceptionProcessForTransition()
617 unsigned req_num = g_app_list.currentRequestNumber();
618 HMI_SEQ_NOTICE(req_num, "Process exception handling for request. Remove current request %d", req_num);
619 g_app_list.removeRequest(req_num);
620 HMI_SEQ_NOTICE(g_app_list.currentRequestNumber(), "Process next request if exists");
621 this->processNextRequest();
624 void WindowManager::timerHandler()
626 unsigned req_num = g_app_list.currentRequestNumber();
627 HMI_SEQ_DEBUG(req_num, "Timer expired remove Request");
628 g_app_list.reqDump();
629 g_app_list.removeRequest(req_num);
630 this->processNextRequest();
633 void WindowManager::startTransitionWrapper(std::vector<WMAction> &actions)
636 unsigned req_num = g_app_list.currentRequestNumber();
640 if (g_app_list.haveRequest())
642 HMI_SEQ_DEBUG(req_num, "There is no WMAction for this request");
643 goto proc_remove_request;
647 HMI_SEQ_DEBUG(req_num, "There is no request");
652 for (auto &act : actions)
657 auto const &surface_id = this->lookup_id(act.role.c_str());
658 std::string appid = g_app_list.getAppID(*surface_id, act.role, &found);
666 ret = g_app_list.setAction(req_num, act);
667 if (ret != WMError::SUCCESS)
673 HMI_SEQ_DEBUG(req_num, "Start transition.");
674 ret = this->startTransition(req_num);
675 if (ret != WMError::SUCCESS)
677 if (ret == WMError::NO_LAYOUT_CHANGE)
679 goto proc_remove_request;
691 HMI_SEQ_ERROR(req_num, errorDescription(ret));
692 this->pmw.undoState();
695 g_app_list.removeRequest(req_num);
696 this->processNextRequest();
699 void WindowManager::processError(WMError error)
701 unsigned req_num = g_app_list.currentRequestNumber();
704 HMI_SEQ_ERROR(req_num, errorDescription(error));
705 g_app_list.removeRequest(req_num);
706 this->processNextRequest();
710 ******* Private Functions *******
713 bool WindowManager::pop_pending_events()
716 return this->pending_events.compare_exchange_strong(
717 x, false, std::memory_order_consume);
720 optional<int> WindowManager::lookup_id(char const *name)
722 return this->id_alloc.lookup(std::string(name));
724 optional<std::string> WindowManager::lookup_name(int id)
726 return this->id_alloc.lookup(id);
732 int WindowManager::init_layers()
734 if (!this->controller)
736 HMI_ERROR("wm", "ivi_controller global not available");
740 if (this->outputs.empty())
742 HMI_ERROR("wm", "no output was set up!");
747 wm_config.loadConfigs();
749 auto &c = this->controller;
751 auto &o = this->outputs.front();
752 auto &s = c->screens.begin()->second;
753 auto &layers = c->layers;
755 this->layers.loadAreaDb();
756 const compositor::rect base = this->layers.getAreaSize("fullscreen");
758 const std::string aspect_setting = wm_config.getConfigAspect();
759 const compositor::rect scale_rect =
760 this->layers.getScaleDestRect(o->width, o->height, aspect_setting);
762 // Write output dimensions to ivi controller...
763 c->output_size = compositor::size{uint32_t(o->width), uint32_t(o->height)};
764 c->physical_size = compositor::size{uint32_t(o->physical_width),
765 uint32_t(o->physical_height)};
773 // Quick and dirty setup of layers
774 for (auto const &i : this->layers.mapping)
776 c->layer_create(i.second.layer_id, scale_rect.w, scale_rect.h);
777 auto &l = layers[i.second.layer_id];
778 l->set_source_rectangle(0, 0, base.w, base.h);
779 l->set_destination_rectangle(
780 scale_rect.x, scale_rect.y, scale_rect.w, scale_rect.h);
781 l->set_visibility(1);
782 HMI_DEBUG("wm", "Setting up layer %s (%d) for surface role match \"%s\"",
783 i.second.name.c_str(), i.second.layer_id, i.second.role.c_str());
786 // Add layers to screen
787 s->set_render_order(this->layers.layers);
789 this->layout_commit();
794 void WindowManager::surface_set_layout(int surface_id, const std::string& area)
796 if (!this->controller->surface_exists(surface_id))
798 HMI_ERROR("wm", "Surface %d does not exist", surface_id);
802 auto o_layer_id = this->layers.get_layer_id(surface_id);
806 HMI_ERROR("wm", "Surface %d is not associated with any layer!", surface_id);
810 uint32_t layer_id = *o_layer_id;
812 auto const &layer = this->layers.get_layer(layer_id);
813 auto rect = this->layers.getAreaSize(area);
814 HMI_SEQ_DEBUG(g_app_list.currentRequestNumber(), "%s : x:%d y:%d w:%d h:%d", area.c_str(),
815 rect.x, rect.y, rect.w, rect.h);
816 auto &s = this->controller->surfaces[surface_id];
823 HMI_DEBUG("wm", "surface_set_layout for surface %u on layer %u", surface_id,
826 // set destination to the display rectangle
827 s->set_destination_rectangle(x, y, w, h);
829 // update area information
830 this->area_info[surface_id].x = x;
831 this->area_info[surface_id].y = y;
832 this->area_info[surface_id].w = w;
833 this->area_info[surface_id].h = h;
835 HMI_DEBUG("wm", "Surface %u now on layer %u with rect { %d, %d, %d, %d }",
836 surface_id, layer_id, x, y, w, h);
839 void WindowManager::layout_commit()
841 this->controller->commit_changes();
842 this->display->flush();
845 void WindowManager::emit_activated(char const *label)
847 this->send_event(kListEventName[Event_Active], label);
850 void WindowManager::emit_deactivated(char const *label)
852 this->send_event(kListEventName[Event_Inactive], label);
855 void WindowManager::emit_syncdraw(char const *label, char const *area, int x, int y, int w, int h)
857 this->send_event(kListEventName[Event_SyncDraw], label, area, x, y, w, h);
860 void WindowManager::emit_syncdraw(const std::string &role, const std::string &area)
862 compositor::rect rect = this->layers.getAreaSize(area);
863 this->send_event(kListEventName[Event_SyncDraw],
864 role.c_str(), area.c_str(), rect.x, rect.y, rect.w, rect.h);
867 void WindowManager::emit_flushdraw(char const *label)
869 this->send_event(kListEventName[Event_FlushDraw], label);
872 void WindowManager::emit_visible(char const *label, bool is_visible)
874 this->send_event(is_visible ? kListEventName[Event_Visible] : kListEventName[Event_Invisible], label);
877 void WindowManager::emit_invisible(char const *label)
879 return emit_visible(label, false);
882 void WindowManager::emit_visible(char const *label) { return emit_visible(label, true); }
884 void WindowManager::activate(int id)
886 auto ip = this->controller->sprops.find(id);
887 if (ip != this->controller->sprops.end())
889 this->controller->surfaces[id]->set_visibility(1);
891 this->lookup_name(id).value_or("unknown-name").c_str();
894 if ((0 == strcmp(label, "radio")) ||
895 (0 == strcmp(label, "music")) ||
896 (0 == strcmp(label, "video")) ||
897 (0 == strcmp(label, "map")))
899 for (auto i = surface_bg.begin(); i != surface_bg.end(); ++i)
904 this->surface_bg.erase(i);
906 // Remove from BG layer (999)
907 HMI_DEBUG("wm", "Remove %s(%d) from BG layer", label, id);
908 this->controller->layers[999]->remove_surface(id);
910 // Add to FG layer (1001)
911 HMI_DEBUG("wm", "Add %s(%d) to FG layer", label, id);
912 this->controller->layers[1001]->add_surface(id);
914 for (int j : this->surface_bg)
916 HMI_DEBUG("wm", "Stored id:%d", j);
924 this->layout_commit();
926 // TODO: application requests by old role,
927 // so convert role new to old for emitting event
928 const char* old_role = this->rolenew2old[label].c_str();
930 this->emit_visible(old_role);
931 this->emit_activated(old_role);
935 void WindowManager::deactivate(int id)
937 auto ip = this->controller->sprops.find(id);
938 if (ip != this->controller->sprops.end())
941 this->lookup_name(id).value_or("unknown-name").c_str();
944 if ((0 == strcmp(label, "radio")) ||
945 (0 == strcmp(label, "music")) ||
946 (0 == strcmp(label, "video")) ||
947 (0 == strcmp(label, "map")))
951 this->surface_bg.push_back(id);
953 // Remove from FG layer (1001)
954 HMI_DEBUG("wm", "Remove %s(%d) from FG layer", label, id);
955 this->controller->layers[1001]->remove_surface(id);
957 // Add to BG layer (999)
958 HMI_DEBUG("wm", "Add %s(%d) to BG layer", label, id);
959 this->controller->layers[999]->add_surface(id);
961 for (int j : surface_bg)
963 HMI_DEBUG("wm", "Stored id:%d", j);
968 this->controller->surfaces[id]->set_visibility(0);
972 this->layout_commit();
974 // TODO: application requests by old role,
975 // so convert role new to old for emitting event
976 const char* old_role = this->rolenew2old[label].c_str();
978 this->emit_deactivated(old_role);
979 this->emit_invisible(old_role);
983 WMError WindowManager::setRequest(const std::string& appid, const std::string &role, const std::string &area,
984 Task task, unsigned* req_num)
986 if (!g_app_list.contains(appid))
988 return WMError::NOT_REGISTERED;
991 auto client = g_app_list.lookUpClient(appid);
996 unsigned current = g_app_list.currentRequestNumber();
997 unsigned requested_num = g_app_list.getRequestNumber(appid);
998 if (requested_num != 0)
1000 HMI_SEQ_INFO(requested_num,
1001 "%s %s %s request is already queued", appid.c_str(), role.c_str(), area.c_str());
1002 return REQ_REJECTED;
1005 WMRequest req = WMRequest(appid, role, area, task);
1006 unsigned new_req = g_app_list.addRequest(req);
1008 g_app_list.reqDump();
1010 HMI_SEQ_DEBUG(current, "%s start sequence with %s, %s", appid.c_str(), role.c_str(), area.c_str());
1012 return WMError::SUCCESS;
1015 WMError WindowManager::doTransition(unsigned req_num)
1017 HMI_SEQ_DEBUG(req_num, "check policy");
1018 WMError ret = this->checkPolicy(req_num);
1022 WMError WindowManager::checkPolicy(unsigned req_num)
1027 // get current trigger
1029 WMError ret = WMError::LAYOUT_CHANGE_FAIL;
1030 auto trigger = g_app_list.getRequest(req_num, &found);
1033 ret = WMError::NO_ENTRY;
1036 std::string req_area = trigger.area;
1038 if (trigger.task == Task::TASK_ALLOCATE)
1040 const char *msg = this->check_surface_exist(trigger.role.c_str());
1044 HMI_SEQ_ERROR(req_num, msg);
1049 // Input event data to PolicyManager
1050 if (0 > this->pmw.setInputEventData(trigger.task, trigger.role, trigger.area))
1052 HMI_SEQ_ERROR(req_num, "Failed to set input event data to PolicyManager");
1056 // Execute state transition of PolicyManager
1057 if (0 > this->pmw.executeStateTransition())
1059 HMI_SEQ_ERROR(req_num, "Failed to execute state transition of PolicyManager");
1063 ret = WMError::SUCCESS;
1065 g_app_list.reqDump();
1070 WMError WindowManager::startTransition(unsigned req_num)
1072 bool sync_draw_happen = false;
1074 WMError ret = WMError::SUCCESS;
1075 auto actions = g_app_list.getActions(req_num, &found);
1078 ret = WMError::NO_ENTRY;
1079 HMI_SEQ_ERROR(req_num,
1080 "Window Manager bug :%s : Action is not set", errorDescription(ret));
1084 for (const auto &action : actions)
1086 if (action.visible == TaskVisible::VISIBLE)
1088 sync_draw_happen = true;
1090 // TODO: application requests by old role,
1091 // so convert role new to old for emitting event
1092 std::string old_role = this->rolenew2old[action.role];
1094 this->emit_syncdraw(old_role, action.area);
1095 /* TODO: emit event for app not subscriber
1096 if(g_app_list.contains(y.appid))
1097 g_app_list.lookUpClient(y.appid)->emit_syncdraw(y.role, y.area); */
1101 if (sync_draw_happen)
1107 // deactivate only, no syncDraw
1108 // Make it deactivate here
1109 for (const auto &x : actions)
1111 if (g_app_list.contains(x.appid))
1113 auto client = g_app_list.lookUpClient(x.appid);
1114 this->deactivate(client->surfaceID(x.role));
1117 ret = WMError::NO_LAYOUT_CHANGE;
1122 WMError WindowManager::doEndDraw(unsigned req_num)
1126 auto actions = g_app_list.getActions(req_num, &found);
1127 WMError ret = WMError::SUCCESS;
1130 ret = WMError::NO_ENTRY;
1134 HMI_SEQ_INFO(req_num, "do endDraw");
1136 // layout change and make it visible
1137 for (const auto &act : actions)
1139 if(act.visible != TaskVisible::NO_CHANGE)
1142 if(!g_app_list.contains(act.appid)){
1143 ret = WMError::NOT_REGISTERED;
1145 ret = this->layoutChange(act);
1146 if(ret != WMError::SUCCESS)
1148 HMI_SEQ_WARNING(req_num,
1149 "Failed to manipulate surfaces while state change : %s", errorDescription(ret));
1152 ret = this->visibilityChange(act);
1153 if (ret != WMError::SUCCESS)
1155 HMI_SEQ_WARNING(req_num,
1156 "Failed to manipulate surfaces while state change : %s", errorDescription(ret));
1159 HMI_SEQ_DEBUG(req_num, "visible %s", act.role.c_str());
1160 //this->lm_enddraw(act.role.c_str());
1164 HMI_SEQ_INFO(req_num, "emit flushDraw");
1166 for(const auto &act_flush : actions)
1168 if(act_flush.visible == TaskVisible::VISIBLE)
1170 // TODO: application requests by old role,
1171 // so convert role new to old for emitting event
1172 std::string old_role = this->rolenew2old[act_flush.role];
1174 this->emit_flushdraw(old_role.c_str());
1181 WMError WindowManager::layoutChange(const WMAction &action)
1183 if (action.visible == TaskVisible::INVISIBLE)
1185 // Visibility is not change -> no redraw is required
1186 return WMError::SUCCESS;
1188 auto client = g_app_list.lookUpClient(action.appid);
1189 unsigned surface = client->surfaceID(action.role);
1192 HMI_SEQ_ERROR(g_app_list.currentRequestNumber(),
1193 "client doesn't have surface with role(%s)", action.role.c_str());
1194 return WMError::NOT_REGISTERED;
1197 WMError ret = this->setSurfaceSize(surface, action.area);
1201 WMError WindowManager::visibilityChange(const WMAction &action)
1203 HMI_SEQ_DEBUG(g_app_list.currentRequestNumber(), "Change visibility");
1204 if(!g_app_list.contains(action.appid)){
1205 return WMError::NOT_REGISTERED;
1207 auto client = g_app_list.lookUpClient(action.appid);
1208 unsigned surface = client->surfaceID(action.role);
1211 HMI_SEQ_ERROR(g_app_list.currentRequestNumber(),
1212 "client doesn't have surface with role(%s)", action.role.c_str());
1213 return WMError::NOT_REGISTERED;
1216 if (action.visible != TaskVisible::INVISIBLE)
1218 this->activate(surface); // Layout Manager task
1222 this->deactivate(surface); // Layout Manager task
1224 return WMError::SUCCESS;
1227 WMError WindowManager::setSurfaceSize(unsigned surface, const std::string &area)
1229 this->surface_set_layout(surface, area);
1231 return WMError::SUCCESS;
1234 void WindowManager::emitScreenUpdated(unsigned req_num)
1237 HMI_SEQ_DEBUG(req_num, "emit screen updated");
1239 auto actions = g_app_list.getActions(req_num, &found);
1241 // create json object
1242 json_object *j = json_object_new_object();
1243 json_object *jarray = json_object_new_array();
1245 for(const auto& action: actions)
1247 if(action.visible != TaskVisible::INVISIBLE)
1249 json_object_array_add(jarray, json_object_new_string(action.appid.c_str()));
1252 json_object_object_add(j, kKeyIds, jarray);
1253 HMI_SEQ_INFO(req_num, "Visible app: %s", json_object_get_string(j));
1255 int ret = afb_event_push(
1256 this->map_afb_event[kListEventName[Event_ScreenUpdated]], j);
1259 HMI_DEBUG("wm", "afb_event_push failed: %m");
1261 json_object_put(jarray);
1264 void WindowManager::setTimer()
1266 HMI_SEQ_DEBUG(g_app_list.currentRequestNumber(), "Timer set activate");
1267 if (g_timer_ev_src == nullptr)
1269 // firsttime set into sd_event
1270 int ret = sd_event_add_time(afb_daemon_get_event_loop(), &g_timer_ev_src,
1271 CLOCK_REALTIME, time(NULL) * (1000000UL) + kTimeOut, 1, processTimerHandler, this);
1274 HMI_ERROR("wm", "Could't set timer");
1279 // update timer limitation after second time
1280 sd_event_source_set_time(g_timer_ev_src, time(NULL) * (1000000UL) + kTimeOut);
1281 sd_event_source_set_enabled(g_timer_ev_src, SD_EVENT_ONESHOT);
1285 void WindowManager::stopTimer()
1287 unsigned req_num = g_app_list.currentRequestNumber();
1288 HMI_SEQ_DEBUG(req_num, "Timer stop");
1289 int rc = sd_event_source_set_enabled(g_timer_ev_src, SD_EVENT_OFF);
1292 HMI_SEQ_ERROR(req_num, "Timer stop failed");
1296 void WindowManager::processNextRequest()
1299 g_app_list.reqDump();
1300 unsigned req_num = g_app_list.currentRequestNumber();
1301 if (g_app_list.haveRequest())
1303 HMI_SEQ_DEBUG(req_num, "Process next request");
1304 WMError rc = doTransition(req_num);
1305 if (rc != WMError::SUCCESS)
1307 HMI_SEQ_ERROR(req_num, errorDescription(rc));
1312 HMI_SEQ_DEBUG(req_num, "Nothing Request. Waiting Request");
1316 const char* WindowManager::convertRoleOldToNew(char const *old_role)
1318 const char *new_role = nullptr;
1320 for (auto const &on : this->roleold2new)
1322 std::regex regex = std::regex(on.first);
1323 if (std::regex_match(old_role, regex))
1325 // role is old. So convert to new.
1326 new_role = on.second.c_str();
1331 if (nullptr == new_role)
1333 // role is new or fallback.
1334 new_role = old_role;
1337 HMI_DEBUG("wm", "old:%s -> new:%s", old_role, new_role);
1342 int WindowManager::loadOldRoleDb()
1344 // Get afm application installed dir
1345 char const *afm_app_install_dir = getenv("AFM_APP_INSTALL_DIR");
1346 HMI_DEBUG("wm", "afm_app_install_dir:%s", afm_app_install_dir);
1348 std::string file_name;
1349 if (!afm_app_install_dir)
1351 HMI_ERROR("wm", "AFM_APP_INSTALL_DIR is not defined");
1355 file_name = std::string(afm_app_install_dir) + std::string("/etc/old_roles.db");
1359 json_object* json_obj;
1360 int ret = jh::inputJsonFilie(file_name.c_str(), &json_obj);
1363 HMI_ERROR("wm", "Could not open old_role.db, so use default old_role information");
1364 json_obj = json_tokener_parse(kDefaultOldRoleDb);
1366 HMI_DEBUG("wm", "json_obj dump:%s", json_object_get_string(json_obj));
1369 json_object* json_cfg;
1370 if (!json_object_object_get_ex(json_obj, "old_roles", &json_cfg))
1372 HMI_ERROR("wm", "Parse Error!!");
1376 int len = json_object_array_length(json_cfg);
1377 HMI_DEBUG("wm", "json_cfg len:%d", len);
1378 HMI_DEBUG("wm", "json_cfg dump:%s", json_object_get_string(json_cfg));
1380 for (int i=0; i<len; i++)
1382 json_object* json_tmp = json_object_array_get_idx(json_cfg, i);
1384 const char* old_role = jh::getStringFromJson(json_tmp, "name");
1385 if (nullptr == old_role)
1387 HMI_ERROR("wm", "Parse Error!!");
1391 const char* new_role = jh::getStringFromJson(json_tmp, "new");
1392 if (nullptr == new_role)
1394 HMI_ERROR("wm", "Parse Error!!");
1398 this->roleold2new[old_role] = std::string(new_role);
1402 for(auto itr = this->roleold2new.begin();
1403 itr != this->roleold2new.end(); ++itr)
1405 HMI_DEBUG("wm", ">>> role old:%s new:%s",
1406 itr->first.c_str(), itr->second.c_str());
1409 // Release json_object
1410 json_object_put(json_obj);
1415 const char *WindowManager::check_surface_exist(const char *drawing_name)
1417 auto const &surface_id = this->lookup_id(drawing_name);
1420 return "Surface does not exist";
1423 if (!this->controller->surface_exists(*surface_id))
1425 return "Surface does not exist in controller!";
1428 auto layer_id = this->layers.get_layer_id(*surface_id);
1432 return "Surface is not on any layer!";
1435 HMI_DEBUG("wm", "surface %d is detected", *surface_id);
1439 const char* WindowManager::kDefaultOldRoleDb = "{ \
1442 \"name\": \"HomeScreen\", \
1443 \"new\": \"homescreen\" \
1446 \"name\": \"Music\", \
1447 \"new\": \"music\" \
1450 \"name\": \"MediaPlayer\", \
1451 \"new\": \"music\" \
1454 \"name\": \"Video\", \
1455 \"new\": \"video\" \
1458 \"name\": \"VideoPlayer\", \
1459 \"new\": \"video\" \
1462 \"name\": \"WebBrowser\", \
1463 \"new\": \"browser\" \
1466 \"name\": \"Radio\", \
1467 \"new\": \"radio\" \
1470 \"name\": \"Phone\", \
1471 \"new\": \"phone\" \
1474 \"name\": \"Navigation\", \
1478 \"name\": \"HVAC\", \
1482 \"name\": \"Settings\", \
1483 \"new\": \"settings\" \
1486 \"name\": \"Dashboard\", \
1487 \"new\": \"dashboard\" \
1490 \"name\": \"POI\", \
1494 \"name\": \"Mixer\", \
1495 \"new\": \"mixer\" \
1498 \"name\": \"Restriction\", \
1499 \"new\": \"restriction\" \
1502 \"name\": \"^OnScreen.*\", \
1503 \"new\": \"on_screen\" \
1511 void controller_hooks::surface_created(uint32_t surface_id)
1513 this->app->surface_created(surface_id);
1516 void controller_hooks::surface_removed(uint32_t surface_id)
1518 this->app->surface_removed(surface_id);
1521 void controller_hooks::surface_visibility(uint32_t /*surface_id*/,
1524 void controller_hooks::surface_destination_rectangle(uint32_t /*surface_id*/,