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