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
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include "wine/port.h"
41 #include "wine/winbase16.h"
42 #include "wine/wingdi16.h"
46 #include "wine/debug.h"
51 WINE_DEFAULT_DEBUG_CHANNEL(print);
53 static char PrinterModel[] = "Printer Model";
54 static char DefaultDevMode[] = "Default DevMode";
55 static char PrinterDriverData[] = "PrinterDriverData";
56 static char Printers[] = "System\\CurrentControlSet\\Control\\Print\\Printers\\";
59 /******************************************************************
62 * StartDoc calls the STARTDOC Escape with the input data pointing to DocName
63 * and the output data (which is used as a second input parameter).pointing at
64 * the whole docinfo structure. This seems to be an undocumented feature of
65 * the STARTDOC Escape.
67 * Note: we now do it the other way, with the STARTDOC Escape calling StartDoc.
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) ret = dc->funcs->pStartDoc( dc->physDev, doc );
80 GDI_ReleaseObj( hdc );
84 /*************************************************************************
88 INT WINAPI StartDocW(HDC hdc, const DOCINFOW* doc)
93 docA.cbSize = doc->cbSize;
94 docA.lpszDocName = doc->lpszDocName ?
95 HEAP_strdupWtoA( GetProcessHeap(), 0, doc->lpszDocName ) : NULL;
96 docA.lpszOutput = doc->lpszOutput ?
97 HEAP_strdupWtoA( GetProcessHeap(), 0, doc->lpszOutput ) : NULL;
98 docA.lpszDatatype = doc->lpszDatatype ?
99 HEAP_strdupWtoA( GetProcessHeap(), 0, doc->lpszDatatype ) : NULL;
100 docA.fwType = doc->fwType;
102 ret = StartDocA(hdc, &docA);
105 HeapFree( GetProcessHeap(), 0, (LPSTR)docA.lpszDocName );
107 HeapFree( GetProcessHeap(), 0, (LPSTR)docA.lpszOutput );
108 if(docA.lpszDatatype)
109 HeapFree( GetProcessHeap(), 0, (LPSTR)docA.lpszDatatype );
115 /******************************************************************
119 INT WINAPI EndDoc(HDC hdc)
122 DC *dc = DC_GetDCPtr( hdc );
123 if(!dc) return SP_ERROR;
125 if (dc->funcs->pEndDoc) ret = dc->funcs->pEndDoc( dc->physDev );
126 GDI_ReleaseObj( hdc );
131 /******************************************************************
132 * StartPage [GDI32.@]
135 INT WINAPI StartPage(HDC hdc)
138 DC *dc = DC_GetDCPtr( hdc );
139 if(!dc) return SP_ERROR;
141 if(dc->funcs->pStartPage)
142 ret = dc->funcs->pStartPage( dc->physDev );
145 GDI_ReleaseObj( hdc );
150 /******************************************************************
154 INT WINAPI EndPage(HDC hdc)
157 DC *dc = DC_GetDCPtr( hdc );
158 if(!dc) return SP_ERROR;
160 if (dc->funcs->pEndPage) ret = dc->funcs->pEndPage( dc->physDev );
161 GDI_ReleaseObj( hdc );
162 if (!QueryAbort16( hdc, 0 ))
171 /******************************************************************************
174 INT WINAPI AbortDoc(HDC hdc)
177 DC *dc = DC_GetDCPtr( hdc );
178 if(!dc) return SP_ERROR;
180 if (dc->funcs->pAbortDoc) ret = dc->funcs->pAbortDoc( dc->physDev );
181 GDI_ReleaseObj( hdc );
185 /**********************************************************************
186 * QueryAbort (GDI.155)
188 * Calls the app's AbortProc function if avail.
191 * TRUE if no AbortProc avail or AbortProc wants to continue printing.
192 * FALSE if AbortProc wants to abort printing.
194 BOOL16 WINAPI QueryAbort16(HDC16 hdc, INT16 reserved)
197 DC *dc = DC_GetDCPtr( hdc );
201 ERR("Invalid hdc %04x\n", hdc);
205 abproc = dc->pAbortProc;
206 GDI_ReleaseObj( hdc );
209 ret = abproc(hdc, 0);
213 /* ### start build ### */
214 extern WORD CALLBACK PRTDRV_CallTo16_word_ww(FARPROC16,WORD,WORD);
215 /* ### stop build ### */
217 /**********************************************************************
220 static BOOL CALLBACK call_abort_proc16( HDC hdc, INT code )
223 DC *dc = DC_GetDCPtr( hdc );
225 if (!dc) return FALSE;
226 proc16 = dc->pAbortProc16;
227 GDI_ReleaseObj( hdc );
228 if (proc16) return PRTDRV_CallTo16_word_ww( (FARPROC16)proc16, hdc, code );
233 /**********************************************************************
234 * SetAbortProc (GDI.381)
236 INT16 WINAPI SetAbortProc16(HDC16 hdc, ABORTPROC16 abrtprc)
238 DC *dc = DC_GetDCPtr( hdc );
240 if (!dc) return FALSE;
241 dc->pAbortProc16 = abrtprc;
242 GDI_ReleaseObj( hdc );
243 return SetAbortProc( hdc, call_abort_proc16 );
246 /**********************************************************************
247 * SetAbortProc (GDI32.@)
250 INT WINAPI SetAbortProc(HDC hdc, ABORTPROC abrtprc)
252 DC *dc = DC_GetDCPtr( hdc );
254 if (!dc) return FALSE;
255 dc->pAbortProc = abrtprc;
256 GDI_ReleaseObj( hdc );
261 /****************** misc. printer related functions */
264 * The following function should implement a queing system
273 static struct hpq *hpqueue;
275 /**********************************************************************
279 HPQ16 WINAPI CreatePQ16(INT16 size)
286 tmp_size = size << 2;
287 if (!(hpq = GlobalAlloc16(GMEM_SHARE|GMEM_MOVEABLE, tmp_size + 8)))
289 pPQ = GlobalLock16(hpq);
298 FIXME("(%d): stub\n",size);
303 /**********************************************************************
307 INT16 WINAPI DeletePQ16(HPQ16 hPQ)
309 return GlobalFree16((HGLOBAL16)hPQ);
312 /**********************************************************************
313 * ExtractPQ (GDI.232)
316 INT16 WINAPI ExtractPQ16(HPQ16 hPQ)
318 struct hpq *queue, *prev, *current, *currentPrev;
319 int key = 0, tag = -1;
320 currentPrev = prev = NULL;
321 queue = current = hpqueue;
327 currentPrev = current;
328 current = current->next;
331 if (current->key < key)
343 prev->next = queue->next;
345 hpqueue = queue->next;
346 HeapFree(GetProcessHeap(), 0, queue);
349 TRACE("%x got tag %d key %d\n", hPQ, tag, key);
354 /**********************************************************************
358 INT16 WINAPI InsertPQ16(HPQ16 hPQ, INT16 tag, INT16 key)
360 struct hpq *queueItem = HeapAlloc(GetProcessHeap(), 0, sizeof(struct hpq));
361 if(queueItem == NULL) {
362 ERR("Memory exausted!\n");
365 queueItem->next = hpqueue;
367 queueItem->key = key;
368 queueItem->tag = tag;
370 FIXME("(%x %d %d): stub???\n", hPQ, tag, key);
374 /**********************************************************************
378 INT16 WINAPI MinPQ16(HPQ16 hPQ)
380 FIXME("(%x): stub\n", hPQ);
384 /**********************************************************************
388 INT16 WINAPI SizePQ16(HPQ16 hPQ, INT16 sizechange)
390 FIXME("(%x %d): stub\n", hPQ, sizechange);
397 * The following functions implement part of the spooling process to
398 * print manager. I would like to see wine have a version of print managers
399 * that used LPR/LPD. For simplicity print jobs will be sent to a file for
402 typedef struct PRINTJOB
410 } PRINTJOB, *PPRINTJOB;
412 #define MAX_PRINT_JOBS 1
415 PPRINTJOB gPrintJobsTable[MAX_PRINT_JOBS];
418 static PPRINTJOB FindPrintJobFromHandle(HANDLE16 hHandle)
420 return gPrintJobsTable[0];
423 static int CreateSpoolFile(LPCSTR pszOutput)
427 char *psCmdP = psCmd;
429 /* TTD convert the 'output device' into a spool file name */
431 if (pszOutput == NULL || *pszOutput == '\0')
434 if (!strncmp("LPR:",pszOutput,4))
435 sprintf(psCmd,"|lpr -P%s",pszOutput+4);
441 if(!RegOpenKeyA(HKEY_LOCAL_MACHINE, "Software\\Wine\\Wine\\Config\\spooler", &hkey))
443 DWORD type, count = sizeof(psCmd);
444 RegQueryValueExA(hkey, pszOutput, 0, &type, psCmd, &count);
448 TRACE("Got printerSpoolCommand '%s' for output device '%s'\n",
451 psCmdP = (char *)pszOutput;
454 while (*psCmdP && isspace(*psCmdP))
470 TRACE("In child need to exec %s\n",psCmdP);
480 TRACE("Need to execute a cmnd and pipe the output to it\n");
484 char buffer[MAX_PATH];
486 TRACE("Just assume it's a file\n");
489 * The file name can be dos based, we have to find its
490 * Unix correspondant file name
492 wine_get_unix_file_name(psCmdP, buffer, sizeof(buffer));
494 if ((fd = open(buffer, O_CREAT | O_TRUNC | O_WRONLY , 0600)) < 0)
496 ERR("Failed to create spool file '%s' ('%s'). (error %s)\n",
497 buffer, psCmdP, strerror(errno));
503 static int FreePrintJob(HANDLE16 hJob)
508 pPrintJob = FindPrintJobFromHandle(hJob);
509 if (pPrintJob != NULL)
511 gPrintJobsTable[pPrintJob->nIndex] = NULL;
512 HeapFree(GetProcessHeap(), 0, pPrintJob->pszOutput);
513 HeapFree(GetProcessHeap(), 0, pPrintJob->pszTitle);
514 if (pPrintJob->fd >= 0) close(pPrintJob->fd);
515 HeapFree(GetProcessHeap(), 0, pPrintJob);
521 /**********************************************************************
525 HPJOB16 WINAPI OpenJob16(LPCSTR lpOutput, LPCSTR lpTitle, HDC16 hDC)
527 HPJOB16 hHandle = (HPJOB16)SP_ERROR;
530 TRACE("'%s' '%s' %04x\n", lpOutput, lpTitle, hDC);
532 pPrintJob = gPrintJobsTable[0];
533 if (pPrintJob == NULL)
537 /* Try and create a spool file */
538 fd = CreateSpoolFile(lpOutput);
541 pPrintJob = HeapAlloc(GetProcessHeap(), 0, sizeof(PRINTJOB));
542 if(pPrintJob == NULL) {
543 WARN("Memory exausted!\n");
549 pPrintJob->pszOutput = HeapAlloc(GetProcessHeap(), 0, strlen(lpOutput)+1);
550 strcpy( pPrintJob->pszOutput, lpOutput );
553 pPrintJob->pszTitle = HeapAlloc(GetProcessHeap(), 0, strlen(lpTitle)+1);
554 strcpy( pPrintJob->pszTitle, 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)
608 /* FIXME: We just cannot call 16 bit functions from here, since we
609 * have acquired several locks (DC). And we do not really need to.
611 if (pPrintJob->hDC == 0) {
612 TRACE("hDC == 0 so no QueryAbort\n");
614 else if (!(QueryAbort16(pPrintJob->hDC, (nRet == SP_OUTOFDISK) ? nRet : 0 )))
616 CloseJob16(hJob); /* printing aborted */
624 typedef INT (WINAPI *MSGBOX_PROC)( HWND, LPCSTR, LPCSTR, UINT );
626 /**********************************************************************
627 * WriteDialog (GDI.242)
630 INT16 WINAPI WriteDialog16(HPJOB16 hJob, LPSTR lpMsg, INT16 cchMsg)
633 MSGBOX_PROC pMessageBoxA;
636 TRACE("%04x %04x '%s'\n", hJob, cchMsg, lpMsg);
638 if ((mod = GetModuleHandleA("user32.dll")))
640 if ((pMessageBoxA = (MSGBOX_PROC)GetProcAddress( mod, "MessageBoxA" )))
641 ret = pMessageBoxA(0, lpMsg, "Printing Error", MB_OKCANCEL);
647 /**********************************************************************
648 * DeleteJob (GDI.244)
651 INT16 WINAPI DeleteJob16(HPJOB16 hJob, INT16 nNotUsed)
655 TRACE("%04x\n", hJob);
657 nRet = FreePrintJob(hJob);
662 * The following two function would allow a page to be sent to the printer
663 * when it has been processed. For simplicity they havn't been implemented.
664 * This means a whole job has to be processed before it is sent to the printer.
667 /**********************************************************************
668 * StartSpoolPage (GDI.246)
671 INT16 WINAPI StartSpoolPage16(HPJOB16 hJob)
673 FIXME("StartSpoolPage GDI.246 unimplemented\n");
679 /**********************************************************************
680 * EndSpoolPage (GDI.247)
683 INT16 WINAPI EndSpoolPage16(HPJOB16 hJob)
685 FIXME("EndSpoolPage GDI.247 unimplemented\n");
690 /**********************************************************************
691 * GetSpoolJob (GDI.245)
694 DWORD WINAPI GetSpoolJob16(int nOption, LONG param)
697 TRACE("In GetSpoolJob param 0x%lx noption %d\n",param, nOption);
702 /******************************************************************
703 * DrvGetPrinterDataInternal
705 * Helper for DrvGetPrinterData
707 static DWORD DrvGetPrinterDataInternal(LPSTR RegStr_Printer,
708 LPBYTE lpPrinterData, int cbData, int what)
712 DWORD dwType, cbQueryData;
714 if (!(RegOpenKeyA(HKEY_LOCAL_MACHINE, RegStr_Printer, &hkey))) {
715 if (what == INT_PD_DEFAULT_DEVMODE) { /* "Default DevMode" */
716 if (!(RegQueryValueExA(hkey, DefaultDevMode, 0, &dwType, 0, &cbQueryData))) {
719 else if ((cbQueryData) && (cbQueryData <= cbData)) {
720 cbQueryData = cbData;
721 if (RegQueryValueExA(hkey, DefaultDevMode, 0,
722 &dwType, lpPrinterData, &cbQueryData))
726 } else { /* "Printer Driver" */
728 RegQueryValueExA(hkey, "Printer Driver", 0,
729 &dwType, lpPrinterData, &cbQueryData);
733 if (hkey) RegCloseKey(hkey);
737 /******************************************************************
738 * DrvGetPrinterData (GDI.282)
741 DWORD WINAPI DrvGetPrinterData16(LPSTR lpPrinter, LPSTR lpProfile,
742 LPDWORD lpType, LPBYTE lpPrinterData,
743 int cbData, LPDWORD lpNeeded)
745 LPSTR RegStr_Printer;
746 HKEY hkey = 0, hkey2 = 0;
748 DWORD dwType, PrinterAttr, cbPrinterAttr, SetData, size;
750 if (HIWORD(lpPrinter))
751 TRACE("printer %s\n",lpPrinter);
753 TRACE("printer %p\n",lpPrinter);
754 if (HIWORD(lpProfile))
755 TRACE("profile %s\n",lpProfile);
757 TRACE("profile %p\n",lpProfile);
758 TRACE("lpType %p\n",lpType);
760 if ((!lpPrinter) || (!lpProfile) || (!lpNeeded))
761 return ERROR_INVALID_PARAMETER;
763 RegStr_Printer = HeapAlloc(GetProcessHeap(), 0,
764 strlen(Printers) + strlen(lpPrinter) + 2);
765 strcpy(RegStr_Printer, Printers);
766 strcat(RegStr_Printer, lpPrinter);
768 if (((DWORD)lpProfile == INT_PD_DEFAULT_DEVMODE) || (HIWORD(lpProfile) &&
769 (!strcmp(lpProfile, DefaultDevMode)))) {
770 size = DrvGetPrinterDataInternal(RegStr_Printer, lpPrinterData, cbData,
771 INT_PD_DEFAULT_DEVMODE);
774 if ((lpPrinterData) && (*lpNeeded > cbData))
775 res = ERROR_MORE_DATA;
777 else res = ERROR_INVALID_PRINTER_NAME;
780 if (((DWORD)lpProfile == INT_PD_DEFAULT_MODEL) || (HIWORD(lpProfile) &&
781 (!strcmp(lpProfile, PrinterModel)))) {
783 if (!lpPrinterData) goto failed;
785 res = ERROR_MORE_DATA;
788 size = DrvGetPrinterDataInternal(RegStr_Printer, lpPrinterData, cbData,
789 INT_PD_DEFAULT_MODEL);
790 if ((size+1) && (lpType))
793 res = ERROR_INVALID_PRINTER_NAME;
797 if ((res = RegOpenKeyA(HKEY_LOCAL_MACHINE, RegStr_Printer, &hkey)))
800 if ((res = RegQueryValueExA(hkey, "Attributes", 0,
801 &dwType, (LPBYTE)&PrinterAttr, &cbPrinterAttr)))
803 if ((res = RegOpenKeyA(hkey, PrinterDriverData, &hkey2)))
806 res = RegQueryValueExA(hkey2, lpProfile, 0,
807 lpType, lpPrinterData, lpNeeded);
808 if ((res != ERROR_CANTREAD) &&
810 (PRINTER_ATTRIBUTE_ENABLE_BIDI|PRINTER_ATTRIBUTE_NETWORK))
811 == PRINTER_ATTRIBUTE_NETWORK))
813 if (!(res) && (*lpType == REG_DWORD) && (*(LPDWORD)lpPrinterData == -1))
814 res = ERROR_INVALID_DATA;
819 RegSetValueExA(hkey2, lpProfile, 0, REG_DWORD, (LPBYTE)&SetData, 4); /* no result returned */
824 if (hkey2) RegCloseKey(hkey2);
825 if (hkey) RegCloseKey(hkey);
826 HeapFree(GetProcessHeap(), 0, RegStr_Printer);
831 /******************************************************************
832 * DrvSetPrinterData (GDI.281)
835 DWORD WINAPI DrvSetPrinterData16(LPSTR lpPrinter, LPSTR lpProfile,
836 DWORD lpType, LPBYTE lpPrinterData,
839 LPSTR RegStr_Printer;
843 if (HIWORD(lpPrinter))
844 TRACE("printer %s\n",lpPrinter);
846 TRACE("printer %p\n",lpPrinter);
847 if (HIWORD(lpProfile))
848 TRACE("profile %s\n",lpProfile);
850 TRACE("profile %p\n",lpProfile);
851 TRACE("lpType %08lx\n",lpType);
853 if ((!lpPrinter) || (!lpProfile) ||
854 ((DWORD)lpProfile == INT_PD_DEFAULT_MODEL) || (HIWORD(lpProfile) &&
855 (!strcmp(lpProfile, PrinterModel))))
856 return ERROR_INVALID_PARAMETER;
858 RegStr_Printer = HeapAlloc(GetProcessHeap(), 0,
859 strlen(Printers) + strlen(lpPrinter) + 2);
860 strcpy(RegStr_Printer, Printers);
861 strcat(RegStr_Printer, lpPrinter);
863 if (((DWORD)lpProfile == INT_PD_DEFAULT_DEVMODE) || (HIWORD(lpProfile) &&
864 (!strcmp(lpProfile, DefaultDevMode)))) {
865 if ( RegOpenKeyA(HKEY_LOCAL_MACHINE, RegStr_Printer, &hkey)
867 RegSetValueExA(hkey, DefaultDevMode, 0, REG_BINARY,
868 lpPrinterData, dwSize) != ERROR_SUCCESS )
869 res = ERROR_INVALID_PRINTER_NAME;
873 strcat(RegStr_Printer, "\\");
875 if( (res = RegOpenKeyA(HKEY_LOCAL_MACHINE, RegStr_Printer, &hkey)) ==
879 res = RegDeleteValueA(hkey, lpProfile);
881 res = RegSetValueExA(hkey, lpProfile, 0, lpType,
882 lpPrinterData, dwSize);
886 if (hkey) RegCloseKey(hkey);
887 HeapFree(GetProcessHeap(), 0, RegStr_Printer);