main/wayland: added a simple main loop
[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 {
24    struct pollfd pfd[2] = {
25       { .fd = d->get_fd(), .events = POLLIN, .revents = 0 },
26       { .fd = fd, .events = POLLIN, .revents = 0 }
27    };
28
29    d->flush();
30
31    if (poll(pfd, fd != -1 ? 2 : 1, -1) != -1 && errno != EINTR) {
32       int ret = 0;
33
34       if (pfd[0].revents & POLLIN) {
35          ret = d->dispatch();
36       }
37
38       if (ret == -1)
39          return ret;
40
41       if (fd != -1 && (pfd[1].revents & POLLIN)) {
42          char buf[10];
43
44          while (read(pfd[1].fd, buf, 10) == 10)
45             ;
46
47          // Display current status
48          for (auto const &i : c->c->surfaces) {
49             printf("Surface %d\n", i.first);
50          }
51       }
52    }
53
54    return 0;
55 }
56 }
57
58 int main(int argc, char **argv) {
59    lognotice("WinMan ver. %s", WINMAN_VERSION_STRING);
60
61    if (!getenv("XDG_RUNTIME_DIR"))
62       fatal("Environment variable XDG_RUNTIME_DIR not set");
63
64    auto d = std::make_unique<wl::display>();
65    if (!d->ok())
66       fatal("Could not connect to compositor");
67
68    struct conn c = {};
69
70    d->r->add_global_handler(
71       "ivi_controller", [&](wl_registry *r, uint32_t name, uint32_t v) {
72          c.c = std::make_unique<genivi::controller>(r, name, v);
73       });
74
75    d->r->add_global_handler(
76       "wl_output", [&](wl_registry *r, uint32_t name, uint32_t v) {
77          c.outputs.emplace_back(std::make_unique<wl::output>(r, name, v));
78       });
79
80    // First level objects
81    d->roundtrip();
82    // Second level objects
83    d->roundtrip();
84    // Third level objects
85    d->roundtrip();
86
87    if (!c.c)
88       fatal("ivi_controller global not available");
89
90    while (check_events(d.get(), &c, STDIN_FILENO) != -1) {
91       ;
92    }
93
94    d->roundtrip();
95
96    return 0;
97 }