Merge branch 'feature/handle-quit-event'
[jumpnbump.git] / dos / input.c
blobe7dc6e08dbe9251aa7395020f616362ea1157c95
1 /*
2 * input.h
3 * Copyright (C) 1998 Brainchild Design - http://brainchilddesign.com/
4 *
5 * Copyright (C) 2002 Florian Schulze - crow@icculus.org
7 * This file is part of Jump'n'Bump.
9 * Jump'n'Bump is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * Jump'n'Bump is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include "globals.h"
26 static int num_mouse_buttons;
28 int init_joy(void)
30 int c1;
32 outportb(0x201, 0);
33 while (c1 < 0x7fff) {
34 if ((inportb(0x201) & 1) == 0)
35 break;
36 c1++;
38 if (c1 != 0x7fff)
39 return 1;
40 else
41 return 0;
45 void read_joy(void)
47 int c1;
48 int x, y;
49 int s1 = 0;
50 char flag;
52 c1 = x = y = flag = 0;
53 outportb(0x201, 0);
55 while (1) {
57 s1 = inportb(0x201);
59 if (x == 0) {
60 if ((s1 & 1) == 0)
61 x = c1;
63 if (y == 0) {
64 if ((s1 & 2) == 0)
65 y = c1;
67 if (x != 0 && y != 0)
68 break;
70 c1++;
71 if (c1 == 0x7fff) {
72 flag = 1;
73 break;
78 if (flag == 0) {
79 joy.raw_x = x;
80 joy.raw_y = y;
82 if (joy.raw_x < joy.calib_data.x2)
83 joy.x = ((long) (joy.raw_x - joy.calib_data.x2) << 10) / (joy.calib_data.x2 - joy.calib_data.x1);
84 else
85 joy.x = ((long) (joy.raw_x - joy.calib_data.x2) << 10) / (joy.calib_data.x3 - joy.calib_data.x2);
86 if (joy.raw_y < joy.calib_data.y2)
87 joy.y = ((long) (joy.raw_y - joy.calib_data.y2) << 10) / (joy.calib_data.y2 - joy.calib_data.y1);
88 else
89 joy.y = ((long) (joy.raw_y - joy.calib_data.y2) << 10) / (joy.calib_data.y3 - joy.calib_data.y2);
91 if (joy.x < -1024)
92 joy.x = -1024;
93 if (joy.x > 1024)
94 joy.x = 1024;
95 if (joy.y < -1024)
96 joy.y = -1024;
97 if (joy.y > 1024)
98 joy.y = 1024;
100 s1 = inportb(0x201);
101 joy.but1 = (((s1 >> 4) & 1) ^ 1);
102 joy.but2 = (((s1 >> 5) & 1) ^ 1);
103 } else {
104 joy.raw_x = joy.calib_data.x2;
105 joy.raw_y = joy.calib_data.y2;
107 joy.x = joy.y = 0;
109 joy.but1 = joy.but2 = 0;
115 int calib_joy(int type)
117 int c1;
118 int x, y;
119 int s1 = 0;
120 int num_times;
121 char flag = 0;
123 while (joy.but1 == 1) {
124 s1 = inportb(0x201);
125 joy.but1 = (((s1 >> 4) & 1) ^ 1);
126 if (key_pressed(1) == 1) {
127 while (key_pressed(1) == 1);
128 return 1;
132 num_times = 0;
134 while (joy.but1 == 0) {
136 c1 = x = y = flag = 0;
137 outportb(0x201, 0);
139 while (1) {
141 s1 = inportb(0x201);
143 if (x == 0) {
144 if ((s1 & 1) == 0)
145 x = c1;
147 if (y == 0) {
148 if ((s1 & 2) == 0)
149 y = c1;
151 if (x != 0 && y != 0)
152 break;
154 c1++;
155 if (c1 == 0x7fff) {
156 flag = 1;
157 break;
162 joy.raw_x = x;
163 joy.raw_y = y;
165 s1 = inportb(0x201);
166 joy.but1 = (((s1 >> 4) & 1) ^ 1);
168 if (num_times < 0x7fffffff)
169 num_times++;
171 if (flag == 1)
172 break;
174 if (key_pressed(1) == 1) {
175 while (key_pressed(1) == 1);
176 return 1;
181 if (num_times < 16)
182 return 1;
184 if (flag == 0) {
186 switch (type) {
187 case 0:
188 joy.calib_data.x1 = joy.raw_x;
189 joy.calib_data.y1 = joy.raw_y;
190 break;
191 case 1:
192 joy.calib_data.x3 = joy.raw_x;
193 joy.calib_data.y3 = joy.raw_y;
194 break;
195 case 2:
196 joy.calib_data.x2 = joy.raw_x;
197 joy.calib_data.y2 = joy.raw_y;
198 break;
201 while (joy.but1 == 1) {
202 s1 = inportb(0x201);
203 joy.but1 = (((s1 >> 4) & 1) ^ 1);
208 return 0;
213 int init_mouse(int *_num_buttons)
215 __dpmi_regs regs;
216 int mouse_enabled, num_mouse_buttons;
218 regs.x.ax = 0;
219 __dpmi_int(0x33, &regs);
220 if (regs.x.ax == 0xffff) {
221 mouse_enabled = 1;
222 num_mouse_buttons = regs.x.bx;
223 if (force2 == 1)
224 num_mouse_buttons = 2;
225 if (force3 == 1)
226 num_mouse_buttons = 3;
227 } else {
228 mouse_enabled = 0;
230 if (_num_buttons)
231 _num_buttons = num_mouse_buttons;
233 return mouse_enabled;
237 void read_mouse(void)
240 regs.x.ax = 3;
241 __dpmi_int(0x33, &regs);
242 mouse.but1 = regs.x.bx & 1;
243 mouse.but2 = (regs.x.bx & 2) >> 1;
244 mouse.but3 = (regs.x.bx & 4) >> 2;
249 void update_player_actions(void)
251 if (main_info.mouse_enabled == 1)
252 read_mouse();
253 if (main_info.joy_enabled == 1)
254 read_joy();
255 player[0].action_left = key_pressed(KEY_PL1_LEFT) == 1;
256 player[0].action_right = key_pressed(KEY_PL1_RIGHT) == 1;
257 player[0].action_up = key_pressed(KEY_PL1_JUMP) == 1;
258 player[1].action_left = key_pressed(KEY_PL2_LEFT) == 1;
259 player[1].action_right = key_pressed(KEY_PL2_RIGHT) == 1;
260 player[1].action_up = key_pressed(KEY_PL2_JUMP) == 1;
261 player[2].action_left = key_pressed(KEY_PL3_LEFT) == 1;
262 player[2].action_right = key_pressed(KEY_PL3_RIGHT) == 1;
263 player[2].action_up = key_pressed(KEY_PL3_JUMP) == 1;
264 player[3].action_left = key_pressed(KEY_PL4_LEFT) == 1;
265 player[3].action_right = key_pressed(KEY_PL4_RIGHT) == 1;
266 player[3].action_up = key_pressed(KEY_PL4_JUMP) == 1;
270 void init_inputs(void)
272 main_info.mouse_enabled = init_mouse(&num_mouse_buttons);
273 main_info.joy_enabled = init_joy(&num_mouse_buttons);