gdi32: Fix coordinates for row copies in mirrored vertical stretching.
[wine/multimedia.git] / dlls / wineps.drv / init.c
blobfa475738a671227b4f51f40ae5e6c6609181503c
1 /*
2 * PostScript driver initialization functions
4 * Copyright 1998 Huw D M Davies
5 * Copyright 2001 Marcus Meissner
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.
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.
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
22 #include "config.h"
23 #include "wine/port.h"
25 #include <stdarg.h>
26 #include <string.h>
27 #ifdef HAVE_UNISTD_H
28 # include <unistd.h>
29 #endif
30 #ifdef HAVE_CUPS_CUPS_H
31 # include <cups/cups.h>
32 #endif
34 #define NONAMELESSUNION
35 #define NONAMELESSSTRUCT
37 #include "windef.h"
38 #include "winbase.h"
39 #include "winerror.h"
40 #include "winreg.h"
41 #include "winnls.h"
42 #include "psdrv.h"
43 #include "winspool.h"
44 #include "wine/library.h"
45 #include "wine/debug.h"
47 WINE_DEFAULT_DEBUG_CHANNEL(psdrv);
49 #ifdef SONAME_LIBCUPS
50 static void *cupshandle = NULL;
51 #endif
53 static const PSDRV_DEVMODEA DefaultDevmode =
55 { /* dmPublic */
56 /* dmDeviceName */ "Wine PostScript Driver",
57 /* dmSpecVersion */ 0x30a,
58 /* dmDriverVersion */ 0x001,
59 /* dmSize */ sizeof(DEVMODEA),
60 /* dmDriverExtra */ sizeof(PSDRV_DEVMODEA)-sizeof(DEVMODEA),
61 /* dmFields */ DM_ORIENTATION | DM_PAPERSIZE | DM_SCALE |
62 DM_COPIES | DM_DEFAULTSOURCE | DM_COLOR |
63 DM_YRESOLUTION | DM_TTOPTION,
64 { /* u1 */
65 { /* s1 */
66 /* dmOrientation */ DMORIENT_PORTRAIT,
67 /* dmPaperSize */ DMPAPER_LETTER,
68 /* dmPaperLength */ 2794,
69 /* dmPaperWidth */ 2159,
70 /* dmScale */ 100, /* ?? */
71 /* dmCopies */ 1,
72 /* dmDefaultSource */ DMBIN_AUTO,
73 /* dmPrintQuality */ 0
76 /* dmColor */ DMCOLOR_COLOR,
77 /* dmDuplex */ DMDUP_SIMPLEX,
78 /* dmYResolution */ 0,
79 /* dmTTOption */ DMTT_SUBDEV,
80 /* dmCollate */ 0,
81 /* dmFormName */ "",
82 /* dmUnusedPadding */ 0,
83 /* dmBitsPerPel */ 0,
84 /* dmPelsWidth */ 0,
85 /* dmPelsHeight */ 0,
86 { /* u2 */
87 /* dmDisplayFlags */ 0
89 /* dmDisplayFrequency */ 0,
90 /* dmICMMethod */ 0,
91 /* dmICMIntent */ 0,
92 /* dmMediaType */ 0,
93 /* dmDitherType */ 0,
94 /* dmReserved1 */ 0,
95 /* dmReserved2 */ 0,
96 /* dmPanningWidth */ 0,
97 /* dmPanningHeight */ 0
99 { /* dmDocPrivate */
100 /* dummy */ 0
102 { /* dmDrvPrivate */
103 /* numInstalledOptions */ 0
107 HINSTANCE PSDRV_hInstance = 0;
108 HANDLE PSDRV_Heap = 0;
110 static HFONT PSDRV_DefaultFont = 0;
111 static const LOGFONTA DefaultLogFont = {
112 100, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, ANSI_CHARSET, 0, 0,
113 DEFAULT_QUALITY, FIXED_PITCH | FF_MODERN, ""
116 static const CHAR default_devmodeA[] = "Default DevMode";
117 static const struct gdi_dc_funcs psdrv_funcs;
119 /*********************************************************************
120 * DllMain
122 * Initializes font metrics and registers driver. wineps dll entry point.
125 BOOL WINAPI DllMain( HINSTANCE hinst, DWORD reason, LPVOID reserved )
127 TRACE("(%p, %d, %p)\n", hinst, reason, reserved);
129 switch(reason) {
131 case DLL_PROCESS_ATTACH:
132 PSDRV_hInstance = hinst;
133 DisableThreadLibraryCalls(hinst);
135 PSDRV_Heap = HeapCreate(0, 0x10000, 0);
136 if (PSDRV_Heap == NULL)
137 return FALSE;
139 if (PSDRV_GetFontMetrics() == FALSE) {
140 HeapDestroy(PSDRV_Heap);
141 return FALSE;
144 PSDRV_DefaultFont = CreateFontIndirectA(&DefaultLogFont);
145 if (PSDRV_DefaultFont == NULL) {
146 HeapDestroy(PSDRV_Heap);
147 return FALSE;
149 #ifdef SONAME_LIBCUPS
150 /* dynamically load CUPS if not yet loaded */
151 if (!cupshandle) {
152 cupshandle = wine_dlopen(SONAME_LIBCUPS, RTLD_NOW, NULL, 0);
153 if (!cupshandle) cupshandle = (void*)-1;
155 #endif
156 break;
158 case DLL_PROCESS_DETACH:
160 DeleteObject( PSDRV_DefaultFont );
161 HeapDestroy( PSDRV_Heap );
162 #ifdef SONAME_LIBCUPS
163 if (cupshandle && (cupshandle != (void*)-1)) {
164 wine_dlclose(cupshandle, NULL, 0);
165 cupshandle = NULL;
167 #endif
168 break;
171 return TRUE;
175 static void PSDRV_UpdateDevCaps( PSDRV_PDEVICE *physDev )
177 PAGESIZE *page;
178 INT width = 0, height = 0;
180 if(physDev->Devmode->dmPublic.dmFields & DM_PAPERSIZE) {
181 LIST_FOR_EACH_ENTRY(page, &physDev->pi->ppd->PageSizes, PAGESIZE, entry) {
182 if(page->WinPage == physDev->Devmode->dmPublic.u1.s1.dmPaperSize)
183 break;
186 if(&page->entry == &physDev->pi->ppd->PageSizes) {
187 FIXME("Can't find page\n");
188 physDev->ImageableArea.left = 0;
189 physDev->ImageableArea.right = 0;
190 physDev->ImageableArea.bottom = 0;
191 physDev->ImageableArea.top = 0;
192 physDev->PageSize.cx = 0;
193 physDev->PageSize.cy = 0;
194 } else if(page->ImageableArea) {
195 /* physDev sizes in device units; ppd sizes in 1/72" */
196 physDev->ImageableArea.left = page->ImageableArea->llx *
197 physDev->logPixelsX / 72;
198 physDev->ImageableArea.right = page->ImageableArea->urx *
199 physDev->logPixelsX / 72;
200 physDev->ImageableArea.bottom = page->ImageableArea->lly *
201 physDev->logPixelsY / 72;
202 physDev->ImageableArea.top = page->ImageableArea->ury *
203 physDev->logPixelsY / 72;
204 physDev->PageSize.cx = page->PaperDimension->x *
205 physDev->logPixelsX / 72;
206 physDev->PageSize.cy = page->PaperDimension->y *
207 physDev->logPixelsY / 72;
208 } else {
209 physDev->ImageableArea.left = physDev->ImageableArea.bottom = 0;
210 physDev->ImageableArea.right = physDev->PageSize.cx =
211 page->PaperDimension->x * physDev->logPixelsX / 72;
212 physDev->ImageableArea.top = physDev->PageSize.cy =
213 page->PaperDimension->y * physDev->logPixelsY / 72;
215 } else if((physDev->Devmode->dmPublic.dmFields & DM_PAPERLENGTH) &&
216 (physDev->Devmode->dmPublic.dmFields & DM_PAPERWIDTH)) {
217 /* physDev sizes in device units; Devmode sizes in 1/10 mm */
218 physDev->ImageableArea.left = physDev->ImageableArea.bottom = 0;
219 physDev->ImageableArea.right = physDev->PageSize.cx =
220 physDev->Devmode->dmPublic.u1.s1.dmPaperWidth *
221 physDev->logPixelsX / 254;
222 physDev->ImageableArea.top = physDev->PageSize.cy =
223 physDev->Devmode->dmPublic.u1.s1.dmPaperLength *
224 physDev->logPixelsY / 254;
225 } else {
226 FIXME("Odd dmFields %x\n", physDev->Devmode->dmPublic.dmFields);
227 physDev->ImageableArea.left = 0;
228 physDev->ImageableArea.right = 0;
229 physDev->ImageableArea.bottom = 0;
230 physDev->ImageableArea.top = 0;
231 physDev->PageSize.cx = 0;
232 physDev->PageSize.cy = 0;
235 TRACE("ImageableArea = %d,%d - %d,%d: PageSize = %dx%d\n",
236 physDev->ImageableArea.left, physDev->ImageableArea.bottom,
237 physDev->ImageableArea.right, physDev->ImageableArea.top,
238 physDev->PageSize.cx, physDev->PageSize.cy);
240 /* these are in device units */
241 width = physDev->ImageableArea.right - physDev->ImageableArea.left;
242 height = physDev->ImageableArea.top - physDev->ImageableArea.bottom;
244 if(physDev->Devmode->dmPublic.u1.s1.dmOrientation == DMORIENT_PORTRAIT) {
245 physDev->horzRes = width;
246 physDev->vertRes = height;
247 } else {
248 physDev->horzRes = height;
249 physDev->vertRes = width;
252 /* these are in mm */
253 physDev->horzSize = (physDev->horzRes * 25.4) / physDev->logPixelsX;
254 physDev->vertSize = (physDev->vertRes * 25.4) / physDev->logPixelsY;
256 TRACE("devcaps: horzSize = %dmm, vertSize = %dmm, "
257 "horzRes = %d, vertRes = %d\n",
258 physDev->horzSize, physDev->vertSize,
259 physDev->horzRes, physDev->vertRes);
263 /***********************************************************
264 * DEVMODEdupWtoA
266 * Creates an ascii copy of supplied devmode on heap
268 * Copied from dlls/winspool/info.c until full unicodification
270 static LPDEVMODEA DEVMODEdupWtoA(HANDLE heap, const DEVMODEW *dmW)
272 LPDEVMODEA dmA;
273 DWORD size;
274 BOOL Formname;
275 /* there is no pointer dereference here, if your code checking tool complains it's broken */
276 ptrdiff_t off_formname = (const char *)dmW->dmFormName - (const char *)dmW;
278 if(!dmW) return NULL;
279 Formname = (dmW->dmSize > off_formname);
280 size = dmW->dmSize - CCHDEVICENAME - (Formname ? CCHFORMNAME : 0);
281 dmA = HeapAlloc(heap, HEAP_ZERO_MEMORY, size + dmW->dmDriverExtra);
282 WideCharToMultiByte(CP_ACP, 0, dmW->dmDeviceName, -1, (LPSTR)dmA->dmDeviceName,
283 CCHDEVICENAME, NULL, NULL);
284 if(!Formname) {
285 memcpy(&dmA->dmSpecVersion, &dmW->dmSpecVersion,
286 dmW->dmSize - CCHDEVICENAME * sizeof(WCHAR));
287 } else {
288 memcpy(&dmA->dmSpecVersion, &dmW->dmSpecVersion,
289 off_formname - CCHDEVICENAME * sizeof(WCHAR));
290 WideCharToMultiByte(CP_ACP, 0, dmW->dmFormName, -1, (LPSTR)dmA->dmFormName,
291 CCHFORMNAME, NULL, NULL);
292 memcpy(&dmA->dmLogPixels, &dmW->dmLogPixels, dmW->dmSize -
293 (off_formname + CCHFORMNAME * sizeof(WCHAR)));
295 dmA->dmSize = size;
296 memcpy((char *)dmA + dmA->dmSize, (const char *)dmW + dmW->dmSize,
297 dmW->dmDriverExtra);
298 return dmA;
302 static PSDRV_PDEVICE *create_psdrv_physdev( PRINTERINFO *pi )
304 PSDRV_PDEVICE *physDev;
306 physDev = HeapAlloc( PSDRV_Heap, HEAP_ZERO_MEMORY, sizeof(*physDev) );
307 if (!physDev) return NULL;
309 physDev->Devmode = HeapAlloc( PSDRV_Heap, 0, sizeof(PSDRV_DEVMODEA) );
310 if (!physDev->Devmode)
312 HeapFree( PSDRV_Heap, 0, physDev );
313 return NULL;
316 *physDev->Devmode = *pi->Devmode;
317 physDev->pi = pi;
318 physDev->logPixelsX = pi->ppd->DefaultResolution;
319 physDev->logPixelsY = pi->ppd->DefaultResolution;
320 return physDev;
323 /**********************************************************************
324 * PSDRV_CreateDC
326 static BOOL PSDRV_CreateDC( PHYSDEV *pdev, LPCWSTR driver, LPCWSTR device,
327 LPCWSTR output, const DEVMODEW* initData )
329 PSDRV_PDEVICE *physDev;
330 PRINTERINFO *pi;
331 DWORD len;
332 char *deviceA;
334 TRACE("(%s %s %s %p)\n", debugstr_w(driver), debugstr_w(device),
335 debugstr_w(output), initData);
337 if (!device) return FALSE;
338 len = WideCharToMultiByte(CP_ACP, 0, device, -1, NULL, 0, NULL, NULL);
339 deviceA = HeapAlloc(GetProcessHeap(), 0, len);
340 WideCharToMultiByte(CP_ACP, 0, device, -1, deviceA, len, NULL, NULL);
341 pi = PSDRV_FindPrinterInfo(deviceA);
342 HeapFree(GetProcessHeap(), 0, deviceA);
343 if(!pi) return FALSE;
345 if(!pi->Fonts) {
346 RASTERIZER_STATUS status;
347 if(!GetRasterizerCaps(&status, sizeof(status)) ||
348 !(status.wFlags & TT_AVAILABLE) ||
349 !(status.wFlags & TT_ENABLED)) {
350 MESSAGE("Disabling printer %s since it has no builtin fonts and there are no TrueType fonts available.\n",
351 debugstr_w(device));
352 return FALSE;
356 if (!(physDev = create_psdrv_physdev( pi ))) return FALSE;
358 if (output && *output) {
359 INT len = WideCharToMultiByte( CP_ACP, 0, output, -1, NULL, 0, NULL, NULL );
360 if ((physDev->job.output = HeapAlloc( PSDRV_Heap, 0, len )))
361 WideCharToMultiByte( CP_ACP, 0, output, -1, physDev->job.output, len, NULL, NULL );
364 if(initData) {
365 DEVMODEA *devmodeA = DEVMODEdupWtoA(PSDRV_Heap, initData);
366 PSDRV_MergeDevmodes(physDev->Devmode, (PSDRV_DEVMODEA *)devmodeA, pi);
367 HeapFree(PSDRV_Heap, 0, devmodeA);
370 PSDRV_UpdateDevCaps(physDev);
371 SelectObject( (*pdev)->hdc, PSDRV_DefaultFont );
372 push_dc_driver( pdev, &physDev->dev, &psdrv_funcs );
373 return TRUE;
377 /**********************************************************************
378 * PSDRV_CreateCompatibleDC
380 static BOOL PSDRV_CreateCompatibleDC( PHYSDEV orig, PHYSDEV *pdev )
382 HDC hdc = (*pdev)->hdc;
383 PSDRV_PDEVICE *physDev, *orig_dev = get_psdrv_dev( orig );
384 PRINTERINFO *pi = PSDRV_FindPrinterInfo( orig_dev->pi->FriendlyName );
386 if (!pi) return FALSE;
387 if (!(physDev = create_psdrv_physdev( pi ))) return FALSE;
388 PSDRV_MergeDevmodes( physDev->Devmode, orig_dev->Devmode, pi );
389 PSDRV_UpdateDevCaps(physDev);
390 SelectObject( hdc, PSDRV_DefaultFont );
391 push_dc_driver( pdev, &physDev->dev, &psdrv_funcs );
392 return TRUE;
397 /**********************************************************************
398 * PSDRV_DeleteDC
400 static BOOL PSDRV_DeleteDC( PHYSDEV dev )
402 PSDRV_PDEVICE *physDev = get_psdrv_dev( dev );
404 TRACE("\n");
406 HeapFree( PSDRV_Heap, 0, physDev->Devmode );
407 HeapFree( PSDRV_Heap, 0, physDev->job.output );
408 HeapFree( PSDRV_Heap, 0, physDev );
410 return TRUE;
414 /**********************************************************************
415 * ResetDC (WINEPS.@)
417 static HDC PSDRV_ResetDC( PHYSDEV dev, const DEVMODEW *lpInitData )
419 PSDRV_PDEVICE *physDev = get_psdrv_dev( dev );
421 if(lpInitData) {
422 DEVMODEA *devmodeA = DEVMODEdupWtoA(PSDRV_Heap, lpInitData);
423 PSDRV_MergeDevmodes(physDev->Devmode, (PSDRV_DEVMODEA *)devmodeA, physDev->pi);
424 HeapFree(PSDRV_Heap, 0, devmodeA);
425 PSDRV_UpdateDevCaps(physDev);
427 return dev->hdc;
430 /***********************************************************************
431 * GetDeviceCaps (WINEPS.@)
433 static INT PSDRV_GetDeviceCaps( PHYSDEV dev, INT cap )
435 PSDRV_PDEVICE *physDev = get_psdrv_dev( dev );
437 switch(cap)
439 case DRIVERVERSION:
440 return 0;
441 case TECHNOLOGY:
442 return DT_RASPRINTER;
443 case HORZSIZE:
444 return MulDiv(physDev->horzSize, 100,
445 physDev->Devmode->dmPublic.u1.s1.dmScale);
446 case VERTSIZE:
447 return MulDiv(physDev->vertSize, 100,
448 physDev->Devmode->dmPublic.u1.s1.dmScale);
449 case HORZRES:
450 case DESKTOPHORZRES:
451 return physDev->horzRes;
452 case VERTRES:
453 case DESKTOPVERTRES:
454 return physDev->vertRes;
455 case BITSPIXEL:
456 return (physDev->pi->ppd->ColorDevice != CD_False) ? 32 : 1;
457 case PLANES:
458 return 1;
459 case NUMBRUSHES:
460 return -1;
461 case NUMPENS:
462 return 10;
463 case NUMMARKERS:
464 return 0;
465 case NUMFONTS:
466 return 39;
467 case NUMCOLORS:
468 return -1;
469 case PDEVICESIZE:
470 return sizeof(PSDRV_PDEVICE);
471 case CURVECAPS:
472 return (CC_CIRCLES | CC_PIE | CC_CHORD | CC_ELLIPSES | CC_WIDE |
473 CC_STYLED | CC_WIDESTYLED | CC_INTERIORS | CC_ROUNDRECT);
474 case LINECAPS:
475 return (LC_POLYLINE | LC_MARKER | LC_POLYMARKER | LC_WIDE |
476 LC_STYLED | LC_WIDESTYLED | LC_INTERIORS);
477 case POLYGONALCAPS:
478 return (PC_POLYGON | PC_RECTANGLE | PC_WINDPOLYGON | PC_SCANLINE |
479 PC_WIDE | PC_STYLED | PC_WIDESTYLED | PC_INTERIORS);
480 case TEXTCAPS:
481 return TC_CR_ANY | TC_VA_ABLE; /* psdrv 0x59f7 */
482 case CLIPCAPS:
483 return CP_RECTANGLE;
484 case RASTERCAPS:
485 return (RC_BITBLT | RC_BITMAP64 | RC_GDI20_OUTPUT | RC_DIBTODEV |
486 RC_STRETCHBLT | RC_STRETCHDIB); /* psdrv 0x6e99 */
487 /* Are aspect[XY] and logPixels[XY] correct? */
488 /* Need to handle different res in x and y => fix ppd */
489 case ASPECTX:
490 case ASPECTY:
491 return physDev->pi->ppd->DefaultResolution;
492 case ASPECTXY:
493 return (int)hypot( (double)physDev->pi->ppd->DefaultResolution,
494 (double)physDev->pi->ppd->DefaultResolution );
495 case LOGPIXELSX:
496 return MulDiv(physDev->logPixelsX,
497 physDev->Devmode->dmPublic.u1.s1.dmScale, 100);
498 case LOGPIXELSY:
499 return MulDiv(physDev->logPixelsY,
500 physDev->Devmode->dmPublic.u1.s1.dmScale, 100);
501 case SIZEPALETTE:
502 return 0;
503 case NUMRESERVED:
504 return 0;
505 case COLORRES:
506 return 0;
507 case PHYSICALWIDTH:
508 return (physDev->Devmode->dmPublic.u1.s1.dmOrientation == DMORIENT_LANDSCAPE) ?
509 physDev->PageSize.cy : physDev->PageSize.cx;
510 case PHYSICALHEIGHT:
511 return (physDev->Devmode->dmPublic.u1.s1.dmOrientation == DMORIENT_LANDSCAPE) ?
512 physDev->PageSize.cx : physDev->PageSize.cy;
513 case PHYSICALOFFSETX:
514 if(physDev->Devmode->dmPublic.u1.s1.dmOrientation == DMORIENT_LANDSCAPE) {
515 if(physDev->pi->ppd->LandscapeOrientation == -90)
516 return physDev->PageSize.cy - physDev->ImageableArea.top;
517 else
518 return physDev->ImageableArea.bottom;
520 return physDev->ImageableArea.left;
522 case PHYSICALOFFSETY:
523 if(physDev->Devmode->dmPublic.u1.s1.dmOrientation == DMORIENT_LANDSCAPE) {
524 if(physDev->pi->ppd->LandscapeOrientation == -90)
525 return physDev->PageSize.cx - physDev->ImageableArea.right;
526 else
527 return physDev->ImageableArea.left;
529 return physDev->PageSize.cy - physDev->ImageableArea.top;
531 case SCALINGFACTORX:
532 case SCALINGFACTORY:
533 case VREFRESH:
534 case BLTALIGNMENT:
535 return 0;
536 case SHADEBLENDCAPS:
537 return SB_NONE;
538 default:
539 FIXME("(%p): unsupported capability %d, will return 0\n", dev->hdc, cap );
540 return 0;
545 /**********************************************************************
546 * PSDRV_FindPrinterInfo
548 PRINTERINFO *PSDRV_FindPrinterInfo(LPCSTR name)
550 static PRINTERINFO *PSDRV_PrinterList;
551 DWORD type = REG_BINARY, needed, res, dwPaperSize;
552 PRINTERINFO *pi = PSDRV_PrinterList, **last = &PSDRV_PrinterList;
553 FONTNAME *font;
554 const AFM *afm;
555 HANDLE hPrinter = 0;
556 const char *ppd = NULL;
557 DWORD ppdType;
558 char* ppdFileName = NULL;
559 HKEY hkey;
560 BOOL using_default_devmode = FALSE;
562 TRACE("'%s'\n", name);
565 * If this loop completes, last will point to the 'next' element of the
566 * final PRINTERINFO in the list
568 for( ; pi; last = &pi->next, pi = pi->next)
569 if(!strcmp(pi->FriendlyName, name))
570 return pi;
572 pi = *last = HeapAlloc( PSDRV_Heap, HEAP_ZERO_MEMORY, sizeof(*pi) );
573 if (pi == NULL)
574 return NULL;
576 if (!(pi->FriendlyName = HeapAlloc( PSDRV_Heap, 0, strlen(name)+1 ))) goto fail;
577 strcpy( pi->FriendlyName, name );
579 if (OpenPrinterA (pi->FriendlyName, &hPrinter, NULL) == 0) {
580 ERR ("OpenPrinterA failed with code %i\n", GetLastError ());
581 goto cleanup;
584 needed = 0;
585 res = GetPrinterDataExA(hPrinter, NULL, default_devmodeA, &type, NULL, 0, &needed);
587 if (needed < sizeof(DefaultDevmode)) {
588 pi->Devmode = HeapAlloc( PSDRV_Heap, 0, sizeof(DefaultDevmode) );
589 if (pi->Devmode == NULL)
590 goto closeprinter;
592 *pi->Devmode = DefaultDevmode;
593 lstrcpynA((LPSTR)pi->Devmode->dmPublic.dmDeviceName,name,CCHDEVICENAME);
594 using_default_devmode = TRUE;
596 else {
597 pi->Devmode = HeapAlloc( PSDRV_Heap, 0, needed );
598 if (pi->Devmode == NULL)
599 goto closeprinter;
601 GetPrinterDataExA(hPrinter, NULL, default_devmodeA, &type, (LPBYTE)pi->Devmode, needed, &needed);
606 #ifdef SONAME_LIBCUPS
607 if (cupshandle != (void*)-1) {
608 typeof(cupsGetPPD) * pcupsGetPPD = NULL;
610 pcupsGetPPD = wine_dlsym(cupshandle, "cupsGetPPD", NULL, 0);
611 if (pcupsGetPPD) {
612 ppd = pcupsGetPPD(name);
614 if (ppd) {
615 needed=strlen(ppd)+1;
616 ppdFileName=HeapAlloc(PSDRV_Heap, 0, needed);
617 memcpy(ppdFileName, ppd, needed);
618 ppdType=REG_SZ;
619 res = ERROR_SUCCESS;
620 /* we should unlink() that file later */
621 } else {
622 res = ERROR_FILE_NOT_FOUND;
623 WARN("Did not find ppd for %s\n",name);
627 #endif
628 if (!ppdFileName) {
629 res = GetPrinterDataExA(hPrinter, "PrinterDriverData", "PPD File", NULL, NULL, 0, &needed);
630 if ((res==ERROR_SUCCESS) || (res==ERROR_MORE_DATA)) {
631 ppdFileName=HeapAlloc(PSDRV_Heap, 0, needed);
632 res = GetPrinterDataExA(hPrinter, "PrinterDriverData", "PPD File", &ppdType,
633 (LPBYTE)ppdFileName, needed, &needed);
636 /* Look for a ppd file for this printer in the config file.
637 * First look under that printer's name, and then under 'generic'
639 /* @@ Wine registry key: HKCU\Software\Wine\Printing\PPD Files */
640 if((res != ERROR_SUCCESS) && !RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Printing\\PPD Files", &hkey))
642 const char* value_name;
644 if (RegQueryValueExA(hkey, name, 0, NULL, NULL, &needed) == ERROR_SUCCESS) {
645 value_name=name;
646 } else if (RegQueryValueExA(hkey, "generic", 0, NULL, NULL, &needed) == ERROR_SUCCESS) {
647 value_name="generic";
648 } else {
649 value_name=NULL;
651 if (value_name) {
652 HeapFree(PSDRV_Heap, 0, ppdFileName);
653 ppdFileName=HeapAlloc(PSDRV_Heap, 0, needed);
654 RegQueryValueExA(hkey, value_name, 0, &ppdType, (LPBYTE)ppdFileName, &needed);
656 RegCloseKey(hkey);
659 if (!ppdFileName)
661 const char *data_dir, *filename;
663 if ((data_dir = wine_get_data_dir())) filename = "/generic.ppd";
664 else if ((data_dir = wine_get_build_dir())) filename = "/dlls/wineps.drv/generic.ppd";
665 else
667 res = ERROR_FILE_NOT_FOUND;
668 ERR ("Error %i getting PPD file name for printer '%s'\n", res, name);
669 goto closeprinter;
671 ppdFileName = HeapAlloc( PSDRV_Heap, 0, strlen(data_dir) + strlen(filename) + 1 );
672 strcpy( ppdFileName, data_dir );
673 strcat( ppdFileName, filename );
674 } else {
675 res = ERROR_SUCCESS;
676 if (ppdType==REG_EXPAND_SZ) {
677 char* tmp;
679 /* Expand environment variable references */
680 needed=ExpandEnvironmentStringsA(ppdFileName,NULL,0);
681 tmp=HeapAlloc(PSDRV_Heap, 0, needed);
682 ExpandEnvironmentStringsA(ppdFileName,tmp,needed);
683 HeapFree(PSDRV_Heap, 0, ppdFileName);
684 ppdFileName=tmp;
688 pi->ppd = PSDRV_ParsePPD(ppdFileName);
689 if(!pi->ppd) {
690 MESSAGE("Couldn't find PPD file '%s', expect a crash now!\n",
691 ppdFileName);
692 goto closeprinter;
695 /* Some gimp-print ppd files don't contain a DefaultResolution line
696 set it to 300 if it's not specified */
697 if(pi->ppd->DefaultResolution == 0)
698 pi->ppd->DefaultResolution = 300;
700 if(using_default_devmode) {
701 DWORD papersize;
703 if(GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_IPAPERSIZE | LOCALE_RETURN_NUMBER,
704 (LPWSTR)&papersize, sizeof(papersize)/sizeof(WCHAR))) {
705 PSDRV_DEVMODEA dm;
706 memset(&dm, 0, sizeof(dm));
707 dm.dmPublic.dmFields = DM_PAPERSIZE;
708 dm.dmPublic.u1.s1.dmPaperSize = papersize;
709 PSDRV_MergeDevmodes(pi->Devmode, &dm, pi);
712 SetPrinterDataExA(hPrinter, NULL, default_devmodeA, REG_BINARY,
713 (LPBYTE)pi->Devmode, sizeof(DefaultDevmode));
716 if(pi->ppd->DefaultPageSize) { /* We'll let the ppd override the devmode */
717 PSDRV_DEVMODEA dm;
718 memset(&dm, 0, sizeof(dm));
719 dm.dmPublic.dmFields = DM_PAPERSIZE;
720 dm.dmPublic.u1.s1.dmPaperSize = pi->ppd->DefaultPageSize->WinPage;
721 PSDRV_MergeDevmodes(pi->Devmode, &dm, pi);
725 * This is a hack. The default paper size should be read in as part of
726 * the Devmode structure, but Wine doesn't currently provide a convenient
727 * way to configure printers.
729 res = GetPrinterDataExA(hPrinter, "PrinterDriverData", "Paper Size", NULL,
730 (LPBYTE)&dwPaperSize, sizeof(DWORD), &needed);
731 if (res == ERROR_SUCCESS)
732 pi->Devmode->dmPublic.u1.s1.dmPaperSize = (SHORT) dwPaperSize;
733 else if (res == ERROR_FILE_NOT_FOUND)
734 TRACE ("No 'Paper Size' for printer '%s'\n", name);
735 else {
736 ERR ("GetPrinterDataA returned %i\n", res);
737 goto closeprinter;
740 /* Duplex is indicated by the setting of the DM_DUPLEX bit in dmFields.
741 WinDuplex == 0 is a special case which means that the ppd has a
742 *DefaultDuplex: NotCapable entry. In this case we'll try not to confuse
743 apps and set dmDuplex to DMDUP_SIMPLEX but leave the DM_DUPLEX clear.
744 PSDRV_WriteHeader understands this and copes. */
745 pi->Devmode->dmPublic.dmFields &= ~DM_DUPLEX;
746 if(pi->ppd->DefaultDuplex) {
747 pi->Devmode->dmPublic.dmDuplex = pi->ppd->DefaultDuplex->WinDuplex;
748 if(pi->Devmode->dmPublic.dmDuplex != 0)
749 pi->Devmode->dmPublic.dmFields |= DM_DUPLEX;
750 else
751 pi->Devmode->dmPublic.dmDuplex = DMDUP_SIMPLEX;
754 res = EnumPrinterDataExA (hPrinter, "PrinterDriverData\\FontSubTable", NULL,
755 0, &needed, &pi->FontSubTableSize);
756 if (res == ERROR_SUCCESS || res == ERROR_FILE_NOT_FOUND) {
757 TRACE ("No 'FontSubTable' for printer '%s'\n", name);
759 else if (res == ERROR_MORE_DATA) {
760 pi->FontSubTable = HeapAlloc (PSDRV_Heap, 0, needed);
761 if (pi->FontSubTable == NULL) {
762 ERR ("Failed to allocate %i bytes from heap\n", needed);
763 goto closeprinter;
766 res = EnumPrinterDataExA (hPrinter, "PrinterDriverData\\FontSubTable",
767 (LPBYTE) pi->FontSubTable, needed, &needed,
768 &pi->FontSubTableSize);
769 if (res != ERROR_SUCCESS) {
770 ERR ("EnumPrinterDataExA returned %i\n", res);
771 goto closeprinter;
774 else {
775 ERR("EnumPrinterDataExA returned %i\n", res);
776 goto closeprinter;
779 if (ClosePrinter (hPrinter) == 0) {
780 ERR ("ClosePrinter failed with code %i\n", GetLastError ());
781 goto cleanup;
784 pi->next = NULL;
785 pi->Fonts = NULL;
787 for(font = pi->ppd->InstalledFonts; font; font = font->next) {
788 afm = PSDRV_FindAFMinList(PSDRV_AFMFontList, font->Name);
789 if(!afm) {
790 TRACE( "Couldn't find AFM file for installed printer font '%s' - "
791 "ignoring\n", font->Name);
793 else {
794 BOOL added;
795 if (PSDRV_AddAFMtoList(&pi->Fonts, afm, &added) == FALSE) {
796 PSDRV_FreeAFMList(pi->Fonts);
797 goto cleanup;
802 if (ppd) unlink(ppd);
803 return pi;
805 closeprinter:
806 ClosePrinter(hPrinter);
807 cleanup:
808 HeapFree(PSDRV_Heap, 0, ppdFileName);
809 HeapFree(PSDRV_Heap, 0, pi->FontSubTable);
810 HeapFree(PSDRV_Heap, 0, pi->FriendlyName);
811 HeapFree(PSDRV_Heap, 0, pi->Devmode);
812 fail:
813 HeapFree(PSDRV_Heap, 0, pi);
814 if (ppd) unlink(ppd);
815 *last = NULL;
816 return NULL;
820 static const struct gdi_dc_funcs psdrv_funcs =
822 NULL, /* pAbortDoc */
823 NULL, /* pAbortPath */
824 NULL, /* pAlphaBlend */
825 NULL, /* pAngleArc */
826 PSDRV_Arc, /* pArc */
827 NULL, /* pArcTo */
828 NULL, /* pBeginPath */
829 NULL, /* pBlendImage */
830 NULL, /* pChoosePixelFormat */
831 PSDRV_Chord, /* pChord */
832 NULL, /* pCloseFigure */
833 NULL, /* pCopyBitmap */
834 NULL, /* pCreateBitmap */
835 PSDRV_CreateCompatibleDC, /* pCreateCompatibleDC */
836 PSDRV_CreateDC, /* pCreateDC */
837 NULL, /* pDeleteBitmap */
838 PSDRV_DeleteDC, /* pDeleteDC */
839 NULL, /* pDeleteObject */
840 NULL, /* pDescribePixelFormat */
841 PSDRV_DeviceCapabilities, /* pDeviceCapabilities */
842 PSDRV_Ellipse, /* pEllipse */
843 PSDRV_EndDoc, /* pEndDoc */
844 PSDRV_EndPage, /* pEndPage */
845 NULL, /* pEndPath */
846 PSDRV_EnumFonts, /* pEnumFonts */
847 NULL, /* pEnumICMProfiles */
848 NULL, /* pExcludeClipRect */
849 PSDRV_ExtDeviceMode, /* pExtDeviceMode */
850 PSDRV_ExtEscape, /* pExtEscape */
851 NULL, /* pExtFloodFill */
852 NULL, /* pExtSelectClipRgn */
853 PSDRV_ExtTextOut, /* pExtTextOut */
854 PSDRV_FillPath, /* pFillPath */
855 NULL, /* pFillRgn */
856 NULL, /* pFlattenPath */
857 NULL, /* pFontIsLinked */
858 NULL, /* pFrameRgn */
859 NULL, /* pGdiComment */
860 NULL, /* pGdiRealizationInfo */
861 NULL, /* pGetCharABCWidths */
862 NULL, /* pGetCharABCWidthsI */
863 PSDRV_GetCharWidth, /* pGetCharWidth */
864 PSDRV_GetDeviceCaps, /* pGetDeviceCaps */
865 NULL, /* pGetDeviceGammaRamp */
866 NULL, /* pGetFontData */
867 NULL, /* pGetFontUnicodeRanges */
868 NULL, /* pGetGlyphIndices */
869 NULL, /* pGetGlyphOutline */
870 NULL, /* pGetICMProfile */
871 NULL, /* pGetImage */
872 NULL, /* pGetKerningPairs */
873 NULL, /* pGetNearestColor */
874 NULL, /* pGetOutlineTextMetrics */
875 NULL, /* pGetPixel */
876 NULL, /* pGetPixelFormat */
877 NULL, /* pGetSystemPaletteEntries */
878 NULL, /* pGetTextCharsetInfo */
879 PSDRV_GetTextExtentExPoint, /* pGetTextExtentExPoint */
880 NULL, /* pGetTextExtentExPointI */
881 NULL, /* pGetTextFace */
882 PSDRV_GetTextMetrics, /* pGetTextMetrics */
883 NULL, /* pGradientFill */
884 NULL, /* pIntersectClipRect */
885 NULL, /* pInvertRgn */
886 PSDRV_LineTo, /* pLineTo */
887 NULL, /* pModifyWorldTransform */
888 NULL, /* pMoveTo */
889 NULL, /* pOffsetClipRgn */
890 NULL, /* pOffsetViewportOrg */
891 NULL, /* pOffsetWindowOrg */
892 PSDRV_PaintRgn, /* pPaintRgn */
893 PSDRV_PatBlt, /* pPatBlt */
894 PSDRV_Pie, /* pPie */
895 PSDRV_PolyBezier, /* pPolyBezier */
896 PSDRV_PolyBezierTo, /* pPolyBezierTo */
897 NULL, /* pPolyDraw */
898 PSDRV_PolyPolygon, /* pPolyPolygon */
899 PSDRV_PolyPolyline, /* pPolyPolyline */
900 NULL, /* pPolygon */
901 NULL, /* pPolyline */
902 NULL, /* pPolylineTo */
903 PSDRV_PutImage, /* pPutImage */
904 NULL, /* pRealizeDefaultPalette */
905 NULL, /* pRealizePalette */
906 PSDRV_Rectangle, /* pRectangle */
907 PSDRV_ResetDC, /* pResetDC */
908 NULL, /* pRestoreDC */
909 PSDRV_RoundRect, /* pRoundRect */
910 NULL, /* pSaveDC */
911 NULL, /* pScaleViewportExt */
912 NULL, /* pScaleWindowExt */
913 NULL, /* pSelectBitmap */
914 PSDRV_SelectBrush, /* pSelectBrush */
915 NULL, /* pSelectClipPath */
916 PSDRV_SelectFont, /* pSelectFont */
917 NULL, /* pSelectPalette */
918 PSDRV_SelectPen, /* pSelectPen */
919 NULL, /* pSetArcDirection */
920 PSDRV_SetBkColor, /* pSetBkColor */
921 NULL, /* pSetBkMode */
922 PSDRV_SetDCBrushColor, /* pSetDCBrushColor */
923 PSDRV_SetDCPenColor, /* pSetDCPenColor */
924 NULL, /* pSetDIBitsToDevice */
925 NULL, /* pSetDeviceClipping */
926 NULL, /* pSetDeviceGammaRamp */
927 NULL, /* pSetLayout */
928 NULL, /* pSetMapMode */
929 NULL, /* pSetMapperFlags */
930 PSDRV_SetPixel, /* pSetPixel */
931 NULL, /* pSetPixelFormat */
932 NULL, /* pSetPolyFillMode */
933 NULL, /* pSetROP2 */
934 NULL, /* pSetRelAbs */
935 NULL, /* pSetStretchBltMode */
936 NULL, /* pSetTextAlign */
937 NULL, /* pSetTextCharacterExtra */
938 PSDRV_SetTextColor, /* pSetTextColor */
939 NULL, /* pSetTextJustification */
940 NULL, /* pSetViewportExt */
941 NULL, /* pSetViewportOrg */
942 NULL, /* pSetWindowExt */
943 NULL, /* pSetWindowOrg */
944 NULL, /* pSetWorldTransform */
945 PSDRV_StartDoc, /* pStartDoc */
946 PSDRV_StartPage, /* pStartPage */
947 NULL, /* pStretchBlt */
948 NULL, /* pStretchDIBits */
949 PSDRV_StrokeAndFillPath, /* pStrokeAndFillPath */
950 PSDRV_StrokePath, /* pStrokePath */
951 NULL, /* pSwapBuffers */
952 NULL, /* pUnrealizePalette */
953 NULL, /* pWidenPath */
954 /* OpenGL not supported */
958 /******************************************************************************
959 * PSDRV_get_gdi_driver
961 const struct gdi_dc_funcs * CDECL PSDRV_get_gdi_driver( unsigned int version )
963 if (version != WINE_GDI_DRIVER_VERSION)
965 ERR( "version mismatch, gdi32 wants %u but wineps has %u\n", version, WINE_GDI_DRIVER_VERSION );
966 return NULL;
968 return &psdrv_funcs;