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