Skip to content

Commit 80e92c5

Browse files
committed
query (misc): use systemInfo_t for multiple disk, gpu, theme queries
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
1 parent 5a1686c commit 80e92c5

File tree

7 files changed

+91
-74
lines changed

7 files changed

+91
-74
lines changed

include/query.hpp

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
#include <string>
3232
#include <unordered_map>
3333
#include <variant>
34-
#include <vector>
3534

3635
#include "config.hpp"
3736
#include "util.hpp"
@@ -153,23 +152,21 @@ class Theme
153152
std::string cursor_size{ UNKNOWN };
154153
};
155154

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

159-
Theme(systemInfo_t& queried_themes, const Config& config, const bool gsettings_only = false);
158+
Theme(const Config& config, const bool gsettings_only = false);
160159

161-
std::string gtk_theme() noexcept;
162-
std::string gtk_icon_theme() noexcept;
163-
std::string gtk_font() noexcept;
160+
std::string& gtk_theme() noexcept;
161+
std::string& gtk_icon_theme() noexcept;
162+
std::string& gtk_font() noexcept;
164163
std::string& cursor() noexcept;
165164
std::string& cursor_size() noexcept;
166165

167166
private:
168-
User query_user;
169-
static Theme_t m_theme_infos;
170-
systemInfo_t& m_queried_themes;
171-
const std::string m_theme_name_version;
172-
std::string m_wmde_name;
167+
User query_user;
168+
std::string m_wmde_name;
169+
static Theme_t m_theme_infos;
173170
};
174171

175172
class CPU
@@ -221,18 +218,18 @@ class GPU
221218
std::string vendor{ UNKNOWN };
222219
};
223220

224-
GPU(const std::uint16_t id, std::vector<std::uint16_t>& queried_gpus);
221+
GPU(const std::string& id, systemInfo_t& queried_gpus);
225222

226223
std::string& name() noexcept;
227224
std::string& vendor() noexcept;
228225

229226
private:
230-
uint16_t m_vendor_id;
231-
uint16_t m_device_id;
232-
std::string m_vendor_id_s;
233-
std::string m_device_id_s;
227+
uint16_t m_vendor_id;
228+
uint16_t m_device_id;
229+
std::string m_vendor_id_s;
230+
std::string m_device_id_s;
234231

235-
static GPU_t m_gpu_infos;
232+
static GPU_t m_gpu_infos;
236233
};
237234

238235
class Disk
@@ -248,7 +245,7 @@ class Disk
248245
std::string mountdir;
249246
};
250247

251-
Disk(const std::string_view path, std::vector<std::string>& paths);
248+
Disk(const std::string& path, systemInfo_t& queried_paths);
252249

253250
double& total_amount() noexcept;
254251
double& free_amount() noexcept;

src/parse.cpp

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1080,10 +1080,10 @@ static std::string prettify_de_name(const std::string_view de_name)
10801080
return de_name.data();
10811081
}
10821082

1083-
std::vector<std::uint16_t> queried_gpus;
1084-
std::vector<std::string> queried_disks;
1085-
std::vector<std::string> queried_themes_names;
1086-
systemInfo_t queried_themes;
1083+
systemInfo_t queried_gpus;
1084+
systemInfo_t queried_disks;
1085+
systemInfo_t queried_themes_names;
1086+
systemInfo_t queried_themes;
10871087

