2 * Implementation of some printer driver bits
4 * Copyright 1996 John Harvey
5 * Copyright 1998 Huw Davies
6 * Copyright 1998 Andreas Mohr
7 * Copyright 1999 Klaas van Gend
18 #include "wine/wingdi16.h"
22 #include "debugtools.h"
29 DEFAULT_DEBUG_CHANNEL(print
)
31 static char PrinterModel
[] = "Printer Model";
32 static char DefaultDevMode
[] = "Default DevMode";
33 static char PrinterDriverData
[] = "PrinterDriverData";
34 static char Printers
[] = "System\\CurrentControlSet\\Control\\Print\\Printers\\";
36 /******************************************************************
37 * StartDoc16 [GDI.377]
40 INT16 WINAPI
StartDoc16( HDC16 hdc
, const DOCINFO16
*lpdoc
)
44 docA
.cbSize
= lpdoc
->cbSize
;
45 docA
.lpszDocName
= PTR_SEG_TO_LIN(lpdoc
->lpszDocName
);
46 docA
.lpszOutput
= PTR_SEG_TO_LIN(lpdoc
->lpszOutput
);
48 if(lpdoc
->cbSize
>= 14)
49 docA
.lpszDatatype
= PTR_SEG_TO_LIN(lpdoc
->lpszDatatype
);
51 docA
.lpszDatatype
= NULL
;
53 if(lpdoc
->cbSize
>= 18)
54 docA
.fwType
= lpdoc
->fwType
;
58 return StartDocA(hdc
, &docA
);
61 /******************************************************************
62 * StartDocA [GDI32.347]
64 * StartDoc calls the STARTDOC Escape with the input data pointing to DocName
65 * and the output data (which is used as a second input parameter).pointing at
66 * the whole docinfo structure. This seems to be an undocumented feature of
67 * the STARTDOC Escape.
69 INT WINAPI
StartDocA(HDC hdc
, const DOCINFOA
* doc
)
71 DC
*dc
= DC_GetDCPtr( hdc
);
73 TRACE("DocName = '%s' Output = '%s' Datatype = '%s'\n",
74 doc
->lpszDocName
, doc
->lpszOutput
, doc
->lpszDatatype
);
78 if(dc
->funcs
->pStartDoc
)
79 return dc
->funcs
->pStartDoc( dc
, doc
);
81 return Escape(hdc
, STARTDOC
, strlen(doc
->lpszDocName
),
82 doc
->lpszDocName
, (LPVOID
)doc
);
85 /*************************************************************************
86 * StartDocW [GDI32.348]
89 INT WINAPI
StartDocW(HDC hdc
, const DOCINFOW
* doc
)
94 docA
.cbSize
= doc
->cbSize
;
95 docA
.lpszDocName
= doc
->lpszDocName
?
96 HEAP_strdupWtoA( GetProcessHeap(), 0, doc
->lpszDocName
) : NULL
;
97 docA
.lpszOutput
= doc
->lpszOutput
?
98 HEAP_strdupWtoA( GetProcessHeap(), 0, doc
->lpszOutput
) : NULL
;
99 docA
.lpszDatatype
= doc
->lpszDatatype
?
100 HEAP_strdupWtoA( GetProcessHeap(), 0, doc
->lpszDatatype
) : NULL
;
101 docA
.fwType
= doc
->fwType
;
103 ret
= StartDocA(hdc
, &docA
);
106 HeapFree( GetProcessHeap(), 0, (LPSTR
)docA
.lpszDocName
);
108 HeapFree( GetProcessHeap(), 0, (LPSTR
)docA
.lpszOutput
);
109 if(docA
.lpszDatatype
)
110 HeapFree( GetProcessHeap(), 0, (LPSTR
)docA
.lpszDatatype
);
115 /******************************************************************
119 INT16 WINAPI
EndDoc16(HDC16 hdc
)
124 /******************************************************************
128 INT WINAPI
EndDoc(HDC hdc
)
130 DC
*dc
= DC_GetDCPtr( hdc
);
133 if(dc
->funcs
->pEndDoc
)
134 return dc
->funcs
->pEndDoc( dc
);
136 return Escape(hdc
, ENDDOC
, 0, 0, 0);
139 /******************************************************************
140 * StartPage16 [GDI.379]
143 INT16 WINAPI
StartPage16(HDC16 hdc
)
145 return StartPage(hdc
);
148 /******************************************************************
149 * StartPage [GDI32.349]
152 INT WINAPI
StartPage(HDC hdc
)
154 DC
*dc
= DC_GetDCPtr( hdc
);
157 if(dc
->funcs
->pStartPage
)
158 return dc
->funcs
->pStartPage( dc
);
164 /******************************************************************
165 * EndPage16 [GDI.380]
168 INT16 WINAPI
EndPage16( HDC16 hdc
)
173 /******************************************************************
177 INT WINAPI
EndPage(HDC hdc
)
179 DC
*dc
= DC_GetDCPtr( hdc
);
182 if(dc
->funcs
->pEndPage
)
183 return dc
->funcs
->pEndPage( dc
);
185 return Escape(hdc
, NEWFRAME
, 0, 0, 0);
188 /******************************************************************************
189 * AbortDoc16 [GDI.382]
191 INT16 WINAPI
AbortDoc16(HDC16 hdc
)
193 return AbortDoc(hdc
);
196 /******************************************************************************
197 * AbortDoc [GDI32.105]
199 INT WINAPI
AbortDoc(HDC hdc
)
201 DC
*dc
= DC_GetDCPtr( hdc
);
204 if(dc
->funcs
->pAbortDoc
)
205 return dc
->funcs
->pAbortDoc( dc
);
207 return Escape(hdc
, ABORTDOC
, 0, 0, 0);
210 /**********************************************************************
211 * QueryAbort16 (GDI.155)
213 * Calls the app's AbortProc function if avail.
216 * TRUE if no AbortProc avail or AbortProc wants to continue printing.
217 * FALSE if AbortProc wants to abort printing.
219 BOOL16 WINAPI
QueryAbort16(HDC16 hdc
, INT16 reserved
)
221 DC
*dc
= DC_GetDCPtr( hdc
);
224 ERR("Invalid hdc %04x\n", hdc
);
228 if(!dc
->w
.pAbortProc
)
230 return dc
->w
.pAbortProc(hdc
, 0);
233 /* ### start build ### */
234 extern WORD CALLBACK
PRTDRV_CallTo16_word_ww(FARPROC16
,WORD
,WORD
);
235 /* ### stop build ### */
237 /**********************************************************************
238 * SetAbortProc16 (GDI.381)
241 INT16 WINAPI
SetAbortProc16(HDC16 hdc
, SEGPTR abrtprc
)
243 ABORTPROC proc32
= (ABORTPROC
)THUNK_Alloc((FARPROC16
)abrtprc
,
244 (RELAY
)PRTDRV_CallTo16_word_ww
);
245 return SetAbortProc(hdc
, proc32
);
248 /**********************************************************************
249 * SetAbortProc32 (GDI32.301)
252 INT WINAPI
SetAbortProc(HDC hdc
, ABORTPROC abrtprc
)
254 DC
*dc
= DC_GetDCPtr( hdc
);
256 if(dc
->w
.pAbortProc
) THUNK_Free((FARPROC
)dc
->w
.pAbortProc
);
257 dc
->w
.pAbortProc
= abrtprc
;
262 /****************** misc. printer related functions */
265 * The following function should implement a queing system
274 static struct hpq
*hpqueue
;
276 /**********************************************************************
280 HPQ16 WINAPI
CreatePQ16(INT16 size
)
287 tmp_size
= size
<< 2;
288 if (!(hpq
= GlobalAlloc16(GMEM_SHARE
|GMEM_MOVEABLE
, tmp_size
+ 8)))
290 pPQ
= GlobalLock16(hpq
);
299 FIXME("(%d): stub\n",size
);
304 /**********************************************************************
308 INT16 WINAPI
DeletePQ16(HPQ16 hPQ
)
310 return GlobalFree16((HGLOBAL16
)hPQ
);
313 /**********************************************************************
314 * ExtractPQ (GDI.232)
317 INT16 WINAPI
ExtractPQ16(HPQ16 hPQ
)
319 struct hpq
*queue
, *prev
, *current
, *currentPrev
;
320 int key
= 0, tag
= -1;
321 currentPrev
= prev
= NULL
;
322 queue
= current
= hpqueue
;
328 currentPrev
= current
;
329 current
= current
->next
;
332 if (current
->key
< key
)
344 prev
->next
= queue
->next
;
346 hpqueue
= queue
->next
;
347 HeapFree(GetProcessHeap(), 0, queue
);
350 TRACE("%x got tag %d key %d\n", hPQ
, tag
, key
);
355 /**********************************************************************
359 INT16 WINAPI
InsertPQ16(HPQ16 hPQ
, INT16 tag
, INT16 key
)
361 struct hpq
*queueItem
= HeapAlloc(GetProcessHeap(), 0, sizeof(struct hpq
));
362 if(queueItem
== NULL
) {
363 ERR("Memory exausted!");
366 queueItem
->next
= hpqueue
;
368 queueItem
->key
= key
;
369 queueItem
->tag
= tag
;
371 FIXME("(%x %d %d): stub???\n", hPQ
, tag
, key
);
375 /**********************************************************************
379 INT16 WINAPI
MinPQ16(HPQ16 hPQ
)
381 FIXME("(%x): stub\n", hPQ
);
385 /**********************************************************************
389 INT16 WINAPI
SizePQ16(HPQ16 hPQ
, INT16 sizechange
)
391 FIXME("(%x %d): stub\n", hPQ
, sizechange
);
398 * The following functions implement part of the spooling process to
399 * print manager. I would like to see wine have a version of print managers
400 * that used LPR/LPD. For simplicity print jobs will be sent to a file for
403 typedef struct PRINTJOB
411 } PRINTJOB
, *PPRINTJOB
;
413 #define MAX_PRINT_JOBS 1
416 PPRINTJOB gPrintJobsTable
[MAX_PRINT_JOBS
];
419 static PPRINTJOB
FindPrintJobFromHandle(HANDLE16 hHandle
)
421 return gPrintJobsTable
[0];
424 /* TTD Need to do some DOS->UNIX file conversion here */
425 static int CreateSpoolFile(LPCSTR pszOutput
)
429 char *psCmdP
= psCmd
;
431 /* TTD convert the 'output device' into a spool file name */
433 if (pszOutput
== NULL
|| *pszOutput
== '\0')
436 PROFILE_GetWineIniString( "spooler", pszOutput
, "", psCmd
, sizeof(psCmd
) );
437 TRACE("Got printerSpoolCommand '%s' for output device '%s'\n",
440 psCmdP
= (char *)pszOutput
;
443 while (*psCmdP
&& isspace(*psCmdP
))
459 TRACE("In child need to exec %s\n",psCmdP
);
469 TRACE("Need to execute a cmnd and pipe the output to it\n");
473 TRACE("Just assume its a file\n");
475 if ((fd
= open(psCmdP
, O_CREAT
| O_TRUNC
| O_WRONLY
, 0600)) < 0)
477 ERR("Failed to create spool file %s, errno = %d\n",
484 static int FreePrintJob(HANDLE16 hJob
)
489 pPrintJob
= FindPrintJobFromHandle(hJob
);
490 if (pPrintJob
!= NULL
)
492 gPrintJobsTable
[pPrintJob
->nIndex
] = NULL
;
493 HeapFree(GetProcessHeap(), 0, pPrintJob
->pszOutput
);
494 HeapFree(GetProcessHeap(), 0, pPrintJob
->pszTitle
);
495 if (pPrintJob
->fd
>= 0) close(pPrintJob
->fd
);
496 HeapFree(GetProcessHeap(), 0, pPrintJob
);
502 /**********************************************************************
506 HPJOB16 WINAPI
OpenJob16(LPCSTR lpOutput
, LPCSTR lpTitle
, HDC16 hDC
)
508 HPJOB16 hHandle
= (HPJOB16
)SP_ERROR
;
511 TRACE("'%s' '%s' %04x\n", lpOutput
, lpTitle
, hDC
);
513 pPrintJob
= gPrintJobsTable
[0];
514 if (pPrintJob
== NULL
)
518 /* Try an create a spool file */
519 fd
= CreateSpoolFile(lpOutput
);
522 pPrintJob
= HeapAlloc(GetProcessHeap(), 0, sizeof(PRINTJOB
));
523 if(pPrintJob
== NULL
) {
524 WARN("Memory exausted!");
530 pPrintJob
->pszOutput
= HEAP_strdupA(GetProcessHeap(), 0, lpOutput
);
532 pPrintJob
->pszTitle
= HEAP_strdupA(GetProcessHeap(), 0, lpTitle
);
533 pPrintJob
->hDC
= hDC
;
535 pPrintJob
->nIndex
= 0;
536 pPrintJob
->hHandle
= hHandle
;
537 gPrintJobsTable
[pPrintJob
->nIndex
] = pPrintJob
;
540 TRACE("return %04x\n", hHandle
);
544 /**********************************************************************
548 INT16 WINAPI
CloseJob16(HPJOB16 hJob
)
551 PPRINTJOB pPrintJob
= NULL
;
553 TRACE("%04x\n", hJob
);
555 pPrintJob
= FindPrintJobFromHandle(hJob
);
556 if (pPrintJob
!= NULL
)
558 /* Close the spool file */
559 close(pPrintJob
->fd
);
566 /**********************************************************************
567 * WriteSpool (GDI.241)
570 INT16 WINAPI
WriteSpool16(HPJOB16 hJob
, LPSTR lpData
, INT16 cch
)
573 PPRINTJOB pPrintJob
= NULL
;
575 TRACE("%04x %08lx %04x\n", hJob
, (DWORD
)lpData
, cch
);
577 pPrintJob
= FindPrintJobFromHandle(hJob
);
578 if (pPrintJob
!= NULL
&& pPrintJob
->fd
>= 0 && cch
)
580 if (write(pPrintJob
->fd
, lpData
, cch
) != cch
)
584 if (pPrintJob
->hDC
== 0) {
585 TRACE("hDC == 0 so no QueryAbort\n");
587 else if (!(QueryAbort16(pPrintJob
->hDC
, (nRet
== SP_OUTOFDISK
) ? nRet
: 0 )))
589 CloseJob16(hJob
); /* printing aborted */
596 /**********************************************************************
597 * WriteDialog (GDI.242)
600 INT16 WINAPI
WriteDialog16(HPJOB16 hJob
, LPSTR lpMsg
, INT16 cchMsg
)
604 TRACE("%04x %04x '%s'\n", hJob
, cchMsg
, lpMsg
);
606 nRet
= MessageBox16(0, lpMsg
, "Printing Error", MB_OKCANCEL
);
611 /**********************************************************************
612 * DeleteJob (GDI.244)
615 INT16 WINAPI
DeleteJob16(HPJOB16 hJob
, INT16 nNotUsed
)
619 TRACE("%04x\n", hJob
);
621 nRet
= FreePrintJob(hJob
);
626 * The following two function would allow a page to be sent to the printer
627 * when it has been processed. For simplicity they havn't been implemented.
628 * This means a whole job has to be processed before it is sent to the printer.
631 /**********************************************************************
632 * StartSpoolPage (GDI.246)
635 INT16 WINAPI
StartSpoolPage16(HPJOB16 hJob
)
637 FIXME("StartSpoolPage GDI.246 unimplemented\n");
643 /**********************************************************************
644 * EndSpoolPage (GDI.247)
647 INT16 WINAPI
EndSpoolPage16(HPJOB16 hJob
)
649 FIXME("EndSpoolPage GDI.247 unimplemented\n");
654 /**********************************************************************
655 * GetSpoolJob (GDI.245)
658 DWORD WINAPI
GetSpoolJob16(int nOption
, LONG param
)
661 TRACE("In GetSpoolJob param 0x%lx noption %d\n",param
, nOption
);
666 /******************************************************************
667 * DrvGetPrinterDataInternal
669 * Helper for DrvGetPrinterData
671 static DWORD
DrvGetPrinterDataInternal(LPSTR RegStr_Printer
,
672 LPBYTE lpPrinterData
, int cbData
, int what
)
676 DWORD dwType
, cbQueryData
;
678 if (!(RegOpenKeyA(HKEY_LOCAL_MACHINE
, RegStr_Printer
, &hkey
))) {
679 if (what
== INT_PD_DEFAULT_DEVMODE
) { /* "Default DevMode" */
680 if (!(RegQueryValueExA(hkey
, DefaultDevMode
, 0, &dwType
, 0, &cbQueryData
))) {
683 else if ((cbQueryData
) && (cbQueryData
<= cbData
)) {
684 cbQueryData
= cbData
;
685 if (RegQueryValueExA(hkey
, DefaultDevMode
, 0,
686 &dwType
, lpPrinterData
, &cbQueryData
))
690 } else { /* "Printer Driver" */
692 RegQueryValueExA(hkey
, "Printer Driver", 0,
693 &dwType
, lpPrinterData
, &cbQueryData
);
697 if (hkey
) RegCloseKey(hkey
);
701 /******************************************************************
702 * DrvGetPrinterData [GDI.282]
705 DWORD WINAPI
DrvGetPrinterData16(LPSTR lpPrinter
, LPSTR lpProfile
,
706 LPDWORD lpType
, LPBYTE lpPrinterData
,
707 int cbData
, LPDWORD lpNeeded
)
709 LPSTR RegStr_Printer
;
710 HKEY hkey
= 0, hkey2
= 0;
712 DWORD dwType
, PrinterAttr
, cbPrinterAttr
, SetData
, size
;
714 if (HIWORD(lpPrinter
))
715 TRACE("printer %s\n",lpPrinter
);
717 TRACE("printer %p\n",lpPrinter
);
718 if (HIWORD(lpProfile
))
719 TRACE("profile %s\n",lpProfile
);
721 TRACE("profile %p\n",lpProfile
);
722 TRACE("lpType %p\n",lpType
);
724 if ((!lpPrinter
) || (!lpProfile
) || (!lpNeeded
))
725 return ERROR_INVALID_PARAMETER
;
727 RegStr_Printer
= HeapAlloc(GetProcessHeap(), 0,
728 strlen(Printers
) + strlen(lpPrinter
) + 2);
729 strcpy(RegStr_Printer
, Printers
);
730 strcat(RegStr_Printer
, lpPrinter
);
732 if (((DWORD
)lpProfile
== INT_PD_DEFAULT_DEVMODE
) || (HIWORD(lpProfile
) &&
733 (!strcmp(lpProfile
, DefaultDevMode
)))) {
734 size
= DrvGetPrinterDataInternal(RegStr_Printer
, lpPrinterData
, cbData
,
735 INT_PD_DEFAULT_DEVMODE
);
738 if ((lpPrinterData
) && (*lpNeeded
> cbData
))
739 res
= ERROR_MORE_DATA
;
741 else res
= ERROR_INVALID_PRINTER_NAME
;
744 if (((DWORD
)lpProfile
== INT_PD_DEFAULT_MODEL
) || (HIWORD(lpProfile
) &&
745 (!strcmp(lpProfile
, PrinterModel
)))) {
747 if (!lpPrinterData
) goto failed
;
749 res
= ERROR_MORE_DATA
;
752 size
= DrvGetPrinterDataInternal(RegStr_Printer
, lpPrinterData
, cbData
,
753 INT_PD_DEFAULT_MODEL
);
754 if ((size
+1) && (lpType
))
757 res
= ERROR_INVALID_PRINTER_NAME
;
761 if ((res
= RegOpenKeyA(HKEY_LOCAL_MACHINE
, RegStr_Printer
, &hkey
)))
764 if ((res
= RegQueryValueExA(hkey
, "Attributes", 0,
765 &dwType
, (LPBYTE
)&PrinterAttr
, &cbPrinterAttr
)))
767 if ((res
= RegOpenKeyA(hkey
, PrinterDriverData
, &hkey2
)))
770 res
= RegQueryValueExA(hkey2
, lpProfile
, 0,
771 lpType
, lpPrinterData
, lpNeeded
);
772 if ((res
!= ERROR_CANTREAD
) &&
774 (PRINTER_ATTRIBUTE_ENABLE_BIDI
|PRINTER_ATTRIBUTE_NETWORK
))
775 == PRINTER_ATTRIBUTE_NETWORK
))
777 if (!(res
) && (*lpType
== REG_DWORD
) && (*(LPDWORD
)lpPrinterData
== -1))
778 res
= ERROR_INVALID_DATA
;
783 RegSetValueExA(hkey2
, lpProfile
, 0, REG_DWORD
, (LPBYTE
)&SetData
, 4); /* no result returned */
788 if (hkey2
) RegCloseKey(hkey2
);
789 if (hkey
) RegCloseKey(hkey
);
790 HeapFree(GetProcessHeap(), 0, RegStr_Printer
);
795 /******************************************************************
796 * DrvSetPrinterData [GDI.281]
799 DWORD WINAPI
DrvSetPrinterData16(LPSTR lpPrinter
, LPSTR lpProfile
,
800 DWORD lpType
, LPBYTE lpPrinterData
,
803 LPSTR RegStr_Printer
;
807 if (HIWORD(lpPrinter
))
808 TRACE("printer %s\n",lpPrinter
);
810 TRACE("printer %p\n",lpPrinter
);
811 if (HIWORD(lpProfile
))
812 TRACE("profile %s\n",lpProfile
);
814 TRACE("profile %p\n",lpProfile
);
815 TRACE("lpType %08lx\n",lpType
);
817 if ((!lpPrinter
) || (!lpProfile
) ||
818 ((DWORD
)lpProfile
== INT_PD_DEFAULT_MODEL
) || (HIWORD(lpProfile
) &&
819 (!strcmp(lpProfile
, PrinterModel
))))
820 return ERROR_INVALID_PARAMETER
;
822 RegStr_Printer
= HeapAlloc(GetProcessHeap(), 0,
823 strlen(Printers
) + strlen(lpPrinter
) + 2);
824 strcpy(RegStr_Printer
, Printers
);
825 strcat(RegStr_Printer
, lpPrinter
);
827 if (((DWORD
)lpProfile
== INT_PD_DEFAULT_DEVMODE
) || (HIWORD(lpProfile
) &&
828 (!strcmp(lpProfile
, DefaultDevMode
)))) {
829 if ( RegOpenKeyA(HKEY_LOCAL_MACHINE
, RegStr_Printer
, &hkey
)
831 RegSetValueExA(hkey
, DefaultDevMode
, 0, REG_BINARY
,
832 lpPrinterData
, dwSize
) != ERROR_SUCCESS
)
833 res
= ERROR_INVALID_PRINTER_NAME
;
837 strcat(RegStr_Printer
, "\\");
839 if( (res
= RegOpenKeyA(HKEY_LOCAL_MACHINE
, RegStr_Printer
, &hkey
)) ==
843 res
= RegDeleteValueA(hkey
, lpProfile
);
845 res
= RegSetValueExA(hkey
, lpProfile
, 0, lpType
,
846 lpPrinterData
, dwSize
);
850 if (hkey
) RegCloseKey(hkey
);
851 HeapFree(GetProcessHeap(), 0, RegStr_Printer
);