Modify event notification from broadcast to subscribe model
[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    void remove_id(std::string const &name) {
97       auto i = this->name2id.find(name);
98       if (i != this->name2id.end()) {
99          this->id2name.erase(i->second);
100          this->name2id.erase(i);
101       }
102    }
103
104    void remove_id(unsigned id) {
105       auto i = this->id2name.find(id);
106       if (i != this->id2name.end()) {
107          this->name2id.erase(i->second);
108          this->id2name.erase(i);
109       }
110    }
111 };
112
113 struct App {
114    enum EventType {
115       Event_Val_Min = 0,
116
117       Event_Active = Event_Val_Min,
118       Event_Inactive,
119
120       Event_Visible,
121       Event_Invisible,
122
123       Event_SyncDraw,
124       Event_FlushDraw,
125
126       Event_Val_Max = Event_FlushDraw,
127    };
128
129    const std::vector<const char *> kListEventName{
130      "active",
131      "inactive",
132      "visible",
133      "invisible",
134      "syncdraw",
135      "flushdraw"
136    };
137
138    struct binding_api api;
139    struct controller_hooks chooks;
140
141    // This is the one thing, we do not own.
142    struct wl::display *display;
143
144    std::unique_ptr<struct genivi::controller> controller;
145    std::vector<std::unique_ptr<struct wl::output>> outputs;
146
147    struct config config;
148
149    // track current layouts separately
150    layer_map layers;
151
152    // ID allocation and proxy methods for lookup
153    struct id_allocator id_alloc;
154
155    // Set by AFB API when wayland events need to be dispatched
156    std::atomic<bool> pending_events;
157
158    std::vector<int> pending_end_draw;
159
160    Policy policy;
161
162    std::map<const char *, struct afb_event> map_afb_event;
163
164    explicit App(wl::display *d);
165    ~App() = default;
166
167    App(App const &) = delete;
168    App &operator=(App const &) = delete;
169    App(App &&) = delete;
170    App &operator=(App &&) = delete;
171
172    int init();
173
174    int dispatch_events();
175    int dispatch_pending_events();
176
177    void set_pending_events();
178
179    result<int> api_request_surface(char const *drawing_name);
180    char const *api_activate_surface(char const *drawing_name, char const *drawing_area);
181    char const *api_deactivate_surface(char const *drawing_name);
182    char const *api_enddraw(char const *drawing_name);
183    char const *api_subscribe(afb_req *req, char const *event_name);
184    void api_ping();
185
186    // Events from the compositor we are interested in
187    void surface_created(uint32_t surface_id);
188    void surface_removed(uint32_t surface_id);
189
190 private:
191    optional<int> lookup_id(char const *name);
192    optional<std::string> lookup_name(int id);
193
194    bool pop_pending_events();
195
196    void enqueue_flushdraw(int surface_id);
197    void check_flushdraw(int surface_id);
198
199    int init_layers();
200
201    void surface_set_layout(int surface_id, optional<int> sub_surface_id = nullopt);
202    void layout_commit();
203
204    // TMC WM Events to clients
205    void emit_activated(char const *label);
206    void emit_deactivated(char const *label);
207    void emit_syncdraw(char const *label, char const *area);
208    void emit_flushdraw(char const *label);
209    void emit_visible(char const *label, bool is_visible);
210    void emit_invisible(char const *label);
211    void emit_visible(char const *label);
212
213    void activate(int id);
214    void deactivate(int id);
215    void deactivate_main_surface();
216
217    bool can_split(struct LayoutState const &state, int new_id);
218    void try_layout(struct LayoutState &state,
219                    struct LayoutState const &new_layout,
220                    std::function<void(LayoutState const &nl)> apply);
221 };
222
223 }  // namespace wm
224
225 #endif  // TMCAGLWM_APP_HPP