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
35 #include "wine/winbase16.h"
36 #include "wine/wingdi16.h"
40 #include "wine/debug.h"
45 WINE_DEFAULT_DEBUG_CHANNEL(print);
47 static char PrinterModel[] = "Printer Model";
48 static char DefaultDevMode[] = "Default DevMode";
49 static char PrinterDriverData[] = "PrinterDriverData";
50 static char Printers[] = "System\\CurrentControlSet\\Control\\Print\\Printers\\";
52 /******************************************************************
56 INT16 WINAPI StartDoc16( HDC16 hdc, const DOCINFO16 *lpdoc )
60 docA.cbSize = lpdoc->cbSize;
61 docA.lpszDocName = MapSL(lpdoc->lpszDocName);
62 docA.lpszOutput = MapSL(lpdoc->lpszOutput);
64 if(lpdoc->cbSize >= 14)
65 docA.lpszDatatype = MapSL(lpdoc->lpszDatatype);
67 docA.lpszDatatype = NULL;
69 if(lpdoc->cbSize >= 18)
70 docA.fwType = lpdoc->fwType;
74 return StartDocA(hdc, &docA);
77 /******************************************************************
80 * StartDoc calls the STARTDOC Escape with the input data pointing to DocName
81 * and the output data (which is used as a second input parameter).pointing at
82 * the whole docinfo structure. This seems to be an undocumented feature of
83 * the STARTDOC Escape.
85 * Note: we now do it the other way, with the STARTDOC Escape calling StartDoc.
87 INT WINAPI StartDocA(HDC hdc, const DOCINFOA* doc)
90 DC *dc = DC_GetDCPtr( hdc );
92 TRACE("DocName = '%s' Output = '%s' Datatype = '%s'\n",
93 doc->lpszDocName, doc->lpszOutput, doc->lpszDatatype);
95 if(!dc) return SP_ERROR;
97 if (dc->funcs->pStartDoc) ret = dc->funcs->pStartDoc( dc->physDev, doc );
98 GDI_ReleaseObj( hdc );
102 /*************************************************************************
103 * StartDocW [GDI32.@]
106 INT WINAPI StartDocW(HDC hdc, const DOCINFOW* doc)
111 docA.cbSize = doc->cbSize;
112 docA.lpszDocName = doc->lpszDocName ?
113 HEAP_strdupWtoA( GetProcessHeap(), 0, doc->lpszDocName ) : NULL;
114 docA.lpszOutput = doc->lpszOutput ?
115 HEAP_strdupWtoA( GetProcessHeap(), 0, doc->lpszOutput ) : NULL;
116 docA.lpszDatatype = doc->lpszDatatype ?
117 HEAP_strdupWtoA( GetProcessHeap(), 0, doc->lpszDatatype ) : NULL;
118 docA.fwType = doc->fwType;
120 ret = StartDocA(hdc, &docA);
123 HeapFree( GetProcessHeap(), 0, (LPSTR)docA.lpszDocName );
125 HeapFree( GetProcessHeap(), 0, (LPSTR)docA.lpszOutput );
126 if(docA.lpszDatatype)
127 HeapFree( GetProcessHeap(), 0, (LPSTR)docA.lpszDatatype );
132 /******************************************************************
136 INT16 WINAPI EndDoc16(HDC16 hdc)
141 /******************************************************************
145 INT WINAPI EndDoc(HDC hdc)
148 DC *dc = DC_GetDCPtr( hdc );
149 if(!dc) return SP_ERROR;
151 if (dc->funcs->pEndDoc) ret = dc->funcs->pEndDoc( dc->physDev );
152 GDI_ReleaseObj( hdc );
156 /******************************************************************
157 * StartPage [GDI.379]
160 INT16 WINAPI StartPage16(HDC16 hdc)
162 return StartPage(hdc);
165 /******************************************************************
166 * StartPage [GDI32.@]
169 INT WINAPI StartPage(HDC hdc)
172 DC *dc = DC_GetDCPtr( hdc );
173 if(!dc) return SP_ERROR;
175 if(dc->funcs->pStartPage)
176 ret = dc->funcs->pStartPage( dc->physDev );
179 GDI_ReleaseObj( hdc );
183 /******************************************************************
187 INT16 WINAPI EndPage16( HDC16 hdc )
192 /******************************************************************
196 INT WINAPI EndPage(HDC hdc)
199 DC *dc = DC_GetDCPtr( hdc );
200 if(!dc) return SP_ERROR;
202 if (dc->funcs->pEndPage) ret = dc->funcs->pEndPage( dc->physDev );
203 GDI_ReleaseObj( hdc );
204 if (!QueryAbort16( hdc, 0 ))
212 /******************************************************************************
215 INT16 WINAPI AbortDoc16(HDC16 hdc)
217 return AbortDoc(hdc);
220 /******************************************************************************
223 INT WINAPI AbortDoc(HDC hdc)
226 DC *dc = DC_GetDCPtr( hdc );
227 if(!dc) return SP_ERROR;
229 if (dc->funcs->pAbortDoc) ret = dc->funcs->pAbortDoc( dc->physDev );
230 GDI_ReleaseObj( hdc );
234 /**********************************************************************
235 * QueryAbort (GDI.155)
237 * Calls the app's AbortProc function if avail.
240 * TRUE if no AbortProc avail or AbortProc wants to continue printing.
241 * FALSE if AbortProc wants to abort printing.
243 BOOL16 WINAPI QueryAbort16(HDC16 hdc, INT16 reserved)
246 DC *dc = DC_GetDCPtr( hdc );
250 ERR("Invalid hdc %04x\n", hdc);
254 abproc = dc->pAbortProc;
255 GDI_ReleaseObj( hdc );
258 ret = abproc(hdc, 0);
262 /* ### start build ### */
263 extern WORD CALLBACK PRTDRV_CallTo16_word_ww(FARPROC16,WORD,WORD);
264 /* ### stop build ### */
266 /**********************************************************************
269 static BOOL CALLBACK call_abort_proc16( HDC hdc, INT code )
272 DC *dc = DC_GetDCPtr( hdc );
274 if (!dc) return FALSE;
275 proc16 = dc->pAbortProc16;
276 GDI_ReleaseObj( hdc );
277 if (proc16) return PRTDRV_CallTo16_word_ww( (FARPROC16)proc16, hdc, code );
282 /**********************************************************************
283 * SetAbortProc (GDI.381)
285 INT16 WINAPI SetAbortProc16(HDC16 hdc, ABORTPROC16 abrtprc)
287 DC *dc = DC_GetDCPtr( hdc );
289 if (!dc) return FALSE;
290 dc->pAbortProc16 = abrtprc;
291 GDI_ReleaseObj( hdc );
292 return SetAbortProc( hdc, call_abort_proc16 );
295 /**********************************************************************
296 * SetAbortProc (GDI32.@)
299 INT WINAPI SetAbortProc(HDC hdc, ABORTPROC abrtprc)
301 DC *dc = DC_GetDCPtr( hdc );
303 if (!dc) return FALSE;
304 dc->pAbortProc = abrtprc;
305 GDI_ReleaseObj( hdc );
310 /****************** misc. printer related functions */
313 * The following function should implement a queing system
322 static struct hpq *hpqueue;
324 /**********************************************************************
328 HPQ16 WINAPI CreatePQ16(INT16 size)
335 tmp_size = size << 2;
336 if (!(hpq = GlobalAlloc16(GMEM_SHARE|GMEM_MOVEABLE, tmp_size + 8)))
338 pPQ = GlobalLock16(hpq);
347 FIXME("(%d): stub\n",size);
352 /**********************************************************************
356 INT16 WINAPI DeletePQ16(HPQ16 hPQ)
358 return GlobalFree16((HGLOBAL16)hPQ);
361 /**********************************************************************
362 * ExtractPQ (GDI.232)
365 INT16 WINAPI ExtractPQ16(HPQ16 hPQ)
367 struct hpq *queue, *prev, *current, *currentPrev;
368 int key = 0, tag = -1;
369 currentPrev = prev = NULL;
370 queue = current = hpqueue;
376 currentPrev = current;
377 current = current->next;
380 if (current->key < key)
392 prev->next = queue->next;
394 hpqueue = queue->next;
395 HeapFree(GetProcessHeap(), 0, queue);
398 TRACE("%x got tag %d key %d\n", hPQ, tag, key);
403 /**********************************************************************
407 INT16 WINAPI InsertPQ16(HPQ16 hPQ, INT16 tag, INT16 key)
409 struct hpq *queueItem = HeapAlloc(GetProcessHeap(), 0, sizeof(struct hpq));
410 if(queueItem == NULL) {
411 ERR("Memory exausted!\n");
414 queueItem->next = hpqueue;
416 queueItem->key = key;
417 queueItem->tag = tag;
419 FIXME("(%x %d %d): stub???\n", hPQ, tag, key);
423 /**********************************************************************
427 INT16 WINAPI MinPQ16(HPQ16 hPQ)
429 FIXME("(%x): stub\n", hPQ);
433 /**********************************************************************
437 INT16 WINAPI SizePQ16(HPQ16 hPQ, INT16 sizechange)
439 FIXME("(%x %d): stub\n", hPQ, sizechange);
446 * The following functions implement part of the spooling process to
447 * print manager. I would like to see wine have a version of print managers
448 * that used LPR/LPD. For simplicity print jobs will be sent to a file for
451 typedef struct PRINTJOB
459 } PRINTJOB, *PPRINTJOB;
461 #define MAX_PRINT_JOBS 1
464 PPRINTJOB gPrintJobsTable[MAX_PRINT_JOBS];
467 static PPRINTJOB FindPrintJobFromHandle(HANDLE16 hHandle)
469 return gPrintJobsTable[0];
472 static int CreateSpoolFile(LPCSTR pszOutput)
476 char *psCmdP = psCmd;
478 /* TTD convert the 'output device' into a spool file name */
480 if (pszOutput == NULL || *pszOutput == '\0')
483 if (!strncmp("LPR:",pszOutput,4))
484 sprintf(psCmd,"|lpr -P%s",pszOutput+4);
490 if(!RegOpenKeyA(HKEY_LOCAL_MACHINE, "Software\\Wine\\Wine\\Config\\spooler", &hkey))
492 DWORD type, count = sizeof(psCmd);
493 RegQueryValueExA(hkey, pszOutput, 0, &type, psCmd, &count);
497 TRACE("Got printerSpoolCommand '%s' for output device '%s'\n",
500 psCmdP = (char *)pszOutput;
503 while (*psCmdP && isspace(*psCmdP))
519 TRACE("In child need to exec %s\n",psCmdP);
529 TRACE("Need to execute a cmnd and pipe the output to it\n");
533 char buffer[MAX_PATH];
535 TRACE("Just assume it's a file\n");
538 * The file name can be dos based, we have to find its
539 * Unix correspondant file name
541 wine_get_unix_file_name(psCmdP, buffer, sizeof(buffer));
543 if ((fd = open(buffer, O_CREAT | O_TRUNC | O_WRONLY , 0600)) < 0)
545 ERR("Failed to create spool file '%s' ('%s'). (error %s)\n",
546 buffer, psCmdP, strerror(errno));
552 static int FreePrintJob(HANDLE16 hJob)
557 pPrintJob = FindPrintJobFromHandle(hJob);
558 if (pPrintJob != NULL)
560 gPrintJobsTable[pPrintJob->nIndex] = NULL;
561 HeapFree(GetProcessHeap(), 0, pPrintJob->pszOutput);
562 HeapFree(GetProcessHeap(), 0, pPrintJob->pszTitle);
563 if (pPrintJob->fd >= 0) close(pPrintJob->fd);
564 HeapFree(GetProcessHeap(), 0, pPrintJob);
570 /**********************************************************************
572 * OpenJob16 (GDI32.@)
575 HPJOB16 WINAPI OpenJob16(LPCSTR lpOutput, LPCSTR lpTitle, HDC16 hDC)
577 HPJOB16 hHandle = (HPJOB16)SP_ERROR;
580 TRACE("'%s' '%s' %04x\n", lpOutput, lpTitle, hDC);
582 pPrintJob = gPrintJobsTable[0];
583 if (pPrintJob == NULL)
587 /* Try and create a spool file */
588 fd = CreateSpoolFile(lpOutput);
591 pPrintJob = HeapAlloc(GetProcessHeap(), 0, sizeof(PRINTJOB));
592 if(pPrintJob == NULL) {
593 WARN("Memory exausted!\n");
599 pPrintJob->pszOutput = HeapAlloc(GetProcessHeap(), 0, strlen(lpOutput)+1);
600 strcpy( pPrintJob->pszOutput, lpOutput );
603 pPrintJob->pszTitle = HeapAlloc(GetProcessHeap(), 0, strlen(lpTitle)+1);
604 strcpy( pPrintJob->pszTitle, lpTitle );
606 pPrintJob->hDC = hDC;
608 pPrintJob->nIndex = 0;
609 pPrintJob->hHandle = hHandle;
610 gPrintJobsTable[pPrintJob->nIndex] = pPrintJob;
613 TRACE("return %04x\n", hHandle);
617 /**********************************************************************
619 * CloseJob16 (GDI32.@)
622 INT16 WINAPI CloseJob16(HPJOB16 hJob)
625 PPRINTJOB pPrintJob = NULL;
627 TRACE("%04x\n", hJob);
629 pPrintJob = FindPrintJobFromHandle(hJob);
630 if (pPrintJob != NULL)
632 /* Close the spool file */
633 close(pPrintJob->fd);
640 /**********************************************************************
641 * WriteSpool (GDI.241)
642 * WriteSpool16 (GDI32.@)
645 INT16 WINAPI WriteSpool16(HPJOB16 hJob, LPSTR lpData, INT16 cch)
648 PPRINTJOB pPrintJob = NULL;
650 TRACE("%04x %08lx %04x\n", hJob, (DWORD)lpData, cch);
652 pPrintJob = FindPrintJobFromHandle(hJob);
653 if (pPrintJob != NULL && pPrintJob->fd >= 0 && cch)
655 if (write(pPrintJob->fd, lpData, cch) != cch)
660 /* FIXME: We just cannot call 16 bit functions from here, since we
661 * have acquired several locks (DC). And we do not really need to.
663 if (pPrintJob->hDC == 0) {
664 TRACE("hDC == 0 so no QueryAbort\n");
666 else if (!(QueryAbort16(pPrintJob->hDC, (nRet == SP_OUTOFDISK) ? nRet : 0 )))
668 CloseJob16(hJob); /* printing aborted */
676 typedef INT (WINAPI *MSGBOX_PROC)( HWND, LPCSTR, LPCSTR, UINT );
678 /**********************************************************************
679 * WriteDialog (GDI.242)
682 INT16 WINAPI WriteDialog16(HPJOB16 hJob, LPSTR lpMsg, INT16 cchMsg)
685 MSGBOX_PROC pMessageBoxA;
688 TRACE("%04x %04x '%s'\n", hJob, cchMsg, lpMsg);
690 if ((mod = GetModuleHandleA("user32.dll")))
692 if ((pMessageBoxA = (MSGBOX_PROC)GetProcAddress( mod, "MessageBoxA" )))
693 ret = pMessageBoxA(0, lpMsg, "Printing Error", MB_OKCANCEL);
699 /**********************************************************************
700 * DeleteJob (GDI.244)
703 INT16 WINAPI DeleteJob16(HPJOB16 hJob, INT16 nNotUsed)
707 TRACE("%04x\n", hJob);
709 nRet = FreePrintJob(hJob);
714 * The following two function would allow a page to be sent to the printer
715 * when it has been processed. For simplicity they havn't been implemented.
716 * This means a whole job has to be processed before it is sent to the printer.
719 /**********************************************************************
720 * StartSpoolPage (GDI.246)
723 INT16 WINAPI StartSpoolPage16(HPJOB16 hJob)
725 FIXME("StartSpoolPage GDI.246 unimplemented\n");
731 /**********************************************************************
732 * EndSpoolPage (GDI.247)
735 INT16 WINAPI EndSpoolPage16(HPJOB16 hJob)
737 FIXME("EndSpoolPage GDI.247 unimplemented\n");
742 /**********************************************************************
743 * GetSpoolJob (GDI.245)
746 DWORD WINAPI GetSpoolJob16(int nOption, LONG param)
749 TRACE("In GetSpoolJob param 0x%lx noption %d\n",param, nOption);
754 /******************************************************************
755 * DrvGetPrinterDataInternal
757 * Helper for DrvGetPrinterData
759 static DWORD DrvGetPrinterDataInternal(LPSTR RegStr_Printer,
760 LPBYTE lpPrinterData, int cbData, int what)
764 DWORD dwType, cbQueryData;
766 if (!(RegOpenKeyA(HKEY_LOCAL_MACHINE, RegStr_Printer, &hkey))) {
767 if (what == INT_PD_DEFAULT_DEVMODE) { /* "Default DevMode" */
768 if (!(RegQueryValueExA(hkey, DefaultDevMode, 0, &dwType, 0, &cbQueryData))) {
771 else if ((cbQueryData) && (cbQueryData <= cbData)) {
772 cbQueryData = cbData;
773 if (RegQueryValueExA(hkey, DefaultDevMode, 0,
774 &dwType, lpPrinterData, &cbQueryData))
778 } else { /* "Printer Driver" */
780 RegQueryValueExA(hkey, "Printer Driver", 0,
781 &dwType, lpPrinterData, &cbQueryData);
785 if (hkey) RegCloseKey(hkey);
789 /******************************************************************
790 * DrvGetPrinterData (GDI.282)
791 * DrvGetPrinterData16 (GDI32.@)
794 DWORD WINAPI DrvGetPrinterData16(LPSTR lpPrinter, LPSTR lpProfile,
795 LPDWORD lpType, LPBYTE lpPrinterData,
796 int cbData, LPDWORD lpNeeded)
798 LPSTR RegStr_Printer;
799 HKEY hkey = 0, hkey2 = 0;
801 DWORD dwType, PrinterAttr, cbPrinterAttr, SetData, size;
803 if (HIWORD(lpPrinter))
804 TRACE("printer %s\n",lpPrinter);
806 TRACE("printer %p\n",lpPrinter);
807 if (HIWORD(lpProfile))
808 TRACE("profile %s\n",lpProfile);
810 TRACE("profile %p\n",lpProfile);
811 TRACE("lpType %p\n",lpType);
813 if ((!lpPrinter) || (!lpProfile) || (!lpNeeded))
814 return ERROR_INVALID_PARAMETER;
816 RegStr_Printer = HeapAlloc(GetProcessHeap(), 0,
817 strlen(Printers) + strlen(lpPrinter) + 2);
818 strcpy(RegStr_Printer, Printers);
819 strcat(RegStr_Printer, lpPrinter);
821 if (((DWORD)lpProfile == INT_PD_DEFAULT_DEVMODE) || (HIWORD(lpProfile) &&
822 (!strcmp(lpProfile, DefaultDevMode)))) {
823 size = DrvGetPrinterDataInternal(RegStr_Printer, lpPrinterData, cbData,
824 INT_PD_DEFAULT_DEVMODE);
827 if ((lpPrinterData) && (*lpNeeded > cbData))
828 res = ERROR_MORE_DATA;
830 else res = ERROR_INVALID_PRINTER_NAME;
833 if (((DWORD)lpProfile == INT_PD_DEFAULT_MODEL) || (HIWORD(lpProfile) &&
834 (!strcmp(lpProfile, PrinterModel)))) {
836 if (!lpPrinterData) goto failed;
838 res = ERROR_MORE_DATA;
841 size = DrvGetPrinterDataInternal(RegStr_Printer, lpPrinterData, cbData,
842 INT_PD_DEFAULT_MODEL);
843 if ((size+1) && (lpType))
846 res = ERROR_INVALID_PRINTER_NAME;
850 if ((res = RegOpenKeyA(HKEY_LOCAL_MACHINE, RegStr_Printer, &hkey)))
853 if ((res = RegQueryValueExA(hkey, "Attributes", 0,
854 &dwType, (LPBYTE)&PrinterAttr, &cbPrinterAttr)))
856 if ((res = RegOpenKeyA(hkey, PrinterDriverData, &hkey2)))
859 res = RegQueryValueExA(hkey2, lpProfile, 0,
860 lpType, lpPrinterData, lpNeeded);
861 if ((res != ERROR_CANTREAD) &&
863 (PRINTER_ATTRIBUTE_ENABLE_BIDI|PRINTER_ATTRIBUTE_NETWORK))
864 == PRINTER_ATTRIBUTE_NETWORK))
866 if (!(res) && (*lpType == REG_DWORD) && (*(LPDWORD)lpPrinterData == -1))
867 res = ERROR_INVALID_DATA;
872 RegSetValueExA(hkey2, lpProfile, 0, REG_DWORD, (LPBYTE)&SetData, 4); /* no result returned */
877 if (hkey2) RegCloseKey(hkey2);
878 if (hkey) RegCloseKey(hkey);
879 HeapFree(GetProcessHeap(), 0, RegStr_Printer);
884 /******************************************************************
885 * DrvSetPrinterData (GDI.281)
886 * DrvSetPrinterData16 (GDI32.@)
889 DWORD WINAPI DrvSetPrinterData16(LPSTR lpPrinter, LPSTR lpProfile,
890 DWORD lpType, LPBYTE lpPrinterData,
893 LPSTR RegStr_Printer;
897 if (HIWORD(lpPrinter))
898 TRACE("printer %s\n",lpPrinter);
900 TRACE("printer %p\n",lpPrinter);
901 if (HIWORD(lpProfile))
902 TRACE("profile %s\n",lpProfile);
904 TRACE("profile %p\n",lpProfile);
905 TRACE("lpType %08lx\n",lpType);
907 if ((!lpPrinter) || (!lpProfile) ||
908 ((DWORD)lpProfile == INT_PD_DEFAULT_MODEL) || (HIWORD(lpProfile) &&
909 (!strcmp(lpProfile, PrinterModel))))
910 return ERROR_INVALID_PARAMETER;
912 RegStr_Printer = HeapAlloc(GetProcessHeap(), 0,
913 strlen(Printers) + strlen(lpPrinter) + 2);
914 strcpy(RegStr_Printer, Printers);
915 strcat(RegStr_Printer, lpPrinter);
917 if (((DWORD)lpProfile == INT_PD_DEFAULT_DEVMODE) || (HIWORD(lpProfile) &&
918 (!strcmp(lpProfile, DefaultDevMode)))) {
919 if ( RegOpenKeyA(HKEY_LOCAL_MACHINE, RegStr_Printer, &hkey)
921 RegSetValueExA(hkey, DefaultDevMode, 0, REG_BINARY,
922 lpPrinterData, dwSize) != ERROR_SUCCESS )
923 res = ERROR_INVALID_PRINTER_NAME;
927 strcat(RegStr_Printer, "\\");
929 if( (res = RegOpenKeyA(HKEY_LOCAL_MACHINE, RegStr_Printer, &hkey)) ==
933 res = RegDeleteValueA(hkey, lpProfile);
935 res = RegSetValueExA(hkey, lpProfile, 0, lpType,
936 lpPrinterData, dwSize);
940 if (hkey) RegCloseKey(hkey);
941 HeapFree(GetProcessHeap(), 0, RegStr_Printer);