widl: Convert expression lists to standard Wine lists.
[wine/hacks.git] / dlls / wineps.drv / init.c
blob8ec88b5af2ca9ea8c4721a73ae2359db92c22138
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 "wownt32.h"
41 #include "winreg.h"
42 #include "psdrv.h"
43 #include "winspool.h"
44 #include "wine/debug.h"
46 WINE_DEFAULT_DEBUG_CHANNEL(psdrv);
48 #ifndef SONAME_LIBCUPS
49 #define SONAME_LIBCUPS "libcups.so"
50 #endif
52 #ifdef HAVE_CUPS_CUPS_H
53 static void *cupshandle = NULL;
54 #endif
56 static PSDRV_DEVMODEA DefaultDevmode =
58 { /* dmPublic */
59 /* dmDeviceName */ "Wine PostScript Driver",
60 /* dmSpecVersion */ 0x30a,
61 /* dmDriverVersion */ 0x001,
62 /* dmSize */ sizeof(DEVMODEA),
63 /* dmDriverExtra */ sizeof(PSDRV_DEVMODEA)-sizeof(DEVMODEA),
64 /* dmFields */ DM_ORIENTATION | DM_PAPERSIZE | DM_SCALE |
65 DM_COPIES | DM_DEFAULTSOURCE | DM_COLOR |
66 DM_YRESOLUTION | DM_TTOPTION,
67 { /* u1 */
68 { /* s1 */
69 /* dmOrientation */ DMORIENT_PORTRAIT,
70 /* dmPaperSize */ DMPAPER_LETTER,
71 /* dmPaperLength */ 2794,
72 /* dmPaperWidth */ 2159
75 /* dmScale */ 100, /* ?? */
76 /* dmCopies */ 1,
77 /* dmDefaultSource */ DMBIN_AUTO,
78 /* dmPrintQuality */ 0,
79 /* dmColor */ DMCOLOR_COLOR,
80 /* dmDuplex */ DMDUP_SIMPLEX,
81 /* dmYResolution */ 0,
82 /* dmTTOption */ DMTT_SUBDEV,
83 /* dmCollate */ 0,
84 /* dmFormName */ "",
85 /* dmUnusedPadding */ 0,
86 /* dmBitsPerPel */ 0,
87 /* dmPelsWidth */ 0,
88 /* dmPelsHeight */ 0,
89 /* dmDisplayFlags */ 0,
90 /* dmDisplayFrequency */ 0,
91 /* dmICMMethod */ 0,
92 /* dmICMIntent */ 0,
93 /* dmMediaType */ 0,
94 /* dmDitherType */ 0,
95 /* dmReserved1 */ 0,
96 /* dmReserved2 */ 0,
97 /* dmPanningWidth */ 0,
98 /* dmPanningHeight */ 0
100 { /* dmDocPrivate */
101 /* dummy */ 0
103 { /* dmDrvPrivate */
104 /* numInstalledOptions */ 0
108 HINSTANCE PSDRV_hInstance = 0;
109 HANDLE PSDRV_Heap = 0;
111 static HFONT PSDRV_DefaultFont = 0;
112 static LOGFONTA DefaultLogFont = {
113 100, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, ANSI_CHARSET, 0, 0,
114 DEFAULT_QUALITY, FIXED_PITCH | FF_MODERN, ""
117 /*********************************************************************
118 * DllMain
120 * Initializes font metrics and registers driver. wineps dll entry point.
123 BOOL WINAPI DllMain( HINSTANCE hinst, DWORD reason, LPVOID reserved )
125 TRACE("(%p, %d, %p)\n", hinst, reason, reserved);
127 switch(reason) {
129 case DLL_PROCESS_ATTACH:
130 PSDRV_hInstance = hinst;
131 DisableThreadLibraryCalls(hinst);
133 PSDRV_Heap = HeapCreate(0, 0x10000, 0);
134 if (PSDRV_Heap == NULL)
135 return FALSE;
137 if (PSDRV_GetFontMetrics() == FALSE) {
138 HeapDestroy(PSDRV_Heap);
139 return FALSE;
142 PSDRV_DefaultFont = CreateFontIndirectA(&DefaultLogFont);
143 if (PSDRV_DefaultFont == NULL) {
144 HeapDestroy(PSDRV_Heap);
145 return FALSE;
147 #ifdef HAVE_CUPS_CUPS_H
148 /* dynamically load CUPS if not yet loaded */
149 if (!cupshandle) {
150 cupshandle = wine_dlopen(SONAME_LIBCUPS, RTLD_NOW, NULL, 0);
151 if (!cupshandle) cupshandle = (void*)-1;
153 #endif
154 break;
156 case DLL_PROCESS_DETACH:
158 DeleteObject( PSDRV_DefaultFont );
159 HeapDestroy( PSDRV_Heap );
160 #ifdef HAVE_CUPS_CUPS_H
161 if (cupshandle && (cupshandle != (void*)-1)) {
162 wine_dlclose(cupshandle, NULL, 0);
163 cupshandle = NULL;
165 #endif
166 break;
169 return TRUE;
173 static void PSDRV_UpdateDevCaps( PSDRV_PDEVICE *physDev )
175 PAGESIZE *page;
176 INT width = 0, height = 0;
178 if(physDev->Devmode->dmPublic.dmFields & DM_PAPERSIZE) {
179 for(page = physDev->pi->ppd->PageSizes; page; page = page->next) {
180 if(page->WinPage == physDev->Devmode->dmPublic.u1.s1.dmPaperSize)
181 break;
184 if(!page) {
185 FIXME("Can't find page\n");
186 physDev->ImageableArea.left = 0;
187 physDev->ImageableArea.right = 0;
188 physDev->ImageableArea.bottom = 0;
189 physDev->ImageableArea.top = 0;
190 physDev->PageSize.cx = 0;
191 physDev->PageSize.cy = 0;
192 } else if(page->ImageableArea) {
193 /* physDev sizes in device units; ppd sizes in 1/72" */
194 physDev->ImageableArea.left = page->ImageableArea->llx *
195 physDev->logPixelsX / 72;
196 physDev->ImageableArea.right = page->ImageableArea->urx *
197 physDev->logPixelsX / 72;
198 physDev->ImageableArea.bottom = page->ImageableArea->lly *
199 physDev->logPixelsY / 72;
200 physDev->ImageableArea.top = page->ImageableArea->ury *
201 physDev->logPixelsY / 72;
202 physDev->PageSize.cx = page->PaperDimension->x *
203 physDev->logPixelsX / 72;
204 physDev->PageSize.cy = page->PaperDimension->y *
205 physDev->logPixelsY / 72;
206 } else {
207 physDev->ImageableArea.left = physDev->ImageableArea.bottom = 0;
208 physDev->ImageableArea.right = physDev->PageSize.cx =
209 page->PaperDimension->x * physDev->logPixelsX / 72;
210 physDev->ImageableArea.top = physDev->PageSize.cy =
211 page->PaperDimension->y * physDev->logPixelsY / 72;
213 } else if((physDev->Devmode->dmPublic.dmFields & DM_PAPERLENGTH) &&
214 (physDev->Devmode->dmPublic.dmFields & DM_PAPERWIDTH)) {
215 /* physDev sizes in device units; Devmode sizes in 1/10 mm */
216 physDev->ImageableArea.left = physDev->ImageableArea.bottom = 0;
217 physDev->ImageableArea.right = physDev->PageSize.cx =
218 physDev->Devmode->dmPublic.u1.s1.dmPaperWidth *
219 physDev->logPixelsX / 254;
220 physDev->ImageableArea.top = physDev->PageSize.cy =
221 physDev->Devmode->dmPublic.u1.s1.dmPaperLength *
222 physDev->logPixelsY / 254;
223 } else {
224 FIXME("Odd dmFields %x\n", physDev->Devmode->dmPublic.dmFields);
225 physDev->ImageableArea.left = 0;
226 physDev->ImageableArea.right = 0;
227 physDev->ImageableArea.bottom = 0;
228 physDev->ImageableArea.top = 0;
229 physDev->PageSize.cx = 0;
230 physDev->PageSize.cy = 0;
233 TRACE("ImageableArea = %d,%d - %d,%d: PageSize = %dx%d\n",
234 physDev->ImageableArea.left, physDev->ImageableArea.bottom,
235 physDev->ImageableArea.right, physDev->ImageableArea.top,
236 physDev->PageSize.cx, physDev->PageSize.cy);
238 /* these are in device units */
239 width = physDev->ImageableArea.right - physDev->ImageableArea.left;
240 height = physDev->ImageableArea.top - physDev->ImageableArea.bottom;
242 if(physDev->Devmode->dmPublic.u1.s1.dmOrientation == DMORIENT_PORTRAIT) {
243 physDev->horzRes = width;
244 physDev->vertRes = height;
245 } else {
246 physDev->horzRes = height;
247 physDev->vertRes = width;
250 /* these are in mm */
251 physDev->horzSize = (physDev->horzRes * 25.4) / physDev->logPixelsX;
252 physDev->vertSize = (physDev->vertRes * 25.4) / physDev->logPixelsY;
254 TRACE("devcaps: horzSize = %dmm, vertSize = %dmm, "
255 "horzRes = %d, vertRes = %d\n",
256 physDev->horzSize, physDev->vertSize,
257 physDev->horzRes, physDev->vertRes);
261 /***********************************************************
262 * DEVMODEdupWtoA
264 * Creates an ascii copy of supplied devmode on heap
266 * Copied from dlls/winspool/info.c until full unicodification
268 static LPDEVMODEA DEVMODEdupWtoA(HANDLE heap, const DEVMODEW *dmW)
270 LPDEVMODEA dmA;
271 DWORD size;
272 BOOL Formname;
273 ptrdiff_t off_formname = (const char *)dmW->dmFormName - (const char *)dmW;
275 if(!dmW) return NULL;
276 Formname = (dmW->dmSize > off_formname);
277 size = dmW->dmSize - CCHDEVICENAME - (Formname ? CCHFORMNAME : 0);
278 dmA = HeapAlloc(heap, HEAP_ZERO_MEMORY, size + dmW->dmDriverExtra);
279 WideCharToMultiByte(CP_ACP, 0, dmW->dmDeviceName, -1, (LPSTR)dmA->dmDeviceName,
280 CCHDEVICENAME, NULL, NULL);
281 if(!Formname) {
282 memcpy(&dmA->dmSpecVersion, &dmW->dmSpecVersion,
283 dmW->dmSize - CCHDEVICENAME * sizeof(WCHAR));
284 } else {
285 memcpy(&dmA->dmSpecVersion, &dmW->dmSpecVersion,
286 off_formname - CCHDEVICENAME * sizeof(WCHAR));
287 WideCharToMultiByte(CP_ACP, 0, dmW->dmFormName, -1, (LPSTR)dmA->dmFormName,
288 CCHFORMNAME, NULL, NULL);
289 memcpy(&dmA->dmLogPixels, &dmW->dmLogPixels, dmW->dmSize -
290 (off_formname + CCHFORMNAME * sizeof(WCHAR)));
292 dmA->dmSize = size;
293 memcpy((char *)dmA + dmA->dmSize, (const char *)dmW + dmW->dmSize,
294 dmW->dmDriverExtra);
295 return dmA;
299 /**********************************************************************
300 * PSDRV_CreateDC
302 BOOL PSDRV_CreateDC( HDC hdc, PSDRV_PDEVICE **pdev, LPCWSTR driver, LPCWSTR device,
303 LPCWSTR output, const DEVMODEW* initData )
305 PSDRV_PDEVICE *physDev;
306 PRINTERINFO *pi;
307 char *deviceA;
309 /* If no device name was specified, retrieve the device name
310 * from the DEVMODE structure from the DC's physDev.
311 * (See CreateCompatibleDC) */
312 if ( !device && *pdev )
314 physDev = *pdev;
315 deviceA = HeapAlloc(GetProcessHeap(), 0, CCHDEVICENAME);
316 lstrcpynA(deviceA, (LPCSTR)physDev->Devmode->dmPublic.dmDeviceName, CCHDEVICENAME);
318 else
320 DWORD len = WideCharToMultiByte(CP_ACP, 0, device, -1, NULL, 0, NULL, NULL);
321 deviceA = HeapAlloc(GetProcessHeap(), 0, len);
322 WideCharToMultiByte(CP_ACP, 0, device, -1, deviceA, len, NULL, NULL);
324 pi = PSDRV_FindPrinterInfo(deviceA);
326 HeapFree(GetProcessHeap(), 0, deviceA);
327 deviceA = NULL;
329 TRACE("(%s %s %s %p)\n", debugstr_w(driver), debugstr_w(device),
330 debugstr_w(output), initData);
332 if(!pi) return FALSE;
334 if(!pi->Fonts) {
335 MESSAGE("To use WINEPS you need to install some AFM files.\n");
336 return FALSE;
339 physDev = HeapAlloc( PSDRV_Heap, HEAP_ZERO_MEMORY, sizeof(*physDev) );
340 if (!physDev) return FALSE;
341 *pdev = physDev;
342 physDev->hdc = hdc;
344 physDev->pi = pi;
346 physDev->Devmode = HeapAlloc( PSDRV_Heap, 0, sizeof(PSDRV_DEVMODEA) );
347 if(!physDev->Devmode) {
348 HeapFree( PSDRV_Heap, 0, physDev );
349 return FALSE;
352 memcpy( physDev->Devmode, pi->Devmode, sizeof(PSDRV_DEVMODEA) );
354 physDev->logPixelsX = physDev->pi->ppd->DefaultResolution;
355 physDev->logPixelsY = physDev->pi->ppd->DefaultResolution;
357 if (output) {
358 INT len = WideCharToMultiByte( CP_ACP, 0, output, -1, NULL, 0, NULL, NULL );
359 if ((physDev->job.output = HeapAlloc( PSDRV_Heap, 0, len )))
360 WideCharToMultiByte( CP_ACP, 0, output, -1, physDev->job.output, len, NULL, NULL );
361 } else
362 physDev->job.output = NULL;
363 physDev->job.hJob = 0;
365 if(initData) {
366 DEVMODEA *devmodeA = DEVMODEdupWtoA(PSDRV_Heap, initData);
367 PSDRV_MergeDevmodes(physDev->Devmode, (PSDRV_DEVMODEA *)devmodeA, pi);
368 HeapFree(PSDRV_Heap, 0, devmodeA);
371 PSDRV_UpdateDevCaps(physDev);
372 SelectObject( hdc, PSDRV_DefaultFont );
373 return TRUE;
378 /**********************************************************************
379 * PSDRV_DeleteDC
381 BOOL PSDRV_DeleteDC( PSDRV_PDEVICE *physDev )
383 TRACE("\n");
385 HeapFree( PSDRV_Heap, 0, physDev->Devmode );
386 HeapFree( PSDRV_Heap, 0, physDev->job.output );
387 HeapFree( PSDRV_Heap, 0, physDev );
389 return TRUE;
393 /**********************************************************************
394 * ResetDC (WINEPS.@)
396 HDC PSDRV_ResetDC( PSDRV_PDEVICE *physDev, const DEVMODEW *lpInitData )
398 if(lpInitData) {
399 HRGN hrgn;
400 DEVMODEA *devmodeA = DEVMODEdupWtoA(PSDRV_Heap, lpInitData);
401 PSDRV_MergeDevmodes(physDev->Devmode, (PSDRV_DEVMODEA *)devmodeA, physDev->pi);
402 HeapFree(PSDRV_Heap, 0, devmodeA);
403 PSDRV_UpdateDevCaps(physDev);
404 hrgn = CreateRectRgn(0, 0, physDev->horzRes, physDev->vertRes);
405 SelectVisRgn16(HDC_16(physDev->hdc), HRGN_16(hrgn));
406 DeleteObject(hrgn);
408 return physDev->hdc;
411 /***********************************************************************
412 * GetDeviceCaps (WINEPS.@)
414 INT PSDRV_GetDeviceCaps( PSDRV_PDEVICE *physDev, INT cap )
416 switch(cap)
418 case DRIVERVERSION:
419 return 0;
420 case TECHNOLOGY:
421 return DT_RASPRINTER;
422 case HORZSIZE:
423 return MulDiv(physDev->horzSize, 100,
424 physDev->Devmode->dmPublic.dmScale);
425 case VERTSIZE:
426 return MulDiv(physDev->vertSize, 100,
427 physDev->Devmode->dmPublic.dmScale);
428 case HORZRES:
429 return physDev->horzRes;
430 case VERTRES:
431 return physDev->vertRes;
432 case BITSPIXEL:
433 return physDev->pi->ppd->ColorDevice ? 8 : 1;
434 case PLANES:
435 return 1;
436 case NUMBRUSHES:
437 return -1;
438 case NUMPENS:
439 return 10;
440 case NUMMARKERS:
441 return 0;
442 case NUMFONTS:
443 return 39;
444 case NUMCOLORS:
445 return (physDev->pi->ppd->ColorDevice ? 256 : -1);
446 case PDEVICESIZE:
447 return sizeof(PSDRV_PDEVICE);
448 case CURVECAPS:
449 return (CC_CIRCLES | CC_PIE | CC_CHORD | CC_ELLIPSES | CC_WIDE |
450 CC_STYLED | CC_WIDESTYLED | CC_INTERIORS | CC_ROUNDRECT);
451 case LINECAPS:
452 return (LC_POLYLINE | LC_MARKER | LC_POLYMARKER | LC_WIDE |
453 LC_STYLED | LC_WIDESTYLED | LC_INTERIORS);
454 case POLYGONALCAPS:
455 return (PC_POLYGON | PC_RECTANGLE | PC_WINDPOLYGON | PC_SCANLINE |
456 PC_WIDE | PC_STYLED | PC_WIDESTYLED | PC_INTERIORS);
457 case TEXTCAPS:
458 return TC_CR_ANY | TC_VA_ABLE; /* psdrv 0x59f7 */
459 case CLIPCAPS:
460 return CP_RECTANGLE;
461 case RASTERCAPS:
462 return (RC_BITBLT | RC_BITMAP64 | RC_GDI20_OUTPUT | RC_DIBTODEV |
463 RC_STRETCHBLT | RC_STRETCHDIB); /* psdrv 0x6e99 */
464 /* Are aspect[XY] and logPixels[XY] correct? */
465 /* Need to handle different res in x and y => fix ppd */
466 case ASPECTX:
467 case ASPECTY:
468 return physDev->pi->ppd->DefaultResolution;
469 case ASPECTXY:
470 return (int)hypot( (double)physDev->pi->ppd->DefaultResolution,
471 (double)physDev->pi->ppd->DefaultResolution );
472 case LOGPIXELSX:
473 return MulDiv(physDev->logPixelsX,
474 physDev->Devmode->dmPublic.dmScale, 100);
475 case LOGPIXELSY:
476 return MulDiv(physDev->logPixelsY,
477 physDev->Devmode->dmPublic.dmScale, 100);
478 case SIZEPALETTE:
479 return 0;
480 case NUMRESERVED:
481 return 0;
482 case COLORRES:
483 return 0;
484 case PHYSICALWIDTH:
485 return (physDev->Devmode->dmPublic.u1.s1.dmOrientation == DMORIENT_LANDSCAPE) ?
486 physDev->PageSize.cy : physDev->PageSize.cx;
487 case PHYSICALHEIGHT:
488 return (physDev->Devmode->dmPublic.u1.s1.dmOrientation == DMORIENT_LANDSCAPE) ?
489 physDev->PageSize.cx : physDev->PageSize.cy;
490 case PHYSICALOFFSETX:
491 if(physDev->Devmode->dmPublic.u1.s1.dmOrientation == DMORIENT_LANDSCAPE) {
492 if(physDev->pi->ppd->LandscapeOrientation == -90)
493 return physDev->PageSize.cy - physDev->ImageableArea.top;
494 else
495 return physDev->ImageableArea.bottom;
497 return physDev->ImageableArea.left;
499 case PHYSICALOFFSETY:
500 if(physDev->Devmode->dmPublic.u1.s1.dmOrientation == DMORIENT_LANDSCAPE) {
501 if(physDev->pi->ppd->LandscapeOrientation == -90)
502 return physDev->PageSize.cx - physDev->ImageableArea.right;
503 else
504 return physDev->ImageableArea.left;
506 return physDev->PageSize.cy - physDev->ImageableArea.top;
508 case SCALINGFACTORX:
509 case SCALINGFACTORY:
510 case VREFRESH:
511 case DESKTOPVERTRES:
512 case DESKTOPHORZRES:
513 case BLTALIGNMENT:
514 return 0;
515 default:
516 FIXME("(%p): unsupported capability %d, will return 0\n", physDev->hdc, cap );
517 return 0;
522 /**********************************************************************
523 * PSDRV_FindPrinterInfo
525 PRINTERINFO *PSDRV_FindPrinterInfo(LPCSTR name)
527 static PRINTERINFO *PSDRV_PrinterList;
528 DWORD type = REG_BINARY, needed, res, dwPaperSize;
529 PRINTERINFO *pi = PSDRV_PrinterList, **last = &PSDRV_PrinterList;
530 FONTNAME *font;
531 const AFM *afm;
532 HANDLE hPrinter;
533 const char *ppd = NULL;
534 DWORD ppdType;
535 char* ppdFileName = NULL;
536 HKEY hkey;
537 BOOL using_default_devmode = FALSE;
538 static CHAR paper_size[] = "Paper Size";
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 /* Use Get|SetPrinterDataExA instead? */
559 res = DrvGetPrinterData16((LPSTR)name, (LPSTR)INT_PD_DEFAULT_DEVMODE, &type,
560 NULL, 0, &needed );
562 if(res == ERROR_INVALID_PRINTER_NAME || needed != sizeof(DefaultDevmode)) {
563 pi->Devmode = HeapAlloc( PSDRV_Heap, 0, sizeof(DefaultDevmode) );
564 if (pi->Devmode == NULL)
565 goto cleanup;
566 memcpy(pi->Devmode, &DefaultDevmode, sizeof(DefaultDevmode) );
567 lstrcpynA((LPSTR)pi->Devmode->dmPublic.dmDeviceName,name,CCHDEVICENAME);
568 using_default_devmode = TRUE;
570 /* need to do something here AddPrinter?? */
572 else {
573 pi->Devmode = HeapAlloc( PSDRV_Heap, 0, needed );
574 DrvGetPrinterData16((LPSTR)name, (LPSTR)INT_PD_DEFAULT_DEVMODE, &type,
575 (LPBYTE)pi->Devmode, needed, &needed);
578 if (OpenPrinterA (pi->FriendlyName, &hPrinter, NULL) == 0) {
579 ERR ("OpenPrinterA failed with code %i\n", GetLastError ());
580 goto cleanup;
583 #ifdef HAVE_CUPS_CUPS_H
584 if (cupshandle != (void*)-1) {
585 typeof(cupsGetPPD) * pcupsGetPPD = NULL;
587 pcupsGetPPD = wine_dlsym(cupshandle, "cupsGetPPD", NULL, 0);
588 if (pcupsGetPPD) {
589 ppd = pcupsGetPPD(name);
591 if (ppd) {
592 needed=strlen(ppd)+1;
593 ppdFileName=HeapAlloc(PSDRV_Heap, 0, needed);
594 memcpy(ppdFileName, ppd, needed);
595 ppdType=REG_SZ;
596 res = ERROR_SUCCESS;
597 /* we should unlink() that file later */
598 } else {
599 res = ERROR_FILE_NOT_FOUND;
600 WARN("Did not find ppd for %s\n",name);
604 #endif
605 if (!ppdFileName) {
606 static CHAR ppd_file[] = "PPD File";
608 res = GetPrinterDataA(hPrinter, ppd_file, NULL, NULL, 0, &needed);
609 if ((res==ERROR_SUCCESS) || (res==ERROR_MORE_DATA)) {
610 ppdFileName=HeapAlloc(PSDRV_Heap, 0, needed);
611 res = GetPrinterDataA(hPrinter, ppd_file, &ppdType, (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 ppdFileName=HeapAlloc(PSDRV_Heap, 0, needed);
631 RegQueryValueExA(hkey, value_name, 0, &ppdType, (LPBYTE)ppdFileName, &needed);
633 RegCloseKey(hkey);
636 if (!ppdFileName)
638 const char *data_dir, *filename;
640 if ((data_dir = wine_get_data_dir())) filename = "/generic.ppd";
641 else if ((data_dir = wine_get_build_dir())) filename = "/dlls/wineps.drv/generic.ppd";
642 else
644 res = ERROR_FILE_NOT_FOUND;
645 ERR ("Error %i getting PPD file name for printer '%s'\n", res, name);
646 goto closeprinter;
648 ppdFileName = HeapAlloc( PSDRV_Heap, 0, strlen(data_dir) + strlen(filename) + 1 );
649 strcpy( ppdFileName, data_dir );
650 strcat( ppdFileName, filename );
651 } else {
652 res = ERROR_SUCCESS;
653 if (ppdType==REG_EXPAND_SZ) {
654 char* tmp;
656 /* Expand environment variable references */
657 needed=ExpandEnvironmentStringsA(ppdFileName,NULL,0);
658 tmp=HeapAlloc(PSDRV_Heap, 0, needed);
659 ExpandEnvironmentStringsA(ppdFileName,tmp,needed);
660 HeapFree(PSDRV_Heap, 0, ppdFileName);
661 ppdFileName=tmp;
665 pi->ppd = PSDRV_ParsePPD(ppdFileName);
666 if(!pi->ppd) {
667 MESSAGE("Couldn't find PPD file '%s', expect a crash now!\n",
668 ppdFileName);
669 goto closeprinter;
672 /* Some gimp-print ppd files don't contain a DefaultResolution line
673 set it to 300 if it's not specified */
674 if(pi->ppd->DefaultResolution == 0)
675 pi->ppd->DefaultResolution = 300;
677 if(using_default_devmode) {
678 DWORD papersize;
680 if(GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_IPAPERSIZE | LOCALE_RETURN_NUMBER,
681 (LPWSTR)&papersize, sizeof(papersize)/sizeof(WCHAR))) {
682 PSDRV_DEVMODEA dm;
683 memset(&dm, 0, sizeof(dm));
684 dm.dmPublic.dmFields = DM_PAPERSIZE;
685 dm.dmPublic.u1.s1.dmPaperSize = papersize;
686 PSDRV_MergeDevmodes(pi->Devmode, &dm, pi);
688 DrvSetPrinterData16((LPSTR)name, (LPSTR)INT_PD_DEFAULT_DEVMODE,
689 REG_BINARY, (LPBYTE)pi->Devmode, sizeof(DefaultDevmode) );
692 if(pi->ppd->DefaultPageSize) { /* We'll let the ppd override the devmode */
693 PSDRV_DEVMODEA dm;
694 memset(&dm, 0, sizeof(dm));
695 dm.dmPublic.dmFields = DM_PAPERSIZE;
696 dm.dmPublic.u1.s1.dmPaperSize = pi->ppd->DefaultPageSize->WinPage;
697 PSDRV_MergeDevmodes(pi->Devmode, &dm, pi);
701 * This is a hack. The default paper size should be read in as part of
702 * the Devmode structure, but Wine doesn't currently provide a convenient
703 * way to configure printers.
705 res = GetPrinterDataA (hPrinter, paper_size, NULL, (LPBYTE) &dwPaperSize,
706 sizeof (DWORD), &needed);
707 if (res == ERROR_SUCCESS)
708 pi->Devmode->dmPublic.u1.s1.dmPaperSize = (SHORT) dwPaperSize;
709 else if (res == ERROR_FILE_NOT_FOUND)
710 TRACE ("No 'Paper Size' for printer '%s'\n", name);
711 else {
712 ERR ("GetPrinterDataA returned %i\n", res);
713 goto closeprinter;
716 /* Duplex is indicated by the setting of the DM_DUPLEX bit in dmFields.
717 WinDuplex == 0 is a special case which means that the ppd has a
718 *DefaultDuplex: NotCapable entry. In this case we'll try not to confuse
719 apps and set dmDuplex to DMDUP_SIMPLEX but leave the DM_DUPLEX clear.
720 PSDRV_WriteHeader understands this and copes. */
721 pi->Devmode->dmPublic.dmFields &= ~DM_DUPLEX;
722 if(pi->ppd->DefaultDuplex) {
723 pi->Devmode->dmPublic.dmDuplex = pi->ppd->DefaultDuplex->WinDuplex;
724 if(pi->Devmode->dmPublic.dmDuplex != 0)
725 pi->Devmode->dmPublic.dmFields |= DM_DUPLEX;
726 else
727 pi->Devmode->dmPublic.dmDuplex = DMDUP_SIMPLEX;
730 res = EnumPrinterDataExA (hPrinter, "PrinterDriverData\\FontSubTable", NULL,
731 0, &needed, &pi->FontSubTableSize);
732 if (res == ERROR_SUCCESS || res == ERROR_FILE_NOT_FOUND) {
733 TRACE ("No 'FontSubTable' for printer '%s'\n", name);
735 else if (res == ERROR_MORE_DATA) {
736 pi->FontSubTable = HeapAlloc (PSDRV_Heap, 0, needed);
737 if (pi->FontSubTable == NULL) {
738 ERR ("Failed to allocate %i bytes from heap\n", needed);
739 goto closeprinter;
742 res = EnumPrinterDataExA (hPrinter, "PrinterDriverData\\FontSubTable",
743 (LPBYTE) pi->FontSubTable, needed, &needed,
744 &pi->FontSubTableSize);
745 if (res != ERROR_SUCCESS) {
746 ERR ("EnumPrinterDataExA returned %i\n", res);
747 goto closeprinter;
750 else {
751 ERR("EnumPrinterDataExA returned %i\n", res);
752 goto closeprinter;
755 if (ClosePrinter (hPrinter) == 0) {
756 ERR ("ClosePrinter failed with code %i\n", GetLastError ());
757 goto cleanup;
760 pi->next = NULL;
761 pi->Fonts = NULL;
763 for(font = pi->ppd->InstalledFonts; font; font = font->next) {
764 afm = PSDRV_FindAFMinList(PSDRV_AFMFontList, font->Name);
765 if(!afm) {
766 TRACE( "Couldn't find AFM file for installed printer font '%s' - "
767 "ignoring\n", font->Name);
769 else {
770 BOOL added;
771 if (PSDRV_AddAFMtoList(&pi->Fonts, afm, &added) == FALSE) {
772 PSDRV_FreeAFMList(pi->Fonts);
773 goto cleanup;
778 if (ppd) unlink(ppd);
779 return pi;
781 closeprinter:
782 ClosePrinter(hPrinter);
783 cleanup:
784 HeapFree(PSDRV_Heap, 0, ppdFileName);
785 HeapFree(PSDRV_Heap, 0, pi->FontSubTable);
786 HeapFree(PSDRV_Heap, 0, pi->FriendlyName);
787 HeapFree(PSDRV_Heap, 0, pi->Devmode);
788 fail:
789 HeapFree(PSDRV_Heap, 0, pi);
790 if (ppd) unlink(ppd);
791 *last = NULL;
792 return NULL;