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;
64 result<json> file_to_json(char const *filename)
67 std::ifstream i(filename);
70 HMI_DEBUG("wm", "Could not open config file, so use default layer information");
71 j = default_layers_json;
81 struct result<layer_map> load_layer_map(char const *filename)
83 HMI_DEBUG("wm", "loading IDs from %s", filename);
85 auto j = file_to_json(filename);
88 return Err<layer_map>(j.unwrap_err());
90 json jids = j.unwrap();
92 return to_layer_map(jids);
95 static int processTimerHandler(sd_event_source *s, uint64_t usec, void *userdata)
97 HMI_NOTICE("wm", "Time out occurs because the client replys endDraw slow, so revert the request");
98 reinterpret_cast<wm::WindowManager *>(userdata)->timerHandler();
107 WindowManager::WindowManager(wl::display *d)
114 pending_events(false)
116 char const *path_layers_json = getenv("AFM_APP_INSTALL_DIR");
118 if (!path_layers_json)
120 HMI_ERROR("wm", "AFM_APP_INSTALL_DIR is not defined");
121 path = std::string(path_layers_json);
125 path = std::string(path_layers_json) + std::string("/etc/layers.json");
131 auto l = load_layer_map(path.c_str());
134 this->layers = l.unwrap();
138 HMI_ERROR("wm", "%s", l.err().value());
142 catch (std::exception &e)
144 HMI_ERROR("wm", "Loading of configuration failed: %s", e.what());
148 int WindowManager::init()
151 if (!this->display->ok())
156 if (this->layers.mapping.empty())
158 HMI_ERROR("wm", "No surface -> layer mapping loaded");
162 // TODO: application requests by old role,
163 // so create role map (old, new)
165 this->loadOldRoleDb();
168 for (int i = Event_Val_Min; i <= Event_Val_Max; i++)
170 map_afb_event[kListEventName[i]] = afb_daemon_make_event(kListEventName[i]);
173 this->display->add_global_handler(
174 "wl_output", [this](wl_registry *r, uint32_t name, uint32_t v) {
175 this->outputs.emplace_back(std::make_unique<wl::output>(r, name, v));
178 this->display->add_global_handler(
179 "ivi_wm", [this](wl_registry *r, uint32_t name, uint32_t v) {
181 std::make_unique<struct compositor::controller>(r, name, v);
183 // Init controller hooks
184 this->controller->chooks = &this->chooks;
186 // This protocol needs the output, so lets just add our mapping here...
187 this->controller->add_proxy_to_id_mapping(
188 this->outputs.back()->proxy.get(),
189 wl_proxy_get_id(reinterpret_cast<struct wl_proxy *>(
190 this->outputs.back()->proxy.get())));
193 this->controller->create_screen(this->outputs.back()->proxy.get());
195 // Set display to controller
196 this->controller->display = this->display;
199 // First level objects
200 this->display->roundtrip();
201 // Second level objects
202 this->display->roundtrip();
203 // Third level objects
204 this->display->roundtrip();
210 int WindowManager::dispatch_pending_events()
212 if (this->pop_pending_events())
214 this->display->dispatch_pending();
220 void WindowManager::set_pending_events()
222 this->pending_events.store(true, std::memory_order_release);
225 result<int> WindowManager::api_request_surface(char const *appid, char const *drawing_name)
227 // TODO: application requests by old role,
228 // so convert role old to new
229 const char *role = this->convertRoleOldToNew(drawing_name);
231 auto lid = this->layers.get_layer_id(std::string(role));
235 * register drawing_name as fallback and make it displayed.
237 lid = this->layers.get_layer_id(std::string("fallback"));
238 HMI_DEBUG("wm", "%s is not registered in layers.json, then fallback as normal app", role);
241 return Err<int>("Drawing name does not match any role, Fallback is disabled");
245 auto rname = this->lookup_id(role);
248 // name does not exist yet, allocate surface id...
249 auto id = int(this->id_alloc.generate_id(role));
250 this->layers.add_surface(id, *lid);
252 // set the main_surface[_name] here and now
253 if (!this->layers.main_surface_name.empty() &&
254 this->layers.main_surface_name == drawing_name)
256 this->layers.main_surface = id;
257 HMI_DEBUG("wm", "Set main_surface id to %u", id);
260 // add client into the db
261 std::string appid_str(appid);
262 g_app_list.addClient(appid_str, *lid, id, std::string(role));
264 // Set role map of (new, old)
265 this->rolenew2old[role] = std::string(drawing_name);
270 // Check currently registered drawing names if it is already there.
271 return Err<int>("Surface already present");
274 char const *WindowManager::api_request_surface(char const *appid, char const *drawing_name,
279 // TODO: application requests by old role,
280 // so convert role old to new
281 const char *role = this->convertRoleOldToNew(drawing_name);
283 auto lid = this->layers.get_layer_id(std::string(role));
284 unsigned sid = std::stol(ivi_id);
289 * register drawing_name as fallback and make it displayed.
291 lid = this->layers.get_layer_id(std::string("fallback"));
292 HMI_DEBUG("wm", "%s is not registered in layers.json, then fallback as normal app", role);
295 return "Drawing name does not match any role, Fallback is disabled";
299 auto rname = this->lookup_id(role);
303 return "Surface already present";
306 // register pair drawing_name and ivi_id
307 this->id_alloc.register_name_id(role, sid);
308 this->layers.add_surface(sid, *lid);
310 // this surface is already created
311 HMI_DEBUG("wm", "surface_id is %u, layer_id is %u", sid, *lid);
313 this->controller->layers[*lid]->add_surface(sid);
314 this->layout_commit();
316 // add client into the db
317 std::string appid_str(appid);
318 g_app_list.addClient(appid_str, *lid, sid, std::string(role));
320 // Set role map of (new, old)
321 this->rolenew2old[role] = std::string(drawing_name);
326 void WindowManager::api_activate_surface(char const *appid, char const *drawing_name,
327 char const *drawing_area, const reply_func &reply)
331 // TODO: application requests by old role,
332 // so convert role old to new
333 const char *c_role = this->convertRoleOldToNew(drawing_name);
335 std::string id = appid;
336 std::string role = c_role;
337 std::string area = drawing_area;
338 Task task = Task::TASK_ALLOCATE;
339 unsigned req_num = 0;
340 WMError ret = WMError::UNKNOWN;
342 ret = this->setRequest(id, role, area, task, &req_num);
344 if(ret != WMError::SUCCESS)
346 HMI_ERROR("wm", errorDescription(ret));
347 reply("Failed to set request");
352 if (req_num != g_app_list.currentRequestNumber())
354 // Add request, then invoked after the previous task is finished
355 HMI_SEQ_DEBUG(req_num, "request is accepted");
362 ret = this->doTransition(req_num);
364 if (ret != WMError::SUCCESS)
367 HMI_SEQ_ERROR(req_num, errorDescription(ret));
368 g_app_list.removeRequest(req_num);
369 this->processNextRequest();
373 void WindowManager::api_deactivate_surface(char const *appid, char const *drawing_name,
374 const reply_func &reply)
378 // TODO: application requests by old role,
379 // so convert role old to new
380 const char *c_role = this->convertRoleOldToNew(drawing_name);
385 std::string id = appid;
386 std::string role = c_role;
387 std::string area = ""; //drawing_area;
388 Task task = Task::TASK_RELEASE;
389 unsigned req_num = 0;
390 WMError ret = WMError::UNKNOWN;
392 ret = this->setRequest(id, role, area, task, &req_num);
394 if (ret != WMError::SUCCESS)
396 HMI_ERROR("wm", errorDescription(ret));
397 reply("Failed to set request");
402 if (req_num != g_app_list.currentRequestNumber())
404 // Add request, then invoked after the previous task is finished
405 HMI_SEQ_DEBUG(req_num, "request is accepted");
412 ret = this->doTransition(req_num);
414 if (ret != WMError::SUCCESS)
417 HMI_SEQ_ERROR(req_num, errorDescription(ret));
418 g_app_list.removeRequest(req_num);
419 this->processNextRequest();
423 void WindowManager::api_enddraw(char const *appid, char const *drawing_name)
425 // TODO: application requests by old role,
426 // so convert role old to new
427 const char *c_role = this->convertRoleOldToNew(drawing_name);
429 std::string id = appid;
430 std::string role = c_role;
431 unsigned current_req = g_app_list.currentRequestNumber();
432 bool result = g_app_list.setEndDrawFinished(current_req, id, role);
436 HMI_ERROR("wm", "%s is not in transition state", id.c_str());
440 if (g_app_list.endDrawFullfilled(current_req))
442 // do task for endDraw
444 WMError ret = this->doEndDraw(current_req);
446 if(ret != WMError::SUCCESS)
448 //this->emit_error();
450 this->emitScreenUpdated(current_req);
451 HMI_SEQ_INFO(current_req, "Finish request status: %s", errorDescription(ret));
453 g_app_list.removeRequest(current_req);
455 this->processNextRequest();
459 HMI_SEQ_INFO(current_req, "Wait other App call endDraw");
464 result<json_object *> WindowManager::api_get_display_info()
467 if (!this->controller)
469 return Err<json_object *>("ivi_controller global not available");
473 compositor::size o_size = this->controller->output_size;
474 compositor::size p_size = this->controller->physical_size;
476 json_object *object = json_object_new_object();
477 json_object_object_add(object, kKeyWidthPixel, json_object_new_int(o_size.w));
478 json_object_object_add(object, kKeyHeightPixel, json_object_new_int(o_size.h));
479 json_object_object_add(object, kKeyWidthMm, json_object_new_int(p_size.w));
480 json_object_object_add(object, kKeyHeightMm, json_object_new_int(p_size.h));
482 return Ok<json_object *>(object);
485 result<json_object *> WindowManager::api_get_area_info(char const *drawing_name)
487 HMI_DEBUG("wm", "called");
489 // TODO: application requests by old role,
490 // so convert role old to new
491 const char *role = this->convertRoleOldToNew(drawing_name);
493 // Check drawing name, surface/layer id
494 auto const &surface_id = this->lookup_id(role);
497 return Err<json_object *>("Surface does not exist");
500 if (!this->controller->surface_exists(*surface_id))
502 return Err<json_object *>("Surface does not exist in controller!");
505 auto layer_id = this->layers.get_layer_id(*surface_id);
508 return Err<json_object *>("Surface is not on any layer!");
511 auto o_state = *this->layers.get_layout_state(*surface_id);
512 if (o_state == nullptr)
514 return Err<json_object *>("Could not find layer for surface");
517 struct LayoutState &state = *o_state;
518 if ((state.main != *surface_id) && (state.sub != *surface_id))
520 return Err<json_object *>("Surface is inactive");
523 // Set area rectangle
524 compositor::rect area_info = this->area_info[*surface_id];
525 json_object *object = json_object_new_object();
526 json_object_object_add(object, kKeyX, json_object_new_int(area_info.x));
527 json_object_object_add(object, kKeyY, json_object_new_int(area_info.y));
528 json_object_object_add(object, kKeyWidth, json_object_new_int(area_info.w));
529 json_object_object_add(object, kKeyHeight, json_object_new_int(area_info.h));
531 return Ok<json_object *>(object);
534 void WindowManager::api_ping() { this->dispatch_pending_events(); }
536 void WindowManager::send_event(char const *evname, char const *label)
538 HMI_DEBUG("wm", "%s: %s(%s)", __func__, evname, label);
540 json_object *j = json_object_new_object();
541 json_object_object_add(j, kKeyDrawingName, json_object_new_string(label));
543 int ret = afb_event_push(this->map_afb_event[evname], j);
546 HMI_DEBUG("wm", "afb_event_push failed: %m");
550 void WindowManager::send_event(char const *evname, char const *label, char const *area,
551 int x, int y, int w, int h)
553 HMI_DEBUG("wm", "%s: %s(%s, %s) x:%d y:%d w:%d h:%d",
554 __func__, evname, label, area, x, y, w, h);
556 json_object *j_rect = json_object_new_object();
557 json_object_object_add(j_rect, kKeyX, json_object_new_int(x));
558 json_object_object_add(j_rect, kKeyY, json_object_new_int(y));
559 json_object_object_add(j_rect, kKeyWidth, json_object_new_int(w));
560 json_object_object_add(j_rect, kKeyHeight, json_object_new_int(h));
562 json_object *j = json_object_new_object();
563 json_object_object_add(j, kKeyDrawingName, json_object_new_string(label));
564 json_object_object_add(j, kKeyDrawingArea, json_object_new_string(area));
565 json_object_object_add(j, kKeyDrawingRect, j_rect);
567 int ret = afb_event_push(this->map_afb_event[evname], j);
570 HMI_DEBUG("wm", "afb_event_push failed: %m");
577 void WindowManager::surface_created(uint32_t surface_id)
579 auto layer_id = this->layers.get_layer_id(surface_id);
582 HMI_DEBUG("wm", "Newly created surfce %d is not associated with any layer!",
587 HMI_DEBUG("wm", "surface_id is %u, layer_id is %u", surface_id, *layer_id);
589 this->controller->layers[*layer_id]->add_surface(surface_id);
590 this->layout_commit();
593 void WindowManager::surface_removed(uint32_t surface_id)
595 HMI_DEBUG("wm", "surface_id is %u", surface_id);
596 g_app_list.removeSurface(surface_id);
599 void WindowManager::removeClient(const std::string &appid)
601 HMI_DEBUG("wm", "Remove clinet %s from list", appid.c_str());
602 g_app_list.removeClient(appid);
605 void WindowManager::exceptionProcessForTransition()
607 unsigned req_num = g_app_list.currentRequestNumber();
608 HMI_SEQ_NOTICE(req_num, "Process exception handling for request. Remove current request %d", req_num);
609 g_app_list.removeRequest(req_num);
610 HMI_SEQ_NOTICE(g_app_list.currentRequestNumber(), "Process next request if exists");
611 this->processNextRequest();
614 void WindowManager::timerHandler()
616 unsigned req_num = g_app_list.currentRequestNumber();
617 HMI_SEQ_DEBUG(req_num, "Timer expired remove Request");
618 g_app_list.reqDump();
619 g_app_list.removeRequest(req_num);
620 this->processNextRequest();
624 ******* Private Functions *******
627 bool WindowManager::pop_pending_events()
630 return this->pending_events.compare_exchange_strong(
631 x, false, std::memory_order_consume);
634 optional<int> WindowManager::lookup_id(char const *name)
636 return this->id_alloc.lookup(std::string(name));
638 optional<std::string> WindowManager::lookup_name(int id)
640 return this->id_alloc.lookup(id);
646 int WindowManager::init_layers()
648 if (!this->controller)
650 HMI_ERROR("wm", "ivi_controller global not available");
654 if (this->outputs.empty())
656 HMI_ERROR("wm", "no output was set up!");
661 wm_config.loadConfigs();
663 auto &c = this->controller;
665 auto &o = this->outputs.front();
666 auto &s = c->screens.begin()->second;
667 auto &layers = c->layers;
669 this->layers.loadAreaDb();
670 const compositor::rect base = this->layers.getAreaSize("fullscreen");
672 const std::string aspect_setting = wm_config.getConfigAspect();
673 const compositor::rect scale_rect =
674 this->layers.getScaleDestRect(o->width, o->height, aspect_setting);
676 // Write output dimensions to ivi controller...
677 c->output_size = compositor::size{uint32_t(o->width), uint32_t(o->height)};
678 c->physical_size = compositor::size{uint32_t(o->physical_width),
679 uint32_t(o->physical_height)};
687 // Quick and dirty setup of layers
688 for (auto const &i : this->layers.mapping)
690 c->layer_create(i.second.layer_id, scale_rect.w, scale_rect.h);
691 auto &l = layers[i.second.layer_id];
692 l->set_source_rectangle(0, 0, base.w, base.h);
693 l->set_destination_rectangle(
694 scale_rect.x, scale_rect.y, scale_rect.w, scale_rect.h);
695 l->set_visibility(1);
696 HMI_DEBUG("wm", "Setting up layer %s (%d) for surface role match \"%s\"",
697 i.second.name.c_str(), i.second.layer_id, i.second.role.c_str());
700 // Add layers to screen
701 s->set_render_order(this->layers.layers);
703 this->layout_commit();
708 void WindowManager::surface_set_layout(int surface_id, const std::string& area)
710 if (!this->controller->surface_exists(surface_id))
712 HMI_ERROR("wm", "Surface %d does not exist", surface_id);
716 auto o_layer_id = this->layers.get_layer_id(surface_id);
720 HMI_ERROR("wm", "Surface %d is not associated with any layer!", surface_id);
724 uint32_t layer_id = *o_layer_id;
726 auto const &layer = this->layers.get_layer(layer_id);
727 auto rect = this->layers.getAreaSize(area);
728 HMI_SEQ_DEBUG(g_app_list.currentRequestNumber(), "%s : x:%d y:%d w:%d h:%d", area.c_str(),
729 rect.x, rect.y, rect.w, rect.h);
730 auto &s = this->controller->surfaces[surface_id];
737 HMI_DEBUG("wm", "surface_set_layout for surface %u on layer %u", surface_id,
740 // set destination to the display rectangle
741 s->set_destination_rectangle(x, y, w, h);
743 // update area information
744 this->area_info[surface_id].x = x;
745 this->area_info[surface_id].y = y;
746 this->area_info[surface_id].w = w;
747 this->area_info[surface_id].h = h;
749 HMI_DEBUG("wm", "Surface %u now on layer %u with rect { %d, %d, %d, %d }",
750 surface_id, layer_id, x, y, w, h);
753 void WindowManager::layout_commit()
755 this->controller->commit_changes();
756 this->display->flush();
759 void WindowManager::emit_activated(char const *label)
761 this->send_event(kListEventName[Event_Active], label);
764 void WindowManager::emit_deactivated(char const *label)
766 this->send_event(kListEventName[Event_Inactive], label);
769 void WindowManager::emit_syncdraw(char const *label, char const *area, int x, int y, int w, int h)
771 this->send_event(kListEventName[Event_SyncDraw], label, area, x, y, w, h);
774 void WindowManager::emit_syncdraw(const std::string &role, const std::string &area)
776 compositor::rect rect = this->layers.getAreaSize(area);
777 this->send_event(kListEventName[Event_SyncDraw],
778 role.c_str(), area.c_str(), rect.x, rect.y, rect.w, rect.h);
781 void WindowManager::emit_flushdraw(char const *label)
783 this->send_event(kListEventName[Event_FlushDraw], label);
786 void WindowManager::emit_visible(char const *label, bool is_visible)
788 this->send_event(is_visible ? kListEventName[Event_Visible] : kListEventName[Event_Invisible], label);
791 void WindowManager::emit_invisible(char const *label)
793 return emit_visible(label, false);
796 void WindowManager::emit_visible(char const *label) { return emit_visible(label, true); }
798 void WindowManager::activate(int id)
800 auto ip = this->controller->sprops.find(id);
801 if (ip != this->controller->sprops.end())
803 this->controller->surfaces[id]->set_visibility(1);
805 this->lookup_name(id).value_or("unknown-name").c_str();
808 if ((0 == strcmp(label, "radio")) ||
809 (0 == strcmp(label, "music")) ||
810 (0 == strcmp(label, "video")) ||
811 (0 == strcmp(label, "map")))
813 for (auto i = surface_bg.begin(); i != surface_bg.end(); ++i)
818 this->surface_bg.erase(i);
820 // Remove from BG layer (999)
821 HMI_DEBUG("wm", "Remove %s(%d) from BG layer", label, id);
822 this->controller->layers[999]->remove_surface(id);
824 // Add to FG layer (1001)
825 HMI_DEBUG("wm", "Add %s(%d) to FG layer", label, id);
826 this->controller->layers[1001]->add_surface(id);
828 for (int j : this->surface_bg)
830 HMI_DEBUG("wm", "Stored id:%d", j);
838 this->layout_commit();
840 // TODO: application requests by old role,
841 // so convert role new to old for emitting event
842 const char* old_role = this->rolenew2old[label].c_str();
844 this->emit_visible(old_role);
845 this->emit_activated(old_role);
849 void WindowManager::deactivate(int id)
851 auto ip = this->controller->sprops.find(id);
852 if (ip != this->controller->sprops.end())
855 this->lookup_name(id).value_or("unknown-name").c_str();
858 if ((0 == strcmp(label, "radio")) ||
859 (0 == strcmp(label, "music")) ||
860 (0 == strcmp(label, "video")) ||
861 (0 == strcmp(label, "map")))
865 this->surface_bg.push_back(id);
867 // Remove from FG layer (1001)
868 HMI_DEBUG("wm", "Remove %s(%d) from FG layer", label, id);
869 this->controller->layers[1001]->remove_surface(id);
871 // Add to BG layer (999)
872 HMI_DEBUG("wm", "Add %s(%d) to BG layer", label, id);
873 this->controller->layers[999]->add_surface(id);
875 for (int j : surface_bg)
877 HMI_DEBUG("wm", "Stored id:%d", j);
882 this->controller->surfaces[id]->set_visibility(0);
886 this->layout_commit();
888 // TODO: application requests by old role,
889 // so convert role new to old for emitting event
890 const char* old_role = this->rolenew2old[label].c_str();
892 this->emit_deactivated(old_role);
893 this->emit_invisible(old_role);
897 WMError WindowManager::setRequest(const std::string& appid, const std::string &role, const std::string &area,
898 Task task, unsigned* req_num)
900 if (!g_app_list.contains(appid))
902 return WMError::NOT_REGISTERED;
905 auto client = g_app_list.lookUpClient(appid);
910 unsigned current = g_app_list.currentRequestNumber();
911 unsigned requested_num = g_app_list.getRequestNumber(appid);
912 if (requested_num != 0)
914 HMI_SEQ_INFO(requested_num,
915 "%s %s %s request is already queued", appid.c_str(), role.c_str(), area.c_str());
919 WMRequest req = WMRequest(appid, role, area, task);
920 unsigned new_req = g_app_list.addRequest(req);
922 g_app_list.reqDump();
924 HMI_SEQ_DEBUG(current, "%s start sequence with %s, %s", appid.c_str(), role.c_str(), area.c_str());
926 return WMError::SUCCESS;
929 WMError WindowManager::doTransition(unsigned req_num)
931 HMI_SEQ_DEBUG(req_num, "check policy");
932 WMError ret = this->checkPolicy(req_num);
933 if (ret != WMError::SUCCESS)
937 HMI_SEQ_DEBUG(req_num, "Start transition.");
938 ret = this->startTransition(req_num);
942 WMError WindowManager::checkPolicy(unsigned req_num)
947 // get current trigger
950 WMError ret = WMError::LAYOUT_CHANGE_FAIL;
951 auto trigger = g_app_list.getRequest(req_num, &found);
954 ret = WMError::NO_ENTRY;
957 std::string req_area = trigger.area;
959 // >>>> Compatible with current window manager until policy manager coming
960 if (trigger.task == Task::TASK_ALLOCATE)
962 HMI_SEQ_DEBUG(req_num, "Check split or not");
963 const char *msg = this->check_surface_exist(trigger.role.c_str());
967 HMI_SEQ_ERROR(req_num, msg);
968 ret = WMError::LAYOUT_CHANGE_FAIL;
972 auto const &surface_id = this->lookup_id(trigger.role.c_str());
973 auto o_state = *this->layers.get_layout_state(*surface_id);
974 struct LayoutState &state = *o_state;
976 unsigned curernt_sid = state.main;
977 split = this->can_split(state, *surface_id);
981 HMI_SEQ_DEBUG(req_num, "Split happens");
982 // Get current visible role
983 std::string add_role = this->lookup_name(state.main).value();
985 std::string add_area = std::string(kNameLayoutSplit) + "." + std::string(kNameAreaMain);
986 // Change request area
987 req_area = std::string(kNameLayoutSplit) + "." + std::string(kNameAreaSub);
988 HMI_SEQ_NOTICE(req_num, "Change request area from %s to %s, because split happens",
989 trigger.area.c_str(), req_area.c_str());
990 // set another action
991 std::string add_name = g_app_list.getAppID(curernt_sid, add_role, &found);
994 HMI_SEQ_ERROR(req_num, "Couldn't widhdraw with surfaceID : %d", curernt_sid);
995 ret = WMError::NOT_REGISTERED;
998 HMI_SEQ_INFO(req_num, "Additional split app %s, role: %s, area: %s",
999 add_name.c_str(), add_role.c_str(), add_area.c_str());
1001 bool end_draw_finished = false;
1002 WMAction split_action{
1006 TaskVisible::VISIBLE,
1008 WMError ret = g_app_list.setAction(req_num, split_action);
1009 if (ret != WMError::SUCCESS)
1011 HMI_SEQ_ERROR(req_num, "Failed to set action");
1014 g_app_list.reqDump();
1019 HMI_SEQ_DEBUG(req_num, "split doesn't happen");
1022 // Set invisible task(Remove if policy manager finish)
1023 ret = this->setInvisibleTask(trigger.role, split);
1024 if(ret != WMError::SUCCESS)
1026 HMI_SEQ_ERROR(req_num, "Failed to set invisible task: %s", errorDescription(ret));
1030 /* get new status from Policy Manager */
1031 HMI_SEQ_NOTICE(req_num, "ATM, Policy manager does't exist, then set WMAction as is");
1032 if(trigger.role == "homescreen")
1034 // TODO : Remove when Policy Manager completed
1035 HMI_SEQ_NOTICE(req_num, "Hack. This process will be removed. Change HomeScreen code!!");
1036 req_area = "fullscreen";
1038 TaskVisible task_visible =
1039 (trigger.task == Task::TASK_ALLOCATE) ? TaskVisible::VISIBLE : TaskVisible::INVISIBLE;
1041 ret = g_app_list.setAction(req_num, trigger.appid, trigger.role, req_area, task_visible);
1042 g_app_list.reqDump();
1047 WMError WindowManager::startTransition(unsigned req_num)
1049 bool sync_draw_happen = false;
1051 WMError ret = WMError::SUCCESS;
1052 auto actions = g_app_list.getActions(req_num, &found);
1055 ret = WMError::NO_ENTRY;
1056 HMI_SEQ_ERROR(req_num,
1057 "Window Manager bug :%s : Action is not set", errorDescription(ret));
1061 for (const auto &action : actions)
1063 if (action.visible != TaskVisible::INVISIBLE)
1065 sync_draw_happen = true;
1067 // TODO: application requests by old role,
1068 // so convert role new to old for emitting event
1069 std::string old_role = this->rolenew2old[action.role];
1071 this->emit_syncdraw(old_role, action.area);
1072 /* TODO: emit event for app not subscriber
1073 if(g_app_list.contains(y.appid))
1074 g_app_list.lookUpClient(y.appid)->emit_syncdraw(y.role, y.area); */
1078 if (sync_draw_happen)
1084 // deactivate only, no syncDraw
1085 // Make it deactivate here
1086 for (const auto &x : actions)
1088 if (g_app_list.contains(x.appid))
1090 auto client = g_app_list.lookUpClient(x.appid);
1091 this->deactivate(client->surfaceID(x.role));
1094 ret = NO_LAYOUT_CHANGE;
1099 WMError WindowManager::setInvisibleTask(const std::string &role, bool split)
1101 unsigned req_num = g_app_list.currentRequestNumber();
1102 HMI_SEQ_DEBUG(req_num, "set current visible app to invisible task");
1103 // This task is copied from original actiavete surface
1104 const char *drawing_name = this->rolenew2old[role].c_str();
1105 auto const &surface_id = this->lookup_id(role.c_str());
1106 auto layer_id = this->layers.get_layer_id(*surface_id);
1107 auto o_state = *this->layers.get_layout_state(*surface_id);
1108 struct LayoutState &state = *o_state;
1109 std::string add_name, add_role;
1110 std::string add_area = "";
1112 TaskVisible task_visible = TaskVisible::INVISIBLE;
1113 bool end_draw_finished = true;
1116 for (auto const &l : this->layers.mapping)
1118 if (l.second.layer_id <= *layer_id)
1122 HMI_DEBUG("wm", "debug: main %d , sub : %d", l.second.state.main, l.second.state.sub);
1123 if (l.second.state.main != -1)
1125 //this->deactivate(l.second.state.main);
1126 surface = l.second.state.main;
1127 add_role = *this->id_alloc.lookup(surface);
1128 add_name = g_app_list.getAppID(surface, add_role, &found);
1130 return WMError::NOT_REGISTERED;
1132 HMI_SEQ_INFO(req_num, "Invisible %s", add_name.c_str());
1133 WMAction act{add_name, add_role, add_area, task_visible, end_draw_finished};
1134 g_app_list.setAction(req_num, act);
1135 l.second.state.main = -1;
1138 if (l.second.state.sub != -1)
1140 //this->deactivate(l.second.state.sub);
1141 surface = l.second.state.sub;
1142 add_role = *this->id_alloc.lookup(surface);
1143 add_name = g_app_list.getAppID(surface, add_role, &found);
1146 return WMError::NOT_REGISTERED;
1148 HMI_SEQ_INFO(req_num, "Invisible %s", add_name.c_str());
1149 WMAction act{add_name, add_role, add_area, task_visible, end_draw_finished};
1150 g_app_list.setAction(req_num, act);
1151 l.second.state.sub = -1;
1155 // change current state here, but this is hack
1156 auto layer = this->layers.get_layer(*layer_id);
1158 if (state.main == -1)
1160 HMI_DEBUG("wm", "Layout: %s", kNameLayoutNormal);
1164 if (0 != strcmp(drawing_name, "HomeScreen"))
1168 if (state.sub != *surface_id)
1170 if (state.sub != -1)
1172 //this->deactivate(state.sub);
1175 std::move(*this->id_alloc.lookup(state.sub));
1176 deact_sub.area = add_area;
1177 deact_sub.appid = g_app_list.getAppID(state.sub, deact_sub.role, &found);
1180 HMI_SEQ_ERROR(req_num, "App doesn't exist for role : %s",
1181 deact_sub.role.c_str());
1182 return WMError::NOT_REGISTERED;
1184 deact_sub.visible = task_visible;
1185 deact_sub.end_draw_finished = end_draw_finished;
1186 HMI_SEQ_DEBUG(req_num, "Set invisible task for %s", deact_sub.appid.c_str());
1187 g_app_list.setAction(req_num, deact_sub);
1190 //state = LayoutState{state.main, *surface_id};
1194 HMI_DEBUG("wm", "Layout: %s", kNameLayoutNormal);
1196 //this->surface_set_layout(*surface_id);
1197 if (state.main != *surface_id)
1199 // this->deactivate(state.main);
1200 WMAction deact_main;
1201 deact_main.role = std::move(*this->id_alloc.lookup(state.main));
1203 deact_main.area = add_area;
1204 deact_main.appid = g_app_list.getAppID(state.main, deact_main.role, &found);
1207 HMI_SEQ_DEBUG(req_num, "sub surface ddoesn't exist");
1208 return WMError::NOT_REGISTERED;
1210 deact_main.visible = task_visible;
1211 deact_main.end_draw_finished = end_draw_finished;
1212 HMI_SEQ_DEBUG(req_num, "sub surface doesn't exist");
1213 g_app_list.setAction(req_num, deact_main);
1215 if (state.sub != -1)
1217 if (state.sub != *surface_id)
1219 //this->deactivate(state.sub);
1221 deact_sub.role = std::move(*this->id_alloc.lookup(state.sub));
1223 deact_sub.area = add_area;
1224 deact_sub.appid = g_app_list.getAppID(state.sub, deact_sub.role, &found);
1227 HMI_SEQ_DEBUG(req_num, "sub surface ddoesn't exist");
1228 return WMError::NOT_REGISTERED;
1230 deact_sub.visible = task_visible;
1231 deact_sub.end_draw_finished = end_draw_finished;
1232 HMI_SEQ_DEBUG(req_num, "sub surface doesn't exist");
1233 g_app_list.setAction(req_num, deact_sub);
1236 //state = LayoutState{*surface_id};
1240 return WMError::SUCCESS;
1243 WMError WindowManager::doEndDraw(unsigned req_num)
1247 auto actions = g_app_list.getActions(req_num, &found);
1248 WMError ret = WMError::SUCCESS;
1251 ret = WMError::NO_ENTRY;
1255 HMI_SEQ_INFO(req_num, "do endDraw");
1257 // layout change and make it visible
1258 for (const auto &act : actions)
1261 if(!g_app_list.contains(act.appid)){
1262 ret = WMError::NOT_REGISTERED;
1264 ret = this->layoutChange(act);
1265 if(ret != WMError::SUCCESS)
1267 HMI_SEQ_WARNING(req_num,
1268 "Failed to manipulate surfaces while state change : %s", errorDescription(ret));
1271 ret = this->visibilityChange(act);
1272 if (ret != WMError::SUCCESS)
1274 HMI_SEQ_WARNING(req_num,
1275 "Failed to manipulate surfaces while state change : %s", errorDescription(ret));
1278 HMI_SEQ_DEBUG(req_num, "visible %s", act.role.c_str());
1279 //this->lm_enddraw(act.role.c_str());
1282 // Change current state
1283 this->changeCurrentState(req_num);
1285 HMI_SEQ_INFO(req_num, "emit flushDraw");
1287 for(const auto &act_flush : actions)
1289 if(act_flush.visible != TaskVisible::INVISIBLE)
1291 // TODO: application requests by old role,
1292 // so convert role new to old for emitting event
1293 std::string old_role = this->rolenew2old[act_flush.role];
1295 this->emit_flushdraw(old_role.c_str());
1302 WMError WindowManager::layoutChange(const WMAction &action)
1304 if (action.visible == TaskVisible::INVISIBLE)
1306 // Visibility is not change -> no redraw is required
1307 return WMError::SUCCESS;
1309 auto client = g_app_list.lookUpClient(action.appid);
1310 unsigned surface = client->surfaceID(action.role);
1313 HMI_SEQ_ERROR(g_app_list.currentRequestNumber(),
1314 "client doesn't have surface with role(%s)", action.role.c_str());
1315 return WMError::NOT_REGISTERED;
1318 WMError ret = this->setSurfaceSize(surface, action.area);
1322 WMError WindowManager::visibilityChange(const WMAction &action)
1324 HMI_SEQ_DEBUG(g_app_list.currentRequestNumber(), "Change visibility");
1325 if(!g_app_list.contains(action.appid)){
1326 return WMError::NOT_REGISTERED;
1328 auto client = g_app_list.lookUpClient(action.appid);
1329 unsigned surface = client->surfaceID(action.role);
1332 HMI_SEQ_ERROR(g_app_list.currentRequestNumber(),
1333 "client doesn't have surface with role(%s)", action.role.c_str());
1334 return WMError::NOT_REGISTERED;
1337 if (action.visible != TaskVisible::INVISIBLE)
1339 this->activate(surface); // Layout Manager task
1343 this->deactivate(surface); // Layout Manager task
1345 return WMError::SUCCESS;
1348 WMError WindowManager::setSurfaceSize(unsigned surface, const std::string &area)
1350 this->surface_set_layout(surface, area);
1352 return WMError::SUCCESS;
1355 WMError WindowManager::changeCurrentState(unsigned req_num)
1357 HMI_SEQ_DEBUG(req_num, "Change current layout state");
1358 bool trigger_found = false, action_found = false;
1359 auto trigger = g_app_list.getRequest(req_num, &trigger_found);
1360 auto actions = g_app_list.getActions(req_num, &action_found);
1361 if (!trigger_found || !action_found)
1363 HMI_SEQ_ERROR(req_num, "Action not found");
1364 return WMError::LAYOUT_CHANGE_FAIL;
1367 // Layout state reset
1368 struct LayoutState reset_state{-1, -1};
1369 HMI_SEQ_DEBUG(req_num,"Reset layout state");
1370 for (const auto &action : actions)
1372 if(!g_app_list.contains(action.appid)){
1373 return WMError::NOT_REGISTERED;
1375 auto client = g_app_list.lookUpClient(action.appid);
1376 auto pCurState = *this->layers.get_layout_state((int)client->surfaceID(action.role));
1377 if(pCurState == nullptr)
1379 HMI_SEQ_ERROR(req_num, "Counldn't find current status");
1382 pCurState->main = reset_state.main;
1383 pCurState->sub = reset_state.sub;
1386 HMI_SEQ_DEBUG(req_num, "Change state");
1387 for (const auto &action : actions)
1389 auto client = g_app_list.lookUpClient(action.appid);
1390 auto pLayerCurState = *this->layers.get_layout_state((int)client->surfaceID(action.role));
1391 if (pLayerCurState == nullptr)
1393 HMI_SEQ_ERROR(req_num, "Counldn't find current status");
1398 if (action.visible != TaskVisible::INVISIBLE)
1400 surface = (int)client->surfaceID(action.role);
1401 HMI_SEQ_INFO(req_num, "Change %s surface : %d, state visible area : %s",
1402 action.role.c_str(), surface, action.area.c_str());
1403 // visible == true -> layout changes
1404 if(action.area == "normal.full" || action.area == "split.main")
1406 pLayerCurState->main = surface;
1408 else if (action.area == "split.sub")
1410 pLayerCurState->sub = surface;
1415 pLayerCurState->main = surface;
1420 return WMError::SUCCESS;
1423 void WindowManager::emitScreenUpdated(unsigned req_num)
1426 HMI_SEQ_DEBUG(req_num, "emit screen updated");
1428 auto actions = g_app_list.getActions(req_num, &found);
1430 // create json object
1431 json_object *j = json_object_new_object();
1432 json_object *jarray = json_object_new_array();
1434 for(const auto& action: actions)
1436 if(action.visible != TaskVisible::INVISIBLE)
1438 json_object_array_add(jarray, json_object_new_string(action.appid.c_str()));
1441 json_object_object_add(j, kKeyIds, jarray);
1442 HMI_SEQ_INFO(req_num, "Visible app: %s", json_object_get_string(j));
1444 int ret = afb_event_push(
1445 this->map_afb_event[kListEventName[Event_ScreenUpdated]], j);
1448 HMI_DEBUG("wm", "afb_event_push failed: %m");
1450 json_object_put(jarray);
1453 void WindowManager::setTimer()
1455 HMI_SEQ_DEBUG(g_app_list.currentRequestNumber(), "Timer set activate");
1456 if (g_timer_ev_src == nullptr)
1458 // firsttime set into sd_event
1459 int ret = sd_event_add_time(afb_daemon_get_event_loop(), &g_timer_ev_src,
1460 CLOCK_REALTIME, time(NULL) * (1000000UL) + kTimeOut, 1, processTimerHandler, this);
1463 HMI_ERROR("wm", "Could't set timer");
1468 // update timer limitation after second time
1469 sd_event_source_set_time(g_timer_ev_src, time(NULL) * (1000000UL) + kTimeOut);
1470 sd_event_source_set_enabled(g_timer_ev_src, SD_EVENT_ONESHOT);
1474 void WindowManager::stopTimer()
1476 unsigned req_num = g_app_list.currentRequestNumber();
1477 HMI_SEQ_DEBUG(req_num, "Timer stop");
1478 int rc = sd_event_source_set_enabled(g_timer_ev_src, SD_EVENT_OFF);
1481 HMI_SEQ_ERROR(req_num, "Timer stop failed");
1485 void WindowManager::processNextRequest()
1488 g_app_list.reqDump();
1489 unsigned req_num = g_app_list.currentRequestNumber();
1490 if (g_app_list.haveRequest())
1492 HMI_SEQ_DEBUG(req_num, "Process next request");
1493 WMError rc = doTransition(req_num);
1494 if (rc != WMError::SUCCESS)
1496 HMI_SEQ_ERROR(req_num, errorDescription(rc));
1501 HMI_SEQ_DEBUG(req_num, "Nothing Request. Waiting Request");
1505 const char* WindowManager::convertRoleOldToNew(char const *old_role)
1507 const char *new_role = nullptr;
1509 for (auto const &on : this->roleold2new)
1511 std::regex regex = std::regex(on.first);
1512 if (std::regex_match(old_role, regex))
1514 // role is old. So convert to new.
1515 new_role = on.second.c_str();
1520 if (nullptr == new_role)
1522 // role is new or fallback.
1523 new_role = old_role;
1526 HMI_DEBUG("wm", "old:%s -> new:%s", old_role, new_role);
1531 int WindowManager::loadOldRoleDb()
1533 // Get afm application installed dir
1534 char const *afm_app_install_dir = getenv("AFM_APP_INSTALL_DIR");
1535 HMI_DEBUG("wm", "afm_app_install_dir:%s", afm_app_install_dir);
1537 std::string file_name;
1538 if (!afm_app_install_dir)
1540 HMI_ERROR("wm", "AFM_APP_INSTALL_DIR is not defined");
1544 file_name = std::string(afm_app_install_dir) + std::string("/etc/old_roles.db");
1548 json_object* json_obj;
1549 int ret = jh::inputJsonFilie(file_name.c_str(), &json_obj);
1552 HMI_ERROR("wm", "Could not open old_role.db, so use default old_role information");
1553 json_obj = json_tokener_parse(kDefaultOldRoleDb);
1555 HMI_DEBUG("wm", "json_obj dump:%s", json_object_get_string(json_obj));
1558 json_object* json_cfg;
1559 if (!json_object_object_get_ex(json_obj, "old_roles", &json_cfg))
1561 HMI_ERROR("wm", "Parse Error!!");
1565 int len = json_object_array_length(json_cfg);
1566 HMI_DEBUG("wm", "json_cfg len:%d", len);
1567 HMI_DEBUG("wm", "json_cfg dump:%s", json_object_get_string(json_cfg));
1569 for (int i=0; i<len; i++)
1571 json_object* json_tmp = json_object_array_get_idx(json_cfg, i);
1573 const char* old_role = jh::getStringFromJson(json_tmp, "name");
1574 if (nullptr == old_role)
1576 HMI_ERROR("wm", "Parse Error!!");
1580 const char* new_role = jh::getStringFromJson(json_tmp, "new");
1581 if (nullptr == new_role)
1583 HMI_ERROR("wm", "Parse Error!!");
1587 this->roleold2new[old_role] = std::string(new_role);
1591 for(auto itr = this->roleold2new.begin();
1592 itr != this->roleold2new.end(); ++itr)
1594 HMI_DEBUG("wm", ">>> role old:%s new:%s",
1595 itr->first.c_str(), itr->second.c_str());
1598 // Release json_object
1599 json_object_put(json_obj);
1604 const char *WindowManager::check_surface_exist(const char *drawing_name)
1606 auto const &surface_id = this->lookup_id(drawing_name);
1609 return "Surface does not exist";
1612 if (!this->controller->surface_exists(*surface_id))
1614 return "Surface does not exist in controller!";
1617 auto layer_id = this->layers.get_layer_id(*surface_id);
1621 return "Surface is not on any layer!";
1624 auto o_state = *this->layers.get_layout_state(*surface_id);
1626 if (o_state == nullptr)
1628 return "Could not find layer for surface";
1631 HMI_DEBUG("wm", "surface %d is detected", *surface_id);
1635 bool WindowManager::can_split(struct LayoutState const &state, int new_id)
1637 if (state.main != -1 && state.main != new_id)
1639 auto new_id_layer = this->layers.get_layer_id(new_id).value();
1640 auto current_id_layer = this->layers.get_layer_id(state.main).value();
1642 // surfaces are on separate layers, don't bother.
1643 if (new_id_layer != current_id_layer)
1648 std::string const &new_id_str = this->lookup_name(new_id).value();
1649 std::string const &cur_id_str = this->lookup_name(state.main).value();
1651 auto const &layer = this->layers.get_layer(new_id_layer);
1653 HMI_DEBUG("wm", "layer info name: %s", layer->name.c_str());
1655 if (layer->layouts.empty())
1660 for (auto i = layer->layouts.cbegin(); i != layer->layouts.cend(); i++)
1662 HMI_DEBUG("wm", "%d main_match '%s'", new_id_layer, i->main_match.c_str());
1663 auto rem = std::regex(i->main_match);
1664 if (std::regex_match(cur_id_str, rem))
1666 // build the second one only if the first already matched
1667 HMI_DEBUG("wm", "%d sub_match '%s'", new_id_layer, i->sub_match.c_str());
1668 auto res = std::regex(i->sub_match);
1669 if (std::regex_match(new_id_str, res))
1671 HMI_DEBUG("wm", "layout matched!");
1681 const char* WindowManager::kDefaultOldRoleDb = "{ \
1684 \"name\": \"HomeScreen\", \
1685 \"new\": \"homescreen\" \
1688 \"name\": \"Music\", \
1689 \"new\": \"music\" \
1692 \"name\": \"MediaPlayer\", \
1693 \"new\": \"music\" \
1696 \"name\": \"Video\", \
1697 \"new\": \"video\" \
1700 \"name\": \"VideoPlayer\", \
1701 \"new\": \"video\" \
1704 \"name\": \"WebBrowser\", \
1705 \"new\": \"browser\" \
1708 \"name\": \"Radio\", \
1709 \"new\": \"radio\" \
1712 \"name\": \"Phone\", \
1713 \"new\": \"phone\" \
1716 \"name\": \"Navigation\", \
1720 \"name\": \"HVAC\", \
1724 \"name\": \"Settings\", \
1725 \"new\": \"settings\" \
1728 \"name\": \"Dashboard\", \
1729 \"new\": \"dashboard\" \
1732 \"name\": \"POI\", \
1736 \"name\": \"Mixer\", \
1737 \"new\": \"mixer\" \
1740 \"name\": \"Restriction\", \
1741 \"new\": \"restriction\" \
1744 \"name\": \"^OnScreen.*\", \
1745 \"new\": \"on_screen\" \
1753 void controller_hooks::surface_created(uint32_t surface_id)
1755 this->app->surface_created(surface_id);
1758 void controller_hooks::surface_removed(uint32_t surface_id)
1760 this->app->surface_removed(surface_id);
1763 void controller_hooks::surface_visibility(uint32_t /*surface_id*/,
1766 void controller_hooks::surface_destination_rectangle(uint32_t /*surface_id*/,