winex11.drv: Produce VK_CANCEL vkey on the Ctrl+Pause/Break pressing.
[wine] / dlls / winex11.drv / bitmap.c
1 /*
2  * X11DRV bitmap objects
3  *
4  * Copyright 1993 Alexandre Julliard
5  *           1999 Noel Borthwick
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 #include "config.h"
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include "windef.h"
27 #include "wingdi.h"
28 #include "wine/debug.h"
29 #include "x11drv.h"
30
31 WINE_DEFAULT_DEBUG_CHANNEL(x11drv);
32
33   /* GCs used for B&W and color bitmap operations */
34 static GC bitmap_gc[32];
35 X_PHYSBITMAP BITMAP_stock_phys_bitmap = { 0 };  /* phys bitmap for the default stock bitmap */
36
37 static XContext bitmap_context;  /* X context to associate a phys bitmap to a handle */
38
39 GC get_bitmap_gc(int depth)
40 {
41     if(depth < 1 || depth > 32)
42         return 0;
43
44     return bitmap_gc[depth-1];
45 }
46
47 /***********************************************************************
48  *           X11DRV_BITMAP_Init
49  */
50 void X11DRV_BITMAP_Init(void)
51 {
52     int depth_count, index, i;
53     int *depth_list;
54     Pixmap tmpPixmap;
55
56     wine_tsx11_lock();
57     bitmap_context = XUniqueContext();
58     BITMAP_stock_phys_bitmap.pixmap_depth = 1;
59     BITMAP_stock_phys_bitmap.pixmap = XCreatePixmap( gdi_display, root_window, 1, 1, 1 );
60     bitmap_gc[0] = XCreateGC( gdi_display, BITMAP_stock_phys_bitmap.pixmap, 0, NULL );
61     XSetGraphicsExposures( gdi_display, bitmap_gc[0], False );
62     XSetSubwindowMode( gdi_display, bitmap_gc[0], IncludeInferiors );
63
64     /* Create a GC for all available depths. GCs at depths other than 1-bit/screen_depth are for use
65      * in combination with XRender which allows us to create dibsections at more depths.
66      */
67     depth_list = XListDepths(gdi_display, DefaultScreen(gdi_display), &depth_count);
68     for (i = 0; i < depth_count; i++)
69     {
70         index = depth_list[i] - 1;
71         if (bitmap_gc[index]) continue;
72         if ((tmpPixmap = XCreatePixmap( gdi_display, root_window, 1, 1, depth_list[i])))
73         {
74             if ((bitmap_gc[index] = XCreateGC( gdi_display, tmpPixmap, 0, NULL )))
75             {
76                 XSetGraphicsExposures( gdi_display, bitmap_gc[index], False );
77                 XSetSubwindowMode( gdi_display, bitmap_gc[index], IncludeInferiors );
78             }
79             XFreePixmap( gdi_display, tmpPixmap );
80         }
81     }
82     XFree( depth_list );
83
84     wine_tsx11_unlock();
85 }
86
87 /***********************************************************************
88  *           SelectBitmap   (X11DRV.@)
89  */
90 HBITMAP CDECL X11DRV_SelectBitmap( X11DRV_PDEVICE *physDev, HBITMAP hbitmap )
91 {
92     X_PHYSBITMAP *physBitmap;
93     BITMAP bitmap;
94
95     if (!GetObjectW( hbitmap, sizeof(bitmap), &bitmap )) return 0;
96
97     if(physDev->xrender)
98         X11DRV_XRender_UpdateDrawable( physDev );
99
100     if (hbitmap == BITMAP_stock_phys_bitmap.hbitmap) physBitmap = &BITMAP_stock_phys_bitmap;
101     else if (!(physBitmap = X11DRV_get_phys_bitmap( hbitmap ))) return 0;
102
103     physDev->bitmap = physBitmap;
104     physDev->drawable = physBitmap->pixmap;
105     physDev->color_shifts = physBitmap->trueColor ? &physBitmap->pixmap_color_shifts : NULL;
106     SetRect( &physDev->drawable_rect, 0, 0, bitmap.bmWidth, bitmap.bmHeight );
107     physDev->dc_rect = physDev->drawable_rect;
108
109       /* Change GC depth if needed */
110
111     if (physDev->depth != physBitmap->pixmap_depth)
112     {
113         physDev->depth = physBitmap->pixmap_depth;
114         wine_tsx11_lock();
115         XFreeGC( gdi_display, physDev->gc );
116         physDev->gc = XCreateGC( gdi_display, physDev->drawable, 0, NULL );
117         XSetGraphicsExposures( gdi_display, physDev->gc, False );
118         XSetSubwindowMode( gdi_display, physDev->gc, IncludeInferiors );
119         XFlush( gdi_display );
120         wine_tsx11_unlock();
121     }
122
123     return hbitmap;
124 }
125
126
127 /****************************************************************************
128  *        CreateBitmap   (X11DRV.@)
129  *
130  * Create a device dependent X11 bitmap
131  *
132  * Returns TRUE on success else FALSE
133  */
134 BOOL CDECL X11DRV_CreateBitmap( X11DRV_PDEVICE *physDev, HBITMAP hbitmap, LPVOID bmBits )
135 {
136     X_PHYSBITMAP *physBitmap;
137     BITMAP bitmap;
138
139     if (!GetObjectW( hbitmap, sizeof(bitmap), &bitmap )) return FALSE;
140
141       /* Check parameters */
142     if (bitmap.bmPlanes != 1) return FALSE;
143
144     /* check if bpp is compatible with screen depth */
145     if (!((bitmap.bmBitsPixel == 1) || (bitmap.bmBitsPixel == screen_bpp)))
146     {
147         WARN("Trying to make bitmap with planes=%d, bpp=%d\n",
148             bitmap.bmPlanes, bitmap.bmBitsPixel);
149         return FALSE;
150     }
151     if (hbitmap == BITMAP_stock_phys_bitmap.hbitmap)
152     {
153         ERR( "called for stock bitmap, please report\n" );
154         return FALSE;
155     }
156
157     TRACE("(%p) %dx%d %d bpp\n", hbitmap, bitmap.bmWidth, bitmap.bmHeight, bitmap.bmBitsPixel);
158
159     if (!(physBitmap = X11DRV_init_phys_bitmap( hbitmap ))) return FALSE;
160
161       /* Create the pixmap */
162     wine_tsx11_lock();
163     if(bitmap.bmBitsPixel == 1)
164     {
165         physBitmap->pixmap_depth = 1;
166         physBitmap->trueColor = FALSE;
167     }
168     else
169     {
170         physBitmap->pixmap_depth = screen_depth;
171         physBitmap->pixmap_color_shifts = X11DRV_PALETTE_default_shifts;
172         physBitmap->trueColor = (visual->class == TrueColor || visual->class == DirectColor);
173     }
174     physBitmap->pixmap = XCreatePixmap(gdi_display, root_window,
175                                        bitmap.bmWidth, bitmap.bmHeight, physBitmap->pixmap_depth);
176     wine_tsx11_unlock();
177     if (!physBitmap->pixmap)
178     {
179         WARN("Can't create Pixmap\n");
180         HeapFree( GetProcessHeap(), 0, physBitmap );
181         return FALSE;
182     }
183
184     if (bmBits) /* Set bitmap bits */
185     {
186         X11DRV_SetBitmapBits( hbitmap, bmBits, bitmap.bmHeight * bitmap.bmWidthBytes );
187     }
188     else  /* else clear the bitmap */
189     {
190         GC gc = get_bitmap_gc(physBitmap->pixmap_depth);
191         wine_tsx11_lock();
192         XSetFunction( gdi_display, gc, GXclear );
193         XFillRectangle( gdi_display, physBitmap->pixmap, gc, 0, 0,
194                         bitmap.bmWidth, bitmap.bmHeight );
195         XSetFunction( gdi_display, gc, GXcopy );
196         wine_tsx11_unlock();
197     }
198     return TRUE;
199 }
200
201
202 /***********************************************************************
203  *           GetBitmapBits   (X11DRV.@)
204  *
205  * RETURNS
206  *    Success: Number of bytes copied
207  *    Failure: 0
208  */
209 LONG CDECL X11DRV_GetBitmapBits( HBITMAP hbitmap, void *buffer, LONG count )
210 {
211     BITMAP bitmap;
212     X_PHYSBITMAP *physBitmap = X11DRV_get_phys_bitmap( hbitmap );
213     LONG height;
214     XImage *image;
215     LPBYTE tbuf, startline;
216     int h, w;
217
218     if (!physBitmap || !GetObjectW( hbitmap, sizeof(bitmap), &bitmap )) return 0;
219
220     TRACE("(bmp=%p, buffer=%p, count=0x%x)\n", hbitmap, buffer, count);
221
222     wine_tsx11_lock();
223     height = count / bitmap.bmWidthBytes;
224     image = XGetImage( gdi_display, physBitmap->pixmap, 0, 0,
225                        bitmap.bmWidth, height, AllPlanes, ZPixmap );
226
227     /* copy XImage to 16 bit padded image buffer with real bitsperpixel */
228
229     startline = buffer;
230     switch (bitmap.bmBitsPixel)
231     {
232     case 1:
233         for (h=0;h<height;h++)
234         {
235             tbuf = startline;
236             *tbuf = 0;
237             for (w=0;w<bitmap.bmWidth;w++)
238             {
239                 if ((w%8) == 0)
240                     *tbuf = 0;
241                 *tbuf |= XGetPixel(image,w,h)<<(7-(w&7));
242                 if ((w&7) == 7) ++tbuf;
243             }
244             startline += bitmap.bmWidthBytes;
245         }
246         break;
247     case 4:
248         for (h=0;h<height;h++)
249         {
250             tbuf = startline;
251             for (w=0;w<bitmap.bmWidth;w++)
252             {
253                 if (!(w & 1)) *tbuf = XGetPixel( image, w, h) << 4;
254                 else *tbuf++ |= XGetPixel( image, w, h) & 0x0f;
255             }
256             startline += bitmap.bmWidthBytes;
257         }
258         break;
259     case 8:
260         for (h=0;h<height;h++)
261         {
262             tbuf = startline;
263             for (w=0;w<bitmap.bmWidth;w++)
264                 *tbuf++ = XGetPixel(image,w,h);
265             startline += bitmap.bmWidthBytes;
266         }
267         break;
268     case 15:
269     case 16:
270         for (h=0;h<height;h++)
271         {
272             tbuf = startline;
273             for (w=0;w<bitmap.bmWidth;w++)
274             {
275                 long pixel = XGetPixel(image,w,h);
276
277                 *tbuf++ = pixel & 0xff;
278                 *tbuf++ = (pixel>>8) & 0xff;
279             }
280             startline += bitmap.bmWidthBytes;
281         }
282         break;
283     case 24:
284         for (h=0;h<height;h++)
285         {
286             tbuf = startline;
287             for (w=0;w<bitmap.bmWidth;w++)
288             {
289                 long pixel = XGetPixel(image,w,h);
290
291                 *tbuf++ = pixel & 0xff;
292                 *tbuf++ = (pixel>> 8) & 0xff;
293                 *tbuf++ = (pixel>>16) & 0xff;
294             }
295             startline += bitmap.bmWidthBytes;
296         }
297         break;
298
299     case 32:
300         for (h=0;h<height;h++)
301         {
302             tbuf = startline;
303             for (w=0;w<bitmap.bmWidth;w++)
304             {
305                 long pixel = XGetPixel(image,w,h);
306
307                 *tbuf++ = pixel & 0xff;
308                 *tbuf++ = (pixel>> 8) & 0xff;
309                 *tbuf++ = (pixel>>16) & 0xff;
310                 *tbuf++ = (pixel>>24) & 0xff;
311             }
312             startline += bitmap.bmWidthBytes;
313         }
314         break;
315     default:
316         FIXME("Unhandled bits:%d\n", bitmap.bmBitsPixel);
317     }
318     XDestroyImage( image );
319     wine_tsx11_unlock();
320     return count;
321 }
322
323
324
325 /******************************************************************************
326  *             SetBitmapBits   (X11DRV.@)
327  *
328  * RETURNS
329  *    Success: Number of bytes used in setting the bitmap bits
330  *    Failure: 0
331  */
332 LONG CDECL X11DRV_SetBitmapBits( HBITMAP hbitmap, const void *bits, LONG count )
333 {
334     BITMAP bitmap;
335     X_PHYSBITMAP *physBitmap = X11DRV_get_phys_bitmap( hbitmap );
336     LONG height;
337     XImage *image;
338     const BYTE *sbuf, *startline;
339     int w, h;
340
341     if (!physBitmap || !GetObjectW( hbitmap, sizeof(bitmap), &bitmap )) return 0;
342
343     TRACE("(bmp=%p, bits=%p, count=0x%x)\n", hbitmap, bits, count);
344
345     height = count / bitmap.bmWidthBytes;
346
347     wine_tsx11_lock();
348     image = XCreateImage( gdi_display, visual, physBitmap->pixmap_depth, ZPixmap, 0, NULL,
349                           bitmap.bmWidth, height, 32, 0 );
350     if (!(image->data = HeapAlloc( GetProcessHeap(), 0, image->bytes_per_line * height )))
351     {
352         WARN("No memory to create image data.\n");
353         XDestroyImage( image );
354         wine_tsx11_unlock();
355         return 0;
356     }
357
358     /* copy 16 bit padded image buffer with real bitsperpixel to XImage */
359
360     startline = bits;
361
362     switch (bitmap.bmBitsPixel)
363     {
364     case 1:
365         for (h=0;h<height;h++)
366         {
367             sbuf = startline;
368             for (w=0;w<bitmap.bmWidth;w++)
369             {
370                 XPutPixel(image,w,h,(sbuf[0]>>(7-(w&7))) & 1);
371                 if ((w&7) == 7)
372                     sbuf++;
373             }
374             startline += bitmap.bmWidthBytes;
375         }
376         break;
377     case 4:
378         for (h=0;h<height;h++)
379         {
380             sbuf = startline;
381             for (w=0;w<bitmap.bmWidth;w++)
382             {
383                 if (!(w & 1)) XPutPixel( image, w, h, *sbuf >> 4 );
384                 else XPutPixel( image, w, h, *sbuf++ & 0xf );
385             }
386             startline += bitmap.bmWidthBytes;
387         }
388         break;
389     case 8:
390         for (h=0;h<height;h++)
391         {
392             sbuf = startline;
393             for (w=0;w<bitmap.bmWidth;w++)
394                 XPutPixel(image,w,h,*sbuf++);
395             startline += bitmap.bmWidthBytes;
396         }
397         break;
398     case 15:
399     case 16:
400         for (h=0;h<height;h++)
401         {
402             sbuf = startline;
403             for (w=0;w<bitmap.bmWidth;w++)
404             {
405                 XPutPixel(image,w,h,sbuf[1]*256+sbuf[0]);
406                 sbuf+=2;
407             }
408             startline += bitmap.bmWidthBytes;
409         }
410         break;
411     case 24:
412         for (h=0;h<height;h++)
413         {
414             sbuf = startline;
415             for (w=0;w<bitmap.bmWidth;w++)
416             {
417                 XPutPixel(image,w,h,(sbuf[2]<<16)+(sbuf[1]<<8)+sbuf[0]);
418                 sbuf += 3;
419             }
420             startline += bitmap.bmWidthBytes;
421         }
422         break;
423     case 32:
424         for (h=0;h<height;h++)
425         {
426             sbuf = startline;
427             for (w=0;w<bitmap.bmWidth;w++)
428             {
429                 XPutPixel(image,w,h,(sbuf[3]<<24)+(sbuf[2]<<16)+(sbuf[1]<<8)+sbuf[0]);
430                 sbuf += 4;
431             }
432             startline += bitmap.bmWidthBytes;
433         }
434         break;
435     default:
436       FIXME("Unhandled bits:%d\n", bitmap.bmBitsPixel);
437
438     }
439     XPutImage( gdi_display, physBitmap->pixmap, get_bitmap_gc(physBitmap->pixmap_depth),
440                image, 0, 0, 0, 0, bitmap.bmWidth, height );
441     HeapFree( GetProcessHeap(), 0, image->data );
442     image->data = NULL;
443     XDestroyImage( image );
444     wine_tsx11_unlock();
445     return count;
446 }
447
448 /***********************************************************************
449  *           DeleteBitmap   (X11DRV.@)
450  */
451 BOOL CDECL X11DRV_DeleteBitmap( HBITMAP hbitmap )
452 {
453     X_PHYSBITMAP *physBitmap = X11DRV_get_phys_bitmap( hbitmap );
454
455     if (physBitmap)
456     {
457         DIBSECTION dib;
458
459         if (GetObjectW( hbitmap, sizeof(dib), &dib ) == sizeof(dib))
460             X11DRV_DIB_DeleteDIBSection( physBitmap, &dib );
461
462         if (physBitmap->glxpixmap)
463             destroy_glxpixmap( gdi_display, physBitmap->glxpixmap );
464         wine_tsx11_lock();
465         if (physBitmap->pixmap) XFreePixmap( gdi_display, physBitmap->pixmap );
466         XDeleteContext( gdi_display, (XID)hbitmap, bitmap_context );
467         wine_tsx11_unlock();
468         HeapFree( GetProcessHeap(), 0, physBitmap );
469     }
470     return TRUE;
471 }
472
473
474 /***********************************************************************
475  *           X11DRV_get_phys_bitmap
476  *
477  * Retrieve the X physical bitmap info.
478  */
479 X_PHYSBITMAP *X11DRV_get_phys_bitmap( HBITMAP hbitmap )
480 {
481     X_PHYSBITMAP *ret;
482
483     wine_tsx11_lock();
484     if (XFindContext( gdi_display, (XID)hbitmap, bitmap_context, (char **)&ret )) ret = NULL;
485     wine_tsx11_unlock();
486     return ret;
487 }
488
489
490 /***********************************************************************
491  *           X11DRV_init_phys_bitmap
492  *
493  * Initialize the X physical bitmap info.
494  */
495 X_PHYSBITMAP *X11DRV_init_phys_bitmap( HBITMAP hbitmap )
496 {
497     X_PHYSBITMAP *ret;
498
499     if ((ret = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*ret) )) != NULL)
500     {
501         ret->hbitmap = hbitmap;
502         wine_tsx11_lock();
503         XSaveContext( gdi_display, (XID)hbitmap, bitmap_context, (char *)ret );
504         wine_tsx11_unlock();
505     }
506     return ret;
507 }
508
509
510 /***********************************************************************
511  *           X11DRV_get_pixmap
512  *
513  * Retrieve the pixmap associated to a bitmap.
514  */
515 Pixmap X11DRV_get_pixmap( HBITMAP hbitmap )
516 {
517     X_PHYSBITMAP *physBitmap = X11DRV_get_phys_bitmap( hbitmap );
518
519     if (!physBitmap) return 0;
520     return physBitmap->pixmap;
521 }