user32: Support returning to the process default in SetThreadDpiAwarenessContext().
[wine.git] / dlls / wineps.drv / init.c
blob2eb03b836ca504b1260be5546515479a567830ba
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>
28 #define NONAMELESSUNION
29 #define NONAMELESSSTRUCT
31 #include "windef.h"
32 #include "winbase.h"
33 #include "winerror.h"
34 #include "winreg.h"
35 #include "winnls.h"
36 #include "winuser.h"
37 #include "psdrv.h"
38 #include "winspool.h"
39 #include "wine/library.h"
40 #include "wine/debug.h"
42 WINE_DEFAULT_DEBUG_CHANNEL(psdrv);
44 static const PSDRV_DEVMODE DefaultDevmode =
46 { /* dmPublic */
47 /* dmDeviceName */ {'W','i','n','e',' ','P','o','s','t','S','c','r','i','p','t',' ','D','r','i','v','e','r',0},
48 /* dmSpecVersion */ 0x30a,
49 /* dmDriverVersion */ 0x001,
50 /* dmSize */ sizeof(DEVMODEW),
51 /* dmDriverExtra */ sizeof(PSDRV_DEVMODE)-sizeof(DEVMODEW),
52 /* dmFields */ DM_ORIENTATION | DM_PAPERSIZE | DM_PAPERLENGTH | DM_PAPERWIDTH |
53 DM_SCALE | DM_COPIES | DM_DEFAULTSOURCE | DM_PRINTQUALITY |
54 DM_COLOR | DM_DUPLEX | DM_YRESOLUTION | DM_TTOPTION |
55 DM_COLLATE | DM_FORMNAME,
56 { /* u1 */
57 { /* s1 */
58 /* dmOrientation */ DMORIENT_PORTRAIT,
59 /* dmPaperSize */ DMPAPER_LETTER,
60 /* dmPaperLength */ 2794,
61 /* dmPaperWidth */ 2159,
62 /* dmScale */ 100,
63 /* dmCopies */ 1,
64 /* dmDefaultSource */ DMBIN_AUTO,
65 /* dmPrintQuality */ 300
68 /* dmColor */ DMCOLOR_COLOR,
69 /* dmDuplex */ DMDUP_SIMPLEX,
70 /* dmYResolution */ 300,
71 /* dmTTOption */ DMTT_SUBDEV,
72 /* dmCollate */ DMCOLLATE_FALSE,
73 /* dmFormName */ {'L','e','t','t','e','r',0},
74 /* dmLogPixels */ 0,
75 /* dmBitsPerPel */ 0,
76 /* dmPelsWidth */ 0,
77 /* dmPelsHeight */ 0,
78 { /* u2 */
79 /* dmDisplayFlags */ 0
81 /* dmDisplayFrequency */ 0,
82 /* dmICMMethod */ 0,
83 /* dmICMIntent */ 0,
84 /* dmMediaType */ 0,
85 /* dmDitherType */ 0,
86 /* dmReserved1 */ 0,
87 /* dmReserved2 */ 0,
88 /* dmPanningWidth */ 0,
89 /* dmPanningHeight */ 0
91 { /* dmDocPrivate */
92 /* dummy */ 0
94 { /* dmDrvPrivate */
95 /* numInstalledOptions */ 0
99 HINSTANCE PSDRV_hInstance = 0;
100 HANDLE PSDRV_Heap = 0;
102 static HFONT PSDRV_DefaultFont = 0;
103 static const LOGFONTA DefaultLogFont = {
104 100, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, ANSI_CHARSET, 0, 0,
105 DEFAULT_QUALITY, FIXED_PITCH | FF_MODERN, ""
108 static const struct gdi_dc_funcs psdrv_funcs;
110 /*********************************************************************
111 * DllMain
113 * Initializes font metrics and registers driver. wineps dll entry point.
116 BOOL WINAPI DllMain( HINSTANCE hinst, DWORD reason, LPVOID reserved )
118 TRACE("(%p, %d, %p)\n", hinst, reason, reserved);
120 switch(reason) {
122 case DLL_PROCESS_ATTACH:
123 PSDRV_hInstance = hinst;
124 DisableThreadLibraryCalls(hinst);
126 PSDRV_Heap = HeapCreate(0, 0x10000, 0);
127 if (PSDRV_Heap == NULL)
128 return FALSE;
130 if (PSDRV_GetFontMetrics() == FALSE) {
131 HeapDestroy(PSDRV_Heap);
132 return FALSE;
135 PSDRV_DefaultFont = CreateFontIndirectA(&DefaultLogFont);
136 if (PSDRV_DefaultFont == NULL) {
137 HeapDestroy(PSDRV_Heap);
138 return FALSE;
140 break;
142 case DLL_PROCESS_DETACH:
143 if (reserved) break;
144 DeleteObject( PSDRV_DefaultFont );
145 HeapDestroy( PSDRV_Heap );
146 break;
149 return TRUE;
152 static void dump_fields(DWORD fields)
154 int add_space = 0;
156 #define CHECK_FIELD(flag) \
157 do \
159 if (fields & flag) \
161 if (add_space++) DPRINTF(" "); \
162 TRACE(#flag); \
163 fields &= ~flag; \
166 while (0)
168 CHECK_FIELD(DM_ORIENTATION);
169 CHECK_FIELD(DM_PAPERSIZE);
170 CHECK_FIELD(DM_PAPERLENGTH);
171 CHECK_FIELD(DM_PAPERWIDTH);
172 CHECK_FIELD(DM_SCALE);
173 CHECK_FIELD(DM_POSITION);
174 CHECK_FIELD(DM_NUP);
175 CHECK_FIELD(DM_DISPLAYORIENTATION);
176 CHECK_FIELD(DM_COPIES);
177 CHECK_FIELD(DM_DEFAULTSOURCE);
178 CHECK_FIELD(DM_PRINTQUALITY);
179 CHECK_FIELD(DM_COLOR);
180 CHECK_FIELD(DM_DUPLEX);
181 CHECK_FIELD(DM_YRESOLUTION);
182 CHECK_FIELD(DM_TTOPTION);
183 CHECK_FIELD(DM_COLLATE);
184 CHECK_FIELD(DM_FORMNAME);
185 CHECK_FIELD(DM_LOGPIXELS);
186 CHECK_FIELD(DM_BITSPERPEL);
187 CHECK_FIELD(DM_PELSWIDTH);
188 CHECK_FIELD(DM_PELSHEIGHT);
189 CHECK_FIELD(DM_DISPLAYFLAGS);
190 CHECK_FIELD(DM_DISPLAYFREQUENCY);
191 CHECK_FIELD(DM_ICMMETHOD);
192 CHECK_FIELD(DM_ICMINTENT);
193 CHECK_FIELD(DM_MEDIATYPE);
194 CHECK_FIELD(DM_DITHERTYPE);
195 CHECK_FIELD(DM_PANNINGWIDTH);
196 CHECK_FIELD(DM_PANNINGHEIGHT);
197 if (fields) TRACE(" %#x", fields);
198 TRACE("\n");
199 #undef CHECK_FIELD
202 /* Dump DEVMODE structure without a device specific part.
203 * Some applications and drivers fail to specify correct field
204 * flags (like DM_FORMNAME), so dump everything.
206 static void dump_devmode(const DEVMODEW *dm)
208 if (!TRACE_ON(psdrv)) return;
210 TRACE("dmDeviceName: %s\n", debugstr_w(dm->dmDeviceName));
211 TRACE("dmSpecVersion: 0x%04x\n", dm->dmSpecVersion);
212 TRACE("dmDriverVersion: 0x%04x\n", dm->dmDriverVersion);
213 TRACE("dmSize: 0x%04x\n", dm->dmSize);
214 TRACE("dmDriverExtra: 0x%04x\n", dm->dmDriverExtra);
215 TRACE("dmFields: 0x%04x\n", dm->dmFields);
216 dump_fields(dm->dmFields);
217 TRACE("dmOrientation: %d\n", dm->u1.s1.dmOrientation);
218 TRACE("dmPaperSize: %d\n", dm->u1.s1.dmPaperSize);
219 TRACE("dmPaperLength: %d\n", dm->u1.s1.dmPaperLength);
220 TRACE("dmPaperWidth: %d\n", dm->u1.s1.dmPaperWidth);
221 TRACE("dmScale: %d\n", dm->u1.s1.dmScale);
222 TRACE("dmCopies: %d\n", dm->u1.s1.dmCopies);
223 TRACE("dmDefaultSource: %d\n", dm->u1.s1.dmDefaultSource);
224 TRACE("dmPrintQuality: %d\n", dm->u1.s1.dmPrintQuality);
225 TRACE("dmColor: %d\n", dm->dmColor);
226 TRACE("dmDuplex: %d\n", dm->dmDuplex);
227 TRACE("dmYResolution: %d\n", dm->dmYResolution);
228 TRACE("dmTTOption: %d\n", dm->dmTTOption);
229 TRACE("dmCollate: %d\n", dm->dmCollate);
230 TRACE("dmFormName: %s\n", debugstr_w(dm->dmFormName));
231 TRACE("dmLogPixels %u\n", dm->dmLogPixels);
232 TRACE("dmBitsPerPel %u\n", dm->dmBitsPerPel);
233 TRACE("dmPelsWidth %u\n", dm->dmPelsWidth);
234 TRACE("dmPelsHeight %u\n", dm->dmPelsHeight);
237 static void PSDRV_UpdateDevCaps( PSDRV_PDEVICE *physDev )
239 PAGESIZE *page;
240 RESOLUTION *res;
241 INT width = 0, height = 0, resx = 0, resy = 0;
243 dump_devmode(&physDev->Devmode->dmPublic);
245 if (physDev->Devmode->dmPublic.dmFields & (DM_PRINTQUALITY | DM_YRESOLUTION | DM_LOGPIXELS))
247 if (physDev->Devmode->dmPublic.dmFields & DM_PRINTQUALITY)
248 resx = resy = physDev->Devmode->dmPublic.u1.s1.dmPrintQuality;
250 if (physDev->Devmode->dmPublic.dmFields & DM_YRESOLUTION)
251 resy = physDev->Devmode->dmPublic.dmYResolution;
253 if (physDev->Devmode->dmPublic.dmFields & DM_LOGPIXELS)
254 resx = resy = physDev->Devmode->dmPublic.dmLogPixels;
256 LIST_FOR_EACH_ENTRY(res, &physDev->pi->ppd->Resolutions, RESOLUTION, entry)
258 if (res->resx == resx && res->resy == resy)
260 physDev->logPixelsX = resx;
261 physDev->logPixelsY = resy;
262 break;
266 if (&res->entry == &physDev->pi->ppd->Resolutions)
268 WARN("Requested resolution %dx%d is not supported by device\n", resx, resy);
269 physDev->logPixelsX = physDev->pi->ppd->DefaultResolution;
270 physDev->logPixelsY = physDev->logPixelsX;
273 else
275 WARN("Using default device resolution %d\n", physDev->pi->ppd->DefaultResolution);
276 physDev->logPixelsX = physDev->pi->ppd->DefaultResolution;
277 physDev->logPixelsY = physDev->logPixelsX;
280 if(physDev->Devmode->dmPublic.dmFields & DM_PAPERSIZE) {
281 LIST_FOR_EACH_ENTRY(page, &physDev->pi->ppd->PageSizes, PAGESIZE, entry) {
282 if(page->WinPage == physDev->Devmode->dmPublic.u1.s1.dmPaperSize)
283 break;
286 if(&page->entry == &physDev->pi->ppd->PageSizes) {
287 FIXME("Can't find page\n");
288 SetRectEmpty(&physDev->ImageableArea);
289 physDev->PageSize.cx = 0;
290 physDev->PageSize.cy = 0;
291 } else if(page->ImageableArea) {
292 /* physDev sizes in device units; ppd sizes in 1/72" */
293 SetRect(&physDev->ImageableArea, page->ImageableArea->llx * physDev->logPixelsX / 72,
294 page->ImageableArea->ury * physDev->logPixelsY / 72,
295 page->ImageableArea->urx * physDev->logPixelsX / 72,
296 page->ImageableArea->lly * physDev->logPixelsY / 72);
297 physDev->PageSize.cx = page->PaperDimension->x *
298 physDev->logPixelsX / 72;
299 physDev->PageSize.cy = page->PaperDimension->y *
300 physDev->logPixelsY / 72;
301 } else {
302 physDev->ImageableArea.left = physDev->ImageableArea.bottom = 0;
303 physDev->ImageableArea.right = physDev->PageSize.cx =
304 page->PaperDimension->x * physDev->logPixelsX / 72;
305 physDev->ImageableArea.top = physDev->PageSize.cy =
306 page->PaperDimension->y * physDev->logPixelsY / 72;
308 } else if((physDev->Devmode->dmPublic.dmFields & DM_PAPERLENGTH) &&
309 (physDev->Devmode->dmPublic.dmFields & DM_PAPERWIDTH)) {
310 /* physDev sizes in device units; Devmode sizes in 1/10 mm */
311 physDev->ImageableArea.left = physDev->ImageableArea.bottom = 0;
312 physDev->ImageableArea.right = physDev->PageSize.cx =
313 physDev->Devmode->dmPublic.u1.s1.dmPaperWidth *
314 physDev->logPixelsX / 254;
315 physDev->ImageableArea.top = physDev->PageSize.cy =
316 physDev->Devmode->dmPublic.u1.s1.dmPaperLength *
317 physDev->logPixelsY / 254;
318 } else {
319 FIXME("Odd dmFields %x\n", physDev->Devmode->dmPublic.dmFields);
320 SetRectEmpty(&physDev->ImageableArea);
321 physDev->PageSize.cx = 0;
322 physDev->PageSize.cy = 0;
325 TRACE("ImageableArea = %s: PageSize = %dx%d\n", wine_dbgstr_rect(&physDev->ImageableArea),
326 physDev->PageSize.cx, physDev->PageSize.cy);
328 /* these are in device units */
329 width = physDev->ImageableArea.right - physDev->ImageableArea.left;
330 height = physDev->ImageableArea.top - physDev->ImageableArea.bottom;
332 if(physDev->Devmode->dmPublic.u1.s1.dmOrientation == DMORIENT_PORTRAIT) {
333 physDev->horzRes = width;
334 physDev->vertRes = height;
335 } else {
336 physDev->horzRes = height;
337 physDev->vertRes = width;
340 /* these are in mm */
341 physDev->horzSize = (physDev->horzRes * 25.4) / physDev->logPixelsX;
342 physDev->vertSize = (physDev->vertRes * 25.4) / physDev->logPixelsY;
344 TRACE("devcaps: horzSize = %dmm, vertSize = %dmm, "
345 "horzRes = %d, vertRes = %d\n",
346 physDev->horzSize, physDev->vertSize,
347 physDev->horzRes, physDev->vertRes);
350 static PSDRV_PDEVICE *create_psdrv_physdev( PRINTERINFO *pi )
352 PSDRV_PDEVICE *physDev;
354 physDev = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*physDev) );
355 if (!physDev) return NULL;
357 physDev->Devmode = HeapAlloc( GetProcessHeap(), 0, sizeof(PSDRV_DEVMODE) );
358 if (!physDev->Devmode)
360 HeapFree( GetProcessHeap(), 0, physDev );
361 return NULL;
364 *physDev->Devmode = *pi->Devmode;
365 physDev->pi = pi;
366 physDev->logPixelsX = pi->ppd->DefaultResolution;
367 physDev->logPixelsY = pi->ppd->DefaultResolution;
368 return physDev;
371 /**********************************************************************
372 * PSDRV_CreateDC
374 static BOOL PSDRV_CreateDC( PHYSDEV *pdev, LPCWSTR driver, LPCWSTR device,
375 LPCWSTR output, const DEVMODEW* initData )
377 PSDRV_PDEVICE *physDev;
378 PRINTERINFO *pi;
380 TRACE("(%s %s %s %p)\n", debugstr_w(driver), debugstr_w(device),
381 debugstr_w(output), initData);
383 if (!device) return FALSE;
384 pi = PSDRV_FindPrinterInfo( device );
385 if(!pi) return FALSE;
387 if(!pi->Fonts) {
388 RASTERIZER_STATUS status;
389 if(!GetRasterizerCaps(&status, sizeof(status)) ||
390 !(status.wFlags & TT_AVAILABLE) ||
391 !(status.wFlags & TT_ENABLED)) {
392 MESSAGE("Disabling printer %s since it has no builtin fonts and there are no TrueType fonts available.\n",
393 debugstr_w(device));
394 return FALSE;
398 if (!(physDev = create_psdrv_physdev( pi ))) return FALSE;
400 if (output && *output) physDev->job.output = strdupW( output );
402 if(initData)
404 dump_devmode(initData);
405 PSDRV_MergeDevmodes(physDev->Devmode, (const PSDRV_DEVMODE *)initData, pi);
408 PSDRV_UpdateDevCaps(physDev);
409 SelectObject( (*pdev)->hdc, PSDRV_DefaultFont );
410 push_dc_driver( pdev, &physDev->dev, &psdrv_funcs );
411 return TRUE;
415 /**********************************************************************
416 * PSDRV_CreateCompatibleDC
418 static BOOL PSDRV_CreateCompatibleDC( PHYSDEV orig, PHYSDEV *pdev )
420 HDC hdc = (*pdev)->hdc;
421 PSDRV_PDEVICE *physDev, *orig_dev = get_psdrv_dev( orig );
422 PRINTERINFO *pi = PSDRV_FindPrinterInfo( orig_dev->pi->friendly_name );
424 if (!pi) return FALSE;
425 if (!(physDev = create_psdrv_physdev( pi ))) return FALSE;
426 PSDRV_MergeDevmodes( physDev->Devmode, orig_dev->Devmode, pi );
427 PSDRV_UpdateDevCaps(physDev);
428 SelectObject( hdc, PSDRV_DefaultFont );
429 push_dc_driver( pdev, &physDev->dev, &psdrv_funcs );
430 return TRUE;
435 /**********************************************************************
436 * PSDRV_DeleteDC
438 static BOOL PSDRV_DeleteDC( PHYSDEV dev )
440 PSDRV_PDEVICE *physDev = get_psdrv_dev( dev );
442 TRACE("\n");
444 HeapFree( GetProcessHeap(), 0, physDev->Devmode );
445 HeapFree( GetProcessHeap(), 0, physDev->job.output );
446 HeapFree( GetProcessHeap(), 0, physDev );
448 return TRUE;
452 /**********************************************************************
453 * ResetDC (WINEPS.@)
455 static HDC PSDRV_ResetDC( PHYSDEV dev, const DEVMODEW *lpInitData )
457 PSDRV_PDEVICE *physDev = get_psdrv_dev( dev );
459 if (lpInitData)
461 PSDRV_MergeDevmodes(physDev->Devmode, (const PSDRV_DEVMODE *)lpInitData, physDev->pi);
462 PSDRV_UpdateDevCaps(physDev);
464 return dev->hdc;
467 /***********************************************************************
468 * GetDeviceCaps (WINEPS.@)
470 static INT PSDRV_GetDeviceCaps( PHYSDEV dev, INT cap )
472 PSDRV_PDEVICE *physDev = get_psdrv_dev( dev );
474 TRACE("%p,%d\n", dev->hdc, cap);
476 switch(cap)
478 case DRIVERVERSION:
479 return 0;
480 case TECHNOLOGY:
481 return DT_RASPRINTER;
482 case HORZSIZE:
483 return MulDiv(physDev->horzSize, 100,
484 physDev->Devmode->dmPublic.u1.s1.dmScale);
485 case VERTSIZE:
486 return MulDiv(physDev->vertSize, 100,
487 physDev->Devmode->dmPublic.u1.s1.dmScale);
488 case HORZRES:
489 return physDev->horzRes;
490 case VERTRES:
491 return physDev->vertRes;
492 case BITSPIXEL:
493 /* Although Windows returns 1 for monochrome printers, we want
494 CreateCompatibleBitmap to provide something other than 1 bpp */
495 return 32;
496 case NUMPENS:
497 return 10;
498 case NUMFONTS:
499 return 39;
500 case NUMCOLORS:
501 return -1;
502 case PDEVICESIZE:
503 return sizeof(PSDRV_PDEVICE);
504 case TEXTCAPS:
505 return TC_CR_ANY | TC_VA_ABLE; /* psdrv 0x59f7 */
506 case RASTERCAPS:
507 return (RC_BITBLT | RC_BITMAP64 | RC_GDI20_OUTPUT | RC_DIBTODEV |
508 RC_STRETCHBLT | RC_STRETCHDIB); /* psdrv 0x6e99 */
509 case ASPECTX:
510 return physDev->logPixelsX;
511 case ASPECTY:
512 return physDev->logPixelsY;
513 case LOGPIXELSX:
514 return MulDiv(physDev->logPixelsX,
515 physDev->Devmode->dmPublic.u1.s1.dmScale, 100);
516 case LOGPIXELSY:
517 return MulDiv(physDev->logPixelsY,
518 physDev->Devmode->dmPublic.u1.s1.dmScale, 100);
519 case NUMRESERVED:
520 return 0;
521 case COLORRES:
522 return 0;
523 case PHYSICALWIDTH:
524 return (physDev->Devmode->dmPublic.u1.s1.dmOrientation == DMORIENT_LANDSCAPE) ?
525 physDev->PageSize.cy : physDev->PageSize.cx;
526 case PHYSICALHEIGHT:
527 return (physDev->Devmode->dmPublic.u1.s1.dmOrientation == DMORIENT_LANDSCAPE) ?
528 physDev->PageSize.cx : physDev->PageSize.cy;
529 case PHYSICALOFFSETX:
530 if(physDev->Devmode->dmPublic.u1.s1.dmOrientation == DMORIENT_LANDSCAPE) {
531 if(physDev->pi->ppd->LandscapeOrientation == -90)
532 return physDev->PageSize.cy - physDev->ImageableArea.top;
533 else
534 return physDev->ImageableArea.bottom;
536 return physDev->ImageableArea.left;
538 case PHYSICALOFFSETY:
539 if(physDev->Devmode->dmPublic.u1.s1.dmOrientation == DMORIENT_LANDSCAPE) {
540 if(physDev->pi->ppd->LandscapeOrientation == -90)
541 return physDev->PageSize.cx - physDev->ImageableArea.right;
542 else
543 return physDev->ImageableArea.left;
545 return physDev->PageSize.cy - physDev->ImageableArea.top;
547 default:
548 dev = GET_NEXT_PHYSDEV( dev, pGetDeviceCaps );
549 return dev->funcs->pGetDeviceCaps( dev, cap );
553 static PRINTER_ENUM_VALUESA *load_font_sub_table( HANDLE printer, DWORD *num_entries )
555 DWORD res, needed, num;
556 PRINTER_ENUM_VALUESA *table = NULL;
557 static const char fontsubkey[] = "PrinterDriverData\\FontSubTable";
559 *num_entries = 0;
561 res = EnumPrinterDataExA( printer, fontsubkey, NULL, 0, &needed, &num );
562 if (res != ERROR_MORE_DATA) return NULL;
564 table = HeapAlloc( PSDRV_Heap, 0, needed );
565 if (!table) return NULL;
567 res = EnumPrinterDataExA( printer, fontsubkey, (LPBYTE)table, needed, &needed, &num );
568 if (res != ERROR_SUCCESS)
570 HeapFree( PSDRV_Heap, 0, table );
571 return NULL;
574 *num_entries = num;
575 return table;
578 static PSDRV_DEVMODE *get_printer_devmode( HANDLE printer )
580 DWORD needed, dm_size;
581 BOOL res;
582 PRINTER_INFO_9W *info;
583 PSDRV_DEVMODE *dm;
585 GetPrinterW( printer, 9, NULL, 0, &needed );
586 if (GetLastError() != ERROR_INSUFFICIENT_BUFFER) return NULL;
588 info = HeapAlloc( PSDRV_Heap, 0, needed );
589 res = GetPrinterW( printer, 9, (BYTE *)info, needed, &needed );
590 if (!res || !info->pDevMode)
592 HeapFree( PSDRV_Heap, 0, info );
593 return NULL;
596 /* sanity check the sizes */
597 dm_size = info->pDevMode->dmSize + info->pDevMode->dmDriverExtra;
598 if ((char *)info->pDevMode - (char *)info + dm_size > needed)
600 HeapFree( PSDRV_Heap, 0, info );
601 return NULL;
604 dm = (PSDRV_DEVMODE*)info;
605 memmove( dm, info->pDevMode, dm_size );
606 return dm;
609 static PSDRV_DEVMODE *get_devmode( HANDLE printer, const WCHAR *name, BOOL *is_default )
611 PSDRV_DEVMODE *dm = get_printer_devmode( printer );
613 *is_default = FALSE;
615 if (dm && dm->dmPublic.dmSize + dm->dmPublic.dmDriverExtra >= sizeof(DefaultDevmode))
617 TRACE( "Retrieved devmode from winspool\n" );
618 return dm;
620 HeapFree( PSDRV_Heap, 0, dm );
622 TRACE( "Using default devmode\n" );
623 dm = HeapAlloc( PSDRV_Heap, 0, sizeof(DefaultDevmode) );
624 if (dm)
626 *dm = DefaultDevmode;
627 lstrcpynW( (WCHAR *)dm->dmPublic.dmDeviceName, name, CCHDEVICENAME );
628 *is_default = TRUE;
630 return dm;
633 static BOOL set_devmode( HANDLE printer, PSDRV_DEVMODE *dm )
635 PRINTER_INFO_9W info;
636 info.pDevMode = &dm->dmPublic;
638 return SetPrinterW( printer, 9, (BYTE *)&info, 0 );
641 static WCHAR *get_ppd_filename( HANDLE printer )
643 DWORD needed;
644 DRIVER_INFO_2W *info;
645 WCHAR *name;
647 GetPrinterDriverW( printer, NULL, 2, NULL, 0, &needed );
648 if (GetLastError() != ERROR_INSUFFICIENT_BUFFER) return NULL;
649 info = HeapAlloc( GetProcessHeap(), 0, needed );
650 if (!info) return NULL;
651 GetPrinterDriverW( printer, NULL, 2, (BYTE*)info, needed, &needed );
652 name = (WCHAR *)info;
653 memmove( name, info->pDataFile, (strlenW( info->pDataFile ) + 1) * sizeof(WCHAR) );
654 return name;
657 static struct list printer_list = LIST_INIT( printer_list );
659 /**********************************************************************
660 * PSDRV_FindPrinterInfo
662 PRINTERINFO *PSDRV_FindPrinterInfo(LPCWSTR name)
664 PRINTERINFO *pi;
665 FONTNAME *font;
666 const AFM *afm;
667 HANDLE hPrinter = 0;
668 WCHAR *ppd_filename = NULL;
669 char *nameA = NULL;
670 BOOL using_default_devmode = FALSE;
671 int len;
673 TRACE("'%s'\n", debugstr_w(name));
675 LIST_FOR_EACH_ENTRY( pi, &printer_list, PRINTERINFO, entry )
677 if (!strcmpW( pi->friendly_name, name ))
678 return pi;
681 pi = HeapAlloc( PSDRV_Heap, HEAP_ZERO_MEMORY, sizeof(*pi) );
682 if (pi == NULL) return NULL;
684 if (!(pi->friendly_name = HeapAlloc( PSDRV_Heap, 0, (strlenW(name)+1)*sizeof(WCHAR) ))) goto fail;
685 strcpyW( pi->friendly_name, name );
687 if (OpenPrinterW( pi->friendly_name, &hPrinter, NULL ) == 0) {
688 ERR ("OpenPrinter failed with code %i\n", GetLastError ());
689 goto fail;
692 len = WideCharToMultiByte( CP_ACP, 0, name, -1, NULL, 0, NULL, NULL );
693 nameA = HeapAlloc( GetProcessHeap(), 0, len );
694 WideCharToMultiByte( CP_ACP, 0, name, -1, nameA, len, NULL, NULL );
696 pi->Devmode = get_devmode( hPrinter, name, &using_default_devmode );
697 if (!pi->Devmode) goto fail;
699 ppd_filename = get_ppd_filename( hPrinter );
700 if (!ppd_filename) goto fail;
702 pi->ppd = PSDRV_ParsePPD( ppd_filename, hPrinter );
703 if (!pi->ppd)
705 WARN( "Couldn't parse PPD file %s\n", debugstr_w(ppd_filename) );
706 goto fail;
709 if(using_default_devmode) {
710 DWORD papersize;
712 if(GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_IPAPERSIZE | LOCALE_RETURN_NUMBER,
713 (LPWSTR)&papersize, sizeof(papersize)/sizeof(WCHAR))) {
714 PSDRV_DEVMODE dm;
715 memset(&dm, 0, sizeof(dm));
716 dm.dmPublic.dmFields = DM_PAPERSIZE;
717 dm.dmPublic.u1.s1.dmPaperSize = papersize;
718 PSDRV_MergeDevmodes(pi->Devmode, &dm, pi);
722 if(pi->ppd->DefaultPageSize) { /* We'll let the ppd override the devmode */
723 PSDRV_DEVMODE dm;
724 memset(&dm, 0, sizeof(dm));
725 dm.dmPublic.dmFields = DM_PAPERSIZE;
726 dm.dmPublic.u1.s1.dmPaperSize = pi->ppd->DefaultPageSize->WinPage;
727 PSDRV_MergeDevmodes(pi->Devmode, &dm, pi);
730 /* Duplex is indicated by the setting of the DM_DUPLEX bit in dmFields.
731 WinDuplex == 0 is a special case which means that the ppd has a
732 *DefaultDuplex: NotCapable entry. In this case we'll try not to confuse
733 apps and set dmDuplex to DMDUP_SIMPLEX but leave the DM_DUPLEX clear.
734 PSDRV_WriteHeader understands this and copes. */
735 pi->Devmode->dmPublic.dmFields &= ~DM_DUPLEX;
736 if(pi->ppd->DefaultDuplex) {
737 pi->Devmode->dmPublic.dmDuplex = pi->ppd->DefaultDuplex->WinDuplex;
738 if(pi->Devmode->dmPublic.dmDuplex != 0)
739 pi->Devmode->dmPublic.dmFields |= DM_DUPLEX;
740 else
741 pi->Devmode->dmPublic.dmDuplex = DMDUP_SIMPLEX;
744 set_devmode( hPrinter, pi->Devmode );
746 pi->FontSubTable = load_font_sub_table( hPrinter, &pi->FontSubTableSize );
748 LIST_FOR_EACH_ENTRY( font, &pi->ppd->InstalledFonts, FONTNAME, entry )
750 afm = PSDRV_FindAFMinList(PSDRV_AFMFontList, font->Name);
751 if(!afm) {
752 TRACE( "Couldn't find AFM file for installed printer font '%s' - "
753 "ignoring\n", font->Name);
755 else {
756 BOOL added;
757 if (PSDRV_AddAFMtoList(&pi->Fonts, afm, &added) == FALSE) {
758 PSDRV_FreeAFMList(pi->Fonts);
759 goto fail;
764 ClosePrinter( hPrinter );
765 HeapFree( GetProcessHeap(), 0, nameA );
766 HeapFree( GetProcessHeap(), 0, ppd_filename );
767 list_add_head( &printer_list, &pi->entry );
768 return pi;
770 fail:
771 if (hPrinter) ClosePrinter( hPrinter );
772 HeapFree(PSDRV_Heap, 0, pi->FontSubTable);
773 HeapFree(PSDRV_Heap, 0, pi->friendly_name);
774 HeapFree(PSDRV_Heap, 0, pi->Devmode);
775 HeapFree(PSDRV_Heap, 0, pi);
776 HeapFree( GetProcessHeap(), 0, nameA );
777 HeapFree( GetProcessHeap(), 0, ppd_filename );
778 return NULL;
782 static const struct gdi_dc_funcs psdrv_funcs =
784 NULL, /* pAbortDoc */
785 NULL, /* pAbortPath */
786 NULL, /* pAlphaBlend */
787 NULL, /* pAngleArc */
788 PSDRV_Arc, /* pArc */
789 NULL, /* pArcTo */
790 NULL, /* pBeginPath */
791 NULL, /* pBlendImage */
792 PSDRV_Chord, /* pChord */
793 NULL, /* pCloseFigure */
794 PSDRV_CreateCompatibleDC, /* pCreateCompatibleDC */
795 PSDRV_CreateDC, /* pCreateDC */
796 PSDRV_DeleteDC, /* pDeleteDC */
797 NULL, /* pDeleteObject */
798 PSDRV_DeviceCapabilities, /* pDeviceCapabilities */
799 PSDRV_Ellipse, /* pEllipse */
800 PSDRV_EndDoc, /* pEndDoc */
801 PSDRV_EndPage, /* pEndPage */
802 NULL, /* pEndPath */
803 PSDRV_EnumFonts, /* pEnumFonts */
804 NULL, /* pEnumICMProfiles */
805 NULL, /* pExcludeClipRect */
806 PSDRV_ExtDeviceMode, /* pExtDeviceMode */
807 PSDRV_ExtEscape, /* pExtEscape */
808 NULL, /* pExtFloodFill */
809 NULL, /* pExtSelectClipRgn */
810 PSDRV_ExtTextOut, /* pExtTextOut */
811 PSDRV_FillPath, /* pFillPath */
812 NULL, /* pFillRgn */
813 NULL, /* pFlattenPath */
814 NULL, /* pFontIsLinked */
815 NULL, /* pFrameRgn */
816 NULL, /* pGdiComment */
817 NULL, /* pGetBoundsRect */
818 NULL, /* pGetCharABCWidths */
819 NULL, /* pGetCharABCWidthsI */
820 PSDRV_GetCharWidth, /* pGetCharWidth */
821 PSDRV_GetDeviceCaps, /* pGetDeviceCaps */
822 NULL, /* pGetDeviceGammaRamp */
823 NULL, /* pGetFontData */
824 NULL, /* pGetFontRealizationInfo */
825 NULL, /* pGetFontUnicodeRanges */
826 NULL, /* pGetGlyphIndices */
827 NULL, /* pGetGlyphOutline */
828 NULL, /* pGetICMProfile */
829 NULL, /* pGetImage */
830 NULL, /* pGetKerningPairs */
831 NULL, /* pGetNearestColor */
832 NULL, /* pGetOutlineTextMetrics */
833 NULL, /* pGetPixel */
834 NULL, /* pGetSystemPaletteEntries */
835 NULL, /* pGetTextCharsetInfo */
836 PSDRV_GetTextExtentExPoint, /* pGetTextExtentExPoint */
837 NULL, /* pGetTextExtentExPointI */
838 NULL, /* pGetTextFace */
839 PSDRV_GetTextMetrics, /* pGetTextMetrics */
840 NULL, /* pGradientFill */
841 NULL, /* pIntersectClipRect */
842 NULL, /* pInvertRgn */
843 PSDRV_LineTo, /* pLineTo */
844 NULL, /* pModifyWorldTransform */
845 NULL, /* pMoveTo */
846 NULL, /* pOffsetClipRgn */
847 NULL, /* pOffsetViewportOrg */
848 NULL, /* pOffsetWindowOrg */
849 PSDRV_PaintRgn, /* pPaintRgn */
850 PSDRV_PatBlt, /* pPatBlt */
851 PSDRV_Pie, /* pPie */
852 PSDRV_PolyBezier, /* pPolyBezier */
853 PSDRV_PolyBezierTo, /* pPolyBezierTo */
854 NULL, /* pPolyDraw */
855 PSDRV_PolyPolygon, /* pPolyPolygon */
856 PSDRV_PolyPolyline, /* pPolyPolyline */
857 NULL, /* pPolygon */
858 NULL, /* pPolyline */
859 NULL, /* pPolylineTo */
860 PSDRV_PutImage, /* pPutImage */
861 NULL, /* pRealizeDefaultPalette */
862 NULL, /* pRealizePalette */
863 PSDRV_Rectangle, /* pRectangle */
864 PSDRV_ResetDC, /* pResetDC */
865 NULL, /* pRestoreDC */
866 PSDRV_RoundRect, /* pRoundRect */
867 NULL, /* pSaveDC */
868 NULL, /* pScaleViewportExt */
869 NULL, /* pScaleWindowExt */
870 NULL, /* pSelectBitmap */
871 PSDRV_SelectBrush, /* pSelectBrush */
872 NULL, /* pSelectClipPath */
873 PSDRV_SelectFont, /* pSelectFont */
874 NULL, /* pSelectPalette */
875 PSDRV_SelectPen, /* pSelectPen */
876 NULL, /* pSetArcDirection */
877 PSDRV_SetBkColor, /* pSetBkColor */
878 NULL, /* pSetBkMode */
879 NULL, /* pSetBoundsRect */
880 PSDRV_SetDCBrushColor, /* pSetDCBrushColor */
881 PSDRV_SetDCPenColor, /* pSetDCPenColor */
882 NULL, /* pSetDIBitsToDevice */
883 NULL, /* pSetDeviceClipping */
884 NULL, /* pSetDeviceGammaRamp */
885 NULL, /* pSetLayout */
886 NULL, /* pSetMapMode */
887 NULL, /* pSetMapperFlags */
888 PSDRV_SetPixel, /* pSetPixel */
889 NULL, /* pSetPolyFillMode */
890 NULL, /* pSetROP2 */
891 NULL, /* pSetRelAbs */
892 NULL, /* pSetStretchBltMode */
893 NULL, /* pSetTextAlign */
894 NULL, /* pSetTextCharacterExtra */
895 PSDRV_SetTextColor, /* pSetTextColor */
896 NULL, /* pSetTextJustification */
897 NULL, /* pSetViewportExt */
898 NULL, /* pSetViewportOrg */
899 NULL, /* pSetWindowExt */
900 NULL, /* pSetWindowOrg */
901 NULL, /* pSetWorldTransform */
902 PSDRV_StartDoc, /* pStartDoc */
903 PSDRV_StartPage, /* pStartPage */
904 NULL, /* pStretchBlt */
905 NULL, /* pStretchDIBits */
906 PSDRV_StrokeAndFillPath, /* pStrokeAndFillPath */
907 PSDRV_StrokePath, /* pStrokePath */
908 NULL, /* pUnrealizePalette */
909 NULL, /* pWidenPath */
910 NULL, /* wine_get_wgl_driver */
911 NULL, /* wine_get_vulkan_driver */
912 GDI_PRIORITY_GRAPHICS_DRV /* priority */
916 /******************************************************************************
917 * PSDRV_get_gdi_driver
919 const struct gdi_dc_funcs * CDECL PSDRV_get_gdi_driver( unsigned int version )
921 if (version != WINE_GDI_DRIVER_VERSION)
923 ERR( "version mismatch, gdi32 wants %u but wineps has %u\n", version, WINE_GDI_DRIVER_VERSION );
924 return NULL;
926 return &psdrv_funcs;