FreeBASIC  0.91.0
gfx_joystick.c
Go to the documentation of this file.
1 /* joystick handling, win32 */
2 
3 #include "../fb_gfx.h"
4 #include "fb_gfx_win32.h"
5 #include <mmsystem.h>
6 
7 #define CALCPOS(pos, min, max) (((((float)(pos) - (float)(min)) * 2.0) / ((float)(max) - (float)(min) + 1.0)) - 1.0)
8 
9 typedef struct _JOYDATA {
11  JOYCAPS caps;
12 } JOYDATA;
13 
14 static JOYDATA joy[16];
15 static int inited = FALSE;
16 
17 #define POV_LAP 2250
18 
19 FBCALL int fb_GfxGetJoystick(int id, ssize_t *buttons, float *a1, float *a2, float *a3, float *a4, float *a5, float *a6, float *a7, float *a8)
20 {
21  JOYINFOEX info;
22  JOYDATA *j;
23 
24  *buttons = -1;
25  *a1 = *a2 = *a3 = *a4 = *a5 = *a6 = *a7 = *a8 = -1000.0;
26 
27  if (!inited) {
28  fb_hMemSet(joy, 0, sizeof(JOYDATA) * 16);
29  inited = TRUE;
30  }
31 
32  if ((id < 0) || (id > 15))
34  j = &joy[id];
35 
36  if (!j->detected) {
37  j->detected = TRUE;
38  if (joyGetDevCaps(id + JOYSTICKID1, &j->caps, sizeof(j->caps)) != JOYERR_NOERROR)
40  j->available = TRUE;
41  }
42  if (!j->available)
44 
45  info.dwSize = sizeof(info);
46  info.dwFlags = JOY_RETURNALL;
47  if (joyGetPosEx(id + JOYSTICKID1, &info) != JOYERR_NOERROR)
49  *a1 = CALCPOS(info.dwXpos, j->caps.wXmin, j->caps.wXmax);
50  *a2 = CALCPOS(info.dwYpos, j->caps.wYmin, j->caps.wYmax);
51  if (j->caps.wCaps & JOYCAPS_HASZ)
52  *a3 = CALCPOS(info.dwZpos, j->caps.wZmin, j->caps.wZmax);
53  if (j->caps.wCaps & JOYCAPS_HASR)
54  *a4 = CALCPOS(info.dwRpos, j->caps.wRmin, j->caps.wRmax);
55  if (j->caps.wCaps & JOYCAPS_HASU)
56  *a5 = CALCPOS(info.dwUpos, j->caps.wUmin, j->caps.wUmax);
57  if (j->caps.wCaps & JOYCAPS_HASV)
58  *a6 = CALCPOS(info.dwVpos, j->caps.wVmin, j->caps.wVmax);
59  if (j->caps.wCaps & JOYCAPS_HASPOV)
60  {
61  *a7 = *a8 = 0;
62 
63  if(( info.dwPOV > 4500 - POV_LAP ) && ( info.dwPOV < 13500 + POV_LAP ))
64  *a7 = 1;
65  else if(( info.dwPOV > 22500 - POV_LAP ) && ( info.dwPOV < 31500 + POV_LAP ))
66  *a7 = -1;
67 
68  if(( info.dwPOV > 13500 - POV_LAP ) && ( info.dwPOV < 22500 + POV_LAP ))
69  *a8 = 1;
70  else if(( info.dwPOV >= 0 ) && ( info.dwPOV < 4500 + POV_LAP ))
71  *a8 = -1;
72  else if(( info.dwPOV > 31500 - POV_LAP ) && ( info.dwPOV < 36000 ))
73  *a8 = -1;
74  }
75 
76  *buttons = info.dwButtons;
77 
78  return fb_ErrorSetNum( FB_RTERROR_OK );
79 }