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