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 );
126 /****************** misc. printer related functions */
129 * The following function should implement a queing system
138 static struct hpq *hpqueue;
140 /**********************************************************************
144 HPQ16 WINAPI CreatePQ16(INT16 size)
151 tmp_size = size << 2;
152 if (!(hpq = GlobalAlloc16(GMEM_SHARE|GMEM_MOVEABLE, tmp_size + 8)))
154 pPQ = GlobalLock16(hpq);
163 FIXME("(%d): stub\n",size);
168 /**********************************************************************
172 INT16 WINAPI DeletePQ16(HPQ16 hPQ)
174 return GlobalFree16(hPQ);
177 /**********************************************************************
178 * ExtractPQ (GDI.232)
181 INT16 WINAPI ExtractPQ16(HPQ16 hPQ)
183 struct hpq *queue, *prev, *current, *currentPrev;
184 int key = 0, tag = -1;
185 currentPrev = prev = NULL;
186 queue = current = hpqueue;
192 currentPrev = current;
193 current = current->next;
196 if (current->key < key)
208 prev->next = queue->next;
210 hpqueue = queue->next;
211 HeapFree(GetProcessHeap(), 0, queue);
214 TRACE("%x got tag %d key %d\n", hPQ, tag, key);
219 /**********************************************************************
223 INT16 WINAPI InsertPQ16(HPQ16 hPQ, INT16 tag, INT16 key)
225 struct hpq *queueItem = HeapAlloc(GetProcessHeap(), 0, sizeof(struct hpq));
226 if(queueItem == NULL) {
227 ERR("Memory exausted!\n");
230 queueItem->next = hpqueue;
232 queueItem->key = key;
233 queueItem->tag = tag;
235 FIXME("(%x %d %d): stub???\n", hPQ, tag, key);
239 /**********************************************************************
243 INT16 WINAPI MinPQ16(HPQ16 hPQ)
245 FIXME("(%x): stub\n", hPQ);
249 /**********************************************************************
253 INT16 WINAPI SizePQ16(HPQ16 hPQ, INT16 sizechange)
255 FIXME("(%x %d): stub\n", hPQ, sizechange);
262 * The following functions implement part of the spooling process to
263 * print manager. I would like to see wine have a version of print managers
264 * that used LPR/LPD. For simplicity print jobs will be sent to a file for
267 typedef struct PRINTJOB
275 } PRINTJOB, *PPRINTJOB;
277 #define MAX_PRINT_JOBS 1
280 static PPRINTJOB gPrintJobsTable[MAX_PRINT_JOBS];
283 static PPRINTJOB FindPrintJobFromHandle(HANDLE16 hHandle)
285 return gPrintJobsTable[0];
288 static int CreateSpoolFile(LPCSTR pszOutput)
292 const char *psCmdP = psCmd;
295 /* TTD convert the 'output device' into a spool file name */
297 if (pszOutput == NULL || *pszOutput == '\0')
301 /* @@ Wine registry key: HKCU\Software\Wine\Printing\Spooler */
302 if(!RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Printing\\Spooler", &hkey))
304 DWORD type, count = sizeof(psCmd);
305 RegQueryValueExA(hkey, pszOutput, 0, &type, (LPBYTE)psCmd, &count);
308 if (!psCmd[0] && !strncmp("LPR:",pszOutput,4))
309 sprintf(psCmd,"|lpr -P'%s'",pszOutput+4);
311 TRACE("Got printerSpoolCommand '%s' for output device '%s'\n",
317 while (*psCmdP && isspace(*psCmdP))
324 TRACE("command: '%s'\n", psCmdP);
330 ERR("pipe() failed!\n");
337 TRACE("In child need to exec %s\n",psCmdP);
342 /* reset signals that we previously set to SIG_IGN */
343 signal( SIGPIPE, SIG_DFL );
344 signal( SIGCHLD, SIG_DFL );
346 execl("/bin/sh", "/bin/sh", "-c", psCmdP, NULL);
352 TRACE("Need to execute a cmnd and pipe the output to it\n");
358 WCHAR psCmdPW[MAX_PATH];
360 TRACE("Just assume it's a file\n");
363 * The file name can be dos based, we have to find its
364 * corresponding Unix file name.
366 MultiByteToWideChar(CP_ACP, 0, psCmdP, -1, psCmdPW, MAX_PATH);
367 if ((buffer = wine_get_unix_file_name(psCmdPW)))
369 if ((fd = open(buffer, O_CREAT | O_TRUNC | O_WRONLY, 0666)) < 0)
371 ERR("Failed to create spool file '%s' ('%s'). (error %s)\n",
372 buffer, psCmdP, strerror(errno));
374 HeapFree(GetProcessHeap(), 0, buffer);
380 static int FreePrintJob(HANDLE16 hJob)
385 pPrintJob = FindPrintJobFromHandle(hJob);
386 if (pPrintJob != NULL)
388 gPrintJobsTable[pPrintJob->nIndex] = NULL;
389 HeapFree(GetProcessHeap(), 0, pPrintJob->pszOutput);
390 HeapFree(GetProcessHeap(), 0, pPrintJob->pszTitle);
391 if (pPrintJob->fd >= 0) close(pPrintJob->fd);
392 HeapFree(GetProcessHeap(), 0, pPrintJob);
398 /**********************************************************************
402 HPJOB16 WINAPI OpenJob16(LPCSTR lpOutput, LPCSTR lpTitle, HDC16 hDC)
404 HPJOB16 hHandle = (HPJOB16)SP_ERROR;
407 TRACE("'%s' '%s' %04x\n", lpOutput, lpTitle, hDC);
409 pPrintJob = gPrintJobsTable[0];
410 if (pPrintJob == NULL)
414 /* Try and create a spool file */
415 fd = CreateSpoolFile(lpOutput);
418 pPrintJob = HeapAlloc(GetProcessHeap(), 0, sizeof(PRINTJOB));
419 if(pPrintJob == NULL) {
420 WARN("Memory exausted!\n");
426 pPrintJob->pszOutput = HeapAlloc(GetProcessHeap(), 0, strlen(lpOutput)+1);
427 strcpy( pPrintJob->pszOutput, lpOutput );
430 pPrintJob->pszTitle = HeapAlloc(GetProcessHeap(), 0, strlen(lpTitle)+1);
431 strcpy( pPrintJob->pszTitle, lpTitle );
433 pPrintJob->hDC = hDC;
435 pPrintJob->nIndex = 0;
436 pPrintJob->hHandle = hHandle;
437 gPrintJobsTable[pPrintJob->nIndex] = pPrintJob;
440 TRACE("return %04x\n", hHandle);
444 /**********************************************************************
448 INT16 WINAPI CloseJob16(HPJOB16 hJob)
451 PPRINTJOB pPrintJob = NULL;
453 TRACE("%04x\n", hJob);
455 pPrintJob = FindPrintJobFromHandle(hJob);
456 if (pPrintJob != NULL)
458 /* Close the spool file */
459 close(pPrintJob->fd);
466 /**********************************************************************
467 * WriteSpool (GDI.241)
470 INT16 WINAPI WriteSpool16(HPJOB16 hJob, LPSTR lpData, INT16 cch)
473 PPRINTJOB pPrintJob = NULL;
475 TRACE("%04x %p %04x\n", hJob, lpData, cch);
477 pPrintJob = FindPrintJobFromHandle(hJob);
478 if (pPrintJob != NULL && pPrintJob->fd >= 0 && cch)
480 if (write(pPrintJob->fd, lpData, cch) != cch)
485 /* FIXME: We just cannot call 16 bit functions from here, since we
486 * have acquired several locks (DC). And we do not really need to.
488 if (pPrintJob->hDC == 0) {
489 TRACE("hDC == 0 so no QueryAbort\n");
491 else if (!(QueryAbort16(pPrintJob->hDC, (nRet == SP_OUTOFDISK) ? nRet : 0 )))
493 CloseJob16(hJob); /* printing aborted */
501 typedef INT (WINAPI *MSGBOX_PROC)( HWND, LPCSTR, LPCSTR, UINT );
503 /**********************************************************************
504 * WriteDialog (GDI.242)
507 INT16 WINAPI WriteDialog16(HPJOB16 hJob, LPSTR lpMsg, INT16 cchMsg)
510 MSGBOX_PROC pMessageBoxA;
513 TRACE("%04x %04x '%s'\n", hJob, cchMsg, lpMsg);
515 if ((mod = GetModuleHandleA("user32.dll")))
517 if ((pMessageBoxA = (MSGBOX_PROC)GetProcAddress( mod, "MessageBoxA" )))
518 ret = pMessageBoxA(0, lpMsg, "Printing Error", MB_OKCANCEL);
524 /**********************************************************************
525 * DeleteJob (GDI.244)
528 INT16 WINAPI DeleteJob16(HPJOB16 hJob, INT16 nNotUsed)
532 TRACE("%04x\n", hJob);
534 nRet = FreePrintJob(hJob);
539 * The following two function would allow a page to be sent to the printer
540 * when it has been processed. For simplicity they haven't been implemented.
541 * This means a whole job has to be processed before it is sent to the printer.
544 /**********************************************************************
545 * StartSpoolPage (GDI.246)
548 INT16 WINAPI StartSpoolPage16(HPJOB16 hJob)
550 FIXME("StartSpoolPage GDI.246 unimplemented\n");
556 /**********************************************************************
557 * EndSpoolPage (GDI.247)
560 INT16 WINAPI EndSpoolPage16(HPJOB16 hJob)
562 FIXME("EndSpoolPage GDI.247 unimplemented\n");
567 /**********************************************************************
568 * GetSpoolJob (GDI.245)
571 DWORD WINAPI GetSpoolJob16(int nOption, LONG param)
574 TRACE("In GetSpoolJob param 0x%x noption %d\n",param, nOption);
579 /******************************************************************
580 * DrvGetPrinterDataInternal
582 * Helper for DrvGetPrinterData
584 static DWORD DrvGetPrinterDataInternal(LPSTR RegStr_Printer,
585 LPBYTE lpPrinterData, int cbData, int what)
589 DWORD dwType, cbQueryData;
591 if (!(RegOpenKeyA(HKEY_LOCAL_MACHINE, RegStr_Printer, &hkey))) {
592 if (what == INT_PD_DEFAULT_DEVMODE) { /* "Default DevMode" */
593 if (!(RegQueryValueExA(hkey, DefaultDevMode, 0, &dwType, 0, &cbQueryData))) {
596 else if ((cbQueryData) && (cbQueryData <= cbData)) {
597 cbQueryData = cbData;
598 if (RegQueryValueExA(hkey, DefaultDevMode, 0,
599 &dwType, lpPrinterData, &cbQueryData))
603 } else { /* "Printer Driver" */
605 RegQueryValueExA(hkey, "Printer Driver", 0,
606 &dwType, lpPrinterData, &cbQueryData);
610 if (hkey) RegCloseKey(hkey);
614 /******************************************************************
615 * DrvGetPrinterData (GDI.282)
618 DWORD WINAPI DrvGetPrinterData16(LPSTR lpPrinter, LPSTR lpProfile,
619 LPDWORD lpType, LPBYTE lpPrinterData,
620 int cbData, LPDWORD lpNeeded)
622 LPSTR RegStr_Printer;
623 HKEY hkey = 0, hkey2 = 0;
625 DWORD dwType, PrinterAttr, cbPrinterAttr, SetData, size;
627 if (HIWORD(lpPrinter))
628 TRACE("printer %s\n",lpPrinter);
630 TRACE("printer %p\n",lpPrinter);
631 if (HIWORD(lpProfile))
632 TRACE("profile %s\n",lpProfile);
634 TRACE("profile %p\n",lpProfile);
635 TRACE("lpType %p\n",lpType);
637 if ((!lpPrinter) || (!lpProfile) || (!lpNeeded))
638 return ERROR_INVALID_PARAMETER;
640 RegStr_Printer = HeapAlloc(GetProcessHeap(), 0,
641 strlen(Printers) + strlen(lpPrinter) + 2);
642 strcpy(RegStr_Printer, Printers);
643 strcat(RegStr_Printer, lpPrinter);
645 if ((PtrToUlong(lpProfile) == INT_PD_DEFAULT_DEVMODE) || (HIWORD(lpProfile) &&
646 (!strcmp(lpProfile, DefaultDevMode)))) {
647 size = DrvGetPrinterDataInternal(RegStr_Printer, lpPrinterData, cbData,
648 INT_PD_DEFAULT_DEVMODE);
651 if ((lpPrinterData) && (*lpNeeded > cbData))
652 res = ERROR_MORE_DATA;
654 else res = ERROR_INVALID_PRINTER_NAME;
657 if ((PtrToUlong(lpProfile) == INT_PD_DEFAULT_MODEL) || (HIWORD(lpProfile) &&
658 (!strcmp(lpProfile, PrinterModel)))) {
660 if (!lpPrinterData) goto failed;
662 res = ERROR_MORE_DATA;
665 size = DrvGetPrinterDataInternal(RegStr_Printer, lpPrinterData, cbData,
666 INT_PD_DEFAULT_MODEL);
667 if ((size+1) && (lpType))
670 res = ERROR_INVALID_PRINTER_NAME;
674 if ((res = RegOpenKeyA(HKEY_LOCAL_MACHINE, RegStr_Printer, &hkey)))
677 if ((res = RegQueryValueExA(hkey, "Attributes", 0,
678 &dwType, (LPBYTE)&PrinterAttr, &cbPrinterAttr)))
680 if ((res = RegOpenKeyA(hkey, PrinterDriverData, &hkey2)))
683 res = RegQueryValueExA(hkey2, lpProfile, 0,
684 lpType, lpPrinterData, lpNeeded);
685 if ((res != ERROR_CANTREAD) &&
687 (PRINTER_ATTRIBUTE_ENABLE_BIDI|PRINTER_ATTRIBUTE_NETWORK))
688 == PRINTER_ATTRIBUTE_NETWORK))
690 if (!(res) && (*lpType == REG_DWORD) && (*(LPDWORD)lpPrinterData == -1))
691 res = ERROR_INVALID_DATA;
696 RegSetValueExA(hkey2, lpProfile, 0, REG_DWORD, (LPBYTE)&SetData, 4); /* no result returned */
701 if (hkey2) RegCloseKey(hkey2);
702 if (hkey) RegCloseKey(hkey);
703 HeapFree(GetProcessHeap(), 0, RegStr_Printer);
708 /******************************************************************
709 * DrvSetPrinterData (GDI.281)
712 DWORD WINAPI DrvSetPrinterData16(LPSTR lpPrinter, LPSTR lpProfile,
713 DWORD lpType, LPBYTE lpPrinterData,
716 LPSTR RegStr_Printer;
720 if (HIWORD(lpPrinter))
721 TRACE("printer %s\n",lpPrinter);
723 TRACE("printer %p\n",lpPrinter);
724 if (HIWORD(lpProfile))
725 TRACE("profile %s\n",lpProfile);
727 TRACE("profile %p\n",lpProfile);
728 TRACE("lpType %08x\n",lpType);
730 if ((!lpPrinter) || (!lpProfile) ||
731 (PtrToUlong(lpProfile) == INT_PD_DEFAULT_MODEL) || (HIWORD(lpProfile) &&
732 (!strcmp(lpProfile, PrinterModel))))
733 return ERROR_INVALID_PARAMETER;
735 RegStr_Printer = HeapAlloc(GetProcessHeap(), 0,
736 strlen(Printers) + strlen(lpPrinter) + 2);
737 strcpy(RegStr_Printer, Printers);
738 strcat(RegStr_Printer, lpPrinter);
740 if ((PtrToUlong(lpProfile) == INT_PD_DEFAULT_DEVMODE) || (HIWORD(lpProfile) &&
741 (!strcmp(lpProfile, DefaultDevMode)))) {
742 if ( RegOpenKeyA(HKEY_LOCAL_MACHINE, RegStr_Printer, &hkey)
744 RegSetValueExA(hkey, DefaultDevMode, 0, REG_BINARY,
745 lpPrinterData, dwSize) != ERROR_SUCCESS )
746 res = ERROR_INVALID_PRINTER_NAME;
750 strcat(RegStr_Printer, "\\");
752 if( (res = RegOpenKeyA(HKEY_LOCAL_MACHINE, RegStr_Printer, &hkey)) ==
756 res = RegDeleteValueA(hkey, lpProfile);
758 res = RegSetValueExA(hkey, lpProfile, 0, lpType,
759 lpPrinterData, dwSize);
763 if (hkey) RegCloseKey(hkey);
764 HeapFree(GetProcessHeap(), 0, RegStr_Printer);