Пример #1
0
void IN_RawInput_Init(void)
{
	  // "0" to exclude, "1" to include
	PRAWINPUTDEVICELIST pRawInputDeviceList;
	int inputdevices, i, j, mtemp;
	char dname[MAX_RI_DEVICE_SIZE];
	qbool validcustomdevice = false;

	// Return 0 if rawinput is not available
	HMODULE user32 = LoadLibrary("user32.dll");
	if (!user32)
	{
		Com_Printf("Raw input: unable to load user32.dll\n");
		return;
	}
	_RRID = (pRegisterRawInputDevices)GetProcAddress(user32,"RegisterRawInputDevices");
	if (!_RRID)
	{
		Com_Printf("Raw input: function RegisterRawInputDevices could not be registered\n");
		return;
	}
	_GRIDL = (pGetRawInputDeviceList)GetProcAddress(user32,"GetRawInputDeviceList");
	if (!_GRIDL)
	{
		Com_Printf("Raw input: function GetRawInputDeviceList could not be registered\n");
		return;
	}
	_GRIDIA = (pGetRawInputDeviceInfoA)GetProcAddress(user32,"GetRawInputDeviceInfoA");
	if (!_GRIDIA)
	{
		Com_Printf("Raw input: function GetRawInputDeviceInfoA could not be registered\n");
		return;
	}
	_GRID = (pGetRawInputData)GetProcAddress(user32,"GetRawInputData");
	if (!_GRID)
	{
		Com_Printf("Raw input: function GetRawInputData could not be registered\n");
		return;
	}

	// 1st call to GetRawInputDeviceList: Pass NULL to get the number of devices.
	if ((*_GRIDL)(NULL, &inputdevices, sizeof(RAWINPUTDEVICELIST)) != 0)
	{
		Com_Printf("Raw input: unable to count raw input devices\n");
		return;
	}

	// Allocate the array to hold the DeviceList
	pRawInputDeviceList = Q_malloc(sizeof(RAWINPUTDEVICELIST) * inputdevices);

	// 2nd call to GetRawInputDeviceList: Pass the pointer to our DeviceList and GetRawInputDeviceList() will fill the array
	if ((*_GRIDL)(pRawInputDeviceList, &inputdevices, sizeof(RAWINPUTDEVICELIST)) == -1)
	{
		Com_Printf("Raw input: unable to get raw input device list\n");
		return;
	}

	// Loop through all devices and count the mice
	for (i = 0, mtemp = 0; i < inputdevices; i++)
	{
		if (pRawInputDeviceList[i].dwType == RIM_TYPEMOUSE) 
		{
			j = MAX_RI_DEVICE_SIZE;

			// Get the device name and use it to determine if it's the RDP Terminal Services virtual device.
			if ((*_GRIDIA)(pRawInputDeviceList[i].hDevice, RIDI_DEVICENAME, dname, &j) < 0)
				dname[0] = 0;

			if (IN_RawInput_IsRDPMouse(dname)) // ignore rdp mouse
				continue;

			// advance temp device count
			mtemp++;
		}
	}

	// exit out if no devices found
	if (!mtemp)
	{
		Com_Printf("Raw input: no usable device found\n");
		return;
	}

	// Loop again and bind devices, malloc only for 1 mouse if we have specified a custom device
	// otherwise malloc for all devices
	rawmice = Q_malloc(sizeof(rawmouse_t) * (in_raw_custom_device_enable.integer ? 1 : mtemp));
	for (i = 0; i < inputdevices; i++)
	{
		if (pRawInputDeviceList[i].dwType == RIM_TYPEMOUSE)
		{
			j = MAX_RI_DEVICE_SIZE;

			// Get the device name and use it to determine if it's the RDP Terminal Services virtual device.
			if ((*_GRIDIA)(pRawInputDeviceList[i].hDevice, RIDI_DEVICENAME, dname, &j) < 0)
				dname[0] = 0;

			if (IN_RawInput_IsRDPMouse(dname)) // ignore rdp mouse
				continue;

			// print pretty message about the mouse
			dname[MAX_RI_DEVICE_SIZE - 1] = 0;
			for (mtemp = strlen(dname); mtemp >= 0; mtemp--)
			{
				if (dname[mtemp] == '#')
				{
					dname[mtemp + 1] = 0;
					break;
				}
			}
			Com_Printf("Raw input: [%i] %s\n", i, dname);

			// set handle for All devices only if user hasn't specified a custom device to use
			if(!in_raw_custom_device_enable.integer) {
				rawmice[rawmicecount].rawinputhandle = pRawInputDeviceList[i].hDevice;
				rawmice[rawmicecount].numbuttons = 10;
				rawmice[rawmicecount].pos[0] = RI_INVALID_POS;
				rawmicecount++;
			}
			// if a custom device is specified: check if its a valid one
			else {
				if(in_raw_custom_device.integer == i)
					validcustomdevice = true;
			}

		}
	}
	// if user wants to specify a specific mouse to init, then only set that handle. 
	// which device is determined by the in_raw_custom_device cvar.
	// note: its still possible to click mouse buttons on other devices if in_raw_allbuttons cvar
	// is set to 1, it needs to be set to 0 to disable button input from other devices.
	if(in_raw_custom_device_enable.integer && validcustomdevice) {
			rawmice[rawmicecount].rawinputhandle = pRawInputDeviceList[in_raw_custom_device.integer].hDevice;
			rawmice[rawmicecount].numbuttons = 10;
			rawmice[rawmicecount].pos[0] = RI_INVALID_POS;
			rawmicecount++;
	}
	else if(in_raw_custom_device_enable.integer && !validcustomdevice){
		Com_Printf("Raw input: ERROR, invalid device number [%d]\n",in_raw_custom_device.integer);
	}

   
	// free the RAWINPUTDEVICELIST
	Q_free(pRawInputDeviceList);

	// alloc raw input buffer
	raw = Q_malloc(INIT_RIBUFFER_SIZE);
	ribuffersize = INIT_RIBUFFER_SIZE;

	Com_Printf("Raw input: initialized with %i mice\n", rawmicecount);

	return; // success
}
Пример #2
0
/*
* IN_RawInput_Init
*
* Returns false if rawinput is not available
*/
bool IN_RawInput_Init( void ) {
	int inputdevices, i, j, mtemp;
	PRAWINPUTDEVICELIST pRawInputDeviceList;
	char dname[MAX_RI_DEVICE_SIZE];
	HMODULE user32 = LoadLibrary( "user32.dll" );

	_GRIDL          = NULL;
	_GRID           = NULL;
	_GRIDIA         = NULL;
	_RRID           = NULL;

	rawmice         = NULL;
	rawmicecount    = 0;
	raw             = NULL;
	ribuffersize    = 0;

	if( !user32 ) {
		Com_Printf( "Raw input: unable to load user32.dll\n" );
		return false;
	}

	if( !( _RRID = ( pRegisterRawInputDevices )GetProcAddress( user32, "RegisterRawInputDevices" ) ) ) {
		Com_Printf( "Raw input: function RegisterRawInputDevices could not be registered\n" );
		return false;
	}

	if( !( _GRIDL = ( pGetRawInputDeviceList )GetProcAddress( user32, "GetRawInputDeviceList" ) ) ) {
		Com_Printf( "Raw input: function GetRawInputDeviceList could not be registered\n" );
		return false;
	}

	if( !( _GRIDIA = ( pGetRawInputDeviceInfoA )GetProcAddress( user32, "GetRawInputDeviceInfoA" ) ) ) {
		Com_Printf( "Raw input: function GetRawInputDeviceInfoA could not be registered\n" );
		return false;
	}

	if( !( _GRID = ( pGetRawInputData )GetProcAddress( user32, "GetRawInputData" ) ) ) {
		Com_Printf( "Raw input: function GetRawInputData could not be registered\n" );
		return false;
	}

	// 1st call to GetRawInputDeviceList: Pass NULL to get the number of devices.
	if( ( *_GRIDL )( NULL, &inputdevices, sizeof( RAWINPUTDEVICELIST ) ) != 0 ) {
		Com_Printf( "Raw input: unable to count raw input devices\n" );
		return false;
	}

	// Allocate the array to hold the DeviceList
	pRawInputDeviceList = Mem_ZoneMalloc( sizeof( RAWINPUTDEVICELIST ) * inputdevices );

	// 2nd call to GetRawInputDeviceList: Pass the pointer to our DeviceList and GetRawInputDeviceList() will fill the array
	if( ( *_GRIDL )( pRawInputDeviceList, &inputdevices, sizeof( RAWINPUTDEVICELIST ) ) == -1 ) {
		Com_Printf( "Raw input: unable to get raw input device list\n" );
		return false;
	}

	// Loop through all devices and count the mice
	for( i = 0, mtemp = 0; i < inputdevices; i++ ) {
		if( pRawInputDeviceList[i].dwType == RIM_TYPEMOUSE ) {
			j = MAX_RI_DEVICE_SIZE;

			// Get the device name and use it to determine if it's the RDP Terminal Services virtual device.
			if( ( *_GRIDIA )( pRawInputDeviceList[i].hDevice, RIDI_DEVICENAME, dname, &j ) < 0 ) {
				dname[0] = 0;
			}

			if( IN_RawInput_IsRDPMouse( dname ) ) { // ignore rdp mouse
				continue;
			}

			// advance temp device count
			mtemp++;
		}
	}

	// exit out if no devices found
	if( !mtemp ) {
		Com_Printf( "Raw input: no usable device found\n" );
		return false;
	}

	// Loop again and bind devices
	rawmice = Mem_ZoneMalloc( sizeof( rawmouse_t ) * mtemp );
	for( i = 0; i < inputdevices; i++ ) {
		if( pRawInputDeviceList[i].dwType == RIM_TYPEMOUSE ) {
			j = MAX_RI_DEVICE_SIZE;

			// Get the device name and use it to determine if it's the RDP Terminal Services virtual device.
			if( ( *_GRIDIA )( pRawInputDeviceList[i].hDevice, RIDI_DEVICENAME, dname, &j ) < 0 ) {
				dname[0] = 0;
			}

			if( IN_RawInput_IsRDPMouse( dname ) ) { // ignore rdp mouse
				continue;
			}

			// print pretty message about the mouse
			dname[MAX_RI_DEVICE_SIZE - 1] = 0;
			for( mtemp = strlen( dname ); mtemp >= 0; mtemp-- ) {
				if( dname[mtemp] == '#' ) {
					dname[mtemp + 1] = 0;
					break;
				}
			}
			Com_Printf( "Raw input: [%i] %s\n", i, dname );

			// set handle
			rawmice[rawmicecount].rawinputhandle = pRawInputDeviceList[i].hDevice;
			rawmice[rawmicecount].numbuttons = 10;
			rawmice[rawmicecount].pos[0] = RI_INVALID_POS;
			rawmicecount++;
		}
	}

	// free the RAWINPUTDEVICELIST
	Mem_ZoneFree( pRawInputDeviceList );

	// alloc raw input buffer
	raw = Mem_ZoneMalloc( INIT_RIBUFFER_SIZE );
	ribuffersize = INIT_RIBUFFER_SIZE;

	return true;
}