⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sdl_sysjoystick.c

📁 SDL库 在进行视频显示程序spcaview安装时必须的库文件
💻 C
📖 第 1 页 / 共 3 页
字号:
		"/dev/input/event%d",#endif		"/dev/input/js%d",		"/dev/js%d"	};	int numjoysticks;	int i, j;	int fd;	char path[PATH_MAX];	dev_t dev_nums[MAX_JOYSTICKS];  /* major/minor device numbers */	struct stat sb;	int n, duplicate;	numjoysticks = 0;	/* First see if the user specified a joystick to use */	if ( SDL_getenv("SDL_JOYSTICK_DEVICE") != NULL ) {		SDL_strlcpy(path, SDL_getenv("SDL_JOYSTICK_DEVICE"), sizeof(path));		if ( stat(path, &sb) == 0 ) {			fd = open(path, O_RDONLY, 0);			if ( fd >= 0 ) {				/* Assume the user knows what they're doing. */				SDL_joylist[numjoysticks].fname = SDL_strdup(path);				if ( SDL_joylist[numjoysticks].fname ) {					dev_nums[numjoysticks] = sb.st_rdev;					++numjoysticks;				}				close(fd);			}		}	}	for ( i=0; i<SDL_arraysize(joydev_pattern); ++i ) {		for ( j=0; j < MAX_JOYSTICKS; ++j ) {			SDL_snprintf(path, SDL_arraysize(path), joydev_pattern[i], j);			/* rcg06302000 replaced access(F_OK) call with stat().			 * stat() will fail if the file doesn't exist, so it's			 * equivalent behaviour.			 */			if ( stat(path, &sb) == 0 ) {				/* Check to make sure it's not already in list.				 * This happens when we see a stick via symlink.				 */				duplicate = 0;				for (n=0; (n<numjoysticks) && !duplicate; ++n) {					if ( sb.st_rdev == dev_nums[n] ) {						duplicate = 1;					}				}				if (duplicate) {					continue;				}				fd = open(path, O_RDONLY, 0);				if ( fd < 0 ) {					continue;				}#if SDL_INPUT_LINUXEV#ifdef DEBUG_INPUT_EVENTS				printf("Checking %s\n", path);#endif				if ( (i == 0) && ! EV_IsJoystick(fd) ) {					close(fd);					continue;				}#endif				close(fd);				/* We're fine, add this joystick */				SDL_joylist[numjoysticks].fname = SDL_strdup(path);				if ( SDL_joylist[numjoysticks].fname ) {					dev_nums[numjoysticks] = sb.st_rdev;					++numjoysticks;				}			}		}#if SDL_INPUT_LINUXEV		/* This is a special case...		   If the event devices are valid then the joystick devices		   will be duplicates but without extra information about their		   hats or balls. Unfortunately, the event devices can't		   currently be calibrated, so it's a win-lose situation.		   So : /dev/input/eventX = /dev/input/jsY = /dev/jsY		*/		if ( (i == 0) && (numjoysticks > 0) )			break;#endif	}#ifndef NO_LOGICAL_JOYSTICKS	numjoysticks += CountLogicalJoysticks(numjoysticks);#endif	return(numjoysticks);}/* Function to get the device-dependent name of a joystick */const char *SDL_SYS_JoystickName(int index){	int fd;	static char namebuf[128];	char *name;	SDL_logical_joydecl(int oindex = index);#ifndef NO_LOGICAL_JOYSTICKS	SDL_joylist_head(index, index);#endif	name = NULL;	fd = open(SDL_joylist[index].fname, O_RDONLY, 0);	if ( fd >= 0 ) {		if ( #if SDL_INPUT_LINUXEV		     (ioctl(fd, EVIOCGNAME(sizeof(namebuf)), namebuf) <= 0) &&#endif		     (ioctl(fd, JSIOCGNAME(sizeof(namebuf)), namebuf) <= 0) ) {			name = SDL_joylist[index].fname;		} else {			name = namebuf;		}		close(fd);#ifndef NO_LOGICAL_JOYSTICKS		if (SDL_joylist[oindex].prev || SDL_joylist[oindex].next || index!=oindex)		{       		   LogicalSuffix(SDL_joylist[oindex].logicalno, namebuf, 128);		}#endif	}	return name;}static int allocate_hatdata(SDL_Joystick *joystick){	int i;	joystick->hwdata->hats = (struct hwdata_hat *)SDL_malloc(		joystick->nhats * sizeof(struct hwdata_hat));	if ( joystick->hwdata->hats == NULL ) {		return(-1);	}	for ( i=0; i<joystick->nhats; ++i ) {		joystick->hwdata->hats[i].axis[0] = 1;		joystick->hwdata->hats[i].axis[1] = 1;	}	return(0);}static int allocate_balldata(SDL_Joystick *joystick){	int i;	joystick->hwdata->balls = (struct hwdata_ball *)SDL_malloc(		joystick->nballs * sizeof(struct hwdata_ball));	if ( joystick->hwdata->balls == NULL ) {		return(-1);	}	for ( i=0; i<joystick->nballs; ++i ) {		joystick->hwdata->balls[i].axis[0] = 0;		joystick->hwdata->balls[i].axis[1] = 0;	}	return(0);}static SDL_bool JS_ConfigJoystick(SDL_Joystick *joystick, int fd){	SDL_bool handled;	unsigned char n;	int old_axes, tmp_naxes, tmp_nhats, tmp_nballs;	const char *name;	char *env, env_name[128];	int i;	handled = SDL_FALSE;	/* Default joystick device settings */	if ( ioctl(fd, JSIOCGAXES, &n) < 0 ) {		joystick->naxes = 2;	} else {		joystick->naxes = n;	}	if ( ioctl(fd, JSIOCGBUTTONS, &n) < 0 ) {		joystick->nbuttons = 2;	} else {		joystick->nbuttons = n;	}	name = SDL_SYS_JoystickName(joystick->index);	old_axes = joystick->naxes;	/* Generic analog joystick support */	if ( SDL_strstr(name, "Analog") == name && SDL_strstr(name, "-hat") ) {		if ( SDL_sscanf(name,"Analog %d-axis %*d-button %d-hat",			&tmp_naxes, &tmp_nhats) == 2 ) {			joystick->naxes = tmp_naxes;			joystick->nhats = tmp_nhats;			handled = SDL_TRUE;		}	}	/* Special joystick support */	for ( i=0; i < SDL_arraysize(special_joysticks); ++i ) {		if ( SDL_strcmp(name, special_joysticks[i].name) == 0 ) {			joystick->naxes = special_joysticks[i].naxes;			joystick->nhats = special_joysticks[i].nhats;			joystick->nballs = special_joysticks[i].nballs;			handled = SDL_TRUE;			break;		}	}	/* User environment joystick support */	if ( (env = SDL_getenv("SDL_LINUX_JOYSTICK")) ) {		*env_name = '\0';		if ( *env == '\'' && SDL_sscanf(env, "'%[^']s'", env_name) == 1 )			env += SDL_strlen(env_name)+2;		else if ( SDL_sscanf(env, "%s", env_name) == 1 )			env += SDL_strlen(env_name);		if ( SDL_strcmp(name, env_name) == 0 ) {			if ( SDL_sscanf(env, "%d %d %d", &tmp_naxes, &tmp_nhats,				&tmp_nballs) == 3 ) {				joystick->naxes = tmp_naxes;				joystick->nhats = tmp_nhats;				joystick->nballs = tmp_nballs;				handled = SDL_TRUE;			}		}	}	/* Remap hats and balls */	if (handled) {		if ( joystick->nhats > 0 ) {			if ( allocate_hatdata(joystick) < 0 ) {				joystick->nhats = 0;			}		}		if ( joystick->nballs > 0 ) {			if ( allocate_balldata(joystick) < 0 ) {				joystick->nballs = 0;			}		}	}	return(handled);}#if SDL_INPUT_LINUXEVstatic SDL_bool EV_ConfigJoystick(SDL_Joystick *joystick, int fd){	int i, t;	unsigned long keybit[40];	unsigned long absbit[40];	unsigned long relbit[40];	/* See if this device uses the new unified event API */	if ( (ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(keybit)), keybit) >= 0) &&	     (ioctl(fd, EVIOCGBIT(EV_ABS, sizeof(absbit)), absbit) >= 0) &&	     (ioctl(fd, EVIOCGBIT(EV_REL, sizeof(relbit)), relbit) >= 0) ) {		joystick->hwdata->is_hid = SDL_TRUE;		/* Get the number of buttons, axes, and other thingamajigs */		for ( i=BTN_JOYSTICK; i < KEY_MAX; ++i ) {			if ( test_bit(i, keybit) ) {#ifdef DEBUG_INPUT_EVENTS				printf("Joystick has button: 0x%x\n", i);#endif				joystick->hwdata->key_map[i-BTN_MISC] =						joystick->nbuttons;				++joystick->nbuttons;			}		}		for ( i=BTN_MISC; i < BTN_JOYSTICK; ++i ) {			if ( test_bit(i, keybit) ) {#ifdef DEBUG_INPUT_EVENTS				printf("Joystick has button: 0x%x\n", i);#endif				joystick->hwdata->key_map[i-BTN_MISC] =						joystick->nbuttons;				++joystick->nbuttons;			}		}		for ( i=0; i<ABS_MAX; ++i ) {			/* Skip hats */			if ( i == ABS_HAT0X ) {				i = ABS_HAT3Y;				continue;			}			if ( test_bit(i, absbit) ) {				int values[5];				if ( ioctl(fd, EVIOCGABS(i), values) < 0 )					continue;#ifdef DEBUG_INPUT_EVENTS				printf("Joystick has absolute axis: %x\n", i);				printf("Values = { %d, %d, %d, %d, %d }\n",					values[0], values[1],					values[2], values[3], values[4]);#endif /* DEBUG_INPUT_EVENTS */				joystick->hwdata->abs_map[i] = joystick->naxes;				if ( values[1] == values[2] ) {				    joystick->hwdata->abs_correct[i].used = 0;				} else {				    joystick->hwdata->abs_correct[i].used = 1;				    joystick->hwdata->abs_correct[i].coef[0] =					(values[2] + values[1]) / 2 - values[4];				    joystick->hwdata->abs_correct[i].coef[1] =					(values[2] + values[1]) / 2 + values[4];				    t = ((values[2] - values[1]) / 2 - 2 * values[4]);				    if ( t != 0 ) {					joystick->hwdata->abs_correct[i].coef[2] = (1 << 29) / t;				    } else {					joystick->hwdata->abs_correct[i].coef[2] = 0;				    }				}				++joystick->naxes;			}		}		for ( i=ABS_HAT0X; i <= ABS_HAT3Y; i += 2 ) {			if ( test_bit(i, absbit) || test_bit(i+1, absbit) ) {#ifdef DEBUG_INPUT_EVENTS				printf("Joystick has hat %d\n",(i-ABS_HAT0X)/2);#endif				++joystick->nhats;			}		}		if ( test_bit(REL_X, relbit) || test_bit(REL_Y, relbit) ) {			++joystick->nballs;		}		/* Allocate data to keep track of these thingamajigs */		if ( joystick->nhats > 0 ) {			if ( allocate_hatdata(joystick) < 0 ) {				joystick->nhats = 0;			}		}		if ( joystick->nballs > 0 ) {			if ( allocate_balldata(joystick) < 0 ) {				joystick->nballs = 0;			}		}	}	return(joystick->hwdata->is_hid);}#endif /* SDL_INPUT_LINUXEV */#ifndef NO_LOGICAL_JOYSTICKSstatic void ConfigLogicalJoystick(SDL_Joystick *joystick){        struct joystick_logical_layout* layout;                                                                                        layout = SDL_joylist[joystick->index].map->layout +                SDL_joylist[joystick->index].logicalno;                                                                                        joystick->nbuttons = layout->nbuttons;        joystick->nhats = layout->nhats;        joystick->naxes = layout->naxes;        joystick->nballs = layout->nballs;}#endif/* Function to open a joystick for use.   The joystick to open is specified by the index field of the joystick.   This should fill the nbuttons and naxes fields of the joystick structure.   It returns 0, or -1 if there is an error. */int SDL_SYS_JoystickOpen(SDL_Joystick *joystick){	int fd;	SDL_logical_joydecl(int realindex);	SDL_logical_joydecl(SDL_Joystick *realjoy = NULL);	/* Open the joystick and set the joystick file descriptor */#ifndef NO_LOGICAL_JOYSTICKS	if (SDL_joylist[joystick->index].fname == NULL) {		SDL_joylist_head(realindex, joystick->index);		realjoy = SDL_JoystickOpen(realindex);		if (realjoy == NULL)			return(-1);                                                                                		fd = realjoy->hwdata->fd;	} else {		fd = open(SDL_joylist[joystick->index].fname, O_RDONLY, 0);	}	SDL_joylist[joystick->index].joy = joystick;#else	fd = open(SDL_joylist[joystick->index].fname, O_RDONLY, 0);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -