out_ds_joy.cpp 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #include "out_ds.h"
  2. #include <dinput.h>
  3. #include <math.h>
  4. static IDirectInput8 * pDI;
  5. static IDirectInputDevice8 * pDev;
  6. #ifndef HAVE_JOY
  7. #error nein!
  8. #endif
  9. static BOOL CALLBACK eCallback(LPCDIDEVICEINSTANCE dev,void * foop)
  10. {
  11. *(GUID*)foop=dev->guidInstance;
  12. return DIENUM_STOP;
  13. }
  14. static bool captured;
  15. #define joy_id JOYSTICKID1
  16. #define POLL 5
  17. void wa2_hack_setpitch(double d);
  18. static double joy_read()
  19. {
  20. DIJOYSTATE2 stat;
  21. if (SUCCEEDED(pDev->GetDeviceState(sizeof(stat),&stat)))
  22. {
  23. return pow(2,(double)stat.lX/(double)0x8000)/2.0;
  24. }
  25. else return 1;
  26. }
  27. void wa2_hack_joy_update()
  28. {
  29. wa2_hack_setpitch(joy_read());
  30. }
  31. static HANDLE hThread;
  32. static bool die;
  33. static DWORD _stdcall joy_thread(void*)
  34. {
  35. while(!die)
  36. {
  37. wa2_hack_setpitch(joy_read());
  38. Sleep(10);
  39. }
  40. return 0;
  41. }
  42. void wa2_hack_joy_init()
  43. {
  44. if (!hThread)
  45. {
  46. DirectInput8Create(mod.hDllInstance,DIRECTINPUT_VERSION,IID_IDirectInput8,(void**)&pDI,0);
  47. if (pDI)
  48. {
  49. GUID foop;
  50. pDI->EnumDevices(DI8DEVCLASS_GAMECTRL,eCallback,&foop,DIEDFL_ATTACHEDONLY);
  51. pDI->CreateDevice(foop,&pDev,0);
  52. if (pDev)
  53. {
  54. pDev->SetDataFormat(&c_dfDIJoystick2);
  55. DIPROPDWORD dw;
  56. dw.dwData=1000;
  57. dw.diph.dwSize=sizeof(DIPROPDWORD);
  58. dw.diph.dwHeaderSize=sizeof(DIPROPHEADER);
  59. dw.diph.dwObj=0;
  60. dw.diph.dwHow=DIPH_DEVICE;
  61. pDev->SetProperty(DIPROP_DEADZONE,&dw.diph);
  62. pDev->SetCooperativeLevel(mod.hMainWindow,DISCL_BACKGROUND);
  63. pDev->Acquire();
  64. die=0;
  65. DWORD id;
  66. hThread=CreateThread(0,0,joy_thread,0,0,&id);
  67. SetThreadPriority(hThread,THREAD_PRIORITY_TIME_CRITICAL);
  68. }
  69. else {pDI->Release();pDI=0;}
  70. }
  71. }
  72. }
  73. void wa2_hack_joy_deinit()
  74. {
  75. if (hThread)
  76. {
  77. die=1;
  78. WaitForSingleObject(hThread,INFINITE);
  79. CloseHandle(hThread);
  80. hThread=0;
  81. if (pDev) {pDev->Unacquire();pDev->Release();pDev=0;}
  82. if (pDI) {pDI->Release();pDI=0;}
  83. }
  84. }