From 393ca9a124efa6630c4823156b8cfceaf4780430 Mon Sep 17 00:00:00 2001 From: Michael Lotz Date: Thu, 2 Jun 2011 12:42:18 +0000 Subject: [PATCH] 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 --- headers/private/device/JoystickTweaker.h | 43 +++++---- src/kits/device/Joystick.cpp | 7 +- src/kits/device/JoystickTweaker.cpp | 115 +++++++++++++---------- 3 files changed, 94 insertions(+), 71 deletions(-) diff --git a/headers/private/device/JoystickTweaker.h b/headers/private/device/JoystickTweaker.h index 97a5472671..1051d21614 100644 --- a/headers/private/device/JoystickTweaker.h +++ b/headers/private/device/JoystickTweaker.h @@ -8,15 +8,18 @@ #include -#include #include +#include + #if DEBUG #include #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 }; diff --git a/src/kits/device/Joystick.cpp b/src/kits/device/Joystick.cpp index 59ae4e84aa..f91e0db457 100644 --- a/src/kits/device/Joystick.cpp +++ b/src/kits/device/Joystick.cpp @@ -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) diff --git a/src/kits/device/JoystickTweaker.cpp b/src/kits/device/JoystickTweaker.cpp index 7281fbc72d..6defb15d21 100644 --- a/src/kits/device/JoystickTweaker.cpp +++ b/src/kits/device/JoystickTweaker.cpp @@ -8,20 +8,21 @@ */ #include "JoystickTweaker.h" +#include #include #include -#include -#include -#include #include - -#include "Joystick.h" - -#define STRINGLENGTHCPY 64 - +#include +#include +#include +#include #include + +#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; }