all: clang-format
[staging/windowmanager.git] / src / main.cpp
1 #include "util.hpp"
2 #include "wayland.hpp"
3
4 #include <unistd.h>
5
6 #include <signal.h>
7 #include <sys/signalfd.h>
8
9 #include <algorithm>
10
11 struct connection {
12    std::vector<std::unique_ptr<wl::output>> outputs;
13    std::unique_ptr<genivi::controller> c;
14 };
15
16 namespace {
17 //  _       _ _       _                         _    ____
18 // (_)_ __ (_) |_    | | __ _ _   _  ___  _   _| |_ / /\ \
19 // | | '_ \| | __|   | |/ _` | | | |/ _ \| | | | __| |  | |
20 // | | | | | | |_    | | (_| | |_| | (_) | |_| | |_| |  | |
21 // |_|_| |_|_|\__|___|_|\__,_|\__, |\___/ \__,_|\__| |  | |
22 //              |_____|       |___/                 \_\/_/
23 char const *init_layout(struct connection &c) {
24    if (!c.c) {
25       return "ivi_controller global not available";
26    }
27
28    if (c.outputs.empty()) {
29       return "no output was set up!";
30    }
31
32    auto &o = c.outputs.front();
33    auto &s = c.c->screens.begin()->second;
34    auto &layers = c.c->layers;
35
36    // XXX: Write output dimensions to ivi controller...
37    c.c->output_size = genivi::size{uint32_t(o->width), uint32_t(o->height)};
38
39    // Clear scene
40    layers.clear();
41
42    // Clear screen
43    s->clear();
44
45    // Setup our dummy scene...
46    c.c->layer_create(100, 0, 0);   // bottom layer, anything else
47    c.c->layer_create(1000, 0, 0);  // top layer, mandelbrot
48
49    auto &l100 = c.c->layers[100];
50    auto &l1k = c.c->layers[1000];
51
52    // Set layers fullscreen
53    l100->set_destination_rectangle(0, 0, o->width, o->height);
54    l1k->set_destination_rectangle(0, 0, o->width, o->height);
55    l100->set_visibility(1);
56    l1k->set_visibility(1);
57
58    // Add layers to screen
59    s->set_render_order({100, 1000});
60
61    c.c->commit_changes();
62    // Note: this does not flush the display!
63
64    return nullptr;
65 }
66 }  // namespace
67
68 //                  _        ____
69 //  _ __ ___   __ _(_)_ __  / /\ \
70 // | '_ ` _ \ / _` | | '_ \| |  | |
71 // | | | | | | (_| | | | | | |  | |
72 // |_| |_| |_|\__,_|_|_| |_| |  | |
73 //                          \_\/_/
74 int main(int /*argc*/, char ** /*argv*/) {
75    lognotice("WinMan ver. %s", WINMAN_VERSION_STRING);
76
77    if (getenv("XDG_RUNTIME_DIR") == nullptr) {
78       fatal("Environment variable XDG_RUNTIME_DIR not set");
79    }
80
81    struct wl::display d {};
82    if (!d.ok()) {
83       fatal("Could not connect to compositor");
84    }
85
86    struct connection c {};
87
88    d.r.add_global_handler(
89       "wl_output", [&c](wl_registry *r, uint32_t name, uint32_t v) {
90          c.outputs.emplace_back(std::make_unique<wl::output>(r, name, v));
91       });
92
93    d.r.add_global_handler(
94       "ivi_controller", [&c](wl_registry *r, uint32_t name, uint32_t v) {
95          c.c = std::make_unique<genivi::controller>(r, name, v);
96
97          // XXX: This protocol needs the output, so lets just add our mapping
98          // here...
99          c.c->add_proxy_to_id_mapping(
100             c.outputs.back()->proxy.get(),
101             wl_proxy_get_id(reinterpret_cast<struct wl_proxy *>(
102                c.outputs.back()->proxy.get())));
103       });
104
105    // First level objects
106    d.roundtrip();
107    // Second level objects
108    d.roundtrip();
109    // Third level objects
110    d.roundtrip();
111
112    if (char const *e = init_layout(c)) {
113       fatal("Could not init layout: %s", e);
114    }
115
116    struct Poller p {};
117    p.add_fd(STDIN_FILENO, [&c](int fd) {
118       int buf;
119       ssize_t ret;
120       ret = read(fd, &buf, sizeof(buf));
121       c.c->debug_dump_current_status();
122       return ret == 0 ? -1 : 0;
123    });
124
125    p.add_fd(d.get_fd(), [&d](int fd) { return d.dispatch(); });
126
127    sigset_t sset{};
128    sigemptyset(&sset);
129    sigaddset(&sset, SIGINT);
130    sigaddset(&sset, SIGTERM);
131
132    auto sfd = unique_fd(signalfd(-1, &sset, SFD_NONBLOCK | SFD_CLOEXEC));
133    sigprocmask(SIG_BLOCK, &sset, NULL);
134    p.add_fd(sfd.fd, [](int fd) {
135       struct signalfd_siginfo si;
136       while (read(fd, &si, sizeof(si)) == sizeof(si)) {
137          lognotice("Received signal %u", si.ssi_signo);
138       }
139       return -1;
140    });
141
142    while ((d.flush(), p.check_events()) != -1) {
143       c.c->execute_pending();
144    }
145
146    c.c->commit_changes();
147    d.roundtrip();
148
149    return 0;
150 }