From 46481813a886e19c16513c993a2cac6a20d1e846 Mon Sep 17 00:00:00 2001 From: Olaf Wintermann Date: Wed, 12 Jan 2022 17:59:34 +0100 Subject: [PATCH] add rpc event handler --- application/json.c | 2 +- application/player.c | 44 +++++++++++++++++++++++++++++++++++++++++++- application/player.h | 5 +++++ application/window.h | 2 ++ 4 files changed, 51 insertions(+), 2 deletions(-) diff --git a/application/json.c b/application/json.c index 5099d9b..719a570 100644 --- a/application/json.c +++ b/application/json.c @@ -900,5 +900,5 @@ int json_strncmp(JSONValue *jsstr, const char *str, size_t slen) { return jsstrlen > slen ? 1 : -1; } - return memcmp(jsstr, jsstr->value.string.string, slen); + return memcmp(jsstr->value.string.string, str, slen); } diff --git a/application/player.c b/application/player.c index 83a5d4f..b34ea7b 100644 --- a/application/player.c +++ b/application/player.c @@ -55,6 +55,7 @@ static void player_io(Player *p); static void handle_json_rpc_msg(Player *player, JSONValue *v); static void handle_json_rpc_reqid(Player *player, JSONValue *v, int reqid); +static void handle_json_rpc_event(Player *player, JSONValue *v, JSONValue *event); void PlayerOpenFile(MainWindow *win) { pthread_t tid; @@ -273,6 +274,8 @@ static void player_io(Player *p) { if((r = read(fds[1].fd, buf, PLAYER_IN_BUFSIZE)) <= 0) { break; } + //fwrite(buf, 1, r, stdout); + fflush(stdout); json_parser_fill(js, buf, r); JSONValue *value; @@ -289,7 +292,7 @@ static void player_io(Player *p) { } char *cmd = "{ \"command\": [\"get_property\", \"playback-time\"], request_id=\"" REQ_ID_PLAYBACK_TIME "\" }\n"; - write(p->ipc, cmd, strlen(cmd)); + //write(p->ipc, cmd, strlen(cmd)); } } @@ -298,6 +301,7 @@ static void handle_json_rpc_msg(Player *player, JSONValue *v) { if(v->type != JSON_OBJECT) return; JSONValue *request_id_v = json_obj_get(&v->value.object, "request_id"); + JSONValue *event = NULL; if(request_id_v && request_id_v->type == JSON_STRING) { int request_id = 0; if(request_id_v->value.string.length == 2) { @@ -305,9 +309,24 @@ static void handle_json_rpc_msg(Player *player, JSONValue *v) { handle_json_rpc_reqid(player, v, request_id); return; } + } else if ((event = json_obj_get(&v->value.object, "event")) != NULL) { + handle_json_rpc_event(player, v, event); } json_print(v, NULL, 0); + fflush(stdout); +} + +static void player_set_size(Player *player, int width, int height) { + if(width >= 0) { + player->width = width; + } + if(height >= 0) { + player->height = height; + } + if(player->width > 0 && player->height > 0) { + printf("TODO: set player size\n"); + } } static void handle_json_rpc_reqid(Player *player, JSONValue *v, int reqid) { @@ -321,6 +340,29 @@ static void handle_json_rpc_reqid(Player *player, JSONValue *v, int reqid) { } break; } + case REQ_ID_WIDTH_INT: { + if(data->type == JSON_INTEGER) { + player_set_size(player, data->value.integer.value, -1); + } + break; + } + case REQ_ID_HEIGHT_INT: { + if(data->type == JSON_INTEGER) { + player_set_size(player, -1, data->value.integer.value); + } + break; + } + } +} + +static void handle_json_rpc_event(Player *p, JSONValue *v, JSONValue *event) { + if(!json_strcmp(event, "property-change")) { + printf("property change\n"); + } else if(!json_strcmp(event, "playback-restart")) { + char *cmd = "{ \"command\": [\"observe_property\", 1, \"playback-time\"] }\n" + "{ \"command\": [\"get_property\", \"width\"], request_id=\"" REQ_ID_WIDTH "\" }\n" + "{ \"command\": [\"get_property\", \"height\"], request_id=\"" REQ_ID_HEIGHT "\" }\n"; + write(p->ipc, cmd, strlen(cmd)); } } diff --git a/application/player.h b/application/player.h index 26980a6..3309096 100644 --- a/application/player.h +++ b/application/player.h @@ -32,6 +32,11 @@ extern "C" { #define REQ_ID_PLAYBACK_TIME "11" #define REQ_ID_PLAYBACK_TIME_INT 11 +#define REQ_ID_WIDTH "12" +#define REQ_ID_WIDTH_INT 12 +#define REQ_ID_HEIGHT "13" +#define REQ_ID_HEIGHT_INT 13 + void PlayerOpenFile(MainWindow *win); diff --git a/application/window.h b/application/window.h index c90dfe5..37d6d17 100644 --- a/application/window.h +++ b/application/window.h @@ -40,6 +40,8 @@ typedef struct Player { bool isactive; double playback_time; + int width; + int height; } Player; typedef struct MainWindow { -- 1.8.3.1