aboutsummaryrefslogtreecommitdiff
path: root/hid_windows.c
blob: ffa1dc0d608b066f8bc9ca6246fa38f59dee88c3 (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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
#ifdef _WIN32
/*
 *  Microsoft Windows DDK HID support for Pd [hid] object
 *
 *  Copyright (c) 2004 Hans-Christoph All rights reserved.
 *
 *   This program 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.
 *
 *   This program 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 this program; if not, write to the Free Software
 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 *
 */

#include <windows.h>
#include <winbase.h>
#include <stdio.h>
#include <setupapi.h> 
#ifdef _MSC_VER
#include <hidsdi.h> 
#else
#include <ddk/hidsdi.h> 
#endif /* _MSC_VER */
#include "hid.h"

//#define DEBUG(x)
#define DEBUG(x) x 

/*==============================================================================
 *  GLOBAL VARS
 *======================================================================== */

extern t_int hid_instance_count;

/*==============================================================================
 * FUNCTION PROTOTYPES
 *==============================================================================
 */


/*==============================================================================
 * Event TYPE/CODE CONVERSION FUNCTIONS
 *==============================================================================
 */




/* ============================================================================== */
/* WINDOWS DDK HID SPECIFIC SUPPORT FUNCTIONS */
/* ============================================================================== */

void hid_get_device_by_number(t_int device_number)
{
	
}


void hid_build_element_list(t_hid *x) 
{
	
}

t_int hid_print_element_list(t_hid *x)
{
	debug_print(LOG_DEBUG,"hid_print_element_list");


	return (0);	
}

t_int hid_print_device_list(t_hid *x) 
{
	struct _GUID GUID;
	SP_INTERFACE_DEVICE_DATA DeviceInterfaceData;
	struct {DWORD cbSize; char DevicePath[256];} FunctionClassDeviceData;
	HIDD_ATTRIBUTES HIDAttributes;
	SECURITY_ATTRIBUTES SecurityAttributes;
	int i;
	HANDLE PnPHandle, HIDHandle;
	ULONG BytesReturned;
	int Success, ManufacturerName, ProductName;
	char ManufacturerBuffer[256];
	char ProductBuffer[256];
	const char NotSupplied[] = "NULL";
	DWORD lastError = 0;

#if 0
// Initialize the GUID array and setup the security attributes for Win2000
	HidD_GetHidGuid(&GUID);
	SecurityAttributes.nLength = sizeof(SECURITY_ATTRIBUTES);
	SecurityAttributes.lpSecurityDescriptor = NULL;
	SecurityAttributes.bInheritHandle = FALSE;

// Get a handle for the Plug and Play node and request currently active devices
	PnPHandle = SetupDiGetClassDevs(&GUID, NULL, NULL, 
											  DIGCF_PRESENT|DIGCF_INTERFACEDEVICE);

	if ((int)PnPHandle == -1) 
	{ 
		error("[hid] ERROR: Could not attach to PnP node\n"); 
		return (t_int) GetLastError(); 
	}

// Lets look for a maximum of 32 Devices
	for (i = 0; i < 32; i++) {
// Initialize our data
		DeviceInterfaceData.cbSize = sizeof(DeviceInterfaceData);
// Is there a device at this table entry
		Success = SetupDiEnumDeviceInterfaces(PnPHandle, NULL, &GUID, i, 
														  &DeviceInterfaceData);
		if (Success) {
// There is a device here, get it's name
			FunctionClassDeviceData.cbSize = 5;
			Success = SetupDiGetDeviceInterfaceDetail(PnPHandle, 
					&DeviceInterfaceData,
					(PSP_INTERFACE_DEVICE_DETAIL_DATA)&FunctionClassDeviceData, 
					256, &BytesReturned, NULL);
			if (!Success) 
			{ 
				error("[hid] ERROR: Could not find the system name for device %d\n",i); 
				return GetLastError();
			}
// Can now open this device
			HIDHandle = CreateFile(FunctionClassDeviceData.DevicePath, 
										  0, 
										  FILE_SHARE_READ|FILE_SHARE_WRITE, 
										  &SecurityAttributes, OPEN_EXISTING, 0, NULL);
			lastError =  GetLastError();
			if (HIDHandle == INVALID_HANDLE_VALUE) 
			{
				error("[hid] ERROR: Could not open HID #%d, Errorcode = %d\n", i, (int)lastError);
				return lastError;
			}
			
// Get the information about this HID
			Success = HidD_GetAttributes(HIDHandle, &HIDAttributes);
			if (!Success) 
			{ 
				error("[hid] ERROR: Could not get HID attributes\n"); 
				return GetLastError(); 
			}
			ManufacturerName = HidD_GetManufacturerString(HIDHandle, ManufacturerBuffer, 256);
			ProductName = HidD_GetProductString(HIDHandle, ProductBuffer, 256);
// And display it!
			post("[hid]: Device %d: %s %s\n",i,
				  ManufacturerName ? ManufacturerBuffer : NotSupplied,
				  ProductName ? ProductBuffer : NotSupplied);
			post("\tVenderID = %4.4x, Name = ", HIDAttributes.VendorID);
			post("%s\n", ManufacturerName ? ManufacturerBuffer : NotSupplied);
			post("\tProductID = %4.4x, Name = ", HIDAttributes.ProductID);
			post("%s\n", ProductName ? ProductBuffer : NotSupplied);

			CloseHandle(HIDHandle);
		} // if (SetupDiEnumDeviceInterfaces . .
	} // for (i = 0; i < 32; i++)
	SetupDiDestroyDeviceInfoList(PnPHandle);
#endif
	return 0;
}

void hid_output_device_name(t_hid *x, char *manufacturer, char *product) 
{
	char      *device_name;
	t_symbol  *device_name_symbol;

	device_name = malloc( strlen(manufacturer) + 1 + strlen(product) + 1 );
//	device_name = malloc( 7 + strlen(manufacturer) + 1 + strlen(product) + 1 );
//	strcpy( device_name, "append " );
	strcat( device_name, manufacturer );
	strcat ( device_name, " ");
	strcat( device_name, product );
//	outlet_anything( x->x_status_outlet, gensym( device_name ),0,NULL );
	outlet_symbol( x->x_status_outlet, gensym( device_name ) );
}

/* ------------------------------------------------------------------------------ */
/*  FORCE FEEDBACK FUNCTIONS */
/* ------------------------------------------------------------------------------ */

/* cross-platform force feedback functions */
t_int hid_ff_autocenter( t_hid *x, t_float value )
{
	return ( 0 );
}


t_int hid_ff_gain( t_hid *x, t_float value )
{
	return ( 0 );
}


t_int hid_ff_motors( t_hid *x, t_float value )
{
	return ( 0 );
}


t_int hid_ff_continue( t_hid *x )
{
	return ( 0 );
}


t_int hid_ff_pause( t_hid *x )
{
	return ( 0 );
}


t_int hid_ff_reset( t_hid *x )
{
	return ( 0 );
}


t_int hid_ff_stopall( t_hid *x )
{
	return ( 0 );
}



// these are just for testing...
t_int hid_ff_fftest ( t_hid *x, t_float value)
{
	return ( 0 );
}


void hid_ff_print( t_hid *x )
{
}

/* ============================================================================== */
/* Pd [hid] FUNCTIONS */
/* ============================================================================== */

t_int hid_get_events(t_hid *x)
{
	//debug_print(LOG_DEBUG,"hid_get_events");

	return (0);	
}


t_int hid_open_device(t_hid *x, t_int device_number)
{
	debug_print(LOG_DEBUG,"hid_open_device");
	t_int result = 0;
	

	return(result);
}


t_int hid_close_device(t_hid *x)
{
	debug_print(LOG_DEBUG,"hid_close_device");

	t_int result = 0;
	
	return(result);
}


t_int hid_build_device_list(t_hid *x)
{
	debug_print(LOG_DEBUG,"hid_build_device_list");
	
/*
 * The Windows DDK "hid.dll" has to be loaded manually because Windows gets
 * confused by this object, which is also named "hid.dll".  This is the first
 * platform-specific function called in hid.c, so that's why this is happening
 * here.
 */
	TCHAR hidDllPath[MAX_PATH];
	UINT hidDllPathLength;
	HMODULE hModule = NULL;

	hidDllPathLength = GetSystemDirectory(hidDllPath, MAX_PATH);
	if( !hidDllPathLength  )
	{
		error("[hid] ERROR: cannot get SystemRoot");
		return 0;
	}
	strcat(hidDllPath,"\\hid.dll");
	post("hidDllPath: %s",hidDllPath);
	hModule = LoadLibrary(hidDllPath);
	if ( !hModule )
	{
		error("[hid] ERROR: couldn't load %s: error %d",hidDllPath,GetLastError());
		return 0;
	}

	return 1;
}


void hid_print(t_hid *x)
{
	hid_print_device_list(x);
	
	if(x->x_device_open) 
	{
		hid_print_element_list(x);
		hid_ff_print( x );
	}
}


void hid_platform_specific_free(t_hid *x)
{
	debug_print(LOG_DEBUG,"hid_platform_specific_free");
/* only call this if the last instance is being freed */
	if (hid_instance_count < 1) 
	{
		DEBUG(post("RELEASE ALL hid_instance_count: %d", hid_instance_count););
	}
}






#endif  /* _WIN32 */