Skip to content

Commit

Permalink
config: add loading of basic JSON config data
Browse files Browse the repository at this point in the history
  • Loading branch information
Lokaltog committed Mar 2, 2014
1 parent e8ba723 commit b20a7a0
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 3 deletions.
1 change: 1 addition & 0 deletions Makefile.am
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
bin_PROGRAMS = wkline
wkline_SOURCES = \
util/log.c \
src/load_config.c \
src/wkline.c \
src/widgets/desktops.c

Expand Down
4 changes: 1 addition & 3 deletions config.def.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@
"height": 26,
"position": "top",
"theme_uri": "http://lokaltog.github.io/wkline-default-theme/",
"background": {
"color": "#111"
},
"background": "#111",
"widgets": [
{
"name": "desktops",
Expand Down
24 changes: 24 additions & 0 deletions src/load_config.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include "load_config.h"

#include <string.h>

int
load_config_file (wkline_config_t *config) {
const char *user_config_file = g_build_filename(g_get_user_config_dir(), "wkline", "config.json", NULL);
json_error_t err;
json_t *json_config = json_load_file(user_config_file, 0, &err);

if (! json_config) {
fprintf(stderr, "\n***************************\nFailed to load config file!\n***************************\n\n%s\n\nPlease make sure that '%s' exists and contains valid JSON data.\n\n", err.text, user_config_file);
exit(1);
}

config->height = json_integer_value(json_object_get(json_config, "height"));
config->position = json_string_value(json_object_get(json_config, "position")) == "top" ? WKLINE_POSITION_TOP : WKLINE_POSITION_BOTTOM;
strcpy(config->theme_uri, json_string_value(json_object_get(json_config, "theme_uri")));

This comment has been minimized.

Copy link
@nHurD

nHurD Mar 2, 2014

Contributor

This might be the cause of your issue here, the config->theme_url pointer needs to be allocated before something is assigned to it. Have a look at strnprintf() and strncpy() to help with getting/setting the size of the pointer, then calling malloc(), and finally setting the value as needed.

This comment has been minimized.

Copy link
@Lokaltog

Lokaltog Mar 2, 2014

Author Owner

It's possibly an incorrect way to do it, but assigningtheme_uri and background in this way actually works without any issue. But it's using static array sizes, I'll look into doing it with snprintf like the fix you committed earlier.

The issue I'm struggling with is assigning a pointer to a json_t object from json_config in this function.

This comment has been minimized.

Copy link
@nHurD

nHurD Mar 2, 2014

Contributor

Ah, sorry, I probably should have paid closer attention to the rest of it! Did you try commenting out the json_decref() call to see what happens? It may be that that call is squashing out all pointers to it? if that's the case, perhaps a memcpy() call could fix that by copying the widget data to a new segment of memory?

Once I get some free time later tonight, I'll fetch this branch and poke around for a bit

This comment has been minimized.

Copy link
@Lokaltog

Lokaltog Mar 2, 2014

Author Owner

Oh man, I totally forgot that was even there. It looks like that was the cause of the issue, it seems to work when not asking C to remove all references to the pointer :P

This comment has been minimized.

Copy link
@ricardomv

ricardomv Mar 3, 2014

Contributor

to copy a string use strdup it allocates the memory and copies the string

strcpy(config->background, json_string_value(json_object_get(json_config, "background")));

json_decref(json_config);

return 0;
}
16 changes: 16 additions & 0 deletions src/load_config.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include <glib.h>
#include <jansson.h>

#define CONFIG_BUF_SIZE 256

enum wkline_position {
WKLINE_POSITION_TOP,
WKLINE_POSITION_BOTTOM
};

typedef struct wkline_config_t {
int height;
enum wkline_position position;
char theme_uri[CONFIG_BUF_SIZE];
char background[CONFIG_BUF_SIZE];
} wkline_config_t;
4 changes: 4 additions & 0 deletions src/wkline.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "config.h"
#include "wkline.h"
#include "load_config.h"
#include "util/log.h"
#include "widgets/widgets.h"

Expand Down Expand Up @@ -68,6 +69,9 @@ main (int argc, char *argv[]) {
GtkWindow *window;
GtkLayout *layout;

wkline_config_t config;
load_config_file(&config);

gtk_init(&argc, &argv);

// GtkScrolledWindow fails to lock small heights (<25px), so a GtkLayout is used instead
Expand Down

0 comments on commit b20a7a0

Please sign in to comment.