clang-tidy
[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
25 #include "afb_binding_api.hpp"
26 #include "config.hpp"
27 #include "controller_hooks.hpp"
28 #include "layers.hpp"
29 #include "layout.hpp"
30 #include "result.hpp"
31 #include "wayland.hpp"
32
33 namespace wl {
34 struct display;
35 }
36
37 namespace genivi {
38 struct controller;
39 }
40
41 namespace wm {
42
43 struct id_allocator {
44    unsigned next = 0x0100'0000;
45
46    // Surfaces that where requested but not yet created
47    std::unordered_map<unsigned, std::string> surfaces;
48    // std::unordered_set<unsigned> pending_surfaces;
49    std::unordered_map<std::string, unsigned> names;
50
51    id_allocator(id_allocator const &) = delete;
52    id_allocator(id_allocator &&) = delete;
53    id_allocator &operator=(id_allocator const &);
54    id_allocator &operator=(id_allocator &&) = delete;
55
56    // Allocate a new ID
57    unsigned operator()(std::string const &name) {
58       unsigned sid = this->next++;
59       this->surfaces[sid] = name;
60       // this->pending_surfaces.insert({sid});
61       this->names[name] = sid;
62       logdebug("allocated new id %u with name %s", sid, name.c_str());
63       return sid;
64    }
65
66    // Lookup by ID or by name
67    optional<unsigned> operator[](std::string const &name) {
68       auto i = this->names.find(name);
69       return i == this->names.end() ? nullopt : optional<unsigned>(i->second);
70    }
71
72    optional<std::string> operator[](unsigned id) {
73       auto i = this->surfaces.find(id);
74       return i == this->surfaces.end() ? nullopt
75                                        : optional<std::string>(i->second);
76    }
77 };
78
79 struct App {
80    struct binding_api api;
81    struct controller_hooks chooks;
82
83    // This is the one thing, we do not own.
84    struct wl::display *display;
85
86    std::unique_ptr<struct genivi::controller> controller;
87    std::vector<std::unique_ptr<struct wl::output>> outputs;
88
89    struct config config;
90
91    layouts_type layouts;
92    layer_map layers;
93
94    typedef std::pair<char const *, std::function<void()>> name_task_pair;
95    std::vector<name_task_pair> pending;
96
97    typedef std::map<std::string, int> drawing_name_map;
98    drawing_name_map name_mapping;
99
100    struct id_allocator id_alloc;
101
102    explicit App(wl::display *d);
103    ~App();
104
105    App(App const &) = delete;
106    App &operator=(App const &) = delete;
107    App(App &&) = delete;
108    App &operator=(App &&) = delete;
109
110    int init();
111    int init_layout();
112
113    int dispatch_events();
114
115    void surface_set_layout(uint32_t surface_id);
116    char const *activate_surface(uint32_t surface_id);
117
118    // Allocate a surface ID for this role
119    result<int> request_surface(char const *drawing_name);
120
121    // Activate (i.e. make visible, if allowed!) a surface
122    char const *activate_surface(char const *drawing_name);
123
124    // add tasks, executed after dispatch_events()
125    void add_task(char const *name, std::function<void()> &&f);
126    void execute_pending();
127
128    // Events from the compositor we are interested in
129    void surface_created(uint32_t surface_id);
130    void surface_removed(uint32_t surface_id);
131 };
132
133 }  // namespace wm
134
135 #endif  // TMCAGLWM_APP_HPP