summaryrefslogtreecommitdiff
path: root/kiosk-shell/kiosk-shell-grab.c
blob: 1a4db85af5e47d37f7db9c0430da39a2e405dc61 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/*
 * Copyright 2010-2012 Intel Corporation
 * Copyright 2013 Raspberry Pi Foundation
 * Copyright 2011-2012,2020 Collabora, Ltd.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 */

#include "kiosk-shell-grab.h"
#include "shared/helpers.h"

struct kiosk_shell_grab {
	struct kiosk_shell_surface *shsurf;
	struct wl_listener shsurf_destroy_listener;

	struct weston_pointer_grab pointer_grab;
	struct weston_touch_grab touch_grab;
	wl_fixed_t dx, dy;
	bool active;
};

static void
kiosk_shell_grab_destroy(struct kiosk_shell_grab *shgrab);

/*
 * pointer_move_grab_interface
 */

static void
pointer_move_grab_focus(struct weston_pointer_grab *grab)
{
}

static void
pointer_move_grab_axis(struct weston_pointer_grab *grab,
		       const struct timespec *time,
		       struct weston_pointer_axis_event *event)
{
}

static void
pointer_move_grab_axis_source(struct weston_pointer_grab *grab,
			      uint32_t source)
{
}

static void
pointer_move_grab_frame(struct weston_pointer_grab *grab)
{
}

static void
pointer_move_grab_motion(struct weston_pointer_grab *pointer_grab,
			 const struct timespec *time,
			 struct weston_pointer_motion_event *event)
{
	struct kiosk_shell_grab *shgrab =
		container_of(pointer_grab, struct kiosk_shell_grab, pointer_grab);
	struct weston_pointer *pointer = pointer_grab->pointer;
	struct kiosk_shell_surface *shsurf = shgrab->shsurf;
	struct weston_surface *surface;
	int dx, dy;

	weston_pointer_move(pointer, event);

	if (!shsurf)
		return;

	surface = weston_desktop_surface_get_surface(shsurf->desktop_surface);

	dx = pointer->pos.c.x + wl_fixed_to_double(shgrab->dx);
	dy = pointer->pos.c.y + wl_fixed_to_double(shgrab->dy);

	weston_view_set_position(shsurf->view, dx, dy);

	weston_compositor_schedule_repaint(surface->compositor);
}

static void
pointer_move_grab_button(struct weston_pointer_grab *pointer_grab,
			 const struct timespec *time,
			 uint32_t button, uint32_t state_w)
{
	struct kiosk_shell_grab *shgrab =
		container_of(pointer_grab, struct kiosk_shell_grab, pointer_grab);
	struct weston_pointer *pointer = pointer_grab->pointer;
	enum wl_pointer_button_state state = state_w;

	if (pointer->button_count == 0 &&
	    state == WL_POINTER_BUTTON_STATE_RELEASED)
		kiosk_shell_grab_destroy(shgrab);
}

static void
pointer_move_grab_cancel(struct weston_pointer_grab *pointer_grab)
{
	struct kiosk_shell_grab *shgrab =
		container_of(pointer_grab, struct kiosk_shell_grab, pointer_grab);

	kiosk_shell_grab_destroy(shgrab);
}

static const struct weston_pointer_grab_interface pointer_move_grab_interface = {
	pointer_move_grab_focus,
	pointer_move_grab_motion,
	pointer_move_grab_button,
	pointer_move_grab_axis,
	pointer_move_grab_axis_source,
	pointer_move_grab_frame,
	pointer_move_grab_cancel,
};

/*
 * touch_move_grab_interface
 */

static void
touch_move_grab_down(struct weston_touch_grab *grab,
		     const struct timespec *time,
		     int touch_id, wl_fixed_t x, wl_fixed_t y)
{
}

static void
touch_move_grab_up(struct weston_touch_grab *touch_grab,
		   const struct timespec *time, int touch_id)
{
	struct kiosk_shell_grab *shgrab =
		container_of(touch_grab, struct kiosk_shell_grab, touch_grab);

	if (touch_id == 0)
		shgrab->active = false;

	if (touch_grab->touch->num_tp == 0)
		kiosk_shell_grab_destroy(shgrab);
}

static void
touch_move_grab_motion(struct weston_touch_grab *touch_grab,
		       const struct timespec *time, int touch_id,
		       wl_fixed_t x, wl_fixed_t y)
{
	struct kiosk_shell_grab *shgrab =
		container_of(touch_grab, struct kiosk_shell_grab, touch_grab);
	struct weston_touch *touch = touch_grab->touch;
	struct kiosk_shell_surface *shsurf = shgrab->shsurf;
	struct weston_surface *surface;
	int dx, dy;

	if (!shsurf || !shgrab->active)
		return;

	surface = weston_desktop_surface_get_surface(shsurf->desktop_surface);

	dx = wl_fixed_to_int(touch->grab_x + shgrab->dx);
	dy = wl_fixed_to_int(touch->grab_y + shgrab->dy);

	weston_view_set_position(shsurf->view, dx, dy);

	weston_compositor_schedule_repaint(surface->compositor);
}

