xref: /haiku/src/servers/input/MouseSettings.cpp (revision 9760dcae2038d47442f4658c2575844c6cf92c40)
1 /*
2  * Copyright 2004-2009, Haiku, Inc. All rights reserved.
3  * Distributed under the terms of the MIT License.
4  *
5  * Authors:
6  *		Jérôme Duval
7  *		Andrew McCall (mccall@digitalparadise.co.uk)
8  *		Axel Dörfler, axeld@pinc-software.de
9  */
10 
11 
12 #include "MouseSettings.h"
13 
14 #include <stdio.h>
15 
16 #include <FindDirectory.h>
17 #include <File.h>
18 #include <Path.h>
19 #include <View.h>
20 
21 
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;
27 
28 
29 
30 MouseSettings::MouseSettings()
31 {
32 	Defaults();
33 	RetrieveSettings();
34 
35 #ifdef DEBUG
36         Dump();
37 #endif
38 
39 	fOriginalSettings = fSettings;
40 	fOriginalMode = fMode;
41 	fOriginalFocusFollowsMouseMode = fFocusFollowsMouseMode;
42 	fOriginalAcceptFirstClick = fAcceptFirstClick;
43 }
44 
45 
46 MouseSettings::~MouseSettings()
47 {
48 	SaveSettings();
49 }
50 
51 
52 status_t
53 MouseSettings::GetSettingsPath(BPath &path)
54 {
55 	status_t status = find_directory(B_USER_SETTINGS_DIRECTORY, &path);
56 	if (status < B_OK)
57 		return status;
58 
59 	path.Append(mouse_settings_file);
60 	return B_OK;
61 }
62 
63 
64 void
65 MouseSettings::RetrieveSettings()
66 {
67 	// retrieve current values
68 
69 	fMode = mouse_mode();
70 	fAcceptFirstClick = accept_first_click();
71 	Defaults();
72 
73 	// also try to load the window position from disk
74 
75 	BPath path;
76 	if (GetSettingsPath(path) < B_OK)
77 		return;
78 
79 	BFile file(path.Path(), B_READ_ONLY);
80 	if (file.InitCheck() < B_OK)
81 		return;
82 
83 	if (file.ReadAt(0, &fSettings, sizeof(mouse_settings))
84 			!= sizeof(mouse_settings)) {
85 		Defaults();
86 	}
87 
88 	if ((fSettings.click_speed == 0)
89 		|| (fSettings.type == 0)) {
90 		Defaults();
91 	}
92 }
93 
94 
95 status_t
96 MouseSettings::SaveSettings()
97 {
98 #ifdef DEBUG
99 	Dump();
100 #endif
101 
102 	BPath path;
103 	status_t status = GetSettingsPath(path);
104 	if (status < B_OK)
105 		return status;
106 
107 	BFile file(path.Path(), B_READ_WRITE | B_CREATE_FILE);
108 	if (status < B_OK)
109 		return status;
110 
111 	file.Write(&fSettings, sizeof(fSettings));
112 
113 	// who is responsible for saving the mouse mode and accept_first_click?
114 
115 	return B_OK;
116 }
117 
118 
119 #ifdef DEBUG
120 void
121 MouseSettings::Dump()
122 {
123 	printf("type:\t\t%ld button mouse\n", fSettings.type);
124 	printf("map:\t\tleft = %lu : middle = %lu : right = %lu\n",
125 		fSettings.map.button[0], fSettings.map.button[2],
126 		fSettings.map.button[1]);
127 	printf("click speed:\t%Ld\n", fSettings.click_speed);
128 	printf("accel:\t\t%s\n", fSettings.accel.enabled
129 		? "enabled" : "disabled");
130 	printf("accel factor:\t%ld\n", fSettings.accel.accel_factor);
131 	printf("speed:\t\t%ld\n", fSettings.accel.speed);
132 
133 	char *mode = "unknown";
134 	switch (fMode) {
135 		case B_NORMAL_MOUSE:
136 			mode = "activate";
137 			break;
138 		case B_CLICK_TO_FOCUS_MOUSE:
139 			mode = "focus";
140 			break;
141 		case B_FOCUS_FOLLOWS_MOUSE:
142 			mode = "auto-focus";
143 			break;
144 	}
145 	printf("mouse mode:\t%s\n", mode);
146 
147 	char *focus_follows_mouse_mode = "unknown";
148 	switch (fMode) {
149 		case B_NORMAL_FOCUS_FOLLOWS_MOUSE:
150 			focus_follows_mouse_mode = "normal";
151 			break;
152 		case B_WARP_FOCUS_FOLLOWS_MOUSE:
153 			focus_follows_mouse_mode = "warp";
154 			break;
155 		case B_INSTANT_WARP_FOCUS_FOLLOWS_MOUSE:
156 			focus_follows_mouse_mode = "instant warp";
157 			break;
158 	}
159 	printf("focus follows mouse mode:\t%s\n", focus_follows_mouse_mode);
160 	printf("accept first click:\t%s\n", fAcceptFirstClick
161 		? "enabled" : "disabled");
162 }
163 #endif
164 
165 
166 /**	Resets the settings to the system defaults
167  */
168 
169 void
170 MouseSettings::Defaults()
171 {
172 	SetClickSpeed(kDefaultClickSpeed);
173 	SetMouseSpeed(kDefaultMouseSpeed);
174 	SetMouseType(kDefaultMouseType);
175 	SetAccelerationFactor(kDefaultAccelerationFactor);
176 	SetMouseMode(B_NORMAL_MOUSE);
177 	SetFocusFollowsMouseMode(B_NORMAL_FOCUS_FOLLOWS_MOUSE);
178 	SetAcceptFirstClick(kDefaultAcceptFirstClick);
179 
180 	fSettings.map.button[0] = B_PRIMARY_MOUSE_BUTTON;
181 	fSettings.map.button[1] = B_SECONDARY_MOUSE_BUTTON;
182 	fSettings.map.button[2] = B_TERTIARY_MOUSE_BUTTON;
183 
184 }
185 
186 
187 void
188 MouseSettings::SetMouseType(int32 type)
189 {
190 	fSettings.type = type;
191 }
192 
193 
194 bigtime_t
195 MouseSettings::ClickSpeed() const
196 {
197 	return fSettings.click_speed;
198 }
199 
200 
201 void
202 MouseSettings::SetClickSpeed(bigtime_t clickSpeed)
203 {
204 	fSettings.click_speed = clickSpeed;
205 }
206 
207 
208 void
209 MouseSettings::SetMouseSpeed(int32 speed)
210 {
211 	fSettings.accel.speed = speed;
212 }
213 
214 
215 void
216 MouseSettings::SetAccelerationFactor(int32 factor)
217 {
218 	fSettings.accel.accel_factor = factor;
219 }
220 
221 
222 uint32
223 MouseSettings::Mapping(int32 index) const
224 {
225 	return fSettings.map.button[index];
226 }
227 
228 
229 void
230 MouseSettings::Mapping(mouse_map &map) const
231 {
232 	map = fSettings.map;
233 }
234 
235 
236 void
237 MouseSettings::SetMapping(int32 index, uint32 button)
238 {
239 	fSettings.map.button[index] = button;
240 }
241 
242 
243 void
244 MouseSettings::SetMapping(mouse_map &map)
245 {
246 	fSettings.map = map;
247 }
248 
249 
250 void
251 MouseSettings::SetMouseMode(mode_mouse mode)
252 {
253 	fMode = mode;
254 }
255 
256 
257 void
258 MouseSettings::SetFocusFollowsMouseMode(mode_focus_follows_mouse mode)
259 {
260 	fFocusFollowsMouseMode = mode;
261 }
262 
263 
264 void
265 MouseSettings::SetAcceptFirstClick(bool acceptFirstClick)
266 {
267 	fAcceptFirstClick = acceptFirstClick;
268 }
269 
270 
271