-
Notifications
You must be signed in to change notification settings - Fork 2
/
x11.c
254 lines (213 loc) · 7.54 KB
/
x11.c
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
int x11_init(){
int status;
Window root;
XSetWindowAttributes window_attributes;
Atom wm_state_fullscreen;
XTextProperty window_name;
pid_t pid = getpid();
XSizeHints* size_hints = NULL;
XWMHints* wm_hints = NULL;
XClassHint* class_hints = NULL;
//x data initialization
x11.display = XOpenDisplay(NULL);
if(!(x11.display)){
fprintf(stderr, "Failed to open display\n");
return -1;
}
if(!XRenderQueryExtension(x11.display, &status, &status)){
fprintf(stderr, "Missing XRender support on the display\n");
return -1;
}
x11.screen = DefaultScreen(x11.display);
root = RootWindow(x11.display, x11.screen);
if(!XMatchVisualInfo(x11.display, x11.screen, 32, TrueColor, &(x11.visual_info))) {
fprintf(stderr, "Display does not support RGBA TrueColor visual\n");
return -1;
}
x11.colormap = XCreateColormap(x11.display, root, x11.visual_info.visual, AllocNone);
//set up window params
window_attributes.background_pixel = XBlackPixel(x11.display, x11.screen);
window_attributes.border_pixel = XBlackPixel(x11.display, x11.screen);
window_attributes.colormap = x11.colormap;
window_attributes.cursor = None;
window_attributes.event_mask = ExposureMask | KeyPressMask | ButtonPressMask | StructureNotifyMask;
x11.width = DisplayWidth(x11.display, x11.screen);
x11.height = DisplayHeight(x11.display, x11.screen);
if(config.windowed){
x11.width = config.width;
x11.height = config.height;
}
//create window
x11.main = XCreateWindow(x11.display,
root,
0,
0,
x11.width,
x11.height,
0,
32,
InputOutput,
x11.visual_info.visual,
CWBackPixel | CWCursor | CWEventMask | CWBorderPixel | CWColormap,
&window_attributes);
//set window properties
if(XStringListToTextProperty(&(config.window_name), 1, &window_name) == 0){
fprintf(stderr, "Failed to create string list, aborting\n");
return -1;
}
//allocate some structures
size_hints = XAllocSizeHints();
wm_hints = XAllocWMHints();
class_hints = XAllocClassHint();
if(!size_hints || !wm_hints || !class_hints){
fprintf(stderr, "Failed to allocate X data structures\n");
return -1;
}
wm_hints->flags = 0;
class_hints->res_name = XELFLUT_VERSION;
class_hints->res_class = XELFLUT_CLASS;
XSetWMProperties(x11.display, x11.main, &window_name, NULL, NULL, 0, NULL, wm_hints, class_hints);
XFree(window_name.value);
XFree(size_hints);
XFree(wm_hints);
XFree(class_hints);
//set fullscreen mode
if(!config.windowed){
wm_state_fullscreen = XInternAtom(x11.display, "_NET_WM_STATE_FULLSCREEN", False);
XChangeProperty(x11.display, x11.main, XInternAtom(x11.display, "_NET_WM_STATE", False), XA_ATOM, 32, PropModeReplace, (unsigned char*) &wm_state_fullscreen, 1);
}
XChangeProperty(x11.display, x11.main, XInternAtom(x11.display, "_NET_WM_PID", False), XA_CARDINAL, 32, PropModeReplace, (unsigned char*)&pid, 1);
//register for WM_DELETE_WINDOW messages
x11.wm_delete = XInternAtom(x11.display, "WM_DELETE_WINDOW", False);
XSetWMProtocols(x11.display, x11.main, &(x11.wm_delete), 1);
//map window
XMapRaised(x11.display, x11.main);
//get x socket fds
if(!xfd_add(&(x11.xfds), XConnectionNumber(x11.display))){
fprintf(stderr, "Failed to allocate xfd memory\n");
return -1;
}
if(XAddConnectionWatch(x11.display, xconn_watch, (void*)(&(x11.xfds))) == 0){
fprintf(stderr, "Failed to register connection watch procedure\n");
return -1;
}
fprintf(stderr, "Creating canvas of dimensions %dx%d\n", config.width, config.height);
x11.canvas = XCreatePixmap(x11.display, x11.main, config.width, config.height, 32);
x11.canvas_handle = XRenderCreatePicture(x11.display, x11.canvas, XRenderFindStandardFormat(x11.display, PictStandardARGB32), 0, 0);
x11.window_handle = XRenderCreatePicture(x11.display, x11.main, XRenderFindStandardFormat(x11.display, PictStandardARGB32), 0, 0);
//clear pixmap
XRenderFillRectangle(x11.display, PictOpOver, x11.canvas_handle, &x11.background, 0, 0, config.width, config.height);
if(!x11.canvas){
fprintf(stderr, "Failed to create canvas pixmap\n");
return -1;
}
return 0;
}
void x11_redraw(){
XRenderComposite(x11.display, PictOpSrc, x11.canvas_handle, None, x11.window_handle, 0, 0, 0, 0, 0, 0, x11.width, x11.height);
XFlush(x11.display);
}
void x11_handle(){
XEvent event;
char pressed_key;
bool reconfigured = false, exposed = false;
double uniform_scaling;
XTransform transform = {{
{XDoubleToFixed(1), XDoubleToFixed(0), XDoubleToFixed(0)},
{XDoubleToFixed(0), XDoubleToFixed(1), XDoubleToFixed(0)},
{XDoubleToFixed(0), XDoubleToFixed(0), XDoubleToFixed(1)}
}};
//handle events
while(XPending(x11.display)){
XNextEvent(x11.display, &event);
//handle events
switch(event.type){
case ConfigureNotify:
x11.width = event.xconfigure.width;
x11.height = event.xconfigure.height;
reconfigured = true;
break;
case Expose:
exposed = true;
break;
case KeyPress:
//translate key event into a character, respecting keyboard layout
if(XLookupString(&event.xkey, &pressed_key, 1, NULL, NULL) != 1){
//disregard combined characters / bound strings
break;
}
switch(pressed_key){
case 'q':
abort_signaled = 1;
break;
case 'r':
fprintf(stderr, "Redrawing on request\n");
x11_redraw();
break;
case 'c':
fprintf(stderr, "Clearing image data\n");
XRenderFillRectangle(x11.display, PictOpOver, x11.canvas_handle, &x11.background, 0, 0, config.width, config.height);
break;
default:
fprintf(stderr, "KeyPress %d (%c)\n", event.xkey.keycode, pressed_key);
break;
}
break;
case ClientMessage:
if(event.xclient.data.l[0] == x11.wm_delete){
fprintf(stderr, "Closing down window\n");
abort_signaled = 1;
}
else{
fprintf(stderr, "Client message\n");
}
break;
default:
fprintf(stderr, "Unhandled X event\n");
break;
}
}
if(reconfigured){
uniform_scaling = min(x11.width / (double) config.width, x11.height / (double) config.height);
if(config.scale_uniform){
//update uniform transform scale
transform.matrix[2][2] = XDoubleToFixed(uniform_scaling);
//translate along unclipped axis
if(config.centered){
transform.matrix[0][2] = XDoubleToFixed((x11.width / config.width < x11.height / config.height) ? 0 : (x11.width - config.width * uniform_scaling) / -2);
transform.matrix[1][2] = XDoubleToFixed((x11.width / config.width > x11.height / config.height) ? 0: (x11.height - config.height * uniform_scaling) / -2);
}
}
else{
//anisotropic scaling
transform.matrix[0][0] = XDoubleToFixed(config.width / (double) x11.width);
transform.matrix[1][1] = XDoubleToFixed(config.height / (double) x11.height);
}
fprintf(stderr, "Window configured to %dx%d, uniform scaling factor %f\n", x11.width, x11.height, uniform_scaling);
//update transformation matrix
XRenderSetPictureTransform(x11.display, x11.canvas_handle, &transform);
//clear the window
XRenderFillRectangle(x11.display, PictOpOver, x11.window_handle, &x11.background, 0, 0, x11.width, x11.height);
}
if(exposed){
//composite pixmap onto window
XRenderComposite(x11.display, PictOpSrc, x11.canvas_handle, None, x11.window_handle, 0, 0, 0, 0, 0, 0, x11.width, x11.height);
}
XFlush(x11.display);
}
void x11_cleanup(){
if(!x11.display){
return;
}
XRenderFreePicture(x11.display, x11.canvas_handle);
XRenderFreePicture(x11.display, x11.window_handle);
if(x11.main){
XDestroyWindow(x11.display, x11.main);
}
if(x11.colormap){
XFreeColormap(x11.display, x11.colormap);
}
XFreePixmap(x11.display, x11.canvas);
XCloseDisplay(x11.display);
xfd_free(&(x11.xfds));
}