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/winbase16.h"
19 #include "wine/wingdi16.h"
23 #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 /******************************************************************
39 * StartDoc16 [GDI.377]
42 INT16 WINAPI StartDoc16( HDC16 hdc, const DOCINFO16 *lpdoc )
46 docA.cbSize = lpdoc->cbSize;
47 docA.lpszDocName = PTR_SEG_TO_LIN(lpdoc->lpszDocName);
48 docA.lpszOutput = PTR_SEG_TO_LIN(lpdoc->lpszOutput);
50 if(lpdoc->cbSize >= 14)
51 docA.lpszDatatype = PTR_SEG_TO_LIN(lpdoc->lpszDatatype);
53 docA.lpszDatatype = NULL;
55 if(lpdoc->cbSize >= 18)
56 docA.fwType = lpdoc->fwType;
60 return StartDocA(hdc, &docA);
63 /******************************************************************
64 * StartDocA [GDI32.347]
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 INT WINAPI StartDocA(HDC hdc, const DOCINFOA* doc)
74 DC *dc = DC_GetDCPtr( hdc );
76 TRACE("DocName = '%s' Output = '%s' Datatype = '%s'\n",
77 doc->lpszDocName, doc->lpszOutput, doc->lpszDatatype);
79 if(!dc) return SP_ERROR;
81 if(dc->funcs->pStartDoc)
82 ret = dc->funcs->pStartDoc( dc, doc );
84 ret = Escape(hdc, STARTDOC, strlen(doc->lpszDocName),
85 doc->lpszDocName, (LPVOID)doc);
86 GDI_ReleaseObj( hdc );
90 /*************************************************************************
91 * StartDocW [GDI32.348]
94 INT WINAPI StartDocW(HDC hdc, const DOCINFOW* doc)
99 docA.cbSize = doc->cbSize;
100 docA.lpszDocName = doc->lpszDocName ?
101 HEAP_strdupWtoA( GetProcessHeap(), 0, doc->lpszDocName ) : NULL;
102 docA.lpszOutput = doc->lpszOutput ?
103 HEAP_strdupWtoA( GetProcessHeap(), 0, doc->lpszOutput ) : NULL;
104 docA.lpszDatatype = doc->lpszDatatype ?
105 HEAP_strdupWtoA( GetProcessHeap(), 0, doc->lpszDatatype ) : NULL;
106 docA.fwType = doc->fwType;
108 ret = StartDocA(hdc, &docA);
111 HeapFree( GetProcessHeap(), 0, (LPSTR)docA.lpszDocName );
113 HeapFree( GetProcessHeap(), 0, (LPSTR)docA.lpszOutput );
114 if(docA.lpszDatatype)
115 HeapFree( GetProcessHeap(), 0, (LPSTR)docA.lpszDatatype );
120 /******************************************************************
124 INT16 WINAPI EndDoc16(HDC16 hdc)
129 /******************************************************************
133 INT WINAPI EndDoc(HDC hdc)
136 DC *dc = DC_GetDCPtr( hdc );
137 if(!dc) return SP_ERROR;
139 if(dc->funcs->pEndDoc)
140 ret = dc->funcs->pEndDoc( dc );
142 ret = Escape(hdc, ENDDOC, 0, 0, 0);
143 GDI_ReleaseObj( hdc );
147 /******************************************************************
148 * StartPage16 [GDI.379]
151 INT16 WINAPI StartPage16(HDC16 hdc)
153 return StartPage(hdc);
156 /******************************************************************
157 * StartPage [GDI32.349]
160 INT WINAPI StartPage(HDC hdc)
163 DC *dc = DC_GetDCPtr( hdc );
164 if(!dc) return SP_ERROR;
166 if(dc->funcs->pStartPage)
167 ret = dc->funcs->pStartPage( dc );
170 GDI_ReleaseObj( hdc );
174 /******************************************************************
175 * EndPage16 [GDI.380]
178 INT16 WINAPI EndPage16( HDC16 hdc )
183 /******************************************************************
187 INT WINAPI EndPage(HDC hdc)
190 DC *dc = DC_GetDCPtr( hdc );
191 if(!dc) return SP_ERROR;
193 if(dc->funcs->pEndPage)
194 ret = dc->funcs->pEndPage( dc );
196 ret = Escape(hdc, NEWFRAME, 0, 0, 0);
197 GDI_ReleaseObj( hdc );
201 /******************************************************************************
202 * AbortDoc16 [GDI.382]
204 INT16 WINAPI AbortDoc16(HDC16 hdc)
206 return AbortDoc(hdc);
209 /******************************************************************************
210 * AbortDoc [GDI32.105]
212 INT WINAPI AbortDoc(HDC hdc)
215 DC *dc = DC_GetDCPtr( hdc );
216 if(!dc) return SP_ERROR;
218 if(dc->funcs->pAbortDoc)
219 ret = dc->funcs->pAbortDoc( dc );
221 ret = Escape(hdc, ABORTDOC, 0, 0, 0);
222 GDI_ReleaseObj( hdc );
226 /**********************************************************************
227 * QueryAbort16 (GDI.155)
229 * Calls the app's AbortProc function if avail.
232 * TRUE if no AbortProc avail or AbortProc wants to continue printing.
233 * FALSE if AbortProc wants to abort printing.
235 BOOL16 WINAPI QueryAbort16(HDC16 hdc, INT16 reserved)
238 DC *dc = DC_GetDCPtr( hdc );
241 ERR("Invalid hdc %04x\n", hdc);
245 if (dc->w.pAbortProc) ret = dc->w.pAbortProc(hdc, 0);
246 GDI_ReleaseObj( hdc );
250 /* ### start build ### */
251 extern WORD CALLBACK PRTDRV_CallTo16_word_ww(FARPROC16,WORD,WORD);
252 /* ### stop build ### */
254 /**********************************************************************
255 * SetAbortProc16 (GDI.381)
258 INT16 WINAPI SetAbortProc16(HDC16 hdc, SEGPTR abrtprc)
260 ABORTPROC proc32 = (ABORTPROC)THUNK_Alloc((FARPROC16)abrtprc,
261 (RELAY)PRTDRV_CallTo16_word_ww);
262 return SetAbortProc(hdc, proc32);
265 /**********************************************************************
266 * SetAbortProc (GDI32.301)
269 INT WINAPI SetAbortProc(HDC hdc, ABORTPROC abrtprc)
271 DC *dc = DC_GetDCPtr( hdc );
273 if(dc->w.pAbortProc) THUNK_Free((FARPROC)dc->w.pAbortProc);
274 dc->w.pAbortProc = abrtprc;
275 GDI_ReleaseObj( hdc );
280 /****************** misc. printer related functions */
283 * The following function should implement a queing system
292 static struct hpq *hpqueue;
294 /**********************************************************************
298 HPQ16 WINAPI CreatePQ16(INT16 size)
305 tmp_size = size << 2;
306 if (!(hpq = GlobalAlloc16(GMEM_SHARE|GMEM_MOVEABLE, tmp_size + 8)))
308 pPQ = GlobalLock16(hpq);
317 FIXME("(%d): stub\n",size);
322 /**********************************************************************
326 INT16 WINAPI DeletePQ16(HPQ16 hPQ)
328 return GlobalFree16((HGLOBAL16)hPQ);
331 /**********************************************************************
332 * ExtractPQ (GDI.232)
335 INT16 WINAPI ExtractPQ16(HPQ16 hPQ)
337 struct hpq *queue, *prev, *current, *currentPrev;
338 int key = 0, tag = -1;
339 currentPrev = prev = NULL;
340 queue = current = hpqueue;
346 currentPrev = current;
347 current = current->next;
350 if (current->key < key)
362 prev->next = queue->next;
364 hpqueue = queue->next;
365 HeapFree(GetProcessHeap(), 0, queue);
368 TRACE("%x got tag %d key %d\n", hPQ, tag, key);
373 /**********************************************************************
377 INT16 WINAPI InsertPQ16(HPQ16 hPQ, INT16 tag, INT16 key)
379 struct hpq *queueItem = HeapAlloc(GetProcessHeap(), 0, sizeof(struct hpq));
380 if(queueItem == NULL) {
381 ERR("Memory exausted!");
384 queueItem->next = hpqueue;
386 queueItem->key = key;
387 queueItem->tag = tag;
389 FIXME("(%x %d %d): stub???\n", hPQ, tag, key);
393 /**********************************************************************
397 INT16 WINAPI MinPQ16(HPQ16 hPQ)
399 FIXME("(%x): stub\n", hPQ);
403 /**********************************************************************
407 INT16 WINAPI SizePQ16(HPQ16 hPQ, INT16 sizechange)
409 FIXME("(%x %d): stub\n", hPQ, sizechange);
416 * The following functions implement part of the spooling process to
417 * print manager. I would like to see wine have a version of print managers
418 * that used LPR/LPD. For simplicity print jobs will be sent to a file for
421 typedef struct PRINTJOB
429 } PRINTJOB, *PPRINTJOB;
431 #define MAX_PRINT_JOBS 1
434 PPRINTJOB gPrintJobsTable[MAX_PRINT_JOBS];
437 static PPRINTJOB FindPrintJobFromHandle(HANDLE16 hHandle)
439 return gPrintJobsTable[0];
442 static int CreateSpoolFile(LPCSTR pszOutput)
446 char *psCmdP = psCmd;
448 /* TTD convert the 'output device' into a spool file name */
450 if (pszOutput == NULL || *pszOutput == '\0')
453 PROFILE_GetWineIniString( "spooler", pszOutput, "", psCmd, sizeof(psCmd) );
454 TRACE("Got printerSpoolCommand '%s' for output device '%s'\n",
457 psCmdP = (char *)pszOutput;
460 while (*psCmdP && isspace(*psCmdP))
476 TRACE("In child need to exec %s\n",psCmdP);
486 TRACE("Need to execute a cmnd and pipe the output to it\n");
490 DOS_FULL_NAME fullName;
492 TRACE("Just assume it's a file\n");
495 * The file name can be dos based, we have to find its
496 * Unix correspondant file name
498 DOSFS_GetFullName(psCmdP, FALSE, &fullName);
500 if ((fd = open(fullName.long_name, O_CREAT | O_TRUNC | O_WRONLY , 0600)) < 0)
502 ERR("Failed to create spool file %s (%s)\n",
503 fullName.long_name, strerror(errno));
509 static int FreePrintJob(HANDLE16 hJob)
514 pPrintJob = FindPrintJobFromHandle(hJob);
515 if (pPrintJob != NULL)
517 gPrintJobsTable[pPrintJob->nIndex] = NULL;
518 HeapFree(GetProcessHeap(), 0, pPrintJob->pszOutput);
519 HeapFree(GetProcessHeap(), 0, pPrintJob->pszTitle);
520 if (pPrintJob->fd >= 0) close(pPrintJob->fd);
521 HeapFree(GetProcessHeap(), 0, pPrintJob);
527 /**********************************************************************
531 HPJOB16 WINAPI OpenJob16(LPCSTR lpOutput, LPCSTR lpTitle, HDC16 hDC)
533 HPJOB16 hHandle = (HPJOB16)SP_ERROR;
536 TRACE("'%s' '%s' %04x\n", lpOutput, lpTitle, hDC);
538 pPrintJob = gPrintJobsTable[0];
539 if (pPrintJob == NULL)
543 /* Try an create a spool file */
544 fd = CreateSpoolFile(lpOutput);
547 pPrintJob = HeapAlloc(GetProcessHeap(), 0, sizeof(PRINTJOB));
548 if(pPrintJob == NULL) {
549 WARN("Memory exausted!");
555 pPrintJob->pszOutput = HEAP_strdupA(GetProcessHeap(), 0, lpOutput);
557 pPrintJob->pszTitle = HEAP_strdupA(GetProcessHeap(), 0, lpTitle);
558 pPrintJob->hDC = hDC;
560 pPrintJob->nIndex = 0;
561 pPrintJob->hHandle = hHandle;
562 gPrintJobsTable[pPrintJob->nIndex] = pPrintJob;
565 TRACE("return %04x\n", hHandle);
569 /**********************************************************************
573 INT16 WINAPI CloseJob16(HPJOB16 hJob)
576 PPRINTJOB pPrintJob = NULL;
578 TRACE("%04x\n", hJob);
580 pPrintJob = FindPrintJobFromHandle(hJob);
581 if (pPrintJob != NULL)
583 /* Close the spool file */
584 close(pPrintJob->fd);
591 /**********************************************************************
592 * WriteSpool (GDI.241)
595 INT16 WINAPI WriteSpool16(HPJOB16 hJob, LPSTR lpData, INT16 cch)
598 PPRINTJOB pPrintJob = NULL;
600 TRACE("%04x %08lx %04x\n", hJob, (DWORD)lpData, cch);
602 pPrintJob = FindPrintJobFromHandle(hJob);
603 if (pPrintJob != NULL && pPrintJob->fd >= 0 && cch)
605 if (write(pPrintJob->fd, lpData, cch) != cch)
609 if (pPrintJob->hDC == 0) {
610 TRACE("hDC == 0 so no QueryAbort\n");
612 else if (!(QueryAbort16(pPrintJob->hDC, (nRet == SP_OUTOFDISK) ? nRet : 0 )))
614 CloseJob16(hJob); /* printing aborted */
621 /**********************************************************************
622 * WriteDialog (GDI.242)
625 INT16 WINAPI WriteDialog16(HPJOB16 hJob, LPSTR lpMsg, INT16 cchMsg)
627 TRACE("%04x %04x '%s'\n", hJob, cchMsg, lpMsg);
629 return Callout.MessageBoxA(0, lpMsg, "Printing Error", MB_OKCANCEL);
633 /**********************************************************************
634 * DeleteJob (GDI.244)
637 INT16 WINAPI DeleteJob16(HPJOB16 hJob, INT16 nNotUsed)
641 TRACE("%04x\n", hJob);
643 nRet = FreePrintJob(hJob);
648 * The following two function would allow a page to be sent to the printer
649 * when it has been processed. For simplicity they havn't been implemented.
650 * This means a whole job has to be processed before it is sent to the printer.
653 /**********************************************************************
654 * StartSpoolPage (GDI.246)
657 INT16 WINAPI StartSpoolPage16(HPJOB16 hJob)
659 FIXME("StartSpoolPage GDI.246 unimplemented\n");
665 /**********************************************************************
666 * EndSpoolPage (GDI.247)
669 INT16 WINAPI EndSpoolPage16(HPJOB16 hJob)
671 FIXME("EndSpoolPage GDI.247 unimplemented\n");
676 /**********************************************************************
677 * GetSpoolJob (GDI.245)
680 DWORD WINAPI GetSpoolJob16(int nOption, LONG param)
683 TRACE("In GetSpoolJob param 0x%lx noption %d\n",param, nOption);
688 /******************************************************************
689 * DrvGetPrinterDataInternal
691 * Helper for DrvGetPrinterData
693 static DWORD DrvGetPrinterDataInternal(LPSTR RegStr_Printer,
694 LPBYTE lpPrinterData, int cbData, int what)
698 DWORD dwType, cbQueryData;
700 if (!(RegOpenKeyA(HKEY_LOCAL_MACHINE, RegStr_Printer, &hkey))) {
701 if (what == INT_PD_DEFAULT_DEVMODE) { /* "Default DevMode" */
702 if (!(RegQueryValueExA(hkey, DefaultDevMode, 0, &dwType, 0, &cbQueryData))) {
705 else if ((cbQueryData) && (cbQueryData <= cbData)) {
706 cbQueryData = cbData;
707 if (RegQueryValueExA(hkey, DefaultDevMode, 0,
708 &dwType, lpPrinterData, &cbQueryData))
712 } else { /* "Printer Driver" */
714 RegQueryValueExA(hkey, "Printer Driver", 0,
715 &dwType, lpPrinterData, &cbQueryData);
719 if (hkey) RegCloseKey(hkey);
723 /******************************************************************
724 * DrvGetPrinterData [GDI.282]
727 DWORD WINAPI DrvGetPrinterData16(LPSTR lpPrinter, LPSTR lpProfile,
728 LPDWORD lpType, LPBYTE lpPrinterData,
729 int cbData, LPDWORD lpNeeded)
731 LPSTR RegStr_Printer;
732 HKEY hkey = 0, hkey2 = 0;
734 DWORD dwType, PrinterAttr, cbPrinterAttr, SetData, size;
736 if (HIWORD(lpPrinter))
737 TRACE("printer %s\n",lpPrinter);
739 TRACE("printer %p\n",lpPrinter);
740 if (HIWORD(lpProfile))
741 TRACE("profile %s\n",lpProfile);
743 TRACE("profile %p\n",lpProfile);
744 TRACE("lpType %p\n",lpType);
746 if ((!lpPrinter) || (!lpProfile) || (!lpNeeded))
747 return ERROR_INVALID_PARAMETER;
749 RegStr_Printer = HeapAlloc(GetProcessHeap(), 0,
750 strlen(Printers) + strlen(lpPrinter) + 2);
751 strcpy(RegStr_Printer, Printers);
752 strcat(RegStr_Printer, lpPrinter);
754 if (((DWORD)lpProfile == INT_PD_DEFAULT_DEVMODE) || (HIWORD(lpProfile) &&
755 (!strcmp(lpProfile, DefaultDevMode)))) {
756 size = DrvGetPrinterDataInternal(RegStr_Printer, lpPrinterData, cbData,
757 INT_PD_DEFAULT_DEVMODE);
760 if ((lpPrinterData) && (*lpNeeded > cbData))
761 res = ERROR_MORE_DATA;
763 else res = ERROR_INVALID_PRINTER_NAME;
766 if (((DWORD)lpProfile == INT_PD_DEFAULT_MODEL) || (HIWORD(lpProfile) &&
767 (!strcmp(lpProfile, PrinterModel)))) {
769 if (!lpPrinterData) goto failed;
771 res = ERROR_MORE_DATA;
774 size = DrvGetPrinterDataInternal(RegStr_Printer, lpPrinterData, cbData,
775 INT_PD_DEFAULT_MODEL);
776 if ((size+1) && (lpType))
779 res = ERROR_INVALID_PRINTER_NAME;
783 if ((res = RegOpenKeyA(HKEY_LOCAL_MACHINE, RegStr_Printer, &hkey)))
786 if ((res = RegQueryValueExA(hkey, "Attributes", 0,
787 &dwType, (LPBYTE)&PrinterAttr, &cbPrinterAttr)))
789 if ((res = RegOpenKeyA(hkey, PrinterDriverData, &hkey2)))
792 res = RegQueryValueExA(hkey2, lpProfile, 0,
793 lpType, lpPrinterData, lpNeeded);
794 if ((res != ERROR_CANTREAD) &&
796 (PRINTER_ATTRIBUTE_ENABLE_BIDI|PRINTER_ATTRIBUTE_NETWORK))
797 == PRINTER_ATTRIBUTE_NETWORK))
799 if (!(res) && (*lpType == REG_DWORD) && (*(LPDWORD)lpPrinterData == -1))
800 res = ERROR_INVALID_DATA;
805 RegSetValueExA(hkey2, lpProfile, 0, REG_DWORD, (LPBYTE)&SetData, 4); /* no result returned */
810 if (hkey2) RegCloseKey(hkey2);
811 if (hkey) RegCloseKey(hkey);
812 HeapFree(GetProcessHeap(), 0, RegStr_Printer);
817 /******************************************************************
818 * DrvSetPrinterData [GDI.281]
821 DWORD WINAPI DrvSetPrinterData16(LPSTR lpPrinter, LPSTR lpProfile,
822 DWORD lpType, LPBYTE lpPrinterData,
825 LPSTR RegStr_Printer;
829 if (HIWORD(lpPrinter))
830 TRACE("printer %s\n",lpPrinter);
832 TRACE("printer %p\n",lpPrinter);
833 if (HIWORD(lpProfile))
834 TRACE("profile %s\n",lpProfile);
836 TRACE("profile %p\n",lpProfile);
837 TRACE("lpType %08lx\n",lpType);
839 if ((!lpPrinter) || (!lpProfile) ||
840 ((DWORD)lpProfile == INT_PD_DEFAULT_MODEL) || (HIWORD(lpProfile) &&
841 (!strcmp(lpProfile, PrinterModel))))
842 return ERROR_INVALID_PARAMETER;
844 RegStr_Printer = HeapAlloc(GetProcessHeap(), 0,
845 strlen(Printers) + strlen(lpPrinter) + 2);
846 strcpy(RegStr_Printer, Printers);
847 strcat(RegStr_Printer, lpPrinter);
849 if (((DWORD)lpProfile == INT_PD_DEFAULT_DEVMODE) || (HIWORD(lpProfile) &&
850 (!strcmp(lpProfile, DefaultDevMode)))) {
851 if ( RegOpenKeyA(HKEY_LOCAL_MACHINE, RegStr_Printer, &hkey)
853 RegSetValueExA(hkey, DefaultDevMode, 0, REG_BINARY,
854 lpPrinterData, dwSize) != ERROR_SUCCESS )
855 res = ERROR_INVALID_PRINTER_NAME;
859 strcat(RegStr_Printer, "\\");
861 if( (res = RegOpenKeyA(HKEY_LOCAL_MACHINE, RegStr_Printer, &hkey)) ==
865 res = RegDeleteValueA(hkey, lpProfile);
867 res = RegSetValueExA(hkey, lpProfile, 0, lpType,
868 lpPrinterData, dwSize);
872 if (hkey) RegCloseKey(hkey);
873 HeapFree(GetProcessHeap(), 0, RegStr_Printer);