2 * Copyright 2003-2009 Haiku Inc. All rights reserved.
3 * Distributed under the terms of the MIT License.
7 * Axel Dörfler (axeld@pinc-software.de)
8 * Andrew McCall (mccall@digitalparadise.co.uk)
9 * Brecht Machiels (brecht@mos6581.org)
12 #include <FindDirectory.h>
19 #include "MouseSettings.h"
21 // The R5 settings file differs from that of OpenBeOS;
22 // the latter maps 16 different mouse buttons
23 #define R5_COMPATIBLE 1
25 static const bigtime_t kDefaultClickSpeed
= 500000;
26 static const int32 kDefaultMouseSpeed
= 65536;
27 static const int32 kDefaultMouseType
= 3; // 3 button mouse
28 static const int32 kDefaultAccelerationFactor
= 65536;
29 static const bool kDefaultAcceptFirstClick
= false;
32 MouseSettings::MouseSettings()
34 fWindowPosition(-1, -1)
38 fOriginalSettings
= fSettings
;
39 fOriginalMode
= fMode
;
40 fOriginalFocusFollowsMouseMode
= fFocusFollowsMouseMode
;
41 fOriginalAcceptFirstClick
= fAcceptFirstClick
;
45 MouseSettings::~MouseSettings()
52 MouseSettings::_GetSettingsPath(BPath
&path
)
54 status_t status
= find_directory(B_USER_SETTINGS_DIRECTORY
, &path
);
58 path
.Append(mouse_settings_file
);
64 MouseSettings::_RetrieveSettings()
66 // retrieve current values
68 if (get_mouse_map(&fSettings
.map
) != B_OK
)
69 fprintf(stderr
, "error when get_mouse_map\n");
70 if (get_click_speed(&fSettings
.click_speed
) != B_OK
)
71 fprintf(stderr
, "error when get_click_speed\n");
72 if (get_mouse_speed(&fSettings
.accel
.speed
) != B_OK
)
73 fprintf(stderr
, "error when get_mouse_speed\n");
74 if (get_mouse_acceleration(&fSettings
.accel
.accel_factor
) != B_OK
)
75 fprintf(stderr
, "error when get_mouse_acceleration\n");
76 if (get_mouse_type(&fSettings
.type
) != B_OK
)
77 fprintf(stderr
, "error when get_mouse_type\n");
80 fFocusFollowsMouseMode
= focus_follows_mouse_mode();
81 fAcceptFirstClick
= accept_first_click();
83 // also try to load the window position from disk
86 if (_GetSettingsPath(path
) < B_OK
)
89 BFile
file(path
.Path(), B_READ_ONLY
);
90 if (file
.InitCheck() < B_OK
)
94 const off_t kOffset
= sizeof(mouse_settings
) - sizeof(mouse_map
)
96 // we have to do this because mouse_map counts 16 buttons in OBOS
98 const off_t kOffset
= sizeof(mouse_settings
);
101 if (file
.ReadAt(kOffset
, &fWindowPosition
, sizeof(BPoint
))
103 // set default window position (invalid positions will be
104 // corrected by the application; the window will be centered
106 fWindowPosition
.x
= -1;
107 fWindowPosition
.y
= -1;
117 MouseSettings::_SaveSettings()
120 status_t status
= _GetSettingsPath(path
);
124 BFile
file(path
.Path(), B_READ_WRITE
| B_CREATE_FILE
);
125 status
= file
.InitCheck();
130 const off_t kOffset
= sizeof(mouse_settings
) - sizeof(mouse_map
)
132 // we have to do this because mouse_map counts 16 buttons in OBOS
134 const off_t kOffset
= sizeof(mouse_settings
);
137 file
.WriteAt(kOffset
, &fWindowPosition
, sizeof(BPoint
));
145 MouseSettings::Dump()
147 printf("type:\t\t%" B_PRId32
" button mouse\n", fSettings
.type
);
148 printf("map:\t\tleft = %" B_PRIu32
" : middle = %" B_PRIu32
" : right = %"
149 B_PRIu32
"\n", fSettings
.map
.button
[0], fSettings
.map
.button
[2],
150 fSettings
.map
.button
[1]);
151 printf("click speed:\t%" B_PRId64
"\n", fSettings
.click_speed
);
152 printf("accel:\t\t%s\n", fSettings
.accel
.enabled
? "enabled" : "disabled");
153 printf("accel factor:\t%" B_PRId32
"\n", fSettings
.accel
.accel_factor
);
154 printf("speed:\t\t%" B_PRId32
"\n", fSettings
.accel
.speed
);
156 const char *mode
= "unknown";
159 mode
= "click to focus and raise";
161 case B_CLICK_TO_FOCUS_MOUSE
:
162 mode
= "click to focus";
164 case B_FOCUS_FOLLOWS_MOUSE
:
165 mode
= "focus follows mouse";
168 printf("mouse mode:\t%s\n", mode
);
170 const char *focus_follows_mouse_mode
= "unknown";
171 switch (fFocusFollowsMouseMode
) {
172 case B_NORMAL_FOCUS_FOLLOWS_MOUSE
:
173 focus_follows_mouse_mode
= "normal";
175 case B_WARP_FOCUS_FOLLOWS_MOUSE
:
176 focus_follows_mouse_mode
= "warp";
178 case B_INSTANT_WARP_FOCUS_FOLLOWS_MOUSE
:
179 focus_follows_mouse_mode
= "instant warp";
182 printf("focus follows mouse mode:\t%s\n", focus_follows_mouse_mode
);
183 printf("accept first click:\t%s\n",
184 fAcceptFirstClick
? "enabled" : "disabled");
189 // Resets the settings to the system defaults
191 MouseSettings::Defaults()
193 SetClickSpeed(kDefaultClickSpeed
);
194 SetMouseSpeed(kDefaultMouseSpeed
);
195 SetMouseType(kDefaultMouseType
);
196 SetAccelerationFactor(kDefaultAccelerationFactor
);
197 SetMouseMode(B_NORMAL_MOUSE
);
198 SetFocusFollowsMouseMode(B_NORMAL_FOCUS_FOLLOWS_MOUSE
);
199 SetAcceptFirstClick(kDefaultAcceptFirstClick
);
202 if (get_mouse_map(&map
) == B_OK
) {
203 map
.button
[0] = B_PRIMARY_MOUSE_BUTTON
;
204 map
.button
[1] = B_SECONDARY_MOUSE_BUTTON
;
205 map
.button
[2] = B_TERTIARY_MOUSE_BUTTON
;
211 // Checks if the settings are different then the system defaults
213 MouseSettings::IsDefaultable()
215 return fSettings
.click_speed
!= kDefaultClickSpeed
216 || fSettings
.accel
.speed
!= kDefaultMouseSpeed
217 || fSettings
.type
!= kDefaultMouseType
218 || fSettings
.accel
.accel_factor
!= kDefaultAccelerationFactor
219 || fMode
!= B_NORMAL_MOUSE
220 || fFocusFollowsMouseMode
!= B_NORMAL_FOCUS_FOLLOWS_MOUSE
221 || fAcceptFirstClick
!= kDefaultAcceptFirstClick
222 || fSettings
.map
.button
[0] != B_PRIMARY_MOUSE_BUTTON
223 || fSettings
.map
.button
[1] != B_SECONDARY_MOUSE_BUTTON
224 || fSettings
.map
.button
[2] != B_TERTIARY_MOUSE_BUTTON
;
228 // Reverts to the active settings at program startup
230 MouseSettings::Revert()
232 SetClickSpeed(fOriginalSettings
.click_speed
);
233 SetMouseSpeed(fOriginalSettings
.accel
.speed
);
234 SetMouseType(fOriginalSettings
.type
);
235 SetAccelerationFactor(fOriginalSettings
.accel
.accel_factor
);
236 SetMouseMode(fOriginalMode
);
237 SetFocusFollowsMouseMode(fOriginalFocusFollowsMouseMode
);
238 SetAcceptFirstClick(fOriginalAcceptFirstClick
);
240 SetMapping(fOriginalSettings
.map
);
244 // Checks if the settings are different then the original settings
246 MouseSettings::IsRevertable()
248 return fSettings
.click_speed
!= fOriginalSettings
.click_speed
249 || fSettings
.accel
.speed
!= fOriginalSettings
.accel
.speed
250 || fSettings
.type
!= fOriginalSettings
.type
251 || fSettings
.accel
.accel_factor
!= fOriginalSettings
.accel
.accel_factor
252 || fMode
!= fOriginalMode
253 || fFocusFollowsMouseMode
!= fOriginalFocusFollowsMouseMode
254 || fAcceptFirstClick
!= fOriginalAcceptFirstClick
255 || fSettings
.map
.button
[0] != fOriginalSettings
.map
.button
[0]
256 || fSettings
.map
.button
[1] != fOriginalSettings
.map
.button
[1]
257 || fSettings
.map
.button
[2] != fOriginalSettings
.map
.button
[2];
262 MouseSettings::SetWindowPosition(BPoint corner
)
264 fWindowPosition
= corner
;
269 MouseSettings::SetMouseType(int32 type
)
271 if (set_mouse_type(type
) == B_OK
)
272 fSettings
.type
= type
;
274 fprintf(stderr
, "error when set_mouse_type\n");
279 MouseSettings::ClickSpeed() const
281 return 1000000LL - fSettings
.click_speed
;
282 // to correct the Sliders 0-100000 scale
287 MouseSettings::SetClickSpeed(bigtime_t clickSpeed
)
289 clickSpeed
= 1000000LL - clickSpeed
;
291 if (set_click_speed(clickSpeed
) == B_OK
)
292 fSettings
.click_speed
= clickSpeed
;
294 fprintf(stderr
, "error when set_click_speed\n");
299 MouseSettings::SetMouseSpeed(int32 speed
)
301 if (set_mouse_speed(speed
) == B_OK
)
302 fSettings
.accel
.speed
= speed
;
304 fprintf(stderr
, "error when set_mouse_speed\n");
309 MouseSettings::SetAccelerationFactor(int32 factor
)
311 if (set_mouse_acceleration(factor
) == B_OK
)
312 fSettings
.accel
.accel_factor
= factor
;
314 fprintf(stderr
, "error when set_mouse_acceleration\n");
319 MouseSettings::Mapping(int32 index
) const
321 return fSettings
.map
.button
[index
];
326 MouseSettings::Mapping(mouse_map
&map
) const
333 MouseSettings::SetMapping(int32 index
, uint32 button
)
335 fSettings
.map
.button
[index
] = button
;
336 if (set_mouse_map(&fSettings
.map
) != B_OK
)
337 fprintf(stderr
, "error when set_mouse_map\n");
342 MouseSettings::SetMapping(mouse_map
&map
)
344 if (set_mouse_map(&map
) == B_OK
)
347 fprintf(stderr
, "error when set_mouse_map\n");
352 MouseSettings::SetMouseMode(mode_mouse mode
)
354 set_mouse_mode(mode
);
360 MouseSettings::SetFocusFollowsMouseMode(mode_focus_follows_mouse mode
)
362 set_focus_follows_mouse_mode(mode
);
363 fFocusFollowsMouseMode
= mode
;
368 MouseSettings::SetAcceptFirstClick(bool accept_first_click
)
370 set_accept_first_click(accept_first_click
);
371 fAcceptFirstClick
= accept_first_click
;