Skip to content

Commit

Permalink
feat(swappy): introduce file option
Browse files Browse the repository at this point in the history
  • Loading branch information
jtheoof committed Dec 29, 2019
1 parent 7d56f77 commit c56df33
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 12 deletions.
1 change: 1 addition & 0 deletions include/swappy.h
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ struct swappy_state {

/* Options */
char *geometry_str;
char *file_str;

struct swappy_box *geometry;

Expand Down
49 changes: 37 additions & 12 deletions src/application.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <time.h>

#include "clipboard.h"
#include "file.h"
#include "notification.h"
#include "paint.h"
#include "render.h"
Expand Down Expand Up @@ -522,27 +523,44 @@ static bool init_gtk_window(struct swappy_state *state) {
return true;
}

static gboolean is_geometry_valid(struct swappy_state *state) {
static gboolean has_option_geometry(struct swappy_state *state) {
return (state->geometry_str != NULL);
}

static gboolean has_option_file(struct swappy_state *state) {
return (state->file_str != NULL);
}

static gboolean is_file_valid(const char *file) {
cairo_surface_t *surface = cairo_image_surface_create_from_png(file);
cairo_status_t status = cairo_surface_status(surface);

if (status) {
g_warning("error while loading: %s - cairo status: %s", file,
cairo_status_to_string(status));
return false;
}

return true;
}

static gint command_line_handler(GtkApplication *app,
GApplicationCommandLine *cmdline,
struct swappy_state *state) {
if (!is_geometry_valid(state)) {
g_printerr("geometry parameter is missing\n");
return EXIT_FAILURE;
}

g_debug("geometry is: %s", state->geometry_str);
if (has_option_geometry(state)) {
if (!screencopy_parse_geometry(state)) {
return EXIT_FAILURE;
}

if (!screencopy_parse_geometry(state)) {
return EXIT_FAILURE;
if (!screencopy_init(state)) {
return EXIT_FAILURE;
}
}

if (!screencopy_init(state)) {
g_printerr("unable to initialize zwlr_screencopy_v1\n");
return false;
if (has_option_file(state)) {
if (!is_file_valid(state->file_str)) {
return EXIT_FAILURE;
}
}

if (!init_gtk_window(state)) {
Expand All @@ -562,6 +580,13 @@ bool application_init(struct swappy_state *state) {
.description =
"Set the region to capture. (Can be an output of slurp)",
},
{
.long_name = "file",
.short_name = 'f',
.arg = G_OPTION_ARG_STRING,
.arg_data = &state->file_str,
.description = "Load a file at a specific path.",
},
{NULL}};

state->app = gtk_application_new("me.jtheoof.swappy",
Expand Down

0 comments on commit c56df33

Please sign in to comment.