Skip to content

Commit

Permalink
query (misc): use systemInfo_t for multiple disk, gpu, theme queries
Browse files Browse the repository at this point in the history
this is a fix that I saw happening in the android widget, where when trying to display $<disk(/)> and $<disk(/storage/emulated)>, they both displayed the same statuses from the latest disk query, because the global std::vector queried infos weren't cleaned.

also I made this because the code was kinda ugly with std::vector
  • Loading branch information
Toni500github committed Dec 15, 2024
1 parent 5a1686c commit 80e92c5
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 74 deletions.
35 changes: 16 additions & 19 deletions include/query.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
#include <string>
#include <unordered_map>
#include <variant>
#include <vector>

#include "config.hpp"
#include "util.hpp"
Expand Down Expand Up @@ -153,23 +152,21 @@ class Theme
std::string cursor_size{ UNKNOWN };
};

Theme(const std::uint8_t ver, systemInfo_t& queried_themes, std::vector<std::string>& queried_themes_names,
const std::string& theme_name_version, const Config& config, const bool gsettings_only = false);
Theme(const std::uint8_t ver, systemInfo_t& queried_themes, const std::string& theme_name_version,
const Config& config, const bool gsettings_only = false);

Theme(systemInfo_t& queried_themes, const Config& config, const bool gsettings_only = false);
Theme(const Config& config, const bool gsettings_only = false);

std::string gtk_theme() noexcept;
std::string gtk_icon_theme() noexcept;
std::string gtk_font() noexcept;
std::string& gtk_theme() noexcept;
std::string& gtk_icon_theme() noexcept;
std::string& gtk_font() noexcept;
std::string& cursor() noexcept;
std::string& cursor_size() noexcept;

private:
User query_user;
static Theme_t m_theme_infos;
systemInfo_t& m_queried_themes;
const std::string m_theme_name_version;
std::string m_wmde_name;
User query_user;
std::string m_wmde_name;
static Theme_t m_theme_infos;
};

class CPU
Expand Down Expand Up @@ -221,18 +218,18 @@ class GPU
std::string vendor{ UNKNOWN };
};

GPU(const std::uint16_t id, std::vector<std::uint16_t>& queried_gpus);
GPU(const std::string& id, systemInfo_t& queried_gpus);

std::string& name() noexcept;
std::string& vendor() noexcept;

private:
uint16_t m_vendor_id;
uint16_t m_device_id;
std::string m_vendor_id_s;
std::string m_device_id_s;
uint16_t m_vendor_id;
uint16_t m_device_id;
std::string m_vendor_id_s;
std::string m_device_id_s;

static GPU_t m_gpu_infos;
static GPU_t m_gpu_infos;
};

class Disk
Expand All @@ -248,7 +245,7 @@ class Disk
std::string mountdir;
};

Disk(const std::string_view path, std::vector<std::string>& paths);
Disk(const std::string& path, systemInfo_t& queried_paths);

double& total_amount() noexcept;
double& free_amount() noexcept;
Expand Down
30 changes: 14 additions & 16 deletions src/parse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1080,10 +1080,10 @@ static std::string prettify_de_name(const std::string_view de_name)
return de_name.data();
}

std::vector<std::uint16_t> queried_gpus;
std::vector<std::string> queried_disks;
std::vector<std::string> queried_themes_names;
systemInfo_t queried_themes;
systemInfo_t queried_gpus;
systemInfo_t queried_disks;
systemInfo_t queried_themes_names;
systemInfo_t queried_themes;

