mp_fifo.c, osdep/: Include corresponding .h in .c files
[mplayer.git] / osdep / getch2-win.c
bloba93978426692e38addea720738026332f9826bd8
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 #include "getch2.h"
15 // HACK, stdin is used as something else below
16 #undef stdin
18 int mp_input_slave_cmd_func(int fd,char* dest,int size){
19 DWORD retval;
20 HANDLE stdin = GetStdHandle(STD_INPUT_HANDLE);
21 if(!PeekNamedPipe(stdin, NULL, size, &retval, NULL, NULL) || !retval){
22 return MP_INPUT_NOTHING;
24 if(retval>size)retval=size;
25 ReadFile(stdin, dest, retval, &retval, NULL);
26 if(retval)return retval;
27 return MP_INPUT_NOTHING;
30 int screen_width=80;
31 int screen_height=24;
32 char * erase_to_end_of_line = NULL;
34 void get_screen_size(){
37 static HANDLE stdin;
38 static int getch2_status=0;
40 static int getch2_internal(void)
42 INPUT_RECORD eventbuffer[128];
43 DWORD retval;
44 int i=0;
45 if(!getch2_status)return -1;
46 /*check if there are input events*/
47 if(!GetNumberOfConsoleInputEvents(stdin,&retval))
49 printf("getch2: can't get number of input events: %i\n",GetLastError());
50 return -1;
52 if(retval<=0)return -1;
54 /*read all events*/
55 if(!ReadConsoleInput(stdin,eventbuffer,128,&retval))
57 printf("getch: can't read input events\n");
58 return -1;
61 /*filter out keyevents*/
62 for (i = 0; i < retval; i++)
64 switch(eventbuffer[i].EventType)
66 case KEY_EVENT:
67 /*only a pressed key is interresting for us*/
68 if(eventbuffer[i].Event.KeyEvent.bKeyDown == TRUE)
70 /*check for special keys*/
71 switch(eventbuffer[i].Event.KeyEvent.wVirtualKeyCode)
73 case VK_HOME:
74 return KEY_HOME;
75 case VK_END:
76 return KEY_END;
77 case VK_DELETE:
78 return KEY_DEL;
79 case VK_INSERT:
80 return KEY_INS;
81 case VK_BACK:
82 return KEY_BS;
83 case VK_PRIOR:
84 return KEY_PGUP;
85 case VK_NEXT:
86 return KEY_PGDWN;
87 case VK_RETURN:
88 return KEY_ENTER;
89 case VK_ESCAPE:
90 return KEY_ESC;
91 case VK_LEFT:
92 return KEY_LEFT;
93 case VK_UP:
94 return KEY_UP;
95 case VK_RIGHT:
96 return KEY_RIGHT;
97 case VK_DOWN:
98 return KEY_DOWN;
99 case VK_SHIFT:
100 continue;
102 /*check for function keys*/
103 if(0x87 >= eventbuffer[i].Event.KeyEvent.wVirtualKeyCode && eventbuffer[i].Event.KeyEvent.wVirtualKeyCode >= 0x70)
104 return (KEY_F + 1 + eventbuffer[i].Event.KeyEvent.wVirtualKeyCode - 0x70);
106 /*only characters should be remaining*/
107 //printf("getch2: YOU PRESSED \"%c\" \n",eventbuffer[i].Event.KeyEvent.uChar.AsciiChar);
108 return eventbuffer[i].Event.KeyEvent.uChar.AsciiChar;
110 break;
112 case MOUSE_EVENT:
113 case WINDOW_BUFFER_SIZE_EVENT:
114 case FOCUS_EVENT:
115 case MENU_EVENT:
116 default:
117 //printf("getch2: unsupported event type");
118 break;
121 return -1;
124 void getch2(void)
126 int r = getch2_internal();
127 if (r >= 0)
128 mplayer_put_key(r);
131 void getch2_enable(){
132 DWORD retval;
133 stdin = GetStdHandle(STD_INPUT_HANDLE);
134 if(!GetNumberOfConsoleInputEvents(stdin,&retval))
136 printf("getch2: %i can't get number of input events [disabling console input]\n",GetLastError());
137 getch2_status = 0;
139 else getch2_status=1;
142 void getch2_disable(){
143 if(!getch2_status) return; // already disabled / never enabled
144 getch2_status=0;
147 #ifdef USE_ICONV
148 static const struct {
149 unsigned cp;
150 char* alias;
151 } cp_alias[] = {
152 { 20127, "ASCII" },
153 { 20866, "KOI8-R" },
154 { 21866, "KOI8-RU" },
155 { 28591, "ISO-8859-1" },
156 { 28592, "ISO-8859-2" },
157 { 28593, "ISO-8859-3" },
158 { 28594, "ISO-8859-4" },
159 { 28595, "ISO-8859-5" },
160 { 28596, "ISO-8859-6" },
161 { 28597, "ISO-8859-7" },
162 { 28598, "ISO-8859-8" },
163 { 28599, "ISO-8859-9" },
164 { 28605, "ISO-8859-15" },
165 { 65001, "UTF-8" },
166 { 0, NULL }
169 char* get_term_charset(void)
171 static char codepage[10];
172 unsigned i, cpno = GetConsoleOutputCP();
173 if (!cpno)
174 cpno = GetACP();
175 if (!cpno)
176 return NULL;
178 for (i = 0; cp_alias[i].cp; i++)
179 if (cpno == cp_alias[i].cp)
180 return cp_alias[i].alias;
182 snprintf(codepage, sizeof(codepage), "CP%u", cpno);
183 return codepage;
185 #endif