ArKanjo 0.5.1
A tool for find code duplicated functions in codebases
posix_utils.cpp
Go to the documentation of this file.
2
3#include <termios.h>
4#include <sys/ioctl.h>
5#include <unistd.h>
6#include <sys/utsname.h>
7#include <tuple>
8
9int convert_16_bit_to_8_bit(const std::string& hex16) {
10 try {
11 unsigned long value = std::stoul(hex16, nullptr, 16);
12 return static_cast<int>(value / 256);
13 } catch (...) {
14 return 0;
15 }
16}
17
18std::tuple<int, int, int> UtilsOSDependable::parse_terminal_color_response(const std::string& response) {
19 const size_t start_pos = response.find("rgb:");
20 if (start_pos == std::string::npos) {
21 return {0, 0, 0};
22 }
23
24 const size_t end_pos = response.find("\033\\", start_pos);
25 if (end_pos == std::string::npos) {
26 return {0, 0, 0};
27 }
28
29 std::string rgb_str = response.substr(start_pos + 4, end_pos - (start_pos + 4));
30
31 replace(rgb_str.begin(), rgb_str.end(), '/', ' ');
32
33 std::istringstream iss(rgb_str);
34 std::string r_hex, g_hex, b_hex;
35 iss >> r_hex >> g_hex >> b_hex;
36
37 int r = convert_16_bit_to_8_bit(r_hex);
38 int g = convert_16_bit_to_8_bit(g_hex);
39 int b = convert_16_bit_to_8_bit(b_hex);
40
41 return {r, g, b};
42}
43
44std::string UtilsOSDependable::capture_terminal_response() {
45 int tty = open("/dev/tty", O_RDWR);
46 if (tty < 0)
47 return "";
48
49 termios oldt, newt;
50 tcgetattr(tty, &oldt);
51
52 newt = oldt;
53 newt.c_lflag &= ~(ICANON | ECHO);
54
55 tcsetattr(tty, TCSANOW, &newt);
56
57 int oldf = fcntl(tty, F_GETFL, 0);
58 fcntl(tty, F_SETFL, oldf | O_NONBLOCK);
59
60 // OSC 11 query (background color)
61 write(tty, "\033]11;?\033\\", 8);
62
63 std::string response;
64 char ch;
65
66 fd_set readfds;
67 timeval tv{};
68
69 while (true) {
70 FD_ZERO(&readfds);
71 FD_SET(tty, &readfds);
72
73 tv.tv_sec = 0;
74 tv.tv_usec = 100000; // 100ms timeout
75
76 int ret = select(tty + 1, &readfds, nullptr, nullptr, &tv);
77
78 if (ret <= 0)
79 break;
80
81 if (read(tty, &ch, 1) > 0) {
82 response += ch;
83 } else {
84 break;
85 }
86 }
87
88 tcsetattr(tty, TCSANOW, &oldt);
89 fcntl(tty, F_SETFL, oldf);
90 close(tty);
91
92 return response;
93}
94
95float UtilsOSDependable::get_terminal_bg_color_luminance() {
96 if (!isatty(fileno(stdout)) || !isatty(fileno(stdin))) {
97 return 0;
98 }
99
100 std::string color_str = capture_terminal_response();
101 auto [r, g, b] = parse_terminal_color_response(color_str);
102 float luminance = 0.2126 * (r / 255.0) + 0.7152 * (g / 255.0) + 0.0722 * (b / 255.0);
103
104 return luminance;
105}
106
108 return get_terminal_bg_color_luminance() <= 0.5;
109}
110
111int UtilsOSDependable::run_process(const char* cmd, char* const argv[]) {
112 execvp(cmd, argv);
113 return -1;
114}
static int run_process(const char *cmd, char *const argv[])
Executes an external program, replacing the current process (exec-style).
static bool is_bg_color_dark()
Determines if terminal background color is dark.
utility functions
int convert_16_bit_to_8_bit(const std::string &hex16)
Definition: posix_utils.cpp:9