util: move DB() debug macro from app
[staging/windowmanager.git] / src / app.cpp
1 //
2 // Created by mfritzsc on 7/11/17.
3 //
4
5 #include "app.hpp"
6 #include "json_helper.hpp"
7 #include "layout.hpp"
8 #include "util.hpp"
9 #include "wayland.hpp"
10
11 #include <cstdio>
12 #include <memory>
13
14 #include <cassert>
15
16 #include <json-c/json.h>
17
18 #include <fstream>
19 #include <json.hpp>
20
21 namespace wm {
22
23 namespace {
24 App *g_app;
25
26 using json = nlohmann::json;
27
28 struct wm::area area_from_json(json const &j) {
29    DB(j);
30    return wm::area{
31       j["name"].get<std::string>(),
32       {
33          get<uint32_t>(j["width"]),
34          get<uint32_t>(j["height"]),
35          get<int32_t>(j["x"]),
36          get<int32_t>(j["y"]),
37       },
38       get<uint32_t>(j["zorder"]),
39    };
40 }
41
42 result<struct layout> layout_from_json(json const &j) {
43    DB(j);
44    auto &ja = j["areas"];
45
46    auto l = wm::layout{j["name"].get<std::string>(), uint32_t(ja.size()), {}};
47
48    if (ja.size() > layout::MAX_N_AREAS) {
49       return Err<struct layout>("Invalid number of areas in layout");
50    }
51
52    logdebug("Loading layout '%s' with %u areas", l.name.c_str(),
53             unsigned(ja.size()));
54
55    std::transform(std::cbegin(ja), std::cend(ja), std::begin(l.areas),
56                   area_from_json);
57
58    return Ok(l);
59 }
60
61 struct result<layouts_type> load_layout(char const *filename) {
62    DB("loading layout from " << filename);
63
64    json jlayouts;
65    std::ifstream i(filename);
66    i >> jlayouts;
67
68    auto layouts = layouts_type();
69    std::transform(std::cbegin(jlayouts), std::cend(jlayouts),
70                   std::back_inserter(layouts), layout_from_json);
71
72    return Ok(layouts);
73 }
74
75 }  // namespace
76
77 App::App(wl::display *d)
78    : api{this},
79      display{d},
80      controller{},
81      outputs(),
82      layouts() {
83      // layouts(load_layout("../layout.json").unwrap()) {
84    assert(g_app == nullptr);
85    g_app = this;
86
87    auto l = load_layout("../layout.json");
88    if (l.is_err()) {
89       DB("Could not load layout: " << l.unwrap_err());
90    }
91 }
92
93 App::~App() { g_app = nullptr; }
94
95 int App::init() {
96    if (!this->display->ok()) {
97       return -1;
98    }
99
100    this->display->add_global_handler(
101       "wl_output", [this](wl_registry *r, uint32_t name, uint32_t v) {
102          this->outputs.emplace_back(std::make_unique<wl::output>(r, name, v));
103       });
104
105    this->display->add_global_handler(
106       "ivi_controller", [this](wl_registry *r, uint32_t name, uint32_t v) {
107          this->controller = std::make_unique<genivi::controller>(r, name, v);
108
109          // XXX: This protocol needs the output, so lets just add our mapping
110          // here...
111          this->controller->add_proxy_to_id_mapping(
112             this->outputs.back()->proxy.get(),
113             wl_proxy_get_id(reinterpret_cast<struct wl_proxy *>(
114                this->outputs.back()->proxy.get())));
115       });
116
117    // First level objects
118    this->display->roundtrip();
119    // Second level objects
120    this->display->roundtrip();
121    // Third level objects
122    this->display->roundtrip();
123
124    return init_layout();
125 }
126
127 int App::dispatch_events() {
128    int ret = this->display->dispatch();
129    if (ret == -1) {
130       logerror("wl_display_dipatch() returned error %d",
131                this->display->get_error());
132       return -1;
133    }
134    this->display->flush();
135
136    // execute pending tasks, that is layout changes etc.
137    this->controller->execute_pending();
138    this->display->roundtrip();
139
140    return 0;
141 }
142
143 //  _       _ _       _                         _    ____
144 // (_)_ __ (_) |_    | | __ _ _   _  ___  _   _| |_ / /\ \
145 // | | '_ \| | __|   | |/ _` | | | |/ _ \| | | | __| |  | |
146 // | | | | | | |_    | | (_| | |_| | (_) | |_| | |_| |  | |
147 // |_|_| |_|_|\__|___|_|\__,_|\__, |\___/ \__,_|\__| |  | |
148 //              |_____|       |___/                 \_\/_/
149 int App::init_layout() {
150    if (!this->controller) {
151       logerror("ivi_controller global not available");
152       return -1;
153    }
154
155    if (this->outputs.empty()) {
156       logerror("no output was set up!");
157       return -1;
158    }
159
160    auto &c = this->controller;
161
162    auto &o = this->outputs.front();
163    auto &s = c->screens.begin()->second;
164    auto &layers = c->layers;
165
166    // XXX: Write output dimensions to ivi controller...
167    c->output_size = genivi::size{uint32_t(o->width), uint32_t(o->height)};
168
169    // Clear scene
170    layers.clear();
171
172    // Clear screen
173    s->clear();
174
175    // Setup our dummy scene...
176    c->layer_create(100, 0, 0);   // bottom layer, anything else
177    c->layer_create(1000, 0, 0);  // top layer, mandelbrot
178
179    auto &l100 = c->layers[100];
180    auto &l1k = c->layers[1000];
181
182    // Set layers fullscreen
183    l100->set_destination_rectangle(0, 0, o->width, o->height);
184    l1k->set_destination_rectangle(0, 0, o->width, o->height);
185    l100->set_visibility(1);
186    l1k->set_visibility(1);
187
188    // Add layers to screen
189    s->set_render_order({100, 1000});
190
191    c->commit_changes();
192
193    this->display->flush();
194
195    return 0;
196 }
197
198 binding_api::result_type binding_api::register_surface(uint32_t appid,
199                                                        uint32_t surfid) {
200    logdebug("%s appid %u surfid %u", __func__, appid, surfid);
201    if (appid > 0xff) {
202       return Err<json_object *>("invalid appid");
203    }
204
205    if (surfid > 0xffff) {
206       return Err<json_object *>("invalid surfaceid");
207    }
208
209    return Ok(json_object_new_int((appid << 16) + surfid));
210 }
211
212 binding_api::result_type binding_api::debug_layers() {
213    logdebug("%s", __func__);
214    return Ok(to_json(this->app->controller->lprops));
215 }
216
217 binding_api::result_type binding_api::debug_surfaces() {
218    logdebug("%s", __func__);
219    return Ok(to_json(this->app->controller->sprops));
220 }
221
222 binding_api::result_type binding_api::debug_status() {
223    logdebug("%s", __func__);
224    json_object *jr = json_object_new_object();
225    json_object_object_add(jr, "surfaces",
226                           to_json(this->app->controller->sprops));
227    json_object_object_add(jr, "layers", to_json(this->app->controller->lprops));
228    return Ok(jr);
229 }
230
231 }  // namespace wm