FreeBASIC  0.91.0
gfx_joystick.c
Go to the documentation of this file.
1 /* joystick handling, dos */
2 
3 #include "../fb_gfx.h"
4 #include "fb_gfx_dos.h"
5 #include <pc.h>
6 #include <go32.h>
7 
8 #define JOY_PORT 0x201
9 #define JOY_AXIS1 0x1
10 #define JOY_AXIS2 0x2
11 #define JOY_AXIS3 0x4
12 #define JOY_AXIS4 0x8
13 #define JOY_COUNT 100000
14 
15 #define CALCPOS(pos, min, max) (((((float)(pos) - (float)(min)) * 2.0) / ((float)(max) - (float)(min) + 1.0)) - 1.0)
16 
17 static int detected = TRUE;
18 
20 static int max_x1 = 0, max_y1 = 0, max_x2 = 0, max_y2 = 0;
21 
22 __dpmi_regs regs;
23 
24 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)
25 {
26  unsigned char status;
27  int count;
28  int x1 = 0, y1 = 0, x2 = 0, y2 = 0;
29 
30  if (!detected) {
31  *buttons = -1;
32  *a1 = *a2 = *a3 = *a4 = *a5 = *a6 = *a7 = *a8 = -1000.0;
34  }
35 
36  fb_dos_cli();
37 
38  outportb(0x201, 0);
39 
40  for (count = 0; count < JOY_COUNT; count++) {
41  status = inportb(0x201);
42 
43  if (status & JOY_AXIS1)
44  x1++;
45 
46  if (status & JOY_AXIS2)
47  y1++;
48 
49  if (status & JOY_AXIS3)
50  x2++;
51 
52  if (status & JOY_AXIS4)
53  y2++;
54 
55  if ((status & 0xF) == 0)
56  break;
57  }
58 
59  fb_dos_sti();
60 
61  if ((x1 >= JOY_COUNT) || (y1 >= JOY_COUNT)) {
62  detected = FALSE;
63  *buttons = -1;
64  *a1 = *a2 = *a3 = *a4 = *a5 = *a6 = -1000.0;
66  }
67 
68  if (x1 < min_x1)
69  min_x1 = x1;
70 
71  if (y1 < min_y1)
72  min_y1 = y1;
73 
74  if (x2 < min_x2)
75  min_x2 = x2;
76 
77  if (y2 < min_y2)
78  min_y2 = y2;
79 
80 
81  if (x1 > max_x1)
82  max_x1 = x1;
83 
84  if (y1 > max_y1)
85  max_y1 = y1;
86 
87  if (x2 > max_x2)
88  max_x2 = x2;
89 
90  if (y2 > max_y2)
91  max_y2 = y2;
92 
93  *a1 = CALCPOS(x1, min_x1, max_x1);
94  *a2 = CALCPOS(y1, min_y1, max_y1);
95  *a3 = CALCPOS(x2, min_x2, max_x2);
96  *a4 = CALCPOS(y2, min_y2, max_y2);
97 
98  status = inportb(JOY_PORT);
99 
100  *buttons = (~status >> 4) & 0xF;
101 
102  *a5 = *a6 = *a7 = *a8 = -1000.0;
103 
104  return fb_ErrorSetNum( FB_RTERROR_OK );
105 }