static void
touch_move_grab_frame(struct weston_touch_grab *grab)
{
}

static void
touch_move_grab_cancel(struct weston_touch_grab *touch_grab)
{
	struct kiosk_shell_grab *shgrab =
		container_of(touch_grab, struct kiosk_shell_grab, touch_grab);

	kiosk_shell_grab_destroy(shgrab);
}

static const struct weston_touch_grab_interface touch_move_grab_interface = {
	touch_move_grab_down,
	touch_move_grab_up,
	touch_move_grab_motion,
	touch_move_grab_frame,
	touch_move_grab_cancel,
};

/*
 * kiosk_shell_grab
 */

static void
kiosk_shell_grab_handle_shsurf_destroy(struct wl_listener *listener, void *data)
{
	struct kiosk_shell_grab *shgrab =
		container_of(listener, struct kiosk_shell_grab,
			     shsurf_destroy_listener);

	shgrab->shsurf = NULL;
}

static struct kiosk_shell_grab *
kiosk_shell_grab_create(struct kiosk_shell_surface *shsurf)
{
	struct kiosk_shell_grab *shgrab;

	shgrab = zalloc(sizeof *shgrab);
	if (!shgrab)
		return NULL;

	shgrab->shsurf = shsurf;
	shgrab->shsurf_destroy_listener.notify =
		kiosk_shell_grab_handle_shsurf_destroy;
	wl_signal_add(&shsurf->destroy_signal,
		      &shgrab->shsurf_destroy_listener);

	shsurf->grabbed = true;

	return shgrab;
}

enum kiosk_shell_grab_result
kiosk_shell_grab_start_for_pointer_move(struct kiosk_shell_surface *shsurf,
					struct weston_pointer *pointer)
{
	struct kiosk_shell_grab *shgrab;
	struct weston_coord_global offset;

	if (!shsurf)
		return KIOSK_SHELL_GRAB_RESULT_ERROR;

	if (shsurf->grabbed ||
	    weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
	    weston_desktop_surface_get_maximized(shsurf->desktop_surface))
		return KIOSK_SHELL_GRAB_RESULT_IGNORED;

	shgrab = kiosk_shell_grab_create(shsurf);
	if (!shgrab)
		return KIOSK_SHELL_GRAB_RESULT_ERROR;

	offset.c = weston_coord_sub(shsurf->view->geometry.pos_offset,
				    pointer->grab_pos.c);
	shgrab->dx = wl_fixed_from_double(offset.c.x);
	shgrab->dy = wl_fixed_from_double(offset.c.y);
	shgrab->active = true;

	weston_seat_break_desktop_grabs(pointer->seat);

	shgrab->pointer_grab.interface = &pointer_move_grab_interface;
	weston_pointer_start_grab(pointer, &shgrab->pointer_grab);

	return KIOSK_SHELL_GRAB_RESULT_OK;
}

enum kiosk_shell_grab_result
kiosk_shell_grab_start_for_touch_move(struct kiosk_shell_surface *shsurf,
				      struct weston_touch *touch)
{
	struct kiosk_shell_grab *shgrab;

	if (!shsurf)
		return KIOSK_SHELL_GRAB_RESULT_ERROR;

	if (shsurf->grabbed ||
	    weston_desktop_surface_get_fullscreen(shsurf->desktop_surface) ||
	    weston_desktop_surface_get_maximized(shsurf->desktop_surface))
		return KIOSK_SHELL_GRAB_RESULT_IGNORED;

	shgrab = kiosk_shell_grab_create(shsurf);
	if (!shgrab)
		return KIOSK_SHELL_GRAB_RESULT_ERROR;

	shgrab->dx = wl_fixed_from_double(shsurf->view->geometry.pos_offset.x) -
		   touch->grab_x;
	shgrab->dy = wl_fixed_from_double(shsurf->view->geometry.pos_offset.y) -
		   touch->grab_y;
	shgrab->active = true;

	weston_seat_break_desktop_grabs(touch->seat);

	shgrab->touch_grab.interface = &touch_move_grab_interface;
	weston_touch_start_grab(touch, &shgrab->touch_grab);

	return KIOSK_SHELL_GRAB_RESULT_OK;
}

static void
kiosk_shell_grab_destroy(struct kiosk_shell_grab *shgrab)
{
	if (shgrab->shsurf) {
		wl_list_remove(&shgrab->shsurf_destroy_listener.link);
		shgrab->shsurf->grabbed = false;
	}

	if (shgrab->pointer_grab.pointer)
		weston_pointer_end_grab(shgrab->pointer_grab.pointer);
	else if (shgrab->touch_grab.touch)
		weston_touch_end_grab(shgrab->touch_grab.touch);

	free(shgrab);
}