File: md-phil.cpp

package info (click to toggle)
dgen 1.23-10
  • links: PTS
  • area: non-free
  • in suites: lenny, squeeze
  • size: 2,176 kB
  • ctags: 3,085
  • sloc: ansic: 45,403; cpp: 4,405; sh: 1,960; makefile: 86
file content (206 lines) | stat: -rw-r--r-- 4,514 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
// Ripped Snes9X joystick code and made it Genesis friendly, with a few
// slight modifications. [PKH]

#include <sys/ioctl.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>

#include "md.h"
#include "md-phil.h"

// Default setup for Gravis GamePad Pro 10 button controllers.

// Yellow ... A
// Green .... C
// Red ...... A
// Blue ..... B
// L1 ....... Y
// R1 ....... Z
// L2 ....... X
// R2 ....... X
// Start .... START  :)
// Select ... MODE

// Mappings are configurable via dgenrc.  Mappings are done from 0-16
// Standard 2 button joysticks will only have buttons 0 and 1.  [PKH]

extern int js_map_button [2][16];

#ifdef LINUX_JOYSTICK_SUPPORT
#warning USING LINUX JOYSTICK
#include <linux/joystick.h>

int joypads[2] = {0};

int js_fd[2] = {-1, -1};

char *js_device [2] = {"/dev/js0", "/dev/js1"};

void md::init_joysticks() {
#ifdef JSIOCGVERSION
    int version;
    unsigned char axes, buttons;

    if ((js_fd [0] = open (js_device [0], O_RDONLY | O_NONBLOCK)) < 0)
    {
	perror (js_device [0]);
	return;
    }

    if (ioctl (js_fd [0], JSIOCGVERSION, &version))
    {
        puts("joystick: You need at least driver version 1.0 for joystick support");
	close (js_fd [0]);
	return;
    }
    js_fd [1] = open (js_device [1], O_RDONLY | O_NONBLOCK);

#ifdef JSIOCGNAME
    char name [130];
    bzero (name, 128);
    if (ioctl (js_fd [0], JSIOCGNAME(128), name) > 0) 
    {
        printf ("Using %s (%s) as pad1", name, js_device [0]);
        if (js_fd [1] > 0)
	{
	    ioctl (js_fd [1], JSIOCGNAME(128), name);
	    printf ("and %s (%s) as pad2", name, js_device [1]);
	}
    } 
    else

#endif // JSIOCGNAME
    {
	ioctl (js_fd [0], JSIOCGAXES, &axes);
	ioctl (js_fd [0], JSIOCGBUTTONS, &buttons);
	printf ("Using %d-axis %d-button joystick (%s) as pad1", axes, buttons, js_device [0]);
	if (js_fd [1] > 0)
	{
	    ioctl (js_fd [0], JSIOCGAXES, &axes);
	    ioctl (js_fd [0], JSIOCGBUTTONS, &buttons);
	    printf (" and %d-axis %d-button (%s) as pad2", axes, buttons, js_device [1]);
	}
    }
    puts (".");
    
#endif // JSIOCGVERSION
}

void md::read_joysticks()
{
#ifdef JSIOCGVERSION
    struct js_event js_ev;
    int i;

    for (i = 0; i < 2 && js_fd [i] >= 0; i++)
    {
	while (read (js_fd[i], &js_ev, sizeof (struct js_event)) == sizeof (struct js_event) )
	{
	    switch (js_ev.type & ~JS_EVENT_INIT)
	    {
	    case JS_EVENT_AXIS:
		if (js_ev.number == 0)
		{
		    if(js_ev.value < -16384)
		    {
			pad[i] &= ~MD_LEFT_MASK; 
			pad[i] |= MD_RIGHT_MASK;
			break;
		    }
		    if (js_ev.value > 16384)
		    {
			pad[i] |= MD_LEFT_MASK;
			pad[i] &= ~MD_RIGHT_MASK; 
			break;
		    }
		    pad[i] |= MD_LEFT_MASK;
		    pad[i] |= MD_RIGHT_MASK;
		    break;	
		}

		if (js_ev.number == 1)
		{
		    if (js_ev.value < -16384)
		    {
			pad[i] &= ~MD_UP_MASK; 
			pad[i] |= MD_DOWN_MASK;
			break;
		    }
		    if (js_ev.value > 16384)
		    {
			pad[i] |= MD_UP_MASK;
			pad[i] &= ~MD_DOWN_MASK; 
			break;
		    }
		    pad[i] |= MD_UP_MASK;
		    pad[i] |= MD_DOWN_MASK;
		    break;	
		}

		break;

	    case JS_EVENT_BUTTON:
		if (js_ev.number > 15)
		    break;

		if (js_ev.value)
		    pad[i] &= ~js_map_button [i][js_ev.number];
		else
		    pad[i] |= js_map_button [i][js_ev.number];

		break;
	    }
	}
    }
#endif // JSIOCGVERSION
}

#elif defined(SDL_JOYSTICK_SUPPORT)
#warning USING SDL JOYSTICK
#include <SDL.h>
#include <SDL_joystick.h>

static SDL_Joystick *js_handle[2] = { NULL, NULL };

void md::init_joysticks() {
  // Initialize the joystick support
  // Thanks to Cameron Moore <cameron@unbeatenpath.net>
  if(SDL_Init(SDL_INIT_JOYSTICK) < 0)
    {
      fprintf(stderr, "joystick: Unable to initialize joystick system\n");
      return;
    }

  // Open the first couple of joysticks, if possible
  js_handle[0] = SDL_JoystickOpen(0);
  js_handle[1] = SDL_JoystickOpen(1);

  // If neither opened, quit
  if(!(js_handle[0] || js_handle[1]))
    {
      fprintf(stderr, "joystick: Unable to open any joysticks\n");
      return;
    }

  // Print the joystick names
  printf("Using ");
  if(js_handle[0]) printf("%s (#0) as pad1 ", SDL_JoystickName(0));
  if(js_handle[0] && js_handle[1]) printf("and ");
  if(js_handle[1]) printf("%s (#1) as pad2 ", SDL_JoystickName(1));
  printf("\n");

  // Enable joystick events
  SDL_JoystickEventState(SDL_ENABLE);
}

// This does nothing; SDL joystick handling is in the main event loop in
// sdl/sdl.cpp
void md::read_joysticks()
{
  //empty
}

#endif