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.
24 #include "json_helper.hpp"
26 #include "wayland_ivi_wm.hpp"
30 #include <afb/afb-binding.h>
31 #include <systemd/sd-event.h>
34 typedef struct WMClientCtxt
38 WMClientCtxt(const char *appName, const char* appRole)
47 std::unique_ptr<wl::display> display;
50 afb_instance() : display{new wl::display}, app{this->display.get()} {}
55 struct afb_instance *g_afb_instance;
58 int afb_instance::init()
60 return this->app.init();
63 int display_event_callback(sd_event_source *evs, int /*fd*/, uint32_t events,
68 if ((events & EPOLLHUP) != 0)
70 HMI_ERROR("wm", "The compositor hung up, dying now.");
71 delete g_afb_instance;
72 g_afb_instance = nullptr;
76 if ((events & EPOLLIN) != 0u)
79 STN(display_read_events);
80 g_afb_instance->app.display->read_events();
81 g_afb_instance->app.set_pending_events();
84 // We want do dispatch pending wayland events from within
86 STN(winman_ping_api_call);
87 afb_service_call("windowmanager", "ping", json_object_new_object(),
88 [](void *c, int st, json_object *j) {
89 STN(winman_ping_api_call_return);
98 sd_event_source_unref(evs);
99 if (getenv("WINMAN_EXIT_ON_HANGUP") != nullptr)
108 HMI_NOTICE("wm", "WinMan ver. %s", WINMAN_VERSION_STRING);
110 if (g_afb_instance != nullptr)
112 HMI_ERROR("wm", "Wayland context already initialized?");
116 if (getenv("XDG_RUNTIME_DIR") == nullptr)
118 HMI_ERROR("wm", "Environment variable XDG_RUNTIME_DIR not set");
123 // wait until wayland compositor starts up.
125 g_afb_instance = new afb_instance;
126 while (!g_afb_instance->display->ok())
131 HMI_ERROR("wm", "Could not connect to compositor");
134 HMI_ERROR("wm", "Wait to start weston ...");
136 delete g_afb_instance;
137 g_afb_instance = new afb_instance;
141 if (g_afb_instance->init() == -1)
143 HMI_ERROR("wm", "Could not connect to compositor");
148 int ret = sd_event_add_io(afb_daemon_get_event_loop(), nullptr,
149 g_afb_instance->display->get_fd(), EPOLLIN,
150 display_event_callback, g_afb_instance);
153 HMI_ERROR("wm", "Could not initialize afb_instance event handler: %d", -ret);
158 atexit([] { delete g_afb_instance; });
163 delete g_afb_instance;
164 g_afb_instance = nullptr;
168 int binding_init() noexcept
172 return _binding_init();
174 catch (std::exception &e)
176 HMI_ERROR("wm", "Uncaught exception in binding_init(): %s", e.what());
181 static bool checkFirstReq(afb_req req)
183 WMClientCtxt *ctxt = (WMClientCtxt *)afb_req_context_get(req);
184 return (ctxt) ? false : true;
187 static void cbRemoveClientCtxt(void *data)
189 WMClientCtxt *ctxt = (WMClientCtxt *)data;
194 HMI_DEBUG("wm", "remove app %s", ctxt->name.c_str());
195 // Lookup surfaceID and remove it because App is dead.
196 auto pSid = g_afb_instance->app.id_alloc.lookup(ctxt->role.c_str());
200 g_afb_instance->app.id_alloc.remove_id(sid);
201 g_afb_instance->app.layers.remove_surface(sid);
202 g_afb_instance->app.controller->sprops.erase(sid);
203 g_afb_instance->app.controller->surfaces.erase(sid);
204 HMI_DEBUG("wm", "delete surfaceID %d", sid);
209 void windowmanager_requestsurface(afb_req req) noexcept
211 std::lock_guard<std::mutex> guard(binding_m);
215 if (g_afb_instance == nullptr)
217 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
223 const char *a_drawing_name = afb_req_value(req, "drawing_name");
226 afb_req_fail(req, "failed", "Need char const* argument drawing_name");
230 /* Create Security Context */
231 bool isFirstReq = checkFirstReq(req);
234 WMClientCtxt *ctxt = (WMClientCtxt *)afb_req_context_get(req);
235 HMI_DEBUG("wm", "You're %s.", ctxt->name.c_str());
236 if (ctxt->name != std::string(a_drawing_name))
238 afb_req_fail_f(req, "failed", "Don't request with other name: %s for now", a_drawing_name);
239 HMI_DEBUG("wm", "Don't request with other name: %s for now", a_drawing_name);
244 auto ret = g_afb_instance->app.api_request_surface(a_drawing_name);
248 WMClientCtxt *ctxt = new WMClientCtxt(afb_req_get_application_id(req), a_drawing_name);
249 HMI_DEBUG("wm", "create session for %s", ctxt->name.c_str());
250 afb_req_session_set_LOA(req, 1);
251 afb_req_context_set(req, ctxt, cbRemoveClientCtxt);
255 HMI_DEBUG("wm", "session already created for %s", a_drawing_name);
260 afb_req_fail(req, "failed", ret.unwrap_err());
264 afb_req_success(req, json_object_new_int(ret.unwrap()), "success");
266 catch (std::exception &e)
268 afb_req_fail_f(req, "failed", "Uncaught exception while calling requestsurface: %s", e.what());
273 void windowmanager_requestsurfacexdg(afb_req req) noexcept
275 std::lock_guard<std::mutex> guard(binding_m);
279 if (g_afb_instance == nullptr)
281 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
287 json_object *jreq = afb_req_json(req);
289 json_object *j_drawing_name = nullptr;
290 if (!json_object_object_get_ex(jreq, "drawing_name", &j_drawing_name))
292 afb_req_fail(req, "failed", "Need char const* argument drawing_name");
295 char const *a_drawing_name = json_object_get_string(j_drawing_name);
297 json_object *j_ivi_id = nullptr;
298 if (!json_object_object_get_ex(jreq, "ivi_id", &j_ivi_id))
300 afb_req_fail(req, "failed", "Need char const* argument ivi_id");
303 char const *a_ivi_id = json_object_get_string(j_ivi_id);
305 auto ret = g_afb_instance->app.api_request_surface(a_drawing_name, a_ivi_id);
308 afb_req_fail(req, "failed", ret);
312 afb_req_success(req, NULL, "success");
314 catch (std::exception &e)
316 afb_req_fail_f(req, "failed", "Uncaught exception while calling requestsurfacexdg: %s", e.what());
321 void windowmanager_activatesurface(afb_req req) noexcept
323 std::lock_guard<std::mutex> guard(binding_m);
327 if (g_afb_instance == nullptr)
329 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
335 const char *a_drawing_name = afb_req_value(req, "drawing_name");
338 afb_req_fail(req, "failed", "Need char const* argument drawing_name");
342 const char *a_drawing_area = afb_req_value(req, "drawing_area");
345 afb_req_fail(req, "failed", "Need char const* argument drawing_area");
349 g_afb_instance->app.api_activate_surface(a_drawing_name, a_drawing_area,
350 [&req](const char *errmsg) {
351 if (errmsg != nullptr)
353 HMI_ERROR("wm", errmsg);
354 afb_req_fail(req, "failed", errmsg);
357 afb_req_success(req, NULL, "success");
360 catch (std::exception &e)
362 HMI_WARNING("wm", "failed", "Uncaught exception while calling activatesurface: %s", e.what());
367 void windowmanager_deactivatesurface(afb_req req) noexcept
369 std::lock_guard<std::mutex> guard(binding_m);
373 if (g_afb_instance == nullptr)
375 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
381 const char *a_drawing_name = afb_req_value(req, "drawing_name");
384 afb_req_fail(req, "failed", "Need char const* argument drawing_name");
388 g_afb_instance->app.api_deactivate_surface(a_drawing_name,
389 [&req](const char *errmsg) {
390 if (errmsg != nullptr)
392 HMI_ERROR("wm", errmsg);
393 afb_req_fail(req, "failed", errmsg);
396 afb_req_success(req, NULL, "success");
399 catch (std::exception &e)
401 HMI_WARNING("wm", "Uncaught exception while calling deactivatesurface: %s", e.what());
406 void windowmanager_enddraw(afb_req req) noexcept
408 std::lock_guard<std::mutex> guard(binding_m);
412 if (g_afb_instance == nullptr)
414 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
420 const char *a_drawing_name = afb_req_value(req, "drawing_name");
423 afb_req_fail(req, "failed", "Need char const* argument drawing_name");
426 afb_req_success(req, NULL, "success");
428 g_afb_instance->app.api_enddraw(a_drawing_name);
430 catch (std::exception &e)
432 HMI_WARNING("wm", "failed", "Uncaught exception while calling enddraw: %s", e.what());
437 void windowmanager_getdisplayinfo_thunk(afb_req req) noexcept
439 std::lock_guard<std::mutex> guard(binding_m);
443 if (g_afb_instance == nullptr)
445 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
451 auto ret = g_afb_instance->app.api_get_display_info();
454 afb_req_fail(req, "failed", ret.unwrap_err());
458 afb_req_success(req, ret.unwrap(), "success");
460 catch (std::exception &e)
462 afb_req_fail_f(req, "failed", "Uncaught exception while calling getdisplayinfo: %s", e.what());
467 void windowmanager_getareainfo_thunk(afb_req req) noexcept
469 std::lock_guard<std::mutex> guard(binding_m);
473 if (g_afb_instance == nullptr)
475 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
481 json_object *jreq = afb_req_json(req);
483 json_object *j_drawing_name = nullptr;
484 if (!json_object_object_get_ex(jreq, "drawing_name", &j_drawing_name))
486 afb_req_fail(req, "failed", "Need char const* argument drawing_name");
489 char const *a_drawing_name = json_object_get_string(j_drawing_name);
491 auto ret = g_afb_instance->app.api_get_area_info(a_drawing_name);
494 afb_req_fail(req, "failed", ret.unwrap_err());
498 afb_req_success(req, ret.unwrap(), "success");
500 catch (std::exception &e)
502 afb_req_fail_f(req, "failed", "Uncaught exception while calling getareainfo: %s", e.what());
507 void windowmanager_wm_subscribe(afb_req req) noexcept
509 std::lock_guard<std::mutex> guard(binding_m);
513 if (g_afb_instance == nullptr)
515 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
521 json_object *jreq = afb_req_json(req);
522 json_object *j = nullptr;
523 if (!json_object_object_get_ex(jreq, "event", &j))
525 afb_req_fail(req, "failed", "Need char const* argument event");
528 int event_type = json_object_get_int(j);
529 const char *event_name = g_afb_instance->app.kListEventName[event_type];
530 struct afb_event event = g_afb_instance->app.map_afb_event[event_name];
531 int ret = afb_req_subscribe(req, event);
534 afb_req_fail(req, "failed", "Error: afb_req_subscribe()");
537 afb_req_success(req, NULL, "success");
539 catch (std::exception &e)
541 afb_req_fail_f(req, "failed", "Uncaught exception while calling wm_subscribe: %s", e.what());
546 void windowmanager_list_drawing_names(afb_req req) noexcept
548 std::lock_guard<std::mutex> guard(binding_m);
552 if (g_afb_instance == nullptr)
554 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
561 nlohmann::json j = g_afb_instance->app.id_alloc.name2id;
562 auto ret = wm::Ok(json_tokener_parse(j.dump().c_str()));
565 afb_req_fail(req, "failed", ret.unwrap_err());
569 afb_req_success(req, ret.unwrap(), "success");
571 catch (std::exception &e)
573 afb_req_fail_f(req, "failed", "Uncaught exception while calling list_drawing_names: %s", e.what());
578 void windowmanager_ping(afb_req req) noexcept
580 std::lock_guard<std::mutex> guard(binding_m);
584 if (g_afb_instance == nullptr)
586 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
593 g_afb_instance->app.api_ping();
595 afb_req_success(req, NULL, "success");
597 catch (std::exception &e)
599 afb_req_fail_f(req, "failed", "Uncaught exception while calling ping: %s", e.what());
604 void windowmanager_debug_status(afb_req req) noexcept
606 std::lock_guard<std::mutex> guard(binding_m);
610 if (g_afb_instance == nullptr)
612 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
619 json_object *jr = json_object_new_object();
620 json_object_object_add(jr, "surfaces",
621 to_json(g_afb_instance->app.controller->sprops));
622 json_object_object_add(jr, "layers", to_json(g_afb_instance->app.controller->lprops));
624 afb_req_success(req, jr, "success");
626 catch (std::exception &e)
628 afb_req_fail_f(req, "failed", "Uncaught exception while calling debug_status: %s", e.what());
633 void windowmanager_debug_layers(afb_req req) noexcept
635 std::lock_guard<std::mutex> guard(binding_m);
639 if (g_afb_instance == nullptr)
641 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
647 auto ret = wm::Ok(json_tokener_parse(g_afb_instance->app.layers.to_json().dump().c_str()));
649 afb_req_success(req, ret, "success");
651 catch (std::exception &e)
653 afb_req_fail_f(req, "failed", "Uncaught exception while calling debug_layers: %s", e.what());
658 void windowmanager_debug_surfaces(afb_req req) noexcept
660 std::lock_guard<std::mutex> guard(binding_m);
664 if (g_afb_instance == nullptr)
666 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
673 auto ret = wm::Ok(to_json(g_afb_instance->app.controller->sprops));
676 afb_req_fail(req, "failed", ret.unwrap_err());
680 afb_req_success(req, ret.unwrap(), "success");
682 catch (std::exception &e)
684 afb_req_fail_f(req, "failed", "Uncaught exception while calling debug_surfaces: %s", e.what());
689 void windowmanager_debug_terminate(afb_req req) noexcept
691 std::lock_guard<std::mutex> guard(binding_m);
695 if (g_afb_instance == nullptr)
697 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
704 if (getenv("WINMAN_DEBUG_TERMINATE") != nullptr)
706 raise(SIGKILL); // afb-daemon kills it's pgroup using TERM, which
707 // doesn't play well with perf
710 afb_req_success(req, NULL, "success");
712 catch (std::exception &e)
714 afb_req_fail_f(req, "failed", "Uncaught exception while calling debug_terminate: %s", e.what());
719 const struct afb_verb_v2 windowmanager_verbs[] = {
720 {"requestsurface", windowmanager_requestsurface, nullptr, nullptr, AFB_SESSION_NONE},
721 {"requestsurfacexdg", windowmanager_requestsurfacexdg, nullptr, nullptr, AFB_SESSION_NONE},
722 {"activatesurface", windowmanager_activatesurface, nullptr, nullptr, AFB_SESSION_NONE},
723 {"deactivatesurface", windowmanager_deactivatesurface, nullptr, nullptr, AFB_SESSION_NONE},
724 {"enddraw", windowmanager_enddraw, nullptr, nullptr, AFB_SESSION_NONE},
725 {"getdisplayinfo", windowmanager_getdisplayinfo_thunk, nullptr, nullptr, AFB_SESSION_NONE},
726 {"getareainfo", windowmanager_getareainfo_thunk, nullptr, nullptr, AFB_SESSION_NONE},
727 {"wm_subscribe", windowmanager_wm_subscribe, nullptr, nullptr, AFB_SESSION_NONE},
728 {"list_drawing_names", windowmanager_list_drawing_names, nullptr, nullptr, AFB_SESSION_NONE},
729 {"ping", windowmanager_ping, nullptr, nullptr, AFB_SESSION_NONE},
730 {"debug_status", windowmanager_debug_status, nullptr, nullptr, AFB_SESSION_NONE},
731 {"debug_layers", windowmanager_debug_layers, nullptr, nullptr, AFB_SESSION_NONE},
732 {"debug_surfaces", windowmanager_debug_surfaces, nullptr, nullptr, AFB_SESSION_NONE},
733 {"debug_terminate", windowmanager_debug_terminate, nullptr, nullptr, AFB_SESSION_NONE},
736 extern "C" const struct afb_binding_v2 afbBindingV2 = {
737 "windowmanager", nullptr, nullptr, windowmanager_verbs, nullptr, binding_init, nullptr, 0};