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 WMClientCtxt(const char *appName, const char* appRole)
43 wm::WindowManager wmgr;
45 afb_instance() : wmgr() {}
46 ~afb_instance() = default;
51 struct afb_instance *g_afb_instance;
54 int afb_instance::init()
56 return this->wmgr.init();
61 HMI_NOTICE("WinMan ver. %s", WINMAN_VERSION_STRING);
63 g_afb_instance = new afb_instance;
65 if (g_afb_instance->init() == -1)
67 HMI_ERROR("Could not connect to compositor");
71 atexit([] { delete g_afb_instance; });
76 delete g_afb_instance;
77 g_afb_instance = nullptr;
81 int binding_init() noexcept
85 return _binding_init();
87 catch (std::exception &e)
89 HMI_ERROR("Uncaught exception in binding_init(): %s", e.what());
94 static void cbRemoveClientCtxt(void *data)
96 WMClientCtxt *ctxt = (WMClientCtxt *)data;
101 HMI_DEBUG("remove app %s", ctxt->name.c_str());
103 // Policy Manager does not know this app was killed,
104 // so notify it by deactivate request.
105 g_afb_instance->wmgr.api_deactivate_surface(
106 ctxt->name.c_str(), ctxt->role.c_str(),
107 [](const char *) {});
109 g_afb_instance->wmgr.removeClient(ctxt->name);
113 static void createSecurityContext(afb_req req, const char* appid, const char* role)
115 WMClientCtxt *ctxt = (WMClientCtxt *)afb_req_context_get(req);
118 // Create Security Context at first time
119 const char *new_role = g_afb_instance->wmgr.convertRoleOldToNew(role);
120 WMClientCtxt *ctxt = new WMClientCtxt(appid, new_role);
121 HMI_DEBUG("create session for %s", ctxt->name.c_str());
122 afb_req_session_set_LOA(req, 1);
123 afb_req_context_set(req, ctxt, cbRemoveClientCtxt);
127 void windowmanager_requestsurface(afb_req req) noexcept
129 std::lock_guard<std::mutex> guard(binding_m);
131 if (g_afb_instance == nullptr)
133 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
139 const char *a_drawing_name = afb_req_value(req, "drawing_name");
142 afb_req_fail(req, "failed", "Need char const* argument drawing_name");
146 const char *appid = afb_req_get_application_id(req);
147 auto ret = g_afb_instance->wmgr.api_request_surface(
148 appid, a_drawing_name);
151 afb_req_fail(req, "failed", ret.unwrap_err());
155 createSecurityContext(req, appid, a_drawing_name);
157 afb_req_success(req, json_object_new_int(ret.unwrap()), "success");
159 catch (std::exception &e)
161 afb_req_fail_f(req, "failed", "Uncaught exception while calling requestsurface: %s", e.what());
166 void windowmanager_requestsurfacexdg(afb_req req) noexcept
168 std::lock_guard<std::mutex> guard(binding_m);
170 if (g_afb_instance == nullptr)
172 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
178 json_object *jreq = afb_req_json(req);
180 json_object *j_drawing_name = nullptr;
181 if (!json_object_object_get_ex(jreq, "drawing_name", &j_drawing_name))
183 afb_req_fail(req, "failed", "Need char const* argument drawing_name");
186 char const *a_drawing_name = json_object_get_string(j_drawing_name);
188 json_object *j_ivi_id = nullptr;
189 if (!json_object_object_get_ex(jreq, "ivi_id", &j_ivi_id))
191 afb_req_fail(req, "failed", "Need char const* argument ivi_id");
194 char const *a_ivi_id = json_object_get_string(j_ivi_id);
195 char const *appid = afb_req_get_application_id(req);
196 auto ret = g_afb_instance->wmgr.api_request_surface(
197 appid, a_drawing_name, a_ivi_id);
201 afb_req_fail(req, "failed", ret);
205 createSecurityContext(req, appid, a_drawing_name);
207 afb_req_success(req, NULL, "success");
209 catch (std::exception &e)
211 afb_req_fail_f(req, "failed", "Uncaught exception while calling requestsurfacexdg: %s", e.what());
216 void windowmanager_setrole(afb_req req) noexcept
218 std::lock_guard<std::mutex> guard(binding_m);
219 if (g_afb_instance == nullptr)
221 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
226 char const *appid = afb_req_get_application_id(req);
227 json_object *jreq = afb_req_json(req);
229 json_object *j_role = nullptr;
230 if (!json_object_object_get_ex(jreq, "role", &j_role))
232 afb_req_fail(req, "failed", "Need char const* argument role");
235 char const *a_role = json_object_get_string(j_role);
237 auto ret = g_afb_instance->wmgr.api_set_role(appid, a_role);
240 afb_req_fail(req, "failed", "Couldn't register");
244 createSecurityContext(req, appid, a_role);
246 afb_req_success(req, NULL, "success");
248 catch (std::exception &e)
250 afb_req_fail_f(req, "failed", "Uncaught exception while calling requestsurfacexdg: %s", e.what());
255 void windowmanager_activatewindow(afb_req req) noexcept
257 std::lock_guard<std::mutex> guard(binding_m);
259 if (g_afb_instance == nullptr)
261 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
267 const char *a_drawing_name = afb_req_value(req, "drawing_name");
270 afb_req_fail(req, "failed", "Need char const* argument drawing_name");
274 const char *a_drawing_area = afb_req_value(req, "drawing_area");
277 afb_req_fail(req, "failed", "Need char const* argument drawing_area");
281 g_afb_instance->wmgr.api_activate_surface(
282 afb_req_get_application_id(req),
283 a_drawing_name, a_drawing_area,
284 [&req](const char *errmsg) {
285 if (errmsg != nullptr)
288 afb_req_fail(req, "failed", errmsg);
291 afb_req_success(req, NULL, "success");
294 catch (std::exception &e)
296 HMI_WARNING("failed: Uncaught exception while calling activatesurface: %s", e.what());
297 g_afb_instance->wmgr.exceptionProcessForTransition();
302 void windowmanager_deactivatewindow(afb_req req) noexcept
304 std::lock_guard<std::mutex> guard(binding_m);
306 if (g_afb_instance == nullptr)
308 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
314 const char *a_drawing_name = afb_req_value(req, "drawing_name");
317 afb_req_fail(req, "failed", "Need char const* argument drawing_name");
321 g_afb_instance->wmgr.api_deactivate_surface(
322 afb_req_get_application_id(req), a_drawing_name,
323 [&req](const char *errmsg) {
324 if (errmsg != nullptr)
327 afb_req_fail(req, "failed", errmsg);
330 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 req) noexcept
343 std::lock_guard<std::mutex> guard(binding_m);
345 if (g_afb_instance == nullptr)
347 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
353 const char *a_drawing_name = afb_req_value(req, "drawing_name");
356 afb_req_fail(req, "failed", "Need char const* argument drawing_name");
359 afb_req_success(req, NULL, "success");
361 g_afb_instance->wmgr.api_enddraw(
362 afb_req_get_application_id(req), a_drawing_name);
364 catch (std::exception &e)
366 HMI_WARNING("failed: Uncaught exception while calling enddraw: %s", e.what());
367 g_afb_instance->wmgr.exceptionProcessForTransition();
372 void windowmanager_getdisplayinfo_thunk(afb_req req) noexcept
374 std::lock_guard<std::mutex> guard(binding_m);
376 if (g_afb_instance == nullptr)
378 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
384 auto ret = g_afb_instance->wmgr.api_get_display_info();
387 afb_req_fail(req, "failed", ret.unwrap_err());
391 afb_req_success(req, ret.unwrap(), "success");
393 catch (std::exception &e)
395 afb_req_fail_f(req, "failed", "Uncaught exception while calling getdisplayinfo: %s", e.what());
400 void windowmanager_getareainfo_thunk(afb_req req) noexcept
402 std::lock_guard<std::mutex> guard(binding_m);
404 if (g_afb_instance == nullptr)
406 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
412 json_object *jreq = afb_req_json(req);
414 json_object *j_drawing_name = nullptr;
415 if (!json_object_object_get_ex(jreq, "drawing_name", &j_drawing_name))
417 afb_req_fail(req, "failed", "Need char const* argument drawing_name");
420 char const *a_drawing_name = json_object_get_string(j_drawing_name);
422 auto ret = g_afb_instance->wmgr.api_get_area_info(a_drawing_name);
425 afb_req_fail(req, "failed", ret.unwrap_err());
429 afb_req_success(req, ret.unwrap(), "success");
431 catch (std::exception &e)
433 afb_req_fail_f(req, "failed", "Uncaught exception while calling getareainfo: %s", e.what());
438 void windowmanager_wm_subscribe(afb_req req) noexcept
440 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_type = json_object_get_int(j);
458 const char *event_name = g_afb_instance->wmgr.kListEventName[event_type];
459 struct afb_event event = g_afb_instance->wmgr.map_afb_event[event_name];
460 int ret = afb_req_subscribe(req, event);
463 afb_req_fail(req, "failed", "Error: afb_req_subscribe()");
466 afb_req_success(req, NULL, "success");
468 catch (std::exception &e)
470 afb_req_fail_f(req, "failed", "Uncaught exception while calling wm_subscribe: %s", e.what());
475 void windowmanager_list_drawing_names(afb_req req) noexcept
477 std::lock_guard<std::mutex> guard(binding_m);
479 /* if (g_afb_instance == nullptr)
481 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
488 nlohmann::json j = g_afb_instance->wmgr.id_alloc.name2id;
489 auto ret = wm::Ok(json_tokener_parse(j.dump().c_str()));
492 afb_req_fail(req, "failed", ret.unwrap_err());
496 afb_req_success(req, ret.unwrap(), "success");
498 catch (std::exception &e)
500 afb_req_fail_f(req, "failed", "Uncaught exception while calling list_drawing_names: %s", e.what());
505 void windowmanager_ping(afb_req req) noexcept
507 std::lock_guard<std::mutex> guard(binding_m);
509 if (g_afb_instance == nullptr)
511 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
516 afb_req_success(req, NULL, "success");
520 void windowmanager_debug_status(afb_req req) noexcept
522 std::lock_guard<std::mutex> guard(binding_m);
524 /* if (g_afb_instance == nullptr)
526 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
533 json_object *jr = json_object_new_object();
534 json_object_object_add(jr, "surfaces",
535 to_json(g_afb_instance->wmgr.controller->sprops));
536 json_object_object_add(jr, "layers", to_json(g_afb_instance->wmgr.controller->lprops));
538 afb_req_success(req, jr, "success");
540 catch (std::exception &e)
542 afb_req_fail_f(req, "failed", "Uncaught exception while calling debug_status: %s", e.what());
547 void windowmanager_debug_layers(afb_req req) noexcept
549 std::lock_guard<std::mutex> guard(binding_m);
551 /* if (g_afb_instance == nullptr)
553 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
559 auto ret = wm::Ok(json_tokener_parse(g_afb_instance->wmgr.layers.to_json().dump().c_str()));
561 afb_req_success(req, ret, "success");
563 catch (std::exception &e)
565 afb_req_fail_f(req, "failed", "Uncaught exception while calling debug_layers: %s", e.what());
570 void windowmanager_debug_surfaces(afb_req req) noexcept
572 std::lock_guard<std::mutex> guard(binding_m);
574 /* if (g_afb_instance == nullptr)
576 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
583 auto ret = wm::Ok(to_json(g_afb_instance->wmgr.controller->sprops));
586 afb_req_fail(req, "failed", ret.unwrap_err());
590 afb_req_success(req, ret.unwrap(), "success");
592 catch (std::exception &e)
594 afb_req_fail_f(req, "failed", "Uncaught exception while calling debug_surfaces: %s", e.what());
599 void windowmanager_debug_terminate(afb_req req) noexcept
601 std::lock_guard<std::mutex> guard(binding_m);
603 if (g_afb_instance == nullptr)
605 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
612 if (getenv("WINMAN_DEBUG_TERMINATE") != nullptr)
614 raise(SIGKILL); // afb-daemon kills it's pgroup using TERM, which
615 // doesn't play well with perf
618 afb_req_success(req, NULL, "success");
620 catch (std::exception &e)
622 afb_req_fail_f(req, "failed", "Uncaught exception while calling debug_terminate: %s", e.what());
627 const struct afb_verb_v2 windowmanager_verbs[] = {
628 {"requestSurface", windowmanager_requestsurface, nullptr, nullptr, AFB_SESSION_NONE},
629 {"requestSurfaceXdg", windowmanager_requestsurfacexdg, nullptr, nullptr, AFB_SESSION_NONE},
630 {"setRole", windowmanager_setrole, nullptr, nullptr, AFB_SESSION_NONE},
631 {"activateWindow", windowmanager_activatewindow, nullptr, nullptr, AFB_SESSION_NONE},
632 {"deactivateWindow", windowmanager_deactivatewindow, nullptr, nullptr, AFB_SESSION_NONE},
633 {"endDraw", windowmanager_enddraw, nullptr, nullptr, AFB_SESSION_NONE},
634 {"getDisplayInfo", windowmanager_getdisplayinfo_thunk, nullptr, nullptr, AFB_SESSION_NONE},
635 {"getAreaInfo", windowmanager_getareainfo_thunk, nullptr, nullptr, AFB_SESSION_NONE},
636 {"wm_subscribe", windowmanager_wm_subscribe, nullptr, nullptr, AFB_SESSION_NONE},
637 {"list_drawing_names", windowmanager_list_drawing_names, nullptr, nullptr, AFB_SESSION_NONE},
638 {"ping", windowmanager_ping, nullptr, nullptr, AFB_SESSION_NONE},
639 {"debug_status", windowmanager_debug_status, nullptr, nullptr, AFB_SESSION_NONE},
640 {"debug_layers", windowmanager_debug_layers, nullptr, nullptr, AFB_SESSION_NONE},
641 {"debug_surfaces", windowmanager_debug_surfaces, nullptr, nullptr, AFB_SESSION_NONE},
642 {"debug_terminate", windowmanager_debug_terminate, nullptr, nullptr, AFB_SESSION_NONE},
645 extern "C" const struct afb_binding_v2 afbBindingV2 = {
646 "windowmanager", nullptr, nullptr, windowmanager_verbs, nullptr, binding_init, nullptr, 0};