Add Swaylock support
This commit is contained in:
parent
90ac4001e7
commit
fe4e18f3b5
7 changed files with 61 additions and 20 deletions
|
@ -12,5 +12,6 @@ set(CMAKE_COMPILE_WARNING_AS_ERROR ON)
|
||||||
add_executable(sway-wallpaper
|
add_executable(sway-wallpaper
|
||||||
src/main.cpp
|
src/main.cpp
|
||||||
src/Wallpapers.cpp
|
src/Wallpapers.cpp
|
||||||
src/SwaybgProcess.cpp
|
src/Process.cpp
|
||||||
|
src/makeProcess.cpp
|
||||||
)
|
)
|
||||||
|
|
|
@ -167,7 +167,7 @@ namespace CliArguments {
|
||||||
}
|
}
|
||||||
if (arguments_[argName.value()].type == Bool) {
|
if (arguments_[argName.value()].type == Bool) {
|
||||||
argumentsValue_[argName.value()] = "true";
|
argumentsValue_[argName.value()] = "true";
|
||||||
return;
|
continue;
|
||||||
}
|
}
|
||||||
++i;
|
++i;
|
||||||
if (i >= argc) {
|
if (i >= argc) {
|
||||||
|
|
|
@ -1,9 +1,10 @@
|
||||||
#include "SwaybgProcess.h"
|
#include "Process.h"
|
||||||
#include <csignal>
|
#include <csignal>
|
||||||
|
#include <unistd.h>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
|
|
||||||
std::vector<SwaybgProcess*> processes;
|
std::vector<Process*> processes;
|
||||||
|
|
||||||
void signalHandler(int sig) {
|
void signalHandler(int sig) {
|
||||||
std::cout << "received signal: " << sig << std::endl;
|
std::cout << "received signal: " << sig << std::endl;
|
||||||
|
@ -13,7 +14,12 @@ void signalHandler(int sig) {
|
||||||
exit(1);
|
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;
|
static bool signalHandlerCreated = false;
|
||||||
if (!signalHandlerCreated) {
|
if (!signalHandlerCreated) {
|
||||||
signalHandlerCreated = true;
|
signalHandlerCreated = true;
|
||||||
|
@ -22,9 +28,9 @@ SwaybgProcess::SwaybgProcess(const std::string& wallpaperPath, const std::string
|
||||||
processes.push_back(this);
|
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) {
|
if (pipe(fds) == -1) {
|
||||||
throw std::runtime_error("Can't create pipe");
|
throw std::runtime_error("Can't create pipe");
|
||||||
}
|
}
|
||||||
|
@ -33,7 +39,8 @@ void SwaybgProcess::exec() {
|
||||||
dup2(fds[1], STDOUT_FILENO);
|
dup2(fds[1], STDOUT_FILENO);
|
||||||
close(fds[0]);
|
close(fds[0]);
|
||||||
close(fds[1]);
|
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) {
|
} else if (pid > 0) {
|
||||||
killed = false;
|
killed = false;
|
||||||
close(fds[1]);
|
close(fds[1]);
|
||||||
|
@ -44,14 +51,17 @@ void SwaybgProcess::exec() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void SwaybgProcess::kill() {
|
void Process::kill() {
|
||||||
if (killed) {
|
if (killed) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
killed = true;
|
killed = true;
|
||||||
::kill(pid, SIGKILL);
|
::kill(pid, SIGKILL);
|
||||||
int status;
|
wait();
|
||||||
::waitpid(pid, &status, 0);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int Process::wait() {
|
||||||
|
int status;
|
||||||
|
::waitpid(pid, &status, 0);
|
||||||
|
return status;
|
||||||
|
}
|
|
@ -5,24 +5,26 @@
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <sys/wait.h>
|
#include <sys/wait.h>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
#define SWAYBGPROCESS_BUFFER_SIZE 4094
|
#define SWAYBGPROCESS_BUFFER_SIZE 4094
|
||||||
|
|
||||||
class SwaybgProcess {
|
class Process {
|
||||||
public:
|
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 exec();
|
||||||
|
|
||||||
void kill();
|
void kill();
|
||||||
|
int wait();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
std::string wallpaperPath;
|
std::string execPath;
|
||||||
std::string output;
|
std::vector<const char*> args;
|
||||||
pid_t pid;
|
pid_t pid;
|
||||||
bool killed = true;
|
bool killed = true;
|
||||||
|
|
18
src/main.cpp
18
src/main.cpp
|
@ -1,6 +1,7 @@
|
||||||
#include "CliArguments.h"
|
#include "CliArguments.h"
|
||||||
|
#include "Process.h"
|
||||||
#include "Wallpapers.h"
|
#include "Wallpapers.h"
|
||||||
#include "SwaybgProcess.h"
|
#include "makeProcess.h"
|
||||||
#include <exception>
|
#include <exception>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <ostream>
|
#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",
|
.description = "if set, wallpapers order is based on the names of the files",
|
||||||
.type = CliArguments::Bool,
|
.type = CliArguments::Bool,
|
||||||
}},
|
}},
|
||||||
|
{"lock", {
|
||||||
|
.description = "Lock the screen using swaylock instead of applying a wallpaper",
|
||||||
|
.type = CliArguments::Bool,
|
||||||
|
}},
|
||||||
{"help", {
|
{"help", {
|
||||||
.aliases = {"h"},
|
.aliases = {"h"},
|
||||||
.description = "Show help",
|
.description = "Show help",
|
||||||
|
@ -63,14 +68,21 @@ int main(int argc, char** argv, char** envp) {
|
||||||
wallpapers.shuffle();
|
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();
|
swayBgProcess.exec();
|
||||||
|
|
||||||
if (arguments.exists("interval")) {
|
if (arguments.exists("interval")) {
|
||||||
int interval = arguments.getInt("interval");
|
int interval = arguments.getInt("interval");
|
||||||
while (true) {
|
while (true) {
|
||||||
sleep(interval);
|
sleep(interval);
|
||||||
SwaybgProcess newSwayBgProcess(wallpapers.next(), output);
|
Process newSwayBgProcess = makeSwaybgProcess(wallpapers.next(), output);
|
||||||
newSwayBgProcess.exec();
|
newSwayBgProcess.exec();
|
||||||
sleep(1);
|
sleep(1);
|
||||||
swayBgProcess.kill();
|
swayBgProcess.kill();
|
||||||
|
|
9
src/makeProcess.cpp
Normal file
9
src/makeProcess.cpp
Normal 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
7
src/makeProcess.h
Normal 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);
|
Loading…
Add table
Add a link
Reference in a new issue