aac28cd35736d57749ce10f4170cb2bc759b28ec
[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
22 #include <atomic>
23 #include <memory>
24 #include <unordered_map>
25 #include <unordered_set>
26 #include <experimental/optional>
27
28 #include "afb_binding_api.hpp"
29 #include "config.hpp"
30 #include "controller_hooks.hpp"
31 #include "layers.hpp"
32 #include "layout.hpp"
33 #include "policy.hpp"
34 #include "result.hpp"
35 #include "wayland.hpp"
36
37 namespace wl {
38 struct display;
39 }
40
41 namespace genivi {
42 struct controller;
43 }
44
45 namespace wm {
46
47 using std::experimental::optional;
48
49 struct id_allocator {
50    unsigned next = 1;
51
52    // Surfaces that where requested but not yet created
53    std::unordered_map<unsigned, std::string> id2name;
54    // std::unordered_set<unsigned> pending_surfaces;
55    std::unordered_map<std::string, unsigned> name2id;
56
57    id_allocator(id_allocator const &) = delete;
58    id_allocator(id_allocator &&) = delete;
59    id_allocator &operator=(id_allocator const &);
60    id_allocator &operator=(id_allocator &&) = delete;
61
62    // Insert and return a new ID
63    unsigned generate_id(std::string const &name) {
64       unsigned sid = this->next++;
65       this->id2name[sid] = name;
66       // this->pending_surfaces.insert({sid});
67       this->name2id[name] = sid;
68       logdebug("allocated new id %u with name %s", sid, name.c_str());
69       return sid;
70    }
71
72    // Lookup by ID or by name
73    optional<unsigned> lookup(std::string const &name) const {
74       auto i = this->name2id.find(name);
75       return i == this->name2id.end() ? nullopt : optional<unsigned>(i->second);
76    }
77
78    optional<std::string> lookup(unsigned id) const {
79       auto i = this->id2name.find(id);
80       return i == this->id2name.end() ? nullopt
81                                        : optional<std::string>(i->second);
82    }
83
84    // Remove a surface id and name
85    // I don't think I will need this, do I?
86    void remove_id(std::string const &name) {
87       auto i = this->name2id.find(name);
88       if (i != this->name2id.end()) {
89          this->id2name.erase(i->second);
90          this->name2id.erase(i);
91       }
92    }
93
94    void remove_id(unsigned id) {
95       auto i = this->id2name.find(id);
96       if (i != this->id2name.end()) {
97          this->name2id.erase(i->second);
98          this->id2name.erase(i);
99       }
100    }
101 };
102
103 struct App {
104    struct binding_api api;
105    struct controller_hooks chooks;
106
107    // This is the one thing, we do not own.
108    struct wl::display *display;
109
110    std::unique_ptr<struct genivi::controller> controller;
111    std::vector<std::unique_ptr<struct wl::output>> outputs;
112
113    struct config config;
114
115    // track current layouts separately
116    layer_map layers;
117
118    // ID allocation and proxy methods for lookup
119    struct id_allocator id_alloc;
120
121    // Set by AFB API when wayland events need to be dispatched
122    std::atomic<bool> pending_events;
123
124    std::vector<int> pending_end_draw;
125
126    Policy policy;
127
128    explicit App(wl::display *d);
129    ~App();
130
131    App(App const &) = delete;
132    App &operator=(App const &) = delete;
133    App(App &&) = delete;
134    App &operator=(App &&) = delete;
135
136    int init();
137
138    int dispatch_events();
139    int dispatch_pending_events();
140
141    void set_pending_events();
142
143    result<int> api_request_surface(char const *drawing_name);
144    char const *api_activate_surface(char const *drawing_name);
145    char const *api_deactivate_surface(char const *drawing_name);
146    char const *api_enddraw(char const *drawing_name);
147    void api_ping();
148
149    // Events from the compositor we are interested in
150    void surface_created(uint32_t surface_id);
151    void surface_removed(uint32_t surface_id);
152
153 private:
154    optional<int> lookup_id(char const *name);
155    optional<std::string> lookup_name(int id);
156
157    bool pop_pending_events();
158
159    void enqueue_flushdraw(int surface_id);
160    void check_flushdraw(int surface_id);
161
162    int init_layers();
163
164    void surface_set_layout(int surface_id, optional<int> sub_surface_id = nullopt);
165    void layout_commit();
166
167    // TMC WM Events to clients
168    void emit_activated(char const *label);
169    void emit_deactivated(char const *label);
170    void emit_syncdraw(char const *label);
171    void emit_flushdraw(char const *label);
172    void emit_visible(char const *label, bool is_visible);
173    void emit_invisible(char const *label);
174    void emit_visible(char const *label);
175
176    void activate(int id);
177    void deactivate(int id);
178    void deactivate_main_surface();
179
180    bool can_split(struct LayoutState const &state, int new_id);
181    void try_layout(struct LayoutState &state,
182                    struct LayoutState const &new_layout,
183                    std::function<void(LayoutState const &nl)> apply);
184 };
185
186 }  // namespace wm
187
188 #endif  // TMCAGLWM_APP_HPP