Merge svn changes up to r28862
[mplayer.git] / osdep / getch2-win.c
blob5df87b23779c853283f42d26519a82a437de5cd5
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 <windows.h>
29 #include "keycodes.h"
30 #include "input/input.h"
31 #include "mp_fifo.h"
32 #include "getch2.h"
34 // HACK, stdin is used as something else below
35 #undef stdin
37 int mp_input_slave_cmd_func(int fd,char* dest,int size){
38 DWORD retval;
39 HANDLE stdin = GetStdHandle(STD_INPUT_HANDLE);
40 if(!PeekNamedPipe(stdin, NULL, size, &retval, NULL, NULL) || !retval){
41 return MP_INPUT_NOTHING;
43 if(retval>size)retval=size;
44 ReadFile(stdin, dest, retval, &retval, NULL);
45 if(retval)return retval;
46 return MP_INPUT_NOTHING;
49 int screen_width=80;
50 int screen_height=24;
51 char * erase_to_end_of_line = NULL;
53 void get_screen_size(void){
56 static HANDLE stdin;
57 static int getch2_status=0;
59 static int getch2_internal(void)
61 INPUT_RECORD eventbuffer[128];
62 DWORD retval;
63 int i=0;
64 if(!getch2_status)return -1;
65 /*check if there are input events*/
66 if(!GetNumberOfConsoleInputEvents(stdin,&retval))
68 printf("getch2: can't get number of input events: %i\n",GetLastError());
69 return -1;
71 if(retval<=0)return -1;
73 /*read all events*/
74 if(!ReadConsoleInput(stdin,eventbuffer,128,&retval))
76 printf("getch: can't read input events\n");
77 return -1;
80 /*filter out keyevents*/
81 for (i = 0; i < retval; i++)
83 switch(eventbuffer[i].EventType)
85 case KEY_EVENT:
86 /*only a pressed key is interresting for us*/
87 if(eventbuffer[i].Event.KeyEvent.bKeyDown == TRUE)
89 /*check for special keys*/
90 switch(eventbuffer[i].Event.KeyEvent.wVirtualKeyCode)
92 case VK_HOME:
93 return KEY_HOME;
94 case VK_END:
95 return KEY_END;
96 case VK_DELETE:
97 return KEY_DEL;
98 case VK_INSERT:
99 return KEY_INS;
100 case VK_BACK:
101 return KEY_BS;
102 case VK_PRIOR:
103 return KEY_PGUP;
104 case VK_NEXT:
105 return KEY_PGDWN;
106 case VK_RETURN:
107 return KEY_ENTER;
108 case VK_ESCAPE:
109 return KEY_ESC;
110 case VK_LEFT:
111 return KEY_LEFT;
112 case VK_UP:
113 return KEY_UP;
114 case VK_RIGHT:
115 return KEY_RIGHT;
116 case VK_DOWN:
117 return KEY_DOWN;
118 case VK_SHIFT:
119 continue;
121 /*check for function keys*/
122 if(0x87 >= eventbuffer[i].Event.KeyEvent.wVirtualKeyCode && eventbuffer[i].Event.KeyEvent.wVirtualKeyCode >= 0x70)
123 return KEY_F + 1 + eventbuffer[i].Event.KeyEvent.wVirtualKeyCode - 0x70;
125 /*only characters should be remaining*/
126 //printf("getch2: YOU PRESSED \"%c\" \n",eventbuffer[i].Event.KeyEvent.uChar.AsciiChar);
127 return eventbuffer[i].Event.KeyEvent.uChar.AsciiChar;
129 break;
131 case MOUSE_EVENT:
132 case WINDOW_BUFFER_SIZE_EVENT:
133 case FOCUS_EVENT:
134 case MENU_EVENT:
135 default:
136 //printf("getch2: unsupported event type");
137 break;
140 return -1;
143 void getch2(struct mp_fifo *fifo)
145 int r = getch2_internal();
146 if (r >= 0)
147 mplayer_put_key(fifo, r);
150 void getch2_enable(void)
152 DWORD retval;
153 stdin = GetStdHandle(STD_INPUT_HANDLE);
154 if(!GetNumberOfConsoleInputEvents(stdin,&retval))
156 printf("getch2: %i can't get number of input events [disabling console input]\n",GetLastError());
157 getch2_status = 0;
159 else getch2_status=1;
162 void getch2_disable(void)
164 if(!getch2_status) return; // already disabled / never enabled
165 getch2_status=0;
168 #ifdef CONFIG_ICONV
169 static const struct {
170 unsigned cp;
171 char* alias;
172 } cp_alias[] = {
173 { 20127, "ASCII" },
174 { 20866, "KOI8-R" },
175 { 21866, "KOI8-RU" },
176 { 28591, "ISO-8859-1" },
177 { 28592, "ISO-8859-2" },
178 { 28593, "ISO-8859-3" },
179 { 28594, "ISO-8859-4" },
180 { 28595, "ISO-8859-5" },
181 { 28596, "ISO-8859-6" },
182 { 28597, "ISO-8859-7" },
183 { 28598, "ISO-8859-8" },
184 { 28599, "ISO-8859-9" },
185 { 28605, "ISO-8859-15" },
186 { 65001, "UTF-8" },
187 { 0, NULL }
190 char* get_term_charset(void)
192 static char codepage[10];
193 unsigned i, cpno = GetConsoleOutputCP();
194 if (!cpno)
195 cpno = GetACP();
196 if (!cpno)
197 return NULL;
199 for (i = 0; cp_alias[i].cp; i++)
200 if (cpno == cp_alias[i].cp)
201 return cp_alias[i].alias;
203 snprintf(codepage, sizeof(codepage), "CP%u", cpno);
204 return codepage;
206 #endif