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 // TODO: write reset process
126 unsigned seq = app_list.currentSequenceNumber();
127 app_list.removeRequest(seq);
133 App::App(wl::display *d)
141 pending_events(false),
147 auto l = load_layer_map(
148 this->config.get_string("layers.json").value().c_str());
151 this->layers = l.unwrap();
155 HMI_ERROR("wm", "%s", l.err().value());
159 catch (std::exception &e)
161 HMI_ERROR("wm", "Loading of configuration failed: %s", e.what());
167 if (!this->display->ok())
172 if (this->layers.mapping.empty())
174 HMI_ERROR("wm", "No surface -> layer mapping loaded");
179 for (int i = Event_Val_Min; i <= Event_Val_Max; i++)
181 map_afb_event[kListEventName[i]] = afb_daemon_make_event(kListEventName[i]);
184 this->display->add_global_handler(
185 "wl_output", [this](wl_registry *r, uint32_t name, uint32_t v) {
186 this->outputs.emplace_back(std::make_unique<wl::output>(r, name, v));
189 this->display->add_global_handler(
190 "ivi_wm", [this](wl_registry *r, uint32_t name, uint32_t v) {
192 std::make_unique<struct compositor::controller>(r, name, v);
194 // Init controller hooks
195 this->controller->chooks = &this->chooks;
197 // This protocol needs the output, so lets just add our mapping here...
198 this->controller->add_proxy_to_id_mapping(
199 this->outputs.back()->proxy.get(),
200 wl_proxy_get_id(reinterpret_cast<struct wl_proxy *>(
201 this->outputs.back()->proxy.get())));
204 this->controller->create_screen(this->outputs.back()->proxy.get());
206 // Set display to controller
207 this->controller->display = this->display;
210 // First level objects
211 this->display->roundtrip();
212 // Second level objects
213 this->display->roundtrip();
214 // Third level objects
215 this->display->roundtrip();
217 return init_layers();
220 int App::dispatch_pending_events()
222 if (this->pop_pending_events())
224 this->display->dispatch_pending();
230 bool App::pop_pending_events()
233 return this->pending_events.compare_exchange_strong(
234 x, false, std::memory_order_consume);
237 void App::set_pending_events()
239 this->pending_events.store(true, std::memory_order_release);
242 optional<int> App::lookup_id(char const *name)
244 return this->id_alloc.lookup(std::string(name));
246 optional<std::string> App::lookup_name(int id)
248 return this->id_alloc.lookup(id);
254 int App::init_layers()
256 if (!this->controller)
258 HMI_ERROR("wm", "ivi_controller global not available");
262 if (this->outputs.empty())
264 HMI_ERROR("wm", "no output was set up!");
268 auto &c = this->controller;
270 auto &o = this->outputs.front();
271 auto &s = c->screens.begin()->second;
272 auto &layers = c->layers;
274 // Write output dimensions to ivi controller...
275 c->output_size = compositor::size{uint32_t(o->width), uint32_t(o->height)};
276 c->physical_size = compositor::size{uint32_t(o->physical_width),
277 uint32_t(o->physical_height)};
285 // Quick and dirty setup of layers
286 for (auto const &i : this->layers.mapping)
288 c->layer_create(i.second.layer_id, o->width, o->height);
289 auto &l = layers[i.second.layer_id];
290 l->set_destination_rectangle(0, 0, o->width, o->height);
291 l->set_visibility(1);
292 HMI_DEBUG("wm", "Setting up layer %s (%d) for surface role match \"%s\"",
293 i.second.name.c_str(), i.second.layer_id, i.second.role.c_str());
296 // Add layers to screen
297 s->set_render_order(this->layers.layers);
299 this->layout_commit();
304 void App::surface_set_layout(int surface_id, optional<int> sub_surface_id)
306 if (!this->controller->surface_exists(surface_id))
308 HMI_ERROR("wm", "Surface %d does not exist", surface_id);
312 auto o_layer_id = this->layers.get_layer_id(surface_id);
316 HMI_ERROR("wm", "Surface %d is not associated with any layer!", surface_id);
320 uint32_t layer_id = *o_layer_id;
322 auto const &layer = this->layers.get_layer(layer_id);
323 auto rect = layer.value().rect;
324 auto &s = this->controller->surfaces[surface_id];
331 // less-than-0 values refer to MAX + 1 - $VALUE
332 // e.g. MAX is either screen width or height
335 w = this->controller->output_size.w + 1 + w;
339 h = this->controller->output_size.h + 1 + h;
344 if (o_layer_id != this->layers.get_layer_id(*sub_surface_id))
347 "surface_set_layout: layers of surfaces (%d and %d) don't match!",
348 surface_id, *sub_surface_id);
355 // split along major axis
367 auto &ss = this->controller->surfaces[*sub_surface_id];
369 HMI_DEBUG("wm", "surface_set_layout for sub surface %u on layer %u",
370 *sub_surface_id, layer_id);
372 // set destination to the display rectangle
373 ss->set_destination_rectangle(x + x_off, y + y_off, w, h);
375 this->area_info[*sub_surface_id].x = x;
376 this->area_info[*sub_surface_id].y = y;
377 this->area_info[*sub_surface_id].w = w;
378 this->area_info[*sub_surface_id].h = h;
381 HMI_DEBUG("wm", "surface_set_layout for surface %u on layer %u", surface_id,
384 // set destination to the display rectangle
385 s->set_destination_rectangle(x, y, w, h);
387 // update area information
388 this->area_info[surface_id].x = x;
389 this->area_info[surface_id].y = y;
390 this->area_info[surface_id].w = w;
391 this->area_info[surface_id].h = h;
393 HMI_DEBUG("wm", "Surface %u now on layer %u with rect { %d, %d, %d, %d }",
394 surface_id, layer_id, x, y, w, h);
397 void App::layout_commit()
399 this->controller->commit_changes();
400 this->display->flush();
403 void App::do_allocate_window_resource(unsigned sequence_number)
407 // TODO: Tasks will be changed according to policy manager result
408 // do task(onTransition (activate))
409 } while (!app_list.requestFinished());
411 // lm_.updateLayout(jobj);
412 // TODO: emit syncDraw with application
415 //client->emit_syncdraw(role);
416 } while (!app_list.requestFinished());
418 // is the below necessary?
419 //app_list.setAllocated(sequence_number);
421 if (timer_ev_src != nullptr)
423 // firsttime set into sd_event
424 int ret = sd_event_add_time(afb_daemon_get_event_loop(), &timer_ev_src,
425 CLOCK_BOOTTIME, time(NULL) + TIME_OUT, 0, processTimerHandler, this);
428 HMI_ERROR("wm", "Can't set timer");
433 // update timer limitation after second time
434 sd_event_source_set_time(timer_ev_src, time(NULL) + TIME_OUT);
435 sd_event_source_set_enabled(timer_ev_src, SD_EVENT_ONESHOT);
439 void App::api_activate_surface(char const *appid, char const *drawing_name, char const *drawing_area, const reply_func &reply)
447 auto const &surface_id = this->lookup_id(drawing_name);
448 std::string id = appid;
449 std::string role = drawing_name;
450 std::string area = drawing_area;
452 if(!app_list.contains(id)){
453 reply("app doesn't request 'requestSurface' yet");
457 auto client = app_list.lookUpClient(id);
462 unsigned current = app_list.currentSequenceNumber();
463 unsigned requested_num = app_list.getSequenceNumber(id);
464 if (requested_num != 0)
466 HMI_SEQ_INFO(requested_num, "%s %s %s request is already queued", id.c_str(), role.c_str(), area.c_str());
467 reply("already requested");
471 WMRequest req = WMRequest(id, role, area, Task::TASK_ALLOCATE);
472 unsigned new_req = app_list.addAllocateRequest(req);
474 HMI_SEQ_DEBUG(new_req, "%s start sequence with %s, %s", id.c_str(), role.c_str(), area.c_str());
476 if (new_req != current)
478 // Add request, then invoked after the previous task is finished
479 HMI_SEQ_DEBUG(new_req, "request is accepted");
488 // json_object* newState = checkPolicy(role);
489 /* The following error check is not necessary because main.cpp will reject the message form not registered object
490 if(client != nullptr){
491 reply("client is not registered");
495 // get surfaceID from role
496 /* auto const &surface_id_tmp = client->surfaceID(role);
497 auto const &layer_id_tmp = client->layerID();
499 if(!surface_id_tmp && !layer_id_tmp){
500 reply("invalid window manager client");
501 HMI_DEBUG("appid:%s, requested_role:%s, surfaceID:%d in layer %d",
502 appid, role, surface_id_tmp, layer_id_tmp);
508 this->do_allocate_window_resource(new_req);
512 reply("Surface does not exist");
516 if (!this->controller->surface_exists(*surface_id))
518 reply("Surface does not exist in controller!");
522 auto layer_id = this->layers.get_layer_id(*surface_id);
526 reply("Surface is not on any layer!");
530 auto o_state = *this->layers.get_layout_state(*surface_id);
532 if (o_state == nullptr)
534 reply("Could not find layer for surface");
538 HMI_DEBUG("wm", "surface %d is detected", *surface_id);
541 struct LayoutState &state = *o_state;
543 // disable layers that are above our current layer
544 for (auto const &l : this->layers.mapping)
546 if (l.second.layer_id <= *layer_id)
552 if (l.second.state.main != -1)
554 this->deactivate(l.second.state.main);
555 l.second.state.main = -1;
559 if (l.second.state.sub != -1)
561 this->deactivate(l.second.state.sub);
562 l.second.state.sub = -1;
568 this->layout_commit();
572 auto layer = this->layers.get_layer(*layer_id);
574 if (state.main == -1)
577 state, LayoutState{*surface_id}, [&](LayoutState const &nl) {
578 HMI_DEBUG("wm", "Layout: %s", kNameLayoutNormal);
579 this->surface_set_layout(*surface_id);
582 // Commit for configuraton
583 this->layout_commit();
585 std::string str_area = std::string(kNameLayoutNormal) + "." + std::string(kNameAreaFull);
586 compositor::rect area_rect = this->area_info[*surface_id];
587 this->emit_syncdraw(drawing_name, str_area.c_str(),
588 area_rect.x, area_rect.y, area_rect.w, area_rect.h);
589 this->enqueue_flushdraw(state.main);
594 if (0 == strcmp(drawing_name, "HomeScreen"))
597 state, LayoutState{*surface_id}, [&](LayoutState const &nl) {
598 HMI_DEBUG("wm", "Layout: %s", kNameLayoutNormal);
599 std::string str_area = std::string(kNameLayoutNormal) + "." + std::string(kNameAreaFull);
600 compositor::rect area_rect = this->area_info[*surface_id];
601 this->emit_syncdraw(drawing_name, str_area.c_str(),
602 area_rect.x, area_rect.y, area_rect.w, area_rect.h);
603 this->enqueue_flushdraw(state.main);
608 bool can_split = this->can_split(state, *surface_id);
614 LayoutState{state.main, *surface_id},
615 [&](LayoutState const &nl) {
616 HMI_DEBUG("wm", "Layout: %s", kNameLayoutSplit);
618 std::move(*this->lookup_name(state.main));
620 this->surface_set_layout(state.main, surface_id);
621 if (state.sub != *surface_id)
625 this->deactivate(state.sub);
630 // Commit for configuration and visibility(0)
631 this->layout_commit();
633 std::string str_area_main = std::string(kNameLayoutSplit) + "." + std::string(kNameAreaMain);
634 std::string str_area_sub = std::string(kNameLayoutSplit) + "." + std::string(kNameAreaSub);
635 compositor::rect area_rect_main = this->area_info[state.main];
636 compositor::rect area_rect_sub = this->area_info[*surface_id];
637 this->emit_syncdraw(main.c_str(), str_area_main.c_str(),
638 area_rect_main.x, area_rect_main.y,
639 area_rect_main.w, area_rect_main.h);
640 this->emit_syncdraw(drawing_name, str_area_sub.c_str(),
641 area_rect_sub.x, area_rect_sub.y,
642 area_rect_sub.w, area_rect_sub.h);
643 this->enqueue_flushdraw(state.main);
644 this->enqueue_flushdraw(state.sub);
650 state, LayoutState{*surface_id}, [&](LayoutState const &nl) {
651 HMI_DEBUG("wm", "Layout: %s", kNameLayoutNormal);
653 this->surface_set_layout(*surface_id);
654 if (state.main != *surface_id)
656 this->deactivate(state.main);
660 if (state.sub != *surface_id)
662 this->deactivate(state.sub);
667 // Commit for configuraton and visibility(0)
668 this->layout_commit();
670 std::string str_area = std::string(kNameLayoutNormal) + "." + std::string(kNameAreaFull);
671 compositor::rect area_rect = this->area_info[*surface_id];
672 this->emit_syncdraw(drawing_name, str_area.c_str(),
673 area_rect.x, area_rect.y, area_rect.w, area_rect.h);
674 this->enqueue_flushdraw(state.main);
681 void App::api_deactivate_surface(char const *appid, char const *drawing_name, const reply_func &reply)
684 auto const &surface_id = this->lookup_id(drawing_name);
686 std::string id = appid;
688 if(app_list.contains(id)){
689 reply("app doesn't request 'requestSurface' yet");
692 auto client = app_list.lookUpClient(id);
696 reply("Surface does not exist");
700 if (*surface_id == this->layers.main_surface)
702 reply("Cannot deactivate main_surface");
706 auto o_state = *this->layers.get_layout_state(*surface_id);
708 if (o_state == nullptr)
710 reply("Could not find layer for surface");
714 struct LayoutState &state = *o_state;
716 if (state.main == -1)
718 reply("No surface active");
722 // Check against main_surface, main_surface_name is the configuration item.
723 if (*surface_id == this->layers.main_surface)
725 HMI_DEBUG("wm", "Refusing to deactivate main_surface %d", *surface_id);
729 if ((state.main == *surface_id) && (state.sub == *surface_id))
731 reply("Surface is not active");
736 if (state.main == *surface_id)
741 state, LayoutState{state.sub, -1}, [&](LayoutState const &nl) {
742 std::string sub = std::move(*this->lookup_name(state.sub));
744 this->deactivate(*surface_id);
745 this->surface_set_layout(state.sub);
748 this->layout_commit();
749 std::string str_area = std::string(kNameLayoutNormal) + "." + std::string(kNameAreaFull);
750 compositor::rect area_rect = this->area_info[state.sub];
751 this->emit_syncdraw(sub.c_str(), str_area.c_str(),
752 area_rect.x, area_rect.y, area_rect.w, area_rect.h);
753 this->enqueue_flushdraw(state.sub);
758 this->try_layout(state, LayoutState{-1, -1}, [&](LayoutState const &nl) {
759 this->deactivate(*surface_id);
761 this->layout_commit();
765 else if (state.sub == *surface_id)
768 state, LayoutState{state.main, -1}, [&](LayoutState const &nl) {
769 std::string main = std::move(*this->lookup_name(state.main));
771 this->deactivate(*surface_id);
772 this->surface_set_layout(state.main);
775 this->layout_commit();
776 std::string str_area = std::string(kNameLayoutNormal) + "." + std::string(kNameAreaFull);
777 compositor::rect area_rect = this->area_info[state.main];
778 this->emit_syncdraw(main.c_str(), str_area.c_str(),
779 area_rect.x, area_rect.y, area_rect.w, area_rect.h);
780 this->enqueue_flushdraw(state.main);
785 void App::enqueue_flushdraw(int surface_id)
787 this->check_flushdraw(surface_id);
788 HMI_DEBUG("wm", "Enqueuing EndDraw for surface_id %d", surface_id);
789 this->pending_end_draw.push_back(surface_id);
792 void App::check_flushdraw(int surface_id)
794 auto i = std::find(std::begin(this->pending_end_draw),
795 std::end(this->pending_end_draw), surface_id);
796 if (i != std::end(this->pending_end_draw))
798 auto n = this->lookup_name(surface_id);
799 HMI_ERROR("wm", "Application %s (%d) has pending EndDraw call(s)!",
800 n ? n->c_str() : "unknown-name", surface_id);
801 std::swap(this->pending_end_draw[std::distance(
802 std::begin(this->pending_end_draw), i)],
803 this->pending_end_draw.back());
804 this->pending_end_draw.resize(this->pending_end_draw.size() - 1);
808 void App::do_enddraw(unsigned sequence_number)
810 HMI_SEQ_INFO(sequence_number, "do endDraw");
813 // make visible application
814 } while (!app_list.requestFinished());
816 HMI_SEQ_INFO(sequence_number, "emit flushDraw");
820 //emitFlushDrawToAll(&app_list, request_seq);
821 // emit status change event
822 } while (!app_list.requestFinished());
825 void App::process_request()
829 void App::api_enddraw(char const *appid, char const *drawing_name)
831 std::string id(appid);
832 unsigned request_seq = app_list.lookUpAllocatingApp(id);
833 unsigned current_seq = app_list.currentSequenceNumber();
834 if (current_seq != request_seq)
836 if (request_seq == 0)
838 HMI_ERROR("wm", "You don't have Window Resource");
842 HMI_ERROR("wm", "unknown error. Application may not obey the sequence manner. please call endDraw after syncDraw");
847 std::string role = drawing_name;
848 //std::string area = drawing_area;
849 app_list.setEndDrawFinished(request_seq, role);
851 if (app_list.endDrawFullfilled(request_seq))
853 // do task for endDraw
854 this->do_enddraw(request_seq);
855 app_list.removeRequest(request_seq);
856 HMI_SEQ_INFO(request_seq, "Finish sequence");
857 app_list.setCurrentSequence(request_seq + 1);
858 if (app_list.haveRequest())
860 this->process_request();
865 HMI_SEQ_INFO(request_seq, "Wait other App call endDraw");
868 for (unsigned i = 0, iend = this->pending_end_draw.size(); i < iend; i++)
870 auto n = this->lookup_name(this->pending_end_draw[i]);
871 if (n && *n == drawing_name)
873 std::swap(this->pending_end_draw[i], this->pending_end_draw[iend - 1]);
874 this->pending_end_draw.resize(iend - 1);
875 this->activate(this->pending_end_draw[i]);
876 this->emit_flushdraw(drawing_name);
881 void App::api_ping() { this->dispatch_pending_events(); }
883 void App::send_event(char const *evname, char const *label)
885 HMI_DEBUG("wm", "%s: %s(%s)", __func__, evname, label);
887 json_object *j = json_object_new_object();
888 json_object_object_add(j, kKeyDrawingName, json_object_new_string(label));
890 int ret = afb_event_push(this->map_afb_event[evname], j);
893 HMI_DEBUG("wm", "afb_event_push failed: %m");
897 void App::send_event(char const *evname, char const *label, char const *area,
898 int x, int y, int w, int h)
900 HMI_DEBUG("wm", "%s: %s(%s, %s) x:%d y:%d w:%d h:%d",
901 __func__, evname, label, area, x, y, w, h);
903 json_object *j_rect = json_object_new_object();
904 json_object_object_add(j_rect, kKeyX, json_object_new_int(x));
905 json_object_object_add(j_rect, kKeyY, json_object_new_int(y));
906 json_object_object_add(j_rect, kKeyWidth, json_object_new_int(w));
907 json_object_object_add(j_rect, kKeyHeight, json_object_new_int(h));
909 json_object *j = json_object_new_object();
910 json_object_object_add(j, kKeyDrawingName, json_object_new_string(label));
911 json_object_object_add(j, kKeyDrawingArea, json_object_new_string(area));
912 json_object_object_add(j, kKeyDrawingRect, j_rect);
914 int ret = afb_event_push(this->map_afb_event[evname], j);
917 HMI_DEBUG("wm", "afb_event_push failed: %m");
924 void App::surface_created(uint32_t surface_id)
926 auto layer_id = this->layers.get_layer_id(surface_id);
929 HMI_DEBUG("wm", "Newly created surfce %d is not associated with any layer!",
934 HMI_DEBUG("wm", "surface_id is %u, layer_id is %u", surface_id, *layer_id);
936 this->controller->layers[*layer_id]->add_surface(surface_id);
937 this->layout_commit();
938 // activate the main_surface right away
939 /*if (surface_id == static_cast<unsigned>(this->layers.main_surface)) {
940 HMI_DEBUG("wm", "Activating main_surface (%d)", surface_id);
942 this->api_activate_surface(
943 this->lookup_name(surface_id).value_or("unknown-name").c_str());
947 void App::surface_removed(uint32_t surface_id)
949 HMI_DEBUG("wm", "surface_id is %u", surface_id);
951 // We cannot normally deactivate the main_surface, so be explicit
953 if (int(surface_id) == this->layers.main_surface)
955 this->deactivate_main_surface();
959 auto drawing_name = this->lookup_name(surface_id);
962 std::string appid = "";
963 /* auto = lookUpClient(surface_id);
964 appid = client->appID();
967 this->api_deactivate_surface(appid.c_str(), drawing_name->c_str(), [](const char *) {});
971 this->id_alloc.remove_id(surface_id);
972 this->layers.remove_surface(surface_id);
975 void App::emit_activated(char const *label)
977 this->send_event(kListEventName[Event_Active], label);
980 void App::emit_deactivated(char const *label)
982 this->send_event(kListEventName[Event_Inactive], label);
985 void App::emit_syncdraw(char const *label, char const *area, int x, int y, int w, int h)
987 this->send_event(kListEventName[Event_SyncDraw], label, area, x, y, w, h);
990 void App::emit_flushdraw(char const *label)
992 this->send_event(kListEventName[Event_FlushDraw], label);
995 void App::emit_visible(char const *label, bool is_visible)
997 this->send_event(is_visible ? kListEventName[Event_Visible] : kListEventName[Event_Invisible], label);
1000 void App::emit_invisible(char const *label)
1002 return emit_visible(label, false);
1005 void App::emit_visible(char const *label) { return emit_visible(label, true); }
1007 result<int> App::api_request_surface(char const *appid, char const *drawing_name)
1009 auto lid = this->layers.get_layer_id(std::string(drawing_name));
1013 * register drawing_name as fallback and make it displayed.
1015 lid = this->layers.get_layer_id(std::string("Fallback"));
1016 HMI_DEBUG("wm", "%s is not registered in layers.json, then fallback as normal app", drawing_name);
1019 return Err<int>("Drawing name does not match any role, Fallback is disabled");
1023 auto rname = this->lookup_id(drawing_name);
1026 // name does not exist yet, allocate surface id...
1027 auto id = int(this->id_alloc.generate_id(drawing_name));
1028 this->layers.add_surface(id, *lid);
1030 // set the main_surface[_name] here and now
1031 if (!this->layers.main_surface_name.empty() &&
1032 this->layers.main_surface_name == drawing_name)
1034 this->layers.main_surface = id;
1035 HMI_DEBUG("wm", "Set main_surface id to %u", id);
1038 // add client into the db
1039 std::string appid_str(appid);
1040 std::string role(drawing_name);
1041 app_list.addClient(appid_str, role);
1046 // Check currently registered drawing names if it is already there.
1047 return Err<int>("Surface already present");
1050 char const *App::api_request_surface(char const *appid, char const *drawing_name,
1055 auto lid = this->layers.get_layer_id(std::string(drawing_name));
1056 unsigned sid = std::stol(ivi_id);
1061 * register drawing_name as fallback and make it displayed.
1063 lid = this->layers.get_layer_id(std::string("Fallback"));
1064 HMI_DEBUG("wm", "%s is not registered in layers.json, then fallback as normal app", drawing_name);
1067 return "Drawing name does not match any role, Fallback is disabled";
1071 auto rname = this->lookup_id(drawing_name);
1075 return "Surface already present";
1078 // register pair drawing_name and ivi_id
1079 this->id_alloc.register_name_id(drawing_name, sid);
1080 this->layers.add_surface(sid, *lid);
1082 // this surface is already created
1083 HMI_DEBUG("wm", "surface_id is %u, layer_id is %u", sid, *lid);
1085 this->controller->layers[*lid]->add_surface(sid);
1086 this->layout_commit();
1091 result<json_object *> App::api_get_display_info()
1094 if (!this->controller)
1096 return Err<json_object *>("ivi_controller global not available");
1100 compositor::size o_size = this->controller->output_size;
1101 compositor::size p_size = this->controller->physical_size;
1103 json_object *object = json_object_new_object();
1104 json_object_object_add(object, kKeyWidthPixel, json_object_new_int(o_size.w));
1105 json_object_object_add(object, kKeyHeightPixel, json_object_new_int(o_size.h));
1106 json_object_object_add(object, kKeyWidthMm, json_object_new_int(p_size.w));
1107 json_object_object_add(object, kKeyHeightMm, json_object_new_int(p_size.h));
1109 return Ok<json_object *>(object);
1112 result<json_object *> App::api_get_area_info(char const *drawing_name)
1114 HMI_DEBUG("wm", "called");
1116 // Check drawing name, surface/layer id
1117 auto const &surface_id = this->lookup_id(drawing_name);
1120 return Err<json_object *>("Surface does not exist");
1123 if (!this->controller->surface_exists(*surface_id))
1125 return Err<json_object *>("Surface does not exist in controller!");
1128 auto layer_id = this->layers.get_layer_id(*surface_id);
1131 return Err<json_object *>("Surface is not on any layer!");
1134 auto o_state = *this->layers.get_layout_state(*surface_id);
1135 if (o_state == nullptr)
1137 return Err<json_object *>("Could not find layer for surface");
1140 struct LayoutState &state = *o_state;
1141 if ((state.main != *surface_id) && (state.sub != *surface_id))
1143 return Err<json_object *>("Surface is inactive");
1146 // Set area rectangle
1147 compositor::rect area_info = this->area_info[*surface_id];
1148 json_object *object = json_object_new_object();
1149 json_object_object_add(object, kKeyX, json_object_new_int(area_info.x));
1150 json_object_object_add(object, kKeyY, json_object_new_int(area_info.y));
1151 json_object_object_add(object, kKeyWidth, json_object_new_int(area_info.w));
1152 json_object_object_add(object, kKeyHeight, json_object_new_int(area_info.h));
1154 return Ok<json_object *>(object);
1157 void App::activate(int id)
1159 auto ip = this->controller->sprops.find(id);
1160 if (ip != this->controller->sprops.end())
1162 this->controller->surfaces[id]->set_visibility(1);
1164 this->lookup_name(id).value_or("unknown-name").c_str();
1167 if ((0 == strcmp(label, "Radio")) || (0 == strcmp(label, "MediaPlayer")) || (0 == strcmp(label, "Music")) || (0 == strcmp(label, "Navigation")))
1169 for (auto i = surface_bg.begin(); i != surface_bg.end(); ++i)
1174 this->surface_bg.erase(i);
1176 // Remove from BG layer (999)
1177 HMI_DEBUG("wm", "Remove %s(%d) from BG layer", label, id);
1178 this->controller->layers[999]->remove_surface(id);
1180 // Add to FG layer (1001)
1181 HMI_DEBUG("wm", "Add %s(%d) to FG layer", label, id);
1182 this->controller->layers[1001]->add_surface(id);
1184 for (int j : this->surface_bg)
1186 HMI_DEBUG("wm", "Stored id:%d", j);
1193 this->layout_commit();
1195 this->emit_visible(label);
1196 this->emit_activated(label);
1200 void App::deactivate(int id)
1202 auto ip = this->controller->sprops.find(id);
1203 if (ip != this->controller->sprops.end())
1206 this->lookup_name(id).value_or("unknown-name").c_str();
1209 if ((0 == strcmp(label, "Radio")) || (0 == strcmp(label, "MediaPlayer")) || (0 == strcmp(label, "Music")) || (0 == strcmp(label, "Navigation")))
1213 this->surface_bg.push_back(id);
1215 // Remove from FG layer (1001)
1216 HMI_DEBUG("wm", "Remove %s(%d) from FG layer", label, id);
1217 this->controller->layers[1001]->remove_surface(id);
1219 // Add to BG layer (999)
1220 HMI_DEBUG("wm", "Add %s(%d) to BG layer", label, id);
1221 this->controller->layers[999]->add_surface(id);
1223 for (int j : surface_bg)
1225 HMI_DEBUG("wm", "Stored id:%d", j);
1230 this->controller->surfaces[id]->set_visibility(0);
1234 this->emit_deactivated(label);
1235 this->emit_invisible(label);
1239 void App::deactivate_main_surface()
1241 this->layers.main_surface = -1;
1242 std::string appid = "HomeScreen";
1243 this->api_deactivate_surface(appid.c_str(), this->layers.main_surface_name.c_str(), [](const char *) {});
1246 bool App::can_split(struct LayoutState const &state, int new_id)
1248 if (state.main != -1 && state.main != new_id)
1250 auto new_id_layer = this->layers.get_layer_id(new_id).value();
1251 auto current_id_layer = this->layers.get_layer_id(state.main).value();
1253 // surfaces are on separate layers, don't bother.
1254 if (new_id_layer != current_id_layer)
1259 std::string const &new_id_str = this->lookup_name(new_id).value();
1260 std::string const &cur_id_str = this->lookup_name(state.main).value();
1262 auto const &layer = this->layers.get_layer(new_id_layer);
1264 HMI_DEBUG("wm", "layer info name: %s", layer->name.c_str());
1266 if (layer->layouts.empty())
1271 for (auto i = layer->layouts.cbegin(); i != layer->layouts.cend(); i++)
1273 HMI_DEBUG("wm", "%d main_match '%s'", new_id_layer, i->main_match.c_str());
1274 auto rem = std::regex(i->main_match);
1275 if (std::regex_match(cur_id_str, rem))
1277 // build the second one only if the first already matched
1278 HMI_DEBUG("wm", "%d sub_match '%s'", new_id_layer, i->sub_match.c_str());
1279 auto res = std::regex(i->sub_match);
1280 if (std::regex_match(new_id_str, res))
1282 HMI_DEBUG("wm", "layout matched!");
1292 void App::try_layout(struct LayoutState & /*state*/,
1293 struct LayoutState const &new_layout,
1294 std::function<void(LayoutState const &nl)> apply)
1296 if (this->policy.layout_is_valid(new_layout))
1305 void controller_hooks::surface_created(uint32_t surface_id)
1307 this->app->surface_created(surface_id);
1310 void controller_hooks::surface_removed(uint32_t surface_id)
1312 this->app->surface_removed(surface_id);
1315 void controller_hooks::surface_visibility(uint32_t /*surface_id*/,
1318 void controller_hooks::surface_destination_rectangle(uint32_t /*surface_id*/,