1 /*****************************************************************************
2 * win.c: Windows specific functions
3 *****************************************************************************
4 * Copyright (C) 2007-2012 the VideoLAN team
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 /*****************************************************************************
26 *****************************************************************************/
31 #include <vlc_common.h>
32 #include <vlc_charset.h>
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" );
49 #define MAX_LINE_LENGTH 1024
51 static bool ReadWin32( HANDLE
*hConsoleIn
, char *p_buffer
, int *pi_size
)
53 INPUT_RECORD input_record
;
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 */
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 */
84 switch( p_buffer
[ *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;
112 static int vlclua_console_init( lua_State
*L
)
115 //if ( !AllocConsole() )
116 // luaL_error( L, "failed to allocate windows console" );
119 freopen( "CONOUT$", "w", stdout
);
120 freopen( "CONOUT$", "w", stderr
);
121 freopen( "CONIN$", "r", stdin
);
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
);
134 static int vlclua_console_read( lua_State
*L
)
136 char psz_buffer
[MAX_LINE_LENGTH
+1];
138 ReadWin32( GetConsole( L
), psz_buffer
, &i_size
);
139 lua_pushlstring( L
, psz_buffer
, i_size
);
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
);
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
},
164 void luaopen_win( lua_State
*L
)
167 luaL_register( L
, NULL
, vlclua_win_reg
);
168 lua_setfield( L
, -2, "win" );
171 #endif /* !VLC_WINSTORE_APP */