winex11: Use the gdi display when initializing the keyboard to avoid creating a secon...
[wine/wine64.git] / dlls / wineps.drv / init.c
blob0024640f4b92ee5ae098793406022f0d01772b35
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 #ifdef SONAME_LIBCUPS
49 static void *cupshandle = NULL;
50 #endif
52 static const 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,
69 /* dmScale */ 100, /* ?? */
70 /* dmCopies */ 1,
71 /* dmDefaultSource */ DMBIN_AUTO,
72 /* 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 { /* u2 */
86 /* dmDisplayFlags */ 0
88 /* dmDisplayFrequency */ 0,
89 /* dmICMMethod */ 0,
90 /* dmICMIntent */ 0,
91 /* dmMediaType */ 0,
92 /* dmDitherType */ 0,
93 /* dmReserved1 */ 0,
94 /* dmReserved2 */ 0,
95 /* dmPanningWidth */ 0,
96 /* dmPanningHeight */ 0
98 { /* dmDocPrivate */
99 /* dummy */ 0
101 { /* dmDrvPrivate */
102 /* numInstalledOptions */ 0
106 HINSTANCE PSDRV_hInstance = 0;
107 HANDLE PSDRV_Heap = 0;
109 static HFONT PSDRV_DefaultFont = 0;
110 static const LOGFONTA DefaultLogFont = {
111 100, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, ANSI_CHARSET, 0, 0,
112 DEFAULT_QUALITY, FIXED_PITCH | FF_MODERN, ""
115 /*********************************************************************
116 * DllMain
118 * Initializes font metrics and registers driver. wineps dll entry point.
121 BOOL WINAPI DllMain( HINSTANCE hinst, DWORD reason, LPVOID reserved )
123 TRACE("(%p, %d, %p)\n", hinst, reason, reserved);
125 switch(reason) {
127 case DLL_PROCESS_ATTACH:
128 PSDRV_hInstance = hinst;
129 DisableThreadLibraryCalls(hinst);
131 PSDRV_Heap = HeapCreate(0, 0x10000, 0);
132 if (PSDRV_Heap == NULL)
133 return FALSE;
135 if (PSDRV_GetFontMetrics() == FALSE) {
136 HeapDestroy(PSDRV_Heap);
137 return FALSE;
140 PSDRV_DefaultFont = CreateFontIndirectA(&DefaultLogFont);
141 if (PSDRV_DefaultFont == NULL) {
142 HeapDestroy(PSDRV_Heap);
143 return FALSE;
145 #ifdef SONAME_LIBCUPS
146 /* dynamically load CUPS if not yet loaded */
147 if (!cupshandle) {
148 cupshandle = wine_dlopen(SONAME_LIBCUPS, RTLD_NOW, NULL, 0);
149 if (!cupshandle) cupshandle = (void*)-1;
151 #endif
152 break;
154 case DLL_PROCESS_DETACH:
156 DeleteObject( PSDRV_DefaultFont );
157 HeapDestroy( PSDRV_Heap );
158 #ifdef SONAME_LIBCUPS
159 if (cupshandle && (cupshandle != (void*)-1)) {
160 wine_dlclose(cupshandle, NULL, 0);
161 cupshandle = NULL;
163 #endif
164 break;
167 return TRUE;
171 static void PSDRV_UpdateDevCaps( PSDRV_PDEVICE *physDev )
173 PAGESIZE *page;
174 INT width = 0, height = 0;
176 if(physDev->Devmode->dmPublic.dmFields & DM_PAPERSIZE) {
177 LIST_FOR_EACH_ENTRY(page, &physDev->pi->ppd->PageSizes, PAGESIZE, entry) {
178 if(page->WinPage == physDev->Devmode->dmPublic.u1.s1.dmPaperSize)
179 break;
182 if(&page->entry == &physDev->pi->ppd->PageSizes) {
183 FIXME("Can't find page\n");
184 physDev->ImageableArea.left = 0;
185 physDev->ImageableArea.right = 0;
186 physDev->ImageableArea.bottom = 0;
187 physDev->ImageableArea.top = 0;
188 physDev->PageSize.cx = 0;
189 physDev->PageSize.cy = 0;
190 } else if(page->ImageableArea) {
191 /* physDev sizes in device units; ppd sizes in 1/72" */
192 physDev->ImageableArea.left = page->ImageableArea->llx *
193 physDev->logPixelsX / 72;
194 physDev->ImageableArea.right = page->ImageableArea->urx *
195 physDev->logPixelsX / 72;
196 physDev->ImageableArea.bottom = page->ImageableArea->lly *
197 physDev->logPixelsY / 72;
198 physDev->ImageableArea.top = page->ImageableArea->ury *
199 physDev->logPixelsY / 72;
200 physDev->PageSize.cx = page->PaperDimension->x *
201 physDev->logPixelsX / 72;
202 physDev->PageSize.cy = page->PaperDimension->y *
203 physDev->logPixelsY / 72;
204 } else {
205 physDev->ImageableArea.left = physDev->ImageableArea.bottom = 0;
206 physDev->ImageableArea.right = physDev->PageSize.cx =
207 page->PaperDimension->x * physDev->logPixelsX / 72;
208 physDev->ImageableArea.top = physDev->PageSize.cy =
209 page->PaperDimension->y * physDev->logPixelsY / 72;
211 } else if((physDev->Devmode->dmPublic.dmFields & DM_PAPERLENGTH) &&
212 (physDev->Devmode->dmPublic.dmFields & DM_PAPERWIDTH)) {
213 /* physDev sizes in device units; Devmode sizes in 1/10 mm */
214 physDev->ImageableArea.left = physDev->ImageableArea.bottom = 0;
215 physDev->ImageableArea.right = physDev->PageSize.cx =
216 physDev->Devmode->dmPublic.u1.s1.dmPaperWidth *
217 physDev->logPixelsX / 254;
218 physDev->ImageableArea.top = physDev->PageSize.cy =
219 physDev->Devmode->dmPublic.u1.s1.dmPaperLength *
220 physDev->logPixelsY / 254;
221 } else {
222 FIXME("Odd dmFields %x\n", physDev->Devmode->dmPublic.dmFields);
223 physDev->ImageableArea.left = 0;
224 physDev->ImageableArea.right = 0;
225 physDev->ImageableArea.bottom = 0;
226 physDev->ImageableArea.top = 0;
227 physDev->PageSize.cx = 0;
228 physDev->PageSize.cy = 0;
231 TRACE("ImageableArea = %d,%d - %d,%d: PageSize = %dx%d\n",
232 physDev->ImageableArea.left, physDev->ImageableArea.bottom,
233 physDev->ImageableArea.right, physDev->ImageableArea.top,
234 physDev->PageSize.cx, physDev->PageSize.cy);
236 /* these are in device units */
237 width = physDev->ImageableArea.right - physDev->ImageableArea.left;
238 height = physDev->ImageableArea.top - physDev->ImageableArea.bottom;
240 if(physDev->Devmode->dmPublic.u1.s1.dmOrientation == DMORIENT_PORTRAIT) {
241 physDev->horzRes = width;
242 physDev->vertRes = height;
243 } else {
244 physDev->horzRes = height;
245 physDev->vertRes = width;
248 /* these are in mm */
249 physDev->horzSize = (physDev->horzRes * 25.4) / physDev->logPixelsX;
250 physDev->vertSize = (physDev->vertRes * 25.4) / physDev->logPixelsY;
252 TRACE("devcaps: horzSize = %dmm, vertSize = %dmm, "
253 "horzRes = %d, vertRes = %d\n",
254 physDev->horzSize, physDev->vertSize,
255 physDev->horzRes, physDev->vertRes);
259 /***********************************************************
260 * DEVMODEdupWtoA
262 * Creates an ascii copy of supplied devmode on heap
264 * Copied from dlls/winspool/info.c until full unicodification
266 static LPDEVMODEA DEVMODEdupWtoA(HANDLE heap, const DEVMODEW *dmW)
268 LPDEVMODEA dmA;
269 DWORD size;
270 BOOL Formname;
271 ptrdiff_t off_formname = (const char *)dmW->dmFormName - (const char *)dmW;
273 if(!dmW) return NULL;
274 Formname = (dmW->dmSize > off_formname);
275 size = dmW->dmSize - CCHDEVICENAME - (Formname ? CCHFORMNAME : 0);
276 dmA = HeapAlloc(heap, HEAP_ZERO_MEMORY, size + dmW->dmDriverExtra);
277 WideCharToMultiByte(CP_ACP, 0, dmW->dmDeviceName, -1, (LPSTR)dmA->dmDeviceName,
278 CCHDEVICENAME, NULL, NULL);
279 if(!Formname) {
280 memcpy(&dmA->dmSpecVersion, &dmW->dmSpecVersion,
281 dmW->dmSize - CCHDEVICENAME * sizeof(WCHAR));
282 } else {
283 memcpy(&dmA->dmSpecVersion, &dmW->dmSpecVersion,
284 off_formname - CCHDEVICENAME * sizeof(WCHAR));
285 WideCharToMultiByte(CP_ACP, 0, dmW->dmFormName, -1, (LPSTR)dmA->dmFormName,
286 CCHFORMNAME, NULL, NULL);
287 memcpy(&dmA->dmLogPixels, &dmW->dmLogPixels, dmW->dmSize -
288 (off_formname + CCHFORMNAME * sizeof(WCHAR)));
290 dmA->dmSize = size;
291 memcpy((char *)dmA + dmA->dmSize, (const char *)dmW + dmW->dmSize,
292 dmW->dmDriverExtra);
293 return dmA;
297 /**********************************************************************
298 * PSDRV_CreateDC
300 BOOL PSDRV_CreateDC( HDC hdc, PSDRV_PDEVICE **pdev, LPCWSTR driver, LPCWSTR device,
301 LPCWSTR output, const DEVMODEW* initData )
303 PSDRV_PDEVICE *physDev;
304 PRINTERINFO *pi;
305 char *deviceA;
307 /* If no device name was specified, retrieve the device name
308 * from the DEVMODE structure from the DC's physDev.
309 * (See CreateCompatibleDC) */
310 if ( !device && *pdev )
312 physDev = *pdev;
313 deviceA = HeapAlloc(GetProcessHeap(), 0, CCHDEVICENAME);
314 lstrcpynA(deviceA, (LPCSTR)physDev->Devmode->dmPublic.dmDeviceName, CCHDEVICENAME);
316 else
318 DWORD len = WideCharToMultiByte(CP_ACP, 0, device, -1, NULL, 0, NULL, NULL);
319 deviceA = HeapAlloc(GetProcessHeap(), 0, len);
320 WideCharToMultiByte(CP_ACP, 0, device, -1, deviceA, len, NULL, NULL);
322 pi = PSDRV_FindPrinterInfo(deviceA);
324 HeapFree(GetProcessHeap(), 0, deviceA);
325 deviceA = NULL;
327 TRACE("(%s %s %s %p)\n", debugstr_w(driver), debugstr_w(device),
328 debugstr_w(output), initData);
330 if(!pi) return FALSE;
332 if(!pi->Fonts) {
333 RASTERIZER_STATUS status;
334 if(!GetRasterizerCaps(&status, sizeof(status)) ||
335 !(status.wFlags & TT_AVAILABLE) ||
336 !(status.wFlags & TT_ENABLED)) {
337 MESSAGE("Disabling printer %s since it has no builtin fonts and there are no TrueType fonts available.\n",
338 debugstr_w(device));
339 return FALSE;
343 physDev = HeapAlloc( PSDRV_Heap, HEAP_ZERO_MEMORY, sizeof(*physDev) );
344 if (!physDev) return FALSE;
345 *pdev = physDev;
346 physDev->hdc = hdc;
348 physDev->pi = pi;
350 physDev->Devmode = HeapAlloc( PSDRV_Heap, 0, sizeof(PSDRV_DEVMODEA) );
351 if(!physDev->Devmode) {
352 HeapFree( PSDRV_Heap, 0, physDev );
353 return FALSE;
356 memcpy( physDev->Devmode, pi->Devmode, sizeof(PSDRV_DEVMODEA) );
358 physDev->logPixelsX = physDev->pi->ppd->DefaultResolution;
359 physDev->logPixelsY = physDev->pi->ppd->DefaultResolution;
361 if (output && *output) {
362 INT len = WideCharToMultiByte( CP_ACP, 0, output, -1, NULL, 0, NULL, NULL );
363 if ((physDev->job.output = HeapAlloc( PSDRV_Heap, 0, len )))
364 WideCharToMultiByte( CP_ACP, 0, output, -1, physDev->job.output, len, NULL, NULL );
365 } else
366 physDev->job.output = NULL;
367 physDev->job.hJob = 0;
369 if(initData) {
370 DEVMODEA *devmodeA = DEVMODEdupWtoA(PSDRV_Heap, initData);
371 PSDRV_MergeDevmodes(physDev->Devmode, (PSDRV_DEVMODEA *)devmodeA, pi);
372 HeapFree(PSDRV_Heap, 0, devmodeA);
375 PSDRV_UpdateDevCaps(physDev);
376 SelectObject( hdc, PSDRV_DefaultFont );
377 return TRUE;
382 /**********************************************************************
383 * PSDRV_DeleteDC
385 BOOL PSDRV_DeleteDC( PSDRV_PDEVICE *physDev )
387 TRACE("\n");
389 HeapFree( PSDRV_Heap, 0, physDev->Devmode );
390 HeapFree( PSDRV_Heap, 0, physDev->job.output );
391 HeapFree( PSDRV_Heap, 0, physDev );
393 return TRUE;
397 /**********************************************************************
398 * ResetDC (WINEPS.@)
400 HDC PSDRV_ResetDC( PSDRV_PDEVICE *physDev, const DEVMODEW *lpInitData )
402 if(lpInitData) {
403 HRGN hrgn;
404 DEVMODEA *devmodeA = DEVMODEdupWtoA(PSDRV_Heap, lpInitData);
405 PSDRV_MergeDevmodes(physDev->Devmode, (PSDRV_DEVMODEA *)devmodeA, physDev->pi);
406 HeapFree(PSDRV_Heap, 0, devmodeA);
407 PSDRV_UpdateDevCaps(physDev);
408 hrgn = CreateRectRgn(0, 0, physDev->horzRes, physDev->vertRes);
409 SelectVisRgn16(HDC_16(physDev->hdc), HRGN_16(hrgn));
410 DeleteObject(hrgn);
412 return physDev->hdc;
415 /***********************************************************************
416 * GetDeviceCaps (WINEPS.@)
418 INT PSDRV_GetDeviceCaps( PSDRV_PDEVICE *physDev, INT cap )
420 switch(cap)
422 case DRIVERVERSION:
423 return 0;
424 case TECHNOLOGY:
425 return DT_RASPRINTER;
426 case HORZSIZE:
427 return MulDiv(physDev->horzSize, 100,
428 physDev->Devmode->dmPublic.u1.s1.dmScale);
429 case VERTSIZE:
430 return MulDiv(physDev->vertSize, 100,
431 physDev->Devmode->dmPublic.u1.s1.dmScale);
432 case HORZRES:
433 case DESKTOPHORZRES:
434 return physDev->horzRes;
435 case VERTRES:
436 case DESKTOPVERTRES:
437 return physDev->vertRes;
438 case BITSPIXEL:
439 return (physDev->pi->ppd->ColorDevice != CD_False) ? 8 : 1;
440 case PLANES:
441 return 1;
442 case NUMBRUSHES:
443 return -1;
444 case NUMPENS:
445 return 10;
446 case NUMMARKERS:
447 return 0;
448 case NUMFONTS:
449 return 39;
450 case NUMCOLORS:
451 return (physDev->pi->ppd->ColorDevice != CD_False) ? 256 : -1;
452 case PDEVICESIZE:
453 return sizeof(PSDRV_PDEVICE);
454 case CURVECAPS:
455 return (CC_CIRCLES | CC_PIE | CC_CHORD | CC_ELLIPSES | CC_WIDE |
456 CC_STYLED | CC_WIDESTYLED | CC_INTERIORS | CC_ROUNDRECT);
457 case LINECAPS:
458 return (LC_POLYLINE | LC_MARKER | LC_POLYMARKER | LC_WIDE |
459 LC_STYLED | LC_WIDESTYLED | LC_INTERIORS);
460 case POLYGONALCAPS:
461 return (PC_POLYGON | PC_RECTANGLE | PC_WINDPOLYGON | PC_SCANLINE |
462 PC_WIDE | PC_STYLED | PC_WIDESTYLED | PC_INTERIORS);
463 case TEXTCAPS:
464 return TC_CR_ANY | TC_VA_ABLE; /* psdrv 0x59f7 */
465 case CLIPCAPS:
466 return CP_RECTANGLE;
467 case RASTERCAPS:
468 return (RC_BITBLT | RC_BITMAP64 | RC_GDI20_OUTPUT | RC_DIBTODEV |
469 RC_STRETCHBLT | RC_STRETCHDIB); /* psdrv 0x6e99 */
470 /* Are aspect[XY] and logPixels[XY] correct? */
471 /* Need to handle different res in x and y => fix ppd */
472 case ASPECTX:
473 case ASPECTY:
474 return physDev->pi->ppd->DefaultResolution;
475 case ASPECTXY:
476 return (int)hypot( (double)physDev->pi->ppd->DefaultResolution,
477 (double)physDev->pi->ppd->DefaultResolution );
478 case LOGPIXELSX:
479 return MulDiv(physDev->logPixelsX,
480 physDev->Devmode->dmPublic.u1.s1.dmScale, 100);
481 case LOGPIXELSY:
482 return MulDiv(physDev->logPixelsY,
483 physDev->Devmode->dmPublic.u1.s1.dmScale, 100);
484 case SIZEPALETTE:
485 return 0;
486 case NUMRESERVED:
487 return 0;
488 case COLORRES:
489 return 0;
490 case PHYSICALWIDTH:
491 return (physDev->Devmode->dmPublic.u1.s1.dmOrientation == DMORIENT_LANDSCAPE) ?
492 physDev->PageSize.cy : physDev->PageSize.cx;
493 case PHYSICALHEIGHT:
494 return (physDev->Devmode->dmPublic.u1.s1.dmOrientation == DMORIENT_LANDSCAPE) ?
495 physDev->PageSize.cx : physDev->PageSize.cy;
496 case PHYSICALOFFSETX:
497 if(physDev->Devmode->dmPublic.u1.s1.dmOrientation == DMORIENT_LANDSCAPE) {
498 if(physDev->pi->ppd->LandscapeOrientation == -90)
499 return physDev->PageSize.cy - physDev->ImageableArea.top;
500 else
501 return physDev->ImageableArea.bottom;
503 return physDev->ImageableArea.left;
505 case PHYSICALOFFSETY:
506 if(physDev->Devmode->dmPublic.u1.s1.dmOrientation == DMORIENT_LANDSCAPE) {
507 if(physDev->pi->ppd->LandscapeOrientation == -90)
508 return physDev->PageSize.cx - physDev->ImageableArea.right;
509 else
510 return physDev->ImageableArea.left;
512 return physDev->PageSize.cy - physDev->ImageableArea.top;
514 case SCALINGFACTORX:
515 case SCALINGFACTORY:
516 case VREFRESH:
517 case BLTALIGNMENT:
518 return 0;
519 default:
520 FIXME("(%p): unsupported capability %d, will return 0\n", physDev->hdc, cap );
521 return 0;
526 /**********************************************************************
527 * PSDRV_FindPrinterInfo
529 PRINTERINFO *PSDRV_FindPrinterInfo(LPCSTR name)
531 static PRINTERINFO *PSDRV_PrinterList;
532 DWORD type = REG_BINARY, needed, res, dwPaperSize;
533 PRINTERINFO *pi = PSDRV_PrinterList, **last = &PSDRV_PrinterList;
534 FONTNAME *font;
535 const AFM *afm;
536 HANDLE hPrinter;
537 const char *ppd = NULL;
538 DWORD ppdType;
539 char* ppdFileName = NULL;
540 HKEY hkey;
541 BOOL using_default_devmode = FALSE;
542 static CHAR paper_size[] = "Paper Size";
544 TRACE("'%s'\n", name);
547 * If this loop completes, last will point to the 'next' element of the
548 * final PRINTERINFO in the list
550 for( ; pi; last = &pi->next, pi = pi->next)
551 if(!strcmp(pi->FriendlyName, name))
552 return pi;
554 pi = *last = HeapAlloc( PSDRV_Heap, HEAP_ZERO_MEMORY, sizeof(*pi) );
555 if (pi == NULL)
556 return NULL;
558 if (!(pi->FriendlyName = HeapAlloc( PSDRV_Heap, 0, strlen(name)+1 ))) goto fail;
559 strcpy( pi->FriendlyName, name );
561 /* Use Get|SetPrinterDataExA instead? */
563 res = DrvGetPrinterData16((LPSTR)name, (LPSTR)INT_PD_DEFAULT_DEVMODE, &type,
564 NULL, 0, &needed );
566 if(res == ERROR_INVALID_PRINTER_NAME || needed != sizeof(DefaultDevmode)) {
567 pi->Devmode = HeapAlloc( PSDRV_Heap, 0, sizeof(DefaultDevmode) );
568 if (pi->Devmode == NULL)
569 goto cleanup;
570 memcpy(pi->Devmode, &DefaultDevmode, sizeof(DefaultDevmode) );
571 lstrcpynA((LPSTR)pi->Devmode->dmPublic.dmDeviceName,name,CCHDEVICENAME);
572 using_default_devmode = TRUE;
574 /* need to do something here AddPrinter?? */
576 else {
577 pi->Devmode = HeapAlloc( PSDRV_Heap, 0, needed );
578 DrvGetPrinterData16((LPSTR)name, (LPSTR)INT_PD_DEFAULT_DEVMODE, &type,
579 (LPBYTE)pi->Devmode, needed, &needed);
582 if (OpenPrinterA (pi->FriendlyName, &hPrinter, NULL) == 0) {
583 ERR ("OpenPrinterA failed with code %i\n", GetLastError ());
584 goto cleanup;
587 #ifdef SONAME_LIBCUPS
588 if (cupshandle != (void*)-1) {
589 typeof(cupsGetPPD) * pcupsGetPPD = NULL;
591 pcupsGetPPD = wine_dlsym(cupshandle, "cupsGetPPD", NULL, 0);
592 if (pcupsGetPPD) {
593 ppd = pcupsGetPPD(name);
595 if (ppd) {
596 needed=strlen(ppd)+1;
597 ppdFileName=HeapAlloc(PSDRV_Heap, 0, needed);
598 memcpy(ppdFileName, ppd, needed);
599 ppdType=REG_SZ;
600 res = ERROR_SUCCESS;
601 /* we should unlink() that file later */
602 } else {
603 res = ERROR_FILE_NOT_FOUND;
604 WARN("Did not find ppd for %s\n",name);
608 #endif
609 if (!ppdFileName) {
610 static CHAR ppd_file[] = "PPD File";
612 res = GetPrinterDataA(hPrinter, ppd_file, NULL, NULL, 0, &needed);
613 if ((res==ERROR_SUCCESS) || (res==ERROR_MORE_DATA)) {
614 ppdFileName=HeapAlloc(PSDRV_Heap, 0, needed);
615 res = GetPrinterDataA(hPrinter, ppd_file, &ppdType, (LPBYTE)ppdFileName, needed, &needed);
618 /* Look for a ppd file for this printer in the config file.
619 * First look under that printer's name, and then under 'generic'
621 /* @@ Wine registry key: HKCU\Software\Wine\Printing\PPD Files */
622 if((res != ERROR_SUCCESS) && !RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Printing\\PPD Files", &hkey))
624 const char* value_name;
626 if (RegQueryValueExA(hkey, name, 0, NULL, NULL, &needed) == ERROR_SUCCESS) {
627 value_name=name;
628 } else if (RegQueryValueExA(hkey, "generic", 0, NULL, NULL, &needed) == ERROR_SUCCESS) {
629 value_name="generic";
630 } else {
631 value_name=NULL;
633 if (value_name) {
634 HeapFree(PSDRV_Heap, 0, ppdFileName);
635 ppdFileName=HeapAlloc(PSDRV_Heap, 0, needed);
636 RegQueryValueExA(hkey, value_name, 0, &ppdType, (LPBYTE)ppdFileName, &needed);
638 RegCloseKey(hkey);
641 if (!ppdFileName)
643 const char *data_dir, *filename;
645 if ((data_dir = wine_get_data_dir())) filename = "/generic.ppd";
646 else if ((data_dir = wine_get_build_dir())) filename = "/dlls/wineps.drv/generic.ppd";
647 else
649 res = ERROR_FILE_NOT_FOUND;
650 ERR ("Error %i getting PPD file name for printer '%s'\n", res, name);
651 goto closeprinter;
653 ppdFileName = HeapAlloc( PSDRV_Heap, 0, strlen(data_dir) + strlen(filename) + 1 );
654 strcpy( ppdFileName, data_dir );
655 strcat( ppdFileName, filename );
656 } else {
657 res = ERROR_SUCCESS;
658 if (ppdType==REG_EXPAND_SZ) {
659 char* tmp;
661 /* Expand environment variable references */
662 needed=ExpandEnvironmentStringsA(ppdFileName,NULL,0);
663 tmp=HeapAlloc(PSDRV_Heap, 0, needed);
664 ExpandEnvironmentStringsA(ppdFileName,tmp,needed);
665 HeapFree(PSDRV_Heap, 0, ppdFileName);
666 ppdFileName=tmp;
670 pi->ppd = PSDRV_ParsePPD(ppdFileName);
671 if(!pi->ppd) {
672 MESSAGE("Couldn't find PPD file '%s', expect a crash now!\n",
673 ppdFileName);
674 goto closeprinter;
677 /* Some gimp-print ppd files don't contain a DefaultResolution line
678 set it to 300 if it's not specified */
679 if(pi->ppd->DefaultResolution == 0)
680 pi->ppd->DefaultResolution = 300;
682 if(using_default_devmode) {
683 DWORD papersize;
685 if(GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_IPAPERSIZE | LOCALE_RETURN_NUMBER,
686 (LPWSTR)&papersize, sizeof(papersize)/sizeof(WCHAR))) {
687 PSDRV_DEVMODEA dm;
688 memset(&dm, 0, sizeof(dm));
689 dm.dmPublic.dmFields = DM_PAPERSIZE;
690 dm.dmPublic.u1.s1.dmPaperSize = papersize;
691 PSDRV_MergeDevmodes(pi->Devmode, &dm, pi);
693 DrvSetPrinterData16((LPSTR)name, (LPSTR)INT_PD_DEFAULT_DEVMODE,
694 REG_BINARY, (LPBYTE)pi->Devmode, sizeof(DefaultDevmode) );
697 if(pi->ppd->DefaultPageSize) { /* We'll let the ppd override the devmode */
698 PSDRV_DEVMODEA dm;
699 memset(&dm, 0, sizeof(dm));
700 dm.dmPublic.dmFields = DM_PAPERSIZE;
701 dm.dmPublic.u1.s1.dmPaperSize = pi->ppd->DefaultPageSize->WinPage;
702 PSDRV_MergeDevmodes(pi->Devmode, &dm, pi);
706 * This is a hack. The default paper size should be read in as part of
707 * the Devmode structure, but Wine doesn't currently provide a convenient
708 * way to configure printers.
710 res = GetPrinterDataA (hPrinter, paper_size, NULL, (LPBYTE) &dwPaperSize,
711 sizeof (DWORD), &needed);
712 if (res == ERROR_SUCCESS)
713 pi->Devmode->dmPublic.u1.s1.dmPaperSize = (SHORT) dwPaperSize;
714 else if (res == ERROR_FILE_NOT_FOUND)
715 TRACE ("No 'Paper Size' for printer '%s'\n", name);
716 else {
717 ERR ("GetPrinterDataA returned %i\n", res);
718 goto closeprinter;
721 /* Duplex is indicated by the setting of the DM_DUPLEX bit in dmFields.
722 WinDuplex == 0 is a special case which means that the ppd has a
723 *DefaultDuplex: NotCapable entry. In this case we'll try not to confuse
724 apps and set dmDuplex to DMDUP_SIMPLEX but leave the DM_DUPLEX clear.
725 PSDRV_WriteHeader understands this and copes. */
726 pi->Devmode->dmPublic.dmFields &= ~DM_DUPLEX;
727 if(pi->ppd->DefaultDuplex) {
728 pi->Devmode->dmPublic.dmDuplex = pi->ppd->DefaultDuplex->WinDuplex;
729 if(pi->Devmode->dmPublic.dmDuplex != 0)
730 pi->Devmode->dmPublic.dmFields |= DM_DUPLEX;
731 else
732 pi->Devmode->dmPublic.dmDuplex = DMDUP_SIMPLEX;
735 res = EnumPrinterDataExA (hPrinter, "PrinterDriverData\\FontSubTable", NULL,
736 0, &needed, &pi->FontSubTableSize);
737 if (res == ERROR_SUCCESS || res == ERROR_FILE_NOT_FOUND) {
738 TRACE ("No 'FontSubTable' for printer '%s'\n", name);
740 else if (res == ERROR_MORE_DATA) {
741 pi->FontSubTable = HeapAlloc (PSDRV_Heap, 0, needed);
742 if (pi->FontSubTable == NULL) {
743 ERR ("Failed to allocate %i bytes from heap\n", needed);
744 goto closeprinter;
747 res = EnumPrinterDataExA (hPrinter, "PrinterDriverData\\FontSubTable",
748 (LPBYTE) pi->FontSubTable, needed, &needed,
749 &pi->FontSubTableSize);
750 if (res != ERROR_SUCCESS) {
751 ERR ("EnumPrinterDataExA returned %i\n", res);
752 goto closeprinter;
755 else {
756 ERR("EnumPrinterDataExA returned %i\n", res);
757 goto closeprinter;
760 if (ClosePrinter (hPrinter) == 0) {
761 ERR ("ClosePrinter failed with code %i\n", GetLastError ());
762 goto cleanup;
765 pi->next = NULL;
766 pi->Fonts = NULL;
768 for(font = pi->ppd->InstalledFonts; font; font = font->next) {
769 afm = PSDRV_FindAFMinList(PSDRV_AFMFontList, font->Name);
770 if(!afm) {
771 TRACE( "Couldn't find AFM file for installed printer font '%s' - "
772 "ignoring\n", font->Name);
774 else {
775 BOOL added;
776 if (PSDRV_AddAFMtoList(&pi->Fonts, afm, &added) == FALSE) {
777 PSDRV_FreeAFMList(pi->Fonts);
778 goto cleanup;
783 if (ppd) unlink(ppd);
784 return pi;
786 closeprinter:
787 ClosePrinter(hPrinter);
788 cleanup:
789 HeapFree(PSDRV_Heap, 0, ppdFileName);
790 HeapFree(PSDRV_Heap, 0, pi->FontSubTable);
791 HeapFree(PSDRV_Heap, 0, pi->FriendlyName);
792 HeapFree(PSDRV_Heap, 0, pi->Devmode);
793 fail:
794 HeapFree(PSDRV_Heap, 0, pi);
795 if (ppd) unlink(ppd);
796 *last = NULL;
797 return NULL;