File: xf_graphics.c

package info (click to toggle)
freerdp2 2.3.0%2Bdfsg1-2%2Bdeb11u1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 27,060 kB
  • sloc: ansic: 330,648; xml: 1,676; cpp: 821; sh: 748; python: 638; perl: 231; lisp: 120; makefile: 86
file content (747 lines) | stat: -rw-r--r-- 18,491 bytes parent folder | download
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
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
/**
 * FreeRDP: A Remote Desktop Protocol Implementation
 * X11 Graphical Objects
 *
 * Copyright 2011 Marc-Andre Moreau <marcandre.moreau@gmail.com>
 * Copyright 2016 Thincast Technologies GmbH
 * Copyright 2016 Armin Novak <armin.novak@thincast.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <X11/Xlib.h>
#include <X11/Xutil.h>

#ifdef WITH_XCURSOR
#include <X11/Xcursor/Xcursor.h>
#endif

#include <winpr/crt.h>

#include <freerdp/codec/bitmap.h>
#include <freerdp/codec/rfx.h>

#include "xf_graphics.h"
#include "xf_gdi.h"
#include "xf_event.h"

#include <freerdp/log.h>
#define TAG CLIENT_TAG("x11")

BOOL xf_decode_color(xfContext* xfc, const UINT32 srcColor, XColor* color)
{
	rdpGdi* gdi;
	rdpSettings* settings;
	UINT32 SrcFormat;
	BYTE r, g, b, a;

	if (!xfc || !color)
		return FALSE;

	gdi = xfc->context.gdi;

	if (!gdi)
		return FALSE;

	settings = xfc->context.settings;

	if (!settings)
		return FALSE;

	switch (settings->ColorDepth)
	{
		case 32:
		case 24:
			SrcFormat = PIXEL_FORMAT_BGR24;
			break;

		case 16:
			SrcFormat = PIXEL_FORMAT_RGB16;
			break;

		case 15:
			SrcFormat = PIXEL_FORMAT_RGB15;
			break;

		case 8:
			SrcFormat = PIXEL_FORMAT_RGB8;
			break;

		default:
			return FALSE;
	}

	SplitColor(srcColor, SrcFormat, &r, &g, &b, &a, &gdi->palette);
	color->blue = (unsigned short)(b << 8);
	color->green = (unsigned short)(g << 8);
	color->red = (unsigned short)(r << 8);
	color->flags = DoRed | DoGreen | DoBlue;

	if (XAllocColor(xfc->display, xfc->colormap, color) == 0)
		return FALSE;

	return TRUE;
}

/* Bitmap Class */
static BOOL xf_Bitmap_New(rdpContext* context, rdpBitmap* bitmap)
{
	BOOL rc = FALSE;
	UINT32 depth;
	BYTE* data;
	rdpGdi* gdi;
	xfBitmap* xbitmap = (xfBitmap*)bitmap;
	xfContext* xfc = (xfContext*)context;

	if (!context || !bitmap || !context->gdi)
		return FALSE;

	gdi = context->gdi;
	xf_lock_x11(xfc);
	depth = GetBitsPerPixel(bitmap->format);
	xbitmap->pixmap =
	    XCreatePixmap(xfc->display, xfc->drawable, bitmap->width, bitmap->height, xfc->depth);

	if (!xbitmap->pixmap)
		goto unlock;

	if (bitmap->data)
	{
		XSetFunction(xfc->display, xfc->gc, GXcopy);

		if ((INT64)depth != xfc->depth)
		{
			if (!(data = _aligned_malloc(bitmap->width * bitmap->height * 4ULL, 16)))
				goto unlock;

			if (!freerdp_image_copy(data, gdi->dstFormat, 0, 0, 0, bitmap->width, bitmap->height,
			                        bitmap->data, bitmap->format, 0, 0, 0, &context->gdi->palette,
			                        FREERDP_FLIP_NONE))
			{
				_aligned_free(data);
				goto unlock;
			}

			_aligned_free(bitmap->data);
			bitmap->data = data;
			bitmap->format = gdi->dstFormat;
		}

		xbitmap->image =
		    XCreateImage(xfc->display, xfc->visual, xfc->depth, ZPixmap, 0, (char*)bitmap->data,
		                 bitmap->width, bitmap->height, xfc->scanline_pad, 0);

		if (!xbitmap->image)
			goto unlock;

		xbitmap->image->byte_order = LSBFirst;
		xbitmap->image->bitmap_bit_order = LSBFirst;
		XPutImage(xfc->display, xbitmap->pixmap, xfc->gc, xbitmap->image, 0, 0, 0, 0, bitmap->width,
		          bitmap->height);
	}

	rc = TRUE;
unlock:
	xf_unlock_x11(xfc);
	return rc;
}

