Skip to content

Commit

Permalink
feat(config): have overridable defaults
Browse files Browse the repository at this point in the history
Defaults:

 - `save_dir`
 - `stroke_size`
 - `text_font`
 - `text_size`

Closes #1
  • Loading branch information
jtheoof committed Jan 13, 2020
1 parent 7f2f6da commit df13b72
Show file tree
Hide file tree
Showing 8 changed files with 216 additions and 36 deletions.
7 changes: 6 additions & 1 deletion include/config.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
#include "swappy.h"

bool config_get_storage_path(struct swappy_state *state);
#define CONFIG_LINE_SIZE_DEFAULT 5
#define CONFIG_TEXT_FONT_DEFAULT "serif"
#define CONFIG_TEXT_SIZE_DEFAULT 20

void config_load(struct swappy_state *state);
void config_free(struct swappy_state *state);
17 changes: 11 additions & 6 deletions include/swappy.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,9 @@

#define GEOMETRY_PATTERN "xx,yy wwxhh"

#define SWAPPY_STROKE_SIZE_DEFAULT 5
#define SWAPPY_STROKE_SIZE_MIN 1
#define SWAPPY_STROKE_SIZE_MAX 50

#define SWAPPY_TEXT_FONT_DEFAULT "serif"
#define SWAPPY_TEXT_SIZE_DEFAULT 20
#define SWAPPY_TEXT_SIZE_MIN 10
#define SWAPPY_TEXT_SIZE_MAX 50

