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"
54 WINE_DEFAULT_DEBUG_CHANNEL(print);
56 static char PrinterModel[] = "Printer Model";
57 static char DefaultDevMode[] = "Default DevMode";
58 static char PrinterDriverData[] = "PrinterDriverData";
59 static 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 StartDocA(HDC hdc, const DOCINFOA* doc)
75 DC *dc = DC_GetDCPtr( hdc );
77 TRACE("DocName = '%s' Output = '%s' Datatype = '%s'\n",
78 doc->lpszDocName, doc->lpszOutput, 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 StartDocW(HDC hdc, const DOCINFOW* doc)
96 docA.cbSize = doc->cbSize;
97 docA.lpszDocName = doc->lpszDocName ?
98 HEAP_strdupWtoA( GetProcessHeap(), 0, doc->lpszDocName ) : NULL;
99 docA.lpszOutput = doc->lpszOutput ?
100 HEAP_strdupWtoA( GetProcessHeap(), 0, doc->lpszOutput ) : NULL;
101 docA.lpszDatatype = doc->lpszDatatype ?
102 HEAP_strdupWtoA( GetProcessHeap(), 0, doc->lpszDatatype ) : NULL;
103 docA.fwType = doc->fwType;
105 ret = StartDocA(hdc, &docA);
108 HeapFree( GetProcessHeap(), 0, (LPSTR)docA.lpszDocName );
110 HeapFree( GetProcessHeap(), 0, (LPSTR)docA.lpszOutput );
111 if(docA.lpszDatatype)
112 HeapFree( GetProcessHeap(), 0, (LPSTR)docA.lpszDatatype );
118 /******************************************************************
122 INT WINAPI EndDoc(HDC hdc)
125 DC *dc = DC_GetDCPtr( hdc );
126 if(!dc) return SP_ERROR;
128 if (dc->funcs->pEndDoc) ret = dc->funcs->pEndDoc( dc->physDev );
129 GDI_ReleaseObj( hdc );
134 /******************************************************************
135 * StartPage [GDI32.@]
138 INT WINAPI StartPage(HDC hdc)
141 DC *dc = DC_GetDCPtr( hdc );
142 if(!dc) return SP_ERROR;
144 if(dc->funcs->pStartPage)
145 ret = dc->funcs->pStartPage( dc->physDev );
148 GDI_ReleaseObj( hdc );
153 /******************************************************************
157 INT WINAPI EndPage(HDC hdc)
159 ABORTPROC abort_proc;
161 DC *dc = DC_GetDCPtr( hdc );
162 if(!dc) return SP_ERROR;
164 if (dc->funcs->pEndPage) ret = dc->funcs->pEndPage( dc->physDev );
165 abort_proc = dc->pAbortProc;
166 GDI_ReleaseObj( hdc );
167 if (abort_proc && !abort_proc( hdc, 0 ))
176 /******************************************************************************
179 INT WINAPI AbortDoc(HDC hdc)
182 DC *dc = DC_GetDCPtr( hdc );
183 if(!dc) return SP_ERROR;
185 if (dc->funcs->pAbortDoc) ret = dc->funcs->pAbortDoc( dc->physDev );
186 GDI_ReleaseObj( hdc );
190 /**********************************************************************
191 * QueryAbort (GDI.155)
193 * Calls the app's AbortProc function if avail.
196 * TRUE if no AbortProc avail or AbortProc wants to continue printing.
197 * FALSE if AbortProc wants to abort printing.
199 BOOL16 WINAPI QueryAbort16(HDC16 hdc16, INT16 reserved)
202 HDC hdc = HDC_32( hdc16 );
203 DC *dc = DC_GetDCPtr( hdc );
207 ERR("Invalid hdc %p\n", hdc);
211 abproc = dc->pAbortProc;
212 GDI_ReleaseObj( hdc );
215 ret = abproc(hdc, 0);
220 /**********************************************************************
223 static BOOL CALLBACK call_abort_proc16( HDC hdc, INT code )
226 DC *dc = DC_GetDCPtr( hdc );
228 if (!dc) return FALSE;
229 proc16 = dc->pAbortProc16;
230 GDI_ReleaseObj( hdc );
236 args[1] = HDC_16(hdc);
238 WOWCallback16Ex( (DWORD)proc16, WCB16_PASCAL, sizeof(args), args, &ret );
245 /**********************************************************************
246 * SetAbortProc (GDI.381)
248 INT16 WINAPI SetAbortProc16(HDC16 hdc16, ABORTPROC16 abrtprc)
250 HDC hdc = HDC_32( hdc16 );
251 DC *dc = DC_GetDCPtr( hdc );
253 if (!dc) return FALSE;
254 dc->pAbortProc16 = abrtprc;
255 GDI_ReleaseObj( hdc );
256 return SetAbortProc( hdc, call_abort_proc16 );
259 /**********************************************************************
260 * SetAbortProc (GDI32.@)
263 INT WINAPI SetAbortProc(HDC hdc, ABORTPROC abrtprc)
265 DC *dc = DC_GetDCPtr( hdc );
267 if (!dc) return FALSE;
268 dc->pAbortProc = abrtprc;
269 GDI_ReleaseObj( hdc );
274 /****************** misc. printer related functions */
277 * The following function should implement a queing system
286 static struct hpq *hpqueue;
288 /**********************************************************************
292 HPQ16 WINAPI CreatePQ16(INT16 size)
299 tmp_size = size << 2;
300 if (!(hpq = GlobalAlloc16(GMEM_SHARE|GMEM_MOVEABLE, tmp_size + 8)))
302 pPQ = GlobalLock16(hpq);
311 FIXME("(%d): stub\n",size);
316 /**********************************************************************
320 INT16 WINAPI DeletePQ16(HPQ16 hPQ)
322 return GlobalFree16((HGLOBAL16)hPQ);
325 /**********************************************************************
326 * ExtractPQ (GDI.232)
329 INT16 WINAPI ExtractPQ16(HPQ16 hPQ)
331 struct hpq *queue, *prev, *current, *currentPrev;
332 int key = 0, tag = -1;
333 currentPrev = prev = NULL;
334 queue = current = hpqueue;
340 currentPrev = current;
341 current = current->next;
344 if (current->key < key)
356 prev->next = queue->next;
358 hpqueue = queue->next;
359 HeapFree(GetProcessHeap(), 0, queue);
362 TRACE("%x got tag %d key %d\n", hPQ, tag, key);
367 /**********************************************************************
371 INT16 WINAPI InsertPQ16(HPQ16 hPQ, INT16 tag, INT16 key)
373 struct hpq *queueItem = HeapAlloc(GetProcessHeap(), 0, sizeof(struct hpq));
374 if(queueItem == NULL) {
375 ERR("Memory exausted!\n");
378 queueItem->next = hpqueue;
380 queueItem->key = key;
381 queueItem->tag = tag;
383 FIXME("(%x %d %d): stub???\n", hPQ, tag, key);
387 /**********************************************************************
391 INT16 WINAPI MinPQ16(HPQ16 hPQ)
393 FIXME("(%x): stub\n", hPQ);
397 /**********************************************************************
401 INT16 WINAPI SizePQ16(HPQ16 hPQ, INT16 sizechange)
403 FIXME("(%x %d): stub\n", hPQ, sizechange);
410 * The following functions implement part of the spooling process to
411 * print manager. I would like to see wine have a version of print managers
412 * that used LPR/LPD. For simplicity print jobs will be sent to a file for
415 typedef struct PRINTJOB
423 } PRINTJOB, *PPRINTJOB;
425 #define MAX_PRINT_JOBS 1
428 PPRINTJOB gPrintJobsTable[MAX_PRINT_JOBS];
431 static PPRINTJOB FindPrintJobFromHandle(HANDLE16 hHandle)
433 return gPrintJobsTable[0];
436 static int CreateSpoolFile(LPCSTR pszOutput)
440 char *psCmdP = psCmd;
443 /* TTD convert the 'output device' into a spool file name */
445 if (pszOutput == NULL || *pszOutput == '\0')
449 if (!strncmp("LPR:",pszOutput,4))
451 if(!RegOpenKeyA(HKEY_LOCAL_MACHINE, "Software\\Wine\\Wine\\Config\\spooler", &hkey))
453 DWORD type, count = sizeof(psCmd);
454 RegQueryValueExA(hkey, pszOutput, 0, &type, psCmd, &count);
458 sprintf(psCmd,"|lpr -P%s",pszOutput+4);
462 if(!RegOpenKeyA(HKEY_LOCAL_MACHINE, "Software\\Wine\\Wine\\Config\\spooler", &hkey))
464 DWORD type, count = sizeof(psCmd);
465 RegQueryValueExA(hkey, pszOutput, 0, &type, psCmd, &count);
469 TRACE("Got printerSpoolCommand '%s' for output device '%s'\n",
472 psCmdP = (char *)pszOutput;
475 while (*psCmdP && isspace(*psCmdP))
482 TRACE("command: '%s'\n", psCmdP);
487 ERR("pipe() failed!\n");
494 TRACE("In child need to exec %s\n",psCmdP);
499 /* reset signals that we previously set to SIG_IGN */
500 signal( SIGPIPE, SIG_DFL );
501 signal( SIGCHLD, SIG_DFL );
509 TRACE("Need to execute a cmnd and pipe the output to it\n");
513 char buffer[MAX_PATH];
515 TRACE("Just assume it's a file\n");
518 * The file name can be dos based, we have to find its
519 * Unix correspondant file name
521 wine_get_unix_file_name(psCmdP, buffer, sizeof(buffer));
523 if ((fd = open(buffer, O_CREAT | O_TRUNC | O_WRONLY , 0600)) < 0)
525 ERR("Failed to create spool file '%s' ('%s'). (error %s)\n",
526 buffer, psCmdP, strerror(errno));
532 static int FreePrintJob(HANDLE16 hJob)
537 pPrintJob = FindPrintJobFromHandle(hJob);
538 if (pPrintJob != NULL)
540 gPrintJobsTable[pPrintJob->nIndex] = NULL;
541 HeapFree(GetProcessHeap(), 0, pPrintJob->pszOutput);
542 HeapFree(GetProcessHeap(), 0, pPrintJob->pszTitle);
543 if (pPrintJob->fd >= 0) close(pPrintJob->fd);
544 HeapFree(GetProcessHeap(), 0, pPrintJob);
550 /**********************************************************************
554 HPJOB16 WINAPI OpenJob16(LPCSTR lpOutput, LPCSTR lpTitle, HDC16 hDC)
556 HPJOB16 hHandle = (HPJOB16)SP_ERROR;
559 TRACE("'%s' '%s' %04x\n", lpOutput, lpTitle, hDC);
561 pPrintJob = gPrintJobsTable[0];
562 if (pPrintJob == NULL)
566 /* Try and create a spool file */
567 fd = CreateSpoolFile(lpOutput);
570 pPrintJob = HeapAlloc(GetProcessHeap(), 0, sizeof(PRINTJOB));
571 if(pPrintJob == NULL) {
572 WARN("Memory exausted!\n");
578 pPrintJob->pszOutput = HeapAlloc(GetProcessHeap(), 0, strlen(lpOutput)+1);
579 strcpy( pPrintJob->pszOutput, lpOutput );
582 pPrintJob->pszTitle = HeapAlloc(GetProcessHeap(), 0, strlen(lpTitle)+1);
583 strcpy( pPrintJob->pszTitle, lpTitle );
585 pPrintJob->hDC = hDC;
587 pPrintJob->nIndex = 0;
588 pPrintJob->hHandle = hHandle;
589 gPrintJobsTable[pPrintJob->nIndex] = pPrintJob;
592 TRACE("return %04x\n", hHandle);
596 /**********************************************************************
600 INT16 WINAPI CloseJob16(HPJOB16 hJob)
603 PPRINTJOB pPrintJob = NULL;
605 TRACE("%04x\n", hJob);
607 pPrintJob = FindPrintJobFromHandle(hJob);
608 if (pPrintJob != NULL)
610 /* Close the spool file */
611 close(pPrintJob->fd);
618 /**********************************************************************
619 * WriteSpool (GDI.241)
622 INT16 WINAPI WriteSpool16(HPJOB16 hJob, LPSTR lpData, INT16 cch)
625 PPRINTJOB pPrintJob = NULL;
627 TRACE("%04x %08lx %04x\n", hJob, (DWORD)lpData, cch);
629 pPrintJob = FindPrintJobFromHandle(hJob);
630 if (pPrintJob != NULL && pPrintJob->fd >= 0 && cch)
632 if (write(pPrintJob->fd, lpData, cch) != cch)
637 /* FIXME: We just cannot call 16 bit functions from here, since we
638 * have acquired several locks (DC). And we do not really need to.
640 if (pPrintJob->hDC == 0) {
641 TRACE("hDC == 0 so no QueryAbort\n");
643 else if (!(QueryAbort16(pPrintJob->hDC, (nRet == SP_OUTOFDISK) ? nRet : 0 )))
645 CloseJob16(hJob); /* printing aborted */
653 typedef INT (WINAPI *MSGBOX_PROC)( HWND, LPCSTR, LPCSTR, UINT );
655 /**********************************************************************
656 * WriteDialog (GDI.242)
659 INT16 WINAPI WriteDialog16(HPJOB16 hJob, LPSTR lpMsg, INT16 cchMsg)
662 MSGBOX_PROC pMessageBoxA;
665 TRACE("%04x %04x '%s'\n", hJob, cchMsg, lpMsg);
667 if ((mod = GetModuleHandleA("user32.dll")))
669 if ((pMessageBoxA = (MSGBOX_PROC)GetProcAddress( mod, "MessageBoxA" )))
670 ret = pMessageBoxA(0, lpMsg, "Printing Error", MB_OKCANCEL);
676 /**********************************************************************
677 * DeleteJob (GDI.244)
680 INT16 WINAPI DeleteJob16(HPJOB16 hJob, INT16 nNotUsed)
684 TRACE("%04x\n", hJob);
686 nRet = FreePrintJob(hJob);
691 * The following two function would allow a page to be sent to the printer
692 * when it has been processed. For simplicity they havn't been implemented.
693 * This means a whole job has to be processed before it is sent to the printer.
696 /**********************************************************************
697 * StartSpoolPage (GDI.246)
700 INT16 WINAPI StartSpoolPage16(HPJOB16 hJob)
702 FIXME("StartSpoolPage GDI.246 unimplemented\n");
708 /**********************************************************************
709 * EndSpoolPage (GDI.247)
712 INT16 WINAPI EndSpoolPage16(HPJOB16 hJob)
714 FIXME("EndSpoolPage GDI.247 unimplemented\n");
719 /**********************************************************************
720 * GetSpoolJob (GDI.245)
723 DWORD WINAPI GetSpoolJob16(int nOption, LONG param)
726 TRACE("In GetSpoolJob param 0x%lx noption %d\n",param, nOption);
731 /******************************************************************
732 * DrvGetPrinterDataInternal
734 * Helper for DrvGetPrinterData
736 static DWORD DrvGetPrinterDataInternal(LPSTR RegStr_Printer,
737 LPBYTE lpPrinterData, int cbData, int what)
741 DWORD dwType, cbQueryData;
743 if (!(RegOpenKeyA(HKEY_LOCAL_MACHINE, RegStr_Printer, &hkey))) {
744 if (what == INT_PD_DEFAULT_DEVMODE) { /* "Default DevMode" */
745 if (!(RegQueryValueExA(hkey, DefaultDevMode, 0, &dwType, 0, &cbQueryData))) {
748 else if ((cbQueryData) && (cbQueryData <= cbData)) {
749 cbQueryData = cbData;
750 if (RegQueryValueExA(hkey, DefaultDevMode, 0,
751 &dwType, lpPrinterData, &cbQueryData))
755 } else { /* "Printer Driver" */
757 RegQueryValueExA(hkey, "Printer Driver", 0,
758 &dwType, lpPrinterData, &cbQueryData);
762 if (hkey) RegCloseKey(hkey);
766 /******************************************************************
767 * DrvGetPrinterData (GDI.282)
770 DWORD WINAPI DrvGetPrinterData16(LPSTR lpPrinter, LPSTR lpProfile,
771 LPDWORD lpType, LPBYTE lpPrinterData,
772 int cbData, LPDWORD lpNeeded)
774 LPSTR RegStr_Printer;
775 HKEY hkey = 0, hkey2 = 0;
777 DWORD dwType, PrinterAttr, cbPrinterAttr, SetData, size;
779 if (HIWORD(lpPrinter))
780 TRACE("printer %s\n",lpPrinter);
782 TRACE("printer %p\n",lpPrinter);
783 if (HIWORD(lpProfile))
784 TRACE("profile %s\n",lpProfile);
786 TRACE("profile %p\n",lpProfile);
787 TRACE("lpType %p\n",lpType);
789 if ((!lpPrinter) || (!lpProfile) || (!lpNeeded))
790 return ERROR_INVALID_PARAMETER;
792 RegStr_Printer = HeapAlloc(GetProcessHeap(), 0,
793 strlen(Printers) + strlen(lpPrinter) + 2);
794 strcpy(RegStr_Printer, Printers);
795 strcat(RegStr_Printer, lpPrinter);
797 if (((DWORD)lpProfile == INT_PD_DEFAULT_DEVMODE) || (HIWORD(lpProfile) &&
798 (!strcmp(lpProfile, DefaultDevMode)))) {
799 size = DrvGetPrinterDataInternal(RegStr_Printer, lpPrinterData, cbData,
800 INT_PD_DEFAULT_DEVMODE);
803 if ((lpPrinterData) && (*lpNeeded > cbData))
804 res = ERROR_MORE_DATA;
806 else res = ERROR_INVALID_PRINTER_NAME;
809 if (((DWORD)lpProfile == INT_PD_DEFAULT_MODEL) || (HIWORD(lpProfile) &&
810 (!strcmp(lpProfile, PrinterModel)))) {
812 if (!lpPrinterData) goto failed;
814 res = ERROR_MORE_DATA;
817 size = DrvGetPrinterDataInternal(RegStr_Printer, lpPrinterData, cbData,
818 INT_PD_DEFAULT_MODEL);
819 if ((size+1) && (lpType))
822 res = ERROR_INVALID_PRINTER_NAME;
826 if ((res = RegOpenKeyA(HKEY_LOCAL_MACHINE, RegStr_Printer, &hkey)))
829 if ((res = RegQueryValueExA(hkey, "Attributes", 0,
830 &dwType, (LPBYTE)&PrinterAttr, &cbPrinterAttr)))
832 if ((res = RegOpenKeyA(hkey, PrinterDriverData, &hkey2)))
835 res = RegQueryValueExA(hkey2, lpProfile, 0,
836 lpType, lpPrinterData, lpNeeded);
837 if ((res != ERROR_CANTREAD) &&
839 (PRINTER_ATTRIBUTE_ENABLE_BIDI|PRINTER_ATTRIBUTE_NETWORK))
840 == PRINTER_ATTRIBUTE_NETWORK))
842 if (!(res) && (*lpType == REG_DWORD) && (*(LPDWORD)lpPrinterData == -1))
843 res = ERROR_INVALID_DATA;
848 RegSetValueExA(hkey2, lpProfile, 0, REG_DWORD, (LPBYTE)&SetData, 4); /* no result returned */
853 if (hkey2) RegCloseKey(hkey2);
854 if (hkey) RegCloseKey(hkey);
855 HeapFree(GetProcessHeap(), 0, RegStr_Printer);
860 /******************************************************************
861 * DrvSetPrinterData (GDI.281)
864 DWORD WINAPI DrvSetPrinterData16(LPSTR lpPrinter, LPSTR lpProfile,
865 DWORD lpType, LPBYTE lpPrinterData,
868 LPSTR RegStr_Printer;
872 if (HIWORD(lpPrinter))
873 TRACE("printer %s\n",lpPrinter);
875 TRACE("printer %p\n",lpPrinter);
876 if (HIWORD(lpProfile))
877 TRACE("profile %s\n",lpProfile);
879 TRACE("profile %p\n",lpProfile);
880 TRACE("lpType %08lx\n",lpType);
882 if ((!lpPrinter) || (!lpProfile) ||
883 ((DWORD)lpProfile == INT_PD_DEFAULT_MODEL) || (HIWORD(lpProfile) &&
884 (!strcmp(lpProfile, PrinterModel))))
885 return ERROR_INVALID_PARAMETER;
887 RegStr_Printer = HeapAlloc(GetProcessHeap(), 0,
888 strlen(Printers) + strlen(lpPrinter) + 2);
889 strcpy(RegStr_Printer, Printers);
890 strcat(RegStr_Printer, lpPrinter);
892 if (((DWORD)lpProfile == INT_PD_DEFAULT_DEVMODE) || (HIWORD(lpProfile) &&
893 (!strcmp(lpProfile, DefaultDevMode)))) {
894 if ( RegOpenKeyA(HKEY_LOCAL_MACHINE, RegStr_Printer, &hkey)
896 RegSetValueExA(hkey, DefaultDevMode, 0, REG_BINARY,
897 lpPrinterData, dwSize) != ERROR_SUCCESS )
898 res = ERROR_INVALID_PRINTER_NAME;
902 strcat(RegStr_Printer, "\\");
904 if( (res = RegOpenKeyA(HKEY_LOCAL_MACHINE, RegStr_Printer, &hkey)) ==
908 res = RegDeleteValueA(hkey, lpProfile);
910 res = RegSetValueExA(hkey, lpProfile, 0, lpType,
911 lpPrinterData, dwSize);
915 if (hkey) RegCloseKey(hkey);
916 HeapFree(GetProcessHeap(), 0, RegStr_Printer);