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 );
236 ERR("Invalid hdc %04x\n", hdc);
240 abproc = dc->pAbortProc;
241 GDI_ReleaseObj( hdc );
244 ret = abproc(hdc, 0);
248 /* ### start build ### */
249 extern WORD CALLBACK PRTDRV_CallTo16_word_ww(FARPROC16,WORD,WORD);
250 /* ### stop build ### */
252 /**********************************************************************
253 * SetAbortProc (GDI.381)
256 INT16 WINAPI SetAbortProc16(HDC16 hdc, SEGPTR abrtprc)
258 ABORTPROC proc32 = (ABORTPROC)THUNK_Alloc((FARPROC16)abrtprc,
259 (RELAY)PRTDRV_CallTo16_word_ww);
260 return SetAbortProc(hdc, proc32);
263 /**********************************************************************
264 * SetAbortProc (GDI32.@)
267 INT WINAPI SetAbortProc(HDC hdc, ABORTPROC abrtprc)
269 DC *dc = DC_GetDCPtr( hdc );
271 if(dc->pAbortProc) THUNK_Free((FARPROC)dc->pAbortProc);
272 dc->pAbortProc = abrtprc;
273 GDI_ReleaseObj( hdc );
278 /****************** misc. printer related functions */
281 * The following function should implement a queing system
290 static struct hpq *hpqueue;
292 /**********************************************************************
296 HPQ16 WINAPI CreatePQ16(INT16 size)
303 tmp_size = size << 2;
304 if (!(hpq = GlobalAlloc16(GMEM_SHARE|GMEM_MOVEABLE, tmp_size + 8)))
306 pPQ = GlobalLock16(hpq);
315 FIXME("(%d): stub\n",size);
320 /**********************************************************************
324 INT16 WINAPI DeletePQ16(HPQ16 hPQ)
326 return GlobalFree16((HGLOBAL16)hPQ);
329 /**********************************************************************
330 * ExtractPQ (GDI.232)
333 INT16 WINAPI ExtractPQ16(HPQ16 hPQ)
335 struct hpq *queue, *prev, *current, *currentPrev;
336 int key = 0, tag = -1;
337 currentPrev = prev = NULL;
338 queue = current = hpqueue;
344 currentPrev = current;
345 current = current->next;
348 if (current->key < key)
360 prev->next = queue->next;
362 hpqueue = queue->next;
363 HeapFree(GetProcessHeap(), 0, queue);
366 TRACE("%x got tag %d key %d\n", hPQ, tag, key);
371 /**********************************************************************
375 INT16 WINAPI InsertPQ16(HPQ16 hPQ, INT16 tag, INT16 key)
377 struct hpq *queueItem = HeapAlloc(GetProcessHeap(), 0, sizeof(struct hpq));
378 if(queueItem == NULL) {
379 ERR("Memory exausted!\n");
382 queueItem->next = hpqueue;
384 queueItem->key = key;
385 queueItem->tag = tag;
387 FIXME("(%x %d %d): stub???\n", hPQ, tag, key);
391 /**********************************************************************
395 INT16 WINAPI MinPQ16(HPQ16 hPQ)
397 FIXME("(%x): stub\n", hPQ);
401 /**********************************************************************
405 INT16 WINAPI SizePQ16(HPQ16 hPQ, INT16 sizechange)
407 FIXME("(%x %d): stub\n", hPQ, sizechange);
414 * The following functions implement part of the spooling process to
415 * print manager. I would like to see wine have a version of print managers
416 * that used LPR/LPD. For simplicity print jobs will be sent to a file for
419 typedef struct PRINTJOB
427 } PRINTJOB, *PPRINTJOB;
429 #define MAX_PRINT_JOBS 1
432 PPRINTJOB gPrintJobsTable[MAX_PRINT_JOBS];
435 static PPRINTJOB FindPrintJobFromHandle(HANDLE16 hHandle)
437 return gPrintJobsTable[0];
440 static int CreateSpoolFile(LPCSTR pszOutput)
444 char *psCmdP = psCmd;
446 /* TTD convert the 'output device' into a spool file name */
448 if (pszOutput == NULL || *pszOutput == '\0')
451 if (!strncmp("LPR:",pszOutput,4))
452 sprintf(psCmd,"|lpr -P%s",pszOutput+4);
458 if(!RegOpenKeyA(HKEY_LOCAL_MACHINE, "Software\\Wine\\Wine\\Config\\spooler", &hkey))
460 DWORD type, count = sizeof(psCmd);
461 RegQueryValueExA(hkey, pszOutput, 0, &type, psCmd, &count);
465 TRACE("Got printerSpoolCommand '%s' for output device '%s'\n",
468 psCmdP = (char *)pszOutput;
471 while (*psCmdP && isspace(*psCmdP))
487 TRACE("In child need to exec %s\n",psCmdP);
497 TRACE("Need to execute a cmnd and pipe the output to it\n");
501 DOS_FULL_NAME fullName;
503 TRACE("Just assume it's a file\n");
506 * The file name can be dos based, we have to find its
507 * Unix correspondant file name
509 DOSFS_GetFullName(psCmdP, FALSE, &fullName);
511 if ((fd = open(fullName.long_name, O_CREAT | O_TRUNC | O_WRONLY , 0600)) < 0)
513 ERR("Failed to create spool file %s (%s)\n",
514 fullName.long_name, strerror(errno));
520 static int FreePrintJob(HANDLE16 hJob)
525 pPrintJob = FindPrintJobFromHandle(hJob);
526 if (pPrintJob != NULL)
528 gPrintJobsTable[pPrintJob->nIndex] = NULL;
529 HeapFree(GetProcessHeap(), 0, pPrintJob->pszOutput);
530 HeapFree(GetProcessHeap(), 0, pPrintJob->pszTitle);
531 if (pPrintJob->fd >= 0) close(pPrintJob->fd);
532 HeapFree(GetProcessHeap(), 0, pPrintJob);
538 /**********************************************************************
542 HPJOB16 WINAPI OpenJob16(LPCSTR lpOutput, LPCSTR lpTitle, HDC16 hDC)
544 HPJOB16 hHandle = (HPJOB16)SP_ERROR;
547 TRACE("'%s' '%s' %04x\n", lpOutput, lpTitle, hDC);
549 pPrintJob = gPrintJobsTable[0];
550 if (pPrintJob == NULL)
554 /* Try an create a spool file */
555 fd = CreateSpoolFile(lpOutput);
558 pPrintJob = HeapAlloc(GetProcessHeap(), 0, sizeof(PRINTJOB));
559 if(pPrintJob == NULL) {
560 WARN("Memory exausted!\n");
566 pPrintJob->pszOutput = HeapAlloc(GetProcessHeap(), 0, strlen(lpOutput)+1);
567 strcpy( pPrintJob->pszOutput, lpOutput );
570 pPrintJob->pszTitle = HeapAlloc(GetProcessHeap(), 0, strlen(lpTitle)+1);
571 strcpy( pPrintJob->pszTitle, lpTitle );
573 pPrintJob->hDC = hDC;
575 pPrintJob->nIndex = 0;
576 pPrintJob->hHandle = hHandle;
577 gPrintJobsTable[pPrintJob->nIndex] = pPrintJob;
580 TRACE("return %04x\n", hHandle);
584 /**********************************************************************
588 INT16 WINAPI CloseJob16(HPJOB16 hJob)
591 PPRINTJOB pPrintJob = NULL;
593 TRACE("%04x\n", hJob);
595 pPrintJob = FindPrintJobFromHandle(hJob);
596 if (pPrintJob != NULL)
598 /* Close the spool file */
599 close(pPrintJob->fd);
606 /**********************************************************************
607 * WriteSpool (GDI.241)
610 INT16 WINAPI WriteSpool16(HPJOB16 hJob, LPSTR lpData, INT16 cch)
613 PPRINTJOB pPrintJob = NULL;
615 TRACE("%04x %08lx %04x\n", hJob, (DWORD)lpData, cch);
617 pPrintJob = FindPrintJobFromHandle(hJob);
618 if (pPrintJob != NULL && pPrintJob->fd >= 0 && cch)
620 if (write(pPrintJob->fd, lpData, cch) != cch)
625 /* FIXME: We just cannot call 16 bit functions from here, since we
626 * have acquired several locks (DC). And we do not really need to.
628 if (pPrintJob->hDC == 0) {
629 TRACE("hDC == 0 so no QueryAbort\n");
631 else if (!(QueryAbort16(pPrintJob->hDC, (nRet == SP_OUTOFDISK) ? nRet : 0 )))
633 CloseJob16(hJob); /* printing aborted */
641 typedef INT WINAPI (*MSGBOX_PROC)( HWND, LPCSTR, LPCSTR, UINT );
643 /**********************************************************************
644 * WriteDialog (GDI.242)
647 INT16 WINAPI WriteDialog16(HPJOB16 hJob, LPSTR lpMsg, INT16 cchMsg)
650 MSGBOX_PROC pMessageBoxA;
653 TRACE("%04x %04x '%s'\n", hJob, cchMsg, lpMsg);
655 if ((mod = GetModuleHandleA("user32.dll")))
657 if ((pMessageBoxA = (MSGBOX_PROC)GetProcAddress( mod, "MessageBoxA" )))
658 ret = pMessageBoxA(0, lpMsg, "Printing Error", MB_OKCANCEL);
664 /**********************************************************************
665 * DeleteJob (GDI.244)
668 INT16 WINAPI DeleteJob16(HPJOB16 hJob, INT16 nNotUsed)
672 TRACE("%04x\n", hJob);
674 nRet = FreePrintJob(hJob);
679 * The following two function would allow a page to be sent to the printer
680 * when it has been processed. For simplicity they havn't been implemented.
681 * This means a whole job has to be processed before it is sent to the printer.
684 /**********************************************************************
685 * StartSpoolPage (GDI.246)
688 INT16 WINAPI StartSpoolPage16(HPJOB16 hJob)
690 FIXME("StartSpoolPage GDI.246 unimplemented\n");
696 /**********************************************************************
697 * EndSpoolPage (GDI.247)
700 INT16 WINAPI EndSpoolPage16(HPJOB16 hJob)
702 FIXME("EndSpoolPage GDI.247 unimplemented\n");
707 /**********************************************************************
708 * GetSpoolJob (GDI.245)
711 DWORD WINAPI GetSpoolJob16(int nOption, LONG param)
714 TRACE("In GetSpoolJob param 0x%lx noption %d\n",param, nOption);
719 /******************************************************************
720 * DrvGetPrinterDataInternal
722 * Helper for DrvGetPrinterData
724 static DWORD DrvGetPrinterDataInternal(LPSTR RegStr_Printer,
725 LPBYTE lpPrinterData, int cbData, int what)
729 DWORD dwType, cbQueryData;
731 if (!(RegOpenKeyA(HKEY_LOCAL_MACHINE, RegStr_Printer, &hkey))) {
732 if (what == INT_PD_DEFAULT_DEVMODE) { /* "Default DevMode" */
733 if (!(RegQueryValueExA(hkey, DefaultDevMode, 0, &dwType, 0, &cbQueryData))) {
736 else if ((cbQueryData) && (cbQueryData <= cbData)) {
737 cbQueryData = cbData;
738 if (RegQueryValueExA(hkey, DefaultDevMode, 0,
739 &dwType, lpPrinterData, &cbQueryData))
743 } else { /* "Printer Driver" */
745 RegQueryValueExA(hkey, "Printer Driver", 0,
746 &dwType, lpPrinterData, &cbQueryData);
750 if (hkey) RegCloseKey(hkey);
754 /******************************************************************
755 * DrvGetPrinterData [GDI.282]
758 DWORD WINAPI DrvGetPrinterData16(LPSTR lpPrinter, LPSTR lpProfile,
759 LPDWORD lpType, LPBYTE lpPrinterData,
760 int cbData, LPDWORD lpNeeded)
762 LPSTR RegStr_Printer;
763 HKEY hkey = 0, hkey2 = 0;
765 DWORD dwType, PrinterAttr, cbPrinterAttr, SetData, size;
767 if (HIWORD(lpPrinter))
768 TRACE("printer %s\n",lpPrinter);
770 TRACE("printer %p\n",lpPrinter);
771 if (HIWORD(lpProfile))
772 TRACE("profile %s\n",lpProfile);
774 TRACE("profile %p\n",lpProfile);
775 TRACE("lpType %p\n",lpType);
777 if ((!lpPrinter) || (!lpProfile) || (!lpNeeded))
778 return ERROR_INVALID_PARAMETER;
780 RegStr_Printer = HeapAlloc(GetProcessHeap(), 0,
781 strlen(Printers) + strlen(lpPrinter) + 2);
782 strcpy(RegStr_Printer, Printers);
783 strcat(RegStr_Printer, lpPrinter);
785 if (((DWORD)lpProfile == INT_PD_DEFAULT_DEVMODE) || (HIWORD(lpProfile) &&
786 (!strcmp(lpProfile, DefaultDevMode)))) {
787 size = DrvGetPrinterDataInternal(RegStr_Printer, lpPrinterData, cbData,
788 INT_PD_DEFAULT_DEVMODE);
791 if ((lpPrinterData) && (*lpNeeded > cbData))
792 res = ERROR_MORE_DATA;
794 else res = ERROR_INVALID_PRINTER_NAME;
797 if (((DWORD)lpProfile == INT_PD_DEFAULT_MODEL) || (HIWORD(lpProfile) &&
798 (!strcmp(lpProfile, PrinterModel)))) {
800 if (!lpPrinterData) goto failed;
802 res = ERROR_MORE_DATA;
805 size = DrvGetPrinterDataInternal(RegStr_Printer, lpPrinterData, cbData,
806 INT_PD_DEFAULT_MODEL);
807 if ((size+1) && (lpType))
810 res = ERROR_INVALID_PRINTER_NAME;
814 if ((res = RegOpenKeyA(HKEY_LOCAL_MACHINE, RegStr_Printer, &hkey)))
817 if ((res = RegQueryValueExA(hkey, "Attributes", 0,
818 &dwType, (LPBYTE)&PrinterAttr, &cbPrinterAttr)))
820 if ((res = RegOpenKeyA(hkey, PrinterDriverData, &hkey2)))
823 res = RegQueryValueExA(hkey2, lpProfile, 0,
824 lpType, lpPrinterData, lpNeeded);
825 if ((res != ERROR_CANTREAD) &&
827 (PRINTER_ATTRIBUTE_ENABLE_BIDI|PRINTER_ATTRIBUTE_NETWORK))
828 == PRINTER_ATTRIBUTE_NETWORK))
830 if (!(res) && (*lpType == REG_DWORD) && (*(LPDWORD)lpPrinterData == -1))
831 res = ERROR_INVALID_DATA;
836 RegSetValueExA(hkey2, lpProfile, 0, REG_DWORD, (LPBYTE)&SetData, 4); /* no result returned */
841 if (hkey2) RegCloseKey(hkey2);
842 if (hkey) RegCloseKey(hkey);
843 HeapFree(GetProcessHeap(), 0, RegStr_Printer);
848 /******************************************************************
849 * DrvSetPrinterData [GDI.281]
852 DWORD WINAPI DrvSetPrinterData16(LPSTR lpPrinter, LPSTR lpProfile,
853 DWORD lpType, LPBYTE lpPrinterData,
856 LPSTR RegStr_Printer;
860 if (HIWORD(lpPrinter))
861 TRACE("printer %s\n",lpPrinter);
863 TRACE("printer %p\n",lpPrinter);
864 if (HIWORD(lpProfile))
865 TRACE("profile %s\n",lpProfile);
867 TRACE("profile %p\n",lpProfile);
868 TRACE("lpType %08lx\n",lpType);
870 if ((!lpPrinter) || (!lpProfile) ||
871 ((DWORD)lpProfile == INT_PD_DEFAULT_MODEL) || (HIWORD(lpProfile) &&
872 (!strcmp(lpProfile, PrinterModel))))
873 return ERROR_INVALID_PARAMETER;
875 RegStr_Printer = HeapAlloc(GetProcessHeap(), 0,
876 strlen(Printers) + strlen(lpPrinter) + 2);
877 strcpy(RegStr_Printer, Printers);
878 strcat(RegStr_Printer, lpPrinter);
880 if (((DWORD)lpProfile == INT_PD_DEFAULT_DEVMODE) || (HIWORD(lpProfile) &&
881 (!strcmp(lpProfile, DefaultDevMode)))) {
882 if ( RegOpenKeyA(HKEY_LOCAL_MACHINE, RegStr_Printer, &hkey)
884 RegSetValueExA(hkey, DefaultDevMode, 0, REG_BINARY,
885 lpPrinterData, dwSize) != ERROR_SUCCESS )
886 res = ERROR_INVALID_PRINTER_NAME;
890 strcat(RegStr_Printer, "\\");
892 if( (res = RegOpenKeyA(HKEY_LOCAL_MACHINE, RegStr_Printer, &hkey)) ==
896 res = RegDeleteValueA(hkey, lpProfile);
898 res = RegSetValueExA(hkey, lpProfile, 0, lpType,
899 lpPrinterData, dwSize);
903 if (hkey) RegCloseKey(hkey);
904 HeapFree(GetProcessHeap(), 0, RegStr_Printer);