1
0
Fork 0
wayland_tests/surface.c

249 lines
6.8 KiB
C
Raw Normal View History

2021-01-03 15:31:38 +02:00
#define _GNU_SOURCE
2021-01-02 22:33:41 +02:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <wayland-client.h>
//#include <wayland-server.h>
#include <wayland-client-protocol.h>
#include "xdg-shell-client-protocol.h"
#include <fcntl.h>
#include <sys/mman.h>
#include <errno.h>
#include <unistd.h>
2021-01-03 15:31:38 +02:00
struct client_state {
/* Globals */
struct wl_display *wl_display;
struct wl_registry *wl_registry;
struct wl_compositor *wl_compositor;
struct wl_shm *wl_shm;
struct xdg_wm_base *xdg_wm_base;
/* Objects */
struct wl_buffer *wl_buffer;
struct wl_surface *wl_surface;
struct xdg_surface *xdg_surface;
struct xdg_toplevel *xdg_toplevel;
};
2021-01-02 22:33:41 +02:00
int WIDTH = 480;
int HEIGHT = 360;
/*
* Create a new, unique, anonymous file of the given size, and
* return the file descriptor for it. The file descriptor is set
* CLOEXEC. The file is immediately suitable for mmap()'ing
* the given size at offset zero.
2021-01-03 15:31:38 +02:00
2021-01-02 22:33:41 +02:00
* The file is suitable for buffer sharing between processes by
* transmitting the file descriptor over Unix sockets using the
* SCM_RIGHTS methods.
*/
int
os_create_anonymous_file(off_t size)
{
2021-01-03 15:31:38 +02:00
int fd;
fd = memfd_create("wayland-shm", MFD_CLOEXEC);
2021-01-02 22:33:41 +02:00
2021-01-03 15:31:38 +02:00
if (fd < 0)
return -1;
2021-01-02 22:33:41 +02:00
2021-01-03 15:31:38 +02:00
if (ftruncate(fd, size) < 0) {
close(fd);
return -1;
2021-01-02 22:33:41 +02:00
}
2021-01-03 15:31:38 +02:00
return fd;
2021-01-02 22:33:41 +02:00
}
static struct wl_buffer *
2021-01-03 15:31:38 +02:00
create_buffer(struct client_state *state) {
2021-01-02 22:33:41 +02:00
struct wl_shm_pool *pool;
int stride = WIDTH * 4; // 4 bytes per pixel
int size = stride * HEIGHT;
int fd;
struct wl_buffer *buff;
fd = os_create_anonymous_file(size);
if (fd < 0) {
fprintf(stderr, "creating a buffer file for %d B failed: %m\n",
size);
exit(1);
}
2021-01-03 15:31:38 +02:00
void *shm_data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
2021-01-02 22:33:41 +02:00
if (shm_data == MAP_FAILED) {
fprintf(stderr, "mmap failed: %m\n");
close(fd);
exit(1);
}
2021-01-03 15:31:38 +02:00
uint32_t *pixel = shm_data;
fprintf(stderr, "Painting pixels\n");
// for (int n =0; n < WIDTH*HEIGHT; n++) {
// *pixel++ = 0xffff;
// }
/* Draw checkerboxed background */
for (int y = 0; y < HEIGHT; ++y) {
for (int x = 0; x < WIDTH; ++x) {
if ((x + y / 8 * 8) % 16 < 8)
pixel[y * WIDTH + x] = 0xFF666666;
else
pixel[y * WIDTH + x] = 0xFFEEEEEE;
}
}
pool = wl_shm_create_pool(state->wl_shm, fd, size);
2021-01-02 22:33:41 +02:00
buff = wl_shm_pool_create_buffer(pool, 0,
2021-01-03 15:31:38 +02:00
WIDTH, HEIGHT,
stride,
WL_SHM_FORMAT_XRGB8888);
2021-01-02 22:33:41 +02:00
//wl_buffer_add_listener(buffer, &buffer_listener, buffer);
wl_shm_pool_destroy(pool);
return buff;
}
static void
2021-01-03 15:31:38 +02:00
handle_configure(void *data, struct xdg_surface *xdg_surface, uint32_t serial)
{
struct client_state *state = data;
xdg_surface_ack_configure(xdg_surface, serial);
fprintf(stderr, "xdg_surface configured\n");
2021-01-02 22:33:41 +02:00
2021-01-03 15:31:38 +02:00
state->wl_buffer = create_buffer(state);
2021-01-02 22:33:41 +02:00
2021-01-03 15:31:38 +02:00
wl_surface_attach(state->wl_surface, state->wl_buffer, 0, 0);
2021-01-02 22:33:41 +02:00
//wl_surface_damage(surface, 0, 0, WIDTH, HEIGHT);
2021-01-03 15:31:38 +02:00
wl_surface_commit(state->wl_surface);
2021-01-02 22:33:41 +02:00
}
2021-01-03 15:31:38 +02:00
static const struct xdg_surface_listener xdg_surface_listener = {
.configure = handle_configure,
};
2021-01-02 22:33:41 +02:00
static void
shm_format(void *data, struct wl_shm *wl_shm, uint32_t format)
{
//struct display *d = data;
// d->formats |= (1 << format);
fprintf(stderr, "Format %d\n", format);
}
struct wl_shm_listener shm_listener = {
shm_format
};
2021-01-03 15:31:38 +02:00
static void
xdg_wm_base_ping(void *data, struct xdg_wm_base *xdg_wm_base, uint32_t serial)
{
xdg_wm_base_pong(xdg_wm_base, serial);
}
static const struct xdg_wm_base_listener xdg_wm_base_listener = {
.ping = xdg_wm_base_ping,
};
2021-01-02 22:33:41 +02:00
static void
global_registry_handler(void *data, struct wl_registry *registry, uint32_t id,
const char *interface, uint32_t version)
{
2021-01-03 15:31:38 +02:00
struct client_state *state = data;
if (strcmp(interface, wl_compositor_interface.name) == 0) {
state->wl_compositor = wl_registry_bind(registry, id,
&wl_compositor_interface, 4);
2021-01-02 22:33:41 +02:00
2021-01-03 15:31:38 +02:00
} else if (strcmp(interface, wl_shm_interface.name) == 0) {
state->wl_shm = wl_registry_bind(registry, id, &wl_shm_interface, 1);
wl_shm_add_listener(state->wl_shm, &shm_listener, state);
} else if (strcmp(interface, xdg_wm_base_interface.name) == 0) {
state->xdg_wm_base = wl_registry_bind(registry, id,
&xdg_wm_base_interface, 1);
xdg_wm_base_add_listener(state->xdg_wm_base, &xdg_wm_base_listener, state);
2021-01-02 22:33:41 +02:00
}
}
static void
global_registry_remover(void *data, struct wl_registry *registry, uint32_t id)
{
printf("Got a registry losing event for %d\n", id);
}
static const struct wl_registry_listener registry_listener = {
global_registry_handler,
global_registry_remover
};
int main(int argc, char **argv) {
2021-01-03 15:31:38 +02:00
struct client_state state = { 0 };
state.wl_display = wl_display_connect(NULL);
if (state.wl_display == NULL) {
2021-01-02 22:33:41 +02:00
fprintf(stderr, "Can't connect to display\n");
exit(1);
}
printf("connected to display\n");
2021-01-03 15:31:38 +02:00
state.wl_registry = wl_display_get_registry(state.wl_display);
wl_registry_add_listener(state.wl_registry, &registry_listener, &state);
wl_display_dispatch(state.wl_display);
wl_display_roundtrip(state.wl_display);
2021-01-02 22:33:41 +02:00
2021-01-03 15:31:38 +02:00
/* By now we should have wl_compositor, wl_shm and xdg_wm_base from global_registry */
2021-01-02 22:33:41 +02:00
2021-01-03 15:31:38 +02:00
if (state.wl_compositor == NULL) {
2021-01-02 22:33:41 +02:00
fprintf(stderr, "Can't find compositor\n");
exit(1);
} else {
fprintf(stderr, "Found compositor\n");
}
2021-01-03 15:31:38 +02:00
state.wl_surface = wl_compositor_create_surface(state.wl_compositor);
if (state.wl_surface == NULL) {
2021-01-02 22:33:41 +02:00
fprintf(stderr, "Can't create surface\n");
exit(1);
} else {
fprintf(stderr, "Created surface\n");
}
2021-01-03 15:31:38 +02:00
if (state.xdg_wm_base == NULL) {
fprintf(stderr, "no wm_base\n");
exit(1);
}
state.xdg_surface = xdg_wm_base_get_xdg_surface(state.xdg_wm_base, state.wl_surface);
if (state.xdg_surface == NULL) {
fprintf(stderr, "no xdg_surface\n");
2021-01-02 22:33:41 +02:00
exit(1);
} else {
2021-01-03 15:31:38 +02:00
fprintf(stderr, "Got xdg_surface\n");
2021-01-02 22:33:41 +02:00
}
2021-01-03 15:31:38 +02:00
xdg_surface_add_listener(state.xdg_surface, &xdg_surface_listener, &state);
2021-01-02 22:33:41 +02:00
2021-01-03 15:31:38 +02:00
state.xdg_toplevel = xdg_surface_get_toplevel(state.xdg_surface);
if (state.xdg_toplevel == NULL) {
fprintf(stderr, "no xdg_toplevel\n");
exit(1);
} else {
fprintf(stderr, "got xdg_toplevel\n");
}
xdg_toplevel_set_title(state.xdg_toplevel, "A surface");
wl_surface_commit(state.wl_surface);
2021-01-02 22:33:41 +02:00
2021-01-03 15:31:38 +02:00
while (wl_display_dispatch(state.wl_display)) {
2021-01-02 22:33:41 +02:00
;
}
2021-01-03 15:31:38 +02:00
wl_display_disconnect(state.wl_display);
2021-01-02 22:33:41 +02:00
printf("disconnected from display\n");
exit(0);
}