static void xf_Bitmap_Free(rdpContext* context, rdpBitmap* bitmap)
{
	xfContext* xfc = (xfContext*)context;
	xfBitmap* xbitmap = (xfBitmap*)bitmap;

	if (!xfc || !xbitmap)
		return;

	xf_lock_x11(xfc);

	if (xbitmap->pixmap != 0)
	{
		XFreePixmap(xfc->display, xbitmap->pixmap);
		xbitmap->pixmap = 0;
	}

	if (xbitmap->image)
	{
		xbitmap->image->data = NULL;
		XDestroyImage(xbitmap->image);
		xbitmap->image = NULL;
	}

	xf_unlock_x11(xfc);
	_aligned_free(bitmap->data);
	free(xbitmap);
}

static BOOL xf_Bitmap_Paint(rdpContext* context, rdpBitmap* bitmap)
{
	int width, height;
	xfContext* xfc = (xfContext*)context;
	xfBitmap* xbitmap = (xfBitmap*)bitmap;
	BOOL ret;

	if (!context || !xbitmap)
		return FALSE;

	width = bitmap->right - bitmap->left + 1;
	height = bitmap->bottom - bitmap->top + 1;
	xf_lock_x11(xfc);
	XSetFunction(xfc->display, xfc->gc, GXcopy);
	XPutImage(xfc->display, xfc->primary, xfc->gc, xbitmap->image, 0, 0, bitmap->left, bitmap->top,
	          width, height);
	ret = gdi_InvalidateRegion(xfc->hdc, bitmap->left, bitmap->top, width, height);
	xf_unlock_x11(xfc);
	return ret;
}

static BOOL xf_Bitmap_SetSurface(rdpContext* context, rdpBitmap* bitmap, BOOL primary)
{
	xfContext* xfc = (xfContext*)context;

	if (!context || (!bitmap && !primary))
		return FALSE;

	xf_lock_x11(xfc);

	if (primary)
		xfc->drawing = xfc->primary;
	else
		xfc->drawing = ((xfBitmap*)bitmap)->pixmap;

	xf_unlock_x11(xfc);
	return TRUE;
}

