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