server: Fix connect failures on newer kernels.
[wine.git] / dlls / wineps.drv / init.c
blobe67ffe111a2f43048f9b05fc1dd63f8ca977338e
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";
118 /*********************************************************************
119 * DllMain
121 * Initializes font metrics and registers driver. wineps dll entry point.
124 BOOL WINAPI DllMain( HINSTANCE hinst, DWORD reason, LPVOID reserved )
126 TRACE("(%p, %d, %p)\n", hinst, reason, reserved);
128 switch(reason) {
130 case DLL_PROCESS_ATTACH:
131 PSDRV_hInstance = hinst;
132 DisableThreadLibraryCalls(hinst);
134 PSDRV_Heap = HeapCreate(0, 0x10000, 0);
135 if (PSDRV_Heap == NULL)
136 return FALSE;
138 if (PSDRV_GetFontMetrics() == FALSE) {
139 HeapDestroy(PSDRV_Heap);
140 return FALSE;
143 PSDRV_DefaultFont = CreateFontIndirectA(&DefaultLogFont);
144 if (PSDRV_DefaultFont == NULL) {
145 HeapDestroy(PSDRV_Heap);
146 return FALSE;
148 #ifdef SONAME_LIBCUPS
149 /* dynamically load CUPS if not yet loaded */
150 if (!cupshandle) {
151 cupshandle = wine_dlopen(SONAME_LIBCUPS, RTLD_NOW, NULL, 0);
152 if (!cupshandle) cupshandle = (void*)-1;
154 #endif
155 break;
157 case DLL_PROCESS_DETACH:
159 DeleteObject( PSDRV_DefaultFont );
160 HeapDestroy( PSDRV_Heap );
161 #ifdef SONAME_LIBCUPS
162 if (cupshandle && (cupshandle != (void*)-1)) {
163 wine_dlclose(cupshandle, NULL, 0);
164 cupshandle = NULL;
166 #endif
167 break;
170 return TRUE;
174 static void PSDRV_UpdateDevCaps( PSDRV_PDEVICE *physDev )
176 PAGESIZE *page;
177 INT width = 0, height = 0;
179 if(physDev->Devmode->dmPublic.dmFields & DM_PAPERSIZE) {
180 LIST_FOR_EACH_ENTRY(page, &physDev->pi->ppd->PageSizes, PAGESIZE, entry) {
181 if(page->WinPage == physDev->Devmode->dmPublic.u1.s1.dmPaperSize)
182 break;
185 if(&page->entry == &physDev->pi->ppd->PageSizes) {
186 FIXME("Can't find page\n");
187 physDev->ImageableArea.left = 0;
188 physDev->ImageableArea.right = 0;
189 physDev->ImageableArea.bottom = 0;
190 physDev->ImageableArea.top = 0;
191 physDev->PageSize.cx = 0;
192 physDev->PageSize.cy = 0;
193 } else if(page->ImageableArea) {
194 /* physDev sizes in device units; ppd sizes in 1/72" */
195 physDev->ImageableArea.left = page->ImageableArea->llx *
196 physDev->logPixelsX / 72;
197 physDev->ImageableArea.right = page->ImageableArea->urx *
198 physDev->logPixelsX / 72;
199 physDev->ImageableArea.bottom = page->ImageableArea->lly *
200 physDev->logPixelsY / 72;
201 physDev->ImageableArea.top = page->ImageableArea->ury *
202 physDev->logPixelsY / 72;
203 physDev->PageSize.cx = page->PaperDimension->x *
204 physDev->logPixelsX / 72;
205 physDev->PageSize.cy = page->PaperDimension->y *
206 physDev->logPixelsY / 72;
207 } else {
208 physDev->ImageableArea.left = physDev->ImageableArea.bottom = 0;
209 physDev->ImageableArea.right = physDev->PageSize.cx =
210 page->PaperDimension->x * physDev->logPixelsX / 72;
211 physDev->ImageableArea.top = physDev->PageSize.cy =
212 page->PaperDimension->y * physDev->logPixelsY / 72;
214 } else if((physDev->Devmode->dmPublic.dmFields & DM_PAPERLENGTH) &&
215 (physDev->Devmode->dmPublic.dmFields & DM_PAPERWIDTH)) {
216 /* physDev sizes in device units; Devmode sizes in 1/10 mm */
217 physDev->ImageableArea.left = physDev->ImageableArea.bottom = 0;
218 physDev->ImageableArea.right = physDev->PageSize.cx =
219 physDev->Devmode->dmPublic.u1.s1.dmPaperWidth *
220 physDev->logPixelsX / 254;
221 physDev->ImageableArea.top = physDev->PageSize.cy =
222 physDev->Devmode->dmPublic.u1.s1.dmPaperLength *
223 physDev->logPixelsY / 254;
224 } else {
225 FIXME("Odd dmFields %x\n", physDev->Devmode->dmPublic.dmFields);
226 physDev->ImageableArea.left = 0;
227 physDev->ImageableArea.right = 0;
228 physDev->ImageableArea.bottom = 0;
229 physDev->ImageableArea.top = 0;
230 physDev->PageSize.cx = 0;
231 physDev->PageSize.cy = 0;
234 TRACE("ImageableArea = %d,%d - %d,%d: PageSize = %dx%d\n",
235 physDev->ImageableArea.left, physDev->ImageableArea.bottom,
236 physDev->ImageableArea.right, physDev->ImageableArea.top,
237 physDev->PageSize.cx, physDev->PageSize.cy);
239 /* these are in device units */
240 width = physDev->ImageableArea.right - physDev->ImageableArea.left;
241 height = physDev->ImageableArea.top - physDev->ImageableArea.bottom;
243 if(physDev->Devmode->dmPublic.u1.s1.dmOrientation == DMORIENT_PORTRAIT) {
244 physDev->horzRes = width;
245 physDev->vertRes = height;
246 } else {
247 physDev->horzRes = height;
248 physDev->vertRes = width;
251 /* these are in mm */
252 physDev->horzSize = (physDev->horzRes * 25.4) / physDev->logPixelsX;
253 physDev->vertSize = (physDev->vertRes * 25.4) / physDev->logPixelsY;
255 TRACE("devcaps: horzSize = %dmm, vertSize = %dmm, "
256 "horzRes = %d, vertRes = %d\n",
257 physDev->horzSize, physDev->vertSize,
258 physDev->horzRes, physDev->vertRes);
262 /***********************************************************
263 * DEVMODEdupWtoA
265 * Creates an ascii copy of supplied devmode on heap
267 * Copied from dlls/winspool/info.c until full unicodification
269 static LPDEVMODEA DEVMODEdupWtoA(HANDLE heap, const DEVMODEW *dmW)
271 LPDEVMODEA dmA;
272 DWORD size;
273 BOOL Formname;
274 /* there is no pointer dereference here, if your code checking tool complains it's broken */
275 ptrdiff_t off_formname = (const char *)dmW->dmFormName - (const char *)dmW;
277 if(!dmW) return NULL;
278 Formname = (dmW->dmSize > off_formname);
279 size = dmW->dmSize - CCHDEVICENAME - (Formname ? CCHFORMNAME : 0);
280 dmA = HeapAlloc(heap, HEAP_ZERO_MEMORY, size + dmW->dmDriverExtra);
281 WideCharToMultiByte(CP_ACP, 0, dmW->dmDeviceName, -1, (LPSTR)dmA->dmDeviceName,
282 CCHDEVICENAME, NULL, NULL);
283 if(!Formname) {
284 memcpy(&dmA->dmSpecVersion, &dmW->dmSpecVersion,
285 dmW->dmSize - CCHDEVICENAME * sizeof(WCHAR));
286 } else {
287 memcpy(&dmA->dmSpecVersion, &dmW->dmSpecVersion,
288 off_formname - CCHDEVICENAME * sizeof(WCHAR));
289 WideCharToMultiByte(CP_ACP, 0, dmW->dmFormName, -1, (LPSTR)dmA->dmFormName,
290 CCHFORMNAME, NULL, NULL);
291 memcpy(&dmA->dmLogPixels, &dmW->dmLogPixels, dmW->dmSize -
292 (off_formname + CCHFORMNAME * sizeof(WCHAR)));
294 dmA->dmSize = size;
295 memcpy((char *)dmA + dmA->dmSize, (const char *)dmW + dmW->dmSize,
296 dmW->dmDriverExtra);
297 return dmA;
301 /**********************************************************************
302 * PSDRV_CreateDC
304 BOOL CDECL PSDRV_CreateDC( HDC hdc, PSDRV_PDEVICE **pdev, LPCWSTR driver, LPCWSTR device,
305 LPCWSTR output, const DEVMODEW* initData )
307 PSDRV_PDEVICE *physDev;
308 PRINTERINFO *pi;
310 /* If no device name was specified, retrieve the device name
311 * from the PRINTERINFO structure from the DC's physDev.
312 * (See CreateCompatibleDC) */
313 if ( !device && *pdev )
315 pi = PSDRV_FindPrinterInfo((*pdev)->pi->FriendlyName);
317 else
319 DWORD len = WideCharToMultiByte(CP_ACP, 0, device, -1, NULL, 0, NULL, NULL);
320 char *deviceA = HeapAlloc(GetProcessHeap(), 0, len);
321 WideCharToMultiByte(CP_ACP, 0, device, -1, deviceA, len, NULL, NULL);
322 pi = PSDRV_FindPrinterInfo(deviceA);
323 HeapFree(GetProcessHeap(), 0, deviceA);
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.id = 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 CDECL 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 CDECL PSDRV_ResetDC( PSDRV_PDEVICE *physDev, const DEVMODEW *lpInitData )
401 if(lpInitData) {
402 DEVMODEA *devmodeA = DEVMODEdupWtoA(PSDRV_Heap, lpInitData);
403 PSDRV_MergeDevmodes(physDev->Devmode, (PSDRV_DEVMODEA *)devmodeA, physDev->pi);
404 HeapFree(PSDRV_Heap, 0, devmodeA);
405 PSDRV_UpdateDevCaps(physDev);
407 return physDev->hdc;
410 /***********************************************************************
411 * GetDeviceCaps (WINEPS.@)
413 INT CDECL PSDRV_GetDeviceCaps( PSDRV_PDEVICE *physDev, INT cap )
415 switch(cap)
417 case DRIVERVERSION:
418 return 0;
419 case TECHNOLOGY:
420 return DT_RASPRINTER;
421 case HORZSIZE:
422 return MulDiv(physDev->horzSize, 100,
423 physDev->Devmode->dmPublic.u1.s1.dmScale);
424 case VERTSIZE:
425 return MulDiv(physDev->vertSize, 100,
426 physDev->Devmode->dmPublic.u1.s1.dmScale);
427 case HORZRES:
428 case DESKTOPHORZRES:
429 return physDev->horzRes;
430 case VERTRES:
431 case DESKTOPVERTRES:
432 return physDev->vertRes;
433 case BITSPIXEL:
434 return (physDev->pi->ppd->ColorDevice != CD_False) ? 8 : 1;
435 case PLANES:
436 return 1;
437 case NUMBRUSHES:
438 return -1;
439 case NUMPENS:
440 return 10;
441 case NUMMARKERS:
442 return 0;
443 case NUMFONTS:
444 return 39;
445 case NUMCOLORS:
446 return (physDev->pi->ppd->ColorDevice != CD_False) ? 256 : -1;
447 case PDEVICESIZE:
448 return sizeof(PSDRV_PDEVICE);
449 case CURVECAPS:
450 return (CC_CIRCLES | CC_PIE | CC_CHORD | CC_ELLIPSES | CC_WIDE |
451 CC_STYLED | CC_WIDESTYLED | CC_INTERIORS | CC_ROUNDRECT);
452 case LINECAPS:
453 return (LC_POLYLINE | LC_MARKER | LC_POLYMARKER | LC_WIDE |
454 LC_STYLED | LC_WIDESTYLED | LC_INTERIORS);
455 case POLYGONALCAPS:
456 return (PC_POLYGON | PC_RECTANGLE | PC_WINDPOLYGON | PC_SCANLINE |
457 PC_WIDE | PC_STYLED | PC_WIDESTYLED | PC_INTERIORS);
458 case TEXTCAPS:
459 return TC_CR_ANY | TC_VA_ABLE; /* psdrv 0x59f7 */
460 case CLIPCAPS:
461 return CP_RECTANGLE;
462 case RASTERCAPS:
463 return (RC_BITBLT | RC_BITMAP64 | RC_GDI20_OUTPUT | RC_DIBTODEV |
464 RC_STRETCHBLT | RC_STRETCHDIB); /* psdrv 0x6e99 */
465 /* Are aspect[XY] and logPixels[XY] correct? */
466 /* Need to handle different res in x and y => fix ppd */
467 case ASPECTX:
468 case ASPECTY:
469 return physDev->pi->ppd->DefaultResolution;
470 case ASPECTXY:
471 return (int)hypot( (double)physDev->pi->ppd->DefaultResolution,
472 (double)physDev->pi->ppd->DefaultResolution );
473 case LOGPIXELSX:
474 return MulDiv(physDev->logPixelsX,
475 physDev->Devmode->dmPublic.u1.s1.dmScale, 100);
476 case LOGPIXELSY:
477 return MulDiv(physDev->logPixelsY,
478 physDev->Devmode->dmPublic.u1.s1.dmScale, 100);
479 case SIZEPALETTE:
480 return 0;
481 case NUMRESERVED:
482 return 0;
483 case COLORRES:
484 return 0;
485 case PHYSICALWIDTH:
486 return (physDev->Devmode->dmPublic.u1.s1.dmOrientation == DMORIENT_LANDSCAPE) ?
487 physDev->PageSize.cy : physDev->PageSize.cx;
488 case PHYSICALHEIGHT:
489 return (physDev->Devmode->dmPublic.u1.s1.dmOrientation == DMORIENT_LANDSCAPE) ?
490 physDev->PageSize.cx : physDev->PageSize.cy;
491 case PHYSICALOFFSETX:
492 if(physDev->Devmode->dmPublic.u1.s1.dmOrientation == DMORIENT_LANDSCAPE) {
493 if(physDev->pi->ppd->LandscapeOrientation == -90)
494 return physDev->PageSize.cy - physDev->ImageableArea.top;
495 else
496 return physDev->ImageableArea.bottom;
498 return physDev->ImageableArea.left;
500 case PHYSICALOFFSETY:
501 if(physDev->Devmode->dmPublic.u1.s1.dmOrientation == DMORIENT_LANDSCAPE) {
502 if(physDev->pi->ppd->LandscapeOrientation == -90)
503 return physDev->PageSize.cx - physDev->ImageableArea.right;
504 else
505 return physDev->ImageableArea.left;
507 return physDev->PageSize.cy - physDev->ImageableArea.top;
509 case SCALINGFACTORX:
510 case SCALINGFACTORY:
511 case VREFRESH:
512 case BLTALIGNMENT:
513 return 0;
514 case SHADEBLENDCAPS:
515 return SB_NONE;
516 default:
517 FIXME("(%p): unsupported capability %d, will return 0\n", physDev->hdc, cap );
518 return 0;
523 /**********************************************************************
524 * PSDRV_FindPrinterInfo
526 PRINTERINFO *PSDRV_FindPrinterInfo(LPCSTR name)
528 static PRINTERINFO *PSDRV_PrinterList;
529 DWORD type = REG_BINARY, needed, res, dwPaperSize;
530 PRINTERINFO *pi = PSDRV_PrinterList, **last = &PSDRV_PrinterList;
531 FONTNAME *font;
532 const AFM *afm;
533 HANDLE hPrinter = 0;
534 const char *ppd = NULL;
535 DWORD ppdType;
536 char* ppdFileName = NULL;
537 HKEY hkey;
538 BOOL using_default_devmode = FALSE;
540 TRACE("'%s'\n", name);
543 * If this loop completes, last will point to the 'next' element of the
544 * final PRINTERINFO in the list
546 for( ; pi; last = &pi->next, pi = pi->next)
547 if(!strcmp(pi->FriendlyName, name))
548 return pi;
550 pi = *last = HeapAlloc( PSDRV_Heap, HEAP_ZERO_MEMORY, sizeof(*pi) );
551 if (pi == NULL)
552 return NULL;
554 if (!(pi->FriendlyName = HeapAlloc( PSDRV_Heap, 0, strlen(name)+1 ))) goto fail;
555 strcpy( pi->FriendlyName, name );
557 if (OpenPrinterA (pi->FriendlyName, &hPrinter, NULL) == 0) {
558 ERR ("OpenPrinterA failed with code %i\n", GetLastError ());
559 goto cleanup;
562 needed = 0;
563 res = GetPrinterDataExA(hPrinter, NULL, default_devmodeA, &type, NULL, 0, &needed);
565 if (needed < sizeof(DefaultDevmode)) {
566 pi->Devmode = HeapAlloc( PSDRV_Heap, 0, sizeof(DefaultDevmode) );
567 if (pi->Devmode == NULL)
568 goto closeprinter;
570 *pi->Devmode = DefaultDevmode;
571 lstrcpynA((LPSTR)pi->Devmode->dmPublic.dmDeviceName,name,CCHDEVICENAME);
572 using_default_devmode = TRUE;
574 else {
575 pi->Devmode = HeapAlloc( PSDRV_Heap, 0, needed );
576 if (pi->Devmode == NULL)
577 goto closeprinter;
579 GetPrinterDataExA(hPrinter, NULL, default_devmodeA, &type, (LPBYTE)pi->Devmode, needed, &needed);
584 #ifdef SONAME_LIBCUPS
585 if (cupshandle != (void*)-1) {
586 typeof(cupsGetPPD) * pcupsGetPPD = NULL;
588 pcupsGetPPD = wine_dlsym(cupshandle, "cupsGetPPD", NULL, 0);
589 if (pcupsGetPPD) {
590 ppd = pcupsGetPPD(name);
592 if (ppd) {
593 needed=strlen(ppd)+1;
594 ppdFileName=HeapAlloc(PSDRV_Heap, 0, needed);
595 memcpy(ppdFileName, ppd, needed);
596 ppdType=REG_SZ;
597 res = ERROR_SUCCESS;
598 /* we should unlink() that file later */
599 } else {
600 res = ERROR_FILE_NOT_FOUND;
601 WARN("Did not find ppd for %s\n",name);
605 #endif
606 if (!ppdFileName) {
607 res = GetPrinterDataExA(hPrinter, "PrinterDriverData", "PPD File", NULL, NULL, 0, &needed);
608 if ((res==ERROR_SUCCESS) || (res==ERROR_MORE_DATA)) {
609 ppdFileName=HeapAlloc(PSDRV_Heap, 0, needed);
610 res = GetPrinterDataExA(hPrinter, "PrinterDriverData", "PPD File", &ppdType,
611 (LPBYTE)ppdFileName, needed, &needed);
614 /* Look for a ppd file for this printer in the config file.
615 * First look under that printer's name, and then under 'generic'
617 /* @@ Wine registry key: HKCU\Software\Wine\Printing\PPD Files */
618 if((res != ERROR_SUCCESS) && !RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Printing\\PPD Files", &hkey))
620 const char* value_name;
622 if (RegQueryValueExA(hkey, name, 0, NULL, NULL, &needed) == ERROR_SUCCESS) {
623 value_name=name;
624 } else if (RegQueryValueExA(hkey, "generic", 0, NULL, NULL, &needed) == ERROR_SUCCESS) {
625 value_name="generic";
626 } else {
627 value_name=NULL;
629 if (value_name) {
630 HeapFree(PSDRV_Heap, 0, ppdFileName);
631 ppdFileName=HeapAlloc(PSDRV_Heap, 0, needed);
632 RegQueryValueExA(hkey, value_name, 0, &ppdType, (LPBYTE)ppdFileName, &needed);
634 RegCloseKey(hkey);
637 if (!ppdFileName)
639 const char *data_dir, *filename;
641 if ((data_dir = wine_get_data_dir())) filename = "/generic.ppd";
642 else if ((data_dir = wine_get_build_dir())) filename = "/dlls/wineps.drv/generic.ppd";
643 else
645 res = ERROR_FILE_NOT_FOUND;
646 ERR ("Error %i getting PPD file name for printer '%s'\n", res, name);
647 goto closeprinter;
649 ppdFileName = HeapAlloc( PSDRV_Heap, 0, strlen(data_dir) + strlen(filename) + 1 );
650 strcpy( ppdFileName, data_dir );
651 strcat( ppdFileName, filename );
652 } else {
653 res = ERROR_SUCCESS;
654 if (ppdType==REG_EXPAND_SZ) {
655 char* tmp;
657 /* Expand environment variable references */
658 needed=ExpandEnvironmentStringsA(ppdFileName,NULL,0);
659 tmp=HeapAlloc(PSDRV_Heap, 0, needed);
660 ExpandEnvironmentStringsA(ppdFileName,tmp,needed);
661 HeapFree(PSDRV_Heap, 0, ppdFileName);
662 ppdFileName=tmp;
666 pi->ppd = PSDRV_ParsePPD(ppdFileName);
667 if(!pi->ppd) {
668 MESSAGE("Couldn't find PPD file '%s', expect a crash now!\n",
669 ppdFileName);
670 goto closeprinter;
673 /* Some gimp-print ppd files don't contain a DefaultResolution line
674 set it to 300 if it's not specified */
675 if(pi->ppd->DefaultResolution == 0)
676 pi->ppd->DefaultResolution = 300;
678 if(using_default_devmode) {
679 DWORD papersize;
681 if(GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_IPAPERSIZE | LOCALE_RETURN_NUMBER,
682 (LPWSTR)&papersize, sizeof(papersize)/sizeof(WCHAR))) {
683 PSDRV_DEVMODEA dm;
684 memset(&dm, 0, sizeof(dm));
685 dm.dmPublic.dmFields = DM_PAPERSIZE;
686 dm.dmPublic.u1.s1.dmPaperSize = papersize;
687 PSDRV_MergeDevmodes(pi->Devmode, &dm, pi);
690 SetPrinterDataExA(hPrinter, NULL, default_devmodeA, REG_BINARY,
691 (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;