fat: Greatly simplify and clean up dosfs_get_file_map().
[haiku.git] / src / preferences / touchpad / TouchpadPref.cpp
blobeaca1746c218ad1b94094af6f0667a9cf7823818
1 /*
2 * Copyright 2008-2010, Haiku, Inc.
3 * Distributed under the terms of the MIT License.
5 * Authors:
6 * Clemens Zeidler (haiku@Clemens-Zeidler.de)
7 */
10 #include "TouchpadPref.h"
12 #include <List.h>
13 #include <FindDirectory.h>
14 #include <File.h>
15 #include <String.h>
17 #include <keyboard_mouse_driver.h>
20 TouchpadPref::TouchpadPref()
22 fConnected = false;
23 // default center position
24 fWindowPosition.x = -1;
25 fWindowPosition.y = -1;
27 ConnectToTouchPad();
29 if (LoadSettings() != B_OK)
30 Defaults();
32 fStartSettings = fSettings;
36 TouchpadPref::~TouchpadPref()
38 if (fConnected)
39 delete fTouchPad;
41 SaveSettings();
45 void
46 TouchpadPref::Revert()
48 fSettings = fStartSettings;
52 status_t
53 TouchpadPref::UpdateSettings()
55 if (!fConnected)
56 return B_ERROR;
58 LOG("UpdateSettings of device %s\n", fTouchPad->Name());
60 BMessage msg;
61 msg.AddBool("scroll_twofinger", fSettings.scroll_twofinger);
62 msg.AddBool("scroll_twofinger_horizontal",
63 fSettings.scroll_twofinger_horizontal);
64 msg.AddFloat("scroll_rightrange", fSettings.scroll_rightrange);
65 msg.AddFloat("scroll_bottomrange", fSettings.scroll_bottomrange);
66 msg.AddInt16("scroll_xstepsize", fSettings.scroll_xstepsize);
67 msg.AddInt16("scroll_ystepsize", fSettings.scroll_ystepsize);
68 msg.AddInt8("scroll_acceleration", fSettings.scroll_acceleration);
69 msg.AddInt8("tapgesture_sensibility", fSettings.tapgesture_sensibility);
71 return fTouchPad->Control(MS_SET_TOUCHPAD_SETTINGS, &msg);
75 void
76 TouchpadPref::Defaults()
78 fSettings = kDefaultTouchpadSettings;
82 status_t
83 TouchpadPref::GetSettingsPath(BPath &path)
85 status_t status = find_directory(B_USER_SETTINGS_DIRECTORY, &path);
86 if (status < B_OK)
87 return status;
89 return path.Append(TOUCHPAD_SETTINGS_FILE);
93 status_t
94 TouchpadPref::LoadSettings()
96 BPath path;
97 status_t status = GetSettingsPath(path);
98 if (status != B_OK)
99 return status;
101 BFile settingsFile(path.Path(), B_READ_ONLY);
102 status = settingsFile.InitCheck();
103 if (status != B_OK)
104 return status;
106 if (settingsFile.Read(&fSettings, sizeof(touchpad_settings))
107 != sizeof(touchpad_settings)) {
108 LOG("failed to load settings\n");
109 return B_ERROR;
112 if (settingsFile.Read(&fWindowPosition, sizeof(BPoint))
113 != sizeof(BPoint)) {
114 LOG("failed to load settings\n");
115 return B_ERROR;
118 return B_OK;
122 status_t
123 TouchpadPref::SaveSettings()
125 BPath path;
126 status_t status = GetSettingsPath(path);
127 if (status != B_OK)
128 return status;
130 BFile settingsFile(path.Path(), B_READ_WRITE | B_CREATE_FILE);
131 status = settingsFile.InitCheck();
132 if (status != B_OK)
133 return status;
135 if (settingsFile.Write(&fSettings, sizeof(touchpad_settings))
136 != sizeof(touchpad_settings)) {
137 LOG("can't save settings\n");
138 return B_ERROR;
141 if (settingsFile.Write(&fWindowPosition, sizeof(BPoint))
142 != sizeof(BPoint)) {
143 LOG("can't save window position\n");
144 return B_ERROR;
147 return B_OK;
151 status_t
152 TouchpadPref::ConnectToTouchPad()
154 BList devList;
155 status_t status = get_input_devices(&devList);
156 if (status != B_OK)
157 return status;
159 int32 i = 0;
160 while (true) {
161 BInputDevice* dev = (BInputDevice*)devList.ItemAt(i);
162 if (dev == NULL)
163 break;
164 i++;
166 LOG("input device %s\n", dev->Name());
168 BString name = dev->Name();
170 if (name.FindFirst("Touchpad") >= 0
171 && dev->Type() == B_POINTING_DEVICE
172 && !fConnected) {
173 fConnected = true;
174 fTouchPad = dev;
175 // Don't bail out here, since we need to delete the other devices
176 // yet.
177 } else {
178 delete dev;
181 if (fConnected)
182 return B_OK;
184 LOG("touchpad input device NOT found\n");
185 return B_ENTRY_NOT_FOUND;