b945ac5165f1da40a96391b73c09308dbad8a738
[staging/windowmanager.git] / src / redraw_fixer.cpp
1 /*
2  * Copyright (C) 2017 Mentor Graphics Development (Deutschland) GmbH
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include "wayland.hpp"
18
19 #include <algorithm>
20 #include <chrono>
21 #include <thread>
22
23 using namespace std::chrono_literals;
24
25 // pretend we are a WM ...
26 namespace wm {
27
28 struct App {
29    controller_hooks chooks;
30    std::unique_ptr<wl::display> display;
31    std::unique_ptr<genivi::controller> controller;
32    std::vector<std::unique_ptr<wl::output>> outputs;
33
34    App();
35    void commit();
36    void surface_visibility(uint32_t surface_id, uint32_t v);
37    void surface_destination_rectangle(uint32_t surface_id, uint32_t x,
38                                       uint32_t y, uint32_t w, uint32_t h);
39    void try_fix(uint32_t surface_id);
40 };
41
42 void controller_hooks::surface_created(uint32_t surface_id) {}
43
44 void controller_hooks::surface_removed(uint32_t surface_id) {}
45
46 void controller_hooks::surface_visibility(uint32_t surface_id, uint32_t v) {
47    this->app->surface_visibility(surface_id, v);
48 }
49
50 void controller_hooks::surface_destination_rectangle(uint32_t surface_id,
51                                                      uint32_t x, uint32_t y,
52                                                      uint32_t w, uint32_t h) {
53    this->app->surface_destination_rectangle(surface_id, x, y, w, h);
54 }
55
56 App::App() : chooks{this}, display{new wl::display}, controller{}, outputs{} {
57    // The same init, the WM does, at least we can reuse the wayland stuff
58    if (!this->display->ok()) {
59       return;
60    }
61
62    this->display->add_global_handler(
63       "wl_output", [this](wl_registry *r, uint32_t name, uint32_t v) {
64          this->outputs.emplace_back(std::make_unique<wl::output>(r, name, v));
65       });
66
67    this->display->add_global_handler(
68       "ivi_controller", [this](wl_registry *r, uint32_t name, uint32_t v) {
69          this->controller =
70             std::make_unique<struct genivi::controller>(r, name, v);
71
72          // Init controller hooks
73          this->controller->chooks = &this->chooks;
74
75          this->controller->add_proxy_to_id_mapping(
76             this->outputs.back()->proxy.get(),
77             wl_proxy_get_id(reinterpret_cast<struct wl_proxy *>(
78                this->outputs.back()->proxy.get())));
79       });
80
81    for (int i : {1, 2, 3})
82       this->display->roundtrip();
83 }
84
85 void App::commit() {
86    this->controller->commit_changes();
87    this->display->roundtrip();  // read: flush()++
88 }
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 && this->controller->sprops[surface_id].visibility != 0) {
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 }