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();
320 void App::surface_set_layout(int surface_id, optional<int> sub_surface_id)
322 if (!this->controller->surface_exists(surface_id))
324 HMI_ERROR("wm", "Surface %d does not exist", surface_id);
328 auto o_layer_id = this->layers.get_layer_id(surface_id);
332 HMI_ERROR("wm", "Surface %d is not associated with any layer!", surface_id);
336 uint32_t layer_id = *o_layer_id;
338 auto const &layer = this->layers.get_layer(layer_id);
339 auto rect = layer.value().rect;
340 auto &s = this->controller->surfaces[surface_id];
347 // less-than-0 values refer to MAX + 1 - $VALUE
348 // e.g. MAX is either screen width or height
351 w = this->controller->output_size.w + 1 + w;
355 h = this->controller->output_size.h + 1 + h;
358 /* if (sub_surface_id)
360 if (o_layer_id != this->layers.get_layer_id(*sub_surface_id))
363 "surface_set_layout: layers of surfaces (%d and %d) don't match!",
364 surface_id, *sub_surface_id);
371 // split along major axis
383 auto &ss = this->controller->surfaces[*sub_surface_id];
385 HMI_DEBUG("wm", "surface_set_layout for sub surface %u on layer %u",
386 *sub_surface_id, layer_id);
388 // set destination to the display rectangle
389 ss->set_destination_rectangle(x + x_off, y + y_off, w, h);
391 this->area_info[*sub_surface_id].x = x;
392 this->area_info[*sub_surface_id].y = y;
393 this->area_info[*sub_surface_id].w = w;
394 this->area_info[*sub_surface_id].h = h;
397 HMI_DEBUG("wm", "surface_set_layout for surface %u on layer %u", surface_id,
400 // set destination to the display rectangle
401 s->set_destination_rectangle(x, y, w, h);
403 // update area information
404 this->area_info[surface_id].x = x;
405 this->area_info[surface_id].y = y;
406 this->area_info[surface_id].w = w;
407 this->area_info[surface_id].h = h;
409 HMI_DEBUG("wm", "Surface %u now on layer %u with rect { %d, %d, %d, %d }",
410 surface_id, layer_id, x, y, w, h);
413 void App::layout_commit()
415 this->controller->commit_changes();
416 this->display->flush();
421 HMI_SEQ_DEBUG(g_app_list.currentRequestNumber(), "Timer set activate");
422 if (g_timer_ev_src == nullptr)
424 // firsttime set into sd_event
425 int ret = sd_event_add_time(afb_daemon_get_event_loop(), &g_timer_ev_src,
426 CLOCK_REALTIME, time(NULL) * (1000000UL) + kTimeOut, 1, processTimerHandler, this);
429 HMI_ERROR("wm", "Could't set timer");
434 // update timer limitation after second time
435 sd_event_source_set_time(g_timer_ev_src, time(NULL) * (1000000UL) + kTimeOut);
436 sd_event_source_set_enabled(g_timer_ev_src, SD_EVENT_ONESHOT);
440 void App::stopTimer()
442 unsigned req_num = g_app_list.currentRequestNumber();
443 HMI_SEQ_DEBUG(req_num, "Timer stop");
444 int rc = sd_event_source_set_enabled(g_timer_ev_src, SD_EVENT_OFF);
447 HMI_SEQ_ERROR(req_num, "Timer stop failed");
451 WMError App::lm_release(const struct WMAction &action)
453 //auto const &surface_id = this->lookup_id(drawing_name);
454 WMError ret = WMError::LAYOUT_CHANGE_FAIL;
455 unsigned req_num = g_app_list.currentRequestNumber();
456 auto const &surface_id = this->lookup_id(action.role.c_str());
459 HMI_SEQ_ERROR(req_num, "Surface does not exist");
463 if (*surface_id == this->layers.main_surface)
465 HMI_SEQ_ERROR(req_num, "Cannot deactivate main_surface");
469 auto o_state = *this->layers.get_layout_state(*surface_id);
471 if (o_state == nullptr)
473 HMI_SEQ_ERROR(req_num, "Could not find layer for surface");
477 struct LayoutState &state = *o_state;
479 if (state.main == -1)
481 HMI_SEQ_ERROR(req_num, "No surface active");
485 // Check against main_surface, main_surface_name is the configuration item.
486 if (*surface_id == this->layers.main_surface)
488 HMI_SEQ_DEBUG(req_num, "Refusing to deactivate main_surface %d", *surface_id);
490 return WMError::SUCCESS;
492 if ((state.main == *surface_id) && (state.sub == *surface_id))
494 HMI_SEQ_ERROR(req_num, "Surface is not active");
498 if (state.main == *surface_id)
503 state, LayoutState{state.sub, -1}, [&](LayoutState const &nl) {
504 std::string sub = std::move(*this->lookup_name(state.sub));
506 this->deactivate(*surface_id);
507 this->surface_set_layout(state.sub);
510 this->layout_commit();
511 std::string str_area = std::string(kNameLayoutNormal) + "." + std::string(kNameAreaFull);
512 compositor::rect area_rect = this->area_info[state.sub];
513 this->emit_syncdraw(sub.c_str(), str_area.c_str(),
514 area_rect.x, area_rect.y, area_rect.w, area_rect.h);
515 this->enqueue_flushdraw(state.sub);
520 this->try_layout(state, LayoutState{-1, -1}, [&](LayoutState const &nl) {
521 this->deactivate(*surface_id);
523 this->layout_commit();
527 else if (state.sub == *surface_id)
530 state, LayoutState{state.main, -1}, [&](LayoutState const &nl) {
531 std::string main = std::move(*this->lookup_name(state.main));
533 this->deactivate(*surface_id);
534 this->surface_set_layout(state.main);
537 this->layout_commit();
538 std::string str_area = std::string(kNameLayoutNormal) + "." + std::string(kNameAreaFull);
539 compositor::rect area_rect = this->area_info[state.main];
540 this->emit_syncdraw(main.c_str(), str_area.c_str(),
541 area_rect.x, area_rect.y, area_rect.w, area_rect.h);
542 this->enqueue_flushdraw(state.main);
545 return WMError::SUCCESS;
548 WMError App::lm_layout_change(const struct WMAction &action)
550 const char *msg = this->check_surface_exist(action.role.c_str());
554 HMI_SEQ_DEBUG(g_app_list.currentRequestNumber(), msg);
555 return WMError::LAYOUT_CHANGE_FAIL;
557 this->lm_layout_change(action.role.c_str());
558 return WMError::SUCCESS;
561 WMError App::doTransition(unsigned req_num)
563 HMI_SEQ_DEBUG(req_num, "check policy");
564 WMError ret = this->checkPolicy(req_num);
565 if (ret != WMError::SUCCESS)
569 HMI_SEQ_DEBUG(req_num, "Start transition.");
570 ret = this->startTransition(req_num);
574 WMError App::checkPolicy(unsigned req_num)
579 // get current trigger
581 WMError ret = WMError::LAYOUT_CHANGE_FAIL;
582 auto trigger = g_app_list.getRequest(req_num, &found);
585 ret = WMError::NO_ENTRY;
588 bool is_activate = (trigger.task == Task::TASK_ALLOCATE);
589 std::string req_area = trigger.area;
591 // >>>> Compatible with current window manager until policy manager coming
594 const char *msg = this->check_surface_exist(trigger.role.c_str());
598 HMI_SEQ_ERROR(g_app_list.currentRequestNumber(), msg);
599 ret = WMError::LAYOUT_CHANGE_FAIL;
603 auto const &surface_id = this->lookup_id(trigger.role.c_str());
604 auto o_state = *this->layers.get_layout_state(*surface_id);
605 struct LayoutState &state = *o_state;
607 unsigned curernt_sid = state.main;
608 bool split = this->can_split(state, *surface_id);
612 // Get current visible role
613 std::string add_role = this->lookup_name(state.main).value();
615 std::string add_area = std::string(kNameLayoutSplit) + "." + std::string(kNameAreaMain);
616 // Change request area
617 req_area = std::string(kNameLayoutSplit) + "." + std::string(kNameAreaSub);
618 // set another action
619 std::string add_name = g_app_list.getAppID(curernt_sid, add_role, &found);
622 HMI_SEQ_ERROR(req_num, "Couldn't widhdraw with surfaceID : %s", curernt_sid);
623 ret = WMError::NOT_REGISTERED;
626 HMI_SEQ_INFO(req_num, "Additional split app %s, role: %s, area: %s",
627 add_name.c_str(), add_role.c_str(), add_area.c_str());
629 WMError ret = g_app_list.setAction(req_num, add_name, add_role, add_area);
630 if (ret != WMError::SUCCESS)
632 HMI_SEQ_ERROR(req_num, "Failed to set action");
635 g_app_list.reqDump();
639 // Set invisible task
640 this->setInvisibleTask(trigger.role, split);
642 /* get new status from Policy Manager */
643 HMI_SEQ_NOTICE(req_num, "ATM, Policy manager does't exist, then set WMAction as is");
645 ret = g_app_list.setAction(req_num, trigger.appid, trigger.role, req_area, is_activate);
646 g_app_list.reqDump();
651 WMError App::startTransition(unsigned req_num)
653 bool sync_draw_happen = false;
655 WMError ret = WMError::SUCCESS;
656 auto actions = g_app_list.getActions(req_num, &found);
659 ret = WMError::NO_ENTRY;
660 HMI_SEQ_ERROR(req_num, "Window Manager bug :%s : Action is not set", errorDescription(ret));
664 for (const auto &y : actions)
668 sync_draw_happen = true;
669 this->emit_syncdraw(y.role, y.area);
670 /* TODO: emit event for app not subscriber
671 if(g_app_list.contains(y.appid))
672 g_app_list.lookUpClient(y.appid)->emit_syncdraw(y.role, y.area); */
676 if (sync_draw_happen)
682 // deactivate only, no syncDraw
683 // Make it deactivate here
684 for (const auto &z : actions)
686 if (g_app_list.contains(z.appid))
688 auto client = g_app_list.lookUpClient(z.appid);
689 this->deactivate(client->surfaceID(z.role));
692 ret = NO_LAYOUT_CHANGE;
697 void App::setInvisibleTask(const std::string &role, bool split)
702 void App::lm_layout_change(const char *drawing_name)
704 auto const &surface_id = this->lookup_id(drawing_name);
705 auto layer_id = this->layers.get_layer_id(*surface_id);
706 auto o_state = *this->layers.get_layout_state(*surface_id);
707 struct LayoutState &state = *o_state;
709 // disable layers that are above our current layer
710 for (auto const &l : this->layers.mapping)
712 if (l.second.layer_id <= *layer_id)
718 if (l.second.state.main != -1)
720 this->deactivate(l.second.state.main);
721 l.second.state.main = -1;
725 if (l.second.state.sub != -1)
727 this->deactivate(l.second.state.sub);
728 l.second.state.sub = -1;
734 this->layout_commit();
738 auto layer = this->layers.get_layer(*layer_id);
740 if (state.main == -1)
743 state, LayoutState{*surface_id}, [&](LayoutState const &nl) {
744 HMI_DEBUG("wm", "Layout: %s", kNameLayoutNormal);
745 this->surface_set_layout(*surface_id);
748 // Commit for configuraton
749 this->layout_commit();
751 std::string str_area = std::string(kNameLayoutNormal) + "." + std::string(kNameAreaFull);
752 compositor::rect area_rect = this->area_info[*surface_id];
753 this->emit_syncdraw(drawing_name, str_area.c_str(),
754 area_rect.x, area_rect.y, area_rect.w, area_rect.h);
755 this->enqueue_flushdraw(state.main);
760 if (0 == strcmp(drawing_name, "HomeScreen"))
763 state, LayoutState{*surface_id}, [&](LayoutState const &nl) {
764 HMI_DEBUG("wm", "Layout: %s", kNameLayoutNormal);
765 std::string str_area = std::string(kNameLayoutNormal) + "." + std::string(kNameAreaFull);
766 compositor::rect area_rect = this->area_info[*surface_id];
767 this->emit_syncdraw(drawing_name, str_area.c_str(),
768 area_rect.x, area_rect.y, area_rect.w, area_rect.h);
769 this->enqueue_flushdraw(state.main);
774 bool can_split = this->can_split(state, *surface_id);
780 LayoutState{state.main, *surface_id},
781 [&](LayoutState const &nl) {
782 HMI_DEBUG("wm", "Layout: %s", kNameLayoutSplit);
784 std::move(*this->lookup_name(state.main));
786 this->surface_set_layout(state.main, surface_id);
787 if (state.sub != *surface_id)
791 this->deactivate(state.sub);
796 // Commit for configuration and visibility(0)
797 this->layout_commit();
799 std::string str_area_main = std::string(kNameLayoutSplit) + "." + std::string(kNameAreaMain);
800 std::string str_area_sub = std::string(kNameLayoutSplit) + "." + std::string(kNameAreaSub);
801 compositor::rect area_rect_main = this->area_info[state.main];
802 compositor::rect area_rect_sub = this->area_info[*surface_id];
804 HMI_WARNING("wm", "HACK!!! mediaplayer and hvac is only supported for split");
805 std::string request_role = drawing_name;
806 //std::string request_app = transform(request_role.begin(), request_role.end(), request_role.begin(), tolower); //hvac or mediaplayer
807 std::string hack_appid = "navigation";
808 std::string hack_role = main;
809 std::string hack_area = str_area_main;
810 g_app_list.setAction(g_app_list.currentRequestNumber(), hack_appid, hack_role, hack_area, true);
811 //g_app_list.setEndDrawFinished(g_app_list.currentRequestNumber(), request_role, request_role);
812 //g_app_list.setEndDrawFinished(g_app_list.currentRequestNumber(), hack_appid, hack_role); // This process is illegal
814 this->emit_syncdraw(main.c_str(), str_area_main.c_str(),
815 area_rect_main.x, area_rect_main.y,
816 area_rect_main.w, area_rect_main.h);
817 this->emit_syncdraw(request_role.c_str(), str_area_sub.c_str(),
818 area_rect_sub.x, area_rect_sub.y,
819 area_rect_sub.w, area_rect_sub.h);
820 this->enqueue_flushdraw(state.main);
821 this->enqueue_flushdraw(state.sub);
827 state, LayoutState{*surface_id}, [&](LayoutState const &nl) {
828 HMI_DEBUG("wm", "Layout: %s", kNameLayoutNormal);
830 this->surface_set_layout(*surface_id);
831 if (state.main != *surface_id)
833 this->deactivate(state.main);
837 if (state.sub != *surface_id)
839 this->deactivate(state.sub);
844 // Commit for configuraton and visibility(0)
845 this->layout_commit();
847 std::string str_area = std::string(kNameLayoutNormal) + "." + std::string(kNameAreaFull);
848 compositor::rect area_rect = this->area_info[*surface_id];
849 this->emit_syncdraw(drawing_name, str_area.c_str(),
850 area_rect.x, area_rect.y, area_rect.w, area_rect.h);
851 this->enqueue_flushdraw(state.main);
858 const char *App::check_surface_exist(const char *drawing_name)
860 auto const &surface_id = this->lookup_id(drawing_name);
863 //reply("Surface does not exist");
864 return "Surface does not exist";
867 if (!this->controller->surface_exists(*surface_id))
869 //reply("Surface does not exist in controller!");
870 return "Surface does not exist in controller!";
873 auto layer_id = this->layers.get_layer_id(*surface_id);
877 //reply("Surface is not on any layer!");
878 return "Surface is not on any layer!";
881 auto o_state = *this->layers.get_layout_state(*surface_id);
883 if (o_state == nullptr)
885 //reply("Could not find layer for surface");
886 return "Could not find layer for surface";
889 HMI_DEBUG("wm", "surface %d is detected", *surface_id);
894 WMError App::setRequest(const std::string& appid, const std::string &role, const std::string &area,
895 Task task, unsigned* req_num)
897 if (!g_app_list.contains(appid))
899 return WMError::NOT_REGISTERED;
902 auto client = g_app_list.lookUpClient(appid);
907 unsigned current = g_app_list.currentRequestNumber();
908 unsigned requested_num = g_app_list.getRequestNumber(appid);
909 if (requested_num != 0)
911 HMI_SEQ_INFO(requested_num, "%s %s %s request is already queued", appid.c_str(), role.c_str(), area.c_str());
915 WMRequest req = WMRequest(appid, role, area, task);
916 unsigned new_req = g_app_list.addAllocateRequest(req);
918 g_app_list.reqDump();
920 HMI_SEQ_DEBUG(current, "%s start sequence with %s, %s", appid.c_str(), role.c_str(), area.c_str());
922 return WMError::SUCCESS;
925 void App::api_activate_surface(char const *appid, char const *drawing_name, char const *drawing_area, const reply_func &reply)
929 std::string id = appid;
930 std::string role = drawing_name;
931 std::string area = drawing_area;
932 Task task = Task::TASK_ALLOCATE;
933 unsigned req_num = 0;
934 WMError ret = WMError::UNKNOWN;
936 ret = this->setRequest(id, role, area, task, &req_num);
938 if(ret != WMError::SUCCESS)
940 HMI_ERROR("wm", errorDescription(ret));
941 reply("Failed to set request");
946 if (req_num != g_app_list.currentRequestNumber())
948 // Add request, then invoked after the previous task is finished
949 HMI_SEQ_DEBUG(req_num, "request is accepted");
956 ret = this->doTransition(req_num);
958 if (ret != WMError::SUCCESS)
961 HMI_SEQ_ERROR(req_num, errorDescription(ret));
962 g_app_list.removeRequest(req_num);
964 g_app_list.reqDump();
968 void App::api_deactivate_surface(char const *appid, char const *drawing_name, const reply_func &reply)
975 std::string id = appid;
976 std::string role = drawing_name;
977 std::string area = ""; //drawing_area;
979 if (!g_app_list.contains(id))
981 reply("app doesn't request 'requestSurface' yet");
984 auto client = g_app_list.lookUpClient(id);
989 unsigned current = g_app_list.currentRequestNumber();
990 unsigned requested_num = g_app_list.getRequestNumber(id);
991 if (requested_num != 0)
993 HMI_SEQ_INFO(requested_num, "%s %s %s request is already queued", id.c_str(), role.c_str(), area.c_str());
994 reply("already requested");
998 WMRequest req = WMRequest(id, role, area, Task::TASK_RELEASE);
999 unsigned new_req = g_app_list.addAllocateRequest(req);
1000 g_app_list.reqDump();
1002 HMI_SEQ_DEBUG(current, "%s start sequence with %s, %s", id.c_str(), role.c_str(), area.c_str());
1005 if (new_req != current)
1007 // Add request, then invoked after the previous task is finished
1008 HMI_SEQ_DEBUG(new_req, "request is accepted");
1015 WMError ret = this->doTransition(new_req);
1017 if (ret != WMError::SUCCESS)
1019 HMI_SEQ_ERROR(new_req, errorDescription(ret));
1020 g_app_list.removeRequest(new_req);
1022 //this->emit_error()
1026 void App::enqueue_flushdraw(int surface_id)
1028 this->check_flushdraw(surface_id);
1029 HMI_DEBUG("wm", "Enqueuing EndDraw for surface_id %d", surface_id);
1030 this->pending_end_draw.push_back(surface_id);
1033 void App::check_flushdraw(int surface_id)
1035 auto i = std::find(std::begin(this->pending_end_draw),
1036 std::end(this->pending_end_draw), surface_id);
1037 if (i != std::end(this->pending_end_draw))
1039 auto n = this->lookup_name(surface_id);
1040 HMI_ERROR("wm", "Application %s (%d) has pending EndDraw call(s)!",
1041 n ? n->c_str() : "unknown-name", surface_id);
1042 std::swap(this->pending_end_draw[std::distance(
1043 std::begin(this->pending_end_draw), i)],
1044 this->pending_end_draw.back());
1045 this->pending_end_draw.resize(this->pending_end_draw.size() - 1);
1049 void App::lm_enddraw(const char *drawing_name)
1051 HMI_DEBUG("wm", "end draw %s", drawing_name);
1052 for (unsigned i = 0, iend = this->pending_end_draw.size(); i < iend; i++)
1054 auto n = this->lookup_name(this->pending_end_draw[i]);
1055 if (n && *n == drawing_name)
1057 std::swap(this->pending_end_draw[i], this->pending_end_draw[iend - 1]);
1058 this->pending_end_draw.resize(iend - 1);
1059 this->activate(this->pending_end_draw[i]);
1060 this->emit_flushdraw(drawing_name);
1065 WMError App::doEndDraw(unsigned req_num)
1069 auto actions = g_app_list.getActions(req_num, &found);
1070 WMError ret = WMError::SUCCESS;
1073 ret = WMError::NO_ENTRY;
1077 HMI_SEQ_INFO(req_num, "do endDraw");
1078 // layout change and make it visible
1081 /* auto itr = actions.cend();
1082 this->currentAppInvisible(itr->role); */
1084 for (const auto &act : actions)
1087 if(!g_app_list.contains(act.appid)){
1088 ret = WMError::NOT_REGISTERED;
1090 ret = this->layoutChange(act);
1091 if(ret != WMError::SUCCESS)
1093 HMI_SEQ_WARNING(req_num, "Failed to manipulate surfaces while state change : %s", errorDescription(ret));
1096 ret = this->visibilityChange(act);
1097 if (ret != WMError::SUCCESS)
1099 HMI_SEQ_WARNING(req_num, "Failed to manipulate surfaces while state change : %s", errorDescription(ret));
1102 HMI_SEQ_DEBUG(req_num, "visible %s", act.role.c_str());
1103 //this->lm_enddraw(act.role.c_str());
1106 HMI_SEQ_INFO(req_num, "emit flushDraw");
1108 for(const auto &act_flush : actions)
1110 if(act_flush.visible)
1112 this->emit_flushdraw(act_flush.role.c_str());
1117 WMError App::setSurfaceSize(unsigned surface, const std::string &area)
1119 // Actually this function is under Layout Manager
1120 /* compositor::rect size = this->lm.getAreaSize(area); // compositor::rect size = this->lm_.getAreaSize(area);
1122 if (!this->controller->surface_exists(surface))
1124 // Block until all pending request are processed by wayland display server
1125 // because waiting for the surface of new app is created
1126 this->display->roundtrip();
1128 auto &s = this->controller->surfaces[surface];
1129 s->set_destination_rectangle(size.x, size.y, size.w, size.h);
1130 this->layout_commit();
1132 // Update area information
1133 this->area_info[surface].x = size.x;
1134 this->area_info[surface].y = size.y;
1135 this->area_info[surface].w = size.w;
1136 this->area_info[surface].h = size.h;
1137 HMI_DEBUG("wm", "Surface %d rect { %d, %d, %d, %d }",
1138 surface, size.x, size.y, size.w, size.h); */
1139 this->surface_set_layout(surface);
1141 return WMError::SUCCESS;
1144 WMError App::layoutChange(const WMAction &action)
1146 if (action.visible == false)
1148 return WMError::SUCCESS;
1150 auto client = g_app_list.lookUpClient(action.appid);
1151 unsigned surface = client->surfaceID(action.role);
1154 HMI_SEQ_ERROR(g_app_list.currentRequestNumber(),
1155 "client doesn't have surface with role(%s)", action.role.c_str());
1156 return WMError::NOT_REGISTERED;
1159 WMError ret = this->setSurfaceSize(surface, action.area);
1163 /* void App::currentAppInvisible(const std::string &role)
1165 const char *drawing_name = role.c_str();
1166 auto const &surface_id = this->lookup_id(drawing_name);
1167 auto layer_id = this->layers.get_layer_id(*surface_id);
1168 auto o_state = *this->layers.get_layout_state(*surface_id);
1169 struct LayoutState &state = *o_state;
1171 // disable layers that are above our current layer
1172 for (auto const &l : this->layers.mapping)
1174 if (l.second.layer_id <= *layer_id)
1180 if (l.second.state.main != -1)
1182 this->deactivate(l.second.state.main);
1183 l.second.state.main = -1;
1187 if (l.second.state.sub != -1)
1189 this->deactivate(l.second.state.sub);
1190 l.second.state.sub = -1;
1196 this->layout_commit();
1201 WMError App::visibilityChange(const WMAction &action)
1203 auto client = g_app_list.lookUpClient(action.appid);
1204 unsigned surface = client->surfaceID(action.role);
1207 HMI_SEQ_ERROR(g_app_list.currentRequestNumber(),
1208 "client doesn't have surface with role(%s)", action.role.c_str());
1209 return WMError::NOT_REGISTERED;
1214 this->activate(surface); // Layout Manager task
1218 this->deactivate(surface); // Layout Manager task
1220 return WMError::SUCCESS;
1223 void App::api_enddraw(char const *appid, char const *drawing_name)
1225 std::string id = appid;
1226 std::string role = drawing_name;
1227 unsigned current_req = g_app_list.currentRequestNumber();
1228 bool result = g_app_list.setEndDrawFinished(current_req, id, role);
1232 HMI_ERROR("wm", "%s is not in transition state", id.c_str());
1236 if (g_app_list.endDrawFullfilled(current_req))
1238 // do task for endDraw
1240 WMError ret = this->doEndDraw(current_req);
1242 if(ret != WMError::SUCCESS)
1244 //this->emit_error();
1246 HMI_SEQ_INFO(current_req, "Finish request status: %s", errorDescription(ret));
1248 g_app_list.removeRequest(current_req);
1250 this->processNextRequest();
1254 HMI_SEQ_INFO(current_req, "Wait other App call endDraw");
1259 void App::processNextRequest()
1262 g_app_list.reqDump();
1263 unsigned req_num = g_app_list.currentRequestNumber();
1264 if (g_app_list.haveRequest())
1266 HMI_SEQ_DEBUG(req_num, "Process next request");
1267 WMError rc = doTransition(req_num);
1268 if (rc != WMError::SUCCESS)
1270 HMI_SEQ_ERROR(req_num, errorDescription(rc));
1275 HMI_SEQ_DEBUG(req_num, "Nothing Request. Waiting Request");
1279 void App::api_ping() { this->dispatch_pending_events(); }
1281 void App::send_event(char const *evname, char const *label)
1283 HMI_DEBUG("wm", "%s: %s(%s)", __func__, evname, label);
1285 json_object *j = json_object_new_object();
1286 json_object_object_add(j, kKeyDrawingName, json_object_new_string(label));
1288 int ret = afb_event_push(this->map_afb_event[evname], j);
1291 HMI_DEBUG("wm", "afb_event_push failed: %m");
1295 void App::send_event(char const *evname, char const *label, char const *area,
1296 int x, int y, int w, int h)
1298 HMI_DEBUG("wm", "%s: %s(%s, %s) x:%d y:%d w:%d h:%d",
1299 __func__, evname, label, area, x, y, w, h);
1301 json_object *j_rect = json_object_new_object();
1302 json_object_object_add(j_rect, kKeyX, json_object_new_int(x));
1303 json_object_object_add(j_rect, kKeyY, json_object_new_int(y));
1304 json_object_object_add(j_rect, kKeyWidth, json_object_new_int(w));
1305 json_object_object_add(j_rect, kKeyHeight, json_object_new_int(h));
1307 json_object *j = json_object_new_object();
1308 json_object_object_add(j, kKeyDrawingName, json_object_new_string(label));
1309 json_object_object_add(j, kKeyDrawingArea, json_object_new_string(area));
1310 json_object_object_add(j, kKeyDrawingRect, j_rect);
1312 int ret = afb_event_push(this->map_afb_event[evname], j);
1315 HMI_DEBUG("wm", "afb_event_push failed: %m");
1322 void App::surface_created(uint32_t surface_id)
1324 auto layer_id = this->layers.get_layer_id(surface_id);
1327 HMI_DEBUG("wm", "Newly created surfce %d is not associated with any layer!",
1332 HMI_DEBUG("wm", "surface_id is %u, layer_id is %u", surface_id, *layer_id);
1334 this->controller->layers[*layer_id]->add_surface(surface_id);
1335 this->layout_commit();
1336 // activate the main_surface right away
1337 /*if (surface_id == static_cast<unsigned>(this->layers.main_surface)) {
1338 HMI_DEBUG("wm", "Activating main_surface (%d)", surface_id);
1340 this->api_activate_surface(
1341 this->lookup_name(surface_id).value_or("unknown-name").c_str());
1344 // search pid from surfaceID
1346 // pick up appid from pid from application manager
1348 // check appid then add it to the client
1351 void App::surface_removed(uint32_t surface_id)
1353 HMI_DEBUG("wm", "surface_id is %u", surface_id);
1355 g_app_list.removeSurface(surface_id);
1358 void App::emit_activated(char const *label)
1360 this->send_event(kListEventName[Event_Active], label);
1363 void App::emit_deactivated(char const *label)
1365 this->send_event(kListEventName[Event_Inactive], label);
1368 void App::emit_syncdraw(char const *label, char const *area, int x, int y, int w, int h)
1370 this->send_event(kListEventName[Event_SyncDraw], label, area, x, y, w, h);
1373 void App::emit_syncdraw(const std::string &role, const std::string &area)
1375 int x = 0, y = 0, w = 0, h = 0;
1376 //this->lm_get_area_info(area, &x, &y, &w, &h);
1377 this->send_event(kListEventName[Event_SyncDraw],
1378 role.c_str(), area.c_str(), x, y, w, h);
1381 void App::emit_flushdraw(char const *label)
1383 this->send_event(kListEventName[Event_FlushDraw], label);
1386 void App::emit_visible(char const *label, bool is_visible)
1388 this->send_event(is_visible ? kListEventName[Event_Visible] : kListEventName[Event_Invisible], label);
1391 void App::emit_invisible(char const *label)
1393 return emit_visible(label, false);
1396 void App::emit_visible(char const *label) { return emit_visible(label, true); }
1398 result<int> App::api_request_surface(char const *appid, char const *drawing_name)
1400 auto lid = this->layers.get_layer_id(std::string(drawing_name));
1404 * register drawing_name as fallback and make it displayed.
1406 lid = this->layers.get_layer_id(std::string("Fallback"));
1407 HMI_DEBUG("wm", "%s is not registered in layers.json, then fallback as normal app", drawing_name);
1410 return Err<int>("Drawing name does not match any role, Fallback is disabled");
1414 auto rname = this->lookup_id(drawing_name);
1417 // name does not exist yet, allocate surface id...
1418 auto id = int(this->id_alloc.generate_id(drawing_name));
1419 this->layers.add_surface(id, *lid);
1421 // set the main_surface[_name] here and now
1422 if (!this->layers.main_surface_name.empty() &&
1423 this->layers.main_surface_name == drawing_name)
1425 this->layers.main_surface = id;
1426 HMI_DEBUG("wm", "Set main_surface id to %u", id);
1429 // add client into the db
1430 std::string appid_str(appid);
1431 std::string role(drawing_name);
1432 //g_app_list.addClient(appid_str, role);
1433 g_app_list.addClient(appid_str, *lid, id, role);
1438 // Check currently registered drawing names if it is already there.
1439 return Err<int>("Surface already present");
1442 char const *App::api_request_surface(char const *appid, char const *drawing_name,
1447 auto lid = this->layers.get_layer_id(std::string(drawing_name));
1448 unsigned sid = std::stol(ivi_id);
1453 * register drawing_name as fallback and make it displayed.
1455 lid = this->layers.get_layer_id(std::string("Fallback"));
1456 HMI_DEBUG("wm", "%s is not registered in layers.json, then fallback as normal app", drawing_name);
1459 return "Drawing name does not match any role, Fallback is disabled";
1463 auto rname = this->lookup_id(drawing_name);
1467 return "Surface already present";
1470 // register pair drawing_name and ivi_id
1471 this->id_alloc.register_name_id(drawing_name, sid);
1472 this->layers.add_surface(sid, *lid);
1474 // this surface is already created
1475 HMI_DEBUG("wm", "surface_id is %u, layer_id is %u", sid, *lid);
1477 this->controller->layers[*lid]->add_surface(sid);
1478 this->layout_commit();
1483 result<json_object *> App::api_get_display_info()
1486 if (!this->controller)
1488 return Err<json_object *>("ivi_controller global not available");
1492 compositor::size o_size = this->controller->output_size;
1493 compositor::size p_size = this->controller->physical_size;
1495 json_object *object = json_object_new_object();
1496 json_object_object_add(object, kKeyWidthPixel, json_object_new_int(o_size.w));
1497 json_object_object_add(object, kKeyHeightPixel, json_object_new_int(o_size.h));
1498 json_object_object_add(object, kKeyWidthMm, json_object_new_int(p_size.w));
1499 json_object_object_add(object, kKeyHeightMm, json_object_new_int(p_size.h));
1501 return Ok<json_object *>(object);
1504 result<json_object *> App::api_get_area_info(char const *drawing_name)
1506 HMI_DEBUG("wm", "called");
1508 // Check drawing name, surface/layer id
1509 auto const &surface_id = this->lookup_id(drawing_name);
1512 return Err<json_object *>("Surface does not exist");
1515 if (!this->controller->surface_exists(*surface_id))
1517 return Err<json_object *>("Surface does not exist in controller!");
1520 auto layer_id = this->layers.get_layer_id(*surface_id);
1523 return Err<json_object *>("Surface is not on any layer!");
1526 auto o_state = *this->layers.get_layout_state(*surface_id);
1527 if (o_state == nullptr)
1529 return Err<json_object *>("Could not find layer for surface");
1532 struct LayoutState &state = *o_state;
1533 if ((state.main != *surface_id) && (state.sub != *surface_id))
1535 return Err<json_object *>("Surface is inactive");
1538 // Set area rectangle
1539 compositor::rect area_info = this->area_info[*surface_id];
1540 json_object *object = json_object_new_object();
1541 json_object_object_add(object, kKeyX, json_object_new_int(area_info.x));
1542 json_object_object_add(object, kKeyY, json_object_new_int(area_info.y));
1543 json_object_object_add(object, kKeyWidth, json_object_new_int(area_info.w));
1544 json_object_object_add(object, kKeyHeight, json_object_new_int(area_info.h));
1546 return Ok<json_object *>(object);
1549 void App::activate(int id)
1551 auto ip = this->controller->sprops.find(id);
1552 if (ip != this->controller->sprops.end())
1554 this->controller->surfaces[id]->set_visibility(1);
1556 this->lookup_name(id).value_or("unknown-name").c_str();
1559 if ((0 == strcmp(label, "Radio")) || (0 == strcmp(label, "MediaPlayer")) || (0 == strcmp(label, "Music")) || (0 == strcmp(label, "Navigation")))
1561 for (auto i = surface_bg.begin(); i != surface_bg.end(); ++i)
1566 this->surface_bg.erase(i);
1568 // Remove from BG layer (999)
1569 HMI_DEBUG("wm", "Remove %s(%d) from BG layer", label, id);
1570 this->controller->layers[999]->remove_surface(id);
1572 // Add to FG layer (1001)
1573 HMI_DEBUG("wm", "Add %s(%d) to FG layer", label, id);
1574 this->controller->layers[1001]->add_surface(id);
1576 for (int j : this->surface_bg)
1578 HMI_DEBUG("wm", "Stored id:%d", j);
1585 this->layout_commit();
1587 this->emit_visible(label);
1588 this->emit_activated(label);
1592 void App::deactivate(int id)
1594 auto ip = this->controller->sprops.find(id);
1595 if (ip != this->controller->sprops.end())
1598 this->lookup_name(id).value_or("unknown-name").c_str();
1601 if ((0 == strcmp(label, "Radio")) || (0 == strcmp(label, "MediaPlayer")) || (0 == strcmp(label, "Music")) || (0 == strcmp(label, "Navigation")))
1605 this->surface_bg.push_back(id);
1607 // Remove from FG layer (1001)
1608 HMI_DEBUG("wm", "Remove %s(%d) from FG layer", label, id);
1609 this->controller->layers[1001]->remove_surface(id);
1611 // Add to BG layer (999)
1612 HMI_DEBUG("wm", "Add %s(%d) to BG layer", label, id);
1613 this->controller->layers[999]->add_surface(id);
1615 for (int j : surface_bg)
1617 HMI_DEBUG("wm", "Stored id:%d", j);
1622 this->controller->surfaces[id]->set_visibility(0);
1626 this->emit_deactivated(label);
1627 this->emit_invisible(label);
1631 void App::deactivate_main_surface()
1633 this->layers.main_surface = -1;
1634 std::string appid = "HomeScreen";
1635 this->api_deactivate_surface(appid.c_str(), this->layers.main_surface_name.c_str(), [](const char *) {});
1638 bool App::can_split(struct LayoutState const &state, int new_id)
1640 if (state.main != -1 && state.main != new_id)
1642 auto new_id_layer = this->layers.get_layer_id(new_id).value();
1643 auto current_id_layer = this->layers.get_layer_id(state.main).value();
1645 // surfaces are on separate layers, don't bother.
1646 if (new_id_layer != current_id_layer)
1651 std::string const &new_id_str = this->lookup_name(new_id).value();
1652 std::string const &cur_id_str = this->lookup_name(state.main).value();
1654 auto const &layer = this->layers.get_layer(new_id_layer);
1656 HMI_DEBUG("wm", "layer info name: %s", layer->name.c_str());
1658 if (layer->layouts.empty())
1663 for (auto i = layer->layouts.cbegin(); i != layer->layouts.cend(); i++)
1665 HMI_DEBUG("wm", "%d main_match '%s'", new_id_layer, i->main_match.c_str());
1666 auto rem = std::regex(i->main_match);
1667 if (std::regex_match(cur_id_str, rem))
1669 // build the second one only if the first already matched
1670 HMI_DEBUG("wm", "%d sub_match '%s'", new_id_layer, i->sub_match.c_str());
1671 auto res = std::regex(i->sub_match);
1672 if (std::regex_match(new_id_str, res))
1674 HMI_DEBUG("wm", "layout matched!");
1684 void App::try_layout(struct LayoutState & /*state*/,
1685 struct LayoutState const &new_layout,
1686 std::function<void(LayoutState const &nl)> apply)
1688 if (this->policy.layout_is_valid(new_layout))
1697 void controller_hooks::surface_created(uint32_t surface_id)
1699 this->app->surface_created(surface_id);
1702 void controller_hooks::surface_removed(uint32_t surface_id)
1704 this->app->surface_removed(surface_id);
1707 void controller_hooks::surface_visibility(uint32_t /*surface_id*/,
1710 void controller_hooks::surface_destination_rectangle(uint32_t /*surface_id*/,