simple-touch.c 8.54 KB
Newer Older
1 2 3 4
/*
 * Copyright © 2011 Benjamin Franzke
 * Copyright © 2011 Intel Corporation
 *
5 6 7 8 9 10
 * 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:
11
 *
12 13 14 15 16 17 18 19 20 21 22
 * 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.
23 24
 */

25
#include "config.h"
26

27
#include <stdint.h>
28 29 30 31 32 33 34 35 36
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <assert.h>
#include <unistd.h>
#include <sys/mman.h>

#include <wayland-client.h>
37
#include "shared/helpers.h"
38
#include "shared/os-compatibility.h"
39

40 41 42 43 44 45
struct seat {
	struct touch *touch;
	struct wl_seat *seat;
	struct wl_touch *wl_touch;
};

46 47
struct touch {
	struct wl_display *display;
48
	struct wl_registry *registry;
49 50 51
	struct wl_compositor *compositor;
	struct wl_shell *shell;
	struct wl_shm *shm;
52 53
	struct wl_pointer *pointer;
	struct wl_keyboard *keyboard;
54 55 56 57 58 59 60 61 62 63 64
	struct wl_surface *surface;
	struct wl_shell_surface *shell_surface;
	struct wl_buffer *buffer;
	int has_argb;
	int width, height;
	void *data;
};

static void
create_shm_buffer(struct touch *touch)
{
65
	struct wl_shm_pool *pool;
66 67 68 69
	int fd, size, stride;

	stride = touch->width * 4;
	size = stride * touch->height;
70 71 72 73 74

	fd = os_create_anonymous_file(size);
	if (fd < 0) {
		fprintf(stderr, "creating a buffer file for %d B failed: %m\n",
			size);
75 76 77 78 79 80 81 82 83 84 85
		exit(1);
	}

	touch->data =
		mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
	if (touch->data == MAP_FAILED) {
		fprintf(stderr, "mmap failed: %m\n");
		close(fd);
		exit(1);
	}

86
	pool = wl_shm_create_pool(touch->shm, fd, size);
87
	touch->buffer =
88 89 90 91
		wl_shm_pool_create_buffer(pool, 0,
					  touch->width, touch->height, stride,
					  WL_SHM_FORMAT_ARGB8888);
	wl_shm_pool_destroy(pool);
92 93 94 95 96 97 98 99 100

	close(fd);
}

static void
shm_format(void *data, struct wl_shm *wl_shm, uint32_t format)
{
	struct touch *touch = data;

Kristian H. Kristensen's avatar
Kristian H. Kristensen committed
101
	if (format == WL_SHM_FORMAT_ARGB8888)
102 103 104
		touch->has_argb = 1;
}

105
struct wl_shm_listener shm_listener = {
106 107 108 109 110 111 112 113 114 115 116 117 118
	shm_format
};


static void
touch_paint(struct touch *touch, int32_t x, int32_t y, int32_t id)
{
	uint32_t *p, c;
	static const uint32_t colors[] = {
		0xffff0000,
		0xffffff00,
		0xff0000ff,
		0xffff00ff,
119 120
		0xff00ff00,
		0xff00ffff,
121 122
	};

123
	if (id < (int32_t) ARRAY_LENGTH(colors))
124 125 126 127
		c = colors[id];
	else
		c = 0xffffffff;

128 129
	if (x < 2 || x >= touch->width - 2 ||
	    y < 2 || y >= touch->height - 2)
130 131
		return;

132 133 134
	p = (uint32_t *) touch->data + (x - 2) + (y - 2) * touch->width;
	p[2] = c;
	p += touch->width;
135
	p[1] = c;
136 137
	p[2] = c;
	p[3] = c;
138 139 140 141
	p += touch->width;
	p[0] = c;
	p[1] = c;
	p[2] = c;
142 143
	p[3] = c;
	p[4] = c;
144 145
	p += touch->width;
	p[1] = c;
146 147 148 149
	p[2] = c;
	p[3] = c;
	p += touch->width;
	p[2] = c;
150

151
	wl_surface_attach(touch->surface, touch->buffer, 0, 0);
152
	wl_surface_damage(touch->surface, x - 2, y - 2, 5, 5);
153 154 155 156
	/* todo: We could queue up more damage before committing, if there
	 * are more input events to handle.
	 */
	wl_surface_commit(touch->surface);
157 158 159
}