Expand Down Expand Up @@ -49,6 +46,7 @@ struct swappy_paint_text {
double b;
double a;
double s;
gchar *font;
gchar *text;
size_t cursor;
struct swappy_point from;
Expand Down Expand Up @@ -124,7 +122,7 @@ struct swappy_state_ui {
GtkRadioButton *custom;
GtkColorButton *color;

GtkButton *stroke_size;
GtkButton *line_size;
GtkButton *text_size;
};

Expand Down Expand Up @@ -170,17 +168,24 @@ struct swappy_wayland {
#endif
};

struct swappy_config {
char *config_file;
char *save_dir;
guint32 line_size;
guint32 text_size;
char *text_font;
};

struct swappy_state {
GtkApplication *app;

struct swappy_state_ui *ui;
struct swappy_config *config;
struct swappy_wayland *wl;

cairo_surface_t *cairo_surface;
GList *patterns; // List of cairo_pattern_t

char *storage_path;

enum swappy_paint_type mode;

/* Options */
Expand Down
32 changes: 19 additions & 13 deletions src/application.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include "buffer.h"
#include "clipboard.h"
#include "config.h"
#include "file.h"
#include "notification.h"
#include "paint.h"
Expand All @@ -22,7 +23,7 @@ static void update_ui_undo_redo(struct swappy_state *state) {
}

static void update_ui_stroke_size_widget(struct swappy_state *state) {
GtkButton *button = GTK_BUTTON(state->ui->stroke_size);
GtkButton *button = GTK_BUTTON(state->ui->line_size);
char label[255];
snprintf(label, 255, "%.0lf", state->settings.w);
gtk_button_set_label(button, label);
Expand Down Expand Up @@ -123,7 +124,7 @@ static void action_stroke_size_decrease(struct swappy_state *state) {
}

static void action_stroke_size_reset(struct swappy_state *state) {
state->settings.w = SWAPPY_STROKE_SIZE_DEFAULT;
state->settings.w = state->config->line_size;

update_ui_stroke_size_widget(state);
}
Expand All @@ -150,7 +151,7 @@ static void action_text_size_decrease(struct swappy_state *state) {
update_ui_text_size_widget(state);
}
static void action_text_size_reset(struct swappy_state *state) {
state->settings.t = SWAPPY_TEXT_SIZE_DEFAULT;
state->settings.t = state->config->text_size;
update_ui_text_size_widget(state);
}
static void action_text_size_increase(struct swappy_state *state) {
Expand Down Expand Up @@ -188,14 +189,14 @@ void application_finish(struct swappy_state *state) {
paint_free_all(state);
buffer_free_all(state);
cairo_surface_destroy(state->cairo_surface);
g_free(state->storage_path);
g_free(state->file_str);
g_free(state->geometry_str);
g_free(state->geometry);
g_free(state->ui);
g_object_unref(state->app);

wayland_finish(state);
config_free(state);
}

static void action_save_area_to_file(struct swappy_state *state) {
Expand All @@ -213,7 +214,7 @@ static void action_save_area_to_file(struct swappy_state *state) {
c_time_string = ctime(&current_time);
c_time_string[strlen(c_time_string) - 1] = '\0';
char path[MAX_PATH];
snprintf(path, MAX_PATH, "%s/%s %s.png", state->storage_path, "Swappshot",
snprintf(path, MAX_PATH, "%s/%s %s.png", state->config->save_dir, "Swappshot",
c_time_string);
gdk_pixbuf_savev(pixbuf, path, "png", NULL, NULL, &error);

Expand Down Expand Up @@ -561,7 +562,7 @@ static bool load_layout(struct swappy_state *state) {
state->ui->color =
GTK_COLOR_BUTTON(gtk_builder_get_object(builder, "custom-color-button"));

state->ui->stroke_size =
state->ui->line_size =
GTK_BUTTON(gtk_builder_get_object(builder, "stroke-size-button"));
state->ui->text_size =
GTK_BUTTON(gtk_builder_get_object(builder, "text-size-button"));
Expand Down Expand Up @@ -616,9 +617,21 @@ static gboolean is_file_from_stdin(const char *file) {
return (strcmp(file, "-") == 0);
}

static void init_settings(struct swappy_state *state) {
state->settings.r = 1;
state->settings.g = 0;
state->settings.b = 0;
state->settings.a = 1;
state->settings.w = state->config->line_size;
state->settings.t = state->config->text_size;
}

static gint command_line_handler(GtkApplication *app,
GApplicationCommandLine *cmdline,
struct swappy_state *state) {
config_load(state);
init_settings(state);

if (!wayland_init(state)) {
g_warning(
"error while initializing wayland objects, can only be used in file "
Expand Down Expand Up @@ -688,13 +701,6 @@ bool application_init(struct swappy_state *state) {
g_signal_connect(state->app, "command-line", G_CALLBACK(command_line_handler),
state);

state->settings.r = 1;
state->settings.g = 0;
state->settings.b = 0;
state->settings.a = 1;
state->settings.w = SWAPPY_STROKE_SIZE_DEFAULT;
state->settings.t = SWAPPY_TEXT_SIZE_DEFAULT;

return true;
}

Expand Down
157 changes: 152 additions & 5 deletions src/config.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#include "config.h"

#include <glib.h>
#include <stdbool.h>
#include <stdio.h>
#include <sys/stat.h>
Expand All @@ -6,11 +9,21 @@
#include "file.h"
#include "swappy.h"

bool config_get_storage_path(struct swappy_state *state) {
static void print_config(struct swappy_config *config) {
g_info("printing config:");
g_info("config_dir: %s", config->config_file);
g_info("save_dir: %s", config->save_dir);
g_info("line_size: %d", config->line_size);
g_info("text_font: %s", config->text_font);
g_info("text_size: %d", config->text_size);
}

static char *get_default_save_dir() {
static const char *storage_paths[] = {
"$XDG_DESKTOP_DIR",
"$XDG_CONFIG_HOME/Desktop",
"$HOME/Desktop",
"$HOME",
};

for (size_t i = 0; i < sizeof(storage_paths) / sizeof(char *); ++i) {
Expand All @@ -19,13 +32,147 @@ bool config_get_storage_path(struct swappy_state *state) {
char *path = g_strdup(p.we_wordv[0]);
wordfree(&p);
if (path && folder_exists(path)) {
g_info("storage path is: %s", path);
state->storage_path = path;
return true;
return path;
}
g_free(path);
}
}

return NULL;
}

static char *get_config_file() {
static const char *storage_paths[] = {
"$XDG_CONFIG_HOME/swappy/config",
"$HOME/.config/swappy/config",
};

for (size_t i = 0; i < sizeof(storage_paths) / sizeof(char *); ++i) {
wordexp_t p;
if (wordexp(storage_paths[i], &p, 0) == 0) {
char *path = g_strdup(p.we_wordv[0]);
wordfree(&p);
if (path && file_exists(path)) {
return path;
}
g_free(path);
}
}

return false;
return NULL;
}

static void load_config_from_file(struct swappy_config *config,
const char *file) {
GKeyFile *gkf;
const gchar *group = "Default";
gchar *save_dir = NULL;
gchar *save_dir_expanded = NULL;
guint64 line_size;
gchar *text_font = NULL;
guint64 text_size;
GError *error = NULL;

if (file == NULL) {
return;
}

gkf = g_key_file_new();

if (!g_key_file_load_from_file(gkf, file, G_KEY_FILE_NONE, NULL)) {
g_warning("could not read config file %s", file);
g_key_file_free(gkf);
return;
}

save_dir = g_key_file_get_string(gkf, group, "save_dir", &error);

if (error == NULL) {
wordexp_t p;
if (wordexp(save_dir, &p, 0) == 0) {
save_dir_expanded = g_strdup(p.we_wordv[0]);
wordfree(&p);
if (!save_dir_expanded || !folder_exists(save_dir_expanded)) {
g_warning("save_dir: %s is not a valid directory", save_dir_expanded);
}

g_free(save_dir);
g_free(config->save_dir);
config->save_dir = save_dir_expanded;
}
} else {
g_info("save_dir is missing in %s (%s)", file, error->message);
g_error_free(error);
error = NULL;
}

line_size = g_key_file_get_uint64(gkf, group, "line_size", &error);

if (error == NULL) {
config->line_size = line_size;
} else {
g_info("line_size is missing in %s (%s)", file, error->message);
g_error_free(error);
error = NULL;
}

text_size = g_key_file_get_uint64(gkf, group, "text_size", &error);

if (error == NULL) {
config->text_size = text_size;
} else {
g_info("text_size is missing in %s (%s)", file, error->message);
g_error_free(error);
error = NULL;
}

text_font = g_key_file_get_string(gkf, group, "text_font", &error);

if (error == NULL) {
g_free(config->text_font);
config->text_font = text_font;
} else {
g_info("text_font is missing in %s (%s)", file, error->message);
g_error_free(error);
error = NULL;
}

g_key_file_free(gkf);
}

static void load_default_config(struct swappy_config *config) {
if (config == NULL) {
return;
}

config->save_dir = get_default_save_dir();
config->line_size = CONFIG_LINE_SIZE_DEFAULT;
config->text_font = g_strdup(CONFIG_TEXT_FONT_DEFAULT);
config->text_size = CONFIG_TEXT_SIZE_DEFAULT;
}

void config_load(struct swappy_state *state) {
struct swappy_config *config = g_new(struct swappy_config, 1);

load_default_config(config);

char *file = get_config_file();
if (file) {
load_config_from_file(config, file);
} else {
g_info("could not find swappy config file, using defaults");
}

config->config_file = file;
state->config = config;

print_config(state->config);
}

void config_free(struct swappy_state *state) {
g_free(state->config->config_file);
g_free(state->config->save_dir);
g_free(state->config->text_font);
g_free(state->config);
state->config = NULL;
}
5 changes: 0 additions & 5 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,6 @@ int main(int argc, char *argv[]) {
state.argv = argv;
state.mode = SWAPPY_PAINT_MODE_BRUSH;

if (!config_get_storage_path(&state)) {
g_critical("could not find a valid pictures path in your env variables");
exit(1);
}

if (!application_init(&state)) {
g_critical("failed to initialize gtk application");
exit(1);
Expand Down
2 changes: 2 additions & 0 deletions src/paint.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ void paint_free(gpointer data) {
break;
case SWAPPY_PAINT_MODE_TEXT:
g_free(paint->content.text.text);
g_free(paint->content.text.font);
break;
default:
break;
Expand Down Expand Up @@ -111,6 +112,7 @@ void paint_add_temporary(struct swappy_state *state, double x, double y,
paint->content.text.b = b;
paint->content.text.a = a;
paint->content.text.s = t;
paint->content.text.font = g_strdup(state->config->text_font);
paint->content.text.cursor = 0;
paint->content.text.mode = SWAPPY_TEXT_MODE_EDIT;
paint->content.text.text = g_new(gchar, 1);
Expand Down
2 changes: 1 addition & 1 deletion src/render.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ static void render_text(cairo_t *cr, struct swappy_paint_text text) {

pango_layout_t *layout = pango_cairo_create_layout(crt);
pango_layout_set_text(layout, text.text, -1);
snprintf(pango_font, 255, "%s %d", SWAPPY_TEXT_FONT_DEFAULT, (int)text.s);
snprintf(pango_font, 255, "%s %d", text.font, (int)text.s);
pango_font_description_t *desc =
pango_font_description_from_string(pango_font);
pango_layout_set_width(layout, pango_units_from_double(w));
Expand Down
Loading

0 comments on commit df13b72

Please sign in to comment.