Skip to content

Commit

Permalink
query: user: add user.wm_version
Browse files Browse the repository at this point in the history
me after contributing to Hyprland with HYPRLAND_VERSION so I could add this info that NOBODY else has

hyprwm/Hyprland#8034
  • Loading branch information
Toni500github committed Oct 14, 2024
1 parent 895ff98 commit fa3e271
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 18 deletions.
4 changes: 4 additions & 0 deletions include/query.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,13 @@ class User
std::string shell_name{ UNKNOWN };
std::string shell_version{ UNKNOWN };
std::string wm_name{ MAGIC_LINE };
std::string wm_version{ UNKNOWN };
std::string de_name{ MAGIC_LINE };
std::string de_version{ UNKNOWN };
std::string term_name{ MAGIC_LINE };
std::string term_version{ MAGIC_LINE };
//private:
std::string m_wm_path;
};

User() noexcept;
Expand All @@ -98,6 +101,7 @@ class User
std::string& shell_name() noexcept;
std::string& shell_version(const std::string_view shell_name);
std::string& wm_name(bool dont_query_dewm, const std::string_view term_name);
std::string& wm_version(bool dont_query_dewm, const std::string_view term_name);
std::string& de_name(bool dont_query_dewm, const std::string_view term_name, const std::string_view wm_name);
std::string& de_version(const std::string_view de_name);
std::string& term_name();
Expand Down
18 changes: 11 additions & 7 deletions src/parse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1083,6 +1083,10 @@ void addValueFromModule(const std::string& moduleName, const std::string& module
SYSINFO_INSERT(query_user.wm_name(query_user.m_bDont_query_dewm, query_user.term_name()));
break;

case "wm_version"_fnv1a16:
SYSINFO_INSERT(query_user.wm_version(query_user.m_bDont_query_dewm, query_user.term_name()));
break;

case "terminal"_fnv1a16:
SYSINFO_INSERT(prettify_term_name(query_user.term_name()) + ' ' +
query_user.term_version(query_user.term_name()));
Expand All @@ -1097,7 +1101,7 @@ void addValueFromModule(const std::string& moduleName, const std::string& module

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

if (sysInfo.find(moduleName) == sysInfo.end())
sysInfo.insert({ moduleName, {} });
Expand All @@ -1107,13 +1111,13 @@ void addValueFromModule(const std::string& moduleName, const std::string& module
switch (moduleMember_hash)
{
case "cursor"_fnv1a16:
if (query_cursor.cursor_size() == UNKNOWN)
SYSINFO_INSERT(query_cursor.cursor());
if (query_theme.cursor_size() == UNKNOWN)
SYSINFO_INSERT(query_theme.cursor());
else
SYSINFO_INSERT(fmt::format("{} ({}px)", query_cursor.cursor(), query_cursor.cursor_size()));
SYSINFO_INSERT(fmt::format("{} ({}px)", query_theme.cursor(), query_theme.cursor_size()));
break;
case "cursor_name"_fnv1a16: SYSINFO_INSERT(query_cursor.cursor()); break;
case "cursor_size"_fnv1a16: SYSINFO_INSERT(query_cursor.cursor_size()); break;
case "cursor_name"_fnv1a16: SYSINFO_INSERT(query_theme.cursor()); break;
case "cursor_size"_fnv1a16: SYSINFO_INSERT(query_theme.cursor_size()); break;
}
}
}
Expand Down Expand Up @@ -1143,7 +1147,7 @@ void addValueFromModule(const std::string& moduleName, const std::string& module
}
else
{
Query::Theme query_theme(0, queried_themes, queried_themes_names, "", config, true);
Query::Theme query_theme(0, queried_themes, queried_themes_names, "gsettings", config, true);
switch (moduleMember_hash)
{
case "name"_fnv1a16: SYSINFO_INSERT(query_theme.gtk_theme()); break;
Expand Down
57 changes: 48 additions & 9 deletions src/query/unix/user.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ static std::string get_de_name()
return ret;
}

static std::string get_wm_name()
static std::string get_wm_name(std::string& wm_path_exec)
{
std::string path, proc_name, wm_name;
const uid_t uid = getuid();
Expand All @@ -53,8 +53,8 @@ static std::string get_wm_name()
continue;

path = dir_entry.path() / "cmdline";
std::ifstream f(path, std::ios::binary);
std::getline(f, proc_name);
std::ifstream f_cmdline(path, std::ios::binary);
std::getline(f_cmdline, proc_name);

size_t pos = 0;
if ((pos = proc_name.find('\0')) != std::string::npos)
Expand All @@ -68,6 +68,8 @@ static std::string get_wm_name()
if ((wm_name = prettify_wm_name(proc_name)) == MAGIC_LINE)
continue;

char buf[PATH_MAX];
wm_path_exec = realpath((dir_entry.path().string() + "/exe").c_str(), buf);
break;
}

Expand Down Expand Up @@ -108,10 +110,10 @@ static std::string get_de_version(const std::string_view de_name)
}
}

