-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathddcX11Env.h
213 lines (201 loc) · 4.64 KB
/
ddcX11Env.h
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
#ifndef __ddcX11Env__
#define __ddcX11Env__
#include <X11/Xlib.h>
#include <X11/keysym.h>
struct ddXE_settings
{
void(*start)(void);
void(*draw)(void);
void(*keypress)(unsigned char);
void(*keyrelease)(unsigned char);
void(*mousedown)(int, int, int);
void(*mouseup)(int, int, int);
void(*mousemove)(int, int);
void(*mousedownmove)(int, int, int);
char bgcolor[3];
char fgcolor[3];
char autoflush;
char nodb;
char* running;
int width;
int height;
};
static Display* d;
static Window w;
static GC gc;
static XEvent e;
static Pixmap db;
static int s;
struct ddXE_settings set;
unsigned long XRGB(int r, int g, int b)
{
return b + (g<<8) + (r<<16);
}
void ddXE_get_mouse_pos(int* x, int* y)
{
void* v;
XQueryPointer(d, w, v, v, v, v, x, y, v);
}
int ddXE_get_depth(void)
{
XWindowAttributes xwa;
XGetWindowAttributes(d, w, &xwa);
return xwa.depth;
}
int ddXE_get_width(void)
{
XWindowAttributes xwa;
XGetWindowAttributes(d, w, &xwa);
return xwa.width;
}
int ddXE_get_height(void)
{
XWindowAttributes xwa;
XGetWindowAttributes(d, w, &xwa);
return xwa.height;
}
void ddXE_set_color(int r, int g, int b)
{
XSetForeground(d, gc, XRGB(r, g, b));
}
void ddXE_clear(void)
{
ddXE_set_color(set.bgcolor[0],set.bgcolor[1],set.bgcolor[2]);
XFillRectangle(d, db, gc, 0, 0, ddXE_get_width(), ddXE_get_height());
}
void ddXE_flush(void)
{
if (set.nodb) XFlush(d);
else XCopyArea(d, db, w, gc, 0, 0, ddXE_get_width(), ddXE_get_height(), 0, 0);
}
void ddXE_set_pixel(int x, int y)
{
XDrawPoint(d, db, gc, x, y);
}
void ddXE_draw_rect(int x1, int y1, int x2, int y2)
{
XFillRectangle(d, db, gc, x1, y1, x2, y2);
}
void ddXE_draw_dot(int x, int y, int size)
{
XFillArc(d, db, gc, x-(size/2), y-(size/2), size, size, 0, 360*64);
}
void ddXE_draw_line(int x1, int y1, int x2, int y2)
{
XDrawLine(d, db, gc, x1, y1, x2, y2);
}
void ddXE_draw_text(int x, int y, char* str, int length)
{
XDrawString(d, db, gc, x, y, str, length);
}
void ddXE_draw_bitmap(int x, int y, char* filename)
{
int width, height;
int hx, hy;
Pixmap pm;
XReadBitmapFile(d, w, filename, &width, &height, &pm, &hx, &hy);
XCopyArea(d, pm, w, gc, 0, 0, width, height, x, y);
}
int ddXE_start(const struct ddXE_settings _set)
{
set = _set;
d = XOpenDisplay(NULL);
if (d == NULL)
{
return 1;
}
s = DefaultScreen(d);
XColor fgc = { set.fgcolor[0], set.fgcolor[1], set.fgcolor[2] };
XColor bgc = { set.bgcolor[0], set.bgcolor[1], set.bgcolor[2] };
w = XCreateSimpleWindow(d, RootWindow(d, s), 100, 100, set.width, set.height, 1,
WhitePixel(d, s), BlackPixel(d, s));
gc = DefaultGC(d, s);
XSelectInput(d, w, ExposureMask | KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask | PointerMotionMask);
XMapWindow(d, w);
bool lstarted = false;
int cmousebut = -1;
int cmousedown = 0;
while (*set.running)
{
if (lstarted && set.autoflush) ddXE_flush();
XNextEvent(d, &e);
if (!lstarted)
{
if (set.nodb)
db = w;
else
db = XCreatePixmap(d, w, ddXE_get_width(), ddXE_get_height(), ddXE_get_depth());
ddXE_clear();
if (set.start) set.start();
lstarted = true;
}
switch (e.type)
{
case Expose:
if (set.draw) set.draw();
break;
case KeyPress:
if (set.keypress) set.keypress(XLookupKeysym(&(e.xkey), 0));
break;
case KeyRelease:
if (set.keyrelease) set.keyrelease(XLookupKeysym(&(e.xkey), 0));
break;
case MotionNotify:
{
if (cmousedown)
{
if (set.mousedownmove) set.mousedownmove(cmousebut, e.xbutton.x, e.xbutton.y);
}
else
{
if (set.mousemove) set.mousemove(e.xbutton.x, e.xbutton.y);
}
} break;
case ButtonPress:
{
cmousedown = 1;
switch (e.xbutton.button)
{
case Button1:
cmousebut = 1;
if (set.mousedown) set.mousedown(1, e.xbutton.x, e.xbutton.y);
break;
case Button2:
cmousebut = 2;
if (set.mousedown) set.mousedown(2, e.xbutton.x, e.xbutton.y);
break;
case Button3:
cmousebut = 3;
if (set.mousedown) set.mousedown(3, e.xbutton.x, e.xbutton.y);
break;
default:
cmousebut = -1;
if (set.mousedown) set.mousedown(-1, e.xbutton.x, e.xbutton.y);
break;
}
} break;
case ButtonRelease:
{
cmousedown = 0;
switch (e.xbutton.button)
{
case Button1:
if (set.mouseup) set.mouseup(1, e.xbutton.x, e.xbutton.y);
break;
case Button2:
if (set.mouseup) set.mouseup(2, e.xbutton.x, e.xbutton.y);
break;
case Button3:
if (set.mouseup) set.mouseup(3, e.xbutton.x, e.xbutton.y);
break;
default:
if (set.mouseup) set.mouseup(-1, e.xbutton.x, e.xbutton.y);
break;
}
} break;
}
}
XCloseDisplay(d);
return 0;
}
#endif