wineps: Retrieve the printer's devmode using GetPrinter.
[wine/multimedia.git] / dlls / wineps.drv / init.c
blobcc950e3e749ed60e0b1627adaea10f22545e0281
1 /*
2 * PostScript driver initialization functions
4 * Copyright 1998 Huw D M Davies
5 * Copyright 2001 Marcus Meissner
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "config.h"
23 #include "wine/port.h"
25 #include <stdarg.h>
26 #include <string.h>
27 #ifdef HAVE_UNISTD_H
28 # include <unistd.h>
29 #endif
30 #ifdef HAVE_CUPS_CUPS_H
31 # include <cups/cups.h>
32 #endif
34 #define NONAMELESSUNION
35 #define NONAMELESSSTRUCT
37 #include "windef.h"
38 #include "winbase.h"
39 #include "winerror.h"
40 #include "winreg.h"
41 #include "winnls.h"
42 #include "psdrv.h"
43 #include "winspool.h"
44 #include "wine/library.h"
45 #include "wine/debug.h"
47 WINE_DEFAULT_DEBUG_CHANNEL(psdrv);
49 #ifdef SONAME_LIBCUPS
50 static void *cupshandle = NULL;
51 #endif
53 static const PSDRV_DEVMODEA DefaultDevmode =
55 { /* dmPublic */
56 /* dmDeviceName */ "Wine PostScript Driver",
57 /* dmSpecVersion */ 0x30a,
58 /* dmDriverVersion */ 0x001,
59 /* dmSize */ sizeof(DEVMODEA),
60 /* dmDriverExtra */ sizeof(PSDRV_DEVMODEA)-sizeof(DEVMODEA),
61 /* dmFields */ DM_ORIENTATION | DM_PAPERSIZE | DM_SCALE |
62 DM_COPIES | DM_DEFAULTSOURCE | DM_COLOR |
63 DM_YRESOLUTION | DM_TTOPTION,
64 { /* u1 */
65 { /* s1 */
66 /* dmOrientation */ DMORIENT_PORTRAIT,
67 /* dmPaperSize */ DMPAPER_LETTER,
68 /* dmPaperLength */ 2794,
69 /* dmPaperWidth */ 2159,
70 /* dmScale */ 100, /* ?? */
71 /* dmCopies */ 1,
72 /* dmDefaultSource */ DMBIN_AUTO,
73 /* dmPrintQuality */ 0
76 /* dmColor */ DMCOLOR_COLOR,
77 /* dmDuplex */ DMDUP_SIMPLEX,
78 /* dmYResolution */ 0,
79 /* dmTTOption */ DMTT_SUBDEV,
80 /* dmCollate */ 0,
81 /* dmFormName */ "",
82 /* dmUnusedPadding */ 0,
83 /* dmBitsPerPel */ 0,
84 /* dmPelsWidth */ 0,
85 /* dmPelsHeight */ 0,
86 { /* u2 */
87 /* dmDisplayFlags */ 0
89 /* dmDisplayFrequency */ 0,
90 /* dmICMMethod */ 0,
91 /* dmICMIntent */ 0,
92 /* dmMediaType */ 0,
93 /* dmDitherType */ 0,
94 /* dmReserved1 */ 0,
95 /* dmReserved2 */ 0,
96 /* dmPanningWidth */ 0,
97 /* dmPanningHeight */ 0
99 { /* dmDocPrivate */
100 /* dummy */ 0
102 { /* dmDrvPrivate */
103 /* numInstalledOptions */ 0
107 HINSTANCE PSDRV_hInstance = 0;
108 HANDLE PSDRV_Heap = 0;
110 static HFONT PSDRV_DefaultFont = 0;
111 static const LOGFONTA DefaultLogFont = {
112 100, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, ANSI_CHARSET, 0, 0,
113 DEFAULT_QUALITY, FIXED_PITCH | FF_MODERN, ""
116 static const CHAR default_devmodeA[] = "Default DevMode";
117 static const struct gdi_dc_funcs psdrv_funcs;
119 /*********************************************************************
120 * DllMain
122 * Initializes font metrics and registers driver. wineps dll entry point.
125 BOOL WINAPI DllMain( HINSTANCE hinst, DWORD reason, LPVOID reserved )
127 TRACE("(%p, %d, %p)\n", hinst, reason, reserved);
129 switch(reason) {
131 case DLL_PROCESS_ATTACH:
132 PSDRV_hInstance = hinst;
133 DisableThreadLibraryCalls(hinst);
135 PSDRV_Heap = HeapCreate(0, 0x10000, 0);
136 if (PSDRV_Heap == NULL)
137 return FALSE;
139 if (PSDRV_GetFontMetrics() == FALSE) {
140 HeapDestroy(PSDRV_Heap);
141 return FALSE;
144 PSDRV_DefaultFont = CreateFontIndirectA(&DefaultLogFont);
145 if (PSDRV_DefaultFont == NULL) {
146 HeapDestroy(PSDRV_Heap);
147 return FALSE;
149 #ifdef SONAME_LIBCUPS
150 /* dynamically load CUPS if not yet loaded */
151 if (!cupshandle) {
152 cupshandle = wine_dlopen(SONAME_LIBCUPS, RTLD_NOW, NULL, 0);
153 if (!cupshandle) cupshandle = (void*)-1;
155 #endif
156 break;
158 case DLL_PROCESS_DETACH:
160 DeleteObject( PSDRV_DefaultFont );
161 HeapDestroy( PSDRV_Heap );
162 #ifdef SONAME_LIBCUPS
163 if (cupshandle && (cupshandle != (void*)-1)) {
164 wine_dlclose(cupshandle, NULL, 0);
165 cupshandle = NULL;
167 #endif
168 break;
171 return TRUE;
174 static void PSDRV_UpdateDevCaps( PSDRV_PDEVICE *physDev )
176 PAGESIZE *page;
177 INT width = 0, height = 0;
179 if(physDev->Devmode->dmPublic.dmFields & DM_PAPERSIZE) {
180 LIST_FOR_EACH_ENTRY(page, &physDev->pi->ppd->PageSizes, PAGESIZE, entry) {
181 if(page->WinPage == physDev->Devmode->dmPublic.u1.s1.dmPaperSize)
182 break;
185 if(&page->entry == &physDev->pi->ppd->PageSizes) {
186 FIXME("Can't find page\n");
187 physDev->ImageableArea.left = 0;
188 physDev->ImageableArea.right = 0;
189 physDev->ImageableArea.bottom = 0;
190 physDev->ImageableArea.top = 0;
191 physDev->PageSize.cx = 0;
192 physDev->PageSize.cy = 0;
193 } else if(page->ImageableArea) {
194 /* physDev sizes in device units; ppd sizes in 1/72" */
195 physDev->ImageableArea.left = page->ImageableArea->llx *
196 physDev->logPixelsX / 72;
197 physDev->ImageableArea.right = page->ImageableArea->urx *
198 physDev->logPixelsX / 72;
199 physDev->ImageableArea.bottom = page->ImageableArea->lly *
200 physDev->logPixelsY / 72;
201 physDev->ImageableArea.top = page->ImageableArea->ury *
202 physDev->logPixelsY / 72;
203 physDev->PageSize.cx = page->PaperDimension->x *
204 physDev->logPixelsX / 72;
205 physDev->PageSize.cy = page->PaperDimension->y *
206 physDev->logPixelsY / 72;
207 } else {
208 physDev->ImageableArea.left = physDev->ImageableArea.bottom = 0;
209 physDev->ImageableArea.right = physDev->PageSize.cx =
210 page->PaperDimension->x * physDev->logPixelsX / 72;
211 physDev->ImageableArea.top = physDev->PageSize.cy =
212 page->PaperDimension->y * physDev->logPixelsY / 72;
214 } else if((physDev->Devmode->dmPublic.dmFields & DM_PAPERLENGTH) &&
215 (physDev->Devmode->dmPublic.dmFields & DM_PAPERWIDTH)) {
216 /* physDev sizes in device units; Devmode sizes in 1/10 mm */
217 physDev->ImageableArea.left = physDev->ImageableArea.bottom = 0;
218 physDev->ImageableArea.right = physDev->PageSize.cx =
219 physDev->Devmode->dmPublic.u1.s1.dmPaperWidth *
220 physDev->logPixelsX / 254;
221 physDev->ImageableArea.top = physDev->PageSize.cy =
222 physDev->Devmode->dmPublic.u1.s1.dmPaperLength *
223 physDev->logPixelsY / 254;
224 } else {
225 FIXME("Odd dmFields %x\n", physDev->Devmode->dmPublic.dmFields);
226 physDev->ImageableArea.left = 0;
227 physDev->ImageableArea.right = 0;
228 physDev->ImageableArea.bottom = 0;
229 physDev->ImageableArea.top = 0;
230 physDev->PageSize.cx = 0;
231 physDev->PageSize.cy = 0;
234 TRACE("ImageableArea = %d,%d - %d,%d: PageSize = %dx%d\n",
235 physDev->ImageableArea.left, physDev->ImageableArea.bottom,
236 physDev->ImageableArea.right, physDev->ImageableArea.top,
237 physDev->PageSize.cx, physDev->PageSize.cy);
239 /* these are in device units */
240 width = physDev->ImageableArea.right - physDev->ImageableArea.left;
241 height = physDev->ImageableArea.top - physDev->ImageableArea.bottom;
243 if(physDev->Devmode->dmPublic.u1.s1.dmOrientation == DMORIENT_PORTRAIT) {
244 physDev->horzRes = width;
245 physDev->vertRes = height;
246 } else {
247 physDev->horzRes = height;
248 physDev->vertRes = width;
251 /* these are in mm */
252 physDev->horzSize = (physDev->horzRes * 25.4) / physDev->logPixelsX;
253 physDev->vertSize = (physDev->vertRes * 25.4) / physDev->logPixelsY;
255 TRACE("devcaps: horzSize = %dmm, vertSize = %dmm, "
256 "horzRes = %d, vertRes = %d\n",
257 physDev->horzSize, physDev->vertSize,
258 physDev->horzRes, physDev->vertRes);
262 /***********************************************************
263 * DEVMODEdupWtoA
265 * Creates an ascii copy of supplied devmode on the process heap
267 * Copied from dlls/winspool/info.c until full unicodification
269 static LPDEVMODEA DEVMODEdupWtoA( const DEVMODEW *dmW )
271 LPDEVMODEA dmA;
272 DWORD size;
273 BOOL Formname;
274 /* there is no pointer dereference here, if your code checking tool complains it's broken */
275 ptrdiff_t off_formname = (const char *)dmW->dmFormName - (const char *)dmW;
277 if(!dmW) return NULL;
278 Formname = (dmW->dmSize > off_formname);
279 size = dmW->dmSize - CCHDEVICENAME - (Formname ? CCHFORMNAME : 0);
280 dmA = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, size + dmW->dmDriverExtra );
281 WideCharToMultiByte(CP_ACP, 0, dmW->dmDeviceName, -1, (LPSTR)dmA->dmDeviceName,
282 CCHDEVICENAME, NULL, NULL);
283 if(!Formname) {
284 memcpy(&dmA->dmSpecVersion, &dmW->dmSpecVersion,
285 dmW->dmSize - CCHDEVICENAME * sizeof(WCHAR));
286 } else {
287 memcpy(&dmA->dmSpecVersion, &dmW->dmSpecVersion,
288 off_formname - CCHDEVICENAME * sizeof(WCHAR));
289 WideCharToMultiByte(CP_ACP, 0, dmW->dmFormName, -1, (LPSTR)dmA->dmFormName,
290 CCHFORMNAME, NULL, NULL);
291 memcpy(&dmA->dmLogPixels, &dmW->dmLogPixels, dmW->dmSize -
292 (off_formname + CCHFORMNAME * sizeof(WCHAR)));
294 dmA->dmSize = size;
295 memcpy((char *)dmA + dmA->dmSize, (const char *)dmW + dmW->dmSize,
296 dmW->dmDriverExtra);
297 return dmA;
301 static PSDRV_PDEVICE *create_psdrv_physdev( PRINTERINFO *pi )
303 PSDRV_PDEVICE *physDev;
305 physDev = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*physDev) );
306 if (!physDev) return NULL;
308 physDev->Devmode = HeapAlloc( GetProcessHeap(), 0, sizeof(PSDRV_DEVMODEA) );
309 if (!physDev->Devmode)
311 HeapFree( GetProcessHeap(), 0, physDev );
312 return NULL;
315 *physDev->Devmode = *pi->Devmode;
316 physDev->pi = pi;
317 physDev->logPixelsX = pi->ppd->DefaultResolution;
318 physDev->logPixelsY = pi->ppd->DefaultResolution;
319 return physDev;
322 /**********************************************************************
323 * PSDRV_CreateDC
325 static BOOL PSDRV_CreateDC( PHYSDEV *pdev, LPCWSTR driver, LPCWSTR device,
326 LPCWSTR output, const DEVMODEW* initData )
328 PSDRV_PDEVICE *physDev;
329 PRINTERINFO *pi;
331 TRACE("(%s %s %s %p)\n", debugstr_w(driver), debugstr_w(device),
332 debugstr_w(output), initData);
334 if (!device) return FALSE;
335 pi = PSDRV_FindPrinterInfo( device );
336 if(!pi) return FALSE;
338 if(!pi->Fonts) {
339 RASTERIZER_STATUS status;
340 if(!GetRasterizerCaps(&status, sizeof(status)) ||
341 !(status.wFlags & TT_AVAILABLE) ||
342 !(status.wFlags & TT_ENABLED)) {
343 MESSAGE("Disabling printer %s since it has no builtin fonts and there are no TrueType fonts available.\n",
344 debugstr_w(device));
345 return FALSE;
349 if (!(physDev = create_psdrv_physdev( pi ))) return FALSE;
351 if (output && *output) physDev->job.output = strdupW( output );
353 if(initData) {
354 DEVMODEA *devmodeA = DEVMODEdupWtoA( initData );
355 PSDRV_MergeDevmodes(physDev->Devmode, (PSDRV_DEVMODEA *)devmodeA, pi);
356 HeapFree( GetProcessHeap(), 0, devmodeA );
359 PSDRV_UpdateDevCaps(physDev);
360 SelectObject( (*pdev)->hdc, PSDRV_DefaultFont );
361 push_dc_driver( pdev, &physDev->dev, &psdrv_funcs );
362 return TRUE;
366 /**********************************************************************
367 * PSDRV_CreateCompatibleDC
369 static BOOL PSDRV_CreateCompatibleDC( PHYSDEV orig, PHYSDEV *pdev )
371 HDC hdc = (*pdev)->hdc;
372 PSDRV_PDEVICE *physDev, *orig_dev = get_psdrv_dev( orig );
373 PRINTERINFO *pi = PSDRV_FindPrinterInfo( orig_dev->pi->friendly_name );
375 if (!pi) return FALSE;
376 if (!(physDev = create_psdrv_physdev( pi ))) return FALSE;
377 PSDRV_MergeDevmodes( physDev->Devmode, orig_dev->Devmode, pi );
378 PSDRV_UpdateDevCaps(physDev);
379 SelectObject( hdc, PSDRV_DefaultFont );
380 push_dc_driver( pdev, &physDev->dev, &psdrv_funcs );
381 return TRUE;
386 /**********************************************************************
387 * PSDRV_DeleteDC
389 static BOOL PSDRV_DeleteDC( PHYSDEV dev )
391 PSDRV_PDEVICE *physDev = get_psdrv_dev( dev );
393 TRACE("\n");
395 HeapFree( GetProcessHeap(), 0, physDev->Devmode );
396 HeapFree( GetProcessHeap(), 0, physDev->job.output );
397 HeapFree( GetProcessHeap(), 0, physDev );
399 return TRUE;
403 /**********************************************************************
404 * ResetDC (WINEPS.@)
406 static HDC PSDRV_ResetDC( PHYSDEV dev, const DEVMODEW *lpInitData )
408 PSDRV_PDEVICE *physDev = get_psdrv_dev( dev );
410 if(lpInitData) {
411 DEVMODEA *devmodeA = DEVMODEdupWtoA( lpInitData );
412 PSDRV_MergeDevmodes(physDev->Devmode, (PSDRV_DEVMODEA *)devmodeA, physDev->pi);
413 HeapFree( GetProcessHeap(), 0, devmodeA );
414 PSDRV_UpdateDevCaps(physDev);
416 return dev->hdc;
419 /***********************************************************************
420 * GetDeviceCaps (WINEPS.@)
422 static INT PSDRV_GetDeviceCaps( PHYSDEV dev, INT cap )
424 PSDRV_PDEVICE *physDev = get_psdrv_dev( dev );
426 switch(cap)
428 case DRIVERVERSION:
429 return 0;
430 case TECHNOLOGY:
431 return DT_RASPRINTER;
432 case HORZSIZE:
433 return MulDiv(physDev->horzSize, 100,
434 physDev->Devmode->dmPublic.u1.s1.dmScale);
435 case VERTSIZE:
436 return MulDiv(physDev->vertSize, 100,
437 physDev->Devmode->dmPublic.u1.s1.dmScale);
438 case HORZRES:
439 case DESKTOPHORZRES:
440 return physDev->horzRes;
441 case VERTRES:
442 case DESKTOPVERTRES:
443 return physDev->vertRes;
444 case BITSPIXEL:
445 return (physDev->pi->ppd->ColorDevice != CD_False) ? 32 : 1;
446 case PLANES:
447 return 1;
448 case NUMBRUSHES:
449 return -1;
450 case NUMPENS:
451 return 10;
452 case NUMMARKERS:
453 return 0;
454 case NUMFONTS:
455 return 39;
456 case NUMCOLORS:
457 return -1;
458 case PDEVICESIZE:
459 return sizeof(PSDRV_PDEVICE);
460 case CURVECAPS:
461 return (CC_CIRCLES | CC_PIE | CC_CHORD | CC_ELLIPSES | CC_WIDE |
462 CC_STYLED | CC_WIDESTYLED | CC_INTERIORS | CC_ROUNDRECT);
463 case LINECAPS:
464 return (LC_POLYLINE | LC_MARKER | LC_POLYMARKER | LC_WIDE |
465 LC_STYLED | LC_WIDESTYLED | LC_INTERIORS);
466 case POLYGONALCAPS:
467 return (PC_POLYGON | PC_RECTANGLE | PC_WINDPOLYGON | PC_SCANLINE |
468 PC_WIDE | PC_STYLED | PC_WIDESTYLED | PC_INTERIORS);
469 case TEXTCAPS:
470 return TC_CR_ANY | TC_VA_ABLE; /* psdrv 0x59f7 */
471 case CLIPCAPS:
472 return CP_RECTANGLE;
473 case RASTERCAPS:
474 return (RC_BITBLT | RC_BITMAP64 | RC_GDI20_OUTPUT | RC_DIBTODEV |
475 RC_STRETCHBLT | RC_STRETCHDIB); /* psdrv 0x6e99 */
476 /* Are aspect[XY] and logPixels[XY] correct? */
477 /* Need to handle different res in x and y => fix ppd */
478 case ASPECTX:
479 case ASPECTY:
480 return physDev->pi->ppd->DefaultResolution;
481 case ASPECTXY:
482 return (int)hypot( (double)physDev->pi->ppd->DefaultResolution,
483 (double)physDev->pi->ppd->DefaultResolution );
484 case LOGPIXELSX:
485 return MulDiv(physDev->logPixelsX,
486 physDev->Devmode->dmPublic.u1.s1.dmScale, 100);
487 case LOGPIXELSY:
488 return MulDiv(physDev->logPixelsY,
489 physDev->Devmode->dmPublic.u1.s1.dmScale, 100);
490 case SIZEPALETTE:
491 return 0;
492 case NUMRESERVED:
493 return 0;
494 case COLORRES:
495 return 0;
496 case PHYSICALWIDTH:
497 return (physDev->Devmode->dmPublic.u1.s1.dmOrientation == DMORIENT_LANDSCAPE) ?
498 physDev->PageSize.cy : physDev->PageSize.cx;
499 case PHYSICALHEIGHT:
500 return (physDev->Devmode->dmPublic.u1.s1.dmOrientation == DMORIENT_LANDSCAPE) ?
501 physDev->PageSize.cx : physDev->PageSize.cy;
502 case PHYSICALOFFSETX:
503 if(physDev->Devmode->dmPublic.u1.s1.dmOrientation == DMORIENT_LANDSCAPE) {
504 if(physDev->pi->ppd->LandscapeOrientation == -90)
505 return physDev->PageSize.cy - physDev->ImageableArea.top;
506 else
507 return physDev->ImageableArea.bottom;
509 return physDev->ImageableArea.left;
511 case PHYSICALOFFSETY:
512 if(physDev->Devmode->dmPublic.u1.s1.dmOrientation == DMORIENT_LANDSCAPE) {
513 if(physDev->pi->ppd->LandscapeOrientation == -90)
514 return physDev->PageSize.cx - physDev->ImageableArea.right;
515 else
516 return physDev->ImageableArea.left;
518 return physDev->PageSize.cy - physDev->ImageableArea.top;
520 case SCALINGFACTORX:
521 case SCALINGFACTORY:
522 case VREFRESH:
523 case BLTALIGNMENT:
524 return 0;
525 case SHADEBLENDCAPS:
526 return SB_NONE;
527 default:
528 FIXME("(%p): unsupported capability %d, will return 0\n", dev->hdc, cap );
529 return 0;
533 static PRINTER_ENUM_VALUESA *load_font_sub_table( HANDLE printer, DWORD *num_entries )
535 DWORD res, needed, num;
536 PRINTER_ENUM_VALUESA *table = NULL;
537 static const char fontsubkey[] = "PrinterDriverData\\FontSubTable";
539 *num_entries = 0;
541 res = EnumPrinterDataExA( printer, fontsubkey, NULL, 0, &needed, &num );
542 if (res != ERROR_MORE_DATA) return NULL;
544 table = HeapAlloc( PSDRV_Heap, 0, needed );
545 if (!table) return NULL;
547 res = EnumPrinterDataExA( printer, fontsubkey, (LPBYTE)table, needed, &needed, &num );
548 if (res != ERROR_SUCCESS)
550 HeapFree( PSDRV_Heap, 0, table );
551 return NULL;
554 *num_entries = num;
555 return table;
558 static PSDRV_DEVMODEA *get_printer_devmode( HANDLE printer )
560 DWORD needed, dm_size;
561 BOOL res;
562 PRINTER_INFO_9A *info;
563 PSDRV_DEVMODEA *dm;
565 GetPrinterA( printer, 9, NULL, 0, &needed );
566 if (GetLastError() != ERROR_INSUFFICIENT_BUFFER) return NULL;
568 info = HeapAlloc( PSDRV_Heap, 0, needed );
569 res = GetPrinterA( printer, 9, (BYTE *)info, needed, &needed );
570 if (!res || !info->pDevMode)
572 HeapFree( PSDRV_Heap, 0, info );
573 return NULL;
576 /* sanity check the sizes */
577 dm_size = info->pDevMode->dmSize + info->pDevMode->dmDriverExtra;
578 if ((char *)info->pDevMode - (char *)info + dm_size > needed)
580 HeapFree( PSDRV_Heap, 0, info );
581 return NULL;
584 dm = (PSDRV_DEVMODEA*)info;
585 memmove( dm, info->pDevMode, dm_size );
586 return dm;
589 static PSDRV_DEVMODEA *get_devmode( HANDLE printer, const char *nameA, BOOL *is_default )
591 PSDRV_DEVMODEA *dm = get_printer_devmode( printer );
593 *is_default = FALSE;
595 if (dm && dm->dmPublic.dmSize + dm->dmPublic.dmDriverExtra >= sizeof(DefaultDevmode))
597 TRACE( "Retrieved devmode from winspool\n" );
598 return dm;
600 HeapFree( PSDRV_Heap, 0, dm );
602 TRACE( "Using default devmode\n" );
603 dm = HeapAlloc( PSDRV_Heap, 0, sizeof(DefaultDevmode) );
604 if (dm)
606 *dm = DefaultDevmode;
607 lstrcpynA((LPSTR)dm->dmPublic.dmDeviceName, nameA, CCHDEVICENAME);
608 *is_default = TRUE;
610 return dm;
613 static struct list printer_list = LIST_INIT( printer_list );
615 /**********************************************************************
616 * PSDRV_FindPrinterInfo
618 PRINTERINFO *PSDRV_FindPrinterInfo(LPCWSTR name)
620 DWORD needed, res, dwPaperSize;
621 PRINTERINFO *pi;
622 FONTNAME *font;
623 const AFM *afm;
624 HANDLE hPrinter = 0;
625 const char *ppd = NULL;
626 DWORD ppdType;
627 char *ppdFileName = NULL, *nameA = NULL;
628 HKEY hkey;
629 BOOL using_default_devmode = FALSE;
630 int len;
632 TRACE("'%s'\n", debugstr_w(name));
634 LIST_FOR_EACH_ENTRY( pi, &printer_list, PRINTERINFO, entry )
636 if (!strcmpW( pi->friendly_name, name ))
637 return pi;
640 pi = HeapAlloc( PSDRV_Heap, HEAP_ZERO_MEMORY, sizeof(*pi) );
641 if (pi == NULL) return NULL;
643 if (!(pi->friendly_name = HeapAlloc( PSDRV_Heap, 0, (strlenW(name)+1)*sizeof(WCHAR) ))) goto fail;
644 strcpyW( pi->friendly_name, name );
646 if (OpenPrinterW( pi->friendly_name, &hPrinter, NULL ) == 0) {
647 ERR ("OpenPrinter failed with code %i\n", GetLastError ());
648 goto cleanup;
651 len = WideCharToMultiByte( CP_ACP, 0, name, -1, NULL, 0, NULL, NULL );
652 nameA = HeapAlloc( GetProcessHeap(), 0, len );
653 WideCharToMultiByte( CP_ACP, 0, name, -1, nameA, len, NULL, NULL );
655 pi->Devmode = get_devmode( hPrinter, nameA, &using_default_devmode );
656 if (!pi->Devmode) goto closeprinter;
658 #ifdef SONAME_LIBCUPS
659 if (cupshandle != (void*)-1) {
660 typeof(cupsGetPPD) * pcupsGetPPD = NULL;
662 pcupsGetPPD = wine_dlsym(cupshandle, "cupsGetPPD", NULL, 0);
663 if (pcupsGetPPD) {
664 ppd = pcupsGetPPD( nameA );
666 if (ppd) {
667 needed=strlen(ppd)+1;
668 ppdFileName=HeapAlloc(PSDRV_Heap, 0, needed);
669 memcpy(ppdFileName, ppd, needed);
670 ppdType=REG_SZ;
671 res = ERROR_SUCCESS;
672 /* we should unlink() that file later */
673 } else {
674 res = ERROR_FILE_NOT_FOUND;
675 WARN("Did not find ppd for %s\n", debugstr_w(name));
679 #endif
680 if (!ppdFileName) {
681 res = GetPrinterDataExA(hPrinter, "PrinterDriverData", "PPD File", NULL, NULL, 0, &needed);
682 if ((res==ERROR_SUCCESS) || (res==ERROR_MORE_DATA)) {
683 ppdFileName=HeapAlloc(PSDRV_Heap, 0, needed);
684 res = GetPrinterDataExA(hPrinter, "PrinterDriverData", "PPD File", &ppdType,
685 (LPBYTE)ppdFileName, needed, &needed);
688 /* Look for a ppd file for this printer in the config file.
689 * First look under that printer's name, and then under 'generic'
691 /* @@ Wine registry key: HKCU\Software\Wine\Printing\PPD Files */
692 if((res != ERROR_SUCCESS) && !RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Printing\\PPD Files", &hkey))
694 const char* value_name;
696 if (RegQueryValueExA(hkey, nameA, 0, NULL, NULL, &needed) == ERROR_SUCCESS) {
697 value_name=nameA;
698 } else if (RegQueryValueExA(hkey, "generic", 0, NULL, NULL, &needed) == ERROR_SUCCESS) {
699 value_name="generic";
700 } else {
701 value_name=NULL;
703 if (value_name) {
704 HeapFree(PSDRV_Heap, 0, ppdFileName);
705 ppdFileName=HeapAlloc(PSDRV_Heap, 0, needed);
706 RegQueryValueExA(hkey, value_name, 0, &ppdType, (LPBYTE)ppdFileName, &needed);
708 RegCloseKey(hkey);
711 if (!ppdFileName)
713 const char *data_dir, *filename;
715 if ((data_dir = wine_get_data_dir())) filename = "/generic.ppd";
716 else if ((data_dir = wine_get_build_dir())) filename = "/dlls/wineps.drv/generic.ppd";
717 else
719 res = ERROR_FILE_NOT_FOUND;
720 ERR ("Error %i getting PPD file name for printer '%s'\n", res, debugstr_w(name));
721 goto closeprinter;
723 ppdFileName = HeapAlloc( PSDRV_Heap, 0, strlen(data_dir) + strlen(filename) + 1 );
724 strcpy( ppdFileName, data_dir );
725 strcat( ppdFileName, filename );
726 } else {
727 res = ERROR_SUCCESS;
728 if (ppdType==REG_EXPAND_SZ) {
729 char* tmp;
731 /* Expand environment variable references */
732 needed=ExpandEnvironmentStringsA(ppdFileName,NULL,0);
733 tmp=HeapAlloc(PSDRV_Heap, 0, needed);
734 ExpandEnvironmentStringsA(ppdFileName,tmp,needed);
735 HeapFree(PSDRV_Heap, 0, ppdFileName);
736 ppdFileName=tmp;
740 pi->ppd = PSDRV_ParsePPD(ppdFileName);
741 if(!pi->ppd) {
742 MESSAGE("Couldn't find PPD file '%s', expect a crash now!\n",
743 ppdFileName);
744 goto closeprinter;
747 /* Some gimp-print ppd files don't contain a DefaultResolution line
748 set it to 300 if it's not specified */
749 if(pi->ppd->DefaultResolution == 0)
750 pi->ppd->DefaultResolution = 300;
752 if(using_default_devmode) {
753 DWORD papersize;
755 if(GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_IPAPERSIZE | LOCALE_RETURN_NUMBER,
756 (LPWSTR)&papersize, sizeof(papersize)/sizeof(WCHAR))) {
757 PSDRV_DEVMODEA dm;
758 memset(&dm, 0, sizeof(dm));
759 dm.dmPublic.dmFields = DM_PAPERSIZE;
760 dm.dmPublic.u1.s1.dmPaperSize = papersize;
761 PSDRV_MergeDevmodes(pi->Devmode, &dm, pi);
764 SetPrinterDataExA(hPrinter, NULL, default_devmodeA, REG_BINARY,
765 (LPBYTE)pi->Devmode, sizeof(DefaultDevmode));
768 if(pi->ppd->DefaultPageSize) { /* We'll let the ppd override the devmode */
769 PSDRV_DEVMODEA dm;
770 memset(&dm, 0, sizeof(dm));
771 dm.dmPublic.dmFields = DM_PAPERSIZE;
772 dm.dmPublic.u1.s1.dmPaperSize = pi->ppd->DefaultPageSize->WinPage;
773 PSDRV_MergeDevmodes(pi->Devmode, &dm, pi);
777 * This is a hack. The default paper size should be read in as part of
778 * the Devmode structure, but Wine doesn't currently provide a convenient
779 * way to configure printers.
781 res = GetPrinterDataExA(hPrinter, "PrinterDriverData", "Paper Size", NULL,
782 (LPBYTE)&dwPaperSize, sizeof(DWORD), &needed);
783 if (res == ERROR_SUCCESS)
784 pi->Devmode->dmPublic.u1.s1.dmPaperSize = (SHORT) dwPaperSize;
785 else if (res == ERROR_FILE_NOT_FOUND)
786 TRACE ("No 'Paper Size' for printer '%s'\n", debugstr_w(name));
787 else {
788 ERR ("GetPrinterDataA returned %i\n", res);
789 goto closeprinter;
792 /* Duplex is indicated by the setting of the DM_DUPLEX bit in dmFields.
793 WinDuplex == 0 is a special case which means that the ppd has a
794 *DefaultDuplex: NotCapable entry. In this case we'll try not to confuse
795 apps and set dmDuplex to DMDUP_SIMPLEX but leave the DM_DUPLEX clear.
796 PSDRV_WriteHeader understands this and copes. */
797 pi->Devmode->dmPublic.dmFields &= ~DM_DUPLEX;
798 if(pi->ppd->DefaultDuplex) {
799 pi->Devmode->dmPublic.dmDuplex = pi->ppd->DefaultDuplex->WinDuplex;
800 if(pi->Devmode->dmPublic.dmDuplex != 0)
801 pi->Devmode->dmPublic.dmFields |= DM_DUPLEX;
802 else
803 pi->Devmode->dmPublic.dmDuplex = DMDUP_SIMPLEX;
806 pi->FontSubTable = load_font_sub_table( hPrinter, &pi->FontSubTableSize );
808 ClosePrinter( hPrinter );
810 pi->Fonts = NULL;
812 LIST_FOR_EACH_ENTRY( font, &pi->ppd->InstalledFonts, FONTNAME, entry )
814 afm = PSDRV_FindAFMinList(PSDRV_AFMFontList, font->Name);
815 if(!afm) {
816 TRACE( "Couldn't find AFM file for installed printer font '%s' - "
817 "ignoring\n", font->Name);
819 else {
820 BOOL added;
821 if (PSDRV_AddAFMtoList(&pi->Fonts, afm, &added) == FALSE) {
822 PSDRV_FreeAFMList(pi->Fonts);
823 goto cleanup;
828 HeapFree( GetProcessHeap(), 0, nameA );
829 if (ppd) unlink(ppd);
830 list_add_head( &printer_list, &pi->entry );
831 return pi;
833 closeprinter:
834 ClosePrinter(hPrinter);
835 cleanup:
836 HeapFree(PSDRV_Heap, 0, ppdFileName);
837 HeapFree(PSDRV_Heap, 0, pi->FontSubTable);
838 HeapFree(PSDRV_Heap, 0, pi->friendly_name);
839 HeapFree(PSDRV_Heap, 0, pi->Devmode);
840 fail:
841 HeapFree(PSDRV_Heap, 0, pi);
842 HeapFree( GetProcessHeap(), 0, nameA );
843 if (ppd) unlink(ppd);
844 return NULL;
848 static const struct gdi_dc_funcs psdrv_funcs =
850 NULL, /* pAbortDoc */
851 NULL, /* pAbortPath */
852 NULL, /* pAlphaBlend */
853 NULL, /* pAngleArc */
854 PSDRV_Arc, /* pArc */
855 NULL, /* pArcTo */
856 NULL, /* pBeginPath */
857 NULL, /* pBlendImage */
858 NULL, /* pChoosePixelFormat */
859 PSDRV_Chord, /* pChord */
860 NULL, /* pCloseFigure */
861 NULL, /* pCopyBitmap */
862 NULL, /* pCreateBitmap */
863 PSDRV_CreateCompatibleDC, /* pCreateCompatibleDC */
864 PSDRV_CreateDC, /* pCreateDC */
865 NULL, /* pDeleteBitmap */
866 PSDRV_DeleteDC, /* pDeleteDC */
867 NULL, /* pDeleteObject */
868 NULL, /* pDescribePixelFormat */
869 PSDRV_DeviceCapabilities, /* pDeviceCapabilities */
870 PSDRV_Ellipse, /* pEllipse */
871 PSDRV_EndDoc, /* pEndDoc */
872 PSDRV_EndPage, /* pEndPage */
873 NULL, /* pEndPath */
874 PSDRV_EnumFonts, /* pEnumFonts */
875 NULL, /* pEnumICMProfiles */
876 NULL, /* pExcludeClipRect */
877 PSDRV_ExtDeviceMode, /* pExtDeviceMode */
878 PSDRV_ExtEscape, /* pExtEscape */
879 NULL, /* pExtFloodFill */
880 NULL, /* pExtSelectClipRgn */
881 PSDRV_ExtTextOut, /* pExtTextOut */
882 PSDRV_FillPath, /* pFillPath */
883 NULL, /* pFillRgn */
884 NULL, /* pFlattenPath */
885 NULL, /* pFontIsLinked */
886 NULL, /* pFrameRgn */
887 NULL, /* pGdiComment */
888 NULL, /* pGdiRealizationInfo */
889 NULL, /* pGetCharABCWidths */
890 NULL, /* pGetCharABCWidthsI */
891 PSDRV_GetCharWidth, /* pGetCharWidth */
892 PSDRV_GetDeviceCaps, /* pGetDeviceCaps */
893 NULL, /* pGetDeviceGammaRamp */
894 NULL, /* pGetFontData */
895 NULL, /* pGetFontUnicodeRanges */
896 NULL, /* pGetGlyphIndices */
897 NULL, /* pGetGlyphOutline */
898 NULL, /* pGetICMProfile */
899 NULL, /* pGetImage */
900 NULL, /* pGetKerningPairs */
901 NULL, /* pGetNearestColor */
902 NULL, /* pGetOutlineTextMetrics */
903 NULL, /* pGetPixel */
904 NULL, /* pGetPixelFormat */
905 NULL, /* pGetSystemPaletteEntries */
906 NULL, /* pGetTextCharsetInfo */
907 PSDRV_GetTextExtentExPoint, /* pGetTextExtentExPoint */
908 NULL, /* pGetTextExtentExPointI */
909 NULL, /* pGetTextFace */
910 PSDRV_GetTextMetrics, /* pGetTextMetrics */
911 NULL, /* pGradientFill */
912 NULL, /* pIntersectClipRect */
913 NULL, /* pInvertRgn */
914 PSDRV_LineTo, /* pLineTo */
915 NULL, /* pModifyWorldTransform */
916 NULL, /* pMoveTo */
917 NULL, /* pOffsetClipRgn */
918 NULL, /* pOffsetViewportOrg */
919 NULL, /* pOffsetWindowOrg */
920 PSDRV_PaintRgn, /* pPaintRgn */
921 PSDRV_PatBlt, /* pPatBlt */
922 PSDRV_Pie, /* pPie */
923 PSDRV_PolyBezier, /* pPolyBezier */
924 PSDRV_PolyBezierTo, /* pPolyBezierTo */
925 NULL, /* pPolyDraw */
926 PSDRV_PolyPolygon, /* pPolyPolygon */
927 PSDRV_PolyPolyline, /* pPolyPolyline */
928 NULL, /* pPolygon */
929 NULL, /* pPolyline */
930 NULL, /* pPolylineTo */
931 PSDRV_PutImage, /* pPutImage */
932 NULL, /* pRealizeDefaultPalette */
933 NULL, /* pRealizePalette */
934 PSDRV_Rectangle, /* pRectangle */
935 PSDRV_ResetDC, /* pResetDC */
936 NULL, /* pRestoreDC */
937 PSDRV_RoundRect, /* pRoundRect */
938 NULL, /* pSaveDC */
939 NULL, /* pScaleViewportExt */
940 NULL, /* pScaleWindowExt */
941 NULL, /* pSelectBitmap */
942 PSDRV_SelectBrush, /* pSelectBrush */
943 NULL, /* pSelectClipPath */
944 PSDRV_SelectFont, /* pSelectFont */
945 NULL, /* pSelectPalette */
946 PSDRV_SelectPen, /* pSelectPen */
947 NULL, /* pSetArcDirection */
948 PSDRV_SetBkColor, /* pSetBkColor */
949 NULL, /* pSetBkMode */
950 PSDRV_SetDCBrushColor, /* pSetDCBrushColor */
951 PSDRV_SetDCPenColor, /* pSetDCPenColor */
952 NULL, /* pSetDIBitsToDevice */
953 NULL, /* pSetDeviceClipping */
954 NULL, /* pSetDeviceGammaRamp */
955 NULL, /* pSetLayout */
956 NULL, /* pSetMapMode */
957 NULL, /* pSetMapperFlags */
958 PSDRV_SetPixel, /* pSetPixel */
959 NULL, /* pSetPixelFormat */
960 NULL, /* pSetPolyFillMode */
961 NULL, /* pSetROP2 */
962 NULL, /* pSetRelAbs */
963 NULL, /* pSetStretchBltMode */
964 NULL, /* pSetTextAlign */
965 NULL, /* pSetTextCharacterExtra */
966 PSDRV_SetTextColor, /* pSetTextColor */
967 NULL, /* pSetTextJustification */
968 NULL, /* pSetViewportExt */
969 NULL, /* pSetViewportOrg */
970 NULL, /* pSetWindowExt */
971 NULL, /* pSetWindowOrg */
972 NULL, /* pSetWorldTransform */
973 PSDRV_StartDoc, /* pStartDoc */
974 PSDRV_StartPage, /* pStartPage */
975 NULL, /* pStretchBlt */
976 NULL, /* pStretchDIBits */
977 PSDRV_StrokeAndFillPath, /* pStrokeAndFillPath */
978 PSDRV_StrokePath, /* pStrokePath */
979 NULL, /* pSwapBuffers */
980 NULL, /* pUnrealizePalette */
981 NULL, /* pWidenPath */
982 /* OpenGL not supported */
986 /******************************************************************************
987 * PSDRV_get_gdi_driver
989 const struct gdi_dc_funcs * CDECL PSDRV_get_gdi_driver( unsigned int version )
991 if (version != WINE_GDI_DRIVER_VERSION)
993 ERR( "version mismatch, gdi32 wants %u but wineps has %u\n", version, WINE_GDI_DRIVER_VERSION );
994 return NULL;
996 return &psdrv_funcs;