Add APIs which can get information of display and area
[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 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 static const char *kKeyDrawingRect = "drawing_rect";
61 static const char *kKeyX           = "x";
62 static const char *kKeyY           = "y";
63 static const char *kKeyWidth       = "width";
64 static const char *kKeyHeight      = "height";
65 static const char *kKeyWidthPixel  = "width_pixel";
66 static const char *kKeyHeightPixel = "height_pixel";
67 static const char *kKeyWidthMm     = "width_mm";
68 static const char *kKeyHeightMm    = "height_mm";
69
70
71
72 struct id_allocator {
73    unsigned next = 1;
74
75    // Surfaces that where requested but not yet created
76    std::unordered_map<unsigned, std::string> id2name;
77    // std::unordered_set<unsigned> pending_surfaces;
78    std::unordered_map<std::string, unsigned> name2id;
79
80    id_allocator(id_allocator const &) = delete;
81    id_allocator(id_allocator &&) = delete;
82    id_allocator &operator=(id_allocator const &);
83    id_allocator &operator=(id_allocator &&) = delete;
84
85    // Insert and return a new ID
86    unsigned generate_id(std::string const &name) {
87       unsigned sid = this->next++;
88       this->id2name[sid] = name;
89       // this->pending_surfaces.insert({sid});
90       this->name2id[name] = sid;
91       HMI_DEBUG("wm", "allocated new id %u with name %s", sid, name.c_str());
92       return sid;
93    }
94
95    // Lookup by ID or by name
96    optional<unsigned> lookup(std::string const &name) const {
97       auto i = this->name2id.find(name);
98       return i == this->name2id.end() ? nullopt : optional<unsigned>(i->second);
99    }
100
101    optional<std::string> lookup(unsigned id) const {
102       auto i = this->id2name.find(id);
103       return i == this->id2name.end() ? nullopt
104                                        : optional<std::string>(i->second);
105    }
106
107    // Remove a surface id and name
108    void remove_id(std::string const &name) {
109       auto i = this->name2id.find(name);
110       if (i != this->name2id.end()) {
111          this->id2name.erase(i->second);
112          this->name2id.erase(i);
113       }
114    }
115
116    void remove_id(unsigned id) {
117       auto i = this->id2name.find(id);
118       if (i != this->id2name.end()) {
119          this->name2id.erase(i->second);
120          this->id2name.erase(i);
121       }
122    }
123 };
124
125 struct App {
126
127    typedef std::unordered_map<uint32_t, struct compositor::rect> rect_map;
128
129    enum EventType {
130       Event_Val_Min = 0,
131
132       Event_Active = Event_Val_Min,
133       Event_Inactive,
134
135       Event_Visible,
136       Event_Invisible,
137
138       Event_SyncDraw,
139       Event_FlushDraw,
140
141       Event_Val_Max = Event_FlushDraw,
142    };
143
144    const std::vector<const char *> kListEventName{
145      "active",
146      "inactive",
147      "visible",
148      "invisible",
149      "syncdraw",
150      "flushdraw"
151    };
152
153    struct binding_api api;
154    struct controller_hooks chooks;
155
156    // This is the one thing, we do not own.
157    struct wl::display *display;
158
159    std::unique_ptr<struct compositor::controller> controller;
160    std::vector<std::unique_ptr<struct wl::output>> outputs;
161
162    struct config config;
163
164    // track current layouts separately
165    layer_map layers;
166
167    // ID allocation and proxy methods for lookup
168    struct id_allocator id_alloc;
169
170    // Set by AFB API when wayland events need to be dispatched
171    std::atomic<bool> pending_events;
172
173    std::vector<int> pending_end_draw;
174
175    Policy policy;
176
177    std::map<const char *, struct afb_event> map_afb_event;
178
179    rect_map area_info;
180
181    explicit App(wl::display *d);
182    ~App() = default;
183
184    App(App const &) = delete;
185    App &operator=(App const &) = delete;
186    App(App &&) = delete;
187    App &operator=(App &&) = delete;
188
189    int init();
190
191    int dispatch_events();
192    int dispatch_pending_events();
193
194    void set_pending_events();
195
196    result<int> api_request_surface(char const *drawing_name);
197    char const *api_activate_surface(char const *drawing_name, char const *drawing_area);
198    char const *api_deactivate_surface(char const *drawing_name);
199    char const *api_enddraw(char const *drawing_name);
200    result<json_object *> api_get_display_info();
201    result<json_object *> api_get_area_info(char const *drawing_name);
202    char const *api_subscribe(afb_req *req, char const *event_name);
203    void api_ping();
204
205    // Events from the compositor we are interested in
206    void surface_created(uint32_t surface_id);
207    void surface_removed(uint32_t surface_id);
208
209 private:
210    optional<int> lookup_id(char const *name);
211    optional<std::string> lookup_name(int id);
212
213    bool pop_pending_events();
214
215    void enqueue_flushdraw(int surface_id);
216    void check_flushdraw(int surface_id);
217
218    int init_layers();
219
220    void surface_set_layout(int surface_id, optional<int> sub_surface_id = nullopt);
221    void layout_commit();
222
223    // TMC WM Events to clients
224    void emit_activated(char const *label);
225    void emit_deactivated(char const *label);
226    void emit_syncdraw(char const *label, char const *area, int x, int y, int w, int h);
227    void emit_flushdraw(char const *label);
228    void emit_visible(char const *label, bool is_visible);
229    void emit_invisible(char const *label);
230    void emit_visible(char const *label);
231
232    void activate(int id);
233    void deactivate(int id);
234    void deactivate_main_surface();
235
236    bool can_split(struct LayoutState const &state, int new_id);
237    void try_layout(struct LayoutState &state,
238                    struct LayoutState const &new_layout,
239                    std::function<void(LayoutState const &nl)> apply);
240 };
241
242 }  // namespace wm
243
244 #endif  // TMCAGLWM_APP_HPP