示例#1
0
int main( int argc, char **argv )
{
	char *error = NULL;
	if ( !tempered_init( &error ) )
	{
		fprintf( stderr, "Failed to initialize libtempered: %s\n", error );
		free( error );
		return 1;
	}
	
	struct tempered_device_list *list = tempered_enumerate( &error );
	if ( list == NULL )
	{
		fprintf( stderr, "Failed to enumerate devices: %s\n", error );
		free( error );
	}
	else
	{
		if ( argc > 1 )
		{
			// We have parameters, so only print those devices that are given.
			int i;
			for ( i = 1; i < argc ; i++ )
			{
				bool found = false;
				struct tempered_device_list *dev;
				for ( dev = list ; dev != NULL ; dev = dev->next )
				{
					if ( strcmp( dev->path, argv[i] ) == 0 )
					{
						found = true;
						print_device( dev );
						break;
					}
				}
				if ( !found )
				{
					fprintf(
						stderr, "%s: TEMPered device not found or ignored.\n",
						argv[i]
					);
				}
			}
		}
		else
		{
			// We don't have any parameters, so print all the devices we found.
			struct tempered_device_list *dev;
			for ( dev = list ; dev != NULL ; dev = dev->next )
			{
				print_device( dev );
			}
		}
		tempered_free_device_list( list );
	}
	
	if ( !tempered_exit( &error ) )
	{
		fprintf( stderr, "%s\n", error );
		free( error );
		return 1;
	}
	return 0;
}
示例#2
0
/** Enumerate the HID TEMPer devices. */
struct tempered_device_list* tempered_type_hid_enumerate( char **error )
{
	struct tempered_device_list *list = NULL, *current = NULL;
	struct hid_device_info *devs, *info;
	devs = hid_enumerate( 0, 0 );
	if ( devs == NULL )
	{
		// No HID devices were found. We unfortunately cannot know if this was
		// because of an error or because there simply aren't any present.
		if ( error != NULL )
		{
			*error = strdup( "No HID devices were found." );
		}
		return NULL;
	}
	for ( info = devs; info; info = info->next )
	{
		struct temper_type* type = temper_type_find(
			info->vendor_id, info->product_id, info->interface_number
		);
		if ( type != NULL && type->open != NULL )
		{
			#ifdef DEBUG
			printf(
				"Device %04hx:%04hx if %d rel %4hx | %s | %ls %ls\n",
				info->vendor_id, info->product_id,
				info->interface_number, info->release_number,
				info->path,
				info->manufacturer_string, info->product_string
			);
			#endif
			struct tempered_device_list *next = malloc(
				sizeof( struct tempered_device_list )
			);
			if ( next == NULL )
			{
				tempered_free_device_list( list );
				if ( error != NULL )
				{
					*error = strdup( "Unable to allocate memory for list." );
				}
				return NULL;
			}
			
			next->next = NULL;
			next->path = strdup( info->path );
			next->type_name = type->name;
			next->vendor_id = info->vendor_id;
			next->product_id = info->product_id;
			next->interface_number = info->interface_number;
			
			if ( next->path == NULL )
			{
				free( next );
				tempered_free_device_list( list );
				if ( error != NULL )
				{
					*error = strdup( "Unable to allocate memory for path." );
				}
				return NULL;
			}
			
			if ( current == NULL )
			{
				list = next;
				current = list;
			}
			else
			{
				current->next = next;
				current = current->next;
			}
		}
	}
	hid_free_enumeration( devs );
	return list;
}