app: do not apply left id_shift to generated ID!
[staging/windowmanager.git] / src / app.hpp
1 /*
2  * Copyright (C) 2017 Mentor Graphics Development (Deutschland) GmbH
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #ifndef TMCAGLWM_APP_HPP
18 #define TMCAGLWM_APP_HPP
19
20 #include <json-c/json.h>
21 #include <memory>
22 #include <unordered_map>
23 #include <unordered_set>
24 #include <deque>
25
26 #include "afb_binding_api.hpp"
27 #include "config.hpp"
28 #include "controller_hooks.hpp"
29 #include "layers.hpp"
30 #include "layout.hpp"
31 #include "result.hpp"
32 #include "wayland.hpp"
33
34 namespace wl {
35 struct display;
36 }
37
38 namespace genivi {
39 struct controller;
40 }
41
42 namespace wm {
43
44 struct id_allocator {
45    constexpr static const unsigned id_shift = 22;
46    constexpr static const unsigned id_mask = (1 << id_shift) - 1;
47
48    unsigned next = 1;
49
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;
54
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;
59
60    // Insert and return a new ID
61    unsigned generate_id(std::string const &name) {
62       unsigned sid = this->next++;
63       this->surfaces[sid] = name;
64       // this->pending_surfaces.insert({sid});
65       this->names[name] = sid;
66       logdebug("allocated new id %u with name %s", sid, name.c_str());
67       return sid;
68    }
69
70    // Lookup by ID or by name
71    optional<unsigned> lookup(std::string const &name) const {
72       auto i = this->names.find(name);
73       return i == this->names.end() ? nullopt : optional<unsigned>(i->second);
74    }
75
76    optional<std::string> lookup(unsigned id) const {
77       auto i = this->surfaces.find(id);
78       return i == this->surfaces.end() ? nullopt
79                                        : optional<std::string>(i->second);
80    }
81
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->names.find(name);
86       if (i != this->names.end()) {
87          this->surfaces.erase(i->second);
88          this->names.erase(i);
89       }
90    }
91
92    void remove_id(unsigned id) {
93       auto i = this->surfaces.find(id);
94       if (i != this->surfaces.end()) {
95          this->names.erase(i->second);
96          this->surfaces.erase(i);
97       }
98    }
99 };
100
101 struct App {
102    struct binding_api api;
103    struct controller_hooks chooks;
104
105    // This is the one thing, we do not own.
106    struct wl::display *display;
107
108    std::unique_ptr<struct genivi::controller> controller;
109    std::vector<std::unique_ptr<struct wl::output>> outputs;
110
111    struct config config;
112
113    layouts_type layouts;
114    layer_map layers;
115
116    typedef std::pair<char const *, std::function<void()>> name_task_pair;
117    std::vector<name_task_pair> pending;
118
119    typedef std::map<std::string, int> drawing_name_map;
120    drawing_name_map name_mapping;
121
122    struct id_allocator id_alloc;
123
124    std::deque<unsigned> last_active;
125
126    explicit App(wl::display *d);
127    ~App();
128
129    App(App const &) = delete;
130    App &operator=(App const &) = delete;
131    App(App &&) = delete;
132    App &operator=(App &&) = delete;
133
134    int init();
135    int init_layout();
136
137    int dispatch_events();
138
139    void surface_set_layout(uint32_t surface_id);
140    char const *activate_surface(uint32_t surface_id);
141    char const *deactivate_surface(uint32_t surface_id);
142
143    // Allocate a surface ID for this role
144    result<int> request_surface(char const *drawing_name);
145
146    // Activate (i.e. make visible, if allowed!) a surface
147    char const *activate_surface(char const *drawing_name);
148    char const *deactivate_surface(char const *drawing_name);
149
150    // add tasks, executed after dispatch_events()
151    void add_task(char const *name, std::function<void()> &&f);
152    void execute_pending();
153
154    // Events from the compositor we are interested in
155    void surface_created(uint32_t surface_id);
156    void surface_removed(uint32_t surface_id);
157
158    // TMC WM Events to clients
159    void emit_activated(char const *label);
160    void emit_deactivated(char const *label);
161    void emit_syncdraw(char const *label);
162    void emit_visible(char const *label, bool is_visible);
163 };
164
165 }  // namespace wm
166
167 #endif  // TMCAGLWM_APP_HPP