add redraw_fixer, separate controller tool
[staging/windowmanager.git] / src / redraw_fixer.cpp
1 //
2 // Created by mfritzsc on 9/11/17.
3 //
4
5 #include "wayland.hpp"
6
7 #include <algorithm>
8 #include <chrono>
9 #include <thread>
10
11 using namespace std::chrono_literals;
12
13 // pretend we are a WM ...
14 namespace wm {
15
16 struct App {
17    controller_hooks chooks;
18    std::unique_ptr<wl::display> display;
19    std::unique_ptr<genivi::controller> controller;
20    std::vector<std::unique_ptr<wl::output>> outputs;
21
22    App();
23    void commit();
24    void surface_created(uint32_t surface_id);
25    void surface_removed(uint32_t surface_id);
26    void surface_visibility(uint32_t surface_id, uint32_t v);
27    void surface_destination_rectangle(uint32_t surface_id, uint32_t x,
28                                       uint32_t y, uint32_t w, uint32_t h);
29    void try_fix(uint32_t surface_id);
30 };
31
32 void controller_hooks::surface_created(uint32_t surface_id) {
33    this->app->surface_created(surface_id);
34 }
35
36 void controller_hooks::surface_removed(uint32_t surface_id) {
37    this->app->surface_removed(surface_id);
38 }
39
40 void controller_hooks::surface_visibility(uint32_t surface_id, uint32_t v) {
41    this->app->surface_visibility(surface_id, v);
42 }
43
44 void controller_hooks::surface_destination_rectangle(uint32_t surface_id,
45                                                      uint32_t x, uint32_t y,
46                                                      uint32_t w, uint32_t h) {
47    this->app->surface_destination_rectangle(surface_id, x, y, w, h);
48 }
49
50 App::App() : chooks{this}, display{new wl::display}, controller{} {
51    // The same init, the WM does, at least we can reuse the wayland stuff
52    if (this->display->ok()) {
53       this->display->add_global_handler("wl_output", [this](wl_registry *r,
54                                                             uint32_t name,
55                                                             uint32_t v) {
56          this->outputs.emplace_back(std::make_unique<wl::output>(r, name, v));
57       });
58
59       this->display->add_global_handler(
60          "ivi_controller", [this](wl_registry *r, uint32_t name, uint32_t v) {
61             this->controller =
62                std::make_unique<struct genivi::controller>(r, name, v);
63
64             // Init controller hooks
65             this->controller->chooks = &this->chooks;
66
67             // XXX: This protocol needs the output, so lets just add our
68             // mapping
69             // here...
70             this->controller->add_proxy_to_id_mapping(
71                this->outputs.back()->proxy.get(),
72                wl_proxy_get_id(reinterpret_cast<struct wl_proxy *>(
73                   this->outputs.back()->proxy.get())));
74          });
75
76       for (int i : {1, 2, 3})
77          this->display->roundtrip();
78    }
79 }
80
81 void App::commit() {
82    this->controller->commit_changes();
83    this->display->dispatch();  // read: flush()++
84 }
85
86 void App::surface_created(uint32_t surface_id) { }
87
88 void App::surface_removed(uint32_t surface_id) { }
89
90 void App::surface_visibility(uint32_t surface_id, uint32_t v) {
91    fprintf(stderr, "surface %u visibility %u\n", surface_id, v);
92
93    if (v == 1) {
94      this->try_fix(surface_id);
95    }
96 }
97
98 void App::surface_destination_rectangle(uint32_t surface_id, uint32_t x,
99                                         uint32_t y, uint32_t w, uint32_t h) {
100    fprintf(stderr, "surface %u dst %u %u %u %u\n", surface_id, x, y, w, h);
101
102    if (w != 1 && h != 1) {
103       this->try_fix(surface_id);
104    }
105 }
106
107 void App::try_fix(uint32_t surface_id) {
108    this->controller->surfaces[surface_id]->set_opacity(255);
109    this->commit();
110    std::this_thread::sleep_for(200ms);
111    this->controller->surfaces[surface_id]->set_opacity(256);
112    this->commit();
113 }
114
115 }  // namespace wm
116
117 int main(int argc, char **argv) {
118    wm::App app;
119    if (!app.display->ok()) {
120       fputs("Could not init wayland display\n", stderr);
121       return 1;
122    }
123    while (app.display->dispatch() != -1)
124       ;
125    return 0;
126 }