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
20 #include "wine/winbase16.h"
21 #include "wine/wingdi16.h"
25 #include "debugtools.h"
32 DEFAULT_DEBUG_CHANNEL(print);
34 static char PrinterModel[] = "Printer Model";
35 static char DefaultDevMode[] = "Default DevMode";
36 static char PrinterDriverData[] = "PrinterDriverData";
37 static char Printers[] = "System\\CurrentControlSet\\Control\\Print\\Printers\\";
39 /******************************************************************
43 INT16 WINAPI StartDoc16( HDC16 hdc, const DOCINFO16 *lpdoc )
47 docA.cbSize = lpdoc->cbSize;
48 docA.lpszDocName = MapSL(lpdoc->lpszDocName);
49 docA.lpszOutput = MapSL(lpdoc->lpszOutput);
51 if(lpdoc->cbSize >= 14)
52 docA.lpszDatatype = MapSL(lpdoc->lpszDatatype);
54 docA.lpszDatatype = NULL;
56 if(lpdoc->cbSize >= 18)
57 docA.fwType = lpdoc->fwType;
61 return StartDocA(hdc, &docA);
64 /******************************************************************
67 * StartDoc calls the STARTDOC Escape with the input data pointing to DocName
68 * and the output data (which is used as a second input parameter).pointing at
69 * the whole docinfo structure. This seems to be an undocumented feature of
70 * the STARTDOC Escape.
72 INT WINAPI StartDocA(HDC hdc, const DOCINFOA* doc)
75 DC *dc = DC_GetDCPtr( hdc );
77 TRACE("DocName = '%s' Output = '%s' Datatype = '%s'\n",
78 doc->lpszDocName, doc->lpszOutput, doc->lpszDatatype);
80 if(!dc) return SP_ERROR;
82 if(dc->funcs->pStartDoc)
83 ret = dc->funcs->pStartDoc( dc, doc );
85 ret = Escape(hdc, STARTDOC, strlen(doc->lpszDocName),
86 doc->lpszDocName, (LPVOID)doc);
87 GDI_ReleaseObj( hdc );
91 /*************************************************************************
95 INT WINAPI StartDocW(HDC hdc, const DOCINFOW* doc)
100 docA.cbSize = doc->cbSize;
101 docA.lpszDocName = doc->lpszDocName ?
102 HEAP_strdupWtoA( GetProcessHeap(), 0, doc->lpszDocName ) : NULL;
103 docA.lpszOutput = doc->lpszOutput ?
104 HEAP_strdupWtoA( GetProcessHeap(), 0, doc->lpszOutput ) : NULL;
105 docA.lpszDatatype = doc->lpszDatatype ?
106 HEAP_strdupWtoA( GetProcessHeap(), 0, doc->lpszDatatype ) : NULL;
107 docA.fwType = doc->fwType;
109 ret = StartDocA(hdc, &docA);
112 HeapFree( GetProcessHeap(), 0, (LPSTR)docA.lpszDocName );
114 HeapFree( GetProcessHeap(), 0, (LPSTR)docA.lpszOutput );
115 if(docA.lpszDatatype)
116 HeapFree( GetProcessHeap(), 0, (LPSTR)docA.lpszDatatype );
121 /******************************************************************
125 INT16 WINAPI EndDoc16(HDC16 hdc)
130 /******************************************************************
134 INT WINAPI EndDoc(HDC hdc)
137 DC *dc = DC_GetDCPtr( hdc );
138 if(!dc) return SP_ERROR;
140 if(dc->funcs->pEndDoc)
141 ret = dc->funcs->pEndDoc( dc );
143 ret = Escape(hdc, ENDDOC, 0, 0, 0);
144 GDI_ReleaseObj( hdc );
148 /******************************************************************
149 * StartPage [GDI.379]
152 INT16 WINAPI StartPage16(HDC16 hdc)
154 return StartPage(hdc);
157 /******************************************************************
158 * StartPage [GDI32.@]
161 INT WINAPI StartPage(HDC hdc)
164 DC *dc = DC_GetDCPtr( hdc );
165 if(!dc) return SP_ERROR;
167 if(dc->funcs->pStartPage)
168 ret = dc->funcs->pStartPage( dc );
171 GDI_ReleaseObj( hdc );
175 /******************************************************************
179 INT16 WINAPI EndPage16( HDC16 hdc )
184 /******************************************************************
188 INT WINAPI EndPage(HDC hdc)
191 DC *dc = DC_GetDCPtr( hdc );
192 if(!dc) return SP_ERROR;
194 if(dc->funcs->pEndPage)
195 ret = dc->funcs->pEndPage( dc );
197 ret = Escape(hdc, NEWFRAME, 0, 0, 0);
198 GDI_ReleaseObj( hdc );
199 if (!QueryAbort16( hdc, 0 ))
207 /******************************************************************************
210 INT16 WINAPI AbortDoc16(HDC16 hdc)
212 return AbortDoc(hdc);
215 /******************************************************************************
218 INT WINAPI AbortDoc(HDC hdc)
221 DC *dc = DC_GetDCPtr( hdc );
222 if(!dc) return SP_ERROR;
224 if(dc->funcs->pAbortDoc)
225 ret = dc->funcs->pAbortDoc( dc );
227 ret = Escape(hdc, ABORTDOC, 0, 0, 0);
228 GDI_ReleaseObj( hdc );
232 /**********************************************************************
233 * QueryAbort (GDI.155)
235 * Calls the app's AbortProc function if avail.
238 * TRUE if no AbortProc avail or AbortProc wants to continue printing.
239 * FALSE if AbortProc wants to abort printing.
241 BOOL16 WINAPI QueryAbort16(HDC16 hdc, INT16 reserved)
244 DC *dc = DC_GetDCPtr( hdc );
247 ERR("Invalid hdc %04x\n", hdc);
251 if (dc->pAbortProc) ret = dc->pAbortProc(hdc, 0);
252 GDI_ReleaseObj( hdc );
256 /* ### start build ### */
257 extern WORD CALLBACK PRTDRV_CallTo16_word_ww(FARPROC16,WORD,WORD);
258 /* ### stop build ### */
260 /**********************************************************************
261 * SetAbortProc (GDI.381)
264 INT16 WINAPI SetAbortProc16(HDC16 hdc, SEGPTR abrtprc)
266 ABORTPROC proc32 = (ABORTPROC)THUNK_Alloc((FARPROC16)abrtprc,
267 (RELAY)PRTDRV_CallTo16_word_ww);
268 return SetAbortProc(hdc, proc32);
271 /**********************************************************************
272 * SetAbortProc (GDI32.@)
275 INT WINAPI SetAbortProc(HDC hdc, ABORTPROC abrtprc)
277 DC *dc = DC_GetDCPtr( hdc );
279 if(dc->pAbortProc) THUNK_Free((FARPROC)dc->pAbortProc);
280 dc->pAbortProc = abrtprc;
281 GDI_ReleaseObj( hdc );
286 /****************** misc. printer related functions */
289 * The following function should implement a queing system
298 static struct hpq *hpqueue;
300 /**********************************************************************
304 HPQ16 WINAPI CreatePQ16(INT16 size)
311 tmp_size = size << 2;
312 if (!(hpq = GlobalAlloc16(GMEM_SHARE|GMEM_MOVEABLE, tmp_size + 8)))
314 pPQ = GlobalLock16(hpq);
323 FIXME("(%d): stub\n",size);
328 /**********************************************************************
332 INT16 WINAPI DeletePQ16(HPQ16 hPQ)
334 return GlobalFree16((HGLOBAL16)hPQ);
337 /**********************************************************************
338 * ExtractPQ (GDI.232)
341 INT16 WINAPI ExtractPQ16(HPQ16 hPQ)
343 struct hpq *queue, *prev, *current, *currentPrev;
344 int key = 0, tag = -1;
345 currentPrev = prev = NULL;
346 queue = current = hpqueue;
352 currentPrev = current;
353 current = current->next;
356 if (current->key < key)
368 prev->next = queue->next;
370 hpqueue = queue->next;
371 HeapFree(GetProcessHeap(), 0, queue);
374 TRACE("%x got tag %d key %d\n", hPQ, tag, key);
379 /**********************************************************************
383 INT16 WINAPI InsertPQ16(HPQ16 hPQ, INT16 tag, INT16 key)
385 struct hpq *queueItem = HeapAlloc(GetProcessHeap(), 0, sizeof(struct hpq));
386 if(queueItem == NULL) {
387 ERR("Memory exausted!\n");
390 queueItem->next = hpqueue;
392 queueItem->key = key;
393 queueItem->tag = tag;
395 FIXME("(%x %d %d): stub???\n", hPQ, tag, key);
399 /**********************************************************************
403 INT16 WINAPI MinPQ16(HPQ16 hPQ)
405 FIXME("(%x): stub\n", hPQ);
409 /**********************************************************************
413 INT16 WINAPI SizePQ16(HPQ16 hPQ, INT16 sizechange)
415 FIXME("(%x %d): stub\n", hPQ, sizechange);
422 * The following functions implement part of the spooling process to
423 * print manager. I would like to see wine have a version of print managers
424 * that used LPR/LPD. For simplicity print jobs will be sent to a file for
427 typedef struct PRINTJOB
435 } PRINTJOB, *PPRINTJOB;
437 #define MAX_PRINT_JOBS 1
440 PPRINTJOB gPrintJobsTable[MAX_PRINT_JOBS];
443 static PPRINTJOB FindPrintJobFromHandle(HANDLE16 hHandle)
445 return gPrintJobsTable[0];
448 static int CreateSpoolFile(LPCSTR pszOutput)
452 char *psCmdP = psCmd;
454 /* TTD convert the 'output device' into a spool file name */
456 if (pszOutput == NULL || *pszOutput == '\0')
459 if (!strncmp("LPR:",pszOutput,4))
460 sprintf(psCmd,"|lpr -P%s",pszOutput+4);
466 if(!RegOpenKeyA(HKEY_LOCAL_MACHINE, "Software\\Wine\\Wine\\Config\\spooler", &hkey))
468 DWORD type, count = sizeof(psCmd);
469 RegQueryValueExA(hkey, pszOutput, 0, &type, psCmd, &count);
473 TRACE("Got printerSpoolCommand '%s' for output device '%s'\n",
476 psCmdP = (char *)pszOutput;
479 while (*psCmdP && isspace(*psCmdP))
495 TRACE("In child need to exec %s\n",psCmdP);
505 TRACE("Need to execute a cmnd and pipe the output to it\n");
509 DOS_FULL_NAME fullName;
511 TRACE("Just assume it's a file\n");
514 * The file name can be dos based, we have to find its
515 * Unix correspondant file name
517 DOSFS_GetFullName(psCmdP, FALSE, &fullName);
519 if ((fd = open(fullName.long_name, O_CREAT | O_TRUNC | O_WRONLY , 0600)) < 0)
521 ERR("Failed to create spool file %s (%s)\n",
522 fullName.long_name, strerror(errno));
528 static int FreePrintJob(HANDLE16 hJob)
533 pPrintJob = FindPrintJobFromHandle(hJob);
534 if (pPrintJob != NULL)
536 gPrintJobsTable[pPrintJob->nIndex] = NULL;
537 HeapFree(GetProcessHeap(), 0, pPrintJob->pszOutput);
538 HeapFree(GetProcessHeap(), 0, pPrintJob->pszTitle);
539 if (pPrintJob->fd >= 0) close(pPrintJob->fd);
540 HeapFree(GetProcessHeap(), 0, pPrintJob);
546 /**********************************************************************
550 HPJOB16 WINAPI OpenJob16(LPCSTR lpOutput, LPCSTR lpTitle, HDC16 hDC)
552 HPJOB16 hHandle = (HPJOB16)SP_ERROR;
555 TRACE("'%s' '%s' %04x\n", lpOutput, lpTitle, hDC);
557 pPrintJob = gPrintJobsTable[0];
558 if (pPrintJob == NULL)
562 /* Try an create a spool file */
563 fd = CreateSpoolFile(lpOutput);
566 pPrintJob = HeapAlloc(GetProcessHeap(), 0, sizeof(PRINTJOB));
567 if(pPrintJob == NULL) {
568 WARN("Memory exausted!\n");
574 pPrintJob->pszOutput = HEAP_strdupA(GetProcessHeap(), 0, lpOutput);
576 pPrintJob->pszTitle = HEAP_strdupA(GetProcessHeap(), 0, lpTitle);
577 pPrintJob->hDC = hDC;
579 pPrintJob->nIndex = 0;
580 pPrintJob->hHandle = hHandle;
581 gPrintJobsTable[pPrintJob->nIndex] = pPrintJob;
584 TRACE("return %04x\n", hHandle);
588 /**********************************************************************
592 INT16 WINAPI CloseJob16(HPJOB16 hJob)
595 PPRINTJOB pPrintJob = NULL;
597 TRACE("%04x\n", hJob);
599 pPrintJob = FindPrintJobFromHandle(hJob);
600 if (pPrintJob != NULL)
602 /* Close the spool file */
603 close(pPrintJob->fd);
610 /**********************************************************************
611 * WriteSpool (GDI.241)
614 INT16 WINAPI WriteSpool16(HPJOB16 hJob, LPSTR lpData, INT16 cch)
617 PPRINTJOB pPrintJob = NULL;
619 TRACE("%04x %08lx %04x\n", hJob, (DWORD)lpData, cch);
621 pPrintJob = FindPrintJobFromHandle(hJob);
622 if (pPrintJob != NULL && pPrintJob->fd >= 0 && cch)
624 if (write(pPrintJob->fd, lpData, cch) != cch)
629 /* FIXME: We just cannot call 16 bit functions from here, since we
630 * have acquired several locks (DC). And we do not really need to.
632 if (pPrintJob->hDC == 0) {
633 TRACE("hDC == 0 so no QueryAbort\n");
635 else if (!(QueryAbort16(pPrintJob->hDC, (nRet == SP_OUTOFDISK) ? nRet : 0 )))
637 CloseJob16(hJob); /* printing aborted */
645 typedef INT WINAPI (*MSGBOX_PROC)( HWND, LPCSTR, LPCSTR, UINT );
647 /**********************************************************************
648 * WriteDialog (GDI.242)
651 INT16 WINAPI WriteDialog16(HPJOB16 hJob, LPSTR lpMsg, INT16 cchMsg)
654 MSGBOX_PROC pMessageBoxA;
657 TRACE("%04x %04x '%s'\n", hJob, cchMsg, lpMsg);
659 if ((mod = GetModuleHandleA("user32.dll")))
661 if ((pMessageBoxA = (MSGBOX_PROC)GetProcAddress( mod, "MessageBoxA" )))
662 ret = pMessageBoxA(0, lpMsg, "Printing Error", MB_OKCANCEL);
668 /**********************************************************************
669 * DeleteJob (GDI.244)
672 INT16 WINAPI DeleteJob16(HPJOB16 hJob, INT16 nNotUsed)
676 TRACE("%04x\n", hJob);
678 nRet = FreePrintJob(hJob);
683 * The following two function would allow a page to be sent to the printer
684 * when it has been processed. For simplicity they havn't been implemented.
685 * This means a whole job has to be processed before it is sent to the printer.
688 /**********************************************************************
689 * StartSpoolPage (GDI.246)
692 INT16 WINAPI StartSpoolPage16(HPJOB16 hJob)
694 FIXME("StartSpoolPage GDI.246 unimplemented\n");
700 /**********************************************************************
701 * EndSpoolPage (GDI.247)
704 INT16 WINAPI EndSpoolPage16(HPJOB16 hJob)
706 FIXME("EndSpoolPage GDI.247 unimplemented\n");
711 /**********************************************************************
712 * GetSpoolJob (GDI.245)
715 DWORD WINAPI GetSpoolJob16(int nOption, LONG param)
718 TRACE("In GetSpoolJob param 0x%lx noption %d\n",param, nOption);
723 /******************************************************************
724 * DrvGetPrinterDataInternal
726 * Helper for DrvGetPrinterData
728 static DWORD DrvGetPrinterDataInternal(LPSTR RegStr_Printer,
729 LPBYTE lpPrinterData, int cbData, int what)
733 DWORD dwType, cbQueryData;
735 if (!(RegOpenKeyA(HKEY_LOCAL_MACHINE, RegStr_Printer, &hkey))) {
736 if (what == INT_PD_DEFAULT_DEVMODE) { /* "Default DevMode" */
737 if (!(RegQueryValueExA(hkey, DefaultDevMode, 0, &dwType, 0, &cbQueryData))) {
740 else if ((cbQueryData) && (cbQueryData <= cbData)) {
741 cbQueryData = cbData;
742 if (RegQueryValueExA(hkey, DefaultDevMode, 0,
743 &dwType, lpPrinterData, &cbQueryData))
747 } else { /* "Printer Driver" */
749 RegQueryValueExA(hkey, "Printer Driver", 0,
750 &dwType, lpPrinterData, &cbQueryData);
754 if (hkey) RegCloseKey(hkey);
758 /******************************************************************
759 * DrvGetPrinterData [GDI.282]
762 DWORD WINAPI DrvGetPrinterData16(LPSTR lpPrinter, LPSTR lpProfile,
763 LPDWORD lpType, LPBYTE lpPrinterData,
764 int cbData, LPDWORD lpNeeded)
766 LPSTR RegStr_Printer;
767 HKEY hkey = 0, hkey2 = 0;
769 DWORD dwType, PrinterAttr, cbPrinterAttr, SetData, size;
771 if (HIWORD(lpPrinter))
772 TRACE("printer %s\n",lpPrinter);
774 TRACE("printer %p\n",lpPrinter);
775 if (HIWORD(lpProfile))
776 TRACE("profile %s\n",lpProfile);
778 TRACE("profile %p\n",lpProfile);
779 TRACE("lpType %p\n",lpType);
781 if ((!lpPrinter) || (!lpProfile) || (!lpNeeded))
782 return ERROR_INVALID_PARAMETER;
784 RegStr_Printer = HeapAlloc(GetProcessHeap(), 0,
785 strlen(Printers) + strlen(lpPrinter) + 2);
786 strcpy(RegStr_Printer, Printers);
787 strcat(RegStr_Printer, lpPrinter);
789 if (((DWORD)lpProfile == INT_PD_DEFAULT_DEVMODE) || (HIWORD(lpProfile) &&
790 (!strcmp(lpProfile, DefaultDevMode)))) {
791 size = DrvGetPrinterDataInternal(RegStr_Printer, lpPrinterData, cbData,
792 INT_PD_DEFAULT_DEVMODE);
795 if ((lpPrinterData) && (*lpNeeded > cbData))
796 res = ERROR_MORE_DATA;
798 else res = ERROR_INVALID_PRINTER_NAME;
801 if (((DWORD)lpProfile == INT_PD_DEFAULT_MODEL) || (HIWORD(lpProfile) &&
802 (!strcmp(lpProfile, PrinterModel)))) {
804 if (!lpPrinterData) goto failed;
806 res = ERROR_MORE_DATA;
809 size = DrvGetPrinterDataInternal(RegStr_Printer, lpPrinterData, cbData,
810 INT_PD_DEFAULT_MODEL);
811 if ((size+1) && (lpType))
814 res = ERROR_INVALID_PRINTER_NAME;
818 if ((res = RegOpenKeyA(HKEY_LOCAL_MACHINE, RegStr_Printer, &hkey)))
821 if ((res = RegQueryValueExA(hkey, "Attributes", 0,
822 &dwType, (LPBYTE)&PrinterAttr, &cbPrinterAttr)))
824 if ((res = RegOpenKeyA(hkey, PrinterDriverData, &hkey2)))
827 res = RegQueryValueExA(hkey2, lpProfile, 0,
828 lpType, lpPrinterData, lpNeeded);
829 if ((res != ERROR_CANTREAD) &&
831 (PRINTER_ATTRIBUTE_ENABLE_BIDI|PRINTER_ATTRIBUTE_NETWORK))
832 == PRINTER_ATTRIBUTE_NETWORK))
834 if (!(res) && (*lpType == REG_DWORD) && (*(LPDWORD)lpPrinterData == -1))
835 res = ERROR_INVALID_DATA;
840 RegSetValueExA(hkey2, lpProfile, 0, REG_DWORD, (LPBYTE)&SetData, 4); /* no result returned */
845 if (hkey2) RegCloseKey(hkey2);
846 if (hkey) RegCloseKey(hkey);
847 HeapFree(GetProcessHeap(), 0, RegStr_Printer);
852 /******************************************************************
853 * DrvSetPrinterData [GDI.281]
856 DWORD WINAPI DrvSetPrinterData16(LPSTR lpPrinter, LPSTR lpProfile,
857 DWORD lpType, LPBYTE lpPrinterData,
860 LPSTR RegStr_Printer;
864 if (HIWORD(lpPrinter))
865 TRACE("printer %s\n",lpPrinter);
867 TRACE("printer %p\n",lpPrinter);
868 if (HIWORD(lpProfile))
869 TRACE("profile %s\n",lpProfile);
871 TRACE("profile %p\n",lpProfile);
872 TRACE("lpType %08lx\n",lpType);
874 if ((!lpPrinter) || (!lpProfile) ||
875 ((DWORD)lpProfile == INT_PD_DEFAULT_MODEL) || (HIWORD(lpProfile) &&
876 (!strcmp(lpProfile, PrinterModel))))
877 return ERROR_INVALID_PARAMETER;
879 RegStr_Printer = HeapAlloc(GetProcessHeap(), 0,
880 strlen(Printers) + strlen(lpPrinter) + 2);
881 strcpy(RegStr_Printer, Printers);
882 strcat(RegStr_Printer, lpPrinter);
884 if (((DWORD)lpProfile == INT_PD_DEFAULT_DEVMODE) || (HIWORD(lpProfile) &&
885 (!strcmp(lpProfile, DefaultDevMode)))) {
886 if ( RegOpenKeyA(HKEY_LOCAL_MACHINE, RegStr_Printer, &hkey)
888 RegSetValueExA(hkey, DefaultDevMode, 0, REG_BINARY,
889 lpPrinterData, dwSize) != ERROR_SUCCESS )
890 res = ERROR_INVALID_PRINTER_NAME;
894 strcat(RegStr_Printer, "\\");
896 if( (res = RegOpenKeyA(HKEY_LOCAL_MACHINE, RegStr_Printer, &hkey)) ==
900 res = RegDeleteValueA(hkey, lpProfile);
902 res = RegSetValueExA(hkey, lpProfile, 0, lpType,
903 lpPrinterData, dwSize);
907 if (hkey) RegCloseKey(hkey);
908 HeapFree(GetProcessHeap(), 0, RegStr_Printer);