void updateCapabilities(int joystick) { if (joystick<0 || MAXJOYSTICKS<=joystick) { return; } capabilities[joystick].povs=0; capabilities[joystick].axes=0; capabilities[joystick].buttons=0; capabilities[joystick].version=0; strcpy(capabilities[joystick].name, "Unknown"); if (fd[joystick]<0 && openJoystick(joystick)<0) return; ioctl(fd[joystick], JSIOCGAXES, &capabilities[joystick].axes); ioctl(fd[joystick], JSIOCGBUTTONS, &capabilities[joystick].buttons); ioctl(fd[joystick], JSIOCGVERSION, &capabilities[joystick].version); int nameLen = ioctl(fd[joystick], JSIOCGNAME(JOYSTICK_NAME_SIZE), capabilities[joystick].name); if (nameLen > 0) { // NULL terminate just in case. capabilities[joystick].name[JOYSTICK_NAME_SIZE - 1] = 0; } else { strcpy(capabilities[joystick].name, "Unknown"); } }
int calcNumDevices() { if (numDevices>=0) return numDevices; initAll(); unsigned int i=0; for (; i<MAXJOYSTICKS; i++) { int fd=openJoystick(i); if (fd<0) break; closeJoystick(i); } return numDevices=i; }
void updateInfo(int joystick) { if (joystick<0 || joystick>=MAXJOYSTICKS) return; if (fd[joystick]<0 && openJoystick(joystick)<0) return; assert(fd[joystick]>=0); JoystickEvent ev; ev.type=0; fd_set rfds; int retval; FD_ZERO(&rfds); FD_SET(fd[joystick], &rfds); struct timeval interval; for (;;) { interval.tv_sec = 0; interval.tv_usec = 1; retval = select(fd[joystick]+1, &rfds, NULL, NULL, &interval); if (retval!=1) break; TEMP_FAILURE_RETRY(read (fd[joystick], &ev, sizeof(struct js_event))); // process event if ((ev.type & ~JS_EVENT_INIT) == JS_EVENT_BUTTON) { if (ev.value) { info[joystick].buttons |= (1 << ev.number); } else { info[joystick].buttons &= ~(1 << ev.number); } } // info[joystick].buttons|=8; if ((ev.type & ~JS_EVENT_INIT) == JS_EVENT_AXIS) { float val=ev.value/32767.0f; if (ev.number<MAXAXES) info[joystick].axis[ev.number]=val; } } }
void InputManager::loadConfig() { LOG(LogDebug) << "Loading input config..."; //clear any old config joystickButtonMap.clear(); joystickAxisPosMap.clear(); joystickAxisNegMap.clear(); std::string path = getConfigPath(); std::ifstream file(path.c_str()); joystickName = ""; while(file.good()) { std::string line; std::getline(file, line); //skip blank lines and comments if(line.empty() || line[0] == *"#") continue; //I know I could probably just read from the file stream directly, but I feel it would be harder to catch errors in a readable way std::istringstream stream(line); std::string token[3]; int tokNum = 0; while(std::getline(stream, token[tokNum], ' ')) { tokNum++; //JOYNAME can have spaces if(tokNum == 1 && token[0] == "JOYNAME") { std::getline(stream, token[1]); break; } if(tokNum >= 3) break; } if(token[0] == "BUTTON") { joystickButtonMap[atoi(token[1].c_str())] = (InputButton)atoi(token[2].c_str()); }else if(token[0] == "AXISPOS") { joystickAxisPosMap[atoi(token[1].c_str())] = (InputButton)atoi(token[2].c_str()); }else if(token[0] == "AXISNEG") { joystickAxisNegMap[atoi(token[1].c_str())] = (InputButton)atoi(token[2].c_str()); }else if(token[0] == "JOYNAME") { joystickName = token[1]; }else{ LOG(LogWarning) << "Invalid input type - " << token[0]; return; } } LOG(LogDebug) << "Finished loading input config"; openJoystick(); }