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 const char PrinterModel[] = "Printer Model";
58 static const char DefaultDevMode[] = "Default DevMode";
59 static const char PrinterDriverData[] = "PrinterDriverData";
60 static const 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);
108 HeapFree( GetProcessHeap(), 0, (LPSTR)docA.lpszDocName );
109 HeapFree( GetProcessHeap(), 0, (LPSTR)docA.lpszOutput );
110 HeapFree( GetProcessHeap(), 0, (LPSTR)docA.lpszDatatype );
116 /******************************************************************
120 INT WINAPI EndDoc(HDC hdc)
123 DC *dc = DC_GetDCPtr( hdc );
124 if(!dc) return SP_ERROR;
126 if (dc->funcs->pEndDoc) ret = dc->funcs->pEndDoc( dc->physDev );
127 GDI_ReleaseObj( hdc );
132 /******************************************************************
133 * StartPage [GDI32.@]
136 INT WINAPI StartPage(HDC hdc)
139 DC *dc = DC_GetDCPtr( hdc );
140 if(!dc) return SP_ERROR;
142 if(dc->funcs->pStartPage)
143 ret = dc->funcs->pStartPage( dc->physDev );
146 GDI_ReleaseObj( hdc );
151 /******************************************************************
155 INT WINAPI EndPage(HDC hdc)
157 ABORTPROC abort_proc;
159 DC *dc = DC_GetDCPtr( hdc );
160 if(!dc) return SP_ERROR;
162 if (dc->funcs->pEndPage) ret = dc->funcs->pEndPage( dc->physDev );
163 abort_proc = dc->pAbortProc;
164 GDI_ReleaseObj( hdc );
165 if (abort_proc && !abort_proc( hdc, 0 ))
174 /******************************************************************************
177 INT WINAPI AbortDoc(HDC hdc)
180 DC *dc = DC_GetDCPtr( hdc );
181 if(!dc) return SP_ERROR;
183 if (dc->funcs->pAbortDoc) ret = dc->funcs->pAbortDoc( dc->physDev );
184 GDI_ReleaseObj( hdc );
188 /**********************************************************************
189 * QueryAbort (GDI.155)
191 * Calls the app's AbortProc function if avail.
194 * TRUE if no AbortProc avail or AbortProc wants to continue printing.
195 * FALSE if AbortProc wants to abort printing.
197 BOOL16 WINAPI QueryAbort16(HDC16 hdc16, INT16 reserved)
200 HDC hdc = HDC_32( hdc16 );
201 DC *dc = DC_GetDCPtr( hdc );
205 ERR("Invalid hdc %p\n", hdc);
209 abproc = dc->pAbortProc;
210 GDI_ReleaseObj( hdc );
213 ret = abproc(hdc, 0);
218 /**********************************************************************
221 static BOOL CALLBACK call_abort_proc16( HDC hdc, INT code )
224 DC *dc = DC_GetDCPtr( hdc );
226 if (!dc) return FALSE;
227 proc16 = dc->pAbortProc16;
228 GDI_ReleaseObj( hdc );
234 args[1] = HDC_16(hdc);
236 WOWCallback16Ex( (DWORD)proc16, WCB16_PASCAL, sizeof(args), args, &ret );
243 /**********************************************************************
244 * SetAbortProc (GDI.381)
246 INT16 WINAPI SetAbortProc16(HDC16 hdc16, ABORTPROC16 abrtprc)
248 HDC hdc = HDC_32( hdc16 );
249 DC *dc = DC_GetDCPtr( hdc );
251 if (!dc) return FALSE;
252 dc->pAbortProc16 = abrtprc;
253 GDI_ReleaseObj( hdc );
254 return SetAbortProc( hdc, call_abort_proc16 );
257 /**********************************************************************
258 * SetAbortProc (GDI32.@)
261 INT WINAPI SetAbortProc(HDC hdc, ABORTPROC abrtprc)
263 DC *dc = DC_GetDCPtr( hdc );
265 if (!dc) return FALSE;
266 dc->pAbortProc = abrtprc;
267 GDI_ReleaseObj( hdc );
272 /****************** misc. printer related functions */
275 * The following function should implement a queing system
284 static struct hpq *hpqueue;
286 /**********************************************************************
290 HPQ16 WINAPI CreatePQ16(INT16 size)
297 tmp_size = size << 2;
298 if (!(hpq = GlobalAlloc16(GMEM_SHARE|GMEM_MOVEABLE, tmp_size + 8)))
300 pPQ = GlobalLock16(hpq);
309 FIXME("(%d): stub\n",size);
314 /**********************************************************************
318 INT16 WINAPI DeletePQ16(HPQ16 hPQ)
320 return GlobalFree16((HGLOBAL16)hPQ);
323 /**********************************************************************
324 * ExtractPQ (GDI.232)
327 INT16 WINAPI ExtractPQ16(HPQ16 hPQ)
329 struct hpq *queue, *prev, *current, *currentPrev;
330 int key = 0, tag = -1;
331 currentPrev = prev = NULL;
332 queue = current = hpqueue;
338 currentPrev = current;
339 current = current->next;
342 if (current->key < key)
354 prev->next = queue->next;
356 hpqueue = queue->next;
357 HeapFree(GetProcessHeap(), 0, queue);
360 TRACE("%x got tag %d key %d\n", hPQ, tag, key);
365 /**********************************************************************
369 INT16 WINAPI InsertPQ16(HPQ16 hPQ, INT16 tag, INT16 key)
371 struct hpq *queueItem = HeapAlloc(GetProcessHeap(), 0, sizeof(struct hpq));
372 if(queueItem == NULL) {
373 ERR("Memory exausted!\n");
376 queueItem->next = hpqueue;
378 queueItem->key = key;
379 queueItem->tag = tag;
381 FIXME("(%x %d %d): stub???\n", hPQ, tag, key);
385 /**********************************************************************
389 INT16 WINAPI MinPQ16(HPQ16 hPQ)
391 FIXME("(%x): stub\n", hPQ);
395 /**********************************************************************
399 INT16 WINAPI SizePQ16(HPQ16 hPQ, INT16 sizechange)
401 FIXME("(%x %d): stub\n", hPQ, sizechange);
408 * The following functions implement part of the spooling process to
409 * print manager. I would like to see wine have a version of print managers
410 * that used LPR/LPD. For simplicity print jobs will be sent to a file for
413 typedef struct PRINTJOB
421 } PRINTJOB, *PPRINTJOB;
423 #define MAX_PRINT_JOBS 1
426 PPRINTJOB gPrintJobsTable[MAX_PRINT_JOBS];
429 static PPRINTJOB FindPrintJobFromHandle(HANDLE16 hHandle)
431 return gPrintJobsTable[0];
434 static int CreateSpoolFile(LPCSTR pszOutput)
438 char *psCmdP = psCmd;
441 /* TTD convert the 'output device' into a spool file name */
443 if (pszOutput == NULL || *pszOutput == '\0')
447 if (!strncmp("LPR:",pszOutput,4))
449 if(!RegOpenKeyA(HKEY_LOCAL_MACHINE, "Software\\Wine\\Wine\\Config\\spooler", &hkey))
451 DWORD type, count = sizeof(psCmd);
452 RegQueryValueExA(hkey, pszOutput, 0, &type, psCmd, &count);
456 sprintf(psCmd,"|lpr -P%s",pszOutput+4);
460 if(!RegOpenKeyA(HKEY_LOCAL_MACHINE, "Software\\Wine\\Wine\\Config\\spooler", &hkey))
462 DWORD type, count = sizeof(psCmd);
463 RegQueryValueExA(hkey, pszOutput, 0, &type, psCmd, &count);
467 TRACE("Got printerSpoolCommand '%s' for output device '%s'\n",
470 psCmdP = (char *)pszOutput;
473 while (*psCmdP && isspace(*psCmdP))
480 TRACE("command: '%s'\n", psCmdP);
486 ERR("pipe() failed!\n");
493 TRACE("In child need to exec %s\n",psCmdP);
498 /* reset signals that we previously set to SIG_IGN */
499 signal( SIGPIPE, SIG_DFL );
500 signal( SIGCHLD, SIG_DFL );
508 TRACE("Need to execute a cmnd and pipe the output to it\n");
514 WCHAR psCmdPW[MAX_PATH];
516 TRACE("Just assume it's a file\n");
519 * The file name can be dos based, we have to find its
520 * Unix correspondant file name
522 MultiByteToWideChar(CP_ACP, 0, psCmdP, -1, psCmdPW, MAX_PATH);
523 if ((buffer = wine_get_unix_file_name(psCmdPW)))
525 if ((fd = open(buffer, O_CREAT | O_TRUNC | O_WRONLY , 0600)) < 0)
527 ERR("Failed to create spool file '%s' ('%s'). (error %s)\n",
528 buffer, psCmdP, strerror(errno));
530 HeapFree(GetProcessHeap(), 0, buffer);
536 static int FreePrintJob(HANDLE16 hJob)
541 pPrintJob = FindPrintJobFromHandle(hJob);
542 if (pPrintJob != NULL)
544 gPrintJobsTable[pPrintJob->nIndex] = NULL;
545 HeapFree(GetProcessHeap(), 0, pPrintJob->pszOutput);
546 HeapFree(GetProcessHeap(), 0, pPrintJob->pszTitle);
547 if (pPrintJob->fd >= 0) close(pPrintJob->fd);
548 HeapFree(GetProcessHeap(), 0, pPrintJob);
554 /**********************************************************************
558 HPJOB16 WINAPI OpenJob16(LPCSTR lpOutput, LPCSTR lpTitle, HDC16 hDC)
560 HPJOB16 hHandle = (HPJOB16)SP_ERROR;
563 TRACE("'%s' '%s' %04x\n", lpOutput, lpTitle, hDC);
565 pPrintJob = gPrintJobsTable[0];
566 if (pPrintJob == NULL)
570 /* Try and create a spool file */
571 fd = CreateSpoolFile(lpOutput);
574 pPrintJob = HeapAlloc(GetProcessHeap(), 0, sizeof(PRINTJOB));
575 if(pPrintJob == NULL) {
576 WARN("Memory exausted!\n");
582 pPrintJob->pszOutput = HeapAlloc(GetProcessHeap(), 0, strlen(lpOutput)+1);
583 strcpy( pPrintJob->pszOutput, lpOutput );
586 pPrintJob->pszTitle = HeapAlloc(GetProcessHeap(), 0, strlen(lpTitle)+1);
587 strcpy( pPrintJob->pszTitle, lpTitle );
589 pPrintJob->hDC = hDC;
591 pPrintJob->nIndex = 0;
592 pPrintJob->hHandle = hHandle;
593 gPrintJobsTable[pPrintJob->nIndex] = pPrintJob;
596 TRACE("return %04x\n", hHandle);
600 /**********************************************************************
604 INT16 WINAPI CloseJob16(HPJOB16 hJob)
607 PPRINTJOB pPrintJob = NULL;
609 TRACE("%04x\n", hJob);
611 pPrintJob = FindPrintJobFromHandle(hJob);
612 if (pPrintJob != NULL)
614 /* Close the spool file */
615 close(pPrintJob->fd);
622 /**********************************************************************
623 * WriteSpool (GDI.241)
626 INT16 WINAPI WriteSpool16(HPJOB16 hJob, LPSTR lpData, INT16 cch)
629 PPRINTJOB pPrintJob = NULL;
631 TRACE("%04x %08lx %04x\n", hJob, (DWORD)lpData, cch);
633 pPrintJob = FindPrintJobFromHandle(hJob);
634 if (pPrintJob != NULL && pPrintJob->fd >= 0 && cch)
636 if (write(pPrintJob->fd, lpData, cch) != cch)
641 /* FIXME: We just cannot call 16 bit functions from here, since we
642 * have acquired several locks (DC). And we do not really need to.
644 if (pPrintJob->hDC == 0) {
645 TRACE("hDC == 0 so no QueryAbort\n");
647 else if (!(QueryAbort16(pPrintJob->hDC, (nRet == SP_OUTOFDISK) ? nRet : 0 )))
649 CloseJob16(hJob); /* printing aborted */
657 typedef INT (WINAPI *MSGBOX_PROC)( HWND, LPCSTR, LPCSTR, UINT );
659 /**********************************************************************
660 * WriteDialog (GDI.242)
663 INT16 WINAPI WriteDialog16(HPJOB16 hJob, LPSTR lpMsg, INT16 cchMsg)
666 MSGBOX_PROC pMessageBoxA;
669 TRACE("%04x %04x '%s'\n", hJob, cchMsg, lpMsg);
671 if ((mod = GetModuleHandleA("user32.dll")))
673 if ((pMessageBoxA = (MSGBOX_PROC)GetProcAddress( mod, "MessageBoxA" )))
674 ret = pMessageBoxA(0, lpMsg, "Printing Error", MB_OKCANCEL);
680 /**********************************************************************
681 * DeleteJob (GDI.244)
684 INT16 WINAPI DeleteJob16(HPJOB16 hJob, INT16 nNotUsed)
688 TRACE("%04x\n", hJob);
690 nRet = FreePrintJob(hJob);
695 * The following two function would allow a page to be sent to the printer
696 * when it has been processed. For simplicity they havn't been implemented.
697 * This means a whole job has to be processed before it is sent to the printer.
700 /**********************************************************************
701 * StartSpoolPage (GDI.246)
704 INT16 WINAPI StartSpoolPage16(HPJOB16 hJob)
706 FIXME("StartSpoolPage GDI.246 unimplemented\n");
712 /**********************************************************************
713 * EndSpoolPage (GDI.247)
716 INT16 WINAPI EndSpoolPage16(HPJOB16 hJob)
718 FIXME("EndSpoolPage GDI.247 unimplemented\n");
723 /**********************************************************************
724 * GetSpoolJob (GDI.245)
727 DWORD WINAPI GetSpoolJob16(int nOption, LONG param)
730 TRACE("In GetSpoolJob param 0x%lx noption %d\n",param, nOption);
735 /******************************************************************
736 * DrvGetPrinterDataInternal
738 * Helper for DrvGetPrinterData
740 static DWORD DrvGetPrinterDataInternal(LPSTR RegStr_Printer,
741 LPBYTE lpPrinterData, int cbData, int what)
745 DWORD dwType, cbQueryData;
747 if (!(RegOpenKeyA(HKEY_LOCAL_MACHINE, RegStr_Printer, &hkey))) {
748 if (what == INT_PD_DEFAULT_DEVMODE) { /* "Default DevMode" */
749 if (!(RegQueryValueExA(hkey, DefaultDevMode, 0, &dwType, 0, &cbQueryData))) {
752 else if ((cbQueryData) && (cbQueryData <= cbData)) {
753 cbQueryData = cbData;
754 if (RegQueryValueExA(hkey, DefaultDevMode, 0,
755 &dwType, lpPrinterData, &cbQueryData))
759 } else { /* "Printer Driver" */
761 RegQueryValueExA(hkey, "Printer Driver", 0,
762 &dwType, lpPrinterData, &cbQueryData);
766 if (hkey) RegCloseKey(hkey);
770 /******************************************************************
771 * DrvGetPrinterData (GDI.282)
774 DWORD WINAPI DrvGetPrinterData16(LPSTR lpPrinter, LPSTR lpProfile,
775 LPDWORD lpType, LPBYTE lpPrinterData,
776 int cbData, LPDWORD lpNeeded)
778 LPSTR RegStr_Printer;
779 HKEY hkey = 0, hkey2 = 0;
781 DWORD dwType, PrinterAttr, cbPrinterAttr, SetData, size;
783 if (HIWORD(lpPrinter))
784 TRACE("printer %s\n",lpPrinter);
786 TRACE("printer %p\n",lpPrinter);
787 if (HIWORD(lpProfile))
788 TRACE("profile %s\n",lpProfile);
790 TRACE("profile %p\n",lpProfile);
791 TRACE("lpType %p\n",lpType);
793 if ((!lpPrinter) || (!lpProfile) || (!lpNeeded))
794 return ERROR_INVALID_PARAMETER;
796 RegStr_Printer = HeapAlloc(GetProcessHeap(), 0,
797 strlen(Printers) + strlen(lpPrinter) + 2);
798 strcpy(RegStr_Printer, Printers);
799 strcat(RegStr_Printer, lpPrinter);
801 if (((DWORD)lpProfile == INT_PD_DEFAULT_DEVMODE) || (HIWORD(lpProfile) &&
802 (!strcmp(lpProfile, DefaultDevMode)))) {
803 size = DrvGetPrinterDataInternal(RegStr_Printer, lpPrinterData, cbData,
804 INT_PD_DEFAULT_DEVMODE);
807 if ((lpPrinterData) && (*lpNeeded > cbData))
808 res = ERROR_MORE_DATA;
810 else res = ERROR_INVALID_PRINTER_NAME;
813 if (((DWORD)lpProfile == INT_PD_DEFAULT_MODEL) || (HIWORD(lpProfile) &&
814 (!strcmp(lpProfile, PrinterModel)))) {
816 if (!lpPrinterData) goto failed;
818 res = ERROR_MORE_DATA;
821 size = DrvGetPrinterDataInternal(RegStr_Printer, lpPrinterData, cbData,
822 INT_PD_DEFAULT_MODEL);
823 if ((size+1) && (lpType))
826 res = ERROR_INVALID_PRINTER_NAME;
830 if ((res = RegOpenKeyA(HKEY_LOCAL_MACHINE, RegStr_Printer, &hkey)))
833 if ((res = RegQueryValueExA(hkey, "Attributes", 0,
834 &dwType, (LPBYTE)&PrinterAttr, &cbPrinterAttr)))
836 if ((res = RegOpenKeyA(hkey, PrinterDriverData, &hkey2)))
839 res = RegQueryValueExA(hkey2, lpProfile, 0,
840 lpType, lpPrinterData, lpNeeded);
841 if ((res != ERROR_CANTREAD) &&
843 (PRINTER_ATTRIBUTE_ENABLE_BIDI|PRINTER_ATTRIBUTE_NETWORK))
844 == PRINTER_ATTRIBUTE_NETWORK))
846 if (!(res) && (*lpType == REG_DWORD) && (*(LPDWORD)lpPrinterData == -1))
847 res = ERROR_INVALID_DATA;
852 RegSetValueExA(hkey2, lpProfile, 0, REG_DWORD, (LPBYTE)&SetData, 4); /* no result returned */
857 if (hkey2) RegCloseKey(hkey2);
858 if (hkey) RegCloseKey(hkey);
859 HeapFree(GetProcessHeap(), 0, RegStr_Printer);
864 /******************************************************************
865 * DrvSetPrinterData (GDI.281)
868 DWORD WINAPI DrvSetPrinterData16(LPSTR lpPrinter, LPSTR lpProfile,
869 DWORD lpType, LPBYTE lpPrinterData,
872 LPSTR RegStr_Printer;
876 if (HIWORD(lpPrinter))
877 TRACE("printer %s\n",lpPrinter);
879 TRACE("printer %p\n",lpPrinter);
880 if (HIWORD(lpProfile))
881 TRACE("profile %s\n",lpProfile);
883 TRACE("profile %p\n",lpProfile);
884 TRACE("lpType %08lx\n",lpType);
886 if ((!lpPrinter) || (!lpProfile) ||
887 ((DWORD)lpProfile == INT_PD_DEFAULT_MODEL) || (HIWORD(lpProfile) &&
888 (!strcmp(lpProfile, PrinterModel))))
889 return ERROR_INVALID_PARAMETER;
891 RegStr_Printer = HeapAlloc(GetProcessHeap(), 0,
892 strlen(Printers) + strlen(lpPrinter) + 2);
893 strcpy(RegStr_Printer, Printers);
894 strcat(RegStr_Printer, lpPrinter);
896 if (((DWORD)lpProfile == INT_PD_DEFAULT_DEVMODE) || (HIWORD(lpProfile) &&
897 (!strcmp(lpProfile, DefaultDevMode)))) {
898 if ( RegOpenKeyA(HKEY_LOCAL_MACHINE, RegStr_Printer, &hkey)
900 RegSetValueExA(hkey, DefaultDevMode, 0, REG_BINARY,
901 lpPrinterData, dwSize) != ERROR_SUCCESS )
902 res = ERROR_INVALID_PRINTER_NAME;
906 strcat(RegStr_Printer, "\\");
908 if( (res = RegOpenKeyA(HKEY_LOCAL_MACHINE, RegStr_Printer, &hkey)) ==
912 res = RegDeleteValueA(hkey, lpProfile);
914 res = RegSetValueExA(hkey, lpProfile, 0, lpType,
915 lpPrinterData, dwSize);
919 if (hkey) RegCloseKey(hkey);
920 HeapFree(GetProcessHeap(), 0, RegStr_Printer);