xref: /haiku/src/preferences/input/InputTouchpadPref.cpp (revision 8272123ea5548bb75f4bcb990edae313d3843657)
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 "InputTouchpadPref.h"
11 
12 #include <List.h>
13 #include <FindDirectory.h>
14 #include <File.h>
15 #include <String.h>
16 
17 #include <keyboard_mouse_driver.h>
18 
19 
20 TouchpadPref::TouchpadPref()
21 {
22 	// default center position
23 	fWindowPosition.x = -1;
24 	fWindowPosition.y = -1;
25 
26 	if (LoadSettings() != B_OK)
27 		Defaults();
28 
29 	fStartSettings = fSettings;
30 }
31 
32 
33 TouchpadPref::~TouchpadPref()
34 {
35 	delete fTouchPad;
36 
37 	SaveSettings();
38 }
39 
40 
41 void
42 TouchpadPref::Revert()
43 {
44 	fSettings = fStartSettings;
45 }
46 
47 
48 status_t
49 TouchpadPref::UpdateSettings()
50 {
51 	BMessage msg;
52 	msg.AddBool("scroll_twofinger", fSettings.scroll_twofinger);
53 	msg.AddBool("scroll_twofinger_horizontal",
54 		fSettings.scroll_twofinger_horizontal);
55 	msg.AddFloat("scroll_rightrange", fSettings.scroll_rightrange);
56 	msg.AddFloat("scroll_bottomrange", fSettings.scroll_bottomrange);
57 	msg.AddInt16("scroll_xstepsize", fSettings.scroll_xstepsize);
58 	msg.AddInt16("scroll_ystepsize", fSettings.scroll_ystepsize);
59 	msg.AddInt8("scroll_acceleration", fSettings.scroll_acceleration);
60 	msg.AddInt8("tapgesture_sensibility", fSettings.tapgesture_sensibility);
61 
62 	return fTouchPad->Control(MS_SET_TOUCHPAD_SETTINGS, &msg);
63 }
64 
65 
66 void
67 TouchpadPref::Defaults()
68 {
69 	fSettings = kDefaultTouchpadSettings;
70 }
71 
72 
73 status_t
74 TouchpadPref::GetSettingsPath(BPath &path)
75 {
76 	status_t status = find_directory(B_USER_SETTINGS_DIRECTORY, &path);
77 	if (status < B_OK)
78 		return status;
79 
80 	return path.Append(TOUCHPAD_SETTINGS_FILE);
81 }
82 
83 
84 status_t
85 TouchpadPref::LoadSettings()
86 {
87 	BPath path;
88 	status_t status = GetSettingsPath(path);
89 	if (status != B_OK)
90 		return status;
91 
92 	BFile settingsFile(path.Path(), B_READ_ONLY);
93 	status = settingsFile.InitCheck();
94 	if (status != B_OK)
95 		return status;
96 
97 	if (settingsFile.Read(&fSettings, sizeof(touchpad_settings))
98 			!= sizeof(touchpad_settings)) {
99 		LOG("failed to load settings\n");
100 		return B_ERROR;
101 	}
102 
103 	if (settingsFile.Read(&fWindowPosition, sizeof(BPoint))
104 			!= sizeof(BPoint)) {
105 		LOG("failed to load settings\n");
106 		return B_ERROR;
107 	}
108 
109 	return B_OK;
110 }
111 
112 
113 status_t
114 TouchpadPref::SaveSettings()
115 {
116 	BPath path;
117 	status_t status = GetSettingsPath(path);
118 	if (status != B_OK)
119 		return status;
120 
121 	BFile settingsFile(path.Path(), B_READ_WRITE | B_CREATE_FILE);
122 	status = settingsFile.InitCheck();
123 	if (status != B_OK)
124 		return status;
125 
126 	if (settingsFile.Write(&fSettings, sizeof(touchpad_settings))
127 			!= sizeof(touchpad_settings)) {
128 		LOG("can't save settings\n");
129 		return B_ERROR;
130 	}
131 
132 	if (settingsFile.Write(&fWindowPosition, sizeof(BPoint))
133 			!= sizeof(BPoint)) {
134 		LOG("can't save window position\n");
135 		return B_ERROR;
136 	}
137 
138 	return B_OK;
139 }