/* * joystick.cpp * * Copyright 2012 Florian Jung * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License Version 3 * as published by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, * MA 02110-1301, USA. */ #include "joystick.h" #include "os.h" #include #include #include #include #include #include #include #include #ifdef JOYSTICK_UINPUT #include #include #endif #include #include #include #include using namespace std; #define THROTTLE_CNT_MAX 6 #ifdef JOYSTICK_PATCHEDINPUTPLUGIN 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 JOYSTICK_PATCHEDINPUTPLUGIN 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)); }