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 fConnected = false; 23 // default center position 24 fWindowPosition.x = -1; 25 fWindowPosition.y = -1; 26 27 ConnectToTouchPad(); 28 29 if (LoadSettings() != B_OK) 30 Defaults(); 31 32 fStartSettings = fSettings; 33 } 34 35 36 TouchpadPref::~TouchpadPref() 37 { 38 if (fConnected) 39 delete fTouchPad; 40 41 SaveSettings(); 42 } 43 44 45 void 46 TouchpadPref::Revert() 47 { 48 fSettings = fStartSettings; 49 } 50 51 52 status_t 53 TouchpadPref::UpdateSettings() 54 { 55 if (!fConnected) 56 return B_ERROR; 57 58 LOG("UpdateSettings of device %s\n", fTouchPad->Name()); 59 60 BMessage msg; 61 msg.AddBool("scroll_twofinger", fSettings.scroll_twofinger); 62 msg.AddBool("scroll_twofinger_horizontal", 63 fSettings.scroll_twofinger_horizontal); 64 msg.AddFloat("scroll_rightrange", fSettings.scroll_rightrange); 65 msg.AddFloat("scroll_bottomrange", fSettings.scroll_bottomrange); 66 msg.AddInt16("scroll_xstepsize", fSettings.scroll_xstepsize); 67 msg.AddInt16("scroll_ystepsize", fSettings.scroll_ystepsize); 68 msg.AddInt8("scroll_acceleration", fSettings.scroll_acceleration); 69 msg.AddInt8("tapgesture_sensibility", fSettings.tapgesture_sensibility); 70 71 return fTouchPad->Control(MS_SET_TOUCHPAD_SETTINGS, &msg); 72 } 73 74 75 void 76 TouchpadPref::Defaults() 77 { 78 fSettings = kDefaultTouchpadSettings; 79 } 80 81 82 status_t 83 TouchpadPref::GetSettingsPath(BPath &path) 84 { 85 status_t status = find_directory(B_USER_SETTINGS_DIRECTORY, &path); 86 if (status < B_OK) 87 return status; 88 89 return path.Append(TOUCHPAD_SETTINGS_FILE); 90 } 91 92 93 status_t 94 TouchpadPref::LoadSettings() 95 { 96 BPath path; 97 status_t status = GetSettingsPath(path); 98 if (status != B_OK) 99 return status; 100 101 BFile settingsFile(path.Path(), B_READ_ONLY); 102 status = settingsFile.InitCheck(); 103 if (status != B_OK) 104 return status; 105 106 if (settingsFile.Read(&fSettings, sizeof(touchpad_settings)) 107 != sizeof(touchpad_settings)) { 108 LOG("failed to load settings\n"); 109 return B_ERROR; 110 } 111 112 if (settingsFile.Read(&fWindowPosition, sizeof(BPoint)) 113 != sizeof(BPoint)) { 114 LOG("failed to load settings\n"); 115 return B_ERROR; 116 } 117 118 return B_OK; 119 } 120 121 122 status_t 123 TouchpadPref::SaveSettings() 124 { 125 BPath path; 126 status_t status = GetSettingsPath(path); 127 if (status != B_OK) 128 return status; 129 130 BFile settingsFile(path.Path(), B_READ_WRITE | B_CREATE_FILE); 131 status = settingsFile.InitCheck(); 132 if (status != B_OK) 133 return status; 134 135 if (settingsFile.Write(&fSettings, sizeof(touchpad_settings)) 136 != sizeof(touchpad_settings)) { 137 LOG("can't save settings\n"); 138 return B_ERROR; 139 } 140 141 if (settingsFile.Write(&fWindowPosition, sizeof(BPoint)) 142 != sizeof(BPoint)) { 143 LOG("can't save window position\n"); 144 return B_ERROR; 145 } 146 147 return B_OK; 148 } 149 150 151 status_t 152 TouchpadPref::ConnectToTouchPad() 153 { 154 BList devList; 155 status_t status = get_input_devices(&devList); 156 if (status != B_OK) 157 return status; 158 159 int32 i = 0; 160 while (true) { 161 BInputDevice* dev = (BInputDevice*)devList.ItemAt(i); 162 if (dev == NULL) 163 break; 164 i++; 165 166 LOG("input device %s\n", dev->Name()); 167 168 BString name = dev->Name(); 169 170 if (name.FindFirst("Touchpad") >= 0 171 && dev->Type() == B_POINTING_DEVICE 172 && !fConnected) { 173 fConnected = true; 174 fTouchPad = dev; 175 // Don't bail out here, since we need to delete the other devices 176 // yet. 177 } else { 178 delete dev; 179 } 180 } 181 if (fConnected) 182 return B_OK; 183 184 LOG("touchpad input device NOT found\n"); 185 return B_ENTRY_NOT_FOUND; 186 }