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