comparison src/joystick/linux/SDL_sysjoystick.c @ 1336:3692456e7b0f

Use SDL_ prefixed versions of C library functions. FIXME: Change #include <stdlib.h> to #include "SDL_stdlib.h" Change #include <string.h> to #include "SDL_string.h" Make sure nothing else broke because of this...
author Sam Lantinga <slouken@libsdl.org>
date Tue, 07 Feb 2006 06:59:48 +0000
parents c9b51268668f
children 604d73db6802
comparison
equal deleted inserted replaced
1335:c39265384763 1336:3692456e7b0f
21 */ 21 */
22 22
23 /* This is the system specific header for the SDL joystick API */ 23 /* This is the system specific header for the SDL joystick API */
24 24
25 #include <stdio.h> /* For the definition of NULL */ 25 #include <stdio.h> /* For the definition of NULL */
26 #include <stdlib.h> /* For getenv() prototype */ 26 #include <stdlib.h> /* For SDL_getenv() prototype */
27 #include <string.h> 27 #include <string.h>
28 #include <sys/stat.h> 28 #include <sys/stat.h>
29 #include <unistd.h> 29 #include <unistd.h>
30 #include <fcntl.h> 30 #include <fcntl.h>
31 #include <sys/ioctl.h> 31 #include <sys/ioctl.h>
185 185
186 static char *mystrdup(const char *string) 186 static char *mystrdup(const char *string)
187 { 187 {
188 char *newstring; 188 char *newstring;
189 189
190 newstring = (char *)malloc(strlen(string)+1); 190 newstring = (char *)SDL_malloc(SDL_strlen(string)+1);
191 if ( newstring ) { 191 if ( newstring ) {
192 strcpy(newstring, string); 192 SDL_strcpy(newstring, string);
193 } 193 }
194 return(newstring); 194 return(newstring);
195 } 195 }
196 196
197 197
207 for(i = 0; i < max; i++) { 207 for(i = 0; i < max; i++) {
208 name = SDL_SYS_JoystickName(i); 208 name = SDL_SYS_JoystickName(i);
209 209
210 if (name) { 210 if (name) {
211 for(j = 0; j < SDL_TABLESIZE(joystick_logicalmap); j++) { 211 for(j = 0; j < SDL_TABLESIZE(joystick_logicalmap); j++) {
212 if (!strcmp(name, joystick_logicalmap[j].name)) { 212 if (!SDL_strcmp(name, joystick_logicalmap[j].name)) {
213 213
214 prev = i; 214 prev = i;
215 SDL_joylist[prev].map = joystick_logicalmap+j; 215 SDL_joylist[prev].map = joystick_logicalmap+j;
216 216
217 for(k = 1; k < joystick_logicalmap[j].njoys; k++) { 217 for(k = 1; k < joystick_logicalmap[j].njoys; k++) {
241 const static char suffixs[] = 241 const static char suffixs[] =
242 "01020304050607080910111213141516171819" 242 "01020304050607080910111213141516171819"
243 "20212223242526272829303132"; 243 "20212223242526272829303132";
244 const char* suffix; 244 const char* suffix;
245 245
246 slen = strlen(namebuf); 246 slen = SDL_strlen(namebuf);
247 247
248 suffix = NULL; 248 suffix = NULL;
249 249
250 if (logicalno*2<sizeof(suffixs)) 250 if (logicalno*2<sizeof(suffixs))
251 suffix = suffixs + (logicalno*2); 251 suffix = suffixs + (logicalno*2);
304 int n, duplicate; 304 int n, duplicate;
305 305
306 numjoysticks = 0; 306 numjoysticks = 0;
307 307
308 /* First see if the user specified a joystick to use */ 308 /* First see if the user specified a joystick to use */
309 if ( getenv("SDL_JOYSTICK_DEVICE") != NULL ) { 309 if ( SDL_getenv("SDL_JOYSTICK_DEVICE") != NULL ) {
310 strncpy(path, getenv("SDL_JOYSTICK_DEVICE"), sizeof(path)); 310 SDL_strncpy(path, SDL_getenv("SDL_JOYSTICK_DEVICE"), sizeof(path));
311 path[sizeof(path)-1] = '\0'; 311 path[sizeof(path)-1] = '\0';
312 if ( stat(path, &sb) == 0 ) { 312 if ( stat(path, &sb) == 0 ) {
313 fd = open(path, O_RDONLY, 0); 313 fd = open(path, O_RDONLY, 0);
314 if ( fd >= 0 ) { 314 if ( fd >= 0 ) {
315 /* Assume the user knows what they're doing. */ 315 /* Assume the user knows what they're doing. */
424 424
425 static int allocate_hatdata(SDL_Joystick *joystick) 425 static int allocate_hatdata(SDL_Joystick *joystick)
426 { 426 {
427 int i; 427 int i;
428 428
429 joystick->hwdata->hats = (struct hwdata_hat *)malloc( 429 joystick->hwdata->hats = (struct hwdata_hat *)SDL_malloc(
430 joystick->nhats * sizeof(struct hwdata_hat)); 430 joystick->nhats * sizeof(struct hwdata_hat));
431 if ( joystick->hwdata->hats == NULL ) { 431 if ( joystick->hwdata->hats == NULL ) {
432 return(-1); 432 return(-1);
433 } 433 }
434 for ( i=0; i<joystick->nhats; ++i ) { 434 for ( i=0; i<joystick->nhats; ++i ) {
440 440
441 static int allocate_balldata(SDL_Joystick *joystick) 441 static int allocate_balldata(SDL_Joystick *joystick)
442 { 442 {
443 int i; 443 int i;
444 444
445 joystick->hwdata->balls = (struct hwdata_ball *)malloc( 445 joystick->hwdata->balls = (struct hwdata_ball *)SDL_malloc(
446 joystick->nballs * sizeof(struct hwdata_ball)); 446 joystick->nballs * sizeof(struct hwdata_ball));
447 if ( joystick->hwdata->balls == NULL ) { 447 if ( joystick->hwdata->balls == NULL ) {
448 return(-1); 448 return(-1);
449 } 449 }
450 for ( i=0; i<joystick->nballs; ++i ) { 450 for ( i=0; i<joystick->nballs; ++i ) {
479 479
480 name = SDL_SYS_JoystickName(joystick->index); 480 name = SDL_SYS_JoystickName(joystick->index);
481 old_axes = joystick->naxes; 481 old_axes = joystick->naxes;
482 482
483 /* Generic analog joystick support */ 483 /* Generic analog joystick support */
484 if ( strstr(name, "Analog") == name && strstr(name, "-hat") ) { 484 if ( SDL_strstr(name, "Analog") == name && SDL_strstr(name, "-hat") ) {
485 if ( sscanf(name,"Analog %d-axis %*d-button %d-hat", 485 if ( SDL_sscanf(name,"Analog %d-axis %*d-button %d-hat",
486 &tmp_naxes, &tmp_nhats) == 2 ) { 486 &tmp_naxes, &tmp_nhats) == 2 ) {
487 487
488 joystick->naxes = tmp_naxes; 488 joystick->naxes = tmp_naxes;
489 joystick->nhats = tmp_nhats; 489 joystick->nhats = tmp_nhats;
490 490
492 } 492 }
493 } 493 }
494 494
495 /* Special joystick support */ 495 /* Special joystick support */
496 for ( i=0; i < SDL_TABLESIZE(special_joysticks); ++i ) { 496 for ( i=0; i < SDL_TABLESIZE(special_joysticks); ++i ) {
497 if ( strcmp(name, special_joysticks[i].name) == 0 ) { 497 if ( SDL_strcmp(name, special_joysticks[i].name) == 0 ) {
498 498
499 joystick->naxes = special_joysticks[i].naxes; 499 joystick->naxes = special_joysticks[i].naxes;
500 joystick->nhats = special_joysticks[i].nhats; 500 joystick->nhats = special_joysticks[i].nhats;
501 joystick->nballs = special_joysticks[i].nballs; 501 joystick->nballs = special_joysticks[i].nballs;
502 502
504 break; 504 break;
505 } 505 }
506 } 506 }
507 507
508 /* User environment joystick support */ 508 /* User environment joystick support */
509 if ( (env = getenv("SDL_LINUX_JOYSTICK")) ) { 509 if ( (env = SDL_getenv("SDL_LINUX_JOYSTICK")) ) {
510 strcpy(env_name, ""); 510 SDL_strcpy(env_name, "");
511 if ( *env == '\'' && sscanf(env, "'%[^']s'", env_name) == 1 ) 511 if ( *env == '\'' && SDL_sscanf(env, "'%[^']s'", env_name) == 1 )
512 env += strlen(env_name)+2; 512 env += SDL_strlen(env_name)+2;
513 else if ( sscanf(env, "%s", env_name) == 1 ) 513 else if ( SDL_sscanf(env, "%s", env_name) == 1 )
514 env += strlen(env_name); 514 env += SDL_strlen(env_name);
515 515
516 if ( strcmp(name, env_name) == 0 ) { 516 if ( SDL_strcmp(name, env_name) == 0 ) {
517 517
518 if ( sscanf(env, "%d %d %d", &tmp_naxes, &tmp_nhats, 518 if ( SDL_sscanf(env, "%d %d %d", &tmp_naxes, &tmp_nhats,
519 &tmp_nballs) == 3 ) { 519 &tmp_nballs) == 3 ) {
520 520
521 joystick->naxes = tmp_naxes; 521 joystick->naxes = tmp_naxes;
522 joystick->nhats = tmp_nhats; 522 joystick->nhats = tmp_nhats;
523 joystick->nballs = tmp_nballs; 523 joystick->nballs = tmp_nballs;
695 SDL_SetError("Unable to open %s\n", 695 SDL_SetError("Unable to open %s\n",
696 SDL_joylist[joystick->index]); 696 SDL_joylist[joystick->index]);
697 return(-1); 697 return(-1);
698 } 698 }
699 joystick->hwdata = (struct joystick_hwdata *) 699 joystick->hwdata = (struct joystick_hwdata *)
700 malloc(sizeof(*joystick->hwdata)); 700 SDL_malloc(sizeof(*joystick->hwdata));
701 if ( joystick->hwdata == NULL ) { 701 if ( joystick->hwdata == NULL ) {
702 SDL_OutOfMemory(); 702 SDL_OutOfMemory();
703 close(fd); 703 close(fd);
704 return(-1); 704 return(-1);
705 } 705 }
706 memset(joystick->hwdata, 0, sizeof(*joystick->hwdata)); 706 SDL_memset(joystick->hwdata, 0, sizeof(*joystick->hwdata));
707 joystick->hwdata->fd = fd; 707 joystick->hwdata->fd = fd;
708 708
709 /* Set the joystick to non-blocking read mode */ 709 /* Set the joystick to non-blocking read mode */
710 fcntl(fd, F_SETFL, O_NONBLOCK); 710 fcntl(fd, F_SETFL, O_NONBLOCK);
711 711
1062 #ifndef NO_LOGICAL_JOYSTICKS 1062 #ifndef NO_LOGICAL_JOYSTICKS
1063 if (SDL_joylist[joystick->index].fname != NULL) 1063 if (SDL_joylist[joystick->index].fname != NULL)
1064 #endif 1064 #endif
1065 close(joystick->hwdata->fd); 1065 close(joystick->hwdata->fd);
1066 if ( joystick->hwdata->hats ) { 1066 if ( joystick->hwdata->hats ) {
1067 free(joystick->hwdata->hats); 1067 SDL_free(joystick->hwdata->hats);
1068 } 1068 }
1069 if ( joystick->hwdata->balls ) { 1069 if ( joystick->hwdata->balls ) {
1070 free(joystick->hwdata->balls); 1070 SDL_free(joystick->hwdata->balls);
1071 } 1071 }
1072 free(joystick->hwdata); 1072 SDL_free(joystick->hwdata);
1073 joystick->hwdata = NULL; 1073 joystick->hwdata = NULL;
1074 } 1074 }
1075 } 1075 }
1076 1076
1077 /* Function to perform any system-specific joystick related cleanup */ 1077 /* Function to perform any system-specific joystick related cleanup */
1078 void SDL_SYS_JoystickQuit(void) 1078 void SDL_SYS_JoystickQuit(void)
1079 { 1079 {
1080 int i; 1080 int i;
1081 1081
1082 for ( i=0; SDL_joylist[i].fname; ++i ) { 1082 for ( i=0; SDL_joylist[i].fname; ++i ) {
1083 free(SDL_joylist[i].fname); 1083 SDL_free(SDL_joylist[i].fname);
1084 } 1084 }
1085 SDL_joylist[0].fname = NULL; 1085 SDL_joylist[0].fname = NULL;
1086 } 1086 }
1087 1087