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 uint64_t kTimeOut = 3ULL; /* 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_source_rectangle(0, 0, w, h);
742 this->layout_commit();
743 s->set_destination_rectangle(x, y, w, h);
745 // update area information
746 this->area_info[surface_id].x = x;
747 this->area_info[surface_id].y = y;
748 this->area_info[surface_id].w = w;
749 this->area_info[surface_id].h = h;
751 HMI_DEBUG("wm", "Surface %u now on layer %u with rect { %d, %d, %d, %d }",
752 surface_id, layer_id, x, y, w, h);
755 void WindowManager::layout_commit()
757 this->controller->commit_changes();
758 this->display->flush();
761 void WindowManager::emit_activated(char const *label)
763 this->send_event(kListEventName[Event_Active], label);
766 void WindowManager::emit_deactivated(char const *label)
768 this->send_event(kListEventName[Event_Inactive], label);
771 void WindowManager::emit_syncdraw(char const *label, char const *area, int x, int y, int w, int h)
773 this->send_event(kListEventName[Event_SyncDraw], label, area, x, y, w, h);
776 void WindowManager::emit_syncdraw(const std::string &role, const std::string &area)
778 compositor::rect rect = this->layers.getAreaSize(area);
779 this->send_event(kListEventName[Event_SyncDraw],
780 role.c_str(), area.c_str(), rect.x, rect.y, rect.w, rect.h);
783 void WindowManager::emit_flushdraw(char const *label)
785 this->send_event(kListEventName[Event_FlushDraw], label);
788 void WindowManager::emit_visible(char const *label, bool is_visible)
790 this->send_event(is_visible ? kListEventName[Event_Visible] : kListEventName[Event_Invisible], label);
793 void WindowManager::emit_invisible(char const *label)
795 return emit_visible(label, false);
798 void WindowManager::emit_visible(char const *label) { return emit_visible(label, true); }
800 void WindowManager::activate(int id)
802 auto ip = this->controller->sprops.find(id);
803 if (ip != this->controller->sprops.end())
805 this->controller->surfaces[id]->set_visibility(1);
807 this->lookup_name(id).value_or("unknown-name").c_str();
810 if ((0 == strcmp(label, "radio")) ||
811 (0 == strcmp(label, "music")) ||
812 (0 == strcmp(label, "video")) ||
813 (0 == strcmp(label, "map")))
815 for (auto i = surface_bg.begin(); i != surface_bg.end(); ++i)
820 this->surface_bg.erase(i);
822 // Remove from BG layer (999)
823 HMI_DEBUG("wm", "Remove %s(%d) from BG layer", label, id);
824 this->controller->layers[999]->remove_surface(id);
826 // Add to FG layer (1001)
827 HMI_DEBUG("wm", "Add %s(%d) to FG layer", label, id);
828 this->controller->layers[1001]->add_surface(id);
830 for (int j : this->surface_bg)
832 HMI_DEBUG("wm", "Stored id:%d", j);
840 this->layout_commit();
842 // TODO: application requests by old role,
843 // so convert role new to old for emitting event
844 const char* old_role = this->rolenew2old[label].c_str();
846 this->emit_visible(old_role);
847 this->emit_activated(old_role);
851 void WindowManager::deactivate(int id)
853 auto ip = this->controller->sprops.find(id);
854 if (ip != this->controller->sprops.end())
857 this->lookup_name(id).value_or("unknown-name").c_str();
860 if ((0 == strcmp(label, "radio")) ||
861 (0 == strcmp(label, "music")) ||
862 (0 == strcmp(label, "video")) ||
863 (0 == strcmp(label, "map")))
867 this->surface_bg.push_back(id);
869 // Remove from FG layer (1001)
870 HMI_DEBUG("wm", "Remove %s(%d) from FG layer", label, id);
871 this->controller->layers[1001]->remove_surface(id);
873 // Add to BG layer (999)
874 HMI_DEBUG("wm", "Add %s(%d) to BG layer", label, id);
875 this->controller->layers[999]->add_surface(id);
877 for (int j : surface_bg)
879 HMI_DEBUG("wm", "Stored id:%d", j);
884 this->controller->surfaces[id]->set_visibility(0);
888 this->layout_commit();
890 // TODO: application requests by old role,
891 // so convert role new to old for emitting event
892 const char* old_role = this->rolenew2old[label].c_str();
894 this->emit_deactivated(old_role);
895 this->emit_invisible(old_role);
899 WMError WindowManager::setRequest(const std::string& appid, const std::string &role, const std::string &area,
900 Task task, unsigned* req_num)
902 if (!g_app_list.contains(appid))
904 return WMError::NOT_REGISTERED;
907 auto client = g_app_list.lookUpClient(appid);
912 unsigned current = g_app_list.currentRequestNumber();
913 unsigned requested_num = g_app_list.getRequestNumber(appid);
914 if (requested_num != 0)
916 HMI_SEQ_INFO(requested_num,
917 "%s %s %s request is already queued", appid.c_str(), role.c_str(), area.c_str());
921 WMRequest req = WMRequest(appid, role, area, task);
922 unsigned new_req = g_app_list.addRequest(req);
924 g_app_list.reqDump();
926 HMI_SEQ_DEBUG(current, "%s start sequence with %s, %s", appid.c_str(), role.c_str(), area.c_str());
928 return WMError::SUCCESS;
931 WMError WindowManager::doTransition(unsigned req_num)
933 HMI_SEQ_DEBUG(req_num, "check policy");
934 WMError ret = this->checkPolicy(req_num);
935 if (ret != WMError::SUCCESS)
939 HMI_SEQ_DEBUG(req_num, "Start transition.");
940 ret = this->startTransition(req_num);
944 WMError WindowManager::checkPolicy(unsigned req_num)
949 // get current trigger
952 WMError ret = WMError::LAYOUT_CHANGE_FAIL;
953 auto trigger = g_app_list.getRequest(req_num, &found);
956 ret = WMError::NO_ENTRY;
959 std::string req_area = trigger.area;
961 // >>>> Compatible with current window manager until policy manager coming
962 if (trigger.task == Task::TASK_ALLOCATE)
964 HMI_SEQ_DEBUG(req_num, "Check split or not");
965 const char *msg = this->check_surface_exist(trigger.role.c_str());
969 HMI_SEQ_ERROR(req_num, msg);
970 ret = WMError::LAYOUT_CHANGE_FAIL;
974 auto const &surface_id = this->lookup_id(trigger.role.c_str());
975 auto o_state = *this->layers.get_layout_state(*surface_id);
976 struct LayoutState &state = *o_state;
978 unsigned curernt_sid = state.main;
979 split = this->can_split(state, *surface_id);
983 HMI_SEQ_DEBUG(req_num, "Split happens");
984 // Get current visible role
985 std::string add_role = this->lookup_name(state.main).value();
987 std::string add_area = std::string(kNameLayoutSplit) + "." + std::string(kNameAreaMain);
988 // Change request area
989 req_area = std::string(kNameLayoutSplit) + "." + std::string(kNameAreaSub);
990 HMI_SEQ_NOTICE(req_num, "Change request area from %s to %s, because split happens",
991 trigger.area.c_str(), req_area.c_str());
992 // set another action
993 std::string add_name = g_app_list.getAppID(curernt_sid, add_role, &found);
996 HMI_SEQ_ERROR(req_num, "Couldn't widhdraw with surfaceID : %d", curernt_sid);
997 ret = WMError::NOT_REGISTERED;
1000 HMI_SEQ_INFO(req_num, "Additional split app %s, role: %s, area: %s",
1001 add_name.c_str(), add_role.c_str(), add_area.c_str());
1003 bool end_draw_finished = false;
1004 WMAction split_action{
1008 TaskVisible::VISIBLE,
1010 WMError ret = g_app_list.setAction(req_num, split_action);
1011 if (ret != WMError::SUCCESS)
1013 HMI_SEQ_ERROR(req_num, "Failed to set action");
1016 g_app_list.reqDump();
1021 HMI_SEQ_DEBUG(req_num, "split doesn't happen");
1024 // Set invisible task(Remove if policy manager finish)
1025 ret = this->setInvisibleTask(trigger.role, split);
1026 if(ret != WMError::SUCCESS)
1028 HMI_SEQ_ERROR(req_num, "Failed to set invisible task: %s", errorDescription(ret));
1032 /* get new status from Policy Manager */
1033 HMI_SEQ_NOTICE(req_num, "ATM, Policy manager does't exist, then set WMAction as is");
1034 if(trigger.role == "homescreen")
1036 // TODO : Remove when Policy Manager completed
1037 HMI_SEQ_NOTICE(req_num, "Hack. This process will be removed. Change HomeScreen code!!");
1038 req_area = "fullscreen";
1040 TaskVisible task_visible =
1041 (trigger.task == Task::TASK_ALLOCATE) ? TaskVisible::VISIBLE : TaskVisible::INVISIBLE;
1043 ret = g_app_list.setAction(req_num, trigger.appid, trigger.role, req_area, task_visible);
1044 g_app_list.reqDump();
1049 WMError WindowManager::startTransition(unsigned req_num)
1051 bool sync_draw_happen = false;
1053 WMError ret = WMError::SUCCESS;
1054 auto actions = g_app_list.getActions(req_num, &found);
1057 ret = WMError::NO_ENTRY;
1058 HMI_SEQ_ERROR(req_num,
1059 "Window Manager bug :%s : Action is not set", errorDescription(ret));
1063 for (const auto &action : actions)
1065 if (action.visible != TaskVisible::INVISIBLE)
1067 sync_draw_happen = true;
1069 // TODO: application requests by old role,
1070 // so convert role new to old for emitting event
1071 std::string old_role = this->rolenew2old[action.role];
1073 this->emit_syncdraw(old_role, action.area);
1074 /* TODO: emit event for app not subscriber
1075 if(g_app_list.contains(y.appid))
1076 g_app_list.lookUpClient(y.appid)->emit_syncdraw(y.role, y.area); */
1080 if (sync_draw_happen)
1086 // deactivate only, no syncDraw
1087 // Make it deactivate here
1088 for (const auto &x : actions)
1090 if (g_app_list.contains(x.appid))
1092 auto client = g_app_list.lookUpClient(x.appid);
1093 this->deactivate(client->surfaceID(x.role));
1096 ret = NO_LAYOUT_CHANGE;
1101 WMError WindowManager::setInvisibleTask(const std::string &role, bool split)
1103 unsigned req_num = g_app_list.currentRequestNumber();
1104 HMI_SEQ_DEBUG(req_num, "set current visible app to invisible task");
1106 auto trigger = g_app_list.getRequest(req_num, &found);
1107 // I don't check found == true here because this is checked in caller.
1108 if(trigger.role == "homescreen")
1110 HMI_SEQ_INFO(req_num, "In case of 'homescreen' visible, don't change app to invisible");
1111 return WMError::SUCCESS;
1114 // This task is copied from original actiavete surface
1115 const char *drawing_name = this->rolenew2old[role].c_str();
1116 auto const &surface_id = this->lookup_id(role.c_str());
1117 auto layer_id = this->layers.get_layer_id(*surface_id);
1118 auto o_state = *this->layers.get_layout_state(*surface_id);
1119 struct LayoutState &state = *o_state;
1120 std::string add_name, add_role;
1121 std::string add_area = "";
1123 TaskVisible task_visible = TaskVisible::INVISIBLE;
1124 bool end_draw_finished = true;
1126 for (auto const &l : this->layers.mapping)
1128 if (l.second.layer_id <= *layer_id)
1132 HMI_DEBUG("wm", "debug: main %d , sub : %d", l.second.state.main, l.second.state.sub);
1133 if (l.second.state.main != -1)
1135 //this->deactivate(l.second.state.main);
1136 surface = l.second.state.main;
1137 add_role = *this->id_alloc.lookup(surface);
1138 add_name = g_app_list.getAppID(surface, add_role, &found);
1140 return WMError::NOT_REGISTERED;
1142 HMI_SEQ_INFO(req_num, "Invisible %s", add_name.c_str());
1143 WMAction act{add_name, add_role, add_area, task_visible, end_draw_finished};
1144 g_app_list.setAction(req_num, act);
1145 l.second.state.main = -1;
1148 if (l.second.state.sub != -1)
1150 //this->deactivate(l.second.state.sub);
1151 surface = l.second.state.sub;
1152 add_role = *this->id_alloc.lookup(surface);
1153 add_name = g_app_list.getAppID(surface, add_role, &found);
1156 return WMError::NOT_REGISTERED;
1158 HMI_SEQ_INFO(req_num, "Invisible %s", add_name.c_str());
1159 WMAction act{add_name, add_role, add_area, task_visible, end_draw_finished};
1160 g_app_list.setAction(req_num, act);
1161 l.second.state.sub = -1;
1165 // change current state here, but this is hack
1166 auto layer = this->layers.get_layer(*layer_id);
1168 if (state.main == -1)
1170 HMI_DEBUG("wm", "Layout: %s", kNameLayoutNormal);
1174 if (0 != strcmp(drawing_name, "HomeScreen"))
1178 if (state.sub != *surface_id)
1180 if (state.sub != -1)
1182 //this->deactivate(state.sub);
1185 std::move(*this->id_alloc.lookup(state.sub));
1186 deact_sub.area = add_area;
1187 deact_sub.appid = g_app_list.getAppID(state.sub, deact_sub.role, &found);
1190 HMI_SEQ_ERROR(req_num, "App doesn't exist for role : %s",
1191 deact_sub.role.c_str());
1192 return WMError::NOT_REGISTERED;
1194 deact_sub.visible = task_visible;
1195 deact_sub.end_draw_finished = end_draw_finished;
1196 HMI_SEQ_DEBUG(req_num, "Set invisible task for %s", deact_sub.appid.c_str());
1197 g_app_list.setAction(req_num, deact_sub);
1200 //state = LayoutState{state.main, *surface_id};
1204 HMI_DEBUG("wm", "Layout: %s", kNameLayoutNormal);
1206 //this->surface_set_layout(*surface_id);
1207 if (state.main != *surface_id)
1209 // this->deactivate(state.main);
1210 WMAction deact_main;
1211 deact_main.role = std::move(*this->id_alloc.lookup(state.main));
1213 deact_main.area = add_area;
1214 deact_main.appid = g_app_list.getAppID(state.main, deact_main.role, &found);
1217 HMI_SEQ_DEBUG(req_num, "sub surface ddoesn't exist");
1218 return WMError::NOT_REGISTERED;
1220 deact_main.visible = task_visible;
1221 deact_main.end_draw_finished = end_draw_finished;
1222 HMI_SEQ_DEBUG(req_num, "sub surface doesn't exist");
1223 g_app_list.setAction(req_num, deact_main);
1225 if (state.sub != -1)
1227 if (state.sub != *surface_id)
1229 //this->deactivate(state.sub);
1231 deact_sub.role = std::move(*this->id_alloc.lookup(state.sub));
1233 deact_sub.area = add_area;
1234 deact_sub.appid = g_app_list.getAppID(state.sub, deact_sub.role, &found);
1237 HMI_SEQ_DEBUG(req_num, "sub surface ddoesn't exist");
1238 return WMError::NOT_REGISTERED;
1240 deact_sub.visible = task_visible;
1241 deact_sub.end_draw_finished = end_draw_finished;
1242 HMI_SEQ_DEBUG(req_num, "sub surface doesn't exist");
1243 g_app_list.setAction(req_num, deact_sub);
1246 //state = LayoutState{*surface_id};
1250 return WMError::SUCCESS;
1253 WMError WindowManager::doEndDraw(unsigned req_num)
1257 auto actions = g_app_list.getActions(req_num, &found);
1258 WMError ret = WMError::SUCCESS;
1261 ret = WMError::NO_ENTRY;
1265 HMI_SEQ_INFO(req_num, "do endDraw");
1267 // layout change and make it visible
1268 for (const auto &act : actions)
1271 if(!g_app_list.contains(act.appid)){
1272 ret = WMError::NOT_REGISTERED;
1274 ret = this->layoutChange(act);
1275 if(ret != WMError::SUCCESS)
1277 HMI_SEQ_WARNING(req_num,
1278 "Failed to manipulate surfaces while state change : %s", errorDescription(ret));
1281 ret = this->visibilityChange(act);
1282 if (ret != WMError::SUCCESS)
1284 HMI_SEQ_WARNING(req_num,
1285 "Failed to manipulate surfaces while state change : %s", errorDescription(ret));
1288 HMI_SEQ_DEBUG(req_num, "visible %s", act.role.c_str());
1289 //this->lm_enddraw(act.role.c_str());
1292 // Change current state
1293 this->changeCurrentState(req_num);
1295 HMI_SEQ_INFO(req_num, "emit flushDraw");
1297 for(const auto &act_flush : actions)
1299 if(act_flush.visible != TaskVisible::INVISIBLE)
1301 // TODO: application requests by old role,
1302 // so convert role new to old for emitting event
1303 std::string old_role = this->rolenew2old[act_flush.role];
1305 this->emit_flushdraw(old_role.c_str());
1312 WMError WindowManager::layoutChange(const WMAction &action)
1314 if (action.visible == TaskVisible::INVISIBLE)
1316 // Visibility is not change -> no redraw is required
1317 return WMError::SUCCESS;
1319 auto client = g_app_list.lookUpClient(action.appid);
1320 unsigned surface = client->surfaceID(action.role);
1323 HMI_SEQ_ERROR(g_app_list.currentRequestNumber(),
1324 "client doesn't have surface with role(%s)", action.role.c_str());
1325 return WMError::NOT_REGISTERED;
1328 WMError ret = this->setSurfaceSize(surface, action.area);
1332 WMError WindowManager::visibilityChange(const WMAction &action)
1334 HMI_SEQ_DEBUG(g_app_list.currentRequestNumber(), "Change visibility");
1335 if(!g_app_list.contains(action.appid)){
1336 return WMError::NOT_REGISTERED;
1338 auto client = g_app_list.lookUpClient(action.appid);
1339 unsigned surface = client->surfaceID(action.role);
1342 HMI_SEQ_ERROR(g_app_list.currentRequestNumber(),
1343 "client doesn't have surface with role(%s)", action.role.c_str());
1344 return WMError::NOT_REGISTERED;
1347 if (action.visible != TaskVisible::INVISIBLE)
1349 this->activate(surface); // Layout Manager task
1353 this->deactivate(surface); // Layout Manager task
1355 return WMError::SUCCESS;
1358 WMError WindowManager::setSurfaceSize(unsigned surface, const std::string &area)
1360 this->surface_set_layout(surface, area);
1362 return WMError::SUCCESS;
1365 WMError WindowManager::changeCurrentState(unsigned req_num)
1367 HMI_SEQ_DEBUG(req_num, "Change current layout state");
1368 bool trigger_found = false, action_found = false;
1369 auto trigger = g_app_list.getRequest(req_num, &trigger_found);
1370 auto actions = g_app_list.getActions(req_num, &action_found);
1371 if (!trigger_found || !action_found)
1373 HMI_SEQ_ERROR(req_num, "Action not found");
1374 return WMError::LAYOUT_CHANGE_FAIL;
1377 // Layout state reset
1378 struct LayoutState reset_state{-1, -1};
1379 HMI_SEQ_DEBUG(req_num,"Reset layout state");
1380 for (const auto &action : actions)
1382 if(!g_app_list.contains(action.appid)){
1383 return WMError::NOT_REGISTERED;
1385 auto client = g_app_list.lookUpClient(action.appid);
1386 auto pCurState = *this->layers.get_layout_state((int)client->surfaceID(action.role));
1387 if(pCurState == nullptr)
1389 HMI_SEQ_ERROR(req_num, "Counldn't find current status");
1392 pCurState->main = reset_state.main;
1393 pCurState->sub = reset_state.sub;
1396 HMI_SEQ_DEBUG(req_num, "Change state");
1397 for (const auto &action : actions)
1399 auto client = g_app_list.lookUpClient(action.appid);
1400 auto pLayerCurState = *this->layers.get_layout_state((int)client->surfaceID(action.role));
1401 if (pLayerCurState == nullptr)
1403 HMI_SEQ_ERROR(req_num, "Counldn't find current status");
1408 if (action.visible != TaskVisible::INVISIBLE)
1410 surface = (int)client->surfaceID(action.role);
1411 HMI_SEQ_INFO(req_num, "Change %s surface : %d, state visible area : %s",
1412 action.role.c_str(), surface, action.area.c_str());
1413 // visible == true -> layout changes
1414 if(action.area == "normal.full" || action.area == "split.main")
1416 pLayerCurState->main = surface;
1418 else if (action.area == "split.sub")
1420 pLayerCurState->sub = surface;
1425 pLayerCurState->main = surface;
1430 return WMError::SUCCESS;
1433 void WindowManager::emitScreenUpdated(unsigned req_num)
1436 HMI_SEQ_DEBUG(req_num, "emit screen updated");
1438 auto actions = g_app_list.getActions(req_num, &found);
1440 // create json object
1441 json_object *j = json_object_new_object();
1442 json_object *jarray = json_object_new_array();
1444 for(const auto& action: actions)
1446 if(action.visible != TaskVisible::INVISIBLE)
1448 json_object_array_add(jarray, json_object_new_string(action.appid.c_str()));
1451 json_object_object_add(j, kKeyIds, jarray);
1452 HMI_SEQ_INFO(req_num, "Visible app: %s", json_object_get_string(j));
1454 int ret = afb_event_push(
1455 this->map_afb_event[kListEventName[Event_ScreenUpdated]], j);
1458 HMI_DEBUG("wm", "afb_event_push failed: %m");
1462 void WindowManager::setTimer()
1465 if (clock_gettime(CLOCK_BOOTTIME, &ts) != 0) {
1466 HMI_ERROR("wm", "Could't set time (clock_gettime() returns with error");
1470 HMI_SEQ_DEBUG(g_app_list.currentRequestNumber(), "Timer set activate");
1471 if (g_timer_ev_src == nullptr)
1473 // firsttime set into sd_event
1474 int ret = sd_event_add_time(afb_daemon_get_event_loop(), &g_timer_ev_src,
1475 CLOCK_BOOTTIME, (uint64_t)(ts.tv_sec + kTimeOut) * 1000000ULL, 1, processTimerHandler, this);
1478 HMI_ERROR("wm", "Could't set timer");
1483 // update timer limitation after second time
1484 sd_event_source_set_time(g_timer_ev_src, (uint64_t)(ts.tv_sec + kTimeOut) * 1000000ULL);
1485 sd_event_source_set_enabled(g_timer_ev_src, SD_EVENT_ONESHOT);
1489 void WindowManager::stopTimer()
1491 unsigned req_num = g_app_list.currentRequestNumber();
1492 HMI_SEQ_DEBUG(req_num, "Timer stop");
1493 int rc = sd_event_source_set_enabled(g_timer_ev_src, SD_EVENT_OFF);
1496 HMI_SEQ_ERROR(req_num, "Timer stop failed");
1500 void WindowManager::processNextRequest()
1503 g_app_list.reqDump();
1504 unsigned req_num = g_app_list.currentRequestNumber();
1505 if (g_app_list.haveRequest())
1507 HMI_SEQ_DEBUG(req_num, "Process next request");
1508 WMError rc = doTransition(req_num);
1509 if (rc != WMError::SUCCESS)
1511 HMI_SEQ_ERROR(req_num, errorDescription(rc));
1516 HMI_SEQ_DEBUG(req_num, "Nothing Request. Waiting Request");
1520 const char* WindowManager::convertRoleOldToNew(char const *old_role)
1522 const char *new_role = nullptr;
1524 for (auto const &on : this->roleold2new)
1526 std::regex regex = std::regex(on.first);
1527 if (std::regex_match(old_role, regex))
1529 // role is old. So convert to new.
1530 new_role = on.second.c_str();
1535 if (nullptr == new_role)
1537 // role is new or fallback.
1538 new_role = old_role;
1541 HMI_DEBUG("wm", "old:%s -> new:%s", old_role, new_role);
1546 int WindowManager::loadOldRoleDb()
1548 // Get afm application installed dir
1549 char const *afm_app_install_dir = getenv("AFM_APP_INSTALL_DIR");
1550 HMI_DEBUG("wm", "afm_app_install_dir:%s", afm_app_install_dir);
1552 std::string file_name;
1553 if (!afm_app_install_dir)
1555 HMI_ERROR("wm", "AFM_APP_INSTALL_DIR is not defined");
1559 file_name = std::string(afm_app_install_dir) + std::string("/etc/old_roles.db");
1563 json_object* json_obj;
1564 int ret = jh::inputJsonFilie(file_name.c_str(), &json_obj);
1567 HMI_ERROR("wm", "Could not open old_role.db, so use default old_role information");
1568 json_obj = json_tokener_parse(kDefaultOldRoleDb);
1570 HMI_DEBUG("wm", "json_obj dump:%s", json_object_get_string(json_obj));
1573 json_object* json_cfg;
1574 if (!json_object_object_get_ex(json_obj, "old_roles", &json_cfg))
1576 HMI_ERROR("wm", "Parse Error!!");
1580 int len = json_object_array_length(json_cfg);
1581 HMI_DEBUG("wm", "json_cfg len:%d", len);
1582 HMI_DEBUG("wm", "json_cfg dump:%s", json_object_get_string(json_cfg));
1584 for (int i=0; i<len; i++)
1586 json_object* json_tmp = json_object_array_get_idx(json_cfg, i);
1588 const char* old_role = jh::getStringFromJson(json_tmp, "name");
1589 if (nullptr == old_role)
1591 HMI_ERROR("wm", "Parse Error!!");
1595 const char* new_role = jh::getStringFromJson(json_tmp, "new");
1596 if (nullptr == new_role)
1598 HMI_ERROR("wm", "Parse Error!!");
1602 this->roleold2new[old_role] = std::string(new_role);
1606 for(auto itr = this->roleold2new.begin();
1607 itr != this->roleold2new.end(); ++itr)
1609 HMI_DEBUG("wm", ">>> role old:%s new:%s",
1610 itr->first.c_str(), itr->second.c_str());
1613 // Release json_object
1614 json_object_put(json_obj);
1619 const char *WindowManager::check_surface_exist(const char *drawing_name)
1621 auto const &surface_id = this->lookup_id(drawing_name);
1624 return "Surface does not exist";
1627 if (!this->controller->surface_exists(*surface_id))
1629 return "Surface does not exist in controller!";
1632 auto layer_id = this->layers.get_layer_id(*surface_id);
1636 return "Surface is not on any layer!";
1639 auto o_state = *this->layers.get_layout_state(*surface_id);
1641 if (o_state == nullptr)
1643 return "Could not find layer for surface";
1646 HMI_DEBUG("wm", "surface %d is detected", *surface_id);
1650 bool WindowManager::can_split(struct LayoutState const &state, int new_id)
1652 if (state.main != -1 && state.main != new_id)
1654 auto new_id_layer = this->layers.get_layer_id(new_id).value();
1655 auto current_id_layer = this->layers.get_layer_id(state.main).value();
1657 // surfaces are on separate layers, don't bother.
1658 if (new_id_layer != current_id_layer)
1663 std::string const &new_id_str = this->lookup_name(new_id).value();
1664 std::string const &cur_id_str = this->lookup_name(state.main).value();
1666 auto const &layer = this->layers.get_layer(new_id_layer);
1668 HMI_DEBUG("wm", "layer info name: %s", layer->name.c_str());
1670 if (layer->layouts.empty())
1675 for (auto i = layer->layouts.cbegin(); i != layer->layouts.cend(); i++)
1677 HMI_DEBUG("wm", "%d main_match '%s'", new_id_layer, i->main_match.c_str());
1678 auto rem = std::regex(i->main_match);
1679 if (std::regex_match(cur_id_str, rem))
1681 // build the second one only if the first already matched
1682 HMI_DEBUG("wm", "%d sub_match '%s'", new_id_layer, i->sub_match.c_str());
1683 auto res = std::regex(i->sub_match);
1684 if (std::regex_match(new_id_str, res))
1686 HMI_DEBUG("wm", "layout matched!");
1696 const char* WindowManager::kDefaultOldRoleDb = "{ \
1699 \"name\": \"HomeScreen\", \
1700 \"new\": \"homescreen\" \
1703 \"name\": \"Music\", \
1704 \"new\": \"music\" \
1707 \"name\": \"MediaPlayer\", \
1708 \"new\": \"music\" \
1711 \"name\": \"Video\", \
1712 \"new\": \"video\" \
1715 \"name\": \"VideoPlayer\", \
1716 \"new\": \"video\" \
1719 \"name\": \"WebBrowser\", \
1720 \"new\": \"browser\" \
1723 \"name\": \"Radio\", \
1724 \"new\": \"radio\" \
1727 \"name\": \"Phone\", \
1728 \"new\": \"phone\" \
1731 \"name\": \"Navigation\", \
1735 \"name\": \"HVAC\", \
1739 \"name\": \"Settings\", \
1740 \"new\": \"settings\" \
1743 \"name\": \"Dashboard\", \
1744 \"new\": \"dashboard\" \
1747 \"name\": \"POI\", \
1751 \"name\": \"Mixer\", \
1752 \"new\": \"mixer\" \
1755 \"name\": \"Restriction\", \
1756 \"new\": \"restriction\" \
1759 \"name\": \"^OnScreen.*\", \
1760 \"new\": \"on_screen\" \
1768 void controller_hooks::surface_created(uint32_t surface_id)
1770 this->app->surface_created(surface_id);
1773 void controller_hooks::surface_removed(uint32_t surface_id)
1775 this->app->surface_removed(surface_id);
1778 void controller_hooks::surface_visibility(uint32_t /*surface_id*/,
1781 void controller_hooks::surface_destination_rectangle(uint32_t /*surface_id*/,