Advertisement
Guest User

VJoy Feeder for FIFA12 v0.01

a guest
Dec 30th, 2011
535
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.85 KB | None | 0 0
  1. //////////////////////////////////////////////////////////////////////
  2. // This console application demonstrates how to wrive a vJoy client
  3. // Не везде удалял авторские комменты. Виноват, я ленивый.
  4. //Тут где-то был копирайт, так вот, самая большая няша, которой мы обязаны
  5. //этим кодом, - это Shaul Eizikovich. Большое ему спасибо.
  6. //////////////////////////////////////////////////////////////////////
  7.  
  8. #include "stdafx.h"
  9. #include "vjoyclient.h"
  10. #include <malloc.h>
  11. #include <iostream>
  12. //#include <Windows.h>
  13.  
  14. using namespace std;
  15.  
  16. //Так мне удобно сравнивать состояния геймпада на двух соседних проходах цикла
  17. bool operator== (JOYSTICK_POSITION &p1, JOYSTICK_POSITION &p2)
  18. {
  19.     return (p1.bHats == p2.bHats) &&
  20.         (p1.lButtons == p2.lButtons) &&
  21.         (p1.wAxisX == p2.wAxisX) &&
  22.         (p1.wAxisY == p2.wAxisY) &&
  23.         (p1.wAxisZ == p2.wAxisZ) &&
  24.         (p1.wAxisZRot == p2.wAxisZRot);
  25. }
  26. bool operator!= (JOYSTICK_POSITION &p1, JOYSTICK_POSITION &p2)
  27. {
  28.     return !(p1==p2);
  29. }
  30. int
  31. __cdecl
  32. _tmain(__in int argc, __in PZPWSTR argv)
  33. {
  34.  
  35.     HDEVINFO hDeviceInfo;
  36.     TCHAR ErrMsg[1000];
  37.     ULONG  bytes;
  38.     HANDLE hDevice;
  39.     USHORT X, Y, Z, ZR;
  40.     JOYSTICK_POSITION   iReport, iOldReport;
  41.     PVOID pPositionMessage;
  42.     UINT    IoCode = LOAD_POSITIONS;
  43.     UINT    IoSize = sizeof(JOYSTICK_POSITION);
  44.     HID_DEVICE_ATTRIBUTES attrib;
  45.  
  46.  
  47.     // ---------------------------------------------------------------------------------------------------------- \\
  48.     // Tests if vJoy device is installed
  49.     // This call to the system requests installation information for the device by its ID (GUID_DEVINTERFACE_VJOY)
  50.     // It does not tests its functionality and it does not care if it is disabled.
  51.     // Returns a valid handle if a driver identified by GUID_DEVINTERFACE_VJOY is installed (Enabeled or Disabled)
  52.     hDeviceInfo = SetupDiGetClassDevs(&GUID_DEVINTERFACE_VJOY, NULL, NULL, DIGCF_PRESENT | DIGCF_INTERFACEDEVICE);
  53.     if (hDeviceInfo == INVALID_HANDLE_VALUE)
  54.     {
  55.         GetErrorString(ErrMsg,1000);
  56.         _tprintf(_T("[E] SetupDiGetClassDevs failed with error: %s\n"), ErrMsg);
  57.         return -1;
  58.     };
  59.     // ---------------------------------------------------------------------------------------------------------- \\
  60.  
  61.  
  62.     // ---------------------------------------------------------------------------------------------------------- \\
  63.     // Opens vJoy device for writing
  64.     // Returns a valid handle to the device if the device is responsive
  65.     // Returns an INvalid handle if the device is not responding for some reason
  66.     hDevice = OpenJoystickDevice();
  67.     if (hDevice  == INVALID_HANDLE_VALUE)
  68.         return -1;
  69.     // ---------------------------------------------------------------------------------------------------------- \\
  70.  
  71.  
  72.     // ---------------------------------------------------------------------------------------------------------- \\
  73.     // Get the driver attributes (Vendor ID, Product ID, Version Number)
  74.     attrib.Size = sizeof (HID_DEVICE_ATTRIBUTES);   // Prepare the buffer that gets the data
  75.     IoCode = IOCTL_VJOY_GET_ATTRIB;                 // The op-code for this request
  76.  
  77.     // Now - access the device (vJoy) with the right op-code and get the data into the buffer (attrib)
  78.     BOOL res = DeviceIoControl(hDevice, IoCode, NULL, 0, &(attrib), sizeof (HID_DEVICE_ATTRIBUTES), &bytes, NULL);
  79.     if (!res)
  80.     {
  81.         GetErrorString(ErrMsg,1000);
  82.         _tprintf(_T("[E] IOCTL_VJOY_GET_ATTRIB failed with error: %s\n"), ErrMsg);
  83.     }
  84.     else
  85.         _tprintf(_T("[I] VendorID:0x%04X ProductID:0x%04X VersionNumber:%d\n"), attrib.VendorID,  attrib.ProductID, attrib.VersionNumber);
  86.     // Expected result: "VendorID:0x1234 ProductID:0xBEAD VersionNumber:2"
  87.     // ---------------------------------------------------------------------------------------------------------- \\
  88.  
  89.     // Initialize axes X,Y,Z,Z-Rotation
  90.     X = 20;
  91.     Y = 30;
  92.     Z = 40;
  93.     ZR = 80;  
  94.    
  95.     while (1)
  96.     {
  97.        
  98.         bool shift = 0!=GetAsyncKeyState(VK_LSHIFT);
  99.  
  100.         bool leftKey = 0!=GetAsyncKeyState(VK_LEFT);
  101.         bool rightKey = 0!=GetAsyncKeyState(VK_RIGHT);
  102.         bool upKey = 0!=GetAsyncKeyState(VK_UP);
  103.         bool downKey = 0!=GetAsyncKeyState(VK_DOWN);
  104.  
  105.         bool homeKey = 0!=GetAsyncKeyState(VK_HOME);
  106.         bool delKey = 0!=GetAsyncKeyState(VK_DELETE);
  107.         bool endKey = 0!=GetAsyncKeyState(VK_END);
  108.         bool pgdnKey = 0!=GetAsyncKeyState(VK_NEXT);
  109.  
  110.         Y = X = 0x3fff;    
  111.         Z = ZR = 0x3fff;
  112.         if(!leftKey != !rightKey)
  113.         {
  114.             if (!shift)
  115.                 X = leftKey ? 0 : 0x7FFF;
  116.             else
  117.                 Z = leftKey ? 0 : 0x7FFF;
  118.         }
  119.         if(!upKey != !downKey)
  120.         {
  121.             if(!shift)
  122.                 Y = upKey ? 0 : 0x7FFF;
  123.             else
  124.                 ZR = upKey ? 0 : 0x7FFF;
  125.         }
  126.  
  127.         iReport.bHats = 4;
  128.         if(delKey) iReport.bHats = 3;
  129.         if(homeKey) iReport.bHats = 0;
  130.         if(pgdnKey) iReport.bHats = 1;
  131.         if(endKey) iReport.bHats = 2;
  132.          
  133.         // Set axes X,Y,Z,Z-Rotation to current values
  134.         iReport.wAxisX=X;
  135.         iReport.wAxisY=Y;
  136.         iReport.wAxisZ=Z;
  137.         iReport.wAxisZRot=ZR;
  138.        
  139.  
  140.         iReport.lButtons = 0;
  141.         if (0!=GetAsyncKeyState(0x41)) iReport.lButtons |= 0x1; //VJoy Button1 (X Button) is set to "A" key
  142.         if (0!=GetAsyncKeyState(0x53)) iReport.lButtons |= 0x2; //(A Button) => "S" key
  143.         if (0!=GetAsyncKeyState(0x44)) iReport.lButtons |= 0x4; //(B Button) => "D" key
  144.         if (0!=GetAsyncKeyState(0x57)) iReport.lButtons |= 0x8; //(Y Button) => "W" key
  145.         if (0!=GetAsyncKeyState(0x51)) iReport.lButtons |= 0x10; //(LB button) => "Q" key
  146.         if (0!=GetAsyncKeyState(0x45)) iReport.lButtons |= 0x20; //(RB button) => "E" key
  147.         if (0!=GetAsyncKeyState(0x5a)) iReport.lButtons |= 0x40; //(LT button) => "Z" key
  148.         if ((0!=GetAsyncKeyState(0x43)) || shift) iReport.lButtons |= 0x80; //(RT button) => "C" key or LSHIFT
  149.         if (0!=GetAsyncKeyState(VK_ESCAPE)) iReport.lButtons |= 0x100; //(Start button) => ESC, как ни странно
  150.         if (0!=GetAsyncKeyState(VK_RETURN)) iReport.lButtons |= 0x200; //(Back button) => Enter, что удивительно. Мне так нравится.
  151.          
  152.         if(iOldReport != iReport)
  153.         {
  154.             // Buffer hoding the joystick position (iReport) is ready
  155.             // Cast it to PVOID and print some of it       
  156.             pPositionMessage = (PVOID)(&iReport);
  157.             //_tprintf(_T("Input: X=%4x, Y=%4x, Buttons=%X; \n"), iReport.wAxisX, iReport.wAxisY, iReport.lButtons);
  158.        
  159.             // Prepare op-code and buffer size for access to vJoy device
  160.             IoCode = LOAD_POSITIONS;
  161.             IoSize = sizeof(JOYSTICK_POSITION);
  162.  
  163.             // Send joystick position structure to vJoy device
  164.             if (!DeviceIoControl (hDevice, IoCode, pPositionMessage, IoSize, NULL, 0, &bytes, NULL))
  165.             {
  166.                 _tprintf(_T("Ioctl to vJoy device failed\n"));
  167.                 break;
  168.             }
  169.  
  170.             iOldReport = iReport;
  171.         }
  172.         Sleep(0);
  173.     };
  174.  
  175.  
  176.     // Loop interupted - close handle and exit
  177.     CloseHandle(hDevice);
  178.     _tprintf(_T("OK\n"));
  179.  
  180.     return 0;
  181. }
  182.  
  183. /*
  184. Open vJoy device
  185. The handle this function returns will be used as the means to communicate with the device
  186. Return handle to open device or INVALID_HANDLE_VALUE
  187. */
  188. HANDLE OpenJoystickDevice(void)
  189. {
  190.     HANDLE hDevice;
  191.     TCHAR ErrMsg[1000];
  192.  
  193.  
  194.     // At the moment vJoy is opened as DOS_FILE_NAME ("\\.\PPJoyIOCTL1") - ASCII only!
  195.     _tprintf(_T("CreateFile:  %s\n"), DOS_FILE_NAME);
  196.     hDevice = CreateFileA(DOS_FILE_NAME, GENERIC_WRITE | GENERIC_READ, FILE_SHARE_WRITE | FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
  197.     if (hDevice == INVALID_HANDLE_VALUE)
  198.     {
  199.         DWORD err = GetErrorString(ErrMsg,1000);
  200.         _tprintf(_T("[E=0x%x] CreateFile failed for %s with error: %s\n"), err, DOS_FILE_NAME, ErrMsg);
  201.         return INVALID_HANDLE_VALUE;
  202.     };
  203.     return hDevice;
  204. }
  205.  
  206.  
  207.  
  208. /* Helper Functions */
  209. DWORD GetErrorString(TCHAR * Msg, int Size)
  210. {
  211.     TCHAR * s;
  212.     DWORD errorcode = GetLastError();
  213.     int nTChars = FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,  NULL, errorcode, 0, (LPSTR)&s,  0, NULL);
  214.     if (!nTChars)
  215.         return errorcode;
  216.  
  217.     _tcsncpy_s(Msg, Size, s, Size);
  218.     LocalFree(s);
  219.     return errorcode;
  220. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement