Modify event notification from broadcast to subscribe model
[apps/agl-service-windowmanager-2017.git] / src / main.cpp
1 /*
2  * Copyright (C) 2017 Mentor Graphics Development (Deutschland) GmbH
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       logerror("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 // | |__ (_)_ __   __| (_)_ __   __ _    (_)_ __ (_) |_ / /\ \
91 // | '_ \| | '_ \ / _` | | '_ \ / _` |   | | '_ \| | __| |  | |
92 // | |_) | | | | | (_| | | | | | (_| |   | | | | | | |_| |  | |
93 // |_.__/|_|_| |_|\__,_|_|_| |_|\__, |___|_|_| |_|_|\__| |  | |
94 //                              |___/_____|             \_\/_/
95 int binding_init_() {
96    lognotice("WinMan ver. %s", WINMAN_VERSION_STRING);
97
98    if (g_afb_instance != nullptr) {
99       logerror("Wayland context already initialized?");
100       return 0;
101    }
102
103    if (getenv("XDG_RUNTIME_DIR") == nullptr) {
104       logerror("Environment variable XDG_RUNTIME_DIR not set");
105       goto error;
106    }
107
108    {
109       // wait until wayland compositor starts up.
110       int cnt = 0;
111       g_afb_instance = new afb_instance;
112       while (!g_afb_instance->display->ok()) {
113          cnt++;
114          if (20 <= cnt) {
115             logerror("Could not connect to compositor");
116             goto error;
117          }
118          logerror("Wait to start weston ...");
119          sleep(1);
120          delete g_afb_instance;
121          g_afb_instance = new afb_instance;
122       }
123    }
124
125    if (g_afb_instance->init() == -1) {
126       logerror("Could not connect to compositor");
127       goto error;
128    }
129
130    {
131       int ret = sd_event_add_io(afb_daemon_get_event_loop(), nullptr,
132                                 g_afb_instance->display->get_fd(), EPOLLIN,
133                                 display_event_callback, g_afb_instance);
134       if (ret < 0) {
135          logerror("Could not initialize afb_instance event handler: %d", -ret);
136          goto error;
137       }
138    }
139
140    atexit([] { delete g_afb_instance; });
141
142    return 0;
143
144 error:
145    delete g_afb_instance;
146    g_afb_instance = nullptr;
147    return -1;
148 }
149
150 int binding_init() noexcept {
151    try {
152       return binding_init_();
153    } catch (std::exception &e) {
154       logerror("Uncaught exception in binding_init(): %s", e.what());
155    }
156    return -1;
157 }
158
159 }  // namespace
160
161 #include "afb_binding_glue.inl"
162
163 namespace wm {
164 void binding_api::send_event(char const *evname, char const *label) {
165    logdebug("%s: %s(%s)", __func__, evname, label);
166
167    json_object *j = json_object_new_object();
168    json_object_object_add(j, kKeyDrawingName, json_object_new_string(label));
169
170    int ret = afb_event_push(g_afb_instance->app.map_afb_event[evname], j);
171    if (ret != 0) {
172       logdebug("afb_event_push failed: %m");
173    }
174 }
175
176 void binding_api::send_event(char const *evname, char const *label, char const *area) {
177    logdebug("%s: %s(%s, %s)", __func__, evname, label, area);
178
179    json_object *j = json_object_new_object();
180    json_object_object_add(j, kKeyDrawingName, json_object_new_string(label));
181    json_object_object_add(j, kKeyDrawingArea, json_object_new_string(area));
182
183    int ret = afb_event_push(g_afb_instance->app.map_afb_event[evname], j);
184    if (ret != 0) {
185       logdebug("afb_event_push failed: %m");
186    }
187 }
188 } // namespace wm
189
190 extern "C" const struct afb_binding_v2 afbBindingV2 = {
191    "windowmanager", nullptr, nullptr, windowmanager_verbs, nullptr, binding_init, nullptr, 0};