Skip to content

Commit

Permalink
Pass Machine pointer to Settings_new instead of internal detail
Browse files Browse the repository at this point in the history
Be more flexible and consistent with the way other structures
are initialised by passing a pointer to struct Machine into the
Settings constructor.  This would allow other alternative setup
of default Meters in the future, e.g. for some nvtop-alike tool
sharing code with htop (hypothetically) showing GPUs instead of
CPUs by default.  Mainly it just makes the code easier to read.
  • Loading branch information
natoscott authored and BenBE committed Sep 4, 2024
1 parent f3c3ac7 commit f3306db
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 10 deletions.
2 changes: 1 addition & 1 deletion CommandLine.c
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ int CommandLine_run(int argc, char** argv) {

Machine* host = Machine_new(ut, flags.userId);
ProcessTable* pt = ProcessTable_new(host, flags.pidMatchList);
Settings* settings = Settings_new(host->activeCPUs, dm, dc, ds);
Settings* settings = Settings_new(host, dm, dc, ds);
Machine_populateTablesFromSettings(host, settings, &pt->super);

Header* header = Header_new(host, 2);
Expand Down
17 changes: 9 additions & 8 deletions Settings.c
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,8 @@ static bool Settings_validateMeters(Settings* this) {
return anyMeter;
}

static void Settings_defaultMeters(Settings* this, unsigned int initialCpuCount) {
static void Settings_defaultMeters(Settings* this, const Machine* host) {
unsigned int initialCpuCount = host->activeCPUs;
int sizes[] = { 3, 3 };

if (initialCpuCount > 4 && initialCpuCount <= 128) {
Expand Down Expand Up @@ -357,7 +358,7 @@ static ScreenSettings* Settings_defaultScreens(Settings* this) {
return this->screens[0];
}

static bool Settings_read(Settings* this, const char* fileName, unsigned int initialCpuCount, bool checkWritability) {
static bool Settings_read(Settings* this, const char* fileName, const Machine* host, bool checkWritability) {
int fd = -1;
const char* fopen_mode = "r+";
if (checkWritability) {
Expand Down Expand Up @@ -595,7 +596,7 @@ static bool Settings_read(Settings* this, const char* fileName, unsigned int ini
}
fclose(fp);
if (!didReadMeters || !Settings_validateMeters(this))
Settings_defaultMeters(this, initialCpuCount);
Settings_defaultMeters(this, host);
if (!this->nScreens)
Settings_defaultScreens(this);
return didReadAny;
Expand Down Expand Up @@ -814,7 +815,7 @@ int Settings_write(const Settings* this, bool onCrash) {
return r;
}

Settings* Settings_new(unsigned int initialCpuCount, Hashtable* dynamicMeters, Hashtable* dynamicColumns, Hashtable* dynamicScreens) {
Settings* Settings_new(const Machine* host, Hashtable* dynamicMeters, Hashtable* dynamicColumns, Hashtable* dynamicScreens) {
Settings* this = xCalloc(1, sizeof(Settings));

this->writeConfig = true;
Expand Down Expand Up @@ -900,9 +901,9 @@ Settings* Settings_new(unsigned int initialCpuCount, Hashtable* dynamicMeters, H
this->changed = false;
this->delay = DEFAULT_DELAY;

bool ok = Settings_read(this, this->filename, initialCpuCount, /*checkWritability*/true);
bool ok = Settings_read(this, this->filename, host, /*checkWritability*/true);
if (!ok && legacyDotfile) {
ok = Settings_read(this, legacyDotfile, initialCpuCount, this->writeConfig);
ok = Settings_read(this, legacyDotfile, host, this->writeConfig);
if (ok && this->writeConfig) {
// Transition to new location and delete old configuration file
if (Settings_write(this, false) == 0) {
Expand All @@ -914,10 +915,10 @@ Settings* Settings_new(unsigned int initialCpuCount, Hashtable* dynamicMeters, H
this->screenTabs = true;
this->changed = true;

ok = Settings_read(this, SYSCONFDIR "/htoprc", initialCpuCount, /*checkWritability*/false);
ok = Settings_read(this, SYSCONFDIR "/htoprc", host, /*checkWritability*/false);
}
if (!ok) {
Settings_defaultMeters(this, initialCpuCount);
Settings_defaultMeters(this, host);
Settings_defaultScreens(this);
}

Expand Down
3 changes: 2 additions & 1 deletion Settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ in the source distribution for its full text.
#define CONFIG_READER_MIN_VERSION 3

struct DynamicScreen_; // IWYU pragma: keep
struct Machine_; // IWYU pragma: keep
struct Table_; // IWYU pragma: keep

typedef struct {
Expand Down Expand Up @@ -128,7 +129,7 @@ void Settings_delete(Settings* this);

int Settings_write(const Settings* this, bool onCrash);

Settings* Settings_new(unsigned int initialCpuCount, Hashtable* dynamicMeters, Hashtable* dynamicColumns, Hashtable* dynamicScreens);
Settings* Settings_new(const struct Machine_* host, Hashtable* dynamicMeters, Hashtable* dynamicColumns, Hashtable* dynamicScreens);

ScreenSettings* Settings_newScreen(Settings* this, const ScreenDefaults* defaults);

Expand Down

0 comments on commit f3306db

Please sign in to comment.