1 /*
2  * Copyright © 2013 Rafal Mielniczuk
3  *
4  * This file is part of the glmark2 OpenGL (ES) 2.0 benchmark.
5  *
6  * glmark2 is free software: you can redistribute it and/or modify it under the
7  * terms of the GNU General Public License as published by the Free Software
8  * Foundation, either version 3 of the License, or (at your option) any later
9  * version.
10  *
11  * glmark2 is distributed in the hope that it will be useful, but WITHOUT ANY
12  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
14  * details.
15  *
16  * You should have received a copy of the GNU General Public License along with
17  * glmark2.  If not, see <http://www.gnu.org/licenses/>.
18  *
19  * Authors:
20  *  Rafal Mielniczuk <rafal.mielniczuk2@gmail.com>
21  */
22 
23 #ifndef GLMARK2_NATIVE_STATE_WAYLAND_H_
24 #define GLMARK2_NATIVE_STATE_WAYLAND_H_
25 
26 #include <vector>
27 #include <wayland-client.h>
28 #include <wayland-egl.h>
29 #include <wayland-cursor.h>
30 #include "xdg-shell-client-protocol.h"
31 
32 #include "native-state.h"
33 
34 class NativeStateWayland : public NativeState
35 {
36 public:
37     NativeStateWayland();
38     ~NativeStateWayland();
39 
40     bool init_display();
41     void* display();
42     bool create_window(WindowProperties const& properties);
43     void* window(WindowProperties& properties);
44     void visible(bool v);
45     bool should_quit();
46     void flip();
47 
48 private:
49     static void quit_handler(int signum);
50 
51     static const struct wl_registry_listener registry_listener_;
52     static const struct xdg_wm_base_listener xdg_wm_base_listener_;
53     static const struct xdg_surface_listener xdg_surface_listener_;
54     static const struct xdg_toplevel_listener xdg_toplevel_listener_;
55     static const struct wl_output_listener output_listener_;
56     static const struct wl_seat_listener seat_listener_;
57     static const struct wl_pointer_listener pointer_listener_;
58     static const struct wl_keyboard_listener keyboard_listener_;
59 
60     static void
61     registry_handle_global(void *data, struct wl_registry *registry,
62                            uint32_t id, const char *interface,
63                            uint32_t version);
64     static void
65     registry_handle_global_remove(void *data, struct wl_registry *registry,
66                                   uint32_t name);
67 
68     static void
69     output_handle_geometry(void *data, struct wl_output *wl_output,
70                            int32_t x, int32_t y, int32_t physical_width,
71                            int32_t physical_height, int32_t subpixel,
72                            const char *make, const char *model,
73                            int32_t transform);
74 
75     static void
76     output_handle_mode(void *data, struct wl_output *wl_output, uint32_t flags,
77              int32_t width, int32_t height, int32_t refresh);
78 
79     static void
80     output_handle_done(void *data, struct wl_output *wl_output);
81 
82     static void
83     output_handle_scale(void *data, struct wl_output *wl_output, int32_t factor);
84 
85     static void
86     xdg_wm_base_handle_ping(void *data, struct xdg_wm_base *xdg_wm_base,
87                             uint32_t serial);
88     static void
89     xdg_toplevel_handle_configure(void *data,
90                                   struct xdg_toplevel *xdg_toplevel,
91                                   int32_t width, int32_t height,
92                                   struct wl_array *states);
93     static void
94     xdg_toplevel_handle_close(void *data, struct xdg_toplevel *xdg_toplevel);
95     static void
96     xdg_surface_handle_configure(void *data, struct xdg_surface *xdg_surface,
97                                  uint32_t serial);
98 
99     static void seat_handle_capabilities(void *data,
100                                          struct wl_seat *seat, uint32_t caps);
101 
102     static void pointer_handle_enter(void *data, struct wl_pointer *pointer,
103                                      uint32_t serial, struct wl_surface *surface,
104                                      wl_fixed_t sx, wl_fixed_t sy);
105 
106     static void pointer_handle_leave(void *data, struct wl_pointer *pointer,
107                                      uint32_t serial, struct wl_surface *surface);
108 
109     static void pointer_handle_motion(void *data, struct wl_pointer *pointer,
110                                       uint32_t time, wl_fixed_t sx, wl_fixed_t sy);
111 
112     static void pointer_handle_button(void *data, struct wl_pointer *wl_pointer,
113                                       uint32_t serial, uint32_t time,
114                                       uint32_t button, uint32_t state);
115 
116     static void pointer_handle_axis(void *data, struct wl_pointer *wl_pointer,
117                                     uint32_t time, uint32_t axis, wl_fixed_t value);
118 
119     static void keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
120                                        uint32_t format, int fd, uint32_t size);
121     static void keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
122                                       uint32_t serial, struct wl_surface *surface,
123                                       struct wl_array *keys);
124     static void keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
125                                       uint32_t serial, struct wl_surface *surface);
126     static void keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
127                                     uint32_t serial, uint32_t time, uint32_t key,
128                                     uint32_t state);
129     static void keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
130                                           uint32_t serial, uint32_t mods_depressed,
131                                           uint32_t mods_latched, uint32_t mods_locked,
132                                           uint32_t group);
133     void setup_cursor();
134 
135     struct my_output {
136         wl_output *output;
137         int32_t width, height;
138         int32_t refresh;
139         int32_t scale;
140     };
141 
142     typedef std::vector<struct my_output *> OutputsVector;
143 
144     struct my_cursor {
145         wl_cursor_theme *cursor_theme;
146         wl_cursor *default_cursor;
147         wl_surface *cursor_surface;
148     } *cursor_;
149 
150     struct my_display {
151         wl_display *display;
152         wl_registry *registry;
153         wl_compositor *compositor;
154         wl_shm *shm;
155         wl_seat *seat;
156         wl_pointer *pointer;
157         wl_keyboard *keyboard;
158         struct xdg_wm_base *xdg_wm_base;
159         OutputsVector outputs;
160     } *display_;
161 
162     struct my_window {
163         WindowProperties properties;
164         bool waiting_for_configure;
165         struct wl_surface *surface;
166         struct wl_egl_window *native;
167         struct xdg_surface *xdg_surface;
168         struct xdg_toplevel *xdg_toplevel;
169     } *window_;
170 
171     static volatile bool should_quit_;
172 };
173 
174 #endif /* GLMARK2_NATIVE_STATE_WAYLAND_H_ */
175