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.
21 #include "window_manager.hpp"
22 #include "json_helper.hpp"
26 #include <afb/afb-binding.h>
27 #include <systemd/sd-event.h>
30 typedef struct WMClientCtxt
34 void* lcm_obs_ctx; // POI: LCM
36 WMClientCtxt(const char *appName, const char* appRole)
37 : name(appName), role(appRole), lcm_obs_ctx(nullptr) {};
38 WMClientCtxt() : lcm_obs_ctx(nullptr) {};
40 void set_name_role(const char *appName, const char* appRole)
49 wm::WindowManager wmgr;
51 afb_instance() : wmgr() {}
52 ~afb_instance() = default;
57 struct afb_instance *g_afb_instance;
60 int afb_instance::init()
62 return this->wmgr.init();
65 static int _binding_init()
67 HMI_NOTICE("WinMan ver. %s", WINMAN_VERSION_STRING);
69 g_afb_instance = new afb_instance;
71 if (g_afb_instance->init() == -1)
73 HMI_ERROR("Could not connect to compositor");
77 atexit([] { delete g_afb_instance; });
82 delete g_afb_instance;
83 g_afb_instance = nullptr;
87 static int binding_init (afb_api_t api) noexcept
91 return _binding_init();
93 catch (std::exception &e)
95 HMI_ERROR("Uncaught exception in binding_init(): %s", e.what());
100 static void cbRemoveClientCtxt(void *data)
102 WMClientCtxt *ctxt = (WMClientCtxt *)data;
108 if (!ctxt->name.empty()) {
109 HMI_DEBUG("remove app %s", ctxt->name.c_str());
111 // Policy Manager does not know this app was killed,
112 // so notify it by deactivate request.
113 g_afb_instance->wmgr.api_deactivate_window(
114 ctxt->name.c_str(), ctxt->role.c_str(),
115 [](const char *) {});
117 g_afb_instance->wmgr.removeClient(ctxt->name);
121 g_afb_instance->wmgr.amgr.lcm_clear_context(ctxt->lcm_obs_ctx);
126 static WMClientCtxt* getWMClientContext(afb_req_t req)
128 WMClientCtxt *ctx = static_cast<WMClientCtxt *>(afb_req_context_get(req));
130 // Create Security Context at first time
131 ctx = new WMClientCtxt();
132 HMI_DEBUG("create session without name & role");
133 afb_req_session_set_LOA(req, 1);
134 afb_req_context_set(req, ctx, cbRemoveClientCtxt);
139 static void createSecurityContext(afb_req_t req, const char* appid, const char* role)
141 WMClientCtxt *ctx = getWMClientContext(req);
142 if (ctx && ctx->name.empty())
144 HMI_DEBUG("create session for name=\"%s\" & role=\"role\"", appid, role);
145 ctx->set_name_role(appid, role);
149 void windowmanager_requestsurface(afb_req_t req) noexcept
151 std::lock_guard<std::mutex> guard(binding_m);
152 if (g_afb_instance == nullptr)
154 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
160 const char *a_drawing_name = afb_req_value(req, "drawing_name");
163 afb_req_fail(req, "failed", "Need char const* argument drawing_name");
167 char *appid = afb_req_get_application_id(req);
170 auto ret = g_afb_instance->wmgr.api_request_surface(
171 appid, a_drawing_name);
174 afb_req_fail(req, "failed", ret.unwrap_err());
178 createSecurityContext(req, appid, a_drawing_name);
179 afb_req_success(req, json_object_new_int(ret.unwrap()), "success");
185 afb_req_fail(req, "failed", nullptr);
188 catch (std::exception &e)
190 afb_req_fail_f(req, "failed", "Uncaught exception while calling requestsurface: %s", e.what());
195 void windowmanager_requestsurfacexdg(afb_req_t req) noexcept
197 std::lock_guard<std::mutex> guard(binding_m);
198 if (g_afb_instance == nullptr)
200 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
206 json_object *jreq = afb_req_json(req);
208 json_object *j_drawing_name = nullptr;
209 if (!json_object_object_get_ex(jreq, "drawing_name", &j_drawing_name))
211 afb_req_fail(req, "failed", "Need char const* argument drawing_name");
214 char const *a_drawing_name = json_object_get_string(j_drawing_name);
216 json_object *j_ivi_id = nullptr;
217 if (!json_object_object_get_ex(jreq, "ivi_id", &j_ivi_id))
219 afb_req_fail(req, "failed", "Need char const* argument ivi_id");
222 char const *a_ivi_id = json_object_get_string(j_ivi_id);
223 char *appid = afb_req_get_application_id(req);
226 auto ret = g_afb_instance->wmgr.api_request_surface(
227 appid, a_drawing_name, a_ivi_id);
231 afb_req_fail(req, "failed", ret);
235 createSecurityContext(req, appid, a_drawing_name);
236 afb_req_success(req, NULL, "success");
241 catch (std::exception &e)
243 afb_req_fail_f(req, "failed", "Uncaught exception while calling requestsurfacexdg: %s", e.what());
248 void windowmanager_activatewindow(afb_req_t req) noexcept
250 std::lock_guard<std::mutex> guard(binding_m);
251 if (g_afb_instance == nullptr)
253 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
259 const char *a_drawing_name = afb_req_value(req, "drawing_name");
262 afb_req_fail(req, "failed", "Need char const* argument drawing_name");
266 const char *a_drawing_area = afb_req_value(req, "drawing_area");
269 afb_req_fail(req, "failed", "Need char const* argument drawing_area");
273 char* appid = afb_req_get_application_id(req);
276 g_afb_instance->wmgr.api_activate_window(
277 appid, a_drawing_name, a_drawing_area,
278 [&req](const char *errmsg) {
279 if (errmsg != nullptr)
282 afb_req_fail(req, "failed", errmsg);
285 afb_req_success(req, NULL, "success");
290 catch (std::exception &e)
292 HMI_WARNING("failed: Uncaught exception while calling activatesurface: %s", e.what());
293 g_afb_instance->wmgr.exceptionProcessForTransition();
298 void windowmanager_deactivatewindow(afb_req_t req) noexcept
300 std::lock_guard<std::mutex> guard(binding_m);
301 if (g_afb_instance == nullptr)
303 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
309 const char *a_drawing_name = afb_req_value(req, "drawing_name");
312 afb_req_fail(req, "failed", "Need char const* argument drawing_name");
316 char* appid = afb_req_get_application_id(req);
319 g_afb_instance->wmgr.api_deactivate_window(
320 appid, a_drawing_name,
321 [&req](const char *errmsg) {
322 if (errmsg != nullptr)
325 afb_req_fail(req, "failed", errmsg);
328 afb_req_success(req, NULL, "success");
333 catch (std::exception &e)
335 HMI_WARNING("failed: Uncaught exception while calling deactivatesurface: %s", e.what());
336 g_afb_instance->wmgr.exceptionProcessForTransition();
341 void windowmanager_enddraw(afb_req_t req) noexcept
343 std::lock_guard<std::mutex> guard(binding_m);
344 if (g_afb_instance == nullptr)
346 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
352 const char *a_drawing_name = afb_req_value(req, "drawing_name");
355 afb_req_fail(req, "failed", "Need char const* argument drawing_name");
358 afb_req_success(req, NULL, "success");
360 char* appid = afb_req_get_application_id(req);
363 g_afb_instance->wmgr.api_enddraw(appid, a_drawing_name);
367 catch (std::exception &e)
369 HMI_WARNING("failed: Uncaught exception while calling enddraw: %s", e.what());
370 g_afb_instance->wmgr.exceptionProcessForTransition();
375 void windowmanager_getdisplayinfo_thunk(afb_req_t req) noexcept
377 std::lock_guard<std::mutex> guard(binding_m);
378 if (g_afb_instance == nullptr)
380 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
386 auto ret = g_afb_instance->wmgr.api_get_display_info();
389 afb_req_fail(req, "failed", ret.unwrap_err());
393 afb_req_success(req, ret.unwrap(), "success");
395 catch (std::exception &e)
397 afb_req_fail_f(req, "failed", "Uncaught exception while calling getdisplayinfo: %s", e.what());
402 void windowmanager_getareainfo_thunk(afb_req_t req) noexcept
404 std::lock_guard<std::mutex> guard(binding_m);
405 if (g_afb_instance == nullptr)
407 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
413 json_object *jreq = afb_req_json(req);
415 json_object *j_drawing_name = nullptr;
416 if (!json_object_object_get_ex(jreq, "drawing_name", &j_drawing_name))
418 afb_req_fail(req, "failed", "Need char const* argument drawing_name");
421 char const *a_drawing_name = json_object_get_string(j_drawing_name);
423 auto ret = g_afb_instance->wmgr.api_get_area_info(a_drawing_name);
426 afb_req_fail(req, "failed", ret.unwrap_err());
430 afb_req_success(req, ret.unwrap(), "success");
432 catch (std::exception &e)
434 afb_req_fail_f(req, "failed", "Uncaught exception while calling getareainfo: %s", e.what());
439 void windowmanager_wm_subscribe(afb_req_t req) noexcept
441 std::lock_guard<std::mutex> guard(binding_m);
442 if (g_afb_instance == nullptr)
444 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
450 json_object *jreq = afb_req_json(req);
451 json_object *j = nullptr;
452 if (!json_object_object_get_ex(jreq, "event", &j))
454 afb_req_fail(req, "failed", "Need char const* argument event");
457 int event_id = json_object_get_int(j);
458 int ret = g_afb_instance->wmgr.api_subscribe(req, event_id);
462 afb_req_fail(req, "failed", "Error: afb_req_subscribe()");
465 afb_req_success(req, NULL, "success");
467 catch (std::exception &e)
469 afb_req_fail_f(req, "failed", "Uncaught exception while calling wm_subscribe: %s", e.what());
474 void windowmanager_ping(afb_req_t req) noexcept
476 std::lock_guard<std::mutex> guard(binding_m);
478 if (g_afb_instance == nullptr)
480 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
485 afb_req_success(req, NULL, "success");
489 void windowmanager_debug_terminate(afb_req_t req) noexcept
491 std::lock_guard<std::mutex> guard(binding_m);
492 if (g_afb_instance == nullptr)
494 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
501 if (getenv("WINMAN_DEBUG_TERMINATE") != nullptr)
503 raise(SIGKILL); // afb-daemon kills it's pgroup using TERM, which
504 // doesn't play well with perf
507 afb_req_success(req, NULL, "success");
509 catch (std::exception &e)
511 afb_req_fail_f(req, "failed", "Uncaught exception while calling debug_terminate: %s", e.what());
516 /* AGL Lifecycle Management API */
517 static void lcm_register_activity_observer (afb_req_t req)
519 std::lock_guard<std::mutex> guard(binding_m);
520 if (g_afb_instance == nullptr) {
521 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
525 WMClientCtxt* wc_ctx = getWMClientContext(req);
526 if (wc_ctx == nullptr) {
527 afb_req_fail(req, "failed", "WMClientCtxt cannot create.");
529 // register new activity observer
531 // id: "app id of target to observe",
532 // type: "type of lifecycle, APP, HMI or GUI to observe",
533 // filter: { states which observer wants to know }
535 g_afb_instance->wmgr.amgr.api_register_activity_observer(req, wc_ctx->lcm_obs_ctx);
536 afb_req_success(req, NULL, "success");
540 static void lcm_unregister_activity_observer (afb_req_t req)
542 std::lock_guard<std::mutex> guard(binding_m);
543 if (g_afb_instance == nullptr)
545 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
549 WMClientCtxt* wc_ctx = getWMClientContext(req);
550 if (wc_ctx == nullptr) {
551 afb_req_fail(req, "failed", "WMClientCtxt not exsit.");
555 // unregister activity observer
557 // id: "uniq id of observer"
559 g_afb_instance->wmgr.amgr.api_unregister_activity_observer(req, wc_ctx->lcm_obs_ctx);
561 afb_req_success(req, NULL, "success");
564 static void lcm_get_activity_status(afb_req_t req)
566 std::lock_guard<std::mutex> guard(binding_m);
568 if (g_afb_instance == nullptr) {
569 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
571 json_object *object = afb_req_json(req);
572 json_object *j_target = nullptr;
573 if (!json_object_object_get_ex(object, "target", &j_target)) {
574 afb_req_fail(req, "failed", "Need const char* argument target");
578 const char *target_id = json_object_get_string(j_target);
580 auto ret = g_afb_instance->wmgr.amgr.api_get_activity_status(target_id);
583 afb_req_fail(req, "failed", ret.unwrap_err());
585 afb_req_success(req, ret.unwrap(), "success");
590 const afb_verb_t windowmanager_verbs[] = {
591 { .verb = "requestSurface", .callback = windowmanager_requestsurface },
592 { .verb = "requestSurfaceXDG", .callback = windowmanager_requestsurfacexdg },
593 { .verb = "activateWindow", .callback = windowmanager_activatewindow },
594 { .verb = "deactivateWindow", .callback = windowmanager_deactivatewindow },
595 { .verb = "endDraw", .callback = windowmanager_enddraw },
596 { .verb = "getDisplayInfo", .callback = windowmanager_getdisplayinfo_thunk },
597 { .verb = "getAreaInfo", .callback = windowmanager_getareainfo_thunk },
598 { .verb = "wm_subscribe", .callback = windowmanager_wm_subscribe },
599 { .verb = "ping", .callback = windowmanager_ping },
600 { .verb = "debug_terminate", .callback = windowmanager_debug_terminate },
601 /* AGL Lifecycle Management API */
602 { .verb = "registerActivityObserver", .callback = lcm_register_activity_observer },
603 { .verb = "unregisterActivityObserver", .callback = lcm_unregister_activity_observer },
604 { .verb = "getActivityStatus", .callback = lcm_get_activity_status },
607 extern "C" const afb_binding_t afbBindingExport = {
608 .api = "windowmanager",
609 .specification = "windowmanager",
610 .info = "windowmanager",
611 .verbs = windowmanager_verbs,
613 .init = binding_init,
616 .provide_class = nullptr,
617 .require_class = nullptr,
618 .require_api = nullptr,