main/init_layout: use screen::set_render_order instead of add_layer calls
[staging/windowmanager.git] / src / main.cpp
1 #include "util.h"
2 #include "wayland.hpp"
3
4 #include <unistd.h>
5
6 #include <stdlib.h>
7 #include <string.h>
8
9 #include <map>
10 #include <memory>
11 #include <string>
12 #include <vector>
13
14 #include <sys/poll.h>
15
16 struct conn {
17    std::vector<std::unique_ptr<wl::output>> outputs;
18    std::unique_ptr<genivi::controller> c;
19 };
20
21 namespace {
22 int check_events(struct wl::display *d, struct conn *c, int fd) {
23    struct pollfd pfd[2] = {{.fd = d->get_fd(), .events = POLLIN, .revents = 0},
24                            {.fd = fd, .events = POLLIN, .revents = 0}};
25
26    d->flush();
27
28    if (poll(pfd, fd != -1 ? 2 : 1, -1) != -1 && errno != EINTR) {
29       int ret = 0;
30
31       if (pfd[0].revents & POLLIN) {
32          ret = d->dispatch();
33       }
34
35       if (ret == -1)
36          return ret;
37
38       if (fd != -1 && (pfd[1].revents & POLLIN)) {
39          char buf[256];
40
41          // read all there is ...
42          while (read(pfd[1].fd, buf, sizeof(buf)) == sizeof(buf))
43             ;
44
45          // Display current status
46          if (!c->c->surfaces.empty()) {
47             puts("Surfaces:");
48             for (auto const &i : c->c->surfaces) {
49                struct genivi::rect const &r = i.second->dst_rect;
50                struct genivi::size const &s = i.second->size;
51                printf("%d [%ux%u] (%ux%u@%dx%d), ", i.first, s.w, s.h, r.w, r.h,
52                       r.x, r.y);
53             }
54             puts("\b\b ");
55          }
56
57          if (!c->c->layers.empty()) {
58             puts("Layers:");
59             for (auto const &i : c->c->layers) {
60                struct genivi::rect const &r = i.second->dst_rect;
61                struct genivi::size const &s = i.second->size;
62                printf("%d [%ux%u] (%ux%u@%dx%d), ", i.first, s.w, s.h, r.w, r.h,
63                       r.x, r.y);
64             }
65             puts("\b\b ");
66          }
67       }
68    }
69
70    return 0;
71 }
72
73 void init_layout(struct conn &c) {
74    auto &o = c.outputs.front();
75    auto &s = c.c->screens.begin()->second;
76    auto &layers = c.c->layers;
77
78    // XXX: Write output dimensions to ivi controller...
79    c.c->output_size = genivi::size{uint32_t(o->width), uint32_t(o->height)};
80
81    // Clear scene
82    layers.clear();
83
84    // Clear screen
85    s->clear();
86
87    // Setup our dummy scene...
88    c.c->layer_create(100, 0, 0);  // bottom layer, anything else
89    c.c->layer_create(1000, 0, 0); // top layer, mandelbrot
90
91    auto &l100 = c.c->layers[100];
92    auto &l1k = c.c->layers[1000];
93
94    // Set layers fullscreen
95    l100->set_destination_rectangle(0, 0, o->width, o->height);
96    l1k->set_destination_rectangle(0, 0, o->width, o->height);
97
98    // Add layers to screen
99    s->set_render_order({100, 1000});
100
101    c.c->commit_changes();
102    // Note: this does not flush the display!
103 }
104 }
105
106 int main(int argc, char **argv) {
107    lognotice("WinMan ver. %s", WINMAN_VERSION_STRING);
108
109    if (!getenv("XDG_RUNTIME_DIR"))
110       fatal("Environment variable XDG_RUNTIME_DIR not set");
111
112    auto d = std::make_unique<wl::display>();
113    if (!d->ok())
114       fatal("Could not connect to compositor");
115
116    struct conn c = {};
117
118    d->r->add_global_handler(
119       "ivi_controller", [&](wl_registry *r, uint32_t name, uint32_t v) {
120          c.c = std::make_unique<genivi::controller>(r, name, v);
121       });
122
123    d->r->add_global_handler(
124       "wl_output", [&](wl_registry *r, uint32_t name, uint32_t v) {
125          c.outputs.emplace_back(std::make_unique<wl::output>(r, name, v));
126       });
127
128    // First level objects
129    d->roundtrip();
130    // Second level objects
131    d->roundtrip();
132    // Third level objects
133    d->roundtrip();
134
135    if (!c.c)
136       fatal("ivi_controller global not available");
137
138    if (c.outputs.empty())
139       fatal("no output was set up!");
140
141    init_layout(c);
142
143    while (check_events(d.get(), &c, STDIN_FILENO) != -1) {
144       c.c->execute_pending();
145       d->flush();
146    }
147
148    d->roundtrip();
149
150    return 0;
151 }