static std::string get_wm_wayland_name()
static std::string get_wm_wayland_name(std::string& wm_path_exec)
{
#if __has_include(<sys/socket.h>) && __has_include(<wayland-client.h>)
LOAD_LIBRARY("libwayland-client.so", return get_wm_name();)
LOAD_LIBRARY("libwayland-client.so", return get_wm_name(wm_path_exec);)

LOAD_LIB_SYMBOL(wl_display*, wl_display_connect, const char* name)
LOAD_LIB_SYMBOL(void, wl_display_disconnect, wl_display* display)
Expand All @@ -130,11 +132,14 @@ static std::string get_wm_wayland_name()
f >> ret;
wl_display_disconnect(display);

char buf[PATH_MAX];
wm_path_exec = realpath(fmt::format("/proc/{}/exe", ucred.pid).c_str(), buf);

UNLOAD_LIBRARY()

return prettify_wm_name(ret);
#else
return get_wm_name();
return get_wm_name(wm_path_exec);
#endif
}

Expand Down Expand Up @@ -352,9 +357,9 @@ std::string& User::wm_name(bool dont_query_dewm, const std::string_view term_nam
{
const char* env = std::getenv("WAYLAND_DISPLAY");
if (env != nullptr && env[0] != '\0')
m_users_infos.wm_name = get_wm_wayland_name();
m_users_infos.wm_name = get_wm_wayland_name(m_users_infos.m_wm_path);
else
m_users_infos.wm_name = get_wm_name();
m_users_infos.wm_name = get_wm_name(m_users_infos.m_wm_path);

if (m_users_infos.de_name == m_users_infos.wm_name)
m_users_infos.de_name = MAGIC_LINE;
Expand All @@ -365,6 +370,39 @@ std::string& User::wm_name(bool dont_query_dewm, const std::string_view term_nam
return m_users_infos.wm_name;
}

std::string& User::wm_version(bool dont_query_dewm, const std::string_view term_name)
{
if (dont_query_dewm || hasStart(term_name, "/dev"))
{
m_users_infos.wm_name = MAGIC_LINE;
return m_users_infos.wm_name;
}

static bool done = false;
if (!done)
{
m_users_infos.wm_version.clear();
if (m_users_infos.wm_name == "dwm")
{
read_exec({m_users_infos.m_wm_path.c_str(), "-v"}, m_users_infos.wm_version, true);
m_users_infos.wm_version.erase(0, "dwm-"_len);
}
else
{
read_exec({m_users_infos.m_wm_path.c_str(), "--version"}, m_users_infos.wm_version);
m_users_infos.wm_version.erase(0, m_users_infos.wm_name.length() + 1);

const size_t pos = m_users_infos.wm_version.find(' ');
if (pos != std::string::npos)
m_users_infos.wm_version.erase(pos);
}

done = true;
}

return m_users_infos.wm_version;
}

std::string& User::de_name(bool dont_query_dewm, const std::string_view term_name, const std::string_view wm_name)
{
// first let's see if we are not in a tty or if the user doesn't want to
Expand All @@ -382,7 +420,8 @@ std::string& User::de_name(bool dont_query_dewm, const std::string_view term_nam

if (!done)
{
if (m_users_infos.de_name != MAGIC_LINE && wm_name != MAGIC_LINE && m_users_infos.de_name == wm_name)
if ((m_users_infos.de_name != MAGIC_LINE && wm_name != MAGIC_LINE) &&
m_users_infos.de_name == wm_name)
{
m_users_infos.de_name = MAGIC_LINE;
done = true;
Expand Down
5 changes: 3 additions & 2 deletions src/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -318,10 +318,11 @@ bool read_exec(std::vector<const char*> cmd, std::string& output, bool useStdErr
if (pipe(pipeout.data()) < 0)
die("pipe() failed: {}", strerror(errno));

pid_t pid = fork();
const pid_t pid = fork();

// we wait for the command to finish then start executing the rest
if (pid > 0)
{ // we wait for the command to finish then start executing the rest
{
close(pipeout.at(1));

int status;
Expand Down

0 comments on commit fa3e271

Please sign in to comment.