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 unsigned id_shift = 20;
46 constexpr static unsigned id_mask = (1 << id_shift) - 1;
50 // Surfaces that where requested but not yet created
51 std::unordered_map<unsigned, std::string> surfaces;
52 // std::unordered_set<unsigned> pending_surfaces;
53 std::unordered_map<std::string, unsigned> names;
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++;
64 this->surfaces[sid] = name;
65 // this->pending_surfaces.insert({sid});
66 this->names[name] = sid;
67 logdebug("allocated new id %u with name %s", sid, name.c_str());
71 // Lookup by ID or by name
72 optional<unsigned> lookup(std::string const &name) const {
73 auto i = this->names.find(name);
74 return i == this->names.end() ? nullopt : optional<unsigned>(i->second);
77 optional<std::string> lookup(unsigned id) const {
78 auto i = this->surfaces.find(id);
79 return i == this->surfaces.end() ? nullopt
80 : optional<std::string>(i->second);
83 // Remove a surface id and name
84 // I don't think I will need this, do I?
85 void remove_id(std::string const &name) {
86 auto i = this->names.find(name);
87 if (i != this->names.end()) {
88 this->surfaces.erase(i->second);
93 void remove_id(unsigned id) {
94 auto i = this->surfaces.find(id);
95 if (i != this->surfaces.end()) {
96 this->names.erase(i->second);
97 this->surfaces.erase(i);
103 struct binding_api api;
104 struct controller_hooks chooks;
106 // This is the one thing, we do not own.
107 struct wl::display *display;
109 std::unique_ptr<struct genivi::controller> controller;
110 std::vector<std::unique_ptr<struct wl::output>> outputs;
112 struct config config;
114 layouts_type layouts;
117 typedef std::pair<char const *, std::function<void()>> name_task_pair;
118 std::vector<name_task_pair> pending;
120 typedef std::map<std::string, int> drawing_name_map;
121 drawing_name_map name_mapping;
123 struct id_allocator id_alloc;
125 std::deque<unsigned> last_active;
127 explicit App(wl::display *d);
130 App(App const &) = delete;
131 App &operator=(App const &) = delete;
132 App(App &&) = delete;
133 App &operator=(App &&) = delete;
138 int dispatch_events();
140 void surface_set_layout(uint32_t surface_id);
141 char const *activate_surface(uint32_t surface_id);
142 char const *deactivate_surface(uint32_t surface_id);
144 // Allocate a surface ID for this role
145 result<int> request_surface(char const *drawing_name);
147 // Activate (i.e. make visible, if allowed!) a surface
148 char const *activate_surface(char const *drawing_name);
149 char const *deactivate_surface(char const *drawing_name);
151 // add tasks, executed after dispatch_events()
152 void add_task(char const *name, std::function<void()> &&f);
153 void execute_pending();
155 // Events from the compositor we are interested in
156 void surface_created(uint32_t surface_id);
157 void surface_removed(uint32_t surface_id);
162 #endif // TMCAGLWM_APP_HPP