2 * Copyright (C) 2017 Mentor Graphics Development (Deutschland) GmbH
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 "afb_binding_api.hpp"
30 #include "controller_hooks.hpp"
35 #include "wayland.hpp"
47 using std::experimental::optional;
49 /* DrawingArea name used by "{layout}.{area}" */
50 static const char *kNameLayoutNormal = "normal";
51 static const char *kNameLayoutSplit = "split";
52 static const char *kNameAreaFull = "full";
53 static const char *kNameAreaMain = "main";
54 static const char *kNameAreaSub = "sub";
56 /* Key for json obejct */
57 static const char *kKeyDrawingName = "drawing_name";
58 static const char *kKeyDrawingArea = "drawing_area";
63 // Surfaces that where requested but not yet created
64 std::unordered_map<unsigned, std::string> id2name;
65 // std::unordered_set<unsigned> pending_surfaces;
66 std::unordered_map<std::string, unsigned> name2id;
68 id_allocator(id_allocator const &) = delete;
69 id_allocator(id_allocator &&) = delete;
70 id_allocator &operator=(id_allocator const &);
71 id_allocator &operator=(id_allocator &&) = delete;
73 // Insert and return a new ID
74 unsigned generate_id(std::string const &name) {
75 unsigned sid = this->next++;
76 this->id2name[sid] = name;
77 // this->pending_surfaces.insert({sid});
78 this->name2id[name] = sid;
79 logdebug("allocated new id %u with name %s", sid, name.c_str());
83 // Lookup by ID or by name
84 optional<unsigned> lookup(std::string const &name) const {
85 auto i = this->name2id.find(name);
86 return i == this->name2id.end() ? nullopt : optional<unsigned>(i->second);
89 optional<std::string> lookup(unsigned id) const {
90 auto i = this->id2name.find(id);
91 return i == this->id2name.end() ? nullopt
92 : optional<std::string>(i->second);
95 // Remove a surface id and name
96 void remove_id(std::string const &name) {
97 auto i = this->name2id.find(name);
98 if (i != this->name2id.end()) {
99 this->id2name.erase(i->second);
100 this->name2id.erase(i);
104 void remove_id(unsigned id) {
105 auto i = this->id2name.find(id);
106 if (i != this->id2name.end()) {
107 this->name2id.erase(i->second);
108 this->id2name.erase(i);
117 Event_Active = Event_Val_Min,
126 Event_Val_Max = Event_FlushDraw,
129 const std::vector<const char *> kListEventName{
138 struct binding_api api;
139 struct controller_hooks chooks;
141 // This is the one thing, we do not own.
142 struct wl::display *display;
144 std::unique_ptr<struct genivi::controller> controller;
145 std::vector<std::unique_ptr<struct wl::output>> outputs;
147 struct config config;
149 // track current layouts separately
152 // ID allocation and proxy methods for lookup
153 struct id_allocator id_alloc;
155 // Set by AFB API when wayland events need to be dispatched
156 std::atomic<bool> pending_events;
158 std::vector<int> pending_end_draw;
162 std::map<const char *, struct afb_event> map_afb_event;
164 explicit App(wl::display *d);
167 App(App const &) = delete;
168 App &operator=(App const &) = delete;
169 App(App &&) = delete;
170 App &operator=(App &&) = delete;
174 int dispatch_events();
175 int dispatch_pending_events();
177 void set_pending_events();
179 result<int> api_request_surface(char const *drawing_name);
180 char const *api_activate_surface(char const *drawing_name, char const *drawing_area);
181 char const *api_deactivate_surface(char const *drawing_name);
182 char const *api_enddraw(char const *drawing_name);
183 char const *api_subscribe(afb_req *req, char const *event_name);
186 // Events from the compositor we are interested in
187 void surface_created(uint32_t surface_id);
188 void surface_removed(uint32_t surface_id);
191 optional<int> lookup_id(char const *name);
192 optional<std::string> lookup_name(int id);
194 bool pop_pending_events();
196 void enqueue_flushdraw(int surface_id);
197 void check_flushdraw(int surface_id);
201 void surface_set_layout(int surface_id, optional<int> sub_surface_id = nullopt);
202 void layout_commit();
204 // TMC WM Events to clients
205 void emit_activated(char const *label);
206 void emit_deactivated(char const *label);
207 void emit_syncdraw(char const *label, char const *area);
208 void emit_flushdraw(char const *label);
209 void emit_visible(char const *label, bool is_visible);
210 void emit_invisible(char const *label);
211 void emit_visible(char const *label);
213 void activate(int id);
214 void deactivate(int id);
215 void deactivate_main_surface();
217 bool can_split(struct LayoutState const &state, int new_id);
218 void try_layout(struct LayoutState &state,
219 struct LayoutState const &new_layout,
220 std::function<void(LayoutState const &nl)> apply);
225 #endif // TMCAGLWM_APP_HPP