2015-12-07 07:51:33 +00:00
|
|
|
/*
|
|
|
|
* This file is part of mpv.
|
|
|
|
*
|
|
|
|
* mpv is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* mpv is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with mpv. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2014-09-14 14:21:04 +00:00
|
|
|
#include <windows.h>
|
|
|
|
#include <io.h>
|
|
|
|
|
|
|
|
#include "common/msg.h"
|
2016-09-07 09:26:25 +00:00
|
|
|
#include "osdep/atomic.h"
|
2014-09-14 14:21:04 +00:00
|
|
|
#include "osdep/io.h"
|
|
|
|
#include "input.h"
|
|
|
|
|
2015-12-07 07:51:33 +00:00
|
|
|
struct priv {
|
|
|
|
atomic_bool cancel_requested;
|
|
|
|
int fd;
|
|
|
|
bool close_fd;
|
|
|
|
HANDLE file;
|
|
|
|
HANDLE thread;
|
|
|
|
};
|
|
|
|
|
2014-09-14 14:21:04 +00:00
|
|
|
static void request_cancel(struct mp_input_src *src)
|
|
|
|
{
|
2015-12-07 07:51:33 +00:00
|
|
|
struct priv *p = src->priv;
|
2014-09-14 14:21:04 +00:00
|
|
|
|
|
|
|
MP_VERBOSE(src, "Exiting...\n");
|
2015-12-07 07:51:33 +00:00
|
|
|
atomic_store(&p->cancel_requested, true);
|
|
|
|
|
|
|
|
// The thread might not be peforming I/O at the exact moment when
|
|
|
|
// CancelIoEx is called, so call it in a loop until it succeeds or the
|
|
|
|
// thread exits
|
|
|
|
do {
|
|
|
|
if (CancelIoEx(p->file, NULL))
|
|
|
|
break;
|
|
|
|
} while (WaitForSingleObject(p->thread, 1) != WAIT_OBJECT_0);
|
2014-09-14 14:21:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void uninit(struct mp_input_src *src)
|
|
|
|
{
|
2015-12-07 07:51:33 +00:00
|
|
|
struct priv *p = src->priv;
|
|
|
|
|
|
|
|
CloseHandle(p->thread);
|
|
|
|
if (p->close_fd)
|
|
|
|
close(p->fd);
|
2014-09-14 14:21:04 +00:00
|
|
|
|
|
|
|
MP_VERBOSE(src, "Exited.\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
static void read_pipe_thread(struct mp_input_src *src, void *param)
|
|
|
|
{
|
|
|
|
char *filename = talloc_strdup(src, param);
|
2015-12-07 07:51:33 +00:00
|
|
|
struct priv *p = talloc_zero(src, struct priv);
|
2014-09-14 14:21:04 +00:00
|
|
|
|
2015-12-07 07:51:33 +00:00
|
|
|
p->fd = -1;
|
|
|
|
p->close_fd = true;
|
2014-09-14 14:21:04 +00:00
|
|
|
if (strcmp(filename, "/dev/stdin") == 0) { // for symmetry with unix
|
2015-12-07 07:51:33 +00:00
|
|
|
p->fd = STDIN_FILENO;
|
|
|
|
p->close_fd = false;
|
2014-09-14 14:21:04 +00:00
|
|
|
}
|
2015-12-07 07:51:33 +00:00
|
|
|
if (p->fd < 0)
|
|
|
|
p->fd = open(filename, O_RDONLY);
|
|
|
|
if (p->fd < 0) {
|
2014-09-14 14:21:04 +00:00
|
|
|
MP_ERR(src, "Can't open %s.\n", filename);
|
2015-12-07 07:51:33 +00:00
|
|
|
return;
|
2014-09-14 14:21:04 +00:00
|
|
|
}
|
|
|
|
|
2015-12-07 07:51:33 +00:00
|
|
|
p->file = (HANDLE)_get_osfhandle(p->fd);
|
|
|
|
if (!p->file || p->file == INVALID_HANDLE_VALUE) {
|
|
|
|
MP_ERR(src, "Can't open %s.\n", filename);
|
|
|
|
return;
|
2014-09-14 14:21:04 +00:00
|
|
|
}
|
|
|
|
|
2015-12-07 07:51:33 +00:00
|
|
|
atomic_store(&p->cancel_requested, false);
|
|
|
|
if (!DuplicateHandle(GetCurrentProcess(), GetCurrentThread(),
|
|
|
|
GetCurrentProcess(), &p->thread, SYNCHRONIZE, FALSE, 0))
|
|
|
|
return;
|
2014-09-14 14:21:04 +00:00
|
|
|
|
2015-12-07 07:51:33 +00:00
|
|
|
src->priv = p;
|
2014-09-14 14:21:04 +00:00
|
|
|
src->cancel = request_cancel;
|
|
|
|
src->uninit = uninit;
|
|
|
|
mp_input_src_init_done(src);
|
|
|
|
|
2015-12-07 07:51:33 +00:00
|
|
|
char buffer[4096];
|
|
|
|
while (!atomic_load(&p->cancel_requested)) {
|
|
|
|
DWORD r;
|
|
|
|
if (!ReadFile(p->file, buffer, 4096, &r, NULL)) {
|
|
|
|
if (GetLastError() != ERROR_OPERATION_ABORTED)
|
|
|
|
MP_ERR(src, "Read operation failed.\n");
|
2014-09-14 14:21:04 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
mp_input_src_feed_cmd_text(src, buffer, r);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void mp_input_pipe_add(struct input_ctx *ictx, const char *filename)
|
|
|
|
{
|
|
|
|
mp_input_add_thread_src(ictx, (void *)filename, read_pipe_thread);
|
|
|
|
}
|