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";
61 const char kKeyAppID[] = "id";
68 result<json> file_to_json(char const *filename) {
70 std::ifstream i(filename);
72 HMI_DEBUG("wm", "Could not open config file, so use default layer information");
73 j = default_layers_json;
82 struct result<layer_map> load_layer_map(char const *filename) {
83 HMI_DEBUG("wm", "loading IDs from %s", filename);
85 auto j = file_to_json(filename);
87 return Err<layer_map>(j.unwrap_err());
89 json jids = j.unwrap();
91 return to_layer_map(jids);
99 std::string g_new_role; // TODO: workaround
100 static void eventHandler(json_object* json_out) {
101 context->updateWindowResource(json_out);
106 void App::updateWindowResource(json_object* json_out) {
107 HMI_DEBUG("wm", "role:%s", rm::g_new_role.c_str());
109 // Check parking brake state
110 json_object* json_parking_brake;
111 if (!json_object_object_get_ex(json_out, "parking_brake", &json_parking_brake)) {
112 HMI_DEBUG("wm", "Not found key \"parking_brake\"");
116 json_bool is_changed;
117 is_changed = jh::getBoolFromJson(json_parking_brake, "is_changed");
119 std::string parking_brake_state = jh::getStringFromJson(json_parking_brake, "state");
120 HMI_DEBUG("wm", "parking_brake_state: %s", parking_brake_state.c_str());
122 // Update state and emit event
123 if ("parking_brake_off" == parking_brake_state) {
124 this->crr_car_info_.parking_brake_stt = false;
125 #if 0 // FOR ALS: using lightstatus brake, so do not emit parking brake event
126 this->emitParkingBrakeOff();
129 else if ("parking_brake_on" == parking_brake_state) {
130 this->crr_car_info_.parking_brake_stt = true;
131 #if 0 // FOR ALS: using lightstatus brake, so do not emit parking brake event
132 this->emitParkingBrakeOn();
136 HMI_DEBUG("wm", "Unknown parking brake state");
141 // Check accelerator pedal state
142 json_object* json_accel_pedal;
143 if (!json_object_object_get_ex(json_out, "accel_pedal", &json_accel_pedal)) {
144 HMI_DEBUG("wm", "Not found key \"accel_pedal\"");
148 is_changed = jh::getBoolFromJson(json_accel_pedal, "is_changed");
150 std::string accel_pedal_state = jh::getStringFromJson(json_accel_pedal, "state");
151 HMI_DEBUG("wm", "accel_pedal_state: %s", accel_pedal_state.c_str());
154 if ("accel_pedal_off" == accel_pedal_state) {
155 this->crr_car_info_.accel_pedal_stt = false;
157 else if ("accel_pedal_on" == accel_pedal_state) {
158 this->crr_car_info_.accel_pedal_stt = true;
161 HMI_DEBUG("wm", "Unknown accel pedal state");
166 // Check lightstatus brake state
167 json_object* json_lightstatus_brake;
168 if (!json_object_object_get_ex(json_out, "lightstatus_brake", &json_lightstatus_brake)) {
169 HMI_DEBUG("wm", "Not found key \"lightstatus_brake\"");
173 is_changed = jh::getBoolFromJson(json_lightstatus_brake, "is_changed");
175 std::string lightstatus_brake_state = jh::getStringFromJson(json_lightstatus_brake, "state");
176 HMI_DEBUG("wm", "lightstatus_brake_state: %s", lightstatus_brake_state.c_str());
178 // Update state and emit event
179 if ("lightstatus_brake_off" == lightstatus_brake_state) {
180 this->crr_car_info_.lightstatus_brake_stt = false;
181 this->emitLightstatusBrakeOff();
183 else if ("lightstatus_brake_on" == lightstatus_brake_state) {
184 this->crr_car_info_.lightstatus_brake_stt = true;
185 this->emitLightstatusBrakeOn();
188 HMI_DEBUG("wm", "Unknown lightstatus brake state");
194 json_object* json_car;
195 if (!json_object_object_get_ex(json_out, "car", &json_car)) {
196 HMI_DEBUG("wm", "Not found key \"car\"");
200 is_changed = jh::getBoolFromJson(json_car, "is_changed");
202 std::string car_state = jh::getStringFromJson(json_car, "state");
203 HMI_DEBUG("wm", "car_state: %s", car_state.c_str());
206 if ("car_stop" == car_state) {
207 this->crr_car_info_.car_stt = "stop";
210 else if ("car_run" == car_state) {
211 this->crr_car_info_.car_stt = "run";
215 HMI_DEBUG("wm", "Unknown car state");
221 json_object* json_lamp;
222 if (!json_object_object_get_ex(json_out, "lamp", &json_lamp)) {
223 HMI_DEBUG("wm", "Not found key \"lamp\"");
227 is_changed = jh::getBoolFromJson(json_lamp, "is_changed");
229 std::string lamp_state = jh::getStringFromJson(json_lamp, "state");
230 HMI_DEBUG("wm", "lamp_state: %s", lamp_state.c_str());
232 // Update state and emit event
233 if ("lamp_off" == lamp_state) {
234 this->crr_car_info_.headlamp_stt = false;
235 this->emitHeadlampOff();
237 else if ("lamp_on" == lamp_state) {
238 this->crr_car_info_.headlamp_stt = true;
239 this->emitHeadlampOn();
242 HMI_DEBUG("wm", "Unknown lamp state");
248 const char* category = nullptr;
249 std::string str_category;
250 str_category = this->pm_.roleToCategory(rm::g_new_role.c_str());
251 category = str_category.c_str();
252 HMI_DEBUG("wm", "role:%s category:%s", rm::g_new_role.c_str(), category);
255 if (this->lm_.updateLayout(json_out, rm::g_new_role.c_str(), category)) {
256 HMI_DEBUG("wm", "Layer is changed!!");
259 this->allocateSurface();
262 HMI_DEBUG("wm", "All layer is NOT changed!!");
270 App::App(wl::display *d)
278 pending_events(false),
282 auto l = load_layer_map(
283 this->config.get_string("layers.json").value().c_str());
285 this->layers = l.unwrap();
287 HMI_ERROR("wm", "%s", l.err().value());
290 } catch (std::exception &e) {
291 HMI_ERROR("wm", "Loading of configuration failed: %s", e.what());
294 // Initialize current car info
295 this->crr_car_info_.parking_brake_stt = true;
296 this->crr_car_info_.accel_pedal_stt = false;
297 this->crr_car_info_.accel_pedal_pos = 0;
298 this->crr_car_info_.car_stt = "stop";
299 this->crr_car_info_.headlamp_stt = false;
303 if (!this->display->ok()) {
307 if (this->layers.mapping.empty()) {
308 HMI_ERROR("wm", "No surface -> layer mapping loaded");
312 // Store my context for calling callback for PolicyManager
320 // Initialize PolicyManager
321 this->pm_.initialize();
323 // Register callback to PolicyManager
324 PolicyManager::CallbackTable callback;
325 callback.onStateTransitioned = rm::eventHandler;
326 callback.onError = nullptr;
327 this->pm_.registerCallback(callback);
329 // Initialize LayoutManager
330 this->lm_.initialize();
333 for (int i=Event_Val_Min; i<=Event_Val_Max; i++) {
334 map_afb_event[kListEventName[i]] = afb_daemon_make_event(kListEventName[i]);
337 this->display->add_global_handler(
338 "wl_output", [this](wl_registry *r, uint32_t name, uint32_t v) {
339 this->outputs.emplace_back(std::make_unique<wl::output>(r, name, v));
342 this->display->add_global_handler(
343 "ivi_wm", [this](wl_registry *r, uint32_t name, uint32_t v) {
345 std::make_unique<struct compositor::controller>(r, name, v);
347 // Init controller hooks
348 this->controller->chooks = &this->chooks;
350 // This protocol needs the output, so lets just add our mapping here...
351 this->controller->add_proxy_to_id_mapping(
352 this->outputs.back()->proxy.get(),
353 wl_proxy_get_id(reinterpret_cast<struct wl_proxy *>(
354 this->outputs.back()->proxy.get())));
357 this->controller->create_screen(this->outputs.back()->proxy.get());
359 // Set display to controller
360 this->controller->display = this->display;
363 // First level objects
364 this->display->roundtrip();
365 // Second level objects
366 this->display->roundtrip();
367 // Third level objects
368 this->display->roundtrip();
370 return init_layers();
373 int App::dispatch_pending_events() {
374 if (this->pop_pending_events()) {
375 this->display->dispatch_pending();
381 bool App::pop_pending_events() {
383 return this->pending_events.compare_exchange_strong(
384 x, false, std::memory_order_consume);
387 void App::set_pending_events() {
388 this->pending_events.store(true, std::memory_order_release);
391 optional<int> App::lookup_id(char const *name) {
392 return this->id_alloc.lookup(std::string(name));
394 optional<std::string> App::lookup_name(int id) {
395 return this->id_alloc.lookup(id);
401 int App::init_layers() {
402 if (!this->controller) {
403 HMI_ERROR("wm", "ivi_controller global not available");
407 if (this->outputs.empty()) {
408 HMI_ERROR("wm", "no output was set up!");
412 auto &c = this->controller;
414 auto &o = this->outputs.front();
415 auto &s = c->screens.begin()->second;
416 auto &layers = c->layers;
418 // Write output dimensions to ivi controller...
419 c->output_size = compositor::size{uint32_t(o->width), uint32_t(o->height)};
420 c->physical_size = compositor::size{uint32_t(o->physical_width),
421 uint32_t(o->physical_height)};
429 // Quick and dirty setup of layers
430 for (auto const &i : this->layers.mapping) {
431 c->layer_create(i.second.layer_id, o->width, o->height);
432 auto &l = layers[i.second.layer_id];
433 l->set_destination_rectangle(0, 0, o->width, o->height);
434 l->set_visibility(1);
435 HMI_DEBUG("wm", "Setting up layer %s (%d) for surface role match \"%s\"",
436 i.second.name.c_str(), i.second.layer_id, i.second.role.c_str());
439 // Add layers to screen
440 s->set_render_order(this->layers.layers);
442 this->layout_commit();
447 void App::layout_commit() {
448 this->controller->commit_changes();
449 this->display->flush();
452 const char* App::convertDrawingNameToRole(char const *drawing_name) {
455 if (this->drawingname2role_.find(drawing_name) != this->drawingname2role_.end()) {
456 // drawing_name is old role. So convert to new role.
457 role = this->drawingname2role_[drawing_name].c_str();
460 // drawing_name is new role.
463 HMI_DEBUG("wm", "drawing_name:%s -> role: %s", drawing_name, role);
468 void App::allocateWindowResource(char const *event, char const *drawing_name,
469 char const *drawing_area, const reply_func &reply) {
470 const char* new_role = nullptr;
471 const char* new_area = nullptr;
473 // Convert old role to new role
474 if ((nullptr != drawing_name) && (0 != strcmp("", drawing_name))) {
475 new_role = this->convertDrawingNameToRole(drawing_name);
478 if (0 == strcmp("activate", event)) {
480 // This process will be removed
481 // because the area "normal.full" and "normalfull" will be prohibited
483 if (0 == strcmp("restriction", new_role)) {
484 new_area = drawing_area;
487 if (nullptr == drawing_area) {
490 else if (0 == strcmp("normal.full", drawing_area)) {
493 else if (0 == strcmp("restriction.split.sub", drawing_area)) {
494 new_area = "restriction.split.sub";
496 else if (0 == strcmp("homescreen", new_role)) {
497 // Now homescreen specifies "normalfull"
504 HMI_DEBUG("wm", "drawing_area:%s, new_area: %s", drawing_area, new_area);
507 else if (0 == strcmp("deactivate", event)) {
515 // If event is "activate" and area is not specifid,
516 // get default value by using role
518 // Input event to PolicyManager
519 json_object* json_in = json_object_new_object();
520 json_object_object_add(json_in, "event", json_object_new_string(event));
521 if (nullptr != new_role) {
522 json_object_object_add(json_in, "role", json_object_new_string(new_role));
524 if (nullptr != new_area) {
525 json_object_object_add(json_in, "area", json_object_new_string(new_area));
529 if (nullptr != new_role) {
530 rm::g_new_role = std::string(new_role);
533 rm::g_new_role = std::string("");
536 this->pm_.inputEvent(json_in);
538 // Release json_object
539 json_object_put(json_in);
544 void App::enqueue_flushdraw(int surface_id) {
545 this->check_flushdraw(surface_id);
546 HMI_DEBUG("wm", "Enqueuing EndDraw for surface_id %d", surface_id);
547 this->pending_end_draw.push_back(surface_id);
550 void App::check_flushdraw(int surface_id) {
551 auto i = std::find(std::begin(this->pending_end_draw),
552 std::end(this->pending_end_draw), surface_id);
553 if (i != std::end(this->pending_end_draw)) {
554 auto n = this->lookup_name(surface_id);
555 HMI_ERROR("wm", "Application %s (%d) has pending EndDraw call(s)!",
556 n ? n->c_str() : "unknown-name", surface_id);
557 std::swap(this->pending_end_draw[std::distance(
558 std::begin(this->pending_end_draw), i)],
559 this->pending_end_draw.back());
560 this->pending_end_draw.resize(this->pending_end_draw.size() - 1);
564 void App::api_enddraw(char const *appid, char const *drawing_name) {
565 // Convert drawing_name to role
566 const char* role = this->convertDrawingNameToRole(drawing_name);
568 for (unsigned i = 0, iend = this->pending_end_draw.size(); i < iend; i++) {
569 auto n = this->lookup_name(this->pending_end_draw[i]);
570 if (n && *n == role) {
571 std::swap(this->pending_end_draw[i], this->pending_end_draw[iend - 1]);
572 this->pending_end_draw.resize(iend - 1);
573 this->activate(this->pending_end_draw[i]);
574 this->emit_flushdraw(drawing_name);
575 this->emitScreenUpdated(appid);
580 void App::api_ping() { this->dispatch_pending_events(); }
582 void App::send_event(char const *evname){
583 HMI_DEBUG("wm", "%s: %s", __func__, evname);
585 int ret = afb_event_push(this->map_afb_event[evname], nullptr);
587 HMI_DEBUG("wm", "afb_event_push failed: %m");
591 void App::send_event(char const *evname, char const *label){
592 HMI_DEBUG("wm", "%s: %s(%s)", __func__, evname, label);
594 json_object *j = json_object_new_object();
595 json_object_object_add(j, kKeyDrawingName, json_object_new_string(label));
597 int ret = afb_event_push(this->map_afb_event[evname], j);
599 HMI_DEBUG("wm", "afb_event_push failed: %m");
603 void App::send_event(char const *evname, char const *label, char const *area,
604 int x, int y, int w, int h) {
605 HMI_DEBUG("wm", "%s: %s(%s, %s) x:%d y:%d w:%d h:%d",
606 __func__, evname, label, area, x, y, w, h);
608 json_object *j_rect = json_object_new_object();
609 json_object_object_add(j_rect, kKeyX, json_object_new_int(x));
610 json_object_object_add(j_rect, kKeyY, json_object_new_int(y));
611 json_object_object_add(j_rect, kKeyWidth, json_object_new_int(w));
612 json_object_object_add(j_rect, kKeyHeight, json_object_new_int(h));
614 json_object *j = json_object_new_object();
615 json_object_object_add(j, kKeyDrawingName, json_object_new_string(label));
616 json_object_object_add(j, kKeyDrawingArea, json_object_new_string(area));
617 json_object_object_add(j, kKeyDrawingRect, j_rect);
619 int ret = afb_event_push(this->map_afb_event[evname], j);
621 HMI_DEBUG("wm", "afb_event_push failed: %m");
628 void App::surface_created(uint32_t surface_id) {
629 auto layer_id = this->layers.get_layer_id(surface_id);
631 HMI_DEBUG("wm", "Newly created surfce %d is not associated with any layer!",
636 HMI_DEBUG("wm", "surface_id is %u, layer_id is %u", surface_id, *layer_id);
638 this->controller->layers[*layer_id]->add_surface(surface_id);
639 this->layout_commit();
640 // activate the main_surface right away
641 /*if (surface_id == static_cast<unsigned>(this->layers.main_surface)) {
642 HMI_DEBUG("wm", "Activating main_surface (%d)", surface_id);
644 this->api_activate_surface(
645 this->lookup_name(surface_id).value_or("unknown-name").c_str());
649 void App::surface_removed(uint32_t surface_id) {
650 HMI_DEBUG("wm", "surface_id is %u", surface_id);
652 // We cannot normally deactivate the main_surface, so be explicit
654 if (int(surface_id) == this->layers.main_surface) {
655 this->deactivate_main_surface();
657 auto role = this->lookup_name(surface_id);
659 this->allocateWindowResource("deactivate",
660 role->c_str(), nullptr,
665 this->id_alloc.remove_id(surface_id);
666 this->layers.remove_surface(surface_id);
669 void App::emit_activated(char const *label) {
670 this->send_event(kListEventName[Event_Active], label);
673 void App::emit_deactivated(char const *label) {
674 this->send_event(kListEventName[Event_Inactive], label);
677 void App::emit_syncdraw(char const *label, char const *area, int x, int y, int w, int h) {
678 this->send_event(kListEventName[Event_SyncDraw], label, area, x, y, w, h);
681 void App::emit_flushdraw(char const *label) {
682 this->send_event(kListEventName[Event_FlushDraw], label);
685 void App::emit_visible(char const *label, bool is_visible) {
686 this->send_event(is_visible ? kListEventName[Event_Visible] : kListEventName[Event_Invisible], label);
689 void App::emit_invisible(char const *label) {
690 return emit_visible(label, false);
693 void App::__send_event_temporary_extend(char const *evname, char const *appid) {
694 HMI_DEBUG("wm", "send event %s: %s", evname, appid);
696 json_object *j = json_object_new_object();
697 json_object_object_add(j, kKeyAppID, json_object_new_string(appid));
699 int ret = afb_event_push(this->map_afb_event[evname], j);
701 HMI_DEBUG("wm", "afb_event_push failed: %m");
705 void App::emit_visible(char const *label) { return emit_visible(label, true); }
707 void App::emitHeadlampOff() {
708 // Send HeadlampOff event for all application
709 this->send_event(kListEventName[Event_HeadlampOff]);
712 void App::emitHeadlampOn() {
713 // Send HeadlampOn event for all application
714 this->send_event(kListEventName[Event_HeadlampOn]);
717 void App::emitParkingBrakeOff() {
718 // Send ParkingBrakeOff event for all application
719 this->send_event(kListEventName[Event_ParkingBrakeOff]);
722 void App::emitParkingBrakeOn() {
723 // Send ParkingBrakeOn event for all application
724 this->send_event(kListEventName[Event_ParkingBrakeOn]);
727 void App::emitLightstatusBrakeOff() {
728 // Send LightstatusBrakeOff event for all application
729 this->send_event(kListEventName[Event_LightstatusBrakeOff]);
732 void App::emitLightstatusBrakeOn() {
733 // Send LightstatusBrakeOn event for all application
734 this->send_event(kListEventName[Event_LightstatusBrakeOn]);
737 void App::emitCarStop() {
738 // Send CarStop event for all application
739 this->send_event(kListEventName[Event_CarStop]);
742 void App::emitCarRun() {
743 // Send CarRun event for all application
744 this->send_event(kListEventName[Event_CarRun]);
747 void App::emitScreenUpdated(char const* appid) {
748 // Send ScreenUpdated event for applications which calls wmsubscribe
749 this->__send_event_temporary_extend(kListEventName[Event_ScreenUpdated], appid);
752 result<int> App::api_request_surface(char const *drawing_name) {
753 // Convert drawing_name to role
754 const char* role = this->convertDrawingNameToRole(drawing_name);
756 auto lid = this->layers.get_layer_id(std::string(role));
759 * register drawing_name as fallback and make it displayed.
761 lid = this->layers.get_layer_id(std::string("Fallback"));
762 HMI_DEBUG("wm", "%s is not registered in layers.json, then fallback as normal app", drawing_name);
764 return Err<int>("Drawing name does not match any role, Fallback is disabled");
768 auto rname = this->lookup_id(role);
770 // name does not exist yet, allocate surface id...
771 auto id = int(this->id_alloc.generate_id(role));
772 this->layers.add_surface(id, *lid);
774 // set the main_surface[_name] here and now
775 if (!this->layers.main_surface_name.empty() &&
776 this->layers.main_surface_name == drawing_name) {
777 this->layers.main_surface = id;
778 HMI_DEBUG("wm", "Set main_surface id to %u", id);
781 // Set map of (role, surface_id)
782 this->role2surfaceid_[role] = id;
784 // Set map of (role, drawing_name)
785 this->role2drawingname_[role] = std::string(drawing_name);
790 // Check currently registered drawing names if it is already there.
791 return Err<int>("Surface already present");
794 char const *App::api_request_surface(char const *drawing_name,
795 char const *ivi_id) {
797 // Convert drawing_name to role
798 const char* role = this->convertDrawingNameToRole(drawing_name);
800 auto lid = this->layers.get_layer_id(std::string(role));
801 unsigned sid = std::stol(ivi_id);
805 * register drawing_name as fallback and make it displayed.
807 lid = this->layers.get_layer_id(std::string("Fallback"));
808 HMI_DEBUG("wm", "%s is not registered in layers.json, then fallback as normal app", drawing_name);
810 return "Drawing name does not match any role, Fallback is disabled";
814 auto rname = this->lookup_id(role);
817 return "Surface already present";
820 // register pair drawing_name and ivi_id
821 this->id_alloc.register_name_id(role, sid);
822 this->layers.add_surface(sid, *lid);
824 // this surface is already created
825 HMI_DEBUG("wm", "surface_id is %u, layer_id is %u", sid, *lid);
827 this->controller->layers[*lid]->add_surface(sid);
828 this->layout_commit();
833 result<json_object *> App::api_get_display_info() {
835 if (!this->controller) {
836 return Err<json_object *>("ivi_controller global not available");
840 compositor::size o_size = this->controller->output_size;
841 compositor::size p_size = this->controller->physical_size;
843 json_object *object = json_object_new_object();
844 json_object_object_add(object, kKeyWidthPixel, json_object_new_int(o_size.w));
845 json_object_object_add(object, kKeyHeightPixel, json_object_new_int(o_size.h));
846 json_object_object_add(object, kKeyWidthMm, json_object_new_int(p_size.w));
847 json_object_object_add(object, kKeyHeightMm, json_object_new_int(p_size.h));
849 return Ok<json_object *>(object);
852 result<json_object *> App::api_get_area_info(char const *drawing_name) {
853 HMI_DEBUG("wm", "called");
855 // Convert drawing_name to role
856 const char* role = this->convertDrawingNameToRole(drawing_name);
858 // Check drawing name, surface/layer id
859 auto const &surface_id = this->lookup_id(role);
861 return Err<json_object *>("Surface does not exist");
864 if (!this->controller->surface_exists(*surface_id)) {
865 return Err<json_object *>("Surface does not exist in controller!");
868 auto layer_id = this->layers.get_layer_id(*surface_id);
870 return Err<json_object *>("Surface is not on any layer!");
873 auto o_state = *this->layers.get_layout_state(*surface_id);
874 if (o_state == nullptr) {
875 return Err<json_object *>("Could not find layer for surface");
878 // Set area rectangle
879 compositor::rect area_info = this->area_info[*surface_id];
880 json_object *object = json_object_new_object();
881 json_object_object_add(object, kKeyX, json_object_new_int(area_info.x));
882 json_object_object_add(object, kKeyY, json_object_new_int(area_info.y));
883 json_object_object_add(object, kKeyWidth, json_object_new_int(area_info.w));
884 json_object_object_add(object, kKeyHeight, json_object_new_int(area_info.h));
886 return Ok<json_object *>(object);
889 result<json_object *> App::api_get_car_info(char const *label) {
890 HMI_DEBUG("wm", "called");
892 json_object *j_in = nullptr;
893 json_object *j_out = nullptr;
895 if (0 == strcmp("parking_brake_status", label)) {
896 // Get parking brake status
897 json_bool val = this->crr_car_info_.parking_brake_stt;
898 j_in = json_object_new_boolean(val);
900 else if (0 == strcmp("accelerator.pedal.position", label)) {
901 // Get accelerator pedal position
902 double val = this->crr_car_info_.accel_pedal_pos;
903 j_in = json_object_new_double(val);
905 else if (0 == strcmp("car_state", label)) {
907 const char* val = this->crr_car_info_.car_stt;
908 j_in = json_object_new_string(val);
910 else if (0 == strcmp("lightstatus.brake", label)) {
911 // Get lightstatus brake status
912 json_bool val = this->crr_car_info_.lightstatus_brake_stt;
913 j_in = json_object_new_boolean(val);
916 return Err<json_object *>("Car info does not exist");
919 // Create output object
920 j_out = json_object_new_object();
921 json_object_object_add(j_out, "value", j_in);
923 return Ok<json_object *>(j_out);
926 void App::activate(int id) {
927 auto ip = this->controller->sprops.find(id);
928 if (ip != this->controller->sprops.end()) {
929 this->controller->surfaces[id]->set_visibility(1);
931 this->lookup_name(id).value_or("unknown-name").c_str();
933 if ((0 == strcmp(label, "radio"))
934 || (0 == strcmp(label, "music"))
935 || (0 == strcmp(label, "video"))
936 || (0 == strcmp(label, "map"))) {
937 for (auto i = surface_bg.begin(); i != surface_bg.end(); ++i) {
940 this->surface_bg.erase(i);
942 // Remove from BG layer (999)
943 HMI_DEBUG("wm", "Remove %s(%d) from BG layer", label, id);
944 this->controller->layers[999]->remove_surface(id);
946 // Add to FG layer (1001)
947 HMI_DEBUG("wm", "Add %s(%d) to FG layer", label, id);
948 this->controller->layers[1001]->add_surface(id);
950 for (int j : this->surface_bg) {
951 HMI_DEBUG("wm", "Stored id:%d", j);
958 this->layout_commit();
960 this->emit_visible(label);
961 this->emit_activated(label);
965 void App::deactivate(int id) {
966 auto ip = this->controller->sprops.find(id);
967 if (ip != this->controller->sprops.end()) {
969 this->lookup_name(id).value_or("unknown-name").c_str();
971 if ((0 == strcmp(label, "radio"))
972 || (0 == strcmp(label, "music"))
973 || (0 == strcmp(label, "video"))
974 || (0 == strcmp(label, "map"))) {
977 this->surface_bg.push_back(id);
979 // Remove from FG layer (1001)
980 HMI_DEBUG("wm", "Remove %s(%d) from FG layer", label, id);
981 this->controller->layers[1001]->remove_surface(id);
983 // Add to BG layer (999)
984 HMI_DEBUG("wm", "Add %s(%d) to BG layer", label, id);
985 this->controller->layers[999]->add_surface(id);
987 for (int j : surface_bg) {
988 HMI_DEBUG("wm", "Stored id:%d", j);
992 this->controller->surfaces[id]->set_visibility(0);
995 this->layout_commit();
997 this->emit_deactivated(label);
998 this->emit_invisible(label);
1002 void App::deactivate(std::string role) {
1003 auto const &id = this->lookup_id(role.c_str());
1005 HMI_ERROR("wm", "Surface does not exist");
1008 std::string drawing_name = this->role2drawingname_[role];
1009 HMI_DEBUG("wm", "Deactivate role:%s (drawing_name:%s)",
1010 role.c_str(), drawing_name.c_str());
1012 this->deactivate(*id);
1015 void App::deactivate_main_surface() {
1016 this->layers.main_surface = -1;
1017 this->allocateWindowResource("deactivate",
1018 this->layers.main_surface_name.c_str(), nullptr,
1025 void controller_hooks::surface_created(uint32_t surface_id) {
1026 this->app->surface_created(surface_id);
1029 void controller_hooks::surface_removed(uint32_t surface_id) {
1030 this->app->surface_removed(surface_id);
1033 void controller_hooks::surface_visibility(uint32_t /*surface_id*/,
1036 void controller_hooks::surface_destination_rectangle(uint32_t /*surface_id*/,
1042 int App::allocateSurface() {
1043 HMI_DEBUG("wm", "Call");
1045 // Get current/previous layers
1046 LayoutManager::TypeLayers crr_layers = this->lm_.getCurrentLayers();
1047 LayoutManager::TypeLayers prv_layers = this->lm_.getPreviousLayers();
1049 // Update resource of all layers
1050 for (auto itr_layers = crr_layers.begin();
1051 itr_layers != crr_layers.end(); ++itr_layers) {
1053 std::string layer = itr_layers->first;
1054 HMI_DEBUG("wm", "Try to update resource in %s layer", layer.c_str());
1056 // If layout is changed, update resouce
1057 if (this->lm_.isLayoutChanged(layer.c_str())) {
1058 // Get current/previous layout
1059 LayoutManager::TypeLayouts crr_layout = itr_layers->second;
1060 LayoutManager::TypeLayouts prv_layout = prv_layers[layer];
1062 // Get current/previous layout name
1063 std::string crr_layout_name = crr_layout.begin()->first;
1064 std::string prv_layout_name = prv_layout.begin()->first;
1065 HMI_DEBUG("wm", "layout name crr:%s prv:%s",
1066 crr_layout_name.c_str(), prv_layout_name.c_str());
1068 // Get current/previous ares
1069 LayoutManager::TypeAreas crr_areas = crr_layout[crr_layout_name];
1070 LayoutManager::TypeAreas prv_areas = prv_layout[prv_layout_name];
1072 // Create previous displayed role list
1073 std::string prv_area_name;
1074 std::vector<std::string> prv_role_list;
1075 for (auto itr_areas = prv_areas.begin();
1076 itr_areas != prv_areas.end(); ++itr_areas) {
1077 prv_area_name = itr_areas->first;
1078 prv_role_list.push_back(prv_areas[prv_area_name]["role"]);
1079 HMI_DEBUG("wm", "previous displayed role:%s",
1080 prv_areas[prv_area_name]["role"].c_str());
1083 // Allocate surface for each area
1084 std::string crr_area_name;
1085 std::string crr_role_name;
1086 LayoutManager::TypeRolCtg crr_rol_ctg;
1087 for (auto itr_areas = crr_areas.begin();
1088 itr_areas != crr_areas.end(); ++itr_areas) {
1089 crr_area_name = itr_areas->first;
1090 crr_rol_ctg = itr_areas->second;
1092 // Get role of current area
1093 if ("category" == crr_rol_ctg.begin()->first) {
1094 // If current area have category
1095 // Get category name
1096 std::string crr_ctg = crr_rol_ctg.begin()->second;
1098 // Serch relevant role from previous displayed role list
1099 for (auto itr_role = prv_role_list.begin();
1100 itr_role != prv_role_list.end(); ++itr_role) {
1101 std::string prv_ctg = this->pm_.roleToCategory((*itr_role).c_str());
1102 if (crr_ctg == prv_ctg) {
1103 // First discovered role is set to current role
1104 crr_role_name = *itr_role;
1106 // Delete used role for other areas
1107 // which have same category
1108 prv_role_list.erase(itr_role);
1115 crr_role_name = itr_areas->second["role"];
1117 HMI_DEBUG("wm", "Allocate surface for area:%s role:%s",
1118 crr_area_name.c_str(), crr_role_name.c_str());
1120 // Deactivate non-displayed role
1121 std::string prv_role_name;
1122 if (crr_layout_name == prv_layout_name) {
1123 HMI_DEBUG("wm", "Current layout is same with previous");
1125 // Deactivate previous role in same area
1126 // if it is different with current
1127 prv_role_name = prv_areas[crr_area_name]["role"];
1128 if (crr_role_name != prv_role_name) {
1129 this->deactivate(prv_role_name);
1133 HMI_DEBUG("wm", "Current layout is different with previous");
1135 if ("none" != prv_layout_name) {
1136 // Deactivate previous role in all area in previous layout
1137 // if it is different with current role
1138 for(auto itr = prv_areas.begin(); itr != prv_areas.end(); ++itr) {
1139 prv_role_name = itr->second["role"].c_str();
1140 if (crr_role_name != prv_role_name) {
1141 this->deactivate(prv_role_name);
1147 // Set surface for displayed role
1148 if ("none" != crr_layout_name) {
1149 // If current layout is not "none",
1150 // set surface for current role
1151 this->setSurfaceSize(crr_role_name.c_str(), crr_area_name.c_str());
1154 // This API is workaround.
1155 // Resource manager should manage each resource infomations
1156 // according to architecture document.
1157 this->lm_.updateArea(layer.c_str(), crr_role_name.c_str(), crr_area_name.c_str());
1165 void App::setSurfaceSize(const char* role, const char* area) {
1166 HMI_DEBUG("wm", "role:%s area:%s", role, area);
1168 // Get size of current area
1169 compositor::rect size = this->lm_.getAreaSize(area);
1171 // Set destination to the display rectangle
1172 int surface_id = this->role2surfaceid_[role];
1174 if (!this->controller->surface_exists(surface_id)) {
1175 // Block until all pending request are processed by wayland display server
1176 // because waiting for the surface of new app is created
1177 this->display->roundtrip();
1179 auto &s = this->controller->surfaces[surface_id];
1180 s->set_destination_rectangle(size.x, size.y, size.w, size.h);
1181 this->layout_commit();
1183 // Update area information
1184 this->area_info[surface_id].x = size.x;
1185 this->area_info[surface_id].y = size.y;
1186 this->area_info[surface_id].w = size.w;
1187 this->area_info[surface_id].h = size.h;
1188 HMI_DEBUG("wm", "Surface rect { %d, %d, %d, %d }",
1189 size.x, size.y, size.w, size.h);
1191 // Emit syncDraw event
1192 const char* drawing_name = this->role2drawingname_[role].c_str();
1193 this->emit_syncdraw(drawing_name, area,
1194 size.x, size.y, size.w, size.h);
1196 // Enqueue flushDraw event
1197 this->enqueue_flushdraw(surface_id);
1200 void App::setAccelPedalPos(double val) {
1201 this->crr_car_info_.accel_pedal_pos = val;
1204 extern const char* kDefaultAppDb;
1205 int App::loadAppDb() {
1206 HMI_DEBUG("wm", "Call");
1208 // Get afm application installed dir
1209 char const *afm_app_install_dir = getenv("AFM_APP_INSTALL_DIR");
1210 HMI_DEBUG("wm", "afm_app_install_dir:%s", afm_app_install_dir);
1212 std::string file_name;
1213 if (!afm_app_install_dir) {
1214 HMI_ERROR("wm", "AFM_APP_INSTALL_DIR is not defined");
1217 file_name = std::string(afm_app_install_dir) + std::string("/etc/app.db");
1221 json_object* json_obj;
1222 int ret = jh::inputJsonFilie(file_name.c_str(), &json_obj);
1224 HMI_ERROR("wm", "Could not open app.db, so use default role information");
1225 json_obj = json_tokener_parse(kDefaultAppDb);
1227 HMI_DEBUG("wm", "json_obj dump:%s", json_object_get_string(json_obj));
1230 HMI_DEBUG("wm", "Perse apps");
1231 json_object* json_cfg;
1232 if (!json_object_object_get_ex(json_obj, "apps", &json_cfg)) {
1233 HMI_ERROR("wm", "Parse Error!!");
1237 int len = json_object_array_length(json_cfg);
1238 HMI_DEBUG("wm", "json_cfg len:%d", len);
1239 HMI_DEBUG("wm", "json_cfg dump:%s", json_object_get_string(json_cfg));
1241 for (int i=0; i<len; i++) {
1242 json_object* json_tmp = json_object_array_get_idx(json_cfg, i);
1243 HMI_DEBUG("wm", "> json_tmp dump:%s", json_object_get_string(json_tmp));
1245 const char* app = jh::getStringFromJson(json_tmp, "name");
1246 if (nullptr == app) {
1247 HMI_ERROR("wm", "Parse Error!!");
1250 HMI_DEBUG("wm", "> app:%s", app);
1252 const char* role = jh::getStringFromJson(json_tmp, "role");
1253 if (nullptr == role) {
1254 HMI_ERROR("wm", "Parse Error!!");
1257 HMI_DEBUG("wm", "> role:%s", role);
1259 this->drawingname2role_[app] = std::string(role);
1263 for(auto itr = this->drawingname2role_.begin();
1264 itr != this->drawingname2role_.end(); ++itr) {
1265 HMI_DEBUG("wm", "app:%s role:%s",
1266 itr->first.c_str(), itr->second.c_str());
1269 // Release json_object
1270 json_object_put(json_obj);
1276 const char* kDefaultAppDb = "{ \
1279 \"name\": \"HomeScreen\", \
1280 \"role\": \"homescreen\" \
1283 \"name\": \"Music\", \
1284 \"role\": \"music\" \
1287 \"name\": \"MediaPlayer\", \
1288 \"role\": \"music\" \
1291 \"name\": \"Video\", \
1292 \"role\": \"video\" \
1295 \"name\": \"VideoPlayer\", \
1296 \"role\": \"video\" \
1299 \"name\": \"WebBrowser\", \
1300 \"role\": \"browser\" \
1303 \"name\": \"Radio\", \
1304 \"role\": \"radio\" \
1307 \"name\": \"Phone\", \
1308 \"role\": \"phone\" \
1311 \"name\": \"Navigation\", \
1315 \"name\": \"HVAC\", \
1316 \"role\": \"hvac\" \
1319 \"name\": \"Settings\", \
1320 \"role\": \"settings\" \
1323 \"name\": \"Dashboard\", \
1324 \"role\": \"dashboard\" \
1327 \"name\": \"POI\", \
1331 \"name\": \"Mixer\", \
1332 \"role\": \"mixer\" \