2 * Copyright (c) 2017 TOYOTA MOTOR CORPORATION
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #ifndef TMCAGLWM_APP_HPP
18 #define TMCAGLWM_APP_HPP
20 #include <json-c/json.h>
24 #include <unordered_map>
25 #include <unordered_set>
26 #include <experimental/optional>
28 #include "controller_hooks.hpp"
33 #include "wayland_ivi_wm.hpp"
34 #include "policy_manager.hpp"
35 #include "hmi-debug.h"
41 namespace compositor {
47 using std::experimental::optional;
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[];
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 extern const char kKeyAppID[];
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;
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;
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());
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());
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);
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);
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);
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);
131 typedef struct CarInfo {
132 bool parking_brake_stt;
133 bool accel_pedal_stt;
134 double accel_pedal_pos;
137 bool lightstatus_brake_stt;
142 typedef std::unordered_map<uint32_t, struct compositor::rect> rect_map;
143 typedef std::function<void(const char* err_msg)> reply_func;
148 Event_Active = Event_Val_Min,
160 Event_ParkingBrakeOff,
161 Event_ParkingBrakeOn,
163 Event_LightstatusBrakeOff,
164 Event_LightstatusBrakeOn,
171 Event_Val_Max = Event_ScreenUpdated,
174 const std::vector<const char *> kListEventName{
185 "lightstatus_brake_off",
186 "lightstatus_brake_on",
192 struct controller_hooks chooks;
194 // This is the one thing, we do not own.
195 struct wl::display *display;
197 std::unique_ptr<struct compositor::controller> controller;
198 std::vector<std::unique_ptr<struct wl::output>> outputs;
200 struct config config;
202 // track current layouts separately
205 // ID allocation and proxy methods for lookup
206 struct id_allocator id_alloc;
208 // Set by AFB API when wayland events need to be dispatched
209 std::atomic<bool> pending_events;
211 std::vector<int> pending_end_draw;
215 std::map<const char *, struct afb_event> map_afb_event;
217 // Surface are info (x, y, w, h)
221 std::vector<int> surface_bg;
223 explicit App(wl::display *d);
226 App(App const &) = delete;
227 App &operator=(App const &) = delete;
228 App(App &&) = delete;
229 App &operator=(App &&) = delete;
233 int dispatch_pending_events();
235 void set_pending_events();
237 result<int> api_request_surface(char const *drawing_name);
238 char const *api_request_surface(char const *drawing_name, char const *ivi_id);
239 void allocateWindowResource(char const *event, char const *drawing_name,
240 char const *drawing_area, const reply_func &reply);
241 void api_enddraw(char const* appid, char const *drawing_name);
242 result<json_object *> api_get_display_info();
243 result<json_object *> api_get_area_info(char const *drawing_name);
244 result<json_object *> api_get_car_info(char const *label);
246 void send_event(char const *evname);
247 void send_event(char const *evname, char const *label);
248 void send_event(char const *evname, char const *label, char const *area, int x, int y, int w, int h);
249 void __send_event_temporary_extend(char const *evname, char const *appid);
251 // Events from the compositor we are interested in
252 void surface_created(uint32_t surface_id);
253 void surface_removed(uint32_t surface_id);
255 void setAccelPedalPos(double val);
260 std::unordered_map<std::string, int> role2surfaceid_;
261 std::unordered_map<std::string, std::string> drawingname2role_;
262 std::unordered_map<std::string, std::string> role2drawingname_;
263 std::unordered_map<int, int> appid2role_;
264 CarInfo crr_car_info_;
266 int allocateSurface();
267 void setSurfaceSize(const char* role, const char* area);
269 const char* convertDrawingNameToRole(char const *drawing_name);
272 struct id_allocator app_id_alloc_;
273 std::unordered_map<std::string, int> appname2appid_;
276 optional<int> lookup_id(char const *name);
277 optional<std::string> lookup_name(int id);
279 bool pop_pending_events();
281 void enqueue_flushdraw(int surface_id);
282 void check_flushdraw(int surface_id);
286 void layout_commit();
288 // TMC WM Events to clients
289 void emit_activated(char const *label);
290 void emit_deactivated(char const *label);
291 void emit_syncdraw(char const *label, char const *area, int x, int y, int w, int h);
292 void emit_flushdraw(char const *label);
293 void emit_visible(char const *label, bool is_visible);
294 void emit_invisible(char const *label);
295 void emit_visible(char const *label);
296 void emitHeadlampOff();
297 void emitHeadlampOn();
298 void emitParkingBrakeOff();
299 void emitParkingBrakeOn();
300 void emitLightstatusBrakeOff();
301 void emitLightstatusBrakeOn();
304 void emitScreenUpdated(char const *appid);
306 void activate(int id);
307 void deactivate(int id);
308 void deactivate(std::string role);
309 void deactivate_main_surface();
315 #endif // TMCAGLWM_APP_HPP