1 /* Copyright (C) 2010-2020 The RetroArch team
3 * ---------------------------------------------------------------------------------------
4 * The following license statement only applies to this file (stdin_stream.c).
5 * ---------------------------------------------------------------------------------------
7 * Permission is hereby granted, free of charge,
8 * to any person obtaining a copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation the rights to
10 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
11 * and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
16 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
19 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
38 #include <retro_environment.h>
39 #include <streams/stdin_stream.h>
41 #if (defined(_WIN32) && defined(_XBOX)) || defined(__WINRT__) || !defined(__PSL1GHT__) && defined(__PS3__)
42 size_t read_stdin(char *buf, size_t size)
44 /* Not implemented. */
48 size_t read_stdin(char *buf, size_t size)
54 HANDLE hnd = GetStdHandle(STD_INPUT_HANDLE);
56 if (hnd == INVALID_HANDLE_VALUE)
59 /* Check first if we're a pipe
62 /* If not a pipe, check if we're running in a console. */
63 if (!PeekNamedPipe(hnd, NULL, 0, NULL, &avail, NULL))
65 INPUT_RECORD recs[256];
70 if (!GetConsoleMode(hnd, &mode))
73 if ((mode & (ENABLE_LINE_INPUT | ENABLE_ECHO_INPUT))
74 && !SetConsoleMode(hnd,
75 mode & ~(ENABLE_LINE_INPUT | ENABLE_ECHO_INPUT)))
78 /* Win32, Y U NO SANE NONBLOCK READ!? */
79 if (!PeekConsoleInput(hnd, recs,
80 sizeof(recs) / sizeof(recs[0]), &has_read))
83 for (i = 0; i < has_read; i++)
85 /* Very crude, but should get the job done. */
86 if (recs[i].EventType == KEY_EVENT &&
87 recs[i].Event.KeyEvent.bKeyDown &&
88 (isgraph(recs[i].Event.KeyEvent.wVirtualKeyCode) ||
89 recs[i].Event.KeyEvent.wVirtualKeyCode == VK_RETURN))
100 FlushConsoleInputBuffer(hnd);
111 if (!ReadFile(hnd, buf, avail, &has_read, NULL))
114 for (i = 0; i < has_read; i++)
118 /* Console won't echo for us while in non-line mode,
119 * so do it manually ... */
122 HANDLE hnd_out = GetStdHandle(STD_OUTPUT_HANDLE);
123 if (hnd_out != INVALID_HANDLE_VALUE)
126 WriteConsole(hnd_out, buf, has_read, &has_written, NULL);
133 size_t read_stdin(char *buf, size_t size)
139 ssize_t ret = read(STDIN_FILENO, buf, size);