5ec19ae471d55507f5934b5ffe2bf9810b7c8a30
[apps/agl-service-windowmanager-2017.git] / src / app.hpp
1 /*
2  * Copyright (c) 2017 TOYOTA MOTOR CORPORATION
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 #include "config.hpp"
28 #include "controller_hooks.hpp"
29 #include "layers.hpp"
30 #include "layout.hpp"
31 #include "policy.hpp"
32 #include "result.hpp"
33 #include "wayland.hpp"
34 #include "hmi-debug.h"
35
36 namespace wl {
37 struct display;
38 }
39
40 namespace compositor {
41 struct controller;
42 }
43
44 namespace wm {
45
46 using std::experimental::optional;
47
48 /* DrawingArea name used by "{layout}.{area}" */
49 static const char *kNameLayoutNormal = "normal";
50 static const char *kNameLayoutSplit = "split";
51 static const char *kNameAreaFull = "full";
52 static const char *kNameAreaMain = "main";
53 static const char *kNameAreaSub = "sub";
54
55 /* Key for json obejct */
56 static const char *kKeyDrawingName = "drawing_name";
57 static const char *kKeyDrawingArea = "drawing_area";
58
59 struct id_allocator {
60    unsigned next = 1;
61
62    // Surfaces that where requested but not yet created
63    std::unordered_map<unsigned, std::string> id2name;
64    // std::unordered_set<unsigned> pending_surfaces;
65    std::unordered_map<std::string, unsigned> name2id;
66
67    id_allocator(id_allocator const &) = delete;
68    id_allocator(id_allocator &&) = delete;
69    id_allocator &operator=(id_allocator const &);
70    id_allocator &operator=(id_allocator &&) = delete;
71
72    // Insert and return a new ID
73    unsigned generate_id(std::string const &name) {
74       unsigned sid = this->next++;
75       this->id2name[sid] = name;
76       // this->pending_surfaces.insert({sid});
77       this->name2id[name] = sid;
78       HMI_DEBUG("wm", "allocated new id %u with name %s", sid, name.c_str());
79       return sid;
80    }
81
82    // Insert a new ID which defined outside
83    void register_name_id(std::string const &name, unsigned sid) {
84       this->id2name[sid] = name;
85       this->name2id[name] = sid;
86       HMI_DEBUG("wm", "register id %u with name %s", sid, name.c_str());
87       return;
88    }
89
90    // Lookup by ID or by name
91    optional<unsigned> lookup(std::string const &name) const {
92       auto i = this->name2id.find(name);
93       return i == this->name2id.end() ? nullopt : optional<unsigned>(i->second);
94    }
95
96    optional<std::string> lookup(unsigned id) const {
97       auto i = this->id2name.find(id);
98       return i == this->id2name.end() ? nullopt
99                                        : optional<std::string>(i->second);
100    }
101
102    // Remove a surface id and name
103    void remove_id(std::string const &name) {
104       auto i = this->name2id.find(name);
105       if (i != this->name2id.end()) {
106          this->id2name.erase(i->second);
107          this->name2id.erase(i);
108       }
109    }
110
111    void remove_id(unsigned id) {
112       auto i = this->id2name.find(id);
113       if (i != this->id2name.end()) {
114          this->name2id.erase(i->second);
115          this->id2name.erase(i);
116       }
117    }
118 };
119
120 struct App {
121    enum EventType {
122       Event_Val_Min = 0,
123
124       Event_Active = Event_Val_Min,
125       Event_Inactive,
126
127       Event_Visible,
128       Event_Invisible,
129
130       Event_SyncDraw,
131       Event_FlushDraw,
132
133       Event_Val_Max = Event_FlushDraw,
134    };
135
136    const std::vector<const char *> kListEventName{
137      "active",
138      "inactive",
139      "visible",
140      "invisible",
141      "syncdraw",
142      "flushdraw"
143    };
144
145    struct controller_hooks chooks;
146
147    // This is the one thing, we do not own.
148    struct wl::display *display;
149
150    std::unique_ptr<struct compositor::controller> controller;
151    std::vector<std::unique_ptr<struct wl::output>> outputs;
152
153    struct config config;
154
155    // track current layouts separately
156    layer_map layers;
157
158    // ID allocation and proxy methods for lookup
159    struct id_allocator id_alloc;
160
161    // Set by AFB API when wayland events need to be dispatched
162    std::atomic<bool> pending_events;
163
164    std::vector<int> pending_end_draw;
165
166    Policy policy;
167
168    std::map<const char *, struct afb_event> map_afb_event;
169
170    // FOR CES DEMO
171    std::vector<int> surface_bg;
172
173    explicit App(wl::display *d);
174    ~App() = default;
175
176    App(App const &) = delete;
177    App &operator=(App const &) = delete;
178    App(App &&) = delete;
179    App &operator=(App &&) = delete;
180
181    int init();
182
183    int dispatch_events();
184    int dispatch_pending_events();
185
186    void set_pending_events();
187
188    result<int> api_request_surface(char const *drawing_name);
189    char const *api_request_surface(char const *drawing_name, char const *ivi_id);
190    char const *api_activate_surface(char const *drawing_name, char const *drawing_area);
191    char const *api_deactivate_surface(char const *drawing_name);
192    char const *api_enddraw(char const *drawing_name);
193    char const *api_subscribe(afb_req *req, char const *event_name);
194    void api_ping();
195    void send_event(char const *evname, char const *label);
196    void send_event(char const *evname, char const *label, char const *area);
197
198    // Events from the compositor we are interested in
199    void surface_created(uint32_t surface_id);
200    void surface_removed(uint32_t surface_id);
201
202 private:
203    optional<int> lookup_id(char const *name);
204    optional<std::string> lookup_name(int id);
205
206    bool pop_pending_events();
207
208    void enqueue_flushdraw(int surface_id);
209    void check_flushdraw(int surface_id);
210
211    int init_layers();
212
213    void surface_set_layout(int surface_id, optional<int> sub_surface_id = nullopt);
214    void layout_commit();
215
216    // TMC WM Events to clients
217    void emit_activated(char const *label);
218    void emit_deactivated(char const *label);
219    void emit_syncdraw(char const *label, char const *area);
220    void emit_flushdraw(char const *label);
221    void emit_visible(char const *label, bool is_visible);
222    void emit_invisible(char const *label);
223    void emit_visible(char const *label);
224
225    void activate(int id);
226    void deactivate(int id);
227    void deactivate_main_surface();
228
229    bool can_split(struct LayoutState const &state, int new_id);
230    void try_layout(struct LayoutState &state,
231                    struct LayoutState const &new_layout,
232                    std::function<void(LayoutState const &nl)> apply);
233 };
234
235 }  // namespace wm
236
237 #endif  // TMCAGLWM_APP_HPP