#include "joystick.h" #include "os.h" #include #include #include #include #include #include #include #include #ifdef LINUX #include #include #endif #include #include #include #include using namespace std; #define THROTTLE_CNT_MAX 10 #ifdef FREEBSD static char* pack(const BUTTONS* buttons, char* buf) { buf[0]= (buttons->A_BUTTON ? 1 : 0) + (buttons->B_BUTTON ? 2 : 0) + (buttons->L_TRIG ? 4 : 0) + (buttons->R_TRIG ? 8 : 0) + (buttons->Z_TRIG ? 16 : 0) + (buttons->START_BUTTON ? 32 : 0) + 128; buf[1]= (buttons->R_DPAD ? 1 : 0) + (buttons->L_DPAD ? 2 : 0) + (buttons->U_DPAD ? 4 : 0) + (buttons->D_DPAD ? 8 : 0) + ((buttons->X_AXIS & 128) ? 16 : 0) + ((buttons->Y_AXIS & 128) ? 32 : 0); buf[2]= (buttons->R_CBUTTON ? 1 : 0) + (buttons->L_CBUTTON ? 2 : 0) + (buttons->U_CBUTTON ? 4 : 0) + (buttons->D_CBUTTON ? 8 : 0); buf[3]= (buttons->X_AXIS & 127); buf[4]= (buttons->Y_AXIS & 127); return buf; } #endif #ifdef FREEBSD Joystick::Joystick() { if ((fifo_fd=open("/var/tmp/mupen64plus_ctl", O_WRONLY )) == -1) {throw string(strerror(errno));} cout << "opened" << endl; if (fcntl(fifo_fd, F_SETFL, O_NONBLOCK) == -1) throw string("failed to set nonblocking io"); reset(); } void Joystick::press_a(bool state) { buttons.A_BUTTON=state; send_data(); } void Joystick::send_data() { char buf[5]; pack(&buttons, buf); write(fifo_fd, buf, 5); } void Joystick::steer(float dir, float dead_zone) { if (dir<-1.0) dir=-1.0; if (dir>1.0) dir=1.0; if (fabs(dir)1.0) dir=1.0; if (fabs(dir)1.0) t=1.0; throt=t; } void Joystick::process() { throttle_cnt++; if (throttle_cnt>=THROTTLE_CNT_MAX) throttle_cnt=0; press_a((throttle_cnt < throt*THROTTLE_CNT_MAX)); }