Skip to content

Commit

Permalink
feat(buffer): add file image support
Browse files Browse the repository at this point in the history
  • Loading branch information
jtheoof committed Dec 29, 2019
1 parent f8494a7 commit f6c189c
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 1 deletion.
5 changes: 4 additions & 1 deletion include/buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ void screencopy_frame_handle_ready(void *data,
uint32_t tv_nsec);
void screencopy_frame_handle_failed(void *data,
struct zwlr_screencopy_frame_v1 *frame);
bool buffer_init_from_screencopy(struct swappy_state *state);
void screencopy_destroy_buffer(struct swappy_buffer *buffer);

bool buffer_init_from_screencopy(struct swappy_state *state);
bool buffer_init_from_file(struct swappy_state *state);

bool buffer_parse_geometry(struct swappy_state *state);
1 change: 1 addition & 0 deletions include/swappy.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ struct swappy_state {
struct swappy_state_ui *ui;

cairo_surface_t *cairo_surface;
cairo_surface_t *image_surface;

struct wl_display *display;
struct wl_registry *registry;
Expand Down
7 changes: 7 additions & 0 deletions src/application.c
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ void arrow_clicked_handler(GtkWidget *widget, struct swappy_state *state) {
void application_finish(struct swappy_state *state) {
paint_free_all(state);
cairo_surface_destroy(state->cairo_surface);
cairo_surface_destroy(state->image_surface);
g_free(state->storage_path);
g_free(state->geometry_str);
g_free(state->geometry);
Expand Down Expand Up @@ -541,6 +542,8 @@ static gboolean is_file_valid(const char *file) {
return false;
}

cairo_surface_destroy(surface);

return true;
}

Expand All @@ -561,6 +564,10 @@ static gint command_line_handler(GtkApplication *app,
if (!is_file_valid(state->file_str)) {
return EXIT_FAILURE;
}

if (!buffer_init_from_file(state)) {
return EXIT_FAILURE;
}
}

if (!init_gtk_window(state)) {
Expand Down
23 changes: 23 additions & 0 deletions src/buffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,29 @@ bool buffer_init_from_screencopy(struct swappy_state *state) {
return true;
}

bool buffer_init_from_file(struct swappy_state *state) {
char *file = state->file_str;

cairo_surface_t *surface = cairo_image_surface_create_from_png(file);

g_assert(surface);

int width = cairo_image_surface_get_width(surface);
int height = cairo_image_surface_get_height(surface);

struct swappy_box *geometry = g_new(struct swappy_box, 1);

geometry->x = 0;
geometry->y = 0;
geometry->width = (int32_t)width;
geometry->height = (int32_t)height;

state->geometry = geometry;
state->image_surface = surface;

return true;
}

bool buffer_parse_geometry(struct swappy_state *state) {
struct swappy_box *geometry = g_new(struct swappy_box, 1);
char *geometry_str = state->geometry_str;
Expand Down
13 changes: 13 additions & 0 deletions src/render.c
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,18 @@ static void render_buffer(cairo_t *cr, struct swappy_state *state) {
}
}

static void render_image(cairo_t *cr, struct swappy_state *state) {
if (!state->image_surface) {
return;
}

cairo_pattern_t *output_pattern =
cairo_pattern_create_for_surface(state->image_surface);
cairo_set_source(cr, output_pattern);
cairo_pattern_destroy(output_pattern);
cairo_paint(cr);
}

static void render_background(cairo_t *cr) {
cairo_set_source_rgb(cr, 0, 0, 0);
cairo_paint(cr);
Expand Down Expand Up @@ -270,6 +282,7 @@ void render_state(struct swappy_state *state) {

render_background(cr);
render_buffer(cr, state);
render_image(cr, state);
render_paints(cr, state);

// Drawing is finished, notify the GtkDrawingArea it needs to be redrawn.
Expand Down

0 comments on commit f6c189c

Please sign in to comment.