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
21 #include "wine/winbase16.h"
22 #include "wine/wingdi16.h"
26 #include "debugtools.h"
31 DEFAULT_DEBUG_CHANNEL(print);
33 static char PrinterModel[] = "Printer Model";
34 static char DefaultDevMode[] = "Default DevMode";
35 static char PrinterDriverData[] = "PrinterDriverData";
36 static char Printers[] = "System\\CurrentControlSet\\Control\\Print\\Printers\\";
38 /******************************************************************
42 INT16 WINAPI StartDoc16( HDC16 hdc, const DOCINFO16 *lpdoc )
46 docA.cbSize = lpdoc->cbSize;
47 docA.lpszDocName = MapSL(lpdoc->lpszDocName);
48 docA.lpszOutput = MapSL(lpdoc->lpszOutput);
50 if(lpdoc->cbSize >= 14)
51 docA.lpszDatatype = MapSL(lpdoc->lpszDatatype);
53 docA.lpszDatatype = NULL;
55 if(lpdoc->cbSize >= 18)
56 docA.fwType = lpdoc->fwType;
60 return StartDocA(hdc, &docA);
63 /******************************************************************
66 * StartDoc calls the STARTDOC Escape with the input data pointing to DocName
67 * and the output data (which is used as a second input parameter).pointing at
68 * the whole docinfo structure. This seems to be an undocumented feature of
69 * the STARTDOC Escape.
71 * Note: we now do it the other way, with the STARTDOC Escape calling StartDoc.
73 INT WINAPI StartDocA(HDC hdc, const DOCINFOA* doc)
76 DC *dc = DC_GetDCPtr( hdc );
78 TRACE("DocName = '%s' Output = '%s' Datatype = '%s'\n",
79 doc->lpszDocName, doc->lpszOutput, doc->lpszDatatype);
81 if(!dc) return SP_ERROR;
83 if (dc->funcs->pStartDoc) ret = dc->funcs->pStartDoc( dc, doc );
84 GDI_ReleaseObj( hdc );
88 /*************************************************************************
92 INT WINAPI StartDocW(HDC hdc, const DOCINFOW* doc)
97 docA.cbSize = doc->cbSize;
98 docA.lpszDocName = doc->lpszDocName ?
99 HEAP_strdupWtoA( GetProcessHeap(), 0, doc->lpszDocName ) : NULL;
100 docA.lpszOutput = doc->lpszOutput ?
101 HEAP_strdupWtoA( GetProcessHeap(), 0, doc->lpszOutput ) : NULL;
102 docA.lpszDatatype = doc->lpszDatatype ?
103 HEAP_strdupWtoA( GetProcessHeap(), 0, doc->lpszDatatype ) : NULL;
104 docA.fwType = doc->fwType;
106 ret = StartDocA(hdc, &docA);
109 HeapFree( GetProcessHeap(), 0, (LPSTR)docA.lpszDocName );
111 HeapFree( GetProcessHeap(), 0, (LPSTR)docA.lpszOutput );
112 if(docA.lpszDatatype)
113 HeapFree( GetProcessHeap(), 0, (LPSTR)docA.lpszDatatype );
118 /******************************************************************
122 INT16 WINAPI EndDoc16(HDC16 hdc)
127 /******************************************************************
131 INT WINAPI EndDoc(HDC hdc)
134 DC *dc = DC_GetDCPtr( hdc );
135 if(!dc) return SP_ERROR;
137 if (dc->funcs->pEndDoc) ret = dc->funcs->pEndDoc( dc );
138 GDI_ReleaseObj( hdc );
142 /******************************************************************
143 * StartPage [GDI.379]
146 INT16 WINAPI StartPage16(HDC16 hdc)
148 return StartPage(hdc);
151 /******************************************************************
152 * StartPage [GDI32.@]
155 INT WINAPI StartPage(HDC hdc)
158 DC *dc = DC_GetDCPtr( hdc );
159 if(!dc) return SP_ERROR;
161 if(dc->funcs->pStartPage)
162 ret = dc->funcs->pStartPage( dc );
165 GDI_ReleaseObj( hdc );
169 /******************************************************************
173 INT16 WINAPI EndPage16( HDC16 hdc )
178 /******************************************************************
182 INT WINAPI EndPage(HDC hdc)
185 DC *dc = DC_GetDCPtr( hdc );
186 if(!dc) return SP_ERROR;
188 if (dc->funcs->pEndPage) ret = dc->funcs->pEndPage( dc );
189 GDI_ReleaseObj( hdc );
190 if (!QueryAbort16( hdc, 0 ))
198 /******************************************************************************
201 INT16 WINAPI AbortDoc16(HDC16 hdc)
203 return AbortDoc(hdc);
206 /******************************************************************************
209 INT WINAPI AbortDoc(HDC hdc)
212 DC *dc = DC_GetDCPtr( hdc );
213 if(!dc) return SP_ERROR;
215 if (dc->funcs->pAbortDoc) ret = dc->funcs->pAbortDoc( dc );
216 GDI_ReleaseObj( hdc );
220 /**********************************************************************
221 * QueryAbort (GDI.155)
223 * Calls the app's AbortProc function if avail.
226 * TRUE if no AbortProc avail or AbortProc wants to continue printing.
227 * FALSE if AbortProc wants to abort printing.
229 BOOL16 WINAPI QueryAbort16(HDC16 hdc, INT16 reserved)
232 DC *dc = DC_GetDCPtr( hdc );
236 ERR("Invalid hdc %04x\n", hdc);
240 abproc = dc->pAbortProc;
241 GDI_ReleaseObj( hdc );
244 ret = abproc(hdc, 0);
248 /* ### start build ### */
249 extern WORD CALLBACK PRTDRV_CallTo16_word_ww(FARPROC16,WORD,WORD);
250 /* ### stop build ### */
252 /**********************************************************************
255 static BOOL CALLBACK call_abort_proc16( HDC hdc, INT code )
258 DC *dc = DC_GetDCPtr( hdc );
260 if (!dc) return FALSE;
261 proc16 = dc->pAbortProc16;
262 GDI_ReleaseObj( hdc );
263 if (proc16) return PRTDRV_CallTo16_word_ww( (FARPROC16)proc16, hdc, code );
268 /**********************************************************************
269 * SetAbortProc (GDI.381)
271 INT16 WINAPI SetAbortProc16(HDC16 hdc, ABORTPROC16 abrtprc)
273 DC *dc = DC_GetDCPtr( hdc );
275 if (!dc) return FALSE;
276 dc->pAbortProc16 = abrtprc;
277 GDI_ReleaseObj( hdc );
278 return SetAbortProc( hdc, call_abort_proc16 );
281 /**********************************************************************
282 * SetAbortProc (GDI32.@)
285 INT WINAPI SetAbortProc(HDC hdc, ABORTPROC abrtprc)
287 DC *dc = DC_GetDCPtr( hdc );
289 if (!dc) return FALSE;
290 dc->pAbortProc = abrtprc;
291 GDI_ReleaseObj( hdc );
296 /****************** misc. printer related functions */
299 * The following function should implement a queing system
308 static struct hpq *hpqueue;
310 /**********************************************************************
314 HPQ16 WINAPI CreatePQ16(INT16 size)
321 tmp_size = size << 2;
322 if (!(hpq = GlobalAlloc16(GMEM_SHARE|GMEM_MOVEABLE, tmp_size + 8)))
324 pPQ = GlobalLock16(hpq);
333 FIXME("(%d): stub\n",size);
338 /**********************************************************************
342 INT16 WINAPI DeletePQ16(HPQ16 hPQ)
344 return GlobalFree16((HGLOBAL16)hPQ);
347 /**********************************************************************
348 * ExtractPQ (GDI.232)
351 INT16 WINAPI ExtractPQ16(HPQ16 hPQ)
353 struct hpq *queue, *prev, *current, *currentPrev;
354 int key = 0, tag = -1;
355 currentPrev = prev = NULL;
356 queue = current = hpqueue;
362 currentPrev = current;
363 current = current->next;
366 if (current->key < key)
378 prev->next = queue->next;
380 hpqueue = queue->next;
381 HeapFree(GetProcessHeap(), 0, queue);
384 TRACE("%x got tag %d key %d\n", hPQ, tag, key);
389 /**********************************************************************
393 INT16 WINAPI InsertPQ16(HPQ16 hPQ, INT16 tag, INT16 key)
395 struct hpq *queueItem = HeapAlloc(GetProcessHeap(), 0, sizeof(struct hpq));
396 if(queueItem == NULL) {
397 ERR("Memory exausted!\n");
400 queueItem->next = hpqueue;
402 queueItem->key = key;
403 queueItem->tag = tag;
405 FIXME("(%x %d %d): stub???\n", hPQ, tag, key);
409 /**********************************************************************
413 INT16 WINAPI MinPQ16(HPQ16 hPQ)
415 FIXME("(%x): stub\n", hPQ);
419 /**********************************************************************
423 INT16 WINAPI SizePQ16(HPQ16 hPQ, INT16 sizechange)
425 FIXME("(%x %d): stub\n", hPQ, sizechange);
432 * The following functions implement part of the spooling process to
433 * print manager. I would like to see wine have a version of print managers
434 * that used LPR/LPD. For simplicity print jobs will be sent to a file for
437 typedef struct PRINTJOB
445 } PRINTJOB, *PPRINTJOB;
447 #define MAX_PRINT_JOBS 1
450 PPRINTJOB gPrintJobsTable[MAX_PRINT_JOBS];
453 static PPRINTJOB FindPrintJobFromHandle(HANDLE16 hHandle)
455 return gPrintJobsTable[0];
458 static int CreateSpoolFile(LPCSTR pszOutput)
462 char *psCmdP = psCmd;
464 /* TTD convert the 'output device' into a spool file name */
466 if (pszOutput == NULL || *pszOutput == '\0')
469 if (!strncmp("LPR:",pszOutput,4))
470 sprintf(psCmd,"|lpr -P%s",pszOutput+4);
476 if(!RegOpenKeyA(HKEY_LOCAL_MACHINE, "Software\\Wine\\Wine\\Config\\spooler", &hkey))
478 DWORD type, count = sizeof(psCmd);
479 RegQueryValueExA(hkey, pszOutput, 0, &type, psCmd, &count);
483 TRACE("Got printerSpoolCommand '%s' for output device '%s'\n",
486 psCmdP = (char *)pszOutput;
489 while (*psCmdP && isspace(*psCmdP))
505 TRACE("In child need to exec %s\n",psCmdP);
515 TRACE("Need to execute a cmnd and pipe the output to it\n");
519 DOS_FULL_NAME fullName;
521 TRACE("Just assume it's a file\n");
524 * The file name can be dos based, we have to find its
525 * Unix correspondant file name
527 DOSFS_GetFullName(psCmdP, FALSE, &fullName);
529 if ((fd = open(fullName.long_name, O_CREAT | O_TRUNC | O_WRONLY , 0600)) < 0)
531 ERR("Failed to create spool file %s (%s)\n",
532 fullName.long_name, strerror(errno));
538 static int FreePrintJob(HANDLE16 hJob)
543 pPrintJob = FindPrintJobFromHandle(hJob);
544 if (pPrintJob != NULL)
546 gPrintJobsTable[pPrintJob->nIndex] = NULL;
547 HeapFree(GetProcessHeap(), 0, pPrintJob->pszOutput);
548 HeapFree(GetProcessHeap(), 0, pPrintJob->pszTitle);
549 if (pPrintJob->fd >= 0) close(pPrintJob->fd);
550 HeapFree(GetProcessHeap(), 0, pPrintJob);
556 /**********************************************************************
560 HPJOB16 WINAPI OpenJob16(LPCSTR lpOutput, LPCSTR lpTitle, HDC16 hDC)
562 HPJOB16 hHandle = (HPJOB16)SP_ERROR;
565 TRACE("'%s' '%s' %04x\n", lpOutput, lpTitle, hDC);
567 pPrintJob = gPrintJobsTable[0];
568 if (pPrintJob == NULL)
572 /* Try an create a spool file */
573 fd = CreateSpoolFile(lpOutput);
576 pPrintJob = HeapAlloc(GetProcessHeap(), 0, sizeof(PRINTJOB));
577 if(pPrintJob == NULL) {
578 WARN("Memory exausted!\n");
584 pPrintJob->pszOutput = HeapAlloc(GetProcessHeap(), 0, strlen(lpOutput)+1);
585 strcpy( pPrintJob->pszOutput, lpOutput );
588 pPrintJob->pszTitle = HeapAlloc(GetProcessHeap(), 0, strlen(lpTitle)+1);
589 strcpy( pPrintJob->pszTitle, lpTitle );
591 pPrintJob->hDC = hDC;
593 pPrintJob->nIndex = 0;
594 pPrintJob->hHandle = hHandle;
595 gPrintJobsTable[pPrintJob->nIndex] = pPrintJob;
598 TRACE("return %04x\n", hHandle);
602 /**********************************************************************
606 INT16 WINAPI CloseJob16(HPJOB16 hJob)
609 PPRINTJOB pPrintJob = NULL;
611 TRACE("%04x\n", hJob);
613 pPrintJob = FindPrintJobFromHandle(hJob);
614 if (pPrintJob != NULL)
616 /* Close the spool file */
617 close(pPrintJob->fd);
624 /**********************************************************************
625 * WriteSpool (GDI.241)
628 INT16 WINAPI WriteSpool16(HPJOB16 hJob, LPSTR lpData, INT16 cch)
631 PPRINTJOB pPrintJob = NULL;
633 TRACE("%04x %08lx %04x\n", hJob, (DWORD)lpData, cch);
635 pPrintJob = FindPrintJobFromHandle(hJob);
636 if (pPrintJob != NULL && pPrintJob->fd >= 0 && cch)
638 if (write(pPrintJob->fd, lpData, cch) != cch)
643 /* FIXME: We just cannot call 16 bit functions from here, since we
644 * have acquired several locks (DC). And we do not really need to.
646 if (pPrintJob->hDC == 0) {
647 TRACE("hDC == 0 so no QueryAbort\n");
649 else if (!(QueryAbort16(pPrintJob->hDC, (nRet == SP_OUTOFDISK) ? nRet : 0 )))
651 CloseJob16(hJob); /* printing aborted */
659 typedef INT WINAPI (*MSGBOX_PROC)( HWND, LPCSTR, LPCSTR, UINT );
661 /**********************************************************************
662 * WriteDialog (GDI.242)
665 INT16 WINAPI WriteDialog16(HPJOB16 hJob, LPSTR lpMsg, INT16 cchMsg)
668 MSGBOX_PROC pMessageBoxA;
671 TRACE("%04x %04x '%s'\n", hJob, cchMsg, lpMsg);
673 if ((mod = GetModuleHandleA("user32.dll")))
675 if ((pMessageBoxA = (MSGBOX_PROC)GetProcAddress( mod, "MessageBoxA" )))
676 ret = pMessageBoxA(0, lpMsg, "Printing Error", MB_OKCANCEL);
682 /**********************************************************************
683 * DeleteJob (GDI.244)
686 INT16 WINAPI DeleteJob16(HPJOB16 hJob, INT16 nNotUsed)
690 TRACE("%04x\n", hJob);
692 nRet = FreePrintJob(hJob);
697 * The following two function would allow a page to be sent to the printer
698 * when it has been processed. For simplicity they havn't been implemented.
699 * This means a whole job has to be processed before it is sent to the printer.
702 /**********************************************************************
703 * StartSpoolPage (GDI.246)
706 INT16 WINAPI StartSpoolPage16(HPJOB16 hJob)
708 FIXME("StartSpoolPage GDI.246 unimplemented\n");
714 /**********************************************************************
715 * EndSpoolPage (GDI.247)
718 INT16 WINAPI EndSpoolPage16(HPJOB16 hJob)
720 FIXME("EndSpoolPage GDI.247 unimplemented\n");
725 /**********************************************************************
726 * GetSpoolJob (GDI.245)
729 DWORD WINAPI GetSpoolJob16(int nOption, LONG param)
732 TRACE("In GetSpoolJob param 0x%lx noption %d\n",param, nOption);
737 /******************************************************************
738 * DrvGetPrinterDataInternal
740 * Helper for DrvGetPrinterData
742 static DWORD DrvGetPrinterDataInternal(LPSTR RegStr_Printer,
743 LPBYTE lpPrinterData, int cbData, int what)
747 DWORD dwType, cbQueryData;
749 if (!(RegOpenKeyA(HKEY_LOCAL_MACHINE, RegStr_Printer, &hkey))) {
750 if (what == INT_PD_DEFAULT_DEVMODE) { /* "Default DevMode" */
751 if (!(RegQueryValueExA(hkey, DefaultDevMode, 0, &dwType, 0, &cbQueryData))) {
754 else if ((cbQueryData) && (cbQueryData <= cbData)) {
755 cbQueryData = cbData;
756 if (RegQueryValueExA(hkey, DefaultDevMode, 0,
757 &dwType, lpPrinterData, &cbQueryData))
761 } else { /* "Printer Driver" */
763 RegQueryValueExA(hkey, "Printer Driver", 0,
764 &dwType, lpPrinterData, &cbQueryData);
768 if (hkey) RegCloseKey(hkey);
772 /******************************************************************
773 * DrvGetPrinterData [GDI.282]
776 DWORD WINAPI DrvGetPrinterData16(LPSTR lpPrinter, LPSTR lpProfile,
777 LPDWORD lpType, LPBYTE lpPrinterData,
778 int cbData, LPDWORD lpNeeded)
780 LPSTR RegStr_Printer;
781 HKEY hkey = 0, hkey2 = 0;
783 DWORD dwType, PrinterAttr, cbPrinterAttr, SetData, size;
785 if (HIWORD(lpPrinter))
786 TRACE("printer %s\n",lpPrinter);
788 TRACE("printer %p\n",lpPrinter);
789 if (HIWORD(lpProfile))
790 TRACE("profile %s\n",lpProfile);
792 TRACE("profile %p\n",lpProfile);
793 TRACE("lpType %p\n",lpType);
795 if ((!lpPrinter) || (!lpProfile) || (!lpNeeded))
796 return ERROR_INVALID_PARAMETER;
798 RegStr_Printer = HeapAlloc(GetProcessHeap(), 0,
799 strlen(Printers) + strlen(lpPrinter) + 2);
800 strcpy(RegStr_Printer, Printers);
801 strcat(RegStr_Printer, lpPrinter);
803 if (((DWORD)lpProfile == INT_PD_DEFAULT_DEVMODE) || (HIWORD(lpProfile) &&
804 (!strcmp(lpProfile, DefaultDevMode)))) {
805 size = DrvGetPrinterDataInternal(RegStr_Printer, lpPrinterData, cbData,
806 INT_PD_DEFAULT_DEVMODE);
809 if ((lpPrinterData) && (*lpNeeded > cbData))
810 res = ERROR_MORE_DATA;
812 else res = ERROR_INVALID_PRINTER_NAME;
815 if (((DWORD)lpProfile == INT_PD_DEFAULT_MODEL) || (HIWORD(lpProfile) &&
816 (!strcmp(lpProfile, PrinterModel)))) {
818 if (!lpPrinterData) goto failed;
820 res = ERROR_MORE_DATA;
823 size = DrvGetPrinterDataInternal(RegStr_Printer, lpPrinterData, cbData,
824 INT_PD_DEFAULT_MODEL);
825 if ((size+1) && (lpType))
828 res = ERROR_INVALID_PRINTER_NAME;
832 if ((res = RegOpenKeyA(HKEY_LOCAL_MACHINE, RegStr_Printer, &hkey)))
835 if ((res = RegQueryValueExA(hkey, "Attributes", 0,
836 &dwType, (LPBYTE)&PrinterAttr, &cbPrinterAttr)))
838 if ((res = RegOpenKeyA(hkey, PrinterDriverData, &hkey2)))
841 res = RegQueryValueExA(hkey2, lpProfile, 0,
842 lpType, lpPrinterData, lpNeeded);
843 if ((res != ERROR_CANTREAD) &&
845 (PRINTER_ATTRIBUTE_ENABLE_BIDI|PRINTER_ATTRIBUTE_NETWORK))
846 == PRINTER_ATTRIBUTE_NETWORK))
848 if (!(res) && (*lpType == REG_DWORD) && (*(LPDWORD)lpPrinterData == -1))
849 res = ERROR_INVALID_DATA;
854 RegSetValueExA(hkey2, lpProfile, 0, REG_DWORD, (LPBYTE)&SetData, 4); /* no result returned */
859 if (hkey2) RegCloseKey(hkey2);
860 if (hkey) RegCloseKey(hkey);
861 HeapFree(GetProcessHeap(), 0, RegStr_Printer);
866 /******************************************************************
867 * DrvSetPrinterData [GDI.281]
870 DWORD WINAPI DrvSetPrinterData16(LPSTR lpPrinter, LPSTR lpProfile,
871 DWORD lpType, LPBYTE lpPrinterData,
874 LPSTR RegStr_Printer;
878 if (HIWORD(lpPrinter))
879 TRACE("printer %s\n",lpPrinter);
881 TRACE("printer %p\n",lpPrinter);
882 if (HIWORD(lpProfile))
883 TRACE("profile %s\n",lpProfile);
885 TRACE("profile %p\n",lpProfile);
886 TRACE("lpType %08lx\n",lpType);
888 if ((!lpPrinter) || (!lpProfile) ||
889 ((DWORD)lpProfile == INT_PD_DEFAULT_MODEL) || (HIWORD(lpProfile) &&
890 (!strcmp(lpProfile, PrinterModel))))
891 return ERROR_INVALID_PARAMETER;
893 RegStr_Printer = HeapAlloc(GetProcessHeap(), 0,
894 strlen(Printers) + strlen(lpPrinter) + 2);
895 strcpy(RegStr_Printer, Printers);
896 strcat(RegStr_Printer, lpPrinter);
898 if (((DWORD)lpProfile == INT_PD_DEFAULT_DEVMODE) || (HIWORD(lpProfile) &&
899 (!strcmp(lpProfile, DefaultDevMode)))) {
900 if ( RegOpenKeyA(HKEY_LOCAL_MACHINE, RegStr_Printer, &hkey)
902 RegSetValueExA(hkey, DefaultDevMode, 0, REG_BINARY,
903 lpPrinterData, dwSize) != ERROR_SUCCESS )
904 res = ERROR_INVALID_PRINTER_NAME;
908 strcat(RegStr_Printer, "\\");
910 if( (res = RegOpenKeyA(HKEY_LOCAL_MACHINE, RegStr_Printer, &hkey)) ==
914 res = RegDeleteValueA(hkey, lpProfile);
916 res = RegSetValueExA(hkey, lpProfile, 0, lpType,
917 lpPrinterData, dwSize);
921 if (hkey) RegCloseKey(hkey);
922 HeapFree(GetProcessHeap(), 0, RegStr_Printer);