sm5svn sync: Fix compile on some systems. [Steve Checkoway]

This commit is contained in:
AJ Kelly
2010-08-01 11:47:20 -05:00
parent 58d4434e7d
commit 54246b3366
3 changed files with 713 additions and 705 deletions
+447 -447
View File
@@ -1,447 +1,447 @@
#include "global.h"
#include "InputHandler_Linux_Event.h"
#include "RageLog.h"
#include "RageUtil.h"
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <linux/input.h>
REGISTER_INPUT_HANDLER_CLASS2( Event, Linux_Event );
bool InputHandler_Linux_Event::m_bFoundAnyJoysticks;
static RString BustypeToString( int iBus )
{
switch( iBus )
{
// case BUS_ADB:
// case BUS_AMIGA: return "amiga input";
case BUS_BLUETOOTH: return "Bluetooth";
case BUS_GAMEPORT: return "gameport";
// case BUS_HIL:
// case BUS_HOST:
// case BUS_I2C:
case BUS_I8042: return "keyboard";
case BUS_ISA: return "ISA";
case BUS_ISAPNP: return "ISAPNP";
case BUS_PARPORT: return "parallel port";
case BUS_PCI: return "PCI";
case BUS_RS232: return "serial port";
case BUS_USB: return "USB";
case BUS_XTKBD: return "XT keyboard";
default: return ssprintf("unknown bus %x", iBus);
}
}
struct EventDevice
{
EventDevice();
~EventDevice();
bool Open( RString sFile, InputDevice dev );
bool IsOpen() const { return m_iFD != -1; }
void Close()
{
if( m_iFD != -1 )
close( m_iFD );
m_iFD = -1;
}
int m_iFD;
RString m_sPath;
RString m_sName;
InputDevice m_Dev;
int aiAbsMin[ABS_MAX];
int aiAbsMax[ABS_MAX];
DeviceButton aiAbsMappingHigh[ABS_MAX];
DeviceButton aiAbsMappingLow[ABS_MAX];
};
static vector<EventDevice *> g_apEventDevices;
/* Return true if the numbered event device exists. sysfs may not always be
* there; return false if we don't know. */
static bool EventDeviceExists( int iNum )
{
RString sDir = ssprintf( "/sys/class" );
struct stat st;
if( stat(sDir, &st) == -1 )
return true;
RString sFile = ssprintf( "/sys/class/input/event%i", iNum );
return stat(sFile, &st) == 0;
}
static bool BitIsSet( const uint8_t *pArray, uint32_t iBit )
{
return !!(pArray[iBit/8] & (1<<(iBit%8)));
}
EventDevice::EventDevice()
{
m_iFD = -1;
}
bool EventDevice::Open( RString sFile, InputDevice dev )
{
m_sPath = sFile;
m_Dev = dev;
m_iFD = open( sFile, O_RDWR );
if( m_iFD == -1 )
{
if( errno == ENODEV )
return false;
if( !EventDeviceExists(m_iFD) )
return false;
LOG->Warn( "Error opening %s: %s", sFile.c_str(), strerror(errno) );
return false;
}
static bool bLogged = false;
if( !bLogged )
{
bLogged = true;
int iVersion;
if( ioctl(m_iFD, EVIOCGVERSION, &iVersion) == -1 )
LOG->Warn( "ioctl(EVIOCGVERSION): %s", strerror(errno) );
else
LOG->Info( "Event driver: v%i.%i.%i", (iVersion >> 16) & 0xFF, (iVersion >> 8) & 0xFF, iVersion & 0xFF );
}
char szName[1024];
if( ioctl(m_iFD, EVIOCGNAME(sizeof(szName)), szName) == -1 )
{
LOG->Warn( "ioctl(EVIOCGNAME): %s", strerror(errno) );
m_sName = "(unknown)";
}
else
{
m_sName = szName;
}
input_id DevInfo;
if( ioctl(m_iFD, EVIOCGID, &DevInfo) == -1 )
{
LOG->Warn( "ioctl(EVIOCGID): %s", strerror(errno) );
}
else
{
LOG->Info( "Input device: %s: %s device, ID %04x:%04x, version %x: %s", sFile.c_str(),
BustypeToString(DevInfo.bustype).c_str(), DevInfo.vendor, DevInfo.product,
DevInfo.version, m_sName.c_str() );
}
uint8_t iABSMask[ABS_MAX/8 + 1];
memset( iABSMask, 0, sizeof(iABSMask) );
if( ioctl(m_iFD, EVIOCGBIT(EV_ABS, sizeof(iABSMask)), iABSMask) < 0 )
LOG->Warn( "ioctl(EVIOCGBIT(EV_ABS)): %s", strerror(errno) );
if( !BitIsSet(iABSMask, ABS_X) && !BitIsSet(iABSMask, ABS_THROTTLE) && !BitIsSet(iABSMask, ABS_WHEEL) )
{
LOG->Info( " Not a joystick; ignored" );
Close();
return false;
}
uint8_t iKeyMask[KEY_MAX/8 + 1];
memset( iKeyMask, 0, sizeof(iKeyMask) );
if( ioctl(m_iFD, EVIOCGBIT(EV_KEY, sizeof(iKeyMask)), iKeyMask) < 0 )
LOG->Warn( "ioctl(EVIOCGBIT(EV_KEY)): %s", strerror(errno) );
uint8_t iEventTypes[EV_MAX/8];
memset( iEventTypes, 0, sizeof(iEventTypes) );
if( ioctl(m_iFD, EVIOCGBIT(0, EV_MAX), iEventTypes) == -1 )
LOG->Warn( "ioctl(EV_MAX): %s", strerror(errno) );
{
vector<RString> setEventTypes;
if( BitIsSet(iEventTypes, EV_SYN) ) setEventTypes.push_back( "syn" );
if( BitIsSet(iEventTypes, EV_KEY) ) setEventTypes.push_back( "key" );
if( BitIsSet(iEventTypes, EV_REL) ) setEventTypes.push_back( "rel" );
if( BitIsSet(iEventTypes, EV_ABS) ) setEventTypes.push_back( "abs" );
if( BitIsSet(iEventTypes, EV_MSC) ) setEventTypes.push_back( "misc" );
if( BitIsSet(iEventTypes, EV_SW) ) setEventTypes.push_back( "sw" );
if( BitIsSet(iEventTypes, EV_LED) ) setEventTypes.push_back( "led" );
if( BitIsSet(iEventTypes, EV_SND) ) setEventTypes.push_back( "snd" );
if( BitIsSet(iEventTypes, EV_REP) ) setEventTypes.push_back( "rep" );
if( BitIsSet(iEventTypes, EV_FF) ) setEventTypes.push_back( "ff" );
if( BitIsSet(iEventTypes, EV_PWR) ) setEventTypes.push_back( "pwr" );
if( BitIsSet(iEventTypes, EV_FF_STATUS) ) setEventTypes.push_back( "ff_status" );
LOG->Info( " Event types: %s", join(", ", setEventTypes).c_str() );
}
int iTotalKeys = 0;
for( int i = 0; i < KEY_MAX; ++i )
{
if( !BitIsSet(iKeyMask, i) )
continue;
++iTotalKeys;
}
int iTotalAxes = 0;
const DeviceButton iExtraAxes[] = { JOY_LEFT_2, JOY_UP_2, JOY_AUX_1, JOY_AUX_3 };
int iNextExtraAxis = 0;
for( int i = 0; i < ABS_MAX; ++i )
{
if( !BitIsSet(iABSMask, i) )
continue;
struct input_absinfo absinfo;
if( ioctl(m_iFD, EVIOCGABS(i), &absinfo) < 0 )
{
LOG->Warn( "ioctl(EVIOCGABS): %s", strerror(errno) );
continue;
}
//LOG->Info( " Axis %i: min: %i; max: %i; fuzz: %i; flat: %i",
// i, absinfo.minimum, absinfo.maximum, absinfo.fuzz, absinfo.flat );
aiAbsMin[i] = absinfo.minimum;
aiAbsMax[i] = absinfo.maximum;
aiAbsMappingHigh[i] = enum_add2(JOY_RIGHT, 2*i);
aiAbsMappingLow[i] = enum_add2(JOY_LEFT, 2*i);
if( i == ABS_X )
{
aiAbsMappingHigh[i] = JOY_RIGHT;
aiAbsMappingLow[i] = JOY_LEFT;
}
else if( i == ABS_Y )
{
aiAbsMappingHigh[i] = JOY_DOWN;
aiAbsMappingLow[i] = JOY_UP;
}
else if( i == ABS_Z )
{
aiAbsMappingHigh[i] = JOY_Z_DOWN;
aiAbsMappingLow[i] = JOY_Z_UP;
}
else if( i == ABS_RX )
{
aiAbsMappingHigh[i] = JOY_ROT_RIGHT;
aiAbsMappingLow[i] = JOY_ROT_LEFT;
}
else if( i == ABS_RY )
{
aiAbsMappingHigh[i] = JOY_ROT_DOWN;
aiAbsMappingLow[i] = JOY_ROT_UP;
}
else if( i == ABS_RZ )
{
aiAbsMappingHigh[i] = JOY_ROT_Z_DOWN;
aiAbsMappingLow[i] = JOY_ROT_Z_UP;
}
else if( i == ABS_HAT0X )
{
aiAbsMappingHigh[i] = JOY_HAT_RIGHT;
aiAbsMappingLow[i] = JOY_HAT_LEFT;
}
else if( i == ABS_HAT0Y )
{
aiAbsMappingHigh[i] = JOY_HAT_UP;
aiAbsMappingLow[i] = JOY_HAT_DOWN;
}
else
{
if( iNextExtraAxis < (int) ARRAYLEN(iExtraAxes) )
{
aiAbsMappingLow[i] = iExtraAxes[iNextExtraAxis];
aiAbsMappingHigh[i] = enum_add2( aiAbsMappingLow[i], 1 );
++iNextExtraAxis;
}
}
++iTotalAxes;
}
LOG->Info( " Total keys: %i; total axes: %i", iTotalKeys, iTotalAxes );
return true;
}
EventDevice::~EventDevice()
{
Close();
}
InputHandler_Linux_Event::InputHandler_Linux_Event()
{
if( InputHandler_Linux_Event::m_bFoundAnyJoysticks )
{
LOG->Trace( "InputHandler_Linux_Event disabled (joystick driver already loaded)" );
return;
}
/* Permission problems are likely. We want to warn about them only if there's actually
* an underlying device, but if we can't open the device, the only way we can tell if
* there'd be anything there is sysfs. That won't always be there. */
m_bFoundAnyJoysticks = false;
InputDevice NextDevice = DEVICE_JOY1;
for( int i = 0; i < 64; ++i )
{
RString sFile = ssprintf( "/dev/input/event%i", i );
g_apEventDevices.push_back( new EventDevice );
EventDevice *pDev = g_apEventDevices.back();
if( !pDev->Open(sFile, NextDevice) )
{
delete pDev;
g_apEventDevices.pop_back();
continue;
}
NextDevice = enum_add2(NextDevice, 1);
m_bFoundAnyJoysticks = true;
}
m_bShutdown = false;
if( m_bFoundAnyJoysticks )
{
m_InputThread.SetName( "Event input thread" );
m_InputThread.Create( InputThread_Start, this );
/* We loaded joysticks, so disable joydev. */
}
}
InputHandler_Linux_Event::~InputHandler_Linux_Event()
{
if( m_InputThread.IsCreated() )
{
m_bShutdown = true;
LOG->Trace( "Shutting down joystick thread ..." );
m_InputThread.Wait();
LOG->Trace( "Joystick thread shut down." );
}
for( int i = 0; i < (int) g_apEventDevices.size(); ++i )
delete g_apEventDevices[i];
g_apEventDevices.clear();
}
int InputHandler_Linux_Event::InputThread_Start( void *p )
{
((InputHandler_Linux_Event *) p)->InputThread();
return 0;
}
void InputHandler_Linux_Event::InputThread()
{
while( !m_bShutdown )
{
fd_set fdset;
FD_ZERO( &fdset );
int iMaxFD = -1;
for( int i = 0; i < (int) g_apEventDevices.size(); ++i )
{
int iFD = g_apEventDevices[i]->m_iFD;
if( !g_apEventDevices[i]->IsOpen() )
continue;
FD_SET( iFD, &fdset );
iMaxFD = max( iMaxFD, iFD );
}
if( iMaxFD == -1 )
break;
struct timeval zero = {0,100000};
if( select(iMaxFD+1, &fdset, NULL, NULL, &zero) <= 0 )
continue;
RageTimer now;
for( int i = 0; i < (int) g_apEventDevices.size(); ++i )
{
if( !g_apEventDevices[i]->IsOpen() )
continue;
if( !FD_ISSET(g_apEventDevices[i]->m_iFD, &fdset) )
continue;
input_event event;
int ret = read( g_apEventDevices[i]->m_iFD, &event, sizeof(event) );
if( ret == -1 )
{
LOG->Warn( "Error reading from %s: %s; disabled", g_apEventDevices[i]->m_sPath.c_str(), strerror(errno) );
g_apEventDevices[i]->Close();
continue;
}
if( ret != sizeof(event) )
{
LOG->Warn("Unexpected packet (size %i != %i) from joystick %i; disabled", ret, (int)sizeof(event), i);
g_apEventDevices[i]->Close();
continue;
}
switch (event.type) {
case EV_KEY: {
int iNum = event.code;
// In 2.6.11 using an EMS USB2, the event number for P1 Tri (the first button)
// is being reported as 32 instead of 0. Correct for this.
wrap( iNum, 32 ); // max number of joystick buttons. Make this a constant?
ButtonPressed( DeviceInput(g_apEventDevices[i]->m_Dev, enum_add2(JOY_BUTTON_1, iNum), event.value != 0, now) );
break;
}
case EV_ABS: {
ASSERT_M( event.code < ABS_MAX, ssprintf("%i", event.code) );
DeviceButton neg = g_apEventDevices[i]->aiAbsMappingLow[event.code];
DeviceButton pos = g_apEventDevices[i]->aiAbsMappingHigh[event.code];
float l = SCALE( int(event.value), (float) g_apEventDevices[i]->aiAbsMin[i], (float) g_apEventDevices[i]->aiAbsMax[i], -1.0f, 1.0f );
ButtonPressed( DeviceInput(g_apEventDevices[i]->m_Dev, neg, max(-l,0), now) );
ButtonPressed( DeviceInput(g_apEventDevices[i]->m_Dev, pos, max(+l,0), now) );
break;
}
}
}
}
InputHandler::UpdateTimer();
}
void InputHandler_Linux_Event::GetDevicesAndDescriptions( vector<InputDeviceInfo>& vDevicesOut )
{
for( unsigned i = 0; i < g_apEventDevices.size(); ++i )
{
EventDevice *pDev = g_apEventDevices[i];
vDevicesOut.push_back( InputDeviceInfo(pDev->m_Dev, pDev->m_sName) );
}
}
/*
* (c) 2003-2008 Glenn Maynard
* All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, and/or sell copies of the Software, and to permit persons to
* whom the Software is furnished to do so, provided that the above
* copyright notice(s) and this permission notice appear in all copies of
* the Software and that both the above copyright notice(s) and this
* permission notice appear in supporting documentation.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF
* THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS
* INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT
* OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
* OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
* OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*/
#include "global.h"
#include "InputHandler_Linux_Event.h"
#include "RageLog.h"
#include "RageUtil.h"
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <linux/input.h>
REGISTER_INPUT_HANDLER_CLASS2( Event, Linux_Event );
bool InputHandler_Linux_Event::m_bFoundAnyJoysticks;
static RString BustypeToString( int iBus )
{
switch( iBus )
{
// case BUS_ADB:
// case BUS_AMIGA: return "amiga input";
case BUS_BLUETOOTH: return "Bluetooth";
case BUS_GAMEPORT: return "gameport";
// case BUS_HIL:
// case BUS_HOST:
// case BUS_I2C:
case BUS_I8042: return "keyboard";
case BUS_ISA: return "ISA";
case BUS_ISAPNP: return "ISAPNP";
case BUS_PARPORT: return "parallel port";
case BUS_PCI: return "PCI";
case BUS_RS232: return "serial port";
case BUS_USB: return "USB";
case BUS_XTKBD: return "XT keyboard";
default: return ssprintf("unknown bus %x", iBus);
}
}
struct EventDevice
{
EventDevice();
~EventDevice();
bool Open( RString sFile, InputDevice dev );
bool IsOpen() const { return m_iFD != -1; }
void Close()
{
if( m_iFD != -1 )
close( m_iFD );
m_iFD = -1;
}
int m_iFD;
RString m_sPath;
RString m_sName;
InputDevice m_Dev;
int aiAbsMin[ABS_MAX];
int aiAbsMax[ABS_MAX];
DeviceButton aiAbsMappingHigh[ABS_MAX];
DeviceButton aiAbsMappingLow[ABS_MAX];
};
static vector<EventDevice *> g_apEventDevices;
/* Return true if the numbered event device exists. sysfs may not always be
* there; return false if we don't know. */
static bool EventDeviceExists( int iNum )
{
RString sDir = ssprintf( "/sys/class" );
struct stat st;
if( stat(sDir, &st) == -1 )
return true;
RString sFile = ssprintf( "/sys/class/input/event%i", iNum );
return stat(sFile, &st) == 0;
}
static bool BitIsSet( const uint8_t *pArray, uint32_t iBit )
{
return !!(pArray[iBit/8] & (1<<(iBit%8)));
}
EventDevice::EventDevice()
{
m_iFD = -1;
}
bool EventDevice::Open( RString sFile, InputDevice dev )
{
m_sPath = sFile;
m_Dev = dev;
m_iFD = open( sFile, O_RDWR );
if( m_iFD == -1 )
{
if( errno == ENODEV )
return false;
if( !EventDeviceExists(m_iFD) )
return false;
LOG->Warn( "Error opening %s: %s", sFile.c_str(), strerror(errno) );
return false;
}
static bool bLogged = false;
if( !bLogged )
{
bLogged = true;
int iVersion;
if( ioctl(m_iFD, EVIOCGVERSION, &iVersion) == -1 )
LOG->Warn( "ioctl(EVIOCGVERSION): %s", strerror(errno) );
else
LOG->Info( "Event driver: v%i.%i.%i", (iVersion >> 16) & 0xFF, (iVersion >> 8) & 0xFF, iVersion & 0xFF );
}
char szName[1024];
if( ioctl(m_iFD, EVIOCGNAME(sizeof(szName)), szName) == -1 )
{
LOG->Warn( "ioctl(EVIOCGNAME): %s", strerror(errno) );
m_sName = "(unknown)";
}
else
{
m_sName = szName;
}
input_id DevInfo;
if( ioctl(m_iFD, EVIOCGID, &DevInfo) == -1 )
{
LOG->Warn( "ioctl(EVIOCGID): %s", strerror(errno) );
}
else
{
LOG->Info( "Input device: %s: %s device, ID %04x:%04x, version %x: %s", sFile.c_str(),
BustypeToString(DevInfo.bustype).c_str(), DevInfo.vendor, DevInfo.product,
DevInfo.version, m_sName.c_str() );
}
uint8_t iABSMask[ABS_MAX/8 + 1];
memset( iABSMask, 0, sizeof(iABSMask) );
if( ioctl(m_iFD, EVIOCGBIT(EV_ABS, sizeof(iABSMask)), iABSMask) < 0 )
LOG->Warn( "ioctl(EVIOCGBIT(EV_ABS)): %s", strerror(errno) );
if( !BitIsSet(iABSMask, ABS_X) && !BitIsSet(iABSMask, ABS_THROTTLE) && !BitIsSet(iABSMask, ABS_WHEEL) )
{
LOG->Info( " Not a joystick; ignored" );
Close();
return false;
}
uint8_t iKeyMask[KEY_MAX/8 + 1];
memset( iKeyMask, 0, sizeof(iKeyMask) );
if( ioctl(m_iFD, EVIOCGBIT(EV_KEY, sizeof(iKeyMask)), iKeyMask) < 0 )
LOG->Warn( "ioctl(EVIOCGBIT(EV_KEY)): %s", strerror(errno) );
uint8_t iEventTypes[EV_MAX/8];
memset( iEventTypes, 0, sizeof(iEventTypes) );
if( ioctl(m_iFD, EVIOCGBIT(0, EV_MAX), iEventTypes) == -1 )
LOG->Warn( "ioctl(EV_MAX): %s", strerror(errno) );
{
vector<RString> setEventTypes;
if( BitIsSet(iEventTypes, EV_SYN) ) setEventTypes.push_back( "syn" );
if( BitIsSet(iEventTypes, EV_KEY) ) setEventTypes.push_back( "key" );
if( BitIsSet(iEventTypes, EV_REL) ) setEventTypes.push_back( "rel" );
if( BitIsSet(iEventTypes, EV_ABS) ) setEventTypes.push_back( "abs" );
if( BitIsSet(iEventTypes, EV_MSC) ) setEventTypes.push_back( "misc" );
if( BitIsSet(iEventTypes, EV_SW) ) setEventTypes.push_back( "sw" );
if( BitIsSet(iEventTypes, EV_LED) ) setEventTypes.push_back( "led" );
if( BitIsSet(iEventTypes, EV_SND) ) setEventTypes.push_back( "snd" );
if( BitIsSet(iEventTypes, EV_REP) ) setEventTypes.push_back( "rep" );
if( BitIsSet(iEventTypes, EV_FF) ) setEventTypes.push_back( "ff" );
if( BitIsSet(iEventTypes, EV_PWR) ) setEventTypes.push_back( "pwr" );
if( BitIsSet(iEventTypes, EV_FF_STATUS) ) setEventTypes.push_back( "ff_status" );
LOG->Info( " Event types: %s", join(", ", setEventTypes).c_str() );
}
int iTotalKeys = 0;
for( int i = 0; i < KEY_MAX; ++i )
{
if( !BitIsSet(iKeyMask, i) )
continue;
++iTotalKeys;
}
int iTotalAxes = 0;
const DeviceButton iExtraAxes[] = { JOY_LEFT_2, JOY_UP_2, JOY_AUX_1, JOY_AUX_3 };
int iNextExtraAxis = 0;
for( int i = 0; i < ABS_MAX; ++i )
{
if( !BitIsSet(iABSMask, i) )
continue;
struct input_absinfo absinfo;
if( ioctl(m_iFD, EVIOCGABS(i), &absinfo) < 0 )
{
LOG->Warn( "ioctl(EVIOCGABS): %s", strerror(errno) );
continue;
}
//LOG->Info( " Axis %i: min: %i; max: %i; fuzz: %i; flat: %i",
// i, absinfo.minimum, absinfo.maximum, absinfo.fuzz, absinfo.flat );
aiAbsMin[i] = absinfo.minimum;
aiAbsMax[i] = absinfo.maximum;
aiAbsMappingHigh[i] = enum_add2(JOY_RIGHT, 2*i);
aiAbsMappingLow[i] = enum_add2(JOY_LEFT, 2*i);
if( i == ABS_X )
{
aiAbsMappingHigh[i] = JOY_RIGHT;
aiAbsMappingLow[i] = JOY_LEFT;
}
else if( i == ABS_Y )
{
aiAbsMappingHigh[i] = JOY_DOWN;
aiAbsMappingLow[i] = JOY_UP;
}
else if( i == ABS_Z )
{
aiAbsMappingHigh[i] = JOY_Z_DOWN;
aiAbsMappingLow[i] = JOY_Z_UP;
}
else if( i == ABS_RX )
{
aiAbsMappingHigh[i] = JOY_ROT_RIGHT;
aiAbsMappingLow[i] = JOY_ROT_LEFT;
}
else if( i == ABS_RY )
{
aiAbsMappingHigh[i] = JOY_ROT_DOWN;
aiAbsMappingLow[i] = JOY_ROT_UP;
}
else if( i == ABS_RZ )
{
aiAbsMappingHigh[i] = JOY_ROT_Z_DOWN;
aiAbsMappingLow[i] = JOY_ROT_Z_UP;
}
else if( i == ABS_HAT0X )
{
aiAbsMappingHigh[i] = JOY_HAT_RIGHT;
aiAbsMappingLow[i] = JOY_HAT_LEFT;
}
else if( i == ABS_HAT0Y )
{
aiAbsMappingHigh[i] = JOY_HAT_UP;
aiAbsMappingLow[i] = JOY_HAT_DOWN;
}
else
{
if( iNextExtraAxis < (int) ARRAYLEN(iExtraAxes) )
{
aiAbsMappingLow[i] = iExtraAxes[iNextExtraAxis];
aiAbsMappingHigh[i] = enum_add2( aiAbsMappingLow[i], 1 );
++iNextExtraAxis;
}
}
++iTotalAxes;
}
LOG->Info( " Total keys: %i; total axes: %i", iTotalKeys, iTotalAxes );
return true;
}
EventDevice::~EventDevice()
{
Close();
}
InputHandler_Linux_Event::InputHandler_Linux_Event()
{
if( InputHandler_Linux_Event::m_bFoundAnyJoysticks )
{
LOG->Trace( "InputHandler_Linux_Event disabled (joystick driver already loaded)" );
return;
}
/* Permission problems are likely. We want to warn about them only if there's actually
* an underlying device, but if we can't open the device, the only way we can tell if
* there'd be anything there is sysfs. That won't always be there. */
m_bFoundAnyJoysticks = false;
InputDevice NextDevice = DEVICE_JOY1;
for( int i = 0; i < 64; ++i )
{
RString sFile = ssprintf( "/dev/input/event%i", i );
g_apEventDevices.push_back( new EventDevice );
EventDevice *pDev = g_apEventDevices.back();
if( !pDev->Open(sFile, NextDevice) )
{
delete pDev;
g_apEventDevices.pop_back();
continue;
}
NextDevice = enum_add2(NextDevice, 1);
m_bFoundAnyJoysticks = true;
}
m_bShutdown = false;
if( m_bFoundAnyJoysticks )
{
m_InputThread.SetName( "Event input thread" );
m_InputThread.Create( InputThread_Start, this );
/* We loaded joysticks, so disable joydev. */
}
}
InputHandler_Linux_Event::~InputHandler_Linux_Event()
{
if( m_InputThread.IsCreated() )
{
m_bShutdown = true;
LOG->Trace( "Shutting down joystick thread ..." );
m_InputThread.Wait();
LOG->Trace( "Joystick thread shut down." );
}
for( int i = 0; i < (int) g_apEventDevices.size(); ++i )
delete g_apEventDevices[i];
g_apEventDevices.clear();
}
int InputHandler_Linux_Event::InputThread_Start( void *p )
{
((InputHandler_Linux_Event *) p)->InputThread();
return 0;
}
void InputHandler_Linux_Event::InputThread()
{
while( !m_bShutdown )
{
fd_set fdset;
FD_ZERO( &fdset );
int iMaxFD = -1;
for( int i = 0; i < (int) g_apEventDevices.size(); ++i )
{
int iFD = g_apEventDevices[i]->m_iFD;
if( !g_apEventDevices[i]->IsOpen() )
continue;
FD_SET( iFD, &fdset );
iMaxFD = max( iMaxFD, iFD );
}
if( iMaxFD == -1 )
break;
struct timeval zero = {0,100000};
if( select(iMaxFD+1, &fdset, NULL, NULL, &zero) <= 0 )
continue;
RageTimer now;
for( int i = 0; i < (int) g_apEventDevices.size(); ++i )
{
if( !g_apEventDevices[i]->IsOpen() )
continue;
if( !FD_ISSET(g_apEventDevices[i]->m_iFD, &fdset) )
continue;
input_event event;
int ret = read( g_apEventDevices[i]->m_iFD, &event, sizeof(event) );
if( ret == -1 )
{
LOG->Warn( "Error reading from %s: %s; disabled", g_apEventDevices[i]->m_sPath.c_str(), strerror(errno) );
g_apEventDevices[i]->Close();
continue;
}
if( ret != sizeof(event) )
{
LOG->Warn("Unexpected packet (size %i != %i) from joystick %i; disabled", ret, (int)sizeof(event), i);
g_apEventDevices[i]->Close();
continue;
}
switch (event.type) {
case EV_KEY: {
int iNum = event.code;
// In 2.6.11 using an EMS USB2, the event number for P1 Tri (the first button)
// is being reported as 32 instead of 0. Correct for this.
wrap( iNum, 32 ); // max number of joystick buttons. Make this a constant?
ButtonPressed( DeviceInput(g_apEventDevices[i]->m_Dev, enum_add2(JOY_BUTTON_1, iNum), event.value != 0, now) );
break;
}
case EV_ABS: {
ASSERT_M( event.code < ABS_MAX, ssprintf("%i", event.code) );
DeviceButton neg = g_apEventDevices[i]->aiAbsMappingLow[event.code];
DeviceButton pos = g_apEventDevices[i]->aiAbsMappingHigh[event.code];
float l = SCALE( int(event.value), (float) g_apEventDevices[i]->aiAbsMin[i], (float) g_apEventDevices[i]->aiAbsMax[i], -1.0f, 1.0f );
ButtonPressed( DeviceInput(g_apEventDevices[i]->m_Dev, neg, max(-l,0), now) );
ButtonPressed( DeviceInput(g_apEventDevices[i]->m_Dev, pos, max(+l,0), now) );
break;
}
}
}
}
InputHandler::UpdateTimer();
}
void InputHandler_Linux_Event::GetDevicesAndDescriptions( vector<InputDeviceInfo>& vDevicesOut )
{
for( unsigned i = 0; i < g_apEventDevices.size(); ++i )
{
EventDevice *pDev = g_apEventDevices[i];
vDevicesOut.push_back( InputDeviceInfo(pDev->m_Dev, pDev->m_sName) );
}
}
/*
* (c) 2003-2008 Glenn Maynard
* All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, and/or sell copies of the Software, and to permit persons to
* whom the Software is furnished to do so, provided that the above
* copyright notice(s) and this permission notice appear in all copies of
* the Software and that both the above copyright notice(s) and this
* permission notice appear in supporting documentation.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF
* THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS
* INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT
* OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
* OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
* OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*/
@@ -1,227 +1,227 @@
#include "global.h"
#include "InputHandler_Linux_Joystick.h"
#include "InputHandler_Linux_Event.h" // for m_bFoundAnyJoysticks
#include "RageLog.h"
#include "RageUtil.h"
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <linux/joystick.h>
#include <set>
REGISTER_INPUT_HANDLER_CLASS2( Joystick, Linux_Joystick );
static const char *Paths[InputHandler_Linux_Joystick::NUM_JOYSTICKS] =
{
"/dev/js0",
"/dev/js1",
"/dev/input/js0",
"/dev/input/js1",
};
InputHandler_Linux_Joystick::InputHandler_Linux_Joystick()
{
LOG->Trace( "InputHandler_Linux_Joystick::InputHandler_Linux_Joystick" );
for(int i = 0; i < NUM_JOYSTICKS; ++i)
fds[i] = -1;
if( InputHandler_Linux_Event::m_bFoundAnyJoysticks )
{
LOG->Trace( "InputHandler_Linux_Joystick disabled (joystick driver already loaded)" );
return;
}
/* We check both eg. /dev/js0 and /dev/input/js0. If both exist, they're probably
* the same device; keep track of device IDs so we don't open the same joystick
* twice. */
set< pair<int,int> > devices;
bool bFoundAnyJoysticks = false;
for(int i = 0; i < NUM_JOYSTICKS; ++i)
{
struct stat st;
if( stat( Paths[i], &st ) == -1 )
{
if( errno != ENOENT )
LOG->Warn( "Couldn't stat %s: %s", Paths[i], strerror(errno) );
continue;
}
if( !S_ISCHR( st.st_mode ) )
{
LOG->Warn( "Ignoring %s: not a character device", Paths[i] );
continue;
}
pair<int,int> dev( major(st.st_rdev), minor(st.st_rdev) );
if( devices.find(dev) != devices.end() )
continue; /* dupe */
devices.insert( dev );
fds[i] = open( Paths[i], O_RDONLY );
if(fds[i] != -1)
{
char szName[1024];
ZERO( szName );
if( ioctl(fds[i], JSIOCGNAME(sizeof(szName)), szName) < 0 )
m_sDescription[i] = ssprintf( "Unknown joystick at %s", Paths[i] );
else
m_sDescription[i] = szName;
LOG->Info("Opened %s", Paths[i]);
bFoundAnyJoysticks = true;
}
}
m_bShutdown = false;
if( bFoundAnyJoysticks )
{
m_InputThread.SetName( "Joystick thread" );
m_InputThread.Create( InputThread_Start, this );
InputHandler_Linux_Event::m_bFoundAnyJoysticks = true;
}
}
InputHandler_Linux_Joystick::~InputHandler_Linux_Joystick()
{
if( m_InputThread.IsCreated() )
{
m_bShutdown = true;
LOG->Trace( "Shutting down joystick thread ..." );
m_InputThread.Wait();
LOG->Trace( "Joystick thread shut down." );
}
for(int i = 0; i < NUM_JOYSTICKS; ++i)
if(fds[i] != -1) close(fds[i]);
}
int InputHandler_Linux_Joystick::InputThread_Start( void *p )
{
((InputHandler_Linux_Joystick *) p)->InputThread();
return 0;
}
void InputHandler_Linux_Joystick::InputThread()
{
while( !m_bShutdown )
{
fd_set fdset;
FD_ZERO(&fdset);
int max_fd = -1;
for(int i = 0; i < NUM_JOYSTICKS; ++i)
{
if (fds[i] < 0)
continue;
FD_SET(fds[i], &fdset);
max_fd = max(max_fd, fds[i]);
}
if(max_fd == -1)
break;
struct timeval zero = {0,100000};
if( select(max_fd+1, &fdset, NULL, NULL, &zero) <= 0 )
continue;
RageTimer now;
printf("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n");
for(int i = 0; i < NUM_JOYSTICKS; ++i)
{
if( fds[i] == -1 )
continue;
if(!FD_ISSET(fds[i], &fdset))
continue;
js_event event;
int ret = read(fds[i], &event, sizeof(event));
if(ret != sizeof(event))
{
LOG->Warn("Unexpected packet (size %i != %i) from joystick %i; disabled", ret, (int)sizeof(event), i);
close(fds[i]);
fds[i] = -1;
continue;
}
InputDevice id = InputDevice(DEVICE_JOY1 + i);
event.type &= ~JS_EVENT_INIT;
switch (event.type) {
case JS_EVENT_BUTTON: {
int iNum = event.number;
// In 2.6.11 using an EMS USB2, the event number for P1 Tri (the first button)
// is being reported as 32 instead of 0. Correct for this.
wrap( iNum, 32 ); // max number of joystick buttons. Make this a constant?
ButtonPressed( DeviceInput(id, enum_add2(JOY_BUTTON_1, iNum), event.value, now) );
break;
}
case JS_EVENT_AXIS: {
DeviceButton neg = enum_add2(JOY_LEFT, 2*event.number);
DeviceButton pos = enum_add2(JOY_RIGHT, 2*event.number);
float l = SCALE( int(event.value), 0.0f, 32767, 0.0f, 1.0f );
ButtonPressed( DeviceInput(id, neg, max(-l,0), now) );
ButtonPressed( DeviceInput(id, pos, max(+l,0), now) );
break;
}
default:
LOG->Warn("Unexpected packet (type %i) from joystick %i; disabled", event.type, i);
close(fds[i]);
fds[i] = -1;
continue;
}
}
}
InputHandler::UpdateTimer();
}
void InputHandler_Linux_Joystick::GetDevicesAndDescriptions( vector<InputDeviceInfo>& vDevicesOut )
{
for(int i = 0; i < NUM_JOYSTICKS; ++i)
{
if (fds[i] < 0)
continue;
vDevicesOut.push_back( InputDeviceInfo(InputDevice(DEVICE_JOY1+i), m_sDescription[i]) );
}
}
/*
* (c) 2003-2004 Glenn Maynard
* All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, and/or sell copies of the Software, and to permit persons to
* whom the Software is furnished to do so, provided that the above
* copyright notice(s) and this permission notice appear in all copies of
* the Software and that both the above copyright notice(s) and this
* permission notice appear in supporting documentation.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF
* THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS
* INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT
* OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
* OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
* OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*/
#include "global.h"
#include "InputHandler_Linux_Joystick.h"
#include "InputHandler_Linux_Event.h" // for m_bFoundAnyJoysticks
#include "RageLog.h"
#include "RageUtil.h"
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <linux/joystick.h>
#include <set>
REGISTER_INPUT_HANDLER_CLASS2( Joystick, Linux_Joystick );
static const char *Paths[InputHandler_Linux_Joystick::NUM_JOYSTICKS] =
{
"/dev/js0",
"/dev/js1",
"/dev/input/js0",
"/dev/input/js1",
};
InputHandler_Linux_Joystick::InputHandler_Linux_Joystick()
{
LOG->Trace( "InputHandler_Linux_Joystick::InputHandler_Linux_Joystick" );
for(int i = 0; i < NUM_JOYSTICKS; ++i)
fds[i] = -1;
if( InputHandler_Linux_Event::m_bFoundAnyJoysticks )
{
LOG->Trace( "InputHandler_Linux_Joystick disabled (joystick driver already loaded)" );
return;
}
/* We check both eg. /dev/js0 and /dev/input/js0. If both exist, they're probably
* the same device; keep track of device IDs so we don't open the same joystick
* twice. */
set< pair<int,int> > devices;
bool bFoundAnyJoysticks = false;
for(int i = 0; i < NUM_JOYSTICKS; ++i)
{
struct stat st;
if( stat( Paths[i], &st ) == -1 )
{
if( errno != ENOENT )
LOG->Warn( "Couldn't stat %s: %s", Paths[i], strerror(errno) );
continue;
}
if( !S_ISCHR( st.st_mode ) )
{
LOG->Warn( "Ignoring %s: not a character device", Paths[i] );
continue;
}
pair<int,int> dev( major(st.st_rdev), minor(st.st_rdev) );
if( devices.find(dev) != devices.end() )
continue; /* dupe */
devices.insert( dev );
fds[i] = open( Paths[i], O_RDONLY );
if(fds[i] != -1)
{
char szName[1024];
ZERO( szName );
if( ioctl(fds[i], JSIOCGNAME(sizeof(szName)), szName) < 0 )
m_sDescription[i] = ssprintf( "Unknown joystick at %s", Paths[i] );
else
m_sDescription[i] = szName;
LOG->Info("Opened %s", Paths[i]);
bFoundAnyJoysticks = true;
}
}
m_bShutdown = false;
if( bFoundAnyJoysticks )
{
m_InputThread.SetName( "Joystick thread" );
m_InputThread.Create( InputThread_Start, this );
InputHandler_Linux_Event::m_bFoundAnyJoysticks = true;
}
}
InputHandler_Linux_Joystick::~InputHandler_Linux_Joystick()
{
if( m_InputThread.IsCreated() )
{
m_bShutdown = true;
LOG->Trace( "Shutting down joystick thread ..." );
m_InputThread.Wait();
LOG->Trace( "Joystick thread shut down." );
}
for(int i = 0; i < NUM_JOYSTICKS; ++i)
if(fds[i] != -1) close(fds[i]);
}
int InputHandler_Linux_Joystick::InputThread_Start( void *p )
{
((InputHandler_Linux_Joystick *) p)->InputThread();
return 0;
}
void InputHandler_Linux_Joystick::InputThread()
{
while( !m_bShutdown )
{
fd_set fdset;
FD_ZERO(&fdset);
int max_fd = -1;
for(int i = 0; i < NUM_JOYSTICKS; ++i)
{
if (fds[i] < 0)
continue;
FD_SET(fds[i], &fdset);
max_fd = max(max_fd, fds[i]);
}
if(max_fd == -1)
break;
struct timeval zero = {0,100000};
if( select(max_fd+1, &fdset, NULL, NULL, &zero) <= 0 )
continue;
RageTimer now;
printf("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n");
for(int i = 0; i < NUM_JOYSTICKS; ++i)
{
if( fds[i] == -1 )
continue;
if(!FD_ISSET(fds[i], &fdset))
continue;
js_event event;
int ret = read(fds[i], &event, sizeof(event));
if(ret != sizeof(event))
{
LOG->Warn("Unexpected packet (size %i != %i) from joystick %i; disabled", ret, (int)sizeof(event), i);
close(fds[i]);
fds[i] = -1;
continue;
}
InputDevice id = InputDevice(DEVICE_JOY1 + i);
event.type &= ~JS_EVENT_INIT;
switch (event.type) {
case JS_EVENT_BUTTON: {
int iNum = event.number;
// In 2.6.11 using an EMS USB2, the event number for P1 Tri (the first button)
// is being reported as 32 instead of 0. Correct for this.
wrap( iNum, 32 ); // max number of joystick buttons. Make this a constant?
ButtonPressed( DeviceInput(id, enum_add2(JOY_BUTTON_1, iNum), event.value, now) );
break;
}
case JS_EVENT_AXIS: {
DeviceButton neg = enum_add2(JOY_LEFT, 2*event.number);
DeviceButton pos = enum_add2(JOY_RIGHT, 2*event.number);
float l = SCALE( int(event.value), 0.0f, 32767, 0.0f, 1.0f );
ButtonPressed( DeviceInput(id, neg, max(-l,0), now) );
ButtonPressed( DeviceInput(id, pos, max(+l,0), now) );
break;
}
default:
LOG->Warn("Unexpected packet (type %i) from joystick %i; disabled", event.type, i);
close(fds[i]);
fds[i] = -1;
continue;
}
}
}
InputHandler::UpdateTimer();
}
void InputHandler_Linux_Joystick::GetDevicesAndDescriptions( vector<InputDeviceInfo>& vDevicesOut )
{
for(int i = 0; i < NUM_JOYSTICKS; ++i)
{
if (fds[i] < 0)
continue;
vDevicesOut.push_back( InputDeviceInfo(InputDevice(DEVICE_JOY1+i), m_sDescription[i]) );
}
}
/*
* (c) 2003-2004 Glenn Maynard
* All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, and/or sell copies of the Software, and to permit persons to
* whom the Software is furnished to do so, provided that the above
* copyright notice(s) and this permission notice appear in all copies of
* the Software and that both the above copyright notice(s) and this
* permission notice appear in supporting documentation.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF
* THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS
* INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT
* OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
* OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
* OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*/
@@ -8,6 +8,7 @@
#include <cerrno>
#include <fcntl.h>
#include <dirent.h>
#include <sys/types.h>
#include <sys/stat.h>
bool MemoryCardDriverThreaded_Linux::TestWrite( UsbStorageDevice* pDevice )
@@ -46,7 +47,7 @@ static bool ReadFile( const RString &sPath, RString &sBuf )
LOG->Warn( "Error opening \"%s\": %s", sPath.c_str(), strerror(errno) );
return false;
}
while(1)
{
char buf[1024];
@@ -62,7 +63,7 @@ static bool ReadFile( const RString &sPath, RString &sBuf )
if( iGot < (int) sizeof(buf) )
break;
}
close(fd);
return true;
}
@@ -88,7 +89,7 @@ bool MemoryCardDriverThreaded_Linux::USBStorageDevicesChanged()
/* If a device is removed and reinserted, the inode of the /sys/block entry
* will change. */
RString sDevicePath = "/sys/block/";
vector<RString> asDevices;
GetFileList( sDevicePath, asDevices );
@@ -100,7 +101,7 @@ bool MemoryCardDriverThreaded_Linux::USBStorageDevicesChanged()
sThisDevices += ssprintf( "%i,", (int) buf.st_ino );
}
bool bChanged = sThisDevices != m_sLastDevices;
m_sLastDevices = sThisDevices;
if( bChanged )
@@ -111,7 +112,7 @@ bool MemoryCardDriverThreaded_Linux::USBStorageDevicesChanged()
void MemoryCardDriverThreaded_Linux::GetUSBStorageDevices( vector<UsbStorageDevice>& vDevicesOut )
{
LOG->Trace( "GetUSBStorageDevices" );
vDevicesOut.clear();
{
@@ -130,18 +131,20 @@ void MemoryCardDriverThreaded_Linux::GetUSBStorageDevices( vector<UsbStorageDevi
RString sPath = sBlockDevicePath + sDevice + "/";
usbd.sSysPath = sPath;
// Ignore non-removable devices.
/* Ignore non-removable devices. */
RString sBuf;
if( !ReadFile( sPath + "removable", sBuf ) )
continue; // already warned
if( atoi(sBuf) != 1 )
continue;
/* The kernel isn't exposing all of /sys atomically, so we end up missing
* the partition due to it not being shown yet. It won't show up until the
/*
* The kernel isn't exposing all of /sys atomically, so we end up missing
* the partition due to it not being shown yet. It won't show up until the
* kernel has scanned the partition table, which can take a variable amount
* of time, sometimes over a second. Watch for the "queue" sysfs directory,
* which is created after this, to tell when partition directories are created. */
* of time, sometimes over a second. Watch for the "queue" sysfs directory,
* which is created after this, to tell when partition directories are created.
*/
RageTimer WaitUntil;
WaitUntil += 5;
RString sQueueFilePath = usbd.sSysPath + "queue";
@@ -166,7 +169,7 @@ void MemoryCardDriverThreaded_Linux::GetUSBStorageDevices( vector<UsbStorageDevi
usleep(10000);
}
// If the first partition device exists, eg. /sys/block/uba/uba1, use it.
/* If the first partition device exists, eg. /sys/block/uba/uba1, use it. */
if( access(usbd.sSysPath + sDevice + "1", F_OK) != -1 )
{
LOG->Trace("OK");
@@ -178,7 +181,8 @@ void MemoryCardDriverThreaded_Linux::GetUSBStorageDevices( vector<UsbStorageDevi
usbd.sDevice = "/dev/" + sDevice;
}
/* sPath/device should be a symlink to the actual device. For USB
/*
* sPath/device should be a symlink to the actual device. For USB
* devices, it looks like this:
*
* device -> ../../devices/pci0000:00/0000:00:02.1/usb2/2-1/2-1:1.0
@@ -193,10 +197,13 @@ void MemoryCardDriverThreaded_Linux::GetUSBStorageDevices( vector<UsbStorageDevi
}
else
{
/* The full path looks like:
* ../../devices/pci0000:00/0000:00:02.1/usb2/2-2/2-2.1/2-2.1:1.0
/*
* The full path looks like
*
* ../../devices/pci0000:00/0000:00:02.1/usb2/2-2/2-2.1/2-2.1:1.0
*
* In newer kernels, it looks like:
*
* ../../../3-2.1:1.0
*
* Each path element refers to a new hop in the chain.
@@ -207,7 +214,8 @@ void MemoryCardDriverThreaded_Linux::GetUSBStorageDevices( vector<UsbStorageDevi
* .2 ... port 2 on the next hub ...
*
* We want the bus number and the port of the last hop. The level is
* the number of hops. */
* the number of hops.
*/
szLink[iRet] = 0;
vector<RString> asBits;
split( szLink, "/", asBits );
@@ -215,12 +223,12 @@ void MemoryCardDriverThreaded_Linux::GetUSBStorageDevices( vector<UsbStorageDevi
RString sHostPort = asBits[asBits.size()-1];
if( !sHostPort.empty() )
{
// Strip off the endpoint information after the colon.
/* Strip off the endpoint information after the colon. */
size_t pos = sHostPort.find(':');
if( pos != string::npos )
sHostPort.erase( pos );
// sHostPort is eg. 2-2.1.
/* sHostPort is eg. 2-2.1. */
sHostPort.Replace( "-", "." );
asBits.clear();
split( sHostPort, ".", asBits );
@@ -261,11 +269,11 @@ void MemoryCardDriverThreaded_Linux::GetUSBStorageDevices( vector<UsbStorageDevi
{
// Find where each device is mounted. Output looks like:
// /dev/sda1 /mnt/flash1 auto noauto,owner 0 0
// /dev/sdb1 /mnt/flash2 auto noauto,owner 0 0
// /dev/sdc1 /mnt/flash3 auto noauto,owner 0 0
RString fn = "/rootfs/etc/fstab";
RageFile f;
if( !f.Open(fn) )
@@ -273,7 +281,7 @@ void MemoryCardDriverThreaded_Linux::GetUSBStorageDevices( vector<UsbStorageDevi
LOG->Warn( "can't open '%s': %s", fn.c_str(), f.GetError().c_str() );
return;
}
RString sLine;
while( !f.AtEOF() )
{
@@ -284,18 +292,18 @@ void MemoryCardDriverThreaded_Linux::GetUSBStorageDevices( vector<UsbStorageDevi
LOG->Warn( "error reading '%s': %s", fn.c_str(), f.GetError().c_str() );
return;
}
char szScsiDevice[1024];
char szMountPoint[1024];
int iRet = sscanf( sLine, "%s %s", szScsiDevice, szMountPoint );
if( iRet != 2 )
continue; // don't process this line
RString sMountPoint = szMountPoint;
TrimLeft( sMountPoint );
TrimRight( sMountPoint );
// search for the mountpoint corresponding to the device
for( unsigned i=0; i<vDevicesOut.size(); i++ )
{
@@ -316,20 +324,20 @@ void MemoryCardDriverThreaded_Linux::GetUSBStorageDevices( vector<UsbStorageDevi
usbd.sDevice.c_str(), usbd.iBus, usbd.iLevel, usbd.iPort, usbd.idVendor, usbd.idProduct, usbd.sVendor.c_str(),
usbd.sProduct.c_str(), usbd.sSerial.c_str(), usbd.sOsMountDir.c_str() );
}
// Remove any devices that we couldn't find a mountpoint for.
/* Remove any devices that we couldn't find a mountpoint for. */
for( unsigned i=0; i<vDevicesOut.size(); i++ )
{
UsbStorageDevice& usbd = vDevicesOut[i];
if( usbd.sOsMountDir.empty() )
{
LOG->Trace( "Ignoring %s (couldn't find in /etc/fstab)", usbd.sDevice.c_str() );
vDevicesOut.erase( vDevicesOut.begin()+i );
--i;
}
}
LOG->Trace( "Done with GetUSBStorageDevices" );
}
@@ -338,8 +346,8 @@ bool MemoryCardDriverThreaded_Linux::Mount( UsbStorageDevice* pDevice )
{
ASSERT( !pDevice->sDevice.empty() );
RString sCommand = "mount " + pDevice->sDevice;
bool bMountedSuccessfully = ExecuteCommand( sCommand );
RString sCommand = "mount " + pDevice->sDevice;
bool bMountedSuccessfully = ExecuteCommand( sCommand );
return bMountedSuccessfully;
}
@@ -348,7 +356,7 @@ void MemoryCardDriverThreaded_Linux::Unmount( UsbStorageDevice* pDevice )
{
if( pDevice->sDevice.empty() )
return;
/* Use umount -l, so we unmount the device even if it's in use. Open
* files remain usable, and the device (eg. /dev/sda) won't be reused
* by new devices until those are closed. Without this, if something