freejoy.win32.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. // freejoy.win32.c
  2. #define WIN32_LEAN_AND_MEAN
  3. #include <windows.h>
  4. #include <mmsystem.h>
  5. #include "freejoy.h"
  6. int joyhandle[256];
  7. int JoyCount()
  8. {
  9. JOYINFO j;
  10. int n,i,t,res;
  11. n=joyGetNumDevs();
  12. t=0;
  13. for (i=0;i<n;i++)
  14. {
  15. res=joyGetPos(i,&j);
  16. if (res==JOYERR_NOERROR && t<256) joyhandle[t++]=i;
  17. }
  18. return t;
  19. }
  20. char *JoyCName(int port)
  21. {
  22. static JOYCAPS joycaps;
  23. int res;
  24. port=joyhandle[port];
  25. res=joyGetDevCaps(port,&joycaps,sizeof(JOYCAPS));
  26. if (res!=JOYERR_NOERROR) return 0;
  27. return joycaps.szPname;
  28. }
  29. int JoyButtonCaps(int port)
  30. {
  31. JOYCAPS caps;
  32. int res,mask;
  33. port=joyhandle[port];
  34. res=joyGetDevCaps(port,&caps,sizeof(JOYCAPS));
  35. if (res!=JOYERR_NOERROR) return 0;
  36. mask=(1<<caps.wNumButtons)-1;
  37. return mask;
  38. }
  39. int JoyAxisCaps(int port)
  40. {
  41. JOYCAPS caps;
  42. int res,mask;
  43. port=joyhandle[port];
  44. res=joyGetDevCaps(port,&caps,sizeof(JOYCAPS));
  45. if (res!=JOYERR_NOERROR) return 0;
  46. mask=(1<<caps.wNumAxes)-1;
  47. if (caps.wCaps&JOYCAPS_HASPOV) mask|=(1<<JOYHAT);
  48. return mask;
  49. }
  50. int ReadJoy(int port,int *buttons,float *axis)
  51. {
  52. JOYCAPS caps;
  53. JOYINFOEX j;
  54. int res,f,pov;
  55. port=joyhandle[port];
  56. res=joyGetDevCaps(port,&caps,sizeof(JOYCAPS));
  57. if (res!=JOYERR_NOERROR) return 0;
  58. j.dwSize=sizeof(JOYINFOEX);
  59. j.dwFlags=JOY_RETURNALL;
  60. res=joyGetPosEx(port,&j);
  61. if (res!=JOYERR_NOERROR) return 0;
  62. *buttons=j.dwButtons;
  63. f=j.dwFlags;
  64. if (f&JOY_RETURNX) axis[JOYX]=-1.0+2.0*(j.dwXpos-caps.wXmin)/caps.wXmax;
  65. if (f&JOY_RETURNY) axis[JOYY]=-1.0+2.0*(j.dwYpos-caps.wYmin)/caps.wYmax;
  66. if (f&JOY_RETURNZ) axis[JOYZ]=-1.0+2.0*(j.dwZpos-caps.wZmin)/caps.wZmax;
  67. if (f&JOY_RETURNR) axis[JOYR]=-1.0+2.0*(j.dwRpos-caps.wRmin)/caps.wRmax;
  68. if (f&JOY_RETURNU) axis[JOYU]=-1.0+2.0*(j.dwUpos-caps.wUmin)/caps.wUmax;
  69. if (f&JOY_RETURNV) axis[JOYV]=-1.0+2.0*(j.dwVpos-caps.wVmin)/caps.wVmax;
  70. if (f&JOY_RETURNPOV)
  71. {
  72. pov=j.dwPOV;
  73. if (pov<0 || pov>36000) axis[JOYHAT]=-1.0;else axis[JOYHAT]=pov/36000.0;
  74. }
  75. return 1;
  76. }
  77. void WriteJoy(int port,int channel,float value)
  78. {
  79. port=joyhandle[port];
  80. }