static BOOL _xf_Pointer_GetCursorForCurrentScale(rdpContext* context, const rdpPointer* pointer,
                                                 Cursor* cursor)
{
#ifdef WITH_XCURSOR
	UINT32 CursorFormat;
	xfContext* xfc = (xfContext*)context;
	xfPointer* xpointer = (xfPointer*)pointer;
	XcursorImage ci;
	rdpSettings* settings;
	UINT32 xTargetSize;
	UINT32 yTargetSize;
	double xscale;
	double yscale;
	size_t size;
	int cursorIndex = -1;

	if (!context || !pointer || !context->gdi)
		return FALSE;

	settings = xfc->context.settings;

	if (!settings)
		return FALSE;

	xscale = (settings->SmartSizing ? xfc->scaledWidth / (double)settings->DesktopWidth : 1);
	yscale = (settings->SmartSizing ? xfc->scaledHeight / (double)settings->DesktopHeight : 1);
	xTargetSize = pointer->width * xscale;
	yTargetSize = pointer->height * yscale;

	for (int i = 0; i < xpointer->nCursors; i++)
	{
		if (xpointer->cursorWidths[i] == xTargetSize && xpointer->cursorHeights[i] == yTargetSize)
		{
			cursorIndex = i;
		}
	}

	if (cursorIndex == -1)
	{
		xf_lock_x11(xfc);

		if (!xfc->invert)
			CursorFormat = (!xfc->big_endian) ? PIXEL_FORMAT_RGBA32 : PIXEL_FORMAT_ABGR32;
		else
			CursorFormat = (!xfc->big_endian) ? PIXEL_FORMAT_BGRA32 : PIXEL_FORMAT_ARGB32;

		if (xpointer->nCursors == xpointer->mCursors)
		{
			xpointer->mCursors = (xpointer->mCursors == 0 ? 1 : xpointer->mCursors * 2);

			if (!(xpointer->cursorWidths = (UINT32*)realloc(xpointer->cursorWidths,
			                                                sizeof(UINT32) * xpointer->mCursors)))
			{
				xf_unlock_x11(xfc);
				return FALSE;
			}
			if (!(xpointer->cursorHeights = (UINT32*)realloc(xpointer->cursorHeights,
			                                                 sizeof(UINT32) * xpointer->mCursors)))
			{
				xf_unlock_x11(xfc);
				return FALSE;
			}
			if (!(xpointer->cursors =
			          (Cursor*)realloc(xpointer->cursors, sizeof(Cursor) * xpointer->mCursors)))
			{
				xf_unlock_x11(xfc);
				return FALSE;
			}
		}

		ZeroMemory(&ci, sizeof(ci));
		ci.version = XCURSOR_IMAGE_VERSION;
		ci.size = sizeof(ci);
		ci.width = xTargetSize;
		ci.height = yTargetSize;
		ci.xhot = pointer->xPos * xscale;
		ci.yhot = pointer->yPos * yscale;
		size = ci.height * ci.width * GetBytesPerPixel(CursorFormat) * 1ULL;

		if (xscale != 1 || yscale != 1)
		{
			if (!(ci.pixels = (XcursorPixel*)_aligned_malloc(size, 16)))
			{
				xf_unlock_x11(xfc);
				return FALSE;
			}

			if (!freerdp_image_scale((BYTE*)ci.pixels, CursorFormat, 0, 0, 0, ci.width, ci.height,
			                         (BYTE*)xpointer->cursorPixels, CursorFormat, 0, 0, 0,
			                         pointer->width, pointer->height))
			{
				_aligned_free(ci.pixels);
				xf_unlock_x11(xfc);
				return FALSE;
			}
		}
		else
		{
			ci.pixels = xpointer->cursorPixels;
		}

		cursorIndex = xpointer->nCursors;
		xpointer->cursorWidths[cursorIndex] = ci.width;
		xpointer->cursorHeights[cursorIndex] = ci.height;
		xpointer->cursors[cursorIndex] = XcursorImageLoadCursor(xfc->display, &ci);
		xpointer->nCursors += 1;
		if (xscale != 1 || yscale != 1)
			_aligned_free(ci.pixels);

		xf_unlock_x11(xfc);
	}

	cursor[0] = xpointer->cursors[cursorIndex];
#endif
	return TRUE;
}

/* Pointer Class */
static Window xf_Pointer_get_window(xfContext* xfc)
{
	if (!xfc)
	{
		WLog_WARN(TAG, "xf_Pointer: Invalid context");
		return 0;
	}
	if (xfc->remote_app)
	{
		if (!xfc->appWindow)
		{
			WLog_WARN(TAG, "xf_Pointer: Invalid appWindow");
			return 0;
		}
		return xfc->appWindow->handle;
	}
	else
	{
		if (!xfc->window)
		{
			WLog_WARN(TAG, "xf_Pointer: Invalid window");
			return 0;
		}
		return xfc->window->handle;
	}
}

static BOOL xf_Pointer_New(rdpContext* context, rdpPointer* pointer)
{
#ifdef WITH_XCURSOR
	UINT32 CursorFormat;
	size_t size;
	xfContext* xfc = (xfContext*)context;
	xfPointer* xpointer = (xfPointer*)pointer;

	if (!context || !pointer || !context->gdi)
		return FALSE;

	if (!xfc->invert)
		CursorFormat = (!xfc->big_endian) ? PIXEL_FORMAT_RGBA32 : PIXEL_FORMAT_ABGR32;
	else
		CursorFormat = (!xfc->big_endian) ? PIXEL_FORMAT_BGRA32 : PIXEL_FORMAT_ARGB32;

	xpointer->nCursors = 0;
	xpointer->mCursors = 0;

	size = pointer->height * pointer->width * GetBytesPerPixel(CursorFormat) * 1ULL;

	if (!(xpointer->cursorPixels = (XcursorPixel*)_aligned_malloc(size, 16)))
		return FALSE;

	if (!freerdp_image_copy_from_pointer_data(
	        (BYTE*)xpointer->cursorPixels, CursorFormat, 0, 0, 0, pointer->width, pointer->height,
	        pointer->xorMaskData, pointer->lengthXorMask, pointer->andMaskData,
	        pointer->lengthAndMask, pointer->xorBpp, &context->gdi->palette))
	{
		_aligned_free(xpointer->cursorPixels);
		return FALSE;
	}

	if (!_xf_Pointer_GetCursorForCurrentScale(context, pointer, &(xpointer->cursor)))
		return FALSE;
#endif
	return TRUE;
}

