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 /**********************************************************************
523 * OpenJob16 (GDI32.@)
526 HPJOB16 WINAPI OpenJob16(LPCSTR lpOutput, LPCSTR lpTitle, HDC16 hDC)
528 HPJOB16 hHandle = (HPJOB16)SP_ERROR;
531 TRACE("'%s' '%s' %04x\n", lpOutput, lpTitle, hDC);
533 pPrintJob = gPrintJobsTable[0];
534 if (pPrintJob == NULL)
538 /* Try and create a spool file */
539 fd = CreateSpoolFile(lpOutput);
542 pPrintJob = HeapAlloc(GetProcessHeap(), 0, sizeof(PRINTJOB));
543 if(pPrintJob == NULL) {
544 WARN("Memory exausted!\n");
550 pPrintJob->pszOutput = HeapAlloc(GetProcessHeap(), 0, strlen(lpOutput)+1);
551 strcpy( pPrintJob->pszOutput, lpOutput );
554 pPrintJob->pszTitle = HeapAlloc(GetProcessHeap(), 0, strlen(lpTitle)+1);
555 strcpy( pPrintJob->pszTitle, 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 /**********************************************************************
570 * CloseJob16 (GDI32.@)
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)
593 * WriteSpool16 (GDI32.@)
596 INT16 WINAPI WriteSpool16(HPJOB16 hJob, LPSTR lpData, INT16 cch)
599 PPRINTJOB pPrintJob = NULL;
601 TRACE("%04x %08lx %04x\n", hJob, (DWORD)lpData, cch);
603 pPrintJob = FindPrintJobFromHandle(hJob);
604 if (pPrintJob != NULL && pPrintJob->fd >= 0 && cch)
606 if (write(pPrintJob->fd, lpData, cch) != cch)
611 /* FIXME: We just cannot call 16 bit functions from here, since we
612 * have acquired several locks (DC). And we do not really need to.
614 if (pPrintJob->hDC == 0) {
615 TRACE("hDC == 0 so no QueryAbort\n");
617 else if (!(QueryAbort16(pPrintJob->hDC, (nRet == SP_OUTOFDISK) ? nRet : 0 )))
619 CloseJob16(hJob); /* printing aborted */
627 typedef INT (WINAPI *MSGBOX_PROC)( HWND, LPCSTR, LPCSTR, UINT );
629 /**********************************************************************
630 * WriteDialog (GDI.242)
633 INT16 WINAPI WriteDialog16(HPJOB16 hJob, LPSTR lpMsg, INT16 cchMsg)
636 MSGBOX_PROC pMessageBoxA;
639 TRACE("%04x %04x '%s'\n", hJob, cchMsg, lpMsg);
641 if ((mod = GetModuleHandleA("user32.dll")))
643 if ((pMessageBoxA = (MSGBOX_PROC)GetProcAddress( mod, "MessageBoxA" )))
644 ret = pMessageBoxA(0, lpMsg, "Printing Error", MB_OKCANCEL);
650 /**********************************************************************
651 * DeleteJob (GDI.244)
654 INT16 WINAPI DeleteJob16(HPJOB16 hJob, INT16 nNotUsed)
658 TRACE("%04x\n", hJob);
660 nRet = FreePrintJob(hJob);
665 * The following two function would allow a page to be sent to the printer
666 * when it has been processed. For simplicity they havn't been implemented.
667 * This means a whole job has to be processed before it is sent to the printer.
670 /**********************************************************************
671 * StartSpoolPage (GDI.246)
674 INT16 WINAPI StartSpoolPage16(HPJOB16 hJob)
676 FIXME("StartSpoolPage GDI.246 unimplemented\n");
682 /**********************************************************************
683 * EndSpoolPage (GDI.247)
686 INT16 WINAPI EndSpoolPage16(HPJOB16 hJob)
688 FIXME("EndSpoolPage GDI.247 unimplemented\n");
693 /**********************************************************************
694 * GetSpoolJob (GDI.245)
697 DWORD WINAPI GetSpoolJob16(int nOption, LONG param)
700 TRACE("In GetSpoolJob param 0x%lx noption %d\n",param, nOption);
705 /******************************************************************
706 * DrvGetPrinterDataInternal
708 * Helper for DrvGetPrinterData
710 static DWORD DrvGetPrinterDataInternal(LPSTR RegStr_Printer,
711 LPBYTE lpPrinterData, int cbData, int what)
715 DWORD dwType, cbQueryData;
717 if (!(RegOpenKeyA(HKEY_LOCAL_MACHINE, RegStr_Printer, &hkey))) {
718 if (what == INT_PD_DEFAULT_DEVMODE) { /* "Default DevMode" */
719 if (!(RegQueryValueExA(hkey, DefaultDevMode, 0, &dwType, 0, &cbQueryData))) {
722 else if ((cbQueryData) && (cbQueryData <= cbData)) {
723 cbQueryData = cbData;
724 if (RegQueryValueExA(hkey, DefaultDevMode, 0,
725 &dwType, lpPrinterData, &cbQueryData))
729 } else { /* "Printer Driver" */
731 RegQueryValueExA(hkey, "Printer Driver", 0,
732 &dwType, lpPrinterData, &cbQueryData);
736 if (hkey) RegCloseKey(hkey);
740 /******************************************************************
741 * DrvGetPrinterData (GDI.282)
742 * DrvGetPrinterData16 (GDI32.@)
745 DWORD WINAPI DrvGetPrinterData16(LPSTR lpPrinter, LPSTR lpProfile,
746 LPDWORD lpType, LPBYTE lpPrinterData,
747 int cbData, LPDWORD lpNeeded)
749 LPSTR RegStr_Printer;
750 HKEY hkey = 0, hkey2 = 0;
752 DWORD dwType, PrinterAttr, cbPrinterAttr, SetData, size;
754 if (HIWORD(lpPrinter))
755 TRACE("printer %s\n",lpPrinter);
757 TRACE("printer %p\n",lpPrinter);
758 if (HIWORD(lpProfile))
759 TRACE("profile %s\n",lpProfile);
761 TRACE("profile %p\n",lpProfile);
762 TRACE("lpType %p\n",lpType);
764 if ((!lpPrinter) || (!lpProfile) || (!lpNeeded))
765 return ERROR_INVALID_PARAMETER;
767 RegStr_Printer = HeapAlloc(GetProcessHeap(), 0,
768 strlen(Printers) + strlen(lpPrinter) + 2);
769 strcpy(RegStr_Printer, Printers);
770 strcat(RegStr_Printer, lpPrinter);
772 if (((DWORD)lpProfile == INT_PD_DEFAULT_DEVMODE) || (HIWORD(lpProfile) &&
773 (!strcmp(lpProfile, DefaultDevMode)))) {
774 size = DrvGetPrinterDataInternal(RegStr_Printer, lpPrinterData, cbData,
775 INT_PD_DEFAULT_DEVMODE);
778 if ((lpPrinterData) && (*lpNeeded > cbData))
779 res = ERROR_MORE_DATA;
781 else res = ERROR_INVALID_PRINTER_NAME;
784 if (((DWORD)lpProfile == INT_PD_DEFAULT_MODEL) || (HIWORD(lpProfile) &&
785 (!strcmp(lpProfile, PrinterModel)))) {
787 if (!lpPrinterData) goto failed;
789 res = ERROR_MORE_DATA;
792 size = DrvGetPrinterDataInternal(RegStr_Printer, lpPrinterData, cbData,
793 INT_PD_DEFAULT_MODEL);
794 if ((size+1) && (lpType))
797 res = ERROR_INVALID_PRINTER_NAME;
801 if ((res = RegOpenKeyA(HKEY_LOCAL_MACHINE, RegStr_Printer, &hkey)))
804 if ((res = RegQueryValueExA(hkey, "Attributes", 0,
805 &dwType, (LPBYTE)&PrinterAttr, &cbPrinterAttr)))
807 if ((res = RegOpenKeyA(hkey, PrinterDriverData, &hkey2)))
810 res = RegQueryValueExA(hkey2, lpProfile, 0,
811 lpType, lpPrinterData, lpNeeded);
812 if ((res != ERROR_CANTREAD) &&
814 (PRINTER_ATTRIBUTE_ENABLE_BIDI|PRINTER_ATTRIBUTE_NETWORK))
815 == PRINTER_ATTRIBUTE_NETWORK))
817 if (!(res) && (*lpType == REG_DWORD) && (*(LPDWORD)lpPrinterData == -1))
818 res = ERROR_INVALID_DATA;
823 RegSetValueExA(hkey2, lpProfile, 0, REG_DWORD, (LPBYTE)&SetData, 4); /* no result returned */
828 if (hkey2) RegCloseKey(hkey2);
829 if (hkey) RegCloseKey(hkey);
830 HeapFree(GetProcessHeap(), 0, RegStr_Printer);
835 /******************************************************************
836 * DrvSetPrinterData (GDI.281)
837 * DrvSetPrinterData16 (GDI32.@)
840 DWORD WINAPI DrvSetPrinterData16(LPSTR lpPrinter, LPSTR lpProfile,
841 DWORD lpType, LPBYTE lpPrinterData,
844 LPSTR RegStr_Printer;
848 if (HIWORD(lpPrinter))
849 TRACE("printer %s\n",lpPrinter);
851 TRACE("printer %p\n",lpPrinter);
852 if (HIWORD(lpProfile))
853 TRACE("profile %s\n",lpProfile);
855 TRACE("profile %p\n",lpProfile);
856 TRACE("lpType %08lx\n",lpType);
858 if ((!lpPrinter) || (!lpProfile) ||
859 ((DWORD)lpProfile == INT_PD_DEFAULT_MODEL) || (HIWORD(lpProfile) &&
860 (!strcmp(lpProfile, PrinterModel))))
861 return ERROR_INVALID_PARAMETER;
863 RegStr_Printer = HeapAlloc(GetProcessHeap(), 0,
864 strlen(Printers) + strlen(lpPrinter) + 2);
865 strcpy(RegStr_Printer, Printers);
866 strcat(RegStr_Printer, lpPrinter);
868 if (((DWORD)lpProfile == INT_PD_DEFAULT_DEVMODE) || (HIWORD(lpProfile) &&
869 (!strcmp(lpProfile, DefaultDevMode)))) {
870 if ( RegOpenKeyA(HKEY_LOCAL_MACHINE, RegStr_Printer, &hkey)
872 RegSetValueExA(hkey, DefaultDevMode, 0, REG_BINARY,
873 lpPrinterData, dwSize) != ERROR_SUCCESS )
874 res = ERROR_INVALID_PRINTER_NAME;
878 strcat(RegStr_Printer, "\\");
880 if( (res = RegOpenKeyA(HKEY_LOCAL_MACHINE, RegStr_Printer, &hkey)) ==
884 res = RegDeleteValueA(hkey, lpProfile);
886 res = RegSetValueExA(hkey, lpProfile, 0, lpType,
887 lpPrinterData, dwSize);
891 if (hkey) RegCloseKey(hkey);
892 HeapFree(GetProcessHeap(), 0, RegStr_Printer);