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
17 #include "wine/winbase16.h"
18 #include "wine/wingdi16.h"
22 #include "debugtools.h"
29 DEFAULT_DEBUG_CHANNEL(print);
31 static char PrinterModel[] = "Printer Model";
32 static char DefaultDevMode[] = "Default DevMode";
33 static char PrinterDriverData[] = "PrinterDriverData";
34 static char Printers[] = "System\\CurrentControlSet\\Control\\Print\\Printers\\";
36 /******************************************************************
37 * StartDoc16 [GDI.377]
40 INT16 WINAPI StartDoc16( HDC16 hdc, const DOCINFO16 *lpdoc )
44 docA.cbSize = lpdoc->cbSize;
45 docA.lpszDocName = MapSL(lpdoc->lpszDocName);
46 docA.lpszOutput = MapSL(lpdoc->lpszOutput);
48 if(lpdoc->cbSize >= 14)
49 docA.lpszDatatype = MapSL(lpdoc->lpszDatatype);
51 docA.lpszDatatype = NULL;
53 if(lpdoc->cbSize >= 18)
54 docA.fwType = lpdoc->fwType;
58 return StartDocA(hdc, &docA);
61 /******************************************************************
62 * StartDocA [GDI32.347]
64 * StartDoc calls the STARTDOC Escape with the input data pointing to DocName
65 * and the output data (which is used as a second input parameter).pointing at
66 * the whole docinfo structure. This seems to be an undocumented feature of
67 * the STARTDOC Escape.
69 INT WINAPI StartDocA(HDC hdc, const DOCINFOA* doc)
72 DC *dc = DC_GetDCPtr( hdc );
74 TRACE("DocName = '%s' Output = '%s' Datatype = '%s'\n",
75 doc->lpszDocName, doc->lpszOutput, doc->lpszDatatype);
77 if(!dc) return SP_ERROR;
79 if(dc->funcs->pStartDoc)
80 ret = dc->funcs->pStartDoc( dc, doc );
82 ret = Escape(hdc, STARTDOC, strlen(doc->lpszDocName),
83 doc->lpszDocName, (LPVOID)doc);
84 GDI_ReleaseObj( hdc );
88 /*************************************************************************
89 * StartDocW [GDI32.348]
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)
138 ret = dc->funcs->pEndDoc( dc );
140 ret = Escape(hdc, ENDDOC, 0, 0, 0);
141 GDI_ReleaseObj( hdc );
145 /******************************************************************
146 * StartPage16 [GDI.379]
149 INT16 WINAPI StartPage16(HDC16 hdc)
151 return StartPage(hdc);
154 /******************************************************************
155 * StartPage [GDI32.349]
158 INT WINAPI StartPage(HDC hdc)
161 DC *dc = DC_GetDCPtr( hdc );
162 if(!dc) return SP_ERROR;
164 if(dc->funcs->pStartPage)
165 ret = dc->funcs->pStartPage( dc );
168 GDI_ReleaseObj( hdc );
172 /******************************************************************
173 * EndPage16 [GDI.380]
176 INT16 WINAPI EndPage16( HDC16 hdc )
181 /******************************************************************
185 INT WINAPI EndPage(HDC hdc)
188 DC *dc = DC_GetDCPtr( hdc );
189 if(!dc) return SP_ERROR;
191 if(dc->funcs->pEndPage)
192 ret = dc->funcs->pEndPage( dc );
194 ret = Escape(hdc, NEWFRAME, 0, 0, 0);
195 GDI_ReleaseObj( hdc );
199 /******************************************************************************
200 * AbortDoc16 [GDI.382]
202 INT16 WINAPI AbortDoc16(HDC16 hdc)
204 return AbortDoc(hdc);
207 /******************************************************************************
208 * AbortDoc [GDI32.105]
210 INT WINAPI AbortDoc(HDC hdc)
213 DC *dc = DC_GetDCPtr( hdc );
214 if(!dc) return SP_ERROR;
216 if(dc->funcs->pAbortDoc)
217 ret = dc->funcs->pAbortDoc( dc );
219 ret = Escape(hdc, ABORTDOC, 0, 0, 0);
220 GDI_ReleaseObj( hdc );
224 /**********************************************************************
225 * QueryAbort16 (GDI.155)
227 * Calls the app's AbortProc function if avail.
230 * TRUE if no AbortProc avail or AbortProc wants to continue printing.
231 * FALSE if AbortProc wants to abort printing.
233 BOOL16 WINAPI QueryAbort16(HDC16 hdc, INT16 reserved)
236 DC *dc = DC_GetDCPtr( hdc );
239 ERR("Invalid hdc %04x\n", hdc);
243 if (dc->pAbortProc) ret = dc->pAbortProc(hdc, 0);
244 GDI_ReleaseObj( hdc );
248 /* ### start build ### */
249 extern WORD CALLBACK PRTDRV_CallTo16_word_ww(FARPROC16,WORD,WORD);
250 /* ### stop build ### */
252 /**********************************************************************
253 * SetAbortProc16 (GDI.381)
256 INT16 WINAPI SetAbortProc16(HDC16 hdc, SEGPTR abrtprc)
258 ABORTPROC proc32 = (ABORTPROC)THUNK_Alloc((FARPROC16)abrtprc,
259 (RELAY)PRTDRV_CallTo16_word_ww);
260 return SetAbortProc(hdc, proc32);
263 /**********************************************************************
264 * SetAbortProc (GDI32.301)
267 INT WINAPI SetAbortProc(HDC hdc, ABORTPROC abrtprc)
269 DC *dc = DC_GetDCPtr( hdc );
271 if(dc->pAbortProc) THUNK_Free((FARPROC)dc->pAbortProc);
272 dc->pAbortProc = abrtprc;
273 GDI_ReleaseObj( hdc );
278 /****************** misc. printer related functions */
281 * The following function should implement a queing system
290 static struct hpq *hpqueue;
292 /**********************************************************************
296 HPQ16 WINAPI CreatePQ16(INT16 size)
303 tmp_size = size << 2;
304 if (!(hpq = GlobalAlloc16(GMEM_SHARE|GMEM_MOVEABLE, tmp_size + 8)))
306 pPQ = GlobalLock16(hpq);
315 FIXME("(%d): stub\n",size);
320 /**********************************************************************
324 INT16 WINAPI DeletePQ16(HPQ16 hPQ)
326 return GlobalFree16((HGLOBAL16)hPQ);
329 /**********************************************************************
330 * ExtractPQ (GDI.232)
333 INT16 WINAPI ExtractPQ16(HPQ16 hPQ)
335 struct hpq *queue, *prev, *current, *currentPrev;
336 int key = 0, tag = -1;
337 currentPrev = prev = NULL;
338 queue = current = hpqueue;
344 currentPrev = current;
345 current = current->next;
348 if (current->key < key)
360 prev->next = queue->next;
362 hpqueue = queue->next;
363 HeapFree(GetProcessHeap(), 0, queue);
366 TRACE("%x got tag %d key %d\n", hPQ, tag, key);
371 /**********************************************************************
375 INT16 WINAPI InsertPQ16(HPQ16 hPQ, INT16 tag, INT16 key)
377 struct hpq *queueItem = HeapAlloc(GetProcessHeap(), 0, sizeof(struct hpq));
378 if(queueItem == NULL) {
379 ERR("Memory exausted!");
382 queueItem->next = hpqueue;
384 queueItem->key = key;
385 queueItem->tag = tag;
387 FIXME("(%x %d %d): stub???\n", hPQ, tag, key);
391 /**********************************************************************
395 INT16 WINAPI MinPQ16(HPQ16 hPQ)
397 FIXME("(%x): stub\n", hPQ);
401 /**********************************************************************
405 INT16 WINAPI SizePQ16(HPQ16 hPQ, INT16 sizechange)
407 FIXME("(%x %d): stub\n", hPQ, sizechange);
414 * The following functions implement part of the spooling process to
415 * print manager. I would like to see wine have a version of print managers
416 * that used LPR/LPD. For simplicity print jobs will be sent to a file for
419 typedef struct PRINTJOB
427 } PRINTJOB, *PPRINTJOB;
429 #define MAX_PRINT_JOBS 1
432 PPRINTJOB gPrintJobsTable[MAX_PRINT_JOBS];
435 static PPRINTJOB FindPrintJobFromHandle(HANDLE16 hHandle)
437 return gPrintJobsTable[0];
440 static int CreateSpoolFile(LPCSTR pszOutput)
444 char *psCmdP = psCmd;
446 /* TTD convert the 'output device' into a spool file name */
448 if (pszOutput == NULL || *pszOutput == '\0')
451 PROFILE_GetWineIniString( "spooler", pszOutput, "", psCmd, sizeof(psCmd) );
452 TRACE("Got printerSpoolCommand '%s' for output device '%s'\n",
455 psCmdP = (char *)pszOutput;
458 while (*psCmdP && isspace(*psCmdP))
474 TRACE("In child need to exec %s\n",psCmdP);
484 TRACE("Need to execute a cmnd and pipe the output to it\n");
488 DOS_FULL_NAME fullName;
490 TRACE("Just assume it's a file\n");
493 * The file name can be dos based, we have to find its
494 * Unix correspondant file name
496 DOSFS_GetFullName(psCmdP, FALSE, &fullName);
498 if ((fd = open(fullName.long_name, O_CREAT | O_TRUNC | O_WRONLY , 0600)) < 0)
500 ERR("Failed to create spool file %s (%s)\n",
501 fullName.long_name, strerror(errno));
507 static int FreePrintJob(HANDLE16 hJob)
512 pPrintJob = FindPrintJobFromHandle(hJob);
513 if (pPrintJob != NULL)
515 gPrintJobsTable[pPrintJob->nIndex] = NULL;
516 HeapFree(GetProcessHeap(), 0, pPrintJob->pszOutput);
517 HeapFree(GetProcessHeap(), 0, pPrintJob->pszTitle);
518 if (pPrintJob->fd >= 0) close(pPrintJob->fd);
519 HeapFree(GetProcessHeap(), 0, pPrintJob);
525 /**********************************************************************
529 HPJOB16 WINAPI OpenJob16(LPCSTR lpOutput, LPCSTR lpTitle, HDC16 hDC)
531 HPJOB16 hHandle = (HPJOB16)SP_ERROR;
534 TRACE("'%s' '%s' %04x\n", lpOutput, lpTitle, hDC);
536 pPrintJob = gPrintJobsTable[0];
537 if (pPrintJob == NULL)
541 /* Try an create a spool file */
542 fd = CreateSpoolFile(lpOutput);
545 pPrintJob = HeapAlloc(GetProcessHeap(), 0, sizeof(PRINTJOB));
546 if(pPrintJob == NULL) {
547 WARN("Memory exausted!");
553 pPrintJob->pszOutput = HEAP_strdupA(GetProcessHeap(), 0, lpOutput);
555 pPrintJob->pszTitle = HEAP_strdupA(GetProcessHeap(), 0, lpTitle);
556 pPrintJob->hDC = hDC;
558 pPrintJob->nIndex = 0;
559 pPrintJob->hHandle = hHandle;
560 gPrintJobsTable[pPrintJob->nIndex] = pPrintJob;
563 TRACE("return %04x\n", hHandle);
567 /**********************************************************************
571 INT16 WINAPI CloseJob16(HPJOB16 hJob)
574 PPRINTJOB pPrintJob = NULL;
576 TRACE("%04x\n", hJob);
578 pPrintJob = FindPrintJobFromHandle(hJob);
579 if (pPrintJob != NULL)
581 /* Close the spool file */
582 close(pPrintJob->fd);
589 /**********************************************************************
590 * WriteSpool (GDI.241)
593 INT16 WINAPI WriteSpool16(HPJOB16 hJob, LPSTR lpData, INT16 cch)
596 PPRINTJOB pPrintJob = NULL;
598 TRACE("%04x %08lx %04x\n", hJob, (DWORD)lpData, cch);
600 pPrintJob = FindPrintJobFromHandle(hJob);
601 if (pPrintJob != NULL && pPrintJob->fd >= 0 && cch)
603 if (write(pPrintJob->fd, lpData, cch) != cch)
607 if (pPrintJob->hDC == 0) {
608 TRACE("hDC == 0 so no QueryAbort\n");
610 else if (!(QueryAbort16(pPrintJob->hDC, (nRet == SP_OUTOFDISK) ? nRet : 0 )))
612 CloseJob16(hJob); /* printing aborted */
619 typedef INT WINAPI (*MSGBOX_PROC)( HWND, LPCSTR, LPCSTR, UINT );
621 /**********************************************************************
622 * WriteDialog (GDI.242)
625 INT16 WINAPI WriteDialog16(HPJOB16 hJob, LPSTR lpMsg, INT16 cchMsg)
628 MSGBOX_PROC pMessageBoxA;
631 TRACE("%04x %04x '%s'\n", hJob, cchMsg, lpMsg);
633 if ((mod = GetModuleHandleA("user32.dll")))
635 if ((pMessageBoxA = (MSGBOX_PROC)GetProcAddress( mod, "MessageBoxA" )))
636 ret = pMessageBoxA(0, lpMsg, "Printing Error", MB_OKCANCEL);
642 /**********************************************************************
643 * DeleteJob (GDI.244)
646 INT16 WINAPI DeleteJob16(HPJOB16 hJob, INT16 nNotUsed)
650 TRACE("%04x\n", hJob);
652 nRet = FreePrintJob(hJob);
657 * The following two function would allow a page to be sent to the printer
658 * when it has been processed. For simplicity they havn't been implemented.
659 * This means a whole job has to be processed before it is sent to the printer.
662 /**********************************************************************
663 * StartSpoolPage (GDI.246)
666 INT16 WINAPI StartSpoolPage16(HPJOB16 hJob)
668 FIXME("StartSpoolPage GDI.246 unimplemented\n");
674 /**********************************************************************
675 * EndSpoolPage (GDI.247)
678 INT16 WINAPI EndSpoolPage16(HPJOB16 hJob)
680 FIXME("EndSpoolPage GDI.247 unimplemented\n");
685 /**********************************************************************
686 * GetSpoolJob (GDI.245)
689 DWORD WINAPI GetSpoolJob16(int nOption, LONG param)
692 TRACE("In GetSpoolJob param 0x%lx noption %d\n",param, nOption);
697 /******************************************************************
698 * DrvGetPrinterDataInternal
700 * Helper for DrvGetPrinterData
702 static DWORD DrvGetPrinterDataInternal(LPSTR RegStr_Printer,
703 LPBYTE lpPrinterData, int cbData, int what)
707 DWORD dwType, cbQueryData;
709 if (!(RegOpenKeyA(HKEY_LOCAL_MACHINE, RegStr_Printer, &hkey))) {
710 if (what == INT_PD_DEFAULT_DEVMODE) { /* "Default DevMode" */
711 if (!(RegQueryValueExA(hkey, DefaultDevMode, 0, &dwType, 0, &cbQueryData))) {
714 else if ((cbQueryData) && (cbQueryData <= cbData)) {
715 cbQueryData = cbData;
716 if (RegQueryValueExA(hkey, DefaultDevMode, 0,
717 &dwType, lpPrinterData, &cbQueryData))
721 } else { /* "Printer Driver" */
723 RegQueryValueExA(hkey, "Printer Driver", 0,
724 &dwType, lpPrinterData, &cbQueryData);
728 if (hkey) RegCloseKey(hkey);
732 /******************************************************************
733 * DrvGetPrinterData [GDI.282]
736 DWORD WINAPI DrvGetPrinterData16(LPSTR lpPrinter, LPSTR lpProfile,
737 LPDWORD lpType, LPBYTE lpPrinterData,
738 int cbData, LPDWORD lpNeeded)
740 LPSTR RegStr_Printer;
741 HKEY hkey = 0, hkey2 = 0;
743 DWORD dwType, PrinterAttr, cbPrinterAttr, SetData, size;
745 if (HIWORD(lpPrinter))
746 TRACE("printer %s\n",lpPrinter);
748 TRACE("printer %p\n",lpPrinter);
749 if (HIWORD(lpProfile))
750 TRACE("profile %s\n",lpProfile);
752 TRACE("profile %p\n",lpProfile);
753 TRACE("lpType %p\n",lpType);
755 if ((!lpPrinter) || (!lpProfile) || (!lpNeeded))
756 return ERROR_INVALID_PARAMETER;
758 RegStr_Printer = HeapAlloc(GetProcessHeap(), 0,
759 strlen(Printers) + strlen(lpPrinter) + 2);
760 strcpy(RegStr_Printer, Printers);
761 strcat(RegStr_Printer, lpPrinter);
763 if (((DWORD)lpProfile == INT_PD_DEFAULT_DEVMODE) || (HIWORD(lpProfile) &&
764 (!strcmp(lpProfile, DefaultDevMode)))) {
765 size = DrvGetPrinterDataInternal(RegStr_Printer, lpPrinterData, cbData,
766 INT_PD_DEFAULT_DEVMODE);
769 if ((lpPrinterData) && (*lpNeeded > cbData))
770 res = ERROR_MORE_DATA;
772 else res = ERROR_INVALID_PRINTER_NAME;
775 if (((DWORD)lpProfile == INT_PD_DEFAULT_MODEL) || (HIWORD(lpProfile) &&
776 (!strcmp(lpProfile, PrinterModel)))) {
778 if (!lpPrinterData) goto failed;
780 res = ERROR_MORE_DATA;
783 size = DrvGetPrinterDataInternal(RegStr_Printer, lpPrinterData, cbData,
784 INT_PD_DEFAULT_MODEL);
785 if ((size+1) && (lpType))
788 res = ERROR_INVALID_PRINTER_NAME;
792 if ((res = RegOpenKeyA(HKEY_LOCAL_MACHINE, RegStr_Printer, &hkey)))
795 if ((res = RegQueryValueExA(hkey, "Attributes", 0,
796 &dwType, (LPBYTE)&PrinterAttr, &cbPrinterAttr)))
798 if ((res = RegOpenKeyA(hkey, PrinterDriverData, &hkey2)))
801 res = RegQueryValueExA(hkey2, lpProfile, 0,
802 lpType, lpPrinterData, lpNeeded);
803 if ((res != ERROR_CANTREAD) &&
805 (PRINTER_ATTRIBUTE_ENABLE_BIDI|PRINTER_ATTRIBUTE_NETWORK))
806 == PRINTER_ATTRIBUTE_NETWORK))
808 if (!(res) && (*lpType == REG_DWORD) && (*(LPDWORD)lpPrinterData == -1))
809 res = ERROR_INVALID_DATA;
814 RegSetValueExA(hkey2, lpProfile, 0, REG_DWORD, (LPBYTE)&SetData, 4); /* no result returned */
819 if (hkey2) RegCloseKey(hkey2);
820 if (hkey) RegCloseKey(hkey);
821 HeapFree(GetProcessHeap(), 0, RegStr_Printer);
826 /******************************************************************
827 * DrvSetPrinterData [GDI.281]
830 DWORD WINAPI DrvSetPrinterData16(LPSTR lpPrinter, LPSTR lpProfile,
831 DWORD lpType, LPBYTE lpPrinterData,
834 LPSTR RegStr_Printer;
838 if (HIWORD(lpPrinter))
839 TRACE("printer %s\n",lpPrinter);
841 TRACE("printer %p\n",lpPrinter);
842 if (HIWORD(lpProfile))
843 TRACE("profile %s\n",lpProfile);
845 TRACE("profile %p\n",lpProfile);
846 TRACE("lpType %08lx\n",lpType);
848 if ((!lpPrinter) || (!lpProfile) ||
849 ((DWORD)lpProfile == INT_PD_DEFAULT_MODEL) || (HIWORD(lpProfile) &&
850 (!strcmp(lpProfile, PrinterModel))))
851 return ERROR_INVALID_PARAMETER;
853 RegStr_Printer = HeapAlloc(GetProcessHeap(), 0,
854 strlen(Printers) + strlen(lpPrinter) + 2);
855 strcpy(RegStr_Printer, Printers);
856 strcat(RegStr_Printer, lpPrinter);
858 if (((DWORD)lpProfile == INT_PD_DEFAULT_DEVMODE) || (HIWORD(lpProfile) &&
859 (!strcmp(lpProfile, DefaultDevMode)))) {
860 if ( RegOpenKeyA(HKEY_LOCAL_MACHINE, RegStr_Printer, &hkey)
862 RegSetValueExA(hkey, DefaultDevMode, 0, REG_BINARY,
863 lpPrinterData, dwSize) != ERROR_SUCCESS )
864 res = ERROR_INVALID_PRINTER_NAME;
868 strcat(RegStr_Printer, "\\");
870 if( (res = RegOpenKeyA(HKEY_LOCAL_MACHINE, RegStr_Printer, &hkey)) ==
874 res = RegDeleteValueA(hkey, lpProfile);
876 res = RegSetValueExA(hkey, lpProfile, 0, lpType,
877 lpPrinterData, dwSize);
881 if (hkey) RegCloseKey(hkey);
882 HeapFree(GetProcessHeap(), 0, RegStr_Printer);