Cleanup of the _BJoystickTweaker:

* Include sorting, header style, macro naming, variable naming, spacing, ...
* Some simplifications, early returns and the like.
* The device name list items were allocated with new but deleted with free. The
  whole private BJoystick list being modified by a friend class is dubious
  though and should be reworked. Ideally node monitoring would be implemented
  so that the list can be re-populated on demand instead of scanning through
  every time.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@41868 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Michael Lotz
2011-06-02 12:42:18 +00:00
parent 258cffcde2
commit 393ca9a124
3 changed files with 94 additions and 71 deletions
+24 -19
View File
@@ -8,15 +8,18 @@
#include <joystick_driver.h>
#include <List.h>
#include <Entry.h>
#include <List.h>
#if DEBUG
#include <stdio.h>
#endif
#define DEVICEPATH "/dev/joystick/"
#define JOYSTICKPATH "/boot/home/config/settings/joysticks/"
#define DEVICE_BASE_PATH "/dev/joystick/"
#define JOYSTICK_CONFIG_BASE_PATH "/boot/home/config/settings/joysticks/"
// TODO: this should use find_directory() instead and it should take
// common/system into account as well
class BJoystick;
@@ -29,29 +32,31 @@ typedef struct _joystick_info {
BList button_names;
} joystick_info;
class _BJoystickTweaker {
public:
_BJoystickTweaker();
_BJoystickTweaker(BJoystick &stick);
virtual ~_BJoystickTweaker();
status_t SendIOCT(uint32 op);
status_t GetInfo(_joystick_info* info, const char * ref);
_BJoystickTweaker();
_BJoystickTweaker(BJoystick &stick);
virtual ~_BJoystickTweaker();
status_t SendIOCT(uint32 op);
status_t GetInfo(_joystick_info* info, const char *ref);
// BeOS R5's joystick pref need these
status_t save_config(const entry_ref * ref = NULL);
void scan_including_disabled();
status_t get_info();
// BeOS R5's joystick pref need these
status_t save_config(const entry_ref *ref = NULL);
void scan_including_disabled();
status_t get_info();
private:
void _BuildFromJoystickDesc(char *string, _joystick_info* info);
status_t _ScanIncludingDisabled(const char* rootPath, BList *list,
BEntry *rootEntry = NULL);
void _BuildFromJoystickDesc(char *string,
_joystick_info *info);
status_t _ScanIncludingDisabled(const char *rootPath,
BList *list, BEntry *rootEntry = NULL);
void _EmpyList(BList *list);
BJoystick* fJoystick;
void _EmpyList(BList *list);
BJoystick * fJoystick;
#if DEBUG
public:
static FILE *sLogFile;
static FILE * sLogFile;
#endif
};
+4 -3
View File
@@ -116,9 +116,10 @@ BJoystick::Open(const char *portName, bool enhanced)
fBeBoxMode = !enhanced;
char nameBuffer[64];
if (portName[0] != '/')
snprintf(nameBuffer, sizeof(nameBuffer), DEVICEPATH"/%s", portName);
else
if (portName[0] != '/') {
snprintf(nameBuffer, sizeof(nameBuffer), DEVICE_BASE_PATH"/%s",
portName);
} else
snprintf(nameBuffer, sizeof(nameBuffer), "%s", portName);
if (fFD >= 0)
+66 -49
View File
@@ -8,20 +8,21 @@
*/
#include "JoystickTweaker.h"
#include <new>
#include <stdio.h>
#include <stdlib.h>
#include <Path.h>
#include <Directory.h>
#include <String.h>
#include <Debug.h>
#include "Joystick.h"
#define STRINGLENGTHCPY 64
#include <Directory.h>
#include <Joystick.h>
#include <Path.h>
#include <String.h>
#include <UTF8.h>
#define STACK_STRING_BUFFER_SIZE 64
#if DEBUG
inline void
LOG(const char *fmt, ...)
@@ -31,7 +32,9 @@ LOG(const char *fmt, ...)
va_start(ap, fmt);
vsprintf(buf, fmt, ap);
va_end(ap);
fputs(buf, _BJoystickTweaker::sLogFile); fflush(_BJoystickTweaker::sLogFile);
fputs(buf, _BJoystickTweaker::sLogFile);
fflush(_BJoystickTweaker::sLogFile);
}
# define LOG_ERR(text...) LOG(text)
FILE *_BJoystickTweaker::sLogFile = NULL;
@@ -42,21 +45,22 @@ FILE *_BJoystickTweaker::sLogFile = NULL;
#define CALLED() LOG("%s\n", __PRETTY_FUNCTION__)
_BJoystickTweaker::_BJoystickTweaker()
{
CALLED();
#if DEBUG
sLogFile = fopen("/var/log/libdevice.log", "a");
sLogFile = fopen("/var/log/joystick.log", "a");
#endif
CALLED();
}
_BJoystickTweaker::_BJoystickTweaker(BJoystick &stick)
{
CALLED();
#if DEBUG
sLogFile = fopen("/var/log/libdevice.log", "a");
sLogFile = fopen("/var/log/joystick.log", "a");
#endif
CALLED();
fJoystick = &stick;
}
@@ -76,13 +80,13 @@ _BJoystickTweaker::save_config(const entry_ref *ref)
status_t
_BJoystickTweaker::_ScanIncludingDisabled(const char* rootPath, BList *list,
_BJoystickTweaker::_ScanIncludingDisabled(const char *rootPath, BList *list,
BEntry *rootEntry)
{
BDirectory root;
if (rootEntry != NULL)
root.SetTo( rootEntry);
root.SetTo(rootEntry);
else if (rootPath != NULL)
root.SetTo(rootPath);
else
@@ -91,18 +95,31 @@ _BJoystickTweaker::_ScanIncludingDisabled(const char* rootPath, BList *list,
BEntry entry;
ASSERT(list != NULL);
while ((root.GetNextEntry(&entry)) > B_ERROR ) {
while (root.GetNextEntry(&entry) == B_OK) {
if (entry.IsDirectory()) {
_ScanIncludingDisabled(rootPath, list, &entry);
} else {
BPath path;
entry.GetPath(&path);
status_t result = _ScanIncludingDisabled(rootPath, list, &entry);
if (result != B_OK)
return result;
BString *str = new BString(path.Path());
str->RemoveFirst(rootPath);
list->AddItem(str);
continue;
}
BPath path;
status_t result = entry.GetPath(&path);
if (result != B_OK)
return result;
BString *deviceName = new(std::nothrow) BString(path.Path());
if (deviceName == NULL)
return B_NO_MEMORY;
deviceName->RemoveFirst(rootPath);
if (!list->AddItem(deviceName)) {
delete deviceName;
return B_ERROR;
}
}
return B_OK;
}
@@ -111,17 +128,18 @@ void
_BJoystickTweaker::scan_including_disabled()
{
CALLED();
// First, we empty the list
_EmpyList(fJoystick->fDevices);
_ScanIncludingDisabled(DEVICEPATH, fJoystick->fDevices);
_ScanIncludingDisabled(DEVICE_BASE_PATH, fJoystick->fDevices);
}
void
_BJoystickTweaker::_EmpyList(BList *list)
{
for (int32 count = list->CountItems() - 1; count >= 0; count--)
free(list->RemoveItem(count));
for (int32 i = 0; i < list->CountItems(); i++)
delete (BString *)list->ItemAt(i);
list->MakeEmpty();
}
@@ -134,33 +152,32 @@ _BJoystickTweaker::get_info()
status_t
_BJoystickTweaker::GetInfo(_joystick_info* info,
const char * ref)
_BJoystickTweaker::GetInfo(_joystick_info *info, const char *ref)
{
CALLED();
status_t err = B_ERROR;
BString str(JOYSTICKPATH);
str.Append(ref);
BString configFilePath(JOYSTICK_CONFIG_BASE_PATH);
configFilePath.Append(ref);
FILE *file = fopen(str.String(), "r");
if (file != NULL) {
char line [STRINGLENGTHCPY];
while (fgets ( line, sizeof line, file ) != NULL ) {
int len = strlen(line);
if (len > 0 && line[len-1] == '\n')
line[len-1] = '\0';
_BuildFromJoystickDesc(line, info);
}
fclose(file);
FILE *file = fopen(configFilePath.String(), "r");
if (file == NULL)
return B_ERROR;
char line[STACK_STRING_BUFFER_SIZE];
while (fgets(line, sizeof(line), file) != NULL) {
int length = strlen(line);
if (length > 0 && line[length - 1] == '\n')
line[length - 1] = '\0';
_BuildFromJoystickDesc(line, info);
}
err = B_OK;
return err;
fclose(file);
return B_OK;
}
void
_BJoystickTweaker::_BuildFromJoystickDesc(char *string, _joystick_info* info)
_BJoystickTweaker::_BuildFromJoystickDesc(char *string, _joystick_info *info)
{
BString str(string);
str.RemoveAll("\"");
@@ -168,11 +185,11 @@ _BJoystickTweaker::_BuildFromJoystickDesc(char *string, _joystick_info* info)
if (str.IFindFirst("module") != -1) {
str.RemoveFirst("module = ");
strlcpy(info->module_info.module_name, str.String(),
STRINGLENGTHCPY);
STACK_STRING_BUFFER_SIZE);
} else if (str.IFindFirst("gadget") != -1) {
str.RemoveFirst("gadget = ");
strlcpy(info->module_info.device_name, str.String(),
STRINGLENGTHCPY);
STACK_STRING_BUFFER_SIZE);
} else if (str.IFindFirst("num_axes") != -1) {
str.RemoveFirst("num_axes = ");
info->module_info.num_axes = atoi(str.String());
@@ -194,7 +211,6 @@ _BJoystickTweaker::_BuildFromJoystickDesc(char *string, _joystick_info* info)
status_t
_BJoystickTweaker::SendIOCT(uint32 op)
{
status_t err = B_ERROR;
switch (op) {
case B_JOYSTICK_SET_DEVICE_MODULE:
break;
@@ -210,5 +226,6 @@ _BJoystickTweaker::SendIOCT(uint32 op)
default:
break;
}
return err;
return B_ERROR;
}