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