Mercurial > sdl-ios-xcode
comparison src/joystick/linux/SDL_sysjoystick.c @ 953:cb5e1d0cad31
Backing out new changes, at Chris Nelson's request.
author | Sam Lantinga <slouken@libsdl.org> |
---|---|
date | Sat, 11 Sep 2004 17:48:41 +0000 |
parents | bbf8dcc8aed6 |
children | 4aa02c3e5e13 |
comparison
equal
deleted
inserted
replaced
952:6f5c8bd997aa | 953:cb5e1d0cad31 |
---|---|
67 { "Saitek Saitek X45", 6, 1, 0 } | 67 { "Saitek Saitek X45", 6, 1, 0 } |
68 }; | 68 }; |
69 | 69 |
70 #ifndef NO_LOGICAL_JOYSTICKS | 70 #ifndef NO_LOGICAL_JOYSTICKS |
71 | 71 |
72 /* | 72 static struct joystick_logical_values { |
73 Some USB HIDs show up as a single joystick even though they actually | |
74 control 2 or more joysticks. | |
75 */ | |
76 /* | |
77 This code handles the MP-8800 (Quad) and MP-8866 (Dual), which can | |
78 be identified by their transparent blue design. It's quite trivial | |
79 to add other joysticks with similar quirky behavior. | |
80 -id | |
81 */ | |
82 | |
83 struct joystick_logical_mapping { | |
84 int njoy; | 73 int njoy; |
85 int nthing; | 74 int nthing; |
86 }; | 75 } joystick_logical_values[] = { |
87 | 76 |
88 /* | 77 /* +0 */ |
89 {logical joy, logical axis}, | 78 /* MP-8800 axes map - map to {logical joystick #, logical axis #} */ |
90 {logical joy, logical hat}, | 79 {0,0},{0,1},{0,2},{1,0},{1,1},{0,3},{1,2},{1,3},{2,0},{2,1},{2,2},{2,3}, |
91 {logical joy, logical ball}, | 80 {3,0},{3,1},{3,2},{3,3},{0,4},{1,4},{2,4}, |
92 {logical joy, logical button} | 81 |
93 */ | 82 /* +19 */ |
94 | 83 /* MP-8800 hat map - map to {logical joystick #, logical hat #} */ |
95 static struct joystick_logical_mapping mp88xx_1_logical_axismap[] = { | 84 {0,0},{1,0},{2,0},{3,0}, |
96 {0,0},{0,1},{0,2},{0,3},{0,4},{0,5} | 85 |
97 }; | 86 /* +23 */ |
98 static struct joystick_logical_mapping mp88xx_1_logical_buttonmap[] = { | 87 /* MP-8800 button map - map to {logical joystick #, logical button #} */ |
99 {0,0},{0,1},{0,2},{0,3},{0,4},{0,5},{0,6},{0,7},{0,8},{0,9},{0,10},{0,11} | |
100 }; | |
101 | |
102 static struct joystick_logical_mapping mp88xx_2_logical_axismap[] = { | |
103 {0,0},{0,1},{0,2},{1,0},{1,1},{0,3}, | |
104 {1,2},{1,3},{0,4},{0,5},{1,4},{1,5} | |
105 }; | |
106 static struct joystick_logical_mapping mp88xx_2_logical_buttonmap[] = { | |
107 {0,0},{0,1},{0,2},{0,3},{0,4},{0,5},{0,6},{0,7},{0,8},{0,9},{0,10},{0,11}, | |
108 {1,0},{1,1},{1,2},{1,3},{1,4},{1,5},{1,6},{1,7},{1,8},{1,9},{1,10},{1,11} | |
109 }; | |
110 | |
111 static struct joystick_logical_mapping mp88xx_3_logical_axismap[] = { | |
112 {0,0},{0,1},{0,2},{1,0},{1,1},{0,3}, | |
113 {1,2},{1,3},{2,0},{2,1},{2,2},{2,3}, | |
114 {0,4},{0,5},{1,4},{1,5},{2,4},{2,5} | |
115 }; | |
116 static struct joystick_logical_mapping mp88xx_3_logical_buttonmap[] = { | |
117 {0,0},{0,1},{0,2},{0,3},{0,4},{0,5},{0,6},{0,7},{0,8},{0,9},{0,10},{0,11}, | |
118 {1,0},{1,1},{1,2},{1,3},{1,4},{1,5},{1,6},{1,7},{1,8},{1,9},{1,10},{1,11}, | |
119 {2,0},{2,1},{2,2},{2,3},{2,4},{2,5},{2,6},{2,7},{2,8},{2,9},{2,10},{2,11} | |
120 }; | |
121 | |
122 static struct joystick_logical_mapping mp88xx_4_logical_axismap[] = { | |
123 {0,0},{0,1},{0,2},{1,0},{1,1},{0,3}, | |
124 {1,2},{1,3},{2,0},{2,1},{2,2},{2,3}, | |
125 {3,0},{3,1},{3,2},{3,3},{0,4},{0,5}, | |
126 {1,4},{1,5},{2,4},{2,5},{3,4},{3,5} | |
127 }; | |
128 static struct joystick_logical_mapping mp88xx_4_logical_buttonmap[] = { | |
129 {0,0},{0,1},{0,2},{0,3},{0,4},{0,5},{0,6},{0,7},{0,8},{0,9},{0,10},{0,11}, | 88 {0,0},{0,1},{0,2},{0,3},{0,4},{0,5},{0,6},{0,7},{0,8},{0,9},{0,10},{0,11}, |
130 {1,0},{1,1},{1,2},{1,3},{1,4},{1,5},{1,6},{1,7},{1,8},{1,9},{1,10},{1,11}, | 89 {1,0},{1,1},{1,2},{1,3},{1,4},{1,5},{1,6},{1,7},{1,8},{1,9},{1,10},{1,11}, |
131 {2,0},{2,1},{2,2},{2,3},{2,4},{2,5},{2,6},{2,7},{2,8},{2,9},{2,10},{2,11}, | 90 {2,0},{2,1},{2,2},{2,3},{2,4},{2,5},{2,6},{2,7},{2,8},{2,9},{2,10},{2,11}, |
132 {3,0},{3,1},{3,2},{3,3},{3,4},{3,5},{3,6},{3,7},{3,8},{3,9},{3,10},{3,11} | 91 {3,0},{3,1},{3,2},{3,3},{3,4},{3,5},{3,6},{3,7},{3,8},{3,9},{3,10},{3,11} |
133 }; | 92 }; |
134 | 93 |
135 struct joystick_logical_layout { | 94 static struct joystick_logical_layout { |
136 int naxes; | 95 int naxes; |
137 int nhats; | 96 int nhats; |
138 int nballs; | 97 int nballs; |
139 int nbuttons; | 98 int nbuttons; |
99 } joystick_logical_layout[] = { | |
100 /* MP-8800 logical layout */ | |
101 {5, 1, 0, 12}, | |
102 {5, 1, 0, 12}, | |
103 {5, 1, 0, 12}, | |
104 {4, 1, 0, 12} | |
140 }; | 105 }; |
141 | 106 |
142 static struct joystick_logical_layout mp88xx_1_logical_layout[] = { | |
143 {6, 0, 0, 12} | |
144 }; | |
145 static struct joystick_logical_layout mp88xx_2_logical_layout[] = { | |
146 {6, 0, 0, 12}, | |
147 {6, 0, 0, 12} | |
148 }; | |
149 static struct joystick_logical_layout mp88xx_3_logical_layout[] = { | |
150 {6, 0, 0, 12}, | |
151 {6, 0, 0, 12}, | |
152 {6, 0, 0, 12} | |
153 }; | |
154 static struct joystick_logical_layout mp88xx_4_logical_layout[] = { | |
155 {6, 0, 0, 12}, | |
156 {6, 0, 0, 12}, | |
157 {6, 0, 0, 12}, | |
158 {6, 0, 0, 12} | |
159 }; | |
160 | |
161 /* | 107 /* |
162 This array sets up a means of mapping a single physical joystick to | 108 Some USB HIDs show up as a single joystick even though they actually |
163 multiple logical joysticks. (djm) | 109 control 2 or more joysticks. This array sets up a means of mapping |
110 a single physical joystick to multiple logical joysticks. (djm) | |
164 | 111 |
165 njoys | 112 njoys |
166 the number of logical joysticks | 113 the number of logical joysticks |
167 | 114 |
168 layouts | 115 layouts |
169 an array of layout structures, one to describe each logical joystick | 116 an array of layout structures, one to describe each logical joystick |
170 | 117 |
171 axes, hats, balls, buttons | 118 axes, hats, balls, buttons |
172 arrays that map a physical thingy to a logical thingy | 119 arrays that map a physical thingy to a logical thingy |
173 */ | 120 */ |
174 struct joystick_logicalmap { | 121 static struct joystick_logicalmap { |
175 const char *name; | 122 const char *name; |
176 int nbuttons; | |
177 int njoys; | 123 int njoys; |
178 struct joystick_logical_layout *layout; | 124 struct joystick_logical_layout *layouts; |
179 struct joystick_logical_mapping *axismap; | 125 struct joystick_logical_values *axes; |
180 struct joystick_logical_mapping *hatmap; | 126 struct joystick_logical_values *hats; |
181 struct joystick_logical_mapping *ballmap; | 127 struct joystick_logical_values *balls; |
182 struct joystick_logical_mapping *buttonmap; | 128 struct joystick_logical_values *buttons; |
183 }; | 129 |
184 | 130 } joystick_logicalmap[] = { |
185 static struct joystick_logicalmap joystick_logicalmap[] = { | 131 {"WiseGroup.,Ltd MP-8800 Quad USB Joypad", 4, joystick_logical_layout, |
186 { | 132 joystick_logical_values, joystick_logical_values+19, NULL, |
187 "WiseGroup.,Ltd MP-8866 Dual USB Joypad", | 133 joystick_logical_values+23} |
188 12, | |
189 1, | |
190 mp88xx_1_logical_layout, | |
191 mp88xx_1_logical_axismap, | |
192 NULL, | |
193 NULL, | |
194 mp88xx_1_logical_buttonmap | |
195 }, | |
196 { | |
197 "WiseGroup.,Ltd MP-8866 Dual USB Joypad", | |
198 24, | |
199 2, | |
200 mp88xx_2_logical_layout, | |
201 mp88xx_2_logical_axismap, | |
202 NULL, | |
203 NULL, | |
204 mp88xx_2_logical_buttonmap | |
205 }, | |
206 { | |
207 "WiseGroup.,Ltd MP-8800 Quad USB Joypad", | |
208 12, | |
209 1, | |
210 mp88xx_1_logical_layout, | |
211 mp88xx_1_logical_axismap, | |
212 NULL, | |
213 NULL, | |
214 mp88xx_1_logical_buttonmap | |
215 }, | |
216 { | |
217 "WiseGroup.,Ltd MP-8800 Quad USB Joypad", | |
218 24, | |
219 2, | |
220 mp88xx_2_logical_layout, | |
221 mp88xx_2_logical_axismap, | |
222 NULL, | |
223 NULL, | |
224 mp88xx_2_logical_buttonmap | |
225 }, | |
226 { | |
227 "WiseGroup.,Ltd MP-8800 Quad USB Joypad", | |
228 36, | |
229 3, | |
230 mp88xx_3_logical_layout, | |
231 mp88xx_3_logical_axismap, | |
232 NULL, | |
233 NULL, | |
234 mp88xx_3_logical_buttonmap | |
235 }, | |
236 { | |
237 "WiseGroup.,Ltd MP-8800 Quad USB Joypad", | |
238 48, | |
239 4, | |
240 mp88xx_4_logical_layout, | |
241 mp88xx_4_logical_axismap, | |
242 NULL, | |
243 NULL, | |
244 mp88xx_4_logical_buttonmap | |
245 } | |
246 }; | 134 }; |
247 | 135 |
248 /* find the head of a linked list, given a point in it | 136 /* find the head of a linked list, given a point in it |
249 */ | 137 */ |
250 #define SDL_joylist_head(i, start)\ | 138 #define SDL_joylist_head(i, start)\ |
316 | 204 |
317 static int CountLogicalJoysticks(int max) | 205 static int CountLogicalJoysticks(int max) |
318 { | 206 { |
319 register int i, j, k, ret, prev; | 207 register int i, j, k, ret, prev; |
320 const char* name; | 208 const char* name; |
321 int nbuttons, fd; | |
322 unsigned char n; | |
323 | 209 |
324 ret = 0; | 210 ret = 0; |
325 | 211 |
326 for(i = 0; i < max; i++) { | 212 for(i = 0; i < max; i++) { |
327 name = SDL_SYS_JoystickName(i); | 213 name = SDL_SYS_JoystickName(i); |
328 | |
329 fd = open(SDL_joylist[i].fname, O_RDONLY, 0); | |
330 if ( fd >= 0 ) { | |
331 if ( ioctl(fd, JSIOCGBUTTONS, &n) < 0 ) { | |
332 nbuttons = -1; | |
333 } else { | |
334 nbuttons = n; | |
335 } | |
336 close(fd); | |
337 } | |
338 else { | |
339 nbuttons=-1; | |
340 } | |
341 | 214 |
342 if (name) { | 215 if (name) { |
343 for(j = 0; j < SDL_TABLESIZE(joystick_logicalmap); j++) { | 216 for(j = 0; j < SDL_TABLESIZE(joystick_logicalmap); j++) { |
344 if (!strcmp(name, joystick_logicalmap[j].name) && (nbuttons==-1 || nbuttons==joystick_logicalmap[j].nbuttons)) { | 217 if (!strcmp(name, joystick_logicalmap[j].name)) { |
218 | |
345 prev = i; | 219 prev = i; |
346 SDL_joylist[prev].map = &(joystick_logicalmap[j]); | 220 SDL_joylist[prev].map = joystick_logicalmap+j; |
347 | 221 |
348 for(k = 1; k < joystick_logicalmap[j].njoys; k++) { | 222 for(k = 1; k < joystick_logicalmap[j].njoys; k++) { |
349 SDL_joylist[prev].next = max + ret; | 223 SDL_joylist[prev].next = max + ret; |
350 SDL_joylist[max+ret].prev = prev; | 224 |
351 | 225 if (prev != i) |
226 SDL_joylist[max+ret].prev = prev; | |
227 | |
352 prev = max + ret; | 228 prev = max + ret; |
353 SDL_joylist[prev].logicalno = k; | 229 SDL_joylist[prev].logicalno = k; |
354 SDL_joylist[prev].map = &(joystick_logicalmap[j]); | 230 SDL_joylist[prev].map = joystick_logicalmap+j; |
355 ret++; | 231 ret++; |
356 } | 232 } |
357 | 233 |
358 break; | 234 break; |
359 } | 235 } |
369 register int slen; | 245 register int slen; |
370 const static char suffixs[] = | 246 const static char suffixs[] = |
371 "01020304050607080910111213141516171819" | 247 "01020304050607080910111213141516171819" |
372 "20212223242526272829303132"; | 248 "20212223242526272829303132"; |
373 const char* suffix; | 249 const char* suffix; |
250 | |
374 slen = strlen(namebuf); | 251 slen = strlen(namebuf); |
252 | |
375 suffix = NULL; | 253 suffix = NULL; |
376 | 254 |
377 if (logicalno*2<sizeof(suffixs)) | 255 if (logicalno*2<sizeof(suffixs)) |
378 suffix = suffixs + (logicalno*2); | 256 suffix = suffixs + (logicalno*2); |
379 | 257 |
539 } else { | 417 } else { |
540 name = namebuf; | 418 name = namebuf; |
541 } | 419 } |
542 close(fd); | 420 close(fd); |
543 | 421 |
544 | 422 #ifndef NO_LOGICAL_JOYSTICKS |
545 #ifndef NO_LOGICAL_JOYSTICKS | 423 if (SDL_joylist[oindex].prev || SDL_joylist[oindex].next) |
546 if (SDL_joylist[oindex].prev || SDL_joylist[oindex].next || index!=oindex) | |
547 { | |
548 LogicalSuffix(SDL_joylist[oindex].logicalno, namebuf, 128); | 424 LogicalSuffix(SDL_joylist[oindex].logicalno, namebuf, 128); |
549 } | |
550 #endif | 425 #endif |
551 } | 426 } |
552 return name; | 427 return name; |
553 } | 428 } |
554 | 429 |
778 #ifndef NO_LOGICAL_JOYSTICKS | 653 #ifndef NO_LOGICAL_JOYSTICKS |
779 static void ConfigLogicalJoystick(SDL_Joystick *joystick) | 654 static void ConfigLogicalJoystick(SDL_Joystick *joystick) |
780 { | 655 { |
781 struct joystick_logical_layout* layout; | 656 struct joystick_logical_layout* layout; |
782 | 657 |
783 layout = SDL_joylist[joystick->index].map->layout + | 658 layout = SDL_joylist[joystick->index].map->layouts + |
784 SDL_joylist[joystick->index].logicalno; | 659 SDL_joylist[joystick->index].logicalno; |
785 | 660 |
786 joystick->nbuttons = layout->nbuttons; | 661 joystick->nbuttons = layout->nbuttons; |
787 joystick->nhats = layout->nhats; | 662 joystick->nhats = layout->nhats; |
788 joystick->naxes = layout->naxes; | 663 joystick->naxes = layout->naxes; |
854 } | 729 } |
855 | 730 |
856 #ifndef NO_LOGICAL_JOYSTICKS | 731 #ifndef NO_LOGICAL_JOYSTICKS |
857 | 732 |
858 static SDL_Joystick* FindLogicalJoystick( | 733 static SDL_Joystick* FindLogicalJoystick( |
859 SDL_Joystick *joystick, struct joystick_logical_mapping* v) | 734 SDL_Joystick *joystick, struct joystick_logical_values* v) |
860 { | 735 { |
861 SDL_Joystick *logicaljoy; | 736 SDL_Joystick *logicaljoy; |
862 register int i; | 737 register int i; |
863 | 738 |
864 i = joystick->index; | 739 i = joystick->index; |
883 return logicaljoy; | 758 return logicaljoy; |
884 } | 759 } |
885 | 760 |
886 static int LogicalJoystickButton( | 761 static int LogicalJoystickButton( |
887 SDL_Joystick *joystick, Uint8 button, Uint8 state){ | 762 SDL_Joystick *joystick, Uint8 button, Uint8 state){ |
888 struct joystick_logical_mapping* buttons; | 763 struct joystick_logical_values* buttons; |
889 SDL_Joystick *logicaljoy = NULL; | 764 SDL_Joystick *logicaljoy = NULL; |
890 | 765 |
891 /* if there's no map then this is just a regular joystick | 766 /* if there's no map then this is just a regular joystick |
892 */ | 767 */ |
893 if (SDL_joylist[joystick->index].map == NULL) | 768 if (SDL_joylist[joystick->index].map == NULL) |
894 return 0; | 769 return 0; |
895 | 770 |
896 /* get the logical joystick that will receive the event | 771 /* get the logical joystick that will receive the event |
897 */ | 772 */ |
898 buttons = SDL_joylist[joystick->index].map->buttonmap+button; | 773 buttons = SDL_joylist[joystick->index].map->buttons+button; |
899 logicaljoy = FindLogicalJoystick(joystick, buttons); | 774 logicaljoy = FindLogicalJoystick(joystick, buttons); |
900 | 775 |
901 if (logicaljoy == NULL) | 776 if (logicaljoy == NULL) |
902 return 1; | 777 return 1; |
903 | 778 |
907 } | 782 } |
908 | 783 |
909 static int LogicalJoystickAxis( | 784 static int LogicalJoystickAxis( |
910 SDL_Joystick *joystick, Uint8 axis, Sint16 value) | 785 SDL_Joystick *joystick, Uint8 axis, Sint16 value) |
911 { | 786 { |
912 struct joystick_logical_mapping* axes; | 787 struct joystick_logical_values* axes; |
913 SDL_Joystick *logicaljoy = NULL; | 788 SDL_Joystick *logicaljoy = NULL; |
914 | 789 |
915 /* if there's no map then this is just a regular joystick | 790 /* if there's no map then this is just a regular joystick |
916 */ | 791 */ |
917 if (SDL_joylist[joystick->index].map == NULL) | 792 if (SDL_joylist[joystick->index].map == NULL) |
918 return 0; | 793 return 0; |
919 | 794 |
920 /* get the logical joystick that will receive the event | 795 /* get the logical joystick that will receive the event |
921 */ | 796 */ |
922 axes = SDL_joylist[joystick->index].map->axismap+axis; | 797 axes = SDL_joylist[joystick->index].map->axes+axis; |
923 logicaljoy = FindLogicalJoystick(joystick, axes); | 798 logicaljoy = FindLogicalJoystick(joystick, axes); |
924 | 799 |
925 if (logicaljoy == NULL) | 800 if (logicaljoy == NULL) |
926 return 1; | 801 return 1; |
927 | 802 |
939 { SDL_HAT_LEFTUP, SDL_HAT_UP, SDL_HAT_RIGHTUP }, | 814 { SDL_HAT_LEFTUP, SDL_HAT_UP, SDL_HAT_RIGHTUP }, |
940 { SDL_HAT_LEFT, SDL_HAT_CENTERED, SDL_HAT_RIGHT }, | 815 { SDL_HAT_LEFT, SDL_HAT_CENTERED, SDL_HAT_RIGHT }, |
941 { SDL_HAT_LEFTDOWN, SDL_HAT_DOWN, SDL_HAT_RIGHTDOWN } | 816 { SDL_HAT_LEFTDOWN, SDL_HAT_DOWN, SDL_HAT_RIGHTDOWN } |
942 }; | 817 }; |
943 SDL_logical_joydecl(SDL_Joystick *logicaljoy = NULL); | 818 SDL_logical_joydecl(SDL_Joystick *logicaljoy = NULL); |
944 SDL_logical_joydecl(struct joystick_logical_mapping* hats = NULL); | 819 SDL_logical_joydecl(struct joystick_logical_values* hats = NULL); |
945 | 820 |
946 the_hat = &stick->hwdata->hats[hat]; | 821 the_hat = &stick->hwdata->hats[hat]; |
947 if ( value < 0 ) { | 822 if ( value < 0 ) { |
948 value = 0; | 823 value = 0; |
949 } else | 824 } else |
961 */ | 836 */ |
962 if (SDL_joylist[stick->index].map != NULL) { | 837 if (SDL_joylist[stick->index].map != NULL) { |
963 | 838 |
964 /* get the fake joystick that will receive the event | 839 /* get the fake joystick that will receive the event |
965 */ | 840 */ |
966 hats = SDL_joylist[stick->index].map->hatmap+hat; | 841 hats = SDL_joylist[stick->index].map->hats+hat; |
967 logicaljoy = FindLogicalJoystick(stick, hats); | 842 logicaljoy = FindLogicalJoystick(stick, hats); |
968 } | 843 } |
969 | 844 |
970 if (logicaljoy) { | 845 if (logicaljoy) { |
971 stick = logicaljoy; | 846 stick = logicaljoy; |