Skip to content

Commit

Permalink
query: gpu: separate vendor from name + add vendor_long
Browse files Browse the repository at this point in the history
  • Loading branch information
Toni500github committed Sep 15, 2024
1 parent bc0b47a commit b2f2887
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 26 deletions.
2 changes: 1 addition & 1 deletion include/config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ layout = [
"${auto}Disk (/): $<disk(/).disk>",
"${auto}Swap: $<swap.swap>",
"${auto}CPU: $<cpu.cpu>",
"${auto}GPU: $<gpu.name>",
"${auto}GPU: $<gpu.vendor> $<gpu.name>",
"${auto}RAM: $<ram.ram>",
"",
"$<builtin.colors_bg>", # normal colors
Expand Down
2 changes: 1 addition & 1 deletion include/query.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ class GPU
std::string vendor{ UNKNOWN };
};

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

std::string& name() noexcept;
std::string& vendor() noexcept;
Expand Down
3 changes: 2 additions & 1 deletion include/util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ std::string str_toupper(std::string str);
void strip(std::string& input);
std::string read_by_syspath(const std::string_view path);
fmt::rgb hexStringToColor(const std::string_view hexstr);
void shorten_vendor_name(std::string& vendor);
void shorten_vendor_name_inplace(std::string& vendor);
std::string shorten_vendor_name(std::string vendor);
std::string getHomeConfigDir();
std::string getConfigDir();
std::vector<std::string> split(const std::string_view text, char delim);
Expand Down
7 changes: 4 additions & 3 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -190,13 +190,14 @@ disk(/path/to/fs)
device : path to device [/dev/sda5]
mountdir : path to the device mount point [/]
# usually people have 1 GPU in their host,
# usually people have 1 GPU in their PC,
# but if you got more than 1 and want to query it,
# you should call gpu module with a number, e.g gpu1 (default gpu0).
# Infos are gotten from `/sys/class/drm/` and on each cardN directory
gpu
name : GPU model name [NVIDIA GeForce GTX 1650]
vendor : GPU vendor [NVIDIA Corporation]
name : GPU model name [GeForce GTX 1650]
vendor : GPU short vendor name [NVIDIA]
vendor_long : GPU vendor name [NVIDIA Corporation]
cpu
cpu : CPU model name with number of virtual proccessors and max freq [AMD Ryzen 5 5500 (12) @ 4.90 GHz]
Expand Down
16 changes: 11 additions & 5 deletions src/parse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -971,7 +971,7 @@ void addValueFromModule(systemInfo_t& sysInfo, const std::string& moduleName, co

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

Query::GPU query_gpu(id, queried_gpus);
Expand All @@ -981,11 +981,17 @@ void addValueFromModule(systemInfo_t& sysInfo, const std::string& moduleName, co

if (sysInfo[moduleName].find(moduleMemberName) == sysInfo[moduleName].end())
{
if (moduleMemberName == "name")
SYSINFO_INSERT(query_gpu.name());
switch (moduleMember_hash)
{
case "name"_fnv1a16:
SYSINFO_INSERT(query_gpu.name()); break;

else if (moduleMemberName == "vendor")
SYSINFO_INSERT(query_gpu.vendor());
case "vendor"_fnv1a16:
SYSINFO_INSERT(shorten_vendor_name(query_gpu.vendor())); break;

case "vendor_long"_fnv1a16:
SYSINFO_INSERT(query_gpu.vendor()); break;
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/query/unix/gpu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ static GPU::GPU_t get_gpu_infos(const std::string_view m_vendor_id_s, const std:
return ret;
}

GPU::GPU(std::uint16_t& id, std::vector<std::uint16_t>& queried_gpus)
GPU::GPU(const std::uint16_t id, std::vector<std::uint16_t>& queried_gpus)
{
if (std::find(queried_gpus.begin(), queried_gpus.end(), id) == queried_gpus.end())
queried_gpus.push_back(id);
Expand Down
32 changes: 18 additions & 14 deletions src/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -211,14 +211,21 @@ void getFileValue(u_short& iterIndex, const std::string_view line, std::string&
iterIndex++;
}

void shorten_vendor_name(std::string& vendor)
void shorten_vendor_name_inplace(std::string& vendor)
{
if (vendor == "Advanced Micro Devices, Inc.")
if (vendor.find("AMD") != vendor.npos ||
vendor.find("Advanced Micro") != vendor.npos)
vendor = "AMD";
else if (vendor == "Intel Corporation")
vendor = "Intel";
else if (vendor == "NVIDIA Corporation")
vendor = "NVIDIA";

size_t pos = 0;
if ((pos = vendor.rfind("Corporation")) != vendor.npos)
vendor.erase(pos - 1);
}

std::string shorten_vendor_name(std::string vendor)
{
shorten_vendor_name_inplace(vendor);
return vendor;
}

fmt::rgb hexStringToColor(const std::string_view hexstr)
Expand Down Expand Up @@ -413,7 +420,7 @@ std::string binarySearchPCIArray(const std::string_view vendor_id_s, const std::
// Here we use find from vendors_location because as it turns out, lower_bound doesn't return WHERE it is, but where
// "val" can be placed without affecting the order of the string (binary search stuff) so we have to find from the
// point onwards to find the actual line, it is still a shortcut, better than searching from 0.
return vendor_from_entry(vendor_location, vendor_id) + " " + name_from_entry(device_location);
return name_from_entry(device_location);
}

// Function to perform binary search on the pci vendors array to find a vendor.
Expand Down Expand Up @@ -465,6 +472,7 @@ std::string shell_exec(const std::string_view cmd)
if (!pipe)
die("popen() failed: {}", std::strerror(errno));

result.reserve(buffer.size());
while (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr)
result += buffer.data();

Expand All @@ -480,8 +488,8 @@ std::string name_from_entry(size_t dev_entry_pos)

std::string name = all_ids.substr(dev_entry_pos, all_ids.find('\n', dev_entry_pos) - dev_entry_pos);

size_t bracket_open_pos = name.find('[');
size_t bracket_close_pos = name.find(']');
const size_t bracket_open_pos = name.find('[');
const size_t bracket_close_pos = name.find(']');
if (bracket_open_pos != std::string::npos && bracket_close_pos != std::string::npos)
name = name.substr(bracket_open_pos + 1, bracket_close_pos - bracket_open_pos - 1);

Expand All @@ -505,11 +513,7 @@ std::string vendor_from_entry(const size_t vendor_entry_pos, const std::string_v

const size_t last = description.find_last_not_of(' ');

std::string vendor = description.substr(first, (last - first + 1));

shorten_vendor_name(vendor);

return vendor;
return description.substr(first, (last - first + 1));
}

// clang-format off
Expand Down

0 comments on commit b2f2887

Please sign in to comment.