App: some quality improvements, add activate/deactivate helper
[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> id2name;
52    // std::unordered_set<unsigned> pending_surfaces;
53    std::unordered_map<std::string, unsigned> name2id;
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->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());
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->name2id.find(name);
73       return i == this->name2id.end() ? nullopt : optional<unsigned>(i->second);
74    }
75
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);
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->name2id.find(name);
86       if (i != this->name2id.end()) {
87          this->id2name.erase(i->second);
88          this->name2id.erase(i);
89       }
90    }
91
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);
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    // ID allocation and proxy methods for lookup
117    struct id_allocator id_alloc;
118    optional<unsigned> lookup_id(char const *name) {
119       return this->id_alloc.lookup(std::string(name));
120    }
121    optional<std::string> lookup_name(unsigned id) {
122       return this->id_alloc.lookup(id);
123    }
124
125    explicit App(wl::display *d);
126    ~App();
127
128    App(App const &) = delete;
129    App &operator=(App const &) = delete;
130    App(App &&) = delete;
131    App &operator=(App &&) = delete;
132
133    int init();
134    int init_layers();
135
136    int dispatch_events();
137
138    void surface_init_layout(uint32_t surface_id);
139
140    // Allocate a surface ID for this role
141    result<int> request_surface(char const *drawing_name);
142
143    // Activate (i.e. make visible, if allowed!) a surface
144    char const *activate_surface(char const *drawing_name);
145    char const *deactivate_surface(char const *drawing_name);
146
147    // Events from the compositor we are interested in
148    void surface_created(uint32_t surface_id);
149    void surface_removed(uint32_t surface_id);
150
151    // TMC WM Events to clients
152    void emit_activated(char const *label);
153    void emit_deactivated(char const *label);
154    void emit_syncdraw(char const *label);
155    void emit_flushdraw(char const *label);
156    void emit_visible(char const *label, bool is_visible);
157
158    void activate(unsigned id);
159    void deactivate(unsigned id);
160 };
161
162 }  // namespace wm
163
164 #endif  // TMCAGLWM_APP_HPP