8f30d96753618f72f82e0ec7782593fbc0aa55ae
[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    // Insert and return a new ID
57    unsigned generate_id(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> lookup(std::string const &name) const {
68       auto i = this->names.find(name);
69       return i == this->names.end() ? nullopt : optional<unsigned>(i->second);
70    }
71
72    optional<std::string> lookup(unsigned id) const {
73       auto i = this->surfaces.find(id);
74       return i == this->surfaces.end() ? nullopt
75                                        : optional<std::string>(i->second);
76    }
77
78    // Remove a surface id and name
79    // I don't think I will need this, do I?
80    void remove_id(std::string const &name) {
81       auto i = this->names.find(name);
82       if (i != this->names.end()) {
83          this->surfaces.erase(i->second);
84          this->names.erase(i);
85       }
86    }
87
88    void remove_id(unsigned id) {
89       auto i = this->surfaces.find(id);
90       if (i != this->surfaces.end()) {
91          this->names.erase(i->second);
92          this->surfaces.erase(i);
93       }
94    }
95 };
96
97 struct App {
98    struct binding_api api;
99    struct controller_hooks chooks;
100
101    // This is the one thing, we do not own.
102    struct wl::display *display;
103
104    std::unique_ptr<struct genivi::controller> controller;
105    std::vector<std::unique_ptr<struct wl::output>> outputs;
106
107    struct config config;
108
109    layouts_type layouts;
110    layer_map layers;
111
112    typedef std::pair<char const *, std::function<void()>> name_task_pair;
113    std::vector<name_task_pair> pending;
114
115    typedef std::map<std::string, int> drawing_name_map;
116    drawing_name_map name_mapping;
117
118    struct id_allocator id_alloc;
119
120    explicit App(wl::display *d);
121    ~App();
122
123    App(App const &) = delete;
124    App &operator=(App const &) = delete;
125    App(App &&) = delete;
126    App &operator=(App &&) = delete;
127
128    int init();
129    int init_layout();
130
131    int dispatch_events();
132
133    void surface_set_layout(uint32_t surface_id);
134    char const *activate_surface(uint32_t surface_id);
135
136    // Allocate a surface ID for this role
137    result<int> request_surface(char const *drawing_name);
138
139    // Activate (i.e. make visible, if allowed!) a surface
140    char const *activate_surface(char const *drawing_name);
141
142    // add tasks, executed after dispatch_events()
143    void add_task(char const *name, std::function<void()> &&f);
144    void execute_pending();
145
146    // Events from the compositor we are interested in
147    void surface_created(uint32_t surface_id);
148    void surface_removed(uint32_t surface_id);
149 };
150
151 }  // namespace wm
152
153 #endif  // TMCAGLWM_APP_HPP