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>
42 /* DrawingArea name used by "{layout}.{area}" */
43 const char kNameLayoutNormal[] = "normal";
44 const char kNameLayoutSplit[] = "split";
45 const char kNameAreaFull[] = "full";
46 const char kNameAreaMain[] = "main";
47 const char kNameAreaSub[] = "sub";
49 /* Key for json obejct */
50 const char kKeyDrawingName[] = "drawing_name";
51 const char kKeyDrawingArea[] = "drawing_area";
52 const char kKeyDrawingRect[] = "drawing_rect";
53 const char kKeyX[] = "x";
54 const char kKeyY[] = "y";
55 const char kKeyWidth[] = "width";
56 const char kKeyHeight[] = "height";
57 const char kKeyWidthPixel[] = "width_pixel";
58 const char kKeyHeightPixel[] = "height_pixel";
59 const char kKeyWidthMm[] = "width_mm";
60 const char kKeyHeightMm[] = "height_mm";
67 result<json> file_to_json(char const *filename) {
69 std::ifstream i(filename);
71 HMI_DEBUG("wm", "Could not open config file, so use default layer information");
72 j = default_layers_json;
81 struct result<layer_map> load_layer_map(char const *filename) {
82 HMI_DEBUG("wm", "loading IDs from %s", filename);
84 auto j = file_to_json(filename);
86 return Err<layer_map>(j.unwrap_err());
88 json jids = j.unwrap();
90 return to_layer_map(jids);
98 const char *g_new_role; // TODO: workaround
99 static void eventHandler(json_object* json_out) {
100 context->updateWindowResource(json_out);
105 void App::updateWindowResource(json_object* json_out) {
106 HMI_DEBUG("wm", "role:%s", rm::g_new_role);
108 // Check parking brake state
109 json_object* json_parking_brake;
110 if (!json_object_object_get_ex(json_out, "parking_brake", &json_parking_brake)) {
111 HMI_DEBUG("wm", "Not found key \"parking_brake\"");
115 json_bool is_changed;
116 is_changed = jh::getBoolFromJson(json_parking_brake, "is_changed");
118 std::string parking_brake_state = jh::getStringFromJson(json_parking_brake, "state");
119 HMI_DEBUG("wm", "parking_brake_state: %s", parking_brake_state.c_str());
121 // Update state and emit event
122 if ("parking_brake_off" == parking_brake_state) {
123 this->crr_car_info_.parking_brake_stt = false;
124 #if 0 // FOR ALS: using lightstatus brake, so do not emit parking brake event
125 this->emitParkingBrakeOff();
128 else if ("parking_brake_on" == parking_brake_state) {
129 this->crr_car_info_.parking_brake_stt = true;
130 #if 0 // FOR ALS: using lightstatus brake, so do not emit parking brake event
131 this->emitParkingBrakeOn();
135 HMI_DEBUG("wm", "Unknown parking brake state");
140 // Check accelerator pedal state
141 json_object* json_accel_pedal;
142 if (!json_object_object_get_ex(json_out, "accel_pedal", &json_accel_pedal)) {
143 HMI_DEBUG("wm", "Not found key \"accel_pedal\"");
147 is_changed = jh::getBoolFromJson(json_accel_pedal, "is_changed");
149 std::string accel_pedal_state = jh::getStringFromJson(json_accel_pedal, "state");
150 HMI_DEBUG("wm", "accel_pedal_state: %s", accel_pedal_state.c_str());
153 if ("accel_pedal_off" == accel_pedal_state) {
154 this->crr_car_info_.accel_pedal_stt = false;
156 else if ("accel_pedal_on" == accel_pedal_state) {
157 this->crr_car_info_.accel_pedal_stt = true;
160 HMI_DEBUG("wm", "Unknown accel pedal state");
165 // Check lightstatus brake state
166 json_object* json_lightstatus_brake;
167 if (!json_object_object_get_ex(json_out, "lightstatus_brake", &json_lightstatus_brake)) {
168 HMI_DEBUG("wm", "Not found key \"lightstatus_brake\"");
172 is_changed = jh::getBoolFromJson(json_lightstatus_brake, "is_changed");
174 std::string lightstatus_brake_state = jh::getStringFromJson(json_lightstatus_brake, "state");
175 HMI_DEBUG("wm", "lightstatus_brake_state: %s", lightstatus_brake_state.c_str());
177 // Update state and emit event
178 if ("lightstatus_brake_off" == lightstatus_brake_state) {
179 this->crr_car_info_.lightstatus_brake_stt = false;
180 this->emitLightstatusBrakeOff();
182 else if ("lightstatus_brake_on" == lightstatus_brake_state) {
183 this->crr_car_info_.lightstatus_brake_stt = true;
184 this->emitLightstatusBrakeOn();
187 HMI_DEBUG("wm", "Unknown lightstatus brake state");
193 json_object* json_car;
194 if (!json_object_object_get_ex(json_out, "car", &json_car)) {
195 HMI_DEBUG("wm", "Not found key \"car\"");
199 is_changed = jh::getBoolFromJson(json_car, "is_changed");
201 std::string car_state = jh::getStringFromJson(json_car, "state");
202 HMI_DEBUG("wm", "car_state: %s", car_state.c_str());
205 if ("car_stop" == car_state) {
206 this->crr_car_info_.car_stt = "stop";
209 else if ("car_run" == car_state) {
210 this->crr_car_info_.car_stt = "run";
214 HMI_DEBUG("wm", "Unknown car state");
220 json_object* json_lamp;
221 if (!json_object_object_get_ex(json_out, "lamp", &json_lamp)) {
222 HMI_DEBUG("wm", "Not found key \"lamp\"");
226 is_changed = jh::getBoolFromJson(json_lamp, "is_changed");
228 std::string lamp_state = jh::getStringFromJson(json_lamp, "state");
229 HMI_DEBUG("wm", "lamp_state: %s", lamp_state.c_str());
231 // Update state and emit event
232 if ("lamp_off" == lamp_state) {
233 this->crr_car_info_.headlamp_stt = false;
234 this->emitHeadlampOff();
236 else if ("lamp_on" == lamp_state) {
237 this->crr_car_info_.headlamp_stt = true;
238 this->emitHeadlampOn();
241 HMI_DEBUG("wm", "Unknown lamp state");
247 const char* category = nullptr;
248 std::string str_category;
249 if (nullptr != rm::g_new_role) {
250 str_category = this->pm_.roleToCategory(rm::g_new_role);
251 category = str_category.c_str();
252 HMI_DEBUG("wm", "role:%s category:%s", rm::g_new_role, category);
256 if (this->lm_.updateLayout(json_out, rm::g_new_role, category)) {
257 HMI_DEBUG("wm", "Layer is changed!!");
260 this->allocateSurface();
263 HMI_DEBUG("wm", "All layer is NOT changed!!");
271 App::App(wl::display *d)
279 pending_events(false),
283 auto l = load_layer_map(
284 this->config.get_string("layers.json").value().c_str());
286 this->layers = l.unwrap();
288 HMI_ERROR("wm", "%s", l.err().value());
291 } catch (std::exception &e) {
292 HMI_ERROR("wm", "Loading of configuration failed: %s", e.what());
295 // Initialize current car info
296 this->crr_car_info_.parking_brake_stt = true;
297 this->crr_car_info_.accel_pedal_stt = false;
298 this->crr_car_info_.accel_pedal_pos = 0;
299 this->crr_car_info_.car_stt = "stop";
300 this->crr_car_info_.headlamp_stt = false;
304 if (!this->display->ok()) {
308 if (this->layers.mapping.empty()) {
309 HMI_ERROR("wm", "No surface -> layer mapping loaded");
313 // Store my context for calling callback for PolicyManager
321 // Initialize PolicyManager
322 this->pm_.initialize();
324 // Register callback to PolicyManager
325 PolicyManager::CallbackTable callback;
326 callback.onStateTransitioned = rm::eventHandler;
327 callback.onError = nullptr;
328 this->pm_.registerCallback(callback);
330 // Initialize LayoutManager
331 this->lm_.initialize();
334 for (int i=Event_Val_Min; i<=Event_Val_Max; i++) {
335 map_afb_event[kListEventName[i]] = afb_daemon_make_event(kListEventName[i]);
338 this->display->add_global_handler(
339 "wl_output", [this](wl_registry *r, uint32_t name, uint32_t v) {
340 this->outputs.emplace_back(std::make_unique<wl::output>(r, name, v));
343 this->display->add_global_handler(
344 "ivi_wm", [this](wl_registry *r, uint32_t name, uint32_t v) {
346 std::make_unique<struct compositor::controller>(r, name, v);
348 // Init controller hooks
349 this->controller->chooks = &this->chooks;
351 // This protocol needs the output, so lets just add our mapping here...
352 this->controller->add_proxy_to_id_mapping(
353 this->outputs.back()->proxy.get(),
354 wl_proxy_get_id(reinterpret_cast<struct wl_proxy *>(
355 this->outputs.back()->proxy.get())));
358 this->controller->create_screen(this->outputs.back()->proxy.get());
360 // Set display to controller
361 this->controller->display = this->display;
364 // First level objects
365 this->display->roundtrip();
366 // Second level objects
367 this->display->roundtrip();
368 // Third level objects
369 this->display->roundtrip();
371 return init_layers();
374 int App::dispatch_pending_events() {
375 if (this->pop_pending_events()) {
376 this->display->dispatch_pending();
382 bool App::pop_pending_events() {
384 return this->pending_events.compare_exchange_strong(
385 x, false, std::memory_order_consume);
388 void App::set_pending_events() {
389 this->pending_events.store(true, std::memory_order_release);
392 optional<int> App::lookup_id(char const *name) {
393 return this->id_alloc.lookup(std::string(name));
395 optional<std::string> App::lookup_name(int id) {
396 return this->id_alloc.lookup(id);
402 int App::init_layers() {
403 if (!this->controller) {
404 HMI_ERROR("wm", "ivi_controller global not available");
408 if (this->outputs.empty()) {
409 HMI_ERROR("wm", "no output was set up!");
413 auto &c = this->controller;
415 auto &o = this->outputs.front();
416 auto &s = c->screens.begin()->second;
417 auto &layers = c->layers;
419 // Write output dimensions to ivi controller...
420 c->output_size = compositor::size{uint32_t(o->width), uint32_t(o->height)};
421 c->physical_size = compositor::size{uint32_t(o->physical_width),
422 uint32_t(o->physical_height)};
430 // Quick and dirty setup of layers
431 for (auto const &i : this->layers.mapping) {
432 c->layer_create(i.second.layer_id, o->width, o->height);
433 auto &l = layers[i.second.layer_id];
434 l->set_destination_rectangle(0, 0, o->width, o->height);
435 l->set_visibility(1);
436 HMI_DEBUG("wm", "Setting up layer %s (%d) for surface role match \"%s\"",
437 i.second.name.c_str(), i.second.layer_id, i.second.role.c_str());
440 // Add layers to screen
441 s->set_render_order(this->layers.layers);
443 this->layout_commit();
448 void App::layout_commit() {
449 this->controller->commit_changes();
450 this->display->flush();
453 const char* App::convertDrawingNameToRole(char const *drawing_name) {
456 if (this->drawingname2role_.find(drawing_name) != this->drawingname2role_.end()) {
457 // drawing_name is old role. So convert to new role.
458 role = this->drawingname2role_[drawing_name].c_str();
461 // drawing_name is new role.
464 HMI_DEBUG("wm", "drawing_name:%s -> role: %s", drawing_name, role);
469 void App::allocateWindowResource(char const *event, char const *drawing_name,
470 char const *drawing_area, const reply_func &reply) {
471 const char* new_role = nullptr;
472 const char* new_area = nullptr;
474 // Convert old role to new role
475 if ((nullptr != drawing_name) && (0 != strcmp("", drawing_name))) {
476 new_role = this->convertDrawingNameToRole(drawing_name);
479 if (0 == strcmp("activate", event)) {
481 // This process will be removed
482 // because the area "normal.full" and "normalfull" will be prohibited
484 if (0 == strcmp("restriction", new_role)) {
485 new_area = drawing_area;
488 if (nullptr == drawing_area) {
491 else if (0 == strcmp("normal.full", drawing_area)) {
494 else if (0 == strcmp("restriction.split.sub", drawing_area)) {
495 new_area = "restriction.split.sub";
497 else if (0 == strcmp("homescreen", new_role)) {
498 // Now homescreen specifies "normalfull"
505 HMI_DEBUG("wm", "drawing_area:%s, new_area: %s", drawing_area, new_area);
508 else if (0 == strcmp("deactivate", event)) {
516 // If event is "activate" and area is not specifid,
517 // get default value by using role
519 // Input event to PolicyManager
520 json_object* json_in = json_object_new_object();
521 json_object_object_add(json_in, "event", json_object_new_string(event));
522 if (nullptr != new_role) {
523 json_object_object_add(json_in, "role", json_object_new_string(new_role));
525 if (nullptr != new_area) {
526 json_object_object_add(json_in, "area", json_object_new_string(new_area));
528 rm::g_new_role = new_role; // TODO: workaround
529 this->pm_.inputEvent(json_in);
531 // Release json_object
532 json_object_put(json_in);
537 void App::enqueue_flushdraw(int surface_id) {
538 this->check_flushdraw(surface_id);
539 HMI_DEBUG("wm", "Enqueuing EndDraw for surface_id %d", surface_id);
540 this->pending_end_draw.push_back(surface_id);
543 void App::check_flushdraw(int surface_id) {
544 auto i = std::find(std::begin(this->pending_end_draw),
545 std::end(this->pending_end_draw), surface_id);
546 if (i != std::end(this->pending_end_draw)) {
547 auto n = this->lookup_name(surface_id);
548 HMI_ERROR("wm", "Application %s (%d) has pending EndDraw call(s)!",
549 n ? n->c_str() : "unknown-name", surface_id);
550 std::swap(this->pending_end_draw[std::distance(
551 std::begin(this->pending_end_draw), i)],
552 this->pending_end_draw.back());
553 this->pending_end_draw.resize(this->pending_end_draw.size() - 1);
557 void App::api_enddraw(char const *drawing_name) {
558 // Convert drawing_name to role
559 const char* role = this->convertDrawingNameToRole(drawing_name);
561 for (unsigned i = 0, iend = this->pending_end_draw.size(); i < iend; i++) {
562 auto n = this->lookup_name(this->pending_end_draw[i]);
563 if (n && *n == role) {
564 std::swap(this->pending_end_draw[i], this->pending_end_draw[iend - 1]);
565 this->pending_end_draw.resize(iend - 1);
566 this->activate(this->pending_end_draw[i]);
567 this->emit_flushdraw(drawing_name);
572 void App::api_ping() { this->dispatch_pending_events(); }
574 void App::send_event(char const *evname){
575 HMI_DEBUG("wm", "%s: %s", __func__, evname);
577 int ret = afb_event_push(this->map_afb_event[evname], nullptr);
579 HMI_DEBUG("wm", "afb_event_push failed: %m");
583 void App::send_event(char const *evname, char const *label){
584 HMI_DEBUG("wm", "%s: %s(%s)", __func__, evname, label);
586 json_object *j = json_object_new_object();
587 json_object_object_add(j, kKeyDrawingName, json_object_new_string(label));
589 int ret = afb_event_push(this->map_afb_event[evname], j);
591 HMI_DEBUG("wm", "afb_event_push failed: %m");
595 void App::send_event(char const *evname, char const *label, char const *area,
596 int x, int y, int w, int h) {
597 HMI_DEBUG("wm", "%s: %s(%s, %s) x:%d y:%d w:%d h:%d",
598 __func__, evname, label, area, x, y, w, h);
600 json_object *j_rect = json_object_new_object();
601 json_object_object_add(j_rect, kKeyX, json_object_new_int(x));
602 json_object_object_add(j_rect, kKeyY, json_object_new_int(y));
603 json_object_object_add(j_rect, kKeyWidth, json_object_new_int(w));
604 json_object_object_add(j_rect, kKeyHeight, json_object_new_int(h));
606 json_object *j = json_object_new_object();
607 json_object_object_add(j, kKeyDrawingName, json_object_new_string(label));
608 json_object_object_add(j, kKeyDrawingArea, json_object_new_string(area));
609 json_object_object_add(j, kKeyDrawingRect, j_rect);
611 int ret = afb_event_push(this->map_afb_event[evname], j);
613 HMI_DEBUG("wm", "afb_event_push failed: %m");
620 void App::surface_created(uint32_t surface_id) {
621 auto layer_id = this->layers.get_layer_id(surface_id);
623 HMI_DEBUG("wm", "Newly created surfce %d is not associated with any layer!",
628 HMI_DEBUG("wm", "surface_id is %u, layer_id is %u", surface_id, *layer_id);
630 this->controller->layers[*layer_id]->add_surface(surface_id);
631 this->layout_commit();
632 // activate the main_surface right away
633 /*if (surface_id == static_cast<unsigned>(this->layers.main_surface)) {
634 HMI_DEBUG("wm", "Activating main_surface (%d)", surface_id);
636 this->api_activate_surface(
637 this->lookup_name(surface_id).value_or("unknown-name").c_str());
641 void App::surface_removed(uint32_t surface_id) {
642 HMI_DEBUG("wm", "surface_id is %u", surface_id);
644 // We cannot normally deactivate the main_surface, so be explicit
646 if (int(surface_id) == this->layers.main_surface) {
647 this->deactivate_main_surface();
649 auto role = this->lookup_name(surface_id);
651 this->allocateWindowResource("deactivate",
652 role->c_str(), nullptr,
657 this->id_alloc.remove_id(surface_id);
658 this->layers.remove_surface(surface_id);
661 void App::emit_activated(char const *label) {
662 this->send_event(kListEventName[Event_Active], label);
665 void App::emit_deactivated(char const *label) {
666 this->send_event(kListEventName[Event_Inactive], label);
669 void App::emit_syncdraw(char const *label, char const *area, int x, int y, int w, int h) {
670 this->send_event(kListEventName[Event_SyncDraw], label, area, x, y, w, h);
673 void App::emit_flushdraw(char const *label) {
674 this->send_event(kListEventName[Event_FlushDraw], label);
677 void App::emit_visible(char const *label, bool is_visible) {
678 this->send_event(is_visible ? kListEventName[Event_Visible] : kListEventName[Event_Invisible], label);
681 void App::emit_invisible(char const *label) {
682 return emit_visible(label, false);
685 void App::emit_visible(char const *label) { return emit_visible(label, true); }
687 void App::emitHeadlampOff() {
688 // Send HeadlampOff event for all application
689 this->send_event(kListEventName[Event_HeadlampOff]);
692 void App::emitHeadlampOn() {
693 // Send HeadlampOn event for all application
694 this->send_event(kListEventName[Event_HeadlampOn]);
697 void App::emitParkingBrakeOff() {
698 // Send ParkingBrakeOff event for all application
699 this->send_event(kListEventName[Event_ParkingBrakeOff]);
702 void App::emitParkingBrakeOn() {
703 // Send ParkingBrakeOn event for all application
704 this->send_event(kListEventName[Event_ParkingBrakeOn]);
707 void App::emitLightstatusBrakeOff() {
708 // Send LightstatusBrakeOff event for all application
709 this->send_event(kListEventName[Event_LightstatusBrakeOff]);
712 void App::emitLightstatusBrakeOn() {
713 // Send LightstatusBrakeOn event for all application
714 this->send_event(kListEventName[Event_LightstatusBrakeOn]);
717 void App::emitCarStop() {
718 // Send CarStop event for all application
719 this->send_event(kListEventName[Event_CarStop]);
722 void App::emitCarRun() {
723 // Send CarRun event for all application
724 this->send_event(kListEventName[Event_CarRun]);
727 result<int> App::api_request_surface(char const *drawing_name) {
728 // Convert drawing_name to role
729 const char* role = this->convertDrawingNameToRole(drawing_name);
730 auto lid = this->layers.get_layer_id(std::string(role));
733 * register drawing_name as fallback and make it displayed.
735 lid = this->layers.get_layer_id(std::string("Fallback"));
736 HMI_DEBUG("wm", "%s is not registered in layers.json, then fallback as normal app", drawing_name);
738 return Err<int>("Drawing name does not match any role, Fallback is disabled");
742 auto rname = this->lookup_id(role);
744 // name does not exist yet, allocate surface id...
745 auto id = int(this->id_alloc.generate_id(role));
746 this->layers.add_surface(id, *lid);
748 // set the main_surface[_name] here and now
749 if (!this->layers.main_surface_name.empty() &&
750 this->layers.main_surface_name == drawing_name) {
751 this->layers.main_surface = id;
752 HMI_DEBUG("wm", "Set main_surface id to %u", id);
755 // Set map of (role, surface_id)
756 this->role2surfaceid_[role] = id;
758 // Set map of (role, drawing_name)
759 this->role2drawingname_[role] = std::string(drawing_name);
764 // Check currently registered drawing names if it is already there.
765 return Err<int>("Surface already present");
768 char const *App::api_request_surface(char const *drawing_name,
769 char const *ivi_id) {
772 auto lid = this->layers.get_layer_id(std::string(drawing_name));
773 unsigned sid = std::stol(ivi_id);
777 * register drawing_name as fallback and make it displayed.
779 lid = this->layers.get_layer_id(std::string("Fallback"));
780 HMI_DEBUG("wm", "%s is not registered in layers.json, then fallback as normal app", drawing_name);
782 return "Drawing name does not match any role, Fallback is disabled";
786 auto rname = this->lookup_id(drawing_name);
789 return "Surface already present";
792 // register pair drawing_name and ivi_id
793 this->id_alloc.register_name_id(drawing_name, sid);
794 this->layers.add_surface(sid, *lid);
796 // this surface is already created
797 HMI_DEBUG("wm", "surface_id is %u, layer_id is %u", sid, *lid);
799 this->controller->layers[*lid]->add_surface(sid);
800 this->layout_commit();
805 result<json_object *> App::api_get_display_info() {
807 if (!this->controller) {
808 return Err<json_object *>("ivi_controller global not available");
812 compositor::size o_size = this->controller->output_size;
813 compositor::size p_size = this->controller->physical_size;
815 json_object *object = json_object_new_object();
816 json_object_object_add(object, kKeyWidthPixel, json_object_new_int(o_size.w));
817 json_object_object_add(object, kKeyHeightPixel, json_object_new_int(o_size.h));
818 json_object_object_add(object, kKeyWidthMm, json_object_new_int(p_size.w));
819 json_object_object_add(object, kKeyHeightMm, json_object_new_int(p_size.h));
821 return Ok<json_object *>(object);
824 result<json_object *> App::api_get_area_info(char const *drawing_name) {
825 HMI_DEBUG("wm", "called");
827 // Convert drawing_name to role
828 const char* role = this->convertDrawingNameToRole(drawing_name);
830 // Check drawing name, surface/layer id
831 auto const &surface_id = this->lookup_id(role);
833 return Err<json_object *>("Surface does not exist");
836 if (!this->controller->surface_exists(*surface_id)) {
837 return Err<json_object *>("Surface does not exist in controller!");
840 auto layer_id = this->layers.get_layer_id(*surface_id);
842 return Err<json_object *>("Surface is not on any layer!");
845 auto o_state = *this->layers.get_layout_state(*surface_id);
846 if (o_state == nullptr) {
847 return Err<json_object *>("Could not find layer for surface");
850 struct LayoutState &state = *o_state;
851 if ((state.main != *surface_id) && (state.sub != *surface_id)) {
852 return Err<json_object *>("Surface is inactive");
855 // Set area rectangle
856 compositor::rect area_info = this->area_info[*surface_id];
857 json_object *object = json_object_new_object();
858 json_object_object_add(object, kKeyX, json_object_new_int(area_info.x));
859 json_object_object_add(object, kKeyY, json_object_new_int(area_info.y));
860 json_object_object_add(object, kKeyWidth, json_object_new_int(area_info.w));
861 json_object_object_add(object, kKeyHeight, json_object_new_int(area_info.h));
863 return Ok<json_object *>(object);
866 void App::activate(int id) {
867 auto ip = this->controller->sprops.find(id);
868 if (ip != this->controller->sprops.end()) {
869 this->controller->surfaces[id]->set_visibility(1);
871 this->lookup_name(id).value_or("unknown-name").c_str();
873 if ((0 == strcmp(label, "radio"))
874 || (0 == strcmp(label, "music"))
875 || (0 == strcmp(label, "map"))) {
876 for (auto i = surface_bg.begin(); i != surface_bg.end(); ++i) {
879 this->surface_bg.erase(i);
881 // Remove from BG layer (999)
882 HMI_DEBUG("wm", "Remove %s(%d) from BG layer", label, id);
883 this->controller->layers[999]->remove_surface(id);
885 // Add to FG layer (1001)
886 HMI_DEBUG("wm", "Add %s(%d) to FG layer", label, id);
887 this->controller->layers[1001]->add_surface(id);
889 for (int j : this->surface_bg) {
890 HMI_DEBUG("wm", "Stored id:%d", j);
897 this->layout_commit();
899 this->emit_visible(label);
900 this->emit_activated(label);
904 void App::deactivate(int id) {
905 auto ip = this->controller->sprops.find(id);
906 if (ip != this->controller->sprops.end()) {
908 this->lookup_name(id).value_or("unknown-name").c_str();
910 if ((0 == strcmp(label, "radio"))
911 || (0 == strcmp(label, "music"))
912 || (0 == strcmp(label, "map"))) {
915 this->surface_bg.push_back(id);
917 // Remove from FG layer (1001)
918 HMI_DEBUG("wm", "Remove %s(%d) from FG layer", label, id);
919 this->controller->layers[1001]->remove_surface(id);
921 // Add to BG layer (999)
922 HMI_DEBUG("wm", "Add %s(%d) to BG layer", label, id);
923 this->controller->layers[999]->add_surface(id);
925 for (int j : surface_bg) {
926 HMI_DEBUG("wm", "Stored id:%d", j);
930 this->controller->surfaces[id]->set_visibility(0);
933 this->layout_commit();
935 this->emit_deactivated(label);
936 this->emit_invisible(label);
940 void App::deactivate(std::string role) {
941 auto const &id = this->lookup_id(role.c_str());
943 HMI_ERROR("wm", "Surface does not exist");
946 std::string drawing_name = this->role2drawingname_[role];
947 HMI_DEBUG("wm", "Deactivate role:%s (drawing_name:%s)",
948 role.c_str(), drawing_name.c_str());
950 this->deactivate(*id);
953 void App::deactivate_main_surface() {
954 this->layers.main_surface = -1;
955 this->allocateWindowResource("deactivate",
956 this->layers.main_surface_name.c_str(), nullptr,
963 void controller_hooks::surface_created(uint32_t surface_id) {
964 this->app->surface_created(surface_id);
967 void controller_hooks::surface_removed(uint32_t surface_id) {
968 this->app->surface_removed(surface_id);
971 void controller_hooks::surface_visibility(uint32_t /*surface_id*/,
974 void controller_hooks::surface_destination_rectangle(uint32_t /*surface_id*/,
980 int App::allocateSurface() {
981 HMI_DEBUG("wm", "Call");
983 // Get current/previous layers
984 LayoutManager::TypeLayers crr_layers = this->lm_.getCurrentLayers();
985 LayoutManager::TypeLayers prv_layers = this->lm_.getPreviousLayers();
987 // Update resource of all layers
988 for (auto itr_layers = crr_layers.begin();
989 itr_layers != crr_layers.end(); ++itr_layers) {
991 std::string layer = itr_layers->first;
992 HMI_DEBUG("wm", "Try to update resource in %s layer", layer.c_str());
994 // If layout is changed, update resouce
995 if (this->lm_.isLayoutChanged(layer.c_str())) {
996 // Get current/previous layout
997 LayoutManager::TypeLayouts crr_layout = itr_layers->second;
998 LayoutManager::TypeLayouts prv_layout = prv_layers[layer];
1000 // Get current/previous layout name
1001 std::string crr_layout_name = crr_layout.begin()->first;
1002 std::string prv_layout_name = prv_layout.begin()->first;
1003 HMI_DEBUG("wm", "layout name crr:%s prv:%s",
1004 crr_layout_name.c_str(), prv_layout_name.c_str());
1006 // Get current/previous ares
1007 LayoutManager::TypeAreas crr_areas = crr_layout[crr_layout_name];
1008 LayoutManager::TypeAreas prv_areas = prv_layout[prv_layout_name];
1010 // Create previous displayed role list
1011 std::string prv_area_name;
1012 std::vector<std::string> prv_role_list;
1013 for (auto itr_areas = prv_areas.begin();
1014 itr_areas != prv_areas.end(); ++itr_areas) {
1015 prv_area_name = itr_areas->first;
1016 prv_role_list.push_back(prv_areas[prv_area_name]["role"]);
1017 HMI_DEBUG("wm", "previous displayed role:%s",
1018 prv_areas[prv_area_name]["role"].c_str());
1021 // Allocate surface for each area
1022 std::string crr_area_name;
1023 std::string crr_role_name;
1024 LayoutManager::TypeRolCtg crr_rol_ctg;
1025 for (auto itr_areas = crr_areas.begin();
1026 itr_areas != crr_areas.end(); ++itr_areas) {
1027 crr_area_name = itr_areas->first;
1028 crr_rol_ctg = itr_areas->second;
1030 // Get role of current area
1031 if ("category" == crr_rol_ctg.begin()->first) {
1032 // If current area have category
1033 // Get category name
1034 std::string crr_ctg = crr_rol_ctg.begin()->second;
1036 // Serch relevant role from previous displayed role list
1037 for (auto itr_role = prv_role_list.begin();
1038 itr_role != prv_role_list.end(); ++itr_role) {
1039 std::string prv_ctg = this->pm_.roleToCategory((*itr_role).c_str());
1040 if (crr_ctg == prv_ctg) {
1041 // First discovered role is set to current role
1042 crr_role_name = *itr_role;
1044 // Delete used role for other areas
1045 // which have same category
1046 prv_role_list.erase(itr_role);
1053 crr_role_name = itr_areas->second["role"];
1055 HMI_DEBUG("wm", "Allocate surface for area:%s role:%s",
1056 crr_area_name.c_str(), crr_role_name.c_str());
1058 // Deactivate non-displayed role
1059 std::string prv_role_name;
1060 if (crr_layout_name == prv_layout_name) {
1061 HMI_DEBUG("wm", "Current layout is same with previous");
1063 // Deactivate previous role in same area
1064 // if it is different with current
1065 prv_role_name = prv_areas[crr_area_name]["role"];
1066 if (crr_role_name != prv_role_name) {
1067 this->deactivate(prv_role_name);
1071 HMI_DEBUG("wm", "Current layout is different with previous");
1073 if ("none" != prv_layout_name) {
1074 // Deactivate previous role in all area in previous layout
1075 // if it is different with current role
1076 for(auto itr = prv_areas.begin(); itr != prv_areas.end(); ++itr) {
1077 prv_role_name = itr->second["role"].c_str();
1078 if (crr_role_name != prv_role_name) {
1079 this->deactivate(prv_role_name);
1085 // Set surface for displayed role
1086 if ("none" != crr_layout_name) {
1087 // If current layout is not "none",
1088 // set surface for current role
1089 this->setSurfaceSize(crr_role_name.c_str(), crr_area_name.c_str());
1092 // This API is workaround.
1093 // Resource manager should manage each resource infomations
1094 // according to architecture document.
1095 this->lm_.updateArea(layer.c_str(), crr_role_name.c_str(), crr_area_name.c_str());
1103 void App::setSurfaceSize(const char* role, const char* area) {
1104 HMI_DEBUG("wm", "role:%s area:%s", role, area);
1106 // Get size of current area
1107 compositor::rect size = this->lm_.getAreaSize(area);
1109 // Set destination to the display rectangle
1110 int surface_id = this->role2surfaceid_[role];
1112 if (!this->controller->surface_exists(surface_id)) {
1113 // Block until all pending request are processed by wayland display server
1114 // because waiting for the surface of new app is created
1115 this->display->roundtrip();
1117 auto &s = this->controller->surfaces[surface_id];
1118 s->set_destination_rectangle(size.x, size.y, size.w, size.h);
1119 this->layout_commit();
1121 // Update area information
1122 this->area_info[surface_id].x = size.x;
1123 this->area_info[surface_id].y = size.y;
1124 this->area_info[surface_id].w = size.w;
1125 this->area_info[surface_id].h = size.h;
1126 HMI_DEBUG("wm", "Surface rect { %d, %d, %d, %d }",
1127 size.x, size.y, size.w, size.h);
1129 // Emit syncDraw event
1130 const char* drawing_name = this->role2drawingname_[role].c_str();
1131 this->emit_syncdraw(drawing_name, area,
1132 size.x, size.y, size.w, size.h);
1134 // Enqueue flushDraw event
1135 this->enqueue_flushdraw(surface_id);
1138 void App::setAccelPedalPos(double val) {
1139 this->crr_car_info_.accel_pedal_pos = val;
1142 extern const char* kDefaultAppDb;
1143 int App::loadAppDb() {
1144 HMI_DEBUG("wm", "Call");
1146 // Get afm application installed dir
1147 char const *afm_app_install_dir = getenv("AFM_APP_INSTALL_DIR");
1148 HMI_DEBUG("wm", "afm_app_install_dir:%s", afm_app_install_dir);
1150 std::string file_name;
1151 if (!afm_app_install_dir) {
1152 HMI_ERROR("wm", "AFM_APP_INSTALL_DIR is not defined");
1155 file_name = std::string(afm_app_install_dir) + std::string("/etc/app.db");
1159 json_object* json_obj;
1160 int ret = jh::inputJsonFilie(file_name.c_str(), &json_obj);
1162 HMI_ERROR("wm", "Could not open app.db, so use default role information");
1163 json_obj = json_tokener_parse(kDefaultAppDb);
1165 HMI_DEBUG("wm", "json_obj dump:%s", json_object_get_string(json_obj));
1168 HMI_DEBUG("wm", "Perse apps");
1169 json_object* json_cfg;
1170 if (!json_object_object_get_ex(json_obj, "apps", &json_cfg)) {
1171 HMI_ERROR("wm", "Parse Error!!");
1175 int len = json_object_array_length(json_cfg);
1176 HMI_DEBUG("wm", "json_cfg len:%d", len);
1177 HMI_DEBUG("wm", "json_cfg dump:%s", json_object_get_string(json_cfg));
1179 for (int i=0; i<len; i++) {
1180 json_object* json_tmp = json_object_array_get_idx(json_cfg, i);
1181 HMI_DEBUG("wm", "> json_tmp dump:%s", json_object_get_string(json_tmp));
1183 const char* app = jh::getStringFromJson(json_tmp, "name");
1184 if (nullptr == app) {
1185 HMI_ERROR("wm", "Parse Error!!");
1188 HMI_DEBUG("wm", "> app:%s", app);
1190 const char* role = jh::getStringFromJson(json_tmp, "role");
1191 if (nullptr == role) {
1192 HMI_ERROR("wm", "Parse Error!!");
1195 HMI_DEBUG("wm", "> role:%s", role);
1197 this->drawingname2role_[app] = std::string(role);
1201 for(auto itr = this->drawingname2role_.begin();
1202 itr != this->drawingname2role_.end(); ++itr) {
1203 HMI_DEBUG("wm", "app:%s role:%s",
1204 itr->first.c_str(), itr->second.c_str());
1207 // Release json_object
1208 json_object_put(json_obj);
1214 const char* kDefaultAppDb = "{ \
1217 \"name\": \"HomeScreen\", \
1218 \"role\": \"homescreen\" \
1221 \"name\": \"Music\", \
1222 \"role\": \"music\" \
1225 \"name\": \"MediaPlayer\", \
1226 \"role\": \"music\" \
1229 \"name\": \"Video\", \
1230 \"role\": \"video\" \
1233 \"name\": \"VideoPlayer\", \
1234 \"role\": \"video\" \
1237 \"name\": \"WebBrowser\", \
1238 \"role\": \"browser\" \
1241 \"name\": \"Radio\", \
1242 \"role\": \"radio\" \
1245 \"name\": \"Phone\", \
1246 \"role\": \"phone\" \
1249 \"name\": \"Navigation\", \
1253 \"name\": \"HVAC\", \
1254 \"role\": \"hvac\" \
1257 \"name\": \"Settings\", \
1258 \"role\": \"settings\" \
1261 \"name\": \"Dashboard\", \
1262 \"role\": \"dashboard\" \
1265 \"name\": \"POI\", \
1269 \"name\": \"Mixer\", \
1270 \"role\": \"mixer\" \