annotate osdep/getch2-win.c @ 23768:b555c8626c34

Use native Windows thread API when pthreads isn't available.
author zuxy
date Mon, 16 Jul 2007 06:34:51 +0000
parents 71b3e04d0555
children 50e1e79056b6
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
9766
afb1150cd5de windows getch2 for MINGW32 port
faust3
parents:
diff changeset
1 /* windows TermIO for MPlayer (C) 2003 Sascha Sommer */
afb1150cd5de windows getch2 for MINGW32 port
faust3
parents:
diff changeset
2
afb1150cd5de windows getch2 for MINGW32 port
faust3
parents:
diff changeset
3 // See http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/WinUI/WindowsUserInterface/UserInput/VirtualKeyCodes.asp
afb1150cd5de windows getch2 for MINGW32 port
faust3
parents:
diff changeset
4 // for additional virtual keycodes
afb1150cd5de windows getch2 for MINGW32 port
faust3
parents:
diff changeset
5
afb1150cd5de windows getch2 for MINGW32 port
faust3
parents:
diff changeset
6
22442
56a0b0f8a66e Add code to detect and convert to console codepage on Windows.
reimar
parents: 22441
diff changeset
7 #include "config.h"
22441
6f183c95aafc Include stdio.h for printf
reimar
parents: 18107
diff changeset
8 #include <stdio.h>
9766
afb1150cd5de windows getch2 for MINGW32 port
faust3
parents:
diff changeset
9 #include <windows.h>
afb1150cd5de windows getch2 for MINGW32 port
faust3
parents:
diff changeset
10 #include "keycodes.h"
16985
08cac43f1e38 Unify include paths, -I.. is in CFLAGS.
diego
parents: 13015
diff changeset
11 #include "input/input.h"
22441
6f183c95aafc Include stdio.h for printf
reimar
parents: 18107
diff changeset
12 // HACK, stdin is used as something else below
6f183c95aafc Include stdio.h for printf
reimar
parents: 18107
diff changeset
13 #undef stdin
10928
f2313bd91594 add a read function for slave mode on mingw
faust3
parents: 10230
diff changeset
14
f2313bd91594 add a read function for slave mode on mingw
faust3
parents: 10230
diff changeset
15 int mp_input_win32_slave_cmd_func(int fd,char* dest,int size){
13015
00a4cf87e2ff fix slave mode for mingw, patch by Anton Ragnarsson <anton.ragnarsson.1093 at student.uu.se> some cleanup by be
faust3
parents: 10928
diff changeset
16 DWORD retval;
10928
f2313bd91594 add a read function for slave mode on mingw
faust3
parents: 10230
diff changeset
17 HANDLE stdin = GetStdHandle(STD_INPUT_HANDLE);
13015
00a4cf87e2ff fix slave mode for mingw, patch by Anton Ragnarsson <anton.ragnarsson.1093 at student.uu.se> some cleanup by be
faust3
parents: 10928
diff changeset
18 if(!PeekNamedPipe(stdin, NULL, size, &retval, NULL, NULL) || !retval){
00a4cf87e2ff fix slave mode for mingw, patch by Anton Ragnarsson <anton.ragnarsson.1093 at student.uu.se> some cleanup by be
faust3
parents: 10928
diff changeset
19 return MP_INPUT_NOTHING;
10928
f2313bd91594 add a read function for slave mode on mingw
faust3
parents: 10230
diff changeset
20 }
13015
00a4cf87e2ff fix slave mode for mingw, patch by Anton Ragnarsson <anton.ragnarsson.1093 at student.uu.se> some cleanup by be
faust3
parents: 10928
diff changeset
21 if(retval>size)retval=size;
00a4cf87e2ff fix slave mode for mingw, patch by Anton Ragnarsson <anton.ragnarsson.1093 at student.uu.se> some cleanup by be
faust3
parents: 10928
diff changeset
22 ReadFile(stdin, dest, retval, &retval, NULL);
00a4cf87e2ff fix slave mode for mingw, patch by Anton Ragnarsson <anton.ragnarsson.1093 at student.uu.se> some cleanup by be
faust3
parents: 10928
diff changeset
23 if(retval)return retval;
10928
f2313bd91594 add a read function for slave mode on mingw
faust3
parents: 10230
diff changeset
24 return MP_INPUT_NOTHING;
f2313bd91594 add a read function for slave mode on mingw
faust3
parents: 10230
diff changeset
25 }
9766
afb1150cd5de windows getch2 for MINGW32 port
faust3
parents:
diff changeset
26
afb1150cd5de windows getch2 for MINGW32 port
faust3
parents:
diff changeset
27 int screen_width=80;
afb1150cd5de windows getch2 for MINGW32 port
faust3
parents:
diff changeset
28 int screen_height=24;
17258
3d02f6e2a432 change erase to end of line, fall back to old behavior if no termcap found
ods15
parents: 17251
diff changeset
29 char * erase_to_end_of_line = NULL;
9766
afb1150cd5de windows getch2 for MINGW32 port
faust3
parents:
diff changeset
30
afb1150cd5de windows getch2 for MINGW32 port
faust3
parents:
diff changeset
31 void get_screen_size(){
afb1150cd5de windows getch2 for MINGW32 port
faust3
parents:
diff changeset
32 }
afb1150cd5de windows getch2 for MINGW32 port
faust3
parents:
diff changeset
33
afb1150cd5de windows getch2 for MINGW32 port
faust3
parents:
diff changeset
34 static HANDLE stdin;
9936
6d9531a5d95b disable getch2 on windows when we can't read stdin (msys)
faust3
parents: 9766
diff changeset
35 static int getch2_status=0;
9766
afb1150cd5de windows getch2 for MINGW32 port
faust3
parents:
diff changeset
36
afb1150cd5de windows getch2 for MINGW32 port
faust3
parents:
diff changeset
37 int getch2(int time){
afb1150cd5de windows getch2 for MINGW32 port
faust3
parents:
diff changeset
38 INPUT_RECORD eventbuffer[128];
afb1150cd5de windows getch2 for MINGW32 port
faust3
parents:
diff changeset
39 DWORD retval;
afb1150cd5de windows getch2 for MINGW32 port
faust3
parents:
diff changeset
40 int i=0;
9936
6d9531a5d95b disable getch2 on windows when we can't read stdin (msys)
faust3
parents: 9766
diff changeset
41 if(!getch2_status)return -1;
9766
afb1150cd5de windows getch2 for MINGW32 port
faust3
parents:
diff changeset
42 /*check if there are input events*/
18070
c1e25c011c54 getch2 should wait for input. Fixes 100% CPU usage during cache fill.
reimar
parents: 17258
diff changeset
43 WaitForSingleObject(stdin, time);
9766
afb1150cd5de windows getch2 for MINGW32 port
faust3
parents:
diff changeset
44 if(!GetNumberOfConsoleInputEvents(stdin,&retval))
afb1150cd5de windows getch2 for MINGW32 port
faust3
parents:
diff changeset
45 {
afb1150cd5de windows getch2 for MINGW32 port
faust3
parents:
diff changeset
46 printf("getch2: can't get number of input events: %i\n",GetLastError());
afb1150cd5de windows getch2 for MINGW32 port
faust3
parents:
diff changeset
47 return -1;
afb1150cd5de windows getch2 for MINGW32 port
faust3
parents:
diff changeset
48 }
afb1150cd5de windows getch2 for MINGW32 port
faust3
parents:
diff changeset
49 if(retval<=0)return -1;
afb1150cd5de windows getch2 for MINGW32 port
faust3
parents:
diff changeset
50
afb1150cd5de windows getch2 for MINGW32 port
faust3
parents:
diff changeset
51 /*read all events*/
afb1150cd5de windows getch2 for MINGW32 port
faust3
parents:
diff changeset
52 if(!ReadConsoleInput(stdin,eventbuffer,128,&retval))
afb1150cd5de windows getch2 for MINGW32 port
faust3
parents:
diff changeset
53 {
afb1150cd5de windows getch2 for MINGW32 port
faust3
parents:
diff changeset
54 printf("getch: can't read input events\n");
afb1150cd5de windows getch2 for MINGW32 port
faust3
parents:
diff changeset
55 return -1;
afb1150cd5de windows getch2 for MINGW32 port
faust3
parents:
diff changeset
56 }
afb1150cd5de windows getch2 for MINGW32 port
faust3
parents:
diff changeset
57
afb1150cd5de windows getch2 for MINGW32 port
faust3
parents:
diff changeset
58 /*filter out keyevents*/
afb1150cd5de windows getch2 for MINGW32 port
faust3
parents:
diff changeset
59 for (i = 0; i < retval; i++)
afb1150cd5de windows getch2 for MINGW32 port
faust3
parents:
diff changeset
60 {
afb1150cd5de windows getch2 for MINGW32 port
faust3
parents:
diff changeset
61 switch(eventbuffer[i].EventType)
afb1150cd5de windows getch2 for MINGW32 port
faust3
parents:
diff changeset
62 {
afb1150cd5de windows getch2 for MINGW32 port
faust3
parents:
diff changeset
63 case KEY_EVENT:
afb1150cd5de windows getch2 for MINGW32 port
faust3
parents:
diff changeset
64 /*only a pressed key is interresting for us*/
afb1150cd5de windows getch2 for MINGW32 port
faust3
parents:
diff changeset
65 if(eventbuffer[i].Event.KeyEvent.bKeyDown == TRUE)
afb1150cd5de windows getch2 for MINGW32 port
faust3
parents:
diff changeset
66 {
afb1150cd5de windows getch2 for MINGW32 port
faust3
parents:
diff changeset
67 /*check for special keys*/
afb1150cd5de windows getch2 for MINGW32 port
faust3
parents:
diff changeset
68 switch(eventbuffer[i].Event.KeyEvent.wVirtualKeyCode)
afb1150cd5de windows getch2 for MINGW32 port
faust3
parents:
diff changeset
69 {
afb1150cd5de windows getch2 for MINGW32 port
faust3
parents:
diff changeset
70 case VK_HOME:
afb1150cd5de windows getch2 for MINGW32 port
faust3
parents:
diff changeset
71 return KEY_HOME;
afb1150cd5de windows getch2 for MINGW32 port
faust3
parents:
diff changeset
72 case VK_END:
afb1150cd5de windows getch2 for MINGW32 port
faust3
parents:
diff changeset
73 return KEY_END;
afb1150cd5de windows getch2 for MINGW32 port
faust3
parents:
diff changeset
74 case VK_DELETE:
afb1150cd5de windows getch2 for MINGW32 port
faust3
parents:
diff changeset
75 return KEY_DEL;
afb1150cd5de windows getch2 for MINGW32 port
faust3
parents:
diff changeset
76 case VK_INSERT:
afb1150cd5de windows getch2 for MINGW32 port
faust3
parents:
diff changeset
77 return KEY_INS;
afb1150cd5de windows getch2 for MINGW32 port
faust3
parents:
diff changeset
78 case VK_BACK:
afb1150cd5de windows getch2 for MINGW32 port
faust3
parents:
diff changeset
79 return KEY_BS;
afb1150cd5de windows getch2 for MINGW32 port
faust3
parents:
diff changeset
80 case VK_PRIOR:
afb1150cd5de windows getch2 for MINGW32 port
faust3
parents:
diff changeset
81 return KEY_PGUP;
afb1150cd5de windows getch2 for MINGW32 port
faust3
parents:
diff changeset
82 case VK_NEXT:
afb1150cd5de windows getch2 for MINGW32 port
faust3
parents:
diff changeset
83 return KEY_PGDWN;
afb1150cd5de windows getch2 for MINGW32 port
faust3
parents:
diff changeset
84 case VK_RETURN:
afb1150cd5de windows getch2 for MINGW32 port
faust3
parents:
diff changeset
85 return KEY_ENTER;
afb1150cd5de windows getch2 for MINGW32 port
faust3
parents:
diff changeset
86 case VK_ESCAPE:
afb1150cd5de windows getch2 for MINGW32 port
faust3
parents:
diff changeset
87 return KEY_ESC;
afb1150cd5de windows getch2 for MINGW32 port
faust3
parents:
diff changeset
88 case VK_LEFT:
afb1150cd5de windows getch2 for MINGW32 port
faust3
parents:
diff changeset
89 return KEY_LEFT;
afb1150cd5de windows getch2 for MINGW32 port
faust3
parents:
diff changeset
90 case VK_UP:
afb1150cd5de windows getch2 for MINGW32 port
faust3
parents:
diff changeset
91 return KEY_UP;
afb1150cd5de windows getch2 for MINGW32 port
faust3
parents:
diff changeset
92 case VK_RIGHT:
afb1150cd5de windows getch2 for MINGW32 port
faust3
parents:
diff changeset
93 return KEY_RIGHT;
afb1150cd5de windows getch2 for MINGW32 port
faust3
parents:
diff changeset
94 case VK_DOWN:
afb1150cd5de windows getch2 for MINGW32 port
faust3
parents:
diff changeset
95 return KEY_DOWN;
10230
3d33d5a19c21 fix volume increasing
faust3
parents: 9983
diff changeset
96 case VK_SHIFT:
3d33d5a19c21 fix volume increasing
faust3
parents: 9983
diff changeset
97 continue;
9766
afb1150cd5de windows getch2 for MINGW32 port
faust3
parents:
diff changeset
98 }
afb1150cd5de windows getch2 for MINGW32 port
faust3
parents:
diff changeset
99 /*check for function keys*/
18107
24003616a623 10l, fix broken if (X >= Y >= Z) comparison, probably stopped F-keys from working
reimar
parents: 18070
diff changeset
100 if(0x87 >= eventbuffer[i].Event.KeyEvent.wVirtualKeyCode && eventbuffer[i].Event.KeyEvent.wVirtualKeyCode >= 0x70)
9766
afb1150cd5de windows getch2 for MINGW32 port
faust3
parents:
diff changeset
101 return (KEY_F + 1 + eventbuffer[i].Event.KeyEvent.wVirtualKeyCode - 0x70);
afb1150cd5de windows getch2 for MINGW32 port
faust3
parents:
diff changeset
102
afb1150cd5de windows getch2 for MINGW32 port
faust3
parents:
diff changeset
103 /*only characters should be remaining*/
afb1150cd5de windows getch2 for MINGW32 port
faust3
parents:
diff changeset
104 //printf("getch2: YOU PRESSED \"%c\" \n",eventbuffer[i].Event.KeyEvent.uChar.AsciiChar);
afb1150cd5de windows getch2 for MINGW32 port
faust3
parents:
diff changeset
105 return eventbuffer[i].Event.KeyEvent.uChar.AsciiChar;
afb1150cd5de windows getch2 for MINGW32 port
faust3
parents:
diff changeset
106 }
afb1150cd5de windows getch2 for MINGW32 port
faust3
parents:
diff changeset
107 break;
afb1150cd5de windows getch2 for MINGW32 port
faust3
parents:
diff changeset
108
afb1150cd5de windows getch2 for MINGW32 port
faust3
parents:
diff changeset
109 case MOUSE_EVENT:
afb1150cd5de windows getch2 for MINGW32 port
faust3
parents:
diff changeset
110 case WINDOW_BUFFER_SIZE_EVENT:
afb1150cd5de windows getch2 for MINGW32 port
faust3
parents:
diff changeset
111 case FOCUS_EVENT:
afb1150cd5de windows getch2 for MINGW32 port
faust3
parents:
diff changeset
112 case MENU_EVENT:
afb1150cd5de windows getch2 for MINGW32 port
faust3
parents:
diff changeset
113 default:
afb1150cd5de windows getch2 for MINGW32 port
faust3
parents:
diff changeset
114 //printf("getch2: unsupported event type");
afb1150cd5de windows getch2 for MINGW32 port
faust3
parents:
diff changeset
115 break;
afb1150cd5de windows getch2 for MINGW32 port
faust3
parents:
diff changeset
116 }
afb1150cd5de windows getch2 for MINGW32 port
faust3
parents:
diff changeset
117 }
afb1150cd5de windows getch2 for MINGW32 port
faust3
parents:
diff changeset
118 return -1;
afb1150cd5de windows getch2 for MINGW32 port
faust3
parents:
diff changeset
119 }
afb1150cd5de windows getch2 for MINGW32 port
faust3
parents:
diff changeset
120
afb1150cd5de windows getch2 for MINGW32 port
faust3
parents:
diff changeset
121
afb1150cd5de windows getch2 for MINGW32 port
faust3
parents:
diff changeset
122 void getch2_enable(){
9983
14c92818ab75 alternative timer and glob emulation code for mingw32 port
faust3
parents: 9936
diff changeset
123 DWORD retval;
9936
6d9531a5d95b disable getch2 on windows when we can't read stdin (msys)
faust3
parents: 9766
diff changeset
124 stdin = GetStdHandle(STD_INPUT_HANDLE);
6d9531a5d95b disable getch2 on windows when we can't read stdin (msys)
faust3
parents: 9766
diff changeset
125 if(!GetNumberOfConsoleInputEvents(stdin,&retval))
6d9531a5d95b disable getch2 on windows when we can't read stdin (msys)
faust3
parents: 9766
diff changeset
126 {
6d9531a5d95b disable getch2 on windows when we can't read stdin (msys)
faust3
parents: 9766
diff changeset
127 printf("getch2: %i can't get number of input events [disabling console input]\n",GetLastError());
6d9531a5d95b disable getch2 on windows when we can't read stdin (msys)
faust3
parents: 9766
diff changeset
128 getch2_status = 0;
6d9531a5d95b disable getch2 on windows when we can't read stdin (msys)
faust3
parents: 9766
diff changeset
129 }
6d9531a5d95b disable getch2 on windows when we can't read stdin (msys)
faust3
parents: 9766
diff changeset
130 else getch2_status=1;
9766
afb1150cd5de windows getch2 for MINGW32 port
faust3
parents:
diff changeset
131 }
afb1150cd5de windows getch2 for MINGW32 port
faust3
parents:
diff changeset
132
afb1150cd5de windows getch2 for MINGW32 port
faust3
parents:
diff changeset
133 void getch2_disable(){
afb1150cd5de windows getch2 for MINGW32 port
faust3
parents:
diff changeset
134 if(!getch2_status) return; // already disabled / never enabled
afb1150cd5de windows getch2 for MINGW32 port
faust3
parents:
diff changeset
135 getch2_status=0;
afb1150cd5de windows getch2 for MINGW32 port
faust3
parents:
diff changeset
136 }
afb1150cd5de windows getch2 for MINGW32 port
faust3
parents:
diff changeset
137
22442
56a0b0f8a66e Add code to detect and convert to console codepage on Windows.
reimar
parents: 22441
diff changeset
138 #ifdef USE_ICONV
56a0b0f8a66e Add code to detect and convert to console codepage on Windows.
reimar
parents: 22441
diff changeset
139 static const struct {
56a0b0f8a66e Add code to detect and convert to console codepage on Windows.
reimar
parents: 22441
diff changeset
140 unsigned cp;
56a0b0f8a66e Add code to detect and convert to console codepage on Windows.
reimar
parents: 22441
diff changeset
141 char* alias;
56a0b0f8a66e Add code to detect and convert to console codepage on Windows.
reimar
parents: 22441
diff changeset
142 } cp_alias[] = {
56a0b0f8a66e Add code to detect and convert to console codepage on Windows.
reimar
parents: 22441
diff changeset
143 { 20127, "ASCII" },
56a0b0f8a66e Add code to detect and convert to console codepage on Windows.
reimar
parents: 22441
diff changeset
144 { 20866, "KOI8-R" },
56a0b0f8a66e Add code to detect and convert to console codepage on Windows.
reimar
parents: 22441
diff changeset
145 { 21866, "KOI8-RU" },
56a0b0f8a66e Add code to detect and convert to console codepage on Windows.
reimar
parents: 22441
diff changeset
146 { 28591, "ISO-8859-1" },
56a0b0f8a66e Add code to detect and convert to console codepage on Windows.
reimar
parents: 22441
diff changeset
147 { 28592, "ISO-8859-2" },
56a0b0f8a66e Add code to detect and convert to console codepage on Windows.
reimar
parents: 22441
diff changeset
148 { 28593, "ISO-8859-3" },
56a0b0f8a66e Add code to detect and convert to console codepage on Windows.
reimar
parents: 22441
diff changeset
149 { 28594, "ISO-8859-4" },
56a0b0f8a66e Add code to detect and convert to console codepage on Windows.
reimar
parents: 22441
diff changeset
150 { 28595, "ISO-8859-5" },
56a0b0f8a66e Add code to detect and convert to console codepage on Windows.
reimar
parents: 22441
diff changeset
151 { 28596, "ISO-8859-6" },
56a0b0f8a66e Add code to detect and convert to console codepage on Windows.
reimar
parents: 22441
diff changeset
152 { 28597, "ISO-8859-7" },
56a0b0f8a66e Add code to detect and convert to console codepage on Windows.
reimar
parents: 22441
diff changeset
153 { 28598, "ISO-8859-8" },
56a0b0f8a66e Add code to detect and convert to console codepage on Windows.
reimar
parents: 22441
diff changeset
154 { 28599, "ISO-8859-9" },
56a0b0f8a66e Add code to detect and convert to console codepage on Windows.
reimar
parents: 22441
diff changeset
155 { 28605, "ISO-8859-15" },
56a0b0f8a66e Add code to detect and convert to console codepage on Windows.
reimar
parents: 22441
diff changeset
156 { 65001, "UTF-8" },
56a0b0f8a66e Add code to detect and convert to console codepage on Windows.
reimar
parents: 22441
diff changeset
157 { 0, NULL }
56a0b0f8a66e Add code to detect and convert to console codepage on Windows.
reimar
parents: 22441
diff changeset
158 };
56a0b0f8a66e Add code to detect and convert to console codepage on Windows.
reimar
parents: 22441
diff changeset
159
22886
71b3e04d0555 "()" to "(void)" function param list fixes
uau
parents: 22442
diff changeset
160 char* get_term_charset(void)
22442
56a0b0f8a66e Add code to detect and convert to console codepage on Windows.
reimar
parents: 22441
diff changeset
161 {
56a0b0f8a66e Add code to detect and convert to console codepage on Windows.
reimar
parents: 22441
diff changeset
162 static char codepage[10];
56a0b0f8a66e Add code to detect and convert to console codepage on Windows.
reimar
parents: 22441
diff changeset
163 unsigned i, cpno = GetConsoleOutputCP();
56a0b0f8a66e Add code to detect and convert to console codepage on Windows.
reimar
parents: 22441
diff changeset
164 if (!cpno)
56a0b0f8a66e Add code to detect and convert to console codepage on Windows.
reimar
parents: 22441
diff changeset
165 cpno = GetACP();
56a0b0f8a66e Add code to detect and convert to console codepage on Windows.
reimar
parents: 22441
diff changeset
166 if (!cpno)
56a0b0f8a66e Add code to detect and convert to console codepage on Windows.
reimar
parents: 22441
diff changeset
167 return NULL;
56a0b0f8a66e Add code to detect and convert to console codepage on Windows.
reimar
parents: 22441
diff changeset
168
56a0b0f8a66e Add code to detect and convert to console codepage on Windows.
reimar
parents: 22441
diff changeset
169 for (i = 0; cp_alias[i].cp; i++)
56a0b0f8a66e Add code to detect and convert to console codepage on Windows.
reimar
parents: 22441
diff changeset
170 if (cpno == cp_alias[i].cp)
56a0b0f8a66e Add code to detect and convert to console codepage on Windows.
reimar
parents: 22441
diff changeset
171 return cp_alias[i].alias;
56a0b0f8a66e Add code to detect and convert to console codepage on Windows.
reimar
parents: 22441
diff changeset
172
56a0b0f8a66e Add code to detect and convert to console codepage on Windows.
reimar
parents: 22441
diff changeset
173 snprintf(codepage, sizeof(codepage), "CP%u", cpno);
56a0b0f8a66e Add code to detect and convert to console codepage on Windows.
reimar
parents: 22441
diff changeset
174 return codepage;
56a0b0f8a66e Add code to detect and convert to console codepage on Windows.
reimar
parents: 22441
diff changeset
175 }
56a0b0f8a66e Add code to detect and convert to console codepage on Windows.
reimar
parents: 22441
diff changeset
176 #endif