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