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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
|
#include <stdbool.h>
#include <assert.h>
#include <glib.h>
#include <math.h>
#include <cairo.h>
#include <cairo-xcb.h>
#include <xcb/xcb.h>
#include <xcb/xcb_aux.h>
#include <xcb/xproto.h>
#include <xcb/xcb_icccm.h>
#include <xcb/xcb_xrm.h>
#include <xcb/randr.h>
#include <xcb/xcb_atom.h>
#include "x11.h"
#include "log.h"
struct Window {
xcb_screen_t *screen;
xcb_connection_t *connection;
xcb_xrm_database_t *database;
xcb_drawable_t window;
xcb_visualtype_t *visual_type;
cairo_surface_t *surface;
cairo_t *cr;
double dpi;
};
static xcb_atom_t intern_atom(Window *win, const char *atom)
{
// TODO: Error handling
xcb_intern_atom_cookie_t cookie = xcb_intern_atom(win->connection, false, strlen(atom), atom);
return xcb_intern_atom_reply(win->connection, cookie, NULL)->atom;
}
Window *window_create(void)
{
Window *win = g_malloc0(sizeof(Window));
int preferred_screen = 0;
win->connection = xcb_connect(NULL, &preferred_screen);
assert(win->connection != NULL && !xcb_connection_has_error(win->connection));
log_debug("Xcb established connection");
xcb_screen_iterator_t iter = xcb_setup_roots_iterator(xcb_get_setup(win->connection));
log_debug("Default screen %d", preferred_screen);
while (preferred_screen != 0 && iter.rem) {
xcb_screen_next(&iter);
preferred_screen--;
}
win->screen = iter.data;
xcb_randr_query_version_cookie_t version_cookie = xcb_randr_query_version(win->connection,
XCB_RANDR_MAJOR_VERSION,
XCB_RANDR_MINOR_VERSION);
xcb_generic_error_t *error;
xcb_randr_query_version_reply_t *randr_version = xcb_randr_query_version_reply(win->connection,
version_cookie,
&error);
log_debug("RandR version %d.%d", randr_version->major_version, randr_version->minor_version);
assert(error == NULL || randr_version->major_version < 1);
xcb_randr_get_screen_info_cookie_t cookie = xcb_randr_get_screen_info(win->connection, win->screen->root);
xcb_randr_get_screen_info_reply_t *info_reply = xcb_randr_get_screen_info_reply(win->connection, cookie, &error);
assert(error == NULL);
xcb_randr_screen_size_t *screen_size = xcb_randr_get_screen_info_sizes(info_reply);
log_debug("Screen size [width=%d, height=%d]", screen_size->width, screen_size->height);
xcb_randr_select_input(win->connection,
win->window,
XCB_RANDR_NOTIFY_MASK_SCREEN_CHANGE |
XCB_RANDR_NOTIFY_MASK_OUTPUT_CHANGE |
XCB_RANDR_NOTIFY_MASK_OUTPUT_PROPERTY);
win->database = xcb_xrm_database_from_default(win->connection);
assert(win->database != NULL);
char *dpi_value;
if (xcb_xrm_resource_get_string(win->database, "Xft.dpi", "Xft.dpi", &dpi_value) >= 0) {
log_debug("Xrm query '%s' found '%s'", "Xft.dpi", dpi_value);
win->dpi = strtod(dpi_value, NULL);
g_free(dpi_value);
} else {
log_debug("Xrm query '%s' not found", "Xft.dpi");
win->dpi = 96.0;
//win->dpi = (double)screen_size->height * 25.4 / (double)screen_size->mheight;
log_debug("Fallback dpi value '%lf'", win->dpi);
}
xcb_depth_iterator_t depth_iter = xcb_screen_allowed_depths_iterator(win->screen);
while (depth_iter.rem) {
xcb_visualtype_iterator_t visual_iter = xcb_depth_visuals_iterator(depth_iter.data);
while (visual_iter.rem) {
if (win->screen->root_visual == visual_iter.data->visual_id) {
win->visual_type = visual_iter.data;
goto found_visual;
}
xcb_visualtype_next(&visual_iter);
}
xcb_depth_next(&depth_iter);
}
found_visual:
log_debug("Xcb visual type found (id %u)", win->visual_type->visual_id);
const uint32_t value_mask = XCB_CW_BACK_PIXMAP | XCB_CW_BACK_PIXEL | XCB_CW_BORDER_PIXEL
| XCB_CW_OVERRIDE_REDIRECT | XCB_CW_EVENT_MASK | XCB_CW_COLORMAP;
const uint32_t event_mask = XCB_EVENT_MASK_EXPOSURE | XCB_EVENT_MASK_STRUCTURE_NOTIFY
| XCB_EVENT_MASK_KEY_PRESS | XCB_EVENT_MASK_FOCUS_CHANGE
| XCB_EVENT_MASK_BUTTON_RELEASE | XCB_EVENT_MASK_BUTTON_PRESS;
xcb_colormap_t colormap = xcb_generate_id(win->connection);
xcb_create_colormap(win->connection, XCB_COLORMAP_ALLOC_NONE, colormap, win->screen->root, win->visual_type->visual_id);
log_debug("Xcb colormap created (id %u)", colormap);
const uint32_t value_list[] = {
XCB_NONE, // back pixmap
0x000000, // back pixel
0x000000, // border pixel
true, // override redirect
event_mask, // event mask
colormap // colormap
};
int width = screen_size->width * 0.98;
int height = screen_size->height * 0.03;
width -= width % 2 != 0;
height -= height % 2 != 0;
log_debug("Window size [width=%d, height=%d]", width, height);
int x = (screen_size->width - width) / 2.0;
int y = screen_size->height * 0.0125;
log_debug("Window position [x=%d, y=%d]", x, y);
win->window = xcb_generate_id(win->connection);
xcb_create_window(win->connection,
XCB_COPY_FROM_PARENT,
win->window, win->screen->root,
x, y, width, height, 0,
XCB_WINDOW_CLASS_INPUT_OUTPUT,
win->screen->root_visual,
value_mask,
value_list);
log_debug("Xcb window created (id %u)", win->window);
xcb_icccm_set_wm_name(win->connection, win->window, XCB_ATOM_STRING, 8, strlen("comet"), "comet");
xcb_map_window(win->connection, win->window);
xcb_atom_t NET_WM_WINDOW_OPACITY = intern_atom(win, "_NET_WM_WINDOW_OPACITY");
xcb_atom_t CARDINAL = intern_atom(win, "CARDINAL");
// Value between 0 and 1
double transparency = 0.8;
unsigned long opacity = 0xffffffff * transparency;
//error =
//xcb_request_check(win->connection,
//xcb_change_property_checked(win->connection,
// XCB_PROP_MODE_REPLACE,
// win->window,
// NET_WM_WINDOW_OPACITY,
// CARDINAL,
// 32,
// 1,
// (char *)&opacity));
//assert(error == NULL);
//log_debug("Window '%s' set to '%d%%'", "opacity", (int)round(100 - 100 * transparency));
xcb_flush(win->connection);
log_debug("Xcb initialized");
win->surface = cairo_xcb_surface_create(win->connection, win->window, win->visual_type, width, height);
assert(cairo_surface_status(win->surface) == CAIRO_STATUS_SUCCESS);
win->cr = cairo_create(win->surface);
assert(cairo_status(win->cr) == CAIRO_STATUS_SUCCESS);
log_debug("Cairo initialized");
return win;
}
cairo_t *window_get_context(Window *win)
{
return win->cr;
}
double window_get_scale(Window *win)
{
return MAX(1, win->dpi/96.);
}
void window_paint_surface(Window *win, cairo_surface_t *surface, int width, int height)
{
double scale = window_get_scale(win);
cairo_xcb_surface_set_size(win->surface, round(width * scale), round(height * scale));
xcb_clear_area(win->connection, false, win->window, 0, 0, 0, 0);
cairo_set_source_surface(win->cr, surface, 0, 0);
cairo_paint(win->cr);
cairo_show_page(win->cr);
xcb_flush(win->connection);
}
void window_destroy(Window *win)
{
cairo_destroy(win->cr);
cairo_surface_destroy(win->surface);
xcb_xrm_database_free(win->database);
xcb_disconnect(win->connection);
g_free(win);
}
// vim: ts=4 sw=4 et
|