1 
2 /*
3 
4   confjoystick.c
5 
6   This file helps to configure your joypad/-stick buttons.
7 
8 */
9 
10 
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <unistd.h>
14 #include <string.h>
15 
16 #include "settings.h"
17 
18 #ifndef _LINUX
19 #include <sys/joystick.h>
20 #include <sys/uio.h>
21 #else
22 #include <linux/joystick.h>
23 #endif
24 #include <fcntl.h>
25 #include <sys/ioctl.h>
26 
27 #ifdef _LINUX
28 #define JOYPAD_DEV "/dev/js0"
29 #else
30 #define JOYPAD_DEV "/dev/joy0"
31 #endif
32 
33 int joypaddev;
34 int min_x;
35 int min_y;
36 int max_x;
37 int max_y;
38 
39 #ifdef _LINUX
read_status(void)40 unsigned int read_status(void)
41 {
42   struct JS_DATA_TYPE js;
43 
44   do read(joypaddev,&js,JS_RETURN); while (js.buttons>0);
45   while (js.buttons==0) read(joypaddev,&js,JS_RETURN);
46   return js.buttons;
47 }
read_coords(void)48 void read_coords(void)
49 {
50   struct JS_DATA_TYPE js;
51 
52   max_x=-65535;
53   max_y=-65535;
54   min_x=65535;
55   min_y=65535;
56 
57   do read(joypaddev,&js,JS_RETURN); while (js.buttons>0);
58   do {
59     read(joypaddev,&js,JS_RETURN);
60     if (js.x<min_x) min_x=js.x;
61     if (js.x>max_x) max_x=js.x;
62     if (js.y<min_y) min_y=js.y;
63     if (js.y>max_y) max_y=js.y;
64 
65     printf("X min:%d, max:%d ; Y min:%d, max:%d                    \r",
66 	   min_x,max_x,min_y,max_y);
67 
68   } while (js.buttons==0);
69 }
70 #else
read_status(void)71 unsigned int read_status(void)
72 {
73   struct joystick js;
74 
75   do read(joypaddev,&js,sizeof(struct joystick)); while (js.b1!=0 || js.b2!=0);
76   while (js.b1==0 && js.b2==0) read(joypaddev,&js,sizeof(struct joystick));
77   return (js.b1&0xFF)|((js.b2&0xFF)<<8);
78 }
read_coords(void)79 void read_coords(void)
80 {
81   struct joystick js;
82 
83   max_x=-65535;
84   max_y=-65535;
85   min_x=65535;
86   min_y=65535;
87 
88   do read(joypaddev,&js,sizeof(struct joystick)); while (js.b1!=0 || js.b2!=0);
89   do {
90     read(joypaddev,&js,sizeof(struct joystick));
91     if (js.x<min_x) min_x=js.x;
92     if (js.x>max_x) max_x=js.x;
93     if (js.y<min_y) min_y=js.y;
94     if (js.y>max_y) max_y=js.y;
95 
96     printf("X min:%d, max:%d ; Y min:%d, max:%d                    \r",
97 	   min_x,max_x,min_y,max_y);
98 
99   } while (js.b1==0 && js.b2==0);
100 }
101 #endif
102 
main(void)103 int main(void)
104 {
105 #ifdef _LINUX
106   struct JS_DATA_SAVE_TYPE_32 jsd;
107   int status;
108 #endif
109   unsigned int keyA=0x0001,keyB=0x0002,
110                keySTART=0x0100,keySELECT=0x0200;
111   char joy_device[128];
112   FILE *cfgfile;
113   char cfgfilename[512];
114 
115   int joy_left=-1,joy_right=-1,joy_top=-1,joy_bottom=-1;
116 
117   if (settings_getconffilename(cfgfilename,sizeof(cfgfilename))<0) {
118 
119     printf("Cannot find user's home.\n");
120     return 0;
121   }
122 
123   printf("Joypad configuration started ... ok\n");
124 
125   printf("Enter the joypad device (e.g. \""JOYPAD_DEV"\"): ");
126   scanf("%s",joy_device);
127   printf("Opening joypad device ... ");
128   joypaddev=open(joy_device,O_RDONLY);
129 
130   if (joypaddev<0) {
131     strcpy(joy_device,JOYPAD_DEV);
132     printf("FAILED\n");
133   } else {
134     printf("ok\n");
135 #ifdef _LINUX
136     printf("Reading joystick info ... ");
137     status=ioctl(joypaddev,JS_GET_ALL,&jsd);
138     if (status<0) {
139       strcpy(joy_device,JOYPAD_DEV);
140       printf("FAILED\n");
141       close(joypaddev);
142     } else {
143 #endif
144       printf("ok\n");
145       printf("-------------------------------------------------\n");
146       printf("Now you must press the proper keys on your joypad\n");
147       printf("-------------------------------------------------\n");
148 
149       printf("Press GAMEBOY-A-key on your joypad.\n");
150       keyA=read_status();
151       printf("Press GAMEBOY-B-key on your joypad.\n");
152       keyB=read_status();
153 #ifdef _LINUX
154       printf("Press GAMEBOY-START-key on your joypad.\n");
155       keySTART=read_status();
156       printf("Press GAMEBOY-SELECT-key on your joypad.\n");
157       keySELECT=read_status();
158 #else
159       keySTART=0;
160       keySELECT=0;
161 #endif
162 
163       printf("-------------------------------------------------\n");
164       printf("Calibration: Please move the joypad in every\n");
165       printf("direction and press a button.\n");
166       printf("-------------------------------------------------\n");
167 
168       read_coords();
169 
170       if (min_x==-65535 || min_y==-65535 ||
171 	  max_x==65535 || max_y==65535) {
172 	printf("\nERROR: Calibration is NOT precise enough!\n");
173       }
174 
175       joy_right=joy_left=(min_x+max_x)/2;
176       joy_left-=(max_x-min_x)/5;
177       joy_right+=(max_x-min_x)/5;
178       joy_top=joy_bottom=(min_y+max_y)/2;
179       joy_top-=(max_y-min_y)/5;
180       joy_bottom+=(max_y-min_y)/5;
181 
182 #ifdef _LINUX
183     }
184 #endif
185   }
186 
187   printf("\nWriting %s ... ",cfgfilename);
188   if ((cfgfile=fopen(cfgfilename,"w"))!=NULL) {
189     fprintf(cfgfile,"\n#   This file was generated by 'cingb_conf'");
190     fprintf(cfgfile,"#   it contains settings for cingb\n\n\n");
191 
192     fprintf(cfgfile,"joy_dev=%s\n",joy_device);
193     fprintf(cfgfile,"joy_left=%d\n",joy_left);
194     fprintf(cfgfile,"joy_right=%d\n",joy_right);
195     fprintf(cfgfile,"joy_top=%d\n",joy_top);
196     fprintf(cfgfile,"joy_bottom=%d\n\n",joy_bottom);
197 
198     fprintf(cfgfile,"joy_buttonA=%d\n",keyA);
199     fprintf(cfgfile,"joy_buttonB=%d\n",keyB);
200     fprintf(cfgfile,"joy_buttonSTART=%d\n",keySTART);
201     fprintf(cfgfile,"joy_buttonSELECT=%d\n",keySELECT);
202     fclose(cfgfile);
203     printf("ok.\n\n");
204   } else
205     printf("FAILED.\n");
206 
207   exit(0);
208 }
209