Add Swaylock support

This commit is contained in:
Vyn 2024-06-05 14:18:45 +02:00
parent 90ac4001e7
commit fe4e18f3b5
7 changed files with 61 additions and 20 deletions

View file

@ -12,5 +12,6 @@ set(CMAKE_COMPILE_WARNING_AS_ERROR ON)
add_executable(sway-wallpaper
src/main.cpp
src/Wallpapers.cpp
src/SwaybgProcess.cpp
src/Process.cpp
src/makeProcess.cpp
)

View file

@ -167,7 +167,7 @@ namespace CliArguments {
}
if (arguments_[argName.value()].type == Bool) {
argumentsValue_[argName.value()] = "true";
return;
continue;
}
++i;
if (i >= argc) {

View file

@ -1,9 +1,10 @@
#include "SwaybgProcess.h"
#include "Process.h"
#include <csignal>
#include <unistd.h>
#include <vector>
std::vector<SwaybgProcess*> processes;
std::vector<Process*> processes;
void signalHandler(int sig) {
std::cout << "received signal: " << sig << std::endl;
@ -13,7 +14,12 @@ void signalHandler(int sig) {
exit(1);
}
SwaybgProcess::SwaybgProcess(const std::string& wallpaperPath, const std::string& output) : wallpaperPath(wallpaperPath), output(output) {
Process::Process(const std::string& execPath, const std::vector<const char*>& args) : execPath(execPath) {
this->args.push_back(this->execPath.c_str());
for (auto& arg : args) {
this->args.push_back(arg);
}
this->args.push_back(0);
static bool signalHandlerCreated = false;
if (!signalHandlerCreated) {
signalHandlerCreated = true;
@ -22,9 +28,9 @@ SwaybgProcess::SwaybgProcess(const std::string& wallpaperPath, const std::string
processes.push_back(this);
}
SwaybgProcess::~SwaybgProcess() { processes.erase(std::ranges::find(processes, this)); }
Process::~Process() { processes.erase(std::ranges::find(processes, this)); }
void SwaybgProcess::exec() {
void Process::exec() {
if (pipe(fds) == -1) {
throw std::runtime_error("Can't create pipe");
}
@ -33,7 +39,8 @@ void SwaybgProcess::exec() {
dup2(fds[1], STDOUT_FILENO);
close(fds[0]);
close(fds[1]);
execl("/bin/swaybg", "/bin/swaybg" , "-o", output.c_str(), "-i", wallpaperPath.c_str(), "-m", "fill", (char*)0) ;
//execv("/bin/swaybg", "/bin/swaybg" , "-o", output.c_str(), "-i", wallpaperPath.c_str(), "-m", "fill", (char*)0) ;
execvp(execPath.c_str(), const_cast<char* const*>(args.data())) ;
} else if (pid > 0) {
killed = false;
close(fds[1]);
@ -44,14 +51,17 @@ void SwaybgProcess::exec() {
}
}
void SwaybgProcess::kill() {
void Process::kill() {
if (killed) {
return;
}
killed = true;
::kill(pid, SIGKILL);
int status;
::waitpid(pid, &status, 0);
wait();
}
int Process::wait() {
int status;
::waitpid(pid, &status, 0);
return status;
}

View file

@ -5,24 +5,26 @@
#include <string>
#include <unistd.h>
#include <sys/wait.h>
#include <vector>
#define SWAYBGPROCESS_BUFFER_SIZE 4094
class SwaybgProcess {
class Process {
public:
SwaybgProcess(const std::string& wallpaperPath, const std::string& output);
Process(const std::string& execPath, const std::vector<const char*>& args);
~SwaybgProcess();
~Process();
void exec();
void kill();
int wait();
private:
std::string wallpaperPath;
std::string output;
std::string execPath;
std::vector<const char*> args;
pid_t pid;
bool killed = true;

View file

@ -1,6 +1,7 @@
#include "CliArguments.h"
#include "Process.h"
#include "Wallpapers.h"
#include "SwaybgProcess.h"
#include "makeProcess.h"
#include <exception>
#include <iostream>
#include <ostream>
@ -35,6 +36,10 @@ int main(int argc, char** argv, char** envp) {
.description = "if set, wallpapers order is based on the names of the files",
.type = CliArguments::Bool,
}},
{"lock", {
.description = "Lock the screen using swaylock instead of applying a wallpaper",
.type = CliArguments::Bool,
}},
{"help", {
.aliases = {"h"},
.description = "Show help",
@ -63,14 +68,21 @@ int main(int argc, char** argv, char** envp) {
wallpapers.shuffle();
}
SwaybgProcess swayBgProcess(wallpapers.next(), output);
if (arguments.exists("lock")) {
Process swaylockProcess = makeSwaylockProcess(wallpapers.next());
swaylockProcess.exec();
swaylockProcess.wait();
return 0;
}
Process swayBgProcess = makeSwaybgProcess(wallpapers.next(), output);
swayBgProcess.exec();
if (arguments.exists("interval")) {
int interval = arguments.getInt("interval");
while (true) {
sleep(interval);
SwaybgProcess newSwayBgProcess(wallpapers.next(), output);
Process newSwayBgProcess = makeSwaybgProcess(wallpapers.next(), output);
newSwayBgProcess.exec();
sleep(1);
swayBgProcess.kill();

9
src/makeProcess.cpp Normal file
View file

@ -0,0 +1,9 @@
#include "makeProcess.h"
Process makeSwaybgProcess(const std::string& wallpaper, const std::string& output) {
return Process("swaybg", {"-o", output.c_str(), "-i", wallpaper.c_str(), "-m", "fill"});
}
Process makeSwaylockProcess(const std::string& wallpaper) {
return Process("swaylock", {"-i", wallpaper.c_str()});
}

7
src/makeProcess.h Normal file
View file

@ -0,0 +1,7 @@
#pragma once
#include "Process.h"
#include <string>
Process makeSwaybgProcess(const std::string& wallpaper, const std::string& output);
Process makeSwaylockProcess(const std::string& wallpaper);