doc: convert asciidoc to md
[staging/windowmanager.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 "app.hpp"
18 #include "json_helper.hpp"
19 #include "util.hpp"
20 #include "wayland.hpp"
21
22 #include <algorithm>
23 #include <mutex>
24
25 #include <json.h>
26
27 extern "C" {
28 #include <afb/afb-binding.h>
29 #include <systemd/sd-event.h>
30 }
31
32 namespace {
33
34 struct afb_instance {
35    std::unique_ptr<wl::display> display;
36    wm::App app;
37
38    afb_instance() : display{new wl::display}, app{this->display.get()} {}
39
40    int init();
41 };
42
43 struct afb_instance *g_afb_instance;
44
45 int afb_instance::init() {
46    if (!this->display->ok()) {
47       return -1;
48    }
49
50    return this->app.init();
51 }
52
53 int display_event_callback(sd_event_source *evs, int  /*fd*/, uint32_t events,
54                            void * /*data*/) {
55    ST();
56
57    if ((events & EPOLLHUP) != 0) {
58       logerror("The compositor hung up, dying now.");
59       delete g_afb_instance;
60       g_afb_instance = nullptr;
61       goto error;
62    }
63
64    if ((events & EPOLLIN) != 0u) {
65       {
66          STN(display_read_events);
67          g_afb_instance->app.display->read_events();
68          g_afb_instance->app.set_pending_events();
69       }
70       {
71          // We want do dispatch pending wayland events from within
72          // the API context
73          STN(winman_ping_api_call);
74          afb_service_call("winman", "ping", json_object_new_object(),
75                           [](void *c, int st, json_object *j) {
76                              STN(winman_ping_api_call_return);
77                           },
78                           nullptr);
79       }
80    }
81
82    return 0;
83
84 error:
85    sd_event_source_unref(evs);
86    if (getenv("WINMAN_EXIT_ON_HANGUP") != nullptr) {
87       exit(1);
88 }
89    return -1;
90 }
91
92 //  _     _           _ _                 _       _ _    ____
93 // | |__ (_)_ __   __| (_)_ __   __ _    (_)_ __ (_) |_ / /\ \
94 // | '_ \| | '_ \ / _` | | '_ \ / _` |   | | '_ \| | __| |  | |
95 // | |_) | | | | | (_| | | | | | (_| |   | | | | | | |_| |  | |
96 // |_.__/|_|_| |_|\__,_|_|_| |_|\__, |___|_|_| |_|_|\__| |  | |
97 //                              |___/_____|             \_\/_/
98 int binding_init_() {
99    lognotice("WinMan ver. %s", WINMAN_VERSION_STRING);
100
101    if (g_afb_instance != nullptr) {
102       logerror("Wayland context already initialized?");
103       return 0;
104    }
105
106    if (getenv("XDG_RUNTIME_DIR") == nullptr) {
107       logerror("Environment variable XDG_RUNTIME_DIR not set");
108       goto error;
109    }
110
111    g_afb_instance = new afb_instance;
112    if (g_afb_instance->init() == -1) {
113       logerror("Could not connect to compositor");
114       goto error;
115    }
116
117    {
118       int ret = sd_event_add_io(afb_daemon_get_event_loop(), nullptr,
119                                 g_afb_instance->display->get_fd(), EPOLLIN,
120                                 display_event_callback, g_afb_instance);
121       if (ret < 0) {
122          logerror("Could not initialize afb_instance event handler: %d", -ret);
123          goto error;
124       }
125    }
126
127    atexit([] { delete g_afb_instance; });
128
129    return 0;
130
131 error:
132    delete g_afb_instance;
133    g_afb_instance = nullptr;
134    return -1;
135 }
136
137 int binding_init() noexcept {
138    try {
139       return binding_init_();
140    } catch (std::exception &e) {
141       logerror("Uncaught exception in binding_init(): %s", e.what());
142    }
143    return -1;
144 }
145
146 }  // namespace
147
148 #include "afb_binding_glue.inl"
149
150 // XXX implement send_event right here...
151 namespace wm {
152 void binding_api::send_event(char const *evname, char const *label) {
153    logdebug("%s: %s(%s)", __func__, evname, label);
154    int ret = afb_daemon_broadcast_event(evname, json_object_new_string(label));
155    if (ret != 0) {
156       logdebug("afb_event_broadcast failed: %m");
157    }
158 }
159 } // namespace wm
160
161 extern "C" const struct afb_binding_v2 afbBindingV2 = {
162    "winman", nullptr, nullptr, winman_verbs, nullptr, binding_init, nullptr, 0};