stick_t* acquire_stick() {
    stick_t* stick = (stick_t*)malloc(sizeof (stick_t));

    stick->device_id = 0;

    if (!vJoyEnabled()) {
        printf("Couldn't connect to vJoy. "
               "Make sure vJoy is installed and enabled");
        exit(0);
    } else {
        printf("vJoy enabled.\n");
        printf(" - Vendor: %s\n - Product: %s\n - Version: %s\n",
               (const char*)GetvJoyManufacturerString(),
               (const char*)GetvJoyProductString(),
               (const char*)GetvJoySerialNumberString()
               );
    }

    UINT i;
    for (i = DEFAULT_VJOY_DEV_ID; i <= MAX_VJOY_DEV_ID; i++) {
        VjdStat status = GetVJDStatus(i);
        if (status == VJD_STAT_OWN || status == VJD_STAT_FREE) {
            stick->device_id = i;
            break;
        }
    }
    if (stick->device_id == 0) {
        printf("No available vJoy devices.");
        exit(0);
    }

    if (!AcquireVJD(stick->device_id)) {
        printf("Failed to acquire vJoy device %i", stick->device_id);
        RelinquishVJD(stick->device_id);
        exit(0);
    } else {
        printf("Acquired vJoy device %i", stick->device_id);
    }

    return stick;
}
int
__cdecl
_tmain(__in int argc, __in PZPWSTR argv)
{
	_tprintf("Usage: vJoyUdpFeeder [device number] [port]\n\n");

	JOYSTICK_POSITION	iReport;					// The structure that holds the full position data
	BYTE id=1;										// ID of the target vJoy device (Default is 1)
	UINT iInterface=1;								// Default target vJoy device
	// BOOL ContinuousPOV=FALSE;						// Continuous POV hat (or 4-direction POV Hat)
	int argPort=0;


	// Get the ID of the target vJoy device
	if (argc>1 && wcslen(argv[1]))
		sscanf_s((char *)(argv[1]), "%d", &iInterface);

	if (argc>2 && wcslen(argv[2]))
		sscanf_s((char *)(argv[2]), "%d", &argPort);


	// Get the driver attributes (Vendor ID, Product ID, Version Number)
	if (!vJoyEnabled())
	{
		_tprintf("vJoy driver not enabled: Failed getting vJoy attributes.\n");
		return -2;
	}
	else
	{
		_tprintf("Vendor: %S\nProduct: %S\nVersion number: %S\n", TEXT(GetvJoyManufacturerString()),  TEXT(GetvJoyProductString()), TEXT(GetvJoySerialNumberString()));
	};

	// Get the state of the requested device
	VjdStat status = GetVJDStatus(iInterface);
	switch (status)
	{
	case VJD_STAT_OWN:
		_tprintf("vJoy device %d is already owned by this feeder\n", iInterface);
		break;
	case VJD_STAT_FREE:
		_tprintf("vJoy device %d is free\n", iInterface);
		break;
	case VJD_STAT_BUSY:
		_tprintf("vJoy device %d is already owned by another feeder\nCannot continue\n", iInterface);
		return -3;
	case VJD_STAT_MISS:
		_tprintf("vJoy device %d is not installed or disabled\nCannot continue\n", iInterface);
		return -4;
	default:
		_tprintf("vJoy device %d general error\nCannot continue\n", iInterface);
		return -1;
	};

	// Check which axes are supported
	BOOL AxisX  = GetVJDAxisExist(iInterface, HID_USAGE_X);
	BOOL AxisY  = GetVJDAxisExist(iInterface, HID_USAGE_Y);
	BOOL AxisZ  = GetVJDAxisExist(iInterface, HID_USAGE_Z);
	BOOL AxisRX = GetVJDAxisExist(iInterface, HID_USAGE_RX);
	BOOL AxisRY = GetVJDAxisExist(iInterface, HID_USAGE_RY);
	BOOL AxisRZ = GetVJDAxisExist(iInterface, HID_USAGE_RZ);
	BOOL Slider = GetVJDAxisExist(iInterface, HID_USAGE_SL0);
	BOOL Dial   = GetVJDAxisExist(iInterface, HID_USAGE_SL1);
	// Get the number of buttons and POV Hat switches supported by this vJoy device
	int nButtons  = GetVJDButtonNumber(iInterface);
	int ContPovNumber = GetVJDContPovNumber(iInterface);
	int DiscPovNumber = GetVJDDiscPovNumber(iInterface);

	// Print results
	_tprintf("\nvJoy device %d capabilities:\n", iInterface);
	_tprintf("Number of buttons\t\t%d\n", nButtons);
	_tprintf("Number of Continuous POVs\t%d\n", ContPovNumber);
	_tprintf("Number of Discrete POVs\t\t%d\n", DiscPovNumber);
	_tprintf("Axis X\t\t%s\n", AxisX?"Yes":"No");
	_tprintf("Axis Y\t\t%s\n", AxisY?"Yes":"No");
	_tprintf("Axis Z\t\t%s\n", AxisZ?"Yes":"No");
	_tprintf("Axis Rx\t\t%s\n", AxisRX?"Yes":"No");
	_tprintf("Axis Ry\t\t%s\n", AxisRY?"Yes":"No");
	_tprintf("Axis Rz\t\t%s\n", AxisRZ?"Yes":"No");
	_tprintf("Slider\t\t%s\n", Slider?"Yes":"No");
	_tprintf("Dial\t\t%s\n", Dial?"Yes":"No");



	// Acquire the target
	if ((status == VJD_STAT_OWN) || ((status == VJD_STAT_FREE) && (!AcquireVJD(iInterface))))
	{
		_tprintf("Failed to acquire vJoy device number %d.\n", iInterface);
		return -1;
	}
	else
	{
		_tprintf("Acquired: vJoy device number %d.\n", iInterface);
	}



	_tprintf("\nPress enter to start feeding...\n");
	getchar();

	WSAData data;
	if (WSAStartup( MAKEWORD( 2, 2 ), &data ) != 0) {
		_tprintf("Could not open Windows connection.\n");
		return -1;
	}

	struct sockaddr_in server;
	struct sockaddr_in client;
	char buf[1024];
	unsigned short server_port = (argPort < 1024 || argPort > 65535) ? 1608 : argPort;
	int sock = socket(AF_INET, SOCK_DGRAM, 0);
	if (sock < 0) {
		_tprintf("Could not create socket.\n");
		WSACleanup();
		return -1;
	}
	memset(&server, 0, sizeof(struct sockaddr_in));
	server.sin_family = AF_INET;
	server.sin_addr.s_addr = INADDR_ANY;
	server.sin_port = htons(server_port);
	if (bind(sock, (struct sockaddr *)&server, sizeof(struct sockaddr_in)) < 0) {
		_tprintf("Could not bind socket.\n");
		closesocket(sock);
		WSACleanup();
		return -1;
	}
	_tprintf("Listening on UDP port %d.\n", server_port);
	int client_length = sizeof(struct sockaddr_in);

	// Start feeding in an endless loop
	while (1)
	{
		int bytes_received = recvfrom(sock, buf, 1024, 0, (struct sockaddr *)&client, &client_length);
		if (bytes_received < 0) {
			_tprintf("Could not receive datagram.\n");
			continue;
		}
		if (bytes_received < 11*sizeof(long)) {
			_tprintf("Received too short datagram of %d bytes.\n", bytes_received);
			continue;
		}

		/*** Create the data packet that holds the entire position info ***/
		id = (BYTE)iInterface;
		iReport.bDevice = id;

		char *pbuf = buf;

		iReport.wAxisX=ntohl(*((long *)pbuf));
		pbuf += sizeof(long);
		iReport.wAxisY=ntohl(*((long *)pbuf));
		pbuf += sizeof(long);
		iReport.wAxisZ=ntohl(*((long *)pbuf));
		pbuf += sizeof(long);
		iReport.wAxisXRot=ntohl(*((long *)pbuf));
		pbuf += sizeof(long);
		iReport.wAxisYRot=ntohl(*((long *)pbuf));
		pbuf += sizeof(long);
		iReport.wAxisZRot=ntohl(*((long *)pbuf));
		pbuf += sizeof(long);
		iReport.wSlider=ntohl(*((long *)pbuf));
		pbuf += sizeof(long);
		iReport.wDial=ntohl(*((long *)pbuf));
		pbuf += sizeof(long);

		iReport.lButtons = ntohl(*((long *)pbuf));
		pbuf += sizeof(long);

		long contPov = ntohl(*((long *)pbuf));
		pbuf += sizeof(long);
		long discPovs = ntohl(*((long *)pbuf));
		pbuf += sizeof(long);

		if (ContPovNumber)
		{
			iReport.bHats		= contPov;
			iReport.bHatsEx1	= -1; // Neutral state
			iReport.bHatsEx2	= -1; // Neutral state
			iReport.bHatsEx3	= -1; // Neutral state
		}
		else
		{
			iReport.bHats		= discPovs;
		};

		/*** Feed the driver with the position packet - is fails then wait for input then try to re-acquire device ***/
		if (!UpdateVJD(iInterface, (PVOID)&iReport))
		{
			_tprintf("Feeding vJoy device number %d failed - try to enable device then press enter\n", iInterface);
			getchar();
			AcquireVJD(iInterface);
			// ContinuousPOV = (BOOL)GetVJDContPovNumber(iInterface);
		}

	};

	closesocket(sock);
	WSACleanup();

	_tprintf("OK\n");

	return 0;
}
Beispiel #3
0
vJoy::vJoy()
{
    vID = V_ID;
    vJoyAcquired = false;
    iReport = {0};
    force = 0;

    vLogsStr = new QString("================== vJoy  Debug ==================\n");
    debugStream = new QTextStream(vLogsStr);

    if (!vJoyEnabled()) {
        vDebug() << "Failed getting vJoy attributes\n";
    }
    else {
        vDebug() << "Vendor: " << PVTEXT(GetvJoyManufacturerString())\
                 << "\nProduct: " << PVTEXT(GetvJoyProductString())\
                 <<"\nVersion Number: " << PVTEXT(GetvJoySerialNumberString()) << "\n";
    }

    if (!DriverMatch(NULL, NULL)) {
        vDebug() << "Failed, vJoy Driver does not match vJoyInterface DLL\n";
    }
    else
        vDebug() << "OK - vJoy Driver and vJoyInterface DLL versions match\n";

    //Get the state of the requested device
    VjdStat status = GetVJDStatus(vID);
    switch (status)
    {
    case VJD_STAT_OWN:
        vDebug() << "vJoy Device " << vID << " is already owned by this feeder\n";
        break;
    case VJD_STAT_FREE:
        vDebug() << "vJoy Device " << vID << " is free\n";
        break;
    case VJD_STAT_BUSY:
        vDebug() << "vJoy Device " << vID << " is already owned by another feeder\nCannot continue\n";
    case VJD_STAT_MISS:
        vDebug() << "vJoy Device " << vID << " is not installed or disabled\nCannot continue\n";
    default:
        vDebug() << "vJoy Device " << vID << " general error\nCannot continue\n";
    };

    //Check which axes are supported
    bool AxisX = GetVJDAxisExist(vID, HID_USAGE_X);
    bool AxisRX = GetVJDAxisExist(vID, HID_USAGE_RX);
    bool AxisRY = GetVJDAxisExist(vID, HID_USAGE_RY);
    bool AxisRZ = GetVJDAxisExist(vID, HID_USAGE_RZ);
    int nButtons = GetVJDButtonNumber(vID);
    // Print results
    vDebug() << "-------- vJoy Device " << vID << " capabilities --------\n";
    vDebug() << "Numner of buttons: " << nButtons << "\n";
    vDebug() << "Axis X\t\t" << (AxisX?"Yes\n":"No\n");
    vDebug() << "Axis Rx\t\t" << (AxisRX?"Yes\n":"No\n");
    vDebug() << "Axis Ry\t\t" << (AxisRY?"Yes\n":"No\n");
    vDebug() << "Axis Rz\t\t" << (AxisRZ?"Yes\n":"No\n");

    //Acquire the target vJoy device
    if ((status == VJD_STAT_OWN) || ((status == VJD_STAT_FREE) && (!AcquireVJD(vID))))
    {
        vDebug() << "Failed to acquire vJoy device number " << vID << "\n";
    }
    else {
        vDebug() << "Acquired: vJoy device number " << vID << "\n";
        ResetVJD(vID);
        vJoyAcquired = true;
    }

    //Start FFB
    if(!FfbStart(vID)) {
        vDebug() << "Failed to start FFB on vJoy device number " << vID << "\n";

    }
    else {
        vDebug() << "Started FFB on vJoy device\n";
    }

    //Register FFB callback function
    FfbRegisterGenCB(processFFB, NULL);
}