aboutsummaryrefslogtreecommitdiffhomepage
path: root/input/pipe-win32.c
blob: 4077ea84ee2134dc994b04b358e855200ad97090 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include <pthread.h>
#include <stdio.h>
#include <windows.h>
#include <io.h>

#include <stdint.h>
#include <waio/waio.h>

#include "common/msg.h"
#include "osdep/io.h"
#include "input.h"

static void request_cancel(struct mp_input_src *src)
{
    HANDLE terminate = src->priv;

    MP_VERBOSE(src, "Exiting...\n");
    SetEvent(terminate);
}

static void uninit(struct mp_input_src *src)
{
    HANDLE terminate = src->priv;

    CloseHandle(terminate);
    MP_VERBOSE(src, "Exited.\n");
}

static void read_pipe_thread(struct mp_input_src *src, void *param)
{
    char *filename = talloc_strdup(src, param);

    struct waio_cx_interface *waio = NULL;
    int mode = O_RDONLY;
    int fd = -1;
    bool close_fd = true;
    if (strcmp(filename, "/dev/stdin") == 0) { // for symmetry with unix
        fd = STDIN_FILENO;
        close_fd = false;
    }
    if (fd < 0)
        fd = open(filename, mode);
    if (fd < 0) {
        MP_ERR(src, "Can't open %s.\n", filename);
        goto done;
    }

    // If we're reading from stdin, unset it. All I/O on synchronous handles is
    // serialized, so stupid DLLs that call GetFileType on stdin can hang the
    // process if they do it while we're reading from it. At least, the
    // VirtualBox OpenGL ICD is affected by this, but only on Windows XP.
    // GetFileType works differently in later versions of Windows. See:
    // https://support.microsoft.com/kb/2009703
    // http://blogs.msdn.com/b/oldnewthing/archive/2011/12/02/10243553.aspx
    if ((void*)_get_osfhandle(fd) == GetStdHandle(STD_INPUT_HANDLE))
        SetStdHandle(STD_INPUT_HANDLE, NULL);

    waio = waio_alloc((void *)_get_osfhandle(fd), 0, NULL, NULL);
    if (!waio) {
        MP_ERR(src, "Can't initialize win32 file reader.\n");
        goto done;
    }

    HANDLE terminate = CreateEvent(NULL, TRUE, FALSE, NULL);
    if (!terminate)
        goto done;

    src->priv = terminate;
    src->cancel = request_cancel;
    src->uninit = uninit;
    mp_input_src_init_done(src);

    char buffer[128];
    struct waio_aiocb cb = {
        .aio_buf = buffer,
        .aio_nbytes = sizeof(buffer),
        .hsignal = terminate,
    };
    while (1) {
        if (waio_read(waio, &cb)) {
            MP_ERR(src, "Read operation failed.\n");
            break;
        }
        if (waio_suspend(waio, (const struct waio_aiocb *[]){&cb}, 1, NULL))
            break;
        ssize_t r = waio_return(waio, &cb);
        if (r <= 0)
            break; // EOF or error
        mp_input_src_feed_cmd_text(src, buffer, r);
    }

done:
    waio_free(waio);
    if (close_fd)
        close(fd);
}

void mp_input_pipe_add(struct input_ctx *ictx, const char *filename)
{
    mp_input_add_thread_src(ictx, (void *)filename, read_pipe_thread);
}