feat(swappy): introduce file option

This commit is contained in:
Jeremy Attali 2019-12-29 02:12:17 -05:00
parent 7d56f772c1
commit c56df33d18
2 changed files with 39 additions and 13 deletions

View File

@ -123,6 +123,7 @@ struct swappy_state {
/* Options */
char *geometry_str;
char *file_str;
struct swappy_box *geometry;

View File

@ -4,6 +4,7 @@
#include <time.h>
#include "clipboard.h"
#include "file.h"
#include "notification.h"
#include "paint.h"
#include "render.h"
@ -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;
if (has_option_geometry(state)) {
if (!screencopy_parse_geometry(state)) {
return EXIT_FAILURE;
}
if (!screencopy_init(state)) {
return EXIT_FAILURE;
}
}
g_debug("geometry is: %s", state->geometry_str);
if (!screencopy_parse_geometry(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)) {
@ -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",