1 #ifndef ROOTSTON_VIEW_H
2 #define ROOTSTON_VIEW_H
3 
4 #include "desktop.h"
5 
6 #include <stdbool.h>
7 #include <wlr/config.h>
8 #include <wlr/types/wlr_box.h>
9 #include <wlr/types/wlr_foreign_toplevel_management_v1.h>
10 #include <wlr/types/wlr_surface.h>
11 #include <wlr/types/wlr_xdg_decoration_v1.h>
12 #include <wlr/types/wlr_xdg_shell.h>
13 
14 #include "output.h"
15 
16 struct roots_view;
17 
18 struct roots_view_interface {
19 	void (*activate)(struct roots_view *view, bool active);
20 	void (*move)(struct roots_view *view, double x, double y);
21 	void (*resize)(struct roots_view *view, uint32_t width, uint32_t height);
22 	void (*move_resize)(struct roots_view *view, double x, double y,
23 		uint32_t width, uint32_t height);
24 	bool (*want_scaling)(struct roots_view *view);
25 	bool (*want_auto_maximize)(struct roots_view *view);
26 	void (*maximize)(struct roots_view *view, bool maximized);
27 	void (*set_fullscreen)(struct roots_view *view, bool fullscreen);
28 	void (*close)(struct roots_view *view);
29 	void (*for_each_surface)(struct roots_view *view,
30 		wlr_surface_iterator_func_t iterator, void *user_data);
31 	void (*get_geometry)(struct roots_view *view, struct wlr_box *box);
32 	void (*destroy)(struct roots_view *view);
33 };
34 
35 enum roots_view_type {
36 	ROOTS_XDG_SHELL_VIEW,
37 #ifdef PHOC_XWAYLAND
38 	ROOTS_XWAYLAND_VIEW,
39 #endif
40 };
41 
42 typedef enum {
43   PHOC_VIEW_TILE_LEFT,
44   PHOC_VIEW_TILE_RIGHT,
45 } PhocViewTileDirection;
46 
47 typedef enum {
48   PHOC_VIEW_STATE_NORMAL,
49   PHOC_VIEW_STATE_MAXIMIZED,
50   PHOC_VIEW_STATE_TILED,
51 } PhocViewState;
52 
53 struct roots_view {
54 	enum roots_view_type type;
55 	const struct roots_view_interface *impl;
56 	PhocDesktop *desktop;
57 	struct wl_list link; // roots_desktop::views
58 	struct wl_list parent_link; // roots_view::stack
59 
60 	struct wlr_box box;
61 	float alpha;
62 	float scale;
63 
64 	bool decorated;
65 	int border_width;
66 	int titlebar_height;
67 
68 	char *title;
69 	char *app_id;
70 
71 	PhocViewState state;
72 	PhocViewTileDirection tile_direction;
73 	PhocOutput *fullscreen_output;
74 	struct {
75 		double x, y;
76 		uint32_t width, height;
77 	} saved;
78 
79 	struct {
80 		bool update_x, update_y;
81 		double x, y;
82 		uint32_t width, height;
83 	} pending_move_resize;
84 
85 	struct roots_view *parent;
86 	struct wl_list stack; // roots_view::link
87 
88 	struct wlr_surface *wlr_surface; // set only when the surface is mapped
89 	struct wl_list child_surfaces; // roots_view_child::link
90 
91 	struct wlr_foreign_toplevel_handle_v1 *toplevel_handle;
92 	struct wl_listener toplevel_handle_request_maximize;
93 	struct wl_listener toplevel_handle_request_activate;
94 	struct wl_listener toplevel_handle_request_fullscreen;
95 	struct wl_listener toplevel_handle_request_close;
96 
97 	struct wl_listener new_subsurface;
98 
99 	struct {
100 		struct wl_signal unmap;
101 		struct wl_signal destroy;
102 	} events;
103 };
104 
105 struct roots_xdg_toplevel_decoration;
106 
107 struct roots_xdg_surface {
108 	struct roots_view view;
109 
110 	struct wlr_xdg_surface *xdg_surface;
111 
112 	struct wlr_box saved_geometry;
113 
114 	struct wl_listener destroy;
115 	struct wl_listener new_popup;
116 	struct wl_listener map;
117 	struct wl_listener unmap;
118 	struct wl_listener request_move;
119 	struct wl_listener request_resize;
120 	struct wl_listener request_maximize;
121 	struct wl_listener request_fullscreen;
122 	struct wl_listener set_title;
123 	struct wl_listener set_app_id;
124 	struct wl_listener set_parent;
125 
126 	struct wl_listener surface_commit;
127 
128 	uint32_t pending_move_resize_configure_serial;
129 
130 	struct roots_xdg_toplevel_decoration *xdg_toplevel_decoration;
131 };
132 
133 #ifdef PHOC_XWAYLAND
134 struct roots_xwayland_surface {
135 	struct roots_view view;
136 
137 	struct wlr_xwayland_surface *xwayland_surface;
138 
139 	struct wl_listener destroy;
140 	struct wl_listener request_configure;
141 	struct wl_listener request_move;
142 	struct wl_listener request_resize;
143 	struct wl_listener request_maximize;
144 	struct wl_listener request_fullscreen;
145 	struct wl_listener map;
146 	struct wl_listener unmap;
147 	struct wl_listener set_title;
148 	struct wl_listener set_class;
149 
150 	struct wl_listener surface_commit;
151 };
152 #endif
153 
154 struct roots_view_child;
155 
156 struct roots_view_child_interface {
157 	void (*destroy)(struct roots_view_child *child);
158 };
159 
160 struct roots_view_child {
161 	struct roots_view *view;
162 	const struct roots_view_child_interface *impl;
163 	struct wlr_surface *wlr_surface;
164 	struct wl_list link;
165 
166 	struct wl_listener commit;
167 	struct wl_listener new_subsurface;
168 };
169 
170 struct roots_subsurface {
171 	struct roots_view_child view_child;
172 	struct wlr_subsurface *wlr_subsurface;
173 	struct wl_listener destroy;
174 	struct wl_listener map;
175 	struct wl_listener unmap;
176 };
177 
178 struct roots_xdg_popup {
179 	struct roots_view_child view_child;
180 	struct wlr_xdg_popup *wlr_popup;
181 	struct wl_listener destroy;
182 	struct wl_listener map;
183 	struct wl_listener unmap;
184 	struct wl_listener new_popup;
185 };
186 
187 struct roots_xdg_toplevel_decoration {
188 	struct wlr_xdg_toplevel_decoration_v1 *wlr_decoration;
189 	struct roots_xdg_surface *surface;
190 	struct wl_listener destroy;
191 	struct wl_listener request_mode;
192 	struct wl_listener surface_commit;
193 };
194 
195 void view_init(struct roots_view *view, const struct roots_view_interface *impl,
196 	enum roots_view_type type, PhocDesktop *desktop);
197 void view_destroy(struct roots_view *view);
198 void view_appear_activated(struct roots_view *view, bool activated);
199 void view_activate(struct roots_view *view, bool activate);
200 void view_apply_damage(struct roots_view *view);
201 void view_damage_whole(struct roots_view *view);
202 gboolean view_is_maximized(const struct roots_view *view);
203 gboolean view_is_tiled(const struct roots_view *view);
204 void view_update_position(struct roots_view *view, int x, int y);
205 void view_update_size(struct roots_view *view, int width, int height);
206 void view_update_decorated(struct roots_view *view, bool decorated);
207 void view_initial_focus(struct roots_view *view);
208 void view_map(struct roots_view *view, struct wlr_surface *surface);
209 void view_unmap(struct roots_view *view);
210 void view_arrange_maximized(struct roots_view *view, struct wlr_output *output);
211 void view_arrange_tiled(struct roots_view *view, struct wlr_output *output);
212 void view_get_box(const struct roots_view *view, struct wlr_box *box);
213 void view_get_geometry(struct roots_view *view, struct wlr_box *box);
214 void view_move(struct roots_view *view, double x, double y);
215 bool view_move_to_next_output (struct roots_view *view, enum wlr_direction direction);
216 void view_resize(struct roots_view *view, uint32_t width, uint32_t height);
217 void view_move_resize(struct roots_view *view, double x, double y,
218 	uint32_t width, uint32_t height);
219 void view_auto_maximize(struct roots_view *view);
220 void view_tile(struct roots_view *view, PhocViewTileDirection direction, struct wlr_output *output);
221 void view_maximize(struct roots_view *view, struct wlr_output *output);
222 void view_restore(struct roots_view *view);
223 void view_set_fullscreen(struct roots_view *view, bool fullscreen,
224 	struct wlr_output *output);
225 void view_close(struct roots_view *view);
226 bool view_center(struct roots_view *view);
227 void view_setup(struct roots_view *view);
228 void view_teardown(struct roots_view *view);
229 void view_set_title(struct roots_view *view, const char *title);
230 void view_set_parent(struct roots_view *view, struct roots_view *parent);
231 void view_set_app_id(struct roots_view *view, const char *app_id);
232 void view_create_foreign_toplevel_handle(struct roots_view *view);
233 void view_get_deco_box(const struct roots_view *view, struct wlr_box *box);
234 void view_for_each_surface(struct roots_view *view,
235 	wlr_surface_iterator_func_t iterator, void *user_data);
236 struct roots_view *roots_view_from_wlr_surface (struct wlr_surface *surface);
237 
238 struct roots_xdg_surface *roots_xdg_surface_from_view(struct roots_view *view);
239 struct roots_xwayland_surface *roots_xwayland_surface_from_view(
240 	struct roots_view *view);
241 
242 enum roots_deco_part {
243 	ROOTS_DECO_PART_NONE = 0,
244 	ROOTS_DECO_PART_TOP_BORDER = (1 << 0),
245 	ROOTS_DECO_PART_BOTTOM_BORDER = (1 << 1),
246 	ROOTS_DECO_PART_LEFT_BORDER = (1 << 2),
247 	ROOTS_DECO_PART_RIGHT_BORDER = (1 << 3),
248 	ROOTS_DECO_PART_TITLEBAR = (1 << 4),
249 };
250 
251 enum roots_deco_part view_get_deco_part(struct roots_view *view, double sx, double sy);
252 
253 void view_child_init(struct roots_view_child *child,
254 	const struct roots_view_child_interface *impl, struct roots_view *view,
255 	struct wlr_surface *wlr_surface);
256 void view_child_destroy(struct roots_view_child *child);
257 
258 struct roots_subsurface *subsurface_create(struct roots_view *view,
259 	struct wlr_subsurface *wlr_subsurface);
260 
261 #endif
262