[MPlayer-dev-eng] [PATCH] Support locale under Windows console
Zuxy Meng
zuxy.meng at gmail.com
Thu Mar 1 05:39:20 CET 2007
Hi,
2007/1/27, Reimar Döffinger <Reimar.Doeffinger at stud.uni-karlsruhe.de>:
> Hello,
> On Sat, Jan 27, 2007 at 09:18:05PM +0800, Zuxy Meng wrote:
> > Thanks! What should I declare the function when it's moved to
> > osdep/getch2-win.c?
>
> You the function name? Maybe get_term_charset?
> And maybe move the nl_langinfo stuff under the same name into
> osdep/getch2.c?
> I.e. so that instead of
>
> > #ifdef USE_LANGINFO
> > if (!mp_msg_charset) {
> > setlocale(LC_CTYPE, "");
> > mp_msg_charset = nl_langinfo(CODESET);
> > setlocale(LC_CTYPE, "C");
> > }
> > #endif
>
> we would just have
>
> > if (!mp_msg_charset)
> > mp_msg_charset = get_term_charset();
>
> What do the others think about that idea?
Please review the updated patch:-)
--
Zuxy
Beauty is truth,
While truth is beauty.
PGP KeyID: E8555ED6
-------------- next part --------------
Index: mp_msg.c
===================================================================
--- mp_msg.c ?????? 22383??
+++ mp_msg.c ????????????
@@ -8,13 +8,10 @@
#include "config.h"
-#ifdef USE_LANGINFO
-#include <locale.h>
-#include <langinfo.h>
-#endif
#ifdef USE_ICONV
#include <iconv.h>
#include <errno.h>
+extern char* get_term_charset();
#endif
#if defined(FOR_MENCODER) || defined(CODECS2HTML)
@@ -79,14 +76,9 @@
mp_msg_levels[MSGT_IDENTIFY] = -1; // no -identify output by default
#ifdef USE_ICONV
mp_msg_charset = getenv("MPLAYER_CHARSET");
-#ifdef USE_LANGINFO
- if (!mp_msg_charset) {
- setlocale(LC_CTYPE, "");
- mp_msg_charset = nl_langinfo(CODESET);
- setlocale(LC_CTYPE, "C");
- }
+ if (!mp_msg_charset)
+ mp_msg_charset = get_term_charset();
#endif
-#endif
}
int mp_msg_test(int mod, int lev)
Index: osdep/getch2-win.c
===================================================================
--- osdep/getch2-win.c ?????? 22383??
+++ osdep/getch2-win.c ????????????
@@ -2,20 +2,21 @@
// See http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/WinUI/WindowsUserInterface/UserInput/VirtualKeyCodes.asp
// for additional virtual keycodes
+#include "config.h"
-
+#include <stdio.h>
#include <windows.h>
#include "keycodes.h"
#include "input/input.h"
int mp_input_win32_slave_cmd_func(int fd,char* dest,int size){
DWORD retval;
- HANDLE stdin = GetStdHandle(STD_INPUT_HANDLE);
- if(!PeekNamedPipe(stdin, NULL, size, &retval, NULL, NULL) || !retval){
+ HANDLE hd_stdin = GetStdHandle(STD_INPUT_HANDLE);
+ if(!PeekNamedPipe(hd_stdin, NULL, size, &retval, NULL, NULL) || !retval){
return MP_INPUT_NOTHING;
}
if(retval>size)retval=size;
- ReadFile(stdin, dest, retval, &retval, NULL);
+ ReadFile(hd_stdin, dest, retval, &retval, NULL);
if(retval)return retval;
return MP_INPUT_NOTHING;
}
@@ -27,7 +28,7 @@
void get_screen_size(){
}
-static HANDLE stdin;
+static HANDLE hd_stdin;
static int getch2_status=0;
int getch2(int time){
@@ -36,8 +37,8 @@
int i=0;
if(!getch2_status)return -1;
/*check if there are input events*/
- WaitForSingleObject(stdin, time);
- if(!GetNumberOfConsoleInputEvents(stdin,&retval))
+ WaitForSingleObject(hd_stdin, time);
+ if(!GetNumberOfConsoleInputEvents(hd_stdin,&retval))
{
printf("getch2: can't get number of input events: %i\n",GetLastError());
return -1;
@@ -45,7 +46,7 @@
if(retval<=0)return -1;
/*read all events*/
- if(!ReadConsoleInput(stdin,eventbuffer,128,&retval))
+ if(!ReadConsoleInput(hd_stdin,eventbuffer,128,&retval))
{
printf("getch: can't read input events\n");
return -1;
@@ -117,8 +118,8 @@
void getch2_enable(){
DWORD retval;
- stdin = GetStdHandle(STD_INPUT_HANDLE);
- if(!GetNumberOfConsoleInputEvents(stdin,&retval))
+ hd_stdin = GetStdHandle(STD_INPUT_HANDLE);
+ if(!GetNumberOfConsoleInputEvents(hd_stdin,&retval))
{
printf("getch2: %i can't get number of input events [disabling console input]\n",GetLastError());
getch2_status = 0;
@@ -131,3 +132,47 @@
getch2_status=0;
}
+#ifdef USE_ICONV
+char* get_term_charset()
+{
+ static char codepage[10];
+ const struct {
+ unsigned cp;
+ char* alias;
+ } cp_alias[] = {
+ { 20127, "ASCII" },
+ { 20866, "KOI8-R" },
+ { 21866, "KOI8-RU" },
+ { 28591, "ISO-8859-1" },
+ { 28592, "ISO-8859-2" },
+ { 28593, "ISO-8859-3" },
+ { 28594, "ISO-8859-4" },
+ { 28595, "ISO-8859-5" },
+ { 28596, "ISO-8859-6" },
+ { 28597, "ISO-8859-7" },
+ { 28598, "ISO-8859-8" },
+ { 28599, "ISO-8859-9" },
+ { 28605, "ISO-8859-15" },
+ { 65001, "UTF-8" },
+ { 0, NULL }
+ };
+ unsigned cpno = GetConsoleOutputCP();
+ if (!cpno)
+ cpno = GetACP();
+ if (!cpno)
+ return NULL;
+
+ if (cpno >= cp_alias[0].cp) {
+ unsigned cur_cp, i = 0;
+ while ((cur_cp = cp_alias[i].cp)) {
+ if (cpno == cur_cp) {
+ return cp_alias[i].alias;
+ }
+ i++;
+ }
+ }
+
+ snprintf(codepage, sizeof(codepage), "CP%u", cpno);
+ return codepage;
+}
+#endif
Index: osdep/getch2.c
===================================================================
--- osdep/getch2.c ?????? 22383??
+++ osdep/getch2.c ????????????
@@ -28,6 +28,11 @@
#endif
#endif
+#if defined(USE_LANGINFO) && defined(USE_ICONV)
+#include <locale.h>
+#include <langinfo.h>
+#endif
+
#include <unistd.h>
#include "keycodes.h"
@@ -238,3 +243,16 @@
getch2_status=0;
}
+#ifdef USE_ICONV
+char* get_term_charset()
+{
+ char* charset = NULL;
+#ifdef USE_LANGINFO
+ setlocale(LC_CTYPE, "");
+ charset = nl_langinfo(CODESET);
+ setlocale(LC_CTYPE, "C");
+#endif
+ return charset;
+}
+#endif
+
More information about the MPlayer-dev-eng
mailing list