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"
30 DEFAULT_DEBUG_CHANNEL(print)
32 static char PrinterModel[] = "Printer Model";
33 static char DefaultDevMode[] = "Default DevMode";
34 static char PrinterDriverData[] = "PrinterDriverData";
35 static char Printers[] = "System\\CurrentControlSet\\Control\\Print\\Printers\\";
37 /******************************************************************
38 * StartDoc16 [GDI.377]
41 INT16 WINAPI StartDoc16( HDC16 hdc, const DOCINFO16 *lpdoc )
45 docA.cbSize = lpdoc->cbSize;
46 docA.lpszDocName = PTR_SEG_TO_LIN(lpdoc->lpszDocName);
47 docA.lpszOutput = PTR_SEG_TO_LIN(lpdoc->lpszOutput);
49 if(lpdoc->cbSize >= 14)
50 docA.lpszDatatype = PTR_SEG_TO_LIN(lpdoc->lpszDatatype);
52 docA.lpszDatatype = NULL;
54 if(lpdoc->cbSize >= 18)
55 docA.fwType = lpdoc->fwType;
59 return StartDocA(hdc, &docA);
62 /******************************************************************
63 * StartDocA [GDI32.347]
65 * StartDoc calls the STARTDOC Escape with the input data pointing to DocName
66 * and the output data (which is used as a second input parameter).pointing at
67 * the whole docinfo structure. This seems to be an undocumented feature of
68 * the STARTDOC Escape.
70 INT WINAPI StartDocA(HDC hdc, const DOCINFOA* doc)
73 DC *dc = DC_GetDCPtr( hdc );
75 TRACE("DocName = '%s' Output = '%s' Datatype = '%s'\n",
76 doc->lpszDocName, doc->lpszOutput, doc->lpszDatatype);
78 if(!dc) return SP_ERROR;
80 if(dc->funcs->pStartDoc)
81 ret = dc->funcs->pStartDoc( dc, doc );
83 ret = Escape(hdc, STARTDOC, strlen(doc->lpszDocName),
84 doc->lpszDocName, (LPVOID)doc);
85 GDI_ReleaseObj( hdc );
89 /*************************************************************************
90 * StartDocW [GDI32.348]
93 INT WINAPI StartDocW(HDC hdc, const DOCINFOW* doc)
98 docA.cbSize = doc->cbSize;
99 docA.lpszDocName = doc->lpszDocName ?
100 HEAP_strdupWtoA( GetProcessHeap(), 0, doc->lpszDocName ) : NULL;
101 docA.lpszOutput = doc->lpszOutput ?
102 HEAP_strdupWtoA( GetProcessHeap(), 0, doc->lpszOutput ) : NULL;
103 docA.lpszDatatype = doc->lpszDatatype ?
104 HEAP_strdupWtoA( GetProcessHeap(), 0, doc->lpszDatatype ) : NULL;
105 docA.fwType = doc->fwType;
107 ret = StartDocA(hdc, &docA);
110 HeapFree( GetProcessHeap(), 0, (LPSTR)docA.lpszDocName );
112 HeapFree( GetProcessHeap(), 0, (LPSTR)docA.lpszOutput );
113 if(docA.lpszDatatype)
114 HeapFree( GetProcessHeap(), 0, (LPSTR)docA.lpszDatatype );
119 /******************************************************************
123 INT16 WINAPI EndDoc16(HDC16 hdc)
128 /******************************************************************
132 INT WINAPI EndDoc(HDC hdc)
135 DC *dc = DC_GetDCPtr( hdc );
136 if(!dc) return SP_ERROR;
138 if(dc->funcs->pEndDoc)
139 ret = dc->funcs->pEndDoc( dc );
141 ret = Escape(hdc, ENDDOC, 0, 0, 0);
142 GDI_ReleaseObj( hdc );
146 /******************************************************************
147 * StartPage16 [GDI.379]
150 INT16 WINAPI StartPage16(HDC16 hdc)
152 return StartPage(hdc);
155 /******************************************************************
156 * StartPage [GDI32.349]
159 INT WINAPI StartPage(HDC hdc)
162 DC *dc = DC_GetDCPtr( hdc );
163 if(!dc) return SP_ERROR;
165 if(dc->funcs->pStartPage)
166 ret = dc->funcs->pStartPage( dc );
169 GDI_ReleaseObj( hdc );
173 /******************************************************************
174 * EndPage16 [GDI.380]
177 INT16 WINAPI EndPage16( HDC16 hdc )
182 /******************************************************************
186 INT WINAPI EndPage(HDC hdc)
189 DC *dc = DC_GetDCPtr( hdc );
190 if(!dc) return SP_ERROR;
192 if(dc->funcs->pEndPage)
193 ret = dc->funcs->pEndPage( dc );
195 ret = Escape(hdc, NEWFRAME, 0, 0, 0);
196 GDI_ReleaseObj( hdc );
200 /******************************************************************************
201 * AbortDoc16 [GDI.382]
203 INT16 WINAPI AbortDoc16(HDC16 hdc)
205 return AbortDoc(hdc);
208 /******************************************************************************
209 * AbortDoc [GDI32.105]
211 INT WINAPI AbortDoc(HDC hdc)
214 DC *dc = DC_GetDCPtr( hdc );
215 if(!dc) return SP_ERROR;
217 if(dc->funcs->pAbortDoc)
218 ret = dc->funcs->pAbortDoc( dc );
220 ret = Escape(hdc, ABORTDOC, 0, 0, 0);
221 GDI_ReleaseObj( hdc );
225 /**********************************************************************
226 * QueryAbort16 (GDI.155)
228 * Calls the app's AbortProc function if avail.
231 * TRUE if no AbortProc avail or AbortProc wants to continue printing.
232 * FALSE if AbortProc wants to abort printing.
234 BOOL16 WINAPI QueryAbort16(HDC16 hdc, INT16 reserved)
237 DC *dc = DC_GetDCPtr( hdc );
240 ERR("Invalid hdc %04x\n", hdc);
244 if (dc->w.pAbortProc) ret = dc->w.pAbortProc(hdc, 0);
245 GDI_ReleaseObj( hdc );
249 /* ### start build ### */
250 extern WORD CALLBACK PRTDRV_CallTo16_word_ww(FARPROC16,WORD,WORD);
251 /* ### stop build ### */
253 /**********************************************************************
254 * SetAbortProc16 (GDI.381)
257 INT16 WINAPI SetAbortProc16(HDC16 hdc, SEGPTR abrtprc)
259 ABORTPROC proc32 = (ABORTPROC)THUNK_Alloc((FARPROC16)abrtprc,
260 (RELAY)PRTDRV_CallTo16_word_ww);
261 return SetAbortProc(hdc, proc32);
264 /**********************************************************************
265 * SetAbortProc (GDI32.301)
268 INT WINAPI SetAbortProc(HDC hdc, ABORTPROC abrtprc)
270 DC *dc = DC_GetDCPtr( hdc );
272 if(dc->w.pAbortProc) THUNK_Free((FARPROC)dc->w.pAbortProc);
273 dc->w.pAbortProc = abrtprc;
274 GDI_ReleaseObj( hdc );
279 /****************** misc. printer related functions */
282 * The following function should implement a queing system
291 static struct hpq *hpqueue;
293 /**********************************************************************
297 HPQ16 WINAPI CreatePQ16(INT16 size)
304 tmp_size = size << 2;
305 if (!(hpq = GlobalAlloc16(GMEM_SHARE|GMEM_MOVEABLE, tmp_size + 8)))
307 pPQ = GlobalLock16(hpq);
316 FIXME("(%d): stub\n",size);
321 /**********************************************************************
325 INT16 WINAPI DeletePQ16(HPQ16 hPQ)
327 return GlobalFree16((HGLOBAL16)hPQ);
330 /**********************************************************************
331 * ExtractPQ (GDI.232)
334 INT16 WINAPI ExtractPQ16(HPQ16 hPQ)
336 struct hpq *queue, *prev, *current, *currentPrev;
337 int key = 0, tag = -1;
338 currentPrev = prev = NULL;
339 queue = current = hpqueue;
345 currentPrev = current;
346 current = current->next;
349 if (current->key < key)
361 prev->next = queue->next;
363 hpqueue = queue->next;
364 HeapFree(GetProcessHeap(), 0, queue);
367 TRACE("%x got tag %d key %d\n", hPQ, tag, key);
372 /**********************************************************************
376 INT16 WINAPI InsertPQ16(HPQ16 hPQ, INT16 tag, INT16 key)
378 struct hpq *queueItem = HeapAlloc(GetProcessHeap(), 0, sizeof(struct hpq));
379 if(queueItem == NULL) {
380 ERR("Memory exausted!");
383 queueItem->next = hpqueue;
385 queueItem->key = key;
386 queueItem->tag = tag;
388 FIXME("(%x %d %d): stub???\n", hPQ, tag, key);
392 /**********************************************************************
396 INT16 WINAPI MinPQ16(HPQ16 hPQ)
398 FIXME("(%x): stub\n", hPQ);
402 /**********************************************************************
406 INT16 WINAPI SizePQ16(HPQ16 hPQ, INT16 sizechange)
408 FIXME("(%x %d): stub\n", hPQ, sizechange);
415 * The following functions implement part of the spooling process to
416 * print manager. I would like to see wine have a version of print managers
417 * that used LPR/LPD. For simplicity print jobs will be sent to a file for
420 typedef struct PRINTJOB
428 } PRINTJOB, *PPRINTJOB;
430 #define MAX_PRINT_JOBS 1
433 PPRINTJOB gPrintJobsTable[MAX_PRINT_JOBS];
436 static PPRINTJOB FindPrintJobFromHandle(HANDLE16 hHandle)
438 return gPrintJobsTable[0];
441 static int CreateSpoolFile(LPCSTR pszOutput)
445 char *psCmdP = psCmd;
447 /* TTD convert the 'output device' into a spool file name */
449 if (pszOutput == NULL || *pszOutput == '\0')
452 PROFILE_GetWineIniString( "spooler", pszOutput, "", psCmd, sizeof(psCmd) );
453 TRACE("Got printerSpoolCommand '%s' for output device '%s'\n",
456 psCmdP = (char *)pszOutput;
459 while (*psCmdP && isspace(*psCmdP))
475 TRACE("In child need to exec %s\n",psCmdP);
485 TRACE("Need to execute a cmnd and pipe the output to it\n");
489 DOS_FULL_NAME fullName;
491 TRACE("Just assume its a file\n");
494 * The file name can be dos based, we have to find its
495 * Unix correspondant file name
497 DOSFS_GetFullName(psCmdP, FALSE, &fullName);
499 if ((fd = open(fullName.long_name, O_CREAT | O_TRUNC | O_WRONLY , 0600)) < 0)
501 ERR("Failed to create spool file %s, errno = %d\n",
502 fullName.long_name, errno);
508 static int FreePrintJob(HANDLE16 hJob)
513 pPrintJob = FindPrintJobFromHandle(hJob);
514 if (pPrintJob != NULL)
516 gPrintJobsTable[pPrintJob->nIndex] = NULL;
517 HeapFree(GetProcessHeap(), 0, pPrintJob->pszOutput);
518 HeapFree(GetProcessHeap(), 0, pPrintJob->pszTitle);
519 if (pPrintJob->fd >= 0) close(pPrintJob->fd);
520 HeapFree(GetProcessHeap(), 0, pPrintJob);
526 /**********************************************************************
530 HPJOB16 WINAPI OpenJob16(LPCSTR lpOutput, LPCSTR lpTitle, HDC16 hDC)
532 HPJOB16 hHandle = (HPJOB16)SP_ERROR;
535 TRACE("'%s' '%s' %04x\n", lpOutput, lpTitle, hDC);
537 pPrintJob = gPrintJobsTable[0];
538 if (pPrintJob == NULL)
542 /* Try an create a spool file */
543 fd = CreateSpoolFile(lpOutput);
546 pPrintJob = HeapAlloc(GetProcessHeap(), 0, sizeof(PRINTJOB));
547 if(pPrintJob == NULL) {
548 WARN("Memory exausted!");
554 pPrintJob->pszOutput = HEAP_strdupA(GetProcessHeap(), 0, lpOutput);
556 pPrintJob->pszTitle = HEAP_strdupA(GetProcessHeap(), 0, lpTitle);
557 pPrintJob->hDC = hDC;
559 pPrintJob->nIndex = 0;
560 pPrintJob->hHandle = hHandle;
561 gPrintJobsTable[pPrintJob->nIndex] = pPrintJob;
564 TRACE("return %04x\n", hHandle);
568 /**********************************************************************
572 INT16 WINAPI CloseJob16(HPJOB16 hJob)
575 PPRINTJOB pPrintJob = NULL;
577 TRACE("%04x\n", hJob);
579 pPrintJob = FindPrintJobFromHandle(hJob);
580 if (pPrintJob != NULL)
582 /* Close the spool file */
583 close(pPrintJob->fd);
590 /**********************************************************************
591 * WriteSpool (GDI.241)
594 INT16 WINAPI WriteSpool16(HPJOB16 hJob, LPSTR lpData, INT16 cch)
597 PPRINTJOB pPrintJob = NULL;
599 TRACE("%04x %08lx %04x\n", hJob, (DWORD)lpData, cch);
601 pPrintJob = FindPrintJobFromHandle(hJob);
602 if (pPrintJob != NULL && pPrintJob->fd >= 0 && cch)
604 if (write(pPrintJob->fd, lpData, cch) != cch)
608 if (pPrintJob->hDC == 0) {
609 TRACE("hDC == 0 so no QueryAbort\n");
611 else if (!(QueryAbort16(pPrintJob->hDC, (nRet == SP_OUTOFDISK) ? nRet : 0 )))
613 CloseJob16(hJob); /* printing aborted */
620 /**********************************************************************
621 * WriteDialog (GDI.242)
624 INT16 WINAPI WriteDialog16(HPJOB16 hJob, LPSTR lpMsg, INT16 cchMsg)
626 TRACE("%04x %04x '%s'\n", hJob, cchMsg, lpMsg);
628 return Callout.MessageBoxA(0, lpMsg, "Printing Error", MB_OKCANCEL);
632 /**********************************************************************
633 * DeleteJob (GDI.244)
636 INT16 WINAPI DeleteJob16(HPJOB16 hJob, INT16 nNotUsed)
640 TRACE("%04x\n", hJob);
642 nRet = FreePrintJob(hJob);
647 * The following two function would allow a page to be sent to the printer
648 * when it has been processed. For simplicity they havn't been implemented.
649 * This means a whole job has to be processed before it is sent to the printer.
652 /**********************************************************************
653 * StartSpoolPage (GDI.246)
656 INT16 WINAPI StartSpoolPage16(HPJOB16 hJob)
658 FIXME("StartSpoolPage GDI.246 unimplemented\n");
664 /**********************************************************************
665 * EndSpoolPage (GDI.247)
668 INT16 WINAPI EndSpoolPage16(HPJOB16 hJob)
670 FIXME("EndSpoolPage GDI.247 unimplemented\n");
675 /**********************************************************************
676 * GetSpoolJob (GDI.245)
679 DWORD WINAPI GetSpoolJob16(int nOption, LONG param)
682 TRACE("In GetSpoolJob param 0x%lx noption %d\n",param, nOption);
687 /******************************************************************
688 * DrvGetPrinterDataInternal
690 * Helper for DrvGetPrinterData
692 static DWORD DrvGetPrinterDataInternal(LPSTR RegStr_Printer,
693 LPBYTE lpPrinterData, int cbData, int what)
697 DWORD dwType, cbQueryData;
699 if (!(RegOpenKeyA(HKEY_LOCAL_MACHINE, RegStr_Printer, &hkey))) {
700 if (what == INT_PD_DEFAULT_DEVMODE) { /* "Default DevMode" */
701 if (!(RegQueryValueExA(hkey, DefaultDevMode, 0, &dwType, 0, &cbQueryData))) {
704 else if ((cbQueryData) && (cbQueryData <= cbData)) {
705 cbQueryData = cbData;
706 if (RegQueryValueExA(hkey, DefaultDevMode, 0,
707 &dwType, lpPrinterData, &cbQueryData))
711 } else { /* "Printer Driver" */
713 RegQueryValueExA(hkey, "Printer Driver", 0,
714 &dwType, lpPrinterData, &cbQueryData);
718 if (hkey) RegCloseKey(hkey);
722 /******************************************************************
723 * DrvGetPrinterData [GDI.282]
726 DWORD WINAPI DrvGetPrinterData16(LPSTR lpPrinter, LPSTR lpProfile,
727 LPDWORD lpType, LPBYTE lpPrinterData,
728 int cbData, LPDWORD lpNeeded)
730 LPSTR RegStr_Printer;
731 HKEY hkey = 0, hkey2 = 0;
733 DWORD dwType, PrinterAttr, cbPrinterAttr, SetData, size;
735 if (HIWORD(lpPrinter))
736 TRACE("printer %s\n",lpPrinter);
738 TRACE("printer %p\n",lpPrinter);
739 if (HIWORD(lpProfile))
740 TRACE("profile %s\n",lpProfile);
742 TRACE("profile %p\n",lpProfile);
743 TRACE("lpType %p\n",lpType);
745 if ((!lpPrinter) || (!lpProfile) || (!lpNeeded))
746 return ERROR_INVALID_PARAMETER;
748 RegStr_Printer = HeapAlloc(GetProcessHeap(), 0,
749 strlen(Printers) + strlen(lpPrinter) + 2);
750 strcpy(RegStr_Printer, Printers);
751 strcat(RegStr_Printer, lpPrinter);
753 if (((DWORD)lpProfile == INT_PD_DEFAULT_DEVMODE) || (HIWORD(lpProfile) &&
754 (!strcmp(lpProfile, DefaultDevMode)))) {
755 size = DrvGetPrinterDataInternal(RegStr_Printer, lpPrinterData, cbData,
756 INT_PD_DEFAULT_DEVMODE);
759 if ((lpPrinterData) && (*lpNeeded > cbData))
760 res = ERROR_MORE_DATA;
762 else res = ERROR_INVALID_PRINTER_NAME;
765 if (((DWORD)lpProfile == INT_PD_DEFAULT_MODEL) || (HIWORD(lpProfile) &&
766 (!strcmp(lpProfile, PrinterModel)))) {
768 if (!lpPrinterData) goto failed;
770 res = ERROR_MORE_DATA;
773 size = DrvGetPrinterDataInternal(RegStr_Printer, lpPrinterData, cbData,
774 INT_PD_DEFAULT_MODEL);
775 if ((size+1) && (lpType))
778 res = ERROR_INVALID_PRINTER_NAME;
782 if ((res = RegOpenKeyA(HKEY_LOCAL_MACHINE, RegStr_Printer, &hkey)))
785 if ((res = RegQueryValueExA(hkey, "Attributes", 0,
786 &dwType, (LPBYTE)&PrinterAttr, &cbPrinterAttr)))
788 if ((res = RegOpenKeyA(hkey, PrinterDriverData, &hkey2)))
791 res = RegQueryValueExA(hkey2, lpProfile, 0,
792 lpType, lpPrinterData, lpNeeded);
793 if ((res != ERROR_CANTREAD) &&
795 (PRINTER_ATTRIBUTE_ENABLE_BIDI|PRINTER_ATTRIBUTE_NETWORK))
796 == PRINTER_ATTRIBUTE_NETWORK))
798 if (!(res) && (*lpType == REG_DWORD) && (*(LPDWORD)lpPrinterData == -1))
799 res = ERROR_INVALID_DATA;
804 RegSetValueExA(hkey2, lpProfile, 0, REG_DWORD, (LPBYTE)&SetData, 4); /* no result returned */
809 if (hkey2) RegCloseKey(hkey2);
810 if (hkey) RegCloseKey(hkey);
811 HeapFree(GetProcessHeap(), 0, RegStr_Printer);
816 /******************************************************************
817 * DrvSetPrinterData [GDI.281]
820 DWORD WINAPI DrvSetPrinterData16(LPSTR lpPrinter, LPSTR lpProfile,
821 DWORD lpType, LPBYTE lpPrinterData,
824 LPSTR RegStr_Printer;
828 if (HIWORD(lpPrinter))
829 TRACE("printer %s\n",lpPrinter);
831 TRACE("printer %p\n",lpPrinter);
832 if (HIWORD(lpProfile))
833 TRACE("profile %s\n",lpProfile);
835 TRACE("profile %p\n",lpProfile);
836 TRACE("lpType %08lx\n",lpType);
838 if ((!lpPrinter) || (!lpProfile) ||
839 ((DWORD)lpProfile == INT_PD_DEFAULT_MODEL) || (HIWORD(lpProfile) &&
840 (!strcmp(lpProfile, PrinterModel))))
841 return ERROR_INVALID_PARAMETER;
843 RegStr_Printer = HeapAlloc(GetProcessHeap(), 0,
844 strlen(Printers) + strlen(lpPrinter) + 2);
845 strcpy(RegStr_Printer, Printers);
846 strcat(RegStr_Printer, lpPrinter);
848 if (((DWORD)lpProfile == INT_PD_DEFAULT_DEVMODE) || (HIWORD(lpProfile) &&
849 (!strcmp(lpProfile, DefaultDevMode)))) {
850 if ( RegOpenKeyA(HKEY_LOCAL_MACHINE, RegStr_Printer, &hkey)
852 RegSetValueExA(hkey, DefaultDevMode, 0, REG_BINARY,
853 lpPrinterData, dwSize) != ERROR_SUCCESS )
854 res = ERROR_INVALID_PRINTER_NAME;
858 strcat(RegStr_Printer, "\\");
860 if( (res = RegOpenKeyA(HKEY_LOCAL_MACHINE, RegStr_Printer, &hkey)) ==
864 res = RegDeleteValueA(hkey, lpProfile);
866 res = RegSetValueExA(hkey, lpProfile, 0, lpType,
867 lpPrinterData, dwSize);
871 if (hkey) RegCloseKey(hkey);
872 HeapFree(GetProcessHeap(), 0, RegStr_Printer);