forked from M-EMBABI/Pc-remote-control
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand.cpp
More file actions
35 lines (32 loc) · 969 Bytes
/
Copy pathcommand.cpp
File metadata and controls
35 lines (32 loc) · 969 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include "command.hpp"
#include <cstdlib>
#include <iostream>
#include <algorithm>
Command::Command() : commands{
{"calculator", "gnome-calculator"},
{"terminal", "gnome-terminal"},
{"firefox", "firefox"},
{"date", "date"},
{"battery", "upower -i /org/freedesktop/UPower/devices/battery_BAT0 | grep 'percentage'"},
{"shutdown", "shutdown now"}
} {}
std::string Command::execute_command(const std::string& cmd) {
std::string cleaned_cmd = cmd;
// Use popen to capture output and error messages
std::string result;
FILE* fp = popen(cleaned_cmd.c_str(), "r");
if (fp) {
char buffer[128];
while (fgets(buffer, sizeof(buffer), fp) != nullptr) {
result += buffer;
}
int status = pclose(fp);
if (status == 0) {
return result;
} else {
return "Command execution failed";
}
} else {
return "Failed to execute command";
}
}