rasapi32: Added some more tests for RasEnumDevicesA, fix Wine not to crash on the...
[wine/multimedia.git] / dlls / wineps.drv / init.c
blob61e31d8778eef75440781eac88f35001a9647056
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 "psdrv.h"
42 #include "winspool.h"
43 #include "wine/debug.h"
45 WINE_DEFAULT_DEBUG_CHANNEL(psdrv);
47 #ifdef SONAME_LIBCUPS
48 static void *cupshandle = NULL;
49 #endif
51 static const PSDRV_DEVMODEA DefaultDevmode =
53 { /* dmPublic */
54 /* dmDeviceName */ "Wine PostScript Driver",
55 /* dmSpecVersion */ 0x30a,
56 /* dmDriverVersion */ 0x001,
57 /* dmSize */ sizeof(DEVMODEA),
58 /* dmDriverExtra */ sizeof(PSDRV_DEVMODEA)-sizeof(DEVMODEA),
59 /* dmFields */ DM_ORIENTATION | DM_PAPERSIZE | DM_SCALE |
60 DM_COPIES | DM_DEFAULTSOURCE | DM_COLOR |
61 DM_YRESOLUTION | DM_TTOPTION,
62 { /* u1 */
63 { /* s1 */
64 /* dmOrientation */ DMORIENT_PORTRAIT,
65 /* dmPaperSize */ DMPAPER_LETTER,
66 /* dmPaperLength */ 2794,
67 /* dmPaperWidth */ 2159,
68 /* dmScale */ 100, /* ?? */
69 /* dmCopies */ 1,
70 /* dmDefaultSource */ DMBIN_AUTO,
71 /* dmPrintQuality */ 0
74 /* dmColor */ DMCOLOR_COLOR,
75 /* dmDuplex */ DMDUP_SIMPLEX,
76 /* dmYResolution */ 0,
77 /* dmTTOption */ DMTT_SUBDEV,
78 /* dmCollate */ 0,
79 /* dmFormName */ "",
80 /* dmUnusedPadding */ 0,
81 /* dmBitsPerPel */ 0,
82 /* dmPelsWidth */ 0,
83 /* dmPelsHeight */ 0,
84 { /* u2 */
85 /* dmDisplayFlags */ 0
87 /* dmDisplayFrequency */ 0,
88 /* dmICMMethod */ 0,
89 /* dmICMIntent */ 0,
90 /* dmMediaType */ 0,
91 /* dmDitherType */ 0,
92 /* dmReserved1 */ 0,
93 /* dmReserved2 */ 0,
94 /* dmPanningWidth */ 0,
95 /* dmPanningHeight */ 0
97 { /* dmDocPrivate */
98 /* dummy */ 0
100 { /* dmDrvPrivate */
101 /* numInstalledOptions */ 0
105 HINSTANCE PSDRV_hInstance = 0;
106 HANDLE PSDRV_Heap = 0;
108 static HFONT PSDRV_DefaultFont = 0;
109 static const LOGFONTA DefaultLogFont = {
110 100, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, ANSI_CHARSET, 0, 0,
111 DEFAULT_QUALITY, FIXED_PITCH | FF_MODERN, ""
114 /*********************************************************************
115 * DllMain
117 * Initializes font metrics and registers driver. wineps dll entry point.
120 BOOL WINAPI DllMain( HINSTANCE hinst, DWORD reason, LPVOID reserved )
122 TRACE("(%p, %d, %p)\n", hinst, reason, reserved);
124 switch(reason) {
126 case DLL_PROCESS_ATTACH:
127 PSDRV_hInstance = hinst;
128 DisableThreadLibraryCalls(hinst);
130 PSDRV_Heap = HeapCreate(0, 0x10000, 0);
131 if (PSDRV_Heap == NULL)
132 return FALSE;
134 if (PSDRV_GetFontMetrics() == FALSE) {
135 HeapDestroy(PSDRV_Heap);
136 return FALSE;
139 PSDRV_DefaultFont = CreateFontIndirectA(&DefaultLogFont);
140 if (PSDRV_DefaultFont == NULL) {
141 HeapDestroy(PSDRV_Heap);
142 return FALSE;
144 #ifdef SONAME_LIBCUPS
145 /* dynamically load CUPS if not yet loaded */
146 if (!cupshandle) {
147 cupshandle = wine_dlopen(SONAME_LIBCUPS, RTLD_NOW, NULL, 0);
148 if (!cupshandle) cupshandle = (void*)-1;
150 #endif
151 break;
153 case DLL_PROCESS_DETACH:
155 DeleteObject( PSDRV_DefaultFont );
156 HeapDestroy( PSDRV_Heap );
157 #ifdef SONAME_LIBCUPS
158 if (cupshandle && (cupshandle != (void*)-1)) {
159 wine_dlclose(cupshandle, NULL, 0);
160 cupshandle = NULL;
162 #endif
163 break;
166 return TRUE;
170 static void PSDRV_UpdateDevCaps( PSDRV_PDEVICE *physDev )
172 PAGESIZE *page;
173 INT width = 0, height = 0;
175 if(physDev->Devmode->dmPublic.dmFields & DM_PAPERSIZE) {
176 LIST_FOR_EACH_ENTRY(page, &physDev->pi->ppd->PageSizes, PAGESIZE, entry) {
177 if(page->WinPage == physDev->Devmode->dmPublic.u1.s1.dmPaperSize)
178 break;
181 if(&page->entry == &physDev->pi->ppd->PageSizes) {
182 FIXME("Can't find page\n");
183 physDev->ImageableArea.left = 0;
184 physDev->ImageableArea.right = 0;
185 physDev->ImageableArea.bottom = 0;
186 physDev->ImageableArea.top = 0;
187 physDev->PageSize.cx = 0;
188 physDev->PageSize.cy = 0;
189 } else if(page->ImageableArea) {
190 /* physDev sizes in device units; ppd sizes in 1/72" */
191 physDev->ImageableArea.left = page->ImageableArea->llx *
192 physDev->logPixelsX / 72;
193 physDev->ImageableArea.right = page->ImageableArea->urx *
194 physDev->logPixelsX / 72;
195 physDev->ImageableArea.bottom = page->ImageableArea->lly *
196 physDev->logPixelsY / 72;
197 physDev->ImageableArea.top = page->ImageableArea->ury *
198 physDev->logPixelsY / 72;
199 physDev->PageSize.cx = page->PaperDimension->x *
200 physDev->logPixelsX / 72;
201 physDev->PageSize.cy = page->PaperDimension->y *
202 physDev->logPixelsY / 72;
203 } else {
204 physDev->ImageableArea.left = physDev->ImageableArea.bottom = 0;
205 physDev->ImageableArea.right = physDev->PageSize.cx =
206 page->PaperDimension->x * physDev->logPixelsX / 72;
207 physDev->ImageableArea.top = physDev->PageSize.cy =
208 page->PaperDimension->y * physDev->logPixelsY / 72;
210 } else if((physDev->Devmode->dmPublic.dmFields & DM_PAPERLENGTH) &&
211 (physDev->Devmode->dmPublic.dmFields & DM_PAPERWIDTH)) {
212 /* physDev sizes in device units; Devmode sizes in 1/10 mm */
213 physDev->ImageableArea.left = physDev->ImageableArea.bottom = 0;
214 physDev->ImageableArea.right = physDev->PageSize.cx =
215 physDev->Devmode->dmPublic.u1.s1.dmPaperWidth *
216 physDev->logPixelsX / 254;
217 physDev->ImageableArea.top = physDev->PageSize.cy =
218 physDev->Devmode->dmPublic.u1.s1.dmPaperLength *
219 physDev->logPixelsY / 254;
220 } else {
221 FIXME("Odd dmFields %x\n", physDev->Devmode->dmPublic.dmFields);
222 physDev->ImageableArea.left = 0;
223 physDev->ImageableArea.right = 0;
224 physDev->ImageableArea.bottom = 0;
225 physDev->ImageableArea.top = 0;
226 physDev->PageSize.cx = 0;
227 physDev->PageSize.cy = 0;
230 TRACE("ImageableArea = %d,%d - %d,%d: PageSize = %dx%d\n",
231 physDev->ImageableArea.left, physDev->ImageableArea.bottom,
232 physDev->ImageableArea.right, physDev->ImageableArea.top,
233 physDev->PageSize.cx, physDev->PageSize.cy);
235 /* these are in device units */
236 width = physDev->ImageableArea.right - physDev->ImageableArea.left;
237 height = physDev->ImageableArea.top - physDev->ImageableArea.bottom;
239 if(physDev->Devmode->dmPublic.u1.s1.dmOrientation == DMORIENT_PORTRAIT) {
240 physDev->horzRes = width;
241 physDev->vertRes = height;
242 } else {
243 physDev->horzRes = height;
244 physDev->vertRes = width;
247 /* these are in mm */
248 physDev->horzSize = (physDev->horzRes * 25.4) / physDev->logPixelsX;
249 physDev->vertSize = (physDev->vertRes * 25.4) / physDev->logPixelsY;
251 TRACE("devcaps: horzSize = %dmm, vertSize = %dmm, "
252 "horzRes = %d, vertRes = %d\n",
253 physDev->horzSize, physDev->vertSize,
254 physDev->horzRes, physDev->vertRes);
258 /***********************************************************
259 * DEVMODEdupWtoA
261 * Creates an ascii copy of supplied devmode on heap
263 * Copied from dlls/winspool/info.c until full unicodification
265 static LPDEVMODEA DEVMODEdupWtoA(HANDLE heap, const DEVMODEW *dmW)
267 LPDEVMODEA dmA;
268 DWORD size;
269 BOOL Formname;
270 ptrdiff_t off_formname = (const char *)dmW->dmFormName - (const char *)dmW;
272 if(!dmW) return NULL;
273 Formname = (dmW->dmSize > off_formname);
274 size = dmW->dmSize - CCHDEVICENAME - (Formname ? CCHFORMNAME : 0);
275 dmA = HeapAlloc(heap, HEAP_ZERO_MEMORY, size + dmW->dmDriverExtra);
276 WideCharToMultiByte(CP_ACP, 0, dmW->dmDeviceName, -1, (LPSTR)dmA->dmDeviceName,
277 CCHDEVICENAME, NULL, NULL);
278 if(!Formname) {
279 memcpy(&dmA->dmSpecVersion, &dmW->dmSpecVersion,
280 dmW->dmSize - CCHDEVICENAME * sizeof(WCHAR));
281 } else {
282 memcpy(&dmA->dmSpecVersion, &dmW->dmSpecVersion,
283 off_formname - CCHDEVICENAME * sizeof(WCHAR));
284 WideCharToMultiByte(CP_ACP, 0, dmW->dmFormName, -1, (LPSTR)dmA->dmFormName,
285 CCHFORMNAME, NULL, NULL);
286 memcpy(&dmA->dmLogPixels, &dmW->dmLogPixels, dmW->dmSize -
287 (off_formname + CCHFORMNAME * sizeof(WCHAR)));
289 dmA->dmSize = size;
290 memcpy((char *)dmA + dmA->dmSize, (const char *)dmW + dmW->dmSize,
291 dmW->dmDriverExtra);
292 return dmA;
296 /**********************************************************************
297 * PSDRV_CreateDC
299 BOOL PSDRV_CreateDC( HDC hdc, PSDRV_PDEVICE **pdev, LPCWSTR driver, LPCWSTR device,
300 LPCWSTR output, const DEVMODEW* initData )
302 PSDRV_PDEVICE *physDev;
303 PRINTERINFO *pi;
304 char *deviceA;
306 /* If no device name was specified, retrieve the device name
307 * from the DEVMODE structure from the DC's physDev.
308 * (See CreateCompatibleDC) */
309 if ( !device && *pdev )
311 physDev = *pdev;
312 deviceA = HeapAlloc(GetProcessHeap(), 0, CCHDEVICENAME);
313 lstrcpynA(deviceA, (LPCSTR)physDev->Devmode->dmPublic.dmDeviceName, CCHDEVICENAME);
315 else
317 DWORD len = WideCharToMultiByte(CP_ACP, 0, device, -1, NULL, 0, NULL, NULL);
318 deviceA = HeapAlloc(GetProcessHeap(), 0, len);
319 WideCharToMultiByte(CP_ACP, 0, device, -1, deviceA, len, NULL, NULL);
321 pi = PSDRV_FindPrinterInfo(deviceA);
323 HeapFree(GetProcessHeap(), 0, deviceA);
324 deviceA = NULL;
326 TRACE("(%s %s %s %p)\n", debugstr_w(driver), debugstr_w(device),
327 debugstr_w(output), initData);
329 if(!pi) return FALSE;
331 if(!pi->Fonts) {
332 RASTERIZER_STATUS status;
333 if(!GetRasterizerCaps(&status, sizeof(status)) ||
334 !(status.wFlags & TT_AVAILABLE) ||
335 !(status.wFlags & TT_ENABLED)) {
336 MESSAGE("Disabling printer %s since it has no builtin fonts and there are no TrueType fonts available.\n",
337 debugstr_w(device));
338 return FALSE;
342 physDev = HeapAlloc( PSDRV_Heap, HEAP_ZERO_MEMORY, sizeof(*physDev) );
343 if (!physDev) return FALSE;
344 *pdev = physDev;
345 physDev->hdc = hdc;
347 physDev->pi = pi;
349 physDev->Devmode = HeapAlloc( PSDRV_Heap, 0, sizeof(PSDRV_DEVMODEA) );
350 if(!physDev->Devmode) {
351 HeapFree( PSDRV_Heap, 0, physDev );
352 return FALSE;
355 *physDev->Devmode = *pi->Devmode;
357 physDev->logPixelsX = physDev->pi->ppd->DefaultResolution;
358 physDev->logPixelsY = physDev->pi->ppd->DefaultResolution;
360 if (output && *output) {
361 INT len = WideCharToMultiByte( CP_ACP, 0, output, -1, NULL, 0, NULL, NULL );
362 if ((physDev->job.output = HeapAlloc( PSDRV_Heap, 0, len )))
363 WideCharToMultiByte( CP_ACP, 0, output, -1, physDev->job.output, len, NULL, NULL );
364 } else
365 physDev->job.output = NULL;
366 physDev->job.hJob = 0;
368 if(initData) {
369 DEVMODEA *devmodeA = DEVMODEdupWtoA(PSDRV_Heap, initData);
370 PSDRV_MergeDevmodes(physDev->Devmode, (PSDRV_DEVMODEA *)devmodeA, pi);
371 HeapFree(PSDRV_Heap, 0, devmodeA);
374 PSDRV_UpdateDevCaps(physDev);
375 SelectObject( hdc, PSDRV_DefaultFont );
376 return TRUE;
381 /**********************************************************************
382 * PSDRV_DeleteDC
384 BOOL PSDRV_DeleteDC( PSDRV_PDEVICE *physDev )
386 TRACE("\n");
388 HeapFree( PSDRV_Heap, 0, physDev->Devmode );
389 HeapFree( PSDRV_Heap, 0, physDev->job.output );
390 HeapFree( PSDRV_Heap, 0, physDev );
392 return TRUE;
396 /**********************************************************************
397 * ResetDC (WINEPS.@)
399 HDC PSDRV_ResetDC( PSDRV_PDEVICE *physDev, const DEVMODEW *lpInitData )
401 if(lpInitData) {
402 HRGN hrgn;
403 DEVMODEA *devmodeA = DEVMODEdupWtoA(PSDRV_Heap, lpInitData);
404 PSDRV_MergeDevmodes(physDev->Devmode, (PSDRV_DEVMODEA *)devmodeA, physDev->pi);
405 HeapFree(PSDRV_Heap, 0, devmodeA);
406 PSDRV_UpdateDevCaps(physDev);
407 hrgn = CreateRectRgn(0, 0, physDev->horzRes, physDev->vertRes);
408 SelectVisRgn( physDev->hdc, hrgn );
409 DeleteObject(hrgn);
411 return physDev->hdc;
414 /***********************************************************************
415 * GetDeviceCaps (WINEPS.@)
417 INT PSDRV_GetDeviceCaps( PSDRV_PDEVICE *physDev, INT cap )
419 switch(cap)
421 case DRIVERVERSION:
422 return 0;
423 case TECHNOLOGY:
424 return DT_RASPRINTER;
425 case HORZSIZE:
426 return MulDiv(physDev->horzSize, 100,
427 physDev->Devmode->dmPublic.u1.s1.dmScale);
428 case VERTSIZE:
429 return MulDiv(physDev->vertSize, 100,
430 physDev->Devmode->dmPublic.u1.s1.dmScale);
431 case HORZRES:
432 case DESKTOPHORZRES:
433 return physDev->horzRes;
434 case VERTRES:
435 case DESKTOPVERTRES:
436 return physDev->vertRes;
437 case BITSPIXEL:
438 return (physDev->pi->ppd->ColorDevice != CD_False) ? 8 : 1;
439 case PLANES:
440 return 1;
441 case NUMBRUSHES:
442 return -1;
443 case NUMPENS:
444 return 10;
445 case NUMMARKERS:
446 return 0;
447 case NUMFONTS:
448 return 39;
449 case NUMCOLORS:
450 return (physDev->pi->ppd->ColorDevice != CD_False) ? 256 : -1;
451 case PDEVICESIZE:
452 return sizeof(PSDRV_PDEVICE);
453 case CURVECAPS:
454 return (CC_CIRCLES | CC_PIE | CC_CHORD | CC_ELLIPSES | CC_WIDE |
455 CC_STYLED | CC_WIDESTYLED | CC_INTERIORS | CC_ROUNDRECT);
456 case LINECAPS:
457 return (LC_POLYLINE | LC_MARKER | LC_POLYMARKER | LC_WIDE |
458 LC_STYLED | LC_WIDESTYLED | LC_INTERIORS);
459 case POLYGONALCAPS:
460 return (PC_POLYGON | PC_RECTANGLE | PC_WINDPOLYGON | PC_SCANLINE |
461 PC_WIDE | PC_STYLED | PC_WIDESTYLED | PC_INTERIORS);
462 case TEXTCAPS:
463 return TC_CR_ANY | TC_VA_ABLE; /* psdrv 0x59f7 */
464 case CLIPCAPS:
465 return CP_RECTANGLE;
466 case RASTERCAPS:
467 return (RC_BITBLT | RC_BITMAP64 | RC_GDI20_OUTPUT | RC_DIBTODEV |
468 RC_STRETCHBLT | RC_STRETCHDIB); /* psdrv 0x6e99 */
469 /* Are aspect[XY] and logPixels[XY] correct? */
470 /* Need to handle different res in x and y => fix ppd */
471 case ASPECTX:
472 case ASPECTY:
473 return physDev->pi->ppd->DefaultResolution;
474 case ASPECTXY:
475 return (int)hypot( (double)physDev->pi->ppd->DefaultResolution,
476 (double)physDev->pi->ppd->DefaultResolution );
477 case LOGPIXELSX:
478 return MulDiv(physDev->logPixelsX,
479 physDev->Devmode->dmPublic.u1.s1.dmScale, 100);
480 case LOGPIXELSY:
481 return MulDiv(physDev->logPixelsY,
482 physDev->Devmode->dmPublic.u1.s1.dmScale, 100);
483 case SIZEPALETTE:
484 return 0;
485 case NUMRESERVED:
486 return 0;
487 case COLORRES:
488 return 0;
489 case PHYSICALWIDTH:
490 return (physDev->Devmode->dmPublic.u1.s1.dmOrientation == DMORIENT_LANDSCAPE) ?
491 physDev->PageSize.cy : physDev->PageSize.cx;
492 case PHYSICALHEIGHT:
493 return (physDev->Devmode->dmPublic.u1.s1.dmOrientation == DMORIENT_LANDSCAPE) ?
494 physDev->PageSize.cx : physDev->PageSize.cy;
495 case PHYSICALOFFSETX:
496 if(physDev->Devmode->dmPublic.u1.s1.dmOrientation == DMORIENT_LANDSCAPE) {
497 if(physDev->pi->ppd->LandscapeOrientation == -90)
498 return physDev->PageSize.cy - physDev->ImageableArea.top;
499 else
500 return physDev->ImageableArea.bottom;
502 return physDev->ImageableArea.left;
504 case PHYSICALOFFSETY:
505 if(physDev->Devmode->dmPublic.u1.s1.dmOrientation == DMORIENT_LANDSCAPE) {
506 if(physDev->pi->ppd->LandscapeOrientation == -90)
507 return physDev->PageSize.cx - physDev->ImageableArea.right;
508 else
509 return physDev->ImageableArea.left;
511 return physDev->PageSize.cy - physDev->ImageableArea.top;
513 case SCALINGFACTORX:
514 case SCALINGFACTORY:
515 case VREFRESH:
516 case BLTALIGNMENT:
517 return 0;
518 default:
519 FIXME("(%p): unsupported capability %d, will return 0\n", physDev->hdc, cap );
520 return 0;
525 /**********************************************************************
526 * PSDRV_FindPrinterInfo
528 PRINTERINFO *PSDRV_FindPrinterInfo(LPCSTR name)
530 static PRINTERINFO *PSDRV_PrinterList;
531 DWORD type = REG_BINARY, needed, res, dwPaperSize;
532 PRINTERINFO *pi = PSDRV_PrinterList, **last = &PSDRV_PrinterList;
533 FONTNAME *font;
534 const AFM *afm;
535 HANDLE hPrinter;
536 const char *ppd = NULL;
537 DWORD ppdType;
538 char* ppdFileName = NULL;
539 HKEY hkey;
540 BOOL using_default_devmode = FALSE;
542 TRACE("'%s'\n", name);
545 * If this loop completes, last will point to the 'next' element of the
546 * final PRINTERINFO in the list
548 for( ; pi; last = &pi->next, pi = pi->next)
549 if(!strcmp(pi->FriendlyName, name))
550 return pi;
552 pi = *last = HeapAlloc( PSDRV_Heap, HEAP_ZERO_MEMORY, sizeof(*pi) );
553 if (pi == NULL)
554 return NULL;
556 if (!(pi->FriendlyName = HeapAlloc( PSDRV_Heap, 0, strlen(name)+1 ))) goto fail;
557 strcpy( pi->FriendlyName, name );
559 /* Use Get|SetPrinterDataExA instead? */
561 res = DrvGetPrinterData16((LPSTR)name, (LPSTR)INT_PD_DEFAULT_DEVMODE, &type,
562 NULL, 0, &needed );
564 if(res == ERROR_INVALID_PRINTER_NAME || needed != sizeof(DefaultDevmode)) {
565 pi->Devmode = HeapAlloc( PSDRV_Heap, 0, sizeof(DefaultDevmode) );
566 if (pi->Devmode == NULL)
567 goto cleanup;
568 *pi->Devmode = DefaultDevmode;
569 lstrcpynA((LPSTR)pi->Devmode->dmPublic.dmDeviceName,name,CCHDEVICENAME);
570 using_default_devmode = TRUE;
572 /* need to do something here AddPrinter?? */
574 else {
575 pi->Devmode = HeapAlloc( PSDRV_Heap, 0, needed );
576 DrvGetPrinterData16((LPSTR)name, (LPSTR)INT_PD_DEFAULT_DEVMODE, &type,
577 (LPBYTE)pi->Devmode, needed, &needed);
580 if (OpenPrinterA (pi->FriendlyName, &hPrinter, NULL) == 0) {
581 ERR ("OpenPrinterA failed with code %i\n", GetLastError ());
582 goto cleanup;
585 #ifdef SONAME_LIBCUPS
586 if (cupshandle != (void*)-1) {
587 typeof(cupsGetPPD) * pcupsGetPPD = NULL;
589 pcupsGetPPD = wine_dlsym(cupshandle, "cupsGetPPD", NULL, 0);
590 if (pcupsGetPPD) {
591 ppd = pcupsGetPPD(name);
593 if (ppd) {
594 needed=strlen(ppd)+1;
595 ppdFileName=HeapAlloc(PSDRV_Heap, 0, needed);
596 memcpy(ppdFileName, ppd, needed);
597 ppdType=REG_SZ;
598 res = ERROR_SUCCESS;
599 /* we should unlink() that file later */
600 } else {
601 res = ERROR_FILE_NOT_FOUND;
602 WARN("Did not find ppd for %s\n",name);
606 #endif
607 if (!ppdFileName) {
608 res = GetPrinterDataExA(hPrinter, "PrinterDriverData", "PPD File", NULL, NULL, 0, &needed);
609 if ((res==ERROR_SUCCESS) || (res==ERROR_MORE_DATA)) {
610 ppdFileName=HeapAlloc(PSDRV_Heap, 0, needed);
611 res = GetPrinterDataExA(hPrinter, "PrinterDriverData", "PPD File", &ppdType,
612 (LPBYTE)ppdFileName, needed, &needed);
615 /* Look for a ppd file for this printer in the config file.
616 * First look under that printer's name, and then under 'generic'
618 /* @@ Wine registry key: HKCU\Software\Wine\Printing\PPD Files */
619 if((res != ERROR_SUCCESS) && !RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Printing\\PPD Files", &hkey))
621 const char* value_name;
623 if (RegQueryValueExA(hkey, name, 0, NULL, NULL, &needed) == ERROR_SUCCESS) {
624 value_name=name;
625 } else if (RegQueryValueExA(hkey, "generic", 0, NULL, NULL, &needed) == ERROR_SUCCESS) {
626 value_name="generic";
627 } else {
628 value_name=NULL;
630 if (value_name) {
631 HeapFree(PSDRV_Heap, 0, ppdFileName);
632 ppdFileName=HeapAlloc(PSDRV_Heap, 0, needed);
633 RegQueryValueExA(hkey, value_name, 0, &ppdType, (LPBYTE)ppdFileName, &needed);
635 RegCloseKey(hkey);
638 if (!ppdFileName)
640 const char *data_dir, *filename;
642 if ((data_dir = wine_get_data_dir())) filename = "/generic.ppd";
643 else if ((data_dir = wine_get_build_dir())) filename = "/dlls/wineps.drv/generic.ppd";
644 else
646 res = ERROR_FILE_NOT_FOUND;
647 ERR ("Error %i getting PPD file name for printer '%s'\n", res, name);
648 goto closeprinter;
650 ppdFileName = HeapAlloc( PSDRV_Heap, 0, strlen(data_dir) + strlen(filename) + 1 );
651 strcpy( ppdFileName, data_dir );
652 strcat( ppdFileName, filename );
653 } else {
654 res = ERROR_SUCCESS;
655 if (ppdType==REG_EXPAND_SZ) {
656 char* tmp;
658 /* Expand environment variable references */
659 needed=ExpandEnvironmentStringsA(ppdFileName,NULL,0);
660 tmp=HeapAlloc(PSDRV_Heap, 0, needed);
661 ExpandEnvironmentStringsA(ppdFileName,tmp,needed);
662 HeapFree(PSDRV_Heap, 0, ppdFileName);
663 ppdFileName=tmp;
667 pi->ppd = PSDRV_ParsePPD(ppdFileName);
668 if(!pi->ppd) {
669 MESSAGE("Couldn't find PPD file '%s', expect a crash now!\n",
670 ppdFileName);
671 goto closeprinter;
674 /* Some gimp-print ppd files don't contain a DefaultResolution line
675 set it to 300 if it's not specified */
676 if(pi->ppd->DefaultResolution == 0)
677 pi->ppd->DefaultResolution = 300;
679 if(using_default_devmode) {
680 DWORD papersize;
682 if(GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_IPAPERSIZE | LOCALE_RETURN_NUMBER,
683 (LPWSTR)&papersize, sizeof(papersize)/sizeof(WCHAR))) {
684 PSDRV_DEVMODEA dm;
685 memset(&dm, 0, sizeof(dm));
686 dm.dmPublic.dmFields = DM_PAPERSIZE;
687 dm.dmPublic.u1.s1.dmPaperSize = papersize;
688 PSDRV_MergeDevmodes(pi->Devmode, &dm, pi);
690 DrvSetPrinterData16((LPSTR)name, (LPSTR)INT_PD_DEFAULT_DEVMODE,
691 REG_BINARY, (LPBYTE)pi->Devmode, sizeof(DefaultDevmode) );
694 if(pi->ppd->DefaultPageSize) { /* We'll let the ppd override the devmode */
695 PSDRV_DEVMODEA dm;
696 memset(&dm, 0, sizeof(dm));
697 dm.dmPublic.dmFields = DM_PAPERSIZE;
698 dm.dmPublic.u1.s1.dmPaperSize = pi->ppd->DefaultPageSize->WinPage;
699 PSDRV_MergeDevmodes(pi->Devmode, &dm, pi);
703 * This is a hack. The default paper size should be read in as part of
704 * the Devmode structure, but Wine doesn't currently provide a convenient
705 * way to configure printers.
707 res = GetPrinterDataExA(hPrinter, "PrinterDriverData", "Paper Size", NULL,
708 (LPBYTE)&dwPaperSize, sizeof(DWORD), &needed);
709 if (res == ERROR_SUCCESS)
710 pi->Devmode->dmPublic.u1.s1.dmPaperSize = (SHORT) dwPaperSize;
711 else if (res == ERROR_FILE_NOT_FOUND)
712 TRACE ("No 'Paper Size' for printer '%s'\n", name);
713 else {
714 ERR ("GetPrinterDataA returned %i\n", res);
715 goto closeprinter;
718 /* Duplex is indicated by the setting of the DM_DUPLEX bit in dmFields.
719 WinDuplex == 0 is a special case which means that the ppd has a
720 *DefaultDuplex: NotCapable entry. In this case we'll try not to confuse
721 apps and set dmDuplex to DMDUP_SIMPLEX but leave the DM_DUPLEX clear.
722 PSDRV_WriteHeader understands this and copes. */
723 pi->Devmode->dmPublic.dmFields &= ~DM_DUPLEX;
724 if(pi->ppd->DefaultDuplex) {
725 pi->Devmode->dmPublic.dmDuplex = pi->ppd->DefaultDuplex->WinDuplex;
726 if(pi->Devmode->dmPublic.dmDuplex != 0)
727 pi->Devmode->dmPublic.dmFields |= DM_DUPLEX;
728 else
729 pi->Devmode->dmPublic.dmDuplex = DMDUP_SIMPLEX;
732 res = EnumPrinterDataExA (hPrinter, "PrinterDriverData\\FontSubTable", NULL,
733 0, &needed, &pi->FontSubTableSize);
734 if (res == ERROR_SUCCESS || res == ERROR_FILE_NOT_FOUND) {
735 TRACE ("No 'FontSubTable' for printer '%s'\n", name);
737 else if (res == ERROR_MORE_DATA) {
738 pi->FontSubTable = HeapAlloc (PSDRV_Heap, 0, needed);
739 if (pi->FontSubTable == NULL) {
740 ERR ("Failed to allocate %i bytes from heap\n", needed);
741 goto closeprinter;
744 res = EnumPrinterDataExA (hPrinter, "PrinterDriverData\\FontSubTable",
745 (LPBYTE) pi->FontSubTable, needed, &needed,
746 &pi->FontSubTableSize);
747 if (res != ERROR_SUCCESS) {
748 ERR ("EnumPrinterDataExA returned %i\n", res);
749 goto closeprinter;
752 else {
753 ERR("EnumPrinterDataExA returned %i\n", res);
754 goto closeprinter;
757 if (ClosePrinter (hPrinter) == 0) {
758 ERR ("ClosePrinter failed with code %i\n", GetLastError ());
759 goto cleanup;
762 pi->next = NULL;
763 pi->Fonts = NULL;
765 for(font = pi->ppd->InstalledFonts; font; font = font->next) {
766 afm = PSDRV_FindAFMinList(PSDRV_AFMFontList, font->Name);
767 if(!afm) {
768 TRACE( "Couldn't find AFM file for installed printer font '%s' - "
769 "ignoring\n", font->Name);
771 else {
772 BOOL added;
773 if (PSDRV_AddAFMtoList(&pi->Fonts, afm, &added) == FALSE) {
774 PSDRV_FreeAFMList(pi->Fonts);
775 goto cleanup;
780 if (ppd) unlink(ppd);
781 return pi;
783 closeprinter:
784 ClosePrinter(hPrinter);
785 cleanup:
786 HeapFree(PSDRV_Heap, 0, ppdFileName);
787 HeapFree(PSDRV_Heap, 0, pi->FontSubTable);
788 HeapFree(PSDRV_Heap, 0, pi->FriendlyName);
789 HeapFree(PSDRV_Heap, 0, pi->Devmode);
790 fail:
791 HeapFree(PSDRV_Heap, 0, pi);
792 if (ppd) unlink(ppd);
793 *last = NULL;
794 return NULL;