return 1;
}
+JSONValue* json_obj_get(JSONObject *obj, const char *name) {
+ for(size_t i=0;i<obj->size;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;i<obj.size;i++) {
+ json_value_free(obj.values[i].value);
+ free(obj.values[i].name);
+ }
+ free(obj.values);
+ break;
+ }
+ case JSON_ARRAY: {
+ JSONArray array = value->value.array;
+ for(size_t i=0;i<array.size;i++) {
+ json_value_free(array.array[i]);
+ }
+ free(array.array);
+ break;
+ }
+ case JSON_STRING: {
+ free(value->value.string.string);
+ break;
+ }
+ default: {
+ break;
+ }
+ }
+ free(value);
+}
#include <sys/un.h>
#include <pthread.h>
-
+#include "json.h"
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);
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) {
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));
}
}
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;i<value->value.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;i<value->value.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');
+ }
+}
+