2011-12-22 07:54:49 +04:00
|
|
|
/*
|
|
|
|
* Copyright © 2011 Benjamin Franzke
|
|
|
|
* Copyright © 2011 Intel Corporation
|
2021-08-13 18:18:34 +03:00
|
|
|
* Copyright © 2021 Collabora, Ltd.
|
2011-12-22 07:54:49 +04:00
|
|
|
*
|
2015-06-11 08:48:59 +03:00
|
|
|
* 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:
|
2011-12-22 07:54:49 +04:00
|
|
|
*
|
2015-06-11 08:48:59 +03:00
|
|
|
* 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.
|
2011-12-22 07:54:49 +04:00
|
|
|
*/
|
|
|
|
|
2016-06-16 04:13:07 +03:00
|
|
|
#include "config.h"
|
2013-08-27 01:43:17 +04:00
|
|
|
|
2016-07-19 14:16:27 +03:00
|
|
|
#include <stdint.h>
|
2011-12-22 07:54:49 +04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <unistd.h>
|
2019-04-27 00:57:31 +03:00
|
|
|
#include <errno.h>
|
2011-12-22 07:54:49 +04:00
|
|
|
#include <sys/mman.h>
|
|
|
|
|
|
|
|
#include <wayland-client.h>
|
2015-06-16 01:37:08 +03:00
|
|
|
#include "shared/helpers.h"
|
2021-08-13 18:18:34 +03:00
|
|
|
#include "shared/xalloc.h"
|
2015-06-16 01:37:07 +03:00
|
|
|
#include "shared/os-compatibility.h"
|
2011-12-22 07:54:49 +04:00
|
|
|
|
2021-08-13 18:18:34 +03:00
|
|
|
#include "xdg-shell-client-protocol.h"
|
|
|
|
|
2014-05-07 12:57:27 +04:00
|
|
|
struct seat {
|
|
|
|
struct touch *touch;
|
|
|
|
struct wl_seat *seat;
|
|
|
|
struct wl_touch *wl_touch;
|
|
|
|
};
|
|
|
|
|
2021-08-13 18:18:34 +03:00
|
|
|
struct buffer {
|
|
|
|
struct wl_buffer *buffer;
|
|
|
|
void *data;
|
|
|
|
};
|
|
|
|
|
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;
|
2021-08-13 18:18:34 +03:00
|
|
|
struct xdg_wm_base *wm_base;
|
2011-12-22 07:54:49 +04:00
|
|
|
struct wl_shm *shm;
|
|
|
|
struct wl_surface *surface;
|
2021-08-13 18:18:34 +03:00
|
|
|
struct xdg_surface *xdg_surface;
|
|
|
|
struct xdg_toplevel *xdg_toplevel;
|
|
|
|
struct buffer *buffer;
|
2011-12-22 07:54:49 +04:00
|
|
|
int width, height;
|
2022-12-22 19:27:14 +03:00
|
|
|
int init_width, init_height;
|
2021-10-14 12:43:46 +03:00
|
|
|
bool running;
|
2021-08-13 18:18:34 +03:00
|
|
|
bool wait_for_configure;
|
2022-12-22 19:27:14 +03:00
|
|
|
bool needs_buffer_update;
|
2021-10-14 12:43:46 +03:00
|
|
|
bool has_argb;
|
2022-12-22 19:27:14 +03:00
|
|
|
bool maximized;
|
|
|
|
bool fullscreen;
|
2011-12-22 07:54:49 +04:00
|
|
|
};
|
|
|
|
|
2021-08-13 18:18:34 +03:00
|
|
|
static struct buffer *
|
2011-12-22 07:54:49 +04:00
|
|
|
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;
|
2021-08-13 18:18:34 +03:00
|
|
|
void *data;
|
2022-07-20 12:45:04 +03:00
|
|
|
struct buffer *buffer;
|
2021-08-13 18:18:34 +03:00
|
|
|
|
2022-07-20 12:45:04 +03:00
|
|
|
buffer = xzalloc(sizeof(*buffer));
|
2011-12-22 07:54:49 +04:00
|
|
|
|
|
|
|
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) {
|
2019-04-27 00:57:31 +03:00
|
|
|
fprintf(stderr, "creating a buffer file for %d B failed: %s\n",
|
|
|
|
size, strerror(errno));
|
2011-12-22 07:54:49 +04:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2021-08-13 18:18:34 +03:00
|
|
|
data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
|
|
|
|
if (data == MAP_FAILED) {
|
2019-04-27 00:57:31 +03:00
|
|
|
fprintf(stderr, "mmap failed: %s\n", strerror(errno));
|
2011-12-22 07:54:49 +04:00
|
|
|
close(fd);
|
2021-08-13 18:18:34 +03:00
|
|
|
return NULL;
|
2011-12-22 07:54:49 +04:00
|
|
|
}
|
|
|
|
|
2012-04-03 19:21:27 +04:00
|
|
|
pool = wl_shm_create_pool(touch->shm, fd, size);
|
2021-08-13 18:18:34 +03:00
|
|
|
buffer->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);
|
2021-08-13 18:18:34 +03:00
|
|
|
|
2012-04-03 19:21:27 +04:00
|
|
|
wl_shm_pool_destroy(pool);
|
2011-12-22 07:54:49 +04:00
|
|
|
|
2021-08-13 18:18:34 +03:00
|
|
|
buffer->data = data;
|
2011-12-22 07:54:49 +04:00
|
|
|
close(fd);
|
2021-08-13 18:18:34 +03:00
|
|
|
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2022-12-22 19:27:14 +03:00
|
|
|
redraw(void *data)
|
2021-08-13 18:18:34 +03:00
|
|
|
{
|
|
|
|
struct touch *touch = data;
|
|
|
|
struct buffer *buffer = NULL;
|
|
|
|
|
|
|
|
buffer = create_shm_buffer(touch);
|
|
|
|
assert(buffer);
|
|
|
|
|
2022-12-22 19:27:14 +03:00
|
|
|
if (touch->buffer)
|
|
|
|
free(touch->buffer);
|
|
|
|
|
2021-08-13 18:18:34 +03:00
|
|
|
touch->buffer = buffer;
|
|
|
|
|
|
|
|
/* paint the "work-area" */
|
|
|
|
memset(buffer->data, 64, touch->width * touch->height * 4);
|
|
|
|
|
|
|
|
wl_surface_attach(touch->surface, buffer->buffer, 0, 0);
|
|
|
|
wl_surface_damage(touch->surface, 0, 0, touch->width, touch->height);
|
|
|
|
wl_surface_commit(touch->surface);
|
2011-12-22 07:54:49 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2021-10-14 12:41:28 +03:00
|
|
|
touch->has_argb = true;
|
2011-12-22 07:54:49 +04:00
|
|
|
}
|
|
|
|
|
2013-08-05 16:50:50 +04:00
|
|
|
struct wl_shm_listener shm_listener = {
|
2011-12-22 07:54:49 +04:00
|
|
|
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;
|
|
|
|
|
2021-08-13 18:18:34 +03:00
|
|
|
p = ((uint32_t *) touch->buffer->data) + (x - 2) + (y - 2) * touch->width;
|
2012-07-31 14:21:11 +04:00
|
|
|
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
|
|
|
|
2021-08-13 18:18:34 +03:00
|
|
|
wl_surface_attach(touch->surface, touch->buffer->buffer, 0, 0);
|
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
|
2014-05-07 12:57:27 +04:00
|
|
|
seat_handle_capabilities(void *data, struct wl_seat *wl_seat,
|
2012-05-16 21:45:18 +04:00
|
|
|
enum wl_seat_capability caps)
|
2011-12-22 07:54:49 +04:00
|
|
|
{
|
2014-05-07 12:57:27 +04:00
|
|
|
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;
|
2012-05-16 21:45:18 +04:00
|
|
|
}
|
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
|
|
|
};
|
|
|
|
|
2014-05-07 12:57:27 +04:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2012-07-31 14:21:10 +04:00
|
|
|
static void
|
2021-08-13 18:18:34 +03:00
|
|
|
handle_xdg_surface_configure(void *data, struct xdg_surface *surface,
|
|
|
|
uint32_t serial)
|
2012-07-31 14:21:10 +04:00
|
|
|
{
|
2021-08-13 18:18:34 +03:00
|
|
|
struct touch *touch = data;
|
2012-07-31 14:21:10 +04:00
|
|
|
|
2021-08-13 18:18:34 +03:00
|
|
|
xdg_surface_ack_configure(surface, serial);
|
|
|
|
|
2022-12-22 19:27:14 +03:00
|
|
|
if (touch->wait_for_configure || touch->needs_buffer_update) {
|
|
|
|
redraw(touch);
|
2021-08-13 18:18:34 +03:00
|
|
|
touch->wait_for_configure = false;
|
2022-12-22 19:27:14 +03:00
|
|
|
touch->needs_buffer_update = false;
|
2021-08-13 18:18:34 +03:00
|
|
|
}
|
2012-07-31 14:21:10 +04:00
|
|
|
}
|
|
|
|
|
2021-08-13 18:18:34 +03:00
|
|
|
static const struct xdg_surface_listener xdg_surface_listener = {
|
|
|
|
handle_xdg_surface_configure,
|
|
|
|
};
|
|
|
|
|
2012-07-31 14:21:10 +04:00
|
|
|
static void
|
2021-08-13 18:18:34 +03:00
|
|
|
xdg_wm_base_ping(void *data, struct xdg_wm_base *shell, uint32_t serial)
|
2012-07-31 14:21:10 +04:00
|
|
|
{
|
2021-08-13 18:18:34 +03:00
|
|
|
xdg_wm_base_pong(shell, serial);
|
2012-07-31 14:21:10 +04:00
|
|
|
}
|
|
|
|
|
2021-08-13 18:18:34 +03:00
|
|
|
static const struct xdg_wm_base_listener wm_base_listener = {
|
|
|
|
xdg_wm_base_ping,
|
2012-07-31 14:21:10 +04:00
|
|
|
};
|
|
|
|
|
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);
|
2021-08-13 18:18:34 +03:00
|
|
|
} else if (strcmp(interface, "xdg_wm_base") == 0) {
|
|
|
|
touch->wm_base =
|
2012-10-11 05:34:26 +04:00
|
|
|
wl_registry_bind(registry, name,
|
2021-08-13 18:18:34 +03:00
|
|
|
&xdg_wm_base_interface, 1);
|
|
|
|
xdg_wm_base_add_listener(touch->wm_base,
|
|
|
|
&wm_base_listener, touch);
|
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);
|
2013-08-05 16:50:50 +04:00
|
|
|
wl_shm_add_listener(touch->shm, &shm_listener, touch);
|
2012-05-16 21:45:18 +04:00
|
|
|
} else if (strcmp(interface, "wl_seat") == 0) {
|
2014-05-07 12:57:27 +04:00
|
|
|
add_seat(touch, name, version);
|
2011-12-22 07:54:49 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-22 16:53:55 +04:00
|
|
|
static void
|
|
|
|
handle_global_remove(void *data, struct wl_registry *registry, uint32_t name)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-10-11 05:34:26 +04:00
|
|
|
static const struct wl_registry_listener registry_listener = {
|
2013-01-22 16:53:55 +04:00
|
|
|
handle_global,
|
|
|
|
handle_global_remove
|
2012-10-11 05:34:26 +04:00
|
|
|
};
|
2011-12-22 07:54:49 +04:00
|
|
|
|
2021-08-13 18:18:34 +03:00
|
|
|
static void
|
|
|
|
handle_toplevel_configure(void *data, struct xdg_toplevel *xdg_toplevel,
|
2022-12-22 19:27:14 +03:00
|
|
|
int32_t width, int32_t height, struct wl_array *states)
|
2021-08-13 18:18:34 +03:00
|
|
|
{
|
2022-12-22 19:27:14 +03:00
|
|
|
struct touch *touch = data;
|
|
|
|
uint32_t *p;
|
|
|
|
|
|
|
|
touch->fullscreen = false;
|
|
|
|
touch->maximized = false;
|
|
|
|
|
|
|
|
wl_array_for_each(p, states) {
|
|
|
|
uint32_t state = *p;
|
|
|
|
switch (state) {
|
|
|
|
case XDG_TOPLEVEL_STATE_FULLSCREEN:
|
|
|
|
touch->fullscreen = true;
|
|
|
|
break;
|
|
|
|
case XDG_TOPLEVEL_STATE_MAXIMIZED:
|
|
|
|
touch->maximized = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (width > 0 && height > 0) {
|
|
|
|
if (!touch->fullscreen && !touch->maximized) {
|
|
|
|
touch->init_width = width;
|
|
|
|
touch->init_width = height;
|
|
|
|
}
|
|
|
|
touch->width = width;
|
|
|
|
touch->height = height;
|
|
|
|
} else if (!touch->fullscreen && !touch->maximized) {
|
|
|
|
touch->width = touch->init_width;
|
|
|
|
touch->height = touch->init_height;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
touch->needs_buffer_update = true;
|
2021-08-13 18:18:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
handle_toplevel_close(void *data, struct xdg_toplevel *xdg_toplevel)
|
|
|
|
{
|
2021-10-14 12:43:46 +03:00
|
|
|
struct touch *touch = data;
|
|
|
|
touch->running = false;
|
2021-08-13 18:18:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static const struct xdg_toplevel_listener xdg_toplevel_listener = {
|
|
|
|
handle_toplevel_configure,
|
|
|
|
handle_toplevel_close,
|
|
|
|
};
|
|
|
|
|
2011-12-22 07:54:49 +04:00
|
|
|
static struct touch *
|
|
|
|
touch_create(int width, int height)
|
|
|
|
{
|
|
|
|
struct touch *touch;
|
|
|
|
|
|
|
|
touch = malloc(sizeof *touch);
|
2013-08-15 22:40:30 +04:00
|
|
|
if (touch == NULL) {
|
|
|
|
fprintf(stderr, "out of memory\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
2011-12-22 07:54:49 +04:00
|
|
|
touch->display = wl_display_connect(NULL);
|
|
|
|
assert(touch->display);
|
|
|
|
|
2021-10-14 12:41:28 +03:00
|
|
|
touch->has_argb = false;
|
2022-12-22 19:27:14 +03:00
|
|
|
touch->buffer = NULL;
|
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);
|
|
|
|
}
|
|
|
|
|
2021-08-13 18:18:34 +03:00
|
|
|
if (!touch->wm_base) {
|
|
|
|
fprintf(stderr, "xdg-shell required!\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2022-12-22 19:27:14 +03:00
|
|
|
touch->init_width = width;
|
|
|
|
touch->init_height = height;
|
2011-12-22 07:54:49 +04:00
|
|
|
touch->surface = wl_compositor_create_surface(touch->compositor);
|
2012-07-31 14:21:10 +04:00
|
|
|
|
2021-08-13 18:18:34 +03:00
|
|
|
touch->xdg_surface =
|
|
|
|
xdg_wm_base_get_xdg_surface(touch->wm_base, touch->surface);
|
|
|
|
assert(touch->xdg_surface);
|
|
|
|
|
|
|
|
xdg_surface_add_listener(touch->xdg_surface, &xdg_surface_listener, touch);
|
2011-12-22 07:54:49 +04:00
|
|
|
|
2021-08-13 18:18:34 +03:00
|
|
|
touch->xdg_toplevel = xdg_surface_get_toplevel(touch->xdg_surface);
|
|
|
|
assert(touch->xdg_toplevel);
|
|
|
|
xdg_toplevel_add_listener(touch->xdg_toplevel,
|
|
|
|
&xdg_toplevel_listener, touch);
|
|
|
|
xdg_toplevel_set_title(touch->xdg_toplevel, "simple-touch");
|
|
|
|
xdg_toplevel_set_app_id(touch->xdg_toplevel, "simple-touch");
|
|
|
|
touch->wait_for_configure = true;
|
2022-12-22 19:27:14 +03:00
|
|
|
touch->needs_buffer_update = false;
|
2012-10-10 13:49:24 +04:00
|
|
|
wl_surface_commit(touch->surface);
|
2011-12-22 07:54:49 +04:00
|
|
|
|
2021-10-14 12:43:46 +03:00
|
|
|
touch->running = true;
|
|
|
|
|
2011-12-22 07:54:49 +04:00
|
|
|
return touch;
|
|
|
|
}
|
|
|
|
|
2021-08-13 18:18:34 +03:00
|
|
|
static void
|
|
|
|
destroy_touch(struct touch *touch)
|
|
|
|
{
|
|
|
|
if (touch->buffer->buffer)
|
|
|
|
wl_buffer_destroy(touch->buffer->buffer);
|
|
|
|
|
|
|
|
if (touch->xdg_toplevel)
|
|
|
|
xdg_toplevel_destroy(touch->xdg_toplevel);
|
|
|
|
if (touch->xdg_surface)
|
|
|
|
xdg_surface_destroy(touch->xdg_surface);
|
|
|
|
if (touch->wm_base)
|
|
|
|
xdg_wm_base_destroy(touch->wm_base);
|
|
|
|
if (touch->shm)
|
|
|
|
wl_shm_destroy(touch->shm);
|
|
|
|
if (touch->compositor)
|
|
|
|
wl_compositor_destroy(touch->compositor);
|
|
|
|
|
|
|
|
|
|
|
|
wl_surface_destroy(touch->surface);
|
|
|
|
free(touch->buffer);
|
|
|
|
free(touch);
|
|
|
|
}
|
|
|
|
|
2011-12-22 07:54:49 +04:00
|
|
|
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);
|
|
|
|
|
2021-10-14 12:43:46 +03:00
|
|
|
while (ret != -1 && touch->running)
|
2012-10-16 21:16:10 +04:00
|
|
|
ret = wl_display_dispatch(touch->display);
|
2011-12-22 07:54:49 +04:00
|
|
|
|
2021-08-13 18:18:34 +03:00
|
|
|
destroy_touch(touch);
|
2011-12-22 07:54:49 +04:00
|
|
|
return 0;
|
|
|
|
}
|