ea3e92b45df890b58432977e10888fea89bc2856
[apps/agl-service-windowmanager-2017.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 "policy.hpp"
34 #include "result.hpp"
35 #include "wayland.hpp"
36
37 namespace wl {
38 struct display;
39 }
40
41 namespace genivi {
42 struct controller;
43 }
44
45 namespace wm {
46
47 using std::experimental::optional;
48
49 /* DrawingArea name used by "{layout}.{area}" */
50 static const char *kNameLayoutNormal = "normal";
51 static const char *kNameLayoutSplit = "split";
52 static const char *kNameAreaFull = "full";
53 static const char *kNameAreaMain = "main";
54 static const char *kNameAreaSub = "sub";
55
56 /* Key for json obejct */
57 static const char *kKeyDrawingName = "drawing_name";
58 static const char *kKeyDrawingArea = "drawing_area";
59
60 struct id_allocator {
61    unsigned next = 1;
62
63    // Surfaces that where requested but not yet created
64    std::unordered_map<unsigned, std::string> id2name;
65    // std::unordered_set<unsigned> pending_surfaces;
66    std::unordered_map<std::string, unsigned> name2id;
67
68    id_allocator(id_allocator const &) = delete;
69    id_allocator(id_allocator &&) = delete;
70    id_allocator &operator=(id_allocator const &);
71    id_allocator &operator=(id_allocator &&) = delete;
72
73    // Insert and return a new ID
74    unsigned generate_id(std::string const &name) {
75       unsigned sid = this->next++;
76       this->id2name[sid] = name;
77       // this->pending_surfaces.insert({sid});
78       this->name2id[name] = sid;
79       logdebug("allocated new id %u with name %s", sid, name.c_str());
80       return sid;
81    }
82
83    // Lookup by ID or by name
84    optional<unsigned> lookup(std::string const &name) const {
85       auto i = this->name2id.find(name);
86       return i == this->name2id.end() ? nullopt : optional<unsigned>(i->second);
87    }
88
89    optional<std::string> lookup(unsigned id) const {
90       auto i = this->id2name.find(id);
91       return i == this->id2name.end() ? nullopt
92                                        : optional<std::string>(i->second);
93    }
94
95    // Remove a surface id and name
96    // I don't think I will need this, do I?
97    void remove_id(std::string const &name) {
98       auto i = this->name2id.find(name);
99       if (i != this->name2id.end()) {
100          this->id2name.erase(i->second);
101          this->name2id.erase(i);
102       }
103    }
104
105    void remove_id(unsigned id) {
106       auto i = this->id2name.find(id);
107       if (i != this->id2name.end()) {
108          this->name2id.erase(i->second);
109          this->id2name.erase(i);
110       }
111    }
112 };
113
114 struct App {
115    struct binding_api api;
116    struct controller_hooks chooks;
117
118    // This is the one thing, we do not own.
119    struct wl::display *display;
120
121    std::unique_ptr<struct genivi::controller> controller;
122    std::vector<std::unique_ptr<struct wl::output>> outputs;
123
124    struct config config;
125
126    // track current layouts separately
127    layer_map layers;
128
129    // ID allocation and proxy methods for lookup
130    struct id_allocator id_alloc;
131
132    // Set by AFB API when wayland events need to be dispatched
133    std::atomic<bool> pending_events;
134
135    std::vector<int> pending_end_draw;
136
137    Policy policy;
138
139    explicit App(wl::display *d);
140    ~App() = default;
141
142    App(App const &) = delete;
143    App &operator=(App const &) = delete;
144    App(App &&) = delete;
145    App &operator=(App &&) = delete;
146
147    int init();
148
149    int dispatch_events();
150    int dispatch_pending_events();
151
152    void set_pending_events();
153
154    result<int> api_request_surface(char const *drawing_name);
155    char const *api_activate_surface(char const *drawing_name, char const *drawing_area);
156    char const *api_deactivate_surface(char const *drawing_name);
157    char const *api_enddraw(char const *drawing_name);
158    void api_ping();
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 private:
165    optional<int> lookup_id(char const *name);
166    optional<std::string> lookup_name(int id);
167
168    bool pop_pending_events();
169
170    void enqueue_flushdraw(int surface_id);
171    void check_flushdraw(int surface_id);
172
173    int init_layers();
174
175    void surface_set_layout(int surface_id, optional<int> sub_surface_id = nullopt);
176    void layout_commit();
177
178    // TMC WM Events to clients
179    void emit_activated(char const *label);
180    void emit_deactivated(char const *label);
181    void emit_syncdraw(char const *label, char const *area);
182    void emit_flushdraw(char const *label);
183    void emit_visible(char const *label, bool is_visible);
184    void emit_invisible(char const *label);
185    void emit_visible(char const *label);
186
187    void activate(int id);
188    void deactivate(int id);
189    void deactivate_main_surface();
190
191    bool can_split(struct LayoutState const &state, int new_id);
192    void try_layout(struct LayoutState &state,
193                    struct LayoutState const &new_layout,
194                    std::function<void(LayoutState const &nl)> apply);
195 };
196
197 }  // namespace wm
198
199 #endif  // TMCAGLWM_APP_HPP