lua: console: Fix output of non ascii characters on win32
[vlc.git] / modules / lua / libs / win.c
blob7d0276331e66337e8b623b6a81ab25d96394b5b5
1 /*****************************************************************************
2 * win.c: Windows specific functions
3 *****************************************************************************
4 * Copyright (C) 2007-2012 the VideoLAN team
5 * $Id$
7 * Authors: Antoine Cellerier <dionoea at videolan tod org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
25 * Preamble
26 *****************************************************************************/
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
31 #include <vlc_common.h>
32 #include <vlc_charset.h>
34 #include "../vlc.h"
35 #include "../libs.h"
37 #if !VLC_WINSTORE_APP
39 /* Based on modules/control/rc.c and include/vlc_interface.h */
40 static HANDLE GetConsole( lua_State *L )
42 /* Get the file descriptor of the console input */
43 HANDLE hConsoleIn = GetStdHandle(STD_INPUT_HANDLE);
44 if( hConsoleIn == INVALID_HANDLE_VALUE )
45 luaL_error( L, "couldn't find user input handle" );
46 return hConsoleIn;
49 #define MAX_LINE_LENGTH 1024
51 static bool ReadWin32( HANDLE *hConsoleIn, char *p_buffer, int *pi_size )
53 INPUT_RECORD input_record;
54 DWORD i_dw;
56 while( *pi_size < MAX_LINE_LENGTH &&
57 ReadConsoleInput( hConsoleIn, &input_record, 1, &i_dw ) )
59 if( input_record.EventType != KEY_EVENT ||
60 !input_record.Event.KeyEvent.bKeyDown ||
61 input_record.Event.KeyEvent.wVirtualKeyCode == VK_SHIFT ||
62 input_record.Event.KeyEvent.wVirtualKeyCode == VK_CONTROL||
63 input_record.Event.KeyEvent.wVirtualKeyCode == VK_MENU ||
64 input_record.Event.KeyEvent.wVirtualKeyCode == VK_CAPITAL )
66 /* nothing interesting */
67 continue;
70 p_buffer[ *pi_size ] = input_record.Event.KeyEvent.uChar.AsciiChar;
72 /* Echo out the command */
73 putc( p_buffer[ *pi_size ], stdout );
75 /* Handle special keys */
76 if( p_buffer[ *pi_size ] == '\r' || p_buffer[ *pi_size ] == '\n' )
78 if ( p_buffer[ *pi_size ] == '\r' )
79 p_buffer[ *pi_size ] = '\n';
80 (*pi_size)++; /* We want the \n to be in the output string */
81 putc( '\n', stdout );
82 break;
84 switch( p_buffer[ *pi_size ] )
86 case '\b':
87 if( *pi_size )
89 *pi_size -= 2;
90 putc( ' ', stdout );
91 putc( '\b', stdout );
93 break;
94 //case '\r':
95 // (*pi_size) --;
96 // break;
99 (*pi_size)++;
102 if( *pi_size == MAX_LINE_LENGTH )
103 // p_buffer[ *pi_size ] == '\r' || p_buffer[ *pi_size ] == '\n' )
105 p_buffer[ *pi_size ] = 0;
106 return true;
109 return false;
112 static int vlclua_console_init( lua_State *L )
114 (void)L;
115 //if ( !AllocConsole() )
116 // luaL_error( L, "failed to allocate windows console" );
117 AllocConsole();
119 freopen( "CONOUT$", "w", stdout );
120 freopen( "CONOUT$", "w", stderr );
121 freopen( "CONIN$", "r", stdin );
122 return 0;
125 static int vlclua_console_wait( lua_State *L )
127 int i_timeout = (int)luaL_optinteger( L, 1, 0 );
128 DWORD status = WaitForSingleObject( GetConsole( L ), i_timeout );
129 lua_pushboolean( L, status == WAIT_OBJECT_0 );
130 return 1;
134 static int vlclua_console_read( lua_State *L )
136 char psz_buffer[MAX_LINE_LENGTH+1];
137 int i_size = 0;
138 ReadWin32( GetConsole( L ), psz_buffer, &i_size );
139 lua_pushlstring( L, psz_buffer, i_size );
141 return 1;
144 static int vlclua_console_write( lua_State *L )
146 if( !lua_isstring( L, 1 ) )
147 return luaL_error( L, "win.console_write usage: (text)" );
148 const char* psz_line = luaL_checkstring( L, 1 );
149 utf8_fprintf( stdout, "%s", psz_line );
150 return 0;
153 /*****************************************************************************
155 *****************************************************************************/
156 static const luaL_Reg vlclua_win_reg[] = {
157 { "console_init", vlclua_console_init },
158 { "console_wait", vlclua_console_wait },
159 { "console_read", vlclua_console_read },
160 { "console_write", vlclua_console_write },
161 { NULL, NULL }
164 void luaopen_win( lua_State *L )
166 lua_newtable( L );
167 luaL_register( L, NULL, vlclua_win_reg );
168 lua_setfield( L, -2, "win" );
171 #endif /* !VLC_WINSTORE_APP */