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"
34 #include "wayland.hpp"
46 using std::experimental::optional;
49 constexpr static const unsigned id_shift = 22;
50 constexpr static const unsigned id_mask = (1 << id_shift) - 1;
54 // Surfaces that where requested but not yet created
55 std::unordered_map<unsigned, std::string> id2name;
56 // std::unordered_set<unsigned> pending_surfaces;
57 std::unordered_map<std::string, unsigned> name2id;
59 id_allocator(id_allocator const &) = delete;
60 id_allocator(id_allocator &&) = delete;
61 id_allocator &operator=(id_allocator const &);
62 id_allocator &operator=(id_allocator &&) = delete;
64 // Insert and return a new ID
65 unsigned generate_id(std::string const &name) {
66 unsigned sid = this->next++;
67 this->id2name[sid] = name;
68 // this->pending_surfaces.insert({sid});
69 this->name2id[name] = sid;
70 logdebug("allocated new id %u with name %s", sid, name.c_str());
74 // Lookup by ID or by name
75 optional<unsigned> lookup(std::string const &name) const {
76 auto i = this->name2id.find(name);
77 return i == this->name2id.end() ? nullopt : optional<unsigned>(i->second);
80 optional<std::string> lookup(unsigned id) const {
81 auto i = this->id2name.find(id);
82 return i == this->id2name.end() ? nullopt
83 : optional<std::string>(i->second);
86 // Remove a surface id and name
87 // I don't think I will need this, do I?
88 void remove_id(std::string const &name) {
89 auto i = this->name2id.find(name);
90 if (i != this->name2id.end()) {
91 this->id2name.erase(i->second);
92 this->name2id.erase(i);
96 void remove_id(unsigned id) {
97 auto i = this->id2name.find(id);
98 if (i != this->id2name.end()) {
99 this->name2id.erase(i->second);
100 this->id2name.erase(i);
106 struct binding_api api;
107 struct controller_hooks chooks;
109 // This is the one thing, we do not own.
110 struct wl::display *display;
112 std::unique_ptr<struct genivi::controller> controller;
113 std::vector<std::unique_ptr<struct wl::output>> outputs;
115 struct config config;
117 // track current layouts separately
118 std::map<int, struct LayoutState> layouts;
121 // ID allocation and proxy methods for lookup
122 struct id_allocator id_alloc;
123 optional<int> lookup_id(char const *name) {
124 return this->id_alloc.lookup(std::string(name));
126 optional<std::string> lookup_name(int id) {
127 return this->id_alloc.lookup(id);
130 // Set by AFB API when wayland events need to be dispatched
131 std::atomic<bool> pending_events;
132 void set_pending_events() {
133 this->pending_events.store(true, std::memory_order_release);
135 bool pop_pending_events() {
137 return this->pending_events.compare_exchange_strong(
138 x, false, std::memory_order_consume);
141 explicit App(wl::display *d);
144 App(App const &) = delete;
145 App &operator=(App const &) = delete;
146 App(App &&) = delete;
147 App &operator=(App &&) = delete;
152 int dispatch_events();
153 int dispatch_pending_events();
155 void surface_set_layout_full(uint32_t surface_id);
156 void surface_set_layout_split(uint32_t surface_id, uint32_t sub_surface_id);
158 // Allocate a surface ID for this role
159 result<int> request_surface(char const *drawing_name);
161 // Activate (i.e. make visible, if allowed!) a surface
162 char const *activate_surface(char const *drawing_name);
163 char const *deactivate_surface(char const *drawing_name);
165 // Events from the compositor we are interested in
166 void surface_created(uint32_t surface_id);
167 void surface_removed(uint32_t surface_id);
169 // TMC WM Events to clients
170 void emit_activated(char const *label);
171 void emit_deactivated(char const *label);
172 void emit_syncdraw(char const *label);
173 void emit_flushdraw(char const *label);
174 void emit_visible(char const *label, bool is_visible);
175 void emit_invisible(char const *label);
176 void emit_visible(char const *label);
178 void activate(int id);
179 void deactivate(int id);
181 bool can_split(struct LayoutState const &state, int new_id);
186 #endif // TMCAGLWM_APP_HPP