dinput: BuildActionMap and SetActionMap stubs for generic joystick.
[wine] / dlls / gdi32 / mfdrv / init.c
1 /*
2  * Metafile driver initialisation functions
3  *
4  * Copyright 1996 Alexandre Julliard
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #include <stdarg.h>
22 #include <string.h>
23
24 #include "windef.h"
25 #include "winbase.h"
26 #include "winnls.h"
27 #include "gdi_private.h"
28 #include "mfdrv/metafiledrv.h"
29 #include "wine/debug.h"
30
31 WINE_DEFAULT_DEBUG_CHANNEL(metafile);
32
33 static BOOL CDECL MFDRV_DeleteDC( PHYSDEV dev );
34
35
36 /**********************************************************************
37  *           MFDRV_ExtEscape
38  */
39 static INT CDECL MFDRV_ExtEscape( PHYSDEV dev, INT nEscape, INT cbInput, LPCVOID in_data,
40                                   INT cbOutput, LPVOID out_data )
41 {
42     METARECORD *mr;
43     DWORD len;
44     INT ret;
45
46     if (cbOutput) return 0;  /* escapes that require output cannot work in metafiles */
47
48     len = sizeof(*mr) + sizeof(WORD) + ((cbInput + 1) & ~1);
49     mr = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, len);
50     mr->rdSize = len / 2;
51     mr->rdFunction = META_ESCAPE;
52     mr->rdParm[0] = nEscape;
53     mr->rdParm[1] = cbInput;
54     memcpy(&(mr->rdParm[2]), in_data, cbInput);
55     ret = MFDRV_WriteRecord( dev, mr, len);
56     HeapFree(GetProcessHeap(), 0, mr);
57     return ret;
58 }
59
60
61 /******************************************************************
62  *         MFDRV_GetDeviceCaps
63  *
64  *A very simple implementation that returns DT_METAFILE
65  */
66 static INT CDECL MFDRV_GetDeviceCaps(PHYSDEV dev, INT cap)
67 {
68     switch(cap)
69     {
70     case TECHNOLOGY:
71         return DT_METAFILE;
72     case TEXTCAPS:
73         return 0;
74     default:
75         TRACE(" unsupported capability %d, will return 0\n", cap );
76     }
77     return 0;
78 }
79
80
81 static const DC_FUNCTIONS MFDRV_Funcs =
82 {
83     NULL,                            /* pAbortDoc */
84     MFDRV_AbortPath,                 /* pAbortPath */
85     NULL,                            /* pAlphaBlend */
86     NULL,                            /* pAngleArc */
87     MFDRV_Arc,                       /* pArc */
88     NULL,                            /* pArcTo */
89     MFDRV_BeginPath,                 /* pBeginPath */
90     NULL,                            /* pChoosePixelFormat */
91     MFDRV_Chord,                     /* pChord */
92     MFDRV_CloseFigure,               /* pCloseFigure */
93     NULL,                            /* pCreateBitmap */
94     NULL,                            /* pCreateDC */
95     NULL,                            /* pCreateDIBSection */
96     NULL,                            /* pDeleteBitmap */
97     MFDRV_DeleteDC,                  /* pDeleteDC */
98     MFDRV_DeleteObject,              /* pDeleteObject */
99     NULL,                            /* pDescribePixelFormat */
100     NULL,                            /* pDeviceCapabilities */
101     MFDRV_Ellipse,                   /* pEllipse */
102     NULL,                            /* pEndDoc */
103     NULL,                            /* pEndPage */
104     MFDRV_EndPath,                   /* pEndPath */
105     NULL,                            /* pEnumDeviceFonts */
106     NULL,                            /* pEnumICMProfiles */
107     MFDRV_ExcludeClipRect,           /* pExcludeClipRect */
108     NULL,                            /* pExtDeviceMode */
109     MFDRV_ExtEscape,                 /* pExtEscape */
110     MFDRV_ExtFloodFill,              /* pExtFloodFill */
111     MFDRV_ExtSelectClipRgn,          /* pExtSelectClipRgn */
112     MFDRV_ExtTextOut,                /* pExtTextOut */
113     MFDRV_FillPath,                  /* pFillPath */
114     MFDRV_FillRgn,                   /* pFillRgn */
115     MFDRV_FlattenPath,               /* pFlattenPath */
116     MFDRV_FrameRgn,                  /* pFrameRgn */
117     NULL,                            /* pGdiComment */
118     NULL,                            /* pGetBitmapBits */
119     NULL,                            /* pGetCharWidth */
120     NULL,                            /* pGetDIBits */
121     MFDRV_GetDeviceCaps,             /* pGetDeviceCaps */
122     NULL,                            /* pGetDeviceGammaRamp */
123     NULL,                            /* pGetICMProfile */
124     NULL,                            /* pGetNearestColor */
125     NULL,                            /* pGetPixel */
126     NULL,                            /* pGetPixelFormat */
127     NULL,                            /* pGetSystemPaletteEntries */
128     NULL,                            /* pGetTextExtentExPoint */
129     NULL,                            /* pGetTextMetrics */
130     MFDRV_IntersectClipRect,         /* pIntersectClipRect */
131     MFDRV_InvertRgn,                 /* pInvertRgn */
132     MFDRV_LineTo,                    /* pLineTo */
133     NULL,                            /* pModifyWorldTransform */
134     MFDRV_MoveTo,                    /* pMoveTo */
135     MFDRV_OffsetClipRgn,             /* pOffsetClipRgn */
136     MFDRV_OffsetViewportOrgEx,       /* pOffsetViewportOrgEx */
137     MFDRV_OffsetWindowOrgEx,         /* pOffsetWindowOrgEx */
138     MFDRV_PaintRgn,                  /* pPaintRgn */
139     MFDRV_PatBlt,                    /* pPatBlt */
140     MFDRV_Pie,                       /* pPie */
141     MFDRV_PolyBezier,                /* pPolyBezier */
142     MFDRV_PolyBezierTo,              /* pPolyBezierTo */
143     NULL,                            /* pPolyDraw */
144     MFDRV_PolyPolygon,               /* pPolyPolygon */
145     NULL,                            /* pPolyPolyline */
146     MFDRV_Polygon,                   /* pPolygon */
147     MFDRV_Polyline,                  /* pPolyline */
148     NULL,                            /* pPolylineTo */
149     NULL,                            /* pRealizeDefaultPalette */
150     MFDRV_RealizePalette,            /* pRealizePalette */
151     MFDRV_Rectangle,                 /* pRectangle */
152     NULL,                            /* pResetDC */
153     MFDRV_RestoreDC,                 /* pRestoreDC */
154     MFDRV_RoundRect,                 /* pRoundRect */
155     MFDRV_SaveDC,                    /* pSaveDC */
156     MFDRV_ScaleViewportExtEx,        /* pScaleViewportExtEx */
157     MFDRV_ScaleWindowExtEx,          /* pScaleWindowExtEx */
158     MFDRV_SelectBitmap,              /* pSelectBitmap */
159     MFDRV_SelectBrush,               /* pSelectBrush */
160     MFDRV_SelectClipPath,            /* pSelectClipPath */
161     MFDRV_SelectFont,                /* pSelectFont */
162     MFDRV_SelectPalette,             /* pSelectPalette */
163     MFDRV_SelectPen,                 /* pSelectPen */
164     NULL,                            /* pSetArcDirection */
165     NULL,                            /* pSetBitmapBits */
166     MFDRV_SetBkColor,                /* pSetBkColor */
167     MFDRV_SetBkMode,                 /* pSetBkMode */
168     MFDRV_SetDCBrushColor,           /* pSetDCBrushColor*/
169     MFDRV_SetDCPenColor,             /* pSetDCPenColor*/
170     NULL,                            /* pSetDIBColorTable */
171     NULL,                            /* pSetDIBits */
172     MFDRV_SetDIBitsToDevice,         /* pSetDIBitsToDevice */
173     NULL,                            /* pSetDeviceClipping */
174     NULL,                            /* pSetDeviceGammaRamp */
175     NULL,                            /* pSetLayout */
176     MFDRV_SetMapMode,                /* pSetMapMode */
177     MFDRV_SetMapperFlags,            /* pSetMapperFlags */
178     MFDRV_SetPixel,                  /* pSetPixel */
179     NULL,                            /* pSetPixelFormat */
180     MFDRV_SetPolyFillMode,           /* pSetPolyFillMode */
181     MFDRV_SetROP2,                   /* pSetROP2 */
182     MFDRV_SetRelAbs,                 /* pSetRelAbs */
183     MFDRV_SetStretchBltMode,         /* pSetStretchBltMode */
184     MFDRV_SetTextAlign,              /* pSetTextAlign */
185     MFDRV_SetTextCharacterExtra,     /* pSetTextCharacterExtra */
186     MFDRV_SetTextColor,              /* pSetTextColor */
187     MFDRV_SetTextJustification,      /* pSetTextJustification */
188     MFDRV_SetViewportExtEx,          /* pSetViewportExtEx */
189     MFDRV_SetViewportOrgEx,          /* pSetViewportOrgEx */
190     MFDRV_SetWindowExtEx,            /* pSetWindowExtEx */
191     MFDRV_SetWindowOrgEx,            /* pSetWindowOrgEx */
192     NULL,                            /* pSetWorldTransform */
193     NULL,                            /* pStartDoc */
194     NULL,                            /* pStartPage */
195     MFDRV_StretchBlt,                /* pStretchBlt */
196     MFDRV_StretchDIBits,             /* pStretchDIBits */
197     MFDRV_StrokeAndFillPath,         /* pStrokeAndFillPath */
198     MFDRV_StrokePath,                /* pStrokePath */
199     NULL,                            /* pSwapBuffers */
200     NULL,                            /* pUnrealizePalette */
201     MFDRV_WidenPath                  /* pWidenPath */
202 };
203
204
205
206 /**********************************************************************
207  *           MFDRV_AllocMetaFile
208  */
209 static DC *MFDRV_AllocMetaFile(void)
210 {
211     DC *dc;
212     METAFILEDRV_PDEVICE *physDev;
213
214     if (!(dc = alloc_dc_ptr( OBJ_METADC ))) return NULL;
215
216     physDev = HeapAlloc(GetProcessHeap(),0,sizeof(*physDev));
217     if (!physDev)
218     {
219         free_dc_ptr( dc );
220         return NULL;
221     }
222     if (!(physDev->mh = HeapAlloc( GetProcessHeap(), 0, sizeof(*physDev->mh) )))
223     {
224         HeapFree( GetProcessHeap(), 0, physDev );
225         free_dc_ptr( dc );
226         return NULL;
227     }
228
229     push_dc_driver( dc, &physDev->dev, &MFDRV_Funcs );
230
231     physDev->handles = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, HANDLE_LIST_INC * sizeof(physDev->handles[0]));
232     physDev->handles_size = HANDLE_LIST_INC;
233     physDev->cur_handles = 0;
234
235     physDev->hFile = 0;
236
237     physDev->mh->mtHeaderSize   = sizeof(METAHEADER) / sizeof(WORD);
238     physDev->mh->mtVersion      = 0x0300;
239     physDev->mh->mtSize         = physDev->mh->mtHeaderSize;
240     physDev->mh->mtNoObjects    = 0;
241     physDev->mh->mtMaxRecord    = 0;
242     physDev->mh->mtNoParameters = 0;
243
244     SetVirtualResolution( physDev->dev.hdc, 0, 0, 0, 0);
245
246     return dc;
247 }
248
249
250 /**********************************************************************
251  *           MFDRV_DeleteDC
252  */
253 static BOOL CDECL MFDRV_DeleteDC( PHYSDEV dev )
254 {
255     METAFILEDRV_PDEVICE *physDev = (METAFILEDRV_PDEVICE *)dev;
256     DWORD index;
257
258     HeapFree( GetProcessHeap(), 0, physDev->mh );
259     for(index = 0; index < physDev->handles_size; index++)
260         if(physDev->handles[index])
261             GDI_hdc_not_using_object(physDev->handles[index], dev->hdc);
262     HeapFree( GetProcessHeap(), 0, physDev->handles );
263     HeapFree( GetProcessHeap(), 0, physDev );
264     return TRUE;
265 }
266
267
268 /**********************************************************************
269  *           CreateMetaFileW   (GDI32.@)
270  *
271  *  Create a new DC and associate it with a metafile. Pass a filename
272  *  to create a disk-based metafile, NULL to create a memory metafile.
273  *
274  * PARAMS
275  *  filename [I] Filename of disk metafile
276  *
277  * RETURNS
278  *  A handle to the metafile DC if successful, NULL on failure.
279  */
280 HDC WINAPI CreateMetaFileW( LPCWSTR filename )
281 {
282     HDC ret;
283     DC *dc;
284     METAFILEDRV_PDEVICE *physDev;
285     HANDLE hFile;
286
287     TRACE("%s\n", debugstr_w(filename) );
288
289     if (!(dc = MFDRV_AllocMetaFile())) return 0;
290     physDev = (METAFILEDRV_PDEVICE *)dc->physDev;
291
292     if (filename)  /* disk based metafile */
293     {
294         physDev->mh->mtType = METAFILE_DISK;
295         if ((hFile = CreateFileW(filename, GENERIC_WRITE, 0, NULL,
296                                 CREATE_ALWAYS, 0, 0)) == INVALID_HANDLE_VALUE) {
297             free_dc_ptr( dc );
298             return 0;
299         }
300         if (!WriteFile( hFile, physDev->mh, sizeof(*physDev->mh), NULL,
301                         NULL )) {
302             free_dc_ptr( dc );
303             return 0;
304         }
305         physDev->hFile = hFile;
306
307         /* Grow METAHEADER to include filename */
308         physDev->mh = MF_CreateMetaHeaderDisk(physDev->mh, filename, TRUE);
309     }
310     else  /* memory based metafile */
311         physDev->mh->mtType = METAFILE_MEMORY;
312
313     TRACE("returning %p\n", physDev->dev.hdc);
314     ret = physDev->dev.hdc;
315     release_dc_ptr( dc );
316     return ret;
317 }
318
319 /**********************************************************************
320  *          CreateMetaFileA   (GDI32.@)
321  *
322  * See CreateMetaFileW.
323  */
324 HDC WINAPI CreateMetaFileA(LPCSTR filename)
325 {
326     LPWSTR filenameW;
327     DWORD len;
328     HDC hReturnDC;
329
330     if (!filename) return CreateMetaFileW(NULL);
331
332     len = MultiByteToWideChar( CP_ACP, 0, filename, -1, NULL, 0 );
333     filenameW = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
334     MultiByteToWideChar( CP_ACP, 0, filename, -1, filenameW, len );
335
336     hReturnDC = CreateMetaFileW(filenameW);
337
338     HeapFree( GetProcessHeap(), 0, filenameW );
339
340     return hReturnDC;
341 }
342
343
344 /**********************************************************************
345  *          MFDRV_CloseMetaFile
346  */
347 static DC *MFDRV_CloseMetaFile( HDC hdc )
348 {
349     DC *dc;
350     METAFILEDRV_PDEVICE *physDev;
351
352     TRACE("(%p)\n", hdc );
353
354     if (!(dc = get_dc_ptr( hdc ))) return NULL;
355     if (dc->header.type != OBJ_METADC)
356     {
357         release_dc_ptr( dc );
358         return NULL;
359     }
360     if (dc->refcount != 1)
361     {
362         FIXME( "not deleting busy DC %p refcount %u\n", hdc, dc->refcount );
363         release_dc_ptr( dc );
364         return NULL;
365     }
366     physDev = (METAFILEDRV_PDEVICE *)dc->physDev;
367
368     /* Construct the end of metafile record - this is documented
369      * in SDK Knowledgebase Q99334.
370      */
371
372     if (!MFDRV_MetaParam0(dc->physDev, META_EOF))
373     {
374         free_dc_ptr( dc );
375         return 0;
376     }
377
378     if (physDev->mh->mtType == METAFILE_DISK)  /* disk based metafile */
379     {
380         if (SetFilePointer(physDev->hFile, 0, NULL, FILE_BEGIN) != 0) {
381             free_dc_ptr( dc );
382             return 0;
383         }
384
385         physDev->mh->mtType = METAFILE_MEMORY; /* This is what windows does */
386         if (!WriteFile(physDev->hFile, physDev->mh, sizeof(*physDev->mh),
387                        NULL, NULL)) {
388             free_dc_ptr( dc );
389             return 0;
390         }
391         CloseHandle(physDev->hFile);
392         physDev->mh->mtType = METAFILE_DISK;
393     }
394
395     return dc;
396 }
397
398 /******************************************************************
399  *           CloseMetaFile   (GDI32.@)
400  *
401  *  Stop recording graphics operations in metafile associated with
402  *  hdc and retrieve metafile.
403  *
404  * PARAMS
405  *  hdc [I] Metafile DC to close 
406  *
407  * RETURNS
408  *  Handle of newly created metafile on success, NULL on failure.
409  */
410 HMETAFILE WINAPI CloseMetaFile(HDC hdc)
411 {
412     HMETAFILE hmf;
413     METAFILEDRV_PDEVICE *physDev;
414     DC *dc = MFDRV_CloseMetaFile(hdc);
415     if (!dc) return 0;
416     physDev = (METAFILEDRV_PDEVICE *)dc->physDev;
417
418     /* Now allocate a global handle for the metafile */
419
420     hmf = MF_Create_HMETAFILE( physDev->mh );
421
422     physDev->mh = NULL;  /* So it won't be deleted */
423     free_dc_ptr( dc );
424     return hmf;
425 }
426
427
428 /******************************************************************
429  *         MFDRV_WriteRecord
430  *
431  * Warning: this function can change the pointer to the metafile header.
432  */
433 BOOL MFDRV_WriteRecord( PHYSDEV dev, METARECORD *mr, DWORD rlen)
434 {
435     DWORD len, size;
436     METAHEADER *mh;
437     METAFILEDRV_PDEVICE *physDev = (METAFILEDRV_PDEVICE *)dev;
438
439     switch(physDev->mh->mtType)
440     {
441     case METAFILE_MEMORY:
442         len = physDev->mh->mtSize * 2 + rlen;
443         /* reallocate memory if needed */
444         size = HeapSize( GetProcessHeap(), 0, physDev->mh );
445         if (len > size)
446         {
447             /*expand size*/
448             size += size / 2 + rlen;
449             mh = HeapReAlloc( GetProcessHeap(), 0, physDev->mh, size);
450             if (!mh) return FALSE;
451             physDev->mh = mh;
452             TRACE("Reallocated metafile: new size is %d\n",size);
453         }
454         memcpy((WORD *)physDev->mh + physDev->mh->mtSize, mr, rlen);
455         break;
456     case METAFILE_DISK:
457         TRACE("Writing record to disk\n");
458         if (!WriteFile(physDev->hFile, mr, rlen, NULL, NULL))
459             return FALSE;
460         break;
461     default:
462         ERR("Unknown metafile type %d\n", physDev->mh->mtType );
463         return FALSE;
464     }
465
466     physDev->mh->mtSize += rlen / 2;
467     physDev->mh->mtMaxRecord = max(physDev->mh->mtMaxRecord, rlen / 2);
468     return TRUE;
469 }
470
471
472 /******************************************************************
473  *         MFDRV_MetaParam0
474  */
475
476 BOOL MFDRV_MetaParam0(PHYSDEV dev, short func)
477 {
478     char buffer[8];
479     METARECORD *mr = (METARECORD *)&buffer;
480
481     mr->rdSize = 3;
482     mr->rdFunction = func;
483     return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
484 }
485
486
487 /******************************************************************
488  *         MFDRV_MetaParam1
489  */
490 BOOL MFDRV_MetaParam1(PHYSDEV dev, short func, short param1)
491 {
492     char buffer[8];
493     METARECORD *mr = (METARECORD *)&buffer;
494
495     mr->rdSize = 4;
496     mr->rdFunction = func;
497     *(mr->rdParm) = param1;
498     return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
499 }
500
501
502 /******************************************************************
503  *         MFDRV_MetaParam2
504  */
505 BOOL MFDRV_MetaParam2(PHYSDEV dev, short func, short param1, short param2)
506 {
507     char buffer[10];
508     METARECORD *mr = (METARECORD *)&buffer;
509
510     mr->rdSize = 5;
511     mr->rdFunction = func;
512     *(mr->rdParm) = param2;
513     *(mr->rdParm + 1) = param1;
514     return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
515 }
516
517
518 /******************************************************************
519  *         MFDRV_MetaParam4
520  */
521
522 BOOL MFDRV_MetaParam4(PHYSDEV dev, short func, short param1, short param2,
523                       short param3, short param4)
524 {
525     char buffer[14];
526     METARECORD *mr = (METARECORD *)&buffer;
527
528     mr->rdSize = 7;
529     mr->rdFunction = func;
530     *(mr->rdParm) = param4;
531     *(mr->rdParm + 1) = param3;
532     *(mr->rdParm + 2) = param2;
533     *(mr->rdParm + 3) = param1;
534     return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
535 }
536
537
538 /******************************************************************
539  *         MFDRV_MetaParam6
540  */
541
542 BOOL MFDRV_MetaParam6(PHYSDEV dev, short func, short param1, short param2,
543                       short param3, short param4, short param5, short param6)
544 {
545     char buffer[18];
546     METARECORD *mr = (METARECORD *)&buffer;
547
548     mr->rdSize = 9;
549     mr->rdFunction = func;
550     *(mr->rdParm) = param6;
551     *(mr->rdParm + 1) = param5;
552     *(mr->rdParm + 2) = param4;
553     *(mr->rdParm + 3) = param3;
554     *(mr->rdParm + 4) = param2;
555     *(mr->rdParm + 5) = param1;
556     return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
557 }
558
559
560 /******************************************************************
561  *         MFDRV_MetaParam8
562  */
563 BOOL MFDRV_MetaParam8(PHYSDEV dev, short func, short param1, short param2,
564                       short param3, short param4, short param5,
565                       short param6, short param7, short param8)
566 {
567     char buffer[22];
568     METARECORD *mr = (METARECORD *)&buffer;
569
570     mr->rdSize = 11;
571     mr->rdFunction = func;
572     *(mr->rdParm) = param8;
573     *(mr->rdParm + 1) = param7;
574     *(mr->rdParm + 2) = param6;
575     *(mr->rdParm + 3) = param5;
576     *(mr->rdParm + 4) = param4;
577     *(mr->rdParm + 5) = param3;
578     *(mr->rdParm + 6) = param2;
579     *(mr->rdParm + 7) = param1;
580     return MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
581 }