copied files from d1x
[btb/d2x.git] / arch / win32 / key_arch.c
1 #define byte w32_byte
2 #define WIN32_LEAN_AND_MEAN
3 #include <windows.h>
4 #include <stdio.h>
5 #include <dinput.h>
6 #undef byte
7 //#include "inferno.h"
8 #include "fix.h"
9 #include "timer.h"
10 #include "key.h"
11
12 extern void PumpMessages(void);
13
14 // These are to kludge up a bit my slightly broken GCC directx port.
15 #ifndef E_FAIL
16 #define E_FAIL (HRESULT)0x80004005L
17 #endif
18 #ifndef SUCCEEDED
19 #define SUCCEEDED(a) ((HRESULT)(a) >= 0)
20 #endif
21 #ifndef S_OK
22 #define S_OK 0
23 #define S_FALSE 1
24 #endif
25
26 extern void PumpMessages(void);
27
28 unsigned char WMKey_Handler_Ready=0;
29
30
31 LPDIRECTINPUT g_lpdi;
32 LPDIRECTINPUTDEVICE g_lpdidKeybd;
33 extern HWND g_hWnd;
34
35 typedef int KEYCODE;
36
37 static BOOL EnsureInit (void)
38 {
39         if (g_lpdidKeybd == NULL)
40         {
41                 key_init ();
42         }
43
44         return g_lpdidKeybd != NULL;
45 }
46
47 void arch_key_init()
48 {
49         HRESULT hr;
50
51         keyd_fake_repeat=1;//direct input doesn't repeat. -MPM
52
53         // my kingdom, my kingdom for C++...
54         if (SUCCEEDED (hr = DirectInputCreate (GetModuleHandle (NULL), DIRECTINPUT_VERSION, &g_lpdi, NULL)))
55         {
56                if (SUCCEEDED (hr = IDirectInput_CreateDevice (g_lpdi, (void *)&GUID_SysKeyboard, &g_lpdidKeybd, NULL)))
57                 {
58                         DIPROPDWORD dipdw;
59
60                         dipdw.diph.dwSize = sizeof (DIPROPDWORD);
61                         dipdw.diph.dwHeaderSize = sizeof (DIPROPHEADER);
62                         dipdw.diph.dwObj = 0;
63                         dipdw.diph.dwHow = DIPH_DEVICE;
64                         dipdw.dwData = 40;
65
66                         if (SUCCEEDED (hr = IDirectInputDevice_SetDataFormat (g_lpdidKeybd, &c_dfDIKeyboard)) &&
67                                                                 SUCCEEDED (hr = IDirectInputDevice_SetCooperativeLevel (g_lpdidKeybd, g_hWnd, DISCL_NONEXCLUSIVE | DISCL_FOREGROUND)) &&
68                                 SUCCEEDED (hr = IDirectInputDevice_SetProperty (g_lpdidKeybd, DIPROP_BUFFERSIZE, &dipdw.diph)) &&
69                                 SUCCEEDED (hr = IDirectInputDevice_Acquire (g_lpdidKeybd)))
70                         {
71                                 // fine
72                                 WMKey_Handler_Ready = 1;
73
74                         }
75                         else
76                         {
77                                 IDirectInputDevice_Release (g_lpdidKeybd);
78                                 g_lpdidKeybd = NULL;
79                         }
80                 }
81         }
82 }
83
84 void arch_key_close(void)
85 {
86         WMKey_Handler_Ready = 0;
87         if (g_lpdidKeybd != NULL)
88         {
89                 IDirectInputDevice_Unacquire (g_lpdidKeybd);
90                 IDirectInputDevice_Release (g_lpdidKeybd);
91                 g_lpdidKeybd = NULL;
92         }
93         if (g_lpdi != NULL)
94         {
95                 IDirectInput_Release (g_lpdi);
96                 g_lpdi = NULL;
97         }
98 }
99
100 HRESULT ReadKey (DIDEVICEOBJECTDATA *pdidod)
101 {
102         DWORD cElements = 1;
103         HRESULT hr;
104         if (g_lpdidKeybd == NULL)
105                 return E_FAIL;
106         hr = IDirectInputDevice_Acquire (g_lpdidKeybd);
107         if (SUCCEEDED (hr))
108         {
109                 hr = IDirectInputDevice_GetDeviceData (
110                         g_lpdidKeybd,
111                         sizeof (*pdidod),
112                         pdidod,
113                         &cElements,
114                         0);
115                 if (SUCCEEDED (hr) && cElements != 1)
116                         hr = E_FAIL;
117         }
118
119         return hr;
120 }
121
122 void UpdateState (DIDEVICEOBJECTDATA *pdidod)
123 {
124         generic_key_handler(pdidod->dwOfs,(pdidod->dwData & 0x80));
125 }
126
127 void keyboard_handler()
128 {
129 //        static int peekmsgcount = 0;
130         DIDEVICEOBJECTDATA didod;
131         while (SUCCEEDED (ReadKey (&didod)))
132         {
133                 UpdateState (&didod);
134                 
135                 //added 02/20/99 by adb to prevent message overflow
136                 //(this should probably go somewhere else...)
137 //              if (++peekmsgcount == 64) // 64 = wild guess...
138 //              {
139 //                      peekmsgcount = 0;
140 //              }
141                 //end additions
142         }
143         PumpMessages();
144 }
145
146
147
148 void arch_key_flush()
149 {
150         if (EnsureInit ())
151         {
152                 DWORD cElements = INFINITE;
153 //                HRESULT hr =
154                 IDirectInputDevice_GetDeviceData (
155                         g_lpdidKeybd,
156                         sizeof (DIDEVICEOBJECTDATA),
157                         NULL,
158                         &cElements,
159                         0);
160
161         }
162 }
163
164
165
166 void arch_key_poll(void)
167 {
168         keyboard_handler();
169 }