Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //////////////////////////////////////////////////////////////////////
- // This console application demonstrates how to wrive a vJoy client
- // Не везде удалял авторские комменты. Виноват, я ленивый.
- //Тут где-то был копирайт, так вот, самая большая няша, которой мы обязаны
- //этим кодом, - это Shaul Eizikovich. Большое ему спасибо.
- //////////////////////////////////////////////////////////////////////
- #include "stdafx.h"
- #include "vjoyclient.h"
- #include <malloc.h>
- #include <iostream>
- //#include <Windows.h>
- #define STICK_STEP 1000
- //#define OLD_CONTROLS
- using namespace std;
- //Так мне удобно сравнивать состояния геймпада на двух соседних проходах цикла
- bool operator== (JOYSTICK_POSITION &p1, JOYSTICK_POSITION &p2)
- {
- return (p1.bHats == p2.bHats) &&
- (p1.lButtons == p2.lButtons) &&
- (p1.wAxisX == p2.wAxisX) &&
- (p1.wAxisY == p2.wAxisY) &&
- (p1.wAxisZ == p2.wAxisZ) &&
- (p1.wAxisZRot == p2.wAxisZRot);
- }
- bool operator!= (JOYSTICK_POSITION &p1, JOYSTICK_POSITION &p2)
- {
- return !(p1==p2);
- }
- int
- __cdecl
- _tmain(__in int argc, __in PZPWSTR argv)
- {
- HDEVINFO hDeviceInfo;
- TCHAR ErrMsg[1000];
- ULONG bytes;
- HANDLE hDevice;
- USHORT X, Y, Z, ZR;
- JOYSTICK_POSITION iReport, iOldReport;
- PVOID pPositionMessage;
- UINT IoCode = LOAD_POSITIONS;
- UINT IoSize = sizeof(JOYSTICK_POSITION);
- HID_DEVICE_ATTRIBUTES attrib;
- // ---------------------------------------------------------------------------------------------------------- \\
- // Tests if vJoy device is installed
- // This call to the system requests installation information for the device by its ID (GUID_DEVINTERFACE_VJOY)
- // It does not tests its functionality and it does not care if it is disabled.
- // Returns a valid handle if a driver identified by GUID_DEVINTERFACE_VJOY is installed (Enabeled or Disabled)
- hDeviceInfo = SetupDiGetClassDevs(&GUID_DEVINTERFACE_VJOY, NULL, NULL, DIGCF_PRESENT | DIGCF_INTERFACEDEVICE);
- if (hDeviceInfo == INVALID_HANDLE_VALUE)
- {
- GetErrorString(ErrMsg,1000);
- _tprintf(_T("[E] SetupDiGetClassDevs failed with error: %s\n"), ErrMsg);
- return -1;
- };
- // ---------------------------------------------------------------------------------------------------------- \\
- // ---------------------------------------------------------------------------------------------------------- \\
- // Opens vJoy device for writing
- // Returns a valid handle to the device if the device is responsive
- // Returns an INvalid handle if the device is not responding for some reason
- hDevice = OpenJoystickDevice();
- if (hDevice == INVALID_HANDLE_VALUE)
- return -1;
- // ---------------------------------------------------------------------------------------------------------- \\
- // ---------------------------------------------------------------------------------------------------------- \\
- // Get the driver attributes (Vendor ID, Product ID, Version Number)
- attrib.Size = sizeof (HID_DEVICE_ATTRIBUTES); // Prepare the buffer that gets the data
- IoCode = IOCTL_VJOY_GET_ATTRIB; // The op-code for this request
- // Now - access the device (vJoy) with the right op-code and get the data into the buffer (attrib)
- BOOL res = DeviceIoControl(hDevice, IoCode, NULL, 0, &(attrib), sizeof (HID_DEVICE_ATTRIBUTES), &bytes, NULL);
- if (!res)
- {
- GetErrorString(ErrMsg,1000);
- _tprintf(_T("[E] IOCTL_VJOY_GET_ATTRIB failed with error: %s\n"), ErrMsg);
- }
- else
- _tprintf(_T("[I] VendorID:0x%04X ProductID:0x%04X VersionNumber:%d\n"), attrib.VendorID, attrib.ProductID, attrib.VersionNumber);
- // Expected result: "VendorID:0x1234 ProductID:0xBEAD VersionNumber:2"
- // ---------------------------------------------------------------------------------------------------------- \\
- // Initialize axes X,Y,Z,Z-Rotation
- X = 20;
- Y = 30;
- Z = 40;
- ZR = 80;
- int XValue = 0x3FFF;
- int YValue = 0x3FFF;
- int XValue_c = 0;
- int YValue_c = 0;
- while (1)
- {
- bool shift = 0!=GetAsyncKeyState(VK_LSHIFT);
- bool leftKey = 0!=GetAsyncKeyState(VK_LEFT);
- bool rightKey = 0!=GetAsyncKeyState(VK_RIGHT);
- bool upKey = 0!=GetAsyncKeyState(VK_UP);
- bool downKey = 0!=GetAsyncKeyState(VK_DOWN);
- bool homeKey = 0!=GetAsyncKeyState(VK_HOME);
- bool delKey = 0!=GetAsyncKeyState(VK_DELETE);
- bool endKey = 0!=GetAsyncKeyState(VK_END);
- bool pgdnKey = 0!=GetAsyncKeyState(VK_NEXT);
- #ifdef OLD_CONTROLS
- XValue = YValue = 0x3fff;
- if(!leftKey != !rightKey)
- XValue = leftKey ? 0 : 0x7FFF;
- if(!upKey != !downKey)
- YValue = upKey ? 0 : 0x7FFF;
- #else
- if(leftKey)
- {
- if(XValue_c > 0) XValue_c = 0;
- XValue_c -= STICK_STEP;
- }
- if(rightKey)
- {
- if(XValue_c < 0) XValue_c = 0;
- XValue_c += STICK_STEP;
- }
- if(upKey)
- {
- if(YValue_c > 0) YValue_c = 0;
- YValue_c -= STICK_STEP;
- }
- if(downKey)
- {
- if(YValue_c < 0) YValue_c = 0;
- YValue_c += STICK_STEP;
- }
- if(XValue_c<-16383) XValue_c = -16383;
- if(YValue_c<-16383) YValue_c = -16383;
- if(XValue_c>16384) XValue_c = 16384;
- if(YValue_c>16384) YValue_c = 16384;
- if (!leftKey && !rightKey)
- {
- XValue_c = 0;
- }
- if(!upKey && !downKey)
- {
- /*if (abs(XValue_c)<(2*STICK_STEP)) XValue_c = 0;
- else XValue_c -= ((XValue_c>0) ? (2*STICK_STEP): (-2*STICK_STEP));
- if (abs(YValue_c)<(2*STICK_STEP)) YValue_c = 0;
- else YValue_c -= ((YValue_c<0) ? -(2*STICK_STEP): (2*STICK_STEP));*/
- YValue_c = 0;
- }
- /*if(!upKey && !downKey)
- {
- }*/
- XValue = XValue_c + 0x3FFF;
- YValue = YValue_c + 0x3fff;
- #endif
- if(!shift)
- {
- X = XValue;
- Y = YValue;
- Z = ZR = 0x3FFF;
- }
- else
- {
- Z = XValue;
- ZR = YValue;
- X = Y = 0x3FFF;
- }
- iReport.bHats = 4;
- if(delKey) iReport.bHats = 3;
- if(homeKey) iReport.bHats = 0;
- if(pgdnKey) iReport.bHats = 1;
- if(endKey) iReport.bHats = 2;
- // Set axes X,Y,Z,Z-Rotation to current values
- iReport.wAxisX=X;
- iReport.wAxisY=Y;
- iReport.wAxisZ=Z;
- iReport.wAxisZRot=ZR;
- iReport.lButtons = 0;
- if (0!=GetAsyncKeyState(0x41)) iReport.lButtons |= 0x1; //VJoy Button1 (X Button) is set to "A" key
- if (0!=GetAsyncKeyState(0x53)) iReport.lButtons |= 0x2; //(A Button) => "S" key
- if (0!=GetAsyncKeyState(0x44)) iReport.lButtons |= 0x4; //(B Button) => "D" key
- if (0!=GetAsyncKeyState(0x57)) iReport.lButtons |= 0x8; //(Y Button) => "W" key
- if (0!=GetAsyncKeyState(0x51)) iReport.lButtons |= 0x10; //(LB button) => "Q" key
- if (0!=GetAsyncKeyState(0x45)) iReport.lButtons |= 0x20; //(RB button) => "E" key
- if (0!=GetAsyncKeyState(0x5a)) iReport.lButtons |= 0x40; //(LT button) => "Z" key
- if ((0!=GetAsyncKeyState(0x43)) || shift) iReport.lButtons |= 0x80; //(RT button) => "C" key or LSHIFT
- if (0!=GetAsyncKeyState(VK_ESCAPE)) iReport.lButtons |= 0x100; //(Start button) => ESC, как ни странно
- if (0!=GetAsyncKeyState(VK_RETURN)) iReport.lButtons |= 0x200; //(Back button) => Enter, что удивительно. Мне так нравится.
- if(iOldReport != iReport)
- {
- // Buffer hoding the joystick position (iReport) is ready
- // Cast it to PVOID and print some of it
- pPositionMessage = (PVOID)(&iReport);
- //_tprintf(_T("Input: X=%4x, Y=%4x, Buttons=%X; \n"), iReport.wAxisX, iReport.wAxisY, iReport.lButtons);
- // Prepare op-code and buffer size for access to vJoy device
- IoCode = LOAD_POSITIONS;
- IoSize = sizeof(JOYSTICK_POSITION);
- // Send joystick position structure to vJoy device
- if (!DeviceIoControl (hDevice, IoCode, pPositionMessage, IoSize, NULL, 0, &bytes, NULL))
- {
- _tprintf(_T("Ioctl to vJoy device failed\n"));
- break;
- }
- iOldReport = iReport;
- }
- Sleep(1);
- };
- // Loop interupted - close handle and exit
- CloseHandle(hDevice);
- _tprintf(_T("OK\n"));
- return 0;
- }
- /*
- Open vJoy device
- The handle this function returns will be used as the means to communicate with the device
- Return handle to open device or INVALID_HANDLE_VALUE
- */
- HANDLE OpenJoystickDevice(void)
- {
- HANDLE hDevice;
- TCHAR ErrMsg[1000];
- // At the moment vJoy is opened as DOS_FILE_NAME ("\\.\PPJoyIOCTL1") - ASCII only!
- _tprintf(_T("CreateFile: %s\n"), DOS_FILE_NAME);
- hDevice = CreateFileA(DOS_FILE_NAME, GENERIC_WRITE | GENERIC_READ, FILE_SHARE_WRITE | FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
- if (hDevice == INVALID_HANDLE_VALUE)
- {
- DWORD err = GetErrorString(ErrMsg,1000);
- _tprintf(_T("[E=0x%x] CreateFile failed for %s with error: %s\n"), err, DOS_FILE_NAME, ErrMsg);
- return INVALID_HANDLE_VALUE;
- };
- return hDevice;
- }
- /* Helper Functions */
- DWORD GetErrorString(TCHAR * Msg, int Size)
- {
- TCHAR * s;
- DWORD errorcode = GetLastError();
- int nTChars = FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, NULL, errorcode, 0, (LPSTR)&s, 0, NULL);
- if (!nTChars)
- return errorcode;
- _tcsncpy_s(Msg, Size, s, Size);
- LocalFree(s);
- return errorcode;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement