dinput: BuildActionMap and SetActionMap stubs for generic joystick.
[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( PHYSDEV dev, HBITMAP hbitmap )
91 {
92     X11DRV_PDEVICE *physDev = get_x11drv_dev( dev );
93     X_PHYSBITMAP *physBitmap;
94     BITMAP bitmap;
95
96     if (!GetObjectW( hbitmap, sizeof(bitmap), &bitmap )) return 0;
97
98     if(physDev->xrender)
99         X11DRV_XRender_UpdateDrawable( physDev );
100
101     if (hbitmap == BITMAP_stock_phys_bitmap.hbitmap) physBitmap = &BITMAP_stock_phys_bitmap;
102     else if (!(physBitmap = X11DRV_get_phys_bitmap( hbitmap ))) return 0;
103
104     physDev->bitmap = physBitmap;
105     physDev->drawable = physBitmap->pixmap;
106     physDev->color_shifts = physBitmap->trueColor ? &physBitmap->pixmap_color_shifts : NULL;
107     SetRect( &physDev->drawable_rect, 0, 0, bitmap.bmWidth, bitmap.bmHeight );
108     physDev->dc_rect = physDev->drawable_rect;
109
110       /* Change GC depth if needed */
111
112     if (physDev->depth != physBitmap->pixmap_depth)
113     {
114         physDev->depth = physBitmap->pixmap_depth;
115         wine_tsx11_lock();
116         XFreeGC( gdi_display, physDev->gc );
117         physDev->gc = XCreateGC( gdi_display, physDev->drawable, 0, NULL );
118         XSetGraphicsExposures( gdi_display, physDev->gc, False );
119         XSetSubwindowMode( gdi_display, physDev->gc, IncludeInferiors );
120         XFlush( gdi_display );
121         wine_tsx11_unlock();
122     }
123
124     return hbitmap;
125 }
126
127
128 /****************************************************************************
129  *        CreateBitmap   (X11DRV.@)
130  *
131  * Create a device dependent X11 bitmap
132  *
133  * Returns TRUE on success else FALSE
134  */
135 BOOL CDECL X11DRV_CreateBitmap( PHYSDEV dev, HBITMAP hbitmap, LPVOID bmBits )
136 {
137     X_PHYSBITMAP *physBitmap;
138     BITMAP bitmap;
139
140     if (!GetObjectW( hbitmap, sizeof(bitmap), &bitmap )) return FALSE;
141
142       /* Check parameters */
143     if (bitmap.bmPlanes != 1) return FALSE;
144
145     /* check if bpp is compatible with screen depth */
146     if (!((bitmap.bmBitsPixel == 1) || (bitmap.bmBitsPixel == screen_bpp)))
147     {
148         WARN("Trying to make bitmap with planes=%d, bpp=%d\n",
149             bitmap.bmPlanes, bitmap.bmBitsPixel);
150         return FALSE;
151     }
152     if (hbitmap == BITMAP_stock_phys_bitmap.hbitmap)
153     {
154         ERR( "called for stock bitmap, please report\n" );
155         return FALSE;
156     }
157
158     TRACE("(%p) %dx%d %d bpp\n", hbitmap, bitmap.bmWidth, bitmap.bmHeight, bitmap.bmBitsPixel);
159
160     if (!(physBitmap = X11DRV_init_phys_bitmap( hbitmap ))) return FALSE;
161
162     if (!X11DRV_XRender_SetPhysBitmapDepth( physBitmap, bitmap.bmBitsPixel, NULL ))
163     {
164         if(bitmap.bmBitsPixel == 1)
165         {
166             physBitmap->pixmap_depth = 1;
167             physBitmap->trueColor = FALSE;
168         }
169         else
170         {
171             physBitmap->pixmap_depth = screen_depth;
172             physBitmap->pixmap_color_shifts = X11DRV_PALETTE_default_shifts;
173             physBitmap->trueColor = (visual->class == TrueColor || visual->class == DirectColor);
174         }
175     }
176
177     wine_tsx11_lock();
178     /* Create the pixmap */
179     physBitmap->pixmap = XCreatePixmap(gdi_display, root_window,
180                                        bitmap.bmWidth, bitmap.bmHeight, physBitmap->pixmap_depth);
181     wine_tsx11_unlock();
182     if (!physBitmap->pixmap)
183     {
184         WARN("Can't create Pixmap\n");
185         HeapFree( GetProcessHeap(), 0, physBitmap );
186         return FALSE;
187     }
188
189     if (bmBits) /* Set bitmap bits */
190     {
191         X11DRV_SetBitmapBits( hbitmap, bmBits, bitmap.bmHeight * bitmap.bmWidthBytes );
192     }
193     else  /* else clear the bitmap */
194     {
195         GC gc = get_bitmap_gc(physBitmap->pixmap_depth);
196         wine_tsx11_lock();
197         XSetFunction( gdi_display, gc, GXclear );
198         XFillRectangle( gdi_display, physBitmap->pixmap, gc, 0, 0,
199                         bitmap.bmWidth, bitmap.bmHeight );
200         XSetFunction( gdi_display, gc, GXcopy );
201         wine_tsx11_unlock();
202     }
203     return TRUE;
204 }
205
206
207 /***********************************************************************
208  *           GetBitmapBits   (X11DRV.@)
209  *
210  * RETURNS
211  *    Success: Number of bytes copied
212  *    Failure: 0
213  */
214 LONG CDECL X11DRV_GetBitmapBits( HBITMAP hbitmap, void *buffer, LONG count )
215 {
216     BITMAP bitmap;
217     X_PHYSBITMAP *physBitmap = X11DRV_get_phys_bitmap( hbitmap );
218     LONG height;
219     XImage *image;
220     LPBYTE tbuf, startline;
221     int h, w;
222
223     if (!physBitmap || !GetObjectW( hbitmap, sizeof(bitmap), &bitmap )) return 0;
224
225     TRACE("(bmp=%p, buffer=%p, count=0x%x)\n", hbitmap, buffer, count);
226
227     wine_tsx11_lock();
228     height = count / bitmap.bmWidthBytes;
229     image = XGetImage( gdi_display, physBitmap->pixmap, 0, 0,
230                        bitmap.bmWidth, height, AllPlanes, ZPixmap );
231
232     /* copy XImage to 16 bit padded image buffer with real bitsperpixel */
233
234     startline = buffer;
235     switch (bitmap.bmBitsPixel)
236     {
237     case 1:
238         for (h=0;h<height;h++)
239         {
240             tbuf = startline;
241             *tbuf = 0;
242             for (w=0;w<bitmap.bmWidth;w++)
243             {
244                 if ((w%8) == 0)
245                     *tbuf = 0;
246                 *tbuf |= XGetPixel(image,w,h)<<(7-(w&7));
247                 if ((w&7) == 7) ++tbuf;
248             }
249             startline += bitmap.bmWidthBytes;
250         }
251         break;
252     case 4:
253         for (h=0;h<height;h++)
254         {
255             tbuf = startline;
256             for (w=0;w<bitmap.bmWidth;w++)
257             {
258                 if (!(w & 1)) *tbuf = XGetPixel( image, w, h) << 4;
259                 else *tbuf++ |= XGetPixel( image, w, h) & 0x0f;
260             }
261             startline += bitmap.bmWidthBytes;
262         }
263         break;
264     case 8:
265         for (h=0;h<height;h++)
266         {
267             tbuf = startline;
268             for (w=0;w<bitmap.bmWidth;w++)
269                 *tbuf++ = XGetPixel(image,w,h);
270             startline += bitmap.bmWidthBytes;
271         }
272         break;
273     case 15:
274     case 16:
275         for (h=0;h<height;h++)
276         {
277             tbuf = startline;
278             for (w=0;w<bitmap.bmWidth;w++)
279             {
280                 long pixel = XGetPixel(image,w,h);
281
282                 *tbuf++ = pixel & 0xff;
283                 *tbuf++ = (pixel>>8) & 0xff;
284             }
285             startline += bitmap.bmWidthBytes;
286         }
287         break;
288     case 24:
289         for (h=0;h<height;h++)
290         {
291             tbuf = startline;
292             for (w=0;w<bitmap.bmWidth;w++)
293             {
294                 long pixel = XGetPixel(image,w,h);
295
296                 *tbuf++ = pixel & 0xff;
297                 *tbuf++ = (pixel>> 8) & 0xff;
298                 *tbuf++ = (pixel>>16) & 0xff;
299             }
300             startline += bitmap.bmWidthBytes;
301         }
302         break;
303
304     case 32:
305         for (h=0;h<height;h++)
306         {
307             tbuf = startline;
308             for (w=0;w<bitmap.bmWidth;w++)
309             {
310                 long pixel = XGetPixel(image,w,h);
311
312                 *tbuf++ = pixel & 0xff;
313                 *tbuf++ = (pixel>> 8) & 0xff;
314                 *tbuf++ = (pixel>>16) & 0xff;
315                 *tbuf++ = (pixel>>24) & 0xff;
316             }
317             startline += bitmap.bmWidthBytes;
318         }
319         break;
320     default:
321         FIXME("Unhandled bits:%d\n", bitmap.bmBitsPixel);
322     }
323     XDestroyImage( image );
324     wine_tsx11_unlock();
325     return count;
326 }
327
328
329
330 /******************************************************************************
331  *             SetBitmapBits   (X11DRV.@)
332  *
333  * RETURNS
334  *    Success: Number of bytes used in setting the bitmap bits
335  *    Failure: 0
336  */
337 LONG CDECL X11DRV_SetBitmapBits( HBITMAP hbitmap, const void *bits, LONG count )
338 {
339     BITMAP bitmap;
340     X_PHYSBITMAP *physBitmap = X11DRV_get_phys_bitmap( hbitmap );
341     LONG height;
342     XImage *image;
343     const BYTE *sbuf, *startline;
344     int w, h;
345
346     if (!physBitmap || !GetObjectW( hbitmap, sizeof(bitmap), &bitmap )) return 0;
347
348     TRACE("(bmp=%p, bits=%p, count=0x%x)\n", hbitmap, bits, count);
349
350     height = count / bitmap.bmWidthBytes;
351
352     wine_tsx11_lock();
353     image = XCreateImage( gdi_display, visual, physBitmap->pixmap_depth, ZPixmap, 0, NULL,
354                           bitmap.bmWidth, height, 32, 0 );
355     if (!(image->data = HeapAlloc( GetProcessHeap(), 0, image->bytes_per_line * height )))
356     {
357         WARN("No memory to create image data.\n");
358         XDestroyImage( image );
359         wine_tsx11_unlock();
360         return 0;
361     }
362
363     /* copy 16 bit padded image buffer with real bitsperpixel to XImage */
364
365     startline = bits;
366
367     switch (bitmap.bmBitsPixel)
368     {
369     case 1:
370         for (h=0;h<height;h++)
371         {
372             sbuf = startline;
373             for (w=0;w<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 += bitmap.bmWidthBytes;
380         }
381         break;
382     case 4:
383         for (h=0;h<height;h++)
384         {
385             sbuf = startline;
386             for (w=0;w<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 += bitmap.bmWidthBytes;
392         }
393         break;
394     case 8:
395         for (h=0;h<height;h++)
396         {
397             sbuf = startline;
398             for (w=0;w<bitmap.bmWidth;w++)
399                 XPutPixel(image,w,h,*sbuf++);
400             startline += 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<bitmap.bmWidth;w++)
409             {
410                 XPutPixel(image,w,h,sbuf[1]*256+sbuf[0]);
411                 sbuf+=2;
412             }
413             startline += bitmap.bmWidthBytes;
414         }
415         break;
416     case 24:
417         for (h=0;h<height;h++)
418         {
419             sbuf = startline;
420             for (w=0;w<bitmap.bmWidth;w++)
421             {
422                 XPutPixel(image,w,h,(sbuf[2]<<16)+(sbuf[1]<<8)+sbuf[0]);
423                 sbuf += 3;
424             }
425             startline += bitmap.bmWidthBytes;
426         }
427         break;
428     case 32:
429         for (h=0;h<height;h++)
430         {
431             sbuf = startline;
432             for (w=0;w<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 += bitmap.bmWidthBytes;
438         }
439         break;
440     default:
441       FIXME("Unhandled bits:%d\n", bitmap.bmBitsPixel);
442
443     }
444     XPutImage( gdi_display, physBitmap->pixmap, get_bitmap_gc(physBitmap->pixmap_depth),
445                image, 0, 0, 0, 0, bitmap.bmWidth, height );
446     HeapFree( GetProcessHeap(), 0, image->data );
447     image->data = NULL;
448     XDestroyImage( image );
449     wine_tsx11_unlock();
450     return count;
451 }
452
453 /***********************************************************************
454  *           DeleteBitmap   (X11DRV.@)
455  */
456 BOOL CDECL X11DRV_DeleteBitmap( HBITMAP hbitmap )
457 {
458     X_PHYSBITMAP *physBitmap = X11DRV_get_phys_bitmap( hbitmap );
459
460     if (physBitmap)
461     {
462         DIBSECTION dib;
463
464         if (GetObjectW( hbitmap, sizeof(dib), &dib ) == sizeof(dib))
465             X11DRV_DIB_DeleteDIBSection( physBitmap, &dib );
466
467         if (physBitmap->glxpixmap)
468             destroy_glxpixmap( gdi_display, physBitmap->glxpixmap );
469         wine_tsx11_lock();
470         if (physBitmap->pixmap) XFreePixmap( gdi_display, physBitmap->pixmap );
471         XDeleteContext( gdi_display, (XID)hbitmap, bitmap_context );
472         wine_tsx11_unlock();
473         HeapFree( GetProcessHeap(), 0, physBitmap );
474     }
475     return TRUE;
476 }
477
478
479 /***********************************************************************
480  *           X11DRV_get_phys_bitmap
481  *
482  * Retrieve the X physical bitmap info.
483  */
484 X_PHYSBITMAP *X11DRV_get_phys_bitmap( HBITMAP hbitmap )
485 {
486     X_PHYSBITMAP *ret;
487
488     wine_tsx11_lock();
489     if (XFindContext( gdi_display, (XID)hbitmap, bitmap_context, (char **)&ret )) ret = NULL;
490     wine_tsx11_unlock();
491     return ret;
492 }
493
494
495 /***********************************************************************
496  *           X11DRV_init_phys_bitmap
497  *
498  * Initialize the X physical bitmap info.
499  */
500 X_PHYSBITMAP *X11DRV_init_phys_bitmap( HBITMAP hbitmap )
501 {
502     X_PHYSBITMAP *ret;
503
504     if ((ret = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*ret) )) != NULL)
505     {
506         ret->hbitmap = hbitmap;
507         wine_tsx11_lock();
508         XSaveContext( gdi_display, (XID)hbitmap, bitmap_context, (char *)ret );
509         wine_tsx11_unlock();
510     }
511     return ret;
512 }
513
514
515 /***********************************************************************
516  *           X11DRV_get_pixmap
517  *
518  * Retrieve the pixmap associated to a bitmap.
519  */
520 Pixmap X11DRV_get_pixmap( HBITMAP hbitmap )
521 {
522     X_PHYSBITMAP *physBitmap = X11DRV_get_phys_bitmap( hbitmap );
523
524     if (!physBitmap) return 0;
525     return physBitmap->pixmap;
526 }