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\\";
60 /**********************************************************************
61 * QueryAbort (GDI.155)
63 * Calls the app's AbortProc function if avail.
66 * TRUE if no AbortProc avail or AbortProc wants to continue printing.
67 * FALSE if AbortProc wants to abort printing.
69 BOOL16 WINAPI QueryAbort16(HDC16 hdc16, INT16 reserved)
72 HDC hdc = HDC_32( hdc16 );
73 DC *dc = get_dc_ptr( hdc );
76 ERR("Invalid hdc %p\n", hdc);
79 if (dc->pAbortProc) ret = dc->pAbortProc(hdc, 0);
85 /**********************************************************************
88 static BOOL CALLBACK call_abort_proc16( HDC hdc, INT code )
91 DC *dc = get_dc_ptr( hdc );
93 if (!dc) return FALSE;
94 proc16 = dc->pAbortProc16;
101 args[1] = HDC_16(hdc);
103 WOWCallback16Ex( (DWORD)proc16, WCB16_PASCAL, sizeof(args), args, &ret );
110 /**********************************************************************
111 * SetAbortProc (GDI.381)
113 INT16 WINAPI SetAbortProc16(HDC16 hdc16, ABORTPROC16 abrtprc)
115 HDC hdc = HDC_32( hdc16 );
116 DC *dc = get_dc_ptr( hdc );
118 if (!dc) return FALSE;
119 dc->pAbortProc16 = abrtprc;
120 dc->pAbortProc = call_abort_proc16;
121 release_dc_ptr( dc );
125 /**********************************************************************
126 * SetAbortProc (GDI32.@)
129 INT WINAPI SetAbortProc(HDC hdc, ABORTPROC abrtprc)
131 DC *dc = get_dc_ptr( hdc );
133 if (!dc) return FALSE;
134 dc->pAbortProc = abrtprc;
135 release_dc_ptr( dc );
140 /****************** misc. printer related functions */
143 * The following function should implement a queing system
152 static struct hpq *hpqueue;
154 /**********************************************************************
158 HPQ16 WINAPI CreatePQ16(INT16 size)
165 tmp_size = size << 2;
166 if (!(hpq = GlobalAlloc16(GMEM_SHARE|GMEM_MOVEABLE, tmp_size + 8)))
168 pPQ = GlobalLock16(hpq);
177 FIXME("(%d): stub\n",size);
182 /**********************************************************************
186 INT16 WINAPI DeletePQ16(HPQ16 hPQ)
188 return GlobalFree16(hPQ);
191 /**********************************************************************
192 * ExtractPQ (GDI.232)
195 INT16 WINAPI ExtractPQ16(HPQ16 hPQ)
197 struct hpq *queue, *prev, *current, *currentPrev;
198 int key = 0, tag = -1;
199 currentPrev = prev = NULL;
200 queue = current = hpqueue;
206 currentPrev = current;
207 current = current->next;
210 if (current->key < key)
222 prev->next = queue->next;
224 hpqueue = queue->next;
225 HeapFree(GetProcessHeap(), 0, queue);
228 TRACE("%x got tag %d key %d\n", hPQ, tag, key);
233 /**********************************************************************
237 INT16 WINAPI InsertPQ16(HPQ16 hPQ, INT16 tag, INT16 key)
239 struct hpq *queueItem = HeapAlloc(GetProcessHeap(), 0, sizeof(struct hpq));
240 if(queueItem == NULL) {
241 ERR("Memory exausted!\n");
244 queueItem->next = hpqueue;
246 queueItem->key = key;
247 queueItem->tag = tag;
249 FIXME("(%x %d %d): stub???\n", hPQ, tag, key);
253 /**********************************************************************
257 INT16 WINAPI MinPQ16(HPQ16 hPQ)
259 FIXME("(%x): stub\n", hPQ);
263 /**********************************************************************
267 INT16 WINAPI SizePQ16(HPQ16 hPQ, INT16 sizechange)
269 FIXME("(%x %d): stub\n", hPQ, sizechange);
276 * The following functions implement part of the spooling process to
277 * print manager. I would like to see wine have a version of print managers
278 * that used LPR/LPD. For simplicity print jobs will be sent to a file for
281 typedef struct PRINTJOB
289 } PRINTJOB, *PPRINTJOB;
291 #define MAX_PRINT_JOBS 1
294 static PPRINTJOB gPrintJobsTable[MAX_PRINT_JOBS];
297 static PPRINTJOB FindPrintJobFromHandle(HANDLE16 hHandle)
299 return gPrintJobsTable[0];
302 static int CreateSpoolFile(LPCSTR pszOutput)
306 const char *psCmdP = psCmd;
309 /* TTD convert the 'output device' into a spool file name */
311 if (pszOutput == NULL || *pszOutput == '\0')
315 /* @@ Wine registry key: HKCU\Software\Wine\Printing\Spooler */
316 if(!RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Printing\\Spooler", &hkey))
318 DWORD type, count = sizeof(psCmd);
319 RegQueryValueExA(hkey, pszOutput, 0, &type, (LPBYTE)psCmd, &count);
322 if (!psCmd[0] && !strncmp("LPR:",pszOutput,4))
323 sprintf(psCmd,"|lpr -P'%s'",pszOutput+4);
325 TRACE("Got printerSpoolCommand '%s' for output device '%s'\n",
331 while (*psCmdP && isspace(*psCmdP))
338 TRACE("command: '%s'\n", psCmdP);
344 ERR("pipe() failed!\n");
351 TRACE("In child need to exec %s\n",psCmdP);
356 /* reset signals that we previously set to SIG_IGN */
357 signal( SIGPIPE, SIG_DFL );
358 signal( SIGCHLD, SIG_DFL );
360 execl("/bin/sh", "/bin/sh", "-c", psCmdP, (char*)0);
366 TRACE("Need to execute a cmnd and pipe the output to it\n");
372 WCHAR psCmdPW[MAX_PATH];
374 TRACE("Just assume it's a file\n");
377 * The file name can be dos based, we have to find its
378 * corresponding Unix file name.
380 MultiByteToWideChar(CP_ACP, 0, psCmdP, -1, psCmdPW, MAX_PATH);
381 if ((buffer = wine_get_unix_file_name(psCmdPW)))
383 if ((fd = open(buffer, O_CREAT | O_TRUNC | O_WRONLY, 0666)) < 0)
385 ERR("Failed to create spool file '%s' ('%s'). (error %s)\n",
386 buffer, psCmdP, strerror(errno));
388 HeapFree(GetProcessHeap(), 0, buffer);
394 static int FreePrintJob(HANDLE16 hJob)
399 pPrintJob = FindPrintJobFromHandle(hJob);
400 if (pPrintJob != NULL)
402 gPrintJobsTable[pPrintJob->nIndex] = NULL;
403 HeapFree(GetProcessHeap(), 0, pPrintJob->pszOutput);
404 HeapFree(GetProcessHeap(), 0, pPrintJob->pszTitle);
405 if (pPrintJob->fd >= 0) close(pPrintJob->fd);
406 HeapFree(GetProcessHeap(), 0, pPrintJob);
412 /**********************************************************************
416 HPJOB16 WINAPI OpenJob16(LPCSTR lpOutput, LPCSTR lpTitle, HDC16 hDC)
418 HPJOB16 hHandle = (HPJOB16)SP_ERROR;
421 TRACE("'%s' '%s' %04x\n", lpOutput, lpTitle, hDC);
423 pPrintJob = gPrintJobsTable[0];
424 if (pPrintJob == NULL)
428 /* Try and create a spool file */
429 fd = CreateSpoolFile(lpOutput);
432 pPrintJob = HeapAlloc(GetProcessHeap(), 0, sizeof(PRINTJOB));
433 if(pPrintJob == NULL) {
434 WARN("Memory exausted!\n");
440 pPrintJob->pszOutput = HeapAlloc(GetProcessHeap(), 0, strlen(lpOutput)+1);
441 strcpy( pPrintJob->pszOutput, lpOutput );
444 pPrintJob->pszTitle = HeapAlloc(GetProcessHeap(), 0, strlen(lpTitle)+1);
445 strcpy( pPrintJob->pszTitle, lpTitle );
447 pPrintJob->hDC = hDC;
449 pPrintJob->nIndex = 0;
450 pPrintJob->hHandle = hHandle;
451 gPrintJobsTable[pPrintJob->nIndex] = pPrintJob;
454 TRACE("return %04x\n", hHandle);
458 /**********************************************************************
462 INT16 WINAPI CloseJob16(HPJOB16 hJob)
465 PPRINTJOB pPrintJob = NULL;
467 TRACE("%04x\n", hJob);
469 pPrintJob = FindPrintJobFromHandle(hJob);
470 if (pPrintJob != NULL)
472 /* Close the spool file */
473 close(pPrintJob->fd);
480 /**********************************************************************
481 * WriteSpool (GDI.241)
484 INT16 WINAPI WriteSpool16(HPJOB16 hJob, LPSTR lpData, INT16 cch)
487 PPRINTJOB pPrintJob = NULL;
489 TRACE("%04x %p %04x\n", hJob, lpData, cch);
491 pPrintJob = FindPrintJobFromHandle(hJob);
492 if (pPrintJob != NULL && pPrintJob->fd >= 0 && cch)
494 if (write(pPrintJob->fd, lpData, cch) != cch)
499 /* FIXME: We just cannot call 16 bit functions from here, since we
500 * have acquired several locks (DC). And we do not really need to.
502 if (pPrintJob->hDC == 0) {
503 TRACE("hDC == 0 so no QueryAbort\n");
505 else if (!(QueryAbort16(pPrintJob->hDC, (nRet == SP_OUTOFDISK) ? nRet : 0 )))
507 CloseJob16(hJob); /* printing aborted */
515 typedef INT (WINAPI *MSGBOX_PROC)( HWND, LPCSTR, LPCSTR, UINT );
517 /**********************************************************************
518 * WriteDialog (GDI.242)
521 INT16 WINAPI WriteDialog16(HPJOB16 hJob, LPSTR lpMsg, INT16 cchMsg)
524 MSGBOX_PROC pMessageBoxA;
527 TRACE("%04x %04x '%s'\n", hJob, cchMsg, lpMsg);
529 if ((mod = GetModuleHandleA("user32.dll")))
531 if ((pMessageBoxA = (MSGBOX_PROC)GetProcAddress( mod, "MessageBoxA" )))
532 ret = pMessageBoxA(0, lpMsg, "Printing Error", MB_OKCANCEL);
538 /**********************************************************************
539 * DeleteJob (GDI.244)
542 INT16 WINAPI DeleteJob16(HPJOB16 hJob, INT16 nNotUsed)
546 TRACE("%04x\n", hJob);
548 nRet = FreePrintJob(hJob);
553 * The following two function would allow a page to be sent to the printer
554 * when it has been processed. For simplicity they haven't been implemented.
555 * This means a whole job has to be processed before it is sent to the printer.
558 /**********************************************************************
559 * StartSpoolPage (GDI.246)
562 INT16 WINAPI StartSpoolPage16(HPJOB16 hJob)
564 FIXME("StartSpoolPage GDI.246 unimplemented\n");
570 /**********************************************************************
571 * EndSpoolPage (GDI.247)
574 INT16 WINAPI EndSpoolPage16(HPJOB16 hJob)
576 FIXME("EndSpoolPage GDI.247 unimplemented\n");
581 /**********************************************************************
582 * GetSpoolJob (GDI.245)
585 DWORD WINAPI GetSpoolJob16(int nOption, LONG param)
588 TRACE("In GetSpoolJob param 0x%x noption %d\n",param, nOption);
593 /******************************************************************
594 * DrvGetPrinterDataInternal
596 * Helper for DrvGetPrinterData
598 static DWORD DrvGetPrinterDataInternal(LPSTR RegStr_Printer,
599 LPBYTE lpPrinterData, int cbData, int what)
603 DWORD dwType, cbQueryData;
605 if (!(RegOpenKeyA(HKEY_LOCAL_MACHINE, RegStr_Printer, &hkey))) {
606 if (what == INT_PD_DEFAULT_DEVMODE) { /* "Default DevMode" */
607 if (!(RegQueryValueExA(hkey, DefaultDevMode, 0, &dwType, 0, &cbQueryData))) {
610 else if ((cbQueryData) && (cbQueryData <= cbData)) {
611 cbQueryData = cbData;
612 if (RegQueryValueExA(hkey, DefaultDevMode, 0,
613 &dwType, lpPrinterData, &cbQueryData))
617 } else { /* "Printer Driver" */
619 RegQueryValueExA(hkey, "Printer Driver", 0,
620 &dwType, lpPrinterData, &cbQueryData);
624 if (hkey) RegCloseKey(hkey);
628 /******************************************************************
629 * DrvGetPrinterData (GDI.282)
632 DWORD WINAPI DrvGetPrinterData16(LPSTR lpPrinter, LPSTR lpProfile,
633 LPDWORD lpType, LPBYTE lpPrinterData,
634 int cbData, LPDWORD lpNeeded)
636 LPSTR RegStr_Printer;
637 HKEY hkey = 0, hkey2 = 0;
639 DWORD dwType, PrinterAttr, cbPrinterAttr, SetData, size;
641 if (HIWORD(lpPrinter))
642 TRACE("printer %s\n",lpPrinter);
644 TRACE("printer %p\n",lpPrinter);
645 if (HIWORD(lpProfile))
646 TRACE("profile %s\n",lpProfile);
648 TRACE("profile %p\n",lpProfile);
649 TRACE("lpType %p\n",lpType);
651 if ((!lpPrinter) || (!lpProfile) || (!lpNeeded))
652 return ERROR_INVALID_PARAMETER;
654 RegStr_Printer = HeapAlloc(GetProcessHeap(), 0,
655 strlen(Printers) + strlen(lpPrinter) + 2);
656 strcpy(RegStr_Printer, Printers);
657 strcat(RegStr_Printer, lpPrinter);
659 if ((PtrToUlong(lpProfile) == INT_PD_DEFAULT_DEVMODE) || (HIWORD(lpProfile) &&
660 (!strcmp(lpProfile, DefaultDevMode)))) {
661 size = DrvGetPrinterDataInternal(RegStr_Printer, lpPrinterData, cbData,
662 INT_PD_DEFAULT_DEVMODE);
665 if ((lpPrinterData) && (*lpNeeded > cbData))
666 res = ERROR_MORE_DATA;
668 else res = ERROR_INVALID_PRINTER_NAME;
671 if ((PtrToUlong(lpProfile) == INT_PD_DEFAULT_MODEL) || (HIWORD(lpProfile) &&
672 (!strcmp(lpProfile, PrinterModel)))) {
674 if (!lpPrinterData) goto failed;
676 res = ERROR_MORE_DATA;
679 size = DrvGetPrinterDataInternal(RegStr_Printer, lpPrinterData, cbData,
680 INT_PD_DEFAULT_MODEL);
681 if ((size+1) && (lpType))
684 res = ERROR_INVALID_PRINTER_NAME;
688 if ((res = RegOpenKeyA(HKEY_LOCAL_MACHINE, RegStr_Printer, &hkey)))
691 if ((res = RegQueryValueExA(hkey, "Attributes", 0,
692 &dwType, (LPBYTE)&PrinterAttr, &cbPrinterAttr)))
694 if ((res = RegOpenKeyA(hkey, PrinterDriverData, &hkey2)))
697 res = RegQueryValueExA(hkey2, lpProfile, 0,
698 lpType, lpPrinterData, lpNeeded);
699 if ((res != ERROR_CANTREAD) &&
701 (PRINTER_ATTRIBUTE_ENABLE_BIDI|PRINTER_ATTRIBUTE_NETWORK))
702 == PRINTER_ATTRIBUTE_NETWORK))
704 if (!(res) && (*lpType == REG_DWORD) && (*(LPDWORD)lpPrinterData == -1))
705 res = ERROR_INVALID_DATA;
710 RegSetValueExA(hkey2, lpProfile, 0, REG_DWORD, (LPBYTE)&SetData, 4); /* no result returned */
715 if (hkey2) RegCloseKey(hkey2);
716 if (hkey) RegCloseKey(hkey);
717 HeapFree(GetProcessHeap(), 0, RegStr_Printer);
722 /******************************************************************
723 * DrvSetPrinterData (GDI.281)
726 DWORD WINAPI DrvSetPrinterData16(LPSTR lpPrinter, LPSTR lpProfile,
727 DWORD lpType, LPBYTE lpPrinterData,
730 LPSTR RegStr_Printer;
734 if (HIWORD(lpPrinter))
735 TRACE("printer %s\n",lpPrinter);
737 TRACE("printer %p\n",lpPrinter);
738 if (HIWORD(lpProfile))
739 TRACE("profile %s\n",lpProfile);
741 TRACE("profile %p\n",lpProfile);
742 TRACE("lpType %08x\n",lpType);
744 if ((!lpPrinter) || (!lpProfile) ||
745 (PtrToUlong(lpProfile) == INT_PD_DEFAULT_MODEL) || (HIWORD(lpProfile) &&
746 (!strcmp(lpProfile, PrinterModel))))
747 return ERROR_INVALID_PARAMETER;
749 RegStr_Printer = HeapAlloc(GetProcessHeap(), 0,
750 strlen(Printers) + strlen(lpPrinter) + 2);
751 strcpy(RegStr_Printer, Printers);
752 strcat(RegStr_Printer, lpPrinter);
754 if ((PtrToUlong(lpProfile) == INT_PD_DEFAULT_DEVMODE) || (HIWORD(lpProfile) &&
755 (!strcmp(lpProfile, DefaultDevMode)))) {
756 if ( RegOpenKeyA(HKEY_LOCAL_MACHINE, RegStr_Printer, &hkey)
758 RegSetValueExA(hkey, DefaultDevMode, 0, REG_BINARY,
759 lpPrinterData, dwSize) != ERROR_SUCCESS )
760 res = ERROR_INVALID_PRINTER_NAME;
764 strcat(RegStr_Printer, "\\");
766 if( (res = RegOpenKeyA(HKEY_LOCAL_MACHINE, RegStr_Printer, &hkey)) ==
770 res = RegDeleteValueA(hkey, lpProfile);
772 res = RegSetValueExA(hkey, lpProfile, 0, lpType,
773 lpPrinterData, dwSize);
777 if (hkey) RegCloseKey(hkey);
778 HeapFree(GetProcessHeap(), 0, RegStr_Printer);