Skip to content

Commit

Permalink
feat(wayland): listing outputs
Browse files Browse the repository at this point in the history
  • Loading branch information
jtheoof committed Dec 2, 2019
1 parent ce27741 commit 5a55c8b
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 2 deletions.
3 changes: 3 additions & 0 deletions build/.idea/dictionaries/jattali.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 21 additions & 1 deletion include/swappy.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <stdlib.h>
#include <wayland-client.h>

#include "xdg-output-unstable-v1-client-protocol.h"
#include "wlr-layer-shell-unstable-v1-client-protocol.h"
#include "wlr-screencopy-unstable-v1-client-protocol.h"

Expand Down Expand Up @@ -79,4 +80,23 @@ struct swappy_state {

int argc;
char **argv;
};
};

struct swappy_output {
struct swappy_state *state;
struct wl_output *wl_output;
struct zxdg_output_v1 *xdg_output;
struct wl_list link;

struct swappy_box geometry;
enum wl_output_transform transform;
int32_t scale;

struct swappy_box logical_geometry;
double logical_scale; // guessed from the logical size
char *name;

// struct swappy_buffer *buffer;
struct zwlr_screencopy_frame_v1 *screencopy_frame;
uint32_t screencopy_frame_flags; // enum zwlr_screencopy_frame_v1_flags
};
75 changes: 74 additions & 1 deletion src/wayland.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "swappy.h"
#include "wlr-layer-shell-unstable-v1-client-protocol.h"
#include "wlr-screencopy-unstable-v1-client-protocol.h"
#include "xdg-output-unstable-v1-client-protocol.h"

static bool parse_box(struct swappy_box *box, const char *str) {
char *end = NULL;
Expand Down Expand Up @@ -34,6 +35,46 @@ static bool parse_box(struct swappy_box *box, const char *str) {
return true;
}

static void output_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) {
struct swappy_output *output = data;

output->geometry.x = x;
output->geometry.y = y;
output->transform = transform;
}

static void output_handle_mode(void *data, struct wl_output *wl_output,
uint32_t flags, int32_t width, int32_t height,
int32_t refresh) {
struct swappy_output *output = data;

if ((flags & WL_OUTPUT_MODE_CURRENT) != 0) {
output->geometry.width = width;
output->geometry.height = height;
}
}

static void output_handle_done(void *data, struct wl_output *wl_output) {
// No-op
}

static void output_handle_scale(void *data, struct wl_output *wl_output,
int32_t factor) {
struct swappy_output *output = data;
output->scale = factor;
}

static const struct wl_output_listener output_listener = {
.geometry = output_handle_geometry,
.mode = output_handle_mode,
.done = output_handle_done,
.scale = output_handle_scale,
};

bool wayland_screencopy_geometry(struct swappy_state *state) {
struct swappy_box *geometry = g_new(struct swappy_box, 1);
char *geometry_str = state->geometry_str;
Expand Down Expand Up @@ -62,6 +103,15 @@ static void global_registry_handler(void *data, struct wl_registry *registry,
} else if (strcmp(interface, wl_shm_interface.name) == 0) {
state->shm = wl_registry_bind(registry, name, &wl_shm_interface, version);
bound = true;
} else if (strcmp(interface, wl_output_interface.name) == 0) {
struct swappy_output *output = calloc(1, sizeof(struct swappy_output));
output->state = state;
output->scale = 1;
output->wl_output =
wl_registry_bind(registry, name, &wl_output_interface, 3);
wl_output_add_listener(output->wl_output, &output_listener, output);
wl_list_insert(&state->outputs, &output->link);
bound = true;
} else if (strcmp(interface, zwlr_layer_shell_v1_interface.name) == 0) {
state->layer_shell = wl_registry_bind(
registry, name, &zwlr_layer_shell_v1_interface, version);
Expand Down Expand Up @@ -98,6 +148,7 @@ bool wayland_init(struct swappy_state *state) {

g_debug("connected to wayland display");

wl_list_init(&state->outputs);
state->registry = wl_display_get_registry(state->display);
wl_registry_add_listener(state->registry, &registry_listener, state);
wl_display_roundtrip(state->display);
Expand All @@ -122,6 +173,28 @@ bool wayland_init(struct swappy_state *state) {
}

void wayland_finish(struct swappy_state *state) {
g_debug("cleaning up wayland resources");

struct swappy_output *output;
struct swappy_output *output_tmp;
wl_list_for_each_safe(output, output_tmp, &state->outputs, link) {
wl_list_remove(&output->link);
free(output->name);
if (output->screencopy_frame != NULL) {
zwlr_screencopy_frame_v1_destroy(output->screencopy_frame);
}
// destroy_buffer(output->buffer);
if (output->xdg_output != NULL) {
zxdg_output_v1_destroy(output->xdg_output);
}
wl_output_release(output->wl_output);
free(output);
}
// zwlr_screencopy_manager_v1_destroy(state.screencopy_manager);
// if (state.xdg_output_manager != NULL) {
// zxdg_output_manager_v1_destroy(state.xdg_output_manager);
// }
// wl_shm_destroy(state.shm);
wl_registry_destroy(state->registry);
wl_display_disconnect(state->display);
g_debug("disconnected from wayland display");
}

0 comments on commit 5a55c8b

Please sign in to comment.