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 "applist.hpp"
26 #include <systemd/sd-event.h>
32 static const unsigned kTimeOut = 3000000UL; /* 3s */
34 /* DrawingArea name used by "{layout}.{area}" */
35 const char kNameLayoutNormal[] = "normal";
36 const char kNameLayoutSplit[] = "split";
37 const char kNameAreaFull[] = "full";
38 const char kNameAreaMain[] = "main";
39 const char kNameAreaSub[] = "sub";
41 /* Key for json obejct */
42 const char kKeyDrawingName[] = "drawing_name";
43 const char kKeyDrawingArea[] = "drawing_area";
44 const char kKeyDrawingRect[] = "drawing_rect";
45 const char kKeyX[] = "x";
46 const char kKeyY[] = "y";
47 const char kKeyWidth[] = "width";
48 const char kKeyHeight[] = "height";
49 const char kKeyWidthPixel[] = "width_pixel";
50 const char kKeyHeightPixel[] = "height_pixel";
51 const char kKeyWidthMm[] = "width_mm";
52 const char kKeyHeightMm[] = "height_mm";
53 const char kKeyIds[] = "ids";
55 static sd_event_source *g_timer_ev_src = nullptr;
56 static AppList g_app_list;
63 result<json> file_to_json(char const *filename)
66 std::ifstream i(filename);
69 HMI_DEBUG("wm", "Could not open config file, so use default layer information");
70 j = default_layers_json;
80 struct result<layer_map> load_layer_map(char const *filename)
82 HMI_DEBUG("wm", "loading IDs from %s", filename);
84 auto j = file_to_json(filename);
87 return Err<layer_map>(j.unwrap_err());
89 json jids = j.unwrap();
91 return to_layer_map(jids);
94 static int processTimerHandler(sd_event_source *s, uint64_t usec, void *userdata)
96 HMI_NOTICE("wm", "Time out occurs because the client replys endDraw slow, so revert the request");
97 reinterpret_cast<wm::WindowManager *>(userdata)->timerHandler();
106 WindowManager::WindowManager(wl::display *d)
113 pending_events(false)
115 char const *path_layers_json = getenv("AFM_APP_INSTALL_DIR");
117 if (!path_layers_json)
119 HMI_ERROR("wm", "AFM_APP_INSTALL_DIR is not defined");
120 path = std::string(path_layers_json);
124 path = std::string(path_layers_json) + std::string("/etc/layers.json");
130 auto l = load_layer_map(path.c_str());
133 this->layers = l.unwrap();
137 HMI_ERROR("wm", "%s", l.err().value());
141 catch (std::exception &e)
143 HMI_ERROR("wm", "Loading of configuration failed: %s", e.what());
147 int WindowManager::init()
149 if (!this->display->ok())
154 if (this->layers.mapping.empty())
156 HMI_ERROR("wm", "No surface -> layer mapping loaded");
160 // TODO: application requests by old role,
161 // so create role map (old, new)
163 this->loadOldRoleDb();
166 for (int i = Event_Val_Min; i <= Event_Val_Max; i++)
168 map_afb_event[kListEventName[i]] = afb_daemon_make_event(kListEventName[i]);
171 this->display->add_global_handler(
172 "wl_output", [this](wl_registry *r, uint32_t name, uint32_t v) {
173 this->outputs.emplace_back(std::make_unique<wl::output>(r, name, v));
176 this->display->add_global_handler(
177 "ivi_wm", [this](wl_registry *r, uint32_t name, uint32_t v) {
179 std::make_unique<struct compositor::controller>(r, name, v);
181 // Init controller hooks
182 this->controller->chooks = &this->chooks;
184 // This protocol needs the output, so lets just add our mapping here...
185 this->controller->add_proxy_to_id_mapping(
186 this->outputs.back()->proxy.get(),
187 wl_proxy_get_id(reinterpret_cast<struct wl_proxy *>(
188 this->outputs.back()->proxy.get())));
191 this->controller->create_screen(this->outputs.back()->proxy.get());
193 // Set display to controller
194 this->controller->display = this->display;
197 // First level objects
198 this->display->roundtrip();
199 // Second level objects
200 this->display->roundtrip();
201 // Third level objects
202 this->display->roundtrip();
204 return init_layers();
207 int WindowManager::dispatch_pending_events()
209 if (this->pop_pending_events())
211 this->display->dispatch_pending();
217 void WindowManager::set_pending_events()
219 this->pending_events.store(true, std::memory_order_release);
222 result<int> WindowManager::api_request_surface(char const *appid, char const *drawing_name)
224 // TODO: application requests by old role,
225 // so convert role old to new
226 const char *role = this->convertRoleOldToNew(drawing_name);
228 auto lid = this->layers.get_layer_id(std::string(role));
232 * register drawing_name as fallback and make it displayed.
234 lid = this->layers.get_layer_id(std::string("fallback"));
235 HMI_DEBUG("wm", "%s is not registered in layers.json, then fallback as normal app", role);
238 return Err<int>("Drawing name does not match any role, Fallback is disabled");
242 auto rname = this->lookup_id(role);
245 // name does not exist yet, allocate surface id...
246 auto id = int(this->id_alloc.generate_id(role));
247 this->layers.add_surface(id, *lid);
249 // set the main_surface[_name] here and now
250 if (!this->layers.main_surface_name.empty() &&
251 this->layers.main_surface_name == drawing_name)
253 this->layers.main_surface = id;
254 HMI_DEBUG("wm", "Set main_surface id to %u", id);
257 // add client into the db
258 std::string appid_str(appid);
259 g_app_list.addClient(appid_str, *lid, id, std::string(role));
261 // Set role map of (new, old)
262 this->rolenew2old[role] = std::string(drawing_name);
267 // Check currently registered drawing names if it is already there.
268 return Err<int>("Surface already present");
271 char const *WindowManager::api_request_surface(char const *appid, char const *drawing_name,
276 // TODO: application requests by old role,
277 // so convert role old to new
278 const char *role = this->convertRoleOldToNew(drawing_name);
280 auto lid = this->layers.get_layer_id(std::string(role));
281 unsigned sid = std::stol(ivi_id);
286 * register drawing_name as fallback and make it displayed.
288 lid = this->layers.get_layer_id(std::string("fallback"));
289 HMI_DEBUG("wm", "%s is not registered in layers.json, then fallback as normal app", role);
292 return "Drawing name does not match any role, Fallback is disabled";
296 auto rname = this->lookup_id(role);
300 return "Surface already present";
303 // register pair drawing_name and ivi_id
304 this->id_alloc.register_name_id(role, sid);
305 this->layers.add_surface(sid, *lid);
307 // this surface is already created
308 HMI_DEBUG("wm", "surface_id is %u, layer_id is %u", sid, *lid);
310 this->controller->layers[*lid]->add_surface(sid);
311 this->layout_commit();
313 // add client into the db
314 std::string appid_str(appid);
315 g_app_list.addClient(appid_str, *lid, sid, std::string(role));
317 // Set role map of (new, old)
318 this->rolenew2old[role] = std::string(drawing_name);
323 void WindowManager::api_activate_surface(char const *appid, char const *drawing_name,
324 char const *drawing_area, const reply_func &reply)
328 // TODO: application requests by old role,
329 // so convert role old to new
330 const char *c_role = this->convertRoleOldToNew(drawing_name);
332 std::string id = appid;
333 std::string role = c_role;
334 std::string area = drawing_area;
335 Task task = Task::TASK_ALLOCATE;
336 unsigned req_num = 0;
337 WMError ret = WMError::UNKNOWN;
339 ret = this->setRequest(id, role, area, task, &req_num);
341 if(ret != WMError::SUCCESS)
343 HMI_ERROR("wm", errorDescription(ret));
344 reply("Failed to set request");
349 if (req_num != g_app_list.currentRequestNumber())
351 // Add request, then invoked after the previous task is finished
352 HMI_SEQ_DEBUG(req_num, "request is accepted");
359 ret = this->doTransition(req_num);
361 if (ret != WMError::SUCCESS)
364 HMI_SEQ_ERROR(req_num, errorDescription(ret));
365 g_app_list.removeRequest(req_num);
366 this->processNextRequest();
370 void WindowManager::api_deactivate_surface(char const *appid, char const *drawing_name,
371 const reply_func &reply)
375 // TODO: application requests by old role,
376 // so convert role old to new
377 const char *c_role = this->convertRoleOldToNew(drawing_name);
382 std::string id = appid;
383 std::string role = c_role;
384 std::string area = ""; //drawing_area;
385 Task task = Task::TASK_RELEASE;
386 unsigned req_num = 0;
387 WMError ret = WMError::UNKNOWN;
389 ret = this->setRequest(id, role, area, task, &req_num);
391 if (ret != WMError::SUCCESS)
393 HMI_ERROR("wm", errorDescription(ret));
394 reply("Failed to set request");
399 if (req_num != g_app_list.currentRequestNumber())
401 // Add request, then invoked after the previous task is finished
402 HMI_SEQ_DEBUG(req_num, "request is accepted");
409 ret = this->doTransition(req_num);
411 if (ret != WMError::SUCCESS)
414 HMI_SEQ_ERROR(req_num, errorDescription(ret));
415 g_app_list.removeRequest(req_num);
416 this->processNextRequest();
420 void WindowManager::api_enddraw(char const *appid, char const *drawing_name)
422 // TODO: application requests by old role,
423 // so convert role old to new
424 const char *c_role = this->convertRoleOldToNew(drawing_name);
426 std::string id = appid;
427 std::string role = c_role;
428 unsigned current_req = g_app_list.currentRequestNumber();
429 bool result = g_app_list.setEndDrawFinished(current_req, id, role);
433 HMI_ERROR("wm", "%s is not in transition state", id.c_str());
437 if (g_app_list.endDrawFullfilled(current_req))
439 // do task for endDraw
441 WMError ret = this->doEndDraw(current_req);
443 if(ret != WMError::SUCCESS)
445 //this->emit_error();
447 this->emitScreenUpdated(current_req);
448 HMI_SEQ_INFO(current_req, "Finish request status: %s", errorDescription(ret));
450 g_app_list.removeRequest(current_req);
452 this->processNextRequest();
456 HMI_SEQ_INFO(current_req, "Wait other App call endDraw");
461 result<json_object *> WindowManager::api_get_display_info()
464 if (!this->controller)
466 return Err<json_object *>("ivi_controller global not available");
470 compositor::size o_size = this->controller->output_size;
471 compositor::size p_size = this->controller->physical_size;
473 json_object *object = json_object_new_object();
474 json_object_object_add(object, kKeyWidthPixel, json_object_new_int(o_size.w));
475 json_object_object_add(object, kKeyHeightPixel, json_object_new_int(o_size.h));
476 json_object_object_add(object, kKeyWidthMm, json_object_new_int(p_size.w));
477 json_object_object_add(object, kKeyHeightMm, json_object_new_int(p_size.h));
479 return Ok<json_object *>(object);
482 result<json_object *> WindowManager::api_get_area_info(char const *drawing_name)
484 HMI_DEBUG("wm", "called");
486 // TODO: application requests by old role,
487 // so convert role old to new
488 const char *role = this->convertRoleOldToNew(drawing_name);
490 // Check drawing name, surface/layer id
491 auto const &surface_id = this->lookup_id(role);
494 return Err<json_object *>("Surface does not exist");
497 if (!this->controller->surface_exists(*surface_id))
499 return Err<json_object *>("Surface does not exist in controller!");
502 auto layer_id = this->layers.get_layer_id(*surface_id);
505 return Err<json_object *>("Surface is not on any layer!");
508 auto o_state = *this->layers.get_layout_state(*surface_id);
509 if (o_state == nullptr)
511 return Err<json_object *>("Could not find layer for surface");
514 struct LayoutState &state = *o_state;
515 if ((state.main != *surface_id) && (state.sub != *surface_id))
517 return Err<json_object *>("Surface is inactive");
520 // Set area rectangle
521 compositor::rect area_info = this->area_info[*surface_id];
522 json_object *object = json_object_new_object();
523 json_object_object_add(object, kKeyX, json_object_new_int(area_info.x));
524 json_object_object_add(object, kKeyY, json_object_new_int(area_info.y));
525 json_object_object_add(object, kKeyWidth, json_object_new_int(area_info.w));
526 json_object_object_add(object, kKeyHeight, json_object_new_int(area_info.h));
528 return Ok<json_object *>(object);
531 void WindowManager::api_ping() { this->dispatch_pending_events(); }
533 void WindowManager::send_event(char const *evname, char const *label)
535 HMI_DEBUG("wm", "%s: %s(%s)", __func__, evname, label);
537 json_object *j = json_object_new_object();
538 json_object_object_add(j, kKeyDrawingName, json_object_new_string(label));
540 int ret = afb_event_push(this->map_afb_event[evname], j);
543 HMI_DEBUG("wm", "afb_event_push failed: %m");
547 void WindowManager::send_event(char const *evname, char const *label, char const *area,
548 int x, int y, int w, int h)
550 HMI_DEBUG("wm", "%s: %s(%s, %s) x:%d y:%d w:%d h:%d",
551 __func__, evname, label, area, x, y, w, h);
553 json_object *j_rect = json_object_new_object();
554 json_object_object_add(j_rect, kKeyX, json_object_new_int(x));
555 json_object_object_add(j_rect, kKeyY, json_object_new_int(y));
556 json_object_object_add(j_rect, kKeyWidth, json_object_new_int(w));
557 json_object_object_add(j_rect, kKeyHeight, json_object_new_int(h));
559 json_object *j = json_object_new_object();
560 json_object_object_add(j, kKeyDrawingName, json_object_new_string(label));
561 json_object_object_add(j, kKeyDrawingArea, json_object_new_string(area));
562 json_object_object_add(j, kKeyDrawingRect, j_rect);
564 int ret = afb_event_push(this->map_afb_event[evname], j);
567 HMI_DEBUG("wm", "afb_event_push failed: %m");
574 void WindowManager::surface_created(uint32_t surface_id)
576 auto layer_id = this->layers.get_layer_id(surface_id);
579 HMI_DEBUG("wm", "Newly created surfce %d is not associated with any layer!",
584 HMI_DEBUG("wm", "surface_id is %u, layer_id is %u", surface_id, *layer_id);
586 this->controller->layers[*layer_id]->add_surface(surface_id);
587 this->layout_commit();
590 void WindowManager::surface_removed(uint32_t surface_id)
592 HMI_DEBUG("wm", "surface_id is %u", surface_id);
593 g_app_list.removeSurface(surface_id);
596 void WindowManager::removeClient(const std::string &appid)
598 HMI_DEBUG("wm", "Remove clinet %s from list", appid.c_str());
599 g_app_list.removeClient(appid);
602 void WindowManager::exceptionProcessForTransition()
604 unsigned req_num = g_app_list.currentRequestNumber();
605 HMI_SEQ_NOTICE(req_num, "Process exception handling for request. Remove current request %d", req_num);
606 g_app_list.removeRequest(req_num);
607 HMI_SEQ_NOTICE(g_app_list.currentRequestNumber(), "Process next request if exists");
608 this->processNextRequest();
611 void WindowManager::timerHandler()
613 unsigned req_num = g_app_list.currentRequestNumber();
614 HMI_SEQ_DEBUG(req_num, "Timer expired remove Request");
615 g_app_list.reqDump();
616 g_app_list.removeRequest(req_num);
617 this->processNextRequest();
621 ******* Private Functions *******
624 bool WindowManager::pop_pending_events()
627 return this->pending_events.compare_exchange_strong(
628 x, false, std::memory_order_consume);
631 optional<int> WindowManager::lookup_id(char const *name)
633 return this->id_alloc.lookup(std::string(name));
635 optional<std::string> WindowManager::lookup_name(int id)
637 return this->id_alloc.lookup(id);
643 int WindowManager::init_layers()
645 if (!this->controller)
647 HMI_ERROR("wm", "ivi_controller global not available");
651 if (this->outputs.empty())
653 HMI_ERROR("wm", "no output was set up!");
657 auto &c = this->controller;
659 auto &o = this->outputs.front();
660 auto &s = c->screens.begin()->second;
661 auto &layers = c->layers;
663 // Write output dimensions to ivi controller...
664 c->output_size = compositor::size{uint32_t(o->width), uint32_t(o->height)};
665 c->physical_size = compositor::size{uint32_t(o->physical_width),
666 uint32_t(o->physical_height)};
674 // Quick and dirty setup of layers
675 for (auto const &i : this->layers.mapping)
677 c->layer_create(i.second.layer_id, o->width, o->height);
678 auto &l = layers[i.second.layer_id];
679 l->set_destination_rectangle(0, 0, o->width, o->height);
680 l->set_visibility(1);
681 HMI_DEBUG("wm", "Setting up layer %s (%d) for surface role match \"%s\"",
682 i.second.name.c_str(), i.second.layer_id, i.second.role.c_str());
685 // Add layers to screen
686 s->set_render_order(this->layers.layers);
688 this->layout_commit();
690 this->layers.loadAreaDb();
691 this->layers.setupArea(o->width, o->height);
696 void WindowManager::surface_set_layout(int surface_id, const std::string& area)
698 if (!this->controller->surface_exists(surface_id))
700 HMI_ERROR("wm", "Surface %d does not exist", surface_id);
704 auto o_layer_id = this->layers.get_layer_id(surface_id);
708 HMI_ERROR("wm", "Surface %d is not associated with any layer!", surface_id);
712 uint32_t layer_id = *o_layer_id;
714 auto const &layer = this->layers.get_layer(layer_id);
715 auto rect = this->layers.getAreaSize(area);
716 HMI_SEQ_DEBUG(g_app_list.currentRequestNumber(), "%s : x:%d y:%d w:%d h:%d", area.c_str(),
717 rect.x, rect.y, rect.w, rect.h);
718 auto &s = this->controller->surfaces[surface_id];
725 HMI_DEBUG("wm", "surface_set_layout for surface %u on layer %u", surface_id,
728 // set destination to the display rectangle
729 s->set_destination_rectangle(x, y, w, h);
731 // update area information
732 this->area_info[surface_id].x = x;
733 this->area_info[surface_id].y = y;
734 this->area_info[surface_id].w = w;
735 this->area_info[surface_id].h = h;
737 HMI_DEBUG("wm", "Surface %u now on layer %u with rect { %d, %d, %d, %d }",
738 surface_id, layer_id, x, y, w, h);
741 void WindowManager::layout_commit()
743 this->controller->commit_changes();
744 this->display->flush();
747 void WindowManager::emit_activated(char const *label)
749 this->send_event(kListEventName[Event_Active], label);
752 void WindowManager::emit_deactivated(char const *label)
754 this->send_event(kListEventName[Event_Inactive], label);
757 void WindowManager::emit_syncdraw(char const *label, char const *area, int x, int y, int w, int h)
759 this->send_event(kListEventName[Event_SyncDraw], label, area, x, y, w, h);
762 void WindowManager::emit_syncdraw(const std::string &role, const std::string &area)
764 compositor::rect rect = this->layers.getAreaSize(area);
765 this->send_event(kListEventName[Event_SyncDraw],
766 role.c_str(), area.c_str(), rect.x, rect.y, rect.w, rect.h);
769 void WindowManager::emit_flushdraw(char const *label)
771 this->send_event(kListEventName[Event_FlushDraw], label);
774 void WindowManager::emit_visible(char const *label, bool is_visible)
776 this->send_event(is_visible ? kListEventName[Event_Visible] : kListEventName[Event_Invisible], label);
779 void WindowManager::emit_invisible(char const *label)
781 return emit_visible(label, false);
784 void WindowManager::emit_visible(char const *label) { return emit_visible(label, true); }
786 void WindowManager::activate(int id)
788 auto ip = this->controller->sprops.find(id);
789 if (ip != this->controller->sprops.end())
791 this->controller->surfaces[id]->set_visibility(1);
793 this->lookup_name(id).value_or("unknown-name").c_str();
796 if ((0 == strcmp(label, "radio")) ||
797 (0 == strcmp(label, "music")) ||
798 (0 == strcmp(label, "video")) ||
799 (0 == strcmp(label, "map")))
801 for (auto i = surface_bg.begin(); i != surface_bg.end(); ++i)
806 this->surface_bg.erase(i);
808 // Remove from BG layer (999)
809 HMI_DEBUG("wm", "Remove %s(%d) from BG layer", label, id);
810 this->controller->layers[999]->remove_surface(id);
812 // Add to FG layer (1001)
813 HMI_DEBUG("wm", "Add %s(%d) to FG layer", label, id);
814 this->controller->layers[1001]->add_surface(id);
816 for (int j : this->surface_bg)
818 HMI_DEBUG("wm", "Stored id:%d", j);
826 this->layout_commit();
828 // TODO: application requests by old role,
829 // so convert role new to old for emitting event
830 const char* old_role = this->rolenew2old[label].c_str();
832 this->emit_visible(old_role);
833 this->emit_activated(old_role);
837 void WindowManager::deactivate(int id)
839 auto ip = this->controller->sprops.find(id);
840 if (ip != this->controller->sprops.end())
843 this->lookup_name(id).value_or("unknown-name").c_str();
846 if ((0 == strcmp(label, "radio")) ||
847 (0 == strcmp(label, "music")) ||
848 (0 == strcmp(label, "video")) ||
849 (0 == strcmp(label, "map")))
853 this->surface_bg.push_back(id);
855 // Remove from FG layer (1001)
856 HMI_DEBUG("wm", "Remove %s(%d) from FG layer", label, id);
857 this->controller->layers[1001]->remove_surface(id);
859 // Add to BG layer (999)
860 HMI_DEBUG("wm", "Add %s(%d) to BG layer", label, id);
861 this->controller->layers[999]->add_surface(id);
863 for (int j : surface_bg)
865 HMI_DEBUG("wm", "Stored id:%d", j);
870 this->controller->surfaces[id]->set_visibility(0);
874 this->layout_commit();
876 // TODO: application requests by old role,
877 // so convert role new to old for emitting event
878 const char* old_role = this->rolenew2old[label].c_str();
880 this->emit_deactivated(old_role);
881 this->emit_invisible(old_role);
885 WMError WindowManager::setRequest(const std::string& appid, const std::string &role, const std::string &area,
886 Task task, unsigned* req_num)
888 if (!g_app_list.contains(appid))
890 return WMError::NOT_REGISTERED;
893 auto client = g_app_list.lookUpClient(appid);
898 unsigned current = g_app_list.currentRequestNumber();
899 unsigned requested_num = g_app_list.getRequestNumber(appid);
900 if (requested_num != 0)
902 HMI_SEQ_INFO(requested_num,
903 "%s %s %s request is already queued", appid.c_str(), role.c_str(), area.c_str());
907 WMRequest req = WMRequest(appid, role, area, task);
908 unsigned new_req = g_app_list.addRequest(req);
910 g_app_list.reqDump();
912 HMI_SEQ_DEBUG(current, "%s start sequence with %s, %s", appid.c_str(), role.c_str(), area.c_str());
914 return WMError::SUCCESS;
917 WMError WindowManager::doTransition(unsigned req_num)
919 HMI_SEQ_DEBUG(req_num, "check policy");
920 WMError ret = this->checkPolicy(req_num);
921 if (ret != WMError::SUCCESS)
925 HMI_SEQ_DEBUG(req_num, "Start transition.");
926 ret = this->startTransition(req_num);
930 WMError WindowManager::checkPolicy(unsigned req_num)
935 // get current trigger
938 WMError ret = WMError::LAYOUT_CHANGE_FAIL;
939 auto trigger = g_app_list.getRequest(req_num, &found);
942 ret = WMError::NO_ENTRY;
945 std::string req_area = trigger.area;
947 // >>>> Compatible with current window manager until policy manager coming
948 if (trigger.task == Task::TASK_ALLOCATE)
950 HMI_SEQ_DEBUG(req_num, "Check split or not");
951 const char *msg = this->check_surface_exist(trigger.role.c_str());
955 HMI_SEQ_ERROR(req_num, msg);
956 ret = WMError::LAYOUT_CHANGE_FAIL;
960 auto const &surface_id = this->lookup_id(trigger.role.c_str());
961 auto o_state = *this->layers.get_layout_state(*surface_id);
962 struct LayoutState &state = *o_state;
964 unsigned curernt_sid = state.main;
965 split = this->can_split(state, *surface_id);
969 HMI_SEQ_DEBUG(req_num, "Split happens");
970 // Get current visible role
971 std::string add_role = this->lookup_name(state.main).value();
973 std::string add_area = std::string(kNameLayoutSplit) + "." + std::string(kNameAreaMain);
974 // Change request area
975 req_area = std::string(kNameLayoutSplit) + "." + std::string(kNameAreaSub);
976 HMI_SEQ_NOTICE(req_num, "Change request area from %s to %s, because split happens",
977 trigger.area.c_str(), req_area.c_str());
978 // set another action
979 std::string add_name = g_app_list.getAppID(curernt_sid, add_role, &found);
982 HMI_SEQ_ERROR(req_num, "Couldn't widhdraw with surfaceID : %d", curernt_sid);
983 ret = WMError::NOT_REGISTERED;
986 HMI_SEQ_INFO(req_num, "Additional split app %s, role: %s, area: %s",
987 add_name.c_str(), add_role.c_str(), add_area.c_str());
989 bool end_draw_finished = false;
990 WMAction split_action{
994 TaskVisible::VISIBLE,
996 WMError ret = g_app_list.setAction(req_num, split_action);
997 if (ret != WMError::SUCCESS)
999 HMI_SEQ_ERROR(req_num, "Failed to set action");
1002 g_app_list.reqDump();
1007 HMI_SEQ_DEBUG(req_num, "split doesn't happen");
1010 // Set invisible task(Remove if policy manager finish)
1011 ret = this->setInvisibleTask(trigger.role, split);
1012 if(ret != WMError::SUCCESS)
1014 HMI_SEQ_ERROR(req_num, "Failed to set invisible task: %s", errorDescription(ret));
1018 /* get new status from Policy Manager */
1019 HMI_SEQ_NOTICE(req_num, "ATM, Policy manager does't exist, then set WMAction as is");
1020 if(trigger.role == "homescreen")
1022 // TODO : Remove when Policy Manager completed
1023 HMI_SEQ_NOTICE(req_num, "Hack. This process will be removed. Change HomeScreen code!!");
1024 req_area = "fullscreen";
1026 TaskVisible task_visible =
1027 (trigger.task == Task::TASK_ALLOCATE) ? TaskVisible::VISIBLE : TaskVisible::INVISIBLE;
1029 ret = g_app_list.setAction(req_num, trigger.appid, trigger.role, req_area, task_visible);
1030 g_app_list.reqDump();
1035 WMError WindowManager::startTransition(unsigned req_num)
1037 bool sync_draw_happen = false;
1039 WMError ret = WMError::SUCCESS;
1040 auto actions = g_app_list.getActions(req_num, &found);
1043 ret = WMError::NO_ENTRY;
1044 HMI_SEQ_ERROR(req_num,
1045 "Window Manager bug :%s : Action is not set", errorDescription(ret));
1049 for (const auto &action : actions)
1051 if (action.visible != TaskVisible::INVISIBLE)
1053 sync_draw_happen = true;
1055 // TODO: application requests by old role,
1056 // so convert role new to old for emitting event
1057 std::string old_role = this->rolenew2old[action.role];
1059 this->emit_syncdraw(old_role, action.area);
1060 /* TODO: emit event for app not subscriber
1061 if(g_app_list.contains(y.appid))
1062 g_app_list.lookUpClient(y.appid)->emit_syncdraw(y.role, y.area); */
1066 if (sync_draw_happen)
1072 // deactivate only, no syncDraw
1073 // Make it deactivate here
1074 for (const auto &x : actions)
1076 if (g_app_list.contains(x.appid))
1078 auto client = g_app_list.lookUpClient(x.appid);
1079 this->deactivate(client->surfaceID(x.role));
1082 ret = NO_LAYOUT_CHANGE;
1087 WMError WindowManager::setInvisibleTask(const std::string &role, bool split)
1089 unsigned req_num = g_app_list.currentRequestNumber();
1090 HMI_SEQ_DEBUG(req_num, "set current visible app to invisible task");
1091 // This task is copied from original actiavete surface
1092 const char *drawing_name = this->rolenew2old[role].c_str();
1093 auto const &surface_id = this->lookup_id(role.c_str());
1094 auto layer_id = this->layers.get_layer_id(*surface_id);
1095 auto o_state = *this->layers.get_layout_state(*surface_id);
1096 struct LayoutState &state = *o_state;
1097 std::string add_name, add_role;
1098 std::string add_area = "";
1100 TaskVisible task_visible = TaskVisible::INVISIBLE;
1101 bool end_draw_finished = true;
1104 for (auto const &l : this->layers.mapping)
1106 if (l.second.layer_id <= *layer_id)
1110 HMI_DEBUG("wm", "debug: main %d , sub : %d", l.second.state.main, l.second.state.sub);
1111 if (l.second.state.main != -1)
1113 //this->deactivate(l.second.state.main);
1114 surface = l.second.state.main;
1115 add_role = *this->id_alloc.lookup(surface);
1116 add_name = g_app_list.getAppID(surface, add_role, &found);
1118 return WMError::NOT_REGISTERED;
1120 HMI_SEQ_INFO(req_num, "Invisible %s", add_name.c_str());
1121 WMAction act{add_name, add_role, add_area, task_visible, end_draw_finished};
1122 g_app_list.setAction(req_num, act);
1123 l.second.state.main = -1;
1126 if (l.second.state.sub != -1)
1128 //this->deactivate(l.second.state.sub);
1129 surface = l.second.state.sub;
1130 add_role = *this->id_alloc.lookup(surface);
1131 add_name = g_app_list.getAppID(surface, add_role, &found);
1134 return WMError::NOT_REGISTERED;
1136 HMI_SEQ_INFO(req_num, "Invisible %s", add_name.c_str());
1137 WMAction act{add_name, add_role, add_area, task_visible, end_draw_finished};
1138 g_app_list.setAction(req_num, act);
1139 l.second.state.sub = -1;
1143 // change current state here, but this is hack
1144 auto layer = this->layers.get_layer(*layer_id);
1146 if (state.main == -1)
1148 HMI_DEBUG("wm", "Layout: %s", kNameLayoutNormal);
1152 if (0 != strcmp(drawing_name, "HomeScreen"))
1156 if (state.sub != *surface_id)
1158 if (state.sub != -1)
1160 //this->deactivate(state.sub);
1163 std::move(*this->id_alloc.lookup(state.sub));
1164 deact_sub.area = add_area;
1165 deact_sub.appid = g_app_list.getAppID(state.sub, deact_sub.role, &found);
1168 HMI_SEQ_ERROR(req_num, "App doesn't exist for role : %s",
1169 deact_sub.role.c_str());
1170 return WMError::NOT_REGISTERED;
1172 deact_sub.visible = task_visible;
1173 deact_sub.end_draw_finished = end_draw_finished;
1174 HMI_SEQ_DEBUG(req_num, "Set invisible task for %s", deact_sub.appid.c_str());
1175 g_app_list.setAction(req_num, deact_sub);
1178 //state = LayoutState{state.main, *surface_id};
1182 HMI_DEBUG("wm", "Layout: %s", kNameLayoutNormal);
1184 //this->surface_set_layout(*surface_id);
1185 if (state.main != *surface_id)
1187 // this->deactivate(state.main);
1188 WMAction deact_main;
1189 deact_main.role = std::move(*this->id_alloc.lookup(state.main));
1191 deact_main.area = add_area;
1192 deact_main.appid = g_app_list.getAppID(state.main, deact_main.role, &found);
1195 HMI_SEQ_DEBUG(req_num, "sub surface ddoesn't exist");
1196 return WMError::NOT_REGISTERED;
1198 deact_main.visible = task_visible;
1199 deact_main.end_draw_finished = end_draw_finished;
1200 HMI_SEQ_DEBUG(req_num, "sub surface doesn't exist");
1201 g_app_list.setAction(req_num, deact_main);
1203 if (state.sub != -1)
1205 if (state.sub != *surface_id)
1207 //this->deactivate(state.sub);
1209 deact_sub.role = std::move(*this->id_alloc.lookup(state.sub));
1211 deact_sub.area = add_area;
1212 deact_sub.appid = g_app_list.getAppID(state.sub, deact_sub.role, &found);
1215 HMI_SEQ_DEBUG(req_num, "sub surface ddoesn't exist");
1216 return WMError::NOT_REGISTERED;
1218 deact_sub.visible = task_visible;
1219 deact_sub.end_draw_finished = end_draw_finished;
1220 HMI_SEQ_DEBUG(req_num, "sub surface doesn't exist");
1221 g_app_list.setAction(req_num, deact_sub);
1224 //state = LayoutState{*surface_id};
1228 return WMError::SUCCESS;
1231 WMError WindowManager::doEndDraw(unsigned req_num)
1235 auto actions = g_app_list.getActions(req_num, &found);
1236 WMError ret = WMError::SUCCESS;
1239 ret = WMError::NO_ENTRY;
1243 HMI_SEQ_INFO(req_num, "do endDraw");
1245 // layout change and make it visible
1246 for (const auto &act : actions)
1249 if(!g_app_list.contains(act.appid)){
1250 ret = WMError::NOT_REGISTERED;
1252 ret = this->layoutChange(act);
1253 if(ret != WMError::SUCCESS)
1255 HMI_SEQ_WARNING(req_num,
1256 "Failed to manipulate surfaces while state change : %s", errorDescription(ret));
1259 ret = this->visibilityChange(act);
1260 if (ret != WMError::SUCCESS)
1262 HMI_SEQ_WARNING(req_num,
1263 "Failed to manipulate surfaces while state change : %s", errorDescription(ret));
1266 HMI_SEQ_DEBUG(req_num, "visible %s", act.role.c_str());
1267 //this->lm_enddraw(act.role.c_str());
1270 // Change current state
1271 this->changeCurrentState(req_num);
1273 HMI_SEQ_INFO(req_num, "emit flushDraw");
1275 for(const auto &act_flush : actions)
1277 if(act_flush.visible != TaskVisible::INVISIBLE)
1279 // TODO: application requests by old role,
1280 // so convert role new to old for emitting event
1281 std::string old_role = this->rolenew2old[act_flush.role];
1283 this->emit_flushdraw(old_role.c_str());
1290 WMError WindowManager::layoutChange(const WMAction &action)
1292 if (action.visible == TaskVisible::INVISIBLE)
1294 // Visibility is not change -> no redraw is required
1295 return WMError::SUCCESS;
1297 auto client = g_app_list.lookUpClient(action.appid);
1298 unsigned surface = client->surfaceID(action.role);
1301 HMI_SEQ_ERROR(g_app_list.currentRequestNumber(),
1302 "client doesn't have surface with role(%s)", action.role.c_str());
1303 return WMError::NOT_REGISTERED;
1306 WMError ret = this->setSurfaceSize(surface, action.area);
1310 WMError WindowManager::visibilityChange(const WMAction &action)
1312 HMI_SEQ_DEBUG(g_app_list.currentRequestNumber(), "Change visibility");
1313 if(!g_app_list.contains(action.appid)){
1314 return WMError::NOT_REGISTERED;
1316 auto client = g_app_list.lookUpClient(action.appid);
1317 unsigned surface = client->surfaceID(action.role);
1320 HMI_SEQ_ERROR(g_app_list.currentRequestNumber(),
1321 "client doesn't have surface with role(%s)", action.role.c_str());
1322 return WMError::NOT_REGISTERED;
1325 if (action.visible != TaskVisible::INVISIBLE)
1327 this->activate(surface); // Layout Manager task
1331 this->deactivate(surface); // Layout Manager task
1333 return WMError::SUCCESS;
1336 WMError WindowManager::setSurfaceSize(unsigned surface, const std::string &area)
1338 this->surface_set_layout(surface, area);
1340 return WMError::SUCCESS;
1343 WMError WindowManager::changeCurrentState(unsigned req_num)
1345 HMI_SEQ_DEBUG(req_num, "Change current layout state");
1346 bool trigger_found = false, action_found = false;
1347 auto trigger = g_app_list.getRequest(req_num, &trigger_found);
1348 auto actions = g_app_list.getActions(req_num, &action_found);
1349 if (!trigger_found || !action_found)
1351 HMI_SEQ_ERROR(req_num, "Action not found");
1352 return WMError::LAYOUT_CHANGE_FAIL;
1355 // Layout state reset
1356 struct LayoutState reset_state{-1, -1};
1357 HMI_SEQ_DEBUG(req_num,"Reset layout state");
1358 for (const auto &action : actions)
1360 if(!g_app_list.contains(action.appid)){
1361 return WMError::NOT_REGISTERED;
1363 auto client = g_app_list.lookUpClient(action.appid);
1364 auto pCurState = *this->layers.get_layout_state((int)client->surfaceID(action.role));
1365 if(pCurState == nullptr)
1367 HMI_SEQ_ERROR(req_num, "Counldn't find current status");
1370 pCurState->main = reset_state.main;
1371 pCurState->sub = reset_state.sub;
1374 HMI_SEQ_DEBUG(req_num, "Change state");
1375 for (const auto &action : actions)
1377 auto client = g_app_list.lookUpClient(action.appid);
1378 auto pLayerCurState = *this->layers.get_layout_state((int)client->surfaceID(action.role));
1379 if (pLayerCurState == nullptr)
1381 HMI_SEQ_ERROR(req_num, "Counldn't find current status");
1386 if (action.visible != TaskVisible::INVISIBLE)
1388 surface = (int)client->surfaceID(action.role);
1389 HMI_SEQ_INFO(req_num, "Change %s surface : %d, state visible area : %s",
1390 action.role.c_str(), surface, action.area.c_str());
1391 // visible == true -> layout changes
1392 if(action.area == "normal.full" || action.area == "split.main")
1394 pLayerCurState->main = surface;
1396 else if (action.area == "split.sub")
1398 pLayerCurState->sub = surface;
1403 pLayerCurState->main = surface;
1408 return WMError::SUCCESS;
1411 void WindowManager::emitScreenUpdated(unsigned req_num)
1414 HMI_SEQ_DEBUG(req_num, "emit screen updated");
1416 auto actions = g_app_list.getActions(req_num, &found);
1418 // create json object
1419 json_object *j = json_object_new_object();
1420 json_object *jarray = json_object_new_array();
1422 for(const auto& action: actions)
1424 if(action.visible != TaskVisible::INVISIBLE)
1426 json_object_array_add(jarray, json_object_new_string(action.appid.c_str()));
1429 json_object_object_add(j, kKeyIds, jarray);
1430 HMI_SEQ_INFO(req_num, "Visible app: %s", json_object_get_string(j));
1432 int ret = afb_event_push(
1433 this->map_afb_event[kListEventName[Event_ScreenUpdated]], j);
1436 HMI_DEBUG("wm", "afb_event_push failed: %m");
1438 json_object_put(jarray);
1441 void WindowManager::setTimer()
1443 HMI_SEQ_DEBUG(g_app_list.currentRequestNumber(), "Timer set activate");
1444 if (g_timer_ev_src == nullptr)
1446 // firsttime set into sd_event
1447 int ret = sd_event_add_time(afb_daemon_get_event_loop(), &g_timer_ev_src,
1448 CLOCK_REALTIME, time(NULL) * (1000000UL) + kTimeOut, 1, processTimerHandler, this);
1451 HMI_ERROR("wm", "Could't set timer");
1456 // update timer limitation after second time
1457 sd_event_source_set_time(g_timer_ev_src, time(NULL) * (1000000UL) + kTimeOut);
1458 sd_event_source_set_enabled(g_timer_ev_src, SD_EVENT_ONESHOT);
1462 void WindowManager::stopTimer()
1464 unsigned req_num = g_app_list.currentRequestNumber();
1465 HMI_SEQ_DEBUG(req_num, "Timer stop");
1466 int rc = sd_event_source_set_enabled(g_timer_ev_src, SD_EVENT_OFF);
1469 HMI_SEQ_ERROR(req_num, "Timer stop failed");
1473 void WindowManager::processNextRequest()
1476 g_app_list.reqDump();
1477 unsigned req_num = g_app_list.currentRequestNumber();
1478 if (g_app_list.haveRequest())
1480 HMI_SEQ_DEBUG(req_num, "Process next request");
1481 WMError rc = doTransition(req_num);
1482 if (rc != WMError::SUCCESS)
1484 HMI_SEQ_ERROR(req_num, errorDescription(rc));
1489 HMI_SEQ_DEBUG(req_num, "Nothing Request. Waiting Request");
1493 const char* WindowManager::convertRoleOldToNew(char const *old_role)
1495 const char *new_role = nullptr;
1497 for (auto const &on : this->roleold2new)
1499 std::regex regex = std::regex(on.first);
1500 if (std::regex_match(old_role, regex))
1502 // role is old. So convert to new.
1503 new_role = on.second.c_str();
1508 if (nullptr == new_role)
1510 // role is new or fallback.
1511 new_role = old_role;
1514 HMI_DEBUG("wm", "old:%s -> new:%s", old_role, new_role);
1519 int WindowManager::loadOldRoleDb()
1521 // Get afm application installed dir
1522 char const *afm_app_install_dir = getenv("AFM_APP_INSTALL_DIR");
1523 HMI_DEBUG("wm", "afm_app_install_dir:%s", afm_app_install_dir);
1525 std::string file_name;
1526 if (!afm_app_install_dir)
1528 HMI_ERROR("wm", "AFM_APP_INSTALL_DIR is not defined");
1532 file_name = std::string(afm_app_install_dir) + std::string("/etc/old_roles.db");
1536 json_object* json_obj;
1537 int ret = jh::inputJsonFilie(file_name.c_str(), &json_obj);
1540 HMI_ERROR("wm", "Could not open old_role.db, so use default old_role information");
1541 json_obj = json_tokener_parse(kDefaultOldRoleDb);
1543 HMI_DEBUG("wm", "json_obj dump:%s", json_object_get_string(json_obj));
1546 json_object* json_cfg;
1547 if (!json_object_object_get_ex(json_obj, "old_roles", &json_cfg))
1549 HMI_ERROR("wm", "Parse Error!!");
1553 int len = json_object_array_length(json_cfg);
1554 HMI_DEBUG("wm", "json_cfg len:%d", len);
1555 HMI_DEBUG("wm", "json_cfg dump:%s", json_object_get_string(json_cfg));
1557 for (int i=0; i<len; i++)
1559 json_object* json_tmp = json_object_array_get_idx(json_cfg, i);
1561 const char* old_role = jh::getStringFromJson(json_tmp, "name");
1562 if (nullptr == old_role)
1564 HMI_ERROR("wm", "Parse Error!!");
1568 const char* new_role = jh::getStringFromJson(json_tmp, "new");
1569 if (nullptr == new_role)
1571 HMI_ERROR("wm", "Parse Error!!");
1575 this->roleold2new[old_role] = std::string(new_role);
1579 for(auto itr = this->roleold2new.begin();
1580 itr != this->roleold2new.end(); ++itr)
1582 HMI_DEBUG("wm", ">>> role old:%s new:%s",
1583 itr->first.c_str(), itr->second.c_str());
1586 // Release json_object
1587 json_object_put(json_obj);
1592 const char *WindowManager::check_surface_exist(const char *drawing_name)
1594 auto const &surface_id = this->lookup_id(drawing_name);
1597 return "Surface does not exist";
1600 if (!this->controller->surface_exists(*surface_id))
1602 return "Surface does not exist in controller!";
1605 auto layer_id = this->layers.get_layer_id(*surface_id);
1609 return "Surface is not on any layer!";
1612 auto o_state = *this->layers.get_layout_state(*surface_id);
1614 if (o_state == nullptr)
1616 return "Could not find layer for surface";
1619 HMI_DEBUG("wm", "surface %d is detected", *surface_id);
1623 bool WindowManager::can_split(struct LayoutState const &state, int new_id)
1625 if (state.main != -1 && state.main != new_id)
1627 auto new_id_layer = this->layers.get_layer_id(new_id).value();
1628 auto current_id_layer = this->layers.get_layer_id(state.main).value();
1630 // surfaces are on separate layers, don't bother.
1631 if (new_id_layer != current_id_layer)
1636 std::string const &new_id_str = this->lookup_name(new_id).value();
1637 std::string const &cur_id_str = this->lookup_name(state.main).value();
1639 auto const &layer = this->layers.get_layer(new_id_layer);
1641 HMI_DEBUG("wm", "layer info name: %s", layer->name.c_str());
1643 if (layer->layouts.empty())
1648 for (auto i = layer->layouts.cbegin(); i != layer->layouts.cend(); i++)
1650 HMI_DEBUG("wm", "%d main_match '%s'", new_id_layer, i->main_match.c_str());
1651 auto rem = std::regex(i->main_match);
1652 if (std::regex_match(cur_id_str, rem))
1654 // build the second one only if the first already matched
1655 HMI_DEBUG("wm", "%d sub_match '%s'", new_id_layer, i->sub_match.c_str());
1656 auto res = std::regex(i->sub_match);
1657 if (std::regex_match(new_id_str, res))
1659 HMI_DEBUG("wm", "layout matched!");
1669 const char* WindowManager::kDefaultOldRoleDb = "{ \
1672 \"name\": \"HomeScreen\", \
1673 \"new\": \"homescreen\" \
1676 \"name\": \"Music\", \
1677 \"new\": \"music\" \
1680 \"name\": \"MediaPlayer\", \
1681 \"new\": \"music\" \
1684 \"name\": \"Video\", \
1685 \"new\": \"video\" \
1688 \"name\": \"VideoPlayer\", \
1689 \"new\": \"video\" \
1692 \"name\": \"WebBrowser\", \
1693 \"new\": \"browser\" \
1696 \"name\": \"Radio\", \
1697 \"new\": \"radio\" \
1700 \"name\": \"Phone\", \
1701 \"new\": \"phone\" \
1704 \"name\": \"Navigation\", \
1708 \"name\": \"HVAC\", \
1712 \"name\": \"Settings\", \
1713 \"new\": \"settings\" \
1716 \"name\": \"Dashboard\", \
1717 \"new\": \"dashboard\" \
1720 \"name\": \"POI\", \
1724 \"name\": \"Mixer\", \
1725 \"new\": \"mixer\" \
1728 \"name\": \"Restriction\", \
1729 \"new\": \"restriction\" \
1732 \"name\": \"^OnScreen.*\", \
1733 \"new\": \"on_screen\" \
1741 void controller_hooks::surface_created(uint32_t surface_id)
1743 this->app->surface_created(surface_id);
1746 void controller_hooks::surface_removed(uint32_t surface_id)
1748 this->app->surface_removed(surface_id);
1751 void controller_hooks::surface_visibility(uint32_t /*surface_id*/,
1754 void controller_hooks::surface_destination_rectangle(uint32_t /*surface_id*/,