vfs: check userland buffers before reading them.
[haiku.git] / src / servers / input / MouseSettings.cpp
blobdca38b7f594945ceedb349350162b9fc8ea27a65
1 /*
2 * Copyright 2004-2009, Haiku, Inc. All rights reserved.
3 * Distributed under the terms of the MIT License.
5 * Authors:
6 * Jérôme Duval
7 * Andrew McCall (mccall@digitalparadise.co.uk)
8 * Axel Dörfler, axeld@pinc-software.de
9 */
12 #include "MouseSettings.h"
14 #include <stdio.h>
16 #include <FindDirectory.h>
17 #include <File.h>
18 #include <Path.h>
19 #include <View.h>
22 static const bigtime_t kDefaultClickSpeed = 500000;
23 static const int32 kDefaultMouseSpeed = 65536;
24 static const int32 kDefaultMouseType = 3; // 3 button mouse
25 static const int32 kDefaultAccelerationFactor = 65536;
26 static const bool kDefaultAcceptFirstClick = false;
30 MouseSettings::MouseSettings()
32 Defaults();
33 RetrieveSettings();
35 #ifdef DEBUG
36 Dump();
37 #endif
39 fOriginalSettings = fSettings;
40 fOriginalMode = fMode;
41 fOriginalFocusFollowsMouseMode = fFocusFollowsMouseMode;
42 fOriginalAcceptFirstClick = fAcceptFirstClick;
46 MouseSettings::~MouseSettings()
48 SaveSettings();
52 status_t
53 MouseSettings::GetSettingsPath(BPath &path)
55 status_t status = find_directory(B_USER_SETTINGS_DIRECTORY, &path);
56 if (status < B_OK)
57 return status;
59 path.Append(mouse_settings_file);
60 return B_OK;
64 void
65 MouseSettings::RetrieveSettings()
67 // retrieve current values
69 fMode = mouse_mode();
70 fAcceptFirstClick = accept_first_click();
71 Defaults();
73 // also try to load the window position from disk
75 BPath path;
76 if (GetSettingsPath(path) < B_OK)
77 return;
79 BFile file(path.Path(), B_READ_ONLY);
80 if (file.InitCheck() < B_OK)
81 return;
83 if (file.ReadAt(0, &fSettings, sizeof(mouse_settings))
84 != sizeof(mouse_settings)) {
85 Defaults();
88 if ((fSettings.click_speed == 0)
89 || (fSettings.type == 0)) {
90 Defaults();
95 status_t
96 MouseSettings::SaveSettings()
98 #ifdef DEBUG
99 Dump();
100 #endif
102 BPath path;
103 status_t status = GetSettingsPath(path);
104 if (status < B_OK)
105 return status;
107 BFile file(path.Path(), B_READ_WRITE | B_CREATE_FILE);
108 status = file.InitCheck();
109 if (status != B_OK)
110 return status;
112 file.Write(&fSettings, sizeof(fSettings));
114 // who is responsible for saving the mouse mode and accept_first_click?
116 return B_OK;
120 #ifdef DEBUG
121 void
122 MouseSettings::Dump()
124 printf("type:\t\t%ld button mouse\n", fSettings.type);
125 printf("map:\t\tleft = %lu : middle = %lu : right = %lu\n",
126 fSettings.map.button[0], fSettings.map.button[2],
127 fSettings.map.button[1]);
128 printf("click speed:\t%Ld\n", fSettings.click_speed);
129 printf("accel:\t\t%s\n", fSettings.accel.enabled
130 ? "enabled" : "disabled");
131 printf("accel factor:\t%ld\n", fSettings.accel.accel_factor);
132 printf("speed:\t\t%ld\n", fSettings.accel.speed);
134 char *mode = "unknown";
135 switch (fMode) {
136 case B_NORMAL_MOUSE:
137 mode = "activate";
138 break;
139 case B_CLICK_TO_FOCUS_MOUSE:
140 mode = "focus";
141 break;
142 case B_FOCUS_FOLLOWS_MOUSE:
143 mode = "auto-focus";
144 break;
146 printf("mouse mode:\t%s\n", mode);
148 char *focus_follows_mouse_mode = "unknown";
149 switch (fFocusFollowsMouseMode) {
150 case B_NORMAL_FOCUS_FOLLOWS_MOUSE:
151 focus_follows_mouse_mode = "normal";
152 break;
153 case B_WARP_FOCUS_FOLLOWS_MOUSE:
154 focus_follows_mouse_mode = "warp";
155 break;
156 case B_INSTANT_WARP_FOCUS_FOLLOWS_MOUSE:
157 focus_follows_mouse_mode = "instant warp";
158 break;
160 printf("focus follows mouse mode:\t%s\n", focus_follows_mouse_mode);
161 printf("accept first click:\t%s\n", fAcceptFirstClick
162 ? "enabled" : "disabled");
164 #endif
167 /** Resets the settings to the system defaults
170 void
171 MouseSettings::Defaults()
173 SetClickSpeed(kDefaultClickSpeed);
174 SetMouseSpeed(kDefaultMouseSpeed);
175 SetMouseType(kDefaultMouseType);
176 SetAccelerationFactor(kDefaultAccelerationFactor);
177 SetMouseMode(B_NORMAL_MOUSE);
178 SetFocusFollowsMouseMode(B_NORMAL_FOCUS_FOLLOWS_MOUSE);
179 SetAcceptFirstClick(kDefaultAcceptFirstClick);
181 fSettings.map.button[0] = B_PRIMARY_MOUSE_BUTTON;
182 fSettings.map.button[1] = B_SECONDARY_MOUSE_BUTTON;
183 fSettings.map.button[2] = B_TERTIARY_MOUSE_BUTTON;
188 void
189 MouseSettings::SetMouseType(int32 type)
191 fSettings.type = type;
195 bigtime_t
196 MouseSettings::ClickSpeed() const
198 return fSettings.click_speed;
202 void
203 MouseSettings::SetClickSpeed(bigtime_t clickSpeed)
205 fSettings.click_speed = clickSpeed;
209 void
210 MouseSettings::SetMouseSpeed(int32 speed)
212 fSettings.accel.speed = speed;
216 void
217 MouseSettings::SetAccelerationFactor(int32 factor)
219 fSettings.accel.accel_factor = factor;
223 uint32
224 MouseSettings::Mapping(int32 index) const
226 return fSettings.map.button[index];
230 void
231 MouseSettings::Mapping(mouse_map &map) const
233 map = fSettings.map;
237 void
238 MouseSettings::SetMapping(int32 index, uint32 button)
240 fSettings.map.button[index] = button;
244 void
245 MouseSettings::SetMapping(mouse_map &map)
247 fSettings.map = map;
251 void
252 MouseSettings::SetMouseMode(mode_mouse mode)
254 fMode = mode;
258 void
259 MouseSettings::SetFocusFollowsMouseMode(mode_focus_follows_mouse mode)
261 fFocusFollowsMouseMode = mode;
265 void
266 MouseSettings::SetAcceptFirstClick(bool acceptFirstClick)
268 fAcceptFirstClick = acceptFirstClick;