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.
17 #include "applist.hpp"
19 #include "json_helper.hpp"
23 #include "wayland_ivi_wm.hpp"
30 #include <json-c/json.h>
39 #include "wm_client.hpp"
44 #include <systemd/sd-event.h>
50 const unsigned kTimeOut = 10000000UL; /* 10s */
52 /* DrawingArea name used by "{layout}.{area}" */
53 const char kNameLayoutNormal[] = "normal";
54 const char kNameLayoutSplit[] = "split";
55 const char kNameAreaFull[] = "full";
56 const char kNameAreaMain[] = "main";
57 const char kNameAreaSub[] = "sub";
59 /* Key for json obejct */
60 const char kKeyDrawingName[] = "drawing_name";
61 const char kKeyDrawingArea[] = "drawing_area";
62 const char kKeyDrawingRect[] = "drawing_rect";
63 const char kKeyX[] = "x";
64 const char kKeyY[] = "y";
65 const char kKeyWidth[] = "width";
66 const char kKeyHeight[] = "height";
67 const char kKeyWidthPixel[] = "width_pixel";
68 const char kKeyHeightPixel[] = "height_pixel";
69 const char kKeyWidthMm[] = "width_mm";
70 const char kKeyHeightMm[] = "height_mm";
72 static sd_event_source *g_timer_ev_src = nullptr;
79 result<json> file_to_json(char const *filename)
82 std::ifstream i(filename);
85 HMI_DEBUG("wm", "Could not open config file, so use default layer information");
86 j = default_layers_json;
96 struct result<layer_map> load_layer_map(char const *filename)
98 HMI_DEBUG("wm", "loading IDs from %s", filename);
100 auto j = file_to_json(filename);
103 return Err<layer_map>(j.unwrap_err());
105 json jids = j.unwrap();
107 return to_layer_map(jids);
111 processTimerHandler(sd_event_source *s, uint64_t usec, void *userdata)
113 HMI_NOTICE("wm", "Time out occurs because the client replys endDraw slow, so revert the request");
114 reinterpret_cast<wm::App *>(userdata)->timerHandler();
120 void App::timerHandler()
122 unsigned req_num = app_list->currentRequestNumber();
123 HMI_SEQ_DEBUG(req_num, "Timer expired remove Request");
125 app_list->removeRequest(req_num);
128 if (app_list->haveRequest())
130 this->process_request();
134 void App::removeClient(const std::string &appid)
136 HMI_DEBUG("wm", "Remove clinet %s from list", appid.c_str());
137 app_list->removeClient(appid);
140 bool App::subscribeEventForApp(const std::string &appid, afb_req req, const std::string &evname)
142 if(!app_list->contains(appid)){
143 HMI_DEBUG("wm", "Client %s is not registered", appid.c_str());
146 auto client = app_list->lookUpClient(appid);
147 return client->subscribe(req, evname);
153 App::App(wl::display *d)
161 pending_events(false),
163 app_list(std::make_unique<AppList>())
168 auto l = load_layer_map(
169 this->config.get_string("layers.json").value().c_str());
172 this->layers = l.unwrap();
176 HMI_ERROR("wm", "%s", l.err().value());
180 catch (std::exception &e)
182 HMI_ERROR("wm", "Loading of configuration failed: %s", e.what());
188 if (!this->display->ok())
193 if (this->layers.mapping.empty())
195 HMI_ERROR("wm", "No surface -> layer mapping loaded");
200 for (int i = Event_Val_Min; i <= Event_Val_Max; i++)
202 map_afb_event[kListEventName[i]] = afb_daemon_make_event(kListEventName[i]);
205 this->display->add_global_handler(
206 "wl_output", [this](wl_registry *r, uint32_t name, uint32_t v) {
207 this->outputs.emplace_back(std::make_unique<wl::output>(r, name, v));
210 this->display->add_global_handler(
211 "ivi_wm", [this](wl_registry *r, uint32_t name, uint32_t v) {
213 std::make_unique<struct compositor::controller>(r, name, v);
215 // Init controller hooks
216 this->controller->chooks = &this->chooks;
218 // This protocol needs the output, so lets just add our mapping here...
219 this->controller->add_proxy_to_id_mapping(
220 this->outputs.back()->proxy.get(),
221 wl_proxy_get_id(reinterpret_cast<struct wl_proxy *>(
222 this->outputs.back()->proxy.get())));
225 this->controller->create_screen(this->outputs.back()->proxy.get());
227 // Set display to controller
228 this->controller->display = this->display;
231 // First level objects
232 this->display->roundtrip();
233 // Second level objects
234 this->display->roundtrip();
235 // Third level objects
236 this->display->roundtrip();
238 return init_layers();
241 App::~App() = default;
243 int App::dispatch_pending_events()
245 if (this->pop_pending_events())
247 this->display->dispatch_pending();
253 bool App::pop_pending_events()
256 return this->pending_events.compare_exchange_strong(
257 x, false, std::memory_order_consume);
260 void App::set_pending_events()
262 this->pending_events.store(true, std::memory_order_release);
265 optional<int> App::lookup_id(char const *name)
267 return this->id_alloc.lookup(std::string(name));
269 optional<std::string> App::lookup_name(int id)
271 return this->id_alloc.lookup(id);
277 int App::init_layers()
279 if (!this->controller)
281 HMI_ERROR("wm", "ivi_controller global not available");
285 if (this->outputs.empty())
287 HMI_ERROR("wm", "no output was set up!");
291 auto &c = this->controller;
293 auto &o = this->outputs.front();
294 auto &s = c->screens.begin()->second;
295 auto &layers = c->layers;
297 // Write output dimensions to ivi controller...
298 c->output_size = compositor::size{uint32_t(o->width), uint32_t(o->height)};
299 c->physical_size = compositor::size{uint32_t(o->physical_width),
300 uint32_t(o->physical_height)};
308 // Quick and dirty setup of layers
309 for (auto const &i : this->layers.mapping)
311 c->layer_create(i.second.layer_id, o->width, o->height);
312 auto &l = layers[i.second.layer_id];
313 l->set_destination_rectangle(0, 0, o->width, o->height);
314 l->set_visibility(1);
315 HMI_DEBUG("wm", "Setting up layer %s (%d) for surface role match \"%s\"",
316 i.second.name.c_str(), i.second.layer_id, i.second.role.c_str());
319 // Add layers to screen
320 s->set_render_order(this->layers.layers);
322 this->layout_commit();
327 void App::surface_set_layout(int surface_id, optional<int> sub_surface_id)
329 if (!this->controller->surface_exists(surface_id))
331 HMI_ERROR("wm", "Surface %d does not exist", surface_id);
335 auto o_layer_id = this->layers.get_layer_id(surface_id);
339 HMI_ERROR("wm", "Surface %d is not associated with any layer!", surface_id);
343 uint32_t layer_id = *o_layer_id;
345 auto const &layer = this->layers.get_layer(layer_id);
346 auto rect = layer.value().rect;
347 auto &s = this->controller->surfaces[surface_id];
354 // less-than-0 values refer to MAX + 1 - $VALUE
355 // e.g. MAX is either screen width or height
358 w = this->controller->output_size.w + 1 + w;
362 h = this->controller->output_size.h + 1 + h;
367 if (o_layer_id != this->layers.get_layer_id(*sub_surface_id))
370 "surface_set_layout: layers of surfaces (%d and %d) don't match!",
371 surface_id, *sub_surface_id);
378 // split along major axis
390 auto &ss = this->controller->surfaces[*sub_surface_id];
392 HMI_DEBUG("wm", "surface_set_layout for sub surface %u on layer %u",
393 *sub_surface_id, layer_id);
395 // set destination to the display rectangle
396 ss->set_destination_rectangle(x + x_off, y + y_off, w, h);
398 this->area_info[*sub_surface_id].x = x;
399 this->area_info[*sub_surface_id].y = y;
400 this->area_info[*sub_surface_id].w = w;
401 this->area_info[*sub_surface_id].h = h;
404 HMI_DEBUG("wm", "surface_set_layout for surface %u on layer %u", surface_id,
407 // set destination to the display rectangle
408 s->set_destination_rectangle(x, y, w, h);
410 // update area information
411 this->area_info[surface_id].x = x;
412 this->area_info[surface_id].y = y;
413 this->area_info[surface_id].w = w;
414 this->area_info[surface_id].h = h;
416 HMI_DEBUG("wm", "Surface %u now on layer %u with rect { %d, %d, %d, %d }",
417 surface_id, layer_id, x, y, w, h);
420 void App::layout_commit()
422 this->controller->commit_changes();
423 this->display->flush();
426 void App::set_timer()
428 HMI_SEQ_DEBUG(app_list->currentRequestNumber(), "Timer set activate");
429 if (g_timer_ev_src == nullptr)
431 // firsttime set into sd_event
432 int ret = sd_event_add_time(afb_daemon_get_event_loop(), &g_timer_ev_src,
433 CLOCK_REALTIME, time(NULL) * (1000000UL) + kTimeOut, 1, processTimerHandler, this);
436 HMI_ERROR("wm", "Could't set timer");
441 // update timer limitation after second time
442 sd_event_source_set_time(g_timer_ev_src, time(NULL) * (1000000UL) + kTimeOut);
443 sd_event_source_set_enabled(g_timer_ev_src, SD_EVENT_ONESHOT);
447 void App::stop_timer()
449 unsigned req_num = app_list->currentRequestNumber();
450 HMI_SEQ_DEBUG(req_num, "Timer stop");
451 int rc = sd_event_source_set_enabled(g_timer_ev_src, SD_EVENT_OFF);
454 HMI_SEQ_ERROR(req_num, "Timer stop failed");
458 WMError App::lm_release(const struct WMAction &action)
460 //auto const &surface_id = this->lookup_id(drawing_name);
461 WMError ret = WMError::LAYOUT_CHANGE_FAIL;
462 unsigned req_num = app_list->currentRequestNumber();
463 auto const &surface_id = this->lookup_id(action.role.c_str());
466 HMI_SEQ_ERROR(req_num, "Surface does not exist");
470 if (*surface_id == this->layers.main_surface)
472 HMI_SEQ_ERROR(req_num, "Cannot deactivate main_surface");
476 auto o_state = *this->layers.get_layout_state(*surface_id);
478 if (o_state == nullptr)
480 HMI_SEQ_ERROR(req_num, "Could not find layer for surface");
484 struct LayoutState &state = *o_state;
486 if (state.main == -1)
488 HMI_SEQ_ERROR(req_num, "No surface active");
492 // Check against main_surface, main_surface_name is the configuration item.
493 if (*surface_id == this->layers.main_surface)
495 HMI_SEQ_DEBUG(req_num, "Refusing to deactivate main_surface %d", *surface_id);
497 return WMError::SUCCESS;
499 if ((state.main == *surface_id) && (state.sub == *surface_id))
501 HMI_SEQ_ERROR(req_num, "Surface is not active");
505 if (state.main == *surface_id)
510 state, LayoutState{state.sub, -1}, [&](LayoutState const &nl) {
511 std::string sub = std::move(*this->lookup_name(state.sub));
513 this->deactivate(*surface_id);
514 this->surface_set_layout(state.sub);
517 this->layout_commit();
518 std::string str_area = std::string(kNameLayoutNormal) + "." + std::string(kNameAreaFull);
519 compositor::rect area_rect = this->area_info[state.sub];
520 this->emit_syncdraw(sub.c_str(), str_area.c_str(),
521 area_rect.x, area_rect.y, area_rect.w, area_rect.h);
522 this->enqueue_flushdraw(state.sub);
527 this->try_layout(state, LayoutState{-1, -1}, [&](LayoutState const &nl) {
528 this->deactivate(*surface_id);
530 this->layout_commit();
534 else if (state.sub == *surface_id)
537 state, LayoutState{state.main, -1}, [&](LayoutState const &nl) {
538 std::string main = std::move(*this->lookup_name(state.main));
540 this->deactivate(*surface_id);
541 this->surface_set_layout(state.main);
544 this->layout_commit();
545 std::string str_area = std::string(kNameLayoutNormal) + "." + std::string(kNameAreaFull);
546 compositor::rect area_rect = this->area_info[state.main];
547 this->emit_syncdraw(main.c_str(), str_area.c_str(),
548 area_rect.x, area_rect.y, area_rect.w, area_rect.h);
549 this->enqueue_flushdraw(state.main);
552 return WMError::SUCCESS;
555 WMError App::lm_layout_change(const struct WMAction &action)
557 const char *msg = this->check_surface_exist(action.role.c_str());
560 lm_.updateLayout(action);
561 TODO: emit syncDraw with application*/
564 HMI_SEQ_DEBUG(app_list->currentRequestNumber(), msg);
565 return WMError::LAYOUT_CHANGE_FAIL;
567 this->lm_layout_change(action.role.c_str());
568 return WMError::SUCCESS;
571 WMError App::do_transition(unsigned req_num)
576 // get current trigger
577 auto trigger = app_list->getRequest(req_num);
578 bool is_activate = true;
580 /* get new status from Policy Manager
582 (json_object*?) newLayout = checkPolicy(trigger);
583 (vector<struct WMAction>&) auto actions = translator.inputActionFromLayout(newLayout, currentLayout)
584 for(const auto& x : actions){
585 app_list->setAciton(req_num, x)
590 translator.inputActionFromLayout(newLayout, currentLayout, &app_list, req_num);
592 /* The following error check is not necessary because main.cpp will reject the message form not registered object
594 HMI_SEQ_NOTICE(req_num, "ATM, Policy manager does't exist, then set WMAction as is");
596 if (trigger.task == Task::TASK_RELEASE)
600 WMError ret = app_list->setAction(req_num, trigger.appid, trigger.role, trigger.area, is_activate);
603 if (ret != WMError::SUCCESS)
605 HMI_SEQ_ERROR(req_num, "Failed to set action");
609 // layer manager task
610 bool sync_draw_happen = false;
611 for (const auto &y : app_list->getActions(req_num))
617 but current we can't do do_task,
618 so divide the processing into lm_layout_change and lm_release
622 sync_draw_happen = true;
623 ret = lm_layout_change(y);
624 if (ret != WMError::SUCCESS)
626 HMI_SEQ_ERROR(req_num, "%s: appid: %s, role: %s, area: %s",
627 errorDescription(ret), y.appid.c_str(), y.role.c_str(), y.area.c_str());
628 app_list->removeRequest(req_num);
630 // TODO: if transition fails, what should we do?
632 /* app_list->lookUpClient(y.appid)->emit_syncdraw(y.role, y.area); */
639 HMI_SEQ_ERROR(req_num, "Failed release resource: %s", y.appid.c_str());
640 app_list->removeRequest(req_num);
642 // TODO: if transition fails, what should we do?
644 /* app_list->lookUpClient(y.appid)->emit_invisible(y.role, y.area); */
648 if (ret != WMError::SUCCESS)
650 //this->emit_error(req_num, 0 /*error_num*/, "error happens"); // test
652 else if (sync_draw_happen)
658 app_list->removeRequest(req_num); // HACK!!!
663 void App::lm_layout_change(const char *drawing_name)
665 auto const &surface_id = this->lookup_id(drawing_name);
666 auto layer_id = this->layers.get_layer_id(*surface_id);
667 auto o_state = *this->layers.get_layout_state(*surface_id);
668 struct LayoutState &state = *o_state;
670 // disable layers that are above our current layer
671 for (auto const &l : this->layers.mapping)
673 if (l.second.layer_id <= *layer_id)
679 if (l.second.state.main != -1)
681 this->deactivate(l.second.state.main);
682 l.second.state.main = -1;
686 if (l.second.state.sub != -1)
688 this->deactivate(l.second.state.sub);
689 l.second.state.sub = -1;
695 this->layout_commit();
699 auto layer = this->layers.get_layer(*layer_id);
701 if (state.main == -1)
704 state, LayoutState{*surface_id}, [&](LayoutState const &nl) {
705 HMI_DEBUG("wm", "Layout: %s", kNameLayoutNormal);
706 this->surface_set_layout(*surface_id);
709 // Commit for configuraton
710 this->layout_commit();
712 std::string str_area = std::string(kNameLayoutNormal) + "." + std::string(kNameAreaFull);
713 compositor::rect area_rect = this->area_info[*surface_id];
714 this->emit_syncdraw(drawing_name, str_area.c_str(),
715 area_rect.x, area_rect.y, area_rect.w, area_rect.h);
716 this->enqueue_flushdraw(state.main);
721 if (0 == strcmp(drawing_name, "HomeScreen"))
724 state, LayoutState{*surface_id}, [&](LayoutState const &nl) {
725 HMI_DEBUG("wm", "Layout: %s", kNameLayoutNormal);
726 std::string str_area = std::string(kNameLayoutNormal) + "." + std::string(kNameAreaFull);
727 compositor::rect area_rect = this->area_info[*surface_id];
728 this->emit_syncdraw(drawing_name, str_area.c_str(),
729 area_rect.x, area_rect.y, area_rect.w, area_rect.h);
730 this->enqueue_flushdraw(state.main);
735 bool can_split = this->can_split(state, *surface_id);
741 LayoutState{state.main, *surface_id},
742 [&](LayoutState const &nl) {
743 HMI_DEBUG("wm", "Layout: %s", kNameLayoutSplit);
745 std::move(*this->lookup_name(state.main));
747 this->surface_set_layout(state.main, surface_id);
748 if (state.sub != *surface_id)
752 this->deactivate(state.sub);
757 // Commit for configuration and visibility(0)
758 this->layout_commit();
760 std::string str_area_main = std::string(kNameLayoutSplit) + "." + std::string(kNameAreaMain);
761 std::string str_area_sub = std::string(kNameLayoutSplit) + "." + std::string(kNameAreaSub);
762 compositor::rect area_rect_main = this->area_info[state.main];
763 compositor::rect area_rect_sub = this->area_info[*surface_id];
765 HMI_WARNING("wm", "HACK!!! mediaplayer and hvac is only supported for split");
766 std::string request_role = drawing_name;
767 //std::string request_app = transform(request_role.begin(), request_role.end(), request_role.begin(), tolower); //hvac or mediaplayer
768 std::string hack_appid = "navigation";
769 std::string hack_role = main;
770 std::string hack_area = str_area_main;
771 app_list->setAction(app_list->currentRequestNumber(), hack_appid, hack_role, hack_area, true);
772 //app_list->setEndDrawFinished(app_list->currentRequestNumber(), request_role, request_role);
773 //app_list->setEndDrawFinished(app_list->currentRequestNumber(), hack_appid, hack_role); // This process is illegal
775 this->emit_syncdraw(main.c_str(), str_area_main.c_str(),
776 area_rect_main.x, area_rect_main.y,
777 area_rect_main.w, area_rect_main.h);
778 this->emit_syncdraw(request_role.c_str(), str_area_sub.c_str(),
779 area_rect_sub.x, area_rect_sub.y,
780 area_rect_sub.w, area_rect_sub.h);
781 this->enqueue_flushdraw(state.main);
782 this->enqueue_flushdraw(state.sub);
788 state, LayoutState{*surface_id}, [&](LayoutState const &nl) {
789 HMI_DEBUG("wm", "Layout: %s", kNameLayoutNormal);
791 this->surface_set_layout(*surface_id);
792 if (state.main != *surface_id)
794 this->deactivate(state.main);
798 if (state.sub != *surface_id)
800 this->deactivate(state.sub);
805 // Commit for configuraton and visibility(0)
806 this->layout_commit();
808 std::string str_area = std::string(kNameLayoutNormal) + "." + std::string(kNameAreaFull);
809 compositor::rect area_rect = this->area_info[*surface_id];
810 this->emit_syncdraw(drawing_name, str_area.c_str(),
811 area_rect.x, area_rect.y, area_rect.w, area_rect.h);
812 this->enqueue_flushdraw(state.main);
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 void App::api_activate_surface(char const *appid, char const *drawing_name, char const *drawing_area, const reply_func &reply)
863 std::string id = appid;
864 std::string role = drawing_name;
865 std::string area = drawing_area;
867 if (!app_list->contains(id))
869 reply("app doesn't request 'requestSurface' yet");
873 auto client = app_list->lookUpClient(id);
875 unsigned srfc = client->surfaceID(role);
876 if(srfc != INVALID_SURFACE_ID){
877 // remove floating surface
878 app_list->removeFloatingSurface(client->surfaceID(role));
883 unsigned current = app_list->currentRequestNumber();
884 unsigned requested_num = app_list->getRequestNumber(id);
885 if (requested_num != 0)
887 HMI_SEQ_INFO(requested_num, "%s %s %s request is already queued", id.c_str(), role.c_str(), area.c_str());
888 reply("already requested");
892 WMRequest req = WMRequest(id, role, area, Task::TASK_ALLOCATE);
893 unsigned new_req = app_list->addAllocateRequest(req);
896 HMI_SEQ_DEBUG(current, "%s start sequence with %s, %s", id.c_str(), role.c_str(), area.c_str());
899 if (new_req != current)
901 // Add request, then invoked after the previous task is finished
902 HMI_SEQ_DEBUG(new_req, "request is accepted");
909 WMError ret = this->do_transition(new_req);
911 if (ret != WMError::SUCCESS)
913 HMI_SEQ_ERROR(new_req, errorDescription(ret));
918 void App::api_deactivate_surface(char const *appid, char const *drawing_name, const reply_func &reply)
925 std::string id = appid;
926 std::string role = drawing_name;
927 std::string area = ""; //drawing_area;
929 if (!app_list->contains(id))
931 reply("app doesn't request 'requestSurface' yet");
934 auto client = app_list->lookUpClient(id);
939 unsigned current = app_list->currentRequestNumber();
940 unsigned requested_num = app_list->getRequestNumber(id);
941 if (requested_num != 0)
943 HMI_SEQ_INFO(requested_num, "%s %s %s request is already queued", id.c_str(), role.c_str(), area.c_str());
944 reply("already requested");
948 WMRequest req = WMRequest(id, role, area, Task::TASK_RELEASE);
949 unsigned new_req = app_list->addAllocateRequest(req);
952 HMI_SEQ_DEBUG(current, "%s start sequence with %s, %s", id.c_str(), role.c_str(), area.c_str());
955 if (new_req != current)
957 // Add request, then invoked after the previous task is finished
958 HMI_SEQ_DEBUG(new_req, "request is accepted");
965 WMError ret = this->do_transition(new_req);
967 if (ret != WMError::SUCCESS)
969 HMI_SEQ_ERROR(new_req, errorDescription(ret));
974 void App::enqueue_flushdraw(int surface_id)
976 this->check_flushdraw(surface_id);
977 HMI_DEBUG("wm", "Enqueuing EndDraw for surface_id %d", surface_id);
978 this->pending_end_draw.push_back(surface_id);
981 void App::check_flushdraw(int surface_id)
983 auto i = std::find(std::begin(this->pending_end_draw),
984 std::end(this->pending_end_draw), surface_id);
985 if (i != std::end(this->pending_end_draw))
987 auto n = this->lookup_name(surface_id);
988 HMI_ERROR("wm", "Application %s (%d) has pending EndDraw call(s)!",
989 n ? n->c_str() : "unknown-name", surface_id);
990 std::swap(this->pending_end_draw[std::distance(
991 std::begin(this->pending_end_draw), i)],
992 this->pending_end_draw.back());
993 this->pending_end_draw.resize(this->pending_end_draw.size() - 1);
997 void App::lm_enddraw(const char *drawing_name)
999 HMI_DEBUG("wm", "end draw %s", drawing_name);
1000 for (unsigned i = 0, iend = this->pending_end_draw.size(); i < iend; i++)
1002 auto n = this->lookup_name(this->pending_end_draw[i]);
1003 if (n && *n == drawing_name)
1005 std::swap(this->pending_end_draw[i], this->pending_end_draw[iend - 1]);
1006 this->pending_end_draw.resize(iend - 1);
1007 this->activate(this->pending_end_draw[i]);
1008 this->emit_flushdraw(drawing_name);
1013 void App::do_enddraw(unsigned req_num)
1016 auto actions = app_list->getActions(req_num);
1017 HMI_SEQ_INFO(req_num, "do endDraw");
1019 for (const auto &act : actions)
1021 HMI_SEQ_DEBUG(req_num, "visible %s", act.role.c_str());
1022 this->lm_enddraw(act.role.c_str());
1025 HMI_SEQ_INFO(req_num, "emit flushDraw");
1029 //emitFlushDrawToAll(&app_list, req_num);
1030 // emit status change event
1031 } while (!app_list->requestFinished());*/
1034 void App::process_request()
1036 unsigned req = app_list->currentRequestNumber();
1037 HMI_SEQ_DEBUG(req, "Do next request");
1038 WMError rc = do_transition(req);
1039 if(rc != WMError::SUCCESS){
1040 HMI_SEQ_ERROR(req, errorDescription(rc));
1044 void App::api_enddraw(char const *appid, char const *drawing_name)
1046 std::string id(appid);
1047 std::string role(drawing_name);
1048 unsigned current_req = app_list->currentRequestNumber();
1049 bool result = app_list->setEndDrawFinished(current_req, id, role);
1053 HMI_ERROR("wm", "%s doesn't have Window Resource", id.c_str());
1057 if (app_list->endDrawFullfilled(current_req))
1059 // do task for endDraw
1060 //this->stop_timer();
1061 this->do_enddraw(current_req);
1065 app_list->removeRequest(current_req);
1066 HMI_SEQ_INFO(current_req, "Finish request");
1068 if (app_list->haveRequest())
1070 this->process_request();
1075 HMI_SEQ_INFO(current_req, "Wait other App call endDraw");
1080 void App::api_ping() { this->dispatch_pending_events(); }
1082 void App::send_event(char const *evname, char const *label)
1084 HMI_DEBUG("wm", "%s: %s(%s)", __func__, evname, label);
1086 json_object *j = json_object_new_object();
1087 json_object_object_add(j, kKeyDrawingName, json_object_new_string(label));
1089 int ret = afb_event_push(this->map_afb_event[evname], j);
1092 HMI_DEBUG("wm", "afb_event_push failed: %m");
1096 void App::send_event(char const *evname, char const *label, char const *area,
1097 int x, int y, int w, int h)
1099 HMI_DEBUG("wm", "%s: %s(%s, %s) x:%d y:%d w:%d h:%d",
1100 __func__, evname, label, area, x, y, w, h);
1102 json_object *j_rect = json_object_new_object();
1103 json_object_object_add(j_rect, kKeyX, json_object_new_int(x));
1104 json_object_object_add(j_rect, kKeyY, json_object_new_int(y));
1105 json_object_object_add(j_rect, kKeyWidth, json_object_new_int(w));
1106 json_object_object_add(j_rect, kKeyHeight, json_object_new_int(h));
1108 json_object *j = json_object_new_object();
1109 json_object_object_add(j, kKeyDrawingName, json_object_new_string(label));
1110 json_object_object_add(j, kKeyDrawingArea, json_object_new_string(area));
1111 json_object_object_add(j, kKeyDrawingRect, j_rect);
1113 int ret = afb_event_push(this->map_afb_event[evname], j);
1116 HMI_DEBUG("wm", "afb_event_push failed: %m");
1123 void App::surface_created(uint32_t surface_id)
1125 // For set role function
1126 HMI_DEBUG("wm", "Get surface pid");
1127 this->controller->get_surface_properties(surface_id);
1129 auto layer_id = this->layers.get_layer_id(surface_id);
1132 HMI_DEBUG("wm", "Newly created surfce %d is not associated with any layer!",
1137 HMI_DEBUG("wm", "surface_id is %u, layer_id is %u", surface_id, *layer_id);
1139 this->controller->layers[*layer_id]->add_surface(surface_id);
1140 this->layout_commit();
1141 // activate the main_surface right away
1142 /*if (surface_id == static_cast<unsigned>(this->layers.main_surface)) {
1143 HMI_DEBUG("wm", "Activating main_surface (%d)", surface_id);
1145 this->api_activate_surface(
1146 this->lookup_name(surface_id).value_or("unknown-name").c_str());
1149 // search pid from surfaceID
1151 // pick up appid from pid from application manager
1153 // check appid then add it to the client
1156 void App::surface_removed(uint32_t surface_id)
1158 HMI_DEBUG("wm", "surface_id is %u", surface_id);
1160 app_list->removeSurface(surface_id);
1163 void App::surface_properties(unsigned surface_id, unsigned pid)
1165 HMI_DEBUG("wm", "get surface properties");
1166 this->app_list->addFloatingSurface(surface_id, pid);
1169 void App::emit_activated(char const *label)
1171 this->send_event(kListEventName[Event_Active], label);
1174 void App::emit_deactivated(char const *label)
1176 this->send_event(kListEventName[Event_Inactive], label);
1179 void App::emit_syncdraw(char const *label, char const *area, int x, int y, int w, int h)
1181 this->send_event(kListEventName[Event_SyncDraw], label, area, x, y, w, h);
1184 void App::emit_flushdraw(char const *label)
1186 this->send_event(kListEventName[Event_FlushDraw], label);
1189 void App::emit_visible(char const *label, bool is_visible)
1191 this->send_event(is_visible ? kListEventName[Event_Visible] : kListEventName[Event_Invisible], label);
1194 void App::emit_invisible(char const *label)
1196 return emit_visible(label, false);
1199 void App::emit_visible(char const *label) { return emit_visible(label, true); }
1201 result<int> App::api_request_surface(char const *appid, char const *drawing_name)
1203 auto lid = this->layers.get_layer_id(std::string(drawing_name));
1207 * register drawing_name as fallback and make it displayed.
1209 lid = this->layers.get_layer_id(std::string("Fallback"));
1210 HMI_DEBUG("wm", "%s is not registered in layers.json, then fallback as normal app", drawing_name);
1213 return Err<int>("Drawing name does not match any role, Fallback is disabled");
1217 auto rname = this->lookup_id(drawing_name);
1220 // name does not exist yet, allocate surface id...
1221 auto id = int(this->id_alloc.generate_id(drawing_name));
1222 this->layers.add_surface(id, *lid);
1224 // set the main_surface[_name] here and now
1225 if (!this->layers.main_surface_name.empty() &&
1226 this->layers.main_surface_name == drawing_name)
1228 this->layers.main_surface = id;
1229 HMI_DEBUG("wm", "Set main_surface id to %u", id);
1232 // add client into the db
1233 std::string appid_str(appid);
1234 std::string role(drawing_name);
1235 //app_list->addClient(appid_str, role);
1236 app_list->addClient(appid_str, *lid, id, role);
1241 // Check currently registered drawing names if it is already there.
1242 return Err<int>("Surface already present");
1245 char const *App::api_request_surface(char const *appid, char const *drawing_name,
1250 auto lid = this->layers.get_layer_id(std::string(drawing_name));
1251 unsigned sid = std::stol(ivi_id);
1256 * register drawing_name as fallback and make it displayed.
1258 lid = this->layers.get_layer_id(std::string("Fallback"));
1259 HMI_DEBUG("wm", "%s is not registered in layers.json, then fallback as normal app", drawing_name);
1262 return "Drawing name does not match any role, Fallback is disabled";
1266 auto rname = this->lookup_id(drawing_name);
1270 return "Surface already present";
1273 // register pair drawing_name and ivi_id
1274 this->id_alloc.register_name_id(drawing_name, sid);
1275 this->layers.add_surface(sid, *lid);
1277 // this surface is already created
1278 HMI_DEBUG("wm", "surface_id is %u, layer_id is %u", sid, *lid);
1280 this->controller->layers[*lid]->add_surface(sid);
1281 this->layout_commit();
1287 * This function is substitute of requestSurface
1288 * If surface creation is faster than application request of this function,
1289 * WM will bind surfaceID with application and role.
1290 * If surface creation is slower than application request of thie function,
1291 * WM will put Client into pending list.
1294 * Application can request with pid but this is temporary solution for now.
1295 * This will be removed.
1297 bool App::api_set_role(char const *appid, char const *drawing_name, unsigned pid){
1298 std::string id = appid;
1299 std::string role = drawing_name;
1300 unsigned surface = 0;
1301 WMError wm_err = WMError::UNKNOWN;
1304 // get layer ID which role should be in
1305 auto lid = this->layers.get_layer_id(role);
1309 * register drawing_name as fallback and make it displayed.
1311 lid = this->layers.get_layer_id(std::string("Fallback"));
1312 HMI_DEBUG("wm", "%s is not registered in layers.json, then fallback as normal app", role.c_str());
1315 HMI_ERROR("wm", "Drawing name does not match any role, Fallback is disabled");
1321 // search floating surfaceID from pid if pid is designated.
1322 // It is not good that application request with its pid
1323 wm_err = app_list->popFloatingSurface(pid, &surface);
1326 // get floating surface with appid. If WM queries appid from pid,
1327 // WM can bind surface and role with appid(not implemented yet)
1328 //wm_err = app_list->popFloatingSurface(id);
1330 if(wm_err != WMError::SUCCESS){
1331 HMI_ERROR("wm", "No floating surface for app: %s", id.c_str());
1332 app_list->addFloatingClient(id, *lid, role);
1333 HMI_NOTICE("wm", "%s : Waiting for surface creation", id.c_str());
1338 if (app_list->contains(id))
1340 HMI_INFO("wm", "Add role: %s with surface: %d. Client %s has multi surfaces.",
1341 role.c_str(), surface, id.c_str());
1342 wm_err = app_list->appendRole(id, role, surface);
1343 if(wm_err != WMError::SUCCESS){
1344 HMI_INFO("wm", errorDescription(wm_err));
1348 HMI_INFO("wm", "Create new client: %s, surface: %d into layer: %d with role: %s",
1349 id.c_str(), surface, *lid, role.c_str());
1350 app_list->addClient(id, *lid, surface, role);
1355 result<json_object *> App::api_get_display_info()
1358 if (!this->controller)
1360 return Err<json_object *>("ivi_controller global not available");
1364 compositor::size o_size = this->controller->output_size;
1365 compositor::size p_size = this->controller->physical_size;
1367 json_object *object = json_object_new_object();
1368 json_object_object_add(object, kKeyWidthPixel, json_object_new_int(o_size.w));
1369 json_object_object_add(object, kKeyHeightPixel, json_object_new_int(o_size.h));
1370 json_object_object_add(object, kKeyWidthMm, json_object_new_int(p_size.w));
1371 json_object_object_add(object, kKeyHeightMm, json_object_new_int(p_size.h));
1373 return Ok<json_object *>(object);
1376 result<json_object *> App::api_get_area_info(char const *drawing_name)
1378 HMI_DEBUG("wm", "called");
1380 // Check drawing name, surface/layer id
1381 auto const &surface_id = this->lookup_id(drawing_name);
1384 return Err<json_object *>("Surface does not exist");
1387 if (!this->controller->surface_exists(*surface_id))
1389 return Err<json_object *>("Surface does not exist in controller!");
1392 auto layer_id = this->layers.get_layer_id(*surface_id);
1395 return Err<json_object *>("Surface is not on any layer!");
1398 auto o_state = *this->layers.get_layout_state(*surface_id);
1399 if (o_state == nullptr)
1401 return Err<json_object *>("Could not find layer for surface");
1404 struct LayoutState &state = *o_state;
1405 if ((state.main != *surface_id) && (state.sub != *surface_id))
1407 return Err<json_object *>("Surface is inactive");
1410 // Set area rectangle
1411 compositor::rect area_info = this->area_info[*surface_id];
1412 json_object *object = json_object_new_object();
1413 json_object_object_add(object, kKeyX, json_object_new_int(area_info.x));
1414 json_object_object_add(object, kKeyY, json_object_new_int(area_info.y));
1415 json_object_object_add(object, kKeyWidth, json_object_new_int(area_info.w));
1416 json_object_object_add(object, kKeyHeight, json_object_new_int(area_info.h));
1418 return Ok<json_object *>(object);
1421 void App::activate(int id)
1423 auto ip = this->controller->sprops.find(id);
1424 if (ip != this->controller->sprops.end())
1426 this->controller->surfaces[id]->set_visibility(1);
1428 this->lookup_name(id).value_or("unknown-name").c_str();
1431 if ((0 == strcmp(label, "Radio")) || (0 == strcmp(label, "MediaPlayer")) || (0 == strcmp(label, "Music")) || (0 == strcmp(label, "Navigation")))
1433 for (auto i = surface_bg.begin(); i != surface_bg.end(); ++i)
1438 this->surface_bg.erase(i);
1440 // Remove from BG layer (999)
1441 HMI_DEBUG("wm", "Remove %s(%d) from BG layer", label, id);
1442 this->controller->layers[999]->remove_surface(id);
1444 // Add to FG layer (1001)
1445 HMI_DEBUG("wm", "Add %s(%d) to FG layer", label, id);
1446 this->controller->layers[1001]->add_surface(id);
1448 for (int j : this->surface_bg)
1450 HMI_DEBUG("wm", "Stored id:%d", j);
1457 this->layout_commit();
1459 this->emit_visible(label);
1460 this->emit_activated(label);
1464 void App::deactivate(int id)
1466 auto ip = this->controller->sprops.find(id);
1467 if (ip != this->controller->sprops.end())
1470 this->lookup_name(id).value_or("unknown-name").c_str();
1473 if ((0 == strcmp(label, "Radio")) || (0 == strcmp(label, "MediaPlayer")) || (0 == strcmp(label, "Music")) || (0 == strcmp(label, "Navigation")))
1477 this->surface_bg.push_back(id);
1479 // Remove from FG layer (1001)
1480 HMI_DEBUG("wm", "Remove %s(%d) from FG layer", label, id);
1481 this->controller->layers[1001]->remove_surface(id);
1483 // Add to BG layer (999)
1484 HMI_DEBUG("wm", "Add %s(%d) to BG layer", label, id);
1485 this->controller->layers[999]->add_surface(id);
1487 for (int j : surface_bg)
1489 HMI_DEBUG("wm", "Stored id:%d", j);
1494 this->controller->surfaces[id]->set_visibility(0);
1498 this->emit_deactivated(label);
1499 this->emit_invisible(label);
1503 void App::deactivate_main_surface()
1505 this->layers.main_surface = -1;
1506 std::string appid = "HomeScreen";
1507 this->api_deactivate_surface(appid.c_str(), this->layers.main_surface_name.c_str(), [](const char *) {});
1510 bool App::can_split(struct LayoutState const &state, int new_id)
1512 if (state.main != -1 && state.main != new_id)
1514 auto new_id_layer = this->layers.get_layer_id(new_id).value();
1515 auto current_id_layer = this->layers.get_layer_id(state.main).value();
1517 // surfaces are on separate layers, don't bother.
1518 if (new_id_layer != current_id_layer)
1523 std::string const &new_id_str = this->lookup_name(new_id).value();
1524 std::string const &cur_id_str = this->lookup_name(state.main).value();
1526 auto const &layer = this->layers.get_layer(new_id_layer);
1528 HMI_DEBUG("wm", "layer info name: %s", layer->name.c_str());
1530 if (layer->layouts.empty())
1535 for (auto i = layer->layouts.cbegin(); i != layer->layouts.cend(); i++)
1537 HMI_DEBUG("wm", "%d main_match '%s'", new_id_layer, i->main_match.c_str());
1538 auto rem = std::regex(i->main_match);
1539 if (std::regex_match(cur_id_str, rem))
1541 // build the second one only if the first already matched
1542 HMI_DEBUG("wm", "%d sub_match '%s'", new_id_layer, i->sub_match.c_str());
1543 auto res = std::regex(i->sub_match);
1544 if (std::regex_match(new_id_str, res))
1546 HMI_DEBUG("wm", "layout matched!");
1556 void App::try_layout(struct LayoutState & /*state*/,
1557 struct LayoutState const &new_layout,
1558 std::function<void(LayoutState const &nl)> apply)
1560 if (this->policy.layout_is_valid(new_layout))
1569 void controller_hooks::surface_created(uint32_t surface_id)
1571 this->app->surface_created(surface_id);
1574 void controller_hooks::surface_removed(uint32_t surface_id)
1576 this->app->surface_removed(surface_id);
1579 void controller_hooks::surface_properties(uint32_t surface_id, uint32_t pid)
1581 this->app->surface_properties(surface_id, pid);
1584 void controller_hooks::surface_visibility(uint32_t /*surface_id*/,
1587 void controller_hooks::surface_destination_rectangle(uint32_t /*surface_id*/,