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 "windowmanager-client.hpp"
39 #include "applist.hpp"
43 #include <systemd/sd-event.h>
49 const unsigned TIME_OUT = 1000000UL; /* 1s */
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 const std::string task_allocate = "allocate";
72 static const std::string task_release = "release";
73 static sd_event_source *timer_ev_src = nullptr;
75 static AppList app_list;
82 result<json> file_to_json(char const *filename)
85 std::ifstream i(filename);
88 HMI_DEBUG("wm", "Could not open config file, so use default layer information");
89 j = default_layers_json;
99 struct result<layer_map> load_layer_map(char const *filename)
101 HMI_DEBUG("wm", "loading IDs from %s", filename);
103 auto j = file_to_json(filename);
106 return Err<layer_map>(j.unwrap_err());
108 json jids = j.unwrap();
110 return to_layer_map(jids);
114 processTimerHandler(sd_event_source *s, uint64_t usec, void *userdata)
116 HMI_NOTICE("wm", "Time out occurs because the client replys endDraw slow, so revert the request");
117 reinterpret_cast<wm::App *>(userdata)->timerHandler();
123 void App::timerHandler()
125 unsigned seq = app_list.currentSequenceNumber();
126 HMI_SEQ_DEBUG(seq, "Timer expired remove Request");
128 app_list.removeRequest(seq);
131 if (app_list.haveRequest())
133 this->process_request();
140 App::App(wl::display *d)
148 pending_events(false),
154 auto l = load_layer_map(
155 this->config.get_string("layers.json").value().c_str());
158 this->layers = l.unwrap();
162 HMI_ERROR("wm", "%s", l.err().value());
166 catch (std::exception &e)
168 HMI_ERROR("wm", "Loading of configuration failed: %s", e.what());
174 if (!this->display->ok())
179 if (this->layers.mapping.empty())
181 HMI_ERROR("wm", "No surface -> layer mapping loaded");
186 for (int i = Event_Val_Min; i <= Event_Val_Max; i++)
188 map_afb_event[kListEventName[i]] = afb_daemon_make_event(kListEventName[i]);
191 this->display->add_global_handler(
192 "wl_output", [this](wl_registry *r, uint32_t name, uint32_t v) {
193 this->outputs.emplace_back(std::make_unique<wl::output>(r, name, v));
196 this->display->add_global_handler(
197 "ivi_wm", [this](wl_registry *r, uint32_t name, uint32_t v) {
199 std::make_unique<struct compositor::controller>(r, name, v);
201 // Init controller hooks
202 this->controller->chooks = &this->chooks;
204 // This protocol needs the output, so lets just add our mapping here...
205 this->controller->add_proxy_to_id_mapping(
206 this->outputs.back()->proxy.get(),
207 wl_proxy_get_id(reinterpret_cast<struct wl_proxy *>(
208 this->outputs.back()->proxy.get())));
211 this->controller->create_screen(this->outputs.back()->proxy.get());
213 // Set display to controller
214 this->controller->display = this->display;
217 // First level objects
218 this->display->roundtrip();
219 // Second level objects
220 this->display->roundtrip();
221 // Third level objects
222 this->display->roundtrip();
224 return init_layers();
227 int App::dispatch_pending_events()
229 if (this->pop_pending_events())
231 this->display->dispatch_pending();
237 bool App::pop_pending_events()
240 return this->pending_events.compare_exchange_strong(
241 x, false, std::memory_order_consume);
244 void App::set_pending_events()
246 this->pending_events.store(true, std::memory_order_release);
249 optional<int> App::lookup_id(char const *name)
251 return this->id_alloc.lookup(std::string(name));
253 optional<std::string> App::lookup_name(int id)
255 return this->id_alloc.lookup(id);
261 int App::init_layers()
263 if (!this->controller)
265 HMI_ERROR("wm", "ivi_controller global not available");
269 if (this->outputs.empty())
271 HMI_ERROR("wm", "no output was set up!");
275 auto &c = this->controller;
277 auto &o = this->outputs.front();
278 auto &s = c->screens.begin()->second;
279 auto &layers = c->layers;
281 // Write output dimensions to ivi controller...
282 c->output_size = compositor::size{uint32_t(o->width), uint32_t(o->height)};
283 c->physical_size = compositor::size{uint32_t(o->physical_width),
284 uint32_t(o->physical_height)};
292 // Quick and dirty setup of layers
293 for (auto const &i : this->layers.mapping)
295 c->layer_create(i.second.layer_id, o->width, o->height);
296 auto &l = layers[i.second.layer_id];
297 l->set_destination_rectangle(0, 0, o->width, o->height);
298 l->set_visibility(1);
299 HMI_DEBUG("wm", "Setting up layer %s (%d) for surface role match \"%s\"",
300 i.second.name.c_str(), i.second.layer_id, i.second.role.c_str());
303 // Add layers to screen
304 s->set_render_order(this->layers.layers);
306 this->layout_commit();
311 void App::surface_set_layout(int surface_id, optional<int> sub_surface_id)
313 if (!this->controller->surface_exists(surface_id))
315 HMI_ERROR("wm", "Surface %d does not exist", surface_id);
319 auto o_layer_id = this->layers.get_layer_id(surface_id);
323 HMI_ERROR("wm", "Surface %d is not associated with any layer!", surface_id);
327 uint32_t layer_id = *o_layer_id;
329 auto const &layer = this->layers.get_layer(layer_id);
330 auto rect = layer.value().rect;
331 auto &s = this->controller->surfaces[surface_id];
338 // less-than-0 values refer to MAX + 1 - $VALUE
339 // e.g. MAX is either screen width or height
342 w = this->controller->output_size.w + 1 + w;
346 h = this->controller->output_size.h + 1 + h;
351 if (o_layer_id != this->layers.get_layer_id(*sub_surface_id))
354 "surface_set_layout: layers of surfaces (%d and %d) don't match!",
355 surface_id, *sub_surface_id);
362 // split along major axis
374 auto &ss = this->controller->surfaces[*sub_surface_id];
376 HMI_DEBUG("wm", "surface_set_layout for sub surface %u on layer %u",
377 *sub_surface_id, layer_id);
379 // set destination to the display rectangle
380 ss->set_destination_rectangle(x + x_off, y + y_off, w, h);
382 this->area_info[*sub_surface_id].x = x;
383 this->area_info[*sub_surface_id].y = y;
384 this->area_info[*sub_surface_id].w = w;
385 this->area_info[*sub_surface_id].h = h;
388 HMI_DEBUG("wm", "surface_set_layout for surface %u on layer %u", surface_id,
391 // set destination to the display rectangle
392 s->set_destination_rectangle(x, y, w, h);
394 // update area information
395 this->area_info[surface_id].x = x;
396 this->area_info[surface_id].y = y;
397 this->area_info[surface_id].w = w;
398 this->area_info[surface_id].h = h;
400 HMI_DEBUG("wm", "Surface %u now on layer %u with rect { %d, %d, %d, %d }",
401 surface_id, layer_id, x, y, w, h);
404 void App::layout_commit()
406 this->controller->commit_changes();
407 this->display->flush();
410 void App::set_timer(){
411 HMI_SEQ_DEBUG(app_list.currentSequenceNumber(), "Timer set activate");
412 if (timer_ev_src == nullptr)
414 // firsttime set into sd_event
415 int ret = sd_event_add_time(afb_daemon_get_event_loop(), &timer_ev_src,
416 CLOCK_REALTIME, time(NULL) * (1000000UL) + TIME_OUT, 0, processTimerHandler, this);
419 HMI_ERROR("wm", "Could't set timer");
424 // update timer limitation after second time
425 sd_event_source_set_time(timer_ev_src, time(NULL) * (1000000UL) + TIME_OUT);
426 sd_event_source_set_enabled(timer_ev_src, SD_EVENT_ONESHOT);
430 void App::stop_timer(){
431 unsigned seq = app_list.currentSequenceNumber();
432 HMI_SEQ_DEBUG(seq, "Timer stop");
433 int rc = sd_event_source_set_enabled(timer_ev_src, SD_EVENT_OFF);
435 HMI_SEQ_ERROR(seq, "Timer stop failed");
439 bool App::lm_release(const struct WMAction &action)
444 bool App::lm_layout_change(const struct WMAction &action)
446 const char *msg = this->check_surface_exist(action.role.c_str());
449 lm_.updateLayout(action);
450 TODO: emit syncDraw with application*/
453 HMI_SEQ_DEBUG(app_list.currentSequenceNumber(), msg);
454 //app_list.removeRequest(req_num);
457 this->lm_layout_change(action.role.c_str());
461 bool App::do_transition(unsigned req_num)
466 // get current trigger
467 auto trigger = app_list.getRequest(req_num);
468 bool is_activate = true;
470 /* get new status from Policy Manager
472 (json_object*?) newLayout = checkPolicy(trigger);
473 (vector<struct WMAction>&) auto actions = translator.inputActionFromLayout(newLayout, currentLayout)
474 for(const auto& x : actions){
475 app_list.setAciton(req_num, x)
480 translator.inputActionFromLayout(newLayout, currentLayout, &app_list, req_num);
482 /* The following error check is not necessary because main.cpp will reject the message form not registered object
484 HMI_SEQ_NOTICE(req_num, "ATM, Policy manager does't exist, then set WMAction as is");
486 if (TASK_RELEASE == trigger.task)
490 bool ret = app_list.setAction(req_num, trigger.appid, trigger.role, trigger.area, is_activate);
494 HMI_SEQ_ERROR(req_num, "Failed to set action");
498 /*client->emit_syncdraw(role); */
500 // layer manager task
501 for (const auto &y : app_list.getActions(req_num))
503 // do_task(y); but current we can't do this
506 ret = lm_layout_change(y);
508 HMI_SEQ_ERROR(req_num, "Failed layout change: %s", y.appid.c_str());
509 app_list.removeRequest(req_num);
511 // TODO: if transition fails, what should we do?
518 HMI_SEQ_ERROR(req_num, "Failed release resource: %s", y.appid.c_str());
519 app_list.removeRequest(req_num);
521 // TODO: if transition fails, what should we do?
529 //this->emit_error(request_seq, 0 /*error_num*/, "error happens"); // test
534 void App::lm_layout_change(const char* drawing_name)
536 auto const &surface_id = this->lookup_id(drawing_name);
537 auto layer_id = this->layers.get_layer_id(*surface_id);
538 auto o_state = *this->layers.get_layout_state(*surface_id);
539 struct LayoutState &state = *o_state;
541 // disable layers that are above our current layer
542 for (auto const &l : this->layers.mapping)
544 if (l.second.layer_id <= *layer_id)
550 if (l.second.state.main != -1)
552 this->deactivate(l.second.state.main);
553 l.second.state.main = -1;
557 if (l.second.state.sub != -1)
559 this->deactivate(l.second.state.sub);
560 l.second.state.sub = -1;
566 this->layout_commit();
570 auto layer = this->layers.get_layer(*layer_id);
572 if (state.main == -1)
575 state, LayoutState{*surface_id}, [&](LayoutState const &nl) {
576 HMI_DEBUG("wm", "Layout: %s", kNameLayoutNormal);
577 this->surface_set_layout(*surface_id);
580 // Commit for configuraton
581 this->layout_commit();
583 std::string str_area = std::string(kNameLayoutNormal) + "." + std::string(kNameAreaFull);
584 compositor::rect area_rect = this->area_info[*surface_id];
585 this->emit_syncdraw(drawing_name, str_area.c_str(),
586 area_rect.x, area_rect.y, area_rect.w, area_rect.h);
587 this->enqueue_flushdraw(state.main);
592 if (0 == strcmp(drawing_name, "HomeScreen"))
595 state, LayoutState{*surface_id}, [&](LayoutState const &nl) {
596 HMI_DEBUG("wm", "Layout: %s", kNameLayoutNormal);
597 std::string str_area = std::string(kNameLayoutNormal) + "." + std::string(kNameAreaFull);
598 compositor::rect area_rect = this->area_info[*surface_id];
599 this->emit_syncdraw(drawing_name, str_area.c_str(),
600 area_rect.x, area_rect.y, area_rect.w, area_rect.h);
601 this->enqueue_flushdraw(state.main);
606 bool can_split = this->can_split(state, *surface_id);
612 LayoutState{state.main, *surface_id},
613 [&](LayoutState const &nl) {
614 HMI_DEBUG("wm", "Layout: %s", kNameLayoutSplit);
616 std::move(*this->lookup_name(state.main));
618 this->surface_set_layout(state.main, surface_id);
619 if (state.sub != *surface_id)
623 this->deactivate(state.sub);
628 // Commit for configuration and visibility(0)
629 this->layout_commit();
631 std::string str_area_main = std::string(kNameLayoutSplit) + "." + std::string(kNameAreaMain);
632 std::string str_area_sub = std::string(kNameLayoutSplit) + "." + std::string(kNameAreaSub);
633 compositor::rect area_rect_main = this->area_info[state.main];
634 compositor::rect area_rect_sub = this->area_info[*surface_id];
635 this->emit_syncdraw(main.c_str(), str_area_main.c_str(),
636 area_rect_main.x, area_rect_main.y,
637 area_rect_main.w, area_rect_main.h);
638 this->emit_syncdraw(drawing_name, str_area_sub.c_str(),
639 area_rect_sub.x, area_rect_sub.y,
640 area_rect_sub.w, area_rect_sub.h);
641 this->enqueue_flushdraw(state.main);
642 this->enqueue_flushdraw(state.sub);
648 state, LayoutState{*surface_id}, [&](LayoutState const &nl) {
649 HMI_DEBUG("wm", "Layout: %s", kNameLayoutNormal);
651 this->surface_set_layout(*surface_id);
652 if (state.main != *surface_id)
654 this->deactivate(state.main);
658 if (state.sub != *surface_id)
660 this->deactivate(state.sub);
665 // Commit for configuraton and visibility(0)
666 this->layout_commit();
668 std::string str_area = std::string(kNameLayoutNormal) + "." + std::string(kNameAreaFull);
669 compositor::rect area_rect = this->area_info[*surface_id];
670 this->emit_syncdraw(drawing_name, str_area.c_str(),
671 area_rect.x, area_rect.y, area_rect.w, area_rect.h);
672 this->enqueue_flushdraw(state.main);
679 const char* App::check_surface_exist(const char* drawing_name)
681 auto const &surface_id = this->lookup_id(drawing_name);
684 //reply("Surface does not exist");
685 return "Surface does not exist";
688 if (!this->controller->surface_exists(*surface_id))
690 //reply("Surface does not exist in controller!");
691 return "Surface does not exist in controller!";
694 auto layer_id = this->layers.get_layer_id(*surface_id);
698 //reply("Surface is not on any layer!");
699 return "Surface is not on any layer!";
702 auto o_state = *this->layers.get_layout_state(*surface_id);
704 if (o_state == nullptr)
706 //reply("Could not find layer for surface");
707 return "Could not find layer for surface";
710 HMI_DEBUG("wm", "surface %d is detected", *surface_id);
715 void App::api_activate_surface(char const *appid, char const *drawing_name, char const *drawing_area, const reply_func &reply)
723 std::string id = appid;
724 std::string role = drawing_name;
725 std::string area = drawing_area;
727 if(!app_list.contains(id)){
728 reply("app doesn't request 'requestSurface' yet");
732 auto client = app_list.lookUpClient(id);
737 unsigned current = app_list.currentSequenceNumber();
738 unsigned requested_num = app_list.getSequenceNumber(id);
739 if (requested_num != 0)
741 HMI_SEQ_INFO(requested_num, "%s %s %s request is already queued", id.c_str(), role.c_str(), area.c_str());
742 reply("already requested");
746 WMRequest req = WMRequest(id, role, area, Task::TASK_ALLOCATE);
747 unsigned new_req = app_list.addAllocateRequest(req);
750 HMI_SEQ_DEBUG(current, "%s start sequence with %s, %s", id.c_str(), role.c_str(), area.c_str());
753 if (new_req != current)
755 // Add request, then invoked after the previous task is finished
756 HMI_SEQ_DEBUG(new_req, "request is accepted");
763 bool ret = this->do_transition(new_req);
766 HMI_SEQ_ERROR(new_req, "failed to do_transition");
771 void App::api_deactivate_surface(char const *appid, char const *drawing_name, const reply_func &reply)
778 std::string id = appid;
779 std::string role = drawing_name;
780 std::string area = ""; //drawing_area;
782 if(!app_list.contains(id)){
783 reply("app doesn't request 'requestSurface' yet");
786 auto client = app_list.lookUpClient(id);
791 unsigned current = app_list.currentSequenceNumber();
792 unsigned requested_num = app_list.getSequenceNumber(id);
793 if (requested_num != 0)
795 HMI_SEQ_INFO(requested_num, "%s %s %s request is already queued", id.c_str(), role.c_str(), area.c_str());
796 reply("already requested");
800 WMRequest req = WMRequest(id, role, area, Task::TASK_RELEASE);
801 unsigned new_req = app_list.addAllocateRequest(req);
804 HMI_SEQ_DEBUG(current, "%s start sequence with %s, %s", id.c_str(), role.c_str(), area.c_str());
806 if (new_req != current)
808 // Add request, then invoked after the previous task is finished
809 HMI_SEQ_DEBUG(new_req, "request is accepted");
817 //bool ret = this->do_transition(new_req);
819 auto const &surface_id = this->lookup_id(drawing_name);
822 reply("Surface does not exist");
826 if (*surface_id == this->layers.main_surface)
828 reply("Cannot deactivate main_surface");
832 auto o_state = *this->layers.get_layout_state(*surface_id);
834 if (o_state == nullptr)
836 reply("Could not find layer for surface");
840 struct LayoutState &state = *o_state;
842 if (state.main == -1)
844 reply("No surface active");
848 // Check against main_surface, main_surface_name is the configuration item.
849 if (*surface_id == this->layers.main_surface)
851 HMI_DEBUG("wm", "Refusing to deactivate main_surface %d", *surface_id);
855 if ((state.main == *surface_id) && (state.sub == *surface_id))
857 reply("Surface is not active");
862 if (state.main == *surface_id)
867 state, LayoutState{state.sub, -1}, [&](LayoutState const &nl) {
868 std::string sub = std::move(*this->lookup_name(state.sub));
870 this->deactivate(*surface_id);
871 this->surface_set_layout(state.sub);
874 this->layout_commit();
875 std::string str_area = std::string(kNameLayoutNormal) + "." + std::string(kNameAreaFull);
876 compositor::rect area_rect = this->area_info[state.sub];
877 this->emit_syncdraw(sub.c_str(), str_area.c_str(),
878 area_rect.x, area_rect.y, area_rect.w, area_rect.h);
879 this->enqueue_flushdraw(state.sub);
884 this->try_layout(state, LayoutState{-1, -1}, [&](LayoutState const &nl) {
885 this->deactivate(*surface_id);
887 this->layout_commit();
891 else if (state.sub == *surface_id)
894 state, LayoutState{state.main, -1}, [&](LayoutState const &nl) {
895 std::string main = std::move(*this->lookup_name(state.main));
897 this->deactivate(*surface_id);
898 this->surface_set_layout(state.main);
901 this->layout_commit();
902 std::string str_area = std::string(kNameLayoutNormal) + "." + std::string(kNameAreaFull);
903 compositor::rect area_rect = this->area_info[state.main];
904 this->emit_syncdraw(main.c_str(), str_area.c_str(),
905 area_rect.x, area_rect.y, area_rect.w, area_rect.h);
906 this->enqueue_flushdraw(state.main);
911 void App::enqueue_flushdraw(int surface_id)
913 this->check_flushdraw(surface_id);
914 HMI_DEBUG("wm", "Enqueuing EndDraw for surface_id %d", surface_id);
915 this->pending_end_draw.push_back(surface_id);
918 void App::check_flushdraw(int surface_id)
920 auto i = std::find(std::begin(this->pending_end_draw),
921 std::end(this->pending_end_draw), surface_id);
922 if (i != std::end(this->pending_end_draw))
924 auto n = this->lookup_name(surface_id);
925 HMI_ERROR("wm", "Application %s (%d) has pending EndDraw call(s)!",
926 n ? n->c_str() : "unknown-name", surface_id);
927 std::swap(this->pending_end_draw[std::distance(
928 std::begin(this->pending_end_draw), i)],
929 this->pending_end_draw.back());
930 this->pending_end_draw.resize(this->pending_end_draw.size() - 1);
934 void App::lm_enddraw(const char* drawing_name){
935 HMI_DEBUG("wm", "end draw %s", drawing_name);
936 for (unsigned i = 0, iend = this->pending_end_draw.size(); i < iend; i++)
938 auto n = this->lookup_name(this->pending_end_draw[i]);
939 if (n && *n == drawing_name)
941 std::swap(this->pending_end_draw[i], this->pending_end_draw[iend - 1]);
942 this->pending_end_draw.resize(iend - 1);
943 this->activate(this->pending_end_draw[i]);
944 this->emit_flushdraw(drawing_name);
949 void App::do_enddraw(unsigned request_seq)
952 auto actions = app_list.getActions(request_seq);
953 HMI_SEQ_INFO(request_seq, "do endDraw");
955 for(const auto& act : actions){
956 HMI_SEQ_DEBUG(request_seq, "visible %s", act.role.c_str());
957 this->lm_enddraw(act.role.c_str());
960 HMI_SEQ_INFO(request_seq, "emit flushDraw");
964 //emitFlushDrawToAll(&app_list, request_seq);
965 // emit status change event
966 } while (!app_list.requestFinished());*/
969 void App::process_request()
971 unsigned req = app_list.currentSequenceNumber();
972 HMI_SEQ_DEBUG(req, "Do next request");
976 void App::api_enddraw(char const *appid, char const *drawing_name)
978 std::string id(appid);
979 std::string role(drawing_name);
980 unsigned current_seq = app_list.currentSequenceNumber();
981 bool result = app_list.setEndDrawFinished(current_seq, id, role);
985 HMI_ERROR("wm", "%s doesn't have Window Resource", id.c_str());
989 if (app_list.endDrawFullfilled(current_seq))
991 // do task for endDraw
992 //this->stop_timer();
993 this->do_enddraw(current_seq);
997 app_list.removeRequest(current_seq);
998 HMI_SEQ_INFO(current_seq, "Finish request");
1000 if (app_list.haveRequest())
1002 this->process_request();
1007 HMI_SEQ_INFO(current_seq, "Wait other App call endDraw");
1012 void App::api_ping() { this->dispatch_pending_events(); }
1014 void App::send_event(char const *evname, char const *label)
1016 HMI_DEBUG("wm", "%s: %s(%s)", __func__, evname, label);
1018 json_object *j = json_object_new_object();
1019 json_object_object_add(j, kKeyDrawingName, json_object_new_string(label));
1021 int ret = afb_event_push(this->map_afb_event[evname], j);
1024 HMI_DEBUG("wm", "afb_event_push failed: %m");
1028 void App::send_event(char const *evname, char const *label, char const *area,
1029 int x, int y, int w, int h)
1031 HMI_DEBUG("wm", "%s: %s(%s, %s) x:%d y:%d w:%d h:%d",
1032 __func__, evname, label, area, x, y, w, h);
1034 json_object *j_rect = json_object_new_object();
1035 json_object_object_add(j_rect, kKeyX, json_object_new_int(x));
1036 json_object_object_add(j_rect, kKeyY, json_object_new_int(y));
1037 json_object_object_add(j_rect, kKeyWidth, json_object_new_int(w));
1038 json_object_object_add(j_rect, kKeyHeight, json_object_new_int(h));
1040 json_object *j = json_object_new_object();
1041 json_object_object_add(j, kKeyDrawingName, json_object_new_string(label));
1042 json_object_object_add(j, kKeyDrawingArea, json_object_new_string(area));
1043 json_object_object_add(j, kKeyDrawingRect, j_rect);
1045 int ret = afb_event_push(this->map_afb_event[evname], j);
1048 HMI_DEBUG("wm", "afb_event_push failed: %m");
1055 void App::surface_created(uint32_t surface_id)
1057 auto layer_id = this->layers.get_layer_id(surface_id);
1060 HMI_DEBUG("wm", "Newly created surfce %d is not associated with any layer!",
1065 HMI_DEBUG("wm", "surface_id is %u, layer_id is %u", surface_id, *layer_id);
1067 this->controller->layers[*layer_id]->add_surface(surface_id);
1068 this->layout_commit();
1069 // activate the main_surface right away
1070 /*if (surface_id == static_cast<unsigned>(this->layers.main_surface)) {
1071 HMI_DEBUG("wm", "Activating main_surface (%d)", surface_id);
1073 this->api_activate_surface(
1074 this->lookup_name(surface_id).value_or("unknown-name").c_str());
1078 void App::surface_removed(uint32_t surface_id)
1080 HMI_DEBUG("wm", "surface_id is %u", surface_id);
1082 // We cannot normally deactivate the main_surface, so be explicit
1084 if (int(surface_id) == this->layers.main_surface)
1086 this->deactivate_main_surface();
1090 auto drawing_name = this->lookup_name(surface_id);
1093 std::string appid = "";
1094 /* auto = lookUpClient(surface_id);
1095 appid = client->appID();
1098 this->api_deactivate_surface(appid.c_str(), drawing_name->c_str(), [](const char *) {});
1102 this->id_alloc.remove_id(surface_id);
1103 this->layers.remove_surface(surface_id);
1106 void App::emit_activated(char const *label)
1108 this->send_event(kListEventName[Event_Active], label);
1111 void App::emit_deactivated(char const *label)
1113 this->send_event(kListEventName[Event_Inactive], label);
1116 void App::emit_syncdraw(char const *label, char const *area, int x, int y, int w, int h)
1118 this->send_event(kListEventName[Event_SyncDraw], label, area, x, y, w, h);
1121 void App::emit_flushdraw(char const *label)
1123 this->send_event(kListEventName[Event_FlushDraw], label);
1126 void App::emit_visible(char const *label, bool is_visible)
1128 this->send_event(is_visible ? kListEventName[Event_Visible] : kListEventName[Event_Invisible], label);
1131 void App::emit_invisible(char const *label)
1133 return emit_visible(label, false);
1136 void App::emit_visible(char const *label) { return emit_visible(label, true); }
1138 result<int> App::api_request_surface(char const *appid, char const *drawing_name)
1140 auto lid = this->layers.get_layer_id(std::string(drawing_name));
1144 * register drawing_name as fallback and make it displayed.
1146 lid = this->layers.get_layer_id(std::string("Fallback"));
1147 HMI_DEBUG("wm", "%s is not registered in layers.json, then fallback as normal app", drawing_name);
1150 return Err<int>("Drawing name does not match any role, Fallback is disabled");
1154 auto rname = this->lookup_id(drawing_name);
1157 // name does not exist yet, allocate surface id...
1158 auto id = int(this->id_alloc.generate_id(drawing_name));
1159 this->layers.add_surface(id, *lid);
1161 // set the main_surface[_name] here and now
1162 if (!this->layers.main_surface_name.empty() &&
1163 this->layers.main_surface_name == drawing_name)
1165 this->layers.main_surface = id;
1166 HMI_DEBUG("wm", "Set main_surface id to %u", id);
1169 // add client into the db
1170 std::string appid_str(appid);
1171 std::string role(drawing_name);
1172 app_list.addClient(appid_str, role);
1177 // Check currently registered drawing names if it is already there.
1178 return Err<int>("Surface already present");
1181 char const *App::api_request_surface(char const *appid, char const *drawing_name,
1186 auto lid = this->layers.get_layer_id(std::string(drawing_name));
1187 unsigned sid = std::stol(ivi_id);
1192 * register drawing_name as fallback and make it displayed.
1194 lid = this->layers.get_layer_id(std::string("Fallback"));
1195 HMI_DEBUG("wm", "%s is not registered in layers.json, then fallback as normal app", drawing_name);
1198 return "Drawing name does not match any role, Fallback is disabled";
1202 auto rname = this->lookup_id(drawing_name);
1206 return "Surface already present";
1209 // register pair drawing_name and ivi_id
1210 this->id_alloc.register_name_id(drawing_name, sid);
1211 this->layers.add_surface(sid, *lid);
1213 // this surface is already created
1214 HMI_DEBUG("wm", "surface_id is %u, layer_id is %u", sid, *lid);
1216 this->controller->layers[*lid]->add_surface(sid);
1217 this->layout_commit();
1222 result<json_object *> App::api_get_display_info()
1225 if (!this->controller)
1227 return Err<json_object *>("ivi_controller global not available");
1231 compositor::size o_size = this->controller->output_size;
1232 compositor::size p_size = this->controller->physical_size;
1234 json_object *object = json_object_new_object();
1235 json_object_object_add(object, kKeyWidthPixel, json_object_new_int(o_size.w));
1236 json_object_object_add(object, kKeyHeightPixel, json_object_new_int(o_size.h));
1237 json_object_object_add(object, kKeyWidthMm, json_object_new_int(p_size.w));
1238 json_object_object_add(object, kKeyHeightMm, json_object_new_int(p_size.h));
1240 return Ok<json_object *>(object);
1243 result<json_object *> App::api_get_area_info(char const *drawing_name)
1245 HMI_DEBUG("wm", "called");
1247 // Check drawing name, surface/layer id
1248 auto const &surface_id = this->lookup_id(drawing_name);
1251 return Err<json_object *>("Surface does not exist");
1254 if (!this->controller->surface_exists(*surface_id))
1256 return Err<json_object *>("Surface does not exist in controller!");
1259 auto layer_id = this->layers.get_layer_id(*surface_id);
1262 return Err<json_object *>("Surface is not on any layer!");
1265 auto o_state = *this->layers.get_layout_state(*surface_id);
1266 if (o_state == nullptr)
1268 return Err<json_object *>("Could not find layer for surface");
1271 struct LayoutState &state = *o_state;
1272 if ((state.main != *surface_id) && (state.sub != *surface_id))
1274 return Err<json_object *>("Surface is inactive");
1277 // Set area rectangle
1278 compositor::rect area_info = this->area_info[*surface_id];
1279 json_object *object = json_object_new_object();
1280 json_object_object_add(object, kKeyX, json_object_new_int(area_info.x));
1281 json_object_object_add(object, kKeyY, json_object_new_int(area_info.y));
1282 json_object_object_add(object, kKeyWidth, json_object_new_int(area_info.w));
1283 json_object_object_add(object, kKeyHeight, json_object_new_int(area_info.h));
1285 return Ok<json_object *>(object);
1288 void App::activate(int id)
1290 auto ip = this->controller->sprops.find(id);
1291 if (ip != this->controller->sprops.end())
1293 this->controller->surfaces[id]->set_visibility(1);
1295 this->lookup_name(id).value_or("unknown-name").c_str();
1298 if ((0 == strcmp(label, "Radio")) || (0 == strcmp(label, "MediaPlayer")) || (0 == strcmp(label, "Music")) || (0 == strcmp(label, "Navigation")))
1300 for (auto i = surface_bg.begin(); i != surface_bg.end(); ++i)
1305 this->surface_bg.erase(i);
1307 // Remove from BG layer (999)
1308 HMI_DEBUG("wm", "Remove %s(%d) from BG layer", label, id);
1309 this->controller->layers[999]->remove_surface(id);
1311 // Add to FG layer (1001)
1312 HMI_DEBUG("wm", "Add %s(%d) to FG layer", label, id);
1313 this->controller->layers[1001]->add_surface(id);
1315 for (int j : this->surface_bg)
1317 HMI_DEBUG("wm", "Stored id:%d", j);
1324 this->layout_commit();
1326 this->emit_visible(label);
1327 this->emit_activated(label);
1331 void App::deactivate(int id)
1333 auto ip = this->controller->sprops.find(id);
1334 if (ip != this->controller->sprops.end())
1337 this->lookup_name(id).value_or("unknown-name").c_str();
1340 if ((0 == strcmp(label, "Radio")) || (0 == strcmp(label, "MediaPlayer")) || (0 == strcmp(label, "Music")) || (0 == strcmp(label, "Navigation")))
1344 this->surface_bg.push_back(id);
1346 // Remove from FG layer (1001)
1347 HMI_DEBUG("wm", "Remove %s(%d) from FG layer", label, id);
1348 this->controller->layers[1001]->remove_surface(id);
1350 // Add to BG layer (999)
1351 HMI_DEBUG("wm", "Add %s(%d) to BG layer", label, id);
1352 this->controller->layers[999]->add_surface(id);
1354 for (int j : surface_bg)
1356 HMI_DEBUG("wm", "Stored id:%d", j);
1361 this->controller->surfaces[id]->set_visibility(0);
1365 this->emit_deactivated(label);
1366 this->emit_invisible(label);
1370 void App::deactivate_main_surface()
1372 this->layers.main_surface = -1;
1373 std::string appid = "HomeScreen";
1374 this->api_deactivate_surface(appid.c_str(), this->layers.main_surface_name.c_str(), [](const char *) {});
1377 bool App::can_split(struct LayoutState const &state, int new_id)
1379 if (state.main != -1 && state.main != new_id)
1381 auto new_id_layer = this->layers.get_layer_id(new_id).value();
1382 auto current_id_layer = this->layers.get_layer_id(state.main).value();
1384 // surfaces are on separate layers, don't bother.
1385 if (new_id_layer != current_id_layer)
1390 std::string const &new_id_str = this->lookup_name(new_id).value();
1391 std::string const &cur_id_str = this->lookup_name(state.main).value();
1393 auto const &layer = this->layers.get_layer(new_id_layer);
1395 HMI_DEBUG("wm", "layer info name: %s", layer->name.c_str());
1397 if (layer->layouts.empty())
1402 for (auto i = layer->layouts.cbegin(); i != layer->layouts.cend(); i++)
1404 HMI_DEBUG("wm", "%d main_match '%s'", new_id_layer, i->main_match.c_str());
1405 auto rem = std::regex(i->main_match);
1406 if (std::regex_match(cur_id_str, rem))
1408 // build the second one only if the first already matched
1409 HMI_DEBUG("wm", "%d sub_match '%s'", new_id_layer, i->sub_match.c_str());
1410 auto res = std::regex(i->sub_match);
1411 if (std::regex_match(new_id_str, res))
1413 HMI_DEBUG("wm", "layout matched!");
1423 void App::try_layout(struct LayoutState & /*state*/,
1424 struct LayoutState const &new_layout,
1425 std::function<void(LayoutState const &nl)> apply)
1427 if (this->policy.layout_is_valid(new_layout))
1436 void controller_hooks::surface_created(uint32_t surface_id)
1438 this->app->surface_created(surface_id);
1441 void controller_hooks::surface_removed(uint32_t surface_id)
1443 this->app->surface_removed(surface_id);
1446 void controller_hooks::surface_visibility(uint32_t /*surface_id*/,
1449 void controller_hooks::surface_destination_rectangle(uint32_t /*surface_id*/,