static void
160 161 162
touch_handle_down(void *data, struct wl_touch *wl_touch,
		  uint32_t serial, uint32_t time, struct wl_surface *surface,
		  int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
163 164
{
	struct touch *touch = data;
165 166
	float x = wl_fixed_to_double(x_w);
	float y = wl_fixed_to_double(y_w);
167 168 169 170 171

	touch_paint(touch, x, y, id);
}

static void
172 173
touch_handle_up(void *data, struct wl_touch *wl_touch,
		uint32_t serial, uint32_t time, int32_t id)
174 175 176 177
{
}

static void
178 179
touch_handle_motion(void *data, struct wl_touch *wl_touch,
		    uint32_t time, int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
180 181
{
	struct touch *touch = data;
182 183
	float x = wl_fixed_to_double(x_w);
	float y = wl_fixed_to_double(y_w);
184 185 186 187 188

	touch_paint(touch, x, y, id);
}

static void
189 190 191 192 193 194
touch_handle_frame(void *data, struct wl_touch *wl_touch)
{
}

static void
touch_handle_cancel(void *data, struct wl_touch *wl_touch)
195 196 197
{
}

198 199 200 201 202 203 204 205
static const struct wl_touch_listener touch_listener = {
	touch_handle_down,
	touch_handle_up,
	touch_handle_motion,
	touch_handle_frame,
	touch_handle_cancel,
};

206
static void
207
seat_handle_capabilities(void *data, struct wl_seat *wl_seat,
208
			 enum wl_seat_capability caps)
209
{
210 211 212 213 214 215 216 217 218 219
	struct seat *seat = data;
	struct touch *touch = seat->touch;

	if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !seat->wl_touch) {
		seat->wl_touch = wl_seat_get_touch(wl_seat);
		wl_touch_set_user_data(seat->wl_touch, touch);
		wl_touch_add_listener(seat->wl_touch, &touch_listener, touch);
	} else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && seat->wl_touch) {
		wl_touch_destroy(seat->wl_touch);
		seat->wl_touch = NULL;
220
	}
221 222
}

223 224
static const struct wl_seat_listener seat_listener = {
	seat_handle_capabilities,
225 226
};

227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
static void
add_seat(struct touch *touch, uint32_t name, uint32_t version)
{
	struct seat *seat;

	seat = malloc(sizeof *seat);
	assert(seat);

	seat->touch = touch;
	seat->wl_touch = NULL;
	seat->seat = wl_registry_bind(touch->registry, name,
				      &wl_seat_interface, 1);
	wl_seat_add_listener(seat->seat, &seat_listener, seat);
}

242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
static void
handle_ping(void *data, struct wl_shell_surface *shell_surface,
	    uint32_t serial)
{
	wl_shell_surface_pong(shell_surface, serial);
}

static void
handle_configure(void *data, struct wl_shell_surface *shell_surface,
		 uint32_t edges, int32_t width, int32_t height)
{
}

static void
handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
{
}

static const struct wl_shell_surface_listener shell_surface_listener = {
	handle_ping,
	handle_configure,
	handle_popup_done
};

266
static void
267 268
handle_global(void *data, struct wl_registry *registry,
	      uint32_t name, const char *interface, uint32_t version)
269 270 271 272 273
{
	struct touch *touch = data;

	if (strcmp(interface, "wl_compositor") == 0) {
		touch->compositor =
274 275
			wl_registry_bind(registry, name,
					 &wl_compositor_interface, 1);
276 277
	} else if (strcmp(interface, "wl_shell") == 0) {
		touch->shell =
278 279
			wl_registry_bind(registry, name,
					 &wl_shell_interface, 1);
280
	} else if (strcmp(interface, "wl_shm") == 0) {
281 282
		touch->shm = wl_registry_bind(registry, name,
					      &wl_shm_interface, 1);
283
		wl_shm_add_listener(touch->shm, &shm_listener, touch);
284
	} else if (strcmp(interface, "wl_seat") == 0) {
285
		add_seat(touch, name, version);
286 287 288
	}
}

289 290 291 292 293
static void
handle_global_remove(void *data, struct wl_registry *registry, uint32_t name)
{
}

294
static const struct wl_registry_listener registry_listener = {
295 296
	handle_global,
	handle_global_remove
297
};
298 299 300 301 302 303 304

static struct touch *
touch_create(int width, int height)
{
	struct touch *touch;

	touch = malloc(sizeof *touch);
305 306 307 308
	if (touch == NULL) {
		fprintf(stderr, "out of memory\n");
		exit(1);
	}
309 310 311 312
	touch->display = wl_display_connect(NULL);
	assert(touch->display);

	touch->has_argb = 0;
313 314 315
	touch->registry = wl_display_get_registry(touch->display);
	wl_registry_add_listener(touch->registry, &registry_listener, touch);
	wl_display_dispatch(touch->display);
316 317 318 319 320 321 322 323 324 325 326 327 328 329
	wl_display_roundtrip(touch->display);

	if (!touch->has_argb) {
		fprintf(stderr, "WL_SHM_FORMAT_ARGB32 not available\n");
		exit(1);
	}

	touch->width = width;
	touch->height = height;
	touch->surface = wl_compositor_create_surface(touch->compositor);
	touch->shell_surface = wl_shell_get_shell_surface(touch->shell,
							  touch->surface);
	create_shm_buffer(touch);

330 331 332 333 334 335
	if (touch->shell_surface) {
		wl_shell_surface_add_listener(touch->shell_surface,
					      &shell_surface_listener, touch);
		wl_shell_surface_set_toplevel(touch->shell_surface);
	}

336
	wl_surface_set_user_data(touch->surface, touch);
337
	wl_shell_surface_set_title(touch->shell_surface, "simple-touch");
338 339 340 341

	memset(touch->data, 64, width * height * 4);
	wl_surface_attach(touch->surface, touch->buffer, 0, 0);
	wl_surface_damage(touch->surface, 0, 0, width, height);
342
	wl_surface_commit(touch->surface);
343 344 345 346 347 348 349 350

	return touch;
}

int
main(int argc, char **argv)
{
	struct touch *touch;
351
	int ret = 0;
352 353 354

	touch = touch_create(600, 500);

355 356
	while (ret != -1)
		ret = wl_display_dispatch(touch->display);
357 358 359

	return 0;
}