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>
22 #include <unordered_map>
23 #include <unordered_set>
26 #include "afb_binding_api.hpp"
28 #include "controller_hooks.hpp"
32 #include "wayland.hpp"
45 constexpr static const unsigned id_shift = 22;
46 constexpr static const unsigned id_mask = (1 << id_shift) - 1;
50 // Surfaces that where requested but not yet created
51 std::unordered_map<unsigned, std::string> id2name;
52 // std::unordered_set<unsigned> pending_surfaces;
53 std::unordered_map<std::string, unsigned> name2id;
55 id_allocator(id_allocator const &) = delete;
56 id_allocator(id_allocator &&) = delete;
57 id_allocator &operator=(id_allocator const &);
58 id_allocator &operator=(id_allocator &&) = delete;
60 // Insert and return a new ID
61 unsigned generate_id(std::string const &name) {
62 unsigned sid = this->next++;
63 this->id2name[sid] = name;
64 // this->pending_surfaces.insert({sid});
65 this->name2id[name] = sid;
66 logdebug("allocated new id %u with name %s", sid, name.c_str());
70 // Lookup by ID or by name
71 optional<unsigned> lookup(std::string const &name) const {
72 auto i = this->name2id.find(name);
73 return i == this->name2id.end() ? nullopt : optional<unsigned>(i->second);
76 optional<std::string> lookup(unsigned id) const {
77 auto i = this->id2name.find(id);
78 return i == this->id2name.end() ? nullopt
79 : optional<std::string>(i->second);
82 // Remove a surface id and name
83 // I don't think I will need this, do I?
84 void remove_id(std::string const &name) {
85 auto i = this->name2id.find(name);
86 if (i != this->name2id.end()) {
87 this->id2name.erase(i->second);
88 this->name2id.erase(i);
92 void remove_id(unsigned id) {
93 auto i = this->id2name.find(id);
94 if (i != this->id2name.end()) {
95 this->name2id.erase(i->second);
96 this->id2name.erase(i);
113 struct binding_api api;
114 struct controller_hooks chooks;
116 // This is the one thing, we do not own.
117 struct wl::display *display;
119 std::unique_ptr<struct genivi::controller> controller;
120 std::vector<std::unique_ptr<struct wl::output>> outputs;
122 struct config config;
124 layouts_type layouts;
127 // ID allocation and proxy methods for lookup
128 struct id_allocator id_alloc;
129 optional<unsigned> lookup_id(char const *name) {
130 return this->id_alloc.lookup(std::string(name));
132 optional<std::string> lookup_name(unsigned id) {
133 return this->id_alloc.lookup(id);
136 struct LayoutState state;
138 explicit App(wl::display *d);
141 App(App const &) = delete;
142 App &operator=(App const &) = delete;
143 App(App &&) = delete;
144 App &operator=(App &&) = delete;
149 int dispatch_events();
151 void surface_init_layout(uint32_t surface_id);
153 // Allocate a surface ID for this role
154 result<int> request_surface(char const *drawing_name);
156 // Activate (i.e. make visible, if allowed!) a surface
157 char const *activate_surface(char const *drawing_name);
158 char const *deactivate_surface(char const *drawing_name);
160 // Events from the compositor we are interested in
161 void surface_created(uint32_t surface_id);
162 void surface_removed(uint32_t surface_id);
164 // TMC WM Events to clients
165 void emit_activated(char const *label);
166 void emit_deactivated(char const *label);
167 void emit_syncdraw(char const *label);
168 void emit_flushdraw(char const *label);
169 void emit_visible(char const *label, bool is_visible);
170 void emit_invisible(char const *label);
171 void emit_visible(char const *label);
173 void activate(unsigned id);
174 void deactivate(unsigned id);
176 bool can_split(unsigned new_id);
181 #endif // TMCAGLWM_APP_HPP