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"
40 namespace compositor {
46 using std::experimental::optional;
48 /* DrawingArea name used by "{layout}.{area}" */
49 extern const char kNameLayoutNormal[];
50 extern const char kNameLayoutSplit[];
51 extern const char kNameAreaFull[];
52 extern const char kNameAreaMain[];
53 extern const char kNameAreaSub[];
55 /* Key for json obejct */
56 extern const char kKeyDrawingName[];
57 extern const char kKeyDrawingArea[];
58 extern const char kKeyDrawingRect[];
59 extern const char kKeyX[];
60 extern const char kKeyY[];
61 extern const char kKeyWidth[];
62 extern const char kKeyHeigh[];
63 extern const char kKeyWidthPixel[];
64 extern const char kKeyHeightPixel[];
65 extern const char kKeyWidthMm[];
66 extern const char kKeyHeightMm[];
72 // Surfaces that where requested but not yet created
73 std::unordered_map<unsigned, std::string> id2name;
74 // std::unordered_set<unsigned> pending_surfaces;
75 std::unordered_map<std::string, unsigned> name2id;
77 id_allocator(id_allocator const &) = delete;
78 id_allocator(id_allocator &&) = delete;
79 id_allocator &operator=(id_allocator const &);
80 id_allocator &operator=(id_allocator &&) = delete;
82 // Insert and return a new ID
83 unsigned generate_id(std::string const &name) {
84 unsigned sid = this->next++;
85 this->id2name[sid] = name;
86 // this->pending_surfaces.insert({sid});
87 this->name2id[name] = sid;
88 HMI_DEBUG("wm", "allocated new id %u with name %s", sid, name.c_str());
92 // Insert a new ID which defined outside
93 void register_name_id(std::string const &name, unsigned sid) {
94 this->id2name[sid] = name;
95 this->name2id[name] = sid;
96 HMI_DEBUG("wm", "register id %u with name %s", sid, name.c_str());
100 // Lookup by ID or by name
101 optional<unsigned> lookup(std::string const &name) const {
102 auto i = this->name2id.find(name);
103 return i == this->name2id.end() ? nullopt : optional<unsigned>(i->second);
106 optional<std::string> lookup(unsigned id) const {
107 auto i = this->id2name.find(id);
108 return i == this->id2name.end() ? nullopt
109 : optional<std::string>(i->second);
112 // Remove a surface id and name
113 void remove_id(std::string const &name) {
114 auto i = this->name2id.find(name);
115 if (i != this->name2id.end()) {
116 this->id2name.erase(i->second);
117 this->name2id.erase(i);
121 void remove_id(unsigned id) {
122 auto i = this->id2name.find(id);
123 if (i != this->id2name.end()) {
124 this->name2id.erase(i->second);
125 this->id2name.erase(i);
132 typedef std::unordered_map<uint32_t, struct compositor::rect> rect_map;
133 typedef std::function<void(const char* err_msg)> reply_func;
138 Event_Active = Event_Val_Min,
147 Event_Val_Max = Event_FlushDraw,
150 const std::vector<const char *> kListEventName{
159 struct controller_hooks chooks;
161 // This is the one thing, we do not own.
162 struct wl::display *display;
164 std::unique_ptr<struct compositor::controller> controller;
165 std::vector<std::unique_ptr<struct wl::output>> outputs;
167 struct config config;
169 // track current layouts separately
172 // ID allocation and proxy methods for lookup
173 struct id_allocator id_alloc;
175 // Set by AFB API when wayland events need to be dispatched
176 std::atomic<bool> pending_events;
178 std::vector<int> pending_end_draw;
182 std::map<const char *, struct afb_event> map_afb_event;
184 // Surface are info (x, y, w, h)
188 std::vector<int> surface_bg;
190 explicit App(wl::display *d);
193 App(App const &) = delete;
194 App &operator=(App const &) = delete;
195 App(App &&) = delete;
196 App &operator=(App &&) = delete;
200 int dispatch_pending_events();
202 void set_pending_events();
204 result<int> api_request_surface(char const *appid, char const *drawing_name);
205 char const *api_request_surface(char const *appid, char const *drawing_name, char const *ivi_id);
206 void api_activate_surface(char const *appid, char const *drawing_name, char const *drawing_area, const reply_func &reply);
207 void api_deactivate_surface(char const *appid, char const *drawing_name, const reply_func &reply);
208 void api_enddraw(char const *appid, char const *drawing_name);
209 result<json_object *> api_get_display_info();
210 result<json_object *> api_get_area_info(char const *drawing_name);
212 void send_event(char const *evname, char const *label);
213 void send_event(char const *evname, char const *label, char const *area, int x, int y, int w, int h);
215 // Events from the compositor we are interested in
216 void surface_created(uint32_t surface_id);
217 void surface_removed(uint32_t surface_id);
219 // Do not use this function
220 //static int processTimerHandler(sd_event_source *s, uint64_t usec, void *userdata);
224 optional<int> lookup_id(char const *name);
225 optional<std::string> lookup_name(int id);
227 bool pop_pending_events();
229 void enqueue_flushdraw(int surface_id);
230 void check_flushdraw(int surface_id);
234 void surface_set_layout(int surface_id, optional<int> sub_surface_id = nullopt);
235 void layout_commit();
237 // TMC WM Events to clients
238 void emit_activated(char const *label);
239 void emit_deactivated(char const *label);
240 void emit_syncdraw(char const *label, char const *area, int x, int y, int w, int h);
241 void emit_flushdraw(char const *label);
242 void emit_visible(char const *label, bool is_visible);
243 void emit_invisible(char const *label);
244 void emit_visible(char const *label);
246 void do_allocate_window_resource(unsigned sequence_number);
247 void do_enddraw(unsigned sequence_number);
248 void process_request();
250 void activate(int id);
251 void deactivate(int id);
252 void deactivate_main_surface();
254 bool can_split(struct LayoutState const &state, int new_id);
255 void try_layout(struct LayoutState &state,
256 struct LayoutState const &new_layout,
257 std::function<void(LayoutState const &nl)> apply);
262 #endif // TMCAGLWM_APP_HPP