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"
51 #include "gdi_private.h"
53 WINE_DEFAULT_DEBUG_CHANNEL(print);
55 static const char PrinterModel[] = "Printer Model";
56 static const char DefaultDevMode[] = "Default DevMode";
57 static const char PrinterDriverData[] = "PrinterDriverData";
58 static const char Printers[] = "System\\CurrentControlSet\\Control\\Print\\Printers\\";
61 /******************************************************************
64 * StartDoc calls the STARTDOC Escape with the input data pointing to DocName
65 * and the output data (which is used as a second input parameter).pointing at
66 * the whole docinfo structure. This seems to be an undocumented feature of
67 * the STARTDOC Escape.
69 * Note: we now do it the other way, with the STARTDOC Escape calling StartDoc.
71 INT WINAPI StartDocW(HDC hdc, const DOCINFOW* doc)
74 DC *dc = DC_GetDCPtr( hdc );
76 TRACE("DocName = %s Output = %s Datatype = %s\n",
77 debugstr_w(doc->lpszDocName), debugstr_w(doc->lpszOutput),
78 debugstr_w(doc->lpszDatatype));
80 if(!dc) return SP_ERROR;
82 if (dc->funcs->pStartDoc) ret = dc->funcs->pStartDoc( dc->physDev, doc );
83 GDI_ReleaseObj( hdc );
87 /*************************************************************************
91 INT WINAPI StartDocA(HDC hdc, const DOCINFOA* doc)
93 LPWSTR szDocName = NULL, szOutput = NULL, szDatatype = NULL;
97 docW.cbSize = doc->cbSize;
100 len = MultiByteToWideChar(CP_ACP,0,doc->lpszDocName,-1,NULL,0);
101 szDocName = HeapAlloc(GetProcessHeap(),0,len*sizeof(WCHAR));
102 MultiByteToWideChar(CP_ACP,0,doc->lpszDocName,-1,szDocName,len);
106 len = MultiByteToWideChar(CP_ACP,0,doc->lpszOutput,-1,NULL,0);
107 szOutput = HeapAlloc(GetProcessHeap(),0,len*sizeof(WCHAR));
108 MultiByteToWideChar(CP_ACP,0,doc->lpszOutput,-1,szOutput,len);
110 if (doc->lpszDatatype)
112 len = MultiByteToWideChar(CP_ACP,0,doc->lpszDatatype,-1,NULL,0);
113 szDatatype = HeapAlloc(GetProcessHeap(),0,len*sizeof(WCHAR));
114 MultiByteToWideChar(CP_ACP,0,doc->lpszDatatype,-1,szDatatype,len);
117 docW.lpszDocName = szDocName;
118 docW.lpszOutput = szOutput;
119 docW.lpszDatatype = szDatatype;
120 docW.fwType = doc->fwType;
122 ret = StartDocW(hdc, &docW);
124 HeapFree( GetProcessHeap(), 0, szDocName );
125 HeapFree( GetProcessHeap(), 0, szOutput );
126 HeapFree( GetProcessHeap(), 0, szDatatype );
132 /******************************************************************
136 INT WINAPI EndDoc(HDC hdc)
139 DC *dc = DC_GetDCPtr( hdc );
140 if(!dc) return SP_ERROR;
142 if (dc->funcs->pEndDoc) ret = dc->funcs->pEndDoc( dc->physDev );
143 GDI_ReleaseObj( hdc );
148 /******************************************************************
149 * StartPage [GDI32.@]
152 INT WINAPI StartPage(HDC hdc)
155 DC *dc = DC_GetDCPtr( hdc );
156 if(!dc) return SP_ERROR;
158 if(dc->funcs->pStartPage)
159 ret = dc->funcs->pStartPage( dc->physDev );
162 GDI_ReleaseObj( hdc );
167 /******************************************************************
171 INT WINAPI EndPage(HDC hdc)
173 ABORTPROC abort_proc;
175 DC *dc = DC_GetDCPtr( hdc );
176 if(!dc) return SP_ERROR;
178 if (dc->funcs->pEndPage) ret = dc->funcs->pEndPage( dc->physDev );
179 abort_proc = dc->pAbortProc;
180 GDI_ReleaseObj( hdc );
181 if (abort_proc && !abort_proc( hdc, 0 ))
190 /******************************************************************************
193 INT WINAPI AbortDoc(HDC hdc)
196 DC *dc = DC_GetDCPtr( hdc );
197 if(!dc) return SP_ERROR;
199 if (dc->funcs->pAbortDoc) ret = dc->funcs->pAbortDoc( dc->physDev );
200 GDI_ReleaseObj( hdc );
204 /**********************************************************************
205 * QueryAbort (GDI.155)
207 * Calls the app's AbortProc function if avail.
210 * TRUE if no AbortProc avail or AbortProc wants to continue printing.
211 * FALSE if AbortProc wants to abort printing.
213 BOOL16 WINAPI QueryAbort16(HDC16 hdc16, INT16 reserved)
216 HDC hdc = HDC_32( hdc16 );
217 DC *dc = DC_GetDCPtr( hdc );
221 ERR("Invalid hdc %p\n", hdc);
225 abproc = dc->pAbortProc;
226 GDI_ReleaseObj( hdc );
229 ret = abproc(hdc, 0);
234 /**********************************************************************
237 static BOOL CALLBACK call_abort_proc16( HDC hdc, INT code )
240 DC *dc = DC_GetDCPtr( hdc );
242 if (!dc) return FALSE;
243 proc16 = dc->pAbortProc16;
244 GDI_ReleaseObj( hdc );
250 args[1] = HDC_16(hdc);
252 WOWCallback16Ex( (DWORD)proc16, WCB16_PASCAL, sizeof(args), args, &ret );
259 /**********************************************************************
260 * SetAbortProc (GDI.381)
262 INT16 WINAPI SetAbortProc16(HDC16 hdc16, ABORTPROC16 abrtprc)
264 HDC hdc = HDC_32( hdc16 );
265 DC *dc = DC_GetDCPtr( hdc );
267 if (!dc) return FALSE;
268 dc->pAbortProc16 = abrtprc;
269 GDI_ReleaseObj( hdc );
270 return SetAbortProc( hdc, call_abort_proc16 );
273 /**********************************************************************
274 * SetAbortProc (GDI32.@)
277 INT WINAPI SetAbortProc(HDC hdc, ABORTPROC abrtprc)
279 DC *dc = DC_GetDCPtr( hdc );
281 if (!dc) return FALSE;
282 dc->pAbortProc = abrtprc;
283 GDI_ReleaseObj( hdc );
288 /****************** misc. printer related functions */
291 * The following function should implement a queing system
300 static struct hpq *hpqueue;
302 /**********************************************************************
306 HPQ16 WINAPI CreatePQ16(INT16 size)
313 tmp_size = size << 2;
314 if (!(hpq = GlobalAlloc16(GMEM_SHARE|GMEM_MOVEABLE, tmp_size + 8)))
316 pPQ = GlobalLock16(hpq);
325 FIXME("(%d): stub\n",size);
330 /**********************************************************************
334 INT16 WINAPI DeletePQ16(HPQ16 hPQ)
336 return GlobalFree16((HGLOBAL16)hPQ);
339 /**********************************************************************
340 * ExtractPQ (GDI.232)
343 INT16 WINAPI ExtractPQ16(HPQ16 hPQ)
345 struct hpq *queue, *prev, *current, *currentPrev;
346 int key = 0, tag = -1;
347 currentPrev = prev = NULL;
348 queue = current = hpqueue;
354 currentPrev = current;
355 current = current->next;
358 if (current->key < key)
370 prev->next = queue->next;
372 hpqueue = queue->next;
373 HeapFree(GetProcessHeap(), 0, queue);
376 TRACE("%x got tag %d key %d\n", hPQ, tag, key);
381 /**********************************************************************
385 INT16 WINAPI InsertPQ16(HPQ16 hPQ, INT16 tag, INT16 key)
387 struct hpq *queueItem = HeapAlloc(GetProcessHeap(), 0, sizeof(struct hpq));
388 if(queueItem == NULL) {
389 ERR("Memory exausted!\n");
392 queueItem->next = hpqueue;
394 queueItem->key = key;
395 queueItem->tag = tag;
397 FIXME("(%x %d %d): stub???\n", hPQ, tag, key);
401 /**********************************************************************
405 INT16 WINAPI MinPQ16(HPQ16 hPQ)
407 FIXME("(%x): stub\n", hPQ);
411 /**********************************************************************
415 INT16 WINAPI SizePQ16(HPQ16 hPQ, INT16 sizechange)
417 FIXME("(%x %d): stub\n", hPQ, sizechange);
424 * The following functions implement part of the spooling process to
425 * print manager. I would like to see wine have a version of print managers
426 * that used LPR/LPD. For simplicity print jobs will be sent to a file for
429 typedef struct PRINTJOB
437 } PRINTJOB, *PPRINTJOB;
439 #define MAX_PRINT_JOBS 1
442 static PPRINTJOB gPrintJobsTable[MAX_PRINT_JOBS];
445 static PPRINTJOB FindPrintJobFromHandle(HANDLE16 hHandle)
447 return gPrintJobsTable[0];
450 static int CreateSpoolFile(LPCSTR pszOutput)
454 const char *psCmdP = psCmd;
457 /* TTD convert the 'output device' into a spool file name */
459 if (pszOutput == NULL || *pszOutput == '\0')
463 /* @@ Wine registry key: HKCU\Software\Wine\Printing\Spooler */
464 if(!RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Printing\\Spooler", &hkey))
466 DWORD type, count = sizeof(psCmd);
467 RegQueryValueExA(hkey, pszOutput, 0, &type, (LPBYTE)psCmd, &count);
470 if (!psCmd[0] && !strncmp("LPR:",pszOutput,4))
471 sprintf(psCmd,"|lpr -P%s",pszOutput+4);
473 TRACE("Got printerSpoolCommand '%s' for output device '%s'\n",
479 while (*psCmdP && isspace(*psCmdP))
486 TRACE("command: '%s'\n", psCmdP);
492 ERR("pipe() failed!\n");
499 TRACE("In child need to exec %s\n",psCmdP);
504 /* reset signals that we previously set to SIG_IGN */
505 signal( SIGPIPE, SIG_DFL );
506 signal( SIGCHLD, SIG_DFL );
508 execl("/bin/sh", "/bin/sh", "-c", psCmdP, (char*)0);
514 TRACE("Need to execute a cmnd and pipe the output to it\n");
520 WCHAR psCmdPW[MAX_PATH];
522 TRACE("Just assume it's a file\n");
525 * The file name can be dos based, we have to find its
526 * Unix correspondant file name
528 MultiByteToWideChar(CP_ACP, 0, psCmdP, -1, psCmdPW, MAX_PATH);
529 if ((buffer = wine_get_unix_file_name(psCmdPW)))
531 if ((fd = open(buffer, O_CREAT | O_TRUNC | O_WRONLY , 0600)) < 0)
533 ERR("Failed to create spool file '%s' ('%s'). (error %s)\n",
534 buffer, psCmdP, strerror(errno));
536 HeapFree(GetProcessHeap(), 0, buffer);
542 static int FreePrintJob(HANDLE16 hJob)
547 pPrintJob = FindPrintJobFromHandle(hJob);
548 if (pPrintJob != NULL)
550 gPrintJobsTable[pPrintJob->nIndex] = NULL;
551 HeapFree(GetProcessHeap(), 0, pPrintJob->pszOutput);
552 HeapFree(GetProcessHeap(), 0, pPrintJob->pszTitle);
553 if (pPrintJob->fd >= 0) close(pPrintJob->fd);
554 HeapFree(GetProcessHeap(), 0, pPrintJob);
560 /**********************************************************************
564 HPJOB16 WINAPI OpenJob16(LPCSTR lpOutput, LPCSTR lpTitle, HDC16 hDC)
566 HPJOB16 hHandle = (HPJOB16)SP_ERROR;
569 TRACE("'%s' '%s' %04x\n", lpOutput, lpTitle, hDC);
571 pPrintJob = gPrintJobsTable[0];
572 if (pPrintJob == NULL)
576 /* Try and create a spool file */
577 fd = CreateSpoolFile(lpOutput);
580 pPrintJob = HeapAlloc(GetProcessHeap(), 0, sizeof(PRINTJOB));
581 if(pPrintJob == NULL) {
582 WARN("Memory exausted!\n");
588 pPrintJob->pszOutput = HeapAlloc(GetProcessHeap(), 0, strlen(lpOutput)+1);
589 strcpy( pPrintJob->pszOutput, lpOutput );
592 pPrintJob->pszTitle = HeapAlloc(GetProcessHeap(), 0, strlen(lpTitle)+1);
593 strcpy( pPrintJob->pszTitle, lpTitle );
595 pPrintJob->hDC = hDC;
597 pPrintJob->nIndex = 0;
598 pPrintJob->hHandle = hHandle;
599 gPrintJobsTable[pPrintJob->nIndex] = pPrintJob;
602 TRACE("return %04x\n", hHandle);
606 /**********************************************************************
610 INT16 WINAPI CloseJob16(HPJOB16 hJob)
613 PPRINTJOB pPrintJob = NULL;
615 TRACE("%04x\n", hJob);
617 pPrintJob = FindPrintJobFromHandle(hJob);
618 if (pPrintJob != NULL)
620 /* Close the spool file */
621 close(pPrintJob->fd);
628 /**********************************************************************
629 * WriteSpool (GDI.241)
632 INT16 WINAPI WriteSpool16(HPJOB16 hJob, LPSTR lpData, INT16 cch)
635 PPRINTJOB pPrintJob = NULL;
637 TRACE("%04x %p %04x\n", hJob, lpData, cch);
639 pPrintJob = FindPrintJobFromHandle(hJob);
640 if (pPrintJob != NULL && pPrintJob->fd >= 0 && cch)
642 if (write(pPrintJob->fd, lpData, cch) != cch)
647 /* FIXME: We just cannot call 16 bit functions from here, since we
648 * have acquired several locks (DC). And we do not really need to.
650 if (pPrintJob->hDC == 0) {
651 TRACE("hDC == 0 so no QueryAbort\n");
653 else if (!(QueryAbort16(pPrintJob->hDC, (nRet == SP_OUTOFDISK) ? nRet : 0 )))
655 CloseJob16(hJob); /* printing aborted */
663 typedef INT (WINAPI *MSGBOX_PROC)( HWND, LPCSTR, LPCSTR, UINT );
665 /**********************************************************************
666 * WriteDialog (GDI.242)
669 INT16 WINAPI WriteDialog16(HPJOB16 hJob, LPSTR lpMsg, INT16 cchMsg)
672 MSGBOX_PROC pMessageBoxA;
675 TRACE("%04x %04x '%s'\n", hJob, cchMsg, lpMsg);
677 if ((mod = GetModuleHandleA("user32.dll")))
679 if ((pMessageBoxA = (MSGBOX_PROC)GetProcAddress( mod, "MessageBoxA" )))
680 ret = pMessageBoxA(0, lpMsg, "Printing Error", MB_OKCANCEL);
686 /**********************************************************************
687 * DeleteJob (GDI.244)
690 INT16 WINAPI DeleteJob16(HPJOB16 hJob, INT16 nNotUsed)
694 TRACE("%04x\n", hJob);
696 nRet = FreePrintJob(hJob);
701 * The following two function would allow a page to be sent to the printer
702 * when it has been processed. For simplicity they havn't been implemented.
703 * This means a whole job has to be processed before it is sent to the printer.
706 /**********************************************************************
707 * StartSpoolPage (GDI.246)
710 INT16 WINAPI StartSpoolPage16(HPJOB16 hJob)
712 FIXME("StartSpoolPage GDI.246 unimplemented\n");
718 /**********************************************************************
719 * EndSpoolPage (GDI.247)
722 INT16 WINAPI EndSpoolPage16(HPJOB16 hJob)
724 FIXME("EndSpoolPage GDI.247 unimplemented\n");
729 /**********************************************************************
730 * GetSpoolJob (GDI.245)
733 DWORD WINAPI GetSpoolJob16(int nOption, LONG param)
736 TRACE("In GetSpoolJob param 0x%x noption %d\n",param, nOption);
741 /******************************************************************
742 * DrvGetPrinterDataInternal
744 * Helper for DrvGetPrinterData
746 static DWORD DrvGetPrinterDataInternal(LPSTR RegStr_Printer,
747 LPBYTE lpPrinterData, int cbData, int what)
751 DWORD dwType, cbQueryData;
753 if (!(RegOpenKeyA(HKEY_LOCAL_MACHINE, RegStr_Printer, &hkey))) {
754 if (what == INT_PD_DEFAULT_DEVMODE) { /* "Default DevMode" */
755 if (!(RegQueryValueExA(hkey, DefaultDevMode, 0, &dwType, 0, &cbQueryData))) {
758 else if ((cbQueryData) && (cbQueryData <= cbData)) {
759 cbQueryData = cbData;
760 if (RegQueryValueExA(hkey, DefaultDevMode, 0,
761 &dwType, lpPrinterData, &cbQueryData))
765 } else { /* "Printer Driver" */
767 RegQueryValueExA(hkey, "Printer Driver", 0,
768 &dwType, lpPrinterData, &cbQueryData);
772 if (hkey) RegCloseKey(hkey);
776 /******************************************************************
777 * DrvGetPrinterData (GDI.282)
780 DWORD WINAPI DrvGetPrinterData16(LPSTR lpPrinter, LPSTR lpProfile,
781 LPDWORD lpType, LPBYTE lpPrinterData,
782 int cbData, LPDWORD lpNeeded)
784 LPSTR RegStr_Printer;
785 HKEY hkey = 0, hkey2 = 0;
787 DWORD dwType, PrinterAttr, cbPrinterAttr, SetData, size;
789 if (HIWORD(lpPrinter))
790 TRACE("printer %s\n",lpPrinter);
792 TRACE("printer %p\n",lpPrinter);
793 if (HIWORD(lpProfile))
794 TRACE("profile %s\n",lpProfile);
796 TRACE("profile %p\n",lpProfile);
797 TRACE("lpType %p\n",lpType);
799 if ((!lpPrinter) || (!lpProfile) || (!lpNeeded))
800 return ERROR_INVALID_PARAMETER;
802 RegStr_Printer = HeapAlloc(GetProcessHeap(), 0,
803 strlen(Printers) + strlen(lpPrinter) + 2);
804 strcpy(RegStr_Printer, Printers);
805 strcat(RegStr_Printer, lpPrinter);
807 if ((PtrToUlong(lpProfile) == INT_PD_DEFAULT_DEVMODE) || (HIWORD(lpProfile) &&
808 (!strcmp(lpProfile, DefaultDevMode)))) {
809 size = DrvGetPrinterDataInternal(RegStr_Printer, lpPrinterData, cbData,
810 INT_PD_DEFAULT_DEVMODE);
813 if ((lpPrinterData) && (*lpNeeded > cbData))
814 res = ERROR_MORE_DATA;
816 else res = ERROR_INVALID_PRINTER_NAME;
819 if ((PtrToUlong(lpProfile) == INT_PD_DEFAULT_MODEL) || (HIWORD(lpProfile) &&
820 (!strcmp(lpProfile, PrinterModel)))) {
822 if (!lpPrinterData) goto failed;
824 res = ERROR_MORE_DATA;
827 size = DrvGetPrinterDataInternal(RegStr_Printer, lpPrinterData, cbData,
828 INT_PD_DEFAULT_MODEL);
829 if ((size+1) && (lpType))
832 res = ERROR_INVALID_PRINTER_NAME;
836 if ((res = RegOpenKeyA(HKEY_LOCAL_MACHINE, RegStr_Printer, &hkey)))
839 if ((res = RegQueryValueExA(hkey, "Attributes", 0,
840 &dwType, (LPBYTE)&PrinterAttr, &cbPrinterAttr)))
842 if ((res = RegOpenKeyA(hkey, PrinterDriverData, &hkey2)))
845 res = RegQueryValueExA(hkey2, lpProfile, 0,
846 lpType, lpPrinterData, lpNeeded);
847 if ((res != ERROR_CANTREAD) &&
849 (PRINTER_ATTRIBUTE_ENABLE_BIDI|PRINTER_ATTRIBUTE_NETWORK))
850 == PRINTER_ATTRIBUTE_NETWORK))
852 if (!(res) && (*lpType == REG_DWORD) && (*(LPDWORD)lpPrinterData == -1))
853 res = ERROR_INVALID_DATA;
858 RegSetValueExA(hkey2, lpProfile, 0, REG_DWORD, (LPBYTE)&SetData, 4); /* no result returned */
863 if (hkey2) RegCloseKey(hkey2);
864 if (hkey) RegCloseKey(hkey);
865 HeapFree(GetProcessHeap(), 0, RegStr_Printer);
870 /******************************************************************
871 * DrvSetPrinterData (GDI.281)
874 DWORD WINAPI DrvSetPrinterData16(LPSTR lpPrinter, LPSTR lpProfile,
875 DWORD lpType, LPBYTE lpPrinterData,
878 LPSTR RegStr_Printer;
882 if (HIWORD(lpPrinter))
883 TRACE("printer %s\n",lpPrinter);
885 TRACE("printer %p\n",lpPrinter);
886 if (HIWORD(lpProfile))
887 TRACE("profile %s\n",lpProfile);
889 TRACE("profile %p\n",lpProfile);
890 TRACE("lpType %08x\n",lpType);
892 if ((!lpPrinter) || (!lpProfile) ||
893 (PtrToUlong(lpProfile) == INT_PD_DEFAULT_MODEL) || (HIWORD(lpProfile) &&
894 (!strcmp(lpProfile, PrinterModel))))
895 return ERROR_INVALID_PARAMETER;
897 RegStr_Printer = HeapAlloc(GetProcessHeap(), 0,
898 strlen(Printers) + strlen(lpPrinter) + 2);
899 strcpy(RegStr_Printer, Printers);
900 strcat(RegStr_Printer, lpPrinter);
902 if ((PtrToUlong(lpProfile) == INT_PD_DEFAULT_DEVMODE) || (HIWORD(lpProfile) &&
903 (!strcmp(lpProfile, DefaultDevMode)))) {
904 if ( RegOpenKeyA(HKEY_LOCAL_MACHINE, RegStr_Printer, &hkey)
906 RegSetValueExA(hkey, DefaultDevMode, 0, REG_BINARY,
907 lpPrinterData, dwSize) != ERROR_SUCCESS )
908 res = ERROR_INVALID_PRINTER_NAME;
912 strcat(RegStr_Printer, "\\");
914 if( (res = RegOpenKeyA(HKEY_LOCAL_MACHINE, RegStr_Printer, &hkey)) ==
918 res = RegDeleteValueA(hkey, lpProfile);
920 res = RegSetValueExA(hkey, lpProfile, 0, lpType,
921 lpPrinterData, dwSize);
925 if (hkey) RegCloseKey(hkey);
926 HeapFree(GetProcessHeap(), 0, RegStr_Printer);