vf_uspp: drop this filter
[mplayer.git] / osdep / getch2-win.c
blobf1ccea6359da8a8f6c46f77b2fd82215744ff7bd
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 <stdint.h>
29 #include <string.h>
30 #include <windows.h>
31 #include "input/keycodes.h"
32 #include "input/input.h"
33 #include "mp_fifo.h"
34 #include "getch2.h"
36 int mp_input_slave_cmd_func(int fd,char* dest,int size){
37 DWORD retval;
38 HANDLE in = GetStdHandle(STD_INPUT_HANDLE);
39 if(PeekNamedPipe(in, NULL, size, &retval, NULL, NULL)){
40 if (size > retval) size = retval;
41 } else {
42 if (WaitForSingleObject(in, 0))
43 size = 0;
45 if(!size){
46 return MP_INPUT_NOTHING;
48 ReadFile(in, dest, size, &retval, NULL);
49 if(retval)return retval;
50 return MP_INPUT_NOTHING;
53 int screen_width=80;
54 int screen_height=24;
55 char * erase_to_end_of_line = NULL;
57 void get_screen_size(void){
58 CONSOLE_SCREEN_BUFFER_INFO cinfo;
59 if (GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cinfo))
61 screen_width = cinfo.dwMaximumWindowSize.X;
62 screen_height = cinfo.dwMaximumWindowSize.Y;
66 static HANDLE in;
67 static int getch2_status=0;
69 static int getch2_internal(void)
71 INPUT_RECORD eventbuffer[128];
72 DWORD retval;
73 int i=0;
74 if(!getch2_status){
75 // supports e.g. MinGW xterm, unfortunately keys are only received after
76 // enter was pressed.
77 uint8_t c;
78 if (!PeekNamedPipe(in, NULL, 1, &retval, NULL, NULL) || !retval)
79 return -1;
80 ReadFile(in, &c, 1, &retval, NULL);
81 return retval == 1 ? c : -1;
83 /*check if there are input events*/
84 if(!GetNumberOfConsoleInputEvents(in,&retval))
86 printf("getch2: can't get number of input events: %i\n",(int)GetLastError());
87 return -1;
89 if(retval<=0)return -1;
91 /*read all events*/
92 if(!ReadConsoleInput(in,eventbuffer,128,&retval))
94 printf("getch: can't read input events\n");
95 return -1;
98 /*filter out keyevents*/
99 for (i = 0; i < retval; i++)
101 switch(eventbuffer[i].EventType)
103 case KEY_EVENT:
104 /*only a pressed key is interresting for us*/
105 if(eventbuffer[i].Event.KeyEvent.bKeyDown == TRUE)
107 /*check for special keys*/
108 switch(eventbuffer[i].Event.KeyEvent.wVirtualKeyCode)
110 case VK_HOME:
111 return KEY_HOME;
112 case VK_END:
113 return KEY_END;
114 case VK_DELETE:
115 return KEY_DEL;
116 case VK_INSERT:
117 return KEY_INS;
118 case VK_BACK:
119 return KEY_BS;
120 case VK_PRIOR:
121 return KEY_PGUP;
122 case VK_NEXT:
123 return KEY_PGDWN;
124 case VK_RETURN:
125 return KEY_ENTER;
126 case VK_ESCAPE:
127 return KEY_ESC;
128 case VK_LEFT:
129 return KEY_LEFT;
130 case VK_UP:
131 return KEY_UP;
132 case VK_RIGHT:
133 return KEY_RIGHT;
134 case VK_DOWN:
135 return KEY_DOWN;
136 case VK_SHIFT:
137 continue;
139 /*check for function keys*/
140 if(0x87 >= eventbuffer[i].Event.KeyEvent.wVirtualKeyCode && eventbuffer[i].Event.KeyEvent.wVirtualKeyCode >= 0x70)
141 return KEY_F + 1 + eventbuffer[i].Event.KeyEvent.wVirtualKeyCode - 0x70;
143 /*only characters should be remaining*/
144 //printf("getch2: YOU PRESSED \"%c\" \n",eventbuffer[i].Event.KeyEvent.uChar.AsciiChar);
145 return eventbuffer[i].Event.KeyEvent.uChar.AsciiChar;
147 break;
149 case MOUSE_EVENT:
150 case WINDOW_BUFFER_SIZE_EVENT:
151 case FOCUS_EVENT:
152 case MENU_EVENT:
153 default:
154 //printf("getch2: unsupported event type");
155 break;
158 return -1;
161 bool getch2(struct mp_fifo *fifo)
163 int r = getch2_internal();
164 if (r >= 0)
165 mplayer_put_key(fifo, r);
166 return true;
169 void getch2_enable(void)
171 DWORD retval;
172 in = GetStdHandle(STD_INPUT_HANDLE);
173 if(!GetNumberOfConsoleInputEvents(in,&retval))
175 printf("getch2: %i can't get number of input events [disabling console input]\n",(int)GetLastError());
176 getch2_status = 0;
178 else getch2_status=1;
181 void getch2_disable(void)
183 if(!getch2_status) return; // already disabled / never enabled
184 getch2_status=0;
187 #ifdef CONFIG_ICONV
188 static const struct {
189 unsigned cp;
190 char* alias;
191 } cp_alias[] = {
192 { 20127, "ASCII" },
193 { 20866, "KOI8-R" },
194 { 21866, "KOI8-RU" },
195 { 28591, "ISO-8859-1" },
196 { 28592, "ISO-8859-2" },
197 { 28593, "ISO-8859-3" },
198 { 28594, "ISO-8859-4" },
199 { 28595, "ISO-8859-5" },
200 { 28596, "ISO-8859-6" },
201 { 28597, "ISO-8859-7" },
202 { 28598, "ISO-8859-8" },
203 { 28599, "ISO-8859-9" },
204 { 28605, "ISO-8859-15" },
205 { 65001, "UTF-8" },
206 { 0, NULL }
209 char* get_term_charset(void)
211 char codepage[10];
212 unsigned i, cpno = GetConsoleOutputCP();
213 if (!cpno)
214 cpno = GetACP();
215 if (!cpno)
216 return NULL;
218 for (i = 0; cp_alias[i].cp; i++)
219 if (cpno == cp_alias[i].cp)
220 return strdup(cp_alias[i].alias);
222 snprintf(codepage, sizeof(codepage), "CP%u", cpno);
223 return strdup(codepage);
225 #endif