forked from baines/MiniGBS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
x11.c
208 lines (171 loc) · 5.26 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
#include "minigbs.h"
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xlibint.h>
#include <dlfcn.h>
#define X11_WIN_W 854
#define X11_WIN_H 480
unsigned long typedef ulong;
unsigned int typedef uint;
static struct x11_funcs {
Display* (*open_dpy) (const char*);
Window (*simple_win) (Display*, Window, int, int, uint, uint, uint, ulong, ulong);
int (*map_window) (Display*, Window);
int (*unmap_window)(Display*, Window);
int (*store_name) (Display*, Window, const char*);
Bool (*query_ext) (Display*, const char*, int*, int*, int*);
XExtCodes* (*init_ext) (Display*, const char*);
GC (*create_gc) (Display*, Drawable, ulong, XGCValues*);
Pixmap (*create_pix) (Display*, Drawable, uint, uint, uint);
int (*fill_rect) (Display*, Drawable, GC, int, int, uint, uint);
int (*draw_lines) (Display*, Drawable, GC, XPoint*, int, int);
int (*flush) (Display*);
int (*sel_input) (Display*, Window, long);
int (*pending) (Display*);
int (*next_event) (Display*, XEvent*);
KeySym (*lookup_key) (XKeyEvent*, int);
Atom (*intern_atom) (Display*, const char*, Bool);
Status (*set_protos) (Display*, Window, Atom*, int);
void* (*get_request) (Display*, uint8_t, size_t);
} x11;
static const char* x11_func_names[] = {
"XOpenDisplay" , "XCreateSimpleWindow", "XMapWindow" , "XUnmapWindow",
"XStoreName" , "XQueryExtension" , "XInitExtension", "XCreateGC",
"XCreatePixmap", "XFillRectangle" , "XDrawLines" , "XFlush",
"XSelectInput" , "XPending" , "XNextEvent" , "XLookupKeysym",
"XInternAtom" , "XSetWMProtocols" , "_XGetRequest" ,
};
struct present_pixmap_req {
uint8_t type;
uint8_t subtype;
uint16_t length;
uint32_t window;
uint32_t pixmap;
uint32_t crap[15];
};
static void* xlib;
static Display* x11_dpy;
static Window x11_win;
static Pixmap x11_pix;
static GC x11_gc;
static GC x11_gc2;
static int x11_present_op;
static bool x11_visible;
static Atom x11_atom_wm_delete;
int x11_init(void){
xlib = dlopen("libX11.so", RTLD_NOW | RTLD_LOCAL);
if(!xlib || dlerror()){
xlib = NULL;
return -1;
}
int i = 0;
for(void** fn = (void**)&x11; fn != (void**)(&x11+1); ++fn, ++i){
*fn = dlsym(xlib, x11_func_names[i]);
const char* msg = dlerror();
if(!*fn || msg){
goto fail;
}
}
if(!(x11_dpy = x11.open_dpy(NULL)))
goto fail;
x11_win = x11.simple_win(x11_dpy, RootWindow(x11_dpy, 0), 0, 0, X11_WIN_W, X11_WIN_H, 0, 0, BlackPixel(x11_dpy, 0));
int op, ev, err;
if(!x11.query_ext(x11_dpy, "Present", &op, &ev, &err))
goto fail;
XExtCodes* xec = x11.init_ext(x11_dpy, "Present");
if(!xec)
goto fail;
x11_present_op = xec->major_opcode;
x11.flush(x11_dpy);
if(!(x11_pix = x11.create_pix(x11_dpy, x11_win, X11_WIN_W, X11_WIN_H, 24)))
goto fail;
XGCValues gcv = {
.foreground = WhitePixel(x11_dpy, 0),
.background = BlackPixel(x11_dpy, 0),
.line_width = 2,
};
x11_gc = x11.create_gc(x11_dpy, x11_pix, GCForeground | GCBackground | GCLineWidth, &gcv);
gcv.foreground = BlackPixel(x11_dpy, 0);
gcv.background = WhitePixel(x11_dpy, 0);
x11_gc2 = x11.create_gc(x11_dpy, x11_pix, GCForeground | GCBackground | GCLineWidth, &gcv);
if(!x11_gc || !x11_gc2)
goto fail;
x11.store_name(x11_dpy, x11_win, "MiniGBS Oscilloscope");
x11.sel_input(x11_dpy, x11_win, KeyPressMask);
x11_atom_wm_delete = x11.intern_atom(x11_dpy, "WM_DELETE_WINDOW", 0);
x11.set_protos(x11_dpy, x11_win, &x11_atom_wm_delete, 1);
return ConnectionNumber(x11_dpy);
fail:
dlclose(xlib);
x11_dpy = xlib = NULL;
x11_win = 0;
return -1;
}
int x11_action(bool* have_more_events){
if(!x11_win || !x11.pending(x11_dpy)){
*have_more_events = false;
return -1;
}
XEvent ev;
x11.next_event(x11_dpy, &ev);
if(ev.type == KeyPress){
KeySym sym = x11.lookup_key(&ev.xkey, 0);
switch(sym){
case XK_Escape:
case XK_q:
return 'o';
case XK_BackSpace:
return KEY_BACKSPACE;
case XK_Up:
return KEY_UP;
case XK_Down:
return KEY_DOWN;
case XK_Left:
return KEY_LEFT;
case XK_Right:
return KEY_RIGHT;
case XK_Return:
return '\n';
case XK_c:
if(ev.xkey.state & ControlMask){
return 'q';
}
default:
return sym;
}
}
if(ev.type == ClientMessage && ev.xclient.data.l[0] == x11_atom_wm_delete){
x11.unmap_window(x11_dpy, x11_win);
x11_visible = false;
}
return -1;
}
void x11_draw_begin(void){
if(!x11_win || !x11_visible) return;
x11.fill_rect(x11_dpy, x11_pix, x11_gc2, 0, 0, X11_WIN_W, X11_WIN_H);
}
void x11_draw_lines(int16_t* points, size_t n){
if(!x11_win || !x11_visible) return;
x11.draw_lines(x11_dpy, x11_pix, x11_gc, (XPoint*)points, n/2, CoordModeOrigin);
}
void x11_draw_end(void){
if(!x11_win || !x11_visible) return;
struct present_pixmap_req* req = x11.get_request(x11_dpy, x11_present_op, sizeof(*req));
memset(req->crap, 0, sizeof(req->crap));
req->subtype = 1;
req->window = x11_win;
req->pixmap = x11_pix;
if(x11_dpy->synchandler)
x11_dpy->synchandler(x11_dpy);
x11.flush(x11_dpy);
}
void x11_toggle(void){
if(!x11_win) return;
if(x11_visible){
x11.unmap_window(x11_dpy, x11_win);
} else {
x11.map_window(x11_dpy, x11_win);
}
x11_visible = !x11_visible;
x11.flush(x11_dpy);
}