moved Poller and unique_fd from main to util, fixed c++ issues in util
[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 here...
98          c.c->add_proxy_to_id_mapping(c.outputs.back()->proxy.get(),
99                                       wl_proxy_get_id(
100                                               reinterpret_cast<struct wl_proxy *>(
101                                                       c.outputs.back()->proxy.get())));
102       });
103
104    // First level objects
105    d.roundtrip();
106    // Second level objects
107    d.roundtrip();
108    // Third level objects
109    d.roundtrip();
110
111    if (char const *e = init_layout(c)) {
112       fatal("Could not init layout: %s", e);
113    }
114
115    struct Poller p{};
116    p.add_fd(STDIN_FILENO, [&c](int fd) {
117        int buf;
118        ssize_t ret;
119        ret = read(fd, &buf, sizeof(buf));
120        c.c->debug_dump_current_status();
121        return ret == 0 ? -1 : 0;
122    });
123
124    p.add_fd(d.get_fd(), [&d](int fd) {
125       return d.dispatch();
126    });
127
128    sigset_t sset{};
129    sigemptyset(&sset);
130    sigaddset(&sset, SIGINT);
131    sigaddset(&sset, SIGTERM);
132
133    auto sfd = unique_fd(signalfd(-1, &sset, SFD_NONBLOCK | SFD_CLOEXEC));
134    sigprocmask(SIG_BLOCK, &sset, NULL);
135    p.add_fd(sfd.fd, [](int fd) {
136       struct signalfd_siginfo si;
137       while (read(fd, &si, sizeof(si)) == sizeof(si)) {
138           lognotice("Received signal %u", si.ssi_signo);
139       }
140       return -1;
141    });
142
143    while ((d.flush(), p.check_events()) != -1) {
144       c.c->execute_pending();
145    }
146
147    c.c->commit_changes();
148    d.roundtrip();
149
150    return 0;
151 }