Remove unnecesarry member variable: role2surfaceid_
[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 #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_ivi_wm.hpp"
34 #include "policy_manager.hpp"
35 #include "hmi-debug.h"
36
37 namespace wl {
38 struct display;
39 }
40
41 namespace compositor {
42 struct controller;
43 }
44
45 namespace wm {
46
47 using std::experimental::optional;
48
49 /* DrawingArea name used by "{layout}.{area}" */
50 extern const char kNameLayoutNormal[];
51 extern const char kNameLayoutSplit[];
52 extern const char kNameAreaFull[];
53 extern const char kNameAreaMain[];
54 extern const char kNameAreaSub[];
55
56 /* Key for json obejct */
57 extern const char kKeyDrawingName[];
58 extern const char kKeyDrawingArea[];
59 extern const char kKeyDrawingRect[];
60 extern const char kKeyX[];
61 extern const char kKeyY[];
62 extern const char kKeyWidth[];
63 extern const char kKeyHeigh[];
64 extern const char kKeyWidthPixel[];
65 extern const char kKeyHeightPixel[];
66 extern const char kKeyWidthMm[];
67 extern const char kKeyHeightMm[];
68
69
70 struct id_allocator {
71    unsigned next = 1;
72
73    // Surfaces that where requested but not yet created
74    std::unordered_map<unsigned, std::string> id2name;
75    // std::unordered_set<unsigned> pending_surfaces;
76    std::unordered_map<std::string, unsigned> name2id;
77
78    id_allocator(id_allocator const &) = delete;
79    id_allocator(id_allocator &&) = delete;
80    id_allocator &operator=(id_allocator const &);
81    id_allocator &operator=(id_allocator &&) = delete;
82
83    // Insert and return a new ID
84    unsigned generate_id(std::string const &name) {
85       unsigned sid = this->next++;
86       this->id2name[sid] = name;
87       // this->pending_surfaces.insert({sid});
88       this->name2id[name] = sid;
89       HMI_DEBUG("wm", "allocated new id %u with name %s", sid, name.c_str());
90       return sid;
91    }
92
93    // Insert a new ID which defined outside
94    void register_name_id(std::string const &name, unsigned sid) {
95       this->id2name[sid] = name;
96       this->name2id[name] = sid;
97       HMI_DEBUG("wm", "register id %u with name %s", sid, name.c_str());
98       return;
99    }
100
101    // Lookup by ID or by name
102    optional<unsigned> lookup(std::string const &name) const {
103       auto i = this->name2id.find(name);
104       return i == this->name2id.end() ? nullopt : optional<unsigned>(i->second);
105    }
106
107    optional<std::string> lookup(unsigned id) const {
108       auto i = this->id2name.find(id);
109       return i == this->id2name.end() ? nullopt
110                                        : optional<std::string>(i->second);
111    }
112
113    // Remove a surface id and name
114    void remove_id(std::string const &name) {
115       auto i = this->name2id.find(name);
116       if (i != this->name2id.end()) {
117          this->id2name.erase(i->second);
118          this->name2id.erase(i);
119       }
120    }
121
122    void remove_id(unsigned id) {
123       auto i = this->id2name.find(id);
124       if (i != this->id2name.end()) {
125          this->name2id.erase(i->second);
126          this->id2name.erase(i);
127       }
128    }
129 };
130
131 typedef struct CarInfo {
132     bool parking_brake_stt;
133     bool accel_pedal_stt;
134     double accel_pedal_pos;
135     const char *car_stt;
136     bool headlamp_stt;
137     bool lightstatus_brake_stt;
138 } CarInfo;
139
140 struct App {
141
142    typedef std::unordered_map<uint32_t, struct compositor::rect> rect_map;
143    typedef std::function<void(const char* err_msg)> reply_func;
144
145    enum EventType {
146       Event_Val_Min = 0,
147
148       Event_Active = Event_Val_Min,
149       Event_Inactive,
150
151       Event_Visible,
152       Event_Invisible,
153
154       Event_SyncDraw,
155       Event_FlushDraw,
156
157       Event_HeadlampOff,
158       Event_HeadlampOn,
159
160       Event_ParkingBrakeOff,
161       Event_ParkingBrakeOn,
162
163       Event_LightstatusBrakeOff,
164       Event_LightstatusBrakeOn,
165
166       Event_CarStop,
167       Event_CarRun,
168
169       Event_Val_Max = Event_CarRun,
170    };
171
172    const std::vector<const char *> kListEventName{
173      "active",
174      "inactive",
175      "visible",
176      "invisible",
177      "syncdraw",
178      "flushdraw",
179      "headlamp_off",
180      "headlamp_on",
181      "parking_brake_off",
182      "parking_brake_on",
183      "lightstatus_brake_off",
184      "lightstatus_brake_on",
185      "car_stop",
186      "car_run",
187    };
188
189    struct controller_hooks chooks;
190
191    // This is the one thing, we do not own.
192    struct wl::display *display;
193
194    std::unique_ptr<struct compositor::controller> controller;
195    std::vector<std::unique_ptr<struct wl::output>> outputs;
196
197    struct config config;
198
199    // track current layouts separately
200    layer_map layers;
201
202    // ID allocation and proxy methods for lookup
203    struct id_allocator id_alloc;
204
205    // Set by AFB API when wayland events need to be dispatched
206    std::atomic<bool> pending_events;
207
208    std::vector<int> pending_end_draw;
209
210    Policy policy;
211
212    std::map<const char *, struct afb_event> map_afb_event;
213
214    // Surface are info (x, y, w, h)
215    rect_map area_info;
216
217    // FOR CES DEMO
218    std::vector<int> surface_bg;
219
220    explicit App(wl::display *d);
221    ~App() = default;
222
223    App(App const &) = delete;
224    App &operator=(App const &) = delete;
225    App(App &&) = delete;
226    App &operator=(App &&) = delete;
227
228    int init();
229
230    int dispatch_pending_events();
231
232    void set_pending_events();
233
234    result<int> api_request_surface(char const *drawing_name);
235    char const *api_request_surface(char const *drawing_name, char const *ivi_id);
236    void allocateWindowResource(char const *event, char const *drawing_name,
237                                char const *drawing_area, const reply_func &reply);
238    void api_enddraw(char const *drawing_name);
239    result<json_object *> api_get_display_info();
240    result<json_object *> api_get_area_info(char const *drawing_name);
241    void api_ping();
242    void send_event(char const *evname);
243    void send_event(char const *evname, char const *label);
244    void send_event(char const *evname, char const *label, char const *area, int x, int y, int w, int h);
245
246    // Events from the compositor we are interested in
247    void surface_created(uint32_t surface_id);
248    void surface_removed(uint32_t surface_id);
249
250    void setAccelPedalPos(double val);
251    void updateWindowResource(json_object* json_out);
252
253 private:
254    typedef std::map<std::string, std::string> RoleState;
255
256    PolicyManager pm_;
257    LayoutManager lm_;
258    std::unordered_map<std::string, std::string> drawingname2role_;
259    std::unordered_map<std::string, std::string> role2drawingname_;
260    std::unordered_map<int, int> appid2role_;
261    CarInfo crr_car_info_;
262    std::map<std::string, RoleState> crr_layer_state;
263
264    void setSurfaceSize(const char* role, const char* area);
265    int loadAppDb();
266    const char* convertDrawingNameToRole(char const *drawing_name);
267
268 #if 0
269    struct id_allocator app_id_alloc_;
270    std::unordered_map<std::string, int> appname2appid_;
271 #endif
272
273    optional<int> lookup_id(char const *name);
274    optional<std::string> lookup_name(int id);
275
276    bool pop_pending_events();
277
278    void enqueue_flushdraw(int surface_id);
279    void check_flushdraw(int surface_id);
280
281    int init_layers();
282
283    void layout_commit();
284
285    // TMC WM Events to clients
286    void emit_activated(char const *label);
287    void emit_deactivated(char const *label);
288    void emit_syncdraw(char const *label, char const *area, int x, int y, int w, int h);
289    void emit_flushdraw(char const *label);
290    void emit_visible(char const *label, bool is_visible);
291    void emit_invisible(char const *label);
292    void emit_visible(char const *label);
293    void emitHeadlampOff();
294    void emitHeadlampOn();
295    void emitParkingBrakeOff();
296    void emitParkingBrakeOn();
297    void emitLightstatusBrakeOff();
298    void emitLightstatusBrakeOn();
299    void emitCarStop();
300    void emitCarRun();
301
302    void activate(int id);
303    void deactivate(int id);
304    void deactivate(std::string role);
305    void deactivate_main_surface();
306
307 };
308
309 }  // namespace wm
310
311 #endif  // TMCAGLWM_APP_HPP