1
0
Fork 0

working resize

This commit is contained in:
Arti Zirk 2021-01-03 16:54:36 +02:00
parent 23f4e6dbac
commit 11fbabb326
1 changed files with 105 additions and 33 deletions

138
surface.c
View File

@ -10,6 +10,7 @@
#include <sys/mman.h>
#include <errno.h>
#include <unistd.h>
#include <stdbool.h>
struct client_state {
@ -24,10 +25,13 @@ struct client_state {
struct wl_surface *wl_surface;
struct xdg_surface *xdg_surface;
struct xdg_toplevel *xdg_toplevel;
/* State */
float offset;
uint32_t last_frame;
int width, height;
bool closed;
};
int WIDTH = 480;
int HEIGHT = 360;
/*
* Create a new, unique, anonymous file of the given size, and
@ -56,55 +60,90 @@ os_create_anonymous_file(off_t size)
return fd;
}
static struct wl_buffer *
create_buffer(struct client_state *state) {
struct wl_shm_pool *pool;
int stride = WIDTH * 4; // 4 bytes per pixel
int size = stride * HEIGHT;
int fd;
struct wl_buffer *buff;
static void
wl_buffer_release(void *data, struct wl_buffer *wl_buffer)
{
/* Sent by the compositor when it's no longer using this buffer */
wl_buffer_destroy(wl_buffer);
}
fd = os_create_anonymous_file(size);
static const struct wl_buffer_listener wl_buffer_listener = {
.release = wl_buffer_release,
};
static struct wl_buffer *
draw_frame(struct client_state *state) {
int width = state->width, height = state->height;
int stride = width * 4; // 4 bytes per pixel
int size = stride * height;
int fd = os_create_anonymous_file(size);
if (fd < 0) {
fprintf(stderr, "creating a buffer file for %d B failed: %m\n",
size);
exit(1);
}
void *shm_data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (shm_data == MAP_FAILED) {
uint32_t *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (data == MAP_FAILED) {
fprintf(stderr, "mmap failed: %m\n");
close(fd);
exit(1);
}
uint32_t *pixel = shm_data;
struct wl_shm_pool *pool = wl_shm_create_pool(state->wl_shm, fd, size);
struct wl_buffer *buffer = wl_shm_pool_create_buffer(pool, 0,
width, height, stride, WL_SHM_FORMAT_XRGB8888);
wl_shm_pool_destroy(pool);
close(fd);
fprintf(stderr, "Painting pixels\n");
// for (int n =0; n < WIDTH*HEIGHT; n++) {
// *pixel++ = 0xffff;
// *data++ = 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;
int offset = (int)state->offset % 8;
for (int y = 0; y < height; ++y) {
for (int x = 0; x < width; ++x) {
if (((x+offset) + (y+offset) / 8 * 8) % 16 < 8)
data[y * width + x] = 0xFF666666;
else
pixel[y * WIDTH + x] = 0xFFEEEEEE;
data[y * width + x] = 0xFFEEEEEE;
}
}
pool = wl_shm_create_pool(state->wl_shm, fd, size);
buff = wl_shm_pool_create_buffer(pool, 0,
WIDTH, HEIGHT,
stride,
WL_SHM_FORMAT_XRGB8888);
//wl_buffer_add_listener(buffer, &buffer_listener, buffer);
wl_shm_pool_destroy(pool);
return buff;
munmap(data, size);
wl_buffer_add_listener(buffer, &wl_buffer_listener, NULL);
return buffer;
}
static void
xdg_toplevel_configure(void *data,
struct xdg_toplevel *xdg_toplevel, int32_t width, int32_t height,
struct wl_array *states)
{
struct client_state *state = data;
if (width == 0 || height == 0) {
/* Compositor is deferring to us */
return;
}
state->width = width;
state->height = height;
}
static void
xdg_toplevel_close(void *data, struct xdg_toplevel *toplevel)
{
struct client_state *state = data;
state->closed = true;
}
static const struct xdg_toplevel_listener xdg_toplevel_listener = {
.configure = xdg_toplevel_configure,
.close = xdg_toplevel_close,
};
static void
handle_configure(void *data, struct xdg_surface *xdg_surface, uint32_t serial)
{
@ -112,10 +151,9 @@ handle_configure(void *data, struct xdg_surface *xdg_surface, uint32_t serial)
xdg_surface_ack_configure(xdg_surface, serial);
fprintf(stderr, "xdg_surface configured\n");
state->wl_buffer = create_buffer(state);
wl_surface_attach(state->wl_surface, state->wl_buffer, 0, 0);
//wl_surface_damage(surface, 0, 0, WIDTH, HEIGHT);
struct wl_buffer *buffer = draw_frame(state);
wl_surface_attach(state->wl_surface, buffer, 0, 0);
wl_surface_damage(state->wl_surface, 0, 0, INT32_MAX, INT32_MAX);
wl_surface_commit(state->wl_surface);
}
@ -154,7 +192,7 @@ global_registry_handler(void *data, struct wl_registry *registry, uint32_t id,
if (strcmp(interface, wl_compositor_interface.name) == 0) {
state->wl_compositor = wl_registry_bind(registry, id,
&wl_compositor_interface, 4);
&wl_compositor_interface, 1);
} else if (strcmp(interface, wl_shm_interface.name) == 0) {
state->wl_shm = wl_registry_bind(registry, id, &wl_shm_interface, 1);
@ -178,10 +216,41 @@ static const struct wl_registry_listener registry_listener = {
global_registry_remover
};
static const struct wl_callback_listener wl_surface_frame_listener;
static void
wl_surface_frame_done(void *data, struct wl_callback *cb, uint32_t time)
{
// Destroy this callback
wl_callback_destroy(cb);
// Request another frame
struct client_state *state = data;
cb = wl_surface_frame(state->wl_surface);
wl_callback_add_listener(cb, &wl_surface_frame_listener, state);
if (state->last_frame != 0) {
int elapsed = time - state->last_frame;
state->offset += elapsed / 1000.0 * 24;
}
struct wl_buffer *buffer = draw_frame(state);
wl_surface_attach(state->wl_surface, buffer, 0, 0);
wl_surface_damage(state->wl_surface, 0, 0, INT32_MAX, INT32_MAX);
wl_surface_commit(state->wl_surface);
state->last_frame = time;
}
static const struct wl_callback_listener wl_surface_frame_listener = {
.done = wl_surface_frame_done,
};
int main(int argc, char **argv) {
struct client_state state = { 0 };
state.width = 640;
state.height = 480;
state.wl_display = wl_display_connect(NULL);
if (state.wl_display == NULL) {
@ -212,6 +281,8 @@ int main(int argc, char **argv) {
} else {
fprintf(stderr, "Created surface\n");
}
struct wl_callback *cb = wl_surface_frame(state.wl_surface);
wl_callback_add_listener(cb, &wl_surface_frame_listener, &state);
if (state.xdg_wm_base == NULL) {
fprintf(stderr, "no wm_base\n");
@ -233,6 +304,7 @@ int main(int argc, char **argv) {
} else {
fprintf(stderr, "got xdg_toplevel\n");
}
xdg_toplevel_add_listener(state.xdg_toplevel, &xdg_toplevel_listener, &state);
xdg_toplevel_set_title(state.xdg_toplevel, "A surface");
wl_surface_commit(state.wl_surface);