xref: /haiku/src/servers/input/MouseSettings.cpp (revision 1214ef1b2100f2b3299fc9d8d6142e46f70a4c3f)
1 // ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
2 //
3 //	Copyright (c) 2004, Haiku
4 //
5 //  This software is part of the Haiku distribution and is covered
6 //  by the Haiku license.
7 //
8 //
9 //  File:			MouseSettings.h
10 //  Authors:		Jérôme Duval,
11 //					Andrew McCall (mccall@digitalparadise.co.uk),
12 //					Axel Dörfler (axeld@pinc-software.de)
13 //  Description:	Input Server
14 //  Created:		August 29, 2004
15 //
16 // ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
17 
18 #include <FindDirectory.h>
19 #include <File.h>
20 #include <Path.h>
21 #include <View.h>
22 
23 #include <stdio.h>
24 
25 #include "MouseSettings.h"
26 
27 static const bigtime_t kDefaultClickSpeed = 500000;
28 static const int32 kDefaultMouseSpeed = 65536;
29 static const int32 kDefaultMouseType = 3;	// 3 button mouse
30 static const int32 kDefaultAccelerationFactor = 65536;
31 
32 
33 MouseSettings::MouseSettings()
34 {
35 	Defaults();
36 	RetrieveSettings();
37 
38 #ifdef DEBUG
39         Dump();
40 #endif
41 
42 	fOriginalSettings = fSettings;
43 	fOriginalMode = fMode;
44 }
45 
46 
47 MouseSettings::~MouseSettings()
48 {
49 	SaveSettings();
50 }
51 
52 
53 status_t
54 MouseSettings::GetSettingsPath(BPath &path)
55 {
56 	status_t status = find_directory(B_USER_SETTINGS_DIRECTORY, &path);
57 	if (status < B_OK)
58 		return status;
59 
60 	path.Append(mouse_settings_file);
61 	return B_OK;
62 }
63 
64 
65 void
66 MouseSettings::RetrieveSettings()
67 {
68 	// retrieve current values
69 
70 	fMode = mouse_mode();
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 #ifdef COMPILE_FOR_R5
84 	// we have to do this because mouse_map counts 16 buttons in OBOS
85 
86 	if ((file.Read(&fSettings.type, sizeof(int32)) != sizeof(int32))
87 		|| (file.Read(&fSettings.map, sizeof(int32) * 3) != sizeof(int32) * 3)
88 		|| (file.Read(&fSettings.accel, sizeof(mouse_accel)) != sizeof(mouse_accel))
89 		|| (file.Read(&fSettings.click_speed, sizeof(bigtime_t)) != sizeof(bigtime_t))) {
90 		Defaults();
91 	}
92 #else
93 	if (file.ReadAt(0, &fSettings, sizeof(mouse_settings)) != sizeof(mouse_settings)) {
94 		Defaults();
95 	}
96 #endif
97 
98 	if ((fSettings.click_speed == 0)
99 		|| (fSettings.type == 0)) {
100 		Defaults();
101 	}
102 }
103 
104 
105 status_t
106 MouseSettings::SaveSettings()
107 {
108 #ifdef DEBUG
109 	Dump();
110 #endif
111 
112 	BPath path;
113 	status_t status = GetSettingsPath(path);
114 	if (status < B_OK)
115 		return status;
116 
117 	BFile file(path.Path(), B_READ_WRITE | B_CREATE_FILE);
118 	if (status < B_OK)
119 		return status;
120 
121 	// we have to do this because mouse_map counts 16 buttons in OBOS
122 #ifdef COMPILE_FOR_R5
123 	file.Write(&fSettings.type, sizeof(fSettings.type));
124 	file.Write(&fSettings.map, 3*sizeof(int32));
125 	file.Write(&fSettings.accel, sizeof(fSettings.accel));
126 	file.Write(&fSettings.click_speed, sizeof(fSettings.click_speed));
127 #else
128 	file.Write(&fSettings, sizeof(fSettings));
129 #endif
130 
131 	// who is responsible for saving the mouse mode?
132 
133 	return B_OK;
134 }
135 
136 
137 #ifdef DEBUG
138 void
139 MouseSettings::Dump()
140 {
141 	printf("type:\t\t%ld button mouse\n", fSettings.type);
142 	printf("map:\t\tleft = %lu : middle = %lu : right = %lu\n", fSettings.map.button[0], fSettings.map.button[2], fSettings.map.button[1]);
143 	printf("click speed:\t%Ld\n", fSettings.click_speed);
144 	printf("accel:\t\t%s\n", fSettings.accel.enabled ? "enabled" : "disabled");
145 	printf("accel factor:\t%ld\n", fSettings.accel.accel_factor);
146 	printf("speed:\t\t%ld\n", fSettings.accel.speed);
147 
148 	char *mode = "unknown";
149 	switch (fMode) {
150 		case B_NORMAL_MOUSE:
151 			mode = "normal";
152 			break;
153 		case B_FOCUS_FOLLOWS_MOUSE:
154 			mode = "focus follows mouse";
155 			break;
156 		case B_WARP_MOUSE:
157 			mode = "warp mouse";
158 			break;
159 		case B_INSTANT_WARP_MOUSE:
160 			mode = "instant warp mouse";
161 			break;
162 	}
163 	printf("mode:\t\t%s\n", mode);
164 }
165 #endif
166 
167 
168 /**	Resets the settings to the system defaults
169  */
170 
171 void
172 MouseSettings::Defaults()
173 {
174 	SetClickSpeed(kDefaultClickSpeed);
175 	SetMouseSpeed(kDefaultMouseSpeed);
176 	SetMouseType(kDefaultMouseType);
177 	SetAccelerationFactor(kDefaultAccelerationFactor);
178 	SetMouseMode(B_NORMAL_MOUSE);
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