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
22 #include <unordered_map>
23 #include <experimental/optional>
24 #include "controller_hooks.hpp"
27 #include "wayland_ivi_wm.hpp"
28 #include "hmi-debug.h"
29 #include "request.hpp"
30 #include "wm_error.hpp"
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 kKeyScale[];
69 extern const char kKeyIds[];
75 // Surfaces that where requested but not yet created
76 std::unordered_map<unsigned, std::string> id2name;
77 std::unordered_map<std::string, unsigned> name2id;
79 id_allocator(id_allocator const &) = delete;
80 id_allocator(id_allocator &&) = delete;
81 id_allocator &operator=(id_allocator const &);
82 id_allocator &operator=(id_allocator &&) = delete;
84 // Insert and return a new ID
85 unsigned generate_id(std::string const &name)
87 unsigned sid = this->next++;
88 this->id2name[sid] = name;
89 this->name2id[name] = sid;
90 HMI_DEBUG("wm", "allocated new id %u with name %s", sid, name.c_str());
94 // Insert a new ID which defined outside
95 void register_name_id(std::string const &name, unsigned sid)
97 this->id2name[sid] = name;
98 this->name2id[name] = sid;
99 HMI_DEBUG("wm", "register id %u with name %s", sid, name.c_str());
103 // Lookup by ID or by name
104 optional<unsigned> lookup(std::string const &name) const
106 auto i = this->name2id.find(name);
107 return i == this->name2id.end() ? nullopt : optional<unsigned>(i->second);
110 optional<std::string> lookup(unsigned id) const
112 auto i = this->id2name.find(id);
113 return i == this->id2name.end() ? nullopt
114 : optional<std::string>(i->second);
117 // Remove a surface id and name
118 void remove_id(std::string const &name)
120 auto i = this->name2id.find(name);
121 if (i != this->name2id.end())
123 this->id2name.erase(i->second);
124 this->name2id.erase(i);
128 void remove_id(unsigned id)
130 auto i = this->id2name.find(id);
131 if (i != this->id2name.end())
133 this->name2id.erase(i->second);
134 this->id2name.erase(i);
142 typedef std::unordered_map<uint32_t, struct compositor::rect> rect_map;
143 typedef std::function<void(const char *err_msg)> reply_func;
149 Event_Active = Event_Val_Min,
162 Event_Val_Max = Event_Error,
165 const std::vector<const char *> kListEventName{
175 struct controller_hooks chooks;
177 // This is the one thing, we do not own.
178 struct wl::display *display;
180 std::unique_ptr<struct compositor::controller> controller;
181 std::vector<std::unique_ptr<struct wl::output>> outputs;
183 // track current layouts separately
186 // ID allocation and proxy methods for lookup
187 struct id_allocator id_alloc;
189 // Set by AFB API when wayland events need to be dispatched
190 std::atomic<bool> pending_events;
192 std::map<const char *, struct afb_event> map_afb_event;
194 // Surface are info (x, y, w, h)
198 std::vector<int> surface_bg;
200 explicit WindowManager(wl::display *d);
201 ~WindowManager() = default;
203 WindowManager(WindowManager const &) = delete;
204 WindowManager &operator=(WindowManager const &) = delete;
205 WindowManager(WindowManager &&) = delete;
206 WindowManager &operator=(WindowManager &&) = delete;
209 int dispatch_pending_events();
210 void set_pending_events();
212 result<int> api_request_surface(char const *appid, char const *role);
213 char const *api_request_surface(char const *appid, char const *role, char const *ivi_id);
214 void api_activate_surface(char const *appid, char const *role, char const *drawing_area, const reply_func &reply);
215 void api_deactivate_surface(char const *appid, char const *role, const reply_func &reply);
216 void api_enddraw(char const *appid, char const *role);
217 result<json_object *> api_get_display_info();
218 result<json_object *> api_get_area_info(char const *role);
220 void send_event(char const *evname, char const *label);
221 void send_event(char const *evname, char const *label, char const *area, int x, int y, int w, int h);
223 // Events from the compositor we are interested in
224 void surface_created(uint32_t surface_id);
225 void surface_removed(uint32_t surface_id);
227 void removeClient(const std::string &appid);
228 void exceptionProcessForTransition();
229 const char* convertRoleOldToNew(char const *role);
230 // Do not use this function
234 bool pop_pending_events();
235 optional<int> lookup_id(char const *name);
236 optional<std::string> lookup_name(int id);
238 void surface_set_layout(int surface_id, const std::string& area = "");
239 void layout_commit();
241 // WM Events to clients
242 void emit_activated(char const *label);
243 void emit_deactivated(char const *label);
244 void emit_syncdraw(char const *label, char const *area, int x, int y, int w, int h);
245 void emit_syncdraw(const std::string &role, const std::string &area);
246 void emit_flushdraw(char const *label);
247 void emit_visible(char const *label, bool is_visible);
248 void emit_invisible(char const *label);
249 void emit_visible(char const *label);
251 void activate(int id);
252 void deactivate(int id);
253 WMError setRequest(const std::string &appid, const std::string &role, const std::string &area,
254 Task task, unsigned *req_num);
255 WMError doTransition(unsigned sequence_number);
256 WMError checkPolicy(unsigned req_num);
257 WMError startTransition(unsigned req_num);
258 WMError setInvisibleTask(const std::string &role, bool split);
260 WMError doEndDraw(unsigned req_num);
261 WMError layoutChange(const WMAction &action);
262 WMError visibilityChange(const WMAction &action);
263 WMError setSurfaceSize(unsigned surface, const std::string& area);
264 WMError changeCurrentState(unsigned req_num);
265 void emitScreenUpdated(unsigned req_num);
269 void processNextRequest();
273 const char *check_surface_exist(const char *role);
275 bool can_split(struct LayoutState const &state, int new_id);
278 std::unordered_map<std::string, struct compositor::rect> area2size;
279 std::unordered_map<std::string, std::string> roleold2new;
280 std::unordered_map<std::string, std::string> rolenew2old;
282 static const char* kDefaultOldRoleDb;
287 #endif // TMCAGLWM_APP_HPP