// clang-format on
void addValueFromModuleMember(const std::string& moduleName, const std::string& moduleMemberName, parse_args_t& parse_args)
Expand Down Expand Up @@ -1251,7 +1251,7 @@ void addValueFromModuleMember(const std::string& moduleName, const std::string&

else if (moduleName == "theme")
{
Query::Theme query_theme(queried_themes, config, false);
Query::Theme query_theme(config, false);

if (sysInfo.find(moduleName) == sysInfo.end())
sysInfo.insert({ moduleName, {} });
Expand Down Expand Up @@ -1281,7 +1281,7 @@ void addValueFromModuleMember(const std::string& moduleName, const std::string&
{
if (hasStart(moduleMemberName, "cursor"))
{
Query::Theme query_cursor(queried_themes, config, true);
Query::Theme query_cursor(config, true);
switch (moduleMember_hash)
{
case "cursor"_fnv1a16:
Expand All @@ -1296,7 +1296,7 @@ void addValueFromModuleMember(const std::string& moduleName, const std::string&
}
else
{
Query::Theme query_theme(0, queried_themes, queried_themes_names, "gsettings", config, true);
Query::Theme query_theme(0, queried_themes, "gsettings", config, true);
switch (moduleMember_hash)
{
case "name"_fnv1a16: SYSINFO_INSERT(query_theme.gtk_theme()); break;
Expand All @@ -1310,9 +1310,9 @@ void addValueFromModuleMember(const std::string& moduleName, const std::string&
// clang-format off
else if (moduleName == "theme-gtk-all")
{
Query::Theme gtk2(2, queried_themes, queried_themes_names, "gtk2", config);
Query::Theme gtk3(3, queried_themes, queried_themes_names, "gtk3", config);
Query::Theme gtk4(4, queried_themes, queried_themes_names, "gtk4", config);
Query::Theme gtk2(2, queried_themes, "gtk2", config);
Query::Theme gtk3(3, queried_themes, "gtk3", config);
Query::Theme gtk4(4, queried_themes, "gtk4", config);

if (sysInfo.find(moduleName) == sysInfo.end())
sysInfo.insert({ moduleName, {} });
Expand All @@ -1338,7 +1338,7 @@ void addValueFromModuleMember(const std::string& moduleName, const std::string&
"Syntax should be like 'theme_gtkN' which N stands for the version of gtk to query (single number)",
moduleName);

Query::Theme query_theme(ver, queried_themes, queried_themes_names, fmt::format("gtk{}", ver), config);
Query::Theme query_theme(ver, queried_themes, fmt::format("gtk{}", ver), config);

if (sysInfo.find(moduleName) == sysInfo.end())
sysInfo.insert({ moduleName, {} });
Expand Down Expand Up @@ -1387,8 +1387,7 @@ void addValueFromModuleMember(const std::string& moduleName, const std::string&

else if (hasStart(moduleName, "gpu"))
{
const std::uint16_t id =
static_cast<std::uint16_t>(moduleName.length() > 3 ? std::stoi(std::string(moduleName).substr(3)) : 0);
const std::string& id = moduleName.length() > 3 ? moduleName.substr(3) : "0";

Query::GPU query_gpu(id, queried_gpus);

Expand Down Expand Up @@ -1417,7 +1416,7 @@ void addValueFromModuleMember(const std::string& moduleName, const std::string&
TOTAL,
FREE
};
std::string path = moduleName.data();
std::string path{moduleName.data()};
path.erase(0, 5); // disk(
path.pop_back(); // )
debug("disk path = {}", path);
Expand Down Expand Up @@ -1652,8 +1651,7 @@ void addValueFromModule(const std::string& moduleName, parse_args_t& parse_args)

else if (hasStart(moduleName, "gpu"))
{
const std::uint16_t id =
static_cast<std::uint16_t>(moduleName.length() > 3 ? std::stoi(std::string(moduleName).substr(3)) : 0);
const std::string& id = (moduleName.length() > 3 ? moduleName.substr(3) : "0");

if (sysInfo.find(moduleName) == sysInfo.end())
sysInfo.insert({ moduleName, {} });
Expand Down
2 changes: 1 addition & 1 deletion src/query/android/gpu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ static std::string detect_adreno(const std::string& cpu_model_name)
return MAGIC_LINE;
}

GPU::GPU(const std::uint16_t id, std::vector<std::uint16_t>& queried_gpus)
GPU::GPU(const std::string& id, systemInfo_t& queried_gpus)
{
CPU query_cpu;
if (query_cpu.vendor() == "QUALCOMM" || query_cpu.vendor() == "QTI")
Expand Down
12 changes: 5 additions & 7 deletions src/query/android/theme.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,29 +31,27 @@

using namespace Query;

Theme::Theme(const std::uint8_t ver, systemInfo_t& queried_themes, std::vector<std::string>& queried_themes_names,
Theme::Theme(const std::uint8_t ver, systemInfo_t& queried_themes,
const std::string& theme_name_version, const Config& config, const bool gsettings_only)
: m_queried_themes(queried_themes), m_theme_name_version(theme_name_version)
{
m_theme_infos.cursor = m_theme_infos.gtk_font = m_theme_infos.cursor_size = m_theme_infos.gtk_theme_name =
m_theme_infos.gtk_icon_theme = MAGIC_LINE;
}

Theme::Theme(systemInfo_t& queried_themes, const Config& config, const bool gsettings_only)
: m_queried_themes(queried_themes)
Theme::Theme(const Config& config, const bool gsettings_only)
{
m_theme_infos.cursor = m_theme_infos.gtk_font = m_theme_infos.cursor_size = m_theme_infos.gtk_theme_name =
m_theme_infos.gtk_icon_theme = MAGIC_LINE;
}

// clang-format off
std::string Theme::gtk_theme() noexcept
std::string& Theme::gtk_theme() noexcept
{ return m_theme_infos.gtk_theme_name; }

std::string Theme::gtk_icon_theme() noexcept
std::string& Theme::gtk_icon_theme() noexcept
{ return m_theme_infos.gtk_icon_theme; }

std::string Theme::gtk_font() noexcept
std::string& Theme::gtk_font() noexcept
{ return m_theme_infos.gtk_font; }

std::string& Theme::cursor() noexcept
Expand Down
33 changes: 24 additions & 9 deletions src/query/unix/disk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,25 +24,31 @@
*/

#include <mntent.h>
#include <algorithm>
#include <cstdio>
#include <filesystem>

#include "query.hpp"
#include "util.hpp"
#include "parse.hpp"

using namespace Query;

Disk::Disk(const std::string_view path, std::vector<std::string>& paths)
Disk::Disk(const std::string& path, systemInfo_t& queried_paths)
{
if (std::find(paths.begin(), paths.end(), path) == paths.end())
paths.push_back(path.data());
else
if (queried_paths.find(path) != queried_paths.end())
{
m_disk_infos.device = getInfoFromName(queried_paths, path, "device");
m_disk_infos.mountdir = getInfoFromName(queried_paths, path, "mountdir");
m_disk_infos.typefs = getInfoFromName(queried_paths, path, "typefs");
m_disk_infos.total_amount = std::stod(getInfoFromName(queried_paths, path, "total_amount"));
m_disk_infos.used_amount = std::stod(getInfoFromName(queried_paths, path, "used_amount"));
m_disk_infos.free_amount = std::stod(getInfoFromName(queried_paths, path, "free_amount"));
return;
}

if (!std::filesystem::exists(path))
{
// if user is using disk.disk or disk.fs
// if user is using $<disk(path)> or $<disk(path).fs>
// then let's just "try" to remove it
m_disk_infos.typefs = MAGIC_LINE;
m_disk_infos.device = MAGIC_LINE;
Expand Down Expand Up @@ -71,9 +77,9 @@ Disk::Disk(const std::string_view path, std::vector<std::string>& paths)
}
}

const std::string_view statpath = (hasStart(path, "/dev") ? pDevice->mnt_dir : path);
const std::string& statpath = (hasStart(path, "/dev") ? pDevice->mnt_dir : path);

if (statvfs(statpath.data(), &m_statvfs) != 0)
if (statvfs(statpath.c_str(), &m_statvfs) != 0)
{
perror("statvfs");
error("Failed to get disk info");
Expand All @@ -85,7 +91,16 @@ Disk::Disk(const std::string_view path, std::vector<std::string>& paths)
m_disk_infos.used_amount = m_disk_infos.total_amount - m_disk_infos.free_amount;

endmntent(mountsFile);

queried_paths.insert(
{path, {
{"total_amount", variant(m_disk_infos.total_amount)},
{"used_amount", variant(m_disk_infos.used_amount)},
{"free_amount", variant(m_disk_infos.free_amount)},
{"typefs", variant(m_disk_infos.typefs)},
{"mountdir", variant(m_disk_infos.mountdir)},
{"device", variant(m_disk_infos.device)}
}}
);
}

// clang-format off
Expand Down
21 changes: 15 additions & 6 deletions src/query/unix/gpu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@
*
*/

#include "fmt/format.h"
#include "parse.hpp"
#include "platform.hpp"
#if CF_UNIX

#include <algorithm>
#include <cstdint>
#include <filesystem>
#include <string>
Expand Down Expand Up @@ -75,15 +76,17 @@ static GPU::GPU_t get_gpu_infos(const std::string_view m_vendor_id_s, const std:
return ret;
}

GPU::GPU(const std::uint16_t id, std::vector<std::uint16_t>& queried_gpus)
GPU::GPU(const std::string& id, systemInfo_t& queried_gpus)
{
if (std::find(queried_gpus.begin(), queried_gpus.end(), id) == queried_gpus.end())
queried_gpus.push_back(id);
else
if (queried_gpus.find(id) != queried_gpus.end())
{
m_gpu_infos.name = getInfoFromName(queried_gpus, id, "name");
m_gpu_infos.vendor = getInfoFromName(queried_gpus, id, "vendor");
return;
}

const std::uint16_t max_iter = 10;
std::uint16_t id_iter = id;
std::uint16_t id_iter = std::stoi(id);
std::string sys_path;
int i = 0;
for (; i <= max_iter; i++)
Expand All @@ -106,6 +109,12 @@ GPU::GPU(const std::uint16_t id, std::vector<std::uint16_t>& queried_gpus)
m_device_id_s = read_by_syspath(sys_path + "/device/device");

m_gpu_infos = get_gpu_infos(m_vendor_id_s, m_device_id_s);
queried_gpus.insert(
{id, {
{"name", variant(m_gpu_infos.name)},
{"vendor", variant(m_gpu_infos.vendor)},
}}
);
}

// clang-format off
Expand Down
32 changes: 16 additions & 16 deletions src/query/unix/theme.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -476,16 +476,16 @@ static void get_gtk_theme(const bool dont_query_dewm, const std::uint8_t ver, co
}

// clang-format off
Theme::Theme(const std::uint8_t ver, systemInfo_t& queried_themes, std::vector<std::string>& queried_themes_names,
Theme::Theme(const std::uint8_t ver, systemInfo_t& queried_themes,
const std::string& theme_name_version, const Config& config, const bool gsettings_only)
: m_queried_themes(queried_themes),
m_theme_name_version(theme_name_version)
{
if (std::find(queried_themes_names.begin(), queried_themes_names.end(), m_theme_name_version)
== queried_themes_names.end())
queried_themes_names.push_back(m_theme_name_version);
else
if (queried_themes.find(theme_name_version) != queried_themes.end())
{
m_theme_infos.gtk_theme_name = getInfoFromName(queried_themes, theme_name_version, "theme-name");
m_theme_infos.gtk_font = getInfoFromName(queried_themes, theme_name_version, "icon-theme-name");
m_theme_infos.gtk_icon_theme = getInfoFromName(queried_themes, theme_name_version, "font-name");
return;
}

const std::string& wm_name = query_user.wm_name(query_user.m_bDont_query_dewm, query_user.term_name());
const std::string& de_name = query_user.de_name(query_user.m_bDont_query_dewm, query_user.term_name(), wm_name);
Expand All @@ -507,8 +507,8 @@ Theme::Theme(const std::uint8_t ver, systemInfo_t& queried_themes, std::vector<s
if (m_theme_infos.gtk_icon_theme.empty())
m_theme_infos.gtk_icon_theme = MAGIC_LINE;

m_queried_themes.insert(
{m_theme_name_version, {
queried_themes.insert(
{theme_name_version, {
{"theme-name", variant(m_theme_infos.gtk_theme_name)},
{"icon-theme-name", variant(m_theme_infos.gtk_icon_theme)},
{"font-name", variant(m_theme_infos.gtk_font)},
Expand All @@ -517,7 +517,7 @@ Theme::Theme(const std::uint8_t ver, systemInfo_t& queried_themes, std::vector<s
}

// only use it for cursor
Theme::Theme(systemInfo_t& queried_themes, const Config& config, const bool gsettings_only) : m_queried_themes(queried_themes)
Theme::Theme(const Config& config, const bool gsettings_only)
{
const std::string& wm_name = query_user.wm_name(query_user.m_bDont_query_dewm, query_user.term_name());
const std::string& de_name = query_user.de_name(query_user.m_bDont_query_dewm, query_user.term_name(), wm_name);
Expand Down Expand Up @@ -551,14 +551,14 @@ Theme::Theme(systemInfo_t& queried_themes, const Config& config, const bool gset

}

std::string Theme::gtk_theme() noexcept
{ return getInfoFromName(m_queried_themes, m_theme_name_version, "theme-name"); }
std::string& Theme::gtk_theme() noexcept
{ return m_theme_infos.gtk_theme_name; }

std::string Theme::gtk_icon_theme() noexcept
{ return getInfoFromName(m_queried_themes, m_theme_name_version, "icon-theme-name"); }
std::string& Theme::gtk_icon_theme() noexcept
{ return m_theme_infos.gtk_icon_theme; }

std::string Theme::gtk_font() noexcept
{ return getInfoFromName(m_queried_themes, m_theme_name_version, "font-name"); }
std::string& Theme::gtk_font() noexcept
{ return m_theme_infos.gtk_font; }

std::string& Theme::cursor() noexcept
{ return m_theme_infos.cursor; }
Expand Down

0 comments on commit 80e92c5

Please sign in to comment.