Added a first-cut version of MapVirtualKeyExW() that has the same
[wine] / graphics / x11drv / bitmap.c
1 /*
2  * X11DRV bitmap objects
3  *
4  * Copyright 1993 Alexandre Julliard
5  *           1999 Noel Borthwick
6  */
7
8 #include "config.h"
9
10 #include "ts_xlib.h"
11 #include "ts_xutil.h"
12
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include "gdi.h"
16 #include "callback.h"
17 #include "bitmap.h"
18 #include "heap.h"
19 #include "debugtools.h"
20 #include "local.h"
21 #include "x11drv.h"
22 #include "wingdi.h"
23 #include "windef.h"
24 #include "wine/winuser16.h"
25
26 DEFAULT_DEBUG_CHANNEL(x11drv);
27
28   /* GCs used for B&W and color bitmap operations */
29 GC BITMAP_monoGC = 0, BITMAP_colorGC = 0;
30
31
32 /***********************************************************************
33  *           X11DRV_BITMAP_Init
34  */
35 BOOL X11DRV_BITMAP_Init(void)
36 {
37     Pixmap tmpPixmap;
38     
39       /* Create the necessary GCs */
40     
41     if ((tmpPixmap = TSXCreatePixmap(display, 
42                                      X11DRV_GetXRootWindow(), 
43                                      1, 1, 
44                                      1)))
45     {
46         BITMAP_monoGC = TSXCreateGC( display, tmpPixmap, 0, NULL );
47         TSXSetGraphicsExposures( display, BITMAP_monoGC, False );
48         TSXFreePixmap( display, tmpPixmap );
49     }
50
51     if (X11DRV_GetDepth() != 1)
52     {
53         if ((tmpPixmap = TSXCreatePixmap(display, X11DRV_GetXRootWindow(),
54                                          1, 1, X11DRV_GetDepth())))
55         {
56             BITMAP_colorGC = TSXCreateGC( display, tmpPixmap, 0, NULL );
57             TSXSetGraphicsExposures( display, BITMAP_colorGC, False );
58             TSXFreePixmap( display, tmpPixmap );
59         }
60     }
61     return TRUE;
62 }
63
64 /***********************************************************************
65  *           X11DRV_BITMAP_SelectObject
66  */
67 HBITMAP X11DRV_BITMAP_SelectObject( DC * dc, HBITMAP hbitmap,
68                                       BITMAPOBJ * bmp )
69 {
70     HRGN hrgn;
71     HBITMAP prevHandle = dc->hBitmap;
72     X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
73
74
75     if (!(dc->flags & DC_MEMORY)) return 0;
76
77     if(!bmp->physBitmap)
78         if(!X11DRV_CreateBitmap(hbitmap))
79             return 0;
80
81     if(bmp->funcs != dc->funcs) {
82         WARN("Trying to select non-X11 DDB into an X11 dc\n");
83         return 0;
84     }
85
86     hrgn = CreateRectRgn(0, 0, bmp->bitmap.bmWidth, bmp->bitmap.bmHeight);
87     if (!hrgn) return 0;
88
89     dc->totalExtent.left   = 0;
90     dc->totalExtent.top    = 0;
91     dc->totalExtent.right  = bmp->bitmap.bmWidth;
92     dc->totalExtent.bottom = bmp->bitmap.bmHeight;
93
94     physDev->drawable = (Pixmap)bmp->physBitmap;
95     dc->hBitmap     = hbitmap;
96
97     SelectVisRgn16( dc->hSelf, hrgn );
98     DeleteObject( hrgn );
99
100       /* Change GC depth if needed */
101
102     if (dc->bitsPerPixel != bmp->bitmap.bmBitsPixel)
103     {
104         TSXFreeGC( display, physDev->gc );
105         physDev->gc = TSXCreateGC( display, physDev->drawable, 0, NULL );
106         TSXSetGraphicsExposures( display, physDev->gc, False );
107         dc->bitsPerPixel = bmp->bitmap.bmBitsPixel;
108         DC_InitDC( dc );
109     }
110     return prevHandle;
111 }
112
113
114 /***********************************************************************
115  *           XPutImage_wrapper
116  *
117  * Wrapper to call XPutImage with CALL_LARGE_STACK.
118  */
119
120 struct XPutImage_descr
121 {
122     BITMAPOBJ *bmp;
123     XImage    *image;
124     INT      width;
125     INT      height;
126 };
127
128 static int XPutImage_wrapper( const struct XPutImage_descr *descr )
129 {
130     return XPutImage( display, (Pixmap)descr->bmp->physBitmap, BITMAP_GC(descr->bmp),
131                       descr->image, 0, 0, 0, 0, descr->width, descr->height );
132 }
133
134
135 /****************************************************************************
136  *
137  *        X11DRV_CreateBitmap
138  *
139  * Create a device dependent X11 bitmap
140  *
141  * Returns TRUE on success else FALSE
142  *
143  */
144
145 BOOL X11DRV_CreateBitmap( HBITMAP hbitmap )
146 {
147     BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
148
149     if(!bmp) {
150         WARN("Bad bitmap handle %08x\n", hbitmap);
151         return FALSE;
152     }
153
154       /* Check parameters */
155     if (bmp->bitmap.bmPlanes != 1)
156     {
157         GDI_ReleaseObj( hbitmap );
158         return 0;
159     }
160     if ((bmp->bitmap.bmBitsPixel != 1) && 
161         (bmp->bitmap.bmBitsPixel != X11DRV_GetDepth()))
162     {
163         ERR("Trying to make bitmap with planes=%d, bpp=%d\n",
164             bmp->bitmap.bmPlanes, bmp->bitmap.bmBitsPixel);
165         GDI_ReleaseObj( hbitmap );
166         return FALSE;
167     }
168
169     TRACE("(%08x) %dx%d %d bpp\n", hbitmap, bmp->bitmap.bmWidth,
170           bmp->bitmap.bmHeight, bmp->bitmap.bmBitsPixel);
171
172       /* Create the pixmap */
173     if (!(bmp->physBitmap = (void *)TSXCreatePixmap(display, X11DRV_GetXRootWindow(),
174                                                     bmp->bitmap.bmWidth, bmp->bitmap.bmHeight,
175                                                     bmp->bitmap.bmBitsPixel)))
176     {
177         WARN("Can't create Pixmap\n");
178         GDI_ReleaseObj( hbitmap );
179         return FALSE;
180     }
181     bmp->funcs = &X11DRV_DC_Funcs;
182
183     if (bmp->bitmap.bmBits) /* Set bitmap bits */
184         X11DRV_BitmapBits( hbitmap, bmp->bitmap.bmBits,
185                            bmp->bitmap.bmHeight * bmp->bitmap.bmWidthBytes,
186                            DDB_SET );
187
188     GDI_ReleaseObj( hbitmap );
189     return TRUE;
190 }
191
192
193 /***********************************************************************
194  *           X11DRV_BITMAP_GetXImage
195  *
196  * Get an X image for a bitmap. For use with CALL_LARGE_STACK.
197  */
198 XImage *X11DRV_BITMAP_GetXImage( const BITMAPOBJ *bmp )
199 {
200     return XGetImage( display, (Pixmap)bmp->physBitmap,
201                       0, 0, bmp->bitmap.bmWidth, bmp->bitmap.bmHeight,
202                       AllPlanes, ZPixmap );
203 }
204
205
206 /***********************************************************************
207  *           X11DRV_GetBitmapBits
208  * 
209  * RETURNS
210  *    Success: Number of bytes copied
211  *    Failure: 0
212  */
213 static LONG X11DRV_GetBitmapBits(BITMAPOBJ *bmp, void *buffer, LONG count)
214 {
215     LONG old_height, height;
216     XImage *image;
217     LPBYTE tbuf, startline;
218     int h, w;
219
220     TRACE("(bmp=%p, buffer=%p, count=0x%lx)\n", bmp, buffer, count);
221
222     EnterCriticalSection( &X11DRV_CritSection );
223
224     /* Hack: change the bitmap height temporarily to avoid */
225     /*       getting unnecessary bitmap rows. */
226
227     old_height = bmp->bitmap.bmHeight;
228     height = bmp->bitmap.bmHeight = count / bmp->bitmap.bmWidthBytes;
229
230     image = (XImage *)CALL_LARGE_STACK( X11DRV_BITMAP_GetXImage, bmp );
231
232     bmp->bitmap.bmHeight = old_height;
233
234     /* copy XImage to 16 bit padded image buffer with real bitsperpixel */
235
236     startline = buffer;
237     switch (bmp->bitmap.bmBitsPixel)
238     {
239     case 1:
240         for (h=0;h<height;h++)
241         {
242             tbuf = startline;
243             *tbuf = 0;
244             for (w=0;w<bmp->bitmap.bmWidth;w++)
245             {
246                 if ((w%8) == 0)
247                     *tbuf = 0;
248                 *tbuf |= XGetPixel(image,w,h)<<(7-(w&7));
249                 if ((w&7) == 7) ++tbuf;
250             }
251             startline += bmp->bitmap.bmWidthBytes;
252         }
253         break;
254     case 4:
255         for (h=0;h<height;h++)
256         {
257             tbuf = startline;
258             for (w=0;w<bmp->bitmap.bmWidth;w++)
259             {
260                 if (!(w & 1)) *tbuf = XGetPixel( image, w, h) << 4;
261                 else *tbuf++ |= XGetPixel( image, w, h) & 0x0f;
262             }
263             startline += bmp->bitmap.bmWidthBytes;
264         }
265         break;
266     case 8:
267         for (h=0;h<height;h++)
268         {
269             tbuf = startline;
270             for (w=0;w<bmp->bitmap.bmWidth;w++)
271                 *tbuf++ = XGetPixel(image,w,h);
272             startline += bmp->bitmap.bmWidthBytes;
273         }
274         break;
275     case 15:
276     case 16:
277         for (h=0;h<height;h++)
278         {
279             tbuf = startline;
280             for (w=0;w<bmp->bitmap.bmWidth;w++)
281             {
282                 long pixel = XGetPixel(image,w,h);
283
284                 *tbuf++ = pixel & 0xff;
285                 *tbuf++ = (pixel>>8) & 0xff;
286             }
287             startline += bmp->bitmap.bmWidthBytes;
288         }
289         break;
290     case 24:
291         for (h=0;h<height;h++)
292         {
293             tbuf = startline;
294             for (w=0;w<bmp->bitmap.bmWidth;w++)
295             {
296                 long pixel = XGetPixel(image,w,h);
297
298                 *tbuf++ = pixel & 0xff;
299                 *tbuf++ = (pixel>> 8) & 0xff;
300                 *tbuf++ = (pixel>>16) & 0xff;
301             }
302             startline += bmp->bitmap.bmWidthBytes;
303         }
304         break;
305
306     case 32:
307         for (h=0;h<height;h++)
308         {
309             tbuf = startline;
310             for (w=0;w<bmp->bitmap.bmWidth;w++)
311             {
312                 long pixel = XGetPixel(image,w,h);
313
314                 *tbuf++ = pixel & 0xff;
315                 *tbuf++ = (pixel>> 8) & 0xff;
316                 *tbuf++ = (pixel>>16) & 0xff;
317                 *tbuf++ = (pixel>>24) & 0xff;
318             }
319             startline += bmp->bitmap.bmWidthBytes;
320         }
321         break;
322     default:
323         FIXME("Unhandled bits:%d\n", bmp->bitmap.bmBitsPixel);
324     }
325     XDestroyImage( image );
326     LeaveCriticalSection( &X11DRV_CritSection );
327
328     return count;
329 }
330
331
332
333 /******************************************************************************
334  *             X11DRV_SetBitmapBits
335  *
336  * RETURNS
337  *    Success: Number of bytes used in setting the bitmap bits
338  *    Failure: 0
339  */
340 static LONG X11DRV_SetBitmapBits(BITMAPOBJ *bmp, void *bits, LONG count)
341 {
342     struct XPutImage_descr descr;
343     LONG height;
344     XImage *image;
345     LPBYTE sbuf, startline;
346     int w, h;
347
348     TRACE("(bmp=%p, bits=%p, count=0x%lx)\n", bmp, bits, count);
349     
350     height = count / bmp->bitmap.bmWidthBytes;
351
352     EnterCriticalSection( &X11DRV_CritSection );
353     image = XCreateImage( display, X11DRV_GetVisual(), bmp->bitmap.bmBitsPixel, ZPixmap, 0, NULL,
354                           bmp->bitmap.bmWidth, height, 32, 0 );
355     if (!(image->data = (LPBYTE)malloc(image->bytes_per_line * height)))
356     {
357         WARN("No memory to create image data.\n");
358         XDestroyImage( image );
359         LeaveCriticalSection( &X11DRV_CritSection );
360         return 0;
361     }
362     
363     /* copy 16 bit padded image buffer with real bitsperpixel to XImage */
364     
365     startline = bits;
366
367     switch (bmp->bitmap.bmBitsPixel)
368     {
369     case 1:
370         for (h=0;h<height;h++)
371         {
372             sbuf = startline;
373             for (w=0;w<bmp->bitmap.bmWidth;w++)
374             {
375                 XPutPixel(image,w,h,(sbuf[0]>>(7-(w&7))) & 1);
376                 if ((w&7) == 7)
377                     sbuf++;
378             }
379             startline += bmp->bitmap.bmWidthBytes;
380         }
381         break;
382     case 4:
383         for (h=0;h<height;h++)
384         {
385             sbuf = startline;
386             for (w=0;w<bmp->bitmap.bmWidth;w++)
387             {
388                 if (!(w & 1)) XPutPixel( image, w, h, *sbuf >> 4 );
389                 else XPutPixel( image, w, h, *sbuf++ & 0xf );
390             }
391             startline += bmp->bitmap.bmWidthBytes;
392         }
393         break;
394     case 8:
395         for (h=0;h<height;h++)
396         {
397             sbuf = startline;
398             for (w=0;w<bmp->bitmap.bmWidth;w++)
399                 XPutPixel(image,w,h,*sbuf++);
400             startline += bmp->bitmap.bmWidthBytes;
401         }
402         break;
403     case 15:
404     case 16:
405         for (h=0;h<height;h++)
406         {
407             sbuf = startline;
408             for (w=0;w<bmp->bitmap.bmWidth;w++)
409             {
410                 XPutPixel(image,w,h,sbuf[1]*256+sbuf[0]);
411                 sbuf+=2;
412             }
413             startline += bmp->bitmap.bmWidthBytes;
414         }
415         break;
416     case 24:
417         for (h=0;h<height;h++)
418         {
419             sbuf = startline;
420             for (w=0;w<bmp->bitmap.bmWidth;w++)
421             {
422                 XPutPixel(image,w,h,(sbuf[2]<<16)+(sbuf[1]<<8)+sbuf[0]);
423                 sbuf += 3;
424             }
425             startline += bmp->bitmap.bmWidthBytes;
426         }
427         break;
428     case 32: 
429         for (h=0;h<height;h++)
430         {
431             sbuf = startline;
432             for (w=0;w<bmp->bitmap.bmWidth;w++)
433             {
434                 XPutPixel(image,w,h,(sbuf[3]<<24)+(sbuf[2]<<16)+(sbuf[1]<<8)+sbuf[0]);
435                 sbuf += 4;
436             }
437             startline += bmp->bitmap.bmWidthBytes;
438         }
439         break;
440     default:
441       FIXME("Unhandled bits:%d\n", bmp->bitmap.bmBitsPixel);
442
443     }
444
445     descr.bmp    = bmp;
446     descr.image  = image;
447     descr.width  = bmp->bitmap.bmWidth;
448     descr.height = height;
449
450     CALL_LARGE_STACK( XPutImage_wrapper, &descr );
451     XDestroyImage( image ); /* frees image->data too */
452     LeaveCriticalSection( &X11DRV_CritSection );
453     
454     return count;
455 }
456
457 /***********************************************************************
458  *           X11DRV_BitmapBits
459  */
460 LONG X11DRV_BitmapBits(HBITMAP hbitmap, void *bits, LONG count, WORD flags)
461 {
462     BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
463     LONG ret;
464     if(!bmp) {
465         WARN("Bad bitmap handle %08x\n", hbitmap);
466         return FALSE;
467     }
468
469     if(flags == DDB_GET)
470         ret = X11DRV_GetBitmapBits(bmp, bits, count);
471     else if(flags == DDB_SET)
472         ret = X11DRV_SetBitmapBits(bmp, bits, count);
473     else {
474         ERR("Unknown flags value %d\n", flags);
475         ret = 0;
476     }
477     GDI_ReleaseObj( hbitmap );
478     return ret;
479 }
480
481 /***********************************************************************
482  *           X11DRV_BITMAP_DeleteObject
483  */
484 BOOL X11DRV_BITMAP_DeleteObject( HBITMAP hbitmap, BITMAPOBJ * bmp )
485 {
486     TSXFreePixmap( display, (Pixmap)bmp->physBitmap );
487     bmp->physBitmap = NULL;
488     bmp->funcs = NULL;
489     return TRUE;
490 }
491
492 /**************************************************************************
493  *              X11DRV_BITMAP_CreateBitmapHeaderFromPixmap
494  *
495  *  Allocates an HBITMAP which references the Pixmap passed in.
496  *  Note: This function makes the bitmap an owner of the Pixmap so subsequently
497  *  calling DeleteObject on this will free the Pixmap as well.
498  */
499 HBITMAP X11DRV_BITMAP_CreateBitmapHeaderFromPixmap(Pixmap pixmap)
500 {
501     HBITMAP hBmp = 0;
502     BITMAPOBJ *pBmp = NULL;
503     Window root;
504     int x,y;               /* Unused */
505     unsigned border_width; /* Unused */
506     unsigned int depth, width, height;
507
508     /* Get the Pixmap dimensions and bit depth */
509     if ( 0 == TSXGetGeometry(display, pixmap, &root, &x, &y, &width, &height,
510                              &border_width, &depth) )
511         goto END;
512
513     TRACE("\tPixmap properties: width=%d, height=%d, depth=%d\n",
514           width, height, depth);
515     
516     /*
517      * Create an HBITMAP with the same dimensions and BPP as the pixmap,
518      * and make it a container for the pixmap passed.
519      */
520     hBmp = CreateBitmap( width, height, 1, depth, NULL );
521
522     pBmp = (BITMAPOBJ *)GDI_GetObjPtr( hBmp, BITMAP_MAGIC );
523     
524     pBmp->funcs = &X11DRV_DC_Funcs;
525     pBmp->physBitmap = (void *)pixmap;
526     GDI_ReleaseObj( hBmp );
527
528 END:
529     TRACE("\tReturning HBITMAP %x\n", hBmp);
530     return hBmp;
531 }
532
533
534 /**************************************************************************
535  *              X11DRV_BITMAP_CreateBitmapFromPixmap
536  *
537  *  Allocates an HBITMAP and copies the Pixmap data into it.
538  *  If bDeletePixmap is TRUE, the Pixmap passed in is deleted after the conversion.
539  */
540 HBITMAP X11DRV_BITMAP_CreateBitmapFromPixmap(Pixmap pixmap, BOOL bDeletePixmap)
541 {
542     HBITMAP hBmp = 0, hBmpCopy = 0;
543     BITMAPOBJ *pBmp = NULL;
544     unsigned int width, height;
545
546     /* Allocate an HBITMAP which references the Pixmap passed to us */
547     hBmp = X11DRV_BITMAP_CreateBitmapHeaderFromPixmap(pixmap);
548     if (!hBmp)
549     {
550         TRACE("\tCould not create bitmap header for Pixmap\n");
551         goto END;
552     }
553
554     /* Get the bitmap dimensions */
555     width = pBmp->bitmap.bmWidth;
556     height = pBmp->bitmap.bmHeight;
557                  
558     hBmpCopy = CopyImage(hBmp, IMAGE_BITMAP, width, height, LR_CREATEDIBSECTION);
559
560     /* We can now get rid of the HBITMAP wrapper we created earlier.
561      * Note: Simply calling DeleteObject will free the embedded Pixmap as well.
562      */
563     if (!bDeletePixmap)
564     {
565         /* Manually clear the bitmap internals to prevent the Pixmap 
566          * from being deleted by DeleteObject.
567          */
568         pBmp->physBitmap = NULL;
569         pBmp->funcs = NULL;
570     }
571     DeleteObject(hBmp);  
572
573 END:
574     TRACE("\tReturning HBITMAP %x\n", hBmpCopy);
575     return hBmpCopy;
576 }
577
578
579 /**************************************************************************
580  *                 X11DRV_BITMAP_CreatePixmapFromBitmap
581  *
582  *    Creates a Pixmap from a bitmap
583  */
584 Pixmap X11DRV_BITMAP_CreatePixmapFromBitmap( HBITMAP hBmp, HDC hdc )
585 {
586     HGLOBAL hPackedDIB = 0;
587     Pixmap pixmap = 0;
588
589     /*
590      * Create a packed DIB from the bitmap passed to us.
591      * A packed DIB contains a BITMAPINFO structure followed immediately by
592      * an optional color palette and the pixel data.
593      */
594     hPackedDIB = DIB_CreateDIBFromBitmap(hdc, hBmp);
595
596     /* Create a Pixmap from the packed DIB */
597     pixmap = X11DRV_DIB_CreatePixmapFromDIB( hPackedDIB, hdc );
598
599     /* Free the temporary packed DIB */
600     GlobalFree(hPackedDIB);
601
602     return pixmap;
603 }
604
605
606 /***********************************************************************
607  *           X11DRV_BITMAP_Pixmap
608  *
609  * This function exists solely for x11 driver of the window system.
610  */
611 Pixmap X11DRV_BITMAP_Pixmap(HBITMAP hbitmap)
612 {
613     Pixmap pixmap;
614     BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
615     pixmap = (Pixmap)bmp->physBitmap;
616     GDI_ReleaseObj( hbitmap );
617     return pixmap;
618 }
619