winex11: Create all the GL drawables using the gdi_display to avoid synchronization...
[wine] / dlls / winex11.drv / graphics.c
1 /*
2  * X11 graphics driver graphics functions
3  *
4  * Copyright 1993,1994 Alexandre Julliard
5  * Copyright 1998 Huw Davies
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20  */
21
22 /*
23  * FIXME: only some of these functions obey the GM_ADVANCED
24  * graphics mode
25  */
26
27 #include "config.h"
28
29 #include <stdarg.h>
30 #include <math.h>
31 #ifdef HAVE_FLOAT_H
32 # include <float.h>
33 #endif
34 #include <stdlib.h>
35 #ifndef PI
36 #define PI M_PI
37 #endif
38 #include <string.h>
39
40 #include "windef.h"
41 #include "winbase.h"
42 #include "winreg.h"
43
44 #include "x11drv.h"
45 #include "x11font.h"
46 #include "wine/debug.h"
47 #include "wine/unicode.h"
48
49 WINE_DEFAULT_DEBUG_CHANNEL(graphics);
50
51 #define ABS(x)    ((x)<0?(-(x)):(x))
52
53   /* ROP code to GC function conversion */
54 const int X11DRV_XROPfunction[16] =
55 {
56     GXclear,        /* R2_BLACK */
57     GXnor,          /* R2_NOTMERGEPEN */
58     GXandInverted,  /* R2_MASKNOTPEN */
59     GXcopyInverted, /* R2_NOTCOPYPEN */
60     GXandReverse,   /* R2_MASKPENNOT */
61     GXinvert,       /* R2_NOT */
62     GXxor,          /* R2_XORPEN */
63     GXnand,         /* R2_NOTMASKPEN */
64     GXand,          /* R2_MASKPEN */
65     GXequiv,        /* R2_NOTXORPEN */
66     GXnoop,         /* R2_NOP */
67     GXorInverted,   /* R2_MERGENOTPEN */
68     GXcopy,         /* R2_COPYPEN */
69     GXorReverse,    /* R2_MERGEPENNOT */
70     GXor,           /* R2_MERGEPEN */
71     GXset           /* R2_WHITE */
72 };
73
74
75 /***********************************************************************
76  *           X11DRV_GetRegionData
77  *
78  * Calls GetRegionData on the given region and converts the rectangle
79  * array to XRectangle format. The returned buffer must be freed by
80  * caller using HeapFree(GetProcessHeap(),...).
81  * If hdc_lptodp is not 0, the rectangles are converted through LPtoDP.
82  */
83 RGNDATA *X11DRV_GetRegionData( HRGN hrgn, HDC hdc_lptodp )
84 {
85     RGNDATA *data;
86     DWORD size;
87     unsigned int i;
88     RECT *rect, tmp;
89     XRectangle *xrect;
90
91     if (!(size = GetRegionData( hrgn, 0, NULL ))) return NULL;
92     if (sizeof(XRectangle) > sizeof(RECT))
93     {
94         /* add extra size for XRectangle array */
95         int count = (size - sizeof(RGNDATAHEADER)) / sizeof(RECT);
96         size += count * (sizeof(XRectangle) - sizeof(RECT));
97     }
98     if (!(data = HeapAlloc( GetProcessHeap(), 0, size ))) return NULL;
99     if (!GetRegionData( hrgn, size, data ))
100     {
101         HeapFree( GetProcessHeap(), 0, data );
102         return NULL;
103     }
104
105     rect = (RECT *)data->Buffer;
106     xrect = (XRectangle *)data->Buffer;
107     if (hdc_lptodp)  /* map to device coordinates */
108     {
109         LPtoDP( hdc_lptodp, (POINT *)rect, data->rdh.nCount * 2 );
110         for (i = 0; i < data->rdh.nCount; i++)
111         {
112             if (rect[i].right < rect[i].left)
113             {
114                 INT tmp = rect[i].right;
115                 rect[i].right = rect[i].left;
116                 rect[i].left = tmp;
117             }
118             if (rect[i].bottom < rect[i].top)
119             {
120                 INT tmp = rect[i].bottom;
121                 rect[i].bottom = rect[i].top;
122                 rect[i].top = tmp;
123             }
124         }
125     }
126
127     if (sizeof(XRectangle) > sizeof(RECT))
128     {
129         int j;
130         /* need to start from the end */
131         for (j = data->rdh.nCount-1; j >= 0; j--)
132         {
133             tmp = rect[j];
134             xrect[j].x      = tmp.left;
135             xrect[j].y      = tmp.top;
136             xrect[j].width  = tmp.right - tmp.left;
137             xrect[j].height = tmp.bottom - tmp.top;
138         }
139     }
140     else
141     {
142         for (i = 0; i < data->rdh.nCount; i++)
143         {
144             tmp = rect[i];
145             xrect[i].x      = tmp.left;
146             xrect[i].y      = tmp.top;
147             xrect[i].width  = tmp.right - tmp.left;
148             xrect[i].height = tmp.bottom - tmp.top;
149         }
150     }
151     return data;
152 }
153
154
155 /***********************************************************************
156  *           X11DRV_SetDeviceClipping
157  */
158 void X11DRV_SetDeviceClipping( X11DRV_PDEVICE *physDev, HRGN vis_rgn, HRGN clip_rgn )
159 {
160     RGNDATA *data;
161
162     CombineRgn( physDev->region, vis_rgn, clip_rgn, clip_rgn ? RGN_AND : RGN_COPY );
163     if (!(data = X11DRV_GetRegionData( physDev->region, 0 ))) return;
164
165     wine_tsx11_lock();
166     XSetClipRectangles( gdi_display, physDev->gc, physDev->dc_rect.left, physDev->dc_rect.top,
167                         (XRectangle *)data->Buffer, data->rdh.nCount, YXBanded );
168     wine_tsx11_unlock();
169     HeapFree( GetProcessHeap(), 0, data );
170 }
171
172
173 /***********************************************************************
174  *           X11DRV_SetupGCForPatBlt
175  *
176  * Setup the GC for a PatBlt operation using current brush.
177  * If fMapColors is TRUE, X pixels are mapped to Windows colors.
178  * Return FALSE if brush is BS_NULL, TRUE otherwise.
179  */
180 BOOL X11DRV_SetupGCForPatBlt( X11DRV_PDEVICE *physDev, GC gc, BOOL fMapColors )
181 {
182     XGCValues val;
183     unsigned long mask;
184     Pixmap pixmap = 0;
185     POINT pt;
186
187     if (physDev->brush.style == BS_NULL) return FALSE;
188     if (physDev->brush.pixel == -1)
189     {
190         /* Special case used for monochrome pattern brushes.
191          * We need to swap foreground and background because
192          * Windows does it the wrong way...
193          */
194         val.foreground = physDev->backgroundPixel;
195         val.background = physDev->textPixel;
196     }
197     else
198     {
199         val.foreground = physDev->brush.pixel;
200         val.background = physDev->backgroundPixel;
201     }
202     if (fMapColors && X11DRV_PALETTE_XPixelToPalette)
203     {
204         val.foreground = X11DRV_PALETTE_XPixelToPalette[val.foreground];
205         val.background = X11DRV_PALETTE_XPixelToPalette[val.background];
206     }
207
208     val.function = X11DRV_XROPfunction[GetROP2(physDev->hdc)-1];
209     /*
210     ** Let's replace GXinvert by GXxor with (black xor white)
211     ** This solves the selection color and leak problems in excel
212     ** FIXME : Let's do that only if we work with X-pixels, not with Win-pixels
213     */
214     if (val.function == GXinvert)
215     {
216         val.foreground = (WhitePixel( gdi_display, DefaultScreen(gdi_display) ) ^
217                           BlackPixel( gdi_display, DefaultScreen(gdi_display) ));
218         val.function = GXxor;
219     }
220     val.fill_style = physDev->brush.fillStyle;
221     switch(val.fill_style)
222     {
223     case FillStippled:
224     case FillOpaqueStippled:
225         if (GetBkMode(physDev->hdc)==OPAQUE) val.fill_style = FillOpaqueStippled;
226         val.stipple = physDev->brush.pixmap;
227         mask = GCStipple;
228         break;
229
230     case FillTiled:
231         if (fMapColors && X11DRV_PALETTE_XPixelToPalette)
232         {
233             register int x, y;
234             XImage *image;
235             wine_tsx11_lock();
236             pixmap = XCreatePixmap( gdi_display, root_window, 8, 8, screen_depth );
237             image = XGetImage( gdi_display, physDev->brush.pixmap, 0, 0, 8, 8,
238                                AllPlanes, ZPixmap );
239             for (y = 0; y < 8; y++)
240                 for (x = 0; x < 8; x++)
241                     XPutPixel( image, x, y,
242                                X11DRV_PALETTE_XPixelToPalette[XGetPixel( image, x, y)] );
243             XPutImage( gdi_display, pixmap, gc, image, 0, 0, 0, 0, 8, 8 );
244             XDestroyImage( image );
245             wine_tsx11_unlock();
246             val.tile = pixmap;
247         }
248         else val.tile = physDev->brush.pixmap;
249         mask = GCTile;
250         break;
251
252     default:
253         mask = 0;
254         break;
255     }
256     GetBrushOrgEx( physDev->hdc, &pt );
257     val.ts_x_origin = physDev->dc_rect.left + pt.x;
258     val.ts_y_origin = physDev->dc_rect.top + pt.y;
259     val.fill_rule = (GetPolyFillMode(physDev->hdc) == WINDING) ? WindingRule : EvenOddRule;
260     wine_tsx11_lock();
261     XChangeGC( gdi_display, gc,
262                GCFunction | GCForeground | GCBackground | GCFillStyle |
263                GCFillRule | GCTileStipXOrigin | GCTileStipYOrigin | mask,
264                &val );
265     if (pixmap) XFreePixmap( gdi_display, pixmap );
266     wine_tsx11_unlock();
267     return TRUE;
268 }
269
270
271 /***********************************************************************
272  *           X11DRV_SetupGCForBrush
273  *
274  * Setup physDev->gc for drawing operations using current brush.
275  * Return FALSE if brush is BS_NULL, TRUE otherwise.
276  */
277 BOOL X11DRV_SetupGCForBrush( X11DRV_PDEVICE *physDev )
278 {
279     return X11DRV_SetupGCForPatBlt( physDev, physDev->gc, FALSE );
280 }
281
282
283 /***********************************************************************
284  *           X11DRV_SetupGCForPen
285  *
286  * Setup physDev->gc for drawing operations using current pen.
287  * Return FALSE if pen is PS_NULL, TRUE otherwise.
288  */
289 BOOL X11DRV_SetupGCForPen( X11DRV_PDEVICE *physDev )
290 {
291     XGCValues val;
292     UINT rop2 = GetROP2(physDev->hdc);
293
294     if (physDev->pen.style == PS_NULL) return FALSE;
295
296     switch (rop2)
297     {
298     case R2_BLACK :
299         val.foreground = BlackPixel( gdi_display, DefaultScreen(gdi_display) );
300         val.function = GXcopy;
301         break;
302     case R2_WHITE :
303         val.foreground = WhitePixel( gdi_display, DefaultScreen(gdi_display) );
304         val.function = GXcopy;
305         break;
306     case R2_XORPEN :
307         val.foreground = physDev->pen.pixel;
308         /* It is very unlikely someone wants to XOR with 0 */
309         /* This fixes the rubber-drawings in paintbrush */
310         if (val.foreground == 0)
311             val.foreground = (WhitePixel( gdi_display, DefaultScreen(gdi_display) ) ^
312                               BlackPixel( gdi_display, DefaultScreen(gdi_display) ));
313         val.function = GXxor;
314         break;
315     default :
316         val.foreground = physDev->pen.pixel;
317         val.function   = X11DRV_XROPfunction[rop2-1];
318     }
319     val.background = physDev->backgroundPixel;
320     val.fill_style = FillSolid;
321     val.line_width = physDev->pen.width;
322     if (val.line_width <= 1) {
323         val.cap_style = CapNotLast;
324     } else {
325         switch (physDev->pen.endcap)
326         {
327         case PS_ENDCAP_SQUARE:
328             val.cap_style = CapProjecting;
329             break;
330         case PS_ENDCAP_FLAT:
331             val.cap_style = CapButt;
332             break;
333         case PS_ENDCAP_ROUND:
334         default:
335             val.cap_style = CapRound;
336         }
337     }
338     switch (physDev->pen.linejoin)
339     {
340     case PS_JOIN_BEVEL:
341         val.join_style = JoinBevel;
342         break;
343     case PS_JOIN_MITER:
344         val.join_style = JoinMiter;
345         break;
346     case PS_JOIN_ROUND:
347     default:
348         val.join_style = JoinRound;
349     }
350
351     if (physDev->pen.dash_len)
352         val.line_style = ((GetBkMode(physDev->hdc) == OPAQUE) && (!physDev->pen.ext))
353                          ? LineDoubleDash : LineOnOffDash;
354     else
355         val.line_style = LineSolid;
356
357     wine_tsx11_lock();
358     if (physDev->pen.dash_len)
359         XSetDashes( gdi_display, physDev->gc, 0, physDev->pen.dashes, physDev->pen.dash_len );
360     XChangeGC( gdi_display, physDev->gc,
361                GCFunction | GCForeground | GCBackground | GCLineWidth |
362                GCLineStyle | GCCapStyle | GCJoinStyle | GCFillStyle, &val );
363     wine_tsx11_unlock();
364     return TRUE;
365 }
366
367
368 /***********************************************************************
369  *           X11DRV_SetupGCForText
370  *
371  * Setup physDev->gc for text drawing operations.
372  * Return FALSE if the font is null, TRUE otherwise.
373  */
374 BOOL X11DRV_SetupGCForText( X11DRV_PDEVICE *physDev )
375 {
376     XFontStruct* xfs = XFONT_GetFontStruct( physDev->font );
377
378     if( xfs )
379     {
380         XGCValues val;
381
382         val.function   = GXcopy;  /* Text is always GXcopy */
383         val.foreground = physDev->textPixel;
384         val.background = physDev->backgroundPixel;
385         val.fill_style = FillSolid;
386         val.font       = xfs->fid;
387
388         wine_tsx11_lock();
389         XChangeGC( gdi_display, physDev->gc,
390                    GCFunction | GCForeground | GCBackground | GCFillStyle |
391                    GCFont, &val );
392         wine_tsx11_unlock();
393         return TRUE;
394     }
395     WARN("Physical font failure\n" );
396     return FALSE;
397 }
398
399 /***********************************************************************
400  *           X11DRV_XWStoDS
401  *
402  * Performs a world-to-viewport transformation on the specified width.
403  */
404 INT X11DRV_XWStoDS( X11DRV_PDEVICE *physDev, INT width )
405 {
406     POINT pt[2];
407
408     pt[0].x = 0;
409     pt[0].y = 0;
410     pt[1].x = width;
411     pt[1].y = 0;
412     LPtoDP( physDev->hdc, pt, 2 );
413     return pt[1].x - pt[0].x;
414 }
415
416 /***********************************************************************
417  *           X11DRV_YWStoDS
418  *
419  * Performs a world-to-viewport transformation on the specified height.
420  */
421 INT X11DRV_YWStoDS( X11DRV_PDEVICE *physDev, INT height )
422 {
423     POINT pt[2];
424
425     pt[0].x = 0;
426     pt[0].y = 0;
427     pt[1].x = 0;
428     pt[1].y = height;
429     LPtoDP( physDev->hdc, pt, 2 );
430     return pt[1].y - pt[0].y;
431 }
432
433 /***********************************************************************
434  *           X11DRV_LineTo
435  */
436 BOOL
437 X11DRV_LineTo( X11DRV_PDEVICE *physDev, INT x, INT y )
438 {
439     POINT pt[2];
440
441     if (X11DRV_SetupGCForPen( physDev )) {
442         /* Update the pixmap from the DIB section */
443         X11DRV_LockDIBSection(physDev, DIB_Status_GdiMod);
444
445         GetCurrentPositionEx( physDev->hdc, &pt[0] );
446         pt[1].x = x;
447         pt[1].y = y;
448         LPtoDP( physDev->hdc, pt, 2 );
449
450         wine_tsx11_lock();
451         XDrawLine(gdi_display, physDev->drawable, physDev->gc,
452                   physDev->dc_rect.left + pt[0].x, physDev->dc_rect.top + pt[0].y,
453                   physDev->dc_rect.left + pt[1].x, physDev->dc_rect.top + pt[1].y );
454         wine_tsx11_unlock();
455
456         /* Update the DIBSection from the pixmap */
457         X11DRV_UnlockDIBSection(physDev, TRUE);
458     }
459     return TRUE;
460 }
461
462
463
464 /***********************************************************************
465  *           X11DRV_DrawArc
466  *
467  * Helper functions for Arc(), Chord() and Pie().
468  * 'lines' is the number of lines to draw: 0 for Arc, 1 for Chord, 2 for Pie.
469  *
470  */
471 static BOOL
472 X11DRV_DrawArc( X11DRV_PDEVICE *physDev, INT left, INT top, INT right,
473                 INT bottom, INT xstart, INT ystart,
474                 INT xend, INT yend, INT lines )
475 {
476     INT xcenter, ycenter, istart_angle, idiff_angle;
477     INT width, oldwidth;
478     double start_angle, end_angle;
479     XPoint points[4];
480     BOOL update = FALSE;
481     POINT start, end;
482     RECT rc;
483
484     SetRect(&rc, left, top, right, bottom);
485     start.x = xstart;
486     start.y = ystart;
487     end.x = xend;
488     end.y = yend;
489     LPtoDP(physDev->hdc, (POINT*)&rc, 2);
490     LPtoDP(physDev->hdc, &start, 1);
491     LPtoDP(physDev->hdc, &end, 1);
492
493     if (rc.right < rc.left) { INT tmp = rc.right; rc.right = rc.left; rc.left = tmp; }
494     if (rc.bottom < rc.top) { INT tmp = rc.bottom; rc.bottom = rc.top; rc.top = tmp; }
495     if ((rc.left == rc.right) || (rc.top == rc.bottom)
496             ||(lines && ((rc.right-rc.left==1)||(rc.bottom-rc.top==1)))) return TRUE;
497
498     if (GetArcDirection( physDev->hdc ) == AD_CLOCKWISE)
499       { POINT tmp = start; start = end; end = tmp; }
500
501     oldwidth = width = physDev->pen.width;
502     if (!width) width = 1;
503     if(physDev->pen.style == PS_NULL) width = 0;
504
505     if ((physDev->pen.style == PS_INSIDEFRAME))
506     {
507         if (2*width > (rc.right-rc.left)) width=(rc.right-rc.left + 1)/2;
508         if (2*width > (rc.bottom-rc.top)) width=(rc.bottom-rc.top + 1)/2;
509         rc.left   += width / 2;
510         rc.right  -= (width - 1) / 2;
511         rc.top    += width / 2;
512         rc.bottom -= (width - 1) / 2;
513     }
514     if(width == 0) width = 1; /* more accurate */
515     physDev->pen.width = width;
516
517     xcenter = (rc.right + rc.left) / 2;
518     ycenter = (rc.bottom + rc.top) / 2;
519     start_angle = atan2( (double)(ycenter-start.y)*(rc.right-rc.left),
520                          (double)(start.x-xcenter)*(rc.bottom-rc.top) );
521     end_angle   = atan2( (double)(ycenter-end.y)*(rc.right-rc.left),
522                          (double)(end.x-xcenter)*(rc.bottom-rc.top) );
523     if ((start.x==end.x)&&(start.y==end.y))
524       { /* A lazy program delivers xstart=xend=ystart=yend=0) */
525         start_angle = 0;
526         end_angle = 2* PI;
527       }
528     else /* notorious cases */
529       if ((start_angle == PI)&&( end_angle <0))
530         start_angle = - PI;
531     else
532       if ((end_angle == PI)&&( start_angle <0))
533         end_angle = - PI;
534     istart_angle = (INT)(start_angle * 180 * 64 / PI + 0.5);
535     idiff_angle  = (INT)((end_angle - start_angle) * 180 * 64 / PI + 0.5);
536     if (idiff_angle <= 0) idiff_angle += 360 * 64;
537
538     /* Update the pixmap from the DIB section */
539     X11DRV_LockDIBSection(physDev, DIB_Status_GdiMod);
540
541       /* Fill arc with brush if Chord() or Pie() */
542
543     if ((lines > 0) && X11DRV_SetupGCForBrush( physDev )) {
544         wine_tsx11_lock();
545         XSetArcMode( gdi_display, physDev->gc, (lines==1) ? ArcChord : ArcPieSlice);
546         XFillArc( gdi_display, physDev->drawable, physDev->gc,
547                   physDev->dc_rect.left + rc.left, physDev->dc_rect.top + rc.top,
548                   rc.right-rc.left-1, rc.bottom-rc.top-1, istart_angle, idiff_angle );
549         wine_tsx11_unlock();
550         update = TRUE;
551     }
552
553       /* Draw arc and lines */
554
555     if (X11DRV_SetupGCForPen( physDev ))
556     {
557         wine_tsx11_lock();
558         XDrawArc( gdi_display, physDev->drawable, physDev->gc,
559                   physDev->dc_rect.left + rc.left, physDev->dc_rect.top + rc.top,
560                   rc.right-rc.left-1, rc.bottom-rc.top-1, istart_angle, idiff_angle );
561         if (lines) {
562             /* use the truncated values */
563             start_angle=(double)istart_angle*PI/64./180.;
564             end_angle=(double)(istart_angle+idiff_angle)*PI/64./180.;
565             /* calculate the endpoints and round correctly */
566             points[0].x = (int) floor(physDev->dc_rect.left + (rc.right+rc.left)/2.0 +
567                     cos(start_angle) * (rc.right-rc.left-width*2+2) / 2. + 0.5);
568             points[0].y = (int) floor(physDev->dc_rect.top + (rc.top+rc.bottom)/2.0 -
569                     sin(start_angle) * (rc.bottom-rc.top-width*2+2) / 2. + 0.5);
570             points[1].x = (int) floor(physDev->dc_rect.left + (rc.right+rc.left)/2.0 +
571                     cos(end_angle) * (rc.right-rc.left-width*2+2) / 2. + 0.5);
572             points[1].y = (int) floor(physDev->dc_rect.top + (rc.top+rc.bottom)/2.0 -
573                     sin(end_angle) * (rc.bottom-rc.top-width*2+2) / 2. + 0.5);
574
575             /* OK, this stuff is optimized for Xfree86
576              * which is probably the server most used by
577              * wine users. Other X servers will not
578              * display correctly. (eXceed for instance)
579              * so if you feel you must make changes, make sure that
580              * you either use Xfree86 or separate your changes
581              * from these (compile switch or whatever)
582              */
583             if (lines == 2) {
584                 INT dx1,dy1;
585                 points[3] = points[1];
586                 points[1].x = physDev->dc_rect.left + xcenter;
587                 points[1].y = physDev->dc_rect.top + ycenter;
588                 points[2] = points[1];
589                 dx1=points[1].x-points[0].x;
590                 dy1=points[1].y-points[0].y;
591                 if(((rc.top-rc.bottom) | -2) == -2)
592                     if(dy1>0) points[1].y--;
593                 if(dx1<0) {
594                     if (((-dx1)*64)<=ABS(dy1)*37) points[0].x--;
595                     if(((-dx1*9))<(dy1*16)) points[0].y--;
596                     if( dy1<0 && ((dx1*9)) < (dy1*16)) points[0].y--;
597                 } else {
598                     if(dy1 < 0)  points[0].y--;
599                     if(((rc.right-rc.left) | -2) == -2) points[1].x--;
600                 }
601                 dx1=points[3].x-points[2].x;
602                 dy1=points[3].y-points[2].y;
603                 if(((rc.top-rc.bottom) | -2 ) == -2)
604                     if(dy1 < 0) points[2].y--;
605                 if( dx1<0){
606                     if( dy1>0) points[3].y--;
607                     if(((rc.right-rc.left) | -2) == -2 ) points[2].x--;
608                 }else {
609                     points[3].y--;
610                     if( dx1 * 64 < dy1 * -37 ) points[3].x--;
611                 }
612                 lines++;
613             }
614             XDrawLines( gdi_display, physDev->drawable, physDev->gc,
615                         points, lines+1, CoordModeOrigin );
616         }
617         wine_tsx11_unlock();
618         update = TRUE;
619     }
620
621     /* Update the DIBSection of the pixmap */
622     X11DRV_UnlockDIBSection(physDev, update);
623
624     physDev->pen.width = oldwidth;
625     return TRUE;
626 }
627
628
629 /***********************************************************************
630  *           X11DRV_Arc
631  */
632 BOOL
633 X11DRV_Arc( X11DRV_PDEVICE *physDev, INT left, INT top, INT right, INT bottom,
634             INT xstart, INT ystart, INT xend, INT yend )
635 {
636     return X11DRV_DrawArc( physDev, left, top, right, bottom,
637                            xstart, ystart, xend, yend, 0 );
638 }
639
640
641 /***********************************************************************
642  *           X11DRV_Pie
643  */
644 BOOL
645 X11DRV_Pie( X11DRV_PDEVICE *physDev, INT left, INT top, INT right, INT bottom,
646             INT xstart, INT ystart, INT xend, INT yend )
647 {
648     return X11DRV_DrawArc( physDev, left, top, right, bottom,
649                            xstart, ystart, xend, yend, 2 );
650 }
651
652 /***********************************************************************
653  *           X11DRV_Chord
654  */
655 BOOL
656 X11DRV_Chord( X11DRV_PDEVICE *physDev, INT left, INT top, INT right, INT bottom,
657               INT xstart, INT ystart, INT xend, INT yend )
658 {
659     return X11DRV_DrawArc( physDev, left, top, right, bottom,
660                            xstart, ystart, xend, yend, 1 );
661 }
662
663
664 /***********************************************************************
665  *           X11DRV_Ellipse
666  */
667 BOOL
668 X11DRV_Ellipse( X11DRV_PDEVICE *physDev, INT left, INT top, INT right, INT bottom )
669 {
670     INT width, oldwidth;
671     BOOL update = FALSE;
672     RECT rc;
673
674     SetRect(&rc, left, top, right, bottom);
675     LPtoDP(physDev->hdc, (POINT*)&rc, 2);
676
677     if ((rc.left == rc.right) || (rc.top == rc.bottom)) return TRUE;
678
679     if (rc.right < rc.left) { INT tmp = rc.right; rc.right = rc.left; rc.left = tmp; }
680     if (rc.bottom < rc.top) { INT tmp = rc.bottom; rc.bottom = rc.top; rc.top = tmp; }
681
682     oldwidth = width = physDev->pen.width;
683     if (!width) width = 1;
684     if(physDev->pen.style == PS_NULL) width = 0;
685
686     if ((physDev->pen.style == PS_INSIDEFRAME))
687     {
688         if (2*width > (rc.right-rc.left)) width=(rc.right-rc.left + 1)/2;
689         if (2*width > (rc.bottom-rc.top)) width=(rc.bottom-rc.top + 1)/2;
690         rc.left   += width / 2;
691         rc.right  -= (width - 1) / 2;
692         rc.top    += width / 2;
693         rc.bottom -= (width - 1) / 2;
694     }
695     if(width == 0) width = 1; /* more accurate */
696     physDev->pen.width = width;
697
698     /* Update the pixmap from the DIB section */
699     X11DRV_LockDIBSection(physDev, DIB_Status_GdiMod);
700
701     if (X11DRV_SetupGCForBrush( physDev ))
702     {
703         wine_tsx11_lock();
704         XFillArc( gdi_display, physDev->drawable, physDev->gc,
705                   physDev->dc_rect.left + rc.left, physDev->dc_rect.top + rc.top,
706                   rc.right-rc.left-1, rc.bottom-rc.top-1, 0, 360*64 );
707         wine_tsx11_unlock();
708         update = TRUE;
709     }
710     if (X11DRV_SetupGCForPen( physDev ))
711     {
712         wine_tsx11_lock();
713         XDrawArc( gdi_display, physDev->drawable, physDev->gc,
714                   physDev->dc_rect.left + rc.left, physDev->dc_rect.top + rc.top,
715                   rc.right-rc.left-1, rc.bottom-rc.top-1, 0, 360*64 );
716         wine_tsx11_unlock();
717         update = TRUE;
718     }
719
720     /* Update the DIBSection from the pixmap */
721     X11DRV_UnlockDIBSection(physDev, update);
722
723     physDev->pen.width = oldwidth;
724     return TRUE;
725 }
726
727
728 /***********************************************************************
729  *           X11DRV_Rectangle
730  */
731 BOOL
732 X11DRV_Rectangle(X11DRV_PDEVICE *physDev, INT left, INT top, INT right, INT bottom)
733 {
734     INT width, oldwidth, oldjoinstyle;
735     BOOL update = FALSE;
736     RECT rc;
737
738     TRACE("(%d %d %d %d)\n", left, top, right, bottom);
739
740     SetRect(&rc, left, top, right, bottom);
741     LPtoDP(physDev->hdc, (POINT*)&rc, 2);
742
743     if ((rc.left == rc.right) || (rc.top == rc.bottom)) return TRUE;
744
745     if (rc.right < rc.left) { INT tmp = rc.right; rc.right = rc.left; rc.left = tmp; }
746     if (rc.bottom < rc.top) { INT tmp = rc.bottom; rc.bottom = rc.top; rc.top = tmp; }
747
748     oldwidth = width = physDev->pen.width;
749     if (!width) width = 1;
750     if(physDev->pen.style == PS_NULL) width = 0;
751
752     if ((physDev->pen.style == PS_INSIDEFRAME))
753     {
754         if (2*width > (rc.right-rc.left)) width=(rc.right-rc.left + 1)/2;
755         if (2*width > (rc.bottom-rc.top)) width=(rc.bottom-rc.top + 1)/2;
756         rc.left   += width / 2;
757         rc.right  -= (width - 1) / 2;
758         rc.top    += width / 2;
759         rc.bottom -= (width - 1) / 2;
760     }
761     if(width == 1) width = 0;
762     physDev->pen.width = width;
763     oldjoinstyle = physDev->pen.linejoin;
764     if(physDev->pen.type != PS_GEOMETRIC)
765         physDev->pen.linejoin = PS_JOIN_MITER;
766
767     /* Update the pixmap from the DIB section */
768     X11DRV_LockDIBSection(physDev, DIB_Status_GdiMod);
769
770     if ((rc.right > rc.left + width) && (rc.bottom > rc.top + width))
771     {
772         if (X11DRV_SetupGCForBrush( physDev ))
773         {
774             wine_tsx11_lock();
775             XFillRectangle( gdi_display, physDev->drawable, physDev->gc,
776                             physDev->dc_rect.left + rc.left + (width + 1) / 2,
777                             physDev->dc_rect.top + rc.top + (width + 1) / 2,
778                             rc.right-rc.left-width-1, rc.bottom-rc.top-width-1);
779             wine_tsx11_unlock();
780             update = TRUE;
781         }
782     }
783     if (X11DRV_SetupGCForPen( physDev ))
784     {
785         wine_tsx11_lock();
786         XDrawRectangle( gdi_display, physDev->drawable, physDev->gc,
787                         physDev->dc_rect.left + rc.left, physDev->dc_rect.top + rc.top,
788                         rc.right-rc.left-1, rc.bottom-rc.top-1 );
789         wine_tsx11_unlock();
790         update = TRUE;
791     }
792
793     /* Update the DIBSection from the pixmap */
794     X11DRV_UnlockDIBSection(physDev, update);
795
796     physDev->pen.width = oldwidth;
797     physDev->pen.linejoin = oldjoinstyle;
798     return TRUE;
799 }
800
801 /***********************************************************************
802  *           X11DRV_RoundRect
803  */
804 BOOL
805 X11DRV_RoundRect( X11DRV_PDEVICE *physDev, INT left, INT top, INT right,
806                   INT bottom, INT ell_width, INT ell_height )
807 {
808     INT width, oldwidth, oldendcap;
809     BOOL update = FALSE;
810     RECT rc;
811     POINT pts[2];
812
813     TRACE("(%d %d %d %d  %d %d\n",
814         left, top, right, bottom, ell_width, ell_height);
815
816     SetRect(&rc, left, top, right, bottom);
817     LPtoDP(physDev->hdc, (POINT*)&rc, 2);
818
819     if ((rc.left == rc.right) || (rc.top == rc.bottom))
820         return TRUE;
821
822     /* Make sure ell_width and ell_height are >= 1 otherwise XDrawArc gets
823        called with width/height < 0 */
824     pts[0].x = pts[0].y = 0;
825     pts[1].x = ell_width;
826     pts[1].y = ell_height;
827     LPtoDP(physDev->hdc, pts, 2);
828     ell_width  = max(abs( pts[1].x - pts[0].x ), 1);
829     ell_height = max(abs( pts[1].y - pts[0].y ), 1);
830
831     /* Fix the coordinates */
832
833     if (rc.right < rc.left) { INT tmp = rc.right; rc.right = rc.left; rc.left = tmp; }
834     if (rc.bottom < rc.top) { INT tmp = rc.bottom; rc.bottom = rc.top; rc.top = tmp; }
835
836     oldwidth = width = physDev->pen.width;
837     oldendcap = physDev->pen.endcap;
838     if (!width) width = 1;
839     if(physDev->pen.style == PS_NULL) width = 0;
840
841     if ((physDev->pen.style == PS_INSIDEFRAME))
842     {
843         if (2*width > (rc.right-rc.left)) width=(rc.right-rc.left + 1)/2;
844         if (2*width > (rc.bottom-rc.top)) width=(rc.bottom-rc.top + 1)/2;
845         rc.left   += width / 2;
846         rc.right  -= (width - 1) / 2;
847         rc.top    += width / 2;
848         rc.bottom -= (width - 1) / 2;
849     }
850     if(width == 0) width = 1;
851     physDev->pen.width = width;
852     physDev->pen.endcap = PS_ENDCAP_SQUARE;
853
854     /* Update the pixmap from the DIB section */
855     X11DRV_LockDIBSection(physDev, DIB_Status_GdiMod);
856
857     if (X11DRV_SetupGCForBrush( physDev ))
858     {
859         wine_tsx11_lock();
860         if (ell_width > (rc.right-rc.left) )
861             if (ell_height > (rc.bottom-rc.top) )
862                 XFillArc( gdi_display, physDev->drawable, physDev->gc,
863                           physDev->dc_rect.left + rc.left, physDev->dc_rect.top + rc.top,
864                           rc.right - rc.left - 1, rc.bottom - rc.top - 1,
865                           0, 360 * 64 );
866             else{
867                 XFillArc( gdi_display, physDev->drawable, physDev->gc,
868                           physDev->dc_rect.left + rc.left, physDev->dc_rect.top + rc.top,
869                           rc.right - rc.left - 1, ell_height, 0, 180 * 64 );
870                 XFillArc( gdi_display, physDev->drawable, physDev->gc,
871                           physDev->dc_rect.left + rc.left,
872                           physDev->dc_rect.top + rc.bottom - ell_height - 1,
873                           rc.right - rc.left - 1, ell_height, 180 * 64,
874                           180 * 64 );
875             }
876         else if (ell_height > (rc.bottom-rc.top) ){
877             XFillArc( gdi_display, physDev->drawable, physDev->gc,
878                       physDev->dc_rect.left + rc.left, physDev->dc_rect.top + rc.top,
879                       ell_width, rc.bottom - rc.top - 1, 90 * 64, 180 * 64 );
880             XFillArc( gdi_display, physDev->drawable, physDev->gc,
881                       physDev->dc_rect.left + rc.right - ell_width - 1, physDev->dc_rect.top + rc.top,
882                       ell_width, rc.bottom - rc.top - 1, 270 * 64, 180 * 64 );
883         }else{
884             XFillArc( gdi_display, physDev->drawable, physDev->gc,
885                       physDev->dc_rect.left + rc.left, physDev->dc_rect.top + rc.top,
886                       ell_width, ell_height, 90 * 64, 90 * 64 );
887             XFillArc( gdi_display, physDev->drawable, physDev->gc,
888                       physDev->dc_rect.left + rc.left,
889                       physDev->dc_rect.top + rc.bottom - ell_height - 1,
890                       ell_width, ell_height, 180 * 64, 90 * 64 );
891             XFillArc( gdi_display, physDev->drawable, physDev->gc,
892                       physDev->dc_rect.left + rc.right - ell_width - 1,
893                       physDev->dc_rect.top + rc.bottom - ell_height - 1,
894                       ell_width, ell_height, 270 * 64, 90 * 64 );
895             XFillArc( gdi_display, physDev->drawable, physDev->gc,
896                       physDev->dc_rect.left + rc.right - ell_width - 1,
897                       physDev->dc_rect.top + rc.top,
898                       ell_width, ell_height, 0, 90 * 64 );
899         }
900         if (ell_width < rc.right - rc.left)
901         {
902             XFillRectangle( gdi_display, physDev->drawable, physDev->gc,
903                             physDev->dc_rect.left + rc.left + (ell_width + 1) / 2,
904                             physDev->dc_rect.top + rc.top + 1,
905                             rc.right - rc.left - ell_width - 1,
906                             (ell_height + 1) / 2 - 1);
907             XFillRectangle( gdi_display, physDev->drawable, physDev->gc,
908                             physDev->dc_rect.left + rc.left + (ell_width + 1) / 2,
909                             physDev->dc_rect.top + rc.bottom - (ell_height) / 2 - 1,
910                             rc.right - rc.left - ell_width - 1,
911                             (ell_height) / 2 );
912         }
913         if  (ell_height < rc.bottom - rc.top)
914         {
915             XFillRectangle( gdi_display, physDev->drawable, physDev->gc,
916                             physDev->dc_rect.left + rc.left + 1,
917                             physDev->dc_rect.top + rc.top + (ell_height + 1) / 2,
918                             rc.right - rc.left - 2,
919                             rc.bottom - rc.top - ell_height - 1);
920         }
921         wine_tsx11_unlock();
922         update = TRUE;
923     }
924     /* FIXME: this could be done with on X call
925      * more efficient and probably more correct
926      * on any X server: XDrawArcs will draw
927      * straight horizontal and vertical lines
928      * if width or height are zero.
929      *
930      * BTW this stuff is optimized for an Xfree86 server
931      * read the comments inside the X11DRV_DrawArc function
932      */
933     if (X11DRV_SetupGCForPen( physDev ))
934     {
935         wine_tsx11_lock();
936         if (ell_width > (rc.right-rc.left) )
937             if (ell_height > (rc.bottom-rc.top) )
938                 XDrawArc( gdi_display, physDev->drawable, physDev->gc,
939                           physDev->dc_rect.left + rc.left, physDev->dc_rect.top + rc.top,
940                           rc.right - rc.left - 1, rc.bottom - rc.top - 1, 0 , 360 * 64 );
941             else{
942                 XDrawArc( gdi_display, physDev->drawable, physDev->gc,
943                           physDev->dc_rect.left + rc.left, physDev->dc_rect.top + rc.top,
944                           rc.right - rc.left - 1, ell_height - 1, 0 , 180 * 64 );
945                 XDrawArc( gdi_display, physDev->drawable, physDev->gc,
946                           physDev->dc_rect.left + rc.left,
947                           physDev->dc_rect.top + rc.bottom - ell_height,
948                           rc.right - rc.left - 1, ell_height - 1, 180 * 64 , 180 * 64 );
949             }
950         else if (ell_height > (rc.bottom-rc.top) ){
951             XDrawArc( gdi_display, physDev->drawable, physDev->gc,
952                       physDev->dc_rect.left + rc.left, physDev->dc_rect.top + rc.top,
953                       ell_width - 1 , rc.bottom - rc.top - 1, 90 * 64 , 180 * 64 );
954             XDrawArc( gdi_display, physDev->drawable, physDev->gc,
955                       physDev->dc_rect.left + rc.right - ell_width,
956                       physDev->dc_rect.top + rc.top,
957                       ell_width - 1 , rc.bottom - rc.top - 1, 270 * 64 , 180 * 64 );
958         }else{
959             XDrawArc( gdi_display, physDev->drawable, physDev->gc,
960                       physDev->dc_rect.left + rc.left, physDev->dc_rect.top + rc.top,
961                       ell_width - 1, ell_height - 1, 90 * 64, 90 * 64 );
962             XDrawArc( gdi_display, physDev->drawable, physDev->gc,
963                       physDev->dc_rect.left + rc.left, physDev->dc_rect.top + rc.bottom - ell_height,
964                       ell_width - 1, ell_height - 1, 180 * 64, 90 * 64 );
965             XDrawArc( gdi_display, physDev->drawable, physDev->gc,
966                       physDev->dc_rect.left + rc.right - ell_width,
967                       physDev->dc_rect.top + rc.bottom - ell_height,
968                       ell_width - 1, ell_height - 1, 270 * 64, 90 * 64 );
969             XDrawArc( gdi_display, physDev->drawable, physDev->gc,
970                       physDev->dc_rect.left + rc.right - ell_width, physDev->dc_rect.top + rc.top,
971                       ell_width - 1, ell_height - 1, 0, 90 * 64 );
972         }
973         if (ell_width < rc.right - rc.left)
974         {
975             XDrawLine( gdi_display, physDev->drawable, physDev->gc,
976                        physDev->dc_rect.left + rc.left + ell_width / 2,
977                        physDev->dc_rect.top + rc.top,
978                        physDev->dc_rect.left + rc.right - (ell_width+1) / 2,
979                        physDev->dc_rect.top + rc.top);
980             XDrawLine( gdi_display, physDev->drawable, physDev->gc,
981                        physDev->dc_rect.left + rc.left + ell_width / 2 ,
982                        physDev->dc_rect.top + rc.bottom - 1,
983                        physDev->dc_rect.left + rc.right - (ell_width+1)/ 2,
984                        physDev->dc_rect.top + rc.bottom - 1);
985         }
986         if (ell_height < rc.bottom - rc.top)
987         {
988             XDrawLine( gdi_display, physDev->drawable, physDev->gc,
989                        physDev->dc_rect.left + rc.right - 1,
990                        physDev->dc_rect.top + rc.top + ell_height / 2,
991                        physDev->dc_rect.left + rc.right - 1,
992                        physDev->dc_rect.top + rc.bottom - (ell_height+1) / 2);
993             XDrawLine( gdi_display, physDev->drawable, physDev->gc,
994                        physDev->dc_rect.left + rc.left,
995                        physDev->dc_rect.top + rc.top + ell_height / 2,
996                        physDev->dc_rect.left + rc.left,
997                        physDev->dc_rect.top + rc.bottom - (ell_height+1) / 2);
998         }
999         wine_tsx11_unlock();
1000         update = TRUE;
1001     }
1002     /* Update the DIBSection from the pixmap */
1003     X11DRV_UnlockDIBSection(physDev, update);
1004
1005     physDev->pen.width = oldwidth;
1006     physDev->pen.endcap = oldendcap;
1007     return TRUE;
1008 }
1009
1010
1011 /***********************************************************************
1012  *           X11DRV_SetPixel
1013  */
1014 COLORREF
1015 X11DRV_SetPixel( X11DRV_PDEVICE *physDev, INT x, INT y, COLORREF color )
1016 {
1017     unsigned long pixel;
1018     POINT pt;
1019
1020     pt.x = x;
1021     pt.y = y;
1022     LPtoDP( physDev->hdc, &pt, 1 );
1023     pixel = X11DRV_PALETTE_ToPhysical( physDev, color );
1024
1025     /* Update the pixmap from the DIB section */
1026     X11DRV_LockDIBSection(physDev, DIB_Status_GdiMod);
1027
1028     /* inefficient but simple... */
1029     wine_tsx11_lock();
1030     XSetForeground( gdi_display, physDev->gc, pixel );
1031     XSetFunction( gdi_display, physDev->gc, GXcopy );
1032     XDrawPoint( gdi_display, physDev->drawable, physDev->gc,
1033                 physDev->dc_rect.left + pt.x, physDev->dc_rect.top + pt.y );
1034     wine_tsx11_unlock();
1035
1036     /* Update the DIBSection from the pixmap */
1037     X11DRV_UnlockDIBSection(physDev, TRUE);
1038
1039     return X11DRV_PALETTE_ToLogical(pixel);
1040 }
1041
1042
1043 /***********************************************************************
1044  *           X11DRV_GetPixel
1045  */
1046 COLORREF
1047 X11DRV_GetPixel( X11DRV_PDEVICE *physDev, INT x, INT y )
1048 {
1049     static Pixmap pixmap = 0;
1050     XImage * image;
1051     int pixel;
1052     POINT pt;
1053     BOOL memdc = (GetObjectType(physDev->hdc) == OBJ_MEMDC);
1054
1055     pt.x = x;
1056     pt.y = y;
1057     LPtoDP( physDev->hdc, &pt, 1 );
1058
1059     /* Update the pixmap from the DIB section */
1060     X11DRV_LockDIBSection(physDev, DIB_Status_GdiMod);
1061
1062     wine_tsx11_lock();
1063     if (memdc)
1064     {
1065         image = XGetImage( gdi_display, physDev->drawable,
1066                            physDev->dc_rect.left + pt.x, physDev->dc_rect.top + pt.y,
1067                            1, 1, AllPlanes, ZPixmap );
1068     }
1069     else
1070     {
1071         /* If we are reading from the screen, use a temporary copy */
1072         /* to avoid a BadMatch error */
1073         if (!pixmap) pixmap = XCreatePixmap( gdi_display, root_window,
1074                                              1, 1, physDev->depth );
1075         XCopyArea( gdi_display, physDev->drawable, pixmap, BITMAP_colorGC,
1076                    physDev->dc_rect.left + pt.x, physDev->dc_rect.top + pt.y, 1, 1, 0, 0 );
1077         image = XGetImage( gdi_display, pixmap, 0, 0, 1, 1, AllPlanes, ZPixmap );
1078     }
1079     pixel = XGetPixel( image, 0, 0 );
1080     XDestroyImage( image );
1081     wine_tsx11_unlock();
1082
1083     /* Update the DIBSection from the pixmap */
1084     X11DRV_UnlockDIBSection(physDev, FALSE);
1085
1086     return X11DRV_PALETTE_ToLogical(pixel);
1087 }
1088
1089
1090 /***********************************************************************
1091  *           X11DRV_PaintRgn
1092  */
1093 BOOL
1094 X11DRV_PaintRgn( X11DRV_PDEVICE *physDev, HRGN hrgn )
1095 {
1096     if (X11DRV_SetupGCForBrush( physDev ))
1097     {
1098         unsigned int i;
1099         XRectangle *rect;
1100         RGNDATA *data = X11DRV_GetRegionData( hrgn, physDev->hdc );
1101
1102         if (!data) return FALSE;
1103         rect = (XRectangle *)data->Buffer;
1104         for (i = 0; i < data->rdh.nCount; i++)
1105         {
1106             rect[i].x += physDev->dc_rect.left;
1107             rect[i].y += physDev->dc_rect.top;
1108         }
1109
1110         X11DRV_LockDIBSection(physDev, DIB_Status_GdiMod);
1111         wine_tsx11_lock();
1112         XFillRectangles( gdi_display, physDev->drawable, physDev->gc, rect, data->rdh.nCount );
1113         wine_tsx11_unlock();
1114         X11DRV_UnlockDIBSection(physDev, TRUE);
1115         HeapFree( GetProcessHeap(), 0, data );
1116     }
1117     return TRUE;
1118 }
1119
1120 /**********************************************************************
1121  *          X11DRV_Polyline
1122  */
1123 BOOL
1124 X11DRV_Polyline( X11DRV_PDEVICE *physDev, const POINT* pt, INT count )
1125 {
1126     int i;
1127     XPoint *points;
1128
1129     if (!(points = HeapAlloc( GetProcessHeap(), 0, sizeof(XPoint) * count )))
1130     {
1131         WARN("No memory to convert POINTs to XPoints!\n");
1132         return FALSE;
1133     }
1134     for (i = 0; i < count; i++)
1135     {
1136         POINT tmp = pt[i];
1137         LPtoDP(physDev->hdc, &tmp, 1);
1138         points[i].x = physDev->dc_rect.left + tmp.x;
1139         points[i].y = physDev->dc_rect.top + tmp.y;
1140     }
1141
1142     if (X11DRV_SetupGCForPen ( physDev ))
1143     {
1144         X11DRV_LockDIBSection(physDev, DIB_Status_GdiMod);
1145         wine_tsx11_lock();
1146         XDrawLines( gdi_display, physDev->drawable, physDev->gc,
1147                     points, count, CoordModeOrigin );
1148         wine_tsx11_unlock();
1149         X11DRV_UnlockDIBSection(physDev, TRUE);
1150     }
1151
1152     HeapFree( GetProcessHeap(), 0, points );
1153     return TRUE;
1154 }
1155
1156
1157 /**********************************************************************
1158  *          X11DRV_Polygon
1159  */
1160 BOOL
1161 X11DRV_Polygon( X11DRV_PDEVICE *physDev, const POINT* pt, INT count )
1162 {
1163     register int i;
1164     XPoint *points;
1165     BOOL update = FALSE;
1166
1167     if (!(points = HeapAlloc( GetProcessHeap(), 0, sizeof(XPoint) * (count+1) )))
1168     {
1169         WARN("No memory to convert POINTs to XPoints!\n");
1170         return FALSE;
1171     }
1172     for (i = 0; i < count; i++)
1173     {
1174         POINT tmp = pt[i];
1175         LPtoDP(physDev->hdc, &tmp, 1);
1176         points[i].x = physDev->dc_rect.left + tmp.x;
1177         points[i].y = physDev->dc_rect.top + tmp.y;
1178     }
1179     points[count] = points[0];
1180
1181     /* Update the pixmap from the DIB section */
1182     X11DRV_LockDIBSection(physDev, DIB_Status_GdiMod);
1183
1184     if (X11DRV_SetupGCForBrush( physDev ))
1185     {
1186         wine_tsx11_lock();
1187         XFillPolygon( gdi_display, physDev->drawable, physDev->gc,
1188                       points, count+1, Complex, CoordModeOrigin);
1189         wine_tsx11_unlock();
1190         update = TRUE;
1191     }
1192     if (X11DRV_SetupGCForPen ( physDev ))
1193     {
1194         wine_tsx11_lock();
1195         XDrawLines( gdi_display, physDev->drawable, physDev->gc,
1196                     points, count+1, CoordModeOrigin );
1197         wine_tsx11_unlock();
1198         update = TRUE;
1199     }
1200
1201     /* Update the DIBSection from the pixmap */
1202     X11DRV_UnlockDIBSection(physDev, update);
1203
1204     HeapFree( GetProcessHeap(), 0, points );
1205     return TRUE;
1206 }
1207
1208
1209 /**********************************************************************
1210  *          X11DRV_PolyPolygon
1211  */
1212 BOOL
1213 X11DRV_PolyPolygon( X11DRV_PDEVICE *physDev, const POINT* pt, const INT* counts, UINT polygons)
1214 {
1215     HRGN hrgn;
1216
1217     /* FIXME: The points should be converted to device coords before */
1218     /* creating the region. */
1219
1220     hrgn = CreatePolyPolygonRgn( pt, counts, polygons, GetPolyFillMode( physDev->hdc ) );
1221     X11DRV_PaintRgn( physDev, hrgn );
1222     DeleteObject( hrgn );
1223
1224       /* Draw the outline of the polygons */
1225
1226     if (X11DRV_SetupGCForPen ( physDev ))
1227     {
1228         unsigned int i;
1229         int j, max = 0;
1230         XPoint *points;
1231
1232         /* Update the pixmap from the DIB section */
1233         X11DRV_LockDIBSection(physDev, DIB_Status_GdiMod);
1234
1235         for (i = 0; i < polygons; i++) if (counts[i] > max) max = counts[i];
1236         if (!(points = HeapAlloc( GetProcessHeap(), 0, sizeof(XPoint) * (max+1) )))
1237         {
1238             WARN("No memory to convert POINTs to XPoints!\n");
1239             return FALSE;
1240         }
1241         for (i = 0; i < polygons; i++)
1242         {
1243             for (j = 0; j < counts[i]; j++)
1244             {
1245                 POINT tmp = *pt;
1246                 LPtoDP(physDev->hdc, &tmp, 1);
1247                 points[j].x = physDev->dc_rect.left + tmp.x;
1248                 points[j].y = physDev->dc_rect.top + tmp.y;
1249                 pt++;
1250             }
1251             points[j] = points[0];
1252             wine_tsx11_lock();
1253             XDrawLines( gdi_display, physDev->drawable, physDev->gc,
1254                         points, j + 1, CoordModeOrigin );
1255             wine_tsx11_unlock();
1256         }
1257
1258         /* Update the DIBSection of the dc's bitmap */
1259         X11DRV_UnlockDIBSection(physDev, TRUE);
1260
1261         HeapFree( GetProcessHeap(), 0, points );
1262     }
1263     return TRUE;
1264 }
1265
1266
1267 /**********************************************************************
1268  *          X11DRV_PolyPolyline
1269  */
1270 BOOL
1271 X11DRV_PolyPolyline( X11DRV_PDEVICE *physDev, const POINT* pt, const DWORD* counts, DWORD polylines )
1272 {
1273     if (X11DRV_SetupGCForPen ( physDev ))
1274     {
1275         unsigned int i, j, max = 0;
1276         XPoint *points;
1277
1278         /* Update the pixmap from the DIB section */
1279         X11DRV_LockDIBSection(physDev, DIB_Status_GdiMod);
1280
1281         for (i = 0; i < polylines; i++) if (counts[i] > max) max = counts[i];
1282         if (!(points = HeapAlloc( GetProcessHeap(), 0, sizeof(XPoint) * max )))
1283         {
1284             WARN("No memory to convert POINTs to XPoints!\n");
1285             return FALSE;
1286         }
1287         for (i = 0; i < polylines; i++)
1288         {
1289             for (j = 0; j < counts[i]; j++)
1290             {
1291                 POINT tmp = *pt;
1292                 LPtoDP(physDev->hdc, &tmp, 1);
1293                 points[j].x = physDev->dc_rect.left + tmp.x;
1294                 points[j].y = physDev->dc_rect.top + tmp.y;
1295                 pt++;
1296             }
1297             wine_tsx11_lock();
1298             XDrawLines( gdi_display, physDev->drawable, physDev->gc,
1299                         points, j, CoordModeOrigin );
1300             wine_tsx11_unlock();
1301         }
1302
1303         /* Update the DIBSection of the dc's bitmap */
1304         X11DRV_UnlockDIBSection(physDev, TRUE);
1305
1306         HeapFree( GetProcessHeap(), 0, points );
1307     }
1308     return TRUE;
1309 }
1310
1311
1312 /**********************************************************************
1313  *          X11DRV_InternalFloodFill
1314  *
1315  * Internal helper function for flood fill.
1316  * (xorg,yorg) is the origin of the X image relative to the drawable.
1317  * (x,y) is relative to the origin of the X image.
1318  */
1319 static void X11DRV_InternalFloodFill(XImage *image, X11DRV_PDEVICE *physDev,
1320                                      int x, int y,
1321                                      int xOrg, int yOrg,
1322                                      unsigned long pixel, WORD fillType )
1323 {
1324     int left, right;
1325
1326 #define TO_FLOOD(x,y)  ((fillType == FLOODFILLBORDER) ? \
1327                         (XGetPixel(image,x,y) != pixel) : \
1328                         (XGetPixel(image,x,y) == pixel))
1329
1330     if (!TO_FLOOD(x,y)) return;
1331
1332       /* Find left and right boundaries */
1333
1334     left = right = x;
1335     while ((left > 0) && TO_FLOOD( left-1, y )) left--;
1336     while ((right < image->width) && TO_FLOOD( right, y )) right++;
1337     XFillRectangle( gdi_display, physDev->drawable, physDev->gc,
1338                     xOrg + left, yOrg + y, right-left, 1 );
1339
1340       /* Set the pixels of this line so we don't fill it again */
1341
1342     for (x = left; x < right; x++)
1343     {
1344         if (fillType == FLOODFILLBORDER) XPutPixel( image, x, y, pixel );
1345         else XPutPixel( image, x, y, ~pixel );
1346     }
1347
1348       /* Fill the line above */
1349
1350     if (--y >= 0)
1351     {
1352         x = left;
1353         while (x < right)
1354         {
1355             while ((x < right) && !TO_FLOOD(x,y)) x++;
1356             if (x >= right) break;
1357             while ((x < right) && TO_FLOOD(x,y)) x++;
1358             X11DRV_InternalFloodFill(image, physDev, x-1, y,
1359                                      xOrg, yOrg, pixel, fillType );
1360         }
1361     }
1362
1363       /* Fill the line below */
1364
1365     if ((y += 2) < image->height)
1366     {
1367         x = left;
1368         while (x < right)
1369         {
1370             while ((x < right) && !TO_FLOOD(x,y)) x++;
1371             if (x >= right) break;
1372             while ((x < right) && TO_FLOOD(x,y)) x++;
1373             X11DRV_InternalFloodFill(image, physDev, x-1, y,
1374                                      xOrg, yOrg, pixel, fillType );
1375         }
1376     }
1377 #undef TO_FLOOD
1378 }
1379
1380
1381 static int ExtFloodFillXGetImageErrorHandler( Display *dpy, XErrorEvent *event, void *arg )
1382 {
1383     return (event->request_code == X_GetImage && event->error_code == BadMatch);
1384 }
1385
1386 /**********************************************************************
1387  *          X11DRV_ExtFloodFill
1388  */
1389 BOOL
1390 X11DRV_ExtFloodFill( X11DRV_PDEVICE *physDev, INT x, INT y, COLORREF color,
1391                      UINT fillType )
1392 {
1393     XImage *image;
1394     RECT rect;
1395     POINT pt;
1396
1397     TRACE("X11DRV_ExtFloodFill %d,%d %06x %d\n", x, y, color, fillType );
1398
1399     pt.x = x;
1400     pt.y = y;
1401     LPtoDP( physDev->hdc, &pt, 1 );
1402     if (!PtInRegion( physDev->region, pt.x, pt.y )) return FALSE;
1403     GetRgnBox( physDev->region, &rect );
1404
1405     X11DRV_expect_error( gdi_display, ExtFloodFillXGetImageErrorHandler, NULL );
1406     image = XGetImage( gdi_display, physDev->drawable,
1407                        physDev->dc_rect.left + rect.left, physDev->dc_rect.top + rect.top,
1408                        rect.right - rect.left, rect.bottom - rect.top,
1409                        AllPlanes, ZPixmap );
1410     if(X11DRV_check_error()) image = NULL;
1411     if (!image) return FALSE;
1412
1413     if (X11DRV_SetupGCForBrush( physDev ))
1414     {
1415         unsigned long pixel = X11DRV_PALETTE_ToPhysical( physDev, color );
1416
1417         /* Update the pixmap from the DIB section */
1418         X11DRV_LockDIBSection(physDev, DIB_Status_GdiMod);
1419
1420           /* ROP mode is always GXcopy for flood-fill */
1421         wine_tsx11_lock();
1422         XSetFunction( gdi_display, physDev->gc, GXcopy );
1423         X11DRV_InternalFloodFill(image, physDev,
1424                                  pt.x - rect.left,
1425                                  pt.y - rect.top,
1426                                  physDev->dc_rect.left + rect.left,
1427                                  physDev->dc_rect.top + rect.top,
1428                                  pixel, fillType );
1429         wine_tsx11_unlock();
1430         /* Update the DIBSection of the dc's bitmap */
1431         X11DRV_UnlockDIBSection(physDev, TRUE);
1432     }
1433
1434     wine_tsx11_lock();
1435     XDestroyImage( image );
1436     wine_tsx11_unlock();
1437     return TRUE;
1438 }
1439
1440 /**********************************************************************
1441  *          X11DRV_SetBkColor
1442  */
1443 COLORREF
1444 X11DRV_SetBkColor( X11DRV_PDEVICE *physDev, COLORREF color )
1445 {
1446     physDev->backgroundPixel = X11DRV_PALETTE_ToPhysical( physDev, color );
1447     return color;
1448 }
1449
1450 /**********************************************************************
1451  *          X11DRV_SetTextColor
1452  */
1453 COLORREF
1454 X11DRV_SetTextColor( X11DRV_PDEVICE *physDev, COLORREF color )
1455 {
1456     physDev->textPixel = X11DRV_PALETTE_ToPhysical( physDev, color );
1457     return color;
1458 }
1459
1460 /***********************************************************************
1461  *           GetDCOrgEx   (X11DRV.@)
1462  */
1463 BOOL X11DRV_GetDCOrgEx( X11DRV_PDEVICE *physDev, LPPOINT lpp )
1464 {
1465     lpp->x = physDev->dc_rect.left + physDev->drawable_rect.left;
1466     lpp->y = physDev->dc_rect.top + physDev->drawable_rect.top;
1467     return TRUE;
1468 }
1469
1470
1471 /***********************************************************************
1472  *           SetDCOrg   (X11DRV.@)
1473  */
1474 DWORD X11DRV_SetDCOrg( X11DRV_PDEVICE *physDev, INT x, INT y )
1475 {
1476     DWORD ret = MAKELONG( physDev->dc_rect.left + physDev->drawable_rect.left,
1477                           physDev->dc_rect.top + physDev->drawable_rect.top );
1478     physDev->dc_rect.left = x - physDev->drawable_rect.left;
1479     physDev->dc_rect.top = y - physDev->drawable_rect.top;
1480     return ret;
1481 }
1482
1483 static unsigned char *get_icm_profile( unsigned long *size )
1484 {
1485     Atom type;
1486     int format;
1487     unsigned long count, remaining;
1488     unsigned char *profile, *ret = NULL;
1489
1490     wine_tsx11_lock();
1491     XGetWindowProperty( gdi_display, DefaultRootWindow(gdi_display),
1492                         x11drv_atom(_ICC_PROFILE), 0, ~0UL, False, AnyPropertyType,
1493                         &type, &format, &count, &remaining, &profile );
1494     *size = get_property_size( format, count );
1495     if (format && count)
1496     {
1497         if ((ret = HeapAlloc( GetProcessHeap(), 0, *size ))) memcpy( ret, profile, *size );
1498         XFree( profile );
1499     }
1500     wine_tsx11_unlock();
1501     return ret;
1502 }
1503
1504 typedef struct
1505 {
1506     unsigned int unknown[6];
1507     unsigned int state[5];
1508     unsigned int count[2];
1509     unsigned char buffer[64];
1510 } sha_ctx;
1511
1512 extern void WINAPI A_SHAInit( sha_ctx * );
1513 extern void WINAPI A_SHAUpdate( sha_ctx *, const unsigned char *, unsigned int );
1514 extern void WINAPI A_SHAFinal( sha_ctx *, unsigned char * );
1515
1516 /***********************************************************************
1517  *              GetICMProfile (X11DRV.@)
1518  */
1519 BOOL X11DRV_GetICMProfile( X11DRV_PDEVICE *physDev, LPDWORD size, LPWSTR filename )
1520 {
1521     static const WCHAR path[] =
1522         {'\\','s','p','o','o','l','\\','d','r','i','v','e','r','s',
1523          '\\','c','o','l','o','r','\\',0};
1524     static const WCHAR srgb[] =
1525         {'s','R','G','B',' ','C','o','l','o','r',' ','S','p','a','c','e',' ',
1526          'P','r','o','f','i','l','e','.','i','c','m',0};
1527     static const WCHAR mntr[] =
1528         {'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
1529          'W','i','n','d','o','w','s',' ','N','T','\\','C','u','r','r','e','n','t',
1530          'V','e','r','s','i','o','n','\\','I','C','M','\\','m','n','t','r',0};
1531
1532     HKEY hkey;
1533     DWORD required, len;
1534     WCHAR profile[MAX_PATH], fullname[2*MAX_PATH + sizeof(path)/sizeof(WCHAR)];
1535     unsigned char *buffer;
1536     unsigned long buflen;
1537
1538     if (!size) return FALSE;
1539
1540     GetSystemDirectoryW( fullname, MAX_PATH );
1541     strcatW( fullname, path );
1542
1543     len = sizeof(profile)/sizeof(WCHAR);
1544     if (!RegCreateKeyExW( HKEY_LOCAL_MACHINE, mntr, 0, NULL, 0, KEY_ALL_ACCESS, NULL, &hkey, NULL ) &&
1545         !RegEnumValueW( hkey, 0, profile, &len, NULL, NULL, NULL, NULL )) /* FIXME handle multiple values */
1546     {
1547         strcatW( fullname, profile );
1548         RegCloseKey( hkey );
1549     }
1550     else if ((buffer = get_icm_profile( &buflen )))
1551     {
1552         static const WCHAR fmt[] = {'%','0','2','x',0};
1553         static const WCHAR icm[] = {'.','i','c','m',0};
1554
1555         unsigned char sha1sum[20];
1556         unsigned int i;
1557         sha_ctx ctx;
1558         HANDLE file;
1559
1560         A_SHAInit( &ctx );
1561         A_SHAUpdate( &ctx, buffer, buflen );
1562         A_SHAFinal( &ctx, sha1sum );
1563
1564         for (i = 0; i < sizeof(sha1sum); i++) sprintfW( &profile[i * 2], fmt, sha1sum[i] );
1565         memcpy( &profile[i * 2], icm, sizeof(icm) );
1566
1567         strcatW( fullname, profile );
1568         file = CreateFileW( fullname, GENERIC_WRITE, 0, NULL, CREATE_NEW, 0, 0 );
1569         if (file != INVALID_HANDLE_VALUE)
1570         {
1571             DWORD written;
1572
1573             if (!WriteFile( file, buffer, buflen, &written, NULL ) || written != buflen)
1574                 ERR( "Unable to write color profile\n" );
1575             CloseHandle( file );
1576         }
1577         HeapFree( GetProcessHeap(), 0, buffer );
1578     }
1579     else strcatW( fullname, srgb );
1580
1581     required = strlenW( fullname ) + 1;
1582     if (*size < required)
1583     {
1584         *size = required;
1585         SetLastError( ERROR_INSUFFICIENT_BUFFER );
1586         return FALSE;
1587     }
1588     if (filename)
1589     {
1590         strcpyW( filename, fullname );
1591         if (GetFileAttributesW( filename ) == INVALID_FILE_ATTRIBUTES)
1592             WARN( "color profile not found\n" );
1593     }
1594     *size = required;
1595     return TRUE;
1596 }