-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathfont_box.c
More file actions
149 lines (110 loc) · 4.11 KB
/
font_box.c
File metadata and controls
149 lines (110 loc) · 4.11 KB
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
// gcc `pkg-config --libs xcb` main.c -o main
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <xcb/xcb.h>
#define HELLO_WORLD "Hello world!"
xcb_connection_t *connection;
xcb_screen_t *screen;
xcb_char2b_t *build_chars(const char *str, size_t length) {
// taken from http://lists.freedesktop.org/archives/xcb/2009-April/004611.html
// but https://bitbucket.org/ryanflannery/xtabs/src/4a36b745532d3a75f98ea115ceaabfac839fb8bc/xutil.c
// juts casts..
int i;
xcb_char2b_t *ret = malloc(length * sizeof(xcb_char2b_t));
if (!ret)
return NULL;
for (i = 0; i < length; i++) {
ret[i].byte1 = 0;
ret[i].byte2 = str[i];
}
return ret;
}
void measure_string(char *string, xcb_font_t font,
int *width, int *height, int *origin) {
// man xcb_query_text_extents
// http://www.x.org/releases/current/doc/xproto/x11protocol.html#requests:QueryFont
// http://www.x.org/releases/current/doc/xproto/x11protocol.html#requests:ImageText8
xcb_char2b_t *xcb_str;
xcb_query_text_extents_cookie_t cookie;
xcb_query_text_extents_reply_t *reply;
xcb_str = build_chars(string, strlen(string));
cookie = xcb_query_text_extents(connection, font, strlen(string), xcb_str);
reply = xcb_query_text_extents_reply(connection, cookie, NULL);
if(!reply) puts("STRING ERROR");
*width = reply->overall_width;
*height = reply->font_ascent + reply->font_descent;
*origin = reply->font_ascent;
free(xcb_str);
free(reply);
}
xcb_window_t create_window(int width, int height) {
// http://www.x.org/releases/current/doc/xproto/x11protocol.html#requests:CreateWindow
// http://www.x.org/archive/current/doc/man/man3/xcb_create_window.3.xhtml
//
// N.B. xcb's order corresponds to the order of the wire.
// You can look at the protocol encoding: http://www.x.org/releases/current/doc/xproto/x11protocol.html#Encoding::Requests
uint32_t mask;
uint32_t values[2];
xcb_window_t window;
xcb_void_cookie_t cookie;
mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
values[0] = screen->white_pixel;
values[1] = XCB_EVENT_MASK_EXPOSURE | XCB_EVENT_MASK_KEY_PRESS;
window = xcb_generate_id(connection);
cookie = xcb_create_window(connection,
XCB_COPY_FROM_PARENT, window, screen->root,
0, 0, width, height,
0,
XCB_WINDOW_CLASS_INPUT_OUTPUT,
screen->root_visual,
mask, values);
xcb_map_window(connection, window);
return window;
}
xcb_gcontext_t create_graphics_context(xcb_window_t window, xcb_font_t font) {
xcb_gcontext_t graphics_context;
uint32_t mask;
uint32_t values[4];
mask = XCB_GC_FOREGROUND | XCB_GC_BACKGROUND | XCB_GC_FONT | XCB_GC_GRAPHICS_EXPOSURES;
values[0] = 0xF778A1;
values[1] = screen->white_pixel;
values[2] = font;
values[3] = 1;
graphics_context = xcb_generate_id(connection);
xcb_create_gc(connection,
graphics_context, window,
mask, values);
return graphics_context;
}
void event_loop(xcb_window_t window, xcb_gcontext_t graphics_context, int origin) {
xcb_generic_event_t *event;
while(event = xcb_wait_for_event(connection)) {
switch(event->response_type) {
case XCB_EXPOSE:
xcb_image_text_8(connection, strlen(HELLO_WORLD), window, graphics_context, 0, origin, HELLO_WORLD);
xcb_flush(connection);
break;
case XCB_KEY_PRESS:
return;
}
}
}
int main(void) {
int width, height, origin;
xcb_font_t font;
xcb_window_t window;
xcb_gcontext_t graphics_context;
connection = xcb_connect(NULL, NULL); // Callers need to use xcb_connection_has_error() to check for failure.
screen = xcb_setup_roots_iterator(xcb_get_setup(connection)).data;
font = xcb_generate_id(connection);
char * font_name = "-misc-dejavu sans-medium-o-normal--0-0-0-0-p-0-ascii-0";
xcb_open_font(connection, font, strlen(font_name), font_name);
measure_string(HELLO_WORLD, font, &width, &height, &origin);
window = create_window(width, height);
graphics_context = create_graphics_context(window, font);
xcb_flush(connection);
event_loop(window, graphics_context, origin);
puts("bye!");
return EXIT_SUCCESS;
}