Added YUV routines needed for v4l driver, and in the future possibly
[wine/gsoc-2012-control.git] / dlls / wineps / init.c
blobf5d2e74f5a6c88e13cfe9cd92c996644db93dabb
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 HINSTANCE PSDRV_hInstance = 0;
105 HANDLE PSDRV_Heap = 0;
107 static HFONT PSDRV_DefaultFont = 0;
108 static LOGFONTA DefaultLogFont = {
109 100, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, ANSI_CHARSET, 0, 0,
110 DEFAULT_QUALITY, FIXED_PITCH | FF_MODERN, ""
113 /*********************************************************************
114 * DllMain
116 * Initializes font metrics and registers driver. wineps dll entry point.
119 BOOL WINAPI DllMain( HINSTANCE hinst, DWORD reason, LPVOID reserved )
121 TRACE("(%p, 0x%08lx, %p)\n", hinst, reason, reserved);
123 switch(reason) {
125 case DLL_PROCESS_ATTACH:
126 PSDRV_hInstance = hinst;
127 DisableThreadLibraryCalls(hinst);
129 PSDRV_Heap = HeapCreate(0, 0x10000, 0);
130 if (PSDRV_Heap == NULL)
131 return FALSE;
133 if (PSDRV_GetFontMetrics() == FALSE) {
134 HeapDestroy(PSDRV_Heap);
135 return FALSE;
138 PSDRV_DefaultFont = CreateFontIndirectA(&DefaultLogFont);
139 if (PSDRV_DefaultFont == NULL) {
140 HeapDestroy(PSDRV_Heap);
141 return FALSE;
143 #ifdef HAVE_CUPS_CUPS_H
144 /* dynamically load CUPS if not yet loaded */
145 if (!cupshandle) {
146 cupshandle = wine_dlopen(SONAME_LIBCUPS, RTLD_NOW, NULL, 0);
147 if (!cupshandle) cupshandle = (void*)-1;
149 #endif
150 break;
152 case DLL_PROCESS_DETACH:
154 DeleteObject( PSDRV_DefaultFont );
155 HeapDestroy( PSDRV_Heap );
156 #ifdef HAVE_CUPS_CUPS_H
157 if (cupshandle && (cupshandle != (void*)-1)) {
158 wine_dlclose(cupshandle, NULL, 0);
159 cupshandle = NULL;
161 #endif
162 break;
165 return TRUE;
169 static void PSDRV_UpdateDevCaps( PSDRV_PDEVICE *physDev )
171 PAGESIZE *page;
172 INT width = 0, height = 0;
174 if(physDev->Devmode->dmPublic.dmFields & DM_PAPERSIZE) {
175 for(page = physDev->pi->ppd->PageSizes; page; page = page->next) {
176 if(page->WinPage == physDev->Devmode->dmPublic.u1.s1.dmPaperSize)
177 break;
180 if(!page) {
181 FIXME("Can't find page\n");
182 physDev->ImageableArea.left = 0;
183 physDev->ImageableArea.right = 0;
184 physDev->ImageableArea.bottom = 0;
185 physDev->ImageableArea.top = 0;
186 physDev->PageSize.cx = 0;
187 physDev->PageSize.cy = 0;
188 } else if(page->ImageableArea) {
189 /* physDev sizes in device units; ppd sizes in 1/72" */
190 physDev->ImageableArea.left = page->ImageableArea->llx *
191 physDev->logPixelsX / 72;
192 physDev->ImageableArea.right = page->ImageableArea->urx *
193 physDev->logPixelsX / 72;
194 physDev->ImageableArea.bottom = page->ImageableArea->lly *
195 physDev->logPixelsY / 72;
196 physDev->ImageableArea.top = page->ImageableArea->ury *
197 physDev->logPixelsY / 72;
198 physDev->PageSize.cx = page->PaperDimension->x *
199 physDev->logPixelsX / 72;
200 physDev->PageSize.cy = page->PaperDimension->y *
201 physDev->logPixelsY / 72;
202 } else {
203 physDev->ImageableArea.left = physDev->ImageableArea.bottom = 0;
204 physDev->ImageableArea.right = physDev->PageSize.cx =
205 page->PaperDimension->x * physDev->logPixelsX / 72;
206 physDev->ImageableArea.top = physDev->PageSize.cy =
207 page->PaperDimension->y * physDev->logPixelsY / 72;
209 } else if((physDev->Devmode->dmPublic.dmFields & DM_PAPERLENGTH) &&
210 (physDev->Devmode->dmPublic.dmFields & DM_PAPERWIDTH)) {
211 /* physDev sizes in device units; Devmode sizes in 1/10 mm */
212 physDev->ImageableArea.left = physDev->ImageableArea.bottom = 0;
213 physDev->ImageableArea.right = physDev->PageSize.cx =
214 physDev->Devmode->dmPublic.u1.s1.dmPaperWidth *
215 physDev->logPixelsX / 254;
216 physDev->ImageableArea.top = physDev->PageSize.cy =
217 physDev->Devmode->dmPublic.u1.s1.dmPaperLength *
218 physDev->logPixelsY / 254;
219 } else {
220 FIXME("Odd dmFields %lx\n", physDev->Devmode->dmPublic.dmFields);
221 physDev->ImageableArea.left = 0;
222 physDev->ImageableArea.right = 0;
223 physDev->ImageableArea.bottom = 0;
224 physDev->ImageableArea.top = 0;
225 physDev->PageSize.cx = 0;
226 physDev->PageSize.cy = 0;
229 TRACE("ImageableArea = %ld,%ld - %ld,%ld: PageSize = %ldx%ld\n",
230 physDev->ImageableArea.left, physDev->ImageableArea.bottom,
231 physDev->ImageableArea.right, physDev->ImageableArea.top,
232 physDev->PageSize.cx, physDev->PageSize.cy);
234 /* these are in device units */
235 width = physDev->ImageableArea.right - physDev->ImageableArea.left;
236 height = physDev->ImageableArea.top - physDev->ImageableArea.bottom;
238 if(physDev->Devmode->dmPublic.u1.s1.dmOrientation == DMORIENT_PORTRAIT) {
239 physDev->horzRes = width;
240 physDev->vertRes = height;
241 } else {
242 physDev->horzRes = height;
243 physDev->vertRes = width;
246 /* these are in mm */
247 physDev->horzSize = (physDev->horzRes * 25.4) / physDev->logPixelsX;
248 physDev->vertSize = (physDev->vertRes * 25.4) / physDev->logPixelsY;
250 TRACE("devcaps: horzSize = %dmm, vertSize = %dmm, "
251 "horzRes = %d, vertRes = %d\n",
252 physDev->horzSize, physDev->vertSize,
253 physDev->horzRes, physDev->vertRes);
257 /***********************************************************
258 * DEVMODEdupWtoA
260 * Creates an ascii copy of supplied devmode on heap
262 * Copied from dlls/winspool/info.c until full unicodification
264 static LPDEVMODEA DEVMODEdupWtoA(HANDLE heap, const DEVMODEW *dmW)
266 LPDEVMODEA dmA;
267 DWORD size;
268 BOOL Formname;
269 ptrdiff_t off_formname = (const char *)dmW->dmFormName - (const char *)dmW;
271 if(!dmW) return NULL;
272 Formname = (dmW->dmSize > off_formname);
273 size = dmW->dmSize - CCHDEVICENAME - (Formname ? CCHFORMNAME : 0);
274 dmA = HeapAlloc(heap, HEAP_ZERO_MEMORY, size + dmW->dmDriverExtra);
275 WideCharToMultiByte(CP_ACP, 0, dmW->dmDeviceName, -1, dmA->dmDeviceName,
276 CCHDEVICENAME, NULL, NULL);
277 if(!Formname) {
278 memcpy(&dmA->dmSpecVersion, &dmW->dmSpecVersion,
279 dmW->dmSize - CCHDEVICENAME * sizeof(WCHAR));
280 } else {
281 memcpy(&dmA->dmSpecVersion, &dmW->dmSpecVersion,
282 off_formname - CCHDEVICENAME * sizeof(WCHAR));
283 WideCharToMultiByte(CP_ACP, 0, dmW->dmFormName, -1, dmA->dmFormName,
284 CCHFORMNAME, NULL, NULL);
285 memcpy(&dmA->dmLogPixels, &dmW->dmLogPixels, dmW->dmSize -
286 (off_formname + CCHFORMNAME * sizeof(WCHAR)));
288 dmA->dmSize = size;
289 memcpy((char *)dmA + dmA->dmSize, (const char *)dmW + dmW->dmSize,
290 dmW->dmDriverExtra);
291 return dmA;
295 /**********************************************************************
296 * PSDRV_CreateDC
298 BOOL PSDRV_CreateDC( HDC hdc, PSDRV_PDEVICE **pdev, LPCWSTR driver, LPCWSTR device,
299 LPCWSTR output, const DEVMODEW* initData )
301 PSDRV_PDEVICE *physDev;
302 PRINTERINFO *pi;
303 char deviceA[CCHDEVICENAME];
305 /* If no device name was specified, retrieve the device name
306 * from the DEVMODE structure from the DC's physDev.
307 * (See CreateCompatibleDC) */
308 if ( !device && *pdev )
310 physDev = *pdev;
311 strcpy(deviceA, physDev->Devmode->dmPublic.dmDeviceName);
313 else
314 WideCharToMultiByte(CP_ACP, 0, device, -1, deviceA, sizeof(deviceA), NULL, NULL);
315 pi = PSDRV_FindPrinterInfo(deviceA);
317 TRACE("(%s %s %s %p)\n", debugstr_w(driver), debugstr_w(device),
318 debugstr_w(output), initData);
320 if(!pi) return FALSE;
322 if(!pi->Fonts) {
323 MESSAGE("To use WINEPS you need to install some AFM files.\n");
324 return FALSE;
327 physDev = HeapAlloc( PSDRV_Heap, HEAP_ZERO_MEMORY, sizeof(*physDev) );
328 if (!physDev) return FALSE;
329 *pdev = physDev;
330 physDev->hdc = hdc;
332 physDev->pi = pi;
334 physDev->Devmode = HeapAlloc( PSDRV_Heap, 0, 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 HeapFree( PSDRV_Heap, 0, physDev->job.output );
373 HeapFree( PSDRV_Heap, 0, physDev );
375 return TRUE;
379 /**********************************************************************
380 * ResetDC (WINEPS.@)
382 HDC PSDRV_ResetDC( PSDRV_PDEVICE *physDev, const DEVMODEW *lpInitData )
384 if(lpInitData) {
385 HRGN hrgn;
386 DEVMODEA *devmodeA = DEVMODEdupWtoA(PSDRV_Heap, lpInitData);
387 PSDRV_MergeDevmodes(physDev->Devmode, (PSDRV_DEVMODEA *)devmodeA, physDev->pi);
388 HeapFree(PSDRV_Heap, 0, devmodeA);
389 PSDRV_UpdateDevCaps(physDev);
390 hrgn = CreateRectRgn(0, 0, physDev->horzRes, physDev->vertRes);
391 SelectVisRgn16(HDC_16(physDev->hdc), HRGN_16(hrgn));
392 DeleteObject(hrgn);
394 return physDev->hdc;
397 /***********************************************************************
398 * GetDeviceCaps (WINEPS.@)
400 INT PSDRV_GetDeviceCaps( PSDRV_PDEVICE *physDev, INT cap )
402 switch(cap)
404 case DRIVERVERSION:
405 return 0;
406 case TECHNOLOGY:
407 return DT_RASPRINTER;
408 case HORZSIZE:
409 return MulDiv(physDev->horzSize, 100,
410 physDev->Devmode->dmPublic.dmScale);
411 case VERTSIZE:
412 return MulDiv(physDev->vertSize, 100,
413 physDev->Devmode->dmPublic.dmScale);
414 case HORZRES:
415 return physDev->horzRes;
416 case VERTRES:
417 return physDev->vertRes;
418 case BITSPIXEL:
419 return physDev->pi->ppd->ColorDevice ? 8 : 1;
420 case PLANES:
421 return 1;
422 case NUMBRUSHES:
423 return -1;
424 case NUMPENS:
425 return 10;
426 case NUMMARKERS:
427 return 0;
428 case NUMFONTS:
429 return 39;
430 case NUMCOLORS:
431 return (physDev->pi->ppd->ColorDevice ? 256 : -1);
432 case PDEVICESIZE:
433 return sizeof(PSDRV_PDEVICE);
434 case CURVECAPS:
435 return (CC_CIRCLES | CC_PIE | CC_CHORD | CC_ELLIPSES | CC_WIDE |
436 CC_STYLED | CC_WIDESTYLED | CC_INTERIORS | CC_ROUNDRECT);
437 case LINECAPS:
438 return (LC_POLYLINE | LC_MARKER | LC_POLYMARKER | LC_WIDE |
439 LC_STYLED | LC_WIDESTYLED | LC_INTERIORS);
440 case POLYGONALCAPS:
441 return (PC_POLYGON | PC_RECTANGLE | PC_WINDPOLYGON | PC_SCANLINE |
442 PC_WIDE | PC_STYLED | PC_WIDESTYLED | PC_INTERIORS);
443 case TEXTCAPS:
444 return TC_CR_ANY | TC_VA_ABLE; /* psdrv 0x59f7 */
445 case CLIPCAPS:
446 return CP_RECTANGLE;
447 case RASTERCAPS:
448 return (RC_BITBLT | RC_BITMAP64 | RC_GDI20_OUTPUT | RC_DIBTODEV |
449 RC_STRETCHBLT | RC_STRETCHDIB); /* psdrv 0x6e99 */
450 /* Are aspect[XY] and logPixels[XY] correct? */
451 /* Need to handle different res in x and y => fix ppd */
452 case ASPECTX:
453 case ASPECTY:
454 return physDev->pi->ppd->DefaultResolution;
455 case ASPECTXY:
456 return (int)hypot( (double)physDev->pi->ppd->DefaultResolution,
457 (double)physDev->pi->ppd->DefaultResolution );
458 case LOGPIXELSX:
459 return MulDiv(physDev->logPixelsX,
460 physDev->Devmode->dmPublic.dmScale, 100);
461 case LOGPIXELSY:
462 return MulDiv(physDev->logPixelsY,
463 physDev->Devmode->dmPublic.dmScale, 100);
464 case SIZEPALETTE:
465 return 0;
466 case NUMRESERVED:
467 return 0;
468 case COLORRES:
469 return 0;
470 case PHYSICALWIDTH:
471 return (physDev->Devmode->dmPublic.u1.s1.dmOrientation == DMORIENT_LANDSCAPE) ?
472 physDev->PageSize.cy : physDev->PageSize.cx;
473 case PHYSICALHEIGHT:
474 return (physDev->Devmode->dmPublic.u1.s1.dmOrientation == DMORIENT_LANDSCAPE) ?
475 physDev->PageSize.cx : physDev->PageSize.cy;
476 case PHYSICALOFFSETX:
477 if(physDev->Devmode->dmPublic.u1.s1.dmOrientation == DMORIENT_LANDSCAPE) {
478 if(physDev->pi->ppd->LandscapeOrientation == -90)
479 return physDev->PageSize.cy - physDev->ImageableArea.top;
480 else
481 return physDev->ImageableArea.bottom;
483 return physDev->ImageableArea.left;
485 case PHYSICALOFFSETY:
486 if(physDev->Devmode->dmPublic.u1.s1.dmOrientation == DMORIENT_LANDSCAPE) {
487 if(physDev->pi->ppd->LandscapeOrientation == -90)
488 return physDev->PageSize.cx - physDev->ImageableArea.right;
489 else
490 return physDev->ImageableArea.left;
492 return physDev->PageSize.cy - physDev->ImageableArea.top;
494 case SCALINGFACTORX:
495 case SCALINGFACTORY:
496 case VREFRESH:
497 case DESKTOPVERTRES:
498 case DESKTOPHORZRES:
499 case BTLALIGNMENT:
500 return 0;
501 default:
502 FIXME("(%p): unsupported capability %d, will return 0\n", physDev->hdc, cap );
503 return 0;
508 /**********************************************************************
509 * PSDRV_FindPrinterInfo
511 PRINTERINFO *PSDRV_FindPrinterInfo(LPCSTR name)
513 static PRINTERINFO *PSDRV_PrinterList;
514 DWORD type = REG_BINARY, needed, res, dwPaperSize;
515 PRINTERINFO *pi = PSDRV_PrinterList, **last = &PSDRV_PrinterList;
516 FONTNAME *font;
517 const AFM *afm;
518 HANDLE hPrinter;
519 const char *ppd = NULL;
520 DWORD ppdType;
521 char* ppdFileName = NULL;
522 HKEY hkey;
523 BOOL using_default_devmode = FALSE;
525 TRACE("'%s'\n", name);
528 * If this loop completes, last will point to the 'next' element of the
529 * final PRINTERINFO in the list
531 for( ; pi; last = &pi->next, pi = pi->next)
532 if(!strcmp(pi->FriendlyName, name))
533 return pi;
535 pi = *last = HeapAlloc( PSDRV_Heap, HEAP_ZERO_MEMORY, sizeof(*pi) );
536 if (pi == NULL)
537 return NULL;
539 if (!(pi->FriendlyName = HeapAlloc( PSDRV_Heap, 0, strlen(name)+1 ))) goto fail;
540 strcpy( pi->FriendlyName, name );
542 /* Use Get|SetPrinterDataExA instead? */
544 res = DrvGetPrinterData16((LPSTR)name, (LPSTR)INT_PD_DEFAULT_DEVMODE, &type,
545 NULL, 0, &needed );
547 if(res == ERROR_INVALID_PRINTER_NAME || needed != sizeof(DefaultDevmode)) {
548 pi->Devmode = HeapAlloc( PSDRV_Heap, 0, sizeof(DefaultDevmode) );
549 if (pi->Devmode == NULL)
550 goto cleanup;
551 memcpy(pi->Devmode, &DefaultDevmode, sizeof(DefaultDevmode) );
552 strcpy(pi->Devmode->dmPublic.dmDeviceName,name);
553 using_default_devmode = TRUE;
555 /* need to do something here AddPrinter?? */
557 else {
558 pi->Devmode = HeapAlloc( PSDRV_Heap, 0, needed );
559 DrvGetPrinterData16((LPSTR)name, (LPSTR)INT_PD_DEFAULT_DEVMODE, &type,
560 (LPBYTE)pi->Devmode, needed, &needed);
563 if (OpenPrinterA (pi->FriendlyName, &hPrinter, NULL) == 0) {
564 ERR ("OpenPrinterA failed with code %li\n", GetLastError ());
565 goto cleanup;
568 #ifdef HAVE_CUPS_CUPS_H
569 if (cupshandle != (void*)-1) {
570 typeof(cupsGetPPD) * pcupsGetPPD = NULL;
572 pcupsGetPPD = wine_dlsym(cupshandle, "cupsGetPPD", NULL, 0);
573 if (pcupsGetPPD) {
574 ppd = pcupsGetPPD(name);
576 if (ppd) {
577 needed=strlen(ppd)+1;
578 ppdFileName=HeapAlloc(PSDRV_Heap, 0, needed);
579 memcpy(ppdFileName, ppd, needed);
580 ppdType=REG_SZ;
581 res = ERROR_SUCCESS;
582 /* we should unlink() that file later */
583 } else {
584 res = ERROR_FILE_NOT_FOUND;
585 WARN("Did not find ppd for %s\n",name);
589 #endif
590 if (!ppdFileName) {
591 res = GetPrinterDataA(hPrinter, "PPD File", NULL, NULL, 0, &needed);
592 if ((res==ERROR_SUCCESS) || (res==ERROR_MORE_DATA)) {
593 ppdFileName=HeapAlloc(PSDRV_Heap, 0, needed);
594 res = GetPrinterDataA(hPrinter, "PPD File", &ppdType, ppdFileName, needed, &needed);
597 /* Look for a ppd file for this printer in the config file.
598 * First look under that printer's name, and then under 'generic'
600 if((res != ERROR_SUCCESS) && !RegOpenKeyA(HKEY_LOCAL_MACHINE, "Software\\Wine\\Wine\\Config\\ppd", &hkey))
602 const char* value_name;
604 if (RegQueryValueExA(hkey, name, 0, NULL, NULL, &needed) == ERROR_SUCCESS) {
605 value_name=name;
606 } else if (RegQueryValueExA(hkey, "generic", 0, NULL, NULL, &needed) == ERROR_SUCCESS) {
607 value_name="generic";
608 } else {
609 value_name=NULL;
611 if (value_name) {
612 ppdFileName=HeapAlloc(PSDRV_Heap, 0, needed);
613 RegQueryValueExA(hkey, value_name, 0, &ppdType, ppdFileName, &needed);
615 RegCloseKey(hkey);
618 if (!ppdFileName) {
619 res = ERROR_FILE_NOT_FOUND;
620 ERR ("Error %li getting PPD file name for printer '%s'\n", res, name);
621 goto closeprinter;
622 } else {
623 res = ERROR_SUCCESS;
624 if (ppdType==REG_EXPAND_SZ) {
625 char* tmp;
627 /* Expand environment variable references */
628 needed=ExpandEnvironmentStringsA(ppdFileName,NULL,0);
629 tmp=HeapAlloc(PSDRV_Heap, 0, needed);
630 ExpandEnvironmentStringsA(ppdFileName,tmp,needed);
631 HeapFree(PSDRV_Heap, 0, ppdFileName);
632 ppdFileName=tmp;
636 pi->ppd = PSDRV_ParsePPD(ppdFileName);
637 if(!pi->ppd) {
638 MESSAGE("Couldn't find PPD file '%s', expect a crash now!\n",
639 ppdFileName);
640 goto closeprinter;
643 /* Some gimp-print ppd files don't contain a DefaultResolution line
644 set it to 300 if it's not specified */
645 if(pi->ppd->DefaultResolution == 0)
646 pi->ppd->DefaultResolution = 300;
648 if(using_default_devmode) {
649 DWORD papersize;
651 if(GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_IPAPERSIZE | LOCALE_RETURN_NUMBER,
652 (LPWSTR)&papersize, sizeof(papersize)/sizeof(WCHAR))) {
653 PSDRV_DEVMODEA dm;
654 memset(&dm, 0, sizeof(dm));
655 dm.dmPublic.dmFields = DM_PAPERSIZE;
656 dm.dmPublic.u1.s1.dmPaperSize = papersize;
657 PSDRV_MergeDevmodes(pi->Devmode, &dm, pi);
659 DrvSetPrinterData16((LPSTR)name, (LPSTR)INT_PD_DEFAULT_DEVMODE,
660 REG_BINARY, (LPBYTE)pi->Devmode, sizeof(DefaultDevmode) );
663 if(pi->ppd->DefaultPageSize) { /* We'll let the ppd override the devmode */
664 PSDRV_DEVMODEA dm;
665 memset(&dm, 0, sizeof(dm));
666 dm.dmPublic.dmFields = DM_PAPERSIZE;
667 dm.dmPublic.u1.s1.dmPaperSize = pi->ppd->DefaultPageSize->WinPage;
668 PSDRV_MergeDevmodes(pi->Devmode, &dm, pi);
672 * This is a hack. The default paper size should be read in as part of
673 * the Devmode structure, but Wine doesn't currently provide a convenient
674 * way to configure printers.
676 res = GetPrinterDataA (hPrinter, "Paper Size", NULL, (LPBYTE) &dwPaperSize,
677 sizeof (DWORD), &needed);
678 if (res == ERROR_SUCCESS)
679 pi->Devmode->dmPublic.u1.s1.dmPaperSize = (SHORT) dwPaperSize;
680 else if (res == ERROR_FILE_NOT_FOUND)
681 TRACE ("No 'Paper Size' for printer '%s'\n", name);
682 else {
683 ERR ("GetPrinterDataA returned %li\n", res);
684 goto closeprinter;
687 /* Duplex is indicated by the setting of the DM_DUPLEX bit in dmFields.
688 WinDuplex == 0 is a special case which means that the ppd has a
689 *DefaultDuplex: NotCapable entry. In this case we'll try not to confuse
690 apps and set dmDuplex to DMDUP_SIMPLEX but leave the DM_DUPLEX clear.
691 PSDRV_WriteHeader understands this and copes. */
692 pi->Devmode->dmPublic.dmFields &= ~DM_DUPLEX;
693 if(pi->ppd->DefaultDuplex) {
694 pi->Devmode->dmPublic.dmDuplex = pi->ppd->DefaultDuplex->WinDuplex;
695 if(pi->Devmode->dmPublic.dmDuplex != 0)
696 pi->Devmode->dmPublic.dmFields |= DM_DUPLEX;
697 else
698 pi->Devmode->dmPublic.dmDuplex = DMDUP_SIMPLEX;
701 res = EnumPrinterDataExA (hPrinter, "PrinterDriverData\\FontSubTable", NULL,
702 0, &needed, &pi->FontSubTableSize);
703 if (res == ERROR_SUCCESS || res == ERROR_FILE_NOT_FOUND) {
704 TRACE ("No 'FontSubTable' for printer '%s'\n", name);
706 else if (res == ERROR_MORE_DATA) {
707 pi->FontSubTable = HeapAlloc (PSDRV_Heap, 0, needed);
708 if (pi->FontSubTable == NULL) {
709 ERR ("Failed to allocate %li bytes from heap\n", needed);
710 goto closeprinter;
713 res = EnumPrinterDataExA (hPrinter, "PrinterDriverData\\FontSubTable",
714 (LPBYTE) pi->FontSubTable, needed, &needed,
715 &pi->FontSubTableSize);
716 if (res != ERROR_SUCCESS) {
717 ERR ("EnumPrinterDataExA returned %li\n", res);
718 goto closeprinter;
721 else {
722 ERR("EnumPrinterDataExA returned %li\n", res);
723 goto closeprinter;
726 if (ClosePrinter (hPrinter) == 0) {
727 ERR ("ClosePrinter failed with code %li\n", GetLastError ());
728 goto cleanup;
731 pi->next = NULL;
732 pi->Fonts = NULL;
734 for(font = pi->ppd->InstalledFonts; font; font = font->next) {
735 afm = PSDRV_FindAFMinList(PSDRV_AFMFontList, font->Name);
736 if(!afm) {
737 TRACE( "Couldn't find AFM file for installed printer font '%s' - "
738 "ignoring\n", font->Name);
740 else {
741 BOOL added;
742 if (PSDRV_AddAFMtoList(&pi->Fonts, afm, &added) == FALSE) {
743 PSDRV_FreeAFMList(pi->Fonts);
744 goto cleanup;
749 if (ppd) unlink(ppd);
750 return pi;
752 closeprinter:
753 ClosePrinter(hPrinter);
754 cleanup:
755 HeapFree(PSDRV_Heap, 0, ppdFileName);
756 HeapFree(PSDRV_Heap, 0, pi->FontSubTable);
757 HeapFree(PSDRV_Heap, 0, pi->FriendlyName);
758 HeapFree(PSDRV_Heap, 0, pi->Devmode);
759 fail:
760 HeapFree(PSDRV_Heap, 0, pi);
761 if (ppd) unlink(ppd);
762 *last = NULL;
763 return NULL;