rnd-19981017-1
[rocksndiamonds.git] / src / joystick.c
1 /***********************************************************
2 *  Rocks'n'Diamonds -- McDuffin Strikes Back!              *
3 *----------------------------------------------------------*
4 *  (c) 1995-98 Artsoft Entertainment                       *
5 *              Holger Schemel                              *
6 *              Oststrasse 11a                              *
7 *              33604 Bielefeld                             *
8 *              phone: ++49 +521 290471                     *
9 *              email: aeglos@valinor.owl.de                *
10 *----------------------------------------------------------*
11 *  joystick.c                                              *
12 ***********************************************************/
13
14 #ifdef __FreeBSD__
15 #include <machine/joystick.h>
16 #endif
17
18 #include "joystick.h"
19 #include "misc.h"
20
21 void CheckJoystickData()
22 {
23   int i;
24   int distance = 100;
25
26   for(i=0;i<2;i++)
27   {
28     if (joystick[i].xmiddle <= distance)
29       joystick[i].xmiddle = distance;
30     if (joystick[i].ymiddle <= distance)
31       joystick[i].ymiddle = distance;
32
33     if (joystick[i].xleft >= joystick[i].xmiddle)
34       joystick[i].xleft = joystick[i].xmiddle-distance;
35     if (joystick[i].xright <= joystick[i].xmiddle)
36       joystick[i].xright = joystick[i].xmiddle+distance;
37
38     if (joystick[i].yupper >= joystick[i].ymiddle)
39       joystick[i].yupper = joystick[i].ymiddle-distance;
40     if (joystick[i].ylower <= joystick[i].ymiddle)
41       joystick[i].ylower = joystick[i].ymiddle+distance;
42   }
43 }
44
45 int JoystickPosition(int middle, int margin, int actual)
46 {
47   long range, pos;
48   int percentage;
49
50   if (margin<middle && actual>middle)
51     return(0);
52   if (margin>middle && actual<middle)
53     return(0);
54
55   range = ABS(margin-middle);
56   pos = ABS(actual-middle);
57   percentage = (int)(pos*100/range);
58   if (percentage>100)
59     percentage = 100;
60
61   return(percentage);
62 }
63
64 int Joystick()
65 {
66 #ifdef __FreeBSD__
67   struct joystick joy_ctrl;
68 #else
69   struct joystick_control
70   {
71     int buttons;
72     int x;
73     int y;
74   } joy_ctrl;
75 #endif
76
77   int js_x,js_y, js_b1,js_b2;
78   int left, right, up, down;
79   int result=0;
80
81   if (joystick_status==JOYSTICK_OFF)
82     return(0);
83
84 #ifndef MSDOS
85   if (read(joystick_device, &joy_ctrl, sizeof(joy_ctrl)) != sizeof(joy_ctrl))
86   {
87     Error(ERR_RETURN, "cannot read joystick settings - no joystick support");
88     joystick_status = JOYSTICK_OFF;
89     return(0);
90   }
91
92   js_x  = joy_ctrl.x;
93   js_y  = joy_ctrl.y;
94 #ifdef __FreeBSD__
95   js_b1 = joy_ctrl.b1;
96   js_b2 = joy_ctrl.b2;
97 #else
98   js_b1 = joy_ctrl.buttons & 1;
99   js_b2 = joy_ctrl.buttons & 2;
100 #endif
101
102   left = JoystickPosition(joystick[setup.joystick_nr].xmiddle,
103                           joystick[setup.joystick_nr].xleft,  js_x);
104   right = JoystickPosition(joystick[setup.joystick_nr].xmiddle,
105                            joystick[setup.joystick_nr].xright, js_x);
106   up =    JoystickPosition(joystick[setup.joystick_nr].ymiddle,
107                            joystick[setup.joystick_nr].yupper, js_y);
108   down =  JoystickPosition(joystick[setup.joystick_nr].ymiddle,
109                            joystick[setup.joystick_nr].ylower, js_y);
110
111   if (left>JOYSTICK_PERCENT)
112     result |= JOY_LEFT;
113   else if (right>JOYSTICK_PERCENT)
114     result |= JOY_RIGHT;
115   if (up>JOYSTICK_PERCENT)
116     result |= JOY_UP;
117   else if (down>JOYSTICK_PERCENT)
118     result |= JOY_DOWN;
119   if (js_b1)
120     result |= JOY_BUTTON_1;
121   if (js_b2)
122     result |= JOY_BUTTON_2;
123
124   return(result);
125 #else
126   return(0);
127 #endif
128 }
129
130 int JoystickButton()
131 {
132   static int last_joy_button = 0;
133   int joy_button = (Joystick() & JOY_BUTTON);
134   int result;
135
136   if (joy_button)
137   {
138     if (last_joy_button)
139       result = JOY_BUTTON_PRESSED;
140     else
141       result = JOY_BUTTON_NEW_PRESSED;
142   }
143   else
144   {
145     if (last_joy_button)
146       result = JOY_BUTTON_NEW_RELEASED;
147     else
148       result = JOY_BUTTON_NOT_PRESSED;
149   }
150
151   last_joy_button = joy_button;
152   return(result);
153 }