WIP split layouts, reading config, defining data layout.
[staging/windowmanager.git] / src / app.hpp
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 #ifndef TMCAGLWM_APP_HPP
18 #define TMCAGLWM_APP_HPP
19
20 #include <json-c/json.h>
21 #include <memory>
22 #include <unordered_map>
23 #include <unordered_set>
24 #include <deque>
25
26 #include "afb_binding_api.hpp"
27 #include "config.hpp"
28 #include "controller_hooks.hpp"
29 #include "layers.hpp"
30 #include "layout.hpp"
31 #include "result.hpp"
32 #include "wayland.hpp"
33
34 namespace wl {
35 struct display;
36 }
37
38 namespace genivi {
39 struct controller;
40 }
41
42 namespace wm {
43
44 struct id_allocator {
45    constexpr static const unsigned id_shift = 22;
46    constexpr static const unsigned id_mask = (1 << id_shift) - 1;
47
48    unsigned next = 1;
49
50    // Surfaces that where requested but not yet created
51    std::unordered_map<unsigned, std::string> id2name;
52    // std::unordered_set<unsigned> pending_surfaces;
53    std::unordered_map<std::string, unsigned> name2id;
54
55    id_allocator(id_allocator const &) = delete;
56    id_allocator(id_allocator &&) = delete;
57    id_allocator &operator=(id_allocator const &);
58    id_allocator &operator=(id_allocator &&) = delete;
59
60    // Insert and return a new ID
61    unsigned generate_id(std::string const &name) {
62       unsigned sid = this->next++;
63       this->id2name[sid] = name;
64       // this->pending_surfaces.insert({sid});
65       this->name2id[name] = sid;
66       logdebug("allocated new id %u with name %s", sid, name.c_str());
67       return sid;
68    }
69
70    // Lookup by ID or by name
71    optional<unsigned> lookup(std::string const &name) const {
72       auto i = this->name2id.find(name);
73       return i == this->name2id.end() ? nullopt : optional<unsigned>(i->second);
74    }
75
76    optional<std::string> lookup(unsigned id) const {
77       auto i = this->id2name.find(id);
78       return i == this->id2name.end() ? nullopt
79                                        : optional<std::string>(i->second);
80    }
81
82    // Remove a surface id and name
83    // I don't think I will need this, do I?
84    void remove_id(std::string const &name) {
85       auto i = this->name2id.find(name);
86       if (i != this->name2id.end()) {
87          this->id2name.erase(i->second);
88          this->name2id.erase(i);
89       }
90    }
91
92    void remove_id(unsigned id) {
93       auto i = this->id2name.find(id);
94       if (i != this->id2name.end()) {
95          this->name2id.erase(i->second);
96          this->id2name.erase(i);
97       }
98    }
99 };
100
101 struct LayoutState {
102    enum States {
103       LayoutSingle,
104       LayoutSplit,
105    };
106
107    enum States state;
108    int main;
109    int sub;
110 };
111
112 struct App {
113    struct binding_api api;
114    struct controller_hooks chooks;
115
116    // This is the one thing, we do not own.
117    struct wl::display *display;
118
119    std::unique_ptr<struct genivi::controller> controller;
120    std::vector<std::unique_ptr<struct wl::output>> outputs;
121
122    struct config config;
123
124    layouts_type layouts;
125    layer_map layers;
126
127    // ID allocation and proxy methods for lookup
128    struct id_allocator id_alloc;
129    optional<unsigned> lookup_id(char const *name) {
130       return this->id_alloc.lookup(std::string(name));
131    }
132    optional<std::string> lookup_name(unsigned id) {
133       return this->id_alloc.lookup(id);
134    }
135
136    struct LayoutState state;
137
138    explicit App(wl::display *d);
139    ~App();
140
141    App(App const &) = delete;
142    App &operator=(App const &) = delete;
143    App(App &&) = delete;
144    App &operator=(App &&) = delete;
145
146    int init();
147    int init_layers();
148
149    int dispatch_events();
150
151    void surface_init_layout(uint32_t surface_id);
152
153    // Allocate a surface ID for this role
154    result<int> request_surface(char const *drawing_name);
155
156    // Activate (i.e. make visible, if allowed!) a surface
157    char const *activate_surface(char const *drawing_name);
158    char const *deactivate_surface(char const *drawing_name);
159
160    // Events from the compositor we are interested in
161    void surface_created(uint32_t surface_id);
162    void surface_removed(uint32_t surface_id);
163
164    // TMC WM Events to clients
165    void emit_activated(char const *label);
166    void emit_deactivated(char const *label);
167    void emit_syncdraw(char const *label);
168    void emit_flushdraw(char const *label);
169    void emit_visible(char const *label, bool is_visible);
170    void emit_invisible(char const *label);
171    void emit_visible(char const *label);
172
173    void activate(unsigned id);
174    void deactivate(unsigned id);
175
176    bool can_split(unsigned new_id);
177 };
178
179 }  // namespace wm
180
181 #endif  // TMCAGLWM_APP_HPP