From 1e2b55c2c2e79a41dd0056fda7966dabc329fc0d Mon Sep 17 00:00:00 2001 From: Olaf Wintermann Date: Sat, 8 Jan 2022 17:54:05 +0100 Subject: [PATCH] add functions for easier json object/array access --- application/Makefile | 1 + application/json.c | 43 ++++++++++++++++++++++ application/json.h | 5 +++ application/player.c | 85 ++++++++++++++++++++++++++++++++++++++++++-- application/player.h | 2 +- 5 files changed, 132 insertions(+), 4 deletions(-) diff --git a/application/Makefile b/application/Makefile index 8424552..ea21439 100644 --- a/application/Makefile +++ b/application/Makefile @@ -37,6 +37,7 @@ SRC += window.c SRC += player.c SRC += settings.c SRC += utils.c +SRC += json.c OBJ = $(SRC:%.c=$(BUILD_ROOT)/build/application/%.$(OBJ_EXT)) diff --git a/application/json.c b/application/json.c index 6b126d4..4dd672c 100644 --- a/application/json.c +++ b/application/json.c @@ -842,3 +842,46 @@ int json_read_value(JSONParser *p, JSONValue **value) { return 1; } +JSONValue* json_obj_get(JSONObject *obj, const char *name) { + for(size_t i=0;isize;i++) { + if(!strcmp(obj->values[i].name, name)) { + return obj->values[i].value; + } + } + return NULL; +} + +JSONValue* json_array_get(JSONArray *array, size_t i) { + if(i >= array->size) return NULL; + return array->array[i]; +} + +void json_value_free(JSONValue *value) { + switch(value->type) { + case JSON_OBJECT: { + JSONObject obj = value->value.object; + for(size_t i=0;ivalue.array; + for(size_t i=0;ivalue.string.string); + break; + } + default: { + break; + } + } + free(value); +} diff --git a/application/json.h b/application/json.h index 88494bf..659c7f6 100644 --- a/application/json.h +++ b/application/json.h @@ -200,6 +200,11 @@ int json_reader_bool(JSONParser *p); int json_read_value(JSONParser *p, JSONValue **value); +JSONValue* json_obj_get(JSONObject *obj, const char *name); + +JSONValue* json_array_get(JSONArray *array, size_t i); + +void json_value_free(JSONValue *value); #ifdef __cplusplus } diff --git a/application/player.c b/application/player.c index d553b81..38b3a0c 100644 --- a/application/player.c +++ b/application/player.c @@ -37,7 +37,7 @@ #include #include - +#include "json.h" extern char **environ; @@ -47,6 +47,8 @@ extern char **environ; #define PLAYER_POLL_TIMEOUT 500 #define PLAYER_IN_BUFSIZE 8192 +static void json_print(JSONValue *value, char *name, int indent); + static void* start_player(void *data); static void player_io(Player *p); @@ -254,6 +256,8 @@ static void player_io(Player *p) { fds[1].events = POLLIN; fds[1].revents = 0; + JSONParser *js = json_parser_new(); + char buf[PLAYER_IN_BUFSIZE]; while(poll(fds, 2, PLAYER_POLL_TIMEOUT)) { if(fds[0].revents == POLLIN) { @@ -266,12 +270,22 @@ static void player_io(Player *p) { if((r = read(fds[1].fd, buf, PLAYER_IN_BUFSIZE)) <= 0) { break; } + json_parser_fill(js, buf, r); - write(1, buf, r); + JSONValue *value; + int ret; + while((ret = json_read_value(js, &value)) == 1) { + json_print(value, NULL, 0); + json_value_free(value); + } + if(ret == -1) { + fprintf(stderr, "JSON-RPC error\n"); + break; + } } - char *cmd = "{ \"command\": [\"get_property\", \"playback-time\"] }\n"; + char *cmd = "{ \"command\": [\"get_property\", \"playback-time\"], request_id=\"" REQ_ID_PLAYBACK_TIME "\" }\n"; write(p->ipc, cmd, strlen(cmd)); } } @@ -295,3 +309,68 @@ void PlayerDestroy(Player *p) { free(p); } + +static void json_print(JSONValue *value, char *name, int indent) { + if(name) { + printf("%*s%s: ", indent*4, "", name); + } else { + printf("%*s", indent*4, ""); + } + + + switch(value->type) { + case JSON_OBJECT: { + printf("{\n"); + + for(int i=0;ivalue.object.size;i++) { + JSONObjValue val = value->value.object.values[i]; + json_print(val.value, val.name, indent+1); + if(i+1 < value->value.object.size) { + printf(",\n"); + } else { + printf("\n"); + } + } + + printf("%*s}", indent*4, ""); + break; + } + case JSON_ARRAY: { + printf("[\n"); + + for(int i=0;ivalue.object.size;i++) { + JSONValue *v = value->value.array.array[i]; + json_print(v, NULL, indent+1); + if(i+1 < value->value.array.size) { + printf(",\n"); + } else { + printf("\n"); + } + } + + printf("%*s]", indent*4, ""); + break; + } + case JSON_STRING: { + printf("\"%s\"", value->value.string.string); + break; + } + case JSON_INTEGER: { + printf("%i", (int)value->value.integer.value); + break; + } + case JSON_NUMBER: { + printf("%f", value->value.number.value); + break; + } + case JSON_LITERAL: { + printf("%s\n", "literal\n"); + break; + } + } + + if(indent == 0) { + putchar('\n'); + } +} + diff --git a/application/player.h b/application/player.h index 8d50a72..dc60827 100644 --- a/application/player.h +++ b/application/player.h @@ -30,7 +30,7 @@ extern "C" { #endif -#define REQ_ID_PLAYBACK_TIME 1 +#define REQ_ID_PLAYBACK_TIME "11" void PlayerOpenFile(MainWindow *win); -- 2.43.5