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 static void eventHandler(json_object* json_out) {
99 context->updateWindowResource(json_out);
103 void App::updateWindowResource(json_object* json_out) {
104 // Check parking brake state
105 json_object* json_parking_brake;
106 if (!json_object_object_get_ex(json_out, "parking_brake", &json_parking_brake)) {
107 HMI_DEBUG("wm", "Not found key \"parking_brake\"");
111 json_bool is_changed;
112 is_changed = jh::getBoolFromJson(json_parking_brake, "is_changed");
114 std::string parking_brake_state = jh::getStringFromJson(json_parking_brake, "state");
115 HMI_DEBUG("wm", "parking_brake_state: %s", parking_brake_state.c_str());
117 // Update state and emit event
118 if ("parking_brake_off" == parking_brake_state) {
119 this->crr_car_info_.parking_brake_stt = false;
120 #if 0 // FOR ALS: using lightstatus brake, so do not emit parking brake event
121 this->emitParkingBrakeOff();
124 else if ("parking_brake_on" == parking_brake_state) {
125 this->crr_car_info_.parking_brake_stt = true;
126 #if 0 // FOR ALS: using lightstatus brake, so do not emit parking brake event
127 this->emitParkingBrakeOn();
131 HMI_DEBUG("wm", "Unknown parking brake state");
136 // Check accelerator pedal state
137 json_object* json_accel_pedal;
138 if (!json_object_object_get_ex(json_out, "accel_pedal", &json_accel_pedal)) {
139 HMI_DEBUG("wm", "Not found key \"accel_pedal\"");
143 is_changed = jh::getBoolFromJson(json_accel_pedal, "is_changed");
145 std::string accel_pedal_state = jh::getStringFromJson(json_accel_pedal, "state");
146 HMI_DEBUG("wm", "accel_pedal_state: %s", accel_pedal_state.c_str());
149 if ("accel_pedal_off" == accel_pedal_state) {
150 this->crr_car_info_.accel_pedal_stt = false;
152 else if ("accel_pedal_on" == accel_pedal_state) {
153 this->crr_car_info_.accel_pedal_stt = true;
156 HMI_DEBUG("wm", "Unknown accel pedal state");
161 // Check lightstatus brake state
162 json_object* json_lightstatus_brake;
163 if (!json_object_object_get_ex(json_out, "lightstatus_brake", &json_lightstatus_brake)) {
164 HMI_DEBUG("wm", "Not found key \"lightstatus_brake\"");
168 is_changed = jh::getBoolFromJson(json_lightstatus_brake, "is_changed");
170 std::string lightstatus_brake_state = jh::getStringFromJson(json_lightstatus_brake, "state");
171 HMI_DEBUG("wm", "lightstatus_brake_state: %s", lightstatus_brake_state.c_str());
173 // Update state and emit event
174 if ("lightstatus_brake_off" == lightstatus_brake_state) {
175 this->crr_car_info_.lightstatus_brake_stt = false;
176 this->emitLightstatusBrakeOff();
178 else if ("lightstatus_brake_on" == lightstatus_brake_state) {
179 this->crr_car_info_.lightstatus_brake_stt = true;
180 this->emitLightstatusBrakeOn();
183 HMI_DEBUG("wm", "Unknown lightstatus brake state");
189 json_object* json_car;
190 if (!json_object_object_get_ex(json_out, "car", &json_car)) {
191 HMI_DEBUG("wm", "Not found key \"car\"");
195 is_changed = jh::getBoolFromJson(json_car, "is_changed");
197 std::string car_state = jh::getStringFromJson(json_car, "state");
198 HMI_DEBUG("wm", "car_state: %s", car_state.c_str());
201 if ("car_stop" == car_state) {
202 this->crr_car_info_.car_stt = "stop";
205 else if ("car_run" == car_state) {
206 this->crr_car_info_.car_stt = "run";
210 HMI_DEBUG("wm", "Unknown car state");
216 json_object* json_lamp;
217 if (!json_object_object_get_ex(json_out, "lamp", &json_lamp)) {
218 HMI_DEBUG("wm", "Not found key \"lamp\"");
222 is_changed = jh::getBoolFromJson(json_lamp, "is_changed");
224 std::string lamp_state = jh::getStringFromJson(json_lamp, "state");
225 HMI_DEBUG("wm", "lamp_state: %s", lamp_state.c_str());
227 // Update state and emit event
228 if ("lamp_off" == lamp_state) {
229 this->crr_car_info_.headlamp_stt = false;
230 this->emitHeadlampOff();
232 else if ("lamp_on" == lamp_state) {
233 this->crr_car_info_.headlamp_stt = true;
234 this->emitHeadlampOn();
237 HMI_DEBUG("wm", "Unknown lamp state");
242 // Get displayed roles from current layout
243 json_object* json_layers;
244 if (!json_object_object_get_ex(json_out, "layers", &json_layers)) {
245 HMI_DEBUG("wm", "Not found key \"layers\"");
249 int len = json_object_array_length(json_layers);
250 HMI_DEBUG("wm", "json_layers len:%d", len);
251 HMI_DEBUG("wm", "json_layers dump:%s", json_object_get_string(json_layers));
253 for (int i=0; i<len; i++) {
254 json_object* json_tmp = json_object_array_get_idx(json_layers, i);
256 std::string layer_name = jh::getStringFromJson(json_tmp, "name");
257 json_bool changed = jh::getBoolFromJson(json_tmp, "changed");
258 HMI_DEBUG("wm", "layer:%s changed:%d", layer_name.c_str(), changed);
261 json_object* json_areas;
262 if (!json_object_object_get_ex(json_tmp, "areas", &json_areas)) {
263 HMI_DEBUG("wm", "Not found key \"areas\"");
267 int len = json_object_array_length(json_areas);
268 HMI_DEBUG("wm", "json_layers len:%d", len);
271 RoleState crr_roles = this->crr_layer_state[layer_name];
272 for (int j=0; j<len; j++) {
273 json_object* json_tmp2 = json_object_array_get_idx(json_areas, j);
275 std::string area_name = jh::getStringFromJson(json_tmp2, "name");
276 std::string role_name = jh::getStringFromJson(json_tmp2, "role");
278 nxt_roles[role_name] = area_name;
280 auto i_crr = crr_roles.find(role_name);
281 HMI_DEBUG("wm", "next role:%s area:%s",
282 role_name.c_str(), area_name.c_str());
284 // If next role does not exist in current
285 if (crr_roles.end() == i_crr) {
286 HMI_DEBUG("wm", "next role does not exist in current");
289 this->setSurfaceSize(role_name.c_str(), area_name.c_str());
292 HMI_DEBUG("wm", "current role:%s area:%s",
293 i_crr->first.c_str(), i_crr->second.c_str());
295 // If next role exists in current and area is different with current
296 if (area_name != i_crr->second) {
297 HMI_DEBUG("wm", "next role exists in current and area is different with current");
300 this->setSurfaceSize(role_name.c_str(), area_name.c_str());
303 // Remove role which exist in next list from current list
304 crr_roles.erase(i_crr);
308 // Deactivate roles which remains in current list
309 // because these are not displayed in next layout
310 for (auto i_crr : crr_roles) {
311 HMI_DEBUG("wm", "Deactivate role:%s", i_crr.first.c_str());
314 this->deactivate(i_crr.first.c_str());
317 // Update current role list
318 this->crr_layer_state[layer_name] = nxt_roles;
327 App::App(wl::display *d)
335 pending_events(false),
339 auto l = load_layer_map(
340 this->config.get_string("layers.json").value().c_str());
342 this->layers = l.unwrap();
344 HMI_ERROR("wm", "%s", l.err().value());
347 } catch (std::exception &e) {
348 HMI_ERROR("wm", "Loading of configuration failed: %s", e.what());
351 // Initialize current car info
352 this->crr_car_info_.parking_brake_stt = true;
353 this->crr_car_info_.accel_pedal_stt = false;
354 this->crr_car_info_.accel_pedal_pos = 0;
355 this->crr_car_info_.car_stt = "stop";
356 this->crr_car_info_.headlamp_stt = false;
360 if (!this->display->ok()) {
364 if (this->layers.mapping.empty()) {
365 HMI_ERROR("wm", "No surface -> layer mapping loaded");
369 // Store my context for calling callback for PolicyManager
377 // Initialize PolicyManager
378 this->pm_.initialize();
380 // Register callback to PolicyManager
381 PolicyManager::CallbackTable callback;
382 callback.onStateTransitioned = rm::eventHandler;
383 callback.onError = nullptr;
384 this->pm_.registerCallback(callback);
386 // Initialize LayoutManager
387 this->lm_.initialize();
390 for (int i=Event_Val_Min; i<=Event_Val_Max; i++) {
391 map_afb_event[kListEventName[i]] = afb_daemon_make_event(kListEventName[i]);
394 this->display->add_global_handler(
395 "wl_output", [this](wl_registry *r, uint32_t name, uint32_t v) {
396 this->outputs.emplace_back(std::make_unique<wl::output>(r, name, v));
399 this->display->add_global_handler(
400 "ivi_wm", [this](wl_registry *r, uint32_t name, uint32_t v) {
402 std::make_unique<struct compositor::controller>(r, name, v);
404 // Init controller hooks
405 this->controller->chooks = &this->chooks;
407 // This protocol needs the output, so lets just add our mapping here...
408 this->controller->add_proxy_to_id_mapping(
409 this->outputs.back()->proxy.get(),
410 wl_proxy_get_id(reinterpret_cast<struct wl_proxy *>(
411 this->outputs.back()->proxy.get())));
414 this->controller->create_screen(this->outputs.back()->proxy.get());
416 // Set display to controller
417 this->controller->display = this->display;
420 // First level objects
421 this->display->roundtrip();
422 // Second level objects
423 this->display->roundtrip();
424 // Third level objects
425 this->display->roundtrip();
427 return init_layers();
430 int App::dispatch_pending_events() {
431 if (this->pop_pending_events()) {
432 this->display->dispatch_pending();
438 bool App::pop_pending_events() {
440 return this->pending_events.compare_exchange_strong(
441 x, false, std::memory_order_consume);
444 void App::set_pending_events() {
445 this->pending_events.store(true, std::memory_order_release);
448 optional<int> App::lookup_id(char const *name) {
449 return this->id_alloc.lookup(std::string(name));
451 optional<std::string> App::lookup_name(int id) {
452 return this->id_alloc.lookup(id);
458 int App::init_layers() {
459 if (!this->controller) {
460 HMI_ERROR("wm", "ivi_controller global not available");
464 if (this->outputs.empty()) {
465 HMI_ERROR("wm", "no output was set up!");
469 auto &c = this->controller;
471 auto &o = this->outputs.front();
472 auto &s = c->screens.begin()->second;
473 auto &layers = c->layers;
475 // Write output dimensions to ivi controller...
476 c->output_size = compositor::size{uint32_t(o->width), uint32_t(o->height)};
477 c->physical_size = compositor::size{uint32_t(o->physical_width),
478 uint32_t(o->physical_height)};
486 // Quick and dirty setup of layers
487 for (auto const &i : this->layers.mapping) {
488 c->layer_create(i.second.layer_id, o->width, o->height);
489 auto &l = layers[i.second.layer_id];
490 l->set_destination_rectangle(0, 0, o->width, o->height);
491 l->set_visibility(1);
492 HMI_DEBUG("wm", "Setting up layer %s (%d) for surface role match \"%s\"",
493 i.second.name.c_str(), i.second.layer_id, i.second.role.c_str());
496 // Add layers to screen
497 s->set_render_order(this->layers.layers);
499 this->layout_commit();
504 void App::layout_commit() {
505 this->controller->commit_changes();
506 this->display->flush();
509 const char* App::convertDrawingNameToRole(char const *drawing_name) {
512 if (this->drawingname2role_.find(drawing_name) != this->drawingname2role_.end()) {
513 // drawing_name is old role. So convert to new role.
514 role = this->drawingname2role_[drawing_name].c_str();
517 // drawing_name is new role.
520 HMI_DEBUG("wm", "drawing_name:%s -> role: %s", drawing_name, role);
525 void App::allocateWindowResource(char const *event, char const *drawing_name,
526 char const *drawing_area, const reply_func &reply) {
527 const char* new_role = nullptr;
528 const char* new_area = nullptr;
530 // Convert old role to new role
531 if ((nullptr != drawing_name) && (0 != strcmp("", drawing_name))) {
532 new_role = this->convertDrawingNameToRole(drawing_name);
535 if (0 == strcmp("activate", event)) {
537 // This process will be removed
538 // because the area "normal.full" and "normalfull" will be prohibited
540 if (0 == strcmp("restriction", new_role)) {
541 new_area = drawing_area;
544 if (nullptr == drawing_area) {
547 else if (0 == strcmp("normal.full", drawing_area)) {
550 else if (0 == strcmp("restriction.split.sub", drawing_area)) {
551 new_area = "restriction.split.sub";
553 else if (0 == strcmp("homescreen", new_role)) {
554 // Now homescreen specifies "normalfull"
555 new_area = "fullscreen";
561 HMI_DEBUG("wm", "drawing_area:%s, new_area: %s", drawing_area, new_area);
564 else if (0 == strcmp("deactivate", event)) {
572 // If event is "activate" and area is not specifid,
573 // get default value by using role
575 // Input event to PolicyManager
576 json_object* json_in = json_object_new_object();
577 json_object_object_add(json_in, "event", json_object_new_string(event));
578 if (nullptr != new_role) {
579 json_object_object_add(json_in, "role", json_object_new_string(new_role));
581 if (nullptr != new_area) {
582 json_object_object_add(json_in, "area", json_object_new_string(new_area));
585 this->pm_.inputEvent(json_in);
587 // Release json_object
588 json_object_put(json_in);
593 void App::enqueue_flushdraw(int surface_id) {
594 this->check_flushdraw(surface_id);
595 HMI_DEBUG("wm", "Enqueuing EndDraw for surface_id %d", surface_id);
596 this->pending_end_draw.push_back(surface_id);
599 void App::check_flushdraw(int surface_id) {
600 auto i = std::find(std::begin(this->pending_end_draw),
601 std::end(this->pending_end_draw), surface_id);
602 if (i != std::end(this->pending_end_draw)) {
603 auto n = this->lookup_name(surface_id);
604 HMI_ERROR("wm", "Application %s (%d) has pending EndDraw call(s)!",
605 n ? n->c_str() : "unknown-name", surface_id);
606 std::swap(this->pending_end_draw[std::distance(
607 std::begin(this->pending_end_draw), i)],
608 this->pending_end_draw.back());
609 this->pending_end_draw.resize(this->pending_end_draw.size() - 1);
613 void App::api_enddraw(char const *drawing_name) {
614 // Convert drawing_name to role
615 const char* role = this->convertDrawingNameToRole(drawing_name);
617 for (unsigned i = 0, iend = this->pending_end_draw.size(); i < iend; i++) {
618 auto n = this->lookup_name(this->pending_end_draw[i]);
619 if (n && *n == role) {
620 std::swap(this->pending_end_draw[i], this->pending_end_draw[iend - 1]);
621 this->pending_end_draw.resize(iend - 1);
622 this->activate(this->pending_end_draw[i]);
623 this->emit_flushdraw(drawing_name);
628 void App::api_ping() { this->dispatch_pending_events(); }
630 void App::send_event(char const *evname){
631 HMI_DEBUG("wm", "%s: %s", __func__, evname);
633 int ret = afb_event_push(this->map_afb_event[evname], nullptr);
635 HMI_DEBUG("wm", "afb_event_push failed: %m");
639 void App::send_event(char const *evname, char const *label){
640 HMI_DEBUG("wm", "%s: %s(%s)", __func__, evname, label);
642 json_object *j = json_object_new_object();
643 json_object_object_add(j, kKeyDrawingName, json_object_new_string(label));
645 int ret = afb_event_push(this->map_afb_event[evname], j);
647 HMI_DEBUG("wm", "afb_event_push failed: %m");
651 void App::send_event(char const *evname, char const *label, char const *area,
652 int x, int y, int w, int h) {
653 HMI_DEBUG("wm", "%s: %s(%s, %s) x:%d y:%d w:%d h:%d",
654 __func__, evname, label, area, x, y, w, h);
656 json_object *j_rect = json_object_new_object();
657 json_object_object_add(j_rect, kKeyX, json_object_new_int(x));
658 json_object_object_add(j_rect, kKeyY, json_object_new_int(y));
659 json_object_object_add(j_rect, kKeyWidth, json_object_new_int(w));
660 json_object_object_add(j_rect, kKeyHeight, json_object_new_int(h));
662 json_object *j = json_object_new_object();
663 json_object_object_add(j, kKeyDrawingName, json_object_new_string(label));
664 json_object_object_add(j, kKeyDrawingArea, json_object_new_string(area));
665 json_object_object_add(j, kKeyDrawingRect, j_rect);
667 int ret = afb_event_push(this->map_afb_event[evname], j);
669 HMI_DEBUG("wm", "afb_event_push failed: %m");
676 void App::surface_created(uint32_t surface_id) {
677 auto layer_id = this->layers.get_layer_id(surface_id);
679 HMI_DEBUG("wm", "Newly created surfce %d is not associated with any layer!",
684 HMI_DEBUG("wm", "surface_id is %u, layer_id is %u", surface_id, *layer_id);
686 this->controller->layers[*layer_id]->add_surface(surface_id);
687 this->layout_commit();
688 // activate the main_surface right away
689 /*if (surface_id == static_cast<unsigned>(this->layers.main_surface)) {
690 HMI_DEBUG("wm", "Activating main_surface (%d)", surface_id);
692 this->api_activate_surface(
693 this->lookup_name(surface_id).value_or("unknown-name").c_str());
697 void App::surface_removed(uint32_t surface_id) {
698 HMI_DEBUG("wm", "surface_id is %u", surface_id);
700 // We cannot normally deactivate the main_surface, so be explicit
702 if (int(surface_id) == this->layers.main_surface) {
703 this->deactivate_main_surface();
705 auto role = this->lookup_name(surface_id);
707 this->allocateWindowResource("deactivate",
708 role->c_str(), nullptr,
713 this->id_alloc.remove_id(surface_id);
714 this->layers.remove_surface(surface_id);
717 void App::emit_activated(char const *label) {
718 this->send_event(kListEventName[Event_Active], label);
721 void App::emit_deactivated(char const *label) {
722 this->send_event(kListEventName[Event_Inactive], label);
725 void App::emit_syncdraw(char const *label, char const *area, int x, int y, int w, int h) {
726 this->send_event(kListEventName[Event_SyncDraw], label, area, x, y, w, h);
729 void App::emit_flushdraw(char const *label) {
730 this->send_event(kListEventName[Event_FlushDraw], label);
733 void App::emit_visible(char const *label, bool is_visible) {
734 this->send_event(is_visible ? kListEventName[Event_Visible] : kListEventName[Event_Invisible], label);
737 void App::emit_invisible(char const *label) {
738 return emit_visible(label, false);
741 void App::emit_visible(char const *label) { return emit_visible(label, true); }
743 void App::emitHeadlampOff() {
744 // Send HeadlampOff event for all application
745 this->send_event(kListEventName[Event_HeadlampOff]);
748 void App::emitHeadlampOn() {
749 // Send HeadlampOn event for all application
750 this->send_event(kListEventName[Event_HeadlampOn]);
753 void App::emitParkingBrakeOff() {
754 // Send ParkingBrakeOff event for all application
755 this->send_event(kListEventName[Event_ParkingBrakeOff]);
758 void App::emitParkingBrakeOn() {
759 // Send ParkingBrakeOn event for all application
760 this->send_event(kListEventName[Event_ParkingBrakeOn]);
763 void App::emitLightstatusBrakeOff() {
764 // Send LightstatusBrakeOff event for all application
765 this->send_event(kListEventName[Event_LightstatusBrakeOff]);
768 void App::emitLightstatusBrakeOn() {
769 // Send LightstatusBrakeOn event for all application
770 this->send_event(kListEventName[Event_LightstatusBrakeOn]);
773 void App::emitCarStop() {
774 // Send CarStop event for all application
775 this->send_event(kListEventName[Event_CarStop]);
778 void App::emitCarRun() {
779 // Send CarRun event for all application
780 this->send_event(kListEventName[Event_CarRun]);
783 result<int> App::api_request_surface(char const *drawing_name) {
784 // Convert drawing_name to role
785 const char* role = this->convertDrawingNameToRole(drawing_name);
787 auto lid = this->layers.get_layer_id(std::string(role));
790 * register drawing_name as fallback and make it displayed.
792 lid = this->layers.get_layer_id(std::string("Fallback"));
793 HMI_DEBUG("wm", "%s is not registered in layers.json, then fallback as normal app", drawing_name);
795 return Err<int>("Drawing name does not match any role, Fallback is disabled");
799 auto rname = this->lookup_id(role);
801 // name does not exist yet, allocate surface id...
802 auto id = int(this->id_alloc.generate_id(role));
803 this->layers.add_surface(id, *lid);
805 // set the main_surface[_name] here and now
806 if (!this->layers.main_surface_name.empty() &&
807 this->layers.main_surface_name == drawing_name) {
808 this->layers.main_surface = id;
809 HMI_DEBUG("wm", "Set main_surface id to %u", id);
812 // Set map of (role, surface_id)
813 this->role2surfaceid_[role] = id;
815 // Set map of (role, drawing_name)
816 this->role2drawingname_[role] = std::string(drawing_name);
821 // Check currently registered drawing names if it is already there.
822 return Err<int>("Surface already present");
825 char const *App::api_request_surface(char const *drawing_name,
826 char const *ivi_id) {
828 // Convert drawing_name to role
829 const char* role = this->convertDrawingNameToRole(drawing_name);
831 auto lid = this->layers.get_layer_id(std::string(role));
832 unsigned sid = std::stol(ivi_id);
836 * register drawing_name as fallback and make it displayed.
838 lid = this->layers.get_layer_id(std::string("Fallback"));
839 HMI_DEBUG("wm", "%s is not registered in layers.json, then fallback as normal app", drawing_name);
841 return "Drawing name does not match any role, Fallback is disabled";
845 auto rname = this->lookup_id(role);
848 return "Surface already present";
851 // register pair drawing_name and ivi_id
852 this->id_alloc.register_name_id(role, sid);
853 this->layers.add_surface(sid, *lid);
855 // this surface is already created
856 HMI_DEBUG("wm", "surface_id is %u, layer_id is %u", sid, *lid);
858 this->controller->layers[*lid]->add_surface(sid);
859 this->layout_commit();
864 result<json_object *> App::api_get_display_info() {
866 if (!this->controller) {
867 return Err<json_object *>("ivi_controller global not available");
871 compositor::size o_size = this->controller->output_size;
872 compositor::size p_size = this->controller->physical_size;
874 json_object *object = json_object_new_object();
875 json_object_object_add(object, kKeyWidthPixel, json_object_new_int(o_size.w));
876 json_object_object_add(object, kKeyHeightPixel, json_object_new_int(o_size.h));
877 json_object_object_add(object, kKeyWidthMm, json_object_new_int(p_size.w));
878 json_object_object_add(object, kKeyHeightMm, json_object_new_int(p_size.h));
880 return Ok<json_object *>(object);
883 result<json_object *> App::api_get_area_info(char const *drawing_name) {
884 HMI_DEBUG("wm", "called");
886 // Convert drawing_name to role
887 const char* role = this->convertDrawingNameToRole(drawing_name);
889 // Check drawing name, surface/layer id
890 auto const &surface_id = this->lookup_id(role);
892 return Err<json_object *>("Surface does not exist");
895 if (!this->controller->surface_exists(*surface_id)) {
896 return Err<json_object *>("Surface does not exist in controller!");
899 auto layer_id = this->layers.get_layer_id(*surface_id);
901 return Err<json_object *>("Surface is not on any layer!");
904 auto o_state = *this->layers.get_layout_state(*surface_id);
905 if (o_state == nullptr) {
906 return Err<json_object *>("Could not find layer for surface");
909 struct LayoutState &state = *o_state;
910 if ((state.main != *surface_id) && (state.sub != *surface_id)) {
911 return Err<json_object *>("Surface is inactive");
914 // Set area rectangle
915 compositor::rect area_info = this->area_info[*surface_id];
916 json_object *object = json_object_new_object();
917 json_object_object_add(object, kKeyX, json_object_new_int(area_info.x));
918 json_object_object_add(object, kKeyY, json_object_new_int(area_info.y));
919 json_object_object_add(object, kKeyWidth, json_object_new_int(area_info.w));
920 json_object_object_add(object, kKeyHeight, json_object_new_int(area_info.h));
922 return Ok<json_object *>(object);
925 void App::activate(int id) {
926 auto ip = this->controller->sprops.find(id);
927 if (ip != this->controller->sprops.end()) {
928 this->controller->surfaces[id]->set_visibility(1);
930 this->lookup_name(id).value_or("unknown-name").c_str();
932 if ((0 == strcmp(label, "radio"))
933 || (0 == strcmp(label, "music"))
934 || (0 == strcmp(label, "map"))) {
935 for (auto i = surface_bg.begin(); i != surface_bg.end(); ++i) {
938 this->surface_bg.erase(i);
940 // Remove from BG layer (999)
941 HMI_DEBUG("wm", "Remove %s(%d) from BG layer", label, id);
942 this->controller->layers[999]->remove_surface(id);
944 // Add to FG layer (1001)
945 HMI_DEBUG("wm", "Add %s(%d) to FG layer", label, id);
946 this->controller->layers[1001]->add_surface(id);
948 for (int j : this->surface_bg) {
949 HMI_DEBUG("wm", "Stored id:%d", j);
956 this->layout_commit();
958 this->emit_visible(label);
959 this->emit_activated(label);
963 void App::deactivate(int id) {
964 auto ip = this->controller->sprops.find(id);
965 if (ip != this->controller->sprops.end()) {
967 this->lookup_name(id).value_or("unknown-name").c_str();
969 if ((0 == strcmp(label, "radio"))
970 || (0 == strcmp(label, "music"))
971 || (0 == strcmp(label, "map"))) {
974 this->surface_bg.push_back(id);
976 // Remove from FG layer (1001)
977 HMI_DEBUG("wm", "Remove %s(%d) from FG layer", label, id);
978 this->controller->layers[1001]->remove_surface(id);
980 // Add to BG layer (999)
981 HMI_DEBUG("wm", "Add %s(%d) to BG layer", label, id);
982 this->controller->layers[999]->add_surface(id);
984 for (int j : surface_bg) {
985 HMI_DEBUG("wm", "Stored id:%d", j);
989 this->controller->surfaces[id]->set_visibility(0);
992 this->layout_commit();
994 this->emit_deactivated(label);
995 this->emit_invisible(label);
999 void App::deactivate(std::string role) {
1000 auto const &id = this->lookup_id(role.c_str());
1002 HMI_ERROR("wm", "Surface does not exist");
1005 std::string drawing_name = this->role2drawingname_[role];
1006 HMI_DEBUG("wm", "Deactivate role:%s (drawing_name:%s)",
1007 role.c_str(), drawing_name.c_str());
1009 this->deactivate(*id);
1012 void App::deactivate_main_surface() {
1013 this->layers.main_surface = -1;
1014 this->allocateWindowResource("deactivate",
1015 this->layers.main_surface_name.c_str(), nullptr,
1022 void controller_hooks::surface_created(uint32_t surface_id) {
1023 this->app->surface_created(surface_id);
1026 void controller_hooks::surface_removed(uint32_t surface_id) {
1027 this->app->surface_removed(surface_id);
1030 void controller_hooks::surface_visibility(uint32_t /*surface_id*/,
1033 void controller_hooks::surface_destination_rectangle(uint32_t /*surface_id*/,
1039 void App::setSurfaceSize(const char* role, const char* area) {
1040 HMI_DEBUG("wm", "role:%s area:%s", role, area);
1042 // Get size of current area
1043 compositor::rect size = this->lm_.getAreaSize(area);
1045 // Set destination to the display rectangle
1046 int surface_id = this->role2surfaceid_[role];
1048 if (!this->controller->surface_exists(surface_id)) {
1049 // Block until all pending request are processed by wayland display server
1050 // because waiting for the surface of new app is created
1051 this->display->roundtrip();
1053 auto &s = this->controller->surfaces[surface_id];
1054 s->set_destination_rectangle(size.x, size.y, size.w, size.h);
1055 this->layout_commit();
1057 // Update area information
1058 this->area_info[surface_id].x = size.x;
1059 this->area_info[surface_id].y = size.y;
1060 this->area_info[surface_id].w = size.w;
1061 this->area_info[surface_id].h = size.h;
1062 HMI_DEBUG("wm", "Surface rect { %d, %d, %d, %d }",
1063 size.x, size.y, size.w, size.h);
1065 // Emit syncDraw event
1066 const char* drawing_name = this->role2drawingname_[role].c_str();
1067 this->emit_syncdraw(drawing_name, area,
1068 size.x, size.y, size.w, size.h);
1070 // Enqueue flushDraw event
1071 this->enqueue_flushdraw(surface_id);
1074 void App::setAccelPedalPos(double val) {
1075 this->crr_car_info_.accel_pedal_pos = val;
1078 extern const char* kDefaultAppDb;
1079 int App::loadAppDb() {
1080 HMI_DEBUG("wm", "Call");
1082 // Get afm application installed dir
1083 char const *afm_app_install_dir = getenv("AFM_APP_INSTALL_DIR");
1084 HMI_DEBUG("wm", "afm_app_install_dir:%s", afm_app_install_dir);
1086 std::string file_name;
1087 if (!afm_app_install_dir) {
1088 HMI_ERROR("wm", "AFM_APP_INSTALL_DIR is not defined");
1091 file_name = std::string(afm_app_install_dir) + std::string("/etc/app.db");
1095 json_object* json_obj;
1096 int ret = jh::inputJsonFilie(file_name.c_str(), &json_obj);
1098 HMI_ERROR("wm", "Could not open app.db, so use default role information");
1099 json_obj = json_tokener_parse(kDefaultAppDb);
1101 HMI_DEBUG("wm", "json_obj dump:%s", json_object_get_string(json_obj));
1104 HMI_DEBUG("wm", "Perse apps");
1105 json_object* json_cfg;
1106 if (!json_object_object_get_ex(json_obj, "apps", &json_cfg)) {
1107 HMI_ERROR("wm", "Parse Error!!");
1111 int len = json_object_array_length(json_cfg);
1112 HMI_DEBUG("wm", "json_cfg len:%d", len);
1113 HMI_DEBUG("wm", "json_cfg dump:%s", json_object_get_string(json_cfg));
1115 for (int i=0; i<len; i++) {
1116 json_object* json_tmp = json_object_array_get_idx(json_cfg, i);
1117 HMI_DEBUG("wm", "> json_tmp dump:%s", json_object_get_string(json_tmp));
1119 const char* app = jh::getStringFromJson(json_tmp, "name");
1120 if (nullptr == app) {
1121 HMI_ERROR("wm", "Parse Error!!");
1124 HMI_DEBUG("wm", "> app:%s", app);
1126 const char* role = jh::getStringFromJson(json_tmp, "role");
1127 if (nullptr == role) {
1128 HMI_ERROR("wm", "Parse Error!!");
1131 HMI_DEBUG("wm", "> role:%s", role);
1133 this->drawingname2role_[app] = std::string(role);
1137 for(auto itr = this->drawingname2role_.begin();
1138 itr != this->drawingname2role_.end(); ++itr) {
1139 HMI_DEBUG("wm", "app:%s role:%s",
1140 itr->first.c_str(), itr->second.c_str());
1143 // Release json_object
1144 json_object_put(json_obj);
1150 const char* kDefaultAppDb = "{ \
1153 \"name\": \"HomeScreen\", \
1154 \"role\": \"homescreen\" \
1157 \"name\": \"Music\", \
1158 \"role\": \"music\" \
1161 \"name\": \"MediaPlayer\", \
1162 \"role\": \"music\" \
1165 \"name\": \"Video\", \
1166 \"role\": \"video\" \
1169 \"name\": \"VideoPlayer\", \
1170 \"role\": \"video\" \
1173 \"name\": \"WebBrowser\", \
1174 \"role\": \"browser\" \
1177 \"name\": \"Radio\", \
1178 \"role\": \"radio\" \
1181 \"name\": \"Phone\", \
1182 \"role\": \"phone\" \
1185 \"name\": \"Navigation\", \
1189 \"name\": \"HVAC\", \
1190 \"role\": \"hvac\" \
1193 \"name\": \"Settings\", \
1194 \"role\": \"settings\" \
1197 \"name\": \"Dashboard\", \
1198 \"role\": \"dashboard\" \
1201 \"name\": \"POI\", \
1205 \"name\": \"Mixer\", \
1206 \"role\": \"mixer\" \