Apply patch for oCERT #2008-013 / CVE-2008-3827
[mplayer/glamo.git] / osdep / getch2-win.c
blob83d502f7c76ff6c87615c6eee705509fec181a99
1 /* windows TermIO for MPlayer (C) 2003 Sascha Sommer */
3 // See http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/WinUI/WindowsUserInterface/UserInput/VirtualKeyCodes.asp
4 // for additional virtual keycodes
7 #include "config.h"
8 #include <stdio.h>
9 #include <windows.h>
10 #include "keycodes.h"
11 #include "input/input.h"
12 #include "mp_fifo.h"
13 // HACK, stdin is used as something else below
14 #undef stdin
16 int mp_input_slave_cmd_func(int fd,char* dest,int size){
17 DWORD retval;
18 HANDLE stdin = GetStdHandle(STD_INPUT_HANDLE);
19 if(!PeekNamedPipe(stdin, NULL, size, &retval, NULL, NULL) || !retval){
20 return MP_INPUT_NOTHING;
22 if(retval>size)retval=size;
23 ReadFile(stdin, dest, retval, &retval, NULL);
24 if(retval)return retval;
25 return MP_INPUT_NOTHING;
28 int screen_width=80;
29 int screen_height=24;
30 char * erase_to_end_of_line = NULL;
32 void get_screen_size(){
35 static HANDLE stdin;
36 static int getch2_status=0;
38 static int getch2_internal(void)
40 INPUT_RECORD eventbuffer[128];
41 DWORD retval;
42 int i=0;
43 if(!getch2_status)return -1;
44 /*check if there are input events*/
45 if(!GetNumberOfConsoleInputEvents(stdin,&retval))
47 printf("getch2: can't get number of input events: %i\n",GetLastError());
48 return -1;
50 if(retval<=0)return -1;
52 /*read all events*/
53 if(!ReadConsoleInput(stdin,eventbuffer,128,&retval))
55 printf("getch: can't read input events\n");
56 return -1;
59 /*filter out keyevents*/
60 for (i = 0; i < retval; i++)
62 switch(eventbuffer[i].EventType)
64 case KEY_EVENT:
65 /*only a pressed key is interresting for us*/
66 if(eventbuffer[i].Event.KeyEvent.bKeyDown == TRUE)
68 /*check for special keys*/
69 switch(eventbuffer[i].Event.KeyEvent.wVirtualKeyCode)
71 case VK_HOME:
72 return KEY_HOME;
73 case VK_END:
74 return KEY_END;
75 case VK_DELETE:
76 return KEY_DEL;
77 case VK_INSERT:
78 return KEY_INS;
79 case VK_BACK:
80 return KEY_BS;
81 case VK_PRIOR:
82 return KEY_PGUP;
83 case VK_NEXT:
84 return KEY_PGDWN;
85 case VK_RETURN:
86 return KEY_ENTER;
87 case VK_ESCAPE:
88 return KEY_ESC;
89 case VK_LEFT:
90 return KEY_LEFT;
91 case VK_UP:
92 return KEY_UP;
93 case VK_RIGHT:
94 return KEY_RIGHT;
95 case VK_DOWN:
96 return KEY_DOWN;
97 case VK_SHIFT:
98 continue;
100 /*check for function keys*/
101 if(0x87 >= eventbuffer[i].Event.KeyEvent.wVirtualKeyCode && eventbuffer[i].Event.KeyEvent.wVirtualKeyCode >= 0x70)
102 return KEY_F + 1 + eventbuffer[i].Event.KeyEvent.wVirtualKeyCode - 0x70;
104 /*only characters should be remaining*/
105 //printf("getch2: YOU PRESSED \"%c\" \n",eventbuffer[i].Event.KeyEvent.uChar.AsciiChar);
106 return eventbuffer[i].Event.KeyEvent.uChar.AsciiChar;
108 break;
110 case MOUSE_EVENT:
111 case WINDOW_BUFFER_SIZE_EVENT:
112 case FOCUS_EVENT:
113 case MENU_EVENT:
114 default:
115 //printf("getch2: unsupported event type");
116 break;
119 return -1;
122 void getch2(void)
124 int r = getch2_internal();
125 if (r >= 0)
126 mplayer_put_key(r);
129 void getch2_enable(){
130 DWORD retval;
131 stdin = GetStdHandle(STD_INPUT_HANDLE);
132 if(!GetNumberOfConsoleInputEvents(stdin,&retval))
134 printf("getch2: %i can't get number of input events [disabling console input]\n",GetLastError());
135 getch2_status = 0;
137 else getch2_status=1;
140 void getch2_disable(){
141 if(!getch2_status) return; // already disabled / never enabled
142 getch2_status=0;
145 #ifdef CONFIG_ICONV
146 static const struct {
147 unsigned cp;
148 char* alias;
149 } cp_alias[] = {
150 { 20127, "ASCII" },
151 { 20866, "KOI8-R" },
152 { 21866, "KOI8-RU" },
153 { 28591, "ISO-8859-1" },
154 { 28592, "ISO-8859-2" },
155 { 28593, "ISO-8859-3" },
156 { 28594, "ISO-8859-4" },
157 { 28595, "ISO-8859-5" },
158 { 28596, "ISO-8859-6" },
159 { 28597, "ISO-8859-7" },
160 { 28598, "ISO-8859-8" },
161 { 28599, "ISO-8859-9" },
162 { 28605, "ISO-8859-15" },
163 { 65001, "UTF-8" },
164 { 0, NULL }
167 char* get_term_charset(void)
169 static char codepage[10];
170 unsigned i, cpno = GetConsoleOutputCP();
171 if (!cpno)
172 cpno = GetACP();
173 if (!cpno)
174 return NULL;
176 for (i = 0; cp_alias[i].cp; i++)
177 if (cpno == cp_alias[i].cp)
178 return cp_alias[i].alias;
180 snprintf(codepage, sizeof(codepage), "CP%u", cpno);
181 return codepage;
183 #endif