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);
100 App::App(wl::display *d)
108 pending_events(false),
112 auto l = load_layer_map(
113 this->config.get_string("layers.json").value().c_str());
115 this->layers = l.unwrap();
117 HMI_ERROR("wm", "%s", l.err().value());
120 } catch (std::exception &e) {
121 HMI_ERROR("wm", "Loading of configuration failed: %s", e.what());
124 // Initialize current car info
125 this->crr_car_info_.parking_brake_stt = true;
126 this->crr_car_info_.accel_pedal_stt = false;
127 this->crr_car_info_.accel_pedal_pos = 0;
128 this->crr_car_info_.car_stt = "stop";
129 this->crr_car_info_.headlamp_stt = false;
133 if (!this->display->ok()) {
137 if (this->layers.mapping.empty()) {
138 HMI_ERROR("wm", "No surface -> layer mapping loaded");
147 // Initialize PolicyManager
148 this->pm_.initialize();
150 // Initialize LayoutManager
151 this->lm_.initialize();
154 for (int i=Event_Val_Min; i<=Event_Val_Max; i++) {
155 map_afb_event[kListEventName[i]] = afb_daemon_make_event(kListEventName[i]);
158 this->display->add_global_handler(
159 "wl_output", [this](wl_registry *r, uint32_t name, uint32_t v) {
160 this->outputs.emplace_back(std::make_unique<wl::output>(r, name, v));
163 this->display->add_global_handler(
164 "ivi_wm", [this](wl_registry *r, uint32_t name, uint32_t v) {
166 std::make_unique<struct compositor::controller>(r, name, v);
168 // Init controller hooks
169 this->controller->chooks = &this->chooks;
171 // This protocol needs the output, so lets just add our mapping here...
172 this->controller->add_proxy_to_id_mapping(
173 this->outputs.back()->proxy.get(),
174 wl_proxy_get_id(reinterpret_cast<struct wl_proxy *>(
175 this->outputs.back()->proxy.get())));
178 this->controller->create_screen(this->outputs.back()->proxy.get());
180 // Set display to controller
181 this->controller->display = this->display;
184 // First level objects
185 this->display->roundtrip();
186 // Second level objects
187 this->display->roundtrip();
188 // Third level objects
189 this->display->roundtrip();
191 return init_layers();
194 int App::dispatch_pending_events() {
195 if (this->pop_pending_events()) {
196 this->display->dispatch_pending();
202 bool App::pop_pending_events() {
204 return this->pending_events.compare_exchange_strong(
205 x, false, std::memory_order_consume);
208 void App::set_pending_events() {
209 this->pending_events.store(true, std::memory_order_release);
212 optional<int> App::lookup_id(char const *name) {
213 return this->id_alloc.lookup(std::string(name));
215 optional<std::string> App::lookup_name(int id) {
216 return this->id_alloc.lookup(id);
222 int App::init_layers() {
223 if (!this->controller) {
224 HMI_ERROR("wm", "ivi_controller global not available");
228 if (this->outputs.empty()) {
229 HMI_ERROR("wm", "no output was set up!");
233 auto &c = this->controller;
235 auto &o = this->outputs.front();
236 auto &s = c->screens.begin()->second;
237 auto &layers = c->layers;
239 // Write output dimensions to ivi controller...
240 c->output_size = compositor::size{uint32_t(o->width), uint32_t(o->height)};
241 c->physical_size = compositor::size{uint32_t(o->physical_width),
242 uint32_t(o->physical_height)};
250 // Quick and dirty setup of layers
251 for (auto const &i : this->layers.mapping) {
252 c->layer_create(i.second.layer_id, o->width, o->height);
253 auto &l = layers[i.second.layer_id];
254 l->set_destination_rectangle(0, 0, o->width, o->height);
255 l->set_visibility(1);
256 HMI_DEBUG("wm", "Setting up layer %s (%d) for surface role match \"%s\"",
257 i.second.name.c_str(), i.second.layer_id, i.second.role.c_str());
260 // Add layers to screen
261 s->set_render_order(this->layers.layers);
263 this->layout_commit();
268 void App::layout_commit() {
269 this->controller->commit_changes();
270 this->display->flush();
273 const char* App::convertDrawingNameToRole(char const *drawing_name) {
276 if (this->drawingname2role_.find(drawing_name) != this->drawingname2role_.end()) {
277 // drawing_name is old role. So convert to new role.
278 role = this->drawingname2role_[drawing_name].c_str();
281 // drawing_name is new role.
284 HMI_DEBUG("wm", "drawing_name:%s -> role: %s", drawing_name, role);
289 void App::allocateWindowResource(char const *event, char const *drawing_name,
290 char const *drawing_area, const reply_func &reply) {
291 const char* new_role = nullptr;
292 const char* new_area = nullptr;
294 // Convert old role to new role
295 if ((nullptr != drawing_name) && (0 != strcmp("", drawing_name))) {
296 new_role = this->convertDrawingNameToRole(drawing_name);
299 if (0 == strcmp("activate", event)) {
301 // This process will be removed
302 // because the area "normal.full" and "normalfull" will be prohibited
304 if (0 == strcmp("restriction", new_role)) {
305 new_area = drawing_area;
308 if (nullptr == drawing_area) {
311 else if (0 == strcmp("normal.full", drawing_area)) {
314 else if (0 == strcmp("restriction.split.sub", drawing_area)) {
315 new_area = "restriction.split.sub";
317 else if (0 == strcmp("homescreen", new_role)) {
318 // Now homescreen specifies "normalfull"
325 HMI_DEBUG("wm", "drawing_area:%s, new_area: %s", drawing_area, new_area);
328 else if (0 == strcmp("deactivate", event)) {
336 // If event is "activate" and area is not specifid,
337 // get default value by using role
340 json_object* json_in = json_object_new_object();
341 json_object* json_out = json_object_new_object();
342 json_object_object_add(json_in, "event", json_object_new_string(event));
344 if (nullptr != new_role) {
345 json_object_object_add(json_in, "role", json_object_new_string(new_role));
347 if (nullptr != new_area) {
348 json_object_object_add(json_in, "area", json_object_new_string(new_area));
351 int ret = this->pm_.checkPolicy(json_in, &json_out);
353 reply("Error checkPolicy()");
357 HMI_DEBUG("wm", "result: %s", json_object_get_string(json_out));
360 // Release json_object
361 json_object_put(json_in);
363 // Check parking brake state
364 json_object* json_parking_brake;
365 if (!json_object_object_get_ex(json_out, "parking_brake", &json_parking_brake)) {
366 reply("Not found key \"parking_brake\"");
370 json_bool is_changed;
371 is_changed = jh::getBoolFromJson(json_parking_brake, "is_changed");
373 std::string parking_brake_state = jh::getStringFromJson(json_parking_brake, "state");
374 HMI_DEBUG("wm", "parking_brake_state: %s", parking_brake_state.c_str());
376 // Update state and emit event
377 if ("parking_brake_off" == parking_brake_state) {
378 this->crr_car_info_.parking_brake_stt = false;
379 #if 0 // FOR ALS: using lightstatus brake, so do not emit parking brake event
380 this->emitParkingBrakeOff();
383 else if ("parking_brake_on" == parking_brake_state) {
384 this->crr_car_info_.parking_brake_stt = true;
385 #if 0 // FOR ALS: using lightstatus brake, so do not emit parking brake event
386 this->emitParkingBrakeOn();
390 reply("Unknown parking brake state");
395 // Check accelerator pedal state
396 json_object* json_accel_pedal;
397 if (!json_object_object_get_ex(json_out, "accel_pedal", &json_accel_pedal)) {
398 reply("Not found key \"accel_pedal\"");
402 is_changed = jh::getBoolFromJson(json_accel_pedal, "is_changed");
404 std::string accel_pedal_state = jh::getStringFromJson(json_accel_pedal, "state");
405 HMI_DEBUG("wm", "accel_pedal_state: %s", accel_pedal_state.c_str());
408 if ("accel_pedal_off" == accel_pedal_state) {
409 this->crr_car_info_.accel_pedal_stt = false;
411 else if ("accel_pedal_on" == accel_pedal_state) {
412 this->crr_car_info_.accel_pedal_stt = true;
415 reply("Unknown accel pedal state");
420 // Check lightstatus brake state
421 json_object* json_lightstatus_brake;
422 if (!json_object_object_get_ex(json_out, "lightstatus_brake", &json_lightstatus_brake)) {
423 reply("Not found key \"lightstatus_brake\"");
427 is_changed = jh::getBoolFromJson(json_lightstatus_brake, "is_changed");
429 std::string lightstatus_brake_state = jh::getStringFromJson(json_lightstatus_brake, "state");
430 HMI_DEBUG("wm", "lightstatus_brake_state: %s", lightstatus_brake_state.c_str());
432 // Update state and emit event
433 if ("lightstatus_brake_off" == lightstatus_brake_state) {
434 this->crr_car_info_.lightstatus_brake_stt = false;
435 this->emitLightstatusBrakeOff();
437 else if ("lightstatus_brake_on" == lightstatus_brake_state) {
438 this->crr_car_info_.lightstatus_brake_stt = true;
439 this->emitLightstatusBrakeOn();
442 reply("Unknown lightstatus brake state");
448 json_object* json_car;
449 if (!json_object_object_get_ex(json_out, "car", &json_car)) {
450 reply("Not found key \"car\"");
454 is_changed = jh::getBoolFromJson(json_car, "is_changed");
456 std::string car_state = jh::getStringFromJson(json_car, "state");
457 HMI_DEBUG("wm", "car_state: %s", car_state.c_str());
460 if ("car_stop" == car_state) {
461 this->crr_car_info_.car_stt = "stop";
464 else if ("car_run" == car_state) {
465 this->crr_car_info_.car_stt = "run";
469 reply("Unknown car state");
475 json_object* json_lamp;
476 if (!json_object_object_get_ex(json_out, "lamp", &json_lamp)) {
477 reply("Not found key \"lamp\"");
481 is_changed = jh::getBoolFromJson(json_lamp, "is_changed");
483 std::string lamp_state = jh::getStringFromJson(json_lamp, "state");
484 HMI_DEBUG("wm", "lamp_state: %s", lamp_state.c_str());
486 // Update state and emit event
487 if ("lamp_off" == lamp_state) {
488 this->crr_car_info_.headlamp_stt = false;
489 this->emitHeadlampOff();
491 else if ("lamp_on" == lamp_state) {
492 this->crr_car_info_.headlamp_stt = true;
493 this->emitHeadlampOn();
496 reply("Unknown lamp state");
502 const char* category = nullptr;
503 std::string str_category;
504 if (nullptr != new_role) {
505 str_category = this->pm_.roleToCategory(new_role);
506 category = str_category.c_str();
507 HMI_DEBUG("wm", "role:%s category:%s", new_role, category);
511 if (this->lm_.updateLayout(json_out, new_role, category)) {
512 HMI_DEBUG("wm", "Layer is changed!!");
515 this->allocateSurface();
518 HMI_DEBUG("wm", "All layer is NOT changed!!");
521 // Release json_object
522 json_object_put(json_out);
527 void App::enqueue_flushdraw(int surface_id) {
528 this->check_flushdraw(surface_id);
529 HMI_DEBUG("wm", "Enqueuing EndDraw for surface_id %d", surface_id);
530 this->pending_end_draw.push_back(surface_id);
533 void App::check_flushdraw(int surface_id) {
534 auto i = std::find(std::begin(this->pending_end_draw),
535 std::end(this->pending_end_draw), surface_id);
536 if (i != std::end(this->pending_end_draw)) {
537 auto n = this->lookup_name(surface_id);
538 HMI_ERROR("wm", "Application %s (%d) has pending EndDraw call(s)!",
539 n ? n->c_str() : "unknown-name", surface_id);
540 std::swap(this->pending_end_draw[std::distance(
541 std::begin(this->pending_end_draw), i)],
542 this->pending_end_draw.back());
543 this->pending_end_draw.resize(this->pending_end_draw.size() - 1);
547 void App::api_enddraw(char const *appid, char const *drawing_name) {
548 for (unsigned i = 0, iend = this->pending_end_draw.size(); i < iend; i++) {
549 auto n = this->lookup_name(this->pending_end_draw[i]);
550 if (n && *n == drawing_name) {
551 std::swap(this->pending_end_draw[i], this->pending_end_draw[iend - 1]);
552 this->pending_end_draw.resize(iend - 1);
553 this->activate(this->pending_end_draw[i]);
554 this->emit_flushdraw(drawing_name);
555 this->emitScreenUpdated(appid);
560 void App::api_ping() { this->dispatch_pending_events(); }
562 void App::send_event(char const *evname){
563 HMI_DEBUG("wm", "%s: %s", __func__, evname);
565 int ret = afb_event_push(this->map_afb_event[evname], nullptr);
567 HMI_DEBUG("wm", "afb_event_push failed: %m");
571 void App::send_event(char const *evname, char const *label){
572 HMI_DEBUG("wm", "%s: %s(%s)", __func__, evname, label);
574 json_object *j = json_object_new_object();
575 json_object_object_add(j, kKeyDrawingName, json_object_new_string(label));
577 int ret = afb_event_push(this->map_afb_event[evname], j);
579 HMI_DEBUG("wm", "afb_event_push failed: %m");
583 void App::send_event(char const *evname, char const *label, char const *area,
584 int x, int y, int w, int h) {
585 HMI_DEBUG("wm", "%s: %s(%s, %s) x:%d y:%d w:%d h:%d",
586 __func__, evname, label, area, x, y, w, h);
588 json_object *j_rect = json_object_new_object();
589 json_object_object_add(j_rect, kKeyX, json_object_new_int(x));
590 json_object_object_add(j_rect, kKeyY, json_object_new_int(y));
591 json_object_object_add(j_rect, kKeyWidth, json_object_new_int(w));
592 json_object_object_add(j_rect, kKeyHeight, json_object_new_int(h));
594 json_object *j = json_object_new_object();
595 json_object_object_add(j, kKeyDrawingName, json_object_new_string(label));
596 json_object_object_add(j, kKeyDrawingArea, json_object_new_string(area));
597 json_object_object_add(j, kKeyDrawingRect, j_rect);
599 int ret = afb_event_push(this->map_afb_event[evname], j);
601 HMI_DEBUG("wm", "afb_event_push failed: %m");
608 void App::surface_created(uint32_t surface_id) {
609 auto layer_id = this->layers.get_layer_id(surface_id);
611 HMI_DEBUG("wm", "Newly created surfce %d is not associated with any layer!",
616 HMI_DEBUG("wm", "surface_id is %u, layer_id is %u", surface_id, *layer_id);
618 this->controller->layers[*layer_id]->add_surface(surface_id);
619 this->layout_commit();
620 // activate the main_surface right away
621 /*if (surface_id == static_cast<unsigned>(this->layers.main_surface)) {
622 HMI_DEBUG("wm", "Activating main_surface (%d)", surface_id);
624 this->api_activate_surface(
625 this->lookup_name(surface_id).value_or("unknown-name").c_str());
629 void App::surface_removed(uint32_t surface_id) {
630 HMI_DEBUG("wm", "surface_id is %u", surface_id);
632 // We cannot normally deactivate the main_surface, so be explicit
634 if (int(surface_id) == this->layers.main_surface) {
635 this->deactivate_main_surface();
637 auto drawing_name = this->lookup_name(surface_id);
639 this->allocateWindowResource("deactivate",
640 drawing_name->c_str(), nullptr,
645 this->id_alloc.remove_id(surface_id);
646 this->layers.remove_surface(surface_id);
649 void App::emit_activated(char const *label) {
650 this->send_event(kListEventName[Event_Active], label);
653 void App::emit_deactivated(char const *label) {
654 this->send_event(kListEventName[Event_Inactive], label);
657 void App::emit_syncdraw(char const *label, char const *area, int x, int y, int w, int h) {
658 this->send_event(kListEventName[Event_SyncDraw], label, area, x, y, w, h);
661 void App::emit_flushdraw(char const *label) {
662 this->send_event(kListEventName[Event_FlushDraw], label);
665 void App::emit_visible(char const *label, bool is_visible) {
666 this->send_event(is_visible ? kListEventName[Event_Visible] : kListEventName[Event_Invisible], label);
669 void App::emit_invisible(char const *label) {
670 return emit_visible(label, false);
673 void App::__send_event_temporary_extend(char const *evname, char const *appid) {
674 HMI_DEBUG("wm", "send event %s: %s", evname, appid);
676 json_object *j = json_object_new_object();
677 json_object_object_add(j, kKeyAppID, json_object_new_string(appid));
679 int ret = afb_event_push(this->map_afb_event[evname], j);
681 HMI_DEBUG("wm", "afb_event_push failed: %m");
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 void App::emitScreenUpdated(char const* appid) {
728 // Send ScreenUpdated event for applications which calls wmsubscribe
729 this->__send_event_temporary_extend(kListEventName[Event_ScreenUpdated], appid);
732 result<int> App::api_request_surface(char const *drawing_name) {
733 auto lid = this->layers.get_layer_id(std::string(drawing_name));
736 * register drawing_name as fallback and make it displayed.
738 lid = this->layers.get_layer_id(std::string("Fallback"));
739 HMI_DEBUG("wm", "%s is not registered in layers.json, then fallback as normal app", drawing_name);
741 return Err<int>("Drawing name does not match any role, Fallback is disabled");
745 auto rname = this->lookup_id(drawing_name);
747 // name does not exist yet, allocate surface id...
748 auto id = int(this->id_alloc.generate_id(drawing_name));
749 this->layers.add_surface(id, *lid);
751 // set the main_surface[_name] here and now
752 if (!this->layers.main_surface_name.empty() &&
753 this->layers.main_surface_name == drawing_name) {
754 this->layers.main_surface = id;
755 HMI_DEBUG("wm", "Set main_surface id to %u", id);
760 // This process will be implemented in SystemManager
763 auto id = int(this->app_id_alloc_.generate_id(drawing_name));
764 this->appname2appid_[drawing_name] = id;
768 // Set map of (role, surface_id)
769 const char* role = this->convertDrawingNameToRole(drawing_name);
770 this->role2surfaceid_[role] = id;
772 // Set map of (role, app)
773 // If the new app which has the same role which is had by existing app is requested,
774 // the role is given to the new app.
775 this->role2app_[role] = std::string(drawing_name);
780 // Check currently registered drawing names if it is already there.
781 return Err<int>("Surface already present");
784 char const *App::api_request_surface(char const *drawing_name,
785 char const *ivi_id) {
788 auto lid = this->layers.get_layer_id(std::string(drawing_name));
789 unsigned sid = std::stol(ivi_id);
793 * register drawing_name as fallback and make it displayed.
795 lid = this->layers.get_layer_id(std::string("Fallback"));
796 HMI_DEBUG("wm", "%s is not registered in layers.json, then fallback as normal app", drawing_name);
798 return "Drawing name does not match any role, Fallback is disabled";
802 auto rname = this->lookup_id(drawing_name);
805 return "Surface already present";
808 // register pair drawing_name and ivi_id
809 this->id_alloc.register_name_id(drawing_name, sid);
810 this->layers.add_surface(sid, *lid);
812 // this surface is already created
813 HMI_DEBUG("wm", "surface_id is %u, layer_id is %u", sid, *lid);
815 this->controller->layers[*lid]->add_surface(sid);
816 this->layout_commit();
821 result<json_object *> App::api_get_display_info() {
823 if (!this->controller) {
824 return Err<json_object *>("ivi_controller global not available");
828 compositor::size o_size = this->controller->output_size;
829 compositor::size p_size = this->controller->physical_size;
831 json_object *object = json_object_new_object();
832 json_object_object_add(object, kKeyWidthPixel, json_object_new_int(o_size.w));
833 json_object_object_add(object, kKeyHeightPixel, json_object_new_int(o_size.h));
834 json_object_object_add(object, kKeyWidthMm, json_object_new_int(p_size.w));
835 json_object_object_add(object, kKeyHeightMm, json_object_new_int(p_size.h));
837 return Ok<json_object *>(object);
840 result<json_object *> App::api_get_area_info(char const *drawing_name) {
841 HMI_DEBUG("wm", "called");
843 // Check drawing name, surface/layer id
844 auto const &surface_id = this->lookup_id(drawing_name);
846 return Err<json_object *>("Surface does not exist");
849 if (!this->controller->surface_exists(*surface_id)) {
850 return Err<json_object *>("Surface does not exist in controller!");
853 auto layer_id = this->layers.get_layer_id(*surface_id);
855 return Err<json_object *>("Surface is not on any layer!");
858 auto o_state = *this->layers.get_layout_state(*surface_id);
859 if (o_state == nullptr) {
860 return Err<json_object *>("Could not find layer for surface");
863 // Set area rectangle
864 compositor::rect area_info = this->area_info[*surface_id];
865 json_object *object = json_object_new_object();
866 json_object_object_add(object, kKeyX, json_object_new_int(area_info.x));
867 json_object_object_add(object, kKeyY, json_object_new_int(area_info.y));
868 json_object_object_add(object, kKeyWidth, json_object_new_int(area_info.w));
869 json_object_object_add(object, kKeyHeight, json_object_new_int(area_info.h));
871 return Ok<json_object *>(object);
874 result<json_object *> App::api_get_car_info(char const *label) {
875 HMI_DEBUG("wm", "called");
877 json_object *j_in = nullptr;
878 json_object *j_out = nullptr;
880 if (0 == strcmp("parking_brake_status", label)) {
881 // Get parking brake status
882 json_bool val = this->crr_car_info_.parking_brake_stt;
883 j_in = json_object_new_boolean(val);
885 else if (0 == strcmp("accelerator.pedal.position", label)) {
886 // Get accelerator pedal position
887 double val = this->crr_car_info_.accel_pedal_pos;
888 j_in = json_object_new_double(val);
890 else if (0 == strcmp("car_state", label)) {
892 const char* val = this->crr_car_info_.car_stt;
893 j_in = json_object_new_string(val);
895 else if (0 == strcmp("lightstatus.brake", label)) {
896 // Get lightstatus brake status
897 json_bool val = this->crr_car_info_.lightstatus_brake_stt;
898 j_in = json_object_new_boolean(val);
901 return Err<json_object *>("Car info does not exist");
904 // Create output object
905 j_out = json_object_new_object();
906 json_object_object_add(j_out, "value", j_in);
908 return Ok<json_object *>(j_out);
911 void App::activate(int id) {
912 auto ip = this->controller->sprops.find(id);
913 if (ip != this->controller->sprops.end()) {
914 this->controller->surfaces[id]->set_visibility(1);
916 this->lookup_name(id).value_or("unknown-name").c_str();
919 if ((0 == strcmp(label, "Radio"))
920 || (0 == strcmp(label, "MediaPlayer"))
921 || (0 == strcmp(label, "Music"))
922 || (0 == strcmp(label, "VideoPlayer"))
923 || (0 == strcmp(label, "Video"))
924 || (0 == strcmp(label, "Navigation"))) {
925 for (auto i = surface_bg.begin(); i != surface_bg.end(); ++i) {
928 this->surface_bg.erase(i);
930 // Remove from BG layer (999)
931 HMI_DEBUG("wm", "Remove %s(%d) from BG layer", label, id);
932 this->controller->layers[999]->remove_surface(id);
934 // Add to FG layer (1001)
935 HMI_DEBUG("wm", "Add %s(%d) to FG layer", label, id);
936 this->controller->layers[1001]->add_surface(id);
938 for (int j : this->surface_bg) {
939 HMI_DEBUG("wm", "Stored id:%d", j);
946 this->layout_commit();
948 this->emit_visible(label);
949 this->emit_activated(label);
953 void App::deactivate(int id) {
954 auto ip = this->controller->sprops.find(id);
955 if (ip != this->controller->sprops.end()) {
957 this->lookup_name(id).value_or("unknown-name").c_str();
960 if ((0 == strcmp(label, "Radio"))
961 || (0 == strcmp(label, "MediaPlayer"))
962 || (0 == strcmp(label, "Music"))
963 || (0 == strcmp(label, "VideoPlayer"))
964 || (0 == strcmp(label, "Video"))
965 || (0 == strcmp(label, "Navigation"))) {
968 this->surface_bg.push_back(id);
970 // Remove from FG layer (1001)
971 HMI_DEBUG("wm", "Remove %s(%d) from FG layer", label, id);
972 this->controller->layers[1001]->remove_surface(id);
974 // Add to BG layer (999)
975 HMI_DEBUG("wm", "Add %s(%d) to BG layer", label, id);
976 this->controller->layers[999]->add_surface(id);
978 for (int j : surface_bg) {
979 HMI_DEBUG("wm", "Stored id:%d", j);
983 this->controller->surfaces[id]->set_visibility(0);
987 this->layout_commit();
989 this->emit_deactivated(label);
990 this->emit_invisible(label);
994 void App::deactivate(std::string role) {
995 std::string app = this->role2app_[role];
996 auto const &id = this->lookup_id(app.c_str());
998 HMI_ERROR("wm", "Surface does not exist");
1001 HMI_DEBUG("wm", "Deactivate role:%s (app:%s)",
1002 role.c_str(), app.c_str());
1004 this->deactivate(*id);
1007 void App::deactivate_main_surface() {
1008 this->layers.main_surface = -1;
1009 this->allocateWindowResource("deactivate",
1010 this->layers.main_surface_name.c_str(), nullptr,
1017 void controller_hooks::surface_created(uint32_t surface_id) {
1018 this->app->surface_created(surface_id);
1021 void controller_hooks::surface_removed(uint32_t surface_id) {
1022 this->app->surface_removed(surface_id);
1025 void controller_hooks::surface_visibility(uint32_t /*surface_id*/,
1028 void controller_hooks::surface_destination_rectangle(uint32_t /*surface_id*/,
1034 int App::allocateSurface() {
1035 HMI_DEBUG("wm", "Call");
1037 // Get current/previous layers
1038 LayoutManager::TypeLayers crr_layers = this->lm_.getCurrentLayers();
1039 LayoutManager::TypeLayers prv_layers = this->lm_.getPreviousLayers();
1041 // Update resource of all layers
1042 for (auto itr_layers = crr_layers.begin();
1043 itr_layers != crr_layers.end(); ++itr_layers) {
1045 std::string layer = itr_layers->first;
1046 HMI_DEBUG("wm", "Try to update resource in %s layer", layer.c_str());
1048 // If layout is changed, update resouce
1049 if (this->lm_.isLayoutChanged(layer.c_str())) {
1050 // Get current/previous layout
1051 LayoutManager::TypeLayouts crr_layout = itr_layers->second;
1052 LayoutManager::TypeLayouts prv_layout = prv_layers[layer];
1054 // Get current/previous layout name
1055 std::string crr_layout_name = crr_layout.begin()->first;
1056 std::string prv_layout_name = prv_layout.begin()->first;
1057 HMI_DEBUG("wm", "layout name crr:%s prv:%s",
1058 crr_layout_name.c_str(), prv_layout_name.c_str());
1060 // Get current/previous ares
1061 LayoutManager::TypeAreas crr_areas = crr_layout[crr_layout_name];
1062 LayoutManager::TypeAreas prv_areas = prv_layout[prv_layout_name];
1064 // Create previous displayed role list
1065 std::string prv_area_name;
1066 std::vector<std::string> prv_role_list;
1067 for (auto itr_areas = prv_areas.begin();
1068 itr_areas != prv_areas.end(); ++itr_areas) {
1069 prv_area_name = itr_areas->first;
1070 prv_role_list.push_back(prv_areas[prv_area_name]["role"]);
1071 HMI_DEBUG("wm", "previous displayed role:%s",
1072 prv_areas[prv_area_name]["role"].c_str());
1075 // Allocate surface for each area
1076 std::string crr_area_name;
1077 std::string crr_role_name;
1078 LayoutManager::TypeRolCtg crr_rol_ctg;
1079 for (auto itr_areas = crr_areas.begin();
1080 itr_areas != crr_areas.end(); ++itr_areas) {
1081 crr_area_name = itr_areas->first;
1082 crr_rol_ctg = itr_areas->second;
1084 // Get role of current area
1085 if ("category" == crr_rol_ctg.begin()->first) {
1086 // If current area have category
1087 // Get category name
1088 std::string crr_ctg = crr_rol_ctg.begin()->second;
1090 // Serch relevant role from previous displayed role list
1091 for (auto itr_role = prv_role_list.begin();
1092 itr_role != prv_role_list.end(); ++itr_role) {
1093 std::string prv_ctg = this->pm_.roleToCategory((*itr_role).c_str());
1094 if (crr_ctg == prv_ctg) {
1095 // First discovered role is set to current role
1096 crr_role_name = *itr_role;
1098 // Delete used role for other areas
1099 // which have same category
1100 prv_role_list.erase(itr_role);
1107 crr_role_name = itr_areas->second["role"];
1109 HMI_DEBUG("wm", "Allocate surface for area:%s role:%s",
1110 crr_area_name.c_str(), crr_role_name.c_str());
1112 // Deactivate non-displayed role
1113 std::string prv_role_name;
1114 if (crr_layout_name == prv_layout_name) {
1115 HMI_DEBUG("wm", "Current layout is same with previous");
1117 // Deactivate previous role in same area
1118 // if it is different with current
1119 prv_role_name = prv_areas[crr_area_name]["role"];
1120 if (crr_role_name != prv_role_name) {
1121 this->deactivate(prv_role_name);
1125 HMI_DEBUG("wm", "Current layout is different with previous");
1127 if ("none" != prv_layout_name) {
1128 // Deactivate previous role in all area in previous layout
1129 // if it is different with current role
1130 for(auto itr = prv_areas.begin(); itr != prv_areas.end(); ++itr) {
1131 prv_role_name = itr->second["role"].c_str();
1132 if (crr_role_name != prv_role_name) {
1133 this->deactivate(prv_role_name);
1139 // Set surface for displayed role
1140 if ("none" != crr_layout_name) {
1141 // If current layout is not "none",
1142 // set surface for current role
1143 this->setSurfaceSize(crr_role_name.c_str(), crr_area_name.c_str());
1146 // This API is workaround.
1147 // Resource manager should manage each resource infomations
1148 // according to architecture document.
1149 this->lm_.updateArea(layer.c_str(), crr_role_name.c_str(), crr_area_name.c_str());
1157 void App::setSurfaceSize(const char* role, const char* area) {
1158 HMI_DEBUG("wm", "role:%s area:%s", role, area);
1160 // Get size of current area
1161 compositor::rect size = this->lm_.getAreaSize(area);
1163 // Set destination to the display rectangle
1164 int surface_id = this->role2surfaceid_[role];
1166 if (!this->controller->surface_exists(surface_id)) {
1167 // Block until all pending request are processed by wayland display server
1168 // because waiting for the surface of new app is created
1169 this->display->roundtrip();
1171 auto &s = this->controller->surfaces[surface_id];
1172 s->set_destination_rectangle(size.x, size.y, size.w, size.h);
1173 this->layout_commit();
1175 // Update area information
1176 this->area_info[surface_id].x = size.x;
1177 this->area_info[surface_id].y = size.y;
1178 this->area_info[surface_id].w = size.w;
1179 this->area_info[surface_id].h = size.h;
1180 HMI_DEBUG("wm", "Surface rect { %d, %d, %d, %d }",
1181 size.x, size.y, size.w, size.h);
1183 // Emit syncDraw event
1184 const char* app = this->role2app_[role].c_str();
1185 this->emit_syncdraw(app, area,
1186 size.x, size.y, size.w, size.h);
1188 // Enqueue flushDraw event
1189 this->enqueue_flushdraw(surface_id);
1192 void App::setAccelPedalPos(double val) {
1193 this->crr_car_info_.accel_pedal_pos = val;
1196 extern const char* kDefaultAppDb;
1197 int App::loadAppDb() {
1198 HMI_DEBUG("wm", "Call");
1200 // Get afm application installed dir
1201 char const *afm_app_install_dir = getenv("AFM_APP_INSTALL_DIR");
1202 HMI_DEBUG("wm", "afm_app_install_dir:%s", afm_app_install_dir);
1204 std::string file_name;
1205 if (!afm_app_install_dir) {
1206 HMI_ERROR("wm", "AFM_APP_INSTALL_DIR is not defined");
1209 file_name = std::string(afm_app_install_dir) + std::string("/etc/app.db");
1213 json_object* json_obj;
1214 int ret = jh::inputJsonFilie(file_name.c_str(), &json_obj);
1216 HMI_ERROR("wm", "Could not open app.db, so use default role information");
1217 json_obj = json_tokener_parse(kDefaultAppDb);
1219 HMI_DEBUG("wm", "json_obj dump:%s", json_object_get_string(json_obj));
1222 HMI_DEBUG("wm", "Perse apps");
1223 json_object* json_cfg;
1224 if (!json_object_object_get_ex(json_obj, "apps", &json_cfg)) {
1225 HMI_ERROR("wm", "Parse Error!!");
1229 int len = json_object_array_length(json_cfg);
1230 HMI_DEBUG("wm", "json_cfg len:%d", len);
1231 HMI_DEBUG("wm", "json_cfg dump:%s", json_object_get_string(json_cfg));
1233 for (int i=0; i<len; i++) {
1234 json_object* json_tmp = json_object_array_get_idx(json_cfg, i);
1235 HMI_DEBUG("wm", "> json_tmp dump:%s", json_object_get_string(json_tmp));
1237 const char* app = jh::getStringFromJson(json_tmp, "name");
1238 if (nullptr == app) {
1239 HMI_ERROR("wm", "Parse Error!!");
1242 HMI_DEBUG("wm", "> app:%s", app);
1244 const char* role = jh::getStringFromJson(json_tmp, "role");
1245 if (nullptr == role) {
1246 HMI_ERROR("wm", "Parse Error!!");
1249 HMI_DEBUG("wm", "> role:%s", role);
1251 this->drawingname2role_[app] = std::string(role);
1255 for(auto itr = this->drawingname2role_.begin();
1256 itr != this->drawingname2role_.end(); ++itr) {
1257 HMI_DEBUG("wm", "app:%s role:%s",
1258 itr->first.c_str(), itr->second.c_str());
1261 // Release json_object
1262 json_object_put(json_obj);
1268 const char* kDefaultAppDb = "{ \
1271 \"name\": \"HomeScreen\", \
1272 \"role\": \"homescreen\" \
1275 \"name\": \"Music\", \
1276 \"role\": \"music\" \
1279 \"name\": \"MediaPlayer\", \
1280 \"role\": \"music\" \
1283 \"name\": \"Video\", \
1284 \"role\": \"video\" \
1287 \"name\": \"VideoPlayer\", \
1288 \"role\": \"video\" \
1291 \"name\": \"WebBrowser\", \
1292 \"role\": \"browser\" \
1295 \"name\": \"Radio\", \
1296 \"role\": \"radio\" \
1299 \"name\": \"Phone\", \
1300 \"role\": \"phone\" \
1303 \"name\": \"Navigation\", \
1307 \"name\": \"HVAC\", \
1308 \"role\": \"hvac\" \
1311 \"name\": \"Settings\", \
1312 \"role\": \"settings\" \
1315 \"name\": \"Dashboard\", \
1316 \"role\": \"dashboard\" \
1319 \"name\": \"POI\", \
1323 \"name\": \"Mixer\", \
1324 \"role\": \"mixer\" \