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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, 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"
54 WINE_DEFAULT_DEBUG_CHANNEL(print);
56 static const char PrinterModel[] = "Printer Model";
57 static const char DefaultDevMode[] = "Default DevMode";
58 static const char PrinterDriverData[] = "PrinterDriverData";
59 static const char Printers[] = "System\\CurrentControlSet\\Control\\Print\\Printers\\";
62 /******************************************************************
65 * StartDoc calls the STARTDOC Escape with the input data pointing to DocName
66 * and the output data (which is used as a second input parameter).pointing at
67 * the whole docinfo structure. This seems to be an undocumented feature of
68 * the STARTDOC Escape.
70 * Note: we now do it the other way, with the STARTDOC Escape calling StartDoc.
72 INT WINAPI StartDocW(HDC hdc, const DOCINFOW* doc)
75 DC *dc = DC_GetDCPtr( hdc );
77 TRACE("DocName = %s Output = %s Datatype = %s\n",
78 debugstr_w(doc->lpszDocName), debugstr_w(doc->lpszOutput),
79 debugstr_w(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 StartDocA(HDC hdc, const DOCINFOA* doc)
94 LPWSTR szDocName = NULL, szOutput = NULL, szDatatype = NULL;
98 docW.cbSize = doc->cbSize;
101 len = MultiByteToWideChar(CP_ACP,0,doc->lpszDocName,-1,NULL,0);
102 szDocName = HeapAlloc(GetProcessHeap(),0,len*sizeof(WCHAR));
103 MultiByteToWideChar(CP_ACP,0,doc->lpszDocName,-1,szDocName,len);
107 len = MultiByteToWideChar(CP_ACP,0,doc->lpszOutput,-1,NULL,0);
108 szOutput = HeapAlloc(GetProcessHeap(),0,len*sizeof(WCHAR));
109 MultiByteToWideChar(CP_ACP,0,doc->lpszOutput,-1,szOutput,len);
111 if (doc->lpszDatatype)
113 len = MultiByteToWideChar(CP_ACP,0,doc->lpszDatatype,-1,NULL,0);
114 szDatatype = HeapAlloc(GetProcessHeap(),0,len*sizeof(WCHAR));
115 MultiByteToWideChar(CP_ACP,0,doc->lpszDatatype,-1,szDatatype,len);
118 docW.lpszDocName = szDocName;
119 docW.lpszOutput = szOutput;
120 docW.lpszDatatype = szDatatype;
121 docW.fwType = doc->fwType;
123 ret = StartDocW(hdc, &docW);
125 HeapFree( GetProcessHeap(), 0, szDocName );
126 HeapFree( GetProcessHeap(), 0, szOutput );
127 HeapFree( GetProcessHeap(), 0, szDatatype );
133 /******************************************************************
137 INT WINAPI EndDoc(HDC hdc)
140 DC *dc = DC_GetDCPtr( hdc );
141 if(!dc) return SP_ERROR;
143 if (dc->funcs->pEndDoc) ret = dc->funcs->pEndDoc( dc->physDev );
144 GDI_ReleaseObj( hdc );
149 /******************************************************************
150 * StartPage [GDI32.@]
153 INT WINAPI StartPage(HDC hdc)
156 DC *dc = DC_GetDCPtr( hdc );
157 if(!dc) return SP_ERROR;
159 if(dc->funcs->pStartPage)
160 ret = dc->funcs->pStartPage( dc->physDev );
163 GDI_ReleaseObj( hdc );
168 /******************************************************************
172 INT WINAPI EndPage(HDC hdc)
174 ABORTPROC abort_proc;
176 DC *dc = DC_GetDCPtr( hdc );
177 if(!dc) return SP_ERROR;
179 if (dc->funcs->pEndPage) ret = dc->funcs->pEndPage( dc->physDev );
180 abort_proc = dc->pAbortProc;
181 GDI_ReleaseObj( hdc );
182 if (abort_proc && !abort_proc( hdc, 0 ))
191 /******************************************************************************
194 INT WINAPI AbortDoc(HDC hdc)
197 DC *dc = DC_GetDCPtr( hdc );
198 if(!dc) return SP_ERROR;
200 if (dc->funcs->pAbortDoc) ret = dc->funcs->pAbortDoc( dc->physDev );
201 GDI_ReleaseObj( hdc );
205 /**********************************************************************
206 * QueryAbort (GDI.155)
208 * Calls the app's AbortProc function if avail.
211 * TRUE if no AbortProc avail or AbortProc wants to continue printing.
212 * FALSE if AbortProc wants to abort printing.
214 BOOL16 WINAPI QueryAbort16(HDC16 hdc16, INT16 reserved)
217 HDC hdc = HDC_32( hdc16 );
218 DC *dc = DC_GetDCPtr( hdc );
222 ERR("Invalid hdc %p\n", hdc);
226 abproc = dc->pAbortProc;
227 GDI_ReleaseObj( hdc );
230 ret = abproc(hdc, 0);
235 /**********************************************************************
238 static BOOL CALLBACK call_abort_proc16( HDC hdc, INT code )
241 DC *dc = DC_GetDCPtr( hdc );
243 if (!dc) return FALSE;
244 proc16 = dc->pAbortProc16;
245 GDI_ReleaseObj( hdc );
251 args[1] = HDC_16(hdc);
253 WOWCallback16Ex( (DWORD)proc16, WCB16_PASCAL, sizeof(args), args, &ret );
260 /**********************************************************************
261 * SetAbortProc (GDI.381)
263 INT16 WINAPI SetAbortProc16(HDC16 hdc16, ABORTPROC16 abrtprc)
265 HDC hdc = HDC_32( hdc16 );
266 DC *dc = DC_GetDCPtr( hdc );
268 if (!dc) return FALSE;
269 dc->pAbortProc16 = abrtprc;
270 GDI_ReleaseObj( hdc );
271 return SetAbortProc( hdc, call_abort_proc16 );
274 /**********************************************************************
275 * SetAbortProc (GDI32.@)
278 INT WINAPI SetAbortProc(HDC hdc, ABORTPROC abrtprc)
280 DC *dc = DC_GetDCPtr( hdc );
282 if (!dc) return FALSE;
283 dc->pAbortProc = abrtprc;
284 GDI_ReleaseObj( hdc );
289 /****************** misc. printer related functions */
292 * The following function should implement a queing system
301 static struct hpq *hpqueue;
303 /**********************************************************************
307 HPQ16 WINAPI CreatePQ16(INT16 size)
314 tmp_size = size << 2;
315 if (!(hpq = GlobalAlloc16(GMEM_SHARE|GMEM_MOVEABLE, tmp_size + 8)))
317 pPQ = GlobalLock16(hpq);
326 FIXME("(%d): stub\n",size);
331 /**********************************************************************
335 INT16 WINAPI DeletePQ16(HPQ16 hPQ)
337 return GlobalFree16((HGLOBAL16)hPQ);
340 /**********************************************************************
341 * ExtractPQ (GDI.232)
344 INT16 WINAPI ExtractPQ16(HPQ16 hPQ)
346 struct hpq *queue, *prev, *current, *currentPrev;
347 int key = 0, tag = -1;
348 currentPrev = prev = NULL;
349 queue = current = hpqueue;
355 currentPrev = current;
356 current = current->next;
359 if (current->key < key)
371 prev->next = queue->next;
373 hpqueue = queue->next;
374 HeapFree(GetProcessHeap(), 0, queue);
377 TRACE("%x got tag %d key %d\n", hPQ, tag, key);
382 /**********************************************************************
386 INT16 WINAPI InsertPQ16(HPQ16 hPQ, INT16 tag, INT16 key)
388 struct hpq *queueItem = HeapAlloc(GetProcessHeap(), 0, sizeof(struct hpq));
389 if(queueItem == NULL) {
390 ERR("Memory exausted!\n");
393 queueItem->next = hpqueue;
395 queueItem->key = key;
396 queueItem->tag = tag;
398 FIXME("(%x %d %d): stub???\n", hPQ, tag, key);
402 /**********************************************************************
406 INT16 WINAPI MinPQ16(HPQ16 hPQ)
408 FIXME("(%x): stub\n", hPQ);
412 /**********************************************************************
416 INT16 WINAPI SizePQ16(HPQ16 hPQ, INT16 sizechange)
418 FIXME("(%x %d): stub\n", hPQ, sizechange);
425 * The following functions implement part of the spooling process to
426 * print manager. I would like to see wine have a version of print managers
427 * that used LPR/LPD. For simplicity print jobs will be sent to a file for
430 typedef struct PRINTJOB
438 } PRINTJOB, *PPRINTJOB;
440 #define MAX_PRINT_JOBS 1
443 PPRINTJOB gPrintJobsTable[MAX_PRINT_JOBS];
446 static PPRINTJOB FindPrintJobFromHandle(HANDLE16 hHandle)
448 return gPrintJobsTable[0];
451 static int CreateSpoolFile(LPCSTR pszOutput)
455 const char *psCmdP = psCmd;
458 /* TTD convert the 'output device' into a spool file name */
460 if (pszOutput == NULL || *pszOutput == '\0')
464 /* @@ Wine registry key: HKCU\Software\Wine\Printing\Spooler */
465 if(!RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Printing\\Spooler", &hkey))
467 DWORD type, count = sizeof(psCmd);
468 RegQueryValueExA(hkey, pszOutput, 0, &type, (LPBYTE)psCmd, &count);
471 if (!psCmd[0] && !strncmp("LPR:",pszOutput,4))
472 sprintf(psCmd,"|lpr -P%s",pszOutput+4);
474 TRACE("Got printerSpoolCommand '%s' for output device '%s'\n",
480 while (*psCmdP && isspace(*psCmdP))
487 TRACE("command: '%s'\n", psCmdP);
493 ERR("pipe() failed!\n");
500 TRACE("In child need to exec %s\n",psCmdP);
505 /* reset signals that we previously set to SIG_IGN */
506 signal( SIGPIPE, SIG_DFL );
507 signal( SIGCHLD, SIG_DFL );
515 TRACE("Need to execute a cmnd and pipe the output to it\n");
521 WCHAR psCmdPW[MAX_PATH];
523 TRACE("Just assume it's a file\n");
526 * The file name can be dos based, we have to find its
527 * Unix correspondant file name
529 MultiByteToWideChar(CP_ACP, 0, psCmdP, -1, psCmdPW, MAX_PATH);
530 if ((buffer = wine_get_unix_file_name(psCmdPW)))
532 if ((fd = open(buffer, O_CREAT | O_TRUNC | O_WRONLY , 0600)) < 0)
534 ERR("Failed to create spool file '%s' ('%s'). (error %s)\n",
535 buffer, psCmdP, strerror(errno));
537 HeapFree(GetProcessHeap(), 0, buffer);
543 static int FreePrintJob(HANDLE16 hJob)
548 pPrintJob = FindPrintJobFromHandle(hJob);
549 if (pPrintJob != NULL)
551 gPrintJobsTable[pPrintJob->nIndex] = NULL;
552 HeapFree(GetProcessHeap(), 0, pPrintJob->pszOutput);
553 HeapFree(GetProcessHeap(), 0, pPrintJob->pszTitle);
554 if (pPrintJob->fd >= 0) close(pPrintJob->fd);
555 HeapFree(GetProcessHeap(), 0, pPrintJob);
561 /**********************************************************************
565 HPJOB16 WINAPI OpenJob16(LPCSTR lpOutput, LPCSTR lpTitle, HDC16 hDC)
567 HPJOB16 hHandle = (HPJOB16)SP_ERROR;
570 TRACE("'%s' '%s' %04x\n", lpOutput, lpTitle, hDC);
572 pPrintJob = gPrintJobsTable[0];
573 if (pPrintJob == NULL)
577 /* Try and create a spool file */
578 fd = CreateSpoolFile(lpOutput);
581 pPrintJob = HeapAlloc(GetProcessHeap(), 0, sizeof(PRINTJOB));
582 if(pPrintJob == NULL) {
583 WARN("Memory exausted!\n");
589 pPrintJob->pszOutput = HeapAlloc(GetProcessHeap(), 0, strlen(lpOutput)+1);
590 strcpy( pPrintJob->pszOutput, lpOutput );
593 pPrintJob->pszTitle = HeapAlloc(GetProcessHeap(), 0, strlen(lpTitle)+1);
594 strcpy( pPrintJob->pszTitle, lpTitle );
596 pPrintJob->hDC = hDC;
598 pPrintJob->nIndex = 0;
599 pPrintJob->hHandle = hHandle;
600 gPrintJobsTable[pPrintJob->nIndex] = pPrintJob;
603 TRACE("return %04x\n", hHandle);
607 /**********************************************************************
611 INT16 WINAPI CloseJob16(HPJOB16 hJob)
614 PPRINTJOB pPrintJob = NULL;
616 TRACE("%04x\n", hJob);
618 pPrintJob = FindPrintJobFromHandle(hJob);
619 if (pPrintJob != NULL)
621 /* Close the spool file */
622 close(pPrintJob->fd);
629 /**********************************************************************
630 * WriteSpool (GDI.241)
633 INT16 WINAPI WriteSpool16(HPJOB16 hJob, LPSTR lpData, INT16 cch)
636 PPRINTJOB pPrintJob = NULL;
638 TRACE("%04x %p %04x\n", hJob, lpData, cch);
640 pPrintJob = FindPrintJobFromHandle(hJob);
641 if (pPrintJob != NULL && pPrintJob->fd >= 0 && cch)
643 if (write(pPrintJob->fd, lpData, cch) != cch)
648 /* FIXME: We just cannot call 16 bit functions from here, since we
649 * have acquired several locks (DC). And we do not really need to.
651 if (pPrintJob->hDC == 0) {
652 TRACE("hDC == 0 so no QueryAbort\n");
654 else if (!(QueryAbort16(pPrintJob->hDC, (nRet == SP_OUTOFDISK) ? nRet : 0 )))
656 CloseJob16(hJob); /* printing aborted */
664 typedef INT (WINAPI *MSGBOX_PROC)( HWND, LPCSTR, LPCSTR, UINT );
666 /**********************************************************************
667 * WriteDialog (GDI.242)
670 INT16 WINAPI WriteDialog16(HPJOB16 hJob, LPSTR lpMsg, INT16 cchMsg)
673 MSGBOX_PROC pMessageBoxA;
676 TRACE("%04x %04x '%s'\n", hJob, cchMsg, lpMsg);
678 if ((mod = GetModuleHandleA("user32.dll")))
680 if ((pMessageBoxA = (MSGBOX_PROC)GetProcAddress( mod, "MessageBoxA" )))
681 ret = pMessageBoxA(0, lpMsg, "Printing Error", MB_OKCANCEL);
687 /**********************************************************************
688 * DeleteJob (GDI.244)
691 INT16 WINAPI DeleteJob16(HPJOB16 hJob, INT16 nNotUsed)
695 TRACE("%04x\n", hJob);
697 nRet = FreePrintJob(hJob);
702 * The following two function would allow a page to be sent to the printer
703 * when it has been processed. For simplicity they havn't been implemented.
704 * This means a whole job has to be processed before it is sent to the printer.
707 /**********************************************************************
708 * StartSpoolPage (GDI.246)
711 INT16 WINAPI StartSpoolPage16(HPJOB16 hJob)
713 FIXME("StartSpoolPage GDI.246 unimplemented\n");
719 /**********************************************************************
720 * EndSpoolPage (GDI.247)
723 INT16 WINAPI EndSpoolPage16(HPJOB16 hJob)
725 FIXME("EndSpoolPage GDI.247 unimplemented\n");
730 /**********************************************************************
731 * GetSpoolJob (GDI.245)
734 DWORD WINAPI GetSpoolJob16(int nOption, LONG param)
737 TRACE("In GetSpoolJob param 0x%x noption %d\n",param, nOption);
742 /******************************************************************
743 * DrvGetPrinterDataInternal
745 * Helper for DrvGetPrinterData
747 static DWORD DrvGetPrinterDataInternal(LPSTR RegStr_Printer,
748 LPBYTE lpPrinterData, int cbData, int what)
752 DWORD dwType, cbQueryData;
754 if (!(RegOpenKeyA(HKEY_LOCAL_MACHINE, RegStr_Printer, &hkey))) {
755 if (what == INT_PD_DEFAULT_DEVMODE) { /* "Default DevMode" */
756 if (!(RegQueryValueExA(hkey, DefaultDevMode, 0, &dwType, 0, &cbQueryData))) {
759 else if ((cbQueryData) && (cbQueryData <= cbData)) {
760 cbQueryData = cbData;
761 if (RegQueryValueExA(hkey, DefaultDevMode, 0,
762 &dwType, lpPrinterData, &cbQueryData))
766 } else { /* "Printer Driver" */
768 RegQueryValueExA(hkey, "Printer Driver", 0,
769 &dwType, lpPrinterData, &cbQueryData);
773 if (hkey) RegCloseKey(hkey);
777 /******************************************************************
778 * DrvGetPrinterData (GDI.282)
781 DWORD WINAPI DrvGetPrinterData16(LPSTR lpPrinter, LPSTR lpProfile,
782 LPDWORD lpType, LPBYTE lpPrinterData,
783 int cbData, LPDWORD lpNeeded)
785 LPSTR RegStr_Printer;
786 HKEY hkey = 0, hkey2 = 0;
788 DWORD dwType, PrinterAttr, cbPrinterAttr, SetData, size;
790 if (HIWORD(lpPrinter))
791 TRACE("printer %s\n",lpPrinter);
793 TRACE("printer %p\n",lpPrinter);
794 if (HIWORD(lpProfile))
795 TRACE("profile %s\n",lpProfile);
797 TRACE("profile %p\n",lpProfile);
798 TRACE("lpType %p\n",lpType);
800 if ((!lpPrinter) || (!lpProfile) || (!lpNeeded))
801 return ERROR_INVALID_PARAMETER;
803 RegStr_Printer = HeapAlloc(GetProcessHeap(), 0,
804 strlen(Printers) + strlen(lpPrinter) + 2);
805 strcpy(RegStr_Printer, Printers);
806 strcat(RegStr_Printer, lpPrinter);
808 if (((DWORD)lpProfile == INT_PD_DEFAULT_DEVMODE) || (HIWORD(lpProfile) &&
809 (!strcmp(lpProfile, DefaultDevMode)))) {
810 size = DrvGetPrinterDataInternal(RegStr_Printer, lpPrinterData, cbData,
811 INT_PD_DEFAULT_DEVMODE);
814 if ((lpPrinterData) && (*lpNeeded > cbData))
815 res = ERROR_MORE_DATA;
817 else res = ERROR_INVALID_PRINTER_NAME;
820 if (((DWORD)lpProfile == INT_PD_DEFAULT_MODEL) || (HIWORD(lpProfile) &&
821 (!strcmp(lpProfile, PrinterModel)))) {
823 if (!lpPrinterData) goto failed;
825 res = ERROR_MORE_DATA;
828 size = DrvGetPrinterDataInternal(RegStr_Printer, lpPrinterData, cbData,
829 INT_PD_DEFAULT_MODEL);
830 if ((size+1) && (lpType))
833 res = ERROR_INVALID_PRINTER_NAME;
837 if ((res = RegOpenKeyA(HKEY_LOCAL_MACHINE, RegStr_Printer, &hkey)))
840 if ((res = RegQueryValueExA(hkey, "Attributes", 0,
841 &dwType, (LPBYTE)&PrinterAttr, &cbPrinterAttr)))
843 if ((res = RegOpenKeyA(hkey, PrinterDriverData, &hkey2)))
846 res = RegQueryValueExA(hkey2, lpProfile, 0,
847 lpType, lpPrinterData, lpNeeded);
848 if ((res != ERROR_CANTREAD) &&
850 (PRINTER_ATTRIBUTE_ENABLE_BIDI|PRINTER_ATTRIBUTE_NETWORK))
851 == PRINTER_ATTRIBUTE_NETWORK))
853 if (!(res) && (*lpType == REG_DWORD) && (*(LPDWORD)lpPrinterData == -1))
854 res = ERROR_INVALID_DATA;
859 RegSetValueExA(hkey2, lpProfile, 0, REG_DWORD, (LPBYTE)&SetData, 4); /* no result returned */
864 if (hkey2) RegCloseKey(hkey2);
865 if (hkey) RegCloseKey(hkey);
866 HeapFree(GetProcessHeap(), 0, RegStr_Printer);
871 /******************************************************************
872 * DrvSetPrinterData (GDI.281)
875 DWORD WINAPI DrvSetPrinterData16(LPSTR lpPrinter, LPSTR lpProfile,
876 DWORD lpType, LPBYTE lpPrinterData,
879 LPSTR RegStr_Printer;
883 if (HIWORD(lpPrinter))
884 TRACE("printer %s\n",lpPrinter);
886 TRACE("printer %p\n",lpPrinter);
887 if (HIWORD(lpProfile))
888 TRACE("profile %s\n",lpProfile);
890 TRACE("profile %p\n",lpProfile);
891 TRACE("lpType %08x\n",lpType);
893 if ((!lpPrinter) || (!lpProfile) ||
894 ((DWORD)lpProfile == INT_PD_DEFAULT_MODEL) || (HIWORD(lpProfile) &&
895 (!strcmp(lpProfile, PrinterModel))))
896 return ERROR_INVALID_PARAMETER;
898 RegStr_Printer = HeapAlloc(GetProcessHeap(), 0,
899 strlen(Printers) + strlen(lpPrinter) + 2);
900 strcpy(RegStr_Printer, Printers);
901 strcat(RegStr_Printer, lpPrinter);
903 if (((DWORD)lpProfile == INT_PD_DEFAULT_DEVMODE) || (HIWORD(lpProfile) &&
904 (!strcmp(lpProfile, DefaultDevMode)))) {
905 if ( RegOpenKeyA(HKEY_LOCAL_MACHINE, RegStr_Printer, &hkey)
907 RegSetValueExA(hkey, DefaultDevMode, 0, REG_BINARY,
908 lpPrinterData, dwSize) != ERROR_SUCCESS )
909 res = ERROR_INVALID_PRINTER_NAME;
913 strcat(RegStr_Printer, "\\");
915 if( (res = RegOpenKeyA(HKEY_LOCAL_MACHINE, RegStr_Printer, &hkey)) ==
919 res = RegDeleteValueA(hkey, lpProfile);
921 res = RegSetValueExA(hkey, lpProfile, 0, lpType,
922 lpPrinterData, dwSize);
926 if (hkey) RegCloseKey(hkey);
927 HeapFree(GetProcessHeap(), 0, RegStr_Printer);