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"
27 #include "low_can_client.hpp"
30 #include <afb/afb-binding.h>
31 #include <systemd/sd-event.h>
34 typedef struct wmClientCtxt{
36 wmClientCtxt(const char* appName){
42 std::unique_ptr<wl::display> display;
43 wm::LowCanClient lcc_;
46 afb_instance() : display{new wl::display}, lcc_{}, app{this->display.get()} {}
51 struct afb_instance *g_afb_instance;
54 int afb_instance::init() {
55 // Initialize LowCanClient class
56 this->lcc_.initialize();
58 // Initialize App class
59 return this->app.init();
62 int display_event_callback(sd_event_source *evs, int /*fd*/, uint32_t events,
66 if ((events & EPOLLHUP) != 0) {
67 HMI_ERROR("wm", "The compositor hung up, dying now.");
68 delete g_afb_instance;
69 g_afb_instance = nullptr;
73 if ((events & EPOLLIN) != 0u) {
75 STN(display_read_events);
76 g_afb_instance->app.display->read_events();
77 g_afb_instance->app.set_pending_events();
80 // We want do dispatch pending wayland events from within
82 STN(winman_ping_api_call);
83 afb_service_call("windowmanager", "ping", json_object_new_object(),
84 [](void *c, int st, json_object *j) {
85 STN(winman_ping_api_call_return);
94 sd_event_source_unref(evs);
95 if (getenv("WINMAN_EXIT_ON_HANGUP") != nullptr) {
101 int _binding_init() {
102 HMI_NOTICE("wm", "WinMan ver. %s", WINMAN_VERSION_STRING);
104 if (g_afb_instance != nullptr) {
105 HMI_ERROR("wm", "Wayland context already initialized?");
109 if (getenv("XDG_RUNTIME_DIR") == nullptr) {
110 HMI_ERROR("wm", "Environment variable XDG_RUNTIME_DIR not set");
115 // wait until wayland compositor starts up.
117 g_afb_instance = new afb_instance;
118 while (!g_afb_instance->display->ok()) {
121 HMI_ERROR("wm", "Could not connect to compositor");
124 HMI_ERROR("wm", "Wait to start weston ...");
126 delete g_afb_instance;
127 g_afb_instance = new afb_instance;
131 if (g_afb_instance->init() == -1) {
132 HMI_ERROR("wm", "Could not connect to compositor");
137 int ret = sd_event_add_io(afb_daemon_get_event_loop(), nullptr,
138 g_afb_instance->display->get_fd(), EPOLLIN,
139 display_event_callback, g_afb_instance);
141 HMI_ERROR("wm", "Could not initialize afb_instance event handler: %d", -ret);
146 atexit([] { delete g_afb_instance; });
151 delete g_afb_instance;
152 g_afb_instance = nullptr;
156 int binding_init() noexcept {
158 return _binding_init();
159 } catch (std::exception &e) {
160 HMI_ERROR("wm", "Uncaught exception in binding_init(): %s", e.what());
165 static bool checkFirstReq(afb_req req){
166 wmClientCtxt* ctxt = (wmClientCtxt*)afb_req_context_get(req);
167 return (ctxt) ? false : true;
170 static void cbRemoveClientCtxt(void* data){
171 wmClientCtxt* ctxt = (wmClientCtxt*)data;
175 HMI_DEBUG("wm","remove app %s", ctxt->name.c_str());
176 // Lookup surfaceID and remove it because App is dead.
177 auto pSid = g_afb_instance->app.id_alloc.lookup(ctxt->name.c_str());
180 g_afb_instance->app.id_alloc.remove_id(sid);
181 g_afb_instance->app.layers.remove_surface(sid);
182 g_afb_instance->app.controller->sprops.erase(sid);
183 g_afb_instance->app.controller->surfaces.erase(sid);
184 HMI_DEBUG("wm", "delete surfaceID %d", sid);
189 void windowmanager_requestsurface(afb_req req) noexcept {
190 std::lock_guard<std::mutex> guard(binding_m);
194 if (g_afb_instance == nullptr) {
195 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
200 const char* a_drawing_name = afb_req_value(req, "drawing_name");
202 afb_req_fail(req, "failed", "Need char const* argument drawing_name");
206 /* Create Security Context */
207 bool isFirstReq = checkFirstReq(req);
209 wmClientCtxt* ctxt = (wmClientCtxt*)afb_req_context_get(req);
210 HMI_DEBUG("wm", "You're %s.", ctxt->name.c_str());
211 if(ctxt->name != std::string(a_drawing_name)){
212 afb_req_fail_f(req, "failed", "Dont request with other name: %s for now", a_drawing_name);
213 HMI_DEBUG("wm", "Don't request with other name: %s for now", a_drawing_name);
218 auto ret = g_afb_instance->app.api_request_surface(a_drawing_name);
221 wmClientCtxt* ctxt = new wmClientCtxt(a_drawing_name);
222 HMI_DEBUG("wm", "create session for %s", ctxt->name.c_str());
223 afb_req_session_set_LOA(req, 1);
224 afb_req_context_set(req, ctxt, cbRemoveClientCtxt);
227 HMI_DEBUG("wm", "session already created for %s", a_drawing_name);
231 afb_req_fail(req, "failed", ret.unwrap_err());
235 afb_req_success(req, json_object_new_int(ret.unwrap()), "success");
237 } catch (std::exception &e) {
238 afb_req_fail_f(req, "failed", "Uncaught exception while calling requestsurface: %s", e.what());
244 void windowmanager_requestsurfacexdg(afb_req req) noexcept {
245 std::lock_guard<std::mutex> guard(binding_m);
249 if (g_afb_instance == nullptr) {
250 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
255 json_object *jreq = afb_req_json(req);
257 json_object *j_drawing_name = nullptr;
258 if (! json_object_object_get_ex(jreq, "drawing_name", &j_drawing_name)) {
259 afb_req_fail(req, "failed", "Need char const* argument drawing_name");
262 char const* a_drawing_name = json_object_get_string(j_drawing_name);
264 json_object *j_ivi_id = nullptr;
265 if (! json_object_object_get_ex(jreq, "ivi_id", &j_ivi_id)) {
266 afb_req_fail(req, "failed", "Need char const* argument ivi_id");
269 char const* a_ivi_id = json_object_get_string(j_ivi_id);
271 auto ret = g_afb_instance->app.api_request_surface(a_drawing_name, a_ivi_id);
272 if (ret != nullptr) {
273 afb_req_fail(req, "failed", ret);
277 afb_req_success(req, NULL, "success");
278 } catch (std::exception &e) {
279 afb_req_fail_f(req, "failed", "Uncaught exception while calling requestsurfacexdg: %s", e.what());
284 void windowmanager_activatesurface(afb_req req) noexcept {
285 std::lock_guard<std::mutex> guard(binding_m);
289 if (g_afb_instance == nullptr) {
290 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
295 const char* a_drawing_name = afb_req_value(req, "drawing_name");
297 afb_req_fail(req, "failed", "Need char const* argument drawing_name");
301 const char* a_drawing_area = afb_req_value(req, "drawing_area");
303 afb_req_fail(req, "failed", "Need char const* argument drawing_area");
307 const char* a_role = afb_req_value(req, "role");
312 g_afb_instance->app.allocateWindowResource("activate", a_drawing_name,
313 a_drawing_area, a_role,
314 [&req](const char* errmsg){
315 if (errmsg != nullptr) {
316 HMI_ERROR("wm", errmsg);
317 afb_req_fail(req, "failed", errmsg);
320 afb_req_success(req, NULL, "success");
323 } catch (std::exception &e) {
324 HMI_WARNING("wm", "failed", "Uncaught exception while calling activatesurface: %s", e.what());
330 void windowmanager_deactivatesurface(afb_req req) noexcept {
331 std::lock_guard<std::mutex> guard(binding_m);
335 if (g_afb_instance == nullptr) {
336 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
341 const char* a_drawing_name = afb_req_value(req, "drawing_name");
343 afb_req_fail(req, "failed", "Need char const* argument drawing_name");
347 const char* a_role = afb_req_value(req, "role");
352 g_afb_instance->app.allocateWindowResource("deactivate", a_drawing_name,
354 [&req](const char* errmsg){
355 if (errmsg != nullptr) {
356 HMI_ERROR("wm", errmsg);
357 afb_req_fail(req, "failed", errmsg);
360 afb_req_success(req, NULL, "success");
363 } catch (std::exception &e) {
364 HMI_WARNING("wm", "Uncaught exception while calling deactivatesurface: %s", e.what());
369 void windowmanager_enddraw(afb_req req) noexcept {
370 std::lock_guard<std::mutex> guard(binding_m);
374 if (g_afb_instance == nullptr) {
375 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
380 const char* a_drawing_name = afb_req_value(req, "drawing_name");
382 afb_req_fail(req, "failed", "Need char const* argument drawing_name");
385 afb_req_success(req, NULL, "success");
387 g_afb_instance->app.api_enddraw(a_drawing_name);
389 } catch (std::exception &e) {
390 HMI_WARNING("wm", "failed", "Uncaught exception while calling enddraw: %s", e.what());
396 void windowmanager_getdisplayinfo_thunk(afb_req req) noexcept {
397 std::lock_guard<std::mutex> guard(binding_m);
401 if (g_afb_instance == nullptr) {
402 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
407 auto ret = g_afb_instance->app.api_get_display_info();
409 afb_req_fail(req, "failed", ret.unwrap_err());
413 afb_req_success(req, ret.unwrap(), "success");
414 } catch (std::exception &e) {
415 afb_req_fail_f(req, "failed", "Uncaught exception while calling getdisplayinfo: %s", e.what());
421 void windowmanager_getareainfo_thunk(afb_req req) noexcept {
422 std::lock_guard<std::mutex> guard(binding_m);
426 if (g_afb_instance == nullptr) {
427 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
432 json_object *jreq = afb_req_json(req);
434 json_object *j_drawing_name = nullptr;
435 if (! json_object_object_get_ex(jreq, "drawing_name", &j_drawing_name)) {
436 afb_req_fail(req, "failed", "Need char const* argument drawing_name");
439 char const* a_drawing_name = json_object_get_string(j_drawing_name);
441 auto ret = g_afb_instance->app.api_get_area_info(a_drawing_name);
443 afb_req_fail(req, "failed", ret.unwrap_err());
447 afb_req_success(req, ret.unwrap(), "success");
448 } catch (std::exception &e) {
449 afb_req_fail_f(req, "failed", "Uncaught exception while calling getareainfo: %s", e.what());
455 void windowmanager_wm_subscribe(afb_req req) noexcept {
456 std::lock_guard<std::mutex> guard(binding_m);
460 if (g_afb_instance == nullptr) {
461 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
466 json_object *jreq = afb_req_json(req);
467 json_object *j = nullptr;
468 if (! json_object_object_get_ex(jreq, "event", &j)) {
469 afb_req_fail(req, "failed", "Need char const* argument event");
472 int event_type = json_object_get_int(j);
473 if ((wm::App::Event_Val_Min > event_type)
474 || (wm::App::Event_Val_Max < event_type)) {
475 afb_req_fail(req, "failed", "Invalid EventType");
479 const char *event_name = g_afb_instance->app.kListEventName[event_type];
480 struct afb_event event = g_afb_instance->app.map_afb_event[event_name];
481 int ret = afb_req_subscribe(req, event);
483 afb_req_fail(req, "failed", "Error: afb_req_subscribe()");
486 afb_req_success(req, NULL, "success");
488 } catch (std::exception &e) {
489 afb_req_fail_f(req, "failed", "Uncaught exception while calling wm_subscribe: %s", e.what());
495 void windowmanager_list_drawing_names(afb_req req) noexcept {
496 std::lock_guard<std::mutex> guard(binding_m);
500 if (g_afb_instance == nullptr) {
501 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
507 nlohmann::json j = g_afb_instance->app.id_alloc.name2id;
508 auto ret = wm::Ok(json_tokener_parse(j.dump().c_str()));
510 afb_req_fail(req, "failed", ret.unwrap_err());
514 afb_req_success(req, ret.unwrap(), "success");
516 } catch (std::exception &e) {
517 afb_req_fail_f(req, "failed", "Uncaught exception while calling list_drawing_names: %s", e.what());
523 void windowmanager_ping(afb_req req) noexcept {
524 std::lock_guard<std::mutex> guard(binding_m);
528 if (g_afb_instance == nullptr) {
529 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
535 g_afb_instance->app.api_ping();
537 afb_req_success(req, NULL, "success");
539 } catch (std::exception &e) {
540 afb_req_fail_f(req, "failed", "Uncaught exception while calling ping: %s", e.what());
545 void windowmanager_debug_status(afb_req req) noexcept {
546 std::lock_guard<std::mutex> guard(binding_m);
550 if (g_afb_instance == nullptr) {
551 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
557 json_object *jr = json_object_new_object();
558 json_object_object_add(jr, "surfaces",
559 to_json(g_afb_instance->app.controller->sprops));
560 json_object_object_add(jr, "layers", to_json(g_afb_instance->app.controller->lprops));
562 afb_req_success(req, jr, "success");
564 } catch (std::exception &e) {
565 afb_req_fail_f(req, "failed", "Uncaught exception while calling debug_status: %s", e.what());
570 void windowmanager_debug_layers(afb_req req) noexcept {
571 std::lock_guard<std::mutex> guard(binding_m);
575 if (g_afb_instance == nullptr) {
576 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
581 auto ret = wm::Ok(json_tokener_parse(g_afb_instance->app.layers.to_json().dump().c_str()));
583 afb_req_success(req, ret, "success");
585 } catch (std::exception &e) {
586 afb_req_fail_f(req, "failed", "Uncaught exception while calling debug_layers: %s", e.what());
591 void windowmanager_debug_surfaces(afb_req req) noexcept {
592 std::lock_guard<std::mutex> guard(binding_m);
596 if (g_afb_instance == nullptr) {
597 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
603 auto ret = wm::Ok(to_json(g_afb_instance->app.controller->sprops));
605 afb_req_fail(req, "failed", ret.unwrap_err());
609 afb_req_success(req, ret.unwrap(), "success");
611 } catch (std::exception &e) {
612 afb_req_fail_f(req, "failed", "Uncaught exception while calling debug_surfaces: %s", e.what());
618 void windowmanager_debug_terminate(afb_req req) noexcept {
619 std::lock_guard<std::mutex> guard(binding_m);
623 if (g_afb_instance == nullptr) {
624 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
630 if (getenv("WINMAN_DEBUG_TERMINATE") != nullptr) {
631 raise(SIGKILL); // afb-daemon kills it's pgroup using TERM, which
632 // doesn't play well with perf
635 afb_req_success(req, NULL, "success");
637 } catch (std::exception &e) {
638 afb_req_fail_f(req, "failed", "Uncaught exception while calling debug_terminate: %s", e.what());
644 const struct afb_verb_v2 windowmanager_verbs[] = {
645 { "requestsurface", windowmanager_requestsurface, nullptr, nullptr, AFB_SESSION_NONE },
646 { "requestsurfacexdg", windowmanager_requestsurfacexdg, nullptr, nullptr, AFB_SESSION_NONE },
647 { "activatesurface", windowmanager_activatesurface, nullptr, nullptr, AFB_SESSION_NONE },
648 { "deactivatesurface", windowmanager_deactivatesurface, nullptr, nullptr, AFB_SESSION_NONE },
649 { "enddraw", windowmanager_enddraw, nullptr, nullptr, AFB_SESSION_NONE },
650 { "getdisplayinfo", windowmanager_getdisplayinfo_thunk, nullptr, nullptr, AFB_SESSION_NONE },
651 { "getareainfo", windowmanager_getareainfo_thunk, nullptr, nullptr, AFB_SESSION_NONE },
652 { "wm_subscribe", windowmanager_wm_subscribe, nullptr, nullptr, AFB_SESSION_NONE },
653 { "list_drawing_names", windowmanager_list_drawing_names, nullptr, nullptr, AFB_SESSION_NONE },
654 { "ping", windowmanager_ping, nullptr, nullptr, AFB_SESSION_NONE },
655 { "debug_status", windowmanager_debug_status, nullptr, nullptr, AFB_SESSION_NONE },
656 { "debug_layers", windowmanager_debug_layers, nullptr, nullptr, AFB_SESSION_NONE },
657 { "debug_surfaces", windowmanager_debug_surfaces, nullptr, nullptr, AFB_SESSION_NONE },
658 { "debug_terminate", windowmanager_debug_terminate, nullptr, nullptr, AFB_SESSION_NONE },
662 void on_event(const char *event, struct json_object *object){
663 HMI_DEBUG("wm", "event:%s", event);
665 // If receive low can signal
666 if (strstr(event, "low-can")) {
667 // Analyze low can signal
668 g_afb_instance->lcc_.analyzeCanSignal(object);
670 if (g_afb_instance->lcc_.isChangedParkingBrakeState()) {
671 // If parking brake state is changed
672 HMI_DEBUG("wm", "Parking Brake state is changed");
674 // Get parking brake state
675 const char* parking_brake_state = g_afb_instance->lcc_.getCurrentParkingBrakeState();
677 // Allocate window resource
678 g_afb_instance->app.allocateWindowResource(parking_brake_state, nullptr,
680 [](const char* errmsg){
681 if (errmsg != nullptr) {
682 HMI_ERROR("wm", errmsg);
686 if (g_afb_instance->lcc_.isChangedAccelPedalState()) {
687 // If accelerator pedal state is changed
688 HMI_DEBUG("wm", "Accelerator Pedal state is changed");
690 // Get parking brake state
691 const char* accel_pedal_state = g_afb_instance->lcc_.getCurrentAccelPedalState();
693 // Allocate window resource
694 g_afb_instance->app.allocateWindowResource(accel_pedal_state, nullptr,
696 [](const char* errmsg){
697 if (errmsg != nullptr) {
698 HMI_ERROR("wm", errmsg);
702 else if (g_afb_instance->lcc_.isChangedLampState()) {
703 // If lamp state is changed
704 HMI_DEBUG("wm", "Lamp state is changed");
707 const char* lamp_state = g_afb_instance->lcc_.getCurrentLampState();
709 // Allocate window resource
710 g_afb_instance->app.allocateWindowResource(lamp_state, nullptr,
712 [](const char* errmsg){
713 if (errmsg != nullptr) {
714 HMI_ERROR("wm", errmsg);
721 extern "C" const struct afb_binding_v2 afbBindingV2 = {
722 "windowmanager", nullptr, nullptr, windowmanager_verbs, nullptr, binding_init, on_event, 0};