summaryrefslogtreecommitdiff
path: root/src/pointer-tracking.c
blob: a148f89f9bc7a966a900c094dd26d9d1cff462b4 (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
/*
 * Copyright © 2012 Collabora Ltd.
 *
 * Permission to use, copy, modify, distribute, and sell this software and
 * its documentation for any purpose is hereby granted without fee, provided
 * that the above copyright notice appear in all copies and that both that
 * copyright notice and this permission notice appear in supporting
 * documentation, and that the name of the copyright holders not be used in
 * advertising or publicity pertaining to distribution of the software
 * without specific, written prior permission.  The copyright holders make
 * no representations about the suitability of this software for any
 * purpose.  It is provided "as is" without express or implied warranty.
 *
 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <stdlib.h>

#include "compositor.h"
#include "pointer-tracking-server-protocol.h"

struct pointer_tracking_surface {
	struct wl_resource *resource;
	struct weston_surface *surface;
	struct wl_list link;
};

static void
pointer_tracking_surface_destroy(struct pointer_tracking_surface *pts)
{
	pts->resource = NULL;
	pts->surface = NULL;
	wl_list_remove(&pts->link);
	free(pts);
}

struct pointer_tracking {
	struct wl_object base;
	struct weston_compositor *ec;
	struct wl_global *global;

	struct wl_list surfaces;
};

static void
pointer_track_relative_to(struct wl_client *client,
			  struct wl_resource *resource,
			  struct wl_resource *surface_resource)
{
	struct pointer_tracking *pt = resource->data;
	struct weston_surface *surface = surface_resource->data;
	struct pointer_tracking_surface *pts;

	pts = malloc(sizeof *pts);

	pts->resource = resource;
	pts->surface = surface;

	wl_list_insert(&pt->surfaces, &pts->link);
}

static struct pointer_tracking_interface pointer_tracking_implementation = {
	pointer_track_relative_to
};

static void
unbind_pointer_tracking(struct wl_resource *resource)
{
	struct pointer_tracking *pt = resource->data;
	struct pointer_tracking_surface *pts, *tmp;

	wl_list_for_each_safe(pts, tmp, &pt->surfaces, link) {
		if (pts->resource == resource) {
			pointer_tracking_surface_destroy(pts);
		}
	}
}

static void
bind_pointer_tracking(struct wl_client *client,
	     void *data, uint32_t version, uint32_t id)
{
	struct wl_resource *resource;

	resource = wl_client_add_object(client, &pointer_tracking_interface,
					&pointer_tracking_implementation,
					id, data);
	resource->destroy = unbind_pointer_tracking;
}

struct pointer_tracking *
pointer_tracking_create(struct weston_compositor *ec)
{
	struct pointer_tracking *pt;

	pt = malloc(sizeof *pt);
	if (pt == NULL)
		return NULL;

	pt->base.interface = &pointer_tracking_interface;
	pt->base.implementation = NULL;
	pt->ec = ec;

	wl_list_init(&pt->surfaces);

	pt->global = wl_display_add_global(ec->wl_display,
					  &pointer_tracking_interface,
					  pt,
					  bind_pointer_tracking);

	return pt;
}

void
pointer_tracking_push(struct pointer_tracking *pt, int32_t x, int32_t y)
{
	struct pointer_tracking_surface *pts;

	wl_list_for_each(pts, &pt->surfaces, link) {
		int32_t sx, sy;

		weston_surface_from_global(pts->surface, x, y, &sx, &sy);
		pointer_tracking_send_pointer_moved(pts->resource, sx, sy);
	}
}

void
pointer_tracking_destroy(struct pointer_tracking *pt)
{
	struct pointer_tracking_surface *pts, *tmp;

	wl_list_for_each_safe(pts, tmp, &pt->surfaces, link) {
		pointer_tracking_surface_destroy(pts);
	}

	wl_display_remove_global(pt->ec->wl_display, pt->global);
	free(pt);
}