mapper fixes for ncpu, debug is broken atm
[fceu.git] / drivers / gp2x / lnx-joystick.c
1 /* FCE Ultra - NES/Famicom Emulator
2  *
3  * Copyright notice for this file:
4  *  Copyright (C) 2002 Ben Parnell
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU 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  02111-1307  USA
19  */
20
21 #include <stdio.h>
22 #include <errno.h>
23 #include <unistd.h>
24 #include <fcntl.h>
25 #include <linux/joystick.h>
26 #include "main.h"
27 #include "lnx-joystick.h"
28
29 int joy[4]={0,0,0,0};
30 int joyBMap[4][4];
31
32 static int32 joybuttons[4]={0,0,0,0};
33 static int32 joyx[4]={0,0,0,0};
34 static int32 joyy[4]={0,0,0,0};
35
36 static void ConfigJoystick(int z);
37 static int fd[4];
38
39 #define JOY_A   1
40 #define JOY_B   2
41 #define JOY_SELECT      4
42 #define JOY_START       8
43 #define JOY_UP  0x10
44 #define JOY_DOWN        0x20
45 #define JOY_LEFT        0x40
46 #define JOY_RIGHT       0x80
47
48 static void UpdateJoyData(int x)
49 {
50         struct js_event e;
51
52         while(read(fd[x],&e,sizeof(struct js_event))==sizeof(struct js_event))
53         {
54          e.type&=~JS_EVENT_INIT;
55          if(e.type==JS_EVENT_BUTTON) 
56          {
57           if(e.value)
58            joybuttons[x]|=(1<<e.number);
59           else
60            joybuttons[x]&=~(1<<e.number);
61          }
62          else if(e.type==JS_EVENT_AXIS)
63          {
64           if(e.number==0)
65            joyx[x]=e.value;
66           else if(e.number==1)
67            joyy[x]=e.value;
68          }
69         }
70 }
71
72 uint32 GetJSOr(void)
73 {
74         int x,y;
75         unsigned long ret;
76         ret=0;
77
78         for(x=0;x<4;x++)
79         {
80          int *joym=joyBMap[x];
81
82          if(!joy[x]) continue;
83          UpdateJoyData(x);
84          for(y=0;y<4;y++)
85           if(joybuttons[x]&joym[y]) ret|=(1<<y)<<(x<<3);
86          if(joyx[x]<=-16383) ret|=JOY_LEFT<<(x<<3);
87          else if(joyx[x]>=16383) ret|=JOY_RIGHT<<(x<<3);
88          if(joyy[x]<=-16383) ret|=JOY_UP<<(x<<3);
89          else if(joyy[x]>=16383) ret|=JOY_DOWN<<(x<<3);
90         }
91         return ret;
92 }
93
94 void KillJoysticks(void)
95 {
96         int x;
97         for(x=0;x<4;x++)
98          if(joy[x]) 
99           close(fd[x]);
100 }
101
102 int InitJoysticks(void)
103 {
104         char dbuf[64];
105         int version;
106         int z;
107
108         for(z=0;z<4;z++)
109         {
110          if(!joy[z]) continue;
111          sprintf(dbuf,"/dev/js%d",joy[z]-1);
112          if((fd[z]=open(dbuf,O_RDONLY|O_NONBLOCK))<0) 
113          {
114           printf("Could not open %s.\n",dbuf);
115           joy[z]=0;
116           continue;
117          }
118
119          if(ioctl(fd[z], JSIOCGVERSION, &version)==-1) 
120          {
121           printf("Error using ioctl JSIOCGVERSION on %s.\n",dbuf);
122           joy[z]=0;
123           close(fd[z]);
124           continue;
125          }
126
127          if(!(joyBMap[z][0]|joyBMap[z][1]|joyBMap[z][2]|joyBMap[z][3]))
128           ConfigJoystick(z);
129         }
130
131         return(joy[0]|joy[1]|joy[2]|joy[3]);
132 }
133
134 #define WNOINPUT(); for(;;){uint8 t; if(read(fileno(stdin),&t,1)==-1) \
135                           {break;}}
136
137 static void BConfig(int z,int b)
138 {
139   WNOINPUT();
140   for(;;)
141   {
142    uint8 t;
143    if(read(fileno(stdin),&t,1)==-1)
144    {
145     if(errno!=EAGAIN) break;
146    }
147    else
148     break;
149
150    {
151     struct js_event e;
152
153     while(read(fd[z],&e,sizeof(struct js_event))==sizeof(struct js_event))
154     {
155       if(e.type==JS_EVENT_BUTTON)
156       {
157        if(!e.value)
158        {
159         joyBMap[z][b]=1<<e.number;
160         goto endsa;
161        }
162       }
163     }
164    }
165
166   }
167   endsa:
168   WNOINPUT();
169 }
170
171 void ConfigJoystick(int z)
172 {
173  int sa;
174  static char *genb="** Press button for ";
175
176  printf("\n\n Joystick button configuration:\n\n");
177  printf("   Push the button to map to the virtual joystick.\n");
178  printf("   If you do not wish to assign a button, press Enter to skip\n");
179  printf("   that button.\n\n");
180  printf("   Press enter to continue...\n");
181  getchar();
182  printf("****  Configuring joystick %d ****\n\n",z+1);
183
184  sa=fcntl(fileno(stdin),F_GETFL);
185  fcntl(fileno(stdin),F_SETFL,O_NONBLOCK);
186
187   printf("%s\"Select\".\n",genb);
188   BConfig(z,2);
189
190   printf("%s\"Start\".\n",genb);
191   BConfig(z,3);
192
193   printf("%s\"B\".\n",genb);
194   BConfig(z,1);
195
196   printf("%s\"A\".\n",genb);
197   BConfig(z,0);
198
199   fcntl(fileno(stdin),F_SETFL,sa);
200 }
201