Arkanjo 0.2
A tool for find code duplicated functions in codebases
Loading...
Searching...
No Matches
windows_utils.cpp
Go to the documentation of this file.
2
3#include <iostream>
4#include <string>
5
6#ifdef _WIN32
7#include <windows.h>
8#include <process.h>
9
10#ifndef ENABLE_VIRTUAL_TERMINAL_PROCESSING
11#define ENABLE_VIRTUAL_TERMINAL_PROCESSING 0x0004
12#endif
13
14std::tuple<int, int, int> UtilsOSDependable::parse_terminal_color_response() {
15 HANDLE hInput = GetStdHandle(STD_INPUT_HANDLE);
16 HANDLE hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
17
18 // Save original console mode
19 DWORD originalOutMode = 0;
20 GetConsoleMode(hOutput, &originalOutMode);
21
22 // Enable virtual terminal processing
23 SetConsoleMode(hOutput, originalOutMode | ENABLE_VIRTUAL_TERMINAL_PROCESSING);
24
25 // Set input to raw mode (disable line input and echo input)
26 DWORD originalInMode = 0;
27 GetConsoleMode(hInput, &originalInMode);
28 SetConsoleMode(hInput, originalInMode & ~(ENABLE_LINE_INPUT | ENABLE_ECHO_INPUT));
29
30 // Send OSC 11 query for background color
31 const char* query = "\x1b]11;?\x1b\\";
32 DWORD written = 0;
33 WriteConsoleA(hOutput, query, (DWORD)strlen(query), &written, nullptr);
34
35 // Read raw input
36 std::string response;
37 char buffer[256];
38 DWORD read = 0;
39 for (int i = 0; i < 20; ++i) {
40 if (ReadFile(hInput, buffer, sizeof(buffer), &read, nullptr) && read > 0) {
41 response.append(buffer, read);
42 if (response.find('\x1b') != std::string::npos && response.find("rgb:") != std::string::npos) {
43 break;
44 }
45 }
46 Sleep(50);
47 }
48
49 // Restore original modes
50 SetConsoleMode(hInput, originalInMode);
51 SetConsoleMode(hOutput, originalOutMode);
52
53 // Parse RGB from response
54 size_t start = response.find("rgb:");
55 if (start == std::string::npos)
56 return {0,0,0};
57 start += 4;
58
59 size_t end = response.find("/", start);
60 if (end == std::string::npos)
61 return {0,0,0};
62 std::string rStr = response.substr(start, end - start);
63
64 start = end + 1;
65 end = response.find("/", start);
66 if (end == std::string::npos)
67 return {0,0,0};
68 std::string gStr = response.substr(start, end - start);
69
70 start = end + 1;
71 end = response.find("\x1b", start);
72 if (end == std::string::npos)
73 return {0,0,0};
74 std::string bStr = response.substr(start, end - start);
75
76 try {
77 auto r = std::stoi(rStr, nullptr, 16) / 256;
78 auto g = std::stoi(gStr, nullptr, 16) / 256;
79 auto b = std::stoi(bStr, nullptr, 16) / 256;
80 return {r, g, b};
81 } catch (...) {
82 return {0, 0, 0};
83 }
84}
85
86float UtilsOSDependable::get_terminal_bg_color_luminance() {
87 auto [r, g, b] = parse_terminal_color_response();
88 float luminance = 0.2126 * (r / 255.0) + 0.7152 * (g / 255.0) + 0.0722 * (b / 255.0);
89
90 return luminance;
91}
92
94 return get_terminal_bg_color_luminance() <= 0.5;
95}
96
97int UtilsOSDependable::run_process(const char* cmd, char* const argv[]) {
98 return _spawnvp(_P_OVERLAY, cmd, argv);
99}
100
101#endif
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