shm wayland window
This commit is contained in:
parent
27b66bec0a
commit
8f551459fe
4 changed files with 197 additions and 17 deletions
148
glonkers.c
148
glonkers.c
|
|
@ -7,7 +7,10 @@
|
|||
#include <wayland-util.h>
|
||||
#include <SDL3/SDL.h>
|
||||
#include <pthread.h>
|
||||
#include <sys/mman.h>
|
||||
#include "renderer.h"
|
||||
#include "shm.h"
|
||||
#include "xdg-shell-protocol.h"
|
||||
|
||||
//int main() {
|
||||
// struct wl_display *display = wl_display_connect(NULL);
|
||||
|
|
@ -22,10 +25,96 @@
|
|||
//
|
||||
// wl_display_disconnect(display);
|
||||
//}
|
||||
struct our_state {
|
||||
// ...
|
||||
struct wl_compositor *compositor;
|
||||
// ...
|
||||
|
||||
struct client_state {
|
||||
/* Globals */
|
||||
struct wl_display *wl_display;
|
||||
struct wl_registry *wl_registry;
|
||||
struct wl_shm *wl_shm;
|
||||
struct wl_compositor *wl_compositor;
|
||||
struct xdg_wm_base *xdg_wm_base;
|
||||
/* Objects */
|
||||
struct wl_surface *wl_surface;
|
||||
struct xdg_surface *xdg_surface;
|
||||
struct xdg_toplevel *xdg_toplevel;
|
||||
};
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
static const struct wl_buffer_listener wl_buffer_listener = {
|
||||
.release = wl_buffer_release,
|
||||
};
|
||||
|
||||
|
||||
static struct wl_buffer *
|
||||
draw_frame(struct client_state *state)
|
||||
{
|
||||
const int width = 640, height = 480;
|
||||
int stride = width * 4;
|
||||
int size = stride * height;
|
||||
|
||||
int fd = allocate_shm_file(size);
|
||||
if (fd == -1) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
uint32_t *data = mmap(NULL, size,
|
||||
PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
|
||||
if (data == MAP_FAILED) {
|
||||
close(fd);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
/* Draw checkerboxed background */
|
||||
for (int y = 0; y < height; ++y) {
|
||||
for (int x = 0; x < width; ++x) {
|
||||
if ((x + y / 8 * 8) % 16 < 8)
|
||||
data[y * width + x] = 0xFF666666;
|
||||
else
|
||||
data[y * width + x] = 0xFFEEEEEE;
|
||||
}
|
||||
}
|
||||
|
||||
munmap(data, size);
|
||||
wl_buffer_add_listener(buffer, &wl_buffer_listener, NULL);
|
||||
return buffer;
|
||||
}
|
||||
|
||||
static void
|
||||
xdg_surface_configure(void *data,
|
||||
struct xdg_surface *xdg_surface, uint32_t serial)
|
||||
{
|
||||
struct client_state *state = data;
|
||||
xdg_surface_ack_configure(xdg_surface, serial);
|
||||
|
||||
struct wl_buffer *buffer = draw_frame(state);
|
||||
wl_surface_attach(state->wl_surface, buffer, 0, 0);
|
||||
wl_surface_commit(state->wl_surface);
|
||||
}
|
||||
|
||||
static const struct xdg_surface_listener xdg_surface_listener = {
|
||||
.configure = xdg_surface_configure,
|
||||
};
|
||||
|
||||
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,
|
||||
};
|
||||
|
||||
static void
|
||||
|
|
@ -35,12 +124,21 @@ registry_handle_global(void *data, struct wl_registry *registry,
|
|||
printf("interface: '%s', version: %d, name: %d\n",
|
||||
interface, version, name);
|
||||
|
||||
struct our_state *state = data;
|
||||
struct client_state *state = data;
|
||||
|
||||
if (strcmp(interface, wl_compositor_interface.name) == 0) {
|
||||
state->compositor = wl_registry_bind(
|
||||
registry, name, &wl_compositor_interface, 4);
|
||||
if (strcmp(interface, wl_shm_interface.name) == 0) {
|
||||
state->wl_shm = wl_registry_bind(
|
||||
registry, name, &wl_shm_interface, 1);
|
||||
} else if (strcmp(interface, wl_compositor_interface.name) == 0) {
|
||||
state->wl_compositor = wl_registry_bind(
|
||||
registry, name, &wl_compositor_interface, 4);
|
||||
} else if (strcmp(interface, xdg_wm_base_interface.name) == 0) {
|
||||
state->xdg_wm_base = wl_registry_bind(
|
||||
registry, name, &xdg_wm_base_interface, 1);
|
||||
xdg_wm_base_add_listener(state->xdg_wm_base,
|
||||
&xdg_wm_base_listener, state);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
static void
|
||||
|
|
@ -51,7 +149,7 @@ registry_handle_global_remove(void *data, struct wl_registry *registry,
|
|||
}
|
||||
|
||||
static const struct wl_registry_listener
|
||||
registry_listener = {
|
||||
wl_registry_listener = {
|
||||
.global = registry_handle_global,
|
||||
.global_remove = registry_handle_global_remove,
|
||||
};
|
||||
|
|
@ -59,12 +157,32 @@ registry_listener = {
|
|||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
struct our_state state = { 0 };
|
||||
struct wl_display *display = wl_display_connect(NULL);
|
||||
struct wl_registry *registry = wl_display_get_registry(display);
|
||||
wl_registry_add_listener(registry, ®istry_listener, &state);
|
||||
wl_display_roundtrip(display);
|
||||
return 0;
|
||||
//struct our_state state = { 0 };
|
||||
//struct wl_display *display = wl_display_connect(NULL);
|
||||
//struct wl_registry *registry = wl_display_get_registry(display);
|
||||
//wl_registry_add_listener(registry, ®istry_listener, &state);
|
||||
//wl_display_roundtrip(display);
|
||||
//return 0;
|
||||
|
||||
struct client_state state = { 0 };
|
||||
state.wl_display = wl_display_connect(NULL);
|
||||
state.wl_registry = wl_display_get_registry(state.wl_display);
|
||||
wl_registry_add_listener(state.wl_registry, &wl_registry_listener, &state);
|
||||
wl_display_roundtrip(state.wl_display);
|
||||
|
||||
state.wl_surface = wl_compositor_create_surface(state.wl_compositor);
|
||||
state.xdg_surface = xdg_wm_base_get_xdg_surface(
|
||||
state.xdg_wm_base, state.wl_surface);
|
||||
xdg_surface_add_listener(state.xdg_surface, &xdg_surface_listener, &state);
|
||||
state.xdg_toplevel = xdg_surface_get_toplevel(state.xdg_surface);
|
||||
xdg_toplevel_set_title(state.xdg_toplevel, "Example client");
|
||||
wl_surface_commit(state.wl_surface);
|
||||
|
||||
while (wl_display_dispatch(state.wl_display)) {
|
||||
/* This space deliberately left blank */
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
//struct timespec program_start;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue