redraw_fixer: a couple of style fixes
[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
86 void App::commit() {
87    this->controller->commit_changes();
88    this->display->roundtrip();  // read: flush()++
89 }
90
91 void App::surface_visibility(uint32_t surface_id, uint32_t v) {
92    fprintf(stderr, "surface %u visibility %u\n", surface_id, v);
93
94    if (v == 1) {
95       this->try_fix(surface_id);
96    }
97 }
98
99 void App::surface_destination_rectangle(uint32_t surface_id, uint32_t x,
100                                         uint32_t y, uint32_t w, uint32_t h) {
101    fprintf(stderr, "surface %u dst %u %u %u %u\n", surface_id, x, y, w, h);
102
103    if (w != 1 && h != 1 && this->controller->sprops[surface_id].visibility != 0) {
104       this->try_fix(surface_id);
105    }
106 }
107
108 void App::try_fix(uint32_t surface_id) {
109    this->controller->surfaces[surface_id]->set_opacity(255);
110    this->commit();
111    std::this_thread::sleep_for(200ms);
112    this->controller->surfaces[surface_id]->set_opacity(256);
113    this->commit();
114 }
115
116 }  // namespace wm
117
118 int main(int /*argc*/, char ** /*argv*/) {
119    wm::App app;
120    if (!app.display->ok()) {
121       fputs("Could not init wayland display\n", stderr);
122       return 1;
123    }
124    while (app.display->dispatch() != -1) {
125       ;
126    }
127    return 0;
128 }