Manager: Add ability to detect Xorg/Wayland session for commenting files
This commit is contained in:
+11
-3
@@ -86,18 +86,26 @@ or uncomments them depending on the machine's configuration. Such a block looks
|
|||||||
like this:
|
like this:
|
||||||
|
|
||||||
#+BEGIN_SRC shell-script
|
#+BEGIN_SRC shell-script
|
||||||
# >>> distro=arch hostname=arch-desktop user=anon
|
# >>> distro=arch hostname=arch-desktop user=anon session=xorg
|
||||||
export PATH="$PATH:$HOME/.local/bin"
|
export PATH="$PATH:$HOME/.local/bin"
|
||||||
# <<<
|
# <<<
|
||||||
#+END_SRC
|
#+END_SRC
|
||||||
|
|
||||||
This block uses all of the supported configurations, ~distro~, ~hostname~ and
|
This block uses all of the supported configurations, ~distro~, ~hostname~,
|
||||||
~user~. These can be used in any combination and order.
|
~user~ and ~session~. These can be used in any combination and order.
|
||||||
|
|
||||||
If *all* of the given configurations match, manafiles will make sure that the
|
If *all* of the given configurations match, manafiles will make sure that the
|
||||||
contents of the block are uncommented. If *any* of them do *not* match, then
|
contents of the block are uncommented. If *any* of them do *not* match, then
|
||||||
manafiles will make sure that the contents of the block are commented.
|
manafiles will make sure that the contents of the block are commented.
|
||||||
|
|
||||||
|
***** Usable values for the configurations
|
||||||
|
|
||||||
|
| distro | session |
|
||||||
|
|--------+---------|
|
||||||
|
| arch | xorg |
|
||||||
|
| debian | wayland |
|
||||||
|
| ubuntu | |
|
||||||
|
|
||||||
** Compiling
|
** Compiling
|
||||||
|
|
||||||
*** Dependencies
|
*** Dependencies
|
||||||
|
|||||||
+13
-6
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (C) 2021-2022 Riyyi
|
* Copyright (C) 2021-2022,2025 Riyyi
|
||||||
*
|
*
|
||||||
* SPDX-License-Identifier: MIT
|
* SPDX-License-Identifier: MIT
|
||||||
*/
|
*/
|
||||||
@@ -15,11 +15,12 @@
|
|||||||
#include <unistd.h> // geteuid, getlogin, setegid, seteuid
|
#include <unistd.h> // geteuid, getlogin, setegid, seteuid
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
|
#include "ruc/file.h"
|
||||||
|
#include "ruc/meta/assert.h"
|
||||||
|
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
#include "dotfile.h"
|
#include "dotfile.h"
|
||||||
#include "machine.h"
|
#include "machine.h"
|
||||||
#include "ruc/file.h"
|
|
||||||
#include "ruc/meta/assert.h"
|
|
||||||
|
|
||||||
Dotfile::Dotfile(s)
|
Dotfile::Dotfile(s)
|
||||||
{
|
{
|
||||||
@@ -375,15 +376,16 @@ void Dotfile::selectivelyCommentOrUncomment(const std::string& path)
|
|||||||
{
|
{
|
||||||
ruc::File dotfile(path);
|
ruc::File dotfile(path);
|
||||||
|
|
||||||
const std::string search[3] = {
|
const std::string search[4] = {
|
||||||
"distro=",
|
"distro=",
|
||||||
"hostname=",
|
"hostname=",
|
||||||
"user=",
|
"user=",
|
||||||
|
"session=",
|
||||||
};
|
};
|
||||||
|
|
||||||
// State of the loop
|
// State of the loop
|
||||||
bool isFiltering = false;
|
bool isFiltering = false;
|
||||||
std::string filter[3];
|
std::string filter[4];
|
||||||
std::string commentCharacter;
|
std::string commentCharacter;
|
||||||
std::string commentTerminationCharacter;
|
std::string commentTerminationCharacter;
|
||||||
size_t positionInFile = 0;
|
size_t positionInFile = 0;
|
||||||
@@ -420,7 +422,7 @@ void Dotfile::selectivelyCommentOrUncomment(const std::string& path)
|
|||||||
if (line.find(">>>") != std::string::npos) {
|
if (line.find(">>>") != std::string::npos) {
|
||||||
// Find machine info
|
// Find machine info
|
||||||
size_t find = 0;
|
size_t find = 0;
|
||||||
for (size_t i = 0; i < 3; ++i) {
|
for (size_t i = 0; i < 4; ++i) {
|
||||||
find = line.find(search[i]) + search[i].size();
|
find = line.find(search[i]) + search[i].size();
|
||||||
if (find < search[i].size()) {
|
if (find < search[i].size()) {
|
||||||
continue;
|
continue;
|
||||||
@@ -450,6 +452,7 @@ void Dotfile::selectivelyCommentOrUncomment(const std::string& path)
|
|||||||
filter[0] = "";
|
filter[0] = "";
|
||||||
filter[1] = "";
|
filter[1] = "";
|
||||||
filter[2] = "";
|
filter[2] = "";
|
||||||
|
filter[3] = "";
|
||||||
commentCharacter.clear();
|
commentCharacter.clear();
|
||||||
commentTerminationCharacter.clear();
|
commentTerminationCharacter.clear();
|
||||||
continue;
|
continue;
|
||||||
@@ -471,6 +474,10 @@ void Dotfile::selectivelyCommentOrUncomment(const std::string& path)
|
|||||||
commentOrUncommentLine(line, true);
|
commentOrUncommentLine(line, true);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
else if (filter[3] != Machine::the().session() && !filter[3].empty()) {
|
||||||
|
commentOrUncommentLine(line, true);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
commentOrUncommentLine(line, false);
|
commentOrUncommentLine(line, false);
|
||||||
}
|
}
|
||||||
|
|||||||
+80
-2
@@ -1,21 +1,25 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (C) 2022 Riyyi
|
* Copyright (C) 2022,2025 Riyyi
|
||||||
*
|
*
|
||||||
* SPDX-License-Identifier: MIT
|
* SPDX-License-Identifier: MIT
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include <cstdint> // int8_t
|
||||||
|
#include <filesystem> // std::filesystem::path
|
||||||
#include <pwd.h> // getpwnam
|
#include <pwd.h> // getpwnam
|
||||||
#include <sstream> // istringstream
|
#include <sstream> // istringstream
|
||||||
#include <unistd.h> // gethostname, getlogin
|
#include <unistd.h> // gethostname, getlogin
|
||||||
|
|
||||||
#include "machine.h"
|
|
||||||
#include "ruc/file.h"
|
#include "ruc/file.h"
|
||||||
|
|
||||||
|
#include "machine.h"
|
||||||
|
|
||||||
Machine::Machine(s)
|
Machine::Machine(s)
|
||||||
{
|
{
|
||||||
fetchDistro();
|
fetchDistro();
|
||||||
fetchHostname();
|
fetchHostname();
|
||||||
fetchUsername();
|
fetchUsername();
|
||||||
|
fetchSession();
|
||||||
}
|
}
|
||||||
|
|
||||||
Machine::~Machine()
|
Machine::~Machine()
|
||||||
@@ -61,3 +65,77 @@ void Machine::fetchUsername()
|
|||||||
perror("\033[31;1mError:\033[0m getpwnam");
|
perror("\033[31;1mError:\033[0m getpwnam");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Machine::fetchSession()
|
||||||
|
{
|
||||||
|
// Determine if this is an Xorg or Wayland session
|
||||||
|
int8_t likelyWayland = 0;
|
||||||
|
|
||||||
|
// Detect via environment variable
|
||||||
|
std::string env;
|
||||||
|
env = std::string(std::getenv("XDG_SESSION_TYPE"));
|
||||||
|
if (env == "wayland") {
|
||||||
|
likelyWayland++;
|
||||||
|
}
|
||||||
|
else if (env == "x11") {
|
||||||
|
likelyWayland--;
|
||||||
|
}
|
||||||
|
env = std::string(std::getenv("WAYLAND_DISPLAY"));
|
||||||
|
if (env.find("wayland-", 0) == 0) {
|
||||||
|
likelyWayland++;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Detect via Wayland socket
|
||||||
|
auto socket = std::filesystem::path("/run/user") / std::to_string(uid());
|
||||||
|
if (std::filesystem::exists(socket) && std::filesystem::is_directory(socket)) {
|
||||||
|
for (const auto& entry : std::filesystem::directory_iterator(socket)) {
|
||||||
|
if (entry.path().filename().string().find("wayland-", 0) == 0) {
|
||||||
|
likelyWayland++;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Detect via Xorg socket
|
||||||
|
if (std::filesystem::exists("/tmp/.X11-unix")) {
|
||||||
|
likelyWayland--;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Detect via running processes, /proc/<id>/comm
|
||||||
|
std::filesystem::path processes = "/proc";
|
||||||
|
if (std::filesystem::exists(processes) && std::filesystem::is_directory(processes)) {
|
||||||
|
for (const auto& entry : std::filesystem::directory_iterator(processes)) {
|
||||||
|
if (std::filesystem::is_directory(entry)) {
|
||||||
|
|
||||||
|
std::filesystem::path comm = entry.path() / "comm";
|
||||||
|
if (!std::filesystem::exists(comm)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Read the contents of the "comm" file
|
||||||
|
std::ifstream stream(comm);
|
||||||
|
std::string command;
|
||||||
|
std::getline(stream, command);
|
||||||
|
|
||||||
|
if (command == "Xwayland" || command == "hyprland" || command == "sway") {
|
||||||
|
likelyWayland++;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (command == "Xorg" || command == "xinit" || command == "bspwm") {
|
||||||
|
likelyWayland--;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// If we detected at least 2 ways, we can be fairly certain
|
||||||
|
m_session = "";
|
||||||
|
if (likelyWayland <= -2) {
|
||||||
|
m_session = "xorg";
|
||||||
|
}
|
||||||
|
else if (likelyWayland >= 2) {
|
||||||
|
m_session = "wayland";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
+10
-6
@@ -17,21 +17,25 @@ public:
|
|||||||
Machine(s);
|
Machine(s);
|
||||||
virtual ~Machine();
|
virtual ~Machine();
|
||||||
|
|
||||||
const std::string& distroId() { return m_distroId; }
|
const std::string& distroId() const { return m_distroId; }
|
||||||
const std::string& distroIdLike() { return m_distroIdLike; }
|
const std::string& distroIdLike() const { return m_distroIdLike; }
|
||||||
const std::string& hostname() { return m_hostname; }
|
const std::string& hostname() const { return m_hostname; }
|
||||||
|
|
||||||
std::string username() { return m_passwd->pw_name; }
|
std::string username() const { return std::string(m_passwd->pw_name); }
|
||||||
uint32_t uid() { return m_passwd->pw_uid; }
|
uint32_t uid() const { return m_passwd->pw_uid; }
|
||||||
uint32_t gid() { return m_passwd->pw_gid; }
|
uint32_t gid() const { return m_passwd->pw_gid; }
|
||||||
|
|
||||||
|
const std::string& session() const { return m_session; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void fetchDistro();
|
void fetchDistro();
|
||||||
void fetchHostname();
|
void fetchHostname();
|
||||||
void fetchUsername();
|
void fetchUsername();
|
||||||
|
void fetchSession();
|
||||||
|
|
||||||
std::string m_distroId;
|
std::string m_distroId;
|
||||||
std::string m_distroIdLike;
|
std::string m_distroIdLike;
|
||||||
std::string m_hostname;
|
std::string m_hostname;
|
||||||
|
std::string m_session;
|
||||||
passwd* m_passwd { nullptr };
|
passwd* m_passwd { nullptr };
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user