From dae6d05bbd9755092bf65a1cbfdba2f814fc4655 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Axel=20D=C3=B6rfler?= Date: Tue, 6 Jan 2004 21:56:21 +0000 Subject: [PATCH] Cleanup of the mouse settings class: - style guide updates - the settings are now retrieved from the current settings, rather than from the (possibly) corrupt settings file - removed InitCheck() as it doesn't serve any useful purpose (if the settings file is corrupt, the application should not fail anyway; it could just issue a warning), as the settings are now retrieved from the current settings - renamed WindowCorner() to WindowPosition() - added the possibility to read/write a settings file that's incompatible to R5 to store all possible mouse mappings (not activated, though). We should probably better save all mappings at the end of the file for compatibility. git-svn-id: file:///srv/svn/repos/haiku/trunk/current@5942 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- src/prefs/mouse/MouseSettings.cpp | 160 ++++++++++++++++++------------ src/prefs/mouse/MouseSettings.h | 46 +++++---- 2 files changed, 122 insertions(+), 84 deletions(-) diff --git a/src/prefs/mouse/MouseSettings.cpp b/src/prefs/mouse/MouseSettings.cpp index 8163d498a7..55e94b4c5c 100644 --- a/src/prefs/mouse/MouseSettings.cpp +++ b/src/prefs/mouse/MouseSettings.cpp @@ -6,11 +6,13 @@ // by the OpenBeOS license. // // -// File: MouseSettings.h -// Author: Jérôme Duval, Andrew McCall (mccall@digitalparadise.co.uk) -// Description: Media Preferences -// Created : December 10, 2003 -// +// File: MouseSettings.h +// Authors: Jérôme Duval, +// Andrew McCall (mccall@digitalparadise.co.uk), +// Axel Dörfler (axeld@pinc-software.de) +// Description: Mouse Preferences +// Created: December 10, 2003 +// // ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ #include @@ -23,85 +25,105 @@ #include "MouseSettings.h" #include "MouseMessages.h" -const char kMouseSettingsFile[] = "Mouse_settings"; + +// The R5 settings file differs from that of OpenBeOS; +// the latter maps 16 different mouse buttons +#define R5_COMPATIBLE 1 + +static const char kMouseSettingsFile[] = "Mouse_settings"; + MouseSettings::MouseSettings() { - fInitCheck = B_OK; - BPath path; - - if (find_directory(B_USER_SETTINGS_DIRECTORY,&path) == B_OK) { - path.Append(kMouseSettingsFile); - BFile file(path.Path(), B_READ_ONLY); - if (file.InitCheck() == B_OK) { - // Now read in the data - // we have to do this because mouse_map counts 16 buttons in OBOS - if ((file.Read(&fSettings.type, sizeof(fSettings.type)) != sizeof(fSettings.type)) - ||(file.Read(&fSettings.map, (3*sizeof(int32))) != (3*sizeof(int32))) - || (file.Read(&fSettings.accel, sizeof(fSettings.accel)) != sizeof(fSettings.accel)) - || (file.Read(&fSettings.click_speed, sizeof(fSettings.click_speed)) != sizeof(fSettings.click_speed))) { - if (get_mouse_type((int32*)&fSettings.type) != B_OK) - fInitCheck = B_ERROR; - if (get_mouse_map(&fSettings.map) != B_OK) - fInitCheck = B_ERROR; - if (get_mouse_speed(&fSettings.accel.speed) != B_OK) - fInitCheck = B_ERROR; - if (get_click_speed(&fSettings.click_speed) != B_OK) - fInitCheck = B_ERROR; - } - - if (file.Read(&fCorner, sizeof(BPoint)) != sizeof(BPoint)) { - fCorner.x=50; - fCorner.y=50; - } - } else { - if (get_mouse_type((int32*)&fSettings.type) != B_OK) - fInitCheck = B_ERROR; - if (get_mouse_map(&fSettings.map) != B_OK) - fInitCheck = B_ERROR; - if (get_mouse_speed(&fSettings.accel.speed) != B_OK) - fInitCheck = B_ERROR; - if (get_click_speed(&fSettings.click_speed) != B_OK) - fInitCheck = B_ERROR; - fCorner.x=50; - fCorner.y=50; - } - } else - fInitCheck = B_ERROR; + RetrieveSettings(); } -MouseSettings::~MouseSettings() -{ - BPath path; - if (find_directory(B_USER_SETTINGS_DIRECTORY,&path) < B_OK) - return; +MouseSettings::~MouseSettings() +{ + SaveSettings(); +} + + +status_t +MouseSettings::GetSettingsPath(BPath &path) +{ + status_t status = find_directory(B_USER_SETTINGS_DIRECTORY, &path); + if (status < B_OK) + return status; path.Append(kMouseSettingsFile); + return B_OK; +} - BFile file(path.Path(), B_READ_WRITE | B_CREATE_FILE | B_ERASE_FILE); - if (file.InitCheck() == B_OK) { + +void +MouseSettings::RetrieveSettings() +{ + // retrieve current values + + get_mouse_map(&fSettings.map); + get_click_speed(&fSettings.click_speed); + get_mouse_acceleration(&fSettings.accel.accel_factor); + get_mouse_type(&fSettings.type); + + // also try to load the window position from disk + + BPath path; + if (GetSettingsPath(path) < B_OK) + return; + + BFile file(path.Path(), B_READ_ONLY); + if (file.InitCheck() < B_OK) + return; + +#if R5_COMPATIBLE + const off_t kOffset = sizeof(mouse_settings) - sizeof(mouse_map) + sizeof(int32) * 3; // we have to do this because mouse_map counts 16 buttons in OBOS - file.Write(&fSettings.type, sizeof(fSettings.type)); - file.Write(&fSettings.map, 3*sizeof(int32)); - file.Write(&fSettings.accel, sizeof(fSettings.accel)); - file.Write(&fSettings.click_speed, sizeof(fSettings.click_speed)); - file.Write(&fCorner, sizeof(BPoint)); +#else + const off_t kOffset = sizeof(mouse_settings); +#endif + + if (file.ReadAt(kOffset, &fWindowPosition, sizeof(BPoint)) != sizeof(BPoint)) { + // set default window position (invalid positions will be + // corrected by the application; the window will be centered + // in this case) + fWindowPosition.x = -1; + fWindowPosition.y = -1; } } -status_t -MouseSettings::InitCheck() +status_t +MouseSettings::SaveSettings() { - return fInitCheck; + BPath path; + status_t status = GetSettingsPath(path); + if (status < B_OK) + return status; + + BFile file(path.Path(), B_READ_WRITE | B_CREATE_FILE | B_ERASE_FILE); + if (status < B_OK) + return status; + + // we have to do this because mouse_map counts 16 buttons in OBOS +#if R5_COMPATIBLE + file.Write(&fSettings.type, sizeof(fSettings.type)); + file.Write(&fSettings.map, 3*sizeof(int32)); + file.Write(&fSettings.accel, sizeof(fSettings.accel)); + file.Write(&fSettings.click_speed, sizeof(fSettings.click_speed)); +#else + file.Write(fSettings, sizeof(fSettings)); +#endif + + file.Write(&fWindowPosition, sizeof(BPoint)); } void -MouseSettings::SetWindowCorner(BPoint corner) +MouseSettings::SetWindowPosition(BPoint corner) { - fCorner = corner; + fWindowPosition = corner; } @@ -112,6 +134,14 @@ MouseSettings::SetMouseType(int32 type) } +bigtime_t +MouseSettings::ClickSpeed() const +{ + return -(fSettings.click_speed - 1000000); + // -1000000 to correct the Sliders 0-100000 scale +} + + void MouseSettings::SetClickSpeed(bigtime_t click_speed) { @@ -122,5 +152,5 @@ MouseSettings::SetClickSpeed(bigtime_t click_speed) void MouseSettings::SetMouseSpeed(int32 accel) { - fSettings.accel.speed=accel; + fSettings.accel.speed = accel; } diff --git a/src/prefs/mouse/MouseSettings.h b/src/prefs/mouse/MouseSettings.h index 1276c42598..48e3241468 100644 --- a/src/prefs/mouse/MouseSettings.h +++ b/src/prefs/mouse/MouseSettings.h @@ -32,25 +32,33 @@ typedef struct { bigtime_t click_speed; } mouse_settings; -class MouseSettings{ -public : - MouseSettings(); - ~MouseSettings(); - status_t InitCheck(); - - BPoint WindowCorner() const { return fCorner; } - void SetWindowCorner(BPoint corner); - int32 MouseType() const { return fSettings.type; } - void SetMouseType(int32 type); - bigtime_t ClickSpeed() const { return -(fSettings.click_speed-1000000); } // -1000000 to correct the Sliders 0-100000 scale - void SetClickSpeed(bigtime_t click_speed); - int32 MouseSpeed() const { return fSettings.accel.speed; } - void SetMouseSpeed(int32 speed); - -private: - status_t fInitCheck; - BPoint fCorner; - mouse_settings fSettings; + +class MouseSettings { + public: + MouseSettings(); + ~MouseSettings(); + + status_t InitCheck(); + + BPoint WindowPosition() const { return fWindowPosition; } + void SetWindowPosition(BPoint corner); + + int32 MouseType() const { return fSettings.type; } + void SetMouseType(int32 type); + + bigtime_t ClickSpeed() const; + void SetClickSpeed(bigtime_t click_speed); + + int32 MouseSpeed() const { return fSettings.accel.speed; } + void SetMouseSpeed(int32 speed); + + private: + static status_t GetSettingsPath(BPath &path); + void RetrieveSettings(); + status_t SaveSettings(); + + mouse_settings fSettings, fOriginalSettings; + BPoint fWindowPosition; }; #endif