2011-12-22 07:54:49 +04:00
|
|
|
/*
|
|
|
|
* Copyright © 2011 Benjamin Franzke
|
|
|
|
* Copyright © 2011 Intel Corporation
|
|
|
|
*
|
|
|
|
* 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 <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>
|
2012-06-06 17:59:43 +04:00
|
|
|
#include "../shared/os-compatibility.h"
|
2011-12-22 07:54:49 +04:00
|
|
|
|
2012-10-20 01:12:38 +04:00
|
|
|
#define ARRAY_LENGTH(a) (sizeof (a) / sizeof (a)[0])
|
|
|
|
|
2011-12-22 07:54:49 +04:00
|
|
|
struct touch {
|
|
|
|
struct wl_display *display;
|
2012-10-11 05:34:26 +04:00
|
|
|
struct wl_registry *registry;
|
2011-12-22 07:54:49 +04:00
|
|
|
struct wl_compositor *compositor;
|
|
|
|
struct wl_shell *shell;
|
|
|
|
struct wl_shm *shm;
|
2012-05-16 21:45:18 +04:00
|
|
|
struct wl_seat *seat;
|
|
|
|
struct wl_touch *wl_touch;
|
|
|
|
struct wl_pointer *pointer;
|
|
|
|
struct wl_keyboard *keyboard;
|
2011-12-22 07:54:49 +04:00
|
|
|
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)
|
|
|
|
{
|
2012-04-03 19:21:27 +04:00
|
|
|
struct wl_shm_pool *pool;
|
2011-12-22 07:54:49 +04:00
|
|
|
int fd, size, stride;
|
|
|
|
|
|
|
|
stride = touch->width * 4;
|
|
|
|
size = stride * touch->height;
|
2012-06-06 17:59:43 +04:00
|
|
|
|
|
|
|
fd = os_create_anonymous_file(size);
|
|
|
|
if (fd < 0) {
|
|
|
|
fprintf(stderr, "creating a buffer file for %d B failed: %m\n",
|
|
|
|
size);
|
2011-12-22 07:54:49 +04:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2012-04-03 19:21:27 +04:00
|
|
|
pool = wl_shm_create_pool(touch->shm, fd, size);
|
2011-12-22 07:54:49 +04:00
|
|
|
touch->buffer =
|
2012-04-03 19:21:27 +04:00
|
|
|
wl_shm_pool_create_buffer(pool, 0,
|
|
|
|
touch->width, touch->height, stride,
|
|
|
|
WL_SHM_FORMAT_ARGB8888);
|
|
|
|
wl_shm_pool_destroy(pool);
|
2011-12-22 07:54:49 +04:00
|
|
|
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
shm_format(void *data, struct wl_shm *wl_shm, uint32_t format)
|
|
|
|
{
|
|
|
|
struct touch *touch = data;
|
|
|
|
|
2012-01-11 23:24:46 +04:00
|
|
|
if (format == WL_SHM_FORMAT_ARGB8888)
|
2011-12-22 07:54:49 +04:00
|
|
|
touch->has_argb = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct wl_shm_listener shm_listenter = {
|
|
|
|
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,
|
2012-07-31 14:21:11 +04:00
|
|
|
0xff00ff00,
|
|
|
|
0xff00ffff,
|
2011-12-22 07:54:49 +04:00
|
|
|
};
|
|
|
|
|
2012-03-30 19:52:39 +04:00
|
|
|
if (id < (int32_t) ARRAY_LENGTH(colors))
|
2011-12-22 07:54:49 +04:00
|
|
|
c = colors[id];
|
|
|
|
else
|
|
|
|
c = 0xffffffff;
|
|
|
|
|
2012-07-31 14:21:12 +04:00
|
|
|
if (x < 2 || x >= touch->width - 2 ||
|
|
|
|
y < 2 || y >= touch->height - 2)
|
2011-12-22 07:54:49 +04:00
|
|
|
return;
|
|
|
|
|
2012-07-31 14:21:11 +04:00
|
|
|
p = (uint32_t *) touch->data + (x - 2) + (y - 2) * touch->width;
|
|
|
|
p[2] = c;
|
|
|
|
p += touch->width;
|
2011-12-22 07:54:49 +04:00
|
|
|
p[1] = c;
|
2012-07-31 14:21:11 +04:00
|
|
|
p[2] = c;
|
|
|
|
p[3] = c;
|
2011-12-22 07:54:49 +04:00
|
|
|
p += touch->width;
|
|
|
|
p[0] = c;
|
|
|
|
p[1] = c;
|
|
|
|
p[2] = c;
|
2012-07-31 14:21:11 +04:00
|
|
|
p[3] = c;
|
|
|
|
p[4] = c;
|
2011-12-22 07:54:49 +04:00
|
|
|
p += touch->width;
|
|
|
|
p[1] = c;
|
2012-07-31 14:21:11 +04:00
|
|
|
p[2] = c;
|
|
|
|
p[3] = c;
|
|
|
|
p += touch->width;
|
|
|
|
p[2] = c;
|
2011-12-22 07:54:49 +04:00
|
|
|
|
2012-07-31 14:21:13 +04:00
|
|
|
wl_surface_damage(touch->surface, x - 2, y - 2, 5, 5);
|
2012-10-10 13:49:25 +04:00
|
|
|
/* todo: We could queue up more damage before committing, if there
|
|
|
|
* are more input events to handle.
|
|
|
|
*/
|
|
|
|
wl_surface_commit(touch->surface);
|
2011-12-22 07:54:49 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2012-05-16 21:45:18 +04:00
|
|
|
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)
|
2011-12-22 07:54:49 +04:00
|
|
|
{
|
|
|
|
struct touch *touch = data;
|
2012-05-10 20:21:37 +04:00
|
|
|
float x = wl_fixed_to_double(x_w);
|
|
|
|
float y = wl_fixed_to_double(y_w);
|
2011-12-22 07:54:49 +04:00
|
|
|
|
|
|
|
touch_paint(touch, x, y, id);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2012-05-16 21:45:18 +04:00
|
|
|
touch_handle_up(void *data, struct wl_touch *wl_touch,
|
|
|
|
uint32_t serial, uint32_t time, int32_t id)
|
2011-12-22 07:54:49 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2012-05-16 21:45:18 +04:00
|
|
|
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)
|
2011-12-22 07:54:49 +04:00
|
|
|
{
|
|
|
|
struct touch *touch = data;
|
2012-05-10 20:21:37 +04:00
|
|
|
float x = wl_fixed_to_double(x_w);
|
|
|
|
float y = wl_fixed_to_double(y_w);
|
2011-12-22 07:54:49 +04:00
|
|
|
|
|
|
|
touch_paint(touch, x, y, id);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2012-05-16 21:45:18 +04:00
|
|
|
touch_handle_frame(void *data, struct wl_touch *wl_touch)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
touch_handle_cancel(void *data, struct wl_touch *wl_touch)
|
2011-12-22 07:54:49 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-05-16 21:45:18 +04:00
|
|
|
static const struct wl_touch_listener touch_listener = {
|
|
|
|
touch_handle_down,
|
|
|
|
touch_handle_up,
|
|
|
|
touch_handle_motion,
|
|
|
|
touch_handle_frame,
|
|
|
|
touch_handle_cancel,
|
|
|
|
};
|
|
|
|
|
2011-12-22 07:54:49 +04:00
|
|
|
static void
|
2012-05-16 21:45:18 +04:00
|
|
|
seat_handle_capabilities(void *data, struct wl_seat *seat,
|
|
|
|
enum wl_seat_capability caps)
|
2011-12-22 07:54:49 +04:00
|
|
|
{
|
2012-05-16 21:45:18 +04:00
|
|
|
struct touch *touch = data;
|
|
|
|
|
|
|
|
if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !touch->wl_touch) {
|
|
|
|
touch->wl_touch = wl_seat_get_touch(seat);
|
|
|
|
wl_touch_set_user_data(touch->wl_touch, touch);
|
|
|
|
wl_touch_add_listener(touch->wl_touch, &touch_listener, touch);
|
|
|
|
} else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && touch->wl_touch) {
|
|
|
|
wl_touch_destroy(touch->wl_touch);
|
|
|
|
touch->wl_touch = NULL;
|
|
|
|
}
|
2011-12-22 07:54:49 +04:00
|
|
|
}
|
|
|
|
|
2012-05-16 21:45:18 +04:00
|
|
|
static const struct wl_seat_listener seat_listener = {
|
|
|
|
seat_handle_capabilities,
|
2011-12-22 07:54:49 +04:00
|
|
|
};
|
|
|
|
|
2012-07-31 14:21:10 +04:00
|
|
|
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
|
|
|
|
};
|
|
|
|
|
2011-12-22 07:54:49 +04:00
|
|
|
static void
|
2012-10-11 05:34:26 +04:00
|
|
|
handle_global(void *data, struct wl_registry *registry,
|
|
|
|
uint32_t name, const char *interface, uint32_t version)
|
2011-12-22 07:54:49 +04:00
|
|
|
{
|
|
|
|
struct touch *touch = data;
|
|
|
|
|
|
|
|
if (strcmp(interface, "wl_compositor") == 0) {
|
|
|
|
touch->compositor =
|
2012-10-11 05:34:26 +04:00
|
|
|
wl_registry_bind(registry, name,
|
|
|
|
&wl_compositor_interface, 1);
|
2011-12-22 07:54:49 +04:00
|
|
|
} else if (strcmp(interface, "wl_shell") == 0) {
|
|
|
|
touch->shell =
|
2012-10-11 05:34:26 +04:00
|
|
|
wl_registry_bind(registry, name,
|
|
|
|
&wl_shell_interface, 1);
|
2011-12-22 07:54:49 +04:00
|
|
|
} else if (strcmp(interface, "wl_shm") == 0) {
|
2012-10-11 05:34:26 +04:00
|
|
|
touch->shm = wl_registry_bind(registry, name,
|
|
|
|
&wl_shm_interface, 1);
|
2011-12-22 07:54:49 +04:00
|
|
|
wl_shm_add_listener(touch->shm, &shm_listenter, touch);
|
2012-05-16 21:45:18 +04:00
|
|
|
} else if (strcmp(interface, "wl_seat") == 0) {
|
2012-10-11 05:34:26 +04:00
|
|
|
touch->seat = wl_registry_bind(registry, name,
|
|
|
|
&wl_seat_interface, 1);
|
2012-05-16 21:45:18 +04:00
|
|
|
wl_seat_add_listener(touch->seat, &seat_listener, touch);
|
2011-12-22 07:54:49 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-11 05:34:26 +04:00
|
|
|
static const struct wl_registry_listener registry_listener = {
|
|
|
|
handle_global
|
|
|
|
};
|
2011-12-22 07:54:49 +04:00
|
|
|
|
|
|
|
static struct touch *
|
|
|
|
touch_create(int width, int height)
|
|
|
|
{
|
|
|
|
struct touch *touch;
|
|
|
|
|
|
|
|
touch = malloc(sizeof *touch);
|
|
|
|
touch->display = wl_display_connect(NULL);
|
|
|
|
assert(touch->display);
|
|
|
|
|
|
|
|
touch->has_argb = 0;
|
2012-10-11 05:34:26 +04:00
|
|
|
touch->registry = wl_display_get_registry(touch->display);
|
|
|
|
wl_registry_add_listener(touch->registry, ®istry_listener, touch);
|
|
|
|
wl_display_dispatch(touch->display);
|
2011-12-22 07:54:49 +04:00
|
|
|
wl_display_roundtrip(touch->display);
|
|
|
|
|
|
|
|
if (!touch->has_argb) {
|
|
|
|
fprintf(stderr, "WL_SHM_FORMAT_ARGB32 not available\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2012-10-11 05:34:26 +04:00
|
|
|
wl_display_get_fd(touch->display);
|
2011-12-22 07:54:49 +04:00
|
|
|
|
|
|
|
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);
|
|
|
|
|
2012-07-31 14:21:10 +04:00
|
|
|
if (touch->shell_surface) {
|
|
|
|
wl_shell_surface_add_listener(touch->shell_surface,
|
|
|
|
&shell_surface_listener, touch);
|
|
|
|
wl_shell_surface_set_toplevel(touch->shell_surface);
|
|
|
|
}
|
|
|
|
|
2011-12-22 07:54:49 +04:00
|
|
|
wl_surface_set_user_data(touch->surface, touch);
|
2012-10-07 18:56:30 +04:00
|
|
|
wl_shell_surface_set_title(touch->shell_surface, "simple-touch");
|
2011-12-22 07:54:49 +04:00
|
|
|
|
|
|
|
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);
|
2012-10-10 13:49:24 +04:00
|
|
|
wl_surface_commit(touch->surface);
|
2011-12-22 07:54:49 +04:00
|
|
|
|
|
|
|
return touch;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
struct touch *touch;
|
2012-10-16 21:16:10 +04:00
|
|
|
int ret = 0;
|
2011-12-22 07:54:49 +04:00
|
|
|
|
|
|
|
touch = touch_create(600, 500);
|
|
|
|
|
2012-10-16 21:16:10 +04:00
|
|
|
while (ret != -1)
|
|
|
|
ret = wl_display_dispatch(touch->display);
|
2011-12-22 07:54:49 +04:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|