Code cleanup

git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@27172 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Fredrik Modeen 2008-08-23 11:16:40 +00:00
parent 88b0546730
commit 6ecf7db5fd
2 changed files with 99 additions and 104 deletions

View File

@ -4,18 +4,8 @@
* Distributed under the terms of the MIT License. * Distributed under the terms of the MIT License.
*/ */
/*
joystick preference app
JoyCalib::JoyCalib(BRect, BJoystick &, BWindow *):
__8JoyCalibG5BRectR9BJoystickP7BWindow:
*/
#include <List.h> #include <List.h>
#include <Path.h> #include "Joystick.h"
#include <Directory.h>
#include <String.h>
#include <Debug.h>
#include <errno.h> #include <errno.h>
#include <fcntl.h> #include <fcntl.h>
@ -23,7 +13,11 @@ __8JoyCalibG5BRectR9BJoystickP7BWindow:
#include <sys/ioctl.h> #include <sys/ioctl.h>
#include "Joystick.h" #include <Path.h>
#include <Directory.h>
#include <String.h>
#include <Debug.h>
#if DEBUG #if DEBUG
inline void inline void
@ -49,9 +43,9 @@ FILE *BJoystick::sLogFile = NULL;
BJoystick::BJoystick() BJoystick::BJoystick()
: :
_mBeBoxMode(false), fBeBoxMode(false),
_fDevices(new BList), fDevices(new BList),
m_info(new _joystick_info()) fJoystickInfo(new _joystick_info())
{ {
#if DEBUG #if DEBUG
sLogFile = fopen("/var/log/libdevice.log", "a"); sLogFile = fopen("/var/log/libdevice.log", "a");
@ -65,12 +59,12 @@ BJoystick::~BJoystick()
if (ffd >= 0) if (ffd >= 0)
close(ffd); close(ffd);
for (int32 count = _fDevices->CountItems() - 1; count >= 0; count--) { for (int32 count = fDevices->CountItems() - 1; count >= 0; count--) {
free(_fDevices->RemoveItem(count)); free(fDevices->RemoveItem(count));
} }
delete _fDevices; delete fDevices;
delete m_info; delete fJoystickInfo;
} }
@ -89,7 +83,7 @@ BJoystick::Open(const char *portName, bool enter_enhanced)
char buf[64]; char buf[64];
if(!enter_enhanced) if(!enter_enhanced)
_mBeBoxMode = !enter_enhanced; fBeBoxMode = !enter_enhanced;
if (portName == NULL) if (portName == NULL)
return B_BAD_VALUE; return B_BAD_VALUE;
@ -114,22 +108,17 @@ BJoystick::Open(const char *portName, bool enter_enhanced)
int flags = fcntl(ffd, F_GETFL); int flags = fcntl(ffd, F_GETFL);
fcntl(ffd, F_SETFL, flags & ~O_NONBLOCK); fcntl(ffd, F_SETFL, flags & ~O_NONBLOCK);
//DriverControl(); //Read the Joystick Description file for this port/joystick
} _BJoystickTweaker jt(*this);
// TODO: I wonder why the return type is a status_t, jt.GetInfo(fJoystickInfo, portName);
// since we (as BeOS does) return the descriptor number for the device...
LOG("ioctl - %d\n", fJoystickInfo->num_buttons);
//Read the Joystick Description file for this port/joystick ioctl(ffd, B_JOYSTICK_SET_DEVICE_MODULE, fJoystickInfo);
_BJoystickTweaker jt(*this); ioctl(ffd, B_JOYSTICK_GET_DEVICE_MODULE, fJoystickInfo);
jt.get_info(m_info, portName); LOG("ioctl - %d\n", fJoystickInfo->num_buttons);
LOG("ioctl - %d\n", m_info->num_buttons);
ioctl(ffd, B_JOYSTICK_SET_DEVICE_MODULE, m_info);
ioctl(ffd, B_JOYSTICK_GET_DEVICE_MODULE, m_info);
LOG("ioctl - %d\n", m_info->num_buttons);
if (ffd >= 0) {
return ffd; return ffd;
} else } else
return errno; return errno;
} }
@ -159,13 +148,13 @@ int32
BJoystick::CountDevices() BJoystick::CountDevices()
{ {
CALLED(); CALLED();
int32 count = 0;
// Refresh devices list // Refresh devices list
ScanDevices(true); ScanDevices(true);
if (_fDevices != NULL) int32 count = 0;
count = _fDevices->CountItems(); if (fDevices != NULL)
count = fDevices->CountItems();
LOG("Count = %d\n", count); LOG("Count = %d\n", count);
return count; return count;
@ -177,8 +166,8 @@ BJoystick::GetDeviceName(int32 n, char *name, size_t bufSize)
{ {
CALLED(); CALLED();
BString *temp = new BString(); BString *temp = new BString();
if (_fDevices != NULL && _fDevices->CountItems() > n) if (fDevices != NULL && fDevices->CountItems() > n)
temp = static_cast<BString*>(_fDevices->ItemAt(n)); temp = static_cast<BString*>(fDevices->ItemAt(n));
else else
return B_BAD_INDEX; return B_BAD_INDEX;
@ -198,8 +187,8 @@ bool
BJoystick::EnterEnhancedMode(const entry_ref *ref) BJoystick::EnterEnhancedMode(const entry_ref *ref)
{ {
CALLED(); CALLED();
_mBeBoxMode = false; fBeBoxMode = false;
return !_mBeBoxMode; return !fBeBoxMode;
} }
@ -207,7 +196,7 @@ int32
BJoystick::CountSticks() BJoystick::CountSticks()
{ {
CALLED(); CALLED();
return m_info->num_sticks; return fJoystickInfo->num_sticks;
} }
@ -215,7 +204,7 @@ int32
BJoystick::CountAxes() BJoystick::CountAxes()
{ {
CALLED(); CALLED();
return m_info->num_axes; return fJoystickInfo->num_axes;
} }
@ -223,7 +212,7 @@ int32
BJoystick::CountHats() BJoystick::CountHats()
{ {
CALLED(); CALLED();
return m_info->num_hats; return fJoystickInfo->num_hats;
} }
@ -231,15 +220,15 @@ int32
BJoystick::CountButtons() BJoystick::CountButtons()
{ {
CALLED(); CALLED();
return m_info->num_buttons; return fJoystickInfo->num_buttons;
} }
status_t status_t
BJoystick::GetControllerModule(BString *out_name) BJoystick::GetControllerModule(BString *out_name)
{ {
CALLED(); CALLED();
if (m_info != NULL && ffd >= 0) { if (fJoystickInfo != NULL && ffd >= 0) {
out_name->SetTo(m_info->module_name); out_name->SetTo(fJoystickInfo->module_name);
return B_OK; return B_OK;
} else } else
return B_ERROR; return B_ERROR;
@ -251,8 +240,8 @@ status_t
BJoystick::GetControllerName(BString *out_name) BJoystick::GetControllerName(BString *out_name)
{ {
CALLED(); CALLED();
if (m_info != NULL && ffd >= 0) { if (fJoystickInfo != NULL && ffd >= 0) {
out_name->SetTo(m_info->controller_name); out_name->SetTo(fJoystickInfo->controller_name);
return B_OK; return B_OK;
} else } else
return B_ERROR; return B_ERROR;
@ -263,7 +252,7 @@ bool
BJoystick::IsCalibrationEnabled() BJoystick::IsCalibrationEnabled()
{ {
CALLED(); CALLED();
return m_info->calibration_enable; return fJoystickInfo->calibration_enable;
} }
@ -271,8 +260,8 @@ status_t
BJoystick::EnableCalibration(bool calibrates) BJoystick::EnableCalibration(bool calibrates)
{ {
CALLED(); CALLED();
if(ffd >= 0) { if (ffd >= 0) {
m_info->calibration_enable = calibrates; fJoystickInfo->calibration_enable = calibrates;
return B_OK; return B_OK;
} else } else
return B_NO_INIT; return B_NO_INIT;
@ -283,7 +272,7 @@ status_t
BJoystick::SetMaxLatency(bigtime_t max_latency) BJoystick::SetMaxLatency(bigtime_t max_latency)
{ {
CALLED(); CALLED();
m_info->max_latency = max_latency; fJoystickInfo->max_latency = max_latency;
//else B_ERROR (when?) //else B_ERROR (when?)
return B_OK; return B_OK;
} }
@ -342,7 +331,7 @@ status_t
BJoystick::Update(void) BJoystick::Update(void)
{ {
CALLED(); CALLED();
if(ffd >= 0) { if (ffd >= 0) {
return B_OK; return B_OK;
} else } else
return B_ERROR; return B_ERROR;
@ -357,7 +346,7 @@ BJoystick::Calibrate(struct _extended_joystick *reading)
status_t status_t
BJoystick::gather_enhanced_info(const entry_ref *ref) BJoystick::GatherEnhanced_info(const entry_ref *ref)
{ {
CALLED(); CALLED();
return B_ERROR; return B_ERROR;
@ -365,17 +354,9 @@ BJoystick::gather_enhanced_info(const entry_ref *ref)
status_t status_t
BJoystick::save_config(const entry_ref *ref) BJoystick::SaveConfig(const entry_ref *ref)
{ {
CALLED(); CALLED();
return B_ERROR; return B_ERROR;
} }
/* These functions are here to maintain Binary Compatibility */
void BJoystick::_ReservedJoystick1() {CALLED();}
void BJoystick::_ReservedJoystick2() {CALLED();}
void BJoystick::_ReservedJoystick3() {CALLED();}
status_t BJoystick::_Reserved_Joystick_4(void *, ...) {CALLED();return B_ERROR;}
status_t BJoystick::_Reserved_Joystick_5(void *, ...) {CALLED();return B_ERROR;}
status_t BJoystick::_Reserved_Joystick_6(void *, ...) {CALLED();return B_ERROR;}

View File

@ -7,25 +7,37 @@
* *
*/ */
#include "JoystickTweaker.h" #include "JoystickTweaker.h"
#include "Joystick.h"
#include <Font.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <Path.h>
#include <Directory.h>
#include <String.h>
#include <Debug.h>
#include "Joystick.h"
#define STRINGLENGTHCPY 64 #define STRINGLENGTHCPY 64
#include <UTF8.h> #include <UTF8.h>
#if DEBUG #if DEBUG
inline void LOG(const char *fmt, ...) { char buf[1024]; va_list ap; va_start(ap, fmt); vsprintf(buf, fmt, ap); va_end(ap); \ inline void
fputs(buf, _BJoystickTweaker::sLogFile); fflush(_BJoystickTweaker::sLogFile); } LOG(const char *fmt, ...)
#define LOG_ERR(text...) LOG(text) {
FILE *_BJoystickTweaker::sLogFile = NULL; char buf[1024];
va_list ap;
va_start(ap, fmt);
vsprintf(buf, fmt, ap);
va_end(ap);
fputs(buf, BJoystick::sLogFile); fflush(BJoystick::sLogFile);
}
# define LOG_ERR(text...) LOG(text)
FILE *BJoystick::sLogFile = NULL;
#else #else
#define LOG(text...) # define LOG(text...)
#define LOG_ERR(text...) fprintf(stderr, text) # define LOG_ERR(text...) fprintf(stderr, text)
#endif #endif
#define CALLED() LOG("%s\n", __PRETTY_FUNCTION__) #define CALLED() LOG("%s\n", __PRETTY_FUNCTION__)
@ -64,7 +76,7 @@ _BJoystickTweaker::save_config(const entry_ref *ref)
status_t status_t
_BJoystickTweaker::scan_including_disabled(const char* rootPath, BList *list, _BJoystickTweaker::_ScanIncludingDisabled(const char* rootPath, BList *list,
BEntry *rootEntry) BEntry *rootEntry)
{ {
BDirectory root; BDirectory root;
@ -81,7 +93,7 @@ _BJoystickTweaker::scan_including_disabled(const char* rootPath, BList *list,
ASSERT(list != NULL); ASSERT(list != NULL);
while ((root.GetNextEntry(&entry)) > B_ERROR ) { while ((root.GetNextEntry(&entry)) > B_ERROR ) {
if (entry.IsDirectory()) { if (entry.IsDirectory()) {
scan_including_disabled(rootPath, list, &entry); _ScanIncludingDisabled(rootPath, list, &entry);
} else { } else {
BPath path; BPath path;
entry.GetPath(&path); entry.GetPath(&path);
@ -100,10 +112,16 @@ _BJoystickTweaker::scan_including_disabled()
{ {
CALLED(); CALLED();
// First, we empty the list // First, we empty the list
for (int32 count = fJoystick->_fDevices->CountItems() - 1; count >= 0; count--) _EmpyList(fJoystick->fDevices);
free(fJoystick->_fDevices->RemoveItem(count)); _ScanIncludingDisabled(DEVICEPATH, fJoystick->fDevices);
}
scan_including_disabled(DEVICEPATH, fJoystick->_fDevices);
void
_BJoystickTweaker::_EmpyList(BList *list)
{
for (int32 count = list->CountItems() - 1; count >= 0; count--)
free(list->RemoveItem(count));
} }
@ -116,8 +134,8 @@ _BJoystickTweaker::get_info()
status_t status_t
_BJoystickTweaker::get_info(_joystick_info* info, _BJoystickTweaker::GetInfo(_joystick_info* info,
const char * ref) const char * ref)
{ {
CALLED(); CALLED();
status_t err = B_ERROR; status_t err = B_ERROR;
@ -131,7 +149,7 @@ _BJoystickTweaker::get_info(_joystick_info* info,
int len = strlen(line); int len = strlen(line);
if (len > 0 && line[len-1] == '\n') if (len > 0 && line[len-1] == '\n')
line[len-1] = '\0'; line[len-1] = '\0';
BuildFromJoystickDesc(line, info); _BuildFromJoystickDesc(line, info);
} }
fclose(file); fclose(file);
} }
@ -142,7 +160,7 @@ _BJoystickTweaker::get_info(_joystick_info* info,
void void
_BJoystickTweaker::BuildFromJoystickDesc(char *string, _joystick_info* info) _BJoystickTweaker::_BuildFromJoystickDesc(char *string, _joystick_info* info)
{ {
BString str(string); BString str(string);
str.RemoveAll("\""); str.RemoveAll("\"");
@ -155,22 +173,18 @@ _BJoystickTweaker::BuildFromJoystickDesc(char *string, _joystick_info* info)
strncpy(info->controller_name, str.String(), STRINGLENGTHCPY); strncpy(info->controller_name, str.String(), STRINGLENGTHCPY);
} else if (str.IFindFirst("num_axes") != -1) { } else if (str.IFindFirst("num_axes") != -1) {
str.RemoveFirst("num_axes = "); str.RemoveFirst("num_axes = ");
//info->num_axes = atoi(str.String()); info->num_axes = atoi(str.String());
//LOG("%s\n", str.String());
} else if (str.IFindFirst("num_hats") != -1) { } else if (str.IFindFirst("num_hats") != -1) {
str.RemoveFirst("num_hats = "); str.RemoveFirst("num_hats = ");
//info->num_hats = atoi(str.String()); info->num_hats = atoi(str.String());
//LOG("%s\n", str.String());
} else if (str.IFindFirst("num_buttons") != -1) { } else if (str.IFindFirst("num_buttons") != -1) {
str.RemoveFirst("num_buttons = "); str.RemoveFirst("num_buttons = ");
//info->num_buttons = atoi(str.String()); info->num_buttons = atoi(str.String());
//LOG("%s\n", str.String());
} else if (str.IFindFirst("num_sticks") != -1) { } else if (str.IFindFirst("num_sticks") != -1) {
str.RemoveFirst("num_sticks = "); str.RemoveFirst("num_sticks = ");
//info->num_sticks = atoi(str.String()); info->num_sticks = atoi(str.String());
//LOG("%s\n", str.String());
} else { } else {
// LOG("Path = %s\n", str->String()); LOG("Path = %s\n", str->String());
} }
} }
@ -180,19 +194,19 @@ _BJoystickTweaker::SendIOCT(uint32 op)
{ {
status_t err = B_ERROR; status_t err = B_ERROR;
switch (op) { switch (op) {
case B_JOYSTICK_SET_DEVICE_MODULE: case B_JOYSTICK_SET_DEVICE_MODULE:
break; break;
case B_JOYSTICK_GET_DEVICE_MODULE: case B_JOYSTICK_GET_DEVICE_MODULE:
break; break;
case B_JOYSTICK_GET_SPEED_COMPENSATION: case B_JOYSTICK_GET_SPEED_COMPENSATION:
case B_JOYSTICK_SET_SPEED_COMPENSATION: case B_JOYSTICK_SET_SPEED_COMPENSATION:
case B_JOYSTICK_GET_MAX_LATENCY: case B_JOYSTICK_GET_MAX_LATENCY:
case B_JOYSTICK_SET_MAX_LATENCY: case B_JOYSTICK_SET_MAX_LATENCY:
case B_JOYSTICK_SET_RAW_MODE: case B_JOYSTICK_SET_RAW_MODE:
default: default:
break; break;
} }
return err; return err;
} }