app: added enddraw() and deactivate_surface()
[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 <json.h>
24
25 extern "C" {
26 #include <afb/afb-binding.h>
27 #include <systemd/sd-event.h>
28 }
29
30 namespace {
31 struct afb_instance {
32    std::unique_ptr<wl::display> display;
33    wm::App app;
34
35    afb_instance() : display{new wl::display}, app{this->display.get()} {}
36
37    int init();
38 };
39
40 struct afb_instance *g_afb_instance;
41
42 int afb_instance::init() {
43    if (!this->display->ok()) {
44       return -1;
45    }
46
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    if ((events & EPOLLHUP) != 0) {
53       logerror("The compositor hung up, dying now.");
54       delete g_afb_instance;
55       g_afb_instance = nullptr;
56       goto error;
57    }
58
59    if ((events & EPOLLIN) != 0u) {
60       if (g_afb_instance->app.dispatch_events() == -1) {
61          goto error;
62       }
63    }
64
65    return 0;
66
67 error:
68    sd_event_source_unref(evs);
69    return -1;
70 }
71
72 //  _     _           _ _                 _       _ _    ____
73 // | |__ (_)_ __   __| (_)_ __   __ _    (_)_ __ (_) |_ / /\ \
74 // | '_ \| | '_ \ / _` | | '_ \ / _` |   | | '_ \| | __| |  | |
75 // | |_) | | | | | (_| | | | | | (_| |   | | | | | | |_| |  | |
76 // |_.__/|_|_| |_|\__,_|_|_| |_|\__, |___|_|_| |_|_|\__| |  | |
77 //                              |___/_____|             \_\/_/
78 int binding_init_() {
79    lognotice("WinMan ver. %s", WINMAN_VERSION_STRING);
80
81    if (g_afb_instance != nullptr) {
82       logerror("Wayland context already initialized?");
83       return 0;
84    }
85
86    if (getenv("XDG_RUNTIME_DIR") == nullptr) {
87       logerror("Environment variable XDG_RUNTIME_DIR not set");
88       goto error;
89    }
90
91    g_afb_instance = new afb_instance;
92    if (g_afb_instance->init() == -1) {
93       logerror("Could not connect to compositor");
94       goto error;
95    }
96
97    {
98       int ret = sd_event_add_io(afb_daemon_get_event_loop(), nullptr,
99                                 g_afb_instance->display->get_fd(), EPOLLIN,
100                                 display_event_callback, g_afb_instance);
101       if (ret < 0) {
102          logerror("Could not initialize afb_instance event handler: %d", -ret);
103          goto error;
104       }
105    }
106
107    atexit([] { delete g_afb_instance; });
108
109    return 0;
110
111 error:
112    delete g_afb_instance;
113    g_afb_instance = nullptr;
114    return -1;
115 }
116
117 int binding_init() noexcept {
118    try {
119       return binding_init_();
120    } catch (std::exception &e) {
121       logerror("Uncaught exception in binding_init(): %s", e.what());
122    }
123    return -1;
124 }
125
126 }  // namespace
127
128 #include "afb_binding_glue.inl"
129
130 extern "C" const struct afb_binding_v2 afbBindingV2 = {
131    "winman", nullptr, nullptr, winman_verbs, nullptr, binding_init, nullptr, 1};