1 /* 2 * Copyright 2005, Axel Dörfler, axeld@pinc-software.de. All rights reserved. 3 * Distributed under the terms of the MIT License. 4 */ 5 6 7 #include "Settings.h" 8 9 #include <File.h> 10 #include <Entry.h> 11 #include <FindDirectory.h> 12 #include <Path.h> 13 #include <Volume.h> 14 #include <VolumeRoster.h> 15 #include <driver_settings.h> 16 17 #include <stdio.h> 18 #include <stdlib.h> 19 #include <string.h> 20 21 22 static const char* kWindowSettingsFile = "VM_data"; 23 static const char* kVirtualMemorySettings = "virtual_memory"; 24 25 26 Settings::Settings() 27 : 28 fPositionUpdated(false), 29 fSwapUpdated(false) 30 { 31 ReadWindowSettings(); 32 ReadSwapSettings(); 33 } 34 35 36 Settings::~Settings() 37 { 38 WriteWindowSettings(); 39 WriteSwapSettings(); 40 } 41 42 43 void 44 Settings::ReadWindowSettings() 45 { 46 bool success = false; 47 48 BPath path; 49 if (find_directory(B_USER_SETTINGS_DIRECTORY, &path) == B_OK) { 50 path.Append(kWindowSettingsFile); 51 BFile file; 52 if (file.SetTo(path.Path(), B_READ_ONLY) == B_OK) 53 // Now read in the data 54 if (file.Read(&fWindowPosition, sizeof(BPoint)) == sizeof(BPoint)) 55 success = true; 56 } 57 58 if (!success) 59 fWindowPosition.Set(-1, -1); 60 } 61 62 63 void 64 Settings::WriteWindowSettings() 65 { 66 if (!fPositionUpdated) 67 return; 68 69 BPath path; 70 if (find_directory(B_USER_SETTINGS_DIRECTORY, &path) < B_OK) 71 return; 72 73 path.Append(kWindowSettingsFile); 74 75 BFile file; 76 if (file.SetTo(path.Path(), B_WRITE_ONLY | B_CREATE_FILE | B_ERASE_FILE) == B_OK) 77 file.Write(&fWindowPosition, sizeof(BPoint)); 78 } 79 80 81 void 82 Settings::SetWindowPosition(BPoint position) 83 { 84 if (position == fWindowPosition) 85 return; 86 87 fWindowPosition = position; 88 fPositionUpdated = true; 89 } 90 91 92 void 93 Settings::ReadSwapSettings() 94 { 95 // read current swap settings from disk 96 void* settings = load_driver_settings("virtual_memory"); 97 if (settings != NULL) { 98 fSwapEnabled = get_driver_boolean_parameter(settings, "vm", false, false); 99 100 const char* string = get_driver_parameter(settings, "swap_size", NULL, NULL); 101 fSwapSize = string ? atoll(string) : 0; 102 103 if (fSwapSize <= 0) { 104 fSwapEnabled = false; 105 fSwapSize = 0; 106 } 107 unload_driver_settings(settings); 108 } else { 109 // settings are not available, try to find out what the kernel is up to 110 // ToDo: introduce a kernel call for this! 111 fSwapSize = 0; 112 113 BPath path; 114 if (find_directory(B_COMMON_VAR_DIRECTORY, &path) == B_OK) { 115 path.Append("swap"); 116 BEntry swap(path.Path()); 117 if (swap.GetSize(&fSwapSize) != B_OK) 118 fSwapSize = 0; 119 } 120 121 fSwapEnabled = fSwapSize != 0; 122 } 123 124 // ToDo: read those as well 125 BVolumeRoster volumeRoster; 126 volumeRoster.GetBootVolume(&fSwapVolume); 127 128 fInitialSwapEnabled = fSwapEnabled; 129 fInitialSwapSize = fSwapSize; 130 fInitialSwapVolume = fSwapVolume.Device(); 131 } 132 133 134 void 135 Settings::WriteSwapSettings() 136 { 137 if (!SwapChanged()) 138 return; 139 140 BPath path; 141 if (find_directory(B_USER_SETTINGS_DIRECTORY, &path) != B_OK) 142 return; 143 144 path.Append("kernel/drivers"); 145 path.Append(kVirtualMemorySettings); 146 147 BFile file; 148 if (file.SetTo(path.Path(), B_WRITE_ONLY | B_CREATE_FILE | B_ERASE_FILE) != B_OK) 149 return; 150 151 char buffer[256]; 152 snprintf(buffer, sizeof(buffer), "vm %s\nswap_size %Ld\n", 153 fSwapEnabled ? "on" : "off", fSwapSize); 154 155 file.Write(buffer, strlen(buffer)); 156 } 157 158 159 void 160 Settings::SetSwapEnabled(bool enabled) 161 { 162 fSwapEnabled = enabled; 163 } 164 165 166 void 167 Settings::SetSwapSize(off_t size) 168 { 169 fSwapSize = size; 170 } 171 172 173 void 174 Settings::SetSwapVolume(BVolume &volume) 175 { 176 if (volume.Device() == fSwapVolume.Device() 177 || volume.InitCheck() != B_OK) 178 return; 179 180 fSwapVolume.SetTo(volume.Device()); 181 } 182 183 184 void 185 Settings::SetSwapDefaults() 186 { 187 fSwapEnabled = true; 188 189 BVolumeRoster volumeRoster; 190 volumeRoster.GetBootVolume(&fSwapVolume); 191 192 system_info info; 193 get_system_info(&info); 194 fSwapSize = (off_t)info.max_pages * B_PAGE_SIZE; 195 } 196 197 198 void 199 Settings::RevertSwapChanges() 200 { 201 fSwapEnabled = fInitialSwapEnabled; 202 fSwapSize = fInitialSwapSize; 203 fSwapVolume.SetTo(fInitialSwapVolume); 204 } 205 206 207 bool 208 Settings::SwapChanged() 209 { 210 return fSwapEnabled != fInitialSwapEnabled 211 || fSwapSize != fInitialSwapSize 212 || fSwapVolume.Device() != fInitialSwapVolume; 213 } 214 215