1 /*------------------------------------------------------------------.
2 | Copyright 1997, 1998, 2000, 2001  Alexandre Duret-Lutz            |
3 |                                    <duret_g@epita.fr>             |
4 |                                                                   |
5 | This file is part of Heroes.                                      |
6 |                                                                   |
7 | Heroes is free software; you can redistribute it and/or modify it |
8 | under the terms of the GNU General Public License version 2 as    |
9 | published by the Free Software Foundation.                        |
10 |                                                                   |
11 | Heroes is distributed in the hope that it will be useful, but     |
12 | WITHOUT ANY WARRANTY; without even the implied warranty of        |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU |
14 | General Public License for more details.                          |
15 |                                                                   |
16 | You should have received a copy of the GNU General Public License |
17 | along with this program; if not, write to the Free Software       |
18 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA          |
19 | 02111-1307 USA                                                    |
20 `------------------------------------------------------------------*/
21 
22 #include "system.h"
23 #include "joystick.h"
24 #include "debugmsg.h"
25 #include "errors.h"
26 #include <ggi/gii.h>
27 
28 int joystick_x[2] = { 0, 0 };	/* coord. X */
29 int joystick_y[2] = { 0, 0 };	/*        Y */
30 char joystick_b[2] = { 0, 0 };	/* buttons (on 2bits) */
31 char joystick_detected = 0;
32 
33 gii_input_t joystick;
34 
35 static void
_get_joystick_state(void)36 _get_joystick_state (void)
37 {
38   struct timeval t = { 0, 0 };
39 
40   if (giiEventPoll (joystick,
41 		    emValAbsolute | emKeyPress | emKeyRelease, &t) != emZero) {
42     int nbr;
43     gii_event ev;
44 
45     nbr = giiEventsQueued (joystick,
46 			   emValAbsolute | emKeyPress | emKeyRelease);
47     for (; nbr; --nbr) {
48       giiEventRead (joystick, &ev,
49 		    emValAbsolute | emKeyPress | emKeyRelease);
50       if (ev.any.type == evKeyPress) {
51   	dmsg (D_JOYSTICK, "joystick button #%d pressed\n", ev.key.button);
52 	switch (ev.key.button) {
53 	case 1:
54 	  joystick_b[0] |= 1;
55 	  break;
56 	case 2:
57 	  joystick_b[0] |= 2;
58 	  break;
59 	case 3:
60 	  joystick_b[1] |= 1;
61 	  break;
62 	case 4:
63 	  joystick_b[1] |= 2;
64 	  break;
65 	}
66       } else if (ev.any.type == evKeyRelease) {
67   	dmsg (D_JOYSTICK, "joystick button #%d released\n", ev.key.button);
68 	switch (ev.key.button) {
69 	case 1:
70 	  joystick_b[0] &= ~1;
71 	  break;
72 	case 2:
73 	  joystick_b[0] &= ~2;
74 	  break;
75 	case 3:
76 	  joystick_b[1] &= ~1;
77 	  break;
78 	case 4:
79 	  joystick_b[1] &= ~2;
80 	  break;
81 	}
82       } else if (ev.any.type == evValAbsolute) {
83 	unsigned int i;
84 	for (i = ev.val.first; i < ev.val.first + ev.val.count; ++i)
85 	  dmsg (D_JOYSTICK, "joystick valuator #%u = %d", i,
86 		ev.val.value[i - ev.val.first]);
87 	if (0 == ev.val.first)
88 	  joystick_x[0] = ev.val.value[0];
89 	if (1 >= ev.val.first && 1 <= ev.val.first + ev.val.count)
90 	  joystick_y[0] = ev.val.value[1 - ev.val.first];
91 	if (2 >= ev.val.first && 2 <= ev.val.first + ev.val.count)
92 	  joystick_x[1] = ev.val.value[2 - ev.val.first];
93 	if (3 >= ev.val.first && 3 <= ev.val.first + ev.val.count)
94 	  joystick_y[1] = ev.val.value[3 - ev.val.first];
95       } else {
96 	wmsg (_("unexpected event %d\n"), ev.any.type);
97       }
98     }
99   }
100 }
101 
102 char
joyinit(void)103 joyinit (void)
104 {
105   dmsg (D_JOYSTICK, "initialize joystick");
106   giiInit ();
107   joystick = giiOpen ("linux-joy",NULL);
108   if (!joystick) {
109     wmsg (_("No joystick found (run with `-J' to suppress this message)."));
110     joystick_detected = 0;
111   } else
112     joystick_detected = 1;
113 
114   return (joystick_detected);
115 }
116 
117 /* Update the state of the joysticks */
118 void
get_joystick_state(void)119 get_joystick_state (void)
120 {
121   if (joystick_detected)
122     _get_joystick_state ();
123 }
124