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