-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathimplementation_xlib.cpp
More file actions
428 lines (352 loc) · 9.74 KB
/
implementation_xlib.cpp
File metadata and controls
428 lines (352 loc) · 9.74 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
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
//
// author: Kovacs Marton
// email: tetra666@gmail.com
// license: whatever. note my name
//
// implementation_xlib.cpp
//
// the drawing implementation using xlib
//
#include <vector>
#include <memory>
#include <map>
#include <cstdlib>
#include <ctime>
#include <X11/Xlib.h> // Every Xlib program must include this
#include <X11/keysym.h> //keysyms
#include <unistd.h> //usleep
#include <sys/stat.h> //mkfifo
struct State
{
Display* dpy;
Window window;
GC gc;
int delay;
unsigned long fg, bg;
int width, height;
int depth;
Colormap cmap;
XFontStruct* font;
Drawable draw;
Wiz* wiz;
//double buffering support
Pixmap double_buffer;
};
State state;
////////////////////////////////////////////////////////////////////////////////////////////////////
void Init(int argc, char *argv[]);
unsigned long Draw();
void Reshape(unsigned int w, unsigned int h);
void Free();
unsigned long TranslateColor(Color color);
int main(int argc, char* argv[])
{
std::srand(std::time(0));
Init(argc, argv);
bool exit = false;
bool drawable = false;
while (!exit)
{
//event handling part
XEvent event;
while (!exit && XPending(state.dpy))
{
XNextEvent(state.dpy, &event);
switch (event.type)
{
case MapNotify:
{
drawable = true;
break;
}
case ConfigureNotify:
{
Reshape(event.xconfigure.width, event.xconfigure.height);
break;
}
case ClientMessage:
{
exit = true;
break;
}
case KeyPress:
{
if (XK_Escape == XLookupKeysym(&event.xkey, 0))
{
exit = true;
}
break;
}
default:
{
break;
}
}
}
//drawing part
if (!exit && drawable)
{
unsigned long delay = Draw();
XFlush(state.dpy);
usleep(delay);
}
}
Free();
XCloseDisplay(state.dpy);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
struct MwmHints
{
unsigned long flags;
unsigned long functions;
unsigned long decorations;
long input_mode;
unsigned long status;
};
enum
{
MWM_HINTS_FUNCTIONS = (1L << 0),
MWM_HINTS_DECORATIONS = (1L << 1),
MWM_FUNC_ALL = (1L << 0),
MWM_FUNC_RESIZE = (1L << 1),
MWM_FUNC_MOVE = (1L << 2),
MWM_FUNC_MINIMIZE = (1L << 3),
MWM_FUNC_MAXIMIZE = (1L << 4),
MWM_FUNC_CLOSE = (1L << 5)
};
void Init(int argc, char* argv[])
{
Options options;
if (!ParseCommandline(argc, argv, options))
{
exit(1);
}
// Open the display
Display *dpy = XOpenDisplay(0);
state.dpy = dpy;
// Create the window
Size resolution;
if (0 != options.size.x)
{
resolution = options.size;
}
else
{
resolution = Size(XDisplayWidth(state.dpy, DefaultScreen(state.dpy)), XDisplayHeight(state.dpy, DefaultScreen(state.dpy)));
}
state.window = XCreateSimpleWindow(dpy, DefaultRootWindow(dpy), 0, 0, resolution.x, resolution.y, 0, BlackPixel(dpy, DefaultScreen(dpy)), BlackPixel(dpy, DefaultScreen(dpy)));
Atom mwmHintsProperty = XInternAtom(state.dpy, "_MOTIF_WM_HINTS", 0);
struct MwmHints hints;
hints.flags = MWM_HINTS_DECORATIONS;
hints.decorations = 0;
XChangeProperty(state.dpy, state.window, mwmHintsProperty, mwmHintsProperty, 32, PropModeReplace, (unsigned char *)&hints, 5);
XWindowAttributes xgwa;
XGetWindowAttributes(state.dpy, state.window, &xgwa);
state.cmap = xgwa.colormap;
state.depth = xgwa.depth;
state.width = resolution.x;
state.height = resolution.y;
XGCValues gcv;
gcv.foreground = state.fg = TranslateColor(Colors::white);
gcv.background = state.bg = TranslateColor(Colors::black);
gcv.fill_style = FillSolid;
Font fid = XLoadFont(state.dpy, options.fontName.c_str());
state.font = XQueryFont(state.dpy, fid);
if (!state.font)
{
std::cerr << "Could not load font: " << options.fontName << '\n';
std::exit(1);
}
gcv.font = fid;
state.gc = XCreateGC (state.dpy, state.window, GCForeground | GCBackground | GCFillStyle | GCFont, &gcv);
state.delay = 1000000 / FramePerSecond; //1000000 == 1 second
state.double_buffer = XCreatePixmap(state.dpy, state.window, state.width, state.height, state.depth);
state.draw = state.double_buffer;
//state.draw = state.window;
XSelectInput(state.dpy, state.window, StructureNotifyMask | KeyPressMask);
Atom wm_delete_window = XInternAtom(state.dpy, "WM_DELETE_WINDOW", False);
XSetWMProtocols(state.dpy, state.window, &wm_delete_window, 1);
XMapWindow(state.dpy, state.window);
state.wiz = new Wiz();
state.wiz->Init(options);
}
unsigned long Draw()
{
// XSetForeground(state.dpy, state.gc, BlackPixelOfScreen(DefaultScreenOfDisplay(state.dpy)));
XSetForeground(state.dpy, state.gc, TranslateColor(Colors::black));
XFillRectangle(state.dpy, state.double_buffer, state.gc, 0, 0, state.width, state.height);
state.wiz->DrawFrame();
/*
XWindowAttributes xgwa;
XGetWindowAttributes (state.dpy, state.window, &xgwa);
*/
XCopyArea(state.dpy, state.double_buffer, state.window, state.gc, 0, 0, state.width, state.height, 0, 0);
return state.delay;
}
void Reshape(unsigned int w, unsigned int h)
{
XWindowAttributes xgwa;
XGetWindowAttributes (state.dpy, state.window, &xgwa);
state.width = xgwa.width;
state.height = xgwa.height;
state.depth = xgwa.depth;
state.cmap = xgwa.colormap;
XFreePixmap(state.dpy, state.double_buffer);
state.double_buffer = XCreatePixmap(state.dpy, state.window, state.width, state.height, state.depth);
state.draw = state.double_buffer;
}
void Free()
{
}
//
// beginning of implementation of common stuff
//
struct CompareColors
{
bool operator()(const XColor& lhs, const XColor& rhs)
{
return lhs.red < rhs.red ||
(lhs.red == rhs.red && lhs.green < rhs.green) ||
(lhs.red == rhs.red && lhs.green == rhs.green && lhs.blue < rhs.blue);
}
};
unsigned long TranslateColor(Color color)
{
CompareColors comp;
typedef std::map<XColor, unsigned long, CompareColors> ColorMap;
static ColorMap ColorCache(comp);
XColor xcol = {0, 257*color.red, 257*color.green, 257*color.blue, DoRed | DoGreen | DoBlue, 0};
ColorMap::iterator colorIt = ColorCache.find(xcol);
if (ColorCache.end() == colorIt)
{
XAllocColor(state.dpy, state.cmap, &xcol);
ColorCache[xcol] = xcol.pixel;
}
else
{
xcol.pixel = colorIt->second;
}
return xcol.pixel;
}
void SetColor(Color color)
{
XSetForeground(state.dpy, state.gc, TranslateColor(color));
}
std::vector<XPoint> ConvertPoints(const Coordinate* const begin, const Coordinate* const end)
{
std::vector<XPoint> result;
const Coordinate* runptr = begin;
while(end != runptr)
{
XPoint xp = {runptr->x, runptr->y};
result.push_back(xp);
++runptr;
}
return result;
}
//
// now really...
//
void DrawWrapper::DrawCircle(Coordinate center, int size, Color color, bool fill)
{
SetColor(color);
if(fill)
{
XFillArc(state.dpy, state.draw, state.gc, center.x - size, center.y - size, 2 * size, 2 * size, 0, 360*64);
}
else
{
XDrawArc(state.dpy, state.draw, state.gc, center.x - size, center.y - size, 2 * size, 2 * size, 0, 360*64);
}
}
void DrawWrapper::DrawLine(Coordinate begin, Coordinate end, Color color)
{
SetColor(color);
XDrawLine(state.dpy, state.draw, state.gc, begin.x, begin.y, end.x, end.y);
}
void DrawWrapper::DrawShape(Coordinate* begin, Coordinate* end, Color color, bool fill)
{
SetColor(color);
std::vector<XPoint> points = ConvertPoints(begin, end);
points.push_back(points.front());
if(fill)
{
XFillPolygon(state.dpy, state.draw, state.gc, &(points[0]), points.size(), Convex, CoordModeOrigin);
}
else
{
XDrawLines(state.dpy, state.draw, state.gc, &(points[0]), points.size(), CoordModeOrigin);
}
}
int DrawWrapper::Random(int sup)
{
return std::rand() % sup;
}
Size DrawWrapper::GetSize()
{
return Size(state.width, state.height);
}
int DrawWrapper::DrawTextCentered(const std::string& text, Coordinate center, Color color, int correction)
{
int res = correction;
if (-1 == res)
{
res = XTextWidth(state.font, text.c_str(), text.size()) / 2;
}
SetColor(color);
XDrawString(state.dpy, state.draw, state.gc, center.x - res, center.y - 1, text.c_str(), text.size());
return res;
}
void DrawWrapper::DrawText(const std::string& text, Coordinate botleft, Color color)
{
SetColor(color);
XDrawString(state.dpy, state.draw, state.gc, botleft.x, botleft.y, text.c_str(), text.size());
}
struct IpcImplementation
{
IpcImplementation(const std::string& name): outName(name + "_info"), inName(name + "_command"), logName(name + "_log")
{
std::remove(outName.c_str());
std::remove(inName.c_str());
if (-1 == mkfifo(outName.c_str(), 0666))
{
throw "Could not open " + outName;
}
if (-1 == mkfifo(inName.c_str(), 0666))
{
throw "Could not open " + inName;
}
// std::cerr << "running " << name + ' ' + inName + ' ' + outName + " &\n";
std::system((name + ' ' + inName + ' ' + outName + " >" + logName + " 2>/dev/null &").c_str());
m_out.reset(new std::ofstream(outName.c_str()));
m_in.reset(new std::ifstream(inName.c_str()));
}
~IpcImplementation()
{
if (m_out.unique())
{
std::remove(outName.c_str());
std::remove(inName.c_str());
}
}
const std::string outName;
const std::string inName;
const std::string logName;
std::shared_ptr<std::ostream> m_out;
std::shared_ptr<std::istream> m_in;
};
Ipc::Ipc(const std::string& name): m_impl(new IpcImplementation(name))
{ }
Ipc::~Ipc()
{ }
void Ipc::Send(const std::string& msg)
{
*m_impl->m_out << msg << '\n';
(*m_impl->m_out).flush();
}
std::string Ipc::Receive()
{
std::string res;
std::getline(*m_impl->m_in, res);
return res;
}