summaryrefslogtreecommitdiffstats
path: root/osdep/getch2-win.c
blob: 2fc8c0f44621f27c324a9ce2d1e3e677f5198db7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/* Windows TermIO
 *
 * copyright (C) 2003 Sascha Sommer
 *
 * This file is part of MPlayer.
 *
 * MPlayer is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * MPlayer is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

// See  http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/WinUI/WindowsUserInterface/UserInput/VirtualKeyCodes.asp
// for additional virtual keycodes


#include "config.h"
#include <stdio.h>
#include <windows.h>
#include "keycodes.h"
#include "input/input.h"
#include "mp_fifo.h"
// HACK, stdin is used as something else below
#undef stdin

int mp_input_slave_cmd_func(int fd,char* dest,int size){
  DWORD retval;
  HANDLE stdin = GetStdHandle(STD_INPUT_HANDLE);
  if(!PeekNamedPipe(stdin, NULL, size, &retval, NULL, NULL) || !retval){
	  return MP_INPUT_NOTHING;
  }
  if(retval>size)retval=size;
  ReadFile(stdin, dest, retval, &retval, NULL);
  if(retval)return retval;
  return MP_INPUT_NOTHING;
}

int screen_width=80;
int screen_height=24;
char * erase_to_end_of_line = NULL;

void get_screen_size(void){
}

static HANDLE stdin;
static int getch2_status=0;

static int getch2_internal(void)
{
	INPUT_RECORD eventbuffer[128];
    DWORD retval;
   	int i=0;
    if(!getch2_status)return -1;    
    /*check if there are input events*/
	if(!GetNumberOfConsoleInputEvents(stdin,&retval))
	{
		printf("getch2: can't get number of input events: %i\n",GetLastError());
		return -1;
	}
    if(retval<=0)return -1;
    
	/*read all events*/	
	if(!ReadConsoleInput(stdin,eventbuffer,128,&retval))
	{
		printf("getch: can't read input events\n");
		return -1;
	}
 
	/*filter out keyevents*/
    for (i = 0; i < retval; i++) 
    {
		switch(eventbuffer[i].EventType) 
		{
			case KEY_EVENT:
				/*only a pressed key is interresting for us*/
				if(eventbuffer[i].Event.KeyEvent.bKeyDown == TRUE)
				{
					/*check for special keys*/
					switch(eventbuffer[i].Event.KeyEvent.wVirtualKeyCode)
					{
					case VK_HOME:
						return KEY_HOME;
					case VK_END:
						return KEY_END;
					case VK_DELETE:
						return KEY_DEL;
                    case VK_INSERT:
						return KEY_INS;
					case VK_BACK:
						return KEY_BS;
					case VK_PRIOR:
						return KEY_PGUP;
					case VK_NEXT:
						return KEY_PGDWN;
					case VK_RETURN:
						return KEY_ENTER;
					case VK_ESCAPE:
						return KEY_ESC;
					case VK_LEFT:
						return KEY_LEFT;
					case VK_UP:
						return KEY_UP;
					case VK_RIGHT:
						return KEY_RIGHT;
					case VK_DOWN:
						return KEY_DOWN;
                    case VK_SHIFT:
                        continue;              
					}
					/*check for function keys*/
        			if(0x87 >= eventbuffer[i].Event.KeyEvent.wVirtualKeyCode && eventbuffer[i].Event.KeyEvent.wVirtualKeyCode >= 0x70)
						return KEY_F + 1 + eventbuffer[i].Event.KeyEvent.wVirtualKeyCode - 0x70;
 						
					/*only characters should be remaining*/
					//printf("getch2: YOU PRESSED \"%c\" \n",eventbuffer[i].Event.KeyEvent.uChar.AsciiChar); 
				    return eventbuffer[i].Event.KeyEvent.uChar.AsciiChar;
				}
				break; 
			
			case MOUSE_EVENT:
            case WINDOW_BUFFER_SIZE_EVENT: 
            case FOCUS_EVENT:  
            case MENU_EVENT:
            default:
				//printf("getch2: unsupported event type"); 
			    break; 
        } 
    }
	return -1;
}

void getch2(void)
{
    int r = getch2_internal();
    if (r >= 0)
	mplayer_put_key(r);
}

void getch2_enable(void)
{
	DWORD retval;
    stdin = GetStdHandle(STD_INPUT_HANDLE);
   	if(!GetNumberOfConsoleInputEvents(stdin,&retval))
	{
		printf("getch2: %i can't get number of input events  [disabling console input]\n",GetLastError());
		getch2_status = 0;
	}
    else getch2_status=1;
}

void getch2_disable(void)
{
    if(!getch2_status) return; // already disabled / never enabled
    getch2_status=0;
}

#ifdef CONFIG_ICONV
static const struct {
    unsigned cp;
    char* alias;
} cp_alias[] = {
    { 20127, "ASCII" },
    { 20866, "KOI8-R" },
    { 21866, "KOI8-RU" },
    { 28591, "ISO-8859-1" },
    { 28592, "ISO-8859-2" },
    { 28593, "ISO-8859-3" },
    { 28594, "ISO-8859-4" },
    { 28595, "ISO-8859-5" },
    { 28596, "ISO-8859-6" },
    { 28597, "ISO-8859-7" },
    { 28598, "ISO-8859-8" },
    { 28599, "ISO-8859-9" },
    { 28605, "ISO-8859-15" },
    { 65001, "UTF-8" },
    { 0, NULL }
};
 
char* get_term_charset(void)
{
    static char codepage[10];
    unsigned i, cpno = GetConsoleOutputCP();
    if (!cpno)
        cpno = GetACP();
    if (!cpno)
        return NULL;

    for (i = 0; cp_alias[i].cp; i++)
        if (cpno == cp_alias[i].cp)
            return cp_alias[i].alias;

    snprintf(codepage, sizeof(codepage), "CP%u", cpno);
    return codepage;
}
#endif