app/main: add send_event() to binding_api
[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 unsigned id_shift = 20;
46    constexpr static 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       sid <<= id_shift;
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());
68       return sid;
69    }
70
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);
75    }
76
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);
81    }
82
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);
89          this->names.erase(i);
90       }
91    }
92
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);
98       }
99    }
100 };
101
102 struct App {
103    struct binding_api api;
104    struct controller_hooks chooks;
105
106    // This is the one thing, we do not own.
107    struct wl::display *display;
108
109    std::unique_ptr<struct genivi::controller> controller;
110    std::vector<std::unique_ptr<struct wl::output>> outputs;
111
112    struct config config;
113
114    layouts_type layouts;
115    layer_map layers;
116
117    typedef std::pair<char const *, std::function<void()>> name_task_pair;
118    std::vector<name_task_pair> pending;
119
120    typedef std::map<std::string, int> drawing_name_map;
121    drawing_name_map name_mapping;
122
123    struct id_allocator id_alloc;
124
125    std::deque<unsigned> last_active;
126
127    explicit App(wl::display *d);
128    ~App();
129
130    App(App const &) = delete;
131    App &operator=(App const &) = delete;
132    App(App &&) = delete;
133    App &operator=(App &&) = delete;
134
135    int init();
136    int init_layout();
137
138    int dispatch_events();
139
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);
143
144    // Allocate a surface ID for this role
145    result<int> request_surface(char const *drawing_name);
146
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);
150
151    // add tasks, executed after dispatch_events()
152    void add_task(char const *name, std::function<void()> &&f);
153    void execute_pending();
154
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);
158
159    // TMC WM Events to clients
160    void emit_activated(char const *label);
161    void emit_deactivated(char const *label);
162    void emit_syncdraw(char const *label);
163    void emit_visible(char const *label, bool is_visible);
164 };
165
166 }  // namespace wm
167
168 #endif  // TMCAGLWM_APP_HPP