demux_mkv: use new EBML parser for Info parsing
[mplayer/kovensky.git] / osdep / getch2-win.c
blob9dae9cd91dbb164a8482ff6511c565fc66c397a3
1 /* Windows TermIO
3 * copyright (C) 2003 Sascha Sommer
5 * This file is part of MPlayer.
7 * MPlayer is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * MPlayer is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 // See http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/WinUI/WindowsUserInterface/UserInput/VirtualKeyCodes.asp
23 // for additional virtual keycodes
26 #include "config.h"
27 #include <stdio.h>
28 #include <string.h>
29 #include <windows.h>
30 #include "keycodes.h"
31 #include "input/input.h"
32 #include "mp_fifo.h"
33 #include "getch2.h"
35 // HACK, stdin is used as something else below
36 #undef stdin
38 int mp_input_slave_cmd_func(int fd,char* dest,int size){
39 DWORD retval;
40 HANDLE stdin = GetStdHandle(STD_INPUT_HANDLE);
41 if(!PeekNamedPipe(stdin, NULL, size, &retval, NULL, NULL) || !retval){
42 return MP_INPUT_NOTHING;
44 if(retval>size)retval=size;
45 ReadFile(stdin, dest, retval, &retval, NULL);
46 if(retval)return retval;
47 return MP_INPUT_NOTHING;
50 int screen_width=80;
51 int screen_height=24;
52 char * erase_to_end_of_line = NULL;
54 void get_screen_size(void){
57 static HANDLE stdin;
58 static int getch2_status=0;
60 static int getch2_internal(void)
62 INPUT_RECORD eventbuffer[128];
63 DWORD retval;
64 int i=0;
65 if(!getch2_status)return -1;
66 /*check if there are input events*/
67 if(!GetNumberOfConsoleInputEvents(stdin,&retval))
69 printf("getch2: can't get number of input events: %i\n",GetLastError());
70 return -1;
72 if(retval<=0)return -1;
74 /*read all events*/
75 if(!ReadConsoleInput(stdin,eventbuffer,128,&retval))
77 printf("getch: can't read input events\n");
78 return -1;
81 /*filter out keyevents*/
82 for (i = 0; i < retval; i++)
84 switch(eventbuffer[i].EventType)
86 case KEY_EVENT:
87 /*only a pressed key is interresting for us*/
88 if(eventbuffer[i].Event.KeyEvent.bKeyDown == TRUE)
90 /*check for special keys*/
91 switch(eventbuffer[i].Event.KeyEvent.wVirtualKeyCode)
93 case VK_HOME:
94 return KEY_HOME;
95 case VK_END:
96 return KEY_END;
97 case VK_DELETE:
98 return KEY_DEL;
99 case VK_INSERT:
100 return KEY_INS;
101 case VK_BACK:
102 return KEY_BS;
103 case VK_PRIOR:
104 return KEY_PGUP;
105 case VK_NEXT:
106 return KEY_PGDWN;
107 case VK_RETURN:
108 return KEY_ENTER;
109 case VK_ESCAPE:
110 return KEY_ESC;
111 case VK_LEFT:
112 return KEY_LEFT;
113 case VK_UP:
114 return KEY_UP;
115 case VK_RIGHT:
116 return KEY_RIGHT;
117 case VK_DOWN:
118 return KEY_DOWN;
119 case VK_SHIFT:
120 continue;
122 /*check for function keys*/
123 if(0x87 >= eventbuffer[i].Event.KeyEvent.wVirtualKeyCode && eventbuffer[i].Event.KeyEvent.wVirtualKeyCode >= 0x70)
124 return KEY_F + 1 + eventbuffer[i].Event.KeyEvent.wVirtualKeyCode - 0x70;
126 /*only characters should be remaining*/
127 //printf("getch2: YOU PRESSED \"%c\" \n",eventbuffer[i].Event.KeyEvent.uChar.AsciiChar);
128 return eventbuffer[i].Event.KeyEvent.uChar.AsciiChar;
130 break;
132 case MOUSE_EVENT:
133 case WINDOW_BUFFER_SIZE_EVENT:
134 case FOCUS_EVENT:
135 case MENU_EVENT:
136 default:
137 //printf("getch2: unsupported event type");
138 break;
141 return -1;
144 void getch2(struct mp_fifo *fifo)
146 int r = getch2_internal();
147 if (r >= 0)
148 mplayer_put_key(fifo, r);
151 void getch2_enable(void)
153 DWORD retval;
154 stdin = GetStdHandle(STD_INPUT_HANDLE);
155 if(!GetNumberOfConsoleInputEvents(stdin,&retval))
157 printf("getch2: %i can't get number of input events [disabling console input]\n",GetLastError());
158 getch2_status = 0;
160 else getch2_status=1;
163 void getch2_disable(void)
165 if(!getch2_status) return; // already disabled / never enabled
166 getch2_status=0;
169 #ifdef CONFIG_ICONV
170 static const struct {
171 unsigned cp;
172 char* alias;
173 } cp_alias[] = {
174 { 20127, "ASCII" },
175 { 20866, "KOI8-R" },
176 { 21866, "KOI8-RU" },
177 { 28591, "ISO-8859-1" },
178 { 28592, "ISO-8859-2" },
179 { 28593, "ISO-8859-3" },
180 { 28594, "ISO-8859-4" },
181 { 28595, "ISO-8859-5" },
182 { 28596, "ISO-8859-6" },
183 { 28597, "ISO-8859-7" },
184 { 28598, "ISO-8859-8" },
185 { 28599, "ISO-8859-9" },
186 { 28605, "ISO-8859-15" },
187 { 65001, "UTF-8" },
188 { 0, NULL }
191 char* get_term_charset(void)
193 char codepage[10];
194 unsigned i, cpno = GetConsoleOutputCP();
195 if (!cpno)
196 cpno = GetACP();
197 if (!cpno)
198 return NULL;
200 for (i = 0; cp_alias[i].cp; i++)
201 if (cpno == cp_alias[i].cp)
202 return strdup(cp_alias[i].alias);
204 snprintf(codepage, sizeof(codepage), "CP%u", cpno);
205 return strdup(codepage);
207 #endif