10881088
// clang-format on
10891089
void addValueFromModuleMember(const std::string& moduleName, const std::string& moduleMemberName, parse_args_t& parse_args)
@@ -1251,7 +1251,7 @@ void addValueFromModuleMember(const std::string& moduleName, const std::string&
12511251

12521252
else if (moduleName == "theme")
12531253
{
1254-
Query::Theme query_theme(queried_themes, config, false);
1254+
Query::Theme query_theme(config, false);
12551255

12561256
if (sysInfo.find(moduleName) == sysInfo.end())
12571257
sysInfo.insert({ moduleName, {} });
@@ -1281,7 +1281,7 @@ void addValueFromModuleMember(const std::string& moduleName, const std::string&
12811281
{
12821282
if (hasStart(moduleMemberName, "cursor"))
12831283
{
1284-
Query::Theme query_cursor(queried_themes, config, true);
1284+
Query::Theme query_cursor(config, true);
12851285
switch (moduleMember_hash)
12861286
{
12871287
case "cursor"_fnv1a16:
@@ -1296,7 +1296,7 @@ void addValueFromModuleMember(const std::string& moduleName, const std::string&
12961296
}
12971297
else
12981298
{
1299-
Query::Theme query_theme(0, queried_themes, queried_themes_names, "gsettings", config, true);
1299+
Query::Theme query_theme(0, queried_themes, "gsettings", config, true);
13001300
switch (moduleMember_hash)
13011301
{
13021302
case "name"_fnv1a16: SYSINFO_INSERT(query_theme.gtk_theme()); break;
@@ -1310,9 +1310,9 @@ void addValueFromModuleMember(const std::string& moduleName, const std::string&
13101310
// clang-format off
13111311
else if (moduleName == "theme-gtk-all")
13121312
{
1313-
Query::Theme gtk2(2, queried_themes, queried_themes_names, "gtk2", config);
1314-
Query::Theme gtk3(3, queried_themes, queried_themes_names, "gtk3", config);
1315-
Query::Theme gtk4(4, queried_themes, queried_themes_names, "gtk4", config);
1313+
Query::Theme gtk2(2, queried_themes, "gtk2", config);
1314+
Query::Theme gtk3(3, queried_themes, "gtk3", config);
1315+
Query::Theme gtk4(4, queried_themes, "gtk4", config);
13161316

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

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

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

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

13931392
Query::GPU query_gpu(id, queried_gpus);
13941393

@@ -1417,7 +1416,7 @@ void addValueFromModuleMember(const std::string& moduleName, const std::string&
14171416
TOTAL,
14181417
FREE
14191418
};
1420-
std::string path = moduleName.data();
1419+
std::string path{moduleName.data()};
14211420
path.erase(0, 5); // disk(
14221421
path.pop_back(); // )
14231422
debug("disk path = {}", path);
@@ -1652,8 +1651,7 @@ void addValueFromModule(const std::string& moduleName, parse_args_t& parse_args)
16521651

16531652
else if (hasStart(moduleName, "gpu"))
16541653
{
1655-
const std::uint16_t id =
1656-
static_cast<std::uint16_t>(moduleName.length() > 3 ? std::stoi(std::string(moduleName).substr(3)) : 0);
1654+
const std::string& id = (moduleName.length() > 3 ? moduleName.substr(3) : "0");
16571655

16581656
if (sysInfo.find(moduleName) == sysInfo.end())
16591657
sysInfo.insert({ moduleName, {} });

src/query/android/gpu.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ static std::string detect_adreno(const std::string& cpu_model_name)
194194
return MAGIC_LINE;
195195
}
196196

197-
GPU::GPU(const std::uint16_t id, std::vector<std::uint16_t>& queried_gpus)
197+
GPU::GPU(const std::string& id, systemInfo_t& queried_gpus)
198198
{
199199
CPU query_cpu;
200200
if (query_cpu.vendor() == "QUALCOMM" || query_cpu.vendor() == "QTI")

src/query/android/theme.cpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,29 +31,27 @@
3131

3232
using namespace Query;
3333

34-
Theme::Theme(const std::uint8_t ver, systemInfo_t& queried_themes, std::vector<std::string>& queried_themes_names,
34+
Theme::Theme(const std::uint8_t ver, systemInfo_t& queried_themes,
3535
const std::string& theme_name_version, const Config& config, const bool gsettings_only)
36-
: m_queried_themes(queried_themes), m_theme_name_version(theme_name_version)
3736
{
3837
m_theme_infos.cursor = m_theme_infos.gtk_font = m_theme_infos.cursor_size = m_theme_infos.gtk_theme_name =
3938
m_theme_infos.gtk_icon_theme = MAGIC_LINE;
4039
}
4140

42-
Theme::Theme(systemInfo_t& queried_themes, const Config& config, const bool gsettings_only)
43-
: m_queried_themes(queried_themes)
41+
Theme::Theme(const Config& config, const bool gsettings_only)
4442
{
4543
m_theme_infos.cursor = m_theme_infos.gtk_font = m_theme_infos.cursor_size = m_theme_infos.gtk_theme_name =
4644
m_theme_infos.gtk_icon_theme = MAGIC_LINE;
4745
}
4846

4947
// clang-format off
50-
std::string Theme::gtk_theme() noexcept
48+
std::string& Theme::gtk_theme() noexcept
5149
{ return m_theme_infos.gtk_theme_name; }
5250

53-
std::string Theme::gtk_icon_theme() noexcept
51+
std::string& Theme::gtk_icon_theme() noexcept
5452
{ return m_theme_infos.gtk_icon_theme; }
5553

56-
std::string Theme::gtk_font() noexcept
54+
std::string& Theme::gtk_font() noexcept
5755
{ return m_theme_infos.gtk_font; }
5856

5957
std::string& Theme::cursor() noexcept

src/query/unix/disk.cpp

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,25 +24,31 @@
2424
*/
2525

2626
#include <mntent.h>
27-
#include <algorithm>
2827
#include <cstdio>
2928
#include <filesystem>
3029

3130
#include "query.hpp"
3231
#include "util.hpp"
32+
#include "parse.hpp"
3333

3434
using namespace Query;
3535

36-
Disk::Disk(const std::string_view path, std::vector<std::string>& paths)
36+
Disk::Disk(const std::string& path, systemInfo_t& queried_paths)
3737
{
38-
if (std::find(paths.begin(), paths.end(), path) == paths.end())
39-
paths.push_back(path.data());
40-
else
38+
if (queried_paths.find(path) != queried_paths.end())
39+
{
40+
m_disk_infos.device = getInfoFromName(queried_paths, path, "device");
41+
m_disk_infos.mountdir = getInfoFromName(queried_paths, path, "mountdir");
42+
m_disk_infos.typefs = getInfoFromName(queried_paths, path, "typefs");
43+
m_disk_infos.total_amount = std::stod(getInfoFromName(queried_paths, path, "total_amount"));
44+
m_disk_infos.used_amount = std::stod(getInfoFromName(queried_paths, path, "used_amount"));
45+
m_disk_infos.free_amount = std::stod(getInfoFromName(queried_paths, path, "free_amount"));
4146
return;
47+
}
4248

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

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

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

8793
endmntent(mountsFile);
88-
94+
queried_paths.insert(
95+
{path, {
96+
{"total_amount", variant(m_disk_infos.total_amount)},
97+
{"used_amount", variant(m_disk_infos.used_amount)},
98+
{"free_amount", variant(m_disk_infos.free_amount)},
99+
{"typefs", variant(m_disk_infos.typefs)},
100+
{"mountdir", variant(m_disk_infos.mountdir)},
101+
{"device", variant(m_disk_infos.device)}
102+
}}
103+
);
89104
}
90105

91106
// clang-format off

src/query/unix/gpu.cpp

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,11 @@
2323
*
2424
*/
2525

26+
#include "fmt/format.h"
27+
#include "parse.hpp"
2628
#include "platform.hpp"
2729
#if CF_UNIX
2830

29-
#include <algorithm>
3031
#include <cstdint>
3132
#include <filesystem>
3233
#include <string>
@@ -75,15 +76,17 @@ static GPU::GPU_t get_gpu_infos(const std::string_view m_vendor_id_s, const std:
7576
return ret;
7677
}
7778

78-
GPU::GPU(const std::uint16_t id, std::vector<std::uint16_t>& queried_gpus)
79+
GPU::GPU(const std::string& id, systemInfo_t& queried_gpus)
7980
{
80-
if (std::find(queried_gpus.begin(), queried_gpus.end(), id) == queried_gpus.end())
81-
queried_gpus.push_back(id);
82-
else
81+
if (queried_gpus.find(id) != queried_gpus.end())
82+
{
83+
m_gpu_infos.name = getInfoFromName(queried_gpus, id, "name");
84+
m_gpu_infos.vendor = getInfoFromName(queried_gpus, id, "vendor");
8385
return;
86+
}
8487

8588
const std::uint16_t max_iter = 10;
86-
std::uint16_t id_iter = id;
89+
std::uint16_t id_iter = std::stoi(id);
8790
std::string sys_path;
8891
int i = 0;
8992
for (; i <= max_iter; i++)
@@ -106,6 +109,12 @@ GPU::GPU(const std::uint16_t id, std::vector<std::uint16_t>& queried_gpus)
106109
m_device_id_s = read_by_syspath(sys_path + "/device/device");
107110

108111
m_gpu_infos = get_gpu_infos(m_vendor_id_s, m_device_id_s);
112+
queried_gpus.insert(
113+
{id, {
114+
{"name", variant(m_gpu_infos.name)},
115+
{"vendor", variant(m_gpu_infos.vendor)},
116+
}}
117+
);
109118
}
110119

111120
// clang-format off

src/query/unix/theme.cpp

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -476,16 +476,16 @@ static void get_gtk_theme(const bool dont_query_dewm, const std::uint8_t ver, co
476476
}
477477

478478
// clang-format off
479-
Theme::Theme(const std::uint8_t ver, systemInfo_t& queried_themes, std::vector<std::string>& queried_themes_names,
479+
Theme::Theme(const std::uint8_t ver, systemInfo_t& queried_themes,
480480
const std::string& theme_name_version, const Config& config, const bool gsettings_only)
481-
: m_queried_themes(queried_themes),
482-
m_theme_name_version(theme_name_version)
483481
{
484-
if (std::find(queried_themes_names.begin(), queried_themes_names.end(), m_theme_name_version)
485-
== queried_themes_names.end())
486-
queried_themes_names.push_back(m_theme_name_version);
487-
else
482+
if (queried_themes.find(theme_name_version) != queried_themes.end())
483+
{
484+
m_theme_infos.gtk_theme_name = getInfoFromName(queried_themes, theme_name_version, "theme-name");
485+
m_theme_infos.gtk_font = getInfoFromName(queried_themes, theme_name_version, "icon-theme-name");
486+
m_theme_infos.gtk_icon_theme = getInfoFromName(queried_themes, theme_name_version, "font-name");
488487
return;
488+
}
489489

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

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

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

552552
}
553553

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

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

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

563563
std::string& Theme::cursor() noexcept
564564
{ return m_theme_infos.cursor; }

0 commit comments

Comments
 (0)