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
20 #include "wine/winbase16.h"
21 #include "wine/wingdi16.h"
25 #include "debugtools.h"
31 DEFAULT_DEBUG_CHANNEL(print);
33 static char PrinterModel[] = "Printer Model";
34 static char DefaultDevMode[] = "Default DevMode";
35 static char PrinterDriverData[] = "PrinterDriverData";
36 static char Printers[] = "System\\CurrentControlSet\\Control\\Print\\Printers\\";
38 /******************************************************************
42 INT16 WINAPI StartDoc16( HDC16 hdc, const DOCINFO16 *lpdoc )
46 docA.cbSize = lpdoc->cbSize;
47 docA.lpszDocName = MapSL(lpdoc->lpszDocName);
48 docA.lpszOutput = MapSL(lpdoc->lpszOutput);
50 if(lpdoc->cbSize >= 14)
51 docA.lpszDatatype = MapSL(lpdoc->lpszDatatype);
53 docA.lpszDatatype = NULL;
55 if(lpdoc->cbSize >= 18)
56 docA.fwType = lpdoc->fwType;
60 return StartDocA(hdc, &docA);
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, 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);
109 HeapFree( GetProcessHeap(), 0, (LPSTR)docA.lpszDocName );
111 HeapFree( GetProcessHeap(), 0, (LPSTR)docA.lpszOutput );
112 if(docA.lpszDatatype)
113 HeapFree( GetProcessHeap(), 0, (LPSTR)docA.lpszDatatype );
118 /******************************************************************
122 INT16 WINAPI EndDoc16(HDC16 hdc)
127 /******************************************************************
131 INT WINAPI EndDoc(HDC hdc)
134 DC *dc = DC_GetDCPtr( hdc );
135 if(!dc) return SP_ERROR;
137 if (dc->funcs->pEndDoc) ret = dc->funcs->pEndDoc( dc );
138 GDI_ReleaseObj( hdc );
142 /******************************************************************
143 * StartPage [GDI.379]
146 INT16 WINAPI StartPage16(HDC16 hdc)
148 return StartPage(hdc);
151 /******************************************************************
152 * StartPage [GDI32.@]
155 INT WINAPI StartPage(HDC hdc)
158 DC *dc = DC_GetDCPtr( hdc );
159 if(!dc) return SP_ERROR;
161 if(dc->funcs->pStartPage)
162 ret = dc->funcs->pStartPage( dc );
165 GDI_ReleaseObj( hdc );
169 /******************************************************************
173 INT16 WINAPI EndPage16( HDC16 hdc )
178 /******************************************************************
182 INT WINAPI EndPage(HDC hdc)
185 DC *dc = DC_GetDCPtr( hdc );
186 if(!dc) return SP_ERROR;
188 if (dc->funcs->pEndPage) ret = dc->funcs->pEndPage( dc );
189 GDI_ReleaseObj( hdc );
190 if (!QueryAbort16( hdc, 0 ))
198 /******************************************************************************
201 INT16 WINAPI AbortDoc16(HDC16 hdc)
203 return AbortDoc(hdc);
206 /******************************************************************************
209 INT WINAPI AbortDoc(HDC hdc)
212 DC *dc = DC_GetDCPtr( hdc );
213 if(!dc) return SP_ERROR;
215 if (dc->funcs->pAbortDoc) ret = dc->funcs->pAbortDoc( dc );
216 GDI_ReleaseObj( hdc );
220 /**********************************************************************
221 * QueryAbort (GDI.155)
223 * Calls the app's AbortProc function if avail.
226 * TRUE if no AbortProc avail or AbortProc wants to continue printing.
227 * FALSE if AbortProc wants to abort printing.
229 BOOL16 WINAPI QueryAbort16(HDC16 hdc, INT16 reserved)
232 DC *dc = DC_GetDCPtr( hdc );
235 ERR("Invalid hdc %04x\n", hdc);
239 if (dc->pAbortProc) ret = dc->pAbortProc(hdc, 0);
240 GDI_ReleaseObj( hdc );
244 /* ### start build ### */
245 extern WORD CALLBACK PRTDRV_CallTo16_word_ww(FARPROC16,WORD,WORD);
246 /* ### stop build ### */
248 /**********************************************************************
249 * SetAbortProc (GDI.381)
252 INT16 WINAPI SetAbortProc16(HDC16 hdc, SEGPTR abrtprc)
254 ABORTPROC proc32 = (ABORTPROC)THUNK_Alloc((FARPROC16)abrtprc,
255 (RELAY)PRTDRV_CallTo16_word_ww);
256 return SetAbortProc(hdc, proc32);
259 /**********************************************************************
260 * SetAbortProc (GDI32.@)
263 INT WINAPI SetAbortProc(HDC hdc, ABORTPROC abrtprc)
265 DC *dc = DC_GetDCPtr( hdc );
267 if(dc->pAbortProc) THUNK_Free((FARPROC)dc->pAbortProc);
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;
442 /* TTD convert the 'output device' into a spool file name */
444 if (pszOutput == NULL || *pszOutput == '\0')
447 if (!strncmp("LPR:",pszOutput,4))
448 sprintf(psCmd,"|lpr -P%s",pszOutput+4);
454 if(!RegOpenKeyA(HKEY_LOCAL_MACHINE, "Software\\Wine\\Wine\\Config\\spooler", &hkey))
456 DWORD type, count = sizeof(psCmd);
457 RegQueryValueExA(hkey, pszOutput, 0, &type, psCmd, &count);
461 TRACE("Got printerSpoolCommand '%s' for output device '%s'\n",
464 psCmdP = (char *)pszOutput;
467 while (*psCmdP && isspace(*psCmdP))
483 TRACE("In child need to exec %s\n",psCmdP);
493 TRACE("Need to execute a cmnd and pipe the output to it\n");
497 DOS_FULL_NAME fullName;
499 TRACE("Just assume it's a file\n");
502 * The file name can be dos based, we have to find its
503 * Unix correspondant file name
505 DOSFS_GetFullName(psCmdP, FALSE, &fullName);
507 if ((fd = open(fullName.long_name, O_CREAT | O_TRUNC | O_WRONLY , 0600)) < 0)
509 ERR("Failed to create spool file %s (%s)\n",
510 fullName.long_name, strerror(errno));
516 static int FreePrintJob(HANDLE16 hJob)
521 pPrintJob = FindPrintJobFromHandle(hJob);
522 if (pPrintJob != NULL)
524 gPrintJobsTable[pPrintJob->nIndex] = NULL;
525 HeapFree(GetProcessHeap(), 0, pPrintJob->pszOutput);
526 HeapFree(GetProcessHeap(), 0, pPrintJob->pszTitle);
527 if (pPrintJob->fd >= 0) close(pPrintJob->fd);
528 HeapFree(GetProcessHeap(), 0, pPrintJob);
534 /**********************************************************************
538 HPJOB16 WINAPI OpenJob16(LPCSTR lpOutput, LPCSTR lpTitle, HDC16 hDC)
540 HPJOB16 hHandle = (HPJOB16)SP_ERROR;
543 TRACE("'%s' '%s' %04x\n", lpOutput, lpTitle, hDC);
545 pPrintJob = gPrintJobsTable[0];
546 if (pPrintJob == NULL)
550 /* Try an create a spool file */
551 fd = CreateSpoolFile(lpOutput);
554 pPrintJob = HeapAlloc(GetProcessHeap(), 0, sizeof(PRINTJOB));
555 if(pPrintJob == NULL) {
556 WARN("Memory exausted!\n");
562 pPrintJob->pszOutput = HeapAlloc(GetProcessHeap(), 0, strlen(lpOutput)+1);
563 strcpy( pPrintJob->pszOutput, lpOutput );
566 pPrintJob->pszTitle = HeapAlloc(GetProcessHeap(), 0, strlen(lpTitle)+1);
567 strcpy( pPrintJob->pszTitle, lpTitle );
569 pPrintJob->hDC = hDC;
571 pPrintJob->nIndex = 0;
572 pPrintJob->hHandle = hHandle;
573 gPrintJobsTable[pPrintJob->nIndex] = pPrintJob;
576 TRACE("return %04x\n", hHandle);
580 /**********************************************************************
584 INT16 WINAPI CloseJob16(HPJOB16 hJob)
587 PPRINTJOB pPrintJob = NULL;
589 TRACE("%04x\n", hJob);
591 pPrintJob = FindPrintJobFromHandle(hJob);
592 if (pPrintJob != NULL)
594 /* Close the spool file */
595 close(pPrintJob->fd);
602 /**********************************************************************
603 * WriteSpool (GDI.241)
606 INT16 WINAPI WriteSpool16(HPJOB16 hJob, LPSTR lpData, INT16 cch)
609 PPRINTJOB pPrintJob = NULL;
611 TRACE("%04x %08lx %04x\n", hJob, (DWORD)lpData, cch);
613 pPrintJob = FindPrintJobFromHandle(hJob);
614 if (pPrintJob != NULL && pPrintJob->fd >= 0 && cch)
616 if (write(pPrintJob->fd, lpData, cch) != cch)
621 /* FIXME: We just cannot call 16 bit functions from here, since we
622 * have acquired several locks (DC). And we do not really need to.
624 if (pPrintJob->hDC == 0) {
625 TRACE("hDC == 0 so no QueryAbort\n");
627 else if (!(QueryAbort16(pPrintJob->hDC, (nRet == SP_OUTOFDISK) ? nRet : 0 )))
629 CloseJob16(hJob); /* printing aborted */
637 typedef INT WINAPI (*MSGBOX_PROC)( HWND, LPCSTR, LPCSTR, UINT );
639 /**********************************************************************
640 * WriteDialog (GDI.242)
643 INT16 WINAPI WriteDialog16(HPJOB16 hJob, LPSTR lpMsg, INT16 cchMsg)
646 MSGBOX_PROC pMessageBoxA;
649 TRACE("%04x %04x '%s'\n", hJob, cchMsg, lpMsg);
651 if ((mod = GetModuleHandleA("user32.dll")))
653 if ((pMessageBoxA = (MSGBOX_PROC)GetProcAddress( mod, "MessageBoxA" )))
654 ret = pMessageBoxA(0, lpMsg, "Printing Error", MB_OKCANCEL);
660 /**********************************************************************
661 * DeleteJob (GDI.244)
664 INT16 WINAPI DeleteJob16(HPJOB16 hJob, INT16 nNotUsed)
668 TRACE("%04x\n", hJob);
670 nRet = FreePrintJob(hJob);
675 * The following two function would allow a page to be sent to the printer
676 * when it has been processed. For simplicity they havn't been implemented.
677 * This means a whole job has to be processed before it is sent to the printer.
680 /**********************************************************************
681 * StartSpoolPage (GDI.246)
684 INT16 WINAPI StartSpoolPage16(HPJOB16 hJob)
686 FIXME("StartSpoolPage GDI.246 unimplemented\n");
692 /**********************************************************************
693 * EndSpoolPage (GDI.247)
696 INT16 WINAPI EndSpoolPage16(HPJOB16 hJob)
698 FIXME("EndSpoolPage GDI.247 unimplemented\n");
703 /**********************************************************************
704 * GetSpoolJob (GDI.245)
707 DWORD WINAPI GetSpoolJob16(int nOption, LONG param)
710 TRACE("In GetSpoolJob param 0x%lx noption %d\n",param, nOption);
715 /******************************************************************
716 * DrvGetPrinterDataInternal
718 * Helper for DrvGetPrinterData
720 static DWORD DrvGetPrinterDataInternal(LPSTR RegStr_Printer,
721 LPBYTE lpPrinterData, int cbData, int what)
725 DWORD dwType, cbQueryData;
727 if (!(RegOpenKeyA(HKEY_LOCAL_MACHINE, RegStr_Printer, &hkey))) {
728 if (what == INT_PD_DEFAULT_DEVMODE) { /* "Default DevMode" */
729 if (!(RegQueryValueExA(hkey, DefaultDevMode, 0, &dwType, 0, &cbQueryData))) {
732 else if ((cbQueryData) && (cbQueryData <= cbData)) {
733 cbQueryData = cbData;
734 if (RegQueryValueExA(hkey, DefaultDevMode, 0,
735 &dwType, lpPrinterData, &cbQueryData))
739 } else { /* "Printer Driver" */
741 RegQueryValueExA(hkey, "Printer Driver", 0,
742 &dwType, lpPrinterData, &cbQueryData);
746 if (hkey) RegCloseKey(hkey);
750 /******************************************************************
751 * DrvGetPrinterData [GDI.282]
754 DWORD WINAPI DrvGetPrinterData16(LPSTR lpPrinter, LPSTR lpProfile,
755 LPDWORD lpType, LPBYTE lpPrinterData,
756 int cbData, LPDWORD lpNeeded)
758 LPSTR RegStr_Printer;
759 HKEY hkey = 0, hkey2 = 0;
761 DWORD dwType, PrinterAttr, cbPrinterAttr, SetData, size;
763 if (HIWORD(lpPrinter))
764 TRACE("printer %s\n",lpPrinter);
766 TRACE("printer %p\n",lpPrinter);
767 if (HIWORD(lpProfile))
768 TRACE("profile %s\n",lpProfile);
770 TRACE("profile %p\n",lpProfile);
771 TRACE("lpType %p\n",lpType);
773 if ((!lpPrinter) || (!lpProfile) || (!lpNeeded))
774 return ERROR_INVALID_PARAMETER;
776 RegStr_Printer = HeapAlloc(GetProcessHeap(), 0,
777 strlen(Printers) + strlen(lpPrinter) + 2);
778 strcpy(RegStr_Printer, Printers);
779 strcat(RegStr_Printer, lpPrinter);
781 if (((DWORD)lpProfile == INT_PD_DEFAULT_DEVMODE) || (HIWORD(lpProfile) &&
782 (!strcmp(lpProfile, DefaultDevMode)))) {
783 size = DrvGetPrinterDataInternal(RegStr_Printer, lpPrinterData, cbData,
784 INT_PD_DEFAULT_DEVMODE);
787 if ((lpPrinterData) && (*lpNeeded > cbData))
788 res = ERROR_MORE_DATA;
790 else res = ERROR_INVALID_PRINTER_NAME;
793 if (((DWORD)lpProfile == INT_PD_DEFAULT_MODEL) || (HIWORD(lpProfile) &&
794 (!strcmp(lpProfile, PrinterModel)))) {
796 if (!lpPrinterData) goto failed;
798 res = ERROR_MORE_DATA;
801 size = DrvGetPrinterDataInternal(RegStr_Printer, lpPrinterData, cbData,
802 INT_PD_DEFAULT_MODEL);
803 if ((size+1) && (lpType))
806 res = ERROR_INVALID_PRINTER_NAME;
810 if ((res = RegOpenKeyA(HKEY_LOCAL_MACHINE, RegStr_Printer, &hkey)))
813 if ((res = RegQueryValueExA(hkey, "Attributes", 0,
814 &dwType, (LPBYTE)&PrinterAttr, &cbPrinterAttr)))
816 if ((res = RegOpenKeyA(hkey, PrinterDriverData, &hkey2)))
819 res = RegQueryValueExA(hkey2, lpProfile, 0,
820 lpType, lpPrinterData, lpNeeded);
821 if ((res != ERROR_CANTREAD) &&
823 (PRINTER_ATTRIBUTE_ENABLE_BIDI|PRINTER_ATTRIBUTE_NETWORK))
824 == PRINTER_ATTRIBUTE_NETWORK))
826 if (!(res) && (*lpType == REG_DWORD) && (*(LPDWORD)lpPrinterData == -1))
827 res = ERROR_INVALID_DATA;
832 RegSetValueExA(hkey2, lpProfile, 0, REG_DWORD, (LPBYTE)&SetData, 4); /* no result returned */
837 if (hkey2) RegCloseKey(hkey2);
838 if (hkey) RegCloseKey(hkey);
839 HeapFree(GetProcessHeap(), 0, RegStr_Printer);
844 /******************************************************************
845 * DrvSetPrinterData [GDI.281]
848 DWORD WINAPI DrvSetPrinterData16(LPSTR lpPrinter, LPSTR lpProfile,
849 DWORD lpType, LPBYTE lpPrinterData,
852 LPSTR RegStr_Printer;
856 if (HIWORD(lpPrinter))
857 TRACE("printer %s\n",lpPrinter);
859 TRACE("printer %p\n",lpPrinter);
860 if (HIWORD(lpProfile))
861 TRACE("profile %s\n",lpProfile);
863 TRACE("profile %p\n",lpProfile);
864 TRACE("lpType %08lx\n",lpType);
866 if ((!lpPrinter) || (!lpProfile) ||
867 ((DWORD)lpProfile == INT_PD_DEFAULT_MODEL) || (HIWORD(lpProfile) &&
868 (!strcmp(lpProfile, PrinterModel))))
869 return ERROR_INVALID_PARAMETER;
871 RegStr_Printer = HeapAlloc(GetProcessHeap(), 0,
872 strlen(Printers) + strlen(lpPrinter) + 2);
873 strcpy(RegStr_Printer, Printers);
874 strcat(RegStr_Printer, lpPrinter);
876 if (((DWORD)lpProfile == INT_PD_DEFAULT_DEVMODE) || (HIWORD(lpProfile) &&
877 (!strcmp(lpProfile, DefaultDevMode)))) {
878 if ( RegOpenKeyA(HKEY_LOCAL_MACHINE, RegStr_Printer, &hkey)
880 RegSetValueExA(hkey, DefaultDevMode, 0, REG_BINARY,
881 lpPrinterData, dwSize) != ERROR_SUCCESS )
882 res = ERROR_INVALID_PRINTER_NAME;
886 strcat(RegStr_Printer, "\\");
888 if( (res = RegOpenKeyA(HKEY_LOCAL_MACHINE, RegStr_Printer, &hkey)) ==
892 res = RegDeleteValueA(hkey, lpProfile);
894 res = RegSetValueExA(hkey, lpProfile, 0, lpType,
895 lpPrinterData, dwSize);
899 if (hkey) RegCloseKey(hkey);
900 HeapFree(GetProcessHeap(), 0, RegStr_Printer);