2022-03-20 16:51:14 +01:00
|
|
|
#include <ctype.h>
|
2022-03-21 18:29:41 +01:00
|
|
|
#include <netdb.h>
|
|
|
|
#include <netinet/in.h>
|
2022-03-20 16:51:14 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2022-03-21 18:29:41 +01:00
|
|
|
#include <string.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
2022-05-24 19:42:43 +02:00
|
|
|
#include <sys/un.h>
|
2022-03-20 16:51:14 +01:00
|
|
|
#include <unistd.h>
|
2022-07-19 14:27:52 +02:00
|
|
|
#include <ranges>
|
2022-07-19 14:28:37 +02:00
|
|
|
#include <algorithm>
|
2022-03-20 16:51:14 +01:00
|
|
|
|
|
|
|
#include <iostream>
|
2022-03-21 18:29:41 +01:00
|
|
|
#include <string>
|
|
|
|
#include <fstream>
|
2022-03-20 16:51:14 +01:00
|
|
|
#include <string>
|
2022-07-15 11:58:09 +02:00
|
|
|
#include <deque>
|
2022-03-20 16:51:14 +01:00
|
|
|
|
2022-07-15 11:58:09 +02:00
|
|
|
const std::string USAGE = R"#(usage: hyprctl [(opt)flags] [command] [(opt)args]
|
2022-09-25 20:07:48 +02:00
|
|
|
|
2022-07-12 23:21:00 +02:00
|
|
|
commands:
|
2022-03-20 16:51:14 +01:00
|
|
|
monitors
|
|
|
|
workspaces
|
|
|
|
clients
|
2022-03-30 22:40:53 +02:00
|
|
|
activewindow
|
|
|
|
layers
|
2022-06-02 13:59:33 +02:00
|
|
|
devices
|
2023-01-06 14:32:25 +01:00
|
|
|
binds
|
2022-04-21 16:11:29 +02:00
|
|
|
dispatch
|
2022-04-21 16:56:27 +02:00
|
|
|
keyword
|
2022-04-22 18:14:25 +02:00
|
|
|
version
|
2022-06-27 13:42:20 +02:00
|
|
|
kill
|
2022-07-10 15:41:26 +02:00
|
|
|
splash
|
2022-07-02 18:27:44 +02:00
|
|
|
hyprpaper
|
2022-07-12 23:21:00 +02:00
|
|
|
reload
|
2022-08-10 21:22:11 +02:00
|
|
|
setcursor
|
2022-08-11 21:16:38 +02:00
|
|
|
getoption
|
2022-10-26 14:19:37 +02:00
|
|
|
cursorpos
|
2022-12-03 16:56:07 +01:00
|
|
|
switchxkblayout
|
2023-01-24 20:05:34 +01:00
|
|
|
seterror
|
|
|
|
setprop
|
2022-09-25 20:07:48 +02:00
|
|
|
|
2022-07-12 23:21:00 +02:00
|
|
|
flags:
|
2022-07-28 14:29:16 +02:00
|
|
|
-j -> output in JSON
|
|
|
|
--batch -> execute a batch of commands, separated by ';'
|
2022-07-12 23:21:00 +02:00
|
|
|
)#";
|
2022-03-20 16:51:14 +01:00
|
|
|
|
2023-01-22 16:38:17 +01:00
|
|
|
void request(std::string arg, int minArgs = 0) {
|
2023-01-24 20:05:34 +01:00
|
|
|
const auto SERVERSOCKET = socket(AF_UNIX, SOCK_STREAM, 0);
|
2022-03-20 16:51:14 +01:00
|
|
|
|
2023-01-24 20:05:34 +01:00
|
|
|
const auto ARGS = std::count(arg.begin(), arg.end(), ' ');
|
2022-12-03 16:56:07 +01:00
|
|
|
|
2023-01-24 20:05:34 +01:00
|
|
|
if (ARGS < minArgs) {
|
|
|
|
std::cout << "Not enough arguments, expected at least " << minArgs;
|
|
|
|
return;
|
2022-12-03 16:56:07 +01:00
|
|
|
}
|
|
|
|
|
2023-01-24 20:05:34 +01:00
|
|
|
if (SERVERSOCKET < 0) {
|
|
|
|
std::cout << "Couldn't open a socket (1)";
|
|
|
|
return;
|
2022-03-21 18:29:41 +01:00
|
|
|
}
|
2022-03-20 16:51:14 +01:00
|
|
|
|
2023-01-24 20:05:34 +01:00
|
|
|
// get the instance signature
|
|
|
|
auto instanceSig = getenv("HYPRLAND_INSTANCE_SIGNATURE");
|
2022-06-03 17:41:57 +02:00
|
|
|
|
2023-01-24 20:05:34 +01:00
|
|
|
if (!instanceSig) {
|
|
|
|
std::cout << "HYPRLAND_INSTANCE_SIGNATURE was not set! (Is Hyprland running?)";
|
|
|
|
return;
|
2022-06-03 17:41:57 +02:00
|
|
|
}
|
|
|
|
|
2023-01-24 20:05:34 +01:00
|
|
|
std::string instanceSigStr = std::string(instanceSig);
|
2022-06-03 17:41:57 +02:00
|
|
|
|
2023-01-24 20:05:34 +01:00
|
|
|
sockaddr_un serverAddress = {0};
|
|
|
|
serverAddress.sun_family = AF_UNIX;
|
2022-06-03 17:41:57 +02:00
|
|
|
|
2023-01-24 20:05:34 +01:00
|
|
|
std::string socketPath = "/tmp/hypr/" + instanceSigStr + "/.socket.sock";
|
2022-06-03 17:41:57 +02:00
|
|
|
|
2023-01-24 20:05:34 +01:00
|
|
|
strcpy(serverAddress.sun_path, socketPath.c_str());
|
2022-03-20 16:51:14 +01:00
|
|
|
|
2023-01-24 20:05:34 +01:00
|
|
|
if (connect(SERVERSOCKET, (sockaddr*)&serverAddress, SUN_LEN(&serverAddress)) < 0) {
|
|
|
|
std::cout << "Couldn't connect to " << socketPath << ". (3)";
|
|
|
|
return;
|
2022-03-21 18:29:41 +01:00
|
|
|
}
|
2022-03-20 16:51:14 +01:00
|
|
|
|
2023-01-24 20:05:34 +01:00
|
|
|
auto sizeWritten = write(SERVERSOCKET, arg.c_str(), arg.length());
|
2022-03-20 16:51:14 +01:00
|
|
|
|
2023-01-24 20:05:34 +01:00
|
|
|
if (sizeWritten < 0) {
|
|
|
|
std::cout << "Couldn't write (4)";
|
|
|
|
return;
|
2022-03-21 18:29:41 +01:00
|
|
|
}
|
2022-03-20 16:51:14 +01:00
|
|
|
|
2023-01-24 20:05:34 +01:00
|
|
|
std::string reply = "";
|
|
|
|
char buffer[8192] = {0};
|
2022-03-20 16:51:14 +01:00
|
|
|
|
2023-01-24 20:05:34 +01:00
|
|
|
sizeWritten = read(SERVERSOCKET, buffer, 8192);
|
2022-07-02 18:27:44 +02:00
|
|
|
|
2023-01-24 20:05:34 +01:00
|
|
|
if (sizeWritten < 0) {
|
|
|
|
std::cout << "Couldn't read (5)";
|
|
|
|
return;
|
2022-07-02 18:27:44 +02:00
|
|
|
}
|
|
|
|
|
2023-01-24 20:05:34 +01:00
|
|
|
reply += std::string(buffer, sizeWritten);
|
2022-10-26 14:11:05 +02:00
|
|
|
|
2023-01-24 20:05:34 +01:00
|
|
|
while (sizeWritten == 8192) {
|
|
|
|
sizeWritten = read(SERVERSOCKET, buffer, 8192);
|
|
|
|
if (sizeWritten < 0) {
|
|
|
|
std::cout << "Couldn't read (5)";
|
|
|
|
return;
|
2022-10-26 14:11:05 +02:00
|
|
|
}
|
2023-01-24 20:05:34 +01:00
|
|
|
reply += std::string(buffer, sizeWritten);
|
2022-10-26 14:11:05 +02:00
|
|
|
}
|
|
|
|
|
2023-01-24 20:05:34 +01:00
|
|
|
close(SERVERSOCKET);
|
2022-07-02 18:27:44 +02:00
|
|
|
|
2023-01-24 20:05:34 +01:00
|
|
|
std::cout << reply;
|
2022-07-02 18:27:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void requestHyprpaper(std::string arg) {
|
|
|
|
const auto SERVERSOCKET = socket(AF_UNIX, SOCK_STREAM, 0);
|
|
|
|
|
|
|
|
if (SERVERSOCKET < 0) {
|
|
|
|
std::cout << "Couldn't open a socket (1)";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// get the instance signature
|
|
|
|
auto instanceSig = getenv("HYPRLAND_INSTANCE_SIGNATURE");
|
|
|
|
|
|
|
|
if (!instanceSig) {
|
|
|
|
std::cout << "HYPRLAND_INSTANCE_SIGNATURE was not set! (Is Hyprland running?)";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string instanceSigStr = std::string(instanceSig);
|
|
|
|
|
|
|
|
sockaddr_un serverAddress = {0};
|
2022-12-20 03:18:47 +01:00
|
|
|
serverAddress.sun_family = AF_UNIX;
|
2022-07-02 18:27:44 +02:00
|
|
|
|
|
|
|
std::string socketPath = "/tmp/hypr/" + instanceSigStr + "/.hyprpaper.sock";
|
|
|
|
|
|
|
|
strcpy(serverAddress.sun_path, socketPath.c_str());
|
|
|
|
|
|
|
|
if (connect(SERVERSOCKET, (sockaddr*)&serverAddress, SUN_LEN(&serverAddress)) < 0) {
|
|
|
|
std::cout << "Couldn't connect to " << socketPath << ". (3)";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto sizeWritten = write(SERVERSOCKET, arg.c_str(), arg.length());
|
|
|
|
|
|
|
|
if (sizeWritten < 0) {
|
|
|
|
std::cout << "Couldn't write (4)";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
char buffer[8192] = {0};
|
|
|
|
|
|
|
|
sizeWritten = read(SERVERSOCKET, buffer, 8192);
|
2022-03-20 16:51:14 +01:00
|
|
|
|
2022-03-21 18:29:41 +01:00
|
|
|
if (sizeWritten < 0) {
|
|
|
|
std::cout << "Couldn't read (5)";
|
|
|
|
return;
|
|
|
|
}
|
2022-03-20 16:51:14 +01:00
|
|
|
|
2022-03-21 18:29:41 +01:00
|
|
|
close(SERVERSOCKET);
|
2022-03-20 16:51:14 +01:00
|
|
|
|
2022-03-21 18:29:41 +01:00
|
|
|
std::cout << std::string(buffer);
|
2022-03-20 16:51:14 +01:00
|
|
|
}
|
|
|
|
|
2022-10-25 18:04:11 +02:00
|
|
|
int dispatchRequest(int argc, char** argv) {
|
2022-04-21 16:11:29 +02:00
|
|
|
|
2023-02-01 11:01:56 +01:00
|
|
|
if (argc < 3) {
|
2022-10-25 18:04:11 +02:00
|
|
|
std::cout << "Usage: hyprctl dispatch <dispatcher> <arg>\n\
|
|
|
|
Execute a hyprland keybind dispatcher with the given argument";
|
|
|
|
return 1;
|
2022-04-21 16:11:29 +02:00
|
|
|
}
|
|
|
|
|
2022-08-22 19:16:35 +02:00
|
|
|
std::string rq = "/dispatch";
|
|
|
|
|
2022-12-20 03:18:47 +01:00
|
|
|
for (int i = 2; i < argc; i++) {
|
2022-11-10 16:36:36 +01:00
|
|
|
if (!strcmp(argv[i], "--"))
|
|
|
|
continue;
|
2022-08-22 19:16:35 +02:00
|
|
|
rq += " " + std::string(argv[i]);
|
2022-11-10 16:36:36 +01:00
|
|
|
}
|
2022-04-21 16:11:29 +02:00
|
|
|
|
|
|
|
request(rq);
|
2022-10-25 18:04:11 +02:00
|
|
|
return 0;
|
2022-04-21 16:11:29 +02:00
|
|
|
}
|
|
|
|
|
2022-10-25 18:04:11 +02:00
|
|
|
int keywordRequest(int argc, char** argv) {
|
2022-04-21 16:56:27 +02:00
|
|
|
if (argc < 4) {
|
2022-10-25 18:04:11 +02:00
|
|
|
std::cout << "Usage: hyprctl keyword <keyword> <arg>\n\
|
|
|
|
Execute a hyprland keyword with the given argument";
|
|
|
|
return 1;
|
2022-04-21 16:56:27 +02:00
|
|
|
}
|
|
|
|
|
2022-08-22 23:25:11 +02:00
|
|
|
std::string rq = "/keyword";
|
|
|
|
|
2022-12-20 03:18:47 +01:00
|
|
|
for (int i = 2; i < argc; i++)
|
2022-08-22 23:25:11 +02:00
|
|
|
rq += " " + std::string(argv[i]);
|
2022-04-21 16:56:27 +02:00
|
|
|
|
|
|
|
request(rq);
|
2022-10-25 18:04:11 +02:00
|
|
|
return 0;
|
2022-04-21 16:56:27 +02:00
|
|
|
}
|
|
|
|
|
2022-10-25 18:04:11 +02:00
|
|
|
int hyprpaperRequest(int argc, char** argv) {
|
2022-07-02 18:27:44 +02:00
|
|
|
if (argc < 4) {
|
2022-10-25 18:04:11 +02:00
|
|
|
std::cout << "Usage: hyprctl hyprpaper <command> <arg>\n\
|
|
|
|
Execute a hyprpaper command with the given argument";
|
|
|
|
return 1;
|
2022-07-02 18:27:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string rq = std::string(argv[2]) + " " + std::string(argv[3]);
|
|
|
|
|
|
|
|
requestHyprpaper(rq);
|
2022-10-25 18:04:11 +02:00
|
|
|
return 0;
|
2022-07-02 18:27:44 +02:00
|
|
|
}
|
|
|
|
|
2022-10-25 18:04:11 +02:00
|
|
|
int setcursorRequest(int argc, char** argv) {
|
2022-08-10 21:22:11 +02:00
|
|
|
if (argc < 4) {
|
2022-10-25 18:04:11 +02:00
|
|
|
std::cout << "Usage: hyprctl setcursor <theme> <size>\n\
|
|
|
|
Sets the cursor theme for everything except GTK and reloads the cursor";
|
|
|
|
return 1;
|
2022-08-10 21:22:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string rq = "setcursor " + std::string(argv[2]) + " " + std::string(argv[3]);
|
|
|
|
|
|
|
|
request(rq);
|
2022-10-25 18:04:11 +02:00
|
|
|
return 0;
|
2022-08-10 21:22:11 +02:00
|
|
|
}
|
|
|
|
|
2022-11-05 19:04:44 +01:00
|
|
|
int outputRequest(int argc, char** argv) {
|
|
|
|
if (argc < 4) {
|
|
|
|
std::cout << "Usage: hyprctl output <mode> <name>\n\
|
|
|
|
creates / destroys a fake output\n\
|
|
|
|
with create, name is the backend name to use (available: auto, x11, wayland, headless)\n\
|
|
|
|
with destroy, name is the output name to destroy";
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string rq = "output " + std::string(argv[2]) + " " + std::string(argv[3]);
|
|
|
|
|
|
|
|
request(rq);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-07-13 00:34:28 +02:00
|
|
|
void batchRequest(std::string arg) {
|
|
|
|
std::string rq = "[[BATCH]]" + arg.substr(arg.find_first_of(" ") + 1);
|
2022-09-25 20:07:48 +02:00
|
|
|
|
2022-04-29 19:44:09 +02:00
|
|
|
request(rq);
|
|
|
|
}
|
|
|
|
|
2022-07-15 11:58:09 +02:00
|
|
|
std::deque<std::string> splitArgs(int argc, char** argv) {
|
|
|
|
std::deque<std::string> result;
|
|
|
|
|
|
|
|
for (auto i = 1 /* skip the executable */; i < argc; ++i)
|
|
|
|
result.push_back(std::string(argv[i]));
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2022-07-19 14:24:03 +02:00
|
|
|
bool isNumber(const std::string& str, bool allowfloat) {
|
2022-11-11 15:04:35 +01:00
|
|
|
if (str.empty())
|
|
|
|
return false;
|
2022-07-19 14:24:03 +02:00
|
|
|
return std::ranges::all_of(str.begin(), str.end(), [&](char c) { return isdigit(c) != 0 || c == '-' || (allowfloat && c == '.'); });
|
|
|
|
}
|
|
|
|
|
2022-03-20 16:51:14 +01:00
|
|
|
int main(int argc, char** argv) {
|
2022-12-20 03:18:47 +01:00
|
|
|
int bflag = 0, sflag = 0, index, c;
|
2022-11-10 16:36:36 +01:00
|
|
|
bool parseArgs = true;
|
2022-03-20 16:51:14 +01:00
|
|
|
|
|
|
|
if (argc < 2) {
|
2022-06-17 12:18:25 +02:00
|
|
|
printf("%s\n", USAGE.c_str());
|
2022-03-20 16:51:14 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2022-07-13 00:34:28 +02:00
|
|
|
std::string fullRequest = "";
|
2022-12-20 03:18:47 +01:00
|
|
|
std::string fullArgs = "";
|
|
|
|
const auto ARGS = splitArgs(argc, argv);
|
2022-07-15 11:58:09 +02:00
|
|
|
|
|
|
|
for (auto i = 0; i < ARGS.size(); ++i) {
|
2022-11-10 16:36:36 +01:00
|
|
|
if (ARGS[i] == "--") {
|
|
|
|
// Stop parsing arguments after --
|
|
|
|
parseArgs = false;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (parseArgs && (ARGS[i][0] == '-') && !isNumber(ARGS[i], true) /* For stuff like -2 */) {
|
2022-07-15 11:58:09 +02:00
|
|
|
// parse
|
|
|
|
if (ARGS[i] == "-j" && !fullArgs.contains("j")) {
|
|
|
|
fullArgs += "j";
|
|
|
|
} else if (ARGS[i] == "--batch") {
|
|
|
|
fullRequest = "--batch ";
|
|
|
|
} else {
|
|
|
|
printf("%s\n", USAGE.c_str());
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
fullRequest += ARGS[i] + " ";
|
2022-07-13 00:34:28 +02:00
|
|
|
}
|
2022-07-15 11:58:09 +02:00
|
|
|
|
2022-07-15 21:39:39 +02:00
|
|
|
if (fullRequest.empty()) {
|
|
|
|
printf("%s\n", USAGE.c_str());
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2022-07-13 00:34:28 +02:00
|
|
|
fullRequest.pop_back(); // remove trailing space
|
2022-07-12 23:17:55 +02:00
|
|
|
|
2022-07-15 11:58:09 +02:00
|
|
|
fullRequest = fullArgs + "/" + fullRequest;
|
2022-12-20 03:18:47 +01:00
|
|
|
|
2022-10-25 18:04:11 +02:00
|
|
|
int exitStatus = 0;
|
2022-07-12 23:17:55 +02:00
|
|
|
|
2022-12-20 03:18:47 +01:00
|
|
|
if (fullRequest.contains("/--batch"))
|
|
|
|
batchRequest(fullRequest);
|
|
|
|
else if (fullRequest.contains("/monitors"))
|
|
|
|
request(fullRequest);
|
|
|
|
else if (fullRequest.contains("/clients"))
|
|
|
|
request(fullRequest);
|
|
|
|
else if (fullRequest.contains("/workspaces"))
|
|
|
|
request(fullRequest);
|
|
|
|
else if (fullRequest.contains("/activewindow"))
|
|
|
|
request(fullRequest);
|
|
|
|
else if (fullRequest.contains("/layers"))
|
|
|
|
request(fullRequest);
|
|
|
|
else if (fullRequest.contains("/version"))
|
|
|
|
request(fullRequest);
|
|
|
|
else if (fullRequest.contains("/kill"))
|
|
|
|
request(fullRequest);
|
|
|
|
else if (fullRequest.contains("/splash"))
|
|
|
|
request(fullRequest);
|
|
|
|
else if (fullRequest.contains("/devices"))
|
|
|
|
request(fullRequest);
|
|
|
|
else if (fullRequest.contains("/reload"))
|
|
|
|
request(fullRequest);
|
|
|
|
else if (fullRequest.contains("/getoption"))
|
|
|
|
request(fullRequest);
|
2023-01-06 14:32:25 +01:00
|
|
|
else if (fullRequest.contains("/binds"))
|
|
|
|
request(fullRequest);
|
2022-12-20 03:18:47 +01:00
|
|
|
else if (fullRequest.contains("/cursorpos"))
|
|
|
|
request(fullRequest);
|
2023-01-25 16:16:28 +01:00
|
|
|
else if (fullRequest.contains("/animations"))
|
|
|
|
request(fullRequest);
|
2022-12-20 03:18:47 +01:00
|
|
|
else if (fullRequest.contains("/switchxkblayout"))
|
|
|
|
request(fullRequest, 2);
|
2023-01-22 16:38:17 +01:00
|
|
|
else if (fullRequest.contains("/seterror"))
|
|
|
|
request(fullRequest, 1);
|
2023-01-24 20:05:34 +01:00
|
|
|
else if (fullRequest.contains("/setprop"))
|
|
|
|
request(fullRequest, 3);
|
2022-12-20 03:18:47 +01:00
|
|
|
else if (fullRequest.contains("/output"))
|
|
|
|
exitStatus = outputRequest(argc, argv);
|
|
|
|
else if (fullRequest.contains("/setcursor"))
|
|
|
|
exitStatus = setcursorRequest(argc, argv);
|
|
|
|
else if (fullRequest.contains("/dispatch"))
|
|
|
|
exitStatus = dispatchRequest(argc, argv);
|
|
|
|
else if (fullRequest.contains("/keyword"))
|
|
|
|
exitStatus = keywordRequest(argc, argv);
|
|
|
|
else if (fullRequest.contains("/hyprpaper"))
|
|
|
|
exitStatus = hyprpaperRequest(argc, argv);
|
|
|
|
else if (fullRequest.contains("/--help"))
|
|
|
|
printf("%s", USAGE.c_str());
|
2022-03-20 16:51:14 +01:00
|
|
|
else {
|
2022-06-17 12:18:25 +02:00
|
|
|
printf("%s\n", USAGE.c_str());
|
2022-03-20 16:51:14 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2022-06-17 12:18:25 +02:00
|
|
|
printf("\n");
|
2022-10-25 18:04:11 +02:00
|
|
|
return exitStatus;
|
2022-06-17 22:02:57 +02:00
|
|
|
}
|