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);
128 g_app_list.reqDump();
129 if (g_app_list.haveRequest())
131 this->process_request();
135 void App::removeClient(const std::string &appid)
137 HMI_DEBUG("wm", "Remove clinet %s from list", appid.c_str());
138 g_app_list.removeClient(appid);
141 bool App::subscribeEventForApp(const std::string &appid, afb_req req, const std::string &evname)
143 if(!g_app_list.contains(appid)){
144 HMI_DEBUG("wm", "Client %s is not registered", appid.c_str());
147 auto client = g_app_list.lookUpClient(appid);
148 return client->subscribe(req, evname);
154 App::App(wl::display *d)
162 pending_events(false),
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 int App::dispatch_pending_events()
243 if (this->pop_pending_events())
245 this->display->dispatch_pending();
251 bool App::pop_pending_events()
254 return this->pending_events.compare_exchange_strong(
255 x, false, std::memory_order_consume);
258 void App::set_pending_events()
260 this->pending_events.store(true, std::memory_order_release);
263 optional<int> App::lookup_id(char const *name)
265 return this->id_alloc.lookup(std::string(name));
267 optional<std::string> App::lookup_name(int id)
269 return this->id_alloc.lookup(id);
275 int App::init_layers()
277 if (!this->controller)
279 HMI_ERROR("wm", "ivi_controller global not available");
283 if (this->outputs.empty())
285 HMI_ERROR("wm", "no output was set up!");
289 auto &c = this->controller;
291 auto &o = this->outputs.front();
292 auto &s = c->screens.begin()->second;
293 auto &layers = c->layers;
295 // Write output dimensions to ivi controller...
296 c->output_size = compositor::size{uint32_t(o->width), uint32_t(o->height)};
297 c->physical_size = compositor::size{uint32_t(o->physical_width),
298 uint32_t(o->physical_height)};
306 // Quick and dirty setup of layers
307 for (auto const &i : this->layers.mapping)
309 c->layer_create(i.second.layer_id, o->width, o->height);
310 auto &l = layers[i.second.layer_id];
311 l->set_destination_rectangle(0, 0, o->width, o->height);
312 l->set_visibility(1);
313 HMI_DEBUG("wm", "Setting up layer %s (%d) for surface role match \"%s\"",
314 i.second.name.c_str(), i.second.layer_id, i.second.role.c_str());
317 // Add layers to screen
318 s->set_render_order(this->layers.layers);
320 this->layout_commit();
325 void App::surface_set_layout(int surface_id, optional<int> sub_surface_id)
327 if (!this->controller->surface_exists(surface_id))
329 HMI_ERROR("wm", "Surface %d does not exist", surface_id);
333 auto o_layer_id = this->layers.get_layer_id(surface_id);
337 HMI_ERROR("wm", "Surface %d is not associated with any layer!", surface_id);
341 uint32_t layer_id = *o_layer_id;
343 auto const &layer = this->layers.get_layer(layer_id);
344 auto rect = layer.value().rect;
345 auto &s = this->controller->surfaces[surface_id];
352 // less-than-0 values refer to MAX + 1 - $VALUE
353 // e.g. MAX is either screen width or height
356 w = this->controller->output_size.w + 1 + w;
360 h = this->controller->output_size.h + 1 + h;
365 if (o_layer_id != this->layers.get_layer_id(*sub_surface_id))
368 "surface_set_layout: layers of surfaces (%d and %d) don't match!",
369 surface_id, *sub_surface_id);
376 // split along major axis
388 auto &ss = this->controller->surfaces[*sub_surface_id];
390 HMI_DEBUG("wm", "surface_set_layout for sub surface %u on layer %u",
391 *sub_surface_id, layer_id);
393 // set destination to the display rectangle
394 ss->set_destination_rectangle(x + x_off, y + y_off, w, h);
396 this->area_info[*sub_surface_id].x = x;
397 this->area_info[*sub_surface_id].y = y;
398 this->area_info[*sub_surface_id].w = w;
399 this->area_info[*sub_surface_id].h = h;
402 HMI_DEBUG("wm", "surface_set_layout for surface %u on layer %u", surface_id,
405 // set destination to the display rectangle
406 s->set_destination_rectangle(x, y, w, h);
408 // update area information
409 this->area_info[surface_id].x = x;
410 this->area_info[surface_id].y = y;
411 this->area_info[surface_id].w = w;
412 this->area_info[surface_id].h = h;
414 HMI_DEBUG("wm", "Surface %u now on layer %u with rect { %d, %d, %d, %d }",
415 surface_id, layer_id, x, y, w, h);
418 void App::layout_commit()
420 this->controller->commit_changes();
421 this->display->flush();
424 void App::set_timer()
426 HMI_SEQ_DEBUG(g_app_list.currentRequestNumber(), "Timer set activate");
427 if (g_timer_ev_src == nullptr)
429 // firsttime set into sd_event
430 int ret = sd_event_add_time(afb_daemon_get_event_loop(), &g_timer_ev_src,
431 CLOCK_REALTIME, time(NULL) * (1000000UL) + kTimeOut, 1, processTimerHandler, this);
434 HMI_ERROR("wm", "Could't set timer");
439 // update timer limitation after second time
440 sd_event_source_set_time(g_timer_ev_src, time(NULL) * (1000000UL) + kTimeOut);
441 sd_event_source_set_enabled(g_timer_ev_src, SD_EVENT_ONESHOT);
445 void App::stop_timer()
447 unsigned req_num = g_app_list.currentRequestNumber();
448 HMI_SEQ_DEBUG(req_num, "Timer stop");
449 int rc = sd_event_source_set_enabled(g_timer_ev_src, SD_EVENT_OFF);
452 HMI_SEQ_ERROR(req_num, "Timer stop failed");
456 WMError App::lm_release(const struct WMAction &action)
458 //auto const &surface_id = this->lookup_id(drawing_name);
459 WMError ret = WMError::LAYOUT_CHANGE_FAIL;
460 unsigned req_num = g_app_list.currentRequestNumber();
461 auto const &surface_id = this->lookup_id(action.role.c_str());
464 HMI_SEQ_ERROR(req_num, "Surface does not exist");
468 if (*surface_id == this->layers.main_surface)
470 HMI_SEQ_ERROR(req_num, "Cannot deactivate main_surface");
474 auto o_state = *this->layers.get_layout_state(*surface_id);
476 if (o_state == nullptr)
478 HMI_SEQ_ERROR(req_num, "Could not find layer for surface");
482 struct LayoutState &state = *o_state;
484 if (state.main == -1)
486 HMI_SEQ_ERROR(req_num, "No surface active");
490 // Check against main_surface, main_surface_name is the configuration item.
491 if (*surface_id == this->layers.main_surface)
493 HMI_SEQ_DEBUG(req_num, "Refusing to deactivate main_surface %d", *surface_id);
495 return WMError::SUCCESS;
497 if ((state.main == *surface_id) && (state.sub == *surface_id))
499 HMI_SEQ_ERROR(req_num, "Surface is not active");
503 if (state.main == *surface_id)
508 state, LayoutState{state.sub, -1}, [&](LayoutState const &nl) {
509 std::string sub = std::move(*this->lookup_name(state.sub));
511 this->deactivate(*surface_id);
512 this->surface_set_layout(state.sub);
515 this->layout_commit();
516 std::string str_area = std::string(kNameLayoutNormal) + "." + std::string(kNameAreaFull);
517 compositor::rect area_rect = this->area_info[state.sub];
518 this->emit_syncdraw(sub.c_str(), str_area.c_str(),
519 area_rect.x, area_rect.y, area_rect.w, area_rect.h);
520 this->enqueue_flushdraw(state.sub);
525 this->try_layout(state, LayoutState{-1, -1}, [&](LayoutState const &nl) {
526 this->deactivate(*surface_id);
528 this->layout_commit();
532 else if (state.sub == *surface_id)
535 state, LayoutState{state.main, -1}, [&](LayoutState const &nl) {
536 std::string main = std::move(*this->lookup_name(state.main));
538 this->deactivate(*surface_id);
539 this->surface_set_layout(state.main);
542 this->layout_commit();
543 std::string str_area = std::string(kNameLayoutNormal) + "." + std::string(kNameAreaFull);
544 compositor::rect area_rect = this->area_info[state.main];
545 this->emit_syncdraw(main.c_str(), str_area.c_str(),
546 area_rect.x, area_rect.y, area_rect.w, area_rect.h);
547 this->enqueue_flushdraw(state.main);
550 return WMError::SUCCESS;
553 WMError App::lm_layout_change(const struct WMAction &action)
555 const char *msg = this->check_surface_exist(action.role.c_str());
558 lm_.updateLayout(action);
559 TODO: emit syncDraw with application*/
562 HMI_SEQ_DEBUG(g_app_list.currentRequestNumber(), msg);
563 return WMError::LAYOUT_CHANGE_FAIL;
565 this->lm_layout_change(action.role.c_str());
566 return WMError::SUCCESS;
569 WMError App::do_transition(unsigned req_num)
574 // get current trigger
576 bool is_activate = true;
577 auto trigger = g_app_list.getRequest(req_num, &found);
580 WMError err = WMError::NO_ENTRY;
581 HMI_SEQ_ERROR(req_num, errorDescription(err));
585 /* get new status from Policy Manager
587 (json_object*?) newLayout = checkPolicy(trigger);
588 (vector<struct WMAction>&) auto actions = translator.inputActionFromLayout(newLayout, currentLayout)
589 for(const auto& x : actions){
590 g_app_list.setAciton(req_num, x)
595 translator.inputActionFromLayout(newLayout, currentLayout, &g_app_list, req_num);
597 /* The following error check is not necessary because main.cpp will reject the message form not registered object
599 HMI_SEQ_NOTICE(req_num, "ATM, Policy manager does't exist, then set WMAction as is");
601 if (trigger.task == Task::TASK_RELEASE)
605 WMError ret = g_app_list.setAction(req_num, trigger.appid, trigger.role, trigger.area, is_activate);
606 g_app_list.reqDump();
608 if (ret != WMError::SUCCESS)
610 HMI_SEQ_ERROR(req_num, "Failed to set action");
614 // layer manager task
615 bool sync_draw_happen = false;
616 for (const auto &y : g_app_list.getActions(req_num, &found))
620 WMError err = WMError::NO_ENTRY;
621 HMI_SEQ_ERROR(req_num, "%s : Action is not set", errorDescription(err));
628 but current we can't do do_task,
629 so divide the processing into lm_layout_change and lm_release
633 sync_draw_happen = true;
634 ret = lm_layout_change(y);
635 if (ret != WMError::SUCCESS)
637 HMI_SEQ_ERROR(req_num, "%s: appid: %s, role: %s, area: %s",
638 errorDescription(ret), y.appid.c_str(), y.role.c_str(), y.area.c_str());
639 g_app_list.removeRequest(req_num);
641 // TODO: if transition fails, what should we do?
643 /* g_app_list.lookUpClient(y.appid)->emit_syncdraw(y.role, y.area); */
650 HMI_SEQ_ERROR(req_num, "Failed release resource: %s", y.appid.c_str());
651 g_app_list.removeRequest(req_num);
653 // TODO: if transition fails, what should we do?
655 /* g_app_list.lookUpClient(y.appid)->emit_invisible(y.role, y.area); */
659 if (ret != WMError::SUCCESS)
661 //this->emit_error(req_num, 0 /*error_num*/, "error happens"); // test
663 else if (sync_draw_happen)
669 g_app_list.removeRequest(req_num); // HACK!!!
674 void App::lm_layout_change(const char *drawing_name)
676 auto const &surface_id = this->lookup_id(drawing_name);
677 auto layer_id = this->layers.get_layer_id(*surface_id);
678 auto o_state = *this->layers.get_layout_state(*surface_id);
679 struct LayoutState &state = *o_state;
681 // disable layers that are above our current layer
682 for (auto const &l : this->layers.mapping)
684 if (l.second.layer_id <= *layer_id)
690 if (l.second.state.main != -1)
692 this->deactivate(l.second.state.main);
693 l.second.state.main = -1;
697 if (l.second.state.sub != -1)
699 this->deactivate(l.second.state.sub);
700 l.second.state.sub = -1;
706 this->layout_commit();
710 auto layer = this->layers.get_layer(*layer_id);
712 if (state.main == -1)
715 state, LayoutState{*surface_id}, [&](LayoutState const &nl) {
716 HMI_DEBUG("wm", "Layout: %s", kNameLayoutNormal);
717 this->surface_set_layout(*surface_id);
720 // Commit for configuraton
721 this->layout_commit();
723 std::string str_area = std::string(kNameLayoutNormal) + "." + std::string(kNameAreaFull);
724 compositor::rect area_rect = this->area_info[*surface_id];
725 this->emit_syncdraw(drawing_name, str_area.c_str(),
726 area_rect.x, area_rect.y, area_rect.w, area_rect.h);
727 this->enqueue_flushdraw(state.main);
732 if (0 == strcmp(drawing_name, "HomeScreen"))
735 state, LayoutState{*surface_id}, [&](LayoutState const &nl) {
736 HMI_DEBUG("wm", "Layout: %s", kNameLayoutNormal);
737 std::string str_area = std::string(kNameLayoutNormal) + "." + std::string(kNameAreaFull);
738 compositor::rect area_rect = this->area_info[*surface_id];
739 this->emit_syncdraw(drawing_name, str_area.c_str(),
740 area_rect.x, area_rect.y, area_rect.w, area_rect.h);
741 this->enqueue_flushdraw(state.main);
746 bool can_split = this->can_split(state, *surface_id);
752 LayoutState{state.main, *surface_id},
753 [&](LayoutState const &nl) {
754 HMI_DEBUG("wm", "Layout: %s", kNameLayoutSplit);
756 std::move(*this->lookup_name(state.main));
758 this->surface_set_layout(state.main, surface_id);
759 if (state.sub != *surface_id)
763 this->deactivate(state.sub);
768 // Commit for configuration and visibility(0)
769 this->layout_commit();
771 std::string str_area_main = std::string(kNameLayoutSplit) + "." + std::string(kNameAreaMain);
772 std::string str_area_sub = std::string(kNameLayoutSplit) + "." + std::string(kNameAreaSub);
773 compositor::rect area_rect_main = this->area_info[state.main];
774 compositor::rect area_rect_sub = this->area_info[*surface_id];
776 HMI_WARNING("wm", "HACK!!! mediaplayer and hvac is only supported for split");
777 std::string request_role = drawing_name;
778 //std::string request_app = transform(request_role.begin(), request_role.end(), request_role.begin(), tolower); //hvac or mediaplayer
779 std::string hack_appid = "navigation";
780 std::string hack_role = main;
781 std::string hack_area = str_area_main;
782 g_app_list.setAction(g_app_list.currentRequestNumber(), hack_appid, hack_role, hack_area, true);
783 //g_app_list.setEndDrawFinished(g_app_list.currentRequestNumber(), request_role, request_role);
784 //g_app_list.setEndDrawFinished(g_app_list.currentRequestNumber(), hack_appid, hack_role); // This process is illegal
786 this->emit_syncdraw(main.c_str(), str_area_main.c_str(),
787 area_rect_main.x, area_rect_main.y,
788 area_rect_main.w, area_rect_main.h);
789 this->emit_syncdraw(request_role.c_str(), str_area_sub.c_str(),
790 area_rect_sub.x, area_rect_sub.y,
791 area_rect_sub.w, area_rect_sub.h);
792 this->enqueue_flushdraw(state.main);
793 this->enqueue_flushdraw(state.sub);
799 state, LayoutState{*surface_id}, [&](LayoutState const &nl) {
800 HMI_DEBUG("wm", "Layout: %s", kNameLayoutNormal);
802 this->surface_set_layout(*surface_id);
803 if (state.main != *surface_id)
805 this->deactivate(state.main);
809 if (state.sub != *surface_id)
811 this->deactivate(state.sub);
816 // Commit for configuraton and visibility(0)
817 this->layout_commit();
819 std::string str_area = std::string(kNameLayoutNormal) + "." + std::string(kNameAreaFull);
820 compositor::rect area_rect = this->area_info[*surface_id];
821 this->emit_syncdraw(drawing_name, str_area.c_str(),
822 area_rect.x, area_rect.y, area_rect.w, area_rect.h);
823 this->enqueue_flushdraw(state.main);
830 const char *App::check_surface_exist(const char *drawing_name)
832 auto const &surface_id = this->lookup_id(drawing_name);
835 //reply("Surface does not exist");
836 return "Surface does not exist";
839 if (!this->controller->surface_exists(*surface_id))
841 //reply("Surface does not exist in controller!");
842 return "Surface does not exist in controller!";
845 auto layer_id = this->layers.get_layer_id(*surface_id);
849 //reply("Surface is not on any layer!");
850 return "Surface is not on any layer!";
853 auto o_state = *this->layers.get_layout_state(*surface_id);
855 if (o_state == nullptr)
857 //reply("Could not find layer for surface");
858 return "Could not find layer for surface";
861 HMI_DEBUG("wm", "surface %d is detected", *surface_id);
866 void App::api_activate_surface(char const *appid, char const *drawing_name, char const *drawing_area, const reply_func &reply)
874 std::string id = appid;
875 std::string role = drawing_name;
876 std::string area = drawing_area;
878 if (!g_app_list.contains(id))
880 reply("app doesn't request 'requestSurface' yet");
884 auto client = g_app_list.lookUpClient(id);
889 unsigned current = g_app_list.currentRequestNumber();
890 unsigned requested_num = g_app_list.getRequestNumber(id);
891 if (requested_num != 0)
893 HMI_SEQ_INFO(requested_num, "%s %s %s request is already queued", id.c_str(), role.c_str(), area.c_str());
894 reply("already requested");
898 WMRequest req = WMRequest(id, role, area, Task::TASK_ALLOCATE);
899 unsigned new_req = g_app_list.addAllocateRequest(req);
900 g_app_list.reqDump();
902 HMI_SEQ_DEBUG(current, "%s start sequence with %s, %s", id.c_str(), role.c_str(), area.c_str());
905 if (new_req != current)
907 // Add request, then invoked after the previous task is finished
908 HMI_SEQ_DEBUG(new_req, "request is accepted");
915 WMError ret = this->do_transition(new_req);
917 if (ret != WMError::SUCCESS)
919 HMI_SEQ_ERROR(new_req, errorDescription(ret));
924 void App::api_deactivate_surface(char const *appid, char const *drawing_name, const reply_func &reply)
931 std::string id = appid;
932 std::string role = drawing_name;
933 std::string area = ""; //drawing_area;
935 if (!g_app_list.contains(id))
937 reply("app doesn't request 'requestSurface' yet");
940 auto client = g_app_list.lookUpClient(id);
945 unsigned current = g_app_list.currentRequestNumber();
946 unsigned requested_num = g_app_list.getRequestNumber(id);
947 if (requested_num != 0)
949 HMI_SEQ_INFO(requested_num, "%s %s %s request is already queued", id.c_str(), role.c_str(), area.c_str());
950 reply("already requested");
954 WMRequest req = WMRequest(id, role, area, Task::TASK_RELEASE);
955 unsigned new_req = g_app_list.addAllocateRequest(req);
956 g_app_list.reqDump();
958 HMI_SEQ_DEBUG(current, "%s start sequence with %s, %s", id.c_str(), role.c_str(), area.c_str());
961 if (new_req != current)
963 // Add request, then invoked after the previous task is finished
964 HMI_SEQ_DEBUG(new_req, "request is accepted");
971 WMError ret = this->do_transition(new_req);
973 if (ret != WMError::SUCCESS)
975 HMI_SEQ_ERROR(new_req, errorDescription(ret));
980 void App::enqueue_flushdraw(int surface_id)
982 this->check_flushdraw(surface_id);
983 HMI_DEBUG("wm", "Enqueuing EndDraw for surface_id %d", surface_id);
984 this->pending_end_draw.push_back(surface_id);
987 void App::check_flushdraw(int surface_id)
989 auto i = std::find(std::begin(this->pending_end_draw),
990 std::end(this->pending_end_draw), surface_id);
991 if (i != std::end(this->pending_end_draw))
993 auto n = this->lookup_name(surface_id);
994 HMI_ERROR("wm", "Application %s (%d) has pending EndDraw call(s)!",
995 n ? n->c_str() : "unknown-name", surface_id);
996 std::swap(this->pending_end_draw[std::distance(
997 std::begin(this->pending_end_draw), i)],
998 this->pending_end_draw.back());
999 this->pending_end_draw.resize(this->pending_end_draw.size() - 1);
1003 void App::lm_enddraw(const char *drawing_name)
1005 HMI_DEBUG("wm", "end draw %s", drawing_name);
1006 for (unsigned i = 0, iend = this->pending_end_draw.size(); i < iend; i++)
1008 auto n = this->lookup_name(this->pending_end_draw[i]);
1009 if (n && *n == drawing_name)
1011 std::swap(this->pending_end_draw[i], this->pending_end_draw[iend - 1]);
1012 this->pending_end_draw.resize(iend - 1);
1013 this->activate(this->pending_end_draw[i]);
1014 this->emit_flushdraw(drawing_name);
1019 void App::do_enddraw(unsigned req_num)
1023 auto actions = g_app_list.getActions(req_num, &found);
1026 WMError err = WMError::NO_ENTRY;
1027 HMI_SEQ_ERROR(req_num, errorDescription(err));
1031 HMI_SEQ_INFO(req_num, "do endDraw");
1033 for (const auto &act : actions)
1035 HMI_SEQ_DEBUG(req_num, "visible %s", act.role.c_str());
1036 this->lm_enddraw(act.role.c_str());
1039 HMI_SEQ_INFO(req_num, "emit flushDraw");
1043 //emitFlushDrawToAll(&g_app_list, req_num);
1044 // emit status change event
1045 } while (!g_app_list.requestFinished());*/
1048 void App::process_request()
1050 unsigned req = g_app_list.currentRequestNumber();
1051 HMI_SEQ_DEBUG(req, "Do next request");
1052 WMError rc = do_transition(req);
1053 if(rc != WMError::SUCCESS){
1054 HMI_SEQ_ERROR(req, errorDescription(rc));
1058 void App::api_enddraw(char const *appid, char const *drawing_name)
1060 std::string id(appid);
1061 std::string role(drawing_name);
1062 unsigned current_req = g_app_list.currentRequestNumber();
1063 bool result = g_app_list.setEndDrawFinished(current_req, id, role);
1067 HMI_ERROR("wm", "%s doesn't have Window Resource", id.c_str());
1071 if (g_app_list.endDrawFullfilled(current_req))
1073 // do task for endDraw
1074 //this->stop_timer();
1075 this->do_enddraw(current_req);
1079 g_app_list.removeRequest(current_req);
1080 HMI_SEQ_INFO(current_req, "Finish request");
1082 if (g_app_list.haveRequest())
1084 this->process_request();
1089 HMI_SEQ_INFO(current_req, "Wait other App call endDraw");
1094 void App::api_ping() { this->dispatch_pending_events(); }
1096 void App::send_event(char const *evname, char const *label)
1098 HMI_DEBUG("wm", "%s: %s(%s)", __func__, evname, label);
1100 json_object *j = json_object_new_object();
1101 json_object_object_add(j, kKeyDrawingName, json_object_new_string(label));
1103 int ret = afb_event_push(this->map_afb_event[evname], j);
1106 HMI_DEBUG("wm", "afb_event_push failed: %m");
1110 void App::send_event(char const *evname, char const *label, char const *area,
1111 int x, int y, int w, int h)
1113 HMI_DEBUG("wm", "%s: %s(%s, %s) x:%d y:%d w:%d h:%d",
1114 __func__, evname, label, area, x, y, w, h);
1116 json_object *j_rect = json_object_new_object();
1117 json_object_object_add(j_rect, kKeyX, json_object_new_int(x));
1118 json_object_object_add(j_rect, kKeyY, json_object_new_int(y));
1119 json_object_object_add(j_rect, kKeyWidth, json_object_new_int(w));
1120 json_object_object_add(j_rect, kKeyHeight, json_object_new_int(h));
1122 json_object *j = json_object_new_object();
1123 json_object_object_add(j, kKeyDrawingName, json_object_new_string(label));
1124 json_object_object_add(j, kKeyDrawingArea, json_object_new_string(area));
1125 json_object_object_add(j, kKeyDrawingRect, j_rect);
1127 int ret = afb_event_push(this->map_afb_event[evname], j);
1130 HMI_DEBUG("wm", "afb_event_push failed: %m");
1137 void App::surface_created(uint32_t surface_id)
1139 auto layer_id = this->layers.get_layer_id(surface_id);
1142 HMI_DEBUG("wm", "Newly created surfce %d is not associated with any layer!",
1147 HMI_DEBUG("wm", "surface_id is %u, layer_id is %u", surface_id, *layer_id);
1149 this->controller->layers[*layer_id]->add_surface(surface_id);
1150 this->layout_commit();
1151 // activate the main_surface right away
1152 /*if (surface_id == static_cast<unsigned>(this->layers.main_surface)) {
1153 HMI_DEBUG("wm", "Activating main_surface (%d)", surface_id);
1155 this->api_activate_surface(
1156 this->lookup_name(surface_id).value_or("unknown-name").c_str());
1159 // search pid from surfaceID
1161 // pick up appid from pid from application manager
1163 // check appid then add it to the client
1166 void App::surface_removed(uint32_t surface_id)
1168 HMI_DEBUG("wm", "surface_id is %u", surface_id);
1170 g_app_list.removeSurface(surface_id);
1173 void App::emit_activated(char const *label)
1175 this->send_event(kListEventName[Event_Active], label);
1178 void App::emit_deactivated(char const *label)
1180 this->send_event(kListEventName[Event_Inactive], label);
1183 void App::emit_syncdraw(char const *label, char const *area, int x, int y, int w, int h)
1185 this->send_event(kListEventName[Event_SyncDraw], label, area, x, y, w, h);
1188 void App::emit_flushdraw(char const *label)
1190 this->send_event(kListEventName[Event_FlushDraw], label);
1193 void App::emit_visible(char const *label, bool is_visible)
1195 this->send_event(is_visible ? kListEventName[Event_Visible] : kListEventName[Event_Invisible], label);
1198 void App::emit_invisible(char const *label)
1200 return emit_visible(label, false);
1203 void App::emit_visible(char const *label) { return emit_visible(label, true); }
1205 result<int> App::api_request_surface(char const *appid, char const *drawing_name)
1207 auto lid = this->layers.get_layer_id(std::string(drawing_name));
1211 * register drawing_name as fallback and make it displayed.
1213 lid = this->layers.get_layer_id(std::string("Fallback"));
1214 HMI_DEBUG("wm", "%s is not registered in layers.json, then fallback as normal app", drawing_name);
1217 return Err<int>("Drawing name does not match any role, Fallback is disabled");
1221 auto rname = this->lookup_id(drawing_name);
1224 // name does not exist yet, allocate surface id...
1225 auto id = int(this->id_alloc.generate_id(drawing_name));
1226 this->layers.add_surface(id, *lid);
1228 // set the main_surface[_name] here and now
1229 if (!this->layers.main_surface_name.empty() &&
1230 this->layers.main_surface_name == drawing_name)
1232 this->layers.main_surface = id;
1233 HMI_DEBUG("wm", "Set main_surface id to %u", id);
1236 // add client into the db
1237 std::string appid_str(appid);
1238 std::string role(drawing_name);
1239 //g_app_list.addClient(appid_str, role);
1240 g_app_list.addClient(appid_str, *lid, id, role);
1245 // Check currently registered drawing names if it is already there.
1246 return Err<int>("Surface already present");
1249 char const *App::api_request_surface(char const *appid, char const *drawing_name,
1254 auto lid = this->layers.get_layer_id(std::string(drawing_name));
1255 unsigned sid = std::stol(ivi_id);
1260 * register drawing_name as fallback and make it displayed.
1262 lid = this->layers.get_layer_id(std::string("Fallback"));
1263 HMI_DEBUG("wm", "%s is not registered in layers.json, then fallback as normal app", drawing_name);
1266 return "Drawing name does not match any role, Fallback is disabled";
1270 auto rname = this->lookup_id(drawing_name);
1274 return "Surface already present";
1277 // register pair drawing_name and ivi_id
1278 this->id_alloc.register_name_id(drawing_name, sid);
1279 this->layers.add_surface(sid, *lid);
1281 // this surface is already created
1282 HMI_DEBUG("wm", "surface_id is %u, layer_id is %u", sid, *lid);
1284 this->controller->layers[*lid]->add_surface(sid);
1285 this->layout_commit();
1290 result<json_object *> App::api_get_display_info()
1293 if (!this->controller)
1295 return Err<json_object *>("ivi_controller global not available");
1299 compositor::size o_size = this->controller->output_size;
1300 compositor::size p_size = this->controller->physical_size;
1302 json_object *object = json_object_new_object();
1303 json_object_object_add(object, kKeyWidthPixel, json_object_new_int(o_size.w));
1304 json_object_object_add(object, kKeyHeightPixel, json_object_new_int(o_size.h));
1305 json_object_object_add(object, kKeyWidthMm, json_object_new_int(p_size.w));
1306 json_object_object_add(object, kKeyHeightMm, json_object_new_int(p_size.h));
1308 return Ok<json_object *>(object);
1311 result<json_object *> App::api_get_area_info(char const *drawing_name)
1313 HMI_DEBUG("wm", "called");
1315 // Check drawing name, surface/layer id
1316 auto const &surface_id = this->lookup_id(drawing_name);
1319 return Err<json_object *>("Surface does not exist");
1322 if (!this->controller->surface_exists(*surface_id))
1324 return Err<json_object *>("Surface does not exist in controller!");
1327 auto layer_id = this->layers.get_layer_id(*surface_id);
1330 return Err<json_object *>("Surface is not on any layer!");
1333 auto o_state = *this->layers.get_layout_state(*surface_id);
1334 if (o_state == nullptr)
1336 return Err<json_object *>("Could not find layer for surface");
1339 struct LayoutState &state = *o_state;
1340 if ((state.main != *surface_id) && (state.sub != *surface_id))
1342 return Err<json_object *>("Surface is inactive");
1345 // Set area rectangle
1346 compositor::rect area_info = this->area_info[*surface_id];
1347 json_object *object = json_object_new_object();
1348 json_object_object_add(object, kKeyX, json_object_new_int(area_info.x));
1349 json_object_object_add(object, kKeyY, json_object_new_int(area_info.y));
1350 json_object_object_add(object, kKeyWidth, json_object_new_int(area_info.w));
1351 json_object_object_add(object, kKeyHeight, json_object_new_int(area_info.h));
1353 return Ok<json_object *>(object);
1356 void App::activate(int id)
1358 auto ip = this->controller->sprops.find(id);
1359 if (ip != this->controller->sprops.end())
1361 this->controller->surfaces[id]->set_visibility(1);
1363 this->lookup_name(id).value_or("unknown-name").c_str();
1366 if ((0 == strcmp(label, "Radio")) || (0 == strcmp(label, "MediaPlayer")) || (0 == strcmp(label, "Music")) || (0 == strcmp(label, "Navigation")))
1368 for (auto i = surface_bg.begin(); i != surface_bg.end(); ++i)
1373 this->surface_bg.erase(i);
1375 // Remove from BG layer (999)
1376 HMI_DEBUG("wm", "Remove %s(%d) from BG layer", label, id);
1377 this->controller->layers[999]->remove_surface(id);
1379 // Add to FG layer (1001)
1380 HMI_DEBUG("wm", "Add %s(%d) to FG layer", label, id);
1381 this->controller->layers[1001]->add_surface(id);
1383 for (int j : this->surface_bg)
1385 HMI_DEBUG("wm", "Stored id:%d", j);
1392 this->layout_commit();
1394 this->emit_visible(label);
1395 this->emit_activated(label);
1399 void App::deactivate(int id)
1401 auto ip = this->controller->sprops.find(id);
1402 if (ip != this->controller->sprops.end())
1405 this->lookup_name(id).value_or("unknown-name").c_str();
1408 if ((0 == strcmp(label, "Radio")) || (0 == strcmp(label, "MediaPlayer")) || (0 == strcmp(label, "Music")) || (0 == strcmp(label, "Navigation")))
1412 this->surface_bg.push_back(id);
1414 // Remove from FG layer (1001)
1415 HMI_DEBUG("wm", "Remove %s(%d) from FG layer", label, id);
1416 this->controller->layers[1001]->remove_surface(id);
1418 // Add to BG layer (999)
1419 HMI_DEBUG("wm", "Add %s(%d) to BG layer", label, id);
1420 this->controller->layers[999]->add_surface(id);
1422 for (int j : surface_bg)
1424 HMI_DEBUG("wm", "Stored id:%d", j);
1429 this->controller->surfaces[id]->set_visibility(0);
1433 this->emit_deactivated(label);
1434 this->emit_invisible(label);
1438 void App::deactivate_main_surface()
1440 this->layers.main_surface = -1;
1441 std::string appid = "HomeScreen";
1442 this->api_deactivate_surface(appid.c_str(), this->layers.main_surface_name.c_str(), [](const char *) {});
1445 bool App::can_split(struct LayoutState const &state, int new_id)
1447 if (state.main != -1 && state.main != new_id)
1449 auto new_id_layer = this->layers.get_layer_id(new_id).value();
1450 auto current_id_layer = this->layers.get_layer_id(state.main).value();
1452 // surfaces are on separate layers, don't bother.
1453 if (new_id_layer != current_id_layer)
1458 std::string const &new_id_str = this->lookup_name(new_id).value();
1459 std::string const &cur_id_str = this->lookup_name(state.main).value();
1461 auto const &layer = this->layers.get_layer(new_id_layer);
1463 HMI_DEBUG("wm", "layer info name: %s", layer->name.c_str());
1465 if (layer->layouts.empty())
1470 for (auto i = layer->layouts.cbegin(); i != layer->layouts.cend(); i++)
1472 HMI_DEBUG("wm", "%d main_match '%s'", new_id_layer, i->main_match.c_str());
1473 auto rem = std::regex(i->main_match);
1474 if (std::regex_match(cur_id_str, rem))
1476 // build the second one only if the first already matched
1477 HMI_DEBUG("wm", "%d sub_match '%s'", new_id_layer, i->sub_match.c_str());
1478 auto res = std::regex(i->sub_match);
1479 if (std::regex_match(new_id_str, res))
1481 HMI_DEBUG("wm", "layout matched!");
1491 void App::try_layout(struct LayoutState & /*state*/,
1492 struct LayoutState const &new_layout,
1493 std::function<void(LayoutState const &nl)> apply)
1495 if (this->policy.layout_is_valid(new_layout))
1504 void controller_hooks::surface_created(uint32_t surface_id)
1506 this->app->surface_created(surface_id);
1509 void controller_hooks::surface_removed(uint32_t surface_id)
1511 this->app->surface_removed(surface_id);
1514 void controller_hooks::surface_visibility(uint32_t /*surface_id*/,
1517 void controller_hooks::surface_destination_rectangle(uint32_t /*surface_id*/,