X-Git-Url: https://develop.uap-core.de/gitweb/uwplayer.git/blobdiff_plain/2a56f611dc3fe723d5b52c54a74845ecd7714e25..5cb490b04836ef624cdd0ba975ee5c2ff2e51a23:/application/player.c diff --git a/application/player.c b/application/player.c index 83a5d4f..a2b522d 100644 --- a/application/player.c +++ b/application/player.c @@ -21,6 +21,7 @@ */ #include "player.h" +#include "main.h" #include #include @@ -38,6 +39,9 @@ #include #include "json.h" +#include "utils.h" +#include "settings.h" +#include "playlist.h" extern char **environ; @@ -55,6 +59,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; @@ -100,6 +105,20 @@ static int prepare_player(Player *player, char *log_arg, char *ipc_arg) { return 0; } +static void* wait_for_process(void *data) { + Player *player = data; + int status = 0; + waitpid(player->process, &status, 0); + + printf("waitpid: %d\n", status); + + player->isactive = FALSE; + player->status = status; + SetPlayerWindow(0); + + return NULL; +} + static int start_player_process(Player *player, MainWindow *win) { char log_arg[STR_BUFSIZE]; char ipc_arg[STR_BUFSIZE]; @@ -108,7 +127,11 @@ static int start_player_process(Player *player, MainWindow *win) { return 1; } - char *player_bin = "/usr/local/bin/mpv"; // TODO: get bin from settings + char *player_bin = SettingsGetPlayerBin(); + if(!player_bin) { + fprintf(stderr, "No mpv binary available\n"); + return 1; + } // -wid parameter value for embedding the player in the player_widget Window wid = XtWindow(win->player_widget); @@ -119,14 +142,16 @@ static int start_player_process(Player *player, MainWindow *win) { // create player arg list char *args[32]; - args[0] = player_bin; - args[1] = "-wid"; - args[2] = wid_arg; - args[3] = "--no-terminal"; - args[4] = log_arg; - args[5] = ipc_arg; - args[6] = win->file; - args[7] = NULL; + int ac = 0; + args[ac++] = player_bin; + args[ac++] = "-wid"; + args[ac++] = wid_arg; + //args[ac++] = "--no-terminal"; + args[ac++] = "--player-operation-mode=pseudo-gui"; + args[ac++] = log_arg; + args[ac++] = ipc_arg; + args[ac++] = win->file; + args[ac++] = NULL; posix_spawn_file_actions_t actions; posix_spawn_file_actions_init(&actions); @@ -145,6 +170,11 @@ static int start_player_process(Player *player, MainWindow *win) { player->process = player_pid; player->isactive = TRUE; + pthread_t tid; + if(pthread_create(&tid, NULL, wait_for_process, player)) { + perror("pthread_create"); + } + return 0; } @@ -188,7 +218,7 @@ static int wait_for_ipc(Player *player) { return 0; } -static int connect_to_ipc(Player *player) { +static int connect_to_ipc(Player *player) { // connect to IPC socket int fd_ipc = socket(AF_UNIX, SOCK_STREAM, 0); if(fd_ipc < 0) { @@ -212,6 +242,12 @@ static int connect_to_ipc(Player *player) { return 0; } +static Boolean update_player_window(XtPointer data) { + MainWindow *win = data; + WindowUpdate(win); + return 0; +} + static void* start_player(void *data) { MainWindow *win = data; @@ -229,6 +265,7 @@ static void* start_player(void *data) { PlayerDestroy(player); return NULL; } + //close(player->log); if(connect_to_ipc(player)) { PlayerDestroy(player); @@ -241,6 +278,9 @@ static void* start_player(void *data) { } win->player = player; + // update main window + AppExecProc(update_player_window, win); + // IO player_io(player); @@ -248,31 +288,24 @@ static void* start_player(void *data) { } static void player_io(Player *p) { - int flags = fcntl(p->log, F_GETFL, 0); - fcntl(p->log, F_SETFL, flags | O_NONBLOCK); - struct pollfd fds[2]; - fds[0].fd = p->log; + fds[0].fd = p->ipc; fds[0].events = POLLIN; fds[0].revents = 0; - fds[1].fd = p->ipc; + fds[1].fd = p->log; 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)) { + while(p->isactive && (poll(fds, 2, PLAYER_POLL_TIMEOUT) >= 0)) { if(fds[0].revents == POLLIN) { - // clean up fifo - read(fds[0].fd, buf, PLAYER_IN_BUFSIZE); - } - - if(fds[1].revents == POLLIN) { ssize_t r; - if((r = read(fds[1].fd, buf, PLAYER_IN_BUFSIZE)) <= 0) { + if((r = read(fds[0].fd, buf, PLAYER_IN_BUFSIZE)) <= 0) { break; } + //fwrite(buf, 1, r, stdout); + fflush(stdout); json_parser_fill(js, buf, r); JSONValue *value; @@ -287,17 +320,26 @@ static void player_io(Player *p) { break; } } + if(fds[1].revents == POLLIN) { + // just read to clean the log pipe + read(fds[1].fd, buf, PLAYER_IN_BUFSIZE); + } - char *cmd = "{ \"command\": [\"get_property\", \"playback-time\"], request_id=\"" REQ_ID_PLAYBACK_TIME "\" }\n"; - write(p->ipc, cmd, strlen(cmd)); + //char *cmd = "{ \"command\": [\"get_property\", \"playback-time\"], request_id=\"" REQ_ID_PLAYBACK_TIME "\" }\n"; + //write(p->ipc, cmd, strlen(cmd)); } + + + printf("PlayerEnd: %s\n", strerror(errno)); + fflush(stdout); } 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 +347,56 @@ 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); +} + +static Boolean player_widget_set_size(XtPointer data) { + Player *player = data; + MainWindow *win = GetMainWindow(); + + if(!win->adjustWindowSize) { + return 0; } + + Dimension win_width, win_height; + XtVaGetValues(win->window, XmNwidth, &win_width, XmNheight, &win_height, NULL); + Dimension player_width, player_height; + XtVaGetValues(win->player_widget, XmNwidth, &player_width, XmNheight, &player_height, NULL); + + Dimension new_width = player->width + win_width - player_width; + Dimension new_height = player->height + win_height - player_height; - json_print(v, NULL, 0); + // set window size + XtVaSetValues(win->window, XmNwidth, new_width, XmNheight, new_height, NULL); + + // set window aspect ratio + XSizeHints hints; + hints.flags = PAspect; + hints.min_aspect.x = new_width; + hints.min_aspect.y = new_height; + hints.max_aspect.x = new_width; + hints.max_aspect.y = new_height; + XSetWMNormalHints(XtDisplay(win->window), XtWindow(win->window), &hints); + + return 0; +} + + + +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) { + AppExecProc(player_widget_set_size, player); + } } static void handle_json_rpc_reqid(Player *player, JSONValue *v, int reqid) { @@ -321,6 +410,88 @@ 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 Boolean get_player_window(XtPointer data) { + Player *p = data; + MainWindow *win = GetMainWindow(); + + Widget player_wid = win->player_widget; + Window root, parent; + Window *child; + unsigned int nchild; + XQueryTree(XtDisplay(player_wid), XtWindow(player_wid), &root, &parent, &child, &nchild); + if(nchild > 0) { + p->window = child[0]; + XFree(child); + + SetPlayerWindow(p->window); + XSelectInput(XtDisplay(win->player_widget), p->window, PointerMotionMask); + } + + return 0; +} + +#define CURSOR_AUTOHIDE_THRESHOLD_SEC 4 + +static Boolean hide_cursor(XtPointer data) { + MainWindow *win = data; + WindowHidePlayerCursor(win); + return 0; +} + +static void check_hide_cursor(Player *p) { + MainWindow *win = GetMainWindow(); + if(win->cursorhidden) return; + + if(p->playback_time - win->motion_playback_time > CURSOR_AUTOHIDE_THRESHOLD_SEC) { + AppExecProc(hide_cursor, win); + } +} + +static void handle_json_rpc_event(Player *p, JSONValue *v, JSONValue *event) { + if(!json_strcmp(event, "property-change")) { + JSONValue *name = json_obj_get(&v->value.object, "name"); + JSONValue *data = json_obj_get(&v->value.object, "data"); + if(!json_strcmp(name, "playback-time")) { + if(data && data->type == JSON_NUMBER) { + p->playback_time = data->value.number.value; + //printf("playback-time: %f\n", p->playback_time); + check_hide_cursor(p); + } + } else if(!json_strcmp(name, "eof-reached")) { + if(data && data->type == JSON_LITERAL && data->value.literal.literal == JSON_TRUE) { + PlayerEOF(p); + } + } else if(!json_strcmp(name, "osd-height")) { + if(data->type == JSON_NUMBER) { + p->osd_height = data->value.number.value; + } + } + } else if(!p->isstarted && !json_strcmp(event, "playback-restart")) { + char *cmd = "{ \"command\": [\"observe_property\", 1, \"playback-time\"] }\n" + "{ \"command\": [\"observe_property\", 1, \"eof-reached\"] }\n" + "{ \"command\": [\"observe_property\", 1, \"osd-height\"] }\n" + "{ \"command\": [\"get_property\", \"width\"], request_id=\"" REQ_ID_WIDTH "\" }\n" + "{ \"command\": [\"get_property\", \"height\"], request_id=\"" REQ_ID_HEIGHT "\" }\n" + "{ \"command\": [\"set_property\", \"keep-open\", true] }\n"; + write(p->ipc, cmd, strlen(cmd)); + p->isstarted = TRUE; + + AppExecProc(get_player_window, p); } } @@ -340,6 +511,7 @@ void PlayerDestroy(Player *p) { kill(p->process, SIGTERM); } + SetPlayerWindow(0); free(p); } @@ -398,7 +570,13 @@ static void json_print(JSONValue *value, char *name, int indent) { break; } case JSON_LITERAL: { - printf("%s\n", "literal\n"); + char *lit = "NULL"; + switch(value->value.literal.literal) { + case JSON_NULL: break; + case JSON_TRUE: lit = "true"; break; + case JSON_FALSE: lit = "false"; break; + } + printf("%s\n", lit); break; } } @@ -408,3 +586,24 @@ static void json_print(JSONValue *value, char *name, int indent) { } } +static Boolean play_next(XtPointer data) { + MainWindow *win = GetMainWindow(); + PlayListPlayNext(win, false); + return 0; +} + +void PlayerEOF(Player *p) { + MainWindow *win = GetMainWindow(); + if(win->playlist.repeatTrack) { + char *cmd = "{ \"command\": [\"set_property\", \"playback-time\", 0] }\n"; + write(p->ipc, cmd, strlen(cmd)); + cmd = "{ \"command\": [\"set_property\", \"pause\", false] }\n"; + write(p->ipc, cmd, strlen(cmd)); + } else { + AppExecProc(play_next, NULL); + } +} + +void PlayerHandleInput(MainWindow *win, Player *p, XmDrawingAreaCallbackStruct *cb) { + +}