xref: /haiku/src/servers/input/MouseSettings.cpp (revision 894526b51a3d931c423878fc0eb8da610fa1fb2a)
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 	status = file.InitCheck();
109 	if (status != B_OK)
110 		return status;
111 
112 	file.Write(&fSettings, sizeof(fSettings));
113 
114 	// who is responsible for saving the mouse mode and accept_first_click?
115 
116 	return B_OK;
117 }
118 
119 
120 #ifdef DEBUG
121 void
122 MouseSettings::Dump()
123 {
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);
133 
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;
145 	}
146 	printf("mouse mode:\t%s\n", mode);
147 
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;
159 	}
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");
163 }
164 #endif
165 
166 
167 /**	Resets the settings to the system defaults
168  */
169 
170 void
171 MouseSettings::Defaults()
172 {
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);
180 
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;
184 
185 }
186 
187 
188 void
189 MouseSettings::SetMouseType(int32 type)
190 {
191 	fSettings.type = type;
192 }
193 
194 
195 bigtime_t
196 MouseSettings::ClickSpeed() const
197 {
198 	return fSettings.click_speed;
199 }
200 
201 
202 void
203 MouseSettings::SetClickSpeed(bigtime_t clickSpeed)
204 {
205 	fSettings.click_speed = clickSpeed;
206 }
207 
208 
209 void
210 MouseSettings::SetMouseSpeed(int32 speed)
211 {
212 	fSettings.accel.speed = speed;
213 }
214 
215 
216 void
217 MouseSettings::SetAccelerationFactor(int32 factor)
218 {
219 	fSettings.accel.accel_factor = factor;
220 }
221 
222 
223 uint32
224 MouseSettings::Mapping(int32 index) const
225 {
226 	return fSettings.map.button[index];
227 }
228 
229 
230 void
231 MouseSettings::Mapping(mouse_map &map) const
232 {
233 	map = fSettings.map;
234 }
235 
236 
237 void
238 MouseSettings::SetMapping(int32 index, uint32 button)
239 {
240 	fSettings.map.button[index] = button;
241 }
242 
243 
244 void
245 MouseSettings::SetMapping(mouse_map &map)
246 {
247 	fSettings.map = map;
248 }
249 
250 
251 void
252 MouseSettings::SetMouseMode(mode_mouse mode)
253 {
254 	fMode = mode;
255 }
256 
257 
258 void
259 MouseSettings::SetFocusFollowsMouseMode(mode_focus_follows_mouse mode)
260 {
261 	fFocusFollowsMouseMode = mode;
262 }
263 
264 
265 void
266 MouseSettings::SetAcceptFirstClick(bool acceptFirstClick)
267 {
268 	fAcceptFirstClick = acceptFirstClick;
269 }
270 
271 
272