mirror of
https://github.com/mpv-player/mpv
synced 2025-03-22 11:18:32 +00:00
input: implement --input-file on unix using the IPC support
This commit is contained in:
parent
e0f0f6fe26
commit
3deb6c3d4f
@ -165,7 +165,6 @@ struct input_opts {
|
||||
int ar_delay;
|
||||
int ar_rate;
|
||||
char *js_dev;
|
||||
char *in_file;
|
||||
int use_joystick;
|
||||
int use_lirc;
|
||||
char *lirc_configfile;
|
||||
@ -188,7 +187,6 @@ const struct m_sub_options input_config = {
|
||||
OPT_PRINT("keylist", mp_print_key_list),
|
||||
OPT_PRINT("cmdlist", mp_print_cmd_list),
|
||||
OPT_STRING("js-dev", js_dev, CONF_GLOBAL),
|
||||
OPT_STRING("file", in_file, CONF_GLOBAL),
|
||||
OPT_FLAG("default-bindings", default_bindings, CONF_GLOBAL),
|
||||
OPT_FLAG("test", test, CONF_GLOBAL),
|
||||
OPT_INTRANGE("doubleclick-time", doubleclick_time, 0, 0, 1000),
|
||||
@ -1296,13 +1294,14 @@ void mp_input_load(struct input_ctx *ictx)
|
||||
|
||||
ictx->win_drag = ictx->global->opts->allow_win_drag;
|
||||
|
||||
if (input_conf->in_file && input_conf->in_file[0]) {
|
||||
#if !defined(__MINGW32__) || HAVE_WAIO
|
||||
mp_input_pipe_add(ictx, input_conf->in_file);
|
||||
#if defined(__MINGW32__)
|
||||
if (ictx->global->opts->input_file && *ictx->global->opts->input_file)
|
||||
#if HAVE_WAIO
|
||||
mp_input_pipe_add(ictx, ictx->global->opts->input_file);
|
||||
#else
|
||||
MP_ERR(ictx, "Pipes not available.\n");
|
||||
#endif
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
static void clear_queue(struct cmd_queue *queue)
|
||||
|
70
input/ipc.c
70
input/ipc.c
@ -31,6 +31,7 @@
|
||||
#include "osdep/io.h"
|
||||
|
||||
#include "common/common.h"
|
||||
#include "common/global.h"
|
||||
#include "common/msg.h"
|
||||
#include "input/input.h"
|
||||
#include "libmpv/client.h"
|
||||
@ -53,6 +54,7 @@ struct client_arg {
|
||||
struct mp_log *log;
|
||||
struct mpv_handle *client;
|
||||
|
||||
char *client_name;
|
||||
int client_fd;
|
||||
|
||||
char *(*encode_event)(mpv_event *event);
|
||||
@ -432,6 +434,15 @@ error:
|
||||
return output;
|
||||
}
|
||||
|
||||
static char *text_execute_command(struct client_arg *arg, bstr msg)
|
||||
{
|
||||
char *cmd_str = bstrdup0(NULL, msg);
|
||||
mpv_command_string(arg->client, cmd_str);
|
||||
talloc_free(cmd_str);
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static int ipc_write(int fd, const char *buf, size_t count)
|
||||
{
|
||||
while (count > 0) {
|
||||
@ -576,19 +587,10 @@ done:
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static void ipc_start_client(struct MPContext *mpctx, int id, int fd)
|
||||
static void ipc_start_client(struct MPContext *mpctx, struct client_arg *client)
|
||||
{
|
||||
struct client_arg *client = talloc_ptrtype(NULL, client);
|
||||
char *client_name = talloc_asprintf(client, "ipc-%d", id);
|
||||
*client = (struct client_arg){
|
||||
.client = mp_new_client(mpctx->clients, client_name),
|
||||
.client_fd = fd,
|
||||
|
||||
.encode_event = json_encode_event,
|
||||
.execute_command = json_execute_command,
|
||||
};
|
||||
|
||||
client->log = mp_client_get_log(client->client);
|
||||
client->client = mp_new_client(mpctx->clients, client->client_name),
|
||||
client->log = mp_client_get_log(client->client);
|
||||
|
||||
pthread_t client_thr;
|
||||
if (pthread_create(&client_thr, NULL, client_thread, client)) {
|
||||
@ -598,6 +600,45 @@ static void ipc_start_client(struct MPContext *mpctx, int id, int fd)
|
||||
}
|
||||
}
|
||||
|
||||
static void ipc_start_client_json(struct MPContext *mpctx, int id, int fd)
|
||||
{
|
||||
struct client_arg *client = talloc_ptrtype(NULL, client);
|
||||
*client = (struct client_arg){
|
||||
.client_name = talloc_asprintf(client, "ipc-%d", id),
|
||||
.client_fd = fd,
|
||||
|
||||
.encode_event = json_encode_event,
|
||||
.execute_command = json_execute_command,
|
||||
};
|
||||
|
||||
ipc_start_client(mpctx, client);
|
||||
}
|
||||
|
||||
static void ipc_start_client_text(struct MPContext *mpctx, const char *path)
|
||||
{
|
||||
int mode = O_RDONLY;
|
||||
// Use RDWR for FIFOs to ensure they stay open over multiple accesses.
|
||||
struct stat st;
|
||||
if (stat(path, &st) == 0 && S_ISFIFO(st.st_mode))
|
||||
mode = O_RDWR;
|
||||
int client_fd = open(path, mode);
|
||||
if (client_fd < 0) {
|
||||
MP_ERR(mpctx, "Could not open pipe at '%s'\n", path);
|
||||
return;
|
||||
}
|
||||
|
||||
struct client_arg *client = talloc_ptrtype(NULL, client);
|
||||
*client = (struct client_arg){
|
||||
.client_name = "input-file",
|
||||
.client_fd = client_fd,
|
||||
|
||||
.encode_event = NULL,
|
||||
.execute_command = text_execute_command,
|
||||
};
|
||||
|
||||
ipc_start_client(mpctx, client);
|
||||
}
|
||||
|
||||
static void *ipc_thread(void *p)
|
||||
{
|
||||
int rc;
|
||||
@ -668,7 +709,7 @@ static void *ipc_thread(void *p)
|
||||
goto done;
|
||||
}
|
||||
|
||||
ipc_start_client(arg->mpctx, client_num++, client_fd);
|
||||
ipc_start_client_json(arg->mpctx, client_num++, client_fd);
|
||||
}
|
||||
}
|
||||
|
||||
@ -688,6 +729,9 @@ done:
|
||||
|
||||
void mp_init_ipc(struct MPContext *mpctx)
|
||||
{
|
||||
if (mpctx->global->opts->input_file && *mpctx->global->opts->input_file)
|
||||
ipc_start_client_text(mpctx, mpctx->global->opts->input_file);
|
||||
|
||||
if (!mpctx->opts->ipc_path || !*mpctx->opts->ipc_path)
|
||||
return;
|
||||
|
||||
|
@ -1,59 +0,0 @@
|
||||
#include <stdio.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include <poll.h>
|
||||
|
||||
#include "common/msg.h"
|
||||
#include "osdep/io.h"
|
||||
#include "input.h"
|
||||
#include "cmd_parse.h"
|
||||
|
||||
static void read_pipe_thread(struct mp_input_src *src, void *param)
|
||||
{
|
||||
void *tmp = talloc_new(NULL);
|
||||
char *filename = talloc_strdup(tmp, param); // param deallocates after init
|
||||
int wakeup_fd = mp_input_src_get_wakeup_fd(src);
|
||||
int fd = -1;
|
||||
|
||||
struct mp_log *log = src->log;
|
||||
|
||||
int mode = O_RDONLY;
|
||||
// Use RDWR for FIFOs to ensure they stay open over multiple accesses.
|
||||
struct stat st;
|
||||
if (stat(filename, &st) == 0 && S_ISFIFO(st.st_mode))
|
||||
mode = O_RDWR;
|
||||
fd = open(filename, mode);
|
||||
if (fd < 0) {
|
||||
mp_err(log, "Can't open %s.\n", filename);
|
||||
goto done;
|
||||
}
|
||||
|
||||
mp_input_src_init_done(src);
|
||||
|
||||
while (1) {
|
||||
struct pollfd fds[2] = {
|
||||
{ .fd = fd, .events = POLLIN },
|
||||
{ .fd = wakeup_fd, .events = POLLIN },
|
||||
};
|
||||
poll(fds, 2, -1);
|
||||
if (!(fds[0].revents & POLLIN))
|
||||
break;
|
||||
char buffer[128];
|
||||
int r = read(fd, buffer, sizeof(buffer));
|
||||
if (r <= 0)
|
||||
break;
|
||||
mp_input_src_feed_cmd_text(src, buffer, r);
|
||||
}
|
||||
|
||||
done:
|
||||
close(fd);
|
||||
talloc_free(tmp);
|
||||
}
|
||||
|
||||
void mp_input_pipe_add(struct input_ctx *ictx, const char *filename)
|
||||
{
|
||||
mp_input_add_thread_src(ictx, (void *)filename, read_pipe_thread);
|
||||
}
|
@ -528,6 +528,7 @@ const m_option_t mp_opts[] = {
|
||||
OPT_FLAG("idle", player_idle_mode, M_OPT_GLOBAL),
|
||||
OPT_FLAG("input-terminal", consolecontrols, CONF_GLOBAL),
|
||||
|
||||
OPT_STRING("input-file", input_file, M_OPT_FILE | M_OPT_GLOBAL),
|
||||
OPT_STRING("input-unix-socket", ipc_path, M_OPT_FILE),
|
||||
|
||||
OPT_SUBSTRUCT("screenshot", screenshot_image_opts, image_writer_conf, 0),
|
||||
|
@ -286,6 +286,7 @@ typedef struct MPOpts {
|
||||
struct encode_opts *encode_opts;
|
||||
|
||||
char *ipc_path;
|
||||
char *input_file;
|
||||
} MPOpts;
|
||||
|
||||
extern const m_option_t mp_opts[];
|
||||
|
@ -191,7 +191,6 @@ def build(ctx):
|
||||
( "input/input.c" ),
|
||||
( "input/ipc.c", "!mingw" ),
|
||||
( "input/keycodes.c" ),
|
||||
( "input/pipe-unix.c", "!mingw" ),
|
||||
( "input/pipe-win32.c", "waio" ),
|
||||
( "input/joystick.c", "joystick" ),
|
||||
( "input/lirc.c", "lirc" ),
|
||||
|
Loading…
Reference in New Issue
Block a user