xref: /haiku/src/preferences/input/MouseSettings.cpp (revision 909af08f4328301fbdef1ffb41f566c3b5bec0c7)
1 /*
2  * Copyright 2019, Haiku, Inc.
3  * Distributed under the terms of the MIT License.
4  *
5  * Author:
6  *		Preetpal Kaur <preetpalok123@gmail.com>
7  */
8 
9 
10 #include "MouseSettings.h"
11 
12 #include <File.h>
13 #include <FindDirectory.h>
14 #include <Path.h>
15 #include <String.h>
16 #include <View.h>
17 
18 #include <stdio.h>
19 
20 
21 MouseSettings::MouseSettings(BString name)
22 	:
23 	fName(name)
24 {
25 	if (_RetrieveSettings() != B_OK)
26 		Defaults();
27 
28 	fOriginalSettings = fSettings;
29 	fOriginalMode = fMode;
30 	fOriginalFocusFollowsMouseMode = fFocusFollowsMouseMode;
31 	fOriginalAcceptFirstClick = fAcceptFirstClick;
32 }
33 
34 
35 MouseSettings::~MouseSettings()
36 {
37 }
38 
39 
40 status_t
41 MouseSettings::_RetrieveSettings()
42 {
43 	// retrieve current values
44 	if (get_mouse_map(fName, &fSettings.map) != B_OK)
45 		return B_ERROR;
46 	if (get_click_speed(fName, &fSettings.click_speed) != B_OK)
47 		return B_ERROR;
48 	if (get_mouse_speed(fName, &fSettings.accel.speed) != B_OK)
49 		return B_ERROR;
50 	if (get_mouse_acceleration(fName, &fSettings.accel.accel_factor) != B_OK)
51 		return B_ERROR;
52 	if (get_mouse_type(fName, &fSettings.type) != B_OK)
53 		return B_ERROR;
54 
55 	fMode = mouse_mode();
56 	fFocusFollowsMouseMode = focus_follows_mouse_mode();
57 	fAcceptFirstClick = accept_first_click();
58 
59 	return B_OK;
60 }
61 
62 
63 // Resets the settings to the system defaults
64 void
65 MouseSettings::Defaults()
66 {
67 	SetClickSpeed(kDefaultClickSpeed);
68 	SetMouseSpeed(kDefaultMouseSpeed);
69 	SetMouseType(kDefaultMouseType);
70 	SetAccelerationFactor(kDefaultAccelerationFactor);
71 	SetMouseMode(B_NORMAL_MOUSE);
72 	SetFocusFollowsMouseMode(B_NORMAL_FOCUS_FOLLOWS_MOUSE);
73 	SetAcceptFirstClick(kDefaultAcceptFirstClick);
74 
75 	mouse_map map;
76 	for (int i = 0; i < B_MAX_MOUSE_BUTTONS; i++)
77 		map.button[i] = B_MOUSE_BUTTON(i + 1);
78 	SetMapping(map);
79 }
80 
81 
82 // Checks if the settings are different then the system defaults
83 bool
84 MouseSettings::IsDefaultable() const
85 {
86 	return fSettings.click_speed != kDefaultClickSpeed
87 		|| fSettings.accel.speed != kDefaultMouseSpeed
88 		|| fSettings.type != kDefaultMouseType
89 		|| fSettings.accel.accel_factor != kDefaultAccelerationFactor
90 		|| fMode != B_NORMAL_MOUSE
91 		|| fFocusFollowsMouseMode != B_NORMAL_FOCUS_FOLLOWS_MOUSE
92 		|| fAcceptFirstClick != kDefaultAcceptFirstClick
93 		|| fSettings.map.button[0] != B_PRIMARY_MOUSE_BUTTON
94 		|| fSettings.map.button[1] != B_SECONDARY_MOUSE_BUTTON
95 		|| fSettings.map.button[2] != B_TERTIARY_MOUSE_BUTTON
96 		|| fSettings.map.button[3] != B_MOUSE_BUTTON(4)
97 		|| fSettings.map.button[4] != B_MOUSE_BUTTON(5)
98 		|| fSettings.map.button[5] != B_MOUSE_BUTTON(6);
99 }
100 
101 
102 //	Reverts to the active settings at program startup
103 void
104 MouseSettings::Revert()
105 {
106 	SetClickSpeed(fOriginalSettings.click_speed);
107 	SetMouseSpeed(fOriginalSettings.accel.speed);
108 	SetMouseType(fOriginalSettings.type);
109 	SetAccelerationFactor(fOriginalSettings.accel.accel_factor);
110 	SetMouseMode(fOriginalMode);
111 	SetFocusFollowsMouseMode(fOriginalFocusFollowsMouseMode);
112 	SetAcceptFirstClick(fOriginalAcceptFirstClick);
113 
114 	SetMapping(fOriginalSettings.map);
115 }
116 
117 
118 // Checks if the settings are different then the original settings
119 bool
120 MouseSettings::IsRevertable() const
121 {
122 	return fSettings.click_speed != fOriginalSettings.click_speed
123 		|| fSettings.accel.speed != fOriginalSettings.accel.speed
124 		|| fSettings.type != fOriginalSettings.type
125 		|| fSettings.accel.accel_factor != fOriginalSettings.accel.accel_factor
126 		|| fMode != fOriginalMode
127 		|| fFocusFollowsMouseMode != fOriginalFocusFollowsMouseMode
128 		|| fAcceptFirstClick != fOriginalAcceptFirstClick
129 		|| fSettings.map.button[0] != fOriginalSettings.map.button[0]
130 		|| fSettings.map.button[1] != fOriginalSettings.map.button[1]
131 		|| fSettings.map.button[2] != fOriginalSettings.map.button[2]
132 		|| fSettings.map.button[3] != fOriginalSettings.map.button[3]
133 		|| fSettings.map.button[4] != fOriginalSettings.map.button[4]
134 		|| fSettings.map.button[5] != fOriginalSettings.map.button[5];
135 }
136 
137 
138 void
139 MouseSettings::SetMouseType(int32 type)
140 {
141 	if (set_mouse_type(fName, type) == B_OK)
142 		fSettings.type = type;
143 }
144 
145 
146 bigtime_t
147 MouseSettings::ClickSpeed() const
148 {
149 	return fSettings.click_speed;
150 }
151 
152 
153 void
154 MouseSettings::SetClickSpeed(bigtime_t clickSpeed)
155 {
156 	if (set_click_speed(fName, clickSpeed) == B_OK)
157 		fSettings.click_speed = clickSpeed;
158 }
159 
160 
161 void
162 MouseSettings::SetMouseSpeed(int32 speed)
163 {
164 	if (set_mouse_speed(fName, speed) == B_OK)
165 		fSettings.accel.speed = speed;
166 }
167 
168 
169 void
170 MouseSettings::SetAccelerationFactor(int32 factor)
171 {
172 	if (set_mouse_acceleration(fName, factor) == B_OK)
173 		fSettings.accel.accel_factor = factor;
174 }
175 
176 
177 uint32
178 MouseSettings::Mapping(int32 index) const
179 {
180 	return fSettings.map.button[index];
181 }
182 
183 
184 void
185 MouseSettings::Mapping(mouse_map& map) const
186 {
187 	map = fSettings.map;
188 }
189 
190 
191 void
192 MouseSettings::SetMapping(int32 index, uint32 button)
193 {
194 	fSettings.map.button[index] = button;
195 	set_mouse_map(fName, &fSettings.map);
196 }
197 
198 
199 void
200 MouseSettings::SetMapping(mouse_map& map)
201 {
202 	if (set_mouse_map(fName, &map) == B_OK)
203 		fSettings.map = map;
204 }
205 
206 
207 void
208 MouseSettings::SetMouseMode(mode_mouse mode)
209 {
210 	set_mouse_mode(mode);
211 	fMode = mode;
212 }
213 
214 
215 void
216 MouseSettings::SetFocusFollowsMouseMode(mode_focus_follows_mouse mode)
217 {
218 	set_focus_follows_mouse_mode(mode);
219 	fFocusFollowsMouseMode = mode;
220 }
221 
222 
223 void
224 MouseSettings::SetAcceptFirstClick(bool accept_first_click)
225 {
226 	set_accept_first_click(accept_first_click);
227 	fAcceptFirstClick = accept_first_click;
228 }
229 
230 
231 mouse_settings*
232 MouseSettings::GetSettings()
233 {
234 	return &fSettings;
235 }
236 
237 
238 MultipleMouseSettings::MultipleMouseSettings()
239 {
240 }
241 
242 
243 MultipleMouseSettings::~MultipleMouseSettings()
244 {
245 	std::map<BString, MouseSettings*>::iterator itr;
246 	for (itr = fMouseSettingsObject.begin(); itr != fMouseSettingsObject.end(); ++itr)
247 		delete itr->second;
248 }
249 
250 
251 MouseSettings*
252 MultipleMouseSettings::AddMouseSettings(BString mouse_name)
253 {
254 	std::map<BString, MouseSettings*>::iterator itr;
255 	itr = fMouseSettingsObject.find(mouse_name);
256 
257 	if (itr != fMouseSettingsObject.end())
258 		return itr->second;
259 
260 	MouseSettings* settings = new(std::nothrow) MouseSettings(mouse_name);
261 	if (settings == NULL)
262 		return NULL;
263 
264 	fMouseSettingsObject.insert(
265 		std::pair<BString, MouseSettings*>(mouse_name, settings));
266 	return settings;
267 }
268