Refactor the crap out of wayland clients
And create a background surface on every output when invoking swaybg.
This commit is contained in:
parent
4a1584be53
commit
b4e5e1381f
|
@ -1,8 +1,8 @@
|
|||
#ifndef _BUFFER_H
|
||||
#define _BUFFER_H
|
||||
|
||||
#include "client/client.h"
|
||||
#include "client/window.h"
|
||||
|
||||
struct buffer *get_next_buffer(struct client_state *state);
|
||||
struct buffer *get_next_buffer(struct window *state);
|
||||
|
||||
#endif
|
||||
|
|
28
include/client/registry.h
Normal file
28
include/client/registry.h
Normal file
|
@ -0,0 +1,28 @@
|
|||
#ifndef _SWAY_CLIENT_REGISTRY_H
|
||||
#define _SWAY_CLIENT_REGISTRY_H
|
||||
|
||||
#include <wayland-client.h>
|
||||
#include "wayland-desktop-shell-client-protocol.h"
|
||||
#include "list.h"
|
||||
|
||||
struct output_state {
|
||||
struct wl_output *output;
|
||||
uint32_t flags;
|
||||
uint32_t width, height;
|
||||
};
|
||||
|
||||
struct registry {
|
||||
struct wl_compositor *compositor;
|
||||
struct wl_display *display;
|
||||
struct wl_pointer *pointer;
|
||||
struct wl_seat *seat;
|
||||
struct wl_shell *shell;
|
||||
struct wl_shm *shm;
|
||||
struct desktop_shell *desktop_shell;
|
||||
list_t *outputs;
|
||||
};
|
||||
|
||||
struct registry *registry_poll(void);
|
||||
void registry_teardown(struct registry *registry);
|
||||
|
||||
#endif
|
|
@ -7,12 +7,7 @@
|
|||
#include <pango/pangocairo.h>
|
||||
#include <stdbool.h>
|
||||
#include "list.h"
|
||||
|
||||
struct output_state {
|
||||
struct wl_output *output;
|
||||
uint32_t flags;
|
||||
uint32_t width, height;
|
||||
};
|
||||
#include "client/registry.h"
|
||||
|
||||
struct buffer {
|
||||
struct wl_buffer *buffer;
|
||||
|
@ -30,28 +25,21 @@ struct cursor {
|
|||
struct wl_poitner *pointer;
|
||||
};
|
||||
|
||||
struct client_state {
|
||||
struct wl_compositor *compositor;
|
||||
struct wl_display *display;
|
||||
struct wl_pointer *pointer;
|
||||
struct wl_seat *seat;
|
||||
struct wl_shell *shell;
|
||||
struct wl_shm *shm;
|
||||
struct window {
|
||||
struct registry *registry;
|
||||
struct buffer buffers[2];
|
||||
struct buffer *buffer;
|
||||
struct wl_surface *surface;
|
||||
struct wl_shell_surface *shell_surface;
|
||||
struct wl_callback *frame_cb;
|
||||
struct desktop_shell *desktop_shell;
|
||||
struct cursor cursor;
|
||||
uint32_t width, height;
|
||||
cairo_t *cairo;
|
||||
list_t *outputs;
|
||||
};
|
||||
|
||||
struct client_state *client_setup(uint32_t width, uint32_t height, bool shell_surface);
|
||||
void client_teardown(struct client_state *state);
|
||||
int client_prerender(struct client_state *state);
|
||||
int client_render(struct client_state *state);
|
||||
struct window *window_setup(struct registry *registry, uint32_t width, uint32_t height, bool shell_surface);
|
||||
void window_teardown(struct window *state);
|
||||
int window_prerender(struct window *state);
|
||||
int window_render(struct window *state);
|
||||
|
||||
#endif
|
|
@ -3,43 +3,69 @@
|
|||
#include <stdlib.h>
|
||||
#include <wayland-client.h>
|
||||
#include <time.h>
|
||||
#include "client/client.h"
|
||||
#include "client/window.h"
|
||||
#include "client/registry.h"
|
||||
#include "log.h"
|
||||
#include "list.h"
|
||||
|
||||
struct client_state *state;
|
||||
list_t *surfaces;
|
||||
|
||||
struct registry *registry;
|
||||
|
||||
void sway_terminate(void) {
|
||||
client_teardown(state);
|
||||
int i;
|
||||
for (i = 0; i < surfaces->length; ++i) {
|
||||
struct window *window = surfaces->items[i];
|
||||
window_teardown(window);
|
||||
}
|
||||
list_free(surfaces);
|
||||
registry_teardown(registry);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
init_log(L_INFO);
|
||||
if (!(state = client_setup(100, 100, false))) {
|
||||
return -1;
|
||||
}
|
||||
if (!state->desktop_shell) {
|
||||
surfaces = create_list();
|
||||
registry = registry_poll();
|
||||
|
||||
if (!registry->desktop_shell) {
|
||||
sway_abort("swaybg requires the compositor to support the desktop-shell extension.");
|
||||
}
|
||||
struct output_state *output = state->outputs->items[0];
|
||||
state->width = output->width;
|
||||
state->height = output->height;
|
||||
desktop_shell_set_background(state->desktop_shell, output->output, state->surface);
|
||||
|
||||
int i;
|
||||
for (i = 0; i < registry->outputs->length; ++i) {
|
||||
struct output_state *output = registry->outputs->items[i];
|
||||
struct window *window = window_setup(registry, 100, 100, false);
|
||||
if (!window) {
|
||||
sway_abort("Failed to create surfaces.");
|
||||
}
|
||||
window->width = output->width;
|
||||
window->height = output->height;
|
||||
desktop_shell_set_background(registry->desktop_shell, output->output, window->surface);
|
||||
list_add(surfaces, window);
|
||||
}
|
||||
|
||||
uint8_t r = 0, g = 0, b = 0;
|
||||
|
||||
do {
|
||||
if (client_prerender(state) && state->cairo) {
|
||||
cairo_set_source_rgb(state->cairo, r / 256.0, g / 256.0, b / 256.0);
|
||||
cairo_rectangle(state->cairo, 0, 0, state->width, state->height);
|
||||
cairo_fill(state->cairo);
|
||||
|
||||
client_render(state);
|
||||
for (i = 0; i < surfaces->length; ++i) {
|
||||
struct window *window = surfaces->items[i];
|
||||
if (window_prerender(window) && window->cairo) {
|
||||
cairo_set_source_rgb(window->cairo, r / 256.0, g / 256.0, b / 256.0);
|
||||
cairo_rectangle(window->cairo, 0, 0, window->width, window->height);
|
||||
cairo_fill(window->cairo);
|
||||
|
||||
window_render(window);
|
||||
}
|
||||
r++; g += 2; b += 4;
|
||||
}
|
||||
} while (wl_display_dispatch(state->display) != -1);
|
||||
} while (wl_display_dispatch(registry->display) != -1);
|
||||
|
||||
client_teardown(state);
|
||||
for (i = 0; i < surfaces->length; ++i) {
|
||||
struct window *window = surfaces->items[i];
|
||||
window_teardown(window);
|
||||
}
|
||||
list_free(surfaces);
|
||||
registry_teardown(registry);
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -49,7 +49,7 @@ static const struct wl_buffer_listener buffer_listener = {
|
|||
.release = buffer_release
|
||||
};
|
||||
|
||||
static struct buffer *create_buffer(struct client_state *state, struct buffer *buf,
|
||||
static struct buffer *create_buffer(struct window *window, struct buffer *buf,
|
||||
int32_t width, int32_t height, uint32_t format) {
|
||||
|
||||
uint32_t stride = width * 4;
|
||||
|
@ -62,7 +62,7 @@ static struct buffer *create_buffer(struct client_state *state, struct buffer *b
|
|||
return NULL; // never reached
|
||||
}
|
||||
void *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
|
||||
struct wl_shm_pool *pool = wl_shm_create_pool(state->shm, fd, size);
|
||||
struct wl_shm_pool *pool = wl_shm_create_pool(window->registry->shm, fd, size);
|
||||
buf->buffer = wl_shm_pool_create_buffer(pool, 0, width, height, stride, format);
|
||||
wl_shm_pool_destroy(pool);
|
||||
close(fd);
|
||||
|
@ -76,7 +76,7 @@ static struct buffer *create_buffer(struct client_state *state, struct buffer *b
|
|||
buf->cairo = cairo_create(buf->surface);
|
||||
buf->pango = pango_cairo_create_context(buf->cairo);
|
||||
|
||||
wl_buffer_add_listener(buf->buffer, &buffer_listener, state);
|
||||
wl_buffer_add_listener(buf->buffer, &buffer_listener, buf);
|
||||
return buf;
|
||||
}
|
||||
|
||||
|
@ -93,32 +93,32 @@ static void destroy_buffer(struct buffer *buffer) {
|
|||
memset(buffer, 0, sizeof(struct buffer));
|
||||
}
|
||||
|
||||
struct buffer *get_next_buffer(struct client_state *state) {
|
||||
struct buffer *get_next_buffer(struct window *window) {
|
||||
struct buffer *buffer = NULL;
|
||||
|
||||
int i;
|
||||
for (i = 0; i < 2; ++i) {
|
||||
if (state->buffers[i].busy) {
|
||||
if (window->buffers[i].busy) {
|
||||
continue;
|
||||
}
|
||||
buffer = &state->buffers[i];
|
||||
buffer = &window->buffers[i];
|
||||
}
|
||||
|
||||
if (!buffer) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (buffer->width != state->width || buffer->height != state->height) {
|
||||
if (buffer->width != window->width || buffer->height != window->height) {
|
||||
destroy_buffer(buffer);
|
||||
}
|
||||
|
||||
if (!buffer->buffer) {
|
||||
if (!create_buffer(state, buffer, state->width, state->height, WL_SHM_FORMAT_ARGB8888)) {
|
||||
if (!create_buffer(window, buffer, window->width, window->height, WL_SHM_FORMAT_ARGB8888)) {
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
state->cairo = buffer->cairo;
|
||||
state->buffer = buffer;
|
||||
window->cairo = buffer->cairo;
|
||||
window->buffer = buffer;
|
||||
return buffer;
|
||||
}
|
||||
|
|
218
wayland/client.c
218
wayland/client.c
|
@ -1,218 +0,0 @@
|
|||
#include <wayland-client.h>
|
||||
#include <wayland-cursor.h>
|
||||
#include "wayland-xdg-shell-client-protocol.h"
|
||||
#include "wayland-desktop-shell-client-protocol.h"
|
||||
#include <cairo/cairo.h>
|
||||
#include <pango/pangocairo.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include <errno.h>
|
||||
#include <sys/mman.h>
|
||||
#include "client/client.h"
|
||||
#include "client/buffer.h"
|
||||
#include "list.h"
|
||||
#include "log.h"
|
||||
|
||||
static void display_handle_mode(void *data, struct wl_output *wl_output,
|
||||
uint32_t flags, int32_t width, int32_t height, int32_t refresh) {
|
||||
struct output_state *state = data;
|
||||
if (flags & WL_OUTPUT_MODE_CURRENT) {
|
||||
state->flags = flags;
|
||||
state->width = width;
|
||||
state->height = height;
|
||||
}
|
||||
}
|
||||
|
||||
static void display_handle_geometry(void *data, struct wl_output *wl_output,
|
||||
int32_t x, int32_t y, int32_t physical_width, int32_t physical_height,
|
||||
int32_t subpixel, const char *make, const char *model, int32_t transform) {
|
||||
// this space intentionally left blank
|
||||
}
|
||||
|
||||
static void display_handle_done(void *data, struct wl_output *wl_output) {
|
||||
// this space intentionally left blank
|
||||
}
|
||||
|
||||
static void display_handle_scale(void *data, struct wl_output *wl_output, int32_t factor) {
|
||||
// this space intentionally left blank
|
||||
}
|
||||
|
||||
static const struct wl_output_listener output_listener = {
|
||||
.mode = display_handle_mode,
|
||||
.geometry = display_handle_geometry,
|
||||
.done = display_handle_done,
|
||||
.scale = display_handle_scale
|
||||
};
|
||||
|
||||
static void pointer_handle_enter(void *data, struct wl_pointer *pointer,
|
||||
uint32_t serial, struct wl_surface *surface, wl_fixed_t sx_w, wl_fixed_t sy_w) {
|
||||
struct client_state *state = data;
|
||||
struct wl_cursor_image *image = state->cursor.cursor->images[0];
|
||||
wl_pointer_set_cursor(pointer, serial, state->cursor.surface, image->hotspot_x, image->hotspot_y);
|
||||
}
|
||||
|
||||
static void pointer_handle_leave(void *data, struct wl_pointer *pointer,
|
||||
uint32_t serial, struct wl_surface *surface) {
|
||||
}
|
||||
|
||||
static void pointer_handle_motion(void *data, struct wl_pointer *pointer,
|
||||
uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w) {
|
||||
}
|
||||
|
||||
static void pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
|
||||
uint32_t time, uint32_t button, uint32_t state_w) {
|
||||
}
|
||||
|
||||
static void pointer_handle_axis(void *data, struct wl_pointer *pointer,
|
||||
uint32_t time, uint32_t axis, wl_fixed_t value) {
|
||||
}
|
||||
|
||||
static const struct wl_pointer_listener pointer_listener = {
|
||||
.enter = pointer_handle_enter,
|
||||
.leave = pointer_handle_leave,
|
||||
.motion = pointer_handle_motion,
|
||||
.button = pointer_handle_button,
|
||||
.axis = pointer_handle_axis
|
||||
};
|
||||
|
||||
static void registry_global(void *data, struct wl_registry *registry,
|
||||
uint32_t name, const char *interface, uint32_t version) {
|
||||
struct client_state *state = data;
|
||||
|
||||
if (strcmp(interface, wl_compositor_interface.name) == 0) {
|
||||
state->compositor = wl_registry_bind(registry, name, &wl_compositor_interface, version);
|
||||
} else if (strcmp(interface, wl_shm_interface.name) == 0) {
|
||||
state->shm = wl_registry_bind(registry, name, &wl_shm_interface, version);
|
||||
} else if (strcmp(interface, wl_shell_interface.name) == 0) {
|
||||
state->shell = wl_registry_bind(registry, name, &wl_shell_interface, version);
|
||||
} else if (strcmp(interface, wl_seat_interface.name) == 0) {
|
||||
state->seat = wl_registry_bind(registry, name, &wl_seat_interface, version);
|
||||
state->pointer = wl_seat_get_pointer(state->seat);
|
||||
wl_pointer_add_listener(state->pointer, &pointer_listener, state);
|
||||
} else if (strcmp(interface, wl_output_interface.name) == 0) {
|
||||
struct wl_output *output = wl_registry_bind(registry, name, &wl_output_interface, version);
|
||||
struct output_state *ostate = malloc(sizeof(struct output_state));
|
||||
ostate->output = output;
|
||||
wl_output_add_listener(output, &output_listener, ostate);
|
||||
list_add(state->outputs, ostate);
|
||||
} else if (strcmp(interface, desktop_shell_interface.name) == 0) {
|
||||
state->desktop_shell = wl_registry_bind(registry, name, &desktop_shell_interface, version);
|
||||
}
|
||||
}
|
||||
|
||||
static void registry_global_remove(void *data, struct wl_registry *registry, uint32_t name) {
|
||||
// this space intentionally left blank
|
||||
}
|
||||
|
||||
static const struct wl_registry_listener registry_listener = {
|
||||
.global = registry_global,
|
||||
.global_remove = registry_global_remove
|
||||
};
|
||||
|
||||
void shell_surface_configure(void *data, struct wl_shell_surface *wl_shell_surface,
|
||||
uint32_t edges, int32_t width, int32_t height) {
|
||||
struct client_state *state = data;
|
||||
state->width = width;
|
||||
state->height = height;
|
||||
}
|
||||
|
||||
static const struct wl_shell_surface_listener surface_listener = {
|
||||
.configure = shell_surface_configure
|
||||
};
|
||||
|
||||
struct client_state *client_setup(uint32_t width, uint32_t height, bool shell_surface) {
|
||||
struct client_state *state = malloc(sizeof(struct client_state));
|
||||
memset(state, 0, sizeof(struct client_state));
|
||||
state->outputs = create_list();
|
||||
state->width = width;
|
||||
state->height = height;
|
||||
|
||||
state->display = wl_display_connect(NULL);
|
||||
if (!state->display) {
|
||||
sway_log(L_ERROR, "Error opening display");
|
||||
client_teardown(state);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct wl_registry *registry = wl_display_get_registry(state->display);
|
||||
wl_registry_add_listener(registry, ®istry_listener, state);
|
||||
wl_display_dispatch(state->display);
|
||||
wl_display_roundtrip(state->display);
|
||||
wl_registry_destroy(registry);
|
||||
|
||||
state->surface = wl_compositor_create_surface(state->compositor);
|
||||
if (shell_surface) {
|
||||
state->shell_surface = wl_shell_get_shell_surface(state->shell, state->surface);
|
||||
wl_shell_surface_add_listener(state->shell_surface, &surface_listener, state);
|
||||
wl_shell_surface_set_toplevel(state->shell_surface);
|
||||
}
|
||||
|
||||
state->cursor.cursor_theme = wl_cursor_theme_load("default", 32, state->shm); // TODO: let you customize this
|
||||
state->cursor.cursor = wl_cursor_theme_get_cursor(state->cursor.cursor_theme, "left_ptr");
|
||||
state->cursor.surface = wl_compositor_create_surface(state->compositor);
|
||||
|
||||
struct wl_cursor_image *image = state->cursor.cursor->images[0];
|
||||
struct wl_buffer *cursor_buf = wl_cursor_image_get_buffer(image);
|
||||
wl_surface_attach(state->cursor.surface, cursor_buf, 0, 0);
|
||||
wl_surface_damage(state->cursor.surface, 0, 0, image->width, image->height);
|
||||
wl_surface_commit(state->cursor.surface);
|
||||
|
||||
return state;
|
||||
}
|
||||
|
||||
static void frame_callback(void *data, struct wl_callback *callback, uint32_t time) {
|
||||
struct client_state *state = data;
|
||||
wl_callback_destroy(callback);
|
||||
state->frame_cb = NULL;
|
||||
}
|
||||
|
||||
static const struct wl_callback_listener listener = {
|
||||
frame_callback
|
||||
};
|
||||
|
||||
int client_prerender(struct client_state *state) {
|
||||
if (state->frame_cb) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
get_next_buffer(state);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int client_render(struct client_state *state) {
|
||||
state->frame_cb = wl_surface_frame(state->surface);
|
||||
wl_callback_add_listener(state->frame_cb, &listener, state);
|
||||
|
||||
wl_surface_damage(state->surface, 0, 0, state->buffer->width, state->buffer->height);
|
||||
wl_surface_attach(state->surface, state->buffer->buffer, 0, 0);
|
||||
wl_surface_commit(state->surface);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
void client_teardown(struct client_state *state) {
|
||||
if (state->pointer) {
|
||||
wl_pointer_destroy(state->pointer);
|
||||
}
|
||||
if (state->seat) {
|
||||
wl_seat_destroy(state->seat);
|
||||
}
|
||||
if (state->shell) {
|
||||
wl_shell_destroy(state->shell);
|
||||
}
|
||||
if (state->shm) {
|
||||
wl_shm_destroy(state->shm);
|
||||
}
|
||||
if (state->compositor) {
|
||||
wl_compositor_destroy(state->compositor);
|
||||
}
|
||||
if (state->display) {
|
||||
wl_display_disconnect(state->display);
|
||||
}
|
||||
if (state->outputs) {
|
||||
// TODO: Free the outputs themselves
|
||||
list_free(state->outputs);
|
||||
}
|
||||
}
|
116
wayland/registry.c
Normal file
116
wayland/registry.c
Normal file
|
@ -0,0 +1,116 @@
|
|||
#include <wayland-client.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "wayland-desktop-shell-client-protocol.h"
|
||||
#include "client/registry.h"
|
||||
#include "log.h"
|
||||
|
||||
static void display_handle_mode(void *data, struct wl_output *wl_output,
|
||||
uint32_t flags, int32_t width, int32_t height, int32_t refresh) {
|
||||
struct output_state *state = data;
|
||||
if (flags & WL_OUTPUT_MODE_CURRENT) {
|
||||
state->flags = flags;
|
||||
state->width = width;
|
||||
state->height = height;
|
||||
}
|
||||
}
|
||||
|
||||
static void display_handle_geometry(void *data, struct wl_output *wl_output,
|
||||
int32_t x, int32_t y, int32_t physical_width, int32_t physical_height,
|
||||
int32_t subpixel, const char *make, const char *model, int32_t transform) {
|
||||
// this space intentionally left blank
|
||||
}
|
||||
|
||||
static void display_handle_done(void *data, struct wl_output *wl_output) {
|
||||
// this space intentionally left blank
|
||||
}
|
||||
|
||||
static void display_handle_scale(void *data, struct wl_output *wl_output, int32_t factor) {
|
||||
// this space intentionally left blank
|
||||
}
|
||||
|
||||
static const struct wl_output_listener output_listener = {
|
||||
.mode = display_handle_mode,
|
||||
.geometry = display_handle_geometry,
|
||||
.done = display_handle_done,
|
||||
.scale = display_handle_scale
|
||||
};
|
||||
|
||||
static void registry_global(void *data, struct wl_registry *registry,
|
||||
uint32_t name, const char *interface, uint32_t version) {
|
||||
struct registry *reg = data;
|
||||
|
||||
if (strcmp(interface, wl_compositor_interface.name) == 0) {
|
||||
reg->compositor = wl_registry_bind(registry, name, &wl_compositor_interface, version);
|
||||
} else if (strcmp(interface, wl_shm_interface.name) == 0) {
|
||||
reg->shm = wl_registry_bind(registry, name, &wl_shm_interface, version);
|
||||
} else if (strcmp(interface, wl_shell_interface.name) == 0) {
|
||||
reg->shell = wl_registry_bind(registry, name, &wl_shell_interface, version);
|
||||
} else if (strcmp(interface, wl_seat_interface.name) == 0) {
|
||||
reg->seat = wl_registry_bind(registry, name, &wl_seat_interface, version);
|
||||
reg->pointer = wl_seat_get_pointer(reg->seat);
|
||||
} else if (strcmp(interface, wl_output_interface.name) == 0) {
|
||||
struct wl_output *output = wl_registry_bind(registry, name, &wl_output_interface, version);
|
||||
struct output_state *ostate = malloc(sizeof(struct output_state));
|
||||
ostate->output = output;
|
||||
wl_output_add_listener(output, &output_listener, ostate);
|
||||
list_add(reg->outputs, ostate);
|
||||
} else if (strcmp(interface, desktop_shell_interface.name) == 0) {
|
||||
reg->desktop_shell = wl_registry_bind(registry, name, &desktop_shell_interface, version);
|
||||
}
|
||||
}
|
||||
|
||||
static void registry_global_remove(void *data, struct wl_registry *registry, uint32_t name) {
|
||||
// this space intentionally left blank
|
||||
}
|
||||
|
||||
static const struct wl_registry_listener registry_listener = {
|
||||
.global = registry_global,
|
||||
.global_remove = registry_global_remove
|
||||
};
|
||||
|
||||
struct registry *registry_poll(void) {
|
||||
struct registry *registry = malloc(sizeof(struct registry));
|
||||
memset(registry, 0, sizeof(struct registry));
|
||||
registry->outputs = create_list();
|
||||
|
||||
registry->display = wl_display_connect(NULL);
|
||||
if (!registry->display) {
|
||||
sway_log(L_ERROR, "Error opening display");
|
||||
registry_teardown(registry);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct wl_registry *reg = wl_display_get_registry(registry->display);
|
||||
wl_registry_add_listener(reg, ®istry_listener, registry);
|
||||
wl_display_dispatch(registry->display);
|
||||
wl_display_roundtrip(registry->display);
|
||||
wl_registry_destroy(reg);
|
||||
|
||||
return registry;
|
||||
}
|
||||
|
||||
void registry_teardown(struct registry *registry) {
|
||||
if (registry->pointer) {
|
||||
wl_pointer_destroy(registry->pointer);
|
||||
}
|
||||
if (registry->seat) {
|
||||
wl_seat_destroy(registry->seat);
|
||||
}
|
||||
if (registry->shell) {
|
||||
wl_shell_destroy(registry->shell);
|
||||
}
|
||||
if (registry->shm) {
|
||||
wl_shm_destroy(registry->shm);
|
||||
}
|
||||
if (registry->compositor) {
|
||||
wl_compositor_destroy(registry->compositor);
|
||||
}
|
||||
if (registry->display) {
|
||||
wl_display_disconnect(registry->display);
|
||||
}
|
||||
if (registry->outputs) {
|
||||
// TODO: Free the outputs themselves
|
||||
list_free(registry->outputs);
|
||||
}
|
||||
}
|
123
wayland/window.c
Normal file
123
wayland/window.c
Normal file
|
@ -0,0 +1,123 @@
|
|||
#include <wayland-client.h>
|
||||
#include <wayland-cursor.h>
|
||||
#include "wayland-xdg-shell-client-protocol.h"
|
||||
#include "wayland-desktop-shell-client-protocol.h"
|
||||
#include <cairo/cairo.h>
|
||||
#include <pango/pangocairo.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include <errno.h>
|
||||
#include <sys/mman.h>
|
||||
#include "client/window.h"
|
||||
#include "client/buffer.h"
|
||||
#include "list.h"
|
||||
#include "log.h"
|
||||
|
||||
static void pointer_handle_enter(void *data, struct wl_pointer *pointer,
|
||||
uint32_t serial, struct wl_surface *surface, wl_fixed_t sx_w, wl_fixed_t sy_w) {
|
||||
sway_log(L_INFO, "Set cursor");
|
||||
struct window *window = data;
|
||||
struct wl_cursor_image *image = window->cursor.cursor->images[0];
|
||||
wl_pointer_set_cursor(pointer, serial, window->cursor.surface, image->hotspot_x, image->hotspot_y);
|
||||
}
|
||||
|
||||
static void pointer_handle_leave(void *data, struct wl_pointer *pointer,
|
||||
uint32_t serial, struct wl_surface *surface) {
|
||||
}
|
||||
|
||||
static void pointer_handle_motion(void *data, struct wl_pointer *pointer,
|
||||
uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w) {
|
||||
}
|
||||
|
||||
static void pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
|
||||
uint32_t time, uint32_t button, uint32_t state_w) {
|
||||
}
|
||||
|
||||
static void pointer_handle_axis(void *data, struct wl_pointer *pointer,
|
||||
uint32_t time, uint32_t axis, wl_fixed_t value) {
|
||||
}
|
||||
|
||||
static const struct wl_pointer_listener pointer_listener = {
|
||||
.enter = pointer_handle_enter,
|
||||
.leave = pointer_handle_leave,
|
||||
.motion = pointer_handle_motion,
|
||||
.button = pointer_handle_button,
|
||||
.axis = pointer_handle_axis
|
||||
};
|
||||
|
||||
void shell_surface_configure(void *data, struct wl_shell_surface *wl_shell_surface,
|
||||
uint32_t edges, int32_t width, int32_t height) {
|
||||
struct window *window = data;
|
||||
window->width = width;
|
||||
window->height = height;
|
||||
}
|
||||
|
||||
static const struct wl_shell_surface_listener surface_listener = {
|
||||
.configure = shell_surface_configure
|
||||
};
|
||||
|
||||
struct window *window_setup(struct registry *registry, uint32_t width, uint32_t height, bool shell_surface) {
|
||||
struct window *window = malloc(sizeof(struct window));
|
||||
memset(window, 0, sizeof(struct window));
|
||||
window->width = width;
|
||||
window->height = height;
|
||||
window->registry = registry;
|
||||
|
||||
window->surface = wl_compositor_create_surface(registry->compositor);
|
||||
if (shell_surface) {
|
||||
window->shell_surface = wl_shell_get_shell_surface(registry->shell, window->surface);
|
||||
wl_shell_surface_add_listener(window->shell_surface, &surface_listener, window);
|
||||
wl_shell_surface_set_toplevel(window->shell_surface);
|
||||
}
|
||||
if (registry->pointer) {
|
||||
sway_log(L_INFO, "Register pointer");
|
||||
wl_pointer_add_listener(registry->pointer, &pointer_listener, window);
|
||||
}
|
||||
|
||||
window->cursor.cursor_theme = wl_cursor_theme_load("default", 32, registry->shm); // TODO: let you customize this
|
||||
window->cursor.cursor = wl_cursor_theme_get_cursor(window->cursor.cursor_theme, "left_ptr");
|
||||
window->cursor.surface = wl_compositor_create_surface(registry->compositor);
|
||||
|
||||
struct wl_cursor_image *image = window->cursor.cursor->images[0];
|
||||
struct wl_buffer *cursor_buf = wl_cursor_image_get_buffer(image);
|
||||
wl_surface_attach(window->cursor.surface, cursor_buf, 0, 0);
|
||||
wl_surface_damage(window->cursor.surface, 0, 0, image->width, image->height);
|
||||
wl_surface_commit(window->cursor.surface);
|
||||
|
||||
return window;
|
||||
}
|
||||
|
||||
static void frame_callback(void *data, struct wl_callback *callback, uint32_t time) {
|
||||
struct window *window = data;
|
||||
wl_callback_destroy(callback);
|
||||
window->frame_cb = NULL;
|
||||
}
|
||||
|
||||
static const struct wl_callback_listener listener = {
|
||||
frame_callback
|
||||
};
|
||||
|
||||
int window_prerender(struct window *window) {
|
||||
if (window->frame_cb) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
get_next_buffer(window);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int window_render(struct window *window) {
|
||||
window->frame_cb = wl_surface_frame(window->surface);
|
||||
wl_callback_add_listener(window->frame_cb, &listener, window);
|
||||
|
||||
wl_surface_damage(window->surface, 0, 0, window->buffer->width, window->buffer->height);
|
||||
wl_surface_attach(window->surface, window->buffer->buffer, 0, 0);
|
||||
wl_surface_commit(window->surface);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
void window_teardown(struct window *window) {
|
||||
}
|
Loading…
Reference in a new issue