2 * Copyright (c) 2017 TOYOTA MOTOR CORPORATION
3 * Copyright (c) 2018 Konsulko Group
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
18 #ifndef TMCAGLWM_APP_HPP
19 #define TMCAGLWM_APP_HPP
23 #include <unordered_map>
24 #include <experimental/optional>
25 #include "controller_hooks.hpp"
28 #include "wayland_ivi_wm.hpp"
29 #include "pm_wrapper.hpp"
30 #include "hmi-debug.h"
31 #include "request.hpp"
32 #include "wm_error.hpp"
35 #include <afb/afb-binding.h>
53 using std::experimental::optional;
55 /* DrawingArea name used by "{layout}.{area}" */
56 extern const char kNameLayoutNormal[];
57 extern const char kNameLayoutSplit[];
58 extern const char kNameAreaFull[];
59 extern const char kNameAreaMain[];
60 extern const char kNameAreaSub[];
62 /* Key for json obejct */
63 extern const char kKeyDrawingName[];
64 extern const char kKeyDrawingArea[];
65 extern const char kKeyDrawingRect[];
66 extern const char kKeyX[];
67 extern const char kKeyY[];
68 extern const char kKeyWidth[];
69 extern const char kKeyHeigh[];
70 extern const char kKeyWidthPixel[];
71 extern const char kKeyHeightPixel[];
72 extern const char kKeyWidthMm[];
73 extern const char kKeyHeightMm[];
74 extern const char kKeyScale[];
75 extern const char kKeyIds[];
81 // Surfaces that where requested but not yet created
82 std::unordered_map<unsigned, std::string> id2name;
83 std::unordered_map<std::string, unsigned> name2id;
85 id_allocator(id_allocator const &) = delete;
86 id_allocator(id_allocator &&) = delete;
87 id_allocator &operator=(id_allocator const &);
88 id_allocator &operator=(id_allocator &&) = delete;
90 // Insert and return a new ID
91 unsigned generate_id(std::string const &name)
93 unsigned sid = this->next++;
94 this->id2name[sid] = name;
95 this->name2id[name] = sid;
96 HMI_DEBUG("wm", "allocated new id %u with name %s", sid, name.c_str());
100 // Insert a new ID which defined outside
101 void register_name_id(std::string const &name, unsigned sid)
103 this->id2name[sid] = name;
104 this->name2id[name] = sid;
105 HMI_DEBUG("wm", "register id %u with name %s", sid, name.c_str());
109 // Lookup by ID or by name
110 optional<unsigned> lookup(std::string const &name) const
112 auto i = this->name2id.find(name);
113 return i == this->name2id.end() ? nullopt : optional<unsigned>(i->second);
116 optional<std::string> lookup(unsigned id) const
118 auto i = this->id2name.find(id);
119 return i == this->id2name.end() ? nullopt
120 : optional<std::string>(i->second);
123 // Remove a surface id and name
124 void remove_id(std::string const &name)
126 auto i = this->name2id.find(name);
127 if (i != this->name2id.end())
129 this->id2name.erase(i->second);
130 this->name2id.erase(i);
134 void remove_id(unsigned id)
136 auto i = this->id2name.find(id);
137 if (i != this->id2name.end())
139 this->name2id.erase(i->second);
140 this->id2name.erase(i);
148 typedef std::unordered_map<uint32_t, struct compositor::rect> rect_map;
149 typedef std::function<void(const char *err_msg)> reply_func;
155 Event_Active = Event_Val_Min,
168 Event_Val_Max = Event_Error,
171 const std::vector<const char *> kListEventName{
181 struct controller_hooks chooks;
183 // This is the one thing, we do not own.
184 struct wl::display *display;
186 std::unique_ptr<struct compositor::controller> controller;
187 std::vector<std::unique_ptr<struct wl::output>> outputs;
189 // track current layouts separately
192 // ID allocation and proxy methods for lookup
193 struct id_allocator id_alloc;
195 // Set by AFB API when wayland events need to be dispatched
196 std::atomic<bool> pending_events;
198 std::map<const char *, struct afb_event> map_afb_event;
200 // Surface are info (x, y, w, h)
204 std::vector<int> surface_bg;
206 explicit WindowManager(wl::display *d);
207 ~WindowManager() = default;
209 WindowManager(WindowManager const &) = delete;
210 WindowManager &operator=(WindowManager const &) = delete;
211 WindowManager(WindowManager &&) = delete;
212 WindowManager &operator=(WindowManager &&) = delete;
215 int dispatch_pending_events();
216 void set_pending_events();
218 result<int> api_request_surface(char const *appid, char const *role);
219 char const *api_request_surface(char const *appid, char const *role, char const *ivi_id);
220 void api_activate_surface(char const *appid, char const *role, char const *drawing_area, const reply_func &reply);
221 void api_deactivate_surface(char const *appid, char const *role, const reply_func &reply);
222 void api_enddraw(char const *appid, char const *role);
223 result<json_object *> api_get_display_info();
224 result<json_object *> api_get_area_info(char const *role);
226 void send_event(char const *evname, char const *label);
227 void send_event(char const *evname, char const *label, char const *area, int x, int y, int w, int h);
229 // Events from the compositor we are interested in
230 void surface_created(uint32_t surface_id);
231 void surface_removed(uint32_t surface_id);
233 void removeClient(const std::string &appid);
234 void exceptionProcessForTransition();
235 const char* convertRoleOldToNew(char const *role);
237 // Do not use this function
239 void startTransitionWrapper(std::vector<WMAction> &actions);
240 void processError(WMError error);
243 bool pop_pending_events();
244 optional<int> lookup_id(char const *name);
245 optional<std::string> lookup_name(int id);
247 void surface_set_layout(int surface_id, const std::string& area = "");
248 void layout_commit();
250 // WM Events to clients
251 void emit_activated(char const *label);
252 void emit_deactivated(char const *label);
253 void emit_syncdraw(char const *label, char const *area, int x, int y, int w, int h);
254 void emit_syncdraw(const std::string &role, const std::string &area);
255 void emit_flushdraw(char const *label);
256 void emit_visible(char const *label, bool is_visible);
257 void emit_invisible(char const *label);
258 void emit_visible(char const *label);
260 void activate(int id);
261 void deactivate(int id);
262 WMError setRequest(const std::string &appid, const std::string &role, const std::string &area,
263 Task task, unsigned *req_num);
264 WMError doTransition(unsigned sequence_number);
265 WMError checkPolicy(unsigned req_num);
266 WMError startTransition(unsigned req_num);
268 WMError doEndDraw(unsigned req_num);
269 WMError layoutChange(const WMAction &action);
270 WMError visibilityChange(const WMAction &action);
271 WMError setSurfaceSize(unsigned surface, const std::string& area);
272 void emitScreenUpdated(unsigned req_num);
276 void processNextRequest();
280 const char *check_surface_exist(const char *role);
283 std::unordered_map<std::string, struct compositor::rect> area2size;
284 std::unordered_map<std::string, std::string> roleold2new;
285 std::unordered_map<std::string, std::string> rolenew2old;
289 static const char* kDefaultOldRoleDb;
294 #endif // TMCAGLWM_APP_HPP