Remove and replace the comments
[apps/agl-service-windowmanager-2017.git] / src / main.cpp
1 /*
2  * Copyright (c) 2017 TOYOTA MOTOR CORPORATION
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include <unistd.h>
18 #include "app.hpp"
19 #include "json_helper.hpp"
20 #include "util.hpp"
21 #include "wayland.hpp"
22
23 #include <algorithm>
24 #include <mutex>
25
26 #include <json.h>
27
28 extern "C" {
29 #include <afb/afb-binding.h>
30 #include <systemd/sd-event.h>
31 }
32
33 namespace {
34
35 struct afb_instance {
36    std::unique_ptr<wl::display> display;
37    wm::App app;
38
39    afb_instance() : display{new wl::display}, app{this->display.get()} {}
40
41    int init();
42 };
43
44 struct afb_instance *g_afb_instance;
45
46 int afb_instance::init() {
47    return this->app.init();
48 }
49
50 int display_event_callback(sd_event_source *evs, int /*fd*/, uint32_t events,
51                            void * /*data*/) {
52    ST();
53
54    if ((events & EPOLLHUP) != 0) {
55       HMI_ERROR("wm", "The compositor hung up, dying now.");
56       delete g_afb_instance;
57       g_afb_instance = nullptr;
58       goto error;
59    }
60
61    if ((events & EPOLLIN) != 0u) {
62       {
63          STN(display_read_events);
64          g_afb_instance->app.display->read_events();
65          g_afb_instance->app.set_pending_events();
66       }
67       {
68          // We want do dispatch pending wayland events from within
69          // the API context
70          STN(winman_ping_api_call);
71          afb_service_call("windowmanager", "ping", json_object_new_object(),
72                           [](void *c, int st, json_object *j) {
73                              STN(winman_ping_api_call_return);
74                           },
75                           nullptr);
76       }
77    }
78
79    return 0;
80
81 error:
82    sd_event_source_unref(evs);
83    if (getenv("WINMAN_EXIT_ON_HANGUP") != nullptr) {
84    exit(1);
85 }
86    return -1;
87 }
88
89 /**
90  * binding_init_()
91  */
92 int binding_init_() {
93    HMI_NOTICE("wm", "WinMan ver. %s", WINMAN_VERSION_STRING);
94
95    if (g_afb_instance != nullptr) {
96       HMI_ERROR("wm", "Wayland context already initialized?");
97       return 0;
98    }
99
100    if (getenv("XDG_RUNTIME_DIR") == nullptr) {
101       HMI_ERROR("wm", "Environment variable XDG_RUNTIME_DIR not set");
102       goto error;
103    }
104
105    {
106       // wait until wayland compositor starts up.
107       int cnt = 0;
108       g_afb_instance = new afb_instance;
109       while (!g_afb_instance->display->ok()) {
110          cnt++;
111          if (20 <= cnt) {
112             HMI_ERROR("wm", "Could not connect to compositor");
113             goto error;
114          }
115          HMI_ERROR("wm", "Wait to start weston ...");
116          sleep(1);
117          delete g_afb_instance;
118          g_afb_instance = new afb_instance;
119       }
120    }
121
122    if (g_afb_instance->init() == -1) {
123       HMI_ERROR("wm", "Could not connect to compositor");
124       goto error;
125    }
126
127    {
128       int ret = sd_event_add_io(afb_daemon_get_event_loop(), nullptr,
129                                 g_afb_instance->display->get_fd(), EPOLLIN,
130                                 display_event_callback, g_afb_instance);
131       if (ret < 0) {
132          HMI_ERROR("wm", "Could not initialize afb_instance event handler: %d", -ret);
133          goto error;
134       }
135    }
136
137    atexit([] { delete g_afb_instance; });
138
139    return 0;
140
141 error:
142    delete g_afb_instance;
143    g_afb_instance = nullptr;
144    return -1;
145 }
146
147 int binding_init() noexcept {
148    try {
149       return binding_init_();
150    } catch (std::exception &e) {
151       HMI_ERROR("wm", "Uncaught exception in binding_init(): %s", e.what());
152    }
153    return -1;
154 }
155
156 }  // namespace
157
158 #include "afb_binding_glue.inl"
159
160 namespace wm {
161 void binding_api::send_event(char const *evname, char const *label) {
162    HMI_DEBUG("wm", "%s: %s(%s)", __func__, evname, label);
163
164    json_object *j = json_object_new_object();
165    json_object_object_add(j, kKeyDrawingName, json_object_new_string(label));
166
167    int ret = afb_event_push(g_afb_instance->app.map_afb_event[evname], j);
168    if (ret != 0) {
169       HMI_DEBUG("wm", "afb_event_push failed: %m");
170    }
171 }
172
173 void binding_api::send_event(char const *evname, char const *label, char const *area) {
174    HMI_DEBUG("wm", "%s: %s(%s, %s)", __func__, evname, label, area);
175
176    json_object *j = json_object_new_object();
177    json_object_object_add(j, kKeyDrawingName, json_object_new_string(label));
178    json_object_object_add(j, kKeyDrawingArea, json_object_new_string(area));
179
180    int ret = afb_event_push(g_afb_instance->app.map_afb_event[evname], j);
181    if (ret != 0) {
182       HMI_DEBUG("wm", "afb_event_push failed: %m");
183    }
184 }
185 } // namespace wm
186
187 extern "C" const struct afb_binding_v2 afbBindingV2 = {
188    "windowmanager", nullptr, nullptr, windowmanager_verbs, nullptr, binding_init, nullptr, 0};