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 /******************************************************************
40 * StartDoc16 [GDI.377]
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 /******************************************************************
65 * StartDocA [GDI32.347]
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 /*************************************************************************
92 * StartDocW [GDI32.348]
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 * StartPage16 [GDI.379]
152 INT16 WINAPI StartPage16(HDC16 hdc)
154 return StartPage(hdc);
157 /******************************************************************
158 * StartPage [GDI32.349]
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 /******************************************************************
176 * EndPage16 [GDI.380]
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 /******************************************************************************
208 * AbortDoc16 [GDI.382]
210 INT16 WINAPI AbortDoc16(HDC16 hdc)
212 return AbortDoc(hdc);
215 /******************************************************************************
216 * AbortDoc [GDI32.105]
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 * QueryAbort16 (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 * SetAbortProc16 (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.301)
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);
462 PROFILE_GetWineIniString("spooler",pszOutput,"",psCmd,sizeof(psCmd));
463 TRACE("Got printerSpoolCommand '%s' for output device '%s'\n",
466 psCmdP = (char *)pszOutput;
469 while (*psCmdP && isspace(*psCmdP))
485 TRACE("In child need to exec %s\n",psCmdP);
495 TRACE("Need to execute a cmnd and pipe the output to it\n");
499 DOS_FULL_NAME fullName;
501 TRACE("Just assume it's a file\n");
504 * The file name can be dos based, we have to find its
505 * Unix correspondant file name
507 DOSFS_GetFullName(psCmdP, FALSE, &fullName);
509 if ((fd = open(fullName.long_name, O_CREAT | O_TRUNC | O_WRONLY , 0600)) < 0)
511 ERR("Failed to create spool file %s (%s)\n",
512 fullName.long_name, strerror(errno));
518 static int FreePrintJob(HANDLE16 hJob)
523 pPrintJob = FindPrintJobFromHandle(hJob);
524 if (pPrintJob != NULL)
526 gPrintJobsTable[pPrintJob->nIndex] = NULL;
527 HeapFree(GetProcessHeap(), 0, pPrintJob->pszOutput);
528 HeapFree(GetProcessHeap(), 0, pPrintJob->pszTitle);
529 if (pPrintJob->fd >= 0) close(pPrintJob->fd);
530 HeapFree(GetProcessHeap(), 0, pPrintJob);
536 /**********************************************************************
540 HPJOB16 WINAPI OpenJob16(LPCSTR lpOutput, LPCSTR lpTitle, HDC16 hDC)
542 HPJOB16 hHandle = (HPJOB16)SP_ERROR;
545 TRACE("'%s' '%s' %04x\n", lpOutput, lpTitle, hDC);
547 pPrintJob = gPrintJobsTable[0];
548 if (pPrintJob == NULL)
552 /* Try an create a spool file */
553 fd = CreateSpoolFile(lpOutput);
556 pPrintJob = HeapAlloc(GetProcessHeap(), 0, sizeof(PRINTJOB));
557 if(pPrintJob == NULL) {
558 WARN("Memory exausted!\n");
564 pPrintJob->pszOutput = HEAP_strdupA(GetProcessHeap(), 0, lpOutput);
566 pPrintJob->pszTitle = HEAP_strdupA(GetProcessHeap(), 0, lpTitle);
567 pPrintJob->hDC = hDC;
569 pPrintJob->nIndex = 0;
570 pPrintJob->hHandle = hHandle;
571 gPrintJobsTable[pPrintJob->nIndex] = pPrintJob;
574 TRACE("return %04x\n", hHandle);
578 /**********************************************************************
582 INT16 WINAPI CloseJob16(HPJOB16 hJob)
585 PPRINTJOB pPrintJob = NULL;
587 TRACE("%04x\n", hJob);
589 pPrintJob = FindPrintJobFromHandle(hJob);
590 if (pPrintJob != NULL)
592 /* Close the spool file */
593 close(pPrintJob->fd);
600 /**********************************************************************
601 * WriteSpool (GDI.241)
604 INT16 WINAPI WriteSpool16(HPJOB16 hJob, LPSTR lpData, INT16 cch)
607 PPRINTJOB pPrintJob = NULL;
609 TRACE("%04x %08lx %04x\n", hJob, (DWORD)lpData, cch);
611 pPrintJob = FindPrintJobFromHandle(hJob);
612 if (pPrintJob != NULL && pPrintJob->fd >= 0 && cch)
614 if (write(pPrintJob->fd, lpData, cch) != cch)
619 /* FIXME: We just cannot call 16 bit functions from here, since we
620 * have acquired several locks (DC). And we do not really need to.
622 if (pPrintJob->hDC == 0) {
623 TRACE("hDC == 0 so no QueryAbort\n");
625 else if (!(QueryAbort16(pPrintJob->hDC, (nRet == SP_OUTOFDISK) ? nRet : 0 )))
627 CloseJob16(hJob); /* printing aborted */
635 typedef INT WINAPI (*MSGBOX_PROC)( HWND, LPCSTR, LPCSTR, UINT );
637 /**********************************************************************
638 * WriteDialog (GDI.242)
641 INT16 WINAPI WriteDialog16(HPJOB16 hJob, LPSTR lpMsg, INT16 cchMsg)
644 MSGBOX_PROC pMessageBoxA;
647 TRACE("%04x %04x '%s'\n", hJob, cchMsg, lpMsg);
649 if ((mod = GetModuleHandleA("user32.dll")))
651 if ((pMessageBoxA = (MSGBOX_PROC)GetProcAddress( mod, "MessageBoxA" )))
652 ret = pMessageBoxA(0, lpMsg, "Printing Error", MB_OKCANCEL);
658 /**********************************************************************
659 * DeleteJob (GDI.244)
662 INT16 WINAPI DeleteJob16(HPJOB16 hJob, INT16 nNotUsed)
666 TRACE("%04x\n", hJob);
668 nRet = FreePrintJob(hJob);
673 * The following two function would allow a page to be sent to the printer
674 * when it has been processed. For simplicity they havn't been implemented.
675 * This means a whole job has to be processed before it is sent to the printer.
678 /**********************************************************************
679 * StartSpoolPage (GDI.246)
682 INT16 WINAPI StartSpoolPage16(HPJOB16 hJob)
684 FIXME("StartSpoolPage GDI.246 unimplemented\n");
690 /**********************************************************************
691 * EndSpoolPage (GDI.247)
694 INT16 WINAPI EndSpoolPage16(HPJOB16 hJob)
696 FIXME("EndSpoolPage GDI.247 unimplemented\n");
701 /**********************************************************************
702 * GetSpoolJob (GDI.245)
705 DWORD WINAPI GetSpoolJob16(int nOption, LONG param)
708 TRACE("In GetSpoolJob param 0x%lx noption %d\n",param, nOption);
713 /******************************************************************
714 * DrvGetPrinterDataInternal
716 * Helper for DrvGetPrinterData
718 static DWORD DrvGetPrinterDataInternal(LPSTR RegStr_Printer,
719 LPBYTE lpPrinterData, int cbData, int what)
723 DWORD dwType, cbQueryData;
725 if (!(RegOpenKeyA(HKEY_LOCAL_MACHINE, RegStr_Printer, &hkey))) {
726 if (what == INT_PD_DEFAULT_DEVMODE) { /* "Default DevMode" */
727 if (!(RegQueryValueExA(hkey, DefaultDevMode, 0, &dwType, 0, &cbQueryData))) {
730 else if ((cbQueryData) && (cbQueryData <= cbData)) {
731 cbQueryData = cbData;
732 if (RegQueryValueExA(hkey, DefaultDevMode, 0,
733 &dwType, lpPrinterData, &cbQueryData))
737 } else { /* "Printer Driver" */
739 RegQueryValueExA(hkey, "Printer Driver", 0,
740 &dwType, lpPrinterData, &cbQueryData);
744 if (hkey) RegCloseKey(hkey);
748 /******************************************************************
749 * DrvGetPrinterData [GDI.282]
752 DWORD WINAPI DrvGetPrinterData16(LPSTR lpPrinter, LPSTR lpProfile,
753 LPDWORD lpType, LPBYTE lpPrinterData,
754 int cbData, LPDWORD lpNeeded)
756 LPSTR RegStr_Printer;
757 HKEY hkey = 0, hkey2 = 0;
759 DWORD dwType, PrinterAttr, cbPrinterAttr, SetData, size;
761 if (HIWORD(lpPrinter))
762 TRACE("printer %s\n",lpPrinter);
764 TRACE("printer %p\n",lpPrinter);
765 if (HIWORD(lpProfile))
766 TRACE("profile %s\n",lpProfile);
768 TRACE("profile %p\n",lpProfile);
769 TRACE("lpType %p\n",lpType);
771 if ((!lpPrinter) || (!lpProfile) || (!lpNeeded))
772 return ERROR_INVALID_PARAMETER;
774 RegStr_Printer = HeapAlloc(GetProcessHeap(), 0,
775 strlen(Printers) + strlen(lpPrinter) + 2);
776 strcpy(RegStr_Printer, Printers);
777 strcat(RegStr_Printer, lpPrinter);
779 if (((DWORD)lpProfile == INT_PD_DEFAULT_DEVMODE) || (HIWORD(lpProfile) &&
780 (!strcmp(lpProfile, DefaultDevMode)))) {
781 size = DrvGetPrinterDataInternal(RegStr_Printer, lpPrinterData, cbData,
782 INT_PD_DEFAULT_DEVMODE);
785 if ((lpPrinterData) && (*lpNeeded > cbData))
786 res = ERROR_MORE_DATA;
788 else res = ERROR_INVALID_PRINTER_NAME;
791 if (((DWORD)lpProfile == INT_PD_DEFAULT_MODEL) || (HIWORD(lpProfile) &&
792 (!strcmp(lpProfile, PrinterModel)))) {
794 if (!lpPrinterData) goto failed;
796 res = ERROR_MORE_DATA;
799 size = DrvGetPrinterDataInternal(RegStr_Printer, lpPrinterData, cbData,
800 INT_PD_DEFAULT_MODEL);
801 if ((size+1) && (lpType))
804 res = ERROR_INVALID_PRINTER_NAME;
808 if ((res = RegOpenKeyA(HKEY_LOCAL_MACHINE, RegStr_Printer, &hkey)))
811 if ((res = RegQueryValueExA(hkey, "Attributes", 0,
812 &dwType, (LPBYTE)&PrinterAttr, &cbPrinterAttr)))
814 if ((res = RegOpenKeyA(hkey, PrinterDriverData, &hkey2)))
817 res = RegQueryValueExA(hkey2, lpProfile, 0,
818 lpType, lpPrinterData, lpNeeded);
819 if ((res != ERROR_CANTREAD) &&
821 (PRINTER_ATTRIBUTE_ENABLE_BIDI|PRINTER_ATTRIBUTE_NETWORK))
822 == PRINTER_ATTRIBUTE_NETWORK))
824 if (!(res) && (*lpType == REG_DWORD) && (*(LPDWORD)lpPrinterData == -1))
825 res = ERROR_INVALID_DATA;
830 RegSetValueExA(hkey2, lpProfile, 0, REG_DWORD, (LPBYTE)&SetData, 4); /* no result returned */
835 if (hkey2) RegCloseKey(hkey2);
836 if (hkey) RegCloseKey(hkey);
837 HeapFree(GetProcessHeap(), 0, RegStr_Printer);
842 /******************************************************************
843 * DrvSetPrinterData [GDI.281]
846 DWORD WINAPI DrvSetPrinterData16(LPSTR lpPrinter, LPSTR lpProfile,
847 DWORD lpType, LPBYTE lpPrinterData,
850 LPSTR RegStr_Printer;
854 if (HIWORD(lpPrinter))
855 TRACE("printer %s\n",lpPrinter);
857 TRACE("printer %p\n",lpPrinter);
858 if (HIWORD(lpProfile))
859 TRACE("profile %s\n",lpProfile);
861 TRACE("profile %p\n",lpProfile);
862 TRACE("lpType %08lx\n",lpType);
864 if ((!lpPrinter) || (!lpProfile) ||
865 ((DWORD)lpProfile == INT_PD_DEFAULT_MODEL) || (HIWORD(lpProfile) &&
866 (!strcmp(lpProfile, PrinterModel))))
867 return ERROR_INVALID_PARAMETER;
869 RegStr_Printer = HeapAlloc(GetProcessHeap(), 0,
870 strlen(Printers) + strlen(lpPrinter) + 2);
871 strcpy(RegStr_Printer, Printers);
872 strcat(RegStr_Printer, lpPrinter);
874 if (((DWORD)lpProfile == INT_PD_DEFAULT_DEVMODE) || (HIWORD(lpProfile) &&
875 (!strcmp(lpProfile, DefaultDevMode)))) {
876 if ( RegOpenKeyA(HKEY_LOCAL_MACHINE, RegStr_Printer, &hkey)
878 RegSetValueExA(hkey, DefaultDevMode, 0, REG_BINARY,
879 lpPrinterData, dwSize) != ERROR_SUCCESS )
880 res = ERROR_INVALID_PRINTER_NAME;
884 strcat(RegStr_Printer, "\\");
886 if( (res = RegOpenKeyA(HKEY_LOCAL_MACHINE, RegStr_Printer, &hkey)) ==
890 res = RegDeleteValueA(hkey, lpProfile);
892 res = RegSetValueExA(hkey, lpProfile, 0, lpType,
893 lpPrinterData, dwSize);
897 if (hkey) RegCloseKey(hkey);
898 HeapFree(GetProcessHeap(), 0, RegStr_Printer);