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);
99 App::App(wl::display *d)
107 pending_events(false),
111 auto l = load_layer_map(
112 this->config.get_string("layers.json").value().c_str());
114 this->layers = l.unwrap();
116 HMI_ERROR("wm", "%s", l.err().value());
119 } catch (std::exception &e) {
120 HMI_ERROR("wm", "Loading of configuration failed: %s", e.what());
125 if (!this->display->ok()) {
129 if (this->layers.mapping.empty()) {
130 HMI_ERROR("wm", "No surface -> layer mapping loaded");
139 // Initialize PolicyManager
140 this->pm_.initialize();
142 // Initialize LayoutManager
143 this->lm_.initialize();
146 for (int i=Event_Val_Min; i<=Event_Val_Max; i++) {
147 map_afb_event[kListEventName[i]] = afb_daemon_make_event(kListEventName[i]);
150 this->display->add_global_handler(
151 "wl_output", [this](wl_registry *r, uint32_t name, uint32_t v) {
152 this->outputs.emplace_back(std::make_unique<wl::output>(r, name, v));
155 this->display->add_global_handler(
156 "ivi_wm", [this](wl_registry *r, uint32_t name, uint32_t v) {
158 std::make_unique<struct compositor::controller>(r, name, v);
160 // Init controller hooks
161 this->controller->chooks = &this->chooks;
163 // This protocol needs the output, so lets just add our mapping here...
164 this->controller->add_proxy_to_id_mapping(
165 this->outputs.back()->proxy.get(),
166 wl_proxy_get_id(reinterpret_cast<struct wl_proxy *>(
167 this->outputs.back()->proxy.get())));
170 this->controller->create_screen(this->outputs.back()->proxy.get());
172 // Set display to controller
173 this->controller->display = this->display;
176 // First level objects
177 this->display->roundtrip();
178 // Second level objects
179 this->display->roundtrip();
180 // Third level objects
181 this->display->roundtrip();
183 return init_layers();
186 int App::dispatch_pending_events() {
187 if (this->pop_pending_events()) {
188 this->display->dispatch_pending();
194 bool App::pop_pending_events() {
196 return this->pending_events.compare_exchange_strong(
197 x, false, std::memory_order_consume);
200 void App::set_pending_events() {
201 this->pending_events.store(true, std::memory_order_release);
204 optional<int> App::lookup_id(char const *name) {
205 return this->id_alloc.lookup(std::string(name));
207 optional<std::string> App::lookup_name(int id) {
208 return this->id_alloc.lookup(id);
214 int App::init_layers() {
215 if (!this->controller) {
216 HMI_ERROR("wm", "ivi_controller global not available");
220 if (this->outputs.empty()) {
221 HMI_ERROR("wm", "no output was set up!");
225 auto &c = this->controller;
227 auto &o = this->outputs.front();
228 auto &s = c->screens.begin()->second;
229 auto &layers = c->layers;
231 // Write output dimensions to ivi controller...
232 c->output_size = compositor::size{uint32_t(o->width), uint32_t(o->height)};
233 c->physical_size = compositor::size{uint32_t(o->physical_width),
234 uint32_t(o->physical_height)};
242 // Quick and dirty setup of layers
243 for (auto const &i : this->layers.mapping) {
244 c->layer_create(i.second.layer_id, o->width, o->height);
245 auto &l = layers[i.second.layer_id];
246 l->set_destination_rectangle(0, 0, o->width, o->height);
247 l->set_visibility(1);
248 HMI_DEBUG("wm", "Setting up layer %s (%d) for surface role match \"%s\"",
249 i.second.name.c_str(), i.second.layer_id, i.second.role.c_str());
252 // Add layers to screen
253 s->set_render_order(this->layers.layers);
255 this->layout_commit();
260 void App::layout_commit() {
261 this->controller->commit_changes();
262 this->display->flush();
265 void App::allocateWindowResource(char const *event, char const *drawing_name,
266 char const *drawing_area, char const *role,
267 const reply_func &reply) {
268 const char* new_role = nullptr;
269 const char* new_area = nullptr;
271 if (0 == strcmp("activate", event)) {
273 // This process will be removed
274 // because the applications will specify role instead of drawing_name
276 if ((nullptr == role) || (0 == strcmp("", role))) {
277 HMI_DEBUG("wm", "Role is not specified, so get by using app name");
278 new_role = this->app2role_[drawing_name].c_str();
283 HMI_DEBUG("wm", "drawing_name:%s, new_role: %s", drawing_name, new_role);
287 // This process will be removed
288 // because the area "normal.full" and "normalfull" will be prohibited
290 if (nullptr == drawing_area) {
293 else if (0 == strcmp("normal.full", drawing_area)) {
296 else if (0 == strcmp("restriction.split.sub", drawing_area)) {
297 new_area = "restriction.split.sub";
299 else if (0 == strcmp("homescreen", new_role)) {
300 // Now homescreen specifies "normalfull"
306 HMI_DEBUG("wm", "drawing_area:%s, new_area: %s", drawing_area, new_area);
309 else if (0 == strcmp("deactivate", event)) {
311 // This process will be removed
312 // because the applications will specify role instead of drawing_name
314 if ((nullptr == role) || (0 == strcmp("", role))) {
315 HMI_DEBUG("wm", "Role is not specified, so get by using app name");
316 new_role = this->app2role_[drawing_name].c_str();
321 HMI_DEBUG("wm", "drawing_name:%s, new_role: %s", drawing_name, new_role);
330 // If event is "activate" and area is not specifid,
331 // get default value by using role
334 json_object* json_in = json_object_new_object();
335 json_object* json_out = json_object_new_object();
336 json_object_object_add(json_in, "event", json_object_new_string(event));
338 if (nullptr != new_role) {
339 json_object_object_add(json_in, "role", json_object_new_string(new_role));
341 if (nullptr != new_area) {
342 json_object_object_add(json_in, "area", json_object_new_string(new_area));
345 int ret = this->pm_.checkPolicy(json_in, &json_out);
347 reply("Error checkPolicy()");
351 HMI_DEBUG("wm", "result: %s", json_object_get_string(json_out));
354 // Release json_object
355 json_object_put(json_in);
357 // Check parking brake state
358 json_object* json_parking_brake;
359 if (!json_object_object_get_ex(json_out, "parking_brake", &json_parking_brake)) {
360 reply("Not found key \"parking_brake\"");
364 json_bool is_changed;
365 is_changed = jh::getBoolFromJson(json_parking_brake, "is_changed");
367 // Update parking brake state
368 std::string parking_brake_state = jh::getStringFromJson(json_parking_brake, "state");
369 HMI_DEBUG("wm", "parking_brake_state: %s", parking_brake_state.c_str());
371 // Emit parking brake event
372 if ("parking_brake_off" == parking_brake_state) {
373 this->emitParkingBrakeOff();
375 else if ("parking_brake_on" == parking_brake_state) {
376 this->emitParkingBrakeOn();
379 reply("Unknown parking brake state");
385 json_object* json_car;
386 if (!json_object_object_get_ex(json_out, "car", &json_car)) {
387 reply("Not found key \"car\"");
391 is_changed = jh::getBoolFromJson(json_car, "is_changed");
394 std::string car_state = jh::getStringFromJson(json_car, "state");
395 HMI_DEBUG("wm", "car_state: %s", car_state.c_str());
398 if ("car_stop" == car_state) {
401 else if ("car_run" == car_state) {
405 reply("Unknown car state");
411 json_object* json_lamp;
412 if (!json_object_object_get_ex(json_out, "lamp", &json_lamp)) {
413 reply("Not found key \"lamp\"");
417 is_changed = jh::getBoolFromJson(json_lamp, "is_changed");
420 std::string lamp_state = jh::getStringFromJson(json_lamp, "state");
421 HMI_DEBUG("wm", "lamp_state: %s", lamp_state.c_str());
424 if ("lamp_off" == lamp_state) {
425 this->emitHeadlampOff();
427 else if ("lamp_on" == lamp_state) {
428 this->emitHeadlampOn();
431 reply("Unknown lamp state");
437 const char* category = nullptr;
438 std::string str_category;
439 if (nullptr != new_role) {
440 str_category = this->pm_.roleToCategory(new_role);
441 category = str_category.c_str();
442 HMI_DEBUG("wm", "role:%s category:%s", new_role, category);
446 if (this->lm_.updateLayout(json_out, new_role, category)) {
447 HMI_DEBUG("wm", "Layer is changed!!");
450 this->allocateSurface();
453 HMI_DEBUG("wm", "All layer is NOT changed!!");
456 // Release json_object
457 json_object_put(json_out);
462 void App::enqueue_flushdraw(int surface_id) {
463 this->check_flushdraw(surface_id);
464 HMI_DEBUG("wm", "Enqueuing EndDraw for surface_id %d", surface_id);
465 this->pending_end_draw.push_back(surface_id);
468 void App::check_flushdraw(int surface_id) {
469 auto i = std::find(std::begin(this->pending_end_draw),
470 std::end(this->pending_end_draw), surface_id);
471 if (i != std::end(this->pending_end_draw)) {
472 auto n = this->lookup_name(surface_id);
473 HMI_ERROR("wm", "Application %s (%d) has pending EndDraw call(s)!",
474 n ? n->c_str() : "unknown-name", surface_id);
475 std::swap(this->pending_end_draw[std::distance(
476 std::begin(this->pending_end_draw), i)],
477 this->pending_end_draw.back());
478 this->pending_end_draw.resize(this->pending_end_draw.size() - 1);
482 void App::api_enddraw(char const *drawing_name) {
483 for (unsigned i = 0, iend = this->pending_end_draw.size(); i < iend; i++) {
484 auto n = this->lookup_name(this->pending_end_draw[i]);
485 if (n && *n == drawing_name) {
486 std::swap(this->pending_end_draw[i], this->pending_end_draw[iend - 1]);
487 this->pending_end_draw.resize(iend - 1);
488 this->activate(this->pending_end_draw[i]);
489 this->emit_flushdraw(drawing_name);
494 void App::api_ping() { this->dispatch_pending_events(); }
496 void App::send_event(char const *evname){
497 HMI_DEBUG("wm", "%s: %s", __func__, evname);
499 int ret = afb_event_push(this->map_afb_event[evname], nullptr);
501 HMI_DEBUG("wm", "afb_event_push failed: %m");
505 void App::send_event(char const *evname, char const *label){
506 HMI_DEBUG("wm", "%s: %s(%s)", __func__, evname, label);
508 json_object *j = json_object_new_object();
509 json_object_object_add(j, kKeyDrawingName, json_object_new_string(label));
511 int ret = afb_event_push(this->map_afb_event[evname], j);
513 HMI_DEBUG("wm", "afb_event_push failed: %m");
517 void App::send_event(char const *evname, char const *label, char const *area,
518 int x, int y, int w, int h) {
519 HMI_DEBUG("wm", "%s: %s(%s, %s) x:%d y:%d w:%d h:%d",
520 __func__, evname, label, area, x, y, w, h);
522 json_object *j_rect = json_object_new_object();
523 json_object_object_add(j_rect, kKeyX, json_object_new_int(x));
524 json_object_object_add(j_rect, kKeyY, json_object_new_int(y));
525 json_object_object_add(j_rect, kKeyWidth, json_object_new_int(w));
526 json_object_object_add(j_rect, kKeyHeight, json_object_new_int(h));
528 json_object *j = json_object_new_object();
529 json_object_object_add(j, kKeyDrawingName, json_object_new_string(label));
530 json_object_object_add(j, kKeyDrawingArea, json_object_new_string(area));
531 json_object_object_add(j, kKeyDrawingRect, j_rect);
533 int ret = afb_event_push(this->map_afb_event[evname], j);
535 HMI_DEBUG("wm", "afb_event_push failed: %m");
542 void App::surface_created(uint32_t surface_id) {
543 auto layer_id = this->layers.get_layer_id(surface_id);
545 HMI_DEBUG("wm", "Newly created surfce %d is not associated with any layer!",
550 HMI_DEBUG("wm", "surface_id is %u, layer_id is %u", surface_id, *layer_id);
552 this->controller->layers[*layer_id]->add_surface(surface_id);
553 this->layout_commit();
554 // activate the main_surface right away
555 /*if (surface_id == static_cast<unsigned>(this->layers.main_surface)) {
556 HMI_DEBUG("wm", "Activating main_surface (%d)", surface_id);
558 this->api_activate_surface(
559 this->lookup_name(surface_id).value_or("unknown-name").c_str());
563 void App::surface_removed(uint32_t surface_id) {
564 HMI_DEBUG("wm", "surface_id is %u", surface_id);
566 // We cannot normally deactivate the main_surface, so be explicit
568 if (int(surface_id) == this->layers.main_surface) {
569 this->deactivate_main_surface();
571 auto drawing_name = this->lookup_name(surface_id);
573 this->allocateWindowResource("deactivate", drawing_name->c_str(),
579 this->id_alloc.remove_id(surface_id);
580 this->layers.remove_surface(surface_id);
583 void App::emit_activated(char const *label) {
584 this->send_event(kListEventName[Event_Active], label);
587 void App::emit_deactivated(char const *label) {
588 this->send_event(kListEventName[Event_Inactive], label);
591 void App::emit_syncdraw(char const *label, char const *area, int x, int y, int w, int h) {
592 this->send_event(kListEventName[Event_SyncDraw], label, area, x, y, w, h);
595 void App::emit_flushdraw(char const *label) {
596 this->send_event(kListEventName[Event_FlushDraw], label);
599 void App::emit_visible(char const *label, bool is_visible) {
600 this->send_event(is_visible ? kListEventName[Event_Visible] : kListEventName[Event_Invisible], label);
603 void App::emit_invisible(char const *label) {
604 return emit_visible(label, false);
607 void App::emit_visible(char const *label) { return emit_visible(label, true); }
609 void App::emitHeadlampOff() {
610 // Send HeadlampOff event for all application
611 this->send_event(kListEventName[Event_HeadlampOff]);
614 void App::emitHeadlampOn() {
615 // Send HeadlampOn event for all application
616 this->send_event(kListEventName[Event_HeadlampOn]);
619 void App::emitParkingBrakeOff() {
620 // Send ParkingBrakeOff event for all application
621 this->send_event(kListEventName[Event_ParkingBrakeOff]);
624 void App::emitParkingBrakeOn() {
625 // Send ParkingBrakeOn event for all application
626 this->send_event(kListEventName[Event_ParkingBrakeOn]);
629 void App::emitCarStop() {
630 // Send CarStop event for all application
631 this->send_event(kListEventName[Event_CarStop]);
634 void App::emitCarRun() {
635 // Send CarRun event for all application
636 this->send_event(kListEventName[Event_CarRun]);
639 result<int> App::api_request_surface(char const *drawing_name) {
640 auto lid = this->layers.get_layer_id(std::string(drawing_name));
643 * register drawing_name as fallback and make it displayed.
645 lid = this->layers.get_layer_id(std::string("Fallback"));
646 HMI_DEBUG("wm", "%s is not registered in layers.json, then fallback as normal app", drawing_name);
648 return Err<int>("Drawing name does not match any role, Fallback is disabled");
652 auto rname = this->lookup_id(drawing_name);
654 // name does not exist yet, allocate surface id...
655 auto id = int(this->id_alloc.generate_id(drawing_name));
656 this->layers.add_surface(id, *lid);
658 // set the main_surface[_name] here and now
659 if (!this->layers.main_surface_name.empty() &&
660 this->layers.main_surface_name == drawing_name) {
661 this->layers.main_surface = id;
662 HMI_DEBUG("wm", "Set main_surface id to %u", id);
667 // This process will be implemented in SystemManager
670 auto id = int(this->app_id_alloc_.generate_id(drawing_name));
671 this->appname2appid_[drawing_name] = id;
675 // Set map of (role, surface_id)
676 std::string role = this->app2role_[std::string(drawing_name)];
677 this->role2surfaceid_[role] = id;
679 // Set map of (role, app)
680 // If the new app which has the same role which is had by existing app is requested,
681 // the role is given to the new app.
682 this->role2app_[role] = std::string(drawing_name);
687 // Check currently registered drawing names if it is already there.
688 return Err<int>("Surface already present");
691 char const *App::api_request_surface(char const *drawing_name,
692 char const *ivi_id) {
695 auto lid = this->layers.get_layer_id(std::string(drawing_name));
696 unsigned sid = std::stol(ivi_id);
700 * register drawing_name as fallback and make it displayed.
702 lid = this->layers.get_layer_id(std::string("Fallback"));
703 HMI_DEBUG("wm", "%s is not registered in layers.json, then fallback as normal app", drawing_name);
705 return "Drawing name does not match any role, Fallback is disabled";
709 auto rname = this->lookup_id(drawing_name);
712 return "Surface already present";
715 // register pair drawing_name and ivi_id
716 this->id_alloc.register_name_id(drawing_name, sid);
717 this->layers.add_surface(sid, *lid);
719 // this surface is already created
720 HMI_DEBUG("wm", "surface_id is %u, layer_id is %u", sid, *lid);
722 this->controller->layers[*lid]->add_surface(sid);
723 this->layout_commit();
728 result<json_object *> App::api_get_display_info() {
730 if (!this->controller) {
731 return Err<json_object *>("ivi_controller global not available");
735 compositor::size o_size = this->controller->output_size;
736 compositor::size p_size = this->controller->physical_size;
738 json_object *object = json_object_new_object();
739 json_object_object_add(object, kKeyWidthPixel, json_object_new_int(o_size.w));
740 json_object_object_add(object, kKeyHeightPixel, json_object_new_int(o_size.h));
741 json_object_object_add(object, kKeyWidthMm, json_object_new_int(p_size.w));
742 json_object_object_add(object, kKeyHeightMm, json_object_new_int(p_size.h));
744 return Ok<json_object *>(object);
747 result<json_object *> App::api_get_area_info(char const *drawing_name) {
748 HMI_DEBUG("wm", "called");
750 // Check drawing name, surface/layer id
751 auto const &surface_id = this->lookup_id(drawing_name);
753 return Err<json_object *>("Surface does not exist");
756 if (!this->controller->surface_exists(*surface_id)) {
757 return Err<json_object *>("Surface does not exist in controller!");
760 auto layer_id = this->layers.get_layer_id(*surface_id);
762 return Err<json_object *>("Surface is not on any layer!");
765 auto o_state = *this->layers.get_layout_state(*surface_id);
766 if (o_state == nullptr) {
767 return Err<json_object *>("Could not find layer for surface");
770 struct LayoutState &state = *o_state;
771 if ((state.main != *surface_id) && (state.sub != *surface_id)) {
772 return Err<json_object *>("Surface is inactive");
775 // Set area rectangle
776 compositor::rect area_info = this->area_info[*surface_id];
777 json_object *object = json_object_new_object();
778 json_object_object_add(object, kKeyX, json_object_new_int(area_info.x));
779 json_object_object_add(object, kKeyY, json_object_new_int(area_info.y));
780 json_object_object_add(object, kKeyWidth, json_object_new_int(area_info.w));
781 json_object_object_add(object, kKeyHeight, json_object_new_int(area_info.h));
783 return Ok<json_object *>(object);
786 void App::activate(int id) {
787 auto ip = this->controller->sprops.find(id);
788 if (ip != this->controller->sprops.end()) {
789 this->controller->surfaces[id]->set_visibility(1);
791 this->lookup_name(id).value_or("unknown-name").c_str();
794 if ((0 == strcmp(label, "Radio"))
795 || (0 == strcmp(label, "MediaPlayer"))
796 || (0 == strcmp(label, "Music"))
797 || (0 == strcmp(label, "Navigation"))) {
798 for (auto i = surface_bg.begin(); i != surface_bg.end(); ++i) {
801 this->surface_bg.erase(i);
803 // Remove from BG layer (999)
804 HMI_DEBUG("wm", "Remove %s(%d) from BG layer", label, id);
805 this->controller->layers[999]->remove_surface(id);
807 // Add to FG layer (1001)
808 HMI_DEBUG("wm", "Add %s(%d) to FG layer", label, id);
809 this->controller->layers[1001]->add_surface(id);
811 for (int j : this->surface_bg) {
812 HMI_DEBUG("wm", "Stored id:%d", j);
819 this->layout_commit();
821 this->emit_visible(label);
822 this->emit_activated(label);
826 void App::deactivate(int id) {
827 auto ip = this->controller->sprops.find(id);
828 if (ip != this->controller->sprops.end()) {
830 this->lookup_name(id).value_or("unknown-name").c_str();
833 if ((0 == strcmp(label, "Radio"))
834 || (0 == strcmp(label, "MediaPlayer"))
835 || (0 == strcmp(label, "Music"))
836 || (0 == strcmp(label, "Navigation"))) {
839 this->surface_bg.push_back(id);
841 // Remove from FG layer (1001)
842 HMI_DEBUG("wm", "Remove %s(%d) from FG layer", label, id);
843 this->controller->layers[1001]->remove_surface(id);
845 // Add to BG layer (999)
846 HMI_DEBUG("wm", "Add %s(%d) to BG layer", label, id);
847 this->controller->layers[999]->add_surface(id);
849 for (int j : surface_bg) {
850 HMI_DEBUG("wm", "Stored id:%d", j);
854 this->controller->surfaces[id]->set_visibility(0);
858 this->emit_deactivated(label);
859 this->emit_invisible(label);
863 void App::deactivate(std::string role) {
864 std::string app = this->role2app_[role];
865 auto const &id = this->lookup_id(app.c_str());
867 HMI_ERROR("wm", "Surface does not exist");
870 HMI_DEBUG("wm", "Deactivate role:%s (app:%s)",
871 role.c_str(), app.c_str());
873 this->deactivate(*id);
876 void App::deactivate_main_surface() {
877 this->layers.main_surface = -1;
878 this->allocateWindowResource("deactivate", this->layers.main_surface_name.c_str(),
886 void controller_hooks::surface_created(uint32_t surface_id) {
887 this->app->surface_created(surface_id);
890 void controller_hooks::surface_removed(uint32_t surface_id) {
891 this->app->surface_removed(surface_id);
894 void controller_hooks::surface_visibility(uint32_t /*surface_id*/,
897 void controller_hooks::surface_destination_rectangle(uint32_t /*surface_id*/,
903 int App::allocateSurface() {
904 HMI_DEBUG("wm", "Call");
906 // Get current/previous layers
907 LayoutManager::TypeLayers crr_layers = this->lm_.getCurrentLayers();
908 LayoutManager::TypeLayers prv_layers = this->lm_.getPreviousLayers();
910 // Update resource of all layers
911 for (auto itr_layers = crr_layers.begin();
912 itr_layers != crr_layers.end(); ++itr_layers) {
914 std::string layer = itr_layers->first;
915 HMI_DEBUG("wm", "Try to update resource in %s layer", layer.c_str());
917 // If layout is changed, update resouce
918 if (this->lm_.isLayoutChanged(layer.c_str())) {
919 // Get current/previous layout
920 LayoutManager::TypeLayouts crr_layout = itr_layers->second;
921 LayoutManager::TypeLayouts prv_layout = prv_layers[layer];
923 // Get current/previous layout name
924 std::string crr_layout_name = crr_layout.begin()->first;
925 std::string prv_layout_name = prv_layout.begin()->first;
926 HMI_DEBUG("wm", "layout name crr:%s prv:%s",
927 crr_layout_name.c_str(), prv_layout_name.c_str());
929 // Get current/previous ares
930 LayoutManager::TypeAreas crr_areas = crr_layout[crr_layout_name];
931 LayoutManager::TypeAreas prv_areas = prv_layout[prv_layout_name];
933 // Create previous displayed role list
934 std::string prv_area_name;
935 std::vector<std::string> prv_role_list;
936 for (auto itr_areas = prv_areas.begin();
937 itr_areas != prv_areas.end(); ++itr_areas) {
938 prv_area_name = itr_areas->first;
939 prv_role_list.push_back(prv_areas[prv_area_name]["role"]);
940 HMI_DEBUG("wm", "previous displayed role:%s",
941 prv_areas[prv_area_name]["role"].c_str());
944 // Allocate surface for each area
945 std::string crr_area_name;
946 std::string crr_role_name;
947 LayoutManager::TypeRolCtg crr_rol_ctg;
948 for (auto itr_areas = crr_areas.begin();
949 itr_areas != crr_areas.end(); ++itr_areas) {
950 crr_area_name = itr_areas->first;
951 crr_rol_ctg = itr_areas->second;
953 // Get role of current area
954 if ("category" == crr_rol_ctg.begin()->first) {
955 // If current area have category
957 std::string crr_ctg = crr_rol_ctg.begin()->second;
959 // Serch relevant role from previous displayed role list
960 for (auto itr_role = prv_role_list.begin();
961 itr_role != prv_role_list.end(); ++itr_role) {
962 std::string prv_ctg = this->pm_.roleToCategory((*itr_role).c_str());
963 if (crr_ctg == prv_ctg) {
964 // First discovered role is set to current role
965 crr_role_name = *itr_role;
967 // Delete used role for other areas
968 // which have same category
969 prv_role_list.erase(itr_role);
976 crr_role_name = itr_areas->second["role"];
978 HMI_DEBUG("wm", "Allocate surface for area:%s role:%s",
979 crr_area_name.c_str(), crr_role_name.c_str());
981 // Deactivate non-displayed role
982 std::string prv_role_name;
983 if (crr_layout_name == prv_layout_name) {
984 HMI_DEBUG("wm", "Current layout is same with previous");
986 // Deactivate previous role in same area
987 // if it is different with current
988 prv_role_name = prv_areas[crr_area_name]["role"];
989 if (crr_role_name != prv_role_name) {
990 this->deactivate(prv_role_name);
994 HMI_DEBUG("wm", "Current layout is different with previous");
996 if ("none" != prv_layout_name) {
997 // Deactivate previous role in all area in previous layout
998 // if it is different with current role
999 for(auto itr = prv_areas.begin(); itr != prv_areas.end(); ++itr) {
1000 prv_role_name = itr->second["role"].c_str();
1001 if (crr_role_name != prv_role_name) {
1002 this->deactivate(prv_role_name);
1008 // Set surface for displayed role
1009 if ("none" != crr_layout_name) {
1010 // If current layout is not "none",
1011 // set surface for current role
1012 this->setSurfaceSize(crr_role_name.c_str(), crr_area_name.c_str());
1015 // This API is workaround.
1016 // Resource manager should manage each resource infomations
1017 // according to architecture document.
1018 this->lm_.updateArea(layer.c_str(), crr_role_name.c_str(), crr_area_name.c_str());
1026 void App::setSurfaceSize(const char* role, const char* area) {
1027 HMI_DEBUG("wm", "role:%s area:%s", role, area);
1029 // Get size of current area
1030 compositor::rect size = this->lm_.getAreaSize(area);
1032 // Set destination to the display rectangle
1033 int surface_id = this->role2surfaceid_[role];
1034 auto &s = this->controller->surfaces[surface_id];
1035 s->set_destination_rectangle(size.x, size.y, size.w, size.h);
1036 this->layout_commit();
1038 // Update area information
1039 this->area_info[surface_id].x = size.x;
1040 this->area_info[surface_id].y = size.y;
1041 this->area_info[surface_id].w = size.w;
1042 this->area_info[surface_id].h = size.h;
1043 HMI_DEBUG("wm", "Surface rect { %d, %d, %d, %d }",
1044 size.x, size.y, size.w, size.h);
1046 // Emit syncDraw event
1047 const char* app = this->role2app_[role].c_str();
1048 this->emit_syncdraw(app, area,
1049 size.x, size.y, size.w, size.h);
1051 // Enqueue flushDraw event
1052 this->enqueue_flushdraw(surface_id);
1055 extern const char* kDefaultAppDb;
1056 int App::loadAppDb() {
1057 HMI_DEBUG("wm", "Call");
1059 // Get afm application installed dir
1060 char const *afm_app_install_dir = getenv("AFM_APP_INSTALL_DIR");
1061 HMI_DEBUG("wm", "afm_app_install_dir:%s", afm_app_install_dir);
1063 std::string file_name;
1064 if (!afm_app_install_dir) {
1065 HMI_ERROR("wm", "AFM_APP_INSTALL_DIR is not defined");
1068 file_name = std::string(afm_app_install_dir) + std::string("/etc/app.db");
1072 json_object* json_obj;
1073 int ret = jh::inputJsonFilie(file_name.c_str(), &json_obj);
1075 HMI_ERROR("wm", "Could not open app.db, so use default role information");
1076 json_obj = json_tokener_parse(kDefaultAppDb);
1078 HMI_DEBUG("wm", "json_obj dump:%s", json_object_get_string(json_obj));
1081 HMI_DEBUG("wm", "Perse apps");
1082 json_object* json_cfg;
1083 if (!json_object_object_get_ex(json_obj, "apps", &json_cfg)) {
1084 HMI_ERROR("wm", "Parse Error!!");
1088 int len = json_object_array_length(json_cfg);
1089 HMI_DEBUG("wm", "json_cfg len:%d", len);
1090 HMI_DEBUG("wm", "json_cfg dump:%s", json_object_get_string(json_cfg));
1092 for (int i=0; i<len; i++) {
1093 json_object* json_tmp = json_object_array_get_idx(json_cfg, i);
1094 HMI_DEBUG("wm", "> json_tmp dump:%s", json_object_get_string(json_tmp));
1096 const char* app = jh::getStringFromJson(json_tmp, "name");
1097 if (nullptr == app) {
1098 HMI_ERROR("wm", "Parse Error!!");
1101 HMI_DEBUG("wm", "> app:%s", app);
1103 const char* role = jh::getStringFromJson(json_tmp, "role");
1104 if (nullptr == role) {
1105 HMI_ERROR("wm", "Parse Error!!");
1108 HMI_DEBUG("wm", "> role:%s", role);
1110 this->app2role_[app] = std::string(role);
1114 for(auto itr = this->app2role_.begin();
1115 itr != this->app2role_.end(); ++itr) {
1116 HMI_DEBUG("wm", "app:%s role:%s",
1117 itr->first.c_str(), itr->second.c_str());
1120 // Release json_object
1121 json_object_put(json_obj);
1127 const char* kDefaultAppDb = "{ \
1130 \"name\": \"HomeScreen\", \
1131 \"role\": \"homescreen\" \
1134 \"name\": \"Music\", \
1135 \"role\": \"music\" \
1138 \"name\": \"MediaPlayer\", \
1139 \"role\": \"music\" \
1142 \"name\": \"Video\", \
1143 \"role\": \"video\" \
1146 \"name\": \"VideoPlayer\", \
1147 \"role\": \"video\" \
1150 \"name\": \"WebBrowser\", \
1151 \"role\": \"browser\" \
1154 \"name\": \"Radio\", \
1155 \"role\": \"radio\" \
1158 \"name\": \"Phone\", \
1159 \"role\": \"phone\" \
1162 \"name\": \"Navigation\", \
1166 \"name\": \"HVAC\", \
1167 \"role\": \"hvac\" \
1170 \"name\": \"Settings\", \
1171 \"role\": \"settings\" \
1174 \"name\": \"Dashboard\", \
1175 \"role\": \"dashboard\" \
1178 \"name\": \"POI\", \
1182 \"name\": \"Mixer\", \
1183 \"role\": \"mixer\" \