Rewrote VirtualMemory, added some Haiku specific functionality (currently disabled).

git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@13570 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Axel Dörfler
2005-07-10 14:58:03 +00:00
parent 26ca407bfc
commit f877fb742f
15 changed files with 773 additions and 628 deletions
+15 -5
View File
@@ -1,10 +1,20 @@
SubDir OBOS_TOP src preferences virtualmemory ;
if $(TARGET_PLATFORM) = r5 {
DRIVER_SETTINGS = driver_settings.c ;
}
Preference VirtualMemory :
main.cpp
Pref_Utils.cpp
MainWindow.cpp
VMSettings.cpp
: libbe.so libstdc++.r4.so
VirtualMemory.cpp
SettingsWindow.cpp
Settings.cpp
$(DRIVER_SETTINGS)
: libbe.so libstdc++.r4.so libroot.so
: VirtualMemory.rdef
;
if $(TARGET_PLATFORM) = r5 {
SEARCH on [ FGristFiles driver_settings.c ] +=
[ FDirName $(OBOS_TOP) src system libroot os ] ;
}
@@ -1,259 +0,0 @@
/*! \file MainWindow.cpp
* \brief Code for the MainWindow class.
*
* Displays the main window, the essence of the app.
*
*/
#include "MainWindow.h"
#include "Pref_Utils.h"
#include <String.h>
const char *kRequestStr = "Requested swap file size: ";
/**
* Constructor.
* @param frame The size to make the window.
* @param physMem The amount of physical memory in the machine.
* @param currSwp The current swap file size.
* @param minVal The minimum value of the swap file.
* @param maxSwapVal The maximum value of the swap file.
*/
MainWindow::MainWindow(BRect frame, int physMemVal, int currSwapVal, int minVal, int maxSwapVal, VMSettings *Settings)
:BWindow(frame, "VirtualMemory", B_TITLED_WINDOW, B_NOT_RESIZABLE | B_ASYNCHRONOUS_CONTROLS | B_NOT_ZOOMABLE){
fSettings = Settings;
/**
* Sets the fill color for the "used" portion of the slider.
*/
rgb_color fillColor = { 0, 102, 152, 255 };
/**
* Set up variables to help handle font sensitivity
*/
float fontheight = FontHeight(true, NULL);
/**
* boxRect sets the size of the visible box around the string views and
* the slider.
*/
BRect boxRect = Bounds();
boxRect.InsetBy(11, 11);
boxRect.bottom -= 25;
BString labels;
forigMemSize = currSwapVal;
fminSwapVal = minVal;
BRect rect(0.0, 0.0, boxRect.Width() -20.0, fontheight);
rect.OffsetTo(10.0, 10.0);
/**
* Set up the "Physical Memory" label.
*/
BStringView *physMem;
labels << "Physical memory: " << physMemVal << " MB";
physMem = new BStringView(rect, "PhysicalMemory", labels.String(), B_FOLLOW_ALL, B_WILL_DRAW);
/**
* Set up the "Current Swap File Size" label.
*/
BStringView *currSwap;
rect.OffsetBy(0, rect.Height() +5);
labels = "Current swap file size: ";
labels << currSwapVal << " MB";
currSwap = new BStringView(rect, "CurrentSwapSize", labels.String(), B_FOLLOW_ALL, B_WILL_DRAW);
/**
* Set up the "Requested Swap File Size" label.
*/
rect.OffsetBy(0, rect.Height() +5);
labels = kRequestStr;
labels << currSwapVal << " MB";
/**
* Set up the slider.
*/
BString sliderMinLabel;
BString sliderMaxLabel;
sliderMinLabel << fminSwapVal << " MB";
sliderMaxLabel << maxSwapVal << " MB";
rect.bottom = rect.top+2;
freqSizeSlider = new BSlider(rect, "ReqSwapSizeSlider", labels.String(),
new BMessage(MEMORY_SLIDER_MSG), fminSwapVal, maxSwapVal, B_TRIANGLE_THUMB, B_FOLLOW_LEFT, B_WILL_DRAW);
freqSizeSlider->SetLimitLabels(sliderMinLabel.String(), sliderMaxLabel.String());
freqSizeSlider->UseFillColor(true, &fillColor);
freqSizeSlider->SetModificationMessage(new BMessage(SLIDER_UPDATE_MSG));
freqSizeSlider->SetValue(currSwapVal);
/**
* Initializes the restart notice view.
*/
rect = freqSizeSlider->Frame();
rect.top = rect.bottom +2;
rect.bottom = rect.top +fontheight;
frestart = new BStringView(rect, "RestartMessage", B_EMPTY_STRING, B_FOLLOW_ALL, B_WILL_DRAW);
frestart->SetAlignment(B_ALIGN_CENTER);
/**
* This view holds the three labels and the slider.
*/
BBox *boxView;
boxView = new BBox(boxRect, "BoxView", B_FOLLOW_ALL, B_WILL_DRAW, B_FANCY_BORDER);
boxView->AddChild(freqSizeSlider);
boxView->AddChild(physMem);
boxView->AddChild(currSwap);
boxView->AddChild(frestart);
rect.Set(0.0, 0.0, 75.0, 20.0);
BButton *defaultButton;
rect.OffsetTo(10, boxRect.bottom +5);
defaultButton = new BButton(rect, "DefaultButton", "Default",
new BMessage(DEFAULT_BUTTON_MSG), B_FOLLOW_ALL, B_WILL_DRAW);
rect.OffsetBy(85, 0);
frevertButton = new BButton(rect, "RevertButton", "Revert",
new BMessage(REVERT_BUTTON_MSG), B_FOLLOW_ALL, B_WILL_DRAW);
frevertButton->SetEnabled(false);
BBox *topLevelView;
topLevelView = new BBox(Bounds(), "TopLevelView", B_FOLLOW_ALL, B_WILL_DRAW, B_PLAIN_BORDER);
topLevelView->AddChild(boxView);
topLevelView->AddChild(defaultButton);
topLevelView->AddChild(frevertButton);
AddChild(topLevelView);
}
/**
* Displays the "Changes will take effect on restart" message.
* @param setTo If true, displays the message If false, un-displays it.
*/
void MainWindow::toggleChangedMessage(bool setTo){
BString message = B_EMPTY_STRING;
if (setTo) {
frevertButton->SetEnabled(true);
message << "Changes will take effect on restart.";
} else {
frevertButton->SetEnabled(false);
}
frestart->SetText(message.String());
}//toggleChangedMessage
/**
* Handles messages.
* @param message The message recieved by the window.
*/
void MainWindow::MessageReceived(BMessage *message){
switch(message->what){
/**
* Updates the requested swap file size during a drag.
*/
case SLIDER_UPDATE_MSG:
{
int32 currVal = freqSizeSlider->Value();
BString label(kRequestStr);
label << currVal << " MB";
freqSizeSlider->SetLabel(label.String());
if (currVal != forigMemSize)
toggleChangedMessage(true);
else
toggleChangedMessage(false);
break;
}
/**
* Case where the slider was moved.
* Resets the "Requested Swap File Size" label to the new value.
*/
case MEMORY_SLIDER_MSG:
{
int32 currVal = freqSizeSlider->Value();
BString label(kRequestStr);
label << currVal << " MB";
freqSizeSlider->SetLabel(label.String());
if (currVal != forigMemSize)
toggleChangedMessage(true);
else
toggleChangedMessage(false);
break;
}
/**
* Case where the default button was pressed.
* Eventually will set the swap file size to the optimum size,
* as decided by this app (as soon as I can figure out how to
* do that).
*/
case DEFAULT_BUTTON_MSG:
{
freqSizeSlider->SetValue(fminSwapVal);
BString label(kRequestStr);
label << fminSwapVal << " MB";
freqSizeSlider->SetLabel(label.String());
if(fminSwapVal != forigMemSize)
toggleChangedMessage(true);
else
toggleChangedMessage(false);
break;
}
/**
* Case where the revert button was pressed.
* Returns things to the way they were when the app was started,
* which is not necessarily the default size.
*/
case REVERT_BUTTON_MSG:
{
frevertButton->SetEnabled(false);
BString label(kRequestStr);
label << forigMemSize << " MB";
freqSizeSlider->SetLabel(label.String());
freqSizeSlider->SetValue(forigMemSize);
toggleChangedMessage(false);
break;
}
/**
* Unhandled messages get passed to BWindow.
*/
default:
BWindow::MessageReceived(message);
}
}
/**
* Quits and Saves.
* Sets the swap size and turns the virtual memory on by writing to the
* /boot/home/config/settings/kernel/drivers/virtual_memory file.
*/
bool MainWindow::QuitRequested(){
if (freqSizeSlider->Value() != forigMemSize) {
FILE *settingsFile = fopen("/boot/home/config/settings/kernel/drivers/virtual_memory", "w");
fprintf(settingsFile, "vm on\n");
fprintf(settingsFile, "swap_size %d\n", (int(freqSizeSlider->Value()) * 1048576));
fclose(settingsFile);
}
be_app->PostMessage(B_QUIT_REQUESTED);
return true;
}
void MainWindow::FrameMoved(BPoint origin)
{
fSettings->SetWindowPosition(Frame());
}
@@ -1,86 +0,0 @@
/*! \file MainWindow.h
\brief Header for the MainWindow class.
*/
#ifndef MAIN_WINDOW_H
#define MAIN_WINDOW_H
/*!
* Default button message.
*/
#define DEFAULT_BUTTON_MSG 'dflt'
/*!
* Revert button message.
*/
#define REVERT_BUTTON_MSG 'rvrt'
/*!
* Slider message.
*/
#define MEMORY_SLIDER_MSG 'sldr'
/*!
* Slider dragging message.
*/
#define SLIDER_UPDATE_MSG 'sldu'
#include <Application.h>
#include <Box.h>
#include <Button.h>
#include <Slider.h>
#include <stdio.h>
#include <StringView.h>
#include <Window.h>
#include "VMSettings.h"
/**
* The main window of the app.
*
* Sets up and displays everything you need for the app.
*/
class MainWindow : public BWindow{
private:
/**
* Saves the size of the swap file when the app is started
* so that it can be restored later if need be.
*/
int forigMemSize;
/**
* Saves the minimum virtual memory value;
*/
int fminSwapVal;
/**
* The slider that lets you adjust the size of the swap file.
*/
BSlider *freqSizeSlider;
/**
* The button that returns the swap file to the original
* size.
*/
BButton *frevertButton;
/**
* The BStringView that informs you that you need to
* restart.
*/
BStringView *frestart;
VMSettings *fSettings;
public:
MainWindow(BRect frame, int physMem, int currSwp, int sliderMin, int sliderMax, VMSettings *fSettings);
virtual bool QuitRequested();
virtual void MessageReceived(BMessage *message);
virtual void FrameMoved(BPoint origin);
virtual void toggleChangedMessage(bool setTo);
};
#endif
@@ -1,30 +0,0 @@
#include "Pref_Utils.h"
float
FontHeight(bool full, BView* target)
{
font_height finfo;
if (target != NULL)
target->GetFontHeight(&finfo);
else
be_plain_font->GetHeight(&finfo);
float height = ceil(finfo.ascent) + ceil(finfo.descent);
if (full)
height += ceil(finfo.leading);
return height;
}
color_map*
ColorMap()
{
color_map* cmap;
BScreen screen(B_MAIN_SCREEN_ID);
cmap = (color_map*)screen.ColorMap();
return cmap;
}
@@ -1,10 +0,0 @@
#ifndef SHARED_PREF_UTILS
#define SHARED_PREF_UTILS
#include <Screen.h>
#include <View.h>
float FontHeight(bool full, BView* view = NULL);
color_map* ColorMap();
#endif
+213
View File
@@ -0,0 +1,213 @@
/*
* Copyright 2005, Axel Dörfler, [email protected]. All rights reserved.
* Distributed under the terms of the MIT License.
*/
#include "Settings.h"
#include <File.h>
#include <Entry.h>
#include <FindDirectory.h>
#include <Path.h>
#include <Volume.h>
#include <VolumeRoster.h>
#include <driver_settings.h>
#include <stdio.h>
#include <stdlib.h>
static const char* kWindowSettingsFile = "VM_data";
static const char* kVirtualMemorySettings = "virtual_memory";
Settings::Settings()
:
fPositionUpdated(false),
fSwapUpdated(false)
{
ReadWindowSettings();
ReadSwapSettings();
}
Settings::~Settings()
{
WriteWindowSettings();
WriteSwapSettings();
}
void
Settings::ReadWindowSettings()
{
bool success = false;
BPath path;
if (find_directory(B_USER_SETTINGS_DIRECTORY, &path) == B_OK) {
path.Append(kWindowSettingsFile);
BFile file;
if (file.SetTo(path.Path(), B_READ_ONLY) == B_OK)
// Now read in the data
if (file.Read(&fWindowPosition, sizeof(BPoint)) == sizeof(BPoint))
success = true;
}
if (!success)
fWindowPosition.Set(-1, -1);
}
void
Settings::WriteWindowSettings()
{
if (!fPositionUpdated)
return;
BPath path;
if (find_directory(B_USER_SETTINGS_DIRECTORY, &path) < B_OK)
return;
path.Append(kWindowSettingsFile);
BFile file;
if (file.SetTo(path.Path(), B_WRITE_ONLY | B_CREATE_FILE | B_ERASE_FILE) == B_OK)
file.Write(&fWindowPosition, sizeof(BPoint));
}
void
Settings::SetWindowPosition(BPoint position)
{
if (position == fWindowPosition)
return;
fWindowPosition = position;
fPositionUpdated = true;
}
void
Settings::ReadSwapSettings()
{
// read current swap settings from disk
void* settings = load_driver_settings("virtual_memory");
if (settings != NULL) {
fSwapEnabled = get_driver_boolean_parameter(settings, "vm", false, false);
const char* string = get_driver_parameter(settings, "swap_size", NULL, NULL);
fSwapSize = string ? atoll(string) : 0;
if (fSwapSize <= 0) {
fSwapEnabled = false;
fSwapSize = 0;
}
unload_driver_settings(settings);
} else {
// settings are not available, try to find out what the kernel is up to
// ToDo: introduce a kernel call for this!
fSwapSize = 0;
BPath path;
if (find_directory(B_COMMON_VAR_DIRECTORY, &path) == B_OK) {
path.Append("swap");
BEntry swap(path.Path());
if (swap.GetSize(&fSwapSize) != B_OK)
fSwapSize = 0;
}
fSwapEnabled = fSwapSize != 0;
}
// ToDo: read those as well
BVolumeRoster volumeRoster;
volumeRoster.GetBootVolume(&fSwapVolume);
fInitialSwapEnabled = fSwapEnabled;
fInitialSwapSize = fSwapSize;
fInitialSwapVolume = fSwapVolume.Device();
}
void
Settings::WriteSwapSettings()
{
if (!SwapChanged())
return;
BPath path;
if (find_directory(B_USER_SETTINGS_DIRECTORY, &path) != B_OK)
return;
path.Append("kernel/drivers");
path.Append(kVirtualMemorySettings);
BFile file;
if (file.SetTo(path.Path(), B_WRITE_ONLY | B_CREATE_FILE | B_ERASE_FILE) != B_OK)
return;
char buffer[256];
snprintf(buffer, sizeof(buffer), "vm %s\nswap_size %Ld\n",
fSwapEnabled ? "on" : "off", fSwapSize);
file.Write(buffer, strlen(buffer));
}
void
Settings::SetSwapEnabled(bool enabled)
{
fSwapEnabled = enabled;
}
void
Settings::SetSwapSize(off_t size)
{
fSwapSize = size;
}
void
Settings::SetSwapVolume(BVolume &volume)
{
if (volume.Device() == fSwapVolume.Device()
|| volume.InitCheck() != B_OK)
return;
fSwapVolume.SetTo(volume.Device());
}
void
Settings::SetSwapDefaults()
{
fSwapEnabled = true;
BVolumeRoster volumeRoster;
volumeRoster.GetBootVolume(&fSwapVolume);
system_info info;
get_system_info(&info);
fSwapSize = (off_t)info.max_pages * B_PAGE_SIZE;
}
void
Settings::RevertSwapChanges()
{
fSwapEnabled = fInitialSwapEnabled;
fSwapSize = fInitialSwapSize;
fSwapVolume.SetTo(fInitialSwapVolume);
}
bool
Settings::SwapChanged()
{
return fSwapEnabled != fInitialSwapEnabled
|| fSwapSize != fInitialSwapSize
|| fSwapVolume.Device() != fInitialSwapVolume;
}
+52
View File
@@ -0,0 +1,52 @@
/*
* Copyright 2005, Axel Dörfler, [email protected]. All rights reserved.
* Distributed under the terms of the MIT License.
*/
#ifndef SETTINGS_H
#define SETTINGS_H
#include <Point.h>
#include <Volume.h>
class Settings {
public :
Settings();
virtual ~Settings();
BPoint WindowPosition() const { return fWindowPosition; }
void SetWindowPosition(BPoint position);
bool SwapEnabled() const { return fSwapEnabled; }
off_t SwapSize() const { return fSwapSize; }
BVolume& SwapVolume() { return fSwapVolume; }
void SetSwapEnabled(bool enabled);
void SetSwapSize(off_t size);
void SetSwapVolume(BVolume& volume);
void SetSwapDefaults();
void RevertSwapChanges();
bool SwapChanged();
private:
void ReadWindowSettings();
void WriteWindowSettings();
void ReadSwapSettings();
void WriteSwapSettings();
BPoint fWindowPosition;
bool fSwapEnabled;
off_t fSwapSize;
BVolume fSwapVolume;
bool fInitialSwapEnabled;
off_t fInitialSwapSize;
dev_t fInitialSwapVolume;
bool fPositionUpdated, fSwapUpdated;
};
#endif /* SETTINGS_H */
@@ -0,0 +1,366 @@
/*
* Copyright 2005, Axel Dörfler, [email protected]. All rights reserved.
* Distributed under the terms of the MIT License.
*/
#include "SettingsWindow.h"
#include "Settings.h"
#include <Application.h>
#include <Alert.h>
#include <Box.h>
#include <Button.h>
#include <CheckBox.h>
#include <StringView.h>
#include <String.h>
#include <Slider.h>
#include <PopUpMenu.h>
#include <MenuItem.h>
#include <MenuField.h>
#include <Screen.h>
#include <FindDirectory.h>
#include <Path.h>
#include <Volume.h>
#include <VolumeRoster.h>
#include <stdio.h>
static const uint32 kMsgDefaults = 'dflt';
static const uint32 kMsgRevert = 'rvrt';
static const uint32 kMsgSliderUpdate = 'slup';
static const uint32 kMsgSwapEnabledUpdate = 'swen';
class SizeSlider : public BSlider {
public:
SizeSlider(BRect rect, const char* name, const char* label,
BMessage* message, int32 min, int32 max);
virtual ~SizeSlider();
virtual char* UpdateText() const;
private:
mutable BString fText;
};
static const int64 kMegaByte = 1048576;
const char *
byte_string(int64 size)
{
double value = 1. * size;
static char string[64];
if (value < 1024)
snprintf(string, sizeof(string), "%Ld B", size);
else {
char *units[] = {"K", "M", "G", NULL};
int32 i = -1;
do {
value /= 1024.0;
i++;
} while (value >= 1024 && units[i + 1]);
off_t rounded = off_t(value * 100LL);
sprintf(string, "%g %sB", rounded / 100.0, units[i]);
}
return string;
}
// #pragma mark -
SizeSlider::SizeSlider(BRect rect, const char* name, const char* label,
BMessage* message, int32 min, int32 max)
: BSlider(rect, name, label, message, min, max)
{
rgb_color color = ui_color(B_CONTROL_HIGHLIGHT_COLOR);
UseFillColor(true, &color);
}
SizeSlider::~SizeSlider()
{
}
char *
SizeSlider::UpdateText() const
{
fText = byte_string(Value() * kMegaByte);
return const_cast<char*>(fText.String());
}
// #pragma mark -
SettingsWindow::SettingsWindow()
: BWindow(BRect(0, 0, 269, 172), "VirtualMemory", B_TITLED_WINDOW,
B_NOT_RESIZABLE | B_ASYNCHRONOUS_CONTROLS | B_NOT_ZOOMABLE)
{
// fSettings = new Settings();
BRect rect = Bounds();
BView* view = new BView(rect, "background", B_FOLLOW_ALL, B_WILL_DRAW);
view->SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));
font_height fontHeight;
be_plain_font->GetHeight(&fontHeight);
float lineHeight = ceil(fontHeight.ascent + fontHeight.descent + fontHeight.ascent);
fSwapEnabledCheckBox = new BCheckBox(rect, "enable swap", "Enable Virtual Memory",
new BMessage(kMsgSwapEnabledUpdate));
fSwapEnabledCheckBox->SetValue(fSettings.SwapEnabled());
fSwapEnabledCheckBox->ResizeToPreferred();
rect.InsetBy(10, 10);
BBox* box = new BBox(rect, "box");
box->SetLabel(fSwapEnabledCheckBox);
view->AddChild(box);
system_info info;
get_system_info(&info);
rect.right -= 20;
rect.top = lineHeight;
BString string = "Physical Memory: ";
string << byte_string((off_t)info.max_pages * B_PAGE_SIZE);
BStringView* stringView = new BStringView(rect, "physical memory", string.String(),
B_FOLLOW_ALL);
stringView->ResizeToPreferred();
box->AddChild(stringView);
rect.OffsetBy(0, lineHeight);
string = "Current Swap File Size: ";
string << byte_string(fSettings.SwapSize());
stringView = new BStringView(rect, "current swap size", string.String(),
B_FOLLOW_ALL);
stringView->ResizeToPreferred();
box->AddChild(stringView);
BPopUpMenu* menu = new BPopUpMenu("volumes");
// collect volumes
// ToDo: listen to volume changes!
// ToDo: accept dropped volumes
BVolumeRoster volumeRoster;
BVolume volume;
while (volumeRoster.GetNextVolume(&volume) == B_OK) {
char name[B_FILE_NAME_LENGTH];
if (!volume.IsPersistent() || volume.GetName(name) != B_OK || !name[0])
continue;
BMenuItem* item = new BMenuItem(name, NULL);
menu->AddItem(item);
if (volume.Device() == fSettings.SwapVolume().Device())
item->SetMarked(true);
}
rect.OffsetBy(0, lineHeight);
BMenuField* field = new BMenuField(rect, "devices", "Use Volume:", menu);
field->SetDivider(field->StringWidth(field->Label()) + 8);
field->ResizeToPreferred();
field->SetEnabled(false);
box->AddChild(field);
off_t minSize, maxSize;
GetSwapFileLimits(minSize, maxSize);
rect.OffsetBy(0, lineHeight + 8);
fSizeSlider = new SizeSlider(rect, "size slider", "Requested Swap File Size:",
new BMessage(kMsgSliderUpdate), minSize / kMegaByte, maxSize / kMegaByte);
fSizeSlider->SetLimitLabels("999 MB", "999 MB");
fSizeSlider->ResizeToPreferred();
box->AddChild(fSizeSlider);
rect.OffsetBy(0, fSizeSlider->Frame().Height() + 5);
rect.bottom = rect.top + stringView->Frame().Height();
fWarningStringView = new BStringView(rect, "", "", B_FOLLOW_ALL);
fWarningStringView->SetAlignment(B_ALIGN_CENTER);
box->AddChild(fWarningStringView);
box->ResizeTo(box->Frame().Width(), fWarningStringView->Frame().bottom + 10);
// Add "Defaults" and "Revert" buttons
rect.top = box->Frame().bottom + 10;
BButton* button = new BButton(rect, "defaults", "Defaults", new BMessage(kMsgDefaults));
button->ResizeToPreferred();
view->AddChild(button);
rect = button->Frame();
rect.OffsetBy(rect.Width() + 10, 0);
fRevertButton = new BButton(rect, "revert", "Revert", new BMessage(kMsgRevert));
button->ResizeToPreferred();
view->AddChild(fRevertButton);
view->ResizeTo(view->Frame().Width(), button->Frame().bottom + 10);
ResizeTo(view->Bounds().Width(), view->Bounds().Height());
AddChild(view);
// add view after resizing the window, so that the view's resizing
// mode is not used (we already layed out the views for the new size)
Update();
BScreen screen;
BRect screenFrame = screen.Frame();
if (!screenFrame.Contains(fSettings.WindowPosition())) {
// move on screen, centered
MoveTo((screenFrame.Width() - Bounds().Width()) / 2,
(screenFrame.Height() - Bounds().Height()) / 2);
} else
MoveTo(fSettings.WindowPosition());
}
SettingsWindow::~SettingsWindow()
{
}
void
SettingsWindow::Update()
{
if ((fSwapEnabledCheckBox->Value() != 0) != fSettings.SwapEnabled())
fSwapEnabledCheckBox->SetValue(fSettings.SwapEnabled());
off_t minSize, maxSize;
GetSwapFileLimits(minSize, maxSize);
BString minLabel, maxLabel;
minLabel << byte_string(minSize);
maxLabel << byte_string(maxSize);
if (minLabel != fSizeSlider->MinLimitLabel()
|| maxLabel != fSizeSlider->MaxLimitLabel()) {
fSizeSlider->SetLimitLabels(minLabel.String(), maxLabel.String());
#ifdef __HAIKU__
fSizeSlider->SetLimits(minSize / kMegaByte, maxSize / kMegaByte);
#endif
}
if (fSizeSlider->Value() != fSettings.SwapSize() / kMegaByte)
fSizeSlider->SetValue(fSettings.SwapSize() / kMegaByte);
// ToDo: set volume
bool changed = fSettings.SwapChanged();
if (fRevertButton->IsEnabled() != changed) {
fRevertButton->SetEnabled(changed);
if (changed)
fWarningStringView->SetText("Changes will take effect on restart!");
else
fWarningStringView->SetText("");
}
}
void
SettingsWindow::GetSwapFileLimits(off_t& minSize, off_t& maxSize)
{
// minimum size is the installed memory
system_info info;
get_system_info(&info);
minSize = (off_t)info.max_pages * B_PAGE_SIZE;
// maximum size is the free space on the current volume
// (minus some safety offset, depending on the disk size)
off_t freeSpace = fSettings.SwapVolume().FreeBytes();
off_t safetyFreeSpace = fSettings.SwapVolume().Capacity() / 100;
if (safetyFreeSpace > 1024 * kMegaByte)
safetyFreeSpace = 1024 * kMegaByte;
// check if there already is a page file on this disk and
// adjust the free space accordingly
BPath path;
if (find_directory(B_COMMON_VAR_DIRECTORY, &path, false,
&fSettings.SwapVolume()) == B_OK) {
path.Append("swap");
BEntry swap(path.Path());
off_t size;
if (swap.GetSize(&size) == B_OK)
freeSpace += size;
}
maxSize = freeSpace - safetyFreeSpace;
// ToDo: we should issue some kind of warning here
if (maxSize < minSize)
maxSize = minSize;
}
void
SettingsWindow::MessageReceived(BMessage* message)
{
switch (message->what) {
case kMsgRevert:
fSettings.RevertSwapChanges();
Update();
break;
case kMsgDefaults:
fSettings.SetSwapDefaults();
Update();
break;
case kMsgSliderUpdate:
fSettings.SetSwapSize((off_t)fSizeSlider->Value() * kMegaByte);
Update();
break;
case kMsgSwapEnabledUpdate:
{
int32 value;
if (message->FindInt32("be:value", &value) != B_OK)
break;
if (value == 0) {
// print out warning, give the user the time to think about it :)
// ToDo: maybe we want to remove this possibility in the GUI
// as Be did, but I thought a proper warning could be helpful
// (for those that want to change that anyway)
int32 choice = (new BAlert("VirtualMemory",
"Disabling virtual memory will have unwanted effects on "
"system stability once the memory is used up.\n"
"Virtual memory does not affect system performance "
"until this point is reached.\n\n"
"Are you really sure you want to turn it off?",
"Turn Off", "Keep Enabled", NULL,
B_WIDTH_AS_USUAL, B_WARNING_ALERT))->Go();
if (choice == 1) {
fSwapEnabledCheckBox->SetValue(1);
break;
}
}
fSettings.SetSwapEnabled(value != 0);
Update();
break;
}
default:
BWindow::MessageReceived(message);
}
}
bool
SettingsWindow::QuitRequested()
{
fSettings.SetWindowPosition(Frame().LeftTop());
be_app->PostMessage(B_QUIT_REQUESTED);
return true;
}
@@ -0,0 +1,39 @@
/*
* Copyright 2005, Axel Dörfler, [email protected]. All rights reserved.
* Distributed under the terms of the MIT License.
*/
#ifndef SETTINGS_WINDOW_H
#define SETTINGS_WINDOW_H
#include <Window.h>
#include "Settings.h"
class BStringView;
class BCheckBox;
class BSlider;
class BButton;
class SettingsWindow : public BWindow {
public:
SettingsWindow();
virtual ~SettingsWindow();
virtual bool QuitRequested();
virtual void MessageReceived(BMessage* message);
private:
void Update();
void GetSwapFileLimits(off_t& minSize, off_t& maxSize);
BCheckBox* fSwapEnabledCheckBox;
BSlider* fSizeSlider;
BButton* fRevertButton;
BStringView* fWarningStringView;
Settings fSettings;
};
#endif /* SETTINGS_WINDOW_H */
@@ -1,69 +0,0 @@
#include "VMSettings.h"
#include <Application.h>
#include <File.h>
#include <FindDirectory.h>
#include <Path.h>
#include <stdio.h>
const char VMSettings::kVMSettingsFile[] = "VM_data";
VMSettings::VMSettings()
{//VMSettings::VMSettings
BPath path;
if (find_directory(B_USER_SETTINGS_DIRECTORY,&path) == B_OK)
{
path.Append(kVMSettingsFile);
BFile file(path.Path(), B_READ_ONLY);
if (file.InitCheck() != B_OK)
be_app->PostMessage(B_QUIT_REQUESTED);
// Now read in the data
if (file.Read(&fcorner, sizeof(BPoint)) != sizeof(BPoint))
be_app->PostMessage(B_QUIT_REQUESTED);
}
printf("VM settings file read.\n");
printf("=========================\n");
printf("fcorner read in as ");
fcorner.PrintToStream();
fWindowFrame.left = fcorner.x;
fWindowFrame.top = fcorner.y;
fWindowFrame.right = fWindowFrame.left+269;
fWindowFrame.bottom = fWindowFrame.top+172;
//Check to see if the co-ords of the window are in the range of the Screen
BScreen screen;
if (screen.Frame().right >= fWindowFrame.right
&& screen.Frame().bottom >= fWindowFrame.bottom)
return;
// If they are not, lets just stick the window in the middle
// of the screen.
fWindowFrame = screen.Frame();
fWindowFrame.left = (fWindowFrame.right -269)/2;
fWindowFrame.right = fWindowFrame.left + 269;
fWindowFrame.top = (fWindowFrame.bottom -172)/2;
fWindowFrame.bottom = fWindowFrame.top + 172;
}//VMSettings::VMSettings
VMSettings::~VMSettings()
{//VMSettings::~VMSettings
BPath path;
if (find_directory(B_USER_SETTINGS_DIRECTORY,&path) < B_OK)
return;
path.Append(kVMSettingsFile);
BFile file(path.Path(), B_WRITE_ONLY | B_CREATE_FILE);
if (file.InitCheck() == B_OK)
{
file.Write(&fcorner, sizeof(BPoint));
}
}//MouseSettings::~MouseSettings
void VMSettings::SetWindowPosition(BRect f)
{//VMSettings::SetWindowFrame
fcorner.x = f.left;
fcorner.y = f.top;
}//VMSettings::SetWindowFrame
@@ -1,20 +0,0 @@
#ifndef VM_SETTINGS_H_
#define VM_SETTINGS_H_
#include <Screen.h>
#include <SupportDefs.h>
class VMSettings{
public :
VMSettings();
virtual ~VMSettings();
BRect WindowPosition() const { return fWindowFrame; }
void SetWindowPosition(BRect);
private:
static const char kVMSettingsFile[];
BRect fWindowFrame;
BPoint fcorner;
};
#endif
@@ -0,0 +1,60 @@
/*
* Copyright 2005, Axel Dörfler, [email protected]. All rights reserved.
* Distributed under the terms of the MIT License.
*/
#include "VirtualMemory.h"
#include "SettingsWindow.h"
#include <Alert.h>
#include <TextView.h>
VirtualMemory::VirtualMemory()
: BApplication("application/x-vnd.Haiku-VirtualMemory")
{
}
VirtualMemory::~VirtualMemory()
{
}
void
VirtualMemory::ReadyToRun()
{
BWindow* window = new SettingsWindow();
window->Show();
}
void
VirtualMemory::AboutRequested()
{
BAlert *alert = new BAlert("about", "VirtualMemory\n"
"\twritten by Axel Dörfler\n"
"\tCopyright 2005, Haiku.\n", "Ok");
BTextView *view = alert->TextView();
BFont font;
view->SetStylable(true);
view->GetFont(&font);
font.SetSize(18);
font.SetFace(B_BOLD_FACE);
view->SetFontAndColor(0, 13, &font);
alert->Go();
}
int
main(int argc, char** argv)
{
VirtualMemory app;
app.Run();
return 0;
}
@@ -0,0 +1,28 @@
/*
* Copyright 2005, Axel Dörfler, [email protected]. All rights reserved.
* Distributed under the terms of the MIT License.
*/
#ifndef VIRTUAL_MEMORY_H
#define VIRTUAL_MEMORY_H
#include <Application.h>
class VMSettings;
class VirtualMemory : public BApplication {
public:
VirtualMemory();
virtual ~VirtualMemory();
virtual void ReadyToRun();
virtual void AboutRequested();
private:
void GetCurrentSettings(bool& enabled, off_t& size);
VMSettings *fSettings;
};
#endif /* VIRTUAL_MEMORY_H */
-109
View File
@@ -1,109 +0,0 @@
/*! \file main.cpp
* \brief Code for the main class.
*
* This file contains the code for the main class. This class sets up all
* of the initial conditions for the app.
*
*/
#include "MainWindow.h"
#include "main.h"
#include <iostream>
using namespace std;
#define MEGABITE 1048576
/**
* Main method.
*
* Starts the whole thing.
*/
int main(int, char**){
/**
* An instance of the application.
*/
new VM_pref();
be_app->Run();
delete be_app;
return 0;
}
/*
* Constructor.
*
* Provides a contstructor for the application.
*/
VM_pref::VM_pref()
:BApplication("application/x-vnd.Haiku-MEM$") {
// read current swap settings
FILE *settingsFile = fopen("/boot/home/config/settings/kernel/drivers/virtual_memory", "r");
char dummy[80];
int physMem; //The amount of physical memory in the machine.
int currSwap; //The current size of the swap file.
int setSwap; //The set size of the swap file.
double minSwap; //The minimum size the swap file can be.
int maxSwap; //The maximum size the swap file can be.
const char *swap_file= "/boot/var/swap";
BEntry swap(swap_file);
off_t swapsize;
swap.GetSize(&swapsize);
currSwap = swapsize / MEGABITE;
system_info info;
get_system_info(&info);
physMem = (info.max_pages * 4096) / MEGABITE;
float memcalc = (physMem +(int)(physMem/3.0));
cout << memcalc << endl;
modf(memcalc/128.0, &minSwap);
cout << minSwap << endl;
minSwap*= 128;
if (settingsFile != NULL) {
fscanf(settingsFile, "%s %s\n", dummy, dummy);
fscanf(settingsFile, "%s %d\n", dummy, &setSwap);
setSwap = setSwap / MEGABITE;
} else {
setSwap = (int)minSwap;
}
fclose(settingsFile);
BVolume bootVol;
BVolumeRoster *vol_rost = new BVolumeRoster();
vol_rost->GetBootVolume(&bootVol);
/* maxSwap is defined by the amount of free space on your boot
* volume, plus the current swap file size, minus an arbitrary
* amount of space, just so you don't fill up your drive completly.
*/
maxSwap = (bootVol.FreeBytes() / MEGABITE) + currSwap - 16;
bool changeMsg = false;
if (currSwap != setSwap) {
currSwap = setSwap;
changeMsg = true;
}
fSettings = new VMSettings();
window = new MainWindow(fSettings->WindowPosition(), physMem, currSwap, minSwap, maxSwap, fSettings);
if (changeMsg) {
window->toggleChangedMessage(true);
}
}
VM_pref::~VM_pref()
{
delete fSettings;
}
void
VM_pref::ReadyToRun()
{
window->Show();
}
-40
View File
@@ -1,40 +0,0 @@
/*! \file main.h
\brief Header file for the main class.
*/
#ifndef MAIN_H
#define MAIN_H
#include <Application.h>
#include <Volume.h>
#include <VolumeRoster.h>
#include <stdio.h>
#include "VMSettings.h"
#include <OS.h>
#include <Entry.h>
class MainWindow;
/**
* Main class.
*
* Gets everything going.
*/
class VM_pref : public BApplication{
public:
/**
* Constructor.
*/
VM_pref();
/**
* Destructor.
*/
virtual ~VM_pref();
virtual void ReadyToRun();
private:
VMSettings *fSettings;
MainWindow *window;
};
#endif