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"
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->pAbortProc) ret = dc->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->pAbortProc) THUNK_Free((FARPROC)dc->pAbortProc);
273 dc->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 it's 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 (%s)\n",
502 fullName.long_name, strerror(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 typedef INT WINAPI (*MSGBOX_PROC)( HWND, LPCSTR, LPCSTR, UINT );
622 /**********************************************************************
623 * WriteDialog (GDI.242)
626 INT16 WINAPI WriteDialog16(HPJOB16 hJob, LPSTR lpMsg, INT16 cchMsg)
629 MSGBOX_PROC pMessageBoxA;
632 TRACE("%04x %04x '%s'\n", hJob, cchMsg, lpMsg);
634 if ((mod = GetModuleHandleA("user32.dll")))
636 if ((pMessageBoxA = (MSGBOX_PROC)GetProcAddress( mod, "MessageBoxA" )))
637 ret = pMessageBoxA(0, lpMsg, "Printing Error", MB_OKCANCEL);
643 /**********************************************************************
644 * DeleteJob (GDI.244)
647 INT16 WINAPI DeleteJob16(HPJOB16 hJob, INT16 nNotUsed)
651 TRACE("%04x\n", hJob);
653 nRet = FreePrintJob(hJob);
658 * The following two function would allow a page to be sent to the printer
659 * when it has been processed. For simplicity they havn't been implemented.
660 * This means a whole job has to be processed before it is sent to the printer.
663 /**********************************************************************
664 * StartSpoolPage (GDI.246)
667 INT16 WINAPI StartSpoolPage16(HPJOB16 hJob)
669 FIXME("StartSpoolPage GDI.246 unimplemented\n");
675 /**********************************************************************
676 * EndSpoolPage (GDI.247)
679 INT16 WINAPI EndSpoolPage16(HPJOB16 hJob)
681 FIXME("EndSpoolPage GDI.247 unimplemented\n");
686 /**********************************************************************
687 * GetSpoolJob (GDI.245)
690 DWORD WINAPI GetSpoolJob16(int nOption, LONG param)
693 TRACE("In GetSpoolJob param 0x%lx noption %d\n",param, nOption);
698 /******************************************************************
699 * DrvGetPrinterDataInternal
701 * Helper for DrvGetPrinterData
703 static DWORD DrvGetPrinterDataInternal(LPSTR RegStr_Printer,
704 LPBYTE lpPrinterData, int cbData, int what)
708 DWORD dwType, cbQueryData;
710 if (!(RegOpenKeyA(HKEY_LOCAL_MACHINE, RegStr_Printer, &hkey))) {
711 if (what == INT_PD_DEFAULT_DEVMODE) { /* "Default DevMode" */
712 if (!(RegQueryValueExA(hkey, DefaultDevMode, 0, &dwType, 0, &cbQueryData))) {
715 else if ((cbQueryData) && (cbQueryData <= cbData)) {
716 cbQueryData = cbData;
717 if (RegQueryValueExA(hkey, DefaultDevMode, 0,
718 &dwType, lpPrinterData, &cbQueryData))
722 } else { /* "Printer Driver" */
724 RegQueryValueExA(hkey, "Printer Driver", 0,
725 &dwType, lpPrinterData, &cbQueryData);
729 if (hkey) RegCloseKey(hkey);
733 /******************************************************************
734 * DrvGetPrinterData [GDI.282]
737 DWORD WINAPI DrvGetPrinterData16(LPSTR lpPrinter, LPSTR lpProfile,
738 LPDWORD lpType, LPBYTE lpPrinterData,
739 int cbData, LPDWORD lpNeeded)
741 LPSTR RegStr_Printer;
742 HKEY hkey = 0, hkey2 = 0;
744 DWORD dwType, PrinterAttr, cbPrinterAttr, SetData, size;
746 if (HIWORD(lpPrinter))
747 TRACE("printer %s\n",lpPrinter);
749 TRACE("printer %p\n",lpPrinter);
750 if (HIWORD(lpProfile))
751 TRACE("profile %s\n",lpProfile);
753 TRACE("profile %p\n",lpProfile);
754 TRACE("lpType %p\n",lpType);
756 if ((!lpPrinter) || (!lpProfile) || (!lpNeeded))
757 return ERROR_INVALID_PARAMETER;
759 RegStr_Printer = HeapAlloc(GetProcessHeap(), 0,
760 strlen(Printers) + strlen(lpPrinter) + 2);
761 strcpy(RegStr_Printer, Printers);
762 strcat(RegStr_Printer, lpPrinter);
764 if (((DWORD)lpProfile == INT_PD_DEFAULT_DEVMODE) || (HIWORD(lpProfile) &&
765 (!strcmp(lpProfile, DefaultDevMode)))) {
766 size = DrvGetPrinterDataInternal(RegStr_Printer, lpPrinterData, cbData,
767 INT_PD_DEFAULT_DEVMODE);
770 if ((lpPrinterData) && (*lpNeeded > cbData))
771 res = ERROR_MORE_DATA;
773 else res = ERROR_INVALID_PRINTER_NAME;
776 if (((DWORD)lpProfile == INT_PD_DEFAULT_MODEL) || (HIWORD(lpProfile) &&
777 (!strcmp(lpProfile, PrinterModel)))) {
779 if (!lpPrinterData) goto failed;
781 res = ERROR_MORE_DATA;
784 size = DrvGetPrinterDataInternal(RegStr_Printer, lpPrinterData, cbData,
785 INT_PD_DEFAULT_MODEL);
786 if ((size+1) && (lpType))
789 res = ERROR_INVALID_PRINTER_NAME;
793 if ((res = RegOpenKeyA(HKEY_LOCAL_MACHINE, RegStr_Printer, &hkey)))
796 if ((res = RegQueryValueExA(hkey, "Attributes", 0,
797 &dwType, (LPBYTE)&PrinterAttr, &cbPrinterAttr)))
799 if ((res = RegOpenKeyA(hkey, PrinterDriverData, &hkey2)))
802 res = RegQueryValueExA(hkey2, lpProfile, 0,
803 lpType, lpPrinterData, lpNeeded);
804 if ((res != ERROR_CANTREAD) &&
806 (PRINTER_ATTRIBUTE_ENABLE_BIDI|PRINTER_ATTRIBUTE_NETWORK))
807 == PRINTER_ATTRIBUTE_NETWORK))
809 if (!(res) && (*lpType == REG_DWORD) && (*(LPDWORD)lpPrinterData == -1))
810 res = ERROR_INVALID_DATA;
815 RegSetValueExA(hkey2, lpProfile, 0, REG_DWORD, (LPBYTE)&SetData, 4); /* no result returned */
820 if (hkey2) RegCloseKey(hkey2);
821 if (hkey) RegCloseKey(hkey);
822 HeapFree(GetProcessHeap(), 0, RegStr_Printer);
827 /******************************************************************
828 * DrvSetPrinterData [GDI.281]
831 DWORD WINAPI DrvSetPrinterData16(LPSTR lpPrinter, LPSTR lpProfile,
832 DWORD lpType, LPBYTE lpPrinterData,
835 LPSTR RegStr_Printer;
839 if (HIWORD(lpPrinter))
840 TRACE("printer %s\n",lpPrinter);
842 TRACE("printer %p\n",lpPrinter);
843 if (HIWORD(lpProfile))
844 TRACE("profile %s\n",lpProfile);
846 TRACE("profile %p\n",lpProfile);
847 TRACE("lpType %08lx\n",lpType);
849 if ((!lpPrinter) || (!lpProfile) ||
850 ((DWORD)lpProfile == INT_PD_DEFAULT_MODEL) || (HIWORD(lpProfile) &&
851 (!strcmp(lpProfile, PrinterModel))))
852 return ERROR_INVALID_PARAMETER;
854 RegStr_Printer = HeapAlloc(GetProcessHeap(), 0,
855 strlen(Printers) + strlen(lpPrinter) + 2);
856 strcpy(RegStr_Printer, Printers);
857 strcat(RegStr_Printer, lpPrinter);
859 if (((DWORD)lpProfile == INT_PD_DEFAULT_DEVMODE) || (HIWORD(lpProfile) &&
860 (!strcmp(lpProfile, DefaultDevMode)))) {
861 if ( RegOpenKeyA(HKEY_LOCAL_MACHINE, RegStr_Printer, &hkey)
863 RegSetValueExA(hkey, DefaultDevMode, 0, REG_BINARY,
864 lpPrinterData, dwSize) != ERROR_SUCCESS )
865 res = ERROR_INVALID_PRINTER_NAME;
869 strcat(RegStr_Printer, "\\");
871 if( (res = RegOpenKeyA(HKEY_LOCAL_MACHINE, RegStr_Printer, &hkey)) ==
875 res = RegDeleteValueA(hkey, lpProfile);
877 res = RegSetValueExA(hkey, lpProfile, 0, lpType,
878 lpPrinterData, dwSize);
882 if (hkey) RegCloseKey(hkey);
883 HeapFree(GetProcessHeap(), 0, RegStr_Printer);