My favorites | Sign in
Project Home Downloads Source
Checkout   Browse   Changes    
 
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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
#include "CtrlCore.h"

#ifdef GUI_X11

#define Time XTime
#define Font XFont
#define Display XDisplay
#define Picture XPicture

#ifndef flagNOGTK
#include <gtk/gtk.h>
#include <gdk/gdkx.h>
#include <gdk/gdkprivate.h>
#endif

#undef Picture
#undef Time
#undef Font
#undef Display

NAMESPACE_UPP

#define LLOG(x) //LOG(x)
#define LTIMING(x) //TIMING(x)

XDisplay *Xdisplay;
int Xscreenno;
Visual *Xvisual;
Window Xroot;
Screen *Xscreen;
Colormap Xcolormap;
int Xheight;
int Xwidth;
int XheightMM;
int XwidthMM;
int Xdepth;
dword Xblack;
dword Xwhite;
int Xconnection;
byte *Xmapcolor;
byte *Xunmapcolor;
bool Xpalette;

dword (*Xgetpixel)(int r, int g, int b);

EXITBLOCK
{
if(Xdisplay) {
XCloseDisplay(Xdisplay);
LLOG("Xdisplay closed");
Xdisplay = NULL;
}
if(Xmapcolor)
delete[] Xmapcolor;
if(Xunmapcolor)
delete[] Xunmapcolor;
}

void XError()
{
Panic("X11 error !");
}

void XError(const char *s)
{
Panic(String("X11 error:") + s + " !");
}

static int sAcs;

bool sAllocColor(int xr, int xg, int xb)
{
XColor ce;
ce.red = xr;
ce.green = xg;
ce.blue = xb;
ce.flags = DoRed | DoGreen | DoBlue;
if(!XAllocColor(Xdisplay, Xcolormap, &ce)) return false;
sAcs++;
return sAcs < 257;
}

void sAllocColors()
{
int r, g, b;
for(r = 0; r < 2; r++)
for(g = 0; g < 2; g++)
for(b = 0; b < 2; b++)
if(!sAllocColor(65535 * r, 65535 * g, 65535 * b)) return;
for(r = 0; r < 3; r++)
for(g = 0; g < 3; g++)
for(b = 0; b < 3; b++)
if((r == 1 || g == 1 || b == 1) &&
!sAllocColor((65535 * r) / 2, (65535 * g) / 2, (65535 * b) / 2)) return;
for(r = 5; r >= 0; r--)
for(g = 5; g >= 0; g--)
for(b = 5; b >= 0; b--)
if((r != 0 && r != 5 || g != 0 && g != 5 || b != 0 && b != 5) &&
!sAllocColor((65535 * r) / 5, (65535 * g) / 5, (65535 * b) / 5)) return;
for(r = 1; r <= 11; r += 2)
if(!sAllocColor((65535 * r) / 11, (65535 * r) / 11, (65535 * r) / 11)) return;
for(int r = 255; r >= 0; r--)
if(!sAllocColor(r << 8, r << 8, r << 8)) return;
}

dword GetPseudoColorPixel(int r, int g, int b)
{
return Xmapcolor[r * 11 / 255 * (24 * 12) + g * 23 / 255 * 12 + b * 11 / 255];
}

static
struct Xshift {
dword mask;
int bits;
int shift;
dword Do(int c) { return (c >> bits << shift) & mask; }
}
Xred, Xgreen, Xblue;

Xshift CalcXShift(dword mask)
{
Xshift f;
f.mask = mask;
f.shift = 0;
f.bits = 0;
while((mask & 1) == 0) {
mask >>= 1;
f.shift++;
}
while((mask & 1) == 1) {
mask >>= 1;
f.bits++;
}
f.bits = 8 - f.bits;
if(f.bits < 0) {
f.shift += f.bits;
f.bits = 0;
}
LLOG("xshift(" << FormatIntHex(mask) << "): mask = "
<< FormatIntHex(f.mask) << ", bits = " << f.bits << ", shift = " << f.shift);
return f;
}

