working xdg_shell
This commit is contained in:
parent
4b374ea8fc
commit
23f4e6dbac
278
surface.c
278
surface.c
@ -1,3 +1,4 @@
|
|||||||
|
#define _GNU_SOURCE
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
@ -11,97 +12,29 @@
|
|||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
|
|
||||||
struct wl_display *display = NULL;
|
struct client_state {
|
||||||
struct wl_compositor *compositor = NULL;
|
/* Globals */
|
||||||
struct wl_surface *surface;
|
struct wl_display *wl_display;
|
||||||
struct wl_shell *shell;
|
struct wl_registry *wl_registry;
|
||||||
struct wl_shell_surface *shell_surface;
|
struct wl_compositor *wl_compositor;
|
||||||
struct wl_shm *shm;
|
struct wl_shm *wl_shm;
|
||||||
struct wl_buffer *buffer;
|
struct xdg_wm_base *xdg_wm_base;
|
||||||
|
/* Objects */
|
||||||
void *shm_data;
|
struct wl_buffer *wl_buffer;
|
||||||
|
struct wl_surface *wl_surface;
|
||||||
|
struct xdg_surface *xdg_surface;
|
||||||
|
struct xdg_toplevel *xdg_toplevel;
|
||||||
|
};
|
||||||
|
|
||||||
int WIDTH = 480;
|
int WIDTH = 480;
|
||||||
int HEIGHT = 360;
|
int HEIGHT = 360;
|
||||||
|
|
||||||
static void
|
|
||||||
handle_ping(void *data, struct wl_shell_surface *shell_surface,
|
|
||||||
uint32_t serial)
|
|
||||||
{
|
|
||||||
wl_shell_surface_pong(shell_surface, serial);
|
|
||||||
fprintf(stderr, "Pinged and ponged\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
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
|
|
||||||
};
|
|
||||||
|
|
||||||
static int
|
|
||||||
set_cloexec_or_close(int fd)
|
|
||||||
{
|
|
||||||
long flags;
|
|
||||||
|
|
||||||
if (fd == -1)
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
flags = fcntl(fd, F_GETFD);
|
|
||||||
if (flags == -1)
|
|
||||||
goto err;
|
|
||||||
|
|
||||||
if (fcntl(fd, F_SETFD, flags | FD_CLOEXEC) == -1)
|
|
||||||
goto err;
|
|
||||||
|
|
||||||
return fd;
|
|
||||||
|
|
||||||
err:
|
|
||||||
close(fd);
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int
|
|
||||||
create_tmpfile_cloexec(char *tmpname)
|
|
||||||
{
|
|
||||||
int fd;
|
|
||||||
|
|
||||||
#ifdef HAVE_MKOSTEMP
|
|
||||||
fd = mkostemp(tmpname, O_CLOEXEC);
|
|
||||||
if (fd >= 0)
|
|
||||||
unlink(tmpname);
|
|
||||||
#else
|
|
||||||
fd = mkstemp(tmpname);
|
|
||||||
if (fd >= 0) {
|
|
||||||
fd = set_cloexec_or_close(fd);
|
|
||||||
unlink(tmpname);
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
return fd;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Create a new, unique, anonymous file of the given size, and
|
* Create a new, unique, anonymous file of the given size, and
|
||||||
* return the file descriptor for it. The file descriptor is set
|
* return the file descriptor for it. The file descriptor is set
|
||||||
* CLOEXEC. The file is immediately suitable for mmap()'ing
|
* CLOEXEC. The file is immediately suitable for mmap()'ing
|
||||||
* the given size at offset zero.
|
* the given size at offset zero.
|
||||||
*
|
|
||||||
* The file should not have a permanent backing store like a disk,
|
|
||||||
* but may have if XDG_RUNTIME_DIR is not properly implemented in OS.
|
|
||||||
*
|
|
||||||
* The file name is deleted from the file system.
|
|
||||||
*
|
|
||||||
* The file is suitable for buffer sharing between processes by
|
* The file is suitable for buffer sharing between processes by
|
||||||
* transmitting the file descriptor over Unix sockets using the
|
* transmitting the file descriptor over Unix sockets using the
|
||||||
* SCM_RIGHTS methods.
|
* SCM_RIGHTS methods.
|
||||||
@ -109,51 +42,22 @@ create_tmpfile_cloexec(char *tmpname)
|
|||||||
int
|
int
|
||||||
os_create_anonymous_file(off_t size)
|
os_create_anonymous_file(off_t size)
|
||||||
{
|
{
|
||||||
static const char template[] = "/weston-shared-XXXXXX";
|
int fd;
|
||||||
const char *path;
|
fd = memfd_create("wayland-shm", MFD_CLOEXEC);
|
||||||
char *name;
|
|
||||||
int fd;
|
|
||||||
|
|
||||||
path = getenv("XDG_RUNTIME_DIR");
|
if (fd < 0)
|
||||||
if (!path) {
|
return -1;
|
||||||
errno = ENOENT;
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
name = malloc(strlen(path) + sizeof(template));
|
if (ftruncate(fd, size) < 0) {
|
||||||
if (!name)
|
close(fd);
|
||||||
return -1;
|
return -1;
|
||||||
strcpy(name, path);
|
|
||||||
strcat(name, template);
|
|
||||||
|
|
||||||
fd = create_tmpfile_cloexec(name);
|
|
||||||
|
|
||||||
free(name);
|
|
||||||
|
|
||||||
if (fd < 0)
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
if (ftruncate(fd, size) < 0) {
|
|
||||||
close(fd);
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
return fd;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void
|
|
||||||
paint_pixels() {
|
|
||||||
int n;
|
|
||||||
uint32_t *pixel = shm_data;
|
|
||||||
|
|
||||||
fprintf(stderr, "Painting pixels\n");
|
|
||||||
for (n =0; n < WIDTH*HEIGHT; n++) {
|
|
||||||
*pixel++ = 0xffff;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return fd;
|
||||||
}
|
}
|
||||||
|
|
||||||
static struct wl_buffer *
|
static struct wl_buffer *
|
||||||
create_buffer() {
|
create_buffer(struct client_state *state) {
|
||||||
struct wl_shm_pool *pool;
|
struct wl_shm_pool *pool;
|
||||||
int stride = WIDTH * 4; // 4 bytes per pixel
|
int stride = WIDTH * 4; // 4 bytes per pixel
|
||||||
int size = stride * HEIGHT;
|
int size = stride * HEIGHT;
|
||||||
@ -167,33 +71,58 @@ create_buffer() {
|
|||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
shm_data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
|
void *shm_data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
|
||||||
if (shm_data == MAP_FAILED) {
|
if (shm_data == MAP_FAILED) {
|
||||||
fprintf(stderr, "mmap failed: %m\n");
|
fprintf(stderr, "mmap failed: %m\n");
|
||||||
close(fd);
|
close(fd);
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
pool = wl_shm_create_pool(shm, fd, size);
|
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);
|
||||||
buff = wl_shm_pool_create_buffer(pool, 0,
|
buff = wl_shm_pool_create_buffer(pool, 0,
|
||||||
WIDTH, HEIGHT,
|
WIDTH, HEIGHT,
|
||||||
stride,
|
stride,
|
||||||
WL_SHM_FORMAT_XRGB8888);
|
WL_SHM_FORMAT_XRGB8888);
|
||||||
//wl_buffer_add_listener(buffer, &buffer_listener, buffer);
|
//wl_buffer_add_listener(buffer, &buffer_listener, buffer);
|
||||||
wl_shm_pool_destroy(pool);
|
wl_shm_pool_destroy(pool);
|
||||||
return buff;
|
return buff;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
create_window() {
|
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");
|
||||||
|
|
||||||
buffer = create_buffer();
|
state->wl_buffer = create_buffer(state);
|
||||||
|
|
||||||
wl_surface_attach(surface, buffer, 0, 0);
|
wl_surface_attach(state->wl_surface, state->wl_buffer, 0, 0);
|
||||||
//wl_surface_damage(surface, 0, 0, WIDTH, HEIGHT);
|
//wl_surface_damage(surface, 0, 0, WIDTH, HEIGHT);
|
||||||
wl_surface_commit(surface);
|
wl_surface_commit(state->wl_surface);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static const struct xdg_surface_listener xdg_surface_listener = {
|
||||||
|
.configure = handle_configure,
|
||||||
|
};
|
||||||
|
|
||||||
static void
|
static void
|
||||||
shm_format(void *data, struct wl_shm *wl_shm, uint32_t format)
|
shm_format(void *data, struct wl_shm *wl_shm, uint32_t format)
|
||||||
{
|
{
|
||||||
@ -207,23 +136,34 @@ struct wl_shm_listener shm_listener = {
|
|||||||
shm_format
|
shm_format
|
||||||
};
|
};
|
||||||
|
|
||||||
|
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
|
static void
|
||||||
global_registry_handler(void *data, struct wl_registry *registry, uint32_t id,
|
global_registry_handler(void *data, struct wl_registry *registry, uint32_t id,
|
||||||
const char *interface, uint32_t version)
|
const char *interface, uint32_t version)
|
||||||
{
|
{
|
||||||
if (strcmp(interface, "wl_compositor") == 0) {
|
struct client_state *state = data;
|
||||||
compositor = wl_registry_bind(registry,
|
|
||||||
id,
|
|
||||||
&wl_compositor_interface,
|
|
||||||
1);
|
|
||||||
} else if (strcmp(interface, "wl_shell") == 0) {
|
|
||||||
shell = wl_registry_bind(registry, id,
|
|
||||||
&wl_shell_interface, 1);
|
|
||||||
} else if (strcmp(interface, "wl_shm") == 0) {
|
|
||||||
shm = wl_registry_bind(registry, id,
|
|
||||||
&wl_shm_interface, 1);
|
|
||||||
wl_shm_add_listener(shm, &shm_listener, NULL);
|
|
||||||
|
|
||||||
|
if (strcmp(interface, wl_compositor_interface.name) == 0) {
|
||||||
|
state->wl_compositor = wl_registry_bind(registry, id,
|
||||||
|
&wl_compositor_interface, 4);
|
||||||
|
|
||||||
|
} 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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -241,55 +181,67 @@ static const struct wl_registry_listener registry_listener = {
|
|||||||
|
|
||||||
int main(int argc, char **argv) {
|
int main(int argc, char **argv) {
|
||||||
|
|
||||||
display = wl_display_connect(NULL);
|
struct client_state state = { 0 };
|
||||||
if (display == NULL) {
|
|
||||||
|
state.wl_display = wl_display_connect(NULL);
|
||||||
|
if (state.wl_display == NULL) {
|
||||||
fprintf(stderr, "Can't connect to display\n");
|
fprintf(stderr, "Can't connect to display\n");
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
printf("connected to display\n");
|
printf("connected to display\n");
|
||||||
|
|
||||||
struct wl_registry *registry = wl_display_get_registry(display);
|
state.wl_registry = wl_display_get_registry(state.wl_display);
|
||||||
wl_registry_add_listener(registry, ®istry_listener, NULL);
|
wl_registry_add_listener(state.wl_registry, ®istry_listener, &state);
|
||||||
|
|
||||||
wl_display_dispatch(display);
|
wl_display_dispatch(state.wl_display);
|
||||||
wl_display_roundtrip(display);
|
wl_display_roundtrip(state.wl_display);
|
||||||
|
|
||||||
if (compositor == NULL) {
|
/* By now we should have wl_compositor, wl_shm and xdg_wm_base from global_registry */
|
||||||
|
|
||||||
|
if (state.wl_compositor == NULL) {
|
||||||
fprintf(stderr, "Can't find compositor\n");
|
fprintf(stderr, "Can't find compositor\n");
|
||||||
exit(1);
|
exit(1);
|
||||||
} else {
|
} else {
|
||||||
fprintf(stderr, "Found compositor\n");
|
fprintf(stderr, "Found compositor\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
surface = wl_compositor_create_surface(compositor);
|
state.wl_surface = wl_compositor_create_surface(state.wl_compositor);
|
||||||
if (surface == NULL) {
|
if (state.wl_surface == NULL) {
|
||||||
fprintf(stderr, "Can't create surface\n");
|
fprintf(stderr, "Can't create surface\n");
|
||||||
exit(1);
|
exit(1);
|
||||||
} else {
|
} else {
|
||||||
fprintf(stderr, "Created surface\n");
|
fprintf(stderr, "Created surface\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
shell_surface = wl_shell_get_shell_surface(shell, surface);
|
if (state.xdg_wm_base == NULL) {
|
||||||
if (shell_surface == NULL) {
|
fprintf(stderr, "no wm_base\n");
|
||||||
fprintf(stderr, "Can't create shell surface\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");
|
||||||
exit(1);
|
exit(1);
|
||||||
} else {
|
} else {
|
||||||
fprintf(stderr, "Created shell surface\n");
|
fprintf(stderr, "Got xdg_surface\n");
|
||||||
}
|
}
|
||||||
wl_shell_surface_set_toplevel(shell_surface);
|
xdg_surface_add_listener(state.xdg_surface, &xdg_surface_listener, &state);
|
||||||
|
|
||||||
wl_shell_surface_add_listener(shell_surface,
|
state.xdg_toplevel = xdg_surface_get_toplevel(state.xdg_surface);
|
||||||
&shell_surface_listener, NULL);
|
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);
|
||||||
|
|
||||||
|
|
||||||
create_window();
|
while (wl_display_dispatch(state.wl_display)) {
|
||||||
paint_pixels();
|
|
||||||
|
|
||||||
while (wl_display_dispatch(display) != -1) {
|
|
||||||
;
|
;
|
||||||
}
|
}
|
||||||
|
|
||||||
wl_display_disconnect(display);
|
wl_display_disconnect(state.wl_display);
|
||||||
printf("disconnected from display\n");
|
printf("disconnected from display\n");
|
||||||
|
|
||||||
exit(0);
|
exit(0);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user