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