c9b5112038489fce8b7a75cfd205eaca9d1dcd17
[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_visibility(uint32_t surface_id, uint32_t v);
25    void surface_destination_rectangle(uint32_t surface_id, uint32_t x,
26                                       uint32_t y, uint32_t w, uint32_t h);
27    void try_fix(uint32_t surface_id);
28 };
29
30 void controller_hooks::surface_created(uint32_t surface_id) {}
31
32 void controller_hooks::surface_removed(uint32_t surface_id) {}
33
34 void controller_hooks::surface_visibility(uint32_t surface_id, uint32_t v) {
35    this->app->surface_visibility(surface_id, v);
36 }
37
38 void controller_hooks::surface_destination_rectangle(uint32_t surface_id,
39                                                      uint32_t x, uint32_t y,
40                                                      uint32_t w, uint32_t h) {
41    this->app->surface_destination_rectangle(surface_id, x, y, w, h);
42 }
43
44 App::App() : chooks{this}, display{new wl::display}, controller{}, outputs{} {
45    // The same init, the WM does, at least we can reuse the wayland stuff
46    if (!this->display->ok()) {
47       return;
48    }
49
50    this->display->add_global_handler(
51       "wl_output", [this](wl_registry *r, uint32_t name, uint32_t v) {
52          this->outputs.emplace_back(std::make_unique<wl::output>(r, name, v));
53       });
54
55    this->display->add_global_handler(
56       "ivi_controller", [this](wl_registry *r, uint32_t name, uint32_t v) {
57          this->controller =
58             std::make_unique<struct genivi::controller>(r, name, v);
59
60          // Init controller hooks
61          this->controller->chooks = &this->chooks;
62
63          this->controller->add_proxy_to_id_mapping(
64             this->outputs.back()->proxy.get(),
65             wl_proxy_get_id(reinterpret_cast<struct wl_proxy *>(
66                this->outputs.back()->proxy.get())));
67       });
68
69    for (int i : {1, 2, 3})
70       this->display->roundtrip();
71 }
72
73 void App::commit() {
74    this->controller->commit_changes();
75    this->display->roundtrip();  // read: flush()++
76 }
77
78 void App::surface_visibility(uint32_t surface_id, uint32_t v) {
79    fprintf(stderr, "surface %u visibility %u\n", surface_id, v);
80
81    if (v == 1) {
82       this->try_fix(surface_id);
83    }
84 }
85
86 void App::surface_destination_rectangle(uint32_t surface_id, uint32_t x,
87                                         uint32_t y, uint32_t w, uint32_t h) {
88    fprintf(stderr, "surface %u dst %u %u %u %u\n", surface_id, x, y, w, h);
89
90    if (w != 1 && h != 1 && this->controller->sprops[surface_id].visibility != 0) {
91       this->try_fix(surface_id);
92    }
93 }
94
95 void App::try_fix(uint32_t surface_id) {
96    this->controller->surfaces[surface_id]->set_opacity(255);
97    this->commit();
98    std::this_thread::sleep_for(200ms);
99    this->controller->surfaces[surface_id]->set_opacity(256);
100    this->commit();
101 }
102
103 }  // namespace wm
104
105 int main(int argc, char **argv) {
106    wm::App app;
107    if (!app.display->ok()) {
108       fputs("Could not init wayland display\n", stderr);
109       return 1;
110    }
111    while (app.display->dispatch() != -1)
112       ;
113    return 0;
114 }