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.
20 #include "window_manager.hpp"
21 #include "../include/json.hpp"
22 #include "applist.hpp"
26 #include <systemd/sd-event.h>
32 static const unsigned kTimeOut = 3000000UL; /* 3s */
34 /* DrawingArea name used by "{layout}.{area}" */
35 const char kNameLayoutNormal[] = "normal";
36 const char kNameLayoutSplit[] = "split";
37 const char kNameAreaFull[] = "full";
38 const char kNameAreaMain[] = "main";
39 const char kNameAreaSub[] = "sub";
41 /* Key for json obejct */
42 const char kKeyDrawingName[] = "drawing_name";
43 const char kKeyDrawingArea[] = "drawing_area";
44 const char kKeyDrawingRect[] = "drawing_rect";
45 const char kKeyX[] = "x";
46 const char kKeyY[] = "y";
47 const char kKeyWidth[] = "width";
48 const char kKeyHeight[] = "height";
49 const char kKeyWidthPixel[] = "width_pixel";
50 const char kKeyHeightPixel[] = "height_pixel";
51 const char kKeyWidthMm[] = "width_mm";
52 const char kKeyHeightMm[] = "height_mm";
53 const char kKeyIds[] = "ids";
55 static sd_event_source *g_timer_ev_src = nullptr;
56 static AppList g_app_list;
63 result<json> file_to_json(char const *filename)
66 std::ifstream i(filename);
69 HMI_DEBUG("wm", "Could not open config file, so use default layer information");
70 j = default_layers_json;
80 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);
87 return Err<layer_map>(j.unwrap_err());
89 json jids = j.unwrap();
91 return to_layer_map(jids);
94 static int processTimerHandler(sd_event_source *s, uint64_t usec, void *userdata)
96 HMI_NOTICE("wm", "Time out occurs because the client replys endDraw slow, so revert the request");
97 reinterpret_cast<wm::WindowManager *>(userdata)->timerHandler();
106 WindowManager::WindowManager(wl::display *d)
113 pending_events(false)
115 char const *path_layers_json = getenv("AFM_APP_INSTALL_DIR");
117 if (!path_layers_json)
119 HMI_ERROR("wm", "AFM_APP_INSTALL_DIR is not defined");
120 path = std::string(path_layers_json);
124 path = std::string(path_layers_json) + std::string("/etc/layers.json");
130 auto l = load_layer_map(path.c_str());
133 this->layers = l.unwrap();
137 HMI_ERROR("wm", "%s", l.err().value());
141 catch (std::exception &e)
143 HMI_ERROR("wm", "Loading of configuration failed: %s", e.what());
147 int WindowManager::init()
149 if (!this->display->ok())
154 if (this->layers.mapping.empty())
156 HMI_ERROR("wm", "No surface -> layer mapping loaded");
161 for (int i = Event_Val_Min; i <= Event_Val_Max; i++)
163 map_afb_event[kListEventName[i]] = afb_daemon_make_event(kListEventName[i]);
166 this->display->add_global_handler(
167 "wl_output", [this](wl_registry *r, uint32_t name, uint32_t v) {
168 this->outputs.emplace_back(std::make_unique<wl::output>(r, name, v));
171 this->display->add_global_handler(
172 "ivi_wm", [this](wl_registry *r, uint32_t name, uint32_t v) {
174 std::make_unique<struct compositor::controller>(r, name, v);
176 // Init controller hooks
177 this->controller->chooks = &this->chooks;
179 // This protocol needs the output, so lets just add our mapping here...
180 this->controller->add_proxy_to_id_mapping(
181 this->outputs.back()->proxy.get(),
182 wl_proxy_get_id(reinterpret_cast<struct wl_proxy *>(
183 this->outputs.back()->proxy.get())));
186 this->controller->create_screen(this->outputs.back()->proxy.get());
188 // Set display to controller
189 this->controller->display = this->display;
192 // First level objects
193 this->display->roundtrip();
194 // Second level objects
195 this->display->roundtrip();
196 // Third level objects
197 this->display->roundtrip();
199 return init_layers();
202 int WindowManager::dispatch_pending_events()
204 if (this->pop_pending_events())
206 this->display->dispatch_pending();
212 void WindowManager::set_pending_events()
214 this->pending_events.store(true, std::memory_order_release);
217 result<int> WindowManager::api_request_surface(char const *appid, char const *drawing_name)
219 auto lid = this->layers.get_layer_id(std::string(drawing_name));
223 * register drawing_name as fallback and make it displayed.
225 lid = this->layers.get_layer_id(std::string("Fallback"));
226 HMI_DEBUG("wm", "%s is not registered in layers.json, then fallback as normal app", drawing_name);
229 return Err<int>("Drawing name does not match any role, Fallback is disabled");
233 auto rname = this->lookup_id(drawing_name);
236 // name does not exist yet, allocate surface id...
237 auto id = int(this->id_alloc.generate_id(drawing_name));
238 this->layers.add_surface(id, *lid);
240 // set the main_surface[_name] here and now
241 if (!this->layers.main_surface_name.empty() &&
242 this->layers.main_surface_name == drawing_name)
244 this->layers.main_surface = id;
245 HMI_DEBUG("wm", "Set main_surface id to %u", id);
248 // add client into the db
249 std::string appid_str(appid);
250 std::string role(drawing_name);
251 g_app_list.addClient(appid_str, *lid, id, role);
256 // Check currently registered drawing names if it is already there.
257 return Err<int>("Surface already present");
260 char const *WindowManager::api_request_surface(char const *appid, char const *drawing_name,
265 auto lid = this->layers.get_layer_id(std::string(drawing_name));
266 unsigned sid = std::stol(ivi_id);
271 * register drawing_name as fallback and make it displayed.
273 lid = this->layers.get_layer_id(std::string("Fallback"));
274 HMI_DEBUG("wm", "%s is not registered in layers.json, then fallback as normal app", drawing_name);
277 return "Drawing name does not match any role, Fallback is disabled";
281 auto rname = this->lookup_id(drawing_name);
285 return "Surface already present";
288 // register pair drawing_name and ivi_id
289 this->id_alloc.register_name_id(drawing_name, sid);
290 this->layers.add_surface(sid, *lid);
292 // this surface is already created
293 HMI_DEBUG("wm", "surface_id is %u, layer_id is %u", sid, *lid);
295 this->controller->layers[*lid]->add_surface(sid);
296 this->layout_commit();
298 // add client into the db
299 std::string appid_str(appid);
300 std::string role(drawing_name);
301 g_app_list.addClient(appid_str, *lid, sid, role);
306 void WindowManager::api_activate_surface(char const *appid, char const *drawing_name,
307 char const *drawing_area, const reply_func &reply)
311 std::string id = appid;
312 std::string role = drawing_name;
313 std::string area = drawing_area;
314 Task task = Task::TASK_ALLOCATE;
315 unsigned req_num = 0;
316 WMError ret = WMError::UNKNOWN;
318 ret = this->setRequest(id, role, area, task, &req_num);
320 if(ret != WMError::SUCCESS)
322 HMI_ERROR("wm", errorDescription(ret));
323 reply("Failed to set request");
328 if (req_num != g_app_list.currentRequestNumber())
330 // Add request, then invoked after the previous task is finished
331 HMI_SEQ_DEBUG(req_num, "request is accepted");
338 ret = this->doTransition(req_num);
340 if (ret != WMError::SUCCESS)
343 HMI_SEQ_ERROR(req_num, errorDescription(ret));
344 g_app_list.removeRequest(req_num);
345 this->processNextRequest();
349 void WindowManager::api_deactivate_surface(char const *appid, char const *drawing_name,
350 const reply_func &reply)
357 std::string id = appid;
358 std::string role = drawing_name;
359 std::string area = ""; //drawing_area;
360 Task task = Task::TASK_RELEASE;
361 unsigned req_num = 0;
362 WMError ret = WMError::UNKNOWN;
364 ret = this->setRequest(id, role, area, task, &req_num);
366 if (ret != WMError::SUCCESS)
368 HMI_ERROR("wm", errorDescription(ret));
369 reply("Failed to set request");
374 if (req_num != g_app_list.currentRequestNumber())
376 // Add request, then invoked after the previous task is finished
377 HMI_SEQ_DEBUG(req_num, "request is accepted");
384 ret = this->doTransition(req_num);
386 if (ret != WMError::SUCCESS)
389 HMI_SEQ_ERROR(req_num, errorDescription(ret));
390 g_app_list.removeRequest(req_num);
391 this->processNextRequest();
395 void WindowManager::api_enddraw(char const *appid, char const *drawing_name)
397 std::string id = appid;
398 std::string role = drawing_name;
399 unsigned current_req = g_app_list.currentRequestNumber();
400 bool result = g_app_list.setEndDrawFinished(current_req, id, role);
404 HMI_ERROR("wm", "%s is not in transition state", id.c_str());
408 if (g_app_list.endDrawFullfilled(current_req))
410 // do task for endDraw
412 WMError ret = this->doEndDraw(current_req);
414 if(ret != WMError::SUCCESS)
416 //this->emit_error();
418 this->emitScreenUpdated(current_req);
419 HMI_SEQ_INFO(current_req, "Finish request status: %s", errorDescription(ret));
421 g_app_list.removeRequest(current_req);
423 this->processNextRequest();
427 HMI_SEQ_INFO(current_req, "Wait other App call endDraw");
432 result<json_object *> WindowManager::api_get_display_info()
435 if (!this->controller)
437 return Err<json_object *>("ivi_controller global not available");
441 compositor::size o_size = this->controller->output_size;
442 compositor::size p_size = this->controller->physical_size;
444 json_object *object = json_object_new_object();
445 json_object_object_add(object, kKeyWidthPixel, json_object_new_int(o_size.w));
446 json_object_object_add(object, kKeyHeightPixel, json_object_new_int(o_size.h));
447 json_object_object_add(object, kKeyWidthMm, json_object_new_int(p_size.w));
448 json_object_object_add(object, kKeyHeightMm, json_object_new_int(p_size.h));
450 return Ok<json_object *>(object);
453 result<json_object *> WindowManager::api_get_area_info(char const *drawing_name)
455 HMI_DEBUG("wm", "called");
457 // Check drawing name, surface/layer id
458 auto const &surface_id = this->lookup_id(drawing_name);
461 return Err<json_object *>("Surface does not exist");
464 if (!this->controller->surface_exists(*surface_id))
466 return Err<json_object *>("Surface does not exist in controller!");
469 auto layer_id = this->layers.get_layer_id(*surface_id);
472 return Err<json_object *>("Surface is not on any layer!");
475 auto o_state = *this->layers.get_layout_state(*surface_id);
476 if (o_state == nullptr)
478 return Err<json_object *>("Could not find layer for surface");
481 struct LayoutState &state = *o_state;
482 if ((state.main != *surface_id) && (state.sub != *surface_id))
484 return Err<json_object *>("Surface is inactive");
487 // Set area rectangle
488 compositor::rect area_info = this->area_info[*surface_id];
489 json_object *object = json_object_new_object();
490 json_object_object_add(object, kKeyX, json_object_new_int(area_info.x));
491 json_object_object_add(object, kKeyY, json_object_new_int(area_info.y));
492 json_object_object_add(object, kKeyWidth, json_object_new_int(area_info.w));
493 json_object_object_add(object, kKeyHeight, json_object_new_int(area_info.h));
495 return Ok<json_object *>(object);
498 void WindowManager::api_ping() { this->dispatch_pending_events(); }
500 void WindowManager::send_event(char const *evname, char const *label)
502 HMI_DEBUG("wm", "%s: %s(%s)", __func__, evname, label);
504 json_object *j = json_object_new_object();
505 json_object_object_add(j, kKeyDrawingName, json_object_new_string(label));
507 int ret = afb_event_push(this->map_afb_event[evname], j);
510 HMI_DEBUG("wm", "afb_event_push failed: %m");
514 void WindowManager::send_event(char const *evname, char const *label, char const *area,
515 int x, int y, int w, int h)
517 HMI_DEBUG("wm", "%s: %s(%s, %s) x:%d y:%d w:%d h:%d",
518 __func__, evname, label, area, x, y, w, h);
520 json_object *j_rect = json_object_new_object();
521 json_object_object_add(j_rect, kKeyX, json_object_new_int(x));
522 json_object_object_add(j_rect, kKeyY, json_object_new_int(y));
523 json_object_object_add(j_rect, kKeyWidth, json_object_new_int(w));
524 json_object_object_add(j_rect, kKeyHeight, json_object_new_int(h));
526 json_object *j = json_object_new_object();
527 json_object_object_add(j, kKeyDrawingName, json_object_new_string(label));
528 json_object_object_add(j, kKeyDrawingArea, json_object_new_string(area));
529 json_object_object_add(j, kKeyDrawingRect, j_rect);
531 int ret = afb_event_push(this->map_afb_event[evname], j);
534 HMI_DEBUG("wm", "afb_event_push failed: %m");
541 void WindowManager::surface_created(uint32_t surface_id)
543 auto layer_id = this->layers.get_layer_id(surface_id);
546 HMI_DEBUG("wm", "Newly created surfce %d is not associated with any layer!",
551 HMI_DEBUG("wm", "surface_id is %u, layer_id is %u", surface_id, *layer_id);
553 this->controller->layers[*layer_id]->add_surface(surface_id);
554 this->layout_commit();
557 void WindowManager::surface_removed(uint32_t surface_id)
559 HMI_DEBUG("wm", "surface_id is %u", surface_id);
560 g_app_list.removeSurface(surface_id);
563 void WindowManager::removeClient(const std::string &appid)
565 HMI_DEBUG("wm", "Remove clinet %s from list", appid.c_str());
566 g_app_list.removeClient(appid);
569 void WindowManager::exceptionProcessForTransition()
571 unsigned req_num = g_app_list.currentRequestNumber();
572 HMI_SEQ_NOTICE(req_num, "Process exception handling for request. Remove current request %d", req_num);
573 g_app_list.removeRequest(req_num);
574 HMI_SEQ_NOTICE(g_app_list.currentRequestNumber(), "Process next request if exists");
575 this->processNextRequest();
578 void WindowManager::timerHandler()
580 unsigned req_num = g_app_list.currentRequestNumber();
581 HMI_SEQ_DEBUG(req_num, "Timer expired remove Request");
582 g_app_list.reqDump();
583 g_app_list.removeRequest(req_num);
584 this->processNextRequest();
588 ******* Private Functions *******
591 bool WindowManager::pop_pending_events()
594 return this->pending_events.compare_exchange_strong(
595 x, false, std::memory_order_consume);
598 optional<int> WindowManager::lookup_id(char const *name)
600 return this->id_alloc.lookup(std::string(name));
602 optional<std::string> WindowManager::lookup_name(int id)
604 return this->id_alloc.lookup(id);
610 int WindowManager::init_layers()
612 if (!this->controller)
614 HMI_ERROR("wm", "ivi_controller global not available");
618 if (this->outputs.empty())
620 HMI_ERROR("wm", "no output was set up!");
624 auto &c = this->controller;
626 auto &o = this->outputs.front();
627 auto &s = c->screens.begin()->second;
628 auto &layers = c->layers;
630 // Write output dimensions to ivi controller...
631 c->output_size = compositor::size{uint32_t(o->width), uint32_t(o->height)};
632 c->physical_size = compositor::size{uint32_t(o->physical_width),
633 uint32_t(o->physical_height)};
641 // Quick and dirty setup of layers
642 for (auto const &i : this->layers.mapping)
644 c->layer_create(i.second.layer_id, o->width, o->height);
645 auto &l = layers[i.second.layer_id];
646 l->set_destination_rectangle(0, 0, o->width, o->height);
647 l->set_visibility(1);
648 HMI_DEBUG("wm", "Setting up layer %s (%d) for surface role match \"%s\"",
649 i.second.name.c_str(), i.second.layer_id, i.second.role.c_str());
652 // Add layers to screen
653 s->set_render_order(this->layers.layers);
655 this->layout_commit();
657 this->layers.setupArea(o->width, o->height);
662 void WindowManager::surface_set_layout(int surface_id, const std::string& area)
664 if (!this->controller->surface_exists(surface_id))
666 HMI_ERROR("wm", "Surface %d does not exist", surface_id);
670 auto o_layer_id = this->layers.get_layer_id(surface_id);
674 HMI_ERROR("wm", "Surface %d is not associated with any layer!", surface_id);
678 uint32_t layer_id = *o_layer_id;
680 auto const &layer = this->layers.get_layer(layer_id);
681 auto rect = this->layers.getAreaSize(area);
682 HMI_SEQ_DEBUG(g_app_list.currentRequestNumber(), "%s : x:%d y:%d w:%d h:%d", area.c_str(),
683 rect.x, rect.y, rect.w, rect.h);
684 auto &s = this->controller->surfaces[surface_id];
691 // less-than-0 values refer to MAX + 1 - $VALUE
692 // e.g. MAX is either screen width or height
695 w = this->controller->output_size.w + 1 + w;
699 h = this->controller->output_size.h + 1 + h;
702 HMI_DEBUG("wm", "surface_set_layout for surface %u on layer %u", surface_id,
705 // set destination to the display rectangle
706 s->set_destination_rectangle(x, y, w, h);
708 // update area information
709 this->area_info[surface_id].x = x;
710 this->area_info[surface_id].y = y;
711 this->area_info[surface_id].w = w;
712 this->area_info[surface_id].h = h;
714 HMI_DEBUG("wm", "Surface %u now on layer %u with rect { %d, %d, %d, %d }",
715 surface_id, layer_id, x, y, w, h);
718 void WindowManager::layout_commit()
720 this->controller->commit_changes();
721 this->display->flush();
724 void WindowManager::emit_activated(char const *label)
726 this->send_event(kListEventName[Event_Active], label);
729 void WindowManager::emit_deactivated(char const *label)
731 this->send_event(kListEventName[Event_Inactive], label);
734 void WindowManager::emit_syncdraw(char const *label, char const *area, int x, int y, int w, int h)
736 this->send_event(kListEventName[Event_SyncDraw], label, area, x, y, w, h);
739 void WindowManager::emit_syncdraw(const std::string &role, const std::string &area)
741 compositor::rect rect = this->layers.getAreaSize(area);
742 this->send_event(kListEventName[Event_SyncDraw],
743 role.c_str(), area.c_str(), rect.x, rect.y, rect.w, rect.h);
746 void WindowManager::emit_flushdraw(char const *label)
748 this->send_event(kListEventName[Event_FlushDraw], label);
751 void WindowManager::emit_visible(char const *label, bool is_visible)
753 this->send_event(is_visible ? kListEventName[Event_Visible] : kListEventName[Event_Invisible], label);
756 void WindowManager::emit_invisible(char const *label)
758 return emit_visible(label, false);
761 void WindowManager::emit_visible(char const *label) { return emit_visible(label, true); }
763 void WindowManager::activate(int id)
765 auto ip = this->controller->sprops.find(id);
766 if (ip != this->controller->sprops.end())
768 this->controller->surfaces[id]->set_visibility(1);
770 this->lookup_name(id).value_or("unknown-name").c_str();
773 if ((0 == strcmp(label, "Radio")) ||
774 (0 == strcmp(label, "MediaPlayer")) ||
775 (0 == strcmp(label, "Music")) ||
776 (0 == strcmp(label, "Navigation")))
778 for (auto i = surface_bg.begin(); i != surface_bg.end(); ++i)
783 this->surface_bg.erase(i);
785 // Remove from BG layer (999)
786 HMI_DEBUG("wm", "Remove %s(%d) from BG layer", label, id);
787 this->controller->layers[999]->remove_surface(id);
789 // Add to FG layer (1001)
790 HMI_DEBUG("wm", "Add %s(%d) to FG layer", label, id);
791 this->controller->layers[1001]->add_surface(id);
793 for (int j : this->surface_bg)
795 HMI_DEBUG("wm", "Stored id:%d", j);
802 this->layout_commit();
804 this->emit_visible(label);
805 this->emit_activated(label);
809 void WindowManager::deactivate(int id)
811 auto ip = this->controller->sprops.find(id);
812 if (ip != this->controller->sprops.end())
815 this->lookup_name(id).value_or("unknown-name").c_str();
818 if ((0 == strcmp(label, "Radio")) ||
819 (0 == strcmp(label, "MediaPlayer")) ||
820 (0 == strcmp(label, "Music")) ||
821 (0 == strcmp(label, "Navigation")))
825 this->surface_bg.push_back(id);
827 // Remove from FG layer (1001)
828 HMI_DEBUG("wm", "Remove %s(%d) from FG layer", label, id);
829 this->controller->layers[1001]->remove_surface(id);
831 // Add to BG layer (999)
832 HMI_DEBUG("wm", "Add %s(%d) to BG layer", label, id);
833 this->controller->layers[999]->add_surface(id);
835 for (int j : surface_bg)
837 HMI_DEBUG("wm", "Stored id:%d", j);
842 this->controller->surfaces[id]->set_visibility(0);
846 this->emit_deactivated(label);
847 this->emit_invisible(label);
851 WMError WindowManager::setRequest(const std::string& appid, const std::string &role, const std::string &area,
852 Task task, unsigned* req_num)
854 if (!g_app_list.contains(appid))
856 return WMError::NOT_REGISTERED;
859 auto client = g_app_list.lookUpClient(appid);
864 unsigned current = g_app_list.currentRequestNumber();
865 unsigned requested_num = g_app_list.getRequestNumber(appid);
866 if (requested_num != 0)
868 HMI_SEQ_INFO(requested_num,
869 "%s %s %s request is already queued", appid.c_str(), role.c_str(), area.c_str());
873 WMRequest req = WMRequest(appid, role, area, task);
874 unsigned new_req = g_app_list.addRequest(req);
876 g_app_list.reqDump();
878 HMI_SEQ_DEBUG(current, "%s start sequence with %s, %s", appid.c_str(), role.c_str(), area.c_str());
880 return WMError::SUCCESS;
883 WMError WindowManager::doTransition(unsigned req_num)
885 HMI_SEQ_DEBUG(req_num, "check policy");
886 WMError ret = this->checkPolicy(req_num);
887 if (ret != WMError::SUCCESS)
891 HMI_SEQ_DEBUG(req_num, "Start transition.");
892 ret = this->startTransition(req_num);
896 WMError WindowManager::checkPolicy(unsigned req_num)
901 // get current trigger
904 WMError ret = WMError::LAYOUT_CHANGE_FAIL;
905 auto trigger = g_app_list.getRequest(req_num, &found);
908 ret = WMError::NO_ENTRY;
911 std::string req_area = trigger.area;
913 // >>>> Compatible with current window manager until policy manager coming
914 if (trigger.task == Task::TASK_ALLOCATE)
916 HMI_SEQ_DEBUG(req_num, "Check split or not");
917 const char *msg = this->check_surface_exist(trigger.role.c_str());
921 HMI_SEQ_ERROR(req_num, msg);
922 ret = WMError::LAYOUT_CHANGE_FAIL;
926 auto const &surface_id = this->lookup_id(trigger.role.c_str());
927 auto o_state = *this->layers.get_layout_state(*surface_id);
928 struct LayoutState &state = *o_state;
930 unsigned curernt_sid = state.main;
931 split = this->can_split(state, *surface_id);
935 HMI_SEQ_DEBUG(req_num, "Split happens");
936 // Get current visible role
937 std::string add_role = this->lookup_name(state.main).value();
939 std::string add_area = std::string(kNameLayoutSplit) + "." + std::string(kNameAreaMain);
940 // Change request area
941 req_area = std::string(kNameLayoutSplit) + "." + std::string(kNameAreaSub);
942 HMI_SEQ_NOTICE(req_num, "Change request area from %s to %s, because split happens",
943 trigger.area.c_str(), req_area.c_str());
944 // set another action
945 std::string add_name = g_app_list.getAppID(curernt_sid, add_role, &found);
948 HMI_SEQ_ERROR(req_num, "Couldn't widhdraw with surfaceID : %d", curernt_sid);
949 ret = WMError::NOT_REGISTERED;
952 HMI_SEQ_INFO(req_num, "Additional split app %s, role: %s, area: %s",
953 add_name.c_str(), add_role.c_str(), add_area.c_str());
955 bool end_draw_finished = false;
956 WMAction split_action{
960 TaskVisible::VISIBLE,
962 WMError ret = g_app_list.setAction(req_num, split_action);
963 if (ret != WMError::SUCCESS)
965 HMI_SEQ_ERROR(req_num, "Failed to set action");
968 g_app_list.reqDump();
973 HMI_SEQ_DEBUG(req_num, "split doesn't happen");
976 // Set invisible task(Remove if policy manager finish)
977 ret = this->setInvisibleTask(trigger.role, split);
978 if(ret != WMError::SUCCESS)
980 HMI_SEQ_ERROR(req_num, "Failed to set invisible task: %s", errorDescription(ret));
984 /* get new status from Policy Manager */
985 HMI_SEQ_NOTICE(req_num, "ATM, Policy manager does't exist, then set WMAction as is");
986 if(trigger.role == "HomeScreen")
988 // TODO : Remove when Policy Manager completed
989 HMI_SEQ_NOTICE(req_num, "Hack. This process will be removed. Change HomeScreen code!!");
990 req_area = "fullscreen";
992 TaskVisible task_visible =
993 (trigger.task == Task::TASK_ALLOCATE) ? TaskVisible::VISIBLE : TaskVisible::INVISIBLE;
995 ret = g_app_list.setAction(req_num, trigger.appid, trigger.role, req_area, task_visible);
996 g_app_list.reqDump();
1001 WMError WindowManager::startTransition(unsigned req_num)
1003 bool sync_draw_happen = false;
1005 WMError ret = WMError::SUCCESS;
1006 auto actions = g_app_list.getActions(req_num, &found);
1009 ret = WMError::NO_ENTRY;
1010 HMI_SEQ_ERROR(req_num,
1011 "Window Manager bug :%s : Action is not set", errorDescription(ret));
1015 for (const auto &action : actions)
1017 if (action.visible != TaskVisible::INVISIBLE)
1019 sync_draw_happen = true;
1020 this->emit_syncdraw(action.role, action.area);
1021 /* TODO: emit event for app not subscriber
1022 if(g_app_list.contains(y.appid))
1023 g_app_list.lookUpClient(y.appid)->emit_syncdraw(y.role, y.area); */
1027 if (sync_draw_happen)
1033 // deactivate only, no syncDraw
1034 // Make it deactivate here
1035 for (const auto &x : actions)
1037 if (g_app_list.contains(x.appid))
1039 auto client = g_app_list.lookUpClient(x.appid);
1040 this->deactivate(client->surfaceID(x.role));
1043 ret = NO_LAYOUT_CHANGE;
1048 WMError WindowManager::setInvisibleTask(const std::string &role, bool split)
1050 unsigned req_num = g_app_list.currentRequestNumber();
1051 HMI_SEQ_DEBUG(req_num, "set current visible app to invisible task");
1052 // This task is copied from original actiavete surface
1053 const char *drawing_name = role.c_str();
1054 auto const &surface_id = this->lookup_id(drawing_name);
1055 auto layer_id = this->layers.get_layer_id(*surface_id);
1056 auto o_state = *this->layers.get_layout_state(*surface_id);
1057 struct LayoutState &state = *o_state;
1058 std::string add_name, add_role;
1059 std::string add_area = "";
1061 TaskVisible task_visible = TaskVisible::INVISIBLE;
1062 bool end_draw_finished = true;
1065 for (auto const &l : this->layers.mapping)
1067 if (l.second.layer_id <= *layer_id)
1071 HMI_DEBUG("wm", "debug: main %d , sub : %d", l.second.state.main, l.second.state.sub);
1072 if (l.second.state.main != -1)
1074 //this->deactivate(l.second.state.main);
1075 surface = l.second.state.main;
1076 add_role = *this->id_alloc.lookup(surface);
1077 add_name = g_app_list.getAppID(surface, add_role, &found);
1079 return WMError::NOT_REGISTERED;
1081 HMI_SEQ_INFO(req_num, "Invisible %s", add_name.c_str());
1082 WMAction act{add_name, add_role, add_area, task_visible, end_draw_finished};
1083 g_app_list.setAction(req_num, act);
1084 l.second.state.main = -1;
1087 if (l.second.state.sub != -1)
1089 //this->deactivate(l.second.state.sub);
1090 surface = l.second.state.sub;
1091 add_role = *this->id_alloc.lookup(surface);
1092 add_name = g_app_list.getAppID(surface, add_role, &found);
1095 return WMError::NOT_REGISTERED;
1097 HMI_SEQ_INFO(req_num, "Invisible %s", add_name.c_str());
1098 WMAction act{add_name, add_role, add_area, task_visible, end_draw_finished};
1099 g_app_list.setAction(req_num, act);
1100 l.second.state.sub = -1;
1104 // change current state here, but this is hack
1105 auto layer = this->layers.get_layer(*layer_id);
1107 if (state.main == -1)
1109 HMI_DEBUG("wm", "Layout: %s", kNameLayoutNormal);
1113 if (0 != strcmp(drawing_name, "HomeScreen"))
1117 if (state.sub != *surface_id)
1119 if (state.sub != -1)
1121 //this->deactivate(state.sub);
1124 std::move(*this->id_alloc.lookup(state.sub));
1125 deact_sub.area = add_area;
1126 deact_sub.appid = g_app_list.getAppID(state.sub, deact_sub.role, &found);
1129 HMI_SEQ_ERROR(req_num, "App doesn't exist for role : %s",
1130 deact_sub.role.c_str());
1131 return WMError::NOT_REGISTERED;
1133 deact_sub.visible = task_visible;
1134 deact_sub.end_draw_finished = end_draw_finished;
1135 HMI_SEQ_DEBUG(req_num, "Set invisible task for %s", deact_sub.appid.c_str());
1136 g_app_list.setAction(req_num, deact_sub);
1139 //state = LayoutState{state.main, *surface_id};
1143 HMI_DEBUG("wm", "Layout: %s", kNameLayoutNormal);
1145 //this->surface_set_layout(*surface_id);
1146 if (state.main != *surface_id)
1148 // this->deactivate(state.main);
1149 WMAction deact_main;
1150 deact_main.role = std::move(*this->id_alloc.lookup(state.main));
1152 deact_main.area = add_area;
1153 deact_main.appid = g_app_list.getAppID(state.main, deact_main.role, &found);
1156 HMI_SEQ_DEBUG(req_num, "sub surface ddoesn't exist");
1157 return WMError::NOT_REGISTERED;
1159 deact_main.visible = task_visible;
1160 deact_main.end_draw_finished = end_draw_finished;
1161 HMI_SEQ_DEBUG(req_num, "sub surface doesn't exist");
1162 g_app_list.setAction(req_num, deact_main);
1164 if (state.sub != -1)
1166 if (state.sub != *surface_id)
1168 //this->deactivate(state.sub);
1170 deact_sub.role = std::move(*this->id_alloc.lookup(state.sub));
1172 deact_sub.area = add_area;
1173 deact_sub.appid = g_app_list.getAppID(state.sub, deact_sub.role, &found);
1176 HMI_SEQ_DEBUG(req_num, "sub surface ddoesn't exist");
1177 return WMError::NOT_REGISTERED;
1179 deact_sub.visible = task_visible;
1180 deact_sub.end_draw_finished = end_draw_finished;
1181 HMI_SEQ_DEBUG(req_num, "sub surface doesn't exist");
1182 g_app_list.setAction(req_num, deact_sub);
1185 //state = LayoutState{*surface_id};
1189 return WMError::SUCCESS;
1192 WMError WindowManager::doEndDraw(unsigned req_num)
1196 auto actions = g_app_list.getActions(req_num, &found);
1197 WMError ret = WMError::SUCCESS;
1200 ret = WMError::NO_ENTRY;
1204 HMI_SEQ_INFO(req_num, "do endDraw");
1206 // layout change and make it visible
1207 for (const auto &act : actions)
1210 if(!g_app_list.contains(act.appid)){
1211 ret = WMError::NOT_REGISTERED;
1213 ret = this->layoutChange(act);
1214 if(ret != WMError::SUCCESS)
1216 HMI_SEQ_WARNING(req_num,
1217 "Failed to manipulate surfaces while state change : %s", errorDescription(ret));
1220 ret = this->visibilityChange(act);
1221 if (ret != WMError::SUCCESS)
1223 HMI_SEQ_WARNING(req_num,
1224 "Failed to manipulate surfaces while state change : %s", errorDescription(ret));
1227 HMI_SEQ_DEBUG(req_num, "visible %s", act.role.c_str());
1228 //this->lm_enddraw(act.role.c_str());
1231 // Change current state
1232 this->changeCurrentState(req_num);
1234 HMI_SEQ_INFO(req_num, "emit flushDraw");
1236 for(const auto &act_flush : actions)
1238 if(act_flush.visible != TaskVisible::INVISIBLE)
1240 this->emit_flushdraw(act_flush.role.c_str());
1247 WMError WindowManager::layoutChange(const WMAction &action)
1249 if (action.visible == TaskVisible::INVISIBLE)
1251 // Visibility is not change -> no redraw is required
1252 return WMError::SUCCESS;
1254 auto client = g_app_list.lookUpClient(action.appid);
1255 unsigned surface = client->surfaceID(action.role);
1258 HMI_SEQ_ERROR(g_app_list.currentRequestNumber(),
1259 "client doesn't have surface with role(%s)", action.role.c_str());
1260 return WMError::NOT_REGISTERED;
1263 WMError ret = this->setSurfaceSize(surface, action.area);
1267 WMError WindowManager::visibilityChange(const WMAction &action)
1269 HMI_SEQ_DEBUG(g_app_list.currentRequestNumber(), "Change visibility");
1270 if(!g_app_list.contains(action.appid)){
1271 return WMError::NOT_REGISTERED;
1273 auto client = g_app_list.lookUpClient(action.appid);
1274 unsigned surface = client->surfaceID(action.role);
1277 HMI_SEQ_ERROR(g_app_list.currentRequestNumber(),
1278 "client doesn't have surface with role(%s)", action.role.c_str());
1279 return WMError::NOT_REGISTERED;
1282 if (action.visible != TaskVisible::INVISIBLE)
1284 this->activate(surface); // Layout Manager task
1288 this->deactivate(surface); // Layout Manager task
1290 return WMError::SUCCESS;
1293 WMError WindowManager::setSurfaceSize(unsigned surface, const std::string &area)
1295 this->surface_set_layout(surface, area);
1297 return WMError::SUCCESS;
1300 WMError WindowManager::changeCurrentState(unsigned req_num)
1302 HMI_SEQ_DEBUG(req_num, "Change current layout state");
1303 bool trigger_found = false, action_found = false;
1304 auto trigger = g_app_list.getRequest(req_num, &trigger_found);
1305 auto actions = g_app_list.getActions(req_num, &action_found);
1306 if (!trigger_found || !action_found)
1308 HMI_SEQ_ERROR(req_num, "Action not found");
1309 return WMError::LAYOUT_CHANGE_FAIL;
1312 // Layout state reset
1313 struct LayoutState reset_state{-1, -1};
1314 HMI_SEQ_DEBUG(req_num,"Reset layout state");
1315 for (const auto &action : actions)
1317 if(!g_app_list.contains(action.appid)){
1318 return WMError::NOT_REGISTERED;
1320 auto client = g_app_list.lookUpClient(action.appid);
1321 auto pCurState = *this->layers.get_layout_state((int)client->surfaceID(action.role));
1322 if(pCurState == nullptr)
1324 HMI_SEQ_ERROR(req_num, "Counldn't find current status");
1327 pCurState->main = reset_state.main;
1328 pCurState->sub = reset_state.sub;
1331 HMI_SEQ_DEBUG(req_num, "Change state");
1332 for (const auto &action : actions)
1334 auto client = g_app_list.lookUpClient(action.appid);
1335 auto pLayerCurState = *this->layers.get_layout_state((int)client->surfaceID(action.role));
1336 if (pLayerCurState == nullptr)
1338 HMI_SEQ_ERROR(req_num, "Counldn't find current status");
1343 if (action.visible != TaskVisible::INVISIBLE)
1345 surface = (int)client->surfaceID(action.role);
1346 HMI_SEQ_INFO(req_num, "Change %s surface : %d, state visible area : %s",
1347 action.role.c_str(), surface, action.area.c_str());
1348 // visible == true -> layout changes
1349 if(action.area == "normal.full" || action.area == "split.main")
1351 pLayerCurState->main = surface;
1353 else if (action.area == "split.sub")
1355 pLayerCurState->sub = surface;
1360 pLayerCurState->main = surface;
1365 return WMError::SUCCESS;
1368 void WindowManager::emitScreenUpdated(unsigned req_num)
1371 HMI_SEQ_DEBUG(req_num, "emit screen updated");
1373 auto actions = g_app_list.getActions(req_num, &found);
1375 // create json object
1376 json_object *j = json_object_new_object();
1377 json_object *jarray = json_object_new_array();
1379 for(const auto& action: actions)
1381 if(action.visible != TaskVisible::INVISIBLE)
1383 json_object_array_add(jarray, json_object_new_string(action.appid.c_str()));
1386 json_object_object_add(j, kKeyIds, jarray);
1387 HMI_SEQ_INFO(req_num, "Visible app: %s", json_object_get_string(j));
1389 int ret = afb_event_push(
1390 this->map_afb_event[kListEventName[Event_ScreenUpdated]], j);
1393 HMI_DEBUG("wm", "afb_event_push failed: %m");
1395 json_object_put(jarray);
1398 void WindowManager::setTimer()
1400 HMI_SEQ_DEBUG(g_app_list.currentRequestNumber(), "Timer set activate");
1401 if (g_timer_ev_src == nullptr)
1403 // firsttime set into sd_event
1404 int ret = sd_event_add_time(afb_daemon_get_event_loop(), &g_timer_ev_src,
1405 CLOCK_REALTIME, time(NULL) * (1000000UL) + kTimeOut, 1, processTimerHandler, this);
1408 HMI_ERROR("wm", "Could't set timer");
1413 // update timer limitation after second time
1414 sd_event_source_set_time(g_timer_ev_src, time(NULL) * (1000000UL) + kTimeOut);
1415 sd_event_source_set_enabled(g_timer_ev_src, SD_EVENT_ONESHOT);
1419 void WindowManager::stopTimer()
1421 unsigned req_num = g_app_list.currentRequestNumber();
1422 HMI_SEQ_DEBUG(req_num, "Timer stop");
1423 int rc = sd_event_source_set_enabled(g_timer_ev_src, SD_EVENT_OFF);
1426 HMI_SEQ_ERROR(req_num, "Timer stop failed");
1430 void WindowManager::processNextRequest()
1433 g_app_list.reqDump();
1434 unsigned req_num = g_app_list.currentRequestNumber();
1435 if (g_app_list.haveRequest())
1437 HMI_SEQ_DEBUG(req_num, "Process next request");
1438 WMError rc = doTransition(req_num);
1439 if (rc != WMError::SUCCESS)
1441 HMI_SEQ_ERROR(req_num, errorDescription(rc));
1446 HMI_SEQ_DEBUG(req_num, "Nothing Request. Waiting Request");
1450 const char *WindowManager::check_surface_exist(const char *drawing_name)
1452 auto const &surface_id = this->lookup_id(drawing_name);
1455 return "Surface does not exist";
1458 if (!this->controller->surface_exists(*surface_id))
1460 return "Surface does not exist in controller!";
1463 auto layer_id = this->layers.get_layer_id(*surface_id);
1467 return "Surface is not on any layer!";
1470 auto o_state = *this->layers.get_layout_state(*surface_id);
1472 if (o_state == nullptr)
1474 return "Could not find layer for surface";
1477 HMI_DEBUG("wm", "surface %d is detected", *surface_id);
1481 bool WindowManager::can_split(struct LayoutState const &state, int new_id)
1483 if (state.main != -1 && state.main != new_id)
1485 auto new_id_layer = this->layers.get_layer_id(new_id).value();
1486 auto current_id_layer = this->layers.get_layer_id(state.main).value();
1488 // surfaces are on separate layers, don't bother.
1489 if (new_id_layer != current_id_layer)
1494 std::string const &new_id_str = this->lookup_name(new_id).value();
1495 std::string const &cur_id_str = this->lookup_name(state.main).value();
1497 auto const &layer = this->layers.get_layer(new_id_layer);
1499 HMI_DEBUG("wm", "layer info name: %s", layer->name.c_str());
1501 if (layer->layouts.empty())
1506 for (auto i = layer->layouts.cbegin(); i != layer->layouts.cend(); i++)
1508 HMI_DEBUG("wm", "%d main_match '%s'", new_id_layer, i->main_match.c_str());
1509 auto rem = std::regex(i->main_match);
1510 if (std::regex_match(cur_id_str, rem))
1512 // build the second one only if the first already matched
1513 HMI_DEBUG("wm", "%d sub_match '%s'", new_id_layer, i->sub_match.c_str());
1514 auto res = std::regex(i->sub_match);
1515 if (std::regex_match(new_id_str, res))
1517 HMI_DEBUG("wm", "layout matched!");
1530 void controller_hooks::surface_created(uint32_t surface_id)
1532 this->app->surface_created(surface_id);
1535 void controller_hooks::surface_removed(uint32_t surface_id)
1537 this->app->surface_removed(surface_id);
1540 void controller_hooks::surface_visibility(uint32_t /*surface_id*/,
1543 void controller_hooks::surface_destination_rectangle(uint32_t /*surface_id*/,