static void xf_Pointer_Free(rdpContext* context, rdpPointer* pointer)
{
#ifdef WITH_XCURSOR
	xfContext* xfc = (xfContext*)context;
	xfPointer* xpointer = (xfPointer*)pointer;

	xf_lock_x11(xfc);

	_aligned_free(xpointer->cursorPixels);
	free(xpointer->cursorWidths);
	free(xpointer->cursorHeights);

	for (int i = 0; i < xpointer->nCursors; i++)
	{
		XFreeCursor(xfc->display, xpointer->cursors[i]);
	}

	free(xpointer->cursors);
	xpointer->nCursors = 0;
	xpointer->mCursors = 0;

	xf_unlock_x11(xfc);
#endif
}

static BOOL xf_Pointer_Set(rdpContext* context, const rdpPointer* pointer)
{
#ifdef WITH_XCURSOR
	xfContext* xfc = (xfContext*)context;
	Window handle = xf_Pointer_get_window(xfc);
	xfc->pointer = (xfPointer*)pointer;

	/* in RemoteApp mode, window can be null if none has had focus */

	if (handle)
	{
		if (!_xf_Pointer_GetCursorForCurrentScale(context, pointer, &(xfc->pointer->cursor)))
			return FALSE;
		xf_lock_x11(xfc);
		XDefineCursor(xfc->display, handle, xfc->pointer->cursor);
		xf_unlock_x11(xfc);
	}
#endif
	return TRUE;
}

static BOOL xf_Pointer_SetNull(rdpContext* context)
{
#ifdef WITH_XCURSOR
	xfContext* xfc = (xfContext*)context;
	static Cursor nullcursor = None;
	Window handle = xf_Pointer_get_window(xfc);
	xf_lock_x11(xfc);

	if (nullcursor == None)
	{
		XcursorImage ci;
		XcursorPixel xp = 0;
		ZeroMemory(&ci, sizeof(ci));
		ci.version = XCURSOR_IMAGE_VERSION;
		ci.size = sizeof(ci);
		ci.width = ci.height = 1;
		ci.xhot = ci.yhot = 0;
		ci.pixels = &xp;
		nullcursor = XcursorImageLoadCursor(xfc->display, &ci);
	}

	xfc->pointer = NULL;

	if ((handle) && (nullcursor != None))
		XDefineCursor(xfc->display, handle, nullcursor);

	xf_unlock_x11(xfc);
#endif
	return TRUE;
}

static BOOL xf_Pointer_SetDefault(rdpContext* context)
{
#ifdef WITH_XCURSOR
	xfContext* xfc = (xfContext*)context;
	Window handle = xf_Pointer_get_window(xfc);
	xf_lock_x11(xfc);
	xfc->pointer = NULL;

	if (handle)
		XUndefineCursor(xfc->display, handle);

	xf_unlock_x11(xfc);
#endif
	return TRUE;
}

static BOOL xf_Pointer_SetPosition(rdpContext* context, UINT32 x, UINT32 y)
{
	xfContext* xfc = (xfContext*)context;
	XWindowAttributes current;
	XSetWindowAttributes tmp;
	BOOL ret = FALSE;
	Status rc;
	Window handle = xf_Pointer_get_window(xfc);

	if (!handle)
	{
		WLog_WARN(TAG, "xf_Pointer_SetPosition: focus %d, handle%lu", xfc->focused, handle);
		return TRUE;
	}

	if (xfc->remote_app && !xfc->focused)
		return TRUE;

	xf_adjust_coordinates_to_screen(xfc, &x, &y);

	xf_lock_x11(xfc);

	rc = XGetWindowAttributes(xfc->display, handle, &current);
	if (rc == 0)
	{
		WLog_WARN(TAG, "xf_Pointer_SetPosition: XGetWindowAttributes==%d", rc);
		goto out;
	}

	tmp.event_mask = (current.your_event_mask & ~(PointerMotionMask));

	rc = XChangeWindowAttributes(xfc->display, handle, CWEventMask, &tmp);
	if (rc == 0)
	{
		WLog_WARN(TAG, "xf_Pointer_SetPosition: XChangeWindowAttributes==%d", rc);
		goto out;
	}

	rc = XWarpPointer(xfc->display, None, handle, 0, 0, 0, 0, x, y);
	if (rc == 0)
		WLog_WARN(TAG, "xf_Pointer_SetPosition: XWarpPointer==%d", rc);
	tmp.event_mask = current.your_event_mask;
	rc = XChangeWindowAttributes(xfc->display, handle, CWEventMask, &tmp);
	if (rc == 0)
		WLog_WARN(TAG, "xf_Pointer_SetPosition: 2.try XChangeWindowAttributes==%d", rc);
	ret = TRUE;
out:
	xf_unlock_x11(xfc);
	return ret;
}

