summaryrefslogtreecommitdiff
path: root/hyprctl/main.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'hyprctl/main.cpp')
-rw-r--r--hyprctl/main.cpp48
1 files changed, 29 insertions, 19 deletions
diff --git a/hyprctl/main.cpp b/hyprctl/main.cpp
index bdf354e7..34abe46f 100644
--- a/hyprctl/main.cpp
+++ b/hyprctl/main.cpp
@@ -30,6 +30,7 @@
#define PAD
std::string instanceSignature;
+bool quiet = false;
struct SInstanceData {
std::string id;
@@ -39,6 +40,13 @@ struct SInstanceData {
bool valid = true;
};
+void log(std::string str) {
+ if (quiet)
+ return;
+
+ std::cout << str;
+}
+
std::string getRuntimeDir() {
const auto XDG = getenv("XDG_RUNTIME_DIR");
@@ -96,17 +104,17 @@ int request(std::string arg, int minArgs = 0) {
const auto ARGS = std::count(arg.begin(), arg.end(), ' ');
if (ARGS < minArgs) {
- std::cout << "Not enough arguments, expected at least " << minArgs;
+ log("Not enough arguments, expected at least " + minArgs);
return -1;
}
if (SERVERSOCKET < 0) {
- std::cout << "Couldn't open a socket (1)";
+ log("Couldn't open a socket (1)");
return 1;
}
if (instanceSignature.empty()) {
- std::cout << "HYPRLAND_INSTANCE_SIGNATURE was not set! (Is Hyprland running?)";
+ log("HYPRLAND_INSTANCE_SIGNATURE was not set! (Is Hyprland running?)");
return 2;
}
@@ -120,14 +128,14 @@ int request(std::string arg, int minArgs = 0) {
strncpy(serverAddress.sun_path, socketPath.c_str(), sizeof(serverAddress.sun_path) - 1);
if (connect(SERVERSOCKET, (sockaddr*)&serverAddress, SUN_LEN(&serverAddress)) < 0) {
- std::cout << "Couldn't connect to " << socketPath << ". (3)";
+ log("Couldn't connect to " + socketPath + ". (3)");
return 3;
}
auto sizeWritten = write(SERVERSOCKET, arg.c_str(), arg.length());
if (sizeWritten < 0) {
- std::cout << "Couldn't write (4)";
+ log("Couldn't write (4)");
return 4;
}
@@ -137,7 +145,7 @@ int request(std::string arg, int minArgs = 0) {
sizeWritten = read(SERVERSOCKET, buffer, 8192);
if (sizeWritten < 0) {
- std::cout << "Couldn't read (5)";
+ log("Couldn't read (5)");
return 5;
}
@@ -146,7 +154,7 @@ int request(std::string arg, int minArgs = 0) {
while (sizeWritten == 8192) {
sizeWritten = read(SERVERSOCKET, buffer, 8192);
if (sizeWritten < 0) {
- std::cout << "Couldn't read (5)";
+ log("Couldn't read (5)");
return 5;
}
reply += std::string(buffer, sizeWritten);
@@ -154,7 +162,7 @@ int request(std::string arg, int minArgs = 0) {
close(SERVERSOCKET);
- std::cout << reply;
+ log(reply);
return 0;
}
@@ -163,12 +171,12 @@ int requestHyprpaper(std::string arg) {
const auto SERVERSOCKET = socket(AF_UNIX, SOCK_STREAM, 0);
if (SERVERSOCKET < 0) {
- std::cout << "Couldn't open a socket (1)";
+ log("Couldn't open a socket (1)");
return 1;
}
if (instanceSignature.empty()) {
- std::cout << "HYPRLAND_INSTANCE_SIGNATURE was not set! (Is Hyprland running?)";
+ log("HYPRLAND_INSTANCE_SIGNATURE was not set! (Is Hyprland running?)");
return 2;
}
@@ -182,7 +190,7 @@ int requestHyprpaper(std::string arg) {
strncpy(serverAddress.sun_path, socketPath.c_str(), sizeof(serverAddress.sun_path) - 1);
if (connect(SERVERSOCKET, (sockaddr*)&serverAddress, SUN_LEN(&serverAddress)) < 0) {
- std::cout << "Couldn't connect to " << socketPath << ". (3)";
+ log("Couldn't connect to " + socketPath + ". (3)");
return 3;
}
@@ -192,7 +200,7 @@ int requestHyprpaper(std::string arg) {
auto sizeWritten = write(SERVERSOCKET, arg.c_str(), arg.length());
if (sizeWritten < 0) {
- std::cout << "Couldn't write (4)";
+ log("Couldn't write (4)");
return 4;
}
@@ -201,13 +209,13 @@ int requestHyprpaper(std::string arg) {
sizeWritten = read(SERVERSOCKET, buffer, 8192);
if (sizeWritten < 0) {
- std::cout << "Couldn't read (5)";
+ log("Couldn't read (5)");
return 5;
}
close(SERVERSOCKET);
- std::cout << std::string(buffer);
+ log(std::string(buffer));
return 0;
}
@@ -250,7 +258,7 @@ void instancesRequest(bool json) {
result += "\n]";
}
- std::cout << result << "\n";
+ log(result + "\n");
}
std::deque<std::string> splitArgs(int argc, char** argv) {
@@ -310,6 +318,8 @@ int main(int argc, char** argv) {
}
overrideInstance = ARGS[i];
+ } else if (ARGS[i] == "-q" || ARGS[i] == "--quiet") {
+ quiet = true;
} else if (ARGS[i] == "--help") {
const std::string& cmd = ARGS[0];
@@ -360,7 +370,7 @@ int main(int argc, char** argv) {
instanceSignature = overrideInstance;
else if (!overrideInstance.empty()) {
if (!isNumber(overrideInstance, false)) {
- std::cout << "instance invalid\n";
+ log("instance invalid\n");
return 1;
}
@@ -369,7 +379,7 @@ int main(int argc, char** argv) {
const auto INSTANCES = instances();
if (INSTANCENO < 0 || static_cast<std::size_t>(INSTANCENO) >= INSTANCES.size()) {
- std::cout << "no such instance\n";
+ log("no such instance\n");
return 1;
}
@@ -378,7 +388,7 @@ int main(int argc, char** argv) {
const auto ISIG = getenv("HYPRLAND_INSTANCE_SIGNATURE");
if (!ISIG) {
- std::cout << "HYPRLAND_INSTANCE_SIGNATURE not set! (is hyprland running?)\n";
+ log("HYPRLAND_INSTANCE_SIGNATURE not set! (is hyprland running?)\n");
return 1;
}
@@ -419,6 +429,6 @@ int main(int argc, char** argv) {
exitStatus = request(fullRequest);
}
- std::cout << std::endl;
+ std::cout << std::flush;
return exitStatus;
}