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"
42 #include "wine/winbase16.h"
43 #include "wine/wingdi16.h"
48 #include "wine/debug.h"
53 WINE_DEFAULT_DEBUG_CHANNEL(print);
55 static char PrinterModel[] = "Printer Model";
56 static char DefaultDevMode[] = "Default DevMode";
57 static char PrinterDriverData[] = "PrinterDriverData";
58 static 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 StartDocA(HDC hdc, const DOCINFOA* doc)
74 DC *dc = DC_GetDCPtr( hdc );
76 TRACE("DocName = '%s' Output = '%s' Datatype = '%s'\n",
77 doc->lpszDocName, doc->lpszOutput, doc->lpszDatatype);
79 if(!dc) return SP_ERROR;
81 if (dc->funcs->pStartDoc) ret = dc->funcs->pStartDoc( dc->physDev, doc );
82 GDI_ReleaseObj( hdc );
86 /*************************************************************************
90 INT WINAPI StartDocW(HDC hdc, const DOCINFOW* doc)
95 docA.cbSize = doc->cbSize;
96 docA.lpszDocName = doc->lpszDocName ?
97 HEAP_strdupWtoA( GetProcessHeap(), 0, doc->lpszDocName ) : NULL;
98 docA.lpszOutput = doc->lpszOutput ?
99 HEAP_strdupWtoA( GetProcessHeap(), 0, doc->lpszOutput ) : NULL;
100 docA.lpszDatatype = doc->lpszDatatype ?
101 HEAP_strdupWtoA( GetProcessHeap(), 0, doc->lpszDatatype ) : NULL;
102 docA.fwType = doc->fwType;
104 ret = StartDocA(hdc, &docA);
107 HeapFree( GetProcessHeap(), 0, (LPSTR)docA.lpszDocName );
109 HeapFree( GetProcessHeap(), 0, (LPSTR)docA.lpszOutput );
110 if(docA.lpszDatatype)
111 HeapFree( GetProcessHeap(), 0, (LPSTR)docA.lpszDatatype );
117 /******************************************************************
121 INT WINAPI EndDoc(HDC hdc)
124 DC *dc = DC_GetDCPtr( hdc );
125 if(!dc) return SP_ERROR;
127 if (dc->funcs->pEndDoc) ret = dc->funcs->pEndDoc( dc->physDev );
128 GDI_ReleaseObj( hdc );
133 /******************************************************************
134 * StartPage [GDI32.@]
137 INT WINAPI StartPage(HDC hdc)
140 DC *dc = DC_GetDCPtr( hdc );
141 if(!dc) return SP_ERROR;
143 if(dc->funcs->pStartPage)
144 ret = dc->funcs->pStartPage( dc->physDev );
147 GDI_ReleaseObj( hdc );
152 /******************************************************************
156 INT WINAPI EndPage(HDC hdc)
158 ABORTPROC abort_proc;
160 DC *dc = DC_GetDCPtr( hdc );
161 if(!dc) return SP_ERROR;
163 if (dc->funcs->pEndPage) ret = dc->funcs->pEndPage( dc->physDev );
164 abort_proc = dc->pAbortProc;
165 GDI_ReleaseObj( hdc );
166 if (abort_proc && !abort_proc( hdc, 0 ))
175 /******************************************************************************
178 INT WINAPI AbortDoc(HDC hdc)
181 DC *dc = DC_GetDCPtr( hdc );
182 if(!dc) return SP_ERROR;
184 if (dc->funcs->pAbortDoc) ret = dc->funcs->pAbortDoc( dc->physDev );
185 GDI_ReleaseObj( hdc );
189 /**********************************************************************
190 * QueryAbort (GDI.155)
192 * Calls the app's AbortProc function if avail.
195 * TRUE if no AbortProc avail or AbortProc wants to continue printing.
196 * FALSE if AbortProc wants to abort printing.
198 BOOL16 WINAPI QueryAbort16(HDC16 hdc16, INT16 reserved)
201 HDC hdc = HDC_32( hdc16 );
202 DC *dc = DC_GetDCPtr( hdc );
206 ERR("Invalid hdc %p\n", hdc);
210 abproc = dc->pAbortProc;
211 GDI_ReleaseObj( hdc );
214 ret = abproc(hdc, 0);
219 /**********************************************************************
222 static BOOL CALLBACK call_abort_proc16( HDC hdc, INT code )
225 DC *dc = DC_GetDCPtr( hdc );
227 if (!dc) return FALSE;
228 proc16 = dc->pAbortProc16;
229 GDI_ReleaseObj( hdc );
235 args[1] = HDC_16(hdc);
237 WOWCallback16Ex( (DWORD)proc16, WCB16_PASCAL, sizeof(args), args, &ret );
244 /**********************************************************************
245 * SetAbortProc (GDI.381)
247 INT16 WINAPI SetAbortProc16(HDC16 hdc16, ABORTPROC16 abrtprc)
249 HDC hdc = HDC_32( hdc16 );
250 DC *dc = DC_GetDCPtr( hdc );
252 if (!dc) return FALSE;
253 dc->pAbortProc16 = abrtprc;
254 GDI_ReleaseObj( hdc );
255 return SetAbortProc( hdc, call_abort_proc16 );
258 /**********************************************************************
259 * SetAbortProc (GDI32.@)
262 INT WINAPI SetAbortProc(HDC hdc, ABORTPROC abrtprc)
264 DC *dc = DC_GetDCPtr( hdc );
266 if (!dc) return FALSE;
267 dc->pAbortProc = abrtprc;
268 GDI_ReleaseObj( hdc );
273 /****************** misc. printer related functions */
276 * The following function should implement a queing system
285 static struct hpq *hpqueue;
287 /**********************************************************************
291 HPQ16 WINAPI CreatePQ16(INT16 size)
298 tmp_size = size << 2;
299 if (!(hpq = GlobalAlloc16(GMEM_SHARE|GMEM_MOVEABLE, tmp_size + 8)))
301 pPQ = GlobalLock16(hpq);
310 FIXME("(%d): stub\n",size);
315 /**********************************************************************
319 INT16 WINAPI DeletePQ16(HPQ16 hPQ)
321 return GlobalFree16((HGLOBAL16)hPQ);
324 /**********************************************************************
325 * ExtractPQ (GDI.232)
328 INT16 WINAPI ExtractPQ16(HPQ16 hPQ)
330 struct hpq *queue, *prev, *current, *currentPrev;
331 int key = 0, tag = -1;
332 currentPrev = prev = NULL;
333 queue = current = hpqueue;
339 currentPrev = current;
340 current = current->next;
343 if (current->key < key)
355 prev->next = queue->next;
357 hpqueue = queue->next;
358 HeapFree(GetProcessHeap(), 0, queue);
361 TRACE("%x got tag %d key %d\n", hPQ, tag, key);
366 /**********************************************************************
370 INT16 WINAPI InsertPQ16(HPQ16 hPQ, INT16 tag, INT16 key)
372 struct hpq *queueItem = HeapAlloc(GetProcessHeap(), 0, sizeof(struct hpq));
373 if(queueItem == NULL) {
374 ERR("Memory exausted!\n");
377 queueItem->next = hpqueue;
379 queueItem->key = key;
380 queueItem->tag = tag;
382 FIXME("(%x %d %d): stub???\n", hPQ, tag, key);
386 /**********************************************************************
390 INT16 WINAPI MinPQ16(HPQ16 hPQ)
392 FIXME("(%x): stub\n", hPQ);
396 /**********************************************************************
400 INT16 WINAPI SizePQ16(HPQ16 hPQ, INT16 sizechange)
402 FIXME("(%x %d): stub\n", hPQ, sizechange);
409 * The following functions implement part of the spooling process to
410 * print manager. I would like to see wine have a version of print managers
411 * that used LPR/LPD. For simplicity print jobs will be sent to a file for
414 typedef struct PRINTJOB
422 } PRINTJOB, *PPRINTJOB;
424 #define MAX_PRINT_JOBS 1
427 PPRINTJOB gPrintJobsTable[MAX_PRINT_JOBS];
430 static PPRINTJOB FindPrintJobFromHandle(HANDLE16 hHandle)
432 return gPrintJobsTable[0];
435 static int CreateSpoolFile(LPCSTR pszOutput)
439 char *psCmdP = psCmd;
442 /* TTD convert the 'output device' into a spool file name */
444 if (pszOutput == NULL || *pszOutput == '\0')
448 if (!strncmp("LPR:",pszOutput,4))
450 if(!RegOpenKeyA(HKEY_LOCAL_MACHINE, "Software\\Wine\\Wine\\Config\\spooler", &hkey))
452 DWORD type, count = sizeof(psCmd);
453 RegQueryValueExA(hkey, pszOutput, 0, &type, psCmd, &count);
457 sprintf(psCmd,"|lpr -P%s",pszOutput+4);
461 if(!RegOpenKeyA(HKEY_LOCAL_MACHINE, "Software\\Wine\\Wine\\Config\\spooler", &hkey))
463 DWORD type, count = sizeof(psCmd);
464 RegQueryValueExA(hkey, pszOutput, 0, &type, psCmd, &count);
468 TRACE("Got printerSpoolCommand '%s' for output device '%s'\n",
471 psCmdP = (char *)pszOutput;
474 while (*psCmdP && isspace(*psCmdP))
481 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");
512 char buffer[MAX_PATH];
514 TRACE("Just assume it's a file\n");
517 * The file name can be dos based, we have to find its
518 * Unix correspondant file name
520 wine_get_unix_file_name(psCmdP, buffer, sizeof(buffer));
522 if ((fd = open(buffer, O_CREAT | O_TRUNC | O_WRONLY , 0600)) < 0)
524 ERR("Failed to create spool file '%s' ('%s'). (error %s)\n",
525 buffer, psCmdP, strerror(errno));
531 static int FreePrintJob(HANDLE16 hJob)
536 pPrintJob = FindPrintJobFromHandle(hJob);
537 if (pPrintJob != NULL)
539 gPrintJobsTable[pPrintJob->nIndex] = NULL;
540 HeapFree(GetProcessHeap(), 0, pPrintJob->pszOutput);
541 HeapFree(GetProcessHeap(), 0, pPrintJob->pszTitle);
542 if (pPrintJob->fd >= 0) close(pPrintJob->fd);
543 HeapFree(GetProcessHeap(), 0, pPrintJob);
549 /**********************************************************************
553 HPJOB16 WINAPI OpenJob16(LPCSTR lpOutput, LPCSTR lpTitle, HDC16 hDC)
555 HPJOB16 hHandle = (HPJOB16)SP_ERROR;
558 TRACE("'%s' '%s' %04x\n", lpOutput, lpTitle, hDC);
560 pPrintJob = gPrintJobsTable[0];
561 if (pPrintJob == NULL)
565 /* Try and create a spool file */
566 fd = CreateSpoolFile(lpOutput);
569 pPrintJob = HeapAlloc(GetProcessHeap(), 0, sizeof(PRINTJOB));
570 if(pPrintJob == NULL) {
571 WARN("Memory exausted!\n");
577 pPrintJob->pszOutput = HeapAlloc(GetProcessHeap(), 0, strlen(lpOutput)+1);
578 strcpy( pPrintJob->pszOutput, lpOutput );
581 pPrintJob->pszTitle = HeapAlloc(GetProcessHeap(), 0, strlen(lpTitle)+1);
582 strcpy( pPrintJob->pszTitle, lpTitle );
584 pPrintJob->hDC = hDC;
586 pPrintJob->nIndex = 0;
587 pPrintJob->hHandle = hHandle;
588 gPrintJobsTable[pPrintJob->nIndex] = pPrintJob;
591 TRACE("return %04x\n", hHandle);
595 /**********************************************************************
599 INT16 WINAPI CloseJob16(HPJOB16 hJob)
602 PPRINTJOB pPrintJob = NULL;
604 TRACE("%04x\n", hJob);
606 pPrintJob = FindPrintJobFromHandle(hJob);
607 if (pPrintJob != NULL)
609 /* Close the spool file */
610 close(pPrintJob->fd);
617 /**********************************************************************
618 * WriteSpool (GDI.241)
621 INT16 WINAPI WriteSpool16(HPJOB16 hJob, LPSTR lpData, INT16 cch)
624 PPRINTJOB pPrintJob = NULL;
626 TRACE("%04x %08lx %04x\n", hJob, (DWORD)lpData, cch);
628 pPrintJob = FindPrintJobFromHandle(hJob);
629 if (pPrintJob != NULL && pPrintJob->fd >= 0 && cch)
631 if (write(pPrintJob->fd, lpData, cch) != cch)
636 /* FIXME: We just cannot call 16 bit functions from here, since we
637 * have acquired several locks (DC). And we do not really need to.
639 if (pPrintJob->hDC == 0) {
640 TRACE("hDC == 0 so no QueryAbort\n");
642 else if (!(QueryAbort16(pPrintJob->hDC, (nRet == SP_OUTOFDISK) ? nRet : 0 )))
644 CloseJob16(hJob); /* printing aborted */
652 typedef INT (WINAPI *MSGBOX_PROC)( HWND, LPCSTR, LPCSTR, UINT );
654 /**********************************************************************
655 * WriteDialog (GDI.242)
658 INT16 WINAPI WriteDialog16(HPJOB16 hJob, LPSTR lpMsg, INT16 cchMsg)
661 MSGBOX_PROC pMessageBoxA;
664 TRACE("%04x %04x '%s'\n", hJob, cchMsg, lpMsg);
666 if ((mod = GetModuleHandleA("user32.dll")))
668 if ((pMessageBoxA = (MSGBOX_PROC)GetProcAddress( mod, "MessageBoxA" )))
669 ret = pMessageBoxA(0, lpMsg, "Printing Error", MB_OKCANCEL);
675 /**********************************************************************
676 * DeleteJob (GDI.244)
679 INT16 WINAPI DeleteJob16(HPJOB16 hJob, INT16 nNotUsed)
683 TRACE("%04x\n", hJob);
685 nRet = FreePrintJob(hJob);
690 * The following two function would allow a page to be sent to the printer
691 * when it has been processed. For simplicity they havn't been implemented.
692 * This means a whole job has to be processed before it is sent to the printer.
695 /**********************************************************************
696 * StartSpoolPage (GDI.246)
699 INT16 WINAPI StartSpoolPage16(HPJOB16 hJob)
701 FIXME("StartSpoolPage GDI.246 unimplemented\n");
707 /**********************************************************************
708 * EndSpoolPage (GDI.247)
711 INT16 WINAPI EndSpoolPage16(HPJOB16 hJob)
713 FIXME("EndSpoolPage GDI.247 unimplemented\n");
718 /**********************************************************************
719 * GetSpoolJob (GDI.245)
722 DWORD WINAPI GetSpoolJob16(int nOption, LONG param)
725 TRACE("In GetSpoolJob param 0x%lx noption %d\n",param, nOption);
730 /******************************************************************
731 * DrvGetPrinterDataInternal
733 * Helper for DrvGetPrinterData
735 static DWORD DrvGetPrinterDataInternal(LPSTR RegStr_Printer,
736 LPBYTE lpPrinterData, int cbData, int what)
740 DWORD dwType, cbQueryData;
742 if (!(RegOpenKeyA(HKEY_LOCAL_MACHINE, RegStr_Printer, &hkey))) {
743 if (what == INT_PD_DEFAULT_DEVMODE) { /* "Default DevMode" */
744 if (!(RegQueryValueExA(hkey, DefaultDevMode, 0, &dwType, 0, &cbQueryData))) {
747 else if ((cbQueryData) && (cbQueryData <= cbData)) {
748 cbQueryData = cbData;
749 if (RegQueryValueExA(hkey, DefaultDevMode, 0,
750 &dwType, lpPrinterData, &cbQueryData))
754 } else { /* "Printer Driver" */
756 RegQueryValueExA(hkey, "Printer Driver", 0,
757 &dwType, lpPrinterData, &cbQueryData);
761 if (hkey) RegCloseKey(hkey);
765 /******************************************************************
766 * DrvGetPrinterData (GDI.282)
769 DWORD WINAPI DrvGetPrinterData16(LPSTR lpPrinter, LPSTR lpProfile,
770 LPDWORD lpType, LPBYTE lpPrinterData,
771 int cbData, LPDWORD lpNeeded)
773 LPSTR RegStr_Printer;
774 HKEY hkey = 0, hkey2 = 0;
776 DWORD dwType, PrinterAttr, cbPrinterAttr, SetData, size;
778 if (HIWORD(lpPrinter))
779 TRACE("printer %s\n",lpPrinter);
781 TRACE("printer %p\n",lpPrinter);
782 if (HIWORD(lpProfile))
783 TRACE("profile %s\n",lpProfile);
785 TRACE("profile %p\n",lpProfile);
786 TRACE("lpType %p\n",lpType);
788 if ((!lpPrinter) || (!lpProfile) || (!lpNeeded))
789 return ERROR_INVALID_PARAMETER;
791 RegStr_Printer = HeapAlloc(GetProcessHeap(), 0,
792 strlen(Printers) + strlen(lpPrinter) + 2);
793 strcpy(RegStr_Printer, Printers);
794 strcat(RegStr_Printer, lpPrinter);
796 if (((DWORD)lpProfile == INT_PD_DEFAULT_DEVMODE) || (HIWORD(lpProfile) &&
797 (!strcmp(lpProfile, DefaultDevMode)))) {
798 size = DrvGetPrinterDataInternal(RegStr_Printer, lpPrinterData, cbData,
799 INT_PD_DEFAULT_DEVMODE);
802 if ((lpPrinterData) && (*lpNeeded > cbData))
803 res = ERROR_MORE_DATA;
805 else res = ERROR_INVALID_PRINTER_NAME;
808 if (((DWORD)lpProfile == INT_PD_DEFAULT_MODEL) || (HIWORD(lpProfile) &&
809 (!strcmp(lpProfile, PrinterModel)))) {
811 if (!lpPrinterData) goto failed;
813 res = ERROR_MORE_DATA;
816 size = DrvGetPrinterDataInternal(RegStr_Printer, lpPrinterData, cbData,
817 INT_PD_DEFAULT_MODEL);
818 if ((size+1) && (lpType))
821 res = ERROR_INVALID_PRINTER_NAME;
825 if ((res = RegOpenKeyA(HKEY_LOCAL_MACHINE, RegStr_Printer, &hkey)))
828 if ((res = RegQueryValueExA(hkey, "Attributes", 0,
829 &dwType, (LPBYTE)&PrinterAttr, &cbPrinterAttr)))
831 if ((res = RegOpenKeyA(hkey, PrinterDriverData, &hkey2)))
834 res = RegQueryValueExA(hkey2, lpProfile, 0,
835 lpType, lpPrinterData, lpNeeded);
836 if ((res != ERROR_CANTREAD) &&
838 (PRINTER_ATTRIBUTE_ENABLE_BIDI|PRINTER_ATTRIBUTE_NETWORK))
839 == PRINTER_ATTRIBUTE_NETWORK))
841 if (!(res) && (*lpType == REG_DWORD) && (*(LPDWORD)lpPrinterData == -1))
842 res = ERROR_INVALID_DATA;
847 RegSetValueExA(hkey2, lpProfile, 0, REG_DWORD, (LPBYTE)&SetData, 4); /* no result returned */
852 if (hkey2) RegCloseKey(hkey2);
853 if (hkey) RegCloseKey(hkey);
854 HeapFree(GetProcessHeap(), 0, RegStr_Printer);
859 /******************************************************************
860 * DrvSetPrinterData (GDI.281)
863 DWORD WINAPI DrvSetPrinterData16(LPSTR lpPrinter, LPSTR lpProfile,
864 DWORD lpType, LPBYTE lpPrinterData,
867 LPSTR RegStr_Printer;
871 if (HIWORD(lpPrinter))
872 TRACE("printer %s\n",lpPrinter);
874 TRACE("printer %p\n",lpPrinter);
875 if (HIWORD(lpProfile))
876 TRACE("profile %s\n",lpProfile);
878 TRACE("profile %p\n",lpProfile);
879 TRACE("lpType %08lx\n",lpType);
881 if ((!lpPrinter) || (!lpProfile) ||
882 ((DWORD)lpProfile == INT_PD_DEFAULT_MODEL) || (HIWORD(lpProfile) &&
883 (!strcmp(lpProfile, PrinterModel))))
884 return ERROR_INVALID_PARAMETER;
886 RegStr_Printer = HeapAlloc(GetProcessHeap(), 0,
887 strlen(Printers) + strlen(lpPrinter) + 2);
888 strcpy(RegStr_Printer, Printers);
889 strcat(RegStr_Printer, lpPrinter);
891 if (((DWORD)lpProfile == INT_PD_DEFAULT_DEVMODE) || (HIWORD(lpProfile) &&
892 (!strcmp(lpProfile, DefaultDevMode)))) {
893 if ( RegOpenKeyA(HKEY_LOCAL_MACHINE, RegStr_Printer, &hkey)
895 RegSetValueExA(hkey, DefaultDevMode, 0, REG_BINARY,
896 lpPrinterData, dwSize) != ERROR_SUCCESS )
897 res = ERROR_INVALID_PRINTER_NAME;
901 strcat(RegStr_Printer, "\\");
903 if( (res = RegOpenKeyA(HKEY_LOCAL_MACHINE, RegStr_Printer, &hkey)) ==
907 res = RegDeleteValueA(hkey, lpProfile);
909 res = RegSetValueExA(hkey, lpProfile, 0, lpType,
910 lpPrinterData, dwSize);
914 if (hkey) RegCloseKey(hkey);
915 HeapFree(GetProcessHeap(), 0, RegStr_Printer);