Const correctness, pointer cast correctness, removed extraneous ';'.
[wine.git] / dlls / wineps / init.c
blob3ab4dd0a10a005a3303bba889613fb1046ca3b57
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "config.h"
23 #include "wine/port.h"
25 #include <string.h>
26 #ifdef HAVE_UNISTD_H
27 # include <unistd.h>
28 #endif
30 #define NONAMELESSUNION
31 #define NONAMELESSSTRUCT
32 #include "psdrv.h"
33 #include "wine/debug.h"
34 #include "winreg.h"
35 #include "winspool.h"
36 #include "winerror.h"
37 #include "wownt32.h"
38 #include "heap.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(psdrv);
42 #ifdef HAVE_CUPS_CUPS_H
43 #include <cups/cups.h>
45 #ifndef SONAME_LIBCUPS
46 #define SONAME_LIBCUPS "libcups.so"
47 #endif
49 static void *cupshandle = NULL;
50 #endif
52 static PSDRV_DEVMODEA DefaultDevmode =
54 { /* dmPublic */
55 /* dmDeviceName */ "Wine PostScript Driver",
56 /* dmSpecVersion */ 0x30a,
57 /* dmDriverVersion */ 0x001,
58 /* dmSize */ sizeof(DEVMODEA),
59 /* dmDriverExtra */ sizeof(PSDRV_DEVMODEA)-sizeof(DEVMODEA),
60 /* dmFields */ DM_ORIENTATION | DM_PAPERSIZE | DM_SCALE |
61 DM_COPIES | DM_DEFAULTSOURCE | DM_COLOR |
62 DM_YRESOLUTION | DM_TTOPTION,
63 { /* u1 */
64 { /* s1 */
65 /* dmOrientation */ DMORIENT_PORTRAIT,
66 /* dmPaperSize */ DMPAPER_LETTER,
67 /* dmPaperLength */ 2794,
68 /* dmPaperWidth */ 2159
71 /* dmScale */ 100, /* ?? */
72 /* dmCopies */ 1,
73 /* dmDefaultSource */ DMBIN_AUTO,
74 /* dmPrintQuality */ 0,
75 /* dmColor */ DMCOLOR_COLOR,
76 /* dmDuplex */ DMDUP_SIMPLEX,
77 /* dmYResolution */ 0,
78 /* dmTTOption */ DMTT_SUBDEV,
79 /* dmCollate */ 0,
80 /* dmFormName */ "",
81 /* dmUnusedPadding */ 0,
82 /* dmBitsPerPel */ 0,
83 /* dmPelsWidth */ 0,
84 /* dmPelsHeight */ 0,
85 /* dmDisplayFlags */ 0,
86 /* dmDisplayFrequency */ 0,
87 /* dmICMMethod */ 0,
88 /* dmICMIntent */ 0,
89 /* dmMediaType */ 0,
90 /* dmDitherType */ 0,
91 /* dmReserved1 */ 0,
92 /* dmReserved2 */ 0,
93 /* dmPanningWidth */ 0,
94 /* dmPanningHeight */ 0
96 { /* dmDocPrivate */
97 /* dummy */ 0
99 { /* dmDrvPrivate */
100 /* numInstalledOptions */ 0
104 HANDLE PSDRV_Heap = 0;
106 static HFONT PSDRV_DefaultFont = 0;
107 static LOGFONTA DefaultLogFont = {
108 100, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, ANSI_CHARSET, 0, 0,
109 DEFAULT_QUALITY, FIXED_PITCH | FF_MODERN, ""
112 /*********************************************************************
113 * DllMain
115 * Initializes font metrics and registers driver. wineps dll entry point.
118 BOOL WINAPI DllMain( HINSTANCE hinst, DWORD reason, LPVOID reserved )
120 TRACE("(%p, 0x%08lx, %p)\n", hinst, reason, reserved);
122 switch(reason) {
124 case DLL_PROCESS_ATTACH:
125 DisableThreadLibraryCalls(hinst);
127 PSDRV_Heap = HeapCreate(0, 0x10000, 0);
128 if (PSDRV_Heap == NULL)
129 return FALSE;
131 if (PSDRV_GetFontMetrics() == FALSE) {
132 HeapDestroy(PSDRV_Heap);
133 return FALSE;
136 PSDRV_DefaultFont = CreateFontIndirectA(&DefaultLogFont);
137 if (PSDRV_DefaultFont == NULL) {
138 HeapDestroy(PSDRV_Heap);
139 return FALSE;
141 #ifdef HAVE_CUPS_CUPS_H
142 /* dynamically load CUPS if not yet loaded */
143 if (!cupshandle) {
144 cupshandle = wine_dlopen(SONAME_LIBCUPS, RTLD_NOW, NULL, 0);
145 if (!cupshandle) cupshandle = (void*)-1;
147 #endif
148 break;
150 case DLL_PROCESS_DETACH:
152 DeleteObject( PSDRV_DefaultFont );
153 HeapDestroy( PSDRV_Heap );
154 #ifdef HAVE_CUPS_CUPS_H
155 if (cupshandle && (cupshandle != (void*)-1)) {
156 wine_dlclose(cupshandle, NULL, 0);
157 cupshandle = NULL;
159 #endif
160 break;
163 return TRUE;
167 static void PSDRV_UpdateDevCaps( PSDRV_PDEVICE *physDev )
169 PAGESIZE *page;
170 INT width = 0, height = 0;
172 if(physDev->Devmode->dmPublic.dmFields & DM_PAPERSIZE) {
173 for(page = physDev->pi->ppd->PageSizes; page; page = page->next) {
174 if(page->WinPage == physDev->Devmode->dmPublic.u1.s1.dmPaperSize)
175 break;
178 if(!page) {
179 FIXME("Can't find page\n");
180 physDev->ImageableArea.left = 0;
181 physDev->ImageableArea.right = 0;
182 physDev->ImageableArea.bottom = 0;
183 physDev->ImageableArea.top = 0;
184 physDev->PageSize.cx = 0;
185 physDev->PageSize.cy = 0;
186 } else if(page->ImageableArea) {
187 /* physDev sizes in device units; ppd sizes in 1/72" */
188 physDev->ImageableArea.left = page->ImageableArea->llx *
189 physDev->logPixelsX / 72;
190 physDev->ImageableArea.right = page->ImageableArea->urx *
191 physDev->logPixelsX / 72;
192 physDev->ImageableArea.bottom = page->ImageableArea->lly *
193 physDev->logPixelsY / 72;
194 physDev->ImageableArea.top = page->ImageableArea->ury *
195 physDev->logPixelsY / 72;
196 physDev->PageSize.cx = page->PaperDimension->x *
197 physDev->logPixelsX / 72;
198 physDev->PageSize.cy = page->PaperDimension->y *
199 physDev->logPixelsY / 72;
200 } else {
201 physDev->ImageableArea.left = physDev->ImageableArea.bottom = 0;
202 physDev->ImageableArea.right = physDev->PageSize.cx =
203 page->PaperDimension->x * physDev->logPixelsX / 72;
204 physDev->ImageableArea.top = physDev->PageSize.cy =
205 page->PaperDimension->y * physDev->logPixelsY / 72;
207 } else if((physDev->Devmode->dmPublic.dmFields & DM_PAPERLENGTH) &&
208 (physDev->Devmode->dmPublic.dmFields & DM_PAPERWIDTH)) {
209 /* physDev sizes in device units; Devmode sizes in 1/10 mm */
210 physDev->ImageableArea.left = physDev->ImageableArea.bottom = 0;
211 physDev->ImageableArea.right = physDev->PageSize.cx =
212 physDev->Devmode->dmPublic.u1.s1.dmPaperWidth *
213 physDev->logPixelsX / 254;
214 physDev->ImageableArea.top = physDev->PageSize.cy =
215 physDev->Devmode->dmPublic.u1.s1.dmPaperLength *
216 physDev->logPixelsY / 254;
217 } else {
218 FIXME("Odd dmFields %lx\n", physDev->Devmode->dmPublic.dmFields);
219 physDev->ImageableArea.left = 0;
220 physDev->ImageableArea.right = 0;
221 physDev->ImageableArea.bottom = 0;
222 physDev->ImageableArea.top = 0;
223 physDev->PageSize.cx = 0;
224 physDev->PageSize.cy = 0;
227 TRACE("ImageableArea = %ld,%ld - %ld,%ld: PageSize = %ldx%ld\n",
228 physDev->ImageableArea.left, physDev->ImageableArea.bottom,
229 physDev->ImageableArea.right, physDev->ImageableArea.top,
230 physDev->PageSize.cx, physDev->PageSize.cy);
232 /* these are in device units */
233 width = physDev->ImageableArea.right - physDev->ImageableArea.left;
234 height = physDev->ImageableArea.top - physDev->ImageableArea.bottom;
236 if(physDev->Devmode->dmPublic.u1.s1.dmOrientation == DMORIENT_PORTRAIT) {
237 physDev->horzRes = width;
238 physDev->vertRes = height;
239 } else {
240 physDev->horzRes = height;
241 physDev->vertRes = width;
244 /* these are in mm */
245 physDev->horzSize = (physDev->horzRes * 25.4) / physDev->logPixelsX;
246 physDev->vertSize = (physDev->vertRes * 25.4) / physDev->logPixelsY;
248 TRACE("devcaps: horzSize = %dmm, vertSize = %dmm, "
249 "horzRes = %d, vertRes = %d\n",
250 physDev->horzSize, physDev->vertSize,
251 physDev->horzRes, physDev->vertRes);
255 /***********************************************************
256 * DEVMODEdupWtoA
258 * Creates an ascii copy of supplied devmode on heap
260 * Copied from dlls/winspool/info.c until full unicodification
262 static LPDEVMODEA DEVMODEdupWtoA(HANDLE heap, const DEVMODEW *dmW)
264 LPDEVMODEA dmA;
265 DWORD size;
266 BOOL Formname;
267 ptrdiff_t off_formname = (char *)dmW->dmFormName - (char *)dmW;
269 if(!dmW) return NULL;
270 Formname = (dmW->dmSize > off_formname);
271 size = dmW->dmSize - CCHDEVICENAME - (Formname ? CCHFORMNAME : 0);
272 dmA = HeapAlloc(heap, HEAP_ZERO_MEMORY, size + dmW->dmDriverExtra);
273 WideCharToMultiByte(CP_ACP, 0, dmW->dmDeviceName, -1, dmA->dmDeviceName,
274 CCHDEVICENAME, NULL, NULL);
275 if(!Formname) {
276 memcpy(&dmA->dmSpecVersion, &dmW->dmSpecVersion,
277 dmW->dmSize - CCHDEVICENAME * sizeof(WCHAR));
278 } else {
279 memcpy(&dmA->dmSpecVersion, &dmW->dmSpecVersion,
280 off_formname - CCHDEVICENAME * sizeof(WCHAR));
281 WideCharToMultiByte(CP_ACP, 0, dmW->dmFormName, -1, dmA->dmFormName,
282 CCHFORMNAME, NULL, NULL);
283 memcpy(&dmA->dmLogPixels, &dmW->dmLogPixels, dmW->dmSize -
284 (off_formname + CCHFORMNAME * sizeof(WCHAR)));
286 dmA->dmSize = size;
287 memcpy((char *)dmA + dmA->dmSize, (char *)dmW + dmW->dmSize,
288 dmW->dmDriverExtra);
289 return dmA;
293 /**********************************************************************
294 * PSDRV_CreateDC
296 BOOL PSDRV_CreateDC( HDC hdc, PSDRV_PDEVICE **pdev, LPCWSTR driver, LPCWSTR device,
297 LPCWSTR output, const DEVMODEW* initData )
299 PSDRV_PDEVICE *physDev;
300 PRINTERINFO *pi;
301 char deviceA[CCHDEVICENAME];
303 /* If no device name was specified, retrieve the device name
304 * from the DEVMODE structure from the DC's physDev.
305 * (See CreateCompatibleDC) */
306 if ( !device && *pdev )
308 physDev = *pdev;
309 strcpy(deviceA, physDev->Devmode->dmPublic.dmDeviceName);
311 else
312 WideCharToMultiByte(CP_ACP, 0, device, -1, deviceA, sizeof(deviceA), NULL, NULL);
313 pi = PSDRV_FindPrinterInfo(deviceA);
315 TRACE("(%s %s %s %p)\n", debugstr_w(driver), debugstr_w(device),
316 debugstr_w(output), initData);
318 if(!pi) return FALSE;
320 if(!pi->Fonts) {
321 MESSAGE("To use WINEPS you need to install some AFM files.\n");
322 return FALSE;
325 physDev = (PSDRV_PDEVICE *)HeapAlloc( PSDRV_Heap, HEAP_ZERO_MEMORY,
326 sizeof(*physDev) );
327 if (!physDev) return FALSE;
328 *pdev = physDev;
329 physDev->hdc = hdc;
331 physDev->pi = pi;
333 physDev->Devmode = (PSDRV_DEVMODEA *)HeapAlloc( PSDRV_Heap, 0,
334 sizeof(PSDRV_DEVMODEA) );
335 if(!physDev->Devmode) {
336 HeapFree( PSDRV_Heap, 0, physDev );
337 return FALSE;
340 memcpy( physDev->Devmode, pi->Devmode, sizeof(PSDRV_DEVMODEA) );
342 physDev->logPixelsX = physDev->pi->ppd->DefaultResolution;
343 physDev->logPixelsY = physDev->pi->ppd->DefaultResolution;
345 if (output) {
346 physDev->job.output = HEAP_strdupWtoA( PSDRV_Heap, 0, output );
347 } else
348 physDev->job.output = NULL;
349 physDev->job.hJob = 0;
351 if(initData) {
352 DEVMODEA *devmodeA = DEVMODEdupWtoA(PSDRV_Heap, initData);
353 PSDRV_MergeDevmodes(physDev->Devmode, (PSDRV_DEVMODEA *)devmodeA, pi);
354 HeapFree(PSDRV_Heap, 0, devmodeA);
357 PSDRV_UpdateDevCaps(physDev);
358 SelectObject( hdc, PSDRV_DefaultFont );
359 return TRUE;
364 /**********************************************************************
365 * PSDRV_DeleteDC
367 BOOL PSDRV_DeleteDC( PSDRV_PDEVICE *physDev )
369 TRACE("\n");
371 HeapFree( PSDRV_Heap, 0, physDev->Devmode );
372 if(physDev->job.output)
373 HeapFree( PSDRV_Heap, 0, physDev->job.output );
374 HeapFree( PSDRV_Heap, 0, physDev );
376 return TRUE;
380 /**********************************************************************
381 * ResetDC (WINEPS.@)
383 HDC PSDRV_ResetDC( PSDRV_PDEVICE *physDev, const DEVMODEW *lpInitData )
385 if(lpInitData) {
386 HRGN hrgn;
387 DEVMODEA *devmodeA = DEVMODEdupWtoA(PSDRV_Heap, lpInitData);
388 PSDRV_MergeDevmodes(physDev->Devmode, (PSDRV_DEVMODEA *)devmodeA, physDev->pi);
389 HeapFree(PSDRV_Heap, 0, devmodeA);
390 PSDRV_UpdateDevCaps(physDev);
391 hrgn = CreateRectRgn(0, 0, physDev->horzRes, physDev->vertRes);
392 SelectVisRgn16(HDC_16(physDev->hdc), HRGN_16(hrgn));
393 DeleteObject(hrgn);
395 return physDev->hdc;
398 /***********************************************************************
399 * GetDeviceCaps (WINEPS.@)
401 INT PSDRV_GetDeviceCaps( PSDRV_PDEVICE *physDev, INT cap )
403 switch(cap)
405 case DRIVERVERSION:
406 return 0;
407 case TECHNOLOGY:
408 return DT_RASPRINTER;
409 case HORZSIZE:
410 return MulDiv(physDev->horzSize, 100,
411 physDev->Devmode->dmPublic.dmScale);
412 case VERTSIZE:
413 return MulDiv(physDev->vertSize, 100,
414 physDev->Devmode->dmPublic.dmScale);
415 case HORZRES:
416 return physDev->horzRes;
417 case VERTRES:
418 return physDev->vertRes;
419 case BITSPIXEL:
420 return physDev->pi->ppd->ColorDevice ? 8 : 1;
421 case PLANES:
422 return 1;
423 case NUMBRUSHES:
424 return -1;
425 case NUMPENS:
426 return 10;
427 case NUMMARKERS:
428 return 0;
429 case NUMFONTS:
430 return 39;
431 case NUMCOLORS:
432 return (physDev->pi->ppd->ColorDevice ? 256 : -1);
433 case PDEVICESIZE:
434 return sizeof(PSDRV_PDEVICE);
435 case CURVECAPS:
436 return (CC_CIRCLES | CC_PIE | CC_CHORD | CC_ELLIPSES | CC_WIDE |
437 CC_STYLED | CC_WIDESTYLED | CC_INTERIORS | CC_ROUNDRECT);
438 case LINECAPS:
439 return (LC_POLYLINE | LC_MARKER | LC_POLYMARKER | LC_WIDE |
440 LC_STYLED | LC_WIDESTYLED | LC_INTERIORS);
441 case POLYGONALCAPS:
442 return (PC_POLYGON | PC_RECTANGLE | PC_WINDPOLYGON | PC_SCANLINE |
443 PC_WIDE | PC_STYLED | PC_WIDESTYLED | PC_INTERIORS);
444 case TEXTCAPS:
445 return TC_CR_ANY | TC_VA_ABLE; /* psdrv 0x59f7 */
446 case CLIPCAPS:
447 return CP_RECTANGLE;
448 case RASTERCAPS:
449 return (RC_BITBLT | RC_BITMAP64 | RC_GDI20_OUTPUT | RC_DIBTODEV |
450 RC_STRETCHBLT | RC_STRETCHDIB); /* psdrv 0x6e99 */
451 /* Are aspect[XY] and logPixels[XY] correct? */
452 /* Need to handle different res in x and y => fix ppd */
453 case ASPECTX:
454 case ASPECTY:
455 return physDev->pi->ppd->DefaultResolution;
456 case ASPECTXY:
457 return (int)hypot( (double)physDev->pi->ppd->DefaultResolution,
458 (double)physDev->pi->ppd->DefaultResolution );
459 case LOGPIXELSX:
460 return MulDiv(physDev->logPixelsX,
461 physDev->Devmode->dmPublic.dmScale, 100);
462 case LOGPIXELSY:
463 return MulDiv(physDev->logPixelsY,
464 physDev->Devmode->dmPublic.dmScale, 100);
465 case SIZEPALETTE:
466 return 0;
467 case NUMRESERVED:
468 return 0;
469 case COLORRES:
470 return 0;
471 case PHYSICALWIDTH:
472 return (physDev->Devmode->dmPublic.u1.s1.dmOrientation == DMORIENT_LANDSCAPE) ?
473 physDev->PageSize.cy : physDev->PageSize.cx;
474 case PHYSICALHEIGHT:
475 return (physDev->Devmode->dmPublic.u1.s1.dmOrientation == DMORIENT_LANDSCAPE) ?
476 physDev->PageSize.cx : physDev->PageSize.cy;
477 case PHYSICALOFFSETX:
478 if(physDev->Devmode->dmPublic.u1.s1.dmOrientation == DMORIENT_LANDSCAPE) {
479 if(physDev->pi->ppd->LandscapeOrientation == -90)
480 return physDev->PageSize.cy - physDev->ImageableArea.top;
481 else
482 return physDev->ImageableArea.bottom;
484 return physDev->ImageableArea.left;
486 case PHYSICALOFFSETY:
487 if(physDev->Devmode->dmPublic.u1.s1.dmOrientation == DMORIENT_LANDSCAPE) {
488 if(physDev->pi->ppd->LandscapeOrientation == -90)
489 return physDev->PageSize.cx - physDev->ImageableArea.right;
490 else
491 return physDev->ImageableArea.left;
493 return physDev->PageSize.cy - physDev->ImageableArea.top;
495 case SCALINGFACTORX:
496 case SCALINGFACTORY:
497 case VREFRESH:
498 case DESKTOPVERTRES:
499 case DESKTOPHORZRES:
500 case BTLALIGNMENT:
501 return 0;
502 default:
503 FIXME("(%p): unsupported capability %d, will return 0\n", physDev->hdc, cap );
504 return 0;
509 /**********************************************************************
510 * PSDRV_FindPrinterInfo
512 PRINTERINFO *PSDRV_FindPrinterInfo(LPCSTR name)
514 static PRINTERINFO *PSDRV_PrinterList;
515 DWORD type = REG_BINARY, needed, res, dwPaperSize;
516 PRINTERINFO *pi = PSDRV_PrinterList, **last = &PSDRV_PrinterList;
517 FONTNAME *font;
518 const AFM *afm;
519 HANDLE hPrinter;
520 const char *ppd = NULL;
521 DWORD ppdType;
522 char* ppdFileName = NULL;
523 HKEY hkey;
524 BOOL using_default_devmode = FALSE;
526 TRACE("'%s'\n", name);
529 * If this loop completes, last will point to the 'next' element of the
530 * final PRINTERINFO in the list
532 for( ; pi; last = &pi->next, pi = pi->next)
533 if(!strcmp(pi->FriendlyName, name))
534 return pi;
536 pi = *last = HeapAlloc( PSDRV_Heap, HEAP_ZERO_MEMORY, sizeof(*pi) );
537 if (pi == NULL)
538 return NULL;
540 if (!(pi->FriendlyName = HeapAlloc( PSDRV_Heap, 0, strlen(name)+1 ))) goto fail;
541 strcpy( pi->FriendlyName, name );
543 /* Use Get|SetPrinterDataExA instead? */
545 res = DrvGetPrinterData16((LPSTR)name, (LPSTR)INT_PD_DEFAULT_DEVMODE, &type,
546 NULL, 0, &needed );
548 if(res == ERROR_INVALID_PRINTER_NAME || needed != sizeof(DefaultDevmode)) {
549 pi->Devmode = HeapAlloc( PSDRV_Heap, 0, sizeof(DefaultDevmode) );
550 if (pi->Devmode == NULL)
551 goto cleanup;
552 memcpy(pi->Devmode, &DefaultDevmode, sizeof(DefaultDevmode) );
553 strcpy(pi->Devmode->dmPublic.dmDeviceName,name);
554 using_default_devmode = TRUE;
556 /* need to do something here AddPrinter?? */
558 else {
559 pi->Devmode = HeapAlloc( PSDRV_Heap, 0, needed );
560 DrvGetPrinterData16((LPSTR)name, (LPSTR)INT_PD_DEFAULT_DEVMODE, &type,
561 (LPBYTE)pi->Devmode, needed, &needed);
564 if (OpenPrinterA (pi->FriendlyName, &hPrinter, NULL) == 0) {
565 ERR ("OpenPrinterA failed with code %li\n", GetLastError ());
566 goto cleanup;
569 #ifdef HAVE_CUPS_CUPS_H
570 if (cupshandle != (void*)-1) {
571 typeof(cupsGetPPD) * pcupsGetPPD = NULL;
573 pcupsGetPPD = wine_dlsym(cupshandle, "cupsGetPPD", NULL, 0);
574 if (pcupsGetPPD) {
575 ppd = pcupsGetPPD(name);
577 if (ppd) {
578 needed=strlen(ppd)+1;
579 ppdFileName=HeapAlloc(PSDRV_Heap, 0, needed);
580 memcpy(ppdFileName, ppd, needed);
581 ppdType=REG_SZ;
582 res = ERROR_SUCCESS;
583 /* we should unlink() that file later */
584 } else {
585 res = ERROR_FILE_NOT_FOUND;
586 WARN("Did not find ppd for %s\n",name);
590 #endif
591 if (!ppdFileName) {
592 res = GetPrinterDataA(hPrinter, "PPD File", NULL, NULL, 0, &needed);
593 if ((res==ERROR_SUCCESS) || (res==ERROR_MORE_DATA)) {
594 ppdFileName=HeapAlloc(PSDRV_Heap, 0, needed);
595 res = GetPrinterDataA(hPrinter, "PPD File", &ppdType, ppdFileName, needed, &needed);
598 /* Look for a ppd file for this printer in the config file.
599 * First look under that printer's name, and then under 'generic'
601 if((res != ERROR_SUCCESS) && !RegOpenKeyA(HKEY_LOCAL_MACHINE, "Software\\Wine\\Wine\\Config\\ppd", &hkey))
603 const char* value_name;
605 if (RegQueryValueExA(hkey, name, 0, NULL, NULL, &needed) == ERROR_SUCCESS) {
606 value_name=name;
607 } else if (RegQueryValueExA(hkey, "generic", 0, NULL, NULL, &needed) == ERROR_SUCCESS) {
608 value_name="generic";
609 } else {
610 value_name=NULL;
612 if (value_name) {
613 ppdFileName=HeapAlloc(PSDRV_Heap, 0, needed);
614 RegQueryValueExA(hkey, value_name, 0, &ppdType, ppdFileName, &needed);
616 RegCloseKey(hkey);
619 if (!ppdFileName) {
620 res = ERROR_FILE_NOT_FOUND;
621 ERR ("Error %li getting PPD file name for printer '%s'\n", res, name);
622 goto closeprinter;
623 } else {
624 res = ERROR_SUCCESS;
625 if (ppdType==REG_EXPAND_SZ) {
626 char* tmp;
628 /* Expand environment variable references */
629 needed=ExpandEnvironmentStringsA(ppdFileName,NULL,0);
630 tmp=HeapAlloc(PSDRV_Heap, 0, needed);
631 ExpandEnvironmentStringsA(ppdFileName,tmp,needed);
632 HeapFree(PSDRV_Heap, 0, ppdFileName);
633 ppdFileName=tmp;
637 pi->ppd = PSDRV_ParsePPD(ppdFileName);
638 if(!pi->ppd) {
639 MESSAGE("Couldn't find PPD file '%s', expect a crash now!\n",
640 ppdFileName);
641 goto closeprinter;
644 /* Some gimp-print ppd files don't contain a DefaultResolution line
645 set it to 300 if it's not specified */
646 if(pi->ppd->DefaultResolution == 0)
647 pi->ppd->DefaultResolution = 300;
649 if(using_default_devmode) {
650 DWORD papersize;
652 if(GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_IPAPERSIZE | LOCALE_RETURN_NUMBER,
653 (LPWSTR)&papersize, sizeof(papersize)/sizeof(WCHAR))) {
654 PSDRV_DEVMODEA dm;
655 memset(&dm, 0, sizeof(dm));
656 dm.dmPublic.dmFields = DM_PAPERSIZE;
657 dm.dmPublic.u1.s1.dmPaperSize = papersize;
658 PSDRV_MergeDevmodes(pi->Devmode, &dm, pi);
660 DrvSetPrinterData16((LPSTR)name, (LPSTR)INT_PD_DEFAULT_DEVMODE,
661 REG_BINARY, (LPBYTE)pi->Devmode, sizeof(DefaultDevmode) );
664 if(pi->ppd->DefaultPageSize) { /* We'll let the ppd override the devmode */
665 PSDRV_DEVMODEA dm;
666 memset(&dm, 0, sizeof(dm));
667 dm.dmPublic.dmFields = DM_PAPERSIZE;
668 dm.dmPublic.u1.s1.dmPaperSize = pi->ppd->DefaultPageSize->WinPage;
669 PSDRV_MergeDevmodes(pi->Devmode, &dm, pi);
673 * This is a hack. The default paper size should be read in as part of
674 * the Devmode structure, but Wine doesn't currently provide a convenient
675 * way to configure printers.
677 res = GetPrinterDataA (hPrinter, "Paper Size", NULL, (LPBYTE) &dwPaperSize,
678 sizeof (DWORD), &needed);
679 if (res == ERROR_SUCCESS)
680 pi->Devmode->dmPublic.u1.s1.dmPaperSize = (SHORT) dwPaperSize;
681 else if (res == ERROR_FILE_NOT_FOUND)
682 TRACE ("No 'Paper Size' for printer '%s'\n", name);
683 else {
684 ERR ("GetPrinterDataA returned %li\n", res);
685 goto closeprinter;
688 /* Duplex is indicated by the setting of the DM_DUPLEX bit in dmFields.
689 WinDuplex == 0 is a special case which means that the ppd has a
690 *DefaultDuplex: NotCapable entry. In this case we'll try not to confuse
691 apps and set dmDuplex to DMDUP_SIMPLEX but leave the DM_DUPLEX clear.
692 PSDRV_WriteHeader understands this and copes. */
693 pi->Devmode->dmPublic.dmFields &= ~DM_DUPLEX;
694 if(pi->ppd->DefaultDuplex) {
695 pi->Devmode->dmPublic.dmDuplex = pi->ppd->DefaultDuplex->WinDuplex;
696 if(pi->Devmode->dmPublic.dmDuplex != 0)
697 pi->Devmode->dmPublic.dmFields |= DM_DUPLEX;
698 else
699 pi->Devmode->dmPublic.dmDuplex = DMDUP_SIMPLEX;
702 res = EnumPrinterDataExA (hPrinter, "PrinterDriverData\\FontSubTable", NULL,
703 0, &needed, &pi->FontSubTableSize);
704 if (res == ERROR_SUCCESS || res == ERROR_FILE_NOT_FOUND) {
705 TRACE ("No 'FontSubTable' for printer '%s'\n", name);
707 else if (res == ERROR_MORE_DATA) {
708 pi->FontSubTable = HeapAlloc (PSDRV_Heap, 0, needed);
709 if (pi->FontSubTable == NULL) {
710 ERR ("Failed to allocate %li bytes from heap\n", needed);
711 goto closeprinter;
714 res = EnumPrinterDataExA (hPrinter, "PrinterDriverData\\FontSubTable",
715 (LPBYTE) pi->FontSubTable, needed, &needed,
716 &pi->FontSubTableSize);
717 if (res != ERROR_SUCCESS) {
718 ERR ("EnumPrinterDataExA returned %li\n", res);
719 goto closeprinter;
722 else {
723 ERR("EnumPrinterDataExA returned %li\n", res);
724 goto closeprinter;
727 if (ClosePrinter (hPrinter) == 0) {
728 ERR ("ClosePrinter failed with code %li\n", GetLastError ());
729 goto cleanup;
732 pi->next = NULL;
733 pi->Fonts = NULL;
735 for(font = pi->ppd->InstalledFonts; font; font = font->next) {
736 afm = PSDRV_FindAFMinList(PSDRV_AFMFontList, font->Name);
737 if(!afm) {
738 TRACE( "Couldn't find AFM file for installed printer font '%s' - "
739 "ignoring\n", font->Name);
741 else {
742 BOOL added;
743 if (PSDRV_AddAFMtoList(&pi->Fonts, afm, &added) == FALSE) {
744 PSDRV_FreeAFMList(pi->Fonts);
745 goto cleanup;
750 if (ppd) unlink(ppd);
751 return pi;
753 closeprinter:
754 ClosePrinter(hPrinter);
755 cleanup:
756 if (ppdFileName)
757 HeapFree(PSDRV_Heap, 0, ppdFileName);
758 if (pi->FontSubTable)
759 HeapFree(PSDRV_Heap, 0, pi->FontSubTable);
760 if (pi->FriendlyName)
761 HeapFree(PSDRV_Heap, 0, pi->FriendlyName);
762 if (pi->Devmode)
763 HeapFree(PSDRV_Heap, 0, pi->Devmode);
764 fail:
765 HeapFree(PSDRV_Heap, 0, pi);
766 if (ppd) unlink(ppd);
767 *last = NULL;
768 return NULL;