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 <joystick_driver.h>
#include <List.h>
#include <Entry.h> #include <Entry.h>
#include <List.h>
#if DEBUG #if DEBUG
#include <stdio.h> #include <stdio.h>
#endif #endif
#define DEVICEPATH "/dev/joystick/" #define DEVICE_BASE_PATH "/dev/joystick/"
#define JOYSTICKPATH "/boot/home/config/settings/joysticks/" #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; class BJoystick;
@@ -29,29 +32,31 @@ typedef struct _joystick_info {
BList button_names; BList button_names;
} joystick_info; } joystick_info;
class _BJoystickTweaker { class _BJoystickTweaker {
public: public:
_BJoystickTweaker(); _BJoystickTweaker();
_BJoystickTweaker(BJoystick &stick); _BJoystickTweaker(BJoystick &stick);
virtual ~_BJoystickTweaker(); virtual ~_BJoystickTweaker();
status_t SendIOCT(uint32 op); status_t SendIOCT(uint32 op);
status_t GetInfo(_joystick_info* info, const char * ref); status_t GetInfo(_joystick_info* info, const char *ref);
// BeOS R5's joystick pref need these // BeOS R5's joystick pref need these
status_t save_config(const entry_ref * ref = NULL); status_t save_config(const entry_ref *ref = NULL);
void scan_including_disabled(); void scan_including_disabled();
status_t get_info(); status_t get_info();
private: private:
void _BuildFromJoystickDesc(char *string, _joystick_info* info); void _BuildFromJoystickDesc(char *string,
status_t _ScanIncludingDisabled(const char* rootPath, BList *list, _joystick_info *info);
BEntry *rootEntry = NULL); status_t _ScanIncludingDisabled(const char *rootPath,
BList *list, BEntry *rootEntry = NULL);
void _EmpyList(BList *list); void _EmpyList(BList *list);
BJoystick* fJoystick;
BJoystick * fJoystick;
#if DEBUG #if DEBUG
public: public:
static FILE *sLogFile; static FILE * sLogFile;
#endif #endif
}; };
+4 -3
View File
@@ -116,9 +116,10 @@ BJoystick::Open(const char *portName, bool enhanced)
fBeBoxMode = !enhanced; fBeBoxMode = !enhanced;
char nameBuffer[64]; char nameBuffer[64];
if (portName[0] != '/') if (portName[0] != '/') {
snprintf(nameBuffer, sizeof(nameBuffer), DEVICEPATH"/%s", portName); snprintf(nameBuffer, sizeof(nameBuffer), DEVICE_BASE_PATH"/%s",
else portName);
} else
snprintf(nameBuffer, sizeof(nameBuffer), "%s", portName); snprintf(nameBuffer, sizeof(nameBuffer), "%s", portName);
if (fFD >= 0) if (fFD >= 0)
+66 -49
View File
@@ -8,20 +8,21 @@
*/ */
#include "JoystickTweaker.h" #include "JoystickTweaker.h"
#include <new>
#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 <Debug.h>
#include <Directory.h>
#include "Joystick.h" #include <Joystick.h>
#include <Path.h>
#define STRINGLENGTHCPY 64 #include <String.h>
#include <UTF8.h> #include <UTF8.h>
#define STACK_STRING_BUFFER_SIZE 64
#if DEBUG #if DEBUG
inline void inline void
LOG(const char *fmt, ...) LOG(const char *fmt, ...)
@@ -31,7 +32,9 @@ LOG(const char *fmt, ...)
va_start(ap, fmt); va_start(ap, fmt);
vsprintf(buf, fmt, ap); vsprintf(buf, fmt, ap);
va_end(ap); va_end(ap);
fputs(buf, _BJoystickTweaker::sLogFile); fflush(_BJoystickTweaker::sLogFile);
fputs(buf, _BJoystickTweaker::sLogFile);
fflush(_BJoystickTweaker::sLogFile);
} }
# define LOG_ERR(text...) LOG(text) # define LOG_ERR(text...) LOG(text)
FILE *_BJoystickTweaker::sLogFile = NULL; FILE *_BJoystickTweaker::sLogFile = NULL;
@@ -42,21 +45,22 @@ FILE *_BJoystickTweaker::sLogFile = NULL;
#define CALLED() LOG("%s\n", __PRETTY_FUNCTION__) #define CALLED() LOG("%s\n", __PRETTY_FUNCTION__)
_BJoystickTweaker::_BJoystickTweaker() _BJoystickTweaker::_BJoystickTweaker()
{ {
CALLED();
#if DEBUG #if DEBUG
sLogFile = fopen("/var/log/libdevice.log", "a"); sLogFile = fopen("/var/log/joystick.log", "a");
#endif #endif
CALLED();
} }
_BJoystickTweaker::_BJoystickTweaker(BJoystick &stick) _BJoystickTweaker::_BJoystickTweaker(BJoystick &stick)
{ {
CALLED();
#if DEBUG #if DEBUG
sLogFile = fopen("/var/log/libdevice.log", "a"); sLogFile = fopen("/var/log/joystick.log", "a");
#endif #endif
CALLED();
fJoystick = &stick; fJoystick = &stick;
} }
@@ -76,13 +80,13 @@ _BJoystickTweaker::save_config(const entry_ref *ref)
status_t status_t
_BJoystickTweaker::_ScanIncludingDisabled(const char* rootPath, BList *list, _BJoystickTweaker::_ScanIncludingDisabled(const char *rootPath, BList *list,
BEntry *rootEntry) BEntry *rootEntry)
{ {
BDirectory root; BDirectory root;
if (rootEntry != NULL) if (rootEntry != NULL)
root.SetTo( rootEntry); root.SetTo(rootEntry);
else if (rootPath != NULL) else if (rootPath != NULL)
root.SetTo(rootPath); root.SetTo(rootPath);
else else
@@ -91,18 +95,31 @@ _BJoystickTweaker::_ScanIncludingDisabled(const char* rootPath, BList *list,
BEntry entry; BEntry entry;
ASSERT(list != NULL); ASSERT(list != NULL);
while ((root.GetNextEntry(&entry)) > B_ERROR ) { while (root.GetNextEntry(&entry) == B_OK) {
if (entry.IsDirectory()) { if (entry.IsDirectory()) {
_ScanIncludingDisabled(rootPath, list, &entry); status_t result = _ScanIncludingDisabled(rootPath, list, &entry);
} else { if (result != B_OK)
BPath path; return result;
entry.GetPath(&path);
BString *str = new BString(path.Path()); continue;
str->RemoveFirst(rootPath); }
list->AddItem(str);
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; return B_OK;
} }
@@ -111,17 +128,18 @@ void
_BJoystickTweaker::scan_including_disabled() _BJoystickTweaker::scan_including_disabled()
{ {
CALLED(); CALLED();
// First, we empty the list
_EmpyList(fJoystick->fDevices); _EmpyList(fJoystick->fDevices);
_ScanIncludingDisabled(DEVICEPATH, fJoystick->fDevices); _ScanIncludingDisabled(DEVICE_BASE_PATH, fJoystick->fDevices);
} }
void void
_BJoystickTweaker::_EmpyList(BList *list) _BJoystickTweaker::_EmpyList(BList *list)
{ {
for (int32 count = list->CountItems() - 1; count >= 0; count--) for (int32 i = 0; i < list->CountItems(); i++)
free(list->RemoveItem(count)); delete (BString *)list->ItemAt(i);
list->MakeEmpty();
} }
@@ -134,33 +152,32 @@ _BJoystickTweaker::get_info()
status_t status_t
_BJoystickTweaker::GetInfo(_joystick_info* info, _BJoystickTweaker::GetInfo(_joystick_info *info, const char *ref)
const char * ref)
{ {
CALLED(); CALLED();
status_t err = B_ERROR; BString configFilePath(JOYSTICK_CONFIG_BASE_PATH);
BString str(JOYSTICKPATH); configFilePath.Append(ref);
str.Append(ref);
FILE *file = fopen(str.String(), "r"); FILE *file = fopen(configFilePath.String(), "r");
if (file != NULL) { if (file == NULL)
char line [STRINGLENGTHCPY]; return B_ERROR;
while (fgets ( line, sizeof line, file ) != NULL ) {
int len = strlen(line); char line[STACK_STRING_BUFFER_SIZE];
if (len > 0 && line[len-1] == '\n') while (fgets(line, sizeof(line), file) != NULL) {
line[len-1] = '\0'; int length = strlen(line);
_BuildFromJoystickDesc(line, info); if (length > 0 && line[length - 1] == '\n')
} line[length - 1] = '\0';
fclose(file);
_BuildFromJoystickDesc(line, info);
} }
err = B_OK; fclose(file);
return err; return B_OK;
} }
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("\"");
@@ -168,11 +185,11 @@ _BJoystickTweaker::_BuildFromJoystickDesc(char *string, _joystick_info* info)
if (str.IFindFirst("module") != -1) { if (str.IFindFirst("module") != -1) {
str.RemoveFirst("module = "); str.RemoveFirst("module = ");
strlcpy(info->module_info.module_name, str.String(), strlcpy(info->module_info.module_name, str.String(),
STRINGLENGTHCPY); STACK_STRING_BUFFER_SIZE);
} else if (str.IFindFirst("gadget") != -1) { } else if (str.IFindFirst("gadget") != -1) {
str.RemoveFirst("gadget = "); str.RemoveFirst("gadget = ");
strlcpy(info->module_info.device_name, str.String(), strlcpy(info->module_info.device_name, str.String(),
STRINGLENGTHCPY); STACK_STRING_BUFFER_SIZE);
} else if (str.IFindFirst("num_axes") != -1) { } else if (str.IFindFirst("num_axes") != -1) {
str.RemoveFirst("num_axes = "); str.RemoveFirst("num_axes = ");
info->module_info.num_axes = atoi(str.String()); info->module_info.num_axes = atoi(str.String());
@@ -194,7 +211,6 @@ _BJoystickTweaker::_BuildFromJoystickDesc(char *string, _joystick_info* info)
status_t status_t
_BJoystickTweaker::SendIOCT(uint32 op) _BJoystickTweaker::SendIOCT(uint32 op)
{ {
status_t err = B_ERROR;
switch (op) { switch (op) {
case B_JOYSTICK_SET_DEVICE_MODULE: case B_JOYSTICK_SET_DEVICE_MODULE:
break; break;
@@ -210,5 +226,6 @@ _BJoystickTweaker::SendIOCT(uint32 op)
default: default:
break; break;
} }
return err;
return B_ERROR;
} }