int main()
{
    Serial serial((char*)COM_PORT);
    JoystickHandler handler;
	
	// Make sure that the vJoy driver is installed
	if (!vJoyEnabled())
	{
		printf("vJoy driver not enabled: Failed Getting vJoy attributes.\n");
		return -1;
	}
	// Make sure that we estabilished a connection to our Arduino
    if( !serial.IsConnected() ) 
	{
		printf("An Arduino is not connected to %s.\n", COM_PORT);
		return -2;
	}
	// Make sure that the device is free or otherwise available
	VjdStat status = GetVJDStatus(VJ_INTERFACE);
	if( status != VJD_STAT_FREE || !AcquireVJD(VJ_INTERFACE) )
	{
		printf("The device #%d isn't available!", VJ_INTERFACE);
		return -3;
	}
	// TODO: Make sure we're interfacing to an actual Wii Controller
	
    // Register all the clickable buttons
    for(int i = X; i <= Home; ++i)
    {
		handler.registerCallback(i, DOWN, onKeyDown);
		handler.registerCallback(i, UP, onKeyUp);
    }
    // Register the analog sticks
	handler.registerCallback(LS, XMOVE, onAnalogMove);
	handler.registerCallback(LS, YMOVE, onAnalogMove);
	handler.registerCallback(RS, XMOVE, onAnalogMove);
	handler.registerCallback(RS, YMOVE, onAnalogMove);
	
	// And at last, the main loop. It reads a packet from the Arduino and passes it to the handler,
	// which, in turn, fires the correct events in response to the right packets.
    char pckt;
	while( 1 )
    {
		//Read two bytes at a time
		if( serial.ReadData(&pckt, 1) == 1 )
		{
			// The first byte contains info regarding what key we're talking about.
			// The second byte contains info on the specified button.
			handler.handleEvent(pckt & 0b00011111, (char)((unsigned char)pckt >> 5) );
		}
		Sleep(SLEEP_MS);
    }
Exemple #2
0
void vJoy::updateWheelStatus(wheel_report_t* wReport)
{
    iReport.bDevice = (BYTE)vID;
    iReport.wAxisX = wReport->wWheel;
    iReport.wAxisXRot = wReport->bThrottle*128;
    iReport.wAxisYRot = wReport->bBrake*128;
    iReport.wAxisZRot = wReport->bClutch*128;
    iReport.lButtons = wReport->wButtons;

    if (!UpdateVJD(vID, (void*)&iReport))
    {
        vDebug() << "Feeding vJoy device failed\n";
        AcquireVJD(vID);
    }
}
void write_stick(stick_t* stick, const stick_stat_t* stat) {
    BYTE id = (BYTE)stick->device_id;
    JOYSTICK_POSITION_V2 report;
    report.bDevice = id;
    report.wAxisX = VJOY_AXIS_CENTER + (long)(stat->axis_x * VJOY_AXIS_RADIUS);
    report.wAxisY = VJOY_AXIS_CENTER + (long)(stat->axis_y * VJOY_AXIS_RADIUS);
    report.wAxisXRot = VJOY_AXIS_CENTER;
    report.wAxisYRot = VJOY_AXIS_CENTER;
    if (!UpdateVJD(stick->device_id, &report)) {
        printf("Can't write to vJoy device %i. "
               "Try to free or re-enable the device and press any character "
               "to continue.",
               stick->device_id);
        getchar();
        AcquireVJD(stick->device_id);
    }
}
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;
}
Exemple #5
0
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR lpCmd, int nShow) {
	printf("3DS Controller Server %s\n", VERSION);
	
	DWORD screenWidth = GetSystemMetrics(SM_CXSCREEN);
	DWORD screenHeight = GetSystemMetrics(SM_CYSCREEN);
	
	double widthMultiplier = screenWidth / 320.0;
	double heightMultiplier = screenHeight / 240.0;
	
	if(!readSettings()) {
		printf("Couldn't read settings file, using default key bindings.\n");
	}
	
	bool vJoy = true;
	UINT iInterface = settings.vJoyDevice;
	
	iReport.wAxisX = JOY_MIDDLE;
	iReport.wAxisY = JOY_MIDDLE;
	iReport.wAxisZ = JOY_MIDDLE;
	iReport.wAxisXRot = JOY_MIDDLE;
	iReport.wAxisYRot = JOY_MIDDLE;
	iReport.wAxisZRot = JOY_MIDDLE;
	iReport.wSlider = JOY_MIDDLE;
	iReport.wDial = JOY_MIDDLE;
	iReport.lButtons = 0;
	iReport.bHats = -1;
	
	if(vJoy && !vJoyEnabled()) {
		printf("vJoy failed (1)! Buttons will still work, but joy stick won't work.\n");
		vJoy = false;
	}
	
	enum VjdStat status = GetVJDStatus(iInterface);
	if(vJoy && (status == VJD_STAT_OWN || (status == VJD_STAT_FREE && !AcquireVJD(iInterface)))) {
		printf("vJoy failed (2)! Buttons will still work, but joy stick won't work.\n");
		vJoy = false;
	}
	
	ContPovNumber = GetVJDContPovNumber(iInterface);
	//int DiscPovNumber = GetVJDDiscPovNumber(iInterface);
	
	if((settings.dPad == pov) && !(ContPovNumber == 0)) settings.dPad = cPov;
	
	if(vJoy && !updateJoystick(iInterface)) {
		printf("vJoy failed (3)! Buttons will still work, but joystick won't work.\nIs vJoy device %d configured?\n",iInterface);
		vJoy = false;
	} else printf("Connected to vJoy device %d\n",iInterface);
	
	initNetwork();
	
	char nButtons = GetVJDButtonNumber(iInterface);
	if(vJoy && nButtons <16) printf("Your vJoy has %d buttons, 3DSController supports 16!\n", nButtons);
	
	printf("Port: %d\n", settings.port);
	
	printf("Running on: %s\n", hostName);
	
	printf("Your local IP(s):\n");
	printIPs();
	
	printf("\n");
	
	startListening();
	
	while(1) {
		memset(&buffer, 0, sizeof(struct packet));
		
		while(receiveBuffer(sizeof(struct packet)) <= 0) {
			// Waiting
			
			Sleep(settings.throttle);
		}
		
		keyboardActive = buffer.keyboardActive;
		
		switch(buffer.command) {
			case CONNECT:
				lastKeys = 0;
				currentKeys = 0;
				circlePad.x = 0;
				circlePad.y = 0;
				lastTouch.x = 0;
				lastTouch.y = 0;
				currentTouch.x = 0;
				currentTouch.y = 0;
				cStick.x = 0;
				cStick.y = 0;
				
				buffer.command = CONNECT;
				printf("3DS Connected!\n");
				
				Sleep(50);
				sendBuffer(1);
				
				Sleep(50);
				sendBuffer(1);
				
				Sleep(50);
				sendBuffer(1);
				break;
			
			case KEYS:
				lastKeys = currentKeys;
				if(currentKeys & KEY_TOUCH) lastTouch = currentTouch;
				
				memcpy(&currentKeys, &buffer.keys, 4);
				memcpy(&circlePad, &buffer.circlePad, 4);
				memcpy(&currentTouch, &buffer.touch, 4);
				memcpy(&cStick, &buffer.cStick, 4);
				memcpy(&volume, &buffer.volume, 4);
				//printf("\rVolume is currently: %x ", volume); //test
				
				handleKey(KEY_A, settings.A);
				handleKey(KEY_B, settings.B);
				handleKey(KEY_SELECT, settings.Select);
				handleKey(KEY_START, settings.Start);
				if(settings.dPad == key) { //Handle normally if not using POV in settings.
					handleKey(KEY_DRIGHT, settings.Right);
					handleKey(KEY_DLEFT, settings.Left);
					handleKey(KEY_DUP, settings.Up);
					handleKey(KEY_DDOWN, settings.Down);
				}
				handleKey(KEY_R, settings.R);
				handleKey(KEY_L, settings.L);
				handleKey(KEY_ZR, settings.ZR);
				handleKey(KEY_ZL, settings.ZL);
				handleKey(KEY_X, settings.X);
				handleKey(KEY_Y, settings.Y);
				
				if(settings.circlePad == keys) {
					handleKey(KEY_CPAD_RIGHT, settings.PadRight);
					handleKey(KEY_CPAD_LEFT, settings.PadLeft);
					handleKey(KEY_CPAD_UP, settings.PadUp);
					handleKey(KEY_CPAD_DOWN, settings.PadDown);
				}
				
				if(settings.cStick == keys) {
					handleKey(KEY_CSTICK_RIGHT, settings.CSRight);
					handleKey(KEY_CSTICK_LEFT, settings.CSLeft);
					handleKey(KEY_CSTICK_UP, settings.CSUp);
					handleKey(KEY_CSTICK_DOWN, settings.CSDown);
				}
				
				//handleKey(KEY_LID, 'I');
				
				if(newpress(KEY_TOUCH)) {
					lastTouch.x = currentTouch.x;
					lastTouch.y = currentTouch.y;
				}
				
				if((currentKeys & KEY_TOUCH)) {
					if(keyboardActive) {
						if(newpress(KEY_TOUCH)) {
							char letter = currentKeyboardKey();
							if(letter) {
								simulateKeyNewpress(letter);
								simulateKeyRelease(letter);
							}
						}
					}
					else if(settings.touch == mouse) {
						if(settings.mouseSpeed) {
							POINT p;
							GetCursorPos(&p);
							SetCursorPos(p.x + (currentTouch.x - lastTouch.x) * settings.mouseSpeed, p.y + (currentTouch.y - lastTouch.y) * settings.mouseSpeed);
						}
						else {
							SetCursorPos((int)((double)currentTouch.x * widthMultiplier), (int)((double)currentTouch.y * heightMultiplier));
						}
					}
					else if(settings.touch == joystick1) { //made a little bit more accurate to the screen size.
						joyX = (int)((float)(currentTouch.x) * 102.3f);
						joyY = (int)((float)(currentTouch.y) * 136.5f);
					}
					
					else if(settings.touch == joystick2) {
						joyRX = (int)((float)(currentTouch.x) * 102.3f);
						joyRY = (int)((float)(currentTouch.y) * 136.5f);
					}
					else {
						handleKey(KEY_TOUCH, settings.Tap);
					}
				} else { //If we are not touching, move to center (Like if you release the joystick on a normal controller).
					if(settings.touch == joystick1) {
						joyX = 16383; //Halfway between the x
						joyY = 16383; //Halfway between the y
					}
					
					else if(settings.touch == joystick2) {
						joyRX = 16383; //Halfway between the rx
						joyRY = 16383; //Halfway between the ry
					}
				}
				
				if(settings.circlePad == mouse) {
					if(abs(circlePad.x) < settings.mouseSpeed * 3) circlePad.x = 0;
					if(abs(circlePad.y) < settings.mouseSpeed * 3) circlePad.y = 0;
					
					POINT p;
					GetCursorPos(&p);
					SetCursorPos(p.x + (circlePad.x * settings.mouseSpeed) / 32, p.y - (circlePad.y * settings.mouseSpeed) / 32);
				}
				else if(settings.circlePad == joystick1) {
					joyX = (circlePad.x + 128) * 128;
					joyY = (128 - circlePad.y) * 128;
				}
				
				else if(settings.circlePad == joystick2) {
					joyRX = (circlePad.x + 128) * 128;
					joyRY = (128 - circlePad.y) * 128;
				}
				
				if(settings.cStick == mouse) {
					if(abs(cStick.x) < settings.mouseSpeed * 3) cStick.x = 0;
					if(abs(cStick.y) < settings.mouseSpeed * 3) cStick.y = 0;
					
					POINT p;
					GetCursorPos(&p);
					SetCursorPos(p.x + (cStick.x * settings.mouseSpeed) / 32, p.y - (cStick.y * settings.mouseSpeed) / 32);
				}
				
				else if(settings.cStick == joystick1) {
					joyX = (cStick.x + 128) * 128;
					joyY = (128 - cStick.y) * 128;
				}
				
				else if(settings.cStick == joystick2) {
					joyRX = (cStick.x + 128) * 128;
					joyRY = (128 - cStick.y) * 128;
				}
				
				
				if(settings.dPad == cPov) {
					if((currentKeys & KEY_DUP) && !(currentKeys & KEY_DLEFT)) {
						if((currentKeys & KEY_DRIGHT)) {
							povHat = 4500;
						} else {
							povHat = 0;
						}
					} else if((currentKeys & KEY_DRIGHT)) {
						if((currentKeys & KEY_DDOWN)) {
							povHat = 13500;
						} else {
							povHat = 9000;
						}
					} else if((currentKeys & KEY_DDOWN)) {
						if((currentKeys & KEY_DLEFT)) {
							povHat = 22500;
						} else {
							povHat = 18000;
						}
					} else if((currentKeys & KEY_DLEFT)) {
						if ((currentKeys & KEY_DUP)) {
							povHat = 31500;
						} else {
							povHat = 27000;
						}
					}
					
					if(!((currentKeys & KEY_DUP) || (currentKeys & KEY_DRIGHT) || (currentKeys & KEY_DDOWN) || (currentKeys & KEY_DLEFT))) {
						//If none are pressed, reset the POV hat
						povHat = -1;
					}
					
				}
				
				else if(settings.dPad == pov) {
					if((currentKeys & KEY_DUP) && !(currentKeys & KEY_DLEFT)) iReport.bHats = 0;
					else if(currentKeys & KEY_DRIGHT) iReport.bHats = 1;
					else if (currentKeys & KEY_DDOWN) iReport.bHats = 2;
					else if (currentKeys & KEY_DLEFT) iReport.bHats = 3;
					else iReport.bHats = -1;
				}
				
				joyVolume = volume * 512;
				
				break;
		}
		
		if(vJoy) updateJoystick(iInterface);
	}
	
	error("accept()");
	return 0;
}
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;
}
Exemple #7
0
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR lpCmd, int nShow) {
    printf("3DS Controller Server %.1f\n", VERSION);

    DWORD screenWidth = GetSystemMetrics(SM_CXSCREEN);
    DWORD screenHeight = GetSystemMetrics(SM_CYSCREEN);

    double widthMultiplier = screenWidth / 320.0;
    double heightMultiplier = screenHeight / 240.0;

    //screenshot(SCREENSHOT_NAMEL, TRUE, 0, 0, 18);

    bool vJoy = true;
    UINT iInterface = 1;

    iReport.wAxisZ = JOY_MIDDLE;
    iReport.wAxisXRot = JOY_MIDDLE;
    iReport.wAxisYRot = JOY_MIDDLE;
    iReport.wAxisZRot = JOY_MIDDLE;
    iReport.wSlider = JOY_MIDDLE;
    iReport.lButtons = 0;
    iReport.bHats = -1;

    if(vJoy && !vJoyEnabled()) {
        printf("vJoy failed (1)! Buttons will still work, but joy stick won't work.\n");
        vJoy = false;
    }

    enum VjdStat status = GetVJDStatus(iInterface);
    if(vJoy && (status == VJD_STAT_OWN || (status == VJD_STAT_FREE && !AcquireVJD(iInterface)))) {
        printf("vJoy failed (2)! Buttons will still work, but joy stick won't work.\n");
        vJoy = false;
    }

    ContPovNumber = GetVJDContPovNumber(iInterface);
    //int DiscPovNumber = GetVJDDiscPovNumber(iInterface);

    if(vJoy && !updateJoystick()) {
        printf("vJoy failed (3)! Buttons will still work, but joystick won't work.\n");
        vJoy = false;
    }

    if(!readSettings()) {
        printf("Couldn't read settings file, using default key bindings.\n");
    }

    initNetwork();

    printf("Port: %d\n", settings.port);

    printf("Running on: %s\n", hostName);

    printf("Your local IP(s):\n");
    printIPs();

    printf("\n");

    startListening();

    while(1) {
        memset(&buffer, 0, sizeof(struct packet));

        while(receiveBuffer(sizeof(struct packet)) <= 0) {
            // Waiting

            Sleep(settings.throttle);
        }

        keyboardActive = buffer.keyboardActive;

        switch(buffer.command) {
        case CONNECT:
            lastKeys = 0;
            currentKeys = 0;
            circlePad.x = 0;
            circlePad.y = 0;
            lastTouch.x = 0;
            lastTouch.y = 0;
            currentTouch.x = 0;
            currentTouch.y = 0;

            buffer.command = CONNECT;
            printf("3DS Connected!\n");

            Sleep(50);
            sendBuffer(1);

            Sleep(50);
            sendBuffer(1);

            Sleep(50);
            sendBuffer(1);
            break;

        case KEYS:
            lastKeys = currentKeys;
            if(currentKeys & KEY_TOUCH) lastTouch = currentTouch;

            memcpy(&currentKeys, &buffer.keys, 4);
            memcpy(&circlePad, &buffer.circlePad, 4);
            memcpy(&currentTouch, &buffer.touch, 4);

            handleKey(KEY_A, settings.A);
            handleKey(KEY_B, settings.B);
            handleKey(KEY_SELECT, settings.Select);
            handleKey(KEY_START, settings.Start);
            handleKey(KEY_DRIGHT, settings.Right);
            handleKey(KEY_DLEFT, settings.Left);
            handleKey(KEY_DUP, settings.Up);
            handleKey(KEY_DDOWN, settings.Down);
            handleKey(KEY_R, settings.R);
            handleKey(KEY_L, settings.L);
            handleKey(KEY_X, settings.X);
            handleKey(KEY_Y, settings.Y);

            //handleKey(KEY_LID, 'I');

            if(newpress(KEY_TOUCH)) {
                lastTouch.x = currentTouch.x;
                lastTouch.y = currentTouch.y;
            }

            if((currentKeys & KEY_TOUCH)) {
                if(keyboardActive) {
                    if(newpress(KEY_TOUCH)) {
                        char letter = currentKeyboardKey();
                        if(letter) {
                            simulateKeyNewpress(letter);
                            simulateKeyRelease(letter);
                        }
                    }
                }
                else if(settings.touch == mouse) {
                    if(settings.mouseSpeed) {
                        POINT p;
                        GetCursorPos(&p);
                        SetCursorPos(p.x + (currentTouch.x - lastTouch.x) * settings.mouseSpeed, p.y + (currentTouch.y - lastTouch.y) * settings.mouseSpeed);
                    }
                    else {
                        SetCursorPos((int)((double)currentTouch.x * widthMultiplier), (int)((double)currentTouch.y * heightMultiplier));
                    }
                }
                else if(settings.touch == joystick) {
                    joyX = (currentTouch.x) * 128;
                    joyY = (currentTouch.y) * 128;
                }
                else {
                    handleKey(KEY_TOUCH, settings.Tap);
                }
            }

            if(settings.circlePad == mouse) {
                if(abs(circlePad.x) < settings.mouseSpeed * 3) circlePad.x = 0;
                if(abs(circlePad.y) < settings.mouseSpeed * 3) circlePad.y = 0;

                POINT p;
                GetCursorPos(&p);
                SetCursorPos(p.x + (circlePad.x * settings.mouseSpeed) / 32, p.y - (circlePad.y * settings.mouseSpeed) / 32);
            }
            else if(settings.circlePad == joystick) {
                joyX = (circlePad.x + 128) * 128;
                joyY = (128 - circlePad.y) * 128;
            }

            break;
        }

        if(vJoy) updateJoystick();

        //sendScreenshot();
    }

    error("accept()");
    return 0;
}
Exemple #8
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);
}