wayland: set surfaces fullscreen when configured.
[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    }
58
59    return 0;
60 }
61
62 void init_layout(struct conn &c) {
63    struct wl::output &o = *c.outputs.begin()->get();
64    struct genivi::screen &s = *c.c->screens.begin()->second;
65    auto &layers = c.c->layers;
66
67    // XXX: Write output dimensions to ivi controller...
68    c.c->output_size = genivi::size{uint32_t(o.width), uint32_t(o.height)};
69
70    // Setup our dummy scene...
71    if (layers.find(100) == layers.end()) {
72       logdebug("Creating layer 100 with output dimensions (%ux%u)", o.width, o.height);
73       c.c->layer_create(100, o.width, o.height);
74    }
75
76    struct genivi::layer &l = *c.c->layers[100].get();
77
78    l.set_destination_rectangle(0, 0, o.width, o.height);
79    s.clear();
80
81    logdebug("Add layer 100 to screen %u", s.id);
82    s.add_layer(&l);
83
84    c.c->commit_changes();
85    // Note: this does not flush the display!
86 }
87 }
88
89 int main(int argc, char **argv) {
90    lognotice("WinMan ver. %s", WINMAN_VERSION_STRING);
91
92    if (!getenv("XDG_RUNTIME_DIR"))
93       fatal("Environment variable XDG_RUNTIME_DIR not set");
94
95    auto d = std::make_unique<wl::display>();
96    if (!d->ok())
97       fatal("Could not connect to compositor");
98
99    struct conn c = {};
100
101    d->r->add_global_handler(
102       "ivi_controller", [&](wl_registry *r, uint32_t name, uint32_t v) {
103          c.c = std::make_unique<genivi::controller>(r, name, v);
104       });
105
106    d->r->add_global_handler(
107       "wl_output", [&](wl_registry *r, uint32_t name, uint32_t v) {
108          c.outputs.emplace_back(std::make_unique<wl::output>(r, name, v));
109       });
110
111    // First level objects
112    d->roundtrip();
113    // Second level objects
114    d->roundtrip();
115    // Third level objects
116    d->roundtrip();
117
118    if (!c.c)
119       fatal("ivi_controller global not available");
120
121    if (c.outputs.empty())
122       fatal("no output was set up!");
123
124    init_layout(c);
125
126    while (check_events(d.get(), &c, STDIN_FILENO) != -1) {
127       c.c->execute_pending();
128    }
129
130    d->roundtrip();
131
132    return 0;
133 }