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"
44 #include "wine/winbase16.h"
45 #include "wine/wingdi16.h"
50 #include "wine/debug.h"
52 #include "gdi_private.h"
55 WINE_DEFAULT_DEBUG_CHANNEL(print);
57 static char PrinterModel[] = "Printer Model";
58 static char DefaultDevMode[] = "Default DevMode";
59 static char PrinterDriverData[] = "PrinterDriverData";
60 static char Printers[] = "System\\CurrentControlSet\\Control\\Print\\Printers\\";
63 /******************************************************************
66 * StartDoc calls the STARTDOC Escape with the input data pointing to DocName
67 * and the output data (which is used as a second input parameter).pointing at
68 * the whole docinfo structure. This seems to be an undocumented feature of
69 * the STARTDOC Escape.
71 * Note: we now do it the other way, with the STARTDOC Escape calling StartDoc.
73 INT WINAPI StartDocA(HDC hdc, const DOCINFOA* doc)
76 DC *dc = DC_GetDCPtr( hdc );
78 TRACE("DocName = '%s' Output = '%s' Datatype = '%s'\n",
79 doc->lpszDocName, doc->lpszOutput, doc->lpszDatatype);
81 if(!dc) return SP_ERROR;
83 if (dc->funcs->pStartDoc) ret = dc->funcs->pStartDoc( dc->physDev, doc );
84 GDI_ReleaseObj( hdc );
88 /*************************************************************************
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 );
119 /******************************************************************
123 INT WINAPI EndDoc(HDC hdc)
126 DC *dc = DC_GetDCPtr( hdc );
127 if(!dc) return SP_ERROR;
129 if (dc->funcs->pEndDoc) ret = dc->funcs->pEndDoc( dc->physDev );
130 GDI_ReleaseObj( hdc );
135 /******************************************************************
136 * StartPage [GDI32.@]
139 INT WINAPI StartPage(HDC hdc)
142 DC *dc = DC_GetDCPtr( hdc );
143 if(!dc) return SP_ERROR;
145 if(dc->funcs->pStartPage)
146 ret = dc->funcs->pStartPage( dc->physDev );
149 GDI_ReleaseObj( hdc );
154 /******************************************************************
158 INT WINAPI EndPage(HDC hdc)
160 ABORTPROC abort_proc;
162 DC *dc = DC_GetDCPtr( hdc );
163 if(!dc) return SP_ERROR;
165 if (dc->funcs->pEndPage) ret = dc->funcs->pEndPage( dc->physDev );
166 abort_proc = dc->pAbortProc;
167 GDI_ReleaseObj( hdc );
168 if (abort_proc && !abort_proc( hdc, 0 ))
177 /******************************************************************************
180 INT WINAPI AbortDoc(HDC hdc)
183 DC *dc = DC_GetDCPtr( hdc );
184 if(!dc) return SP_ERROR;
186 if (dc->funcs->pAbortDoc) ret = dc->funcs->pAbortDoc( dc->physDev );
187 GDI_ReleaseObj( hdc );
191 /**********************************************************************
192 * QueryAbort (GDI.155)
194 * Calls the app's AbortProc function if avail.
197 * TRUE if no AbortProc avail or AbortProc wants to continue printing.
198 * FALSE if AbortProc wants to abort printing.
200 BOOL16 WINAPI QueryAbort16(HDC16 hdc16, INT16 reserved)
203 HDC hdc = HDC_32( hdc16 );
204 DC *dc = DC_GetDCPtr( hdc );
208 ERR("Invalid hdc %p\n", hdc);
212 abproc = dc->pAbortProc;
213 GDI_ReleaseObj( hdc );
216 ret = abproc(hdc, 0);
221 /**********************************************************************
224 static BOOL CALLBACK call_abort_proc16( HDC hdc, INT code )
227 DC *dc = DC_GetDCPtr( hdc );
229 if (!dc) return FALSE;
230 proc16 = dc->pAbortProc16;
231 GDI_ReleaseObj( hdc );
237 args[1] = HDC_16(hdc);
239 WOWCallback16Ex( (DWORD)proc16, WCB16_PASCAL, sizeof(args), args, &ret );
246 /**********************************************************************
247 * SetAbortProc (GDI.381)
249 INT16 WINAPI SetAbortProc16(HDC16 hdc16, ABORTPROC16 abrtprc)
251 HDC hdc = HDC_32( hdc16 );
252 DC *dc = DC_GetDCPtr( hdc );
254 if (!dc) return FALSE;
255 dc->pAbortProc16 = abrtprc;
256 GDI_ReleaseObj( hdc );
257 return SetAbortProc( hdc, call_abort_proc16 );
260 /**********************************************************************
261 * SetAbortProc (GDI32.@)
264 INT WINAPI SetAbortProc(HDC hdc, ABORTPROC abrtprc)
266 DC *dc = DC_GetDCPtr( hdc );
268 if (!dc) return FALSE;
269 dc->pAbortProc = abrtprc;
270 GDI_ReleaseObj( hdc );
275 /****************** misc. printer related functions */
278 * The following function should implement a queing system
287 static struct hpq *hpqueue;
289 /**********************************************************************
293 HPQ16 WINAPI CreatePQ16(INT16 size)
300 tmp_size = size << 2;
301 if (!(hpq = GlobalAlloc16(GMEM_SHARE|GMEM_MOVEABLE, tmp_size + 8)))
303 pPQ = GlobalLock16(hpq);
312 FIXME("(%d): stub\n",size);
317 /**********************************************************************
321 INT16 WINAPI DeletePQ16(HPQ16 hPQ)
323 return GlobalFree16((HGLOBAL16)hPQ);
326 /**********************************************************************
327 * ExtractPQ (GDI.232)
330 INT16 WINAPI ExtractPQ16(HPQ16 hPQ)
332 struct hpq *queue, *prev, *current, *currentPrev;
333 int key = 0, tag = -1;
334 currentPrev = prev = NULL;
335 queue = current = hpqueue;
341 currentPrev = current;
342 current = current->next;
345 if (current->key < key)
357 prev->next = queue->next;
359 hpqueue = queue->next;
360 HeapFree(GetProcessHeap(), 0, queue);
363 TRACE("%x got tag %d key %d\n", hPQ, tag, key);
368 /**********************************************************************
372 INT16 WINAPI InsertPQ16(HPQ16 hPQ, INT16 tag, INT16 key)
374 struct hpq *queueItem = HeapAlloc(GetProcessHeap(), 0, sizeof(struct hpq));
375 if(queueItem == NULL) {
376 ERR("Memory exausted!\n");
379 queueItem->next = hpqueue;
381 queueItem->key = key;
382 queueItem->tag = tag;
384 FIXME("(%x %d %d): stub???\n", hPQ, tag, key);
388 /**********************************************************************
392 INT16 WINAPI MinPQ16(HPQ16 hPQ)
394 FIXME("(%x): stub\n", hPQ);
398 /**********************************************************************
402 INT16 WINAPI SizePQ16(HPQ16 hPQ, INT16 sizechange)
404 FIXME("(%x %d): stub\n", hPQ, sizechange);
411 * The following functions implement part of the spooling process to
412 * print manager. I would like to see wine have a version of print managers
413 * that used LPR/LPD. For simplicity print jobs will be sent to a file for
416 typedef struct PRINTJOB
424 } PRINTJOB, *PPRINTJOB;
426 #define MAX_PRINT_JOBS 1
429 PPRINTJOB gPrintJobsTable[MAX_PRINT_JOBS];
432 static PPRINTJOB FindPrintJobFromHandle(HANDLE16 hHandle)
434 return gPrintJobsTable[0];
437 static int CreateSpoolFile(LPCSTR pszOutput)
441 char *psCmdP = psCmd;
444 /* TTD convert the 'output device' into a spool file name */
446 if (pszOutput == NULL || *pszOutput == '\0')
450 if (!strncmp("LPR:",pszOutput,4))
452 if(!RegOpenKeyA(HKEY_LOCAL_MACHINE, "Software\\Wine\\Wine\\Config\\spooler", &hkey))
454 DWORD type, count = sizeof(psCmd);
455 RegQueryValueExA(hkey, pszOutput, 0, &type, psCmd, &count);
459 sprintf(psCmd,"|lpr -P%s",pszOutput+4);
463 if(!RegOpenKeyA(HKEY_LOCAL_MACHINE, "Software\\Wine\\Wine\\Config\\spooler", &hkey))
465 DWORD type, count = sizeof(psCmd);
466 RegQueryValueExA(hkey, pszOutput, 0, &type, psCmd, &count);
470 TRACE("Got printerSpoolCommand '%s' for output device '%s'\n",
473 psCmdP = (char *)pszOutput;
476 while (*psCmdP && isspace(*psCmdP))
483 TRACE("command: '%s'\n", psCmdP);
488 ERR("pipe() failed!\n");
495 TRACE("In child need to exec %s\n",psCmdP);
500 /* reset signals that we previously set to SIG_IGN */
501 signal( SIGPIPE, SIG_DFL );
502 signal( SIGCHLD, SIG_DFL );
510 TRACE("Need to execute a cmnd and pipe the output to it\n");
514 char buffer[MAX_PATH];
515 WCHAR psCmdPW[MAX_PATH];
517 TRACE("Just assume it's a file\n");
520 * The file name can be dos based, we have to find its
521 * Unix correspondant file name
523 MultiByteToWideChar(CP_ACP, 0, psCmdP, -1, psCmdPW, MAX_PATH);
524 wine_get_unix_file_name(psCmdPW, buffer, sizeof(buffer));
526 if ((fd = open(buffer, O_CREAT | O_TRUNC | O_WRONLY , 0600)) < 0)
528 ERR("Failed to create spool file '%s' ('%s'). (error %s)\n",
529 buffer, psCmdP, strerror(errno));
535 static int FreePrintJob(HANDLE16 hJob)
540 pPrintJob = FindPrintJobFromHandle(hJob);
541 if (pPrintJob != NULL)
543 gPrintJobsTable[pPrintJob->nIndex] = NULL;
544 HeapFree(GetProcessHeap(), 0, pPrintJob->pszOutput);
545 HeapFree(GetProcessHeap(), 0, pPrintJob->pszTitle);
546 if (pPrintJob->fd >= 0) close(pPrintJob->fd);
547 HeapFree(GetProcessHeap(), 0, pPrintJob);
553 /**********************************************************************
557 HPJOB16 WINAPI OpenJob16(LPCSTR lpOutput, LPCSTR lpTitle, HDC16 hDC)
559 HPJOB16 hHandle = (HPJOB16)SP_ERROR;
562 TRACE("'%s' '%s' %04x\n", lpOutput, lpTitle, hDC);
564 pPrintJob = gPrintJobsTable[0];
565 if (pPrintJob == NULL)
569 /* Try and create a spool file */
570 fd = CreateSpoolFile(lpOutput);
573 pPrintJob = HeapAlloc(GetProcessHeap(), 0, sizeof(PRINTJOB));
574 if(pPrintJob == NULL) {
575 WARN("Memory exausted!\n");
581 pPrintJob->pszOutput = HeapAlloc(GetProcessHeap(), 0, strlen(lpOutput)+1);
582 strcpy( pPrintJob->pszOutput, lpOutput );
585 pPrintJob->pszTitle = HeapAlloc(GetProcessHeap(), 0, strlen(lpTitle)+1);
586 strcpy( pPrintJob->pszTitle, lpTitle );
588 pPrintJob->hDC = hDC;
590 pPrintJob->nIndex = 0;
591 pPrintJob->hHandle = hHandle;
592 gPrintJobsTable[pPrintJob->nIndex] = pPrintJob;
595 TRACE("return %04x\n", hHandle);
599 /**********************************************************************
603 INT16 WINAPI CloseJob16(HPJOB16 hJob)
606 PPRINTJOB pPrintJob = NULL;
608 TRACE("%04x\n", hJob);
610 pPrintJob = FindPrintJobFromHandle(hJob);
611 if (pPrintJob != NULL)
613 /* Close the spool file */
614 close(pPrintJob->fd);
621 /**********************************************************************
622 * WriteSpool (GDI.241)
625 INT16 WINAPI WriteSpool16(HPJOB16 hJob, LPSTR lpData, INT16 cch)
628 PPRINTJOB pPrintJob = NULL;
630 TRACE("%04x %08lx %04x\n", hJob, (DWORD)lpData, cch);
632 pPrintJob = FindPrintJobFromHandle(hJob);
633 if (pPrintJob != NULL && pPrintJob->fd >= 0 && cch)
635 if (write(pPrintJob->fd, lpData, cch) != cch)
640 /* FIXME: We just cannot call 16 bit functions from here, since we
641 * have acquired several locks (DC). And we do not really need to.
643 if (pPrintJob->hDC == 0) {
644 TRACE("hDC == 0 so no QueryAbort\n");
646 else if (!(QueryAbort16(pPrintJob->hDC, (nRet == SP_OUTOFDISK) ? nRet : 0 )))
648 CloseJob16(hJob); /* printing aborted */
656 typedef INT (WINAPI *MSGBOX_PROC)( HWND, LPCSTR, LPCSTR, UINT );
658 /**********************************************************************
659 * WriteDialog (GDI.242)
662 INT16 WINAPI WriteDialog16(HPJOB16 hJob, LPSTR lpMsg, INT16 cchMsg)
665 MSGBOX_PROC pMessageBoxA;
668 TRACE("%04x %04x '%s'\n", hJob, cchMsg, lpMsg);
670 if ((mod = GetModuleHandleA("user32.dll")))
672 if ((pMessageBoxA = (MSGBOX_PROC)GetProcAddress( mod, "MessageBoxA" )))
673 ret = pMessageBoxA(0, lpMsg, "Printing Error", MB_OKCANCEL);
679 /**********************************************************************
680 * DeleteJob (GDI.244)
683 INT16 WINAPI DeleteJob16(HPJOB16 hJob, INT16 nNotUsed)
687 TRACE("%04x\n", hJob);
689 nRet = FreePrintJob(hJob);
694 * The following two function would allow a page to be sent to the printer
695 * when it has been processed. For simplicity they havn't been implemented.
696 * This means a whole job has to be processed before it is sent to the printer.
699 /**********************************************************************
700 * StartSpoolPage (GDI.246)
703 INT16 WINAPI StartSpoolPage16(HPJOB16 hJob)
705 FIXME("StartSpoolPage GDI.246 unimplemented\n");
711 /**********************************************************************
712 * EndSpoolPage (GDI.247)
715 INT16 WINAPI EndSpoolPage16(HPJOB16 hJob)
717 FIXME("EndSpoolPage GDI.247 unimplemented\n");
722 /**********************************************************************
723 * GetSpoolJob (GDI.245)
726 DWORD WINAPI GetSpoolJob16(int nOption, LONG param)
729 TRACE("In GetSpoolJob param 0x%lx noption %d\n",param, nOption);
734 /******************************************************************
735 * DrvGetPrinterDataInternal
737 * Helper for DrvGetPrinterData
739 static DWORD DrvGetPrinterDataInternal(LPSTR RegStr_Printer,
740 LPBYTE lpPrinterData, int cbData, int what)
744 DWORD dwType, cbQueryData;
746 if (!(RegOpenKeyA(HKEY_LOCAL_MACHINE, RegStr_Printer, &hkey))) {
747 if (what == INT_PD_DEFAULT_DEVMODE) { /* "Default DevMode" */
748 if (!(RegQueryValueExA(hkey, DefaultDevMode, 0, &dwType, 0, &cbQueryData))) {
751 else if ((cbQueryData) && (cbQueryData <= cbData)) {
752 cbQueryData = cbData;
753 if (RegQueryValueExA(hkey, DefaultDevMode, 0,
754 &dwType, lpPrinterData, &cbQueryData))
758 } else { /* "Printer Driver" */
760 RegQueryValueExA(hkey, "Printer Driver", 0,
761 &dwType, lpPrinterData, &cbQueryData);
765 if (hkey) RegCloseKey(hkey);
769 /******************************************************************
770 * DrvGetPrinterData (GDI.282)
773 DWORD WINAPI DrvGetPrinterData16(LPSTR lpPrinter, LPSTR lpProfile,
774 LPDWORD lpType, LPBYTE lpPrinterData,
775 int cbData, LPDWORD lpNeeded)
777 LPSTR RegStr_Printer;
778 HKEY hkey = 0, hkey2 = 0;
780 DWORD dwType, PrinterAttr, cbPrinterAttr, SetData, size;
782 if (HIWORD(lpPrinter))
783 TRACE("printer %s\n",lpPrinter);
785 TRACE("printer %p\n",lpPrinter);
786 if (HIWORD(lpProfile))
787 TRACE("profile %s\n",lpProfile);
789 TRACE("profile %p\n",lpProfile);
790 TRACE("lpType %p\n",lpType);
792 if ((!lpPrinter) || (!lpProfile) || (!lpNeeded))
793 return ERROR_INVALID_PARAMETER;
795 RegStr_Printer = HeapAlloc(GetProcessHeap(), 0,
796 strlen(Printers) + strlen(lpPrinter) + 2);
797 strcpy(RegStr_Printer, Printers);
798 strcat(RegStr_Printer, lpPrinter);
800 if (((DWORD)lpProfile == INT_PD_DEFAULT_DEVMODE) || (HIWORD(lpProfile) &&
801 (!strcmp(lpProfile, DefaultDevMode)))) {
802 size = DrvGetPrinterDataInternal(RegStr_Printer, lpPrinterData, cbData,
803 INT_PD_DEFAULT_DEVMODE);
806 if ((lpPrinterData) && (*lpNeeded > cbData))
807 res = ERROR_MORE_DATA;
809 else res = ERROR_INVALID_PRINTER_NAME;
812 if (((DWORD)lpProfile == INT_PD_DEFAULT_MODEL) || (HIWORD(lpProfile) &&
813 (!strcmp(lpProfile, PrinterModel)))) {
815 if (!lpPrinterData) goto failed;
817 res = ERROR_MORE_DATA;
820 size = DrvGetPrinterDataInternal(RegStr_Printer, lpPrinterData, cbData,
821 INT_PD_DEFAULT_MODEL);
822 if ((size+1) && (lpType))
825 res = ERROR_INVALID_PRINTER_NAME;
829 if ((res = RegOpenKeyA(HKEY_LOCAL_MACHINE, RegStr_Printer, &hkey)))
832 if ((res = RegQueryValueExA(hkey, "Attributes", 0,
833 &dwType, (LPBYTE)&PrinterAttr, &cbPrinterAttr)))
835 if ((res = RegOpenKeyA(hkey, PrinterDriverData, &hkey2)))
838 res = RegQueryValueExA(hkey2, lpProfile, 0,
839 lpType, lpPrinterData, lpNeeded);
840 if ((res != ERROR_CANTREAD) &&
842 (PRINTER_ATTRIBUTE_ENABLE_BIDI|PRINTER_ATTRIBUTE_NETWORK))
843 == PRINTER_ATTRIBUTE_NETWORK))
845 if (!(res) && (*lpType == REG_DWORD) && (*(LPDWORD)lpPrinterData == -1))
846 res = ERROR_INVALID_DATA;
851 RegSetValueExA(hkey2, lpProfile, 0, REG_DWORD, (LPBYTE)&SetData, 4); /* no result returned */
856 if (hkey2) RegCloseKey(hkey2);
857 if (hkey) RegCloseKey(hkey);
858 HeapFree(GetProcessHeap(), 0, RegStr_Printer);
863 /******************************************************************
864 * DrvSetPrinterData (GDI.281)
867 DWORD WINAPI DrvSetPrinterData16(LPSTR lpPrinter, LPSTR lpProfile,
868 DWORD lpType, LPBYTE lpPrinterData,
871 LPSTR RegStr_Printer;
875 if (HIWORD(lpPrinter))
876 TRACE("printer %s\n",lpPrinter);
878 TRACE("printer %p\n",lpPrinter);
879 if (HIWORD(lpProfile))
880 TRACE("profile %s\n",lpProfile);
882 TRACE("profile %p\n",lpProfile);
883 TRACE("lpType %08lx\n",lpType);
885 if ((!lpPrinter) || (!lpProfile) ||
886 ((DWORD)lpProfile == INT_PD_DEFAULT_MODEL) || (HIWORD(lpProfile) &&
887 (!strcmp(lpProfile, PrinterModel))))
888 return ERROR_INVALID_PARAMETER;
890 RegStr_Printer = HeapAlloc(GetProcessHeap(), 0,
891 strlen(Printers) + strlen(lpPrinter) + 2);
892 strcpy(RegStr_Printer, Printers);
893 strcat(RegStr_Printer, lpPrinter);
895 if (((DWORD)lpProfile == INT_PD_DEFAULT_DEVMODE) || (HIWORD(lpProfile) &&
896 (!strcmp(lpProfile, DefaultDevMode)))) {
897 if ( RegOpenKeyA(HKEY_LOCAL_MACHINE, RegStr_Printer, &hkey)
899 RegSetValueExA(hkey, DefaultDevMode, 0, REG_BINARY,
900 lpPrinterData, dwSize) != ERROR_SUCCESS )
901 res = ERROR_INVALID_PRINTER_NAME;
905 strcat(RegStr_Printer, "\\");
907 if( (res = RegOpenKeyA(HKEY_LOCAL_MACHINE, RegStr_Printer, &hkey)) ==
911 res = RegDeleteValueA(hkey, lpProfile);
913 res = RegSetValueExA(hkey, lpProfile, 0, lpType,
914 lpPrinterData, dwSize);
918 if (hkey) RegCloseKey(hkey);
919 HeapFree(GetProcessHeap(), 0, RegStr_Printer);