dword GetTrueColorPixel(int r, int g, int b)
{
return Xred.Do(r) | Xgreen.Do(g) | Xblue.Do(b);
}

inline int ssq(int x) { return x * x; }

void InitX11Draw(XDisplay *display)
{
Xdisplay = display;
if(!Xdisplay) {
puts(NFormat("No X11 display, errno = %d, %s", errno, strerror(errno)));
fflush(stdout);
XError();
}
Xscreenno = DefaultScreen(Xdisplay);
Xroot = RootWindow(Xdisplay, Xscreenno);
Xscreen = ScreenOfDisplay(Xdisplay, Xscreenno);
Xcolormap = DefaultColormap(Xdisplay, Xscreenno);
// Xcolormap = (Colormap)GDK().gdk_x11_colormap_get_xcolormap(GDK().gdk_rgb_get_colormap());
Xheight = DisplayHeight(Xdisplay, Xscreenno);
Xwidth = DisplayWidth(Xdisplay, Xscreenno);
XheightMM = DisplayHeightMM(Xdisplay, Xscreenno);
XwidthMM = DisplayWidthMM(Xdisplay, Xscreenno);
LLOG("Xwidth = " << Xwidth << ", XwidthMM = " << XwidthMM);
LLOG("Xheight = " << Xheight << ", XheightMM = " << XheightMM);
Xdepth = DefaultDepth(Xdisplay, Xscreenno);
Xblack = BlackPixel(Xdisplay, 0);
Xwhite = WhitePixel(Xdisplay, 0);
Xconnection = XConnectionNumber(Xdisplay);
Xvisual = DefaultVisual(Xdisplay, Xscreenno);
Visual *v = Xvisual;
if(v->c_class == TrueColor) {
Xred = CalcXShift(v->red_mask);
Xgreen = CalcXShift(v->green_mask);
Xblue = CalcXShift(v->blue_mask);
Xgetpixel = GetTrueColorPixel;
}
else {
Xpalette = true;
sAllocColors();
int colorcount = max(1 << Xdepth, 256);
Buffer<XColor> cs(colorcount);
int i;
for(i = 0; i < colorcount; i++)
cs[i].pixel = i;
XQueryColors(Xdisplay, Xcolormap, cs, colorcount);
Xunmapcolor = new byte[3 * colorcount];
for(i = 0; i < colorcount; i++)
{
Xunmapcolor[3 * i + 0] = cs[i].blue;
Xunmapcolor[3 * i + 1] = cs[i].green;
Xunmapcolor[3 * i + 2] = cs[i].red;
}
byte *cm = Xmapcolor = new byte[12 * 24 * 12];
for(int r = 0; r < 12; r++)
for(int g = 0; g < 24; g++)
for(int b = 0; b < 12; b++) {
int mind = INT_MAX;
int mini = 0;
for(int i = 0; i < colorcount; i++) {
int d = ssq(r * 255 / 11 - (cs[i].red >> 8)) +
ssq(g * 255 / 23 - (cs[i].green >> 8)) +
ssq(b * 255 / 11 - (cs[i].blue >> 8));
if(d < mind) {
mini = i;
mind = d;
}
}
*cm++ = mini;
}
Xgetpixel = GetPseudoColorPixel;
}
// XFree(v);

Font::SetStdFont(ScreenSans(12));
}

void InitX11Draw(const char *dispname)
{
#ifdef flagNOGTK
if(!dispname || !*dispname) {
int f = Environment().Find("DISPLAY");
dispname = (f >= 0 ? ~Environment()[f] : ":0.0");
}
InitX11Draw(XOpenDisplay(dispname));
#else
MemoryIgnoreLeaksBlock __;
const Vector<String>& cmd = CommandLine();
char **argv = (char**) MemoryAllocPermanent(sizeof(char *) * cmd.GetCount());
for(int i = 0; i < cmd.GetCount(); i++)
argv[i] = PermanentCopy(cmd[i]);
int argc = cmd.GetCount();
gtk_init (&argc, &argv);
GtkWidget *w = gtk_window_new(GTK_WINDOW_TOPLEVEL);
GdkDisplay *d = gtk_widget_get_display(w);
gdk_x11_display_get_xdisplay(d);
InitX11Draw(gdk_x11_display_get_xdisplay(d));
gtk_widget_destroy(w);
#endif
}

void SetClip(GC gc, XftDraw *xftdraw, const Vector<Rect>& cl)
{
GuiLock __;
LTIMING("SetClip");
Buffer<XRectangle> xr(cl.GetCount());
LLOG("SetClip");
for(int i = 0; i < cl.GetCount(); i++) {
XRectangle& r = xr[i];
const Rect& cr = cl[i];
LLOG("[" << i << "] = " << cr);
r.x = cr.left;
r.y = cr.top;
r.width = cr.Width();
r.height = cr.Height();
}
XSetClipRectangles(Xdisplay, gc, 0, 0, xr, cl.GetCount(), Unsorted);
LLOG("XftDrawSetClipRectangles, # = " << cl.GetCount() << ", xftdraw = " << FormatIntHex(xftdraw));
XftDrawSetClipRectangles(xftdraw, 0, 0, xr, cl.GetCount());
LLOG("//XftDrawSetClipRectangles");
}

void SystemDraw::CloneClip()
{
if(cloff.GetCount() > 1 && cloff.Top().clipi == cloff[cloff.GetCount() - 2].clipi) {
cloff.Top().clipi = clip.GetCount();
Vector<Rect>& c = clip.Add();
c <<= clip[clip.GetCount() - 2];
}
}

void SystemDraw::SetForeground(Color color)
{
GuiLock __;
LTIMING("SetForeground");
int p = GetXPixel(color.GetR(), color.GetG(), color.GetB());
if(p == foreground) return;
LTIMING("XSetForeground");
LLOG("XSetForeground " << color);
foreground = p;
XSetForeground(Xdisplay, gc, foreground);
}

void SystemDraw::SetClip() {
GuiLock __;
drawingclip = Rect(-(INT_MAX >> 1), -(INT_MAX >> 1), +(INT_MAX >> 1), +(INT_MAX >> 1));
if(!clip.IsEmpty()) {
const Vector<Rect>& topclip = clip.Top();
drawingclip = Rect(0, 0, 0, 0);
if(!topclip.IsEmpty()) {
drawingclip = topclip[0];
for(int i = 1; i < topclip.GetCount(); i++)
drawingclip |= topclip[i];
}
drawingclip -= actual_offset;
}
if(dw == Xroot) return;
LTIMING("SetClip");
UPP::SetClip(gc, xftdraw, clip.Top());
}

void SystemDraw::SetLineStyle(int width)
{
GuiLock __;
if(width == linewidth) return;
linewidth = width;
if(IsNull(width))
width = 1;
if(width < PEN_SOLID) {
static const char dash[] = { 18, 6 };
static const char dot[] = { 3, 3 };
static const char dashdot[] = { 9, 6, 3, 6 };
static const char dashdotdot[] = { 9, 3, 3, 3, 3, 3 };
static struct {
const char *dash;
int len;
} ds[] = {
{ dash, __countof(dash) },
{ dot, __countof(dot) },
{ dashdot, __countof(dashdot) },
{ dashdotdot, __countof(dashdotdot) }
};
int i = -(width - PEN_DASH);
ASSERT(i >= 0 && i < 4);
XSetDashes(Xdisplay, gc, 0, ds[i].dash, ds[i].len);
}
XSetLineAttributes(Xdisplay, gc, max(width, 1),
width < PEN_SOLID ? LineOnOffDash : LineSolid, CapRound, JoinRound);
}

void SystemDraw::Init()
{
GuiLock __;
pageSize = Size(Xwidth, Xheight);
cloff.Clear();
clip.Clear();
drawingclip = Rect(-(INT_MAX >> 1), -(INT_MAX >> 1), +(INT_MAX >> 1), +(INT_MAX >> 1));
foreground = linewidth = Null;
}

void SystemDraw::Init(const Vector<Rect>& _clip, Point _offset)
{
GuiLock __;
Init();
clip.Add() <<= _clip;
offset.Add(_offset);
actual_offset = _offset;
Cloff& f = cloff.Add();
f.offseti = 0;
f.clipi = 0;
SetClip();
}

dword SystemDraw::GetInfo() const
{
return 0;
}

Size SystemDraw::GetPageSize() const
{
return pageSize;
}

SystemDraw::SystemDraw()
{
GuiLock __;
dw = None;
gc = None;
actual_offset = Point(0, 0);
Init();
}

Size SystemDraw::GetNativeDpi() const
{
return Size(96, 96);
}

void SystemDraw::BeginNative() {}
void SystemDraw::EndNative() {}

int SystemDraw::GetCloffLevel() const
{
return cloff.GetCount();
}

Rect SystemDraw::GetClip() const
{
LLOG("Draw::GetClipOp; #clip = " << clip.GetCount() << ", #cloff = " << cloff.GetCount()
<< ", clipi = " << cloff.Top().clipi);
const Vector<Rect>& cl = clip[cloff.Top().clipi];
Rect box(0, 0, 0, 0);
if(!cl.GetCount()) return box;
box = cl[0];
LLOG("cl[0] = " << box);
for(int i = 1; i < cl.GetCount(); i++) {
LLOG("cl[" << i << "] = " << cl[i]);
box |= cl[i];
}
LLOG("out box = " << box << ", actual offset = " << actual_offset);
return box - actual_offset;
}

SystemDraw::SystemDraw(Drawable _dw, GC _gc, XftDraw *_xftdraw, const Vector<Rect>& _clip)
{
LLOG("SystemDraw");
dw = _dw;
gc = _gc;
xftdraw = _xftdraw;
Init(_clip);
}

void BackDraw::Create(SystemDraw& w, int cx, int cy)
{
GuiLock __;
LLOG("Creating BackDraw " << cx << "x" << cy);
Destroy();
size.cx = cx;
size.cy = cy;
dw = XCreatePixmap(Xdisplay, w.GetDrawable(), max(cx, 1), max(cy, 1), Xdepth);
gc = XCreateGC(Xdisplay, dw, 0, 0);
xftdraw = XftDrawCreate(Xdisplay, (Drawable) dw,
DefaultVisual(Xdisplay, Xscreenno), Xcolormap);
Vector<Rect> clip;
clip.Add(RectC(0, 0, cx, cy));
Init(clip, Point(0, 0));
}

void BackDraw::Put(SystemDraw& w, int x, int y)
{
GuiLock __;
LLOG("Putting BackDraw");
ASSERT(dw != None);
XCopyArea(Xdisplay, dw, w.GetDrawable(), w.GetGC(), 0, 0, size.cx, size.cy,
x + w.GetOffset().x, y + w.GetOffset().y);
}

void BackDraw::Destroy()
{
GuiLock __;
if(dw != None) {
XftDrawDestroy(xftdraw);
XFreePixmap(Xdisplay, dw);
XFreeGC(Xdisplay, gc);
}
}

bool ScreenInPaletteMode()
{
return Xpalette;
}

Size GetScreenSize()
{
return Size(Xwidth, Xheight);
}

END_UPP_NAMESPACE

#endif

Change log

r4457 by cxl on Jan 21, 2012   Diff
*uppsrc: Fixed many GCC warnings
Go to: 
Project members, sign in to write a code review

Older revisions

r4453 by cxl on Jan 21, 2012   Diff
Fixed some warning
r3509 by cxl on Jun 10, 2011   Diff
.developing rainbow
r3501 by cxl on Jun 9, 2011   Diff
.developing rainbow
All revisions of this file

File info

Size: 10918 bytes, 463 lines

File properties

svn:mergeinfo
Powered by Google Project Hosting