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;
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 g_afb_instance->app.allocateWindowResource("activate",
308 a_drawing_name, a_drawing_area,
309 [&req](const char* errmsg){
310 if (errmsg != nullptr) {
311 HMI_ERROR("wm", errmsg);
312 afb_req_fail(req, "failed", errmsg);
315 afb_req_success(req, NULL, "success");
318 } catch (std::exception &e) {
319 HMI_WARNING("wm", "failed", "Uncaught exception while calling activatesurface: %s", e.what());
325 void windowmanager_deactivatesurface(afb_req req) noexcept {
326 std::lock_guard<std::mutex> guard(binding_m);
330 if (g_afb_instance == nullptr) {
331 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
336 const char* a_drawing_name = afb_req_value(req, "drawing_name");
338 afb_req_fail(req, "failed", "Need char const* argument drawing_name");
342 g_afb_instance->app.allocateWindowResource("deactivate",
343 a_drawing_name, nullptr,
344 [&req](const char* errmsg){
345 if (errmsg != nullptr) {
346 HMI_ERROR("wm", errmsg);
347 afb_req_fail(req, "failed", errmsg);
350 afb_req_success(req, NULL, "success");
353 } catch (std::exception &e) {
354 HMI_WARNING("wm", "Uncaught exception while calling deactivatesurface: %s", e.what());
359 void windowmanager_enddraw(afb_req req) noexcept {
360 std::lock_guard<std::mutex> guard(binding_m);
364 if (g_afb_instance == nullptr) {
365 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
370 const char* a_drawing_name = afb_req_value(req, "drawing_name");
372 afb_req_fail(req, "failed", "Need char const* argument drawing_name");
375 afb_req_success(req, NULL, "success");
377 g_afb_instance->app.api_enddraw(a_drawing_name);
379 } catch (std::exception &e) {
380 HMI_WARNING("wm", "failed", "Uncaught exception while calling enddraw: %s", e.what());
386 void windowmanager_getdisplayinfo_thunk(afb_req req) noexcept {
387 std::lock_guard<std::mutex> guard(binding_m);
391 if (g_afb_instance == nullptr) {
392 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
397 auto ret = g_afb_instance->app.api_get_display_info();
399 afb_req_fail(req, "failed", ret.unwrap_err());
403 afb_req_success(req, ret.unwrap(), "success");
404 } catch (std::exception &e) {
405 afb_req_fail_f(req, "failed", "Uncaught exception while calling getdisplayinfo: %s", e.what());
411 void windowmanager_getareainfo_thunk(afb_req req) noexcept {
412 std::lock_guard<std::mutex> guard(binding_m);
416 if (g_afb_instance == nullptr) {
417 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
422 json_object *jreq = afb_req_json(req);
424 json_object *j_drawing_name = nullptr;
425 if (! json_object_object_get_ex(jreq, "drawing_name", &j_drawing_name)) {
426 afb_req_fail(req, "failed", "Need char const* argument drawing_name");
429 char const* a_drawing_name = json_object_get_string(j_drawing_name);
431 auto ret = g_afb_instance->app.api_get_area_info(a_drawing_name);
433 afb_req_fail(req, "failed", ret.unwrap_err());
437 afb_req_success(req, ret.unwrap(), "success");
438 } catch (std::exception &e) {
439 afb_req_fail_f(req, "failed", "Uncaught exception while calling getareainfo: %s", e.what());
445 void windowmanager_wm_subscribe(afb_req req) noexcept {
446 std::lock_guard<std::mutex> guard(binding_m);
450 if (g_afb_instance == nullptr) {
451 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
456 json_object *jreq = afb_req_json(req);
457 json_object *j = nullptr;
458 if (! json_object_object_get_ex(jreq, "event", &j)) {
459 afb_req_fail(req, "failed", "Need char const* argument event");
462 int event_type = json_object_get_int(j);
463 if ((wm::App::Event_Val_Min > event_type)
464 || (wm::App::Event_Val_Max < event_type)) {
465 afb_req_fail(req, "failed", "Invalid EventType");
469 const char *event_name = g_afb_instance->app.kListEventName[event_type];
470 struct afb_event event = g_afb_instance->app.map_afb_event[event_name];
471 int ret = afb_req_subscribe(req, event);
473 afb_req_fail(req, "failed", "Error: afb_req_subscribe()");
476 afb_req_success(req, NULL, "success");
478 } catch (std::exception &e) {
479 afb_req_fail_f(req, "failed", "Uncaught exception while calling wm_subscribe: %s", e.what());
485 void windowmanager_list_drawing_names(afb_req req) noexcept {
486 std::lock_guard<std::mutex> guard(binding_m);
490 if (g_afb_instance == nullptr) {
491 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
497 nlohmann::json j = g_afb_instance->app.id_alloc.name2id;
498 auto ret = wm::Ok(json_tokener_parse(j.dump().c_str()));
500 afb_req_fail(req, "failed", ret.unwrap_err());
504 afb_req_success(req, ret.unwrap(), "success");
506 } catch (std::exception &e) {
507 afb_req_fail_f(req, "failed", "Uncaught exception while calling list_drawing_names: %s", e.what());
513 void windowmanager_ping(afb_req req) noexcept {
514 std::lock_guard<std::mutex> guard(binding_m);
518 if (g_afb_instance == nullptr) {
519 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
525 g_afb_instance->app.api_ping();
527 afb_req_success(req, NULL, "success");
529 } catch (std::exception &e) {
530 afb_req_fail_f(req, "failed", "Uncaught exception while calling ping: %s", e.what());
535 void windowmanager_debug_status(afb_req req) noexcept {
536 std::lock_guard<std::mutex> guard(binding_m);
540 if (g_afb_instance == nullptr) {
541 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
547 json_object *jr = json_object_new_object();
548 json_object_object_add(jr, "surfaces",
549 to_json(g_afb_instance->app.controller->sprops));
550 json_object_object_add(jr, "layers", to_json(g_afb_instance->app.controller->lprops));
552 afb_req_success(req, jr, "success");
554 } catch (std::exception &e) {
555 afb_req_fail_f(req, "failed", "Uncaught exception while calling debug_status: %s", e.what());
560 void windowmanager_debug_layers(afb_req req) noexcept {
561 std::lock_guard<std::mutex> guard(binding_m);
565 if (g_afb_instance == nullptr) {
566 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
571 auto ret = wm::Ok(json_tokener_parse(g_afb_instance->app.layers.to_json().dump().c_str()));
573 afb_req_success(req, ret, "success");
575 } catch (std::exception &e) {
576 afb_req_fail_f(req, "failed", "Uncaught exception while calling debug_layers: %s", e.what());
581 void windowmanager_debug_surfaces(afb_req req) noexcept {
582 std::lock_guard<std::mutex> guard(binding_m);
586 if (g_afb_instance == nullptr) {
587 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
593 auto ret = wm::Ok(to_json(g_afb_instance->app.controller->sprops));
595 afb_req_fail(req, "failed", ret.unwrap_err());
599 afb_req_success(req, ret.unwrap(), "success");
601 } catch (std::exception &e) {
602 afb_req_fail_f(req, "failed", "Uncaught exception while calling debug_surfaces: %s", e.what());
608 void windowmanager_debug_terminate(afb_req req) noexcept {
609 std::lock_guard<std::mutex> guard(binding_m);
613 if (g_afb_instance == nullptr) {
614 afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
620 if (getenv("WINMAN_DEBUG_TERMINATE") != nullptr) {
621 raise(SIGKILL); // afb-daemon kills it's pgroup using TERM, which
622 // doesn't play well with perf
625 afb_req_success(req, NULL, "success");
627 } catch (std::exception &e) {
628 afb_req_fail_f(req, "failed", "Uncaught exception while calling debug_terminate: %s", e.what());
634 const struct afb_verb_v2 windowmanager_verbs[] = {
635 { "requestsurface", windowmanager_requestsurface, nullptr, nullptr, AFB_SESSION_NONE },
636 { "requestsurfacexdg", windowmanager_requestsurfacexdg, nullptr, nullptr, AFB_SESSION_NONE },
637 { "activatesurface", windowmanager_activatesurface, nullptr, nullptr, AFB_SESSION_NONE },
638 { "deactivatesurface", windowmanager_deactivatesurface, nullptr, nullptr, AFB_SESSION_NONE },
639 { "enddraw", windowmanager_enddraw, nullptr, nullptr, AFB_SESSION_NONE },
640 { "getdisplayinfo", windowmanager_getdisplayinfo_thunk, nullptr, nullptr, AFB_SESSION_NONE },
641 { "getareainfo", windowmanager_getareainfo_thunk, nullptr, nullptr, AFB_SESSION_NONE },
642 { "wm_subscribe", windowmanager_wm_subscribe, nullptr, nullptr, AFB_SESSION_NONE },
643 { "list_drawing_names", windowmanager_list_drawing_names, nullptr, nullptr, AFB_SESSION_NONE },
644 { "ping", windowmanager_ping, nullptr, nullptr, AFB_SESSION_NONE },
645 { "debug_status", windowmanager_debug_status, nullptr, nullptr, AFB_SESSION_NONE },
646 { "debug_layers", windowmanager_debug_layers, nullptr, nullptr, AFB_SESSION_NONE },
647 { "debug_surfaces", windowmanager_debug_surfaces, nullptr, nullptr, AFB_SESSION_NONE },
648 { "debug_terminate", windowmanager_debug_terminate, nullptr, nullptr, AFB_SESSION_NONE },
652 void on_event(const char *event, struct json_object *object){
653 HMI_DEBUG("wm", "event:%s", event);
655 // If receive low can signal
656 if (strstr(event, "low-can")) {
657 wm::LowCanClient *lcc = &(g_afb_instance->lcc);
658 wm::App *app = &(g_afb_instance->app);
660 // Analyze low can signal
661 const char* signal_name = lcc->analyzeCanSignal(object);
663 // If car info is updated, set event name
664 const char *can_event = nullptr;
665 if (strstr(signal_name, lcc->kSignalName[lcc->SignalNoParkingBrake])) {
666 HMI_DEBUG("wm", "Parking Brake state is changed");
669 if (lcc->getCurrentParkingBrakeState()) {
670 can_event = "parking_brake_on";
673 can_event = "parking_brake_off";
676 else if (strstr(signal_name, lcc->kSignalName[lcc->SignalNoAccelPedalPos])) {
677 // Update accel pedal position
678 app->setAccelPedalPos(lcc->getCurrentAccelPedalPosition());
680 if (lcc->isChangedAccelPedalState()) {
681 HMI_DEBUG("wm", "Accelerator Pedal state is changed");
684 if (lcc->getCurrentAccelPedalState()) {
685 can_event = "accel_pedal_on";
688 can_event = "accel_pedal_off";
692 else if (strstr(signal_name, lcc->kSignalName[lcc->SignalNoHeadlame])) {
693 HMI_DEBUG("wm", "Headlamp state is changed");
696 if (lcc->getCurrentHeadlampState()) {
697 can_event = "headlamp_on";
700 can_event = "headlamp_off";
703 else if (strstr(signal_name, lcc->kSignalName[lcc->SignalNoLightstatusBrake])) {
704 HMI_DEBUG("wm", "Lightstatus Brake state is changed");
707 if (lcc->getCurrentLightstatusBrakeState()) {
708 can_event = "lightstatus_brake_on";
711 can_event = "lightstatus_brake_off";
715 // Allocate window resource
716 if (nullptr != can_event) {
717 g_afb_instance->app.allocateWindowResource(can_event,
719 [](const char* errmsg){
720 if (errmsg != nullptr) {
721 HMI_ERROR("wm", errmsg);
728 extern "C" const struct afb_binding_v2 afbBindingV2 = {
729 "windowmanager", nullptr, nullptr, windowmanager_verbs, nullptr, binding_init, on_event, 0};