xref: /haiku/src/servers/input/MouseSettings.cpp (revision caed67a8cba83913b9c21ac2b06ebc6bd1cb3111)
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 MouseSettings::MouseSettings()
23 {
24 	Defaults();
25 
26 #ifdef DEBUG
27 	Dump();
28 #endif
29 }
30 
31 
32 MouseSettings::MouseSettings(const mouse_settings* originalSettings)
33 {
34 	Defaults();
35 
36 	if (originalSettings != NULL) {
37 		fMode = mouse_mode();
38 		fFocusFollowsMouseMode = focus_follows_mouse_mode();
39 		fAcceptFirstClick = accept_first_click();
40 
41 		fSettings = *originalSettings;
42 	}
43 
44 #ifdef DEBUG
45 	Dump();
46 #endif
47 }
48 
49 
50 MouseSettings::~MouseSettings()
51 {
52 }
53 
54 
55 #ifdef DEBUG
56 void
57 MouseSettings::Dump()
58 {
59 	printf("type:\t\t%" B_PRId32 " button mouse\n", fSettings.type);
60 	printf("map:\t\tleft = %" B_PRIu32 " : middle = %" B_PRIu32 " : "
61 		"right = %" B_PRIu32 "\n",
62 		fSettings.map.button[0], fSettings.map.button[2],
63 		fSettings.map.button[1]);
64 	printf("click speed:\t%" B_PRId64 "\n", fSettings.click_speed);
65 	printf("accel:\t\t%s\n", fSettings.accel.enabled
66 		? "enabled" : "disabled");
67 	printf("accel factor:\t%" B_PRId32 "\n", fSettings.accel.accel_factor);
68 	printf("speed:\t\t%" B_PRId32 "\n", fSettings.accel.speed);
69 
70 	const char *mode = "unknown";
71 	switch (fMode) {
72 		case B_NORMAL_MOUSE:
73 			mode = "activate";
74 			break;
75 		case B_CLICK_TO_FOCUS_MOUSE:
76 			mode = "focus";
77 			break;
78 		case B_FOCUS_FOLLOWS_MOUSE:
79 			mode = "auto-focus";
80 			break;
81 	}
82 	printf("mouse mode:\t%s\n", mode);
83 
84 	const char *focus_follows_mouse_mode = "unknown";
85 	switch (fFocusFollowsMouseMode) {
86 		case B_NORMAL_FOCUS_FOLLOWS_MOUSE:
87 			focus_follows_mouse_mode = "normal";
88 			break;
89 		case B_WARP_FOCUS_FOLLOWS_MOUSE:
90 			focus_follows_mouse_mode = "warp";
91 			break;
92 		case B_INSTANT_WARP_FOCUS_FOLLOWS_MOUSE:
93 			focus_follows_mouse_mode = "instant warp";
94 			break;
95 	}
96 	printf("focus follows mouse mode:\t%s\n", focus_follows_mouse_mode);
97 	printf("accept first click:\t%s\n", fAcceptFirstClick
98 		? "enabled" : "disabled");
99 }
100 #endif
101 
102 
103 /**	Resets the settings to the system defaults
104  */
105 
106 void
107 MouseSettings::Defaults()
108 {
109 	SetClickSpeed(kDefaultClickSpeed);
110 	SetMouseSpeed(kDefaultMouseSpeed);
111 	SetMouseType(kDefaultMouseType);
112 	SetAccelerationFactor(kDefaultAccelerationFactor);
113 	SetMouseMode(B_NORMAL_MOUSE);
114 	SetFocusFollowsMouseMode(B_NORMAL_FOCUS_FOLLOWS_MOUSE);
115 	SetAcceptFirstClick(kDefaultAcceptFirstClick);
116 
117 	for (int i = 0; i < B_MAX_MOUSE_BUTTONS; i++)
118 		fSettings.map.button[i] = B_MOUSE_BUTTON(i + 1);
119 }
120 
121 
122 void
123 MouseSettings::SetMouseType(int32 type)
124 {
125 	fSettings.type = type;
126 }
127 
128 
129 bigtime_t
130 MouseSettings::ClickSpeed() const
131 {
132 	return fSettings.click_speed;
133 }
134 
135 
136 void
137 MouseSettings::SetClickSpeed(bigtime_t clickSpeed)
138 {
139 	fSettings.click_speed = clickSpeed;
140 }
141 
142 
143 void
144 MouseSettings::SetMouseSpeed(int32 speed)
145 {
146 	fSettings.accel.speed = speed;
147 }
148 
149 
150 void
151 MouseSettings::SetAccelerationFactor(int32 factor)
152 {
153 	fSettings.accel.accel_factor = factor;
154 }
155 
156 
157 uint32
158 MouseSettings::Mapping(int32 index) const
159 {
160 	return fSettings.map.button[index];
161 }
162 
163 
164 void
165 MouseSettings::Mapping(mouse_map &map) const
166 {
167 	map = fSettings.map;
168 }
169 
170 
171 void
172 MouseSettings::SetMapping(int32 index, uint32 button)
173 {
174 	fSettings.map.button[index] = button;
175 }
176 
177 
178 void
179 MouseSettings::SetMapping(mouse_map &map)
180 {
181 	fSettings.map = map;
182 }
183 
184 
185 void
186 MouseSettings::SetMouseMode(mode_mouse mode)
187 {
188 	fMode = mode;
189 }
190 
191 
192 void
193 MouseSettings::SetFocusFollowsMouseMode(mode_focus_follows_mouse mode)
194 {
195 	fFocusFollowsMouseMode = mode;
196 }
197 
198 
199 void
200 MouseSettings::SetAcceptFirstClick(bool acceptFirstClick)
201 {
202 	fAcceptFirstClick = acceptFirstClick;
203 }
204 
205 
206 /* MultiMouseSettings functions */
207 
208 MultipleMouseSettings::MultipleMouseSettings()
209 {
210 	RetrieveSettings();
211 
212 #ifdef DEBUG
213 	Dump();
214 #endif
215 }
216 
217 
218 MultipleMouseSettings::~MultipleMouseSettings()
219 {
220 	SaveSettings();
221 
222 #ifdef DEBUG
223 	Dump();
224 #endif
225 
226 	std::map<BString, MouseSettings*>::iterator itr;
227 	for (itr = fMouseSettingsObject.begin(); itr != fMouseSettingsObject.end(); ++itr)
228 		delete itr->second;
229 }
230 
231 
232 status_t
233 MultipleMouseSettings::GetSettingsPath(BPath &path)
234 {
235 	status_t status = find_directory(B_USER_SETTINGS_DIRECTORY, &path);
236 	if (status < B_OK)
237 		return status;
238 
239 	path.Append(mouse_settings_file);
240 	return B_OK;
241 }
242 
243 
244 void
245 MultipleMouseSettings::RetrieveSettings()
246 {
247 	BPath path;
248 	if (GetSettingsPath(path) < B_OK)
249 		return;
250 
251 	BFile file(path.Path(), B_READ_ONLY);
252 	if (file.InitCheck() < B_OK)
253 		return;
254 
255 	BMessage message;
256 
257 	if (message.Unflatten(&file) == B_OK) {
258 		int i = 0;
259 		BString deviceName;
260 		mouse_settings* settings;
261 		ssize_t size = 0;
262 
263 		while (message.FindString("mouseDevice", i, &deviceName) == B_OK) {
264 			message.FindData("mouseSettings", B_ANY_TYPE, i,
265 				(const void**)&settings, &size);
266 			MouseSettings* mouseSettings = new MouseSettings(settings);
267 			fMouseSettingsObject.insert(std::pair<BString, MouseSettings*>
268 				(deviceName, mouseSettings));
269 			i++;
270 		}
271 	}
272 }
273 
274 
275 status_t
276 MultipleMouseSettings::Archive(BMessage* into, bool deep) const
277 {
278 	std::map<BString, MouseSettings*>::const_iterator itr;
279 	for (itr = fMouseSettingsObject.begin(); itr != fMouseSettingsObject.end();
280 		++itr) {
281 		into->AddString("mouseDevice", itr->first);
282 		into->AddData("mouseSettings", B_ANY_TYPE, itr->second->GetSettings(),
283 			sizeof(*(itr->second->GetSettings())));
284 	}
285 
286 	return B_OK;
287 }
288 
289 
290 status_t
291 MultipleMouseSettings::SaveSettings()
292 {
293 	BPath path;
294 	status_t status = GetSettingsPath(path);
295 	if (status < B_OK)
296 		return status;
297 
298 	BFile file(path.Path(), B_WRITE_ONLY | B_CREATE_FILE | B_ERASE_FILE);
299 	status = file.InitCheck();
300 	if (status != B_OK)
301 		return status;
302 
303 	BMessage message;
304 	Archive(&message, true);
305 	message.Flatten(&file);
306 
307 	return B_OK;
308 }
309 
310 
311 void
312 MultipleMouseSettings::Defaults()
313 {
314 	std::map<BString, MouseSettings*>::iterator itr;
315 	for (itr = fMouseSettingsObject.begin(); itr != fMouseSettingsObject.end();
316 		++itr) {
317 		itr->second->Defaults();
318 	}
319 }
320 
321 
322 #ifdef DEBUG
323 void
324 MultipleMouseSettings::Dump()
325 {
326 	std::map<BString, MouseSettings*>::iterator itr;
327 	for (itr = fMouseSettingsObject.begin();
328 		itr != fMouseSettingsObject.end(); ++itr) {
329 		printf("mouse_name:\t%s\n", itr->first.String());
330 		itr->second->Dump();
331 		printf("\n");
332 	}
333 
334 }
335 #endif
336 
337 
338 MouseSettings*
339 MultipleMouseSettings::AddMouseSettings(BString mouse_name)
340 {
341 	MouseSettings* settings = GetMouseSettings(mouse_name);
342 	if (settings != NULL)
343 		return settings;
344 
345 	settings = new(std::nothrow) MouseSettings();
346 
347 	if(settings != NULL) {
348 		fMouseSettingsObject.insert(std::pair<BString, MouseSettings*>
349 			(mouse_name, settings));
350 		return settings;
351 	}
352 	return NULL;
353 }
354 
355 
356 MouseSettings*
357 MultipleMouseSettings::GetMouseSettings(BString mouse_name)
358 {
359 	std::map<BString, MouseSettings*>::iterator itr;
360 	itr = fMouseSettingsObject.find(mouse_name);
361 
362 	if (itr != fMouseSettingsObject.end())
363 		return itr->second;
364 	return NULL;
365  }
366 
367