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