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.
18 #include "json_helper.hpp"
22 #include "wayland_ivi_wm.hpp"
29 #include <json-c/json.h>
38 #include "wm_client.hpp"
39 #include "applist.hpp"
43 #include <systemd/sd-event.h>
49 const unsigned kTimeOut = 10000000UL; /* 10s */
51 /* DrawingArea name used by "{layout}.{area}" */
52 const char kNameLayoutNormal[] = "normal";
53 const char kNameLayoutSplit[] = "split";
54 const char kNameAreaFull[] = "full";
55 const char kNameAreaMain[] = "main";
56 const char kNameAreaSub[] = "sub";
58 /* Key for json obejct */
59 const char kKeyDrawingName[] = "drawing_name";
60 const char kKeyDrawingArea[] = "drawing_area";
61 const char kKeyDrawingRect[] = "drawing_rect";
62 const char kKeyX[] = "x";
63 const char kKeyY[] = "y";
64 const char kKeyWidth[] = "width";
65 const char kKeyHeight[] = "height";
66 const char kKeyWidthPixel[] = "width_pixel";
67 const char kKeyHeightPixel[] = "height_pixel";
68 const char kKeyWidthMm[] = "width_mm";
69 const char kKeyHeightMm[] = "height_mm";
71 static sd_event_source *g_timer_ev_src = nullptr;
73 static AppList g_app_list;
80 result<json> file_to_json(char const *filename)
83 std::ifstream i(filename);
86 HMI_DEBUG("wm", "Could not open config file, so use default layer information");
87 j = default_layers_json;
97 struct result<layer_map> load_layer_map(char const *filename)
99 HMI_DEBUG("wm", "loading IDs from %s", filename);
101 auto j = file_to_json(filename);
104 return Err<layer_map>(j.unwrap_err());
106 json jids = j.unwrap();
108 return to_layer_map(jids);
112 processTimerHandler(sd_event_source *s, uint64_t usec, void *userdata)
114 HMI_NOTICE("wm", "Time out occurs because the client replys endDraw slow, so revert the request");
115 reinterpret_cast<wm::App *>(userdata)->timerHandler();
121 void App::timerHandler()
123 unsigned req_num = g_app_list.currentRequestNumber();
124 HMI_SEQ_DEBUG(req_num, "Timer expired remove Request");
125 g_app_list.reqDump();
126 g_app_list.removeRequest(req_num);
127 this->processNextRequest();
130 void App::removeClient(const std::string &appid)
132 HMI_DEBUG("wm", "Remove clinet %s from list", appid.c_str());
133 g_app_list.removeClient(appid);
136 bool App::subscribeEventForApp(const std::string &appid, afb_req req, const std::string &evname)
138 if(!g_app_list.contains(appid)){
139 HMI_DEBUG("wm", "Client %s is not registered", appid.c_str());
142 auto client = g_app_list.lookUpClient(appid);
143 return client->subscribe(req, evname);
149 App::App(wl::display *d)
157 pending_events(false),
163 auto l = load_layer_map(
164 this->config.get_string("layers.json").value().c_str());
167 this->layers = l.unwrap();
171 HMI_ERROR("wm", "%s", l.err().value());
175 catch (std::exception &e)
177 HMI_ERROR("wm", "Loading of configuration failed: %s", e.what());
183 if (!this->display->ok())
188 if (this->layers.mapping.empty())
190 HMI_ERROR("wm", "No surface -> layer mapping loaded");
195 for (int i = Event_Val_Min; i <= Event_Val_Max; i++)
197 map_afb_event[kListEventName[i]] = afb_daemon_make_event(kListEventName[i]);
200 this->display->add_global_handler(
201 "wl_output", [this](wl_registry *r, uint32_t name, uint32_t v) {
202 this->outputs.emplace_back(std::make_unique<wl::output>(r, name, v));
205 this->display->add_global_handler(
206 "ivi_wm", [this](wl_registry *r, uint32_t name, uint32_t v) {
208 std::make_unique<struct compositor::controller>(r, name, v);
210 // Init controller hooks
211 this->controller->chooks = &this->chooks;
213 // This protocol needs the output, so lets just add our mapping here...
214 this->controller->add_proxy_to_id_mapping(
215 this->outputs.back()->proxy.get(),
216 wl_proxy_get_id(reinterpret_cast<struct wl_proxy *>(
217 this->outputs.back()->proxy.get())));
220 this->controller->create_screen(this->outputs.back()->proxy.get());
222 // Set display to controller
223 this->controller->display = this->display;
226 // First level objects
227 this->display->roundtrip();
228 // Second level objects
229 this->display->roundtrip();
230 // Third level objects
231 this->display->roundtrip();
233 return init_layers();
236 int App::dispatch_pending_events()
238 if (this->pop_pending_events())
240 this->display->dispatch_pending();
246 bool App::pop_pending_events()
249 return this->pending_events.compare_exchange_strong(
250 x, false, std::memory_order_consume);
253 void App::set_pending_events()
255 this->pending_events.store(true, std::memory_order_release);
258 optional<int> App::lookup_id(char const *name)
260 return this->id_alloc.lookup(std::string(name));
262 optional<std::string> App::lookup_name(int id)
264 return this->id_alloc.lookup(id);
270 int App::init_layers()
272 if (!this->controller)
274 HMI_ERROR("wm", "ivi_controller global not available");
278 if (this->outputs.empty())
280 HMI_ERROR("wm", "no output was set up!");
284 auto &c = this->controller;
286 auto &o = this->outputs.front();
287 auto &s = c->screens.begin()->second;
288 auto &layers = c->layers;
290 // Write output dimensions to ivi controller...
291 c->output_size = compositor::size{uint32_t(o->width), uint32_t(o->height)};
292 c->physical_size = compositor::size{uint32_t(o->physical_width),
293 uint32_t(o->physical_height)};
301 // Quick and dirty setup of layers
302 for (auto const &i : this->layers.mapping)
304 c->layer_create(i.second.layer_id, o->width, o->height);
305 auto &l = layers[i.second.layer_id];
306 l->set_destination_rectangle(0, 0, o->width, o->height);
307 l->set_visibility(1);
308 HMI_DEBUG("wm", "Setting up layer %s (%d) for surface role match \"%s\"",
309 i.second.name.c_str(), i.second.layer_id, i.second.role.c_str());
312 // Add layers to screen
313 s->set_render_order(this->layers.layers);
315 this->layout_commit();
317 this->layers.setupArea(o->width, o->height);
322 void App::surface_set_layout(int surface_id, const std::string& area)
324 if (!this->controller->surface_exists(surface_id))
326 HMI_ERROR("wm", "Surface %d does not exist", surface_id);
330 auto o_layer_id = this->layers.get_layer_id(surface_id);
334 HMI_ERROR("wm", "Surface %d is not associated with any layer!", surface_id);
338 uint32_t layer_id = *o_layer_id;
340 auto const &layer = this->layers.get_layer(layer_id);
341 auto rect = this->layers.getAreaSize(area);
342 auto &s = this->controller->surfaces[surface_id];
349 // less-than-0 values refer to MAX + 1 - $VALUE
350 // e.g. MAX is either screen width or height
353 w = this->controller->output_size.w + 1 + w;
357 h = this->controller->output_size.h + 1 + h;
360 HMI_DEBUG("wm", "surface_set_layout for surface %u on layer %u", surface_id,
363 // set destination to the display rectangle
364 s->set_destination_rectangle(x, y, w, h);
366 // update area information
367 this->area_info[surface_id].x = x;
368 this->area_info[surface_id].y = y;
369 this->area_info[surface_id].w = w;
370 this->area_info[surface_id].h = h;
372 HMI_DEBUG("wm", "Surface %u now on layer %u with rect { %d, %d, %d, %d }",
373 surface_id, layer_id, x, y, w, h);
376 void App::layout_commit()
378 this->controller->commit_changes();
379 this->display->flush();
384 HMI_SEQ_DEBUG(g_app_list.currentRequestNumber(), "Timer set activate");
385 if (g_timer_ev_src == nullptr)
387 // firsttime set into sd_event
388 int ret = sd_event_add_time(afb_daemon_get_event_loop(), &g_timer_ev_src,
389 CLOCK_REALTIME, time(NULL) * (1000000UL) + kTimeOut, 1, processTimerHandler, this);
392 HMI_ERROR("wm", "Could't set timer");
397 // update timer limitation after second time
398 sd_event_source_set_time(g_timer_ev_src, time(NULL) * (1000000UL) + kTimeOut);
399 sd_event_source_set_enabled(g_timer_ev_src, SD_EVENT_ONESHOT);
403 void App::stopTimer()
405 unsigned req_num = g_app_list.currentRequestNumber();
406 HMI_SEQ_DEBUG(req_num, "Timer stop");
407 int rc = sd_event_source_set_enabled(g_timer_ev_src, SD_EVENT_OFF);
410 HMI_SEQ_ERROR(req_num, "Timer stop failed");
414 WMError App::lm_release(const struct WMAction &action)
416 //auto const &surface_id = this->lookup_id(drawing_name);
417 WMError ret = WMError::LAYOUT_CHANGE_FAIL;
418 unsigned req_num = g_app_list.currentRequestNumber();
419 auto const &surface_id = this->lookup_id(action.role.c_str());
422 HMI_SEQ_ERROR(req_num, "Surface does not exist");
426 if (*surface_id == this->layers.main_surface)
428 HMI_SEQ_ERROR(req_num, "Cannot deactivate main_surface");
432 auto o_state = *this->layers.get_layout_state(*surface_id);
434 if (o_state == nullptr)
436 HMI_SEQ_ERROR(req_num, "Could not find layer for surface");
440 struct LayoutState &state = *o_state;
442 if (state.main == -1)
444 HMI_SEQ_ERROR(req_num, "No surface active");
448 // Check against main_surface, main_surface_name is the configuration item.
449 if (*surface_id == this->layers.main_surface)
451 HMI_SEQ_DEBUG(req_num, "Refusing to deactivate main_surface %d", *surface_id);
453 return WMError::SUCCESS;
455 if ((state.main == *surface_id) && (state.sub == *surface_id))
457 HMI_SEQ_ERROR(req_num, "Surface is not active");
461 if (state.main == *surface_id)
466 state, LayoutState{state.sub, -1}, [&](LayoutState const &nl) {
467 std::string sub = std::move(*this->lookup_name(state.sub));
469 this->deactivate(*surface_id);
470 this->surface_set_layout(state.sub);
473 this->layout_commit();
474 std::string str_area = std::string(kNameLayoutNormal) + "." + std::string(kNameAreaFull);
475 compositor::rect area_rect = this->area_info[state.sub];
476 this->emit_syncdraw(sub.c_str(), str_area.c_str(),
477 area_rect.x, area_rect.y, area_rect.w, area_rect.h);
478 this->enqueue_flushdraw(state.sub);
483 this->try_layout(state, LayoutState{-1, -1}, [&](LayoutState const &nl) {
484 this->deactivate(*surface_id);
486 this->layout_commit();
490 else if (state.sub == *surface_id)
493 state, LayoutState{state.main, -1}, [&](LayoutState const &nl) {
494 std::string main = std::move(*this->lookup_name(state.main));
496 this->deactivate(*surface_id);
497 this->surface_set_layout(state.main);
500 this->layout_commit();
501 std::string str_area = std::string(kNameLayoutNormal) + "." + std::string(kNameAreaFull);
502 compositor::rect area_rect = this->area_info[state.main];
503 this->emit_syncdraw(main.c_str(), str_area.c_str(),
504 area_rect.x, area_rect.y, area_rect.w, area_rect.h);
505 this->enqueue_flushdraw(state.main);
508 return WMError::SUCCESS;
511 WMError App::doTransition(unsigned req_num)
513 HMI_SEQ_DEBUG(req_num, "check policy");
514 WMError ret = this->checkPolicy(req_num);
515 if (ret != WMError::SUCCESS)
519 HMI_SEQ_DEBUG(req_num, "Start transition.");
520 ret = this->startTransition(req_num);
524 WMError App::checkPolicy(unsigned req_num)
529 // get current trigger
532 WMError ret = WMError::LAYOUT_CHANGE_FAIL;
533 auto trigger = g_app_list.getRequest(req_num, &found);
536 ret = WMError::NO_ENTRY;
539 std::string req_area = trigger.area;
541 // >>>> Compatible with current window manager until policy manager coming
542 if (trigger.task == Task::TASK_ALLOCATE)
544 HMI_SEQ_DEBUG(req_num, "Check split or not");
545 const char *msg = this->check_surface_exist(trigger.role.c_str());
549 HMI_SEQ_ERROR(req_num, msg);
550 ret = WMError::LAYOUT_CHANGE_FAIL;
554 auto const &surface_id = this->lookup_id(trigger.role.c_str());
555 auto o_state = *this->layers.get_layout_state(*surface_id);
556 struct LayoutState &state = *o_state;
558 unsigned curernt_sid = state.main;
559 split = this->can_split(state, *surface_id);
563 HMI_SEQ_DEBUG(req_num, "Split happens");
564 // Get current visible role
565 std::string add_role = this->lookup_name(state.main).value();
567 std::string add_area = std::string(kNameLayoutSplit) + "." + std::string(kNameAreaMain);
568 // Change request area
569 req_area = std::string(kNameLayoutSplit) + "." + std::string(kNameAreaSub);
570 HMI_SEQ_NOTICE(req_num, "Change request area from %s to %s, because split is happen", trigger.area, req_area);
571 // set another action
572 std::string add_name = g_app_list.getAppID(curernt_sid, add_role, &found);
575 HMI_SEQ_ERROR(req_num, "Couldn't widhdraw with surfaceID : %s", curernt_sid);
576 ret = WMError::NOT_REGISTERED;
579 HMI_SEQ_INFO(req_num, "Additional split app %s, role: %s, area: %s",
580 add_name.c_str(), add_role.c_str(), add_area.c_str());
582 bool end_draw_finished = false;
583 WMAction split_action{
587 TaskVisible::VISIBLE,
589 WMError ret = g_app_list.setAction(req_num, split_action);
590 if (ret != WMError::SUCCESS)
592 HMI_SEQ_ERROR(req_num, "Failed to set action");
595 g_app_list.reqDump();
600 HMI_SEQ_DEBUG(req_num, "split doesn't happen");
603 // Set invisible task(Remove if policy manager finish)
604 ret = this->setInvisibleTask(trigger.role, split);
605 if(ret != WMError::SUCCESS)
607 HMI_SEQ_ERROR(req_num, "Failed to set invisible task: %s", errorDescription(ret));
611 /* get new status from Policy Manager */
612 HMI_SEQ_NOTICE(req_num, "ATM, Policy manager does't exist, then set WMAction as is");
613 TaskVisible task_visible = (trigger.task == Task::TASK_ALLOCATE) ? TaskVisible::VISIBLE : TaskVisible::INVISIBLE;
615 ret = g_app_list.setAction(req_num, trigger.appid, trigger.role, req_area, task_visible);
616 g_app_list.reqDump();
621 WMError App::startTransition(unsigned req_num)
623 bool sync_draw_happen = false;
625 WMError ret = WMError::SUCCESS;
626 auto actions = g_app_list.getActions(req_num, &found);
629 ret = WMError::NO_ENTRY;
630 HMI_SEQ_ERROR(req_num, "Window Manager bug :%s : Action is not set", errorDescription(ret));
634 for (const auto &y : actions)
636 if (y.visible != TaskVisible::INVISIBLE)
638 sync_draw_happen = true;
639 this->emit_syncdraw(y.role, y.area);
640 /* TODO: emit event for app not subscriber
641 if(g_app_list.contains(y.appid))
642 g_app_list.lookUpClient(y.appid)->emit_syncdraw(y.role, y.area); */
646 if (sync_draw_happen)
652 // deactivate only, no syncDraw
653 // Make it deactivate here
654 for (const auto &z : actions)
656 if (g_app_list.contains(z.appid))
658 auto client = g_app_list.lookUpClient(z.appid);
659 this->deactivate(client->surfaceID(z.role));
662 ret = NO_LAYOUT_CHANGE;
667 WMError App::setInvisibleTask(const std::string &role, bool split)
669 unsigned req = g_app_list.currentRequestNumber();
670 HMI_SEQ_DEBUG(req, "set current visible app to invisible task");
671 // This task is copied from original actiavete surface
672 const char *drawing_name = role.c_str();
673 auto const &surface_id = this->lookup_id(drawing_name);
674 auto layer_id = this->layers.get_layer_id(*surface_id);
675 auto o_state = *this->layers.get_layout_state(*surface_id);
676 struct LayoutState &state = *o_state;
677 std::string add_name, add_role;
678 std::string add_area = "";
680 TaskVisible task_visible = TaskVisible::INVISIBLE;
681 bool end_draw_finished = true;
684 for (auto const &l : this->layers.mapping)
686 if (l.second.layer_id <= *layer_id)
690 if (l.second.state.main != -1)
692 //this->deactivate(l.second.state.main);
693 surface = l.second.state.main;
694 add_role = *this->id_alloc.lookup(surface);
695 add_name = g_app_list.getAppID(surface, add_role, &found);
697 return WMError::NOT_REGISTERED;
699 HMI_SEQ_INFO(req, "Invisible %s", add_name.c_str());
700 WMAction act{add_name, add_role, add_area, task_visible, end_draw_finished};
701 g_app_list.setAction(req, act);
702 l.second.state.main = -1;
705 if (l.second.state.sub != -1)
707 //this->deactivate(l.second.state.sub);
708 surface = l.second.state.sub;
709 add_role = *this->id_alloc.lookup(surface);
710 add_name = g_app_list.getAppID(surface, add_role, &found);
713 return WMError::NOT_REGISTERED;
715 HMI_SEQ_INFO(req, "Invisible %s", add_name.c_str());
716 WMAction act{add_name, add_role, add_area, task_visible, end_draw_finished};
717 g_app_list.setAction(req, act);
718 l.second.state.sub = -1;
722 // change current state here, but this is hack
723 auto layer = this->layers.get_layer(*layer_id);
725 if (state.main == -1)
727 HMI_DEBUG("wm", "Layout: %s", kNameLayoutNormal);
728 //state = LayoutState{*surface_id};
730 state, LayoutState{*surface_id}, [&](LayoutState const &nl) {
731 HMI_DEBUG("wm", "Layout: %s", kNameLayoutNormal);
732 //this->surface_set_layout(*surface_id);
735 //compositor::rect area_rect = this->area_info[*surface_id];
740 if (0 != strcmp(drawing_name, "HomeScreen"))
745 std::move(*this->id_alloc.lookup(state.main));
746 if (state.sub != *surface_id)
750 //this->deactivate(state.sub);
752 deact_sub.role = main;
753 deact_sub.area = add_area;
754 deact_sub.appid = g_app_list.getAppID(state.sub, main, &found);
757 HMI_SEQ_DEBUG(req, "sub surface doesn't exist");
758 return WMError::NOT_REGISTERED;
760 deact_sub.visible = task_visible;
761 deact_sub.end_draw_finished = end_draw_finished;
762 HMI_SEQ_DEBUG(req, "sub surface doesn't exist");
763 g_app_list.setAction(req, deact_sub);
766 //state = LayoutState{state.main, *surface_id};
770 HMI_DEBUG("wm", "Layout: %s", kNameLayoutNormal);
772 //this->surface_set_layout(*surface_id);
773 if (state.main != *surface_id)
775 // this->deactivate(state.main);
777 deact_main.role = std::move(*this->id_alloc.lookup(state.main));
779 deact_main.area = add_area;
780 deact_main.appid = g_app_list.getAppID(state.main, deact_main.role, &found);
783 HMI_SEQ_DEBUG(req, "sub surface ddoesn't exist");
784 return WMError::NOT_REGISTERED;
786 deact_main.visible = task_visible;
787 deact_main.end_draw_finished = end_draw_finished;
788 HMI_SEQ_DEBUG(req, "sub surface doesn't exist");
789 g_app_list.setAction(req, deact_main);
793 if (state.sub != *surface_id)
795 //this->deactivate(state.sub);
797 deact_sub.role = std::move(*this->id_alloc.lookup(state.sub));
799 deact_sub.area = add_area;
800 deact_sub.appid = g_app_list.getAppID(state.sub, deact_sub.role, &found);
803 HMI_SEQ_DEBUG(req, "sub surface ddoesn't exist");
804 return WMError::NOT_REGISTERED;
806 deact_sub.visible = task_visible;
807 deact_sub.end_draw_finished = end_draw_finished;
808 HMI_SEQ_DEBUG(req, "sub surface doesn't exist");
809 g_app_list.setAction(req, deact_sub);
812 //state = LayoutState{*surface_id};
816 return WMError::SUCCESS;
819 const char *App::check_surface_exist(const char *drawing_name)
821 auto const &surface_id = this->lookup_id(drawing_name);
824 //reply("Surface does not exist");
825 return "Surface does not exist";
828 if (!this->controller->surface_exists(*surface_id))
830 //reply("Surface does not exist in controller!");
831 return "Surface does not exist in controller!";
834 auto layer_id = this->layers.get_layer_id(*surface_id);
838 //reply("Surface is not on any layer!");
839 return "Surface is not on any layer!";
842 auto o_state = *this->layers.get_layout_state(*surface_id);
844 if (o_state == nullptr)
846 //reply("Could not find layer for surface");
847 return "Could not find layer for surface";
850 HMI_DEBUG("wm", "surface %d is detected", *surface_id);
855 WMError App::setRequest(const std::string& appid, const std::string &role, const std::string &area,
856 Task task, unsigned* req_num)
858 if (!g_app_list.contains(appid))
860 return WMError::NOT_REGISTERED;
863 auto client = g_app_list.lookUpClient(appid);
868 unsigned current = g_app_list.currentRequestNumber();
869 unsigned requested_num = g_app_list.getRequestNumber(appid);
870 if (requested_num != 0)
872 HMI_SEQ_INFO(requested_num, "%s %s %s request is already queued", appid.c_str(), role.c_str(), area.c_str());
876 WMRequest req = WMRequest(appid, role, area, task);
877 unsigned new_req = g_app_list.addAllocateRequest(req);
879 g_app_list.reqDump();
881 HMI_SEQ_DEBUG(current, "%s start sequence with %s, %s", appid.c_str(), role.c_str(), area.c_str());
883 return WMError::SUCCESS;
886 void App::api_activate_surface(char const *appid, char const *drawing_name, char const *drawing_area, const reply_func &reply)
890 std::string id = appid;
891 std::string role = drawing_name;
892 std::string area = drawing_area;
893 Task task = Task::TASK_ALLOCATE;
894 unsigned req_num = 0;
895 WMError ret = WMError::UNKNOWN;
897 ret = this->setRequest(id, role, area, task, &req_num);
899 if(ret != WMError::SUCCESS)
901 HMI_ERROR("wm", errorDescription(ret));
902 reply("Failed to set request");
907 if (req_num != g_app_list.currentRequestNumber())
909 // Add request, then invoked after the previous task is finished
910 HMI_SEQ_DEBUG(req_num, "request is accepted");
917 ret = this->doTransition(req_num);
919 if (ret != WMError::SUCCESS)
922 HMI_SEQ_ERROR(req_num, errorDescription(ret));
923 g_app_list.removeRequest(req_num);
925 g_app_list.reqDump();
929 void App::api_deactivate_surface(char const *appid, char const *drawing_name, const reply_func &reply)
936 std::string id = appid;
937 std::string role = drawing_name;
938 std::string area = ""; //drawing_area;
940 if (!g_app_list.contains(id))
942 reply("app doesn't request 'requestSurface' yet");
945 auto client = g_app_list.lookUpClient(id);
950 unsigned current = g_app_list.currentRequestNumber();
951 unsigned requested_num = g_app_list.getRequestNumber(id);
952 if (requested_num != 0)
954 HMI_SEQ_INFO(requested_num, "%s %s %s request is already queued", id.c_str(), role.c_str(), area.c_str());
955 reply("already requested");
959 WMRequest req = WMRequest(id, role, area, Task::TASK_RELEASE);
960 unsigned new_req = g_app_list.addAllocateRequest(req);
961 g_app_list.reqDump();
963 HMI_SEQ_DEBUG(current, "%s start sequence with %s, %s", id.c_str(), role.c_str(), area.c_str());
966 if (new_req != current)
968 // Add request, then invoked after the previous task is finished
969 HMI_SEQ_DEBUG(new_req, "request is accepted");
976 WMError ret = this->doTransition(new_req);
978 if (ret != WMError::SUCCESS)
980 HMI_SEQ_ERROR(new_req, errorDescription(ret));
981 g_app_list.removeRequest(new_req);
987 void App::enqueue_flushdraw(int surface_id)
989 this->check_flushdraw(surface_id);
990 HMI_DEBUG("wm", "Enqueuing EndDraw for surface_id %d", surface_id);
991 this->pending_end_draw.push_back(surface_id);
994 void App::check_flushdraw(int surface_id)
996 auto i = std::find(std::begin(this->pending_end_draw),
997 std::end(this->pending_end_draw), surface_id);
998 if (i != std::end(this->pending_end_draw))
1000 auto n = this->lookup_name(surface_id);
1001 HMI_ERROR("wm", "Application %s (%d) has pending EndDraw call(s)!",
1002 n ? n->c_str() : "unknown-name", surface_id);
1003 std::swap(this->pending_end_draw[std::distance(
1004 std::begin(this->pending_end_draw), i)],
1005 this->pending_end_draw.back());
1006 this->pending_end_draw.resize(this->pending_end_draw.size() - 1);
1010 WMError App::doEndDraw(unsigned req_num)
1014 auto actions = g_app_list.getActions(req_num, &found);
1015 WMError ret = WMError::SUCCESS;
1018 ret = WMError::NO_ENTRY;
1022 HMI_SEQ_INFO(req_num, "do endDraw");
1024 // layout change and make it visible
1025 for (const auto &act : actions)
1028 if(!g_app_list.contains(act.appid)){
1029 ret = WMError::NOT_REGISTERED;
1031 ret = this->layoutChange(act);
1032 if(ret != WMError::SUCCESS)
1034 HMI_SEQ_WARNING(req_num, "Failed to manipulate surfaces while state change : %s", errorDescription(ret));
1037 ret = this->visibilityChange(act);
1038 if (ret != WMError::SUCCESS)
1040 HMI_SEQ_WARNING(req_num, "Failed to manipulate surfaces while state change : %s", errorDescription(ret));
1043 HMI_SEQ_DEBUG(req_num, "visible %s", act.role.c_str());
1044 //this->lm_enddraw(act.role.c_str());
1047 // Change current state
1048 this->changeCurrentState(req_num);
1050 HMI_SEQ_INFO(req_num, "emit flushDraw");
1052 for(const auto &act_flush : actions)
1054 if(act_flush.visible != TaskVisible::INVISIBLE)
1056 this->emit_flushdraw(act_flush.role.c_str());
1063 WMError App::setSurfaceSize(unsigned surface, const std::string &area)
1065 this->surface_set_layout(surface, area);
1067 return WMError::SUCCESS;
1070 WMError App::layoutChange(const WMAction &action)
1072 if (action.visible == TaskVisible::INVISIBLE)
1074 // Visibility is not change -> no redraw is required
1075 return WMError::SUCCESS;
1077 auto client = g_app_list.lookUpClient(action.appid);
1078 unsigned surface = client->surfaceID(action.role);
1081 HMI_SEQ_ERROR(g_app_list.currentRequestNumber(),
1082 "client doesn't have surface with role(%s)", action.role.c_str());
1083 return WMError::NOT_REGISTERED;
1086 WMError ret = this->setSurfaceSize(surface, action.area);
1090 WMError App::visibilityChange(const WMAction &action)
1092 HMI_SEQ_DEBUG(g_app_list.currentRequestNumber(), "Change visibility");
1093 if(!g_app_list.contains(action.appid)){
1094 return WMError::NOT_REGISTERED;
1096 auto client = g_app_list.lookUpClient(action.appid);
1097 unsigned surface = client->surfaceID(action.role);
1100 HMI_SEQ_ERROR(g_app_list.currentRequestNumber(),
1101 "client doesn't have surface with role(%s)", action.role.c_str());
1102 return WMError::NOT_REGISTERED;
1105 if (action.visible != TaskVisible::INVISIBLE)
1107 this->activate(surface); // Layout Manager task
1111 this->deactivate(surface); // Layout Manager task
1113 return WMError::SUCCESS;
1116 WMError App::changeCurrentState(unsigned req_num)
1118 HMI_SEQ_DEBUG(req_num, "Change current layout state");
1119 bool trigger_found = false, action_found = false;
1120 auto trigger = g_app_list.getRequest(req_num, &trigger_found);
1121 auto actions = g_app_list.getActions(req_num, &action_found);
1122 if (!trigger_found || !action_found)
1124 HMI_SEQ_ERROR(req_num, "Action not found");
1125 return WMError::LAYOUT_CHANGE_FAIL;
1128 // Layout state reset
1129 struct LayoutState reset_state{-1, -1};
1130 HMI_SEQ_DEBUG(req_num,"Reset layout state");
1131 for (const auto &action : actions)
1133 if(!g_app_list.contains(action.appid)){
1134 return WMError::NOT_REGISTERED;
1136 auto client = g_app_list.lookUpClient(trigger.appid);
1137 auto pCurState = *this->layers.get_layout_state((int)client->surfaceID(trigger.role));
1138 if(pCurState == nullptr)
1140 HMI_SEQ_ERROR(req_num, "Counldn't find current status");
1143 pCurState->main = reset_state.main;
1144 pCurState->sub = reset_state.sub;
1147 HMI_SEQ_DEBUG(req_num, "Change state");
1148 for (const auto &action : actions)
1150 auto client = g_app_list.lookUpClient(action.appid);
1151 auto pLayerCurState = *this->layers.get_layout_state((int)client->surfaceID(action.role));
1152 if (pLayerCurState == nullptr)
1154 HMI_SEQ_ERROR(req_num, "Counldn't find current status");
1159 if (action.visible != TaskVisible::INVISIBLE)
1161 surface = (int)client->surfaceID(action.role);
1162 HMI_SEQ_INFO(req_num, "Change %s state visible area : %s", action.role.c_str(), action.area.c_str());
1163 // visible == true -> layout changes
1164 if(action.area == "normal.full" || action.area == "split.main")
1166 pLayerCurState->main = surface;
1168 else if(action.role == "split.sub")
1170 pLayerCurState->sub = surface;
1175 pLayerCurState->main = surface;
1180 return WMError::SUCCESS;
1183 void App::api_enddraw(char const *appid, char const *drawing_name)
1185 std::string id = appid;
1186 std::string role = drawing_name;
1187 unsigned current_req = g_app_list.currentRequestNumber();
1188 bool result = g_app_list.setEndDrawFinished(current_req, id, role);
1192 HMI_ERROR("wm", "%s is not in transition state", id.c_str());
1196 if (g_app_list.endDrawFullfilled(current_req))
1198 // do task for endDraw
1200 WMError ret = this->doEndDraw(current_req);
1202 if(ret != WMError::SUCCESS)
1204 //this->emit_error();
1206 HMI_SEQ_INFO(current_req, "Finish request status: %s", errorDescription(ret));
1208 g_app_list.removeRequest(current_req);
1210 this->processNextRequest();
1214 HMI_SEQ_INFO(current_req, "Wait other App call endDraw");
1219 void App::processNextRequest()
1222 g_app_list.reqDump();
1223 unsigned req_num = g_app_list.currentRequestNumber();
1224 if (g_app_list.haveRequest())
1226 HMI_SEQ_DEBUG(req_num, "Process next request");
1227 WMError rc = doTransition(req_num);
1228 if (rc != WMError::SUCCESS)
1230 HMI_SEQ_ERROR(req_num, errorDescription(rc));
1235 HMI_SEQ_DEBUG(req_num, "Nothing Request. Waiting Request");
1239 void App::api_ping() { this->dispatch_pending_events(); }
1241 void App::send_event(char const *evname, char const *label)
1243 HMI_DEBUG("wm", "%s: %s(%s)", __func__, evname, label);
1245 json_object *j = json_object_new_object();
1246 json_object_object_add(j, kKeyDrawingName, json_object_new_string(label));
1248 int ret = afb_event_push(this->map_afb_event[evname], j);
1251 HMI_DEBUG("wm", "afb_event_push failed: %m");
1255 void App::send_event(char const *evname, char const *label, char const *area,
1256 int x, int y, int w, int h)
1258 HMI_DEBUG("wm", "%s: %s(%s, %s) x:%d y:%d w:%d h:%d",
1259 __func__, evname, label, area, x, y, w, h);
1261 json_object *j_rect = json_object_new_object();
1262 json_object_object_add(j_rect, kKeyX, json_object_new_int(x));
1263 json_object_object_add(j_rect, kKeyY, json_object_new_int(y));
1264 json_object_object_add(j_rect, kKeyWidth, json_object_new_int(w));
1265 json_object_object_add(j_rect, kKeyHeight, json_object_new_int(h));
1267 json_object *j = json_object_new_object();
1268 json_object_object_add(j, kKeyDrawingName, json_object_new_string(label));
1269 json_object_object_add(j, kKeyDrawingArea, json_object_new_string(area));
1270 json_object_object_add(j, kKeyDrawingRect, j_rect);
1272 int ret = afb_event_push(this->map_afb_event[evname], j);
1275 HMI_DEBUG("wm", "afb_event_push failed: %m");
1282 void App::surface_created(uint32_t surface_id)
1284 auto layer_id = this->layers.get_layer_id(surface_id);
1287 HMI_DEBUG("wm", "Newly created surfce %d is not associated with any layer!",
1292 HMI_DEBUG("wm", "surface_id is %u, layer_id is %u", surface_id, *layer_id);
1294 this->controller->layers[*layer_id]->add_surface(surface_id);
1295 this->layout_commit();
1296 // activate the main_surface right away
1297 /*if (surface_id == static_cast<unsigned>(this->layers.main_surface)) {
1298 HMI_DEBUG("wm", "Activating main_surface (%d)", surface_id);
1300 this->api_activate_surface(
1301 this->lookup_name(surface_id).value_or("unknown-name").c_str());
1304 // search pid from surfaceID
1306 // pick up appid from pid from application manager
1308 // check appid then add it to the client
1311 void App::surface_removed(uint32_t surface_id)
1313 HMI_DEBUG("wm", "surface_id is %u", surface_id);
1315 g_app_list.removeSurface(surface_id);
1318 void App::emit_activated(char const *label)
1320 this->send_event(kListEventName[Event_Active], label);
1323 void App::emit_deactivated(char const *label)
1325 this->send_event(kListEventName[Event_Inactive], label);
1328 void App::emit_syncdraw(char const *label, char const *area, int x, int y, int w, int h)
1330 this->send_event(kListEventName[Event_SyncDraw], label, area, x, y, w, h);
1333 void App::emit_syncdraw(const std::string &role, const std::string &area)
1335 compositor::rect rect = this->layers.getAreaSize(area);
1336 //this->lm_get_area_info(area, &x, &y, &w, &h);
1337 this->send_event(kListEventName[Event_SyncDraw],
1338 role.c_str(), area.c_str(), rect.x, rect.y, rect.w, rect.h);
1341 void App::emit_flushdraw(char const *label)
1343 this->send_event(kListEventName[Event_FlushDraw], label);
1346 void App::emit_visible(char const *label, bool is_visible)
1348 this->send_event(is_visible ? kListEventName[Event_Visible] : kListEventName[Event_Invisible], label);
1351 void App::emit_invisible(char const *label)
1353 return emit_visible(label, false);
1356 void App::emit_visible(char const *label) { return emit_visible(label, true); }
1358 result<int> App::api_request_surface(char const *appid, char const *drawing_name)
1360 auto lid = this->layers.get_layer_id(std::string(drawing_name));
1364 * register drawing_name as fallback and make it displayed.
1366 lid = this->layers.get_layer_id(std::string("Fallback"));
1367 HMI_DEBUG("wm", "%s is not registered in layers.json, then fallback as normal app", drawing_name);
1370 return Err<int>("Drawing name does not match any role, Fallback is disabled");
1374 auto rname = this->lookup_id(drawing_name);
1377 // name does not exist yet, allocate surface id...
1378 auto id = int(this->id_alloc.generate_id(drawing_name));
1379 this->layers.add_surface(id, *lid);
1381 // set the main_surface[_name] here and now
1382 if (!this->layers.main_surface_name.empty() &&
1383 this->layers.main_surface_name == drawing_name)
1385 this->layers.main_surface = id;
1386 HMI_DEBUG("wm", "Set main_surface id to %u", id);
1389 // add client into the db
1390 std::string appid_str(appid);
1391 std::string role(drawing_name);
1392 //g_app_list.addClient(appid_str, role);
1393 g_app_list.addClient(appid_str, *lid, id, role);
1398 // Check currently registered drawing names if it is already there.
1399 return Err<int>("Surface already present");
1402 char const *App::api_request_surface(char const *appid, char const *drawing_name,
1407 auto lid = this->layers.get_layer_id(std::string(drawing_name));
1408 unsigned sid = std::stol(ivi_id);
1413 * register drawing_name as fallback and make it displayed.
1415 lid = this->layers.get_layer_id(std::string("Fallback"));
1416 HMI_DEBUG("wm", "%s is not registered in layers.json, then fallback as normal app", drawing_name);
1419 return "Drawing name does not match any role, Fallback is disabled";
1423 auto rname = this->lookup_id(drawing_name);
1427 return "Surface already present";
1430 // register pair drawing_name and ivi_id
1431 this->id_alloc.register_name_id(drawing_name, sid);
1432 this->layers.add_surface(sid, *lid);
1434 // this surface is already created
1435 HMI_DEBUG("wm", "surface_id is %u, layer_id is %u", sid, *lid);
1437 this->controller->layers[*lid]->add_surface(sid);
1438 this->layout_commit();
1443 result<json_object *> App::api_get_display_info()
1446 if (!this->controller)
1448 return Err<json_object *>("ivi_controller global not available");
1452 compositor::size o_size = this->controller->output_size;
1453 compositor::size p_size = this->controller->physical_size;
1455 json_object *object = json_object_new_object();
1456 json_object_object_add(object, kKeyWidthPixel, json_object_new_int(o_size.w));
1457 json_object_object_add(object, kKeyHeightPixel, json_object_new_int(o_size.h));
1458 json_object_object_add(object, kKeyWidthMm, json_object_new_int(p_size.w));
1459 json_object_object_add(object, kKeyHeightMm, json_object_new_int(p_size.h));
1461 return Ok<json_object *>(object);
1464 result<json_object *> App::api_get_area_info(char const *drawing_name)
1466 HMI_DEBUG("wm", "called");
1468 // Check drawing name, surface/layer id
1469 auto const &surface_id = this->lookup_id(drawing_name);
1472 return Err<json_object *>("Surface does not exist");
1475 if (!this->controller->surface_exists(*surface_id))
1477 return Err<json_object *>("Surface does not exist in controller!");
1480 auto layer_id = this->layers.get_layer_id(*surface_id);
1483 return Err<json_object *>("Surface is not on any layer!");
1486 auto o_state = *this->layers.get_layout_state(*surface_id);
1487 if (o_state == nullptr)
1489 return Err<json_object *>("Could not find layer for surface");
1492 struct LayoutState &state = *o_state;
1493 if ((state.main != *surface_id) && (state.sub != *surface_id))
1495 return Err<json_object *>("Surface is inactive");
1498 // Set area rectangle
1499 compositor::rect area_info = this->area_info[*surface_id];
1500 json_object *object = json_object_new_object();
1501 json_object_object_add(object, kKeyX, json_object_new_int(area_info.x));
1502 json_object_object_add(object, kKeyY, json_object_new_int(area_info.y));
1503 json_object_object_add(object, kKeyWidth, json_object_new_int(area_info.w));
1504 json_object_object_add(object, kKeyHeight, json_object_new_int(area_info.h));
1506 return Ok<json_object *>(object);
1509 void App::activate(int id)
1511 auto ip = this->controller->sprops.find(id);
1512 if (ip != this->controller->sprops.end())
1514 this->controller->surfaces[id]->set_visibility(1);
1516 this->lookup_name(id).value_or("unknown-name").c_str();
1519 if ((0 == strcmp(label, "Radio")) ||
1520 (0 == strcmp(label, "MediaPlayer")) ||
1521 (0 == strcmp(label, "Music")) ||
1522 (0 == strcmp(label, "Navigation")))
1524 for (auto i = surface_bg.begin(); i != surface_bg.end(); ++i)
1529 this->surface_bg.erase(i);
1531 // Remove from BG layer (999)
1532 HMI_DEBUG("wm", "Remove %s(%d) from BG layer", label, id);
1533 this->controller->layers[999]->remove_surface(id);
1535 // Add to FG layer (1001)
1536 HMI_DEBUG("wm", "Add %s(%d) to FG layer", label, id);
1537 this->controller->layers[1001]->add_surface(id);
1539 for (int j : this->surface_bg)
1541 HMI_DEBUG("wm", "Stored id:%d", j);
1548 this->layout_commit();
1550 this->emit_visible(label);
1551 this->emit_activated(label);
1555 void App::deactivate(int id)
1557 auto ip = this->controller->sprops.find(id);
1558 if (ip != this->controller->sprops.end())
1561 this->lookup_name(id).value_or("unknown-name").c_str();
1564 if ((0 == strcmp(label, "Radio")) || (0 == strcmp(label, "MediaPlayer")) || (0 == strcmp(label, "Music")) || (0 == strcmp(label, "Navigation")))
1568 this->surface_bg.push_back(id);
1570 // Remove from FG layer (1001)
1571 HMI_DEBUG("wm", "Remove %s(%d) from FG layer", label, id);
1572 this->controller->layers[1001]->remove_surface(id);
1574 // Add to BG layer (999)
1575 HMI_DEBUG("wm", "Add %s(%d) to BG layer", label, id);
1576 this->controller->layers[999]->add_surface(id);
1578 for (int j : surface_bg)
1580 HMI_DEBUG("wm", "Stored id:%d", j);
1585 this->controller->surfaces[id]->set_visibility(0);
1589 this->emit_deactivated(label);
1590 this->emit_invisible(label);
1594 void App::deactivate_main_surface()
1596 this->layers.main_surface = -1;
1597 std::string appid = "HomeScreen";
1598 this->api_deactivate_surface(appid.c_str(), this->layers.main_surface_name.c_str(), [](const char *) {});
1601 bool App::can_split(struct LayoutState const &state, int new_id)
1603 if (state.main != -1 && state.main != new_id)
1605 auto new_id_layer = this->layers.get_layer_id(new_id).value();
1606 auto current_id_layer = this->layers.get_layer_id(state.main).value();
1608 // surfaces are on separate layers, don't bother.
1609 if (new_id_layer != current_id_layer)
1614 std::string const &new_id_str = this->lookup_name(new_id).value();
1615 std::string const &cur_id_str = this->lookup_name(state.main).value();
1617 auto const &layer = this->layers.get_layer(new_id_layer);
1619 HMI_DEBUG("wm", "layer info name: %s", layer->name.c_str());
1621 if (layer->layouts.empty())
1626 for (auto i = layer->layouts.cbegin(); i != layer->layouts.cend(); i++)
1628 HMI_DEBUG("wm", "%d main_match '%s'", new_id_layer, i->main_match.c_str());
1629 auto rem = std::regex(i->main_match);
1630 if (std::regex_match(cur_id_str, rem))
1632 // build the second one only if the first already matched
1633 HMI_DEBUG("wm", "%d sub_match '%s'", new_id_layer, i->sub_match.c_str());
1634 auto res = std::regex(i->sub_match);
1635 if (std::regex_match(new_id_str, res))
1637 HMI_DEBUG("wm", "layout matched!");
1647 void App::try_layout(struct LayoutState & /*state*/,
1648 struct LayoutState const &new_layout,
1649 std::function<void(LayoutState const &nl)> apply)
1651 if (this->policy.layout_is_valid(new_layout))
1660 void controller_hooks::surface_created(uint32_t surface_id)
1662 this->app->surface_created(surface_id);
1665 void controller_hooks::surface_removed(uint32_t surface_id)
1667 this->app->surface_removed(surface_id);
1670 void controller_hooks::surface_visibility(uint32_t /*surface_id*/,
1673 void controller_hooks::surface_destination_rectangle(uint32_t /*surface_id*/,