/* Glyph Class */
static BOOL xf_Glyph_New(rdpContext* context, const rdpGlyph* glyph)
{
	int scanline;
	XImage* image;
	xfGlyph* xf_glyph;
	xf_glyph = (xfGlyph*)glyph;
	xfContext* xfc = (xfContext*)context;
	xf_lock_x11(xfc);
	scanline = (glyph->cx + 7) / 8;
	xf_glyph->pixmap = XCreatePixmap(xfc->display, xfc->drawing, glyph->cx, glyph->cy, 1);
	image = XCreateImage(xfc->display, xfc->visual, 1, ZPixmap, 0, (char*)glyph->aj, glyph->cx,
	                     glyph->cy, 8, scanline);
	image->byte_order = MSBFirst;
	image->bitmap_bit_order = MSBFirst;
	XInitImage(image);
	XPutImage(xfc->display, xf_glyph->pixmap, xfc->gc_mono, image, 0, 0, 0, 0, glyph->cx,
	          glyph->cy);
	image->data = NULL;
	XDestroyImage(image);
	xf_unlock_x11(xfc);
	return TRUE;
}

static void xf_Glyph_Free(rdpContext* context, rdpGlyph* glyph)
{
	xfContext* xfc = (xfContext*)context;
	xf_lock_x11(xfc);

	if (((xfGlyph*)glyph)->pixmap != 0)
		XFreePixmap(xfc->display, ((xfGlyph*)glyph)->pixmap);

	xf_unlock_x11(xfc);
	free(glyph->aj);
	free(glyph);
}

static BOOL xf_Glyph_Draw(rdpContext* context, const rdpGlyph* glyph, INT32 x, INT32 y, INT32 w,
                          INT32 h, INT32 sx, INT32 sy, BOOL fOpRedundant)
{
	xfGlyph* xf_glyph;
	xfContext* xfc = (xfContext*)context;
	xf_glyph = (xfGlyph*)glyph;
	xf_lock_x11(xfc);

	if (!fOpRedundant)
	{
		XSetFillStyle(xfc->display, xfc->gc, FillOpaqueStippled);
		XFillRectangle(xfc->display, xfc->drawable, xfc->gc, x, y, w, h);
	}

	XSetFillStyle(xfc->display, xfc->gc, FillStippled);
	XSetStipple(xfc->display, xfc->gc, xf_glyph->pixmap);

	if (sx || sy)
		WLog_ERR(TAG, "");

	// XSetClipOrigin(xfc->display, xfc->gc, sx, sy);
	XSetTSOrigin(xfc->display, xfc->gc, x, y);
	XFillRectangle(xfc->display, xfc->drawing, xfc->gc, x, y, w, h);
	xf_unlock_x11(xfc);
	return TRUE;
}

static BOOL xf_Glyph_BeginDraw(rdpContext* context, INT32 x, INT32 y, INT32 width, INT32 height,
                               UINT32 bgcolor, UINT32 fgcolor, BOOL fOpRedundant)
{
	xfContext* xfc = (xfContext*)context;
	XRectangle rect;
	XColor xbgcolor, xfgcolor;

	if (!xf_decode_color(xfc, bgcolor, &xbgcolor))
		return FALSE;

	if (!xf_decode_color(xfc, fgcolor, &xfgcolor))
		return FALSE;

	rect.x = x;
	rect.y = y;
	rect.width = width;
	rect.height = height;
	xf_lock_x11(xfc);

	if (!fOpRedundant)
	{
		XSetForeground(xfc->display, xfc->gc, xfgcolor.pixel);
		XSetBackground(xfc->display, xfc->gc, xfgcolor.pixel);
		XSetFillStyle(xfc->display, xfc->gc, FillOpaqueStippled);
		XFillRectangle(xfc->display, xfc->drawable, xfc->gc, x, y, width, height);
	}

	XSetForeground(xfc->display, xfc->gc, xbgcolor.pixel);
	XSetBackground(xfc->display, xfc->gc, xfgcolor.pixel);
	xf_unlock_x11(xfc);
	return TRUE;
}

static BOOL xf_Glyph_EndDraw(rdpContext* context, INT32 x, INT32 y, INT32 width, INT32 height,
                             UINT32 bgcolor, UINT32 fgcolor)
{
	xfContext* xfc = (xfContext*)context;
	BOOL ret = TRUE;
	XColor xfgcolor, xbgcolor;

	if (!xf_decode_color(xfc, bgcolor, &xbgcolor))
		return FALSE;

	if (!xf_decode_color(xfc, fgcolor, &xfgcolor))
		return FALSE;

	if (xfc->drawing == xfc->primary)
		ret = gdi_InvalidateRegion(xfc->hdc, x, y, width, height);

	return ret;
}

/* Graphics Module */
BOOL xf_register_pointer(rdpGraphics* graphics)
{
	rdpPointer* pointer = NULL;

	if (!(pointer = (rdpPointer*)calloc(1, sizeof(rdpPointer))))
		return FALSE;

	pointer->size = sizeof(xfPointer);
	pointer->New = xf_Pointer_New;
	pointer->Free = xf_Pointer_Free;
	pointer->Set = xf_Pointer_Set;
	pointer->SetNull = xf_Pointer_SetNull;
	pointer->SetDefault = xf_Pointer_SetDefault;
	pointer->SetPosition = xf_Pointer_SetPosition;
	graphics_register_pointer(graphics, pointer);
	free(pointer);
	return TRUE;
}

BOOL xf_register_graphics(rdpGraphics* graphics)
{
	rdpBitmap bitmap;
	rdpGlyph glyph;

	if (!graphics || !graphics->Bitmap_Prototype || !graphics->Glyph_Prototype)
		return FALSE;

	bitmap = *graphics->Bitmap_Prototype;
	glyph = *graphics->Glyph_Prototype;
	bitmap.size = sizeof(xfBitmap);
	bitmap.New = xf_Bitmap_New;
	bitmap.Free = xf_Bitmap_Free;
	bitmap.Paint = xf_Bitmap_Paint;
	bitmap.SetSurface = xf_Bitmap_SetSurface;
	graphics_register_bitmap(graphics, &bitmap);
	glyph.size = sizeof(xfGlyph);
	glyph.New = xf_Glyph_New;
	glyph.Free = xf_Glyph_Free;
	glyph.Draw = xf_Glyph_Draw;
	glyph.BeginDraw = xf_Glyph_BeginDraw;
	glyph.EndDraw = xf_Glyph_EndDraw;
	graphics_register_glyph(graphics, &glyph);
	return TRUE;
}

UINT32 xf_get_local_color_format(xfContext* xfc, BOOL aligned)
{
	UINT32 DstFormat;
	BOOL invert = FALSE;

	if (!xfc)
		return 0;

	invert = xfc->invert;

	if (xfc->depth == 32)
		DstFormat = (!invert) ? PIXEL_FORMAT_RGBA32 : PIXEL_FORMAT_BGRA32;
	else if (xfc->depth == 24)
	{
		if (aligned)
			DstFormat = (!invert) ? PIXEL_FORMAT_RGBX32 : PIXEL_FORMAT_BGRX32;
		else
			DstFormat = (!invert) ? PIXEL_FORMAT_RGB24 : PIXEL_FORMAT_BGR24;
	}
	else if (xfc->depth == 16)
		DstFormat = PIXEL_FORMAT_RGB16;
	else if (xfc->depth == 15)
		DstFormat = PIXEL_FORMAT_RGB15;
	else
		DstFormat = (!invert) ? PIXEL_FORMAT_RGBX32 : PIXEL_FORMAT_BGRX32;

	return DstFormat;
}