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 "hmi-debug.h"
49 using std::experimental::optional;
51 /* DrawingArea name used by "{layout}.{area}" */
52 extern const char kNameLayoutNormal[];
53 extern const char kNameLayoutSplit[];
54 extern const char kNameAreaFull[];
55 extern const char kNameAreaMain[];
56 extern const char kNameAreaSub[];
58 /* Key for json obejct */
59 extern const char kKeyDrawingName[];
60 extern const char kKeyDrawingArea[];
61 extern const char kKeyDrawingRect[];
62 extern const char kKeyX[];
63 extern const char kKeyY[];
64 extern const char kKeyWidth[];
65 extern const char kKeyHeigh[];
66 extern const char kKeyWidthPixel[];
67 extern const char kKeyHeightPixel[];
68 extern const char kKeyWidthMm[];
69 extern const char kKeyHeightMm[];
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;
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;
85 // Insert and return a new ID
86 unsigned generate_id(std::string const &name)
88 unsigned sid = this->next++;
89 this->id2name[sid] = name;
90 // this->pending_surfaces.insert({sid});
91 this->name2id[name] = sid;
92 HMI_DEBUG("wm", "allocated new id %u with name %s", sid, name.c_str());
96 // Insert a new ID which defined outside
97 void register_name_id(std::string const &name, unsigned sid)
99 this->id2name[sid] = name;
100 this->name2id[name] = sid;
101 HMI_DEBUG("wm", "register id %u with name %s", sid, name.c_str());
105 // Lookup by ID or by name
106 optional<unsigned> lookup(std::string const &name) const
108 auto i = this->name2id.find(name);
109 return i == this->name2id.end() ? nullopt : optional<unsigned>(i->second);
112 optional<std::string> lookup(unsigned id) const
114 auto i = this->id2name.find(id);
115 return i == this->id2name.end() ? nullopt
116 : optional<std::string>(i->second);
119 // Remove a surface id and name
120 void remove_id(std::string const &name)
122 auto i = this->name2id.find(name);
123 if (i != this->name2id.end())
125 this->id2name.erase(i->second);
126 this->name2id.erase(i);
130 void remove_id(unsigned id)
132 auto i = this->id2name.find(id);
133 if (i != this->id2name.end())
135 this->name2id.erase(i->second);
136 this->id2name.erase(i);
144 typedef std::unordered_map<uint32_t, struct compositor::rect> rect_map;
145 typedef std::function<void(const char *err_msg)> reply_func;
151 Event_Active = Event_Val_Min,
160 Event_Val_Max = Event_FlushDraw,
163 const std::vector<const char *> kListEventName{
171 struct controller_hooks chooks;
173 // This is the one thing, we do not own.
174 struct wl::display *display;
176 std::unique_ptr<struct compositor::controller> controller;
177 std::vector<std::unique_ptr<struct wl::output>> outputs;
179 struct config config;
181 // track current layouts separately
184 // ID allocation and proxy methods for lookup
185 struct id_allocator id_alloc;
187 // Set by AFB API when wayland events need to be dispatched
188 std::atomic<bool> pending_events;
190 std::vector<int> pending_end_draw;
194 std::map<const char *, struct afb_event> map_afb_event;
196 // Surface are info (x, y, w, h)
200 std::vector<int> surface_bg;
202 explicit App(wl::display *d);
205 App(App const &) = delete;
206 App &operator=(App const &) = delete;
207 App(App &&) = delete;
208 App &operator=(App &&) = delete;
212 int dispatch_pending_events();
214 void set_pending_events();
216 result<int> api_request_surface(char const *drawing_name);
217 char const *api_request_surface(char const *drawing_name, char const *ivi_id);
218 void api_activate_surface(char const *drawing_name, char const *drawing_area, const reply_func &reply);
219 void api_deactivate_surface(char const *drawing_name, const reply_func &reply);
220 void api_enddraw(char const *drawing_name);
221 result<json_object *> api_get_display_info();
222 result<json_object *> api_get_area_info(char const *drawing_name);
224 void send_event(char const *evname, char const *label);
225 void send_event(char const *evname, char const *label, char const *area, int x, int y, int w, int h);
227 // Events from the compositor we are interested in
228 void surface_created(uint32_t surface_id);
229 void surface_removed(uint32_t surface_id);
232 optional<int> lookup_id(char const *name);
233 optional<std::string> lookup_name(int id);
235 bool pop_pending_events();
237 void enqueue_flushdraw(int surface_id);
238 void check_flushdraw(int surface_id);
242 void surface_set_layout(int surface_id, optional<int> sub_surface_id = nullopt);
243 void layout_commit();
245 // TMC WM Events to clients
246 void emit_activated(char const *label);
247 void emit_deactivated(char const *label);
248 void emit_syncdraw(char const *label, char const *area, int x, int y, int w, int h);
249 void emit_flushdraw(char const *label);
250 void emit_visible(char const *label, bool is_visible);
251 void emit_invisible(char const *label);
252 void emit_visible(char const *label);
254 void activate(int id);
255 void deactivate(int id);
256 void deactivate_main_surface();
258 bool can_split(struct LayoutState const &state, int new_id);
259 void try_layout(struct LayoutState &state,
260 struct LayoutState const &new_layout,
261 std::function<void(LayoutState const &nl)> apply);
266 #endif // TMCAGLWM_APP_HPP