-
Notifications
You must be signed in to change notification settings - Fork 0
/
x_display.cpp
123 lines (106 loc) · 2.53 KB
/
x_display.cpp
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/**
* @file x_display.cpp
* @brief
*
* Details
*
* @author First_Last_Name username
* @date 11-08-2018
* @version 0.0
*/
#include <stdexcept>
#include "x_display.h"
XDisplay* XDisplay::singleton = nullptr;
//
// Default Constructor
//
XDisplay::XDisplay() {
// Connect to x11 server
if((this->display = XOpenDisplay(nullptr)) == nullptr)
throw std::domain_error("Could not open display.");
this->screen = XDefaultScreen(this->display);
this->root_window = RootWindow(this->display, this->screen);
this->cmap = DefaultColormap(this->display, this->screen);
this->wmDeleteMessage = XInternAtom(display, "WM_DELETE_WINDOW", False);
}
//
// get() : XDisplay*
//
XDisplay* XDisplay::get() {
if(singleton == nullptr)
singleton = new XDisplay();
return singleton;
}
//
// getDisplay() : Display*
//
Display* XDisplay::getDisplay() {
return XDisplay::get()->display;
}
//
// getScreen() : int
//
int XDisplay::getScreen() {
return XDisplay::get()->screen;
}
//
// getRootWindow() : Window
//
Window XDisplay::getRootWindow() {
return XDisplay::get()->root_window;
}
//
// getWMDeleteMessage() : Atom*
//
Atom XDisplay::getWMDeleteMessage() {
return XDisplay::get()->wmDeleteMessage;
}
//
// getColormap() : Colormap
//
Colormap XDisplay::getColormap() {
return XDisplay::get()->cmap;
}
//
// getColor(const char*) : unsigned long
//
unsigned long XDisplay::getColor(const char* color_name) {
unsigned long pixel = 0;
// Check if the color has been requested before
auto colors = XDisplay::getColors();
auto color = colors.find(std::string(color_name));
if(color == colors.end()) {
// The Color has not been requested before
// we need to build it.
XColor color;
XParseColor(XDisplay::getDisplay(), XDisplay::getColormap(),
color_name, &color);
XAllocColor(XDisplay::getDisplay(), XDisplay::getColormap(), &color);
pixel = color.pixel;
colors[std::string(color_name)] = pixel;
}
else {
// The color was requested before so just return its pixel value
pixel = colors[std::string(color_name)];
}
return pixel;
}
std::unordered_map<std::string, unsigned long> XDisplay::getColors() {
return XDisplay::get()->colors;
}
//
// Destructor
//
XDisplay::~XDisplay() {
// Free Colors
auto colors = XDisplay::getColors();
unsigned long pixels[colors.size()];
int index = 0;
for(const auto& pixel : colors) {
pixels[index] = pixel.second;
++index;
}
XFreeColors(this->display, this->cmap, pixels, colors.size(), (unsigned long)0);
XFreeColormap(this->display, this->cmap);
XCloseDisplay(this->display);
}