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"
37 #ifdef HAVE_SYS_WAIT_H
38 # include <sys/wait.h>
47 #include "wine/winbase16.h"
48 #include "wine/wingdi16.h"
53 #include "wine/debug.h"
55 WINE_DEFAULT_DEBUG_CHANNEL(print);
57 static const char PrinterModel[] = "Printer Model";
58 static const char DefaultDevMode[] = "Default DevMode";
59 static const char PrinterDriverData[] = "PrinterDriverData";
60 static const char Printers[] = "System\\CurrentControlSet\\Control\\Print\\Printers\\";
62 /****************** misc. printer related functions */
65 * The following function should implement a queing system
74 static struct hpq *hpqueue;
76 /**********************************************************************
80 HPQ16 WINAPI CreatePQ16(INT16 size)
88 if (!(hpq = GlobalAlloc16(GMEM_SHARE|GMEM_MOVEABLE, tmp_size + 8)))
90 pPQ = GlobalLock16(hpq);
99 FIXME("(%d): stub\n",size);
104 /**********************************************************************
108 INT16 WINAPI DeletePQ16(HPQ16 hPQ)
110 return GlobalFree16(hPQ);
113 /**********************************************************************
114 * ExtractPQ (GDI.232)
117 INT16 WINAPI ExtractPQ16(HPQ16 hPQ)
119 struct hpq *queue, *prev, *current, *currentPrev;
120 int key = 0, tag = -1;
122 queue = current = hpqueue;
128 currentPrev = current;
129 current = current->next;
132 if (current->key < key)
144 prev->next = queue->next;
146 hpqueue = queue->next;
147 HeapFree(GetProcessHeap(), 0, queue);
150 TRACE("%x got tag %d key %d\n", hPQ, tag, key);
155 /**********************************************************************
159 INT16 WINAPI InsertPQ16(HPQ16 hPQ, INT16 tag, INT16 key)
161 struct hpq *queueItem = HeapAlloc(GetProcessHeap(), 0, sizeof(struct hpq));
162 if(queueItem == NULL) {
163 ERR("Memory exausted!\n");
166 queueItem->next = hpqueue;
168 queueItem->key = key;
169 queueItem->tag = tag;
171 FIXME("(%x %d %d): stub???\n", hPQ, tag, key);
175 /**********************************************************************
179 INT16 WINAPI MinPQ16(HPQ16 hPQ)
181 FIXME("(%x): stub\n", hPQ);
185 /**********************************************************************
189 INT16 WINAPI SizePQ16(HPQ16 hPQ, INT16 sizechange)
191 FIXME("(%x %d): stub\n", hPQ, sizechange);
198 * The following functions implement part of the spooling process to
199 * print manager. I would like to see wine have a version of print managers
200 * that used LPR/LPD. For simplicity print jobs will be sent to a file for
203 typedef struct PRINTJOB
212 } PRINTJOB, *PPRINTJOB;
214 #define MAX_PRINT_JOBS 1
217 static PPRINTJOB gPrintJobsTable[MAX_PRINT_JOBS];
220 static PPRINTJOB FindPrintJobFromHandle(HANDLE16 hHandle)
222 return gPrintJobsTable[0];
225 static int CreateSpoolFile(LPCSTR pszOutput, pid_t *out_pid)
229 const char *psCmdP = psCmd;
232 /* TTD convert the 'output device' into a spool file name */
234 if (pszOutput == NULL || *pszOutput == '\0' || out_pid == NULL)
240 /* @@ Wine registry key: HKCU\Software\Wine\Printing\Spooler */
241 if(!RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Printing\\Spooler", &hkey))
243 DWORD type, count = sizeof(psCmd);
244 RegQueryValueExA(hkey, pszOutput, 0, &type, (LPBYTE)psCmd, &count);
247 if (!psCmd[0] && !strncmp("LPR:",pszOutput,4))
248 sprintf(psCmd,"|lpr -P'%s'",pszOutput+4);
250 TRACE("Got printerSpoolCommand '%s' for output device '%s'\n",
256 while (*psCmdP && isspace(*psCmdP))
263 TRACE("command: '%s'\n", psCmdP);
269 ERR("pipe() failed!\n");
272 if ((*out_pid = fork()) == 0)
276 TRACE("In child need to exec %s\n",psCmdP);
281 /* reset signals that we previously set to SIG_IGN */
282 signal( SIGPIPE, SIG_DFL );
284 execl("/bin/sh", "/bin/sh", "-c", psCmdP, NULL);
290 TRACE("Need to execute a cmnd and pipe the output to it\n");
296 WCHAR psCmdPW[MAX_PATH];
298 TRACE("Just assume it's a file\n");
301 * The file name can be dos based, we have to find its
302 * corresponding Unix file name.
304 MultiByteToWideChar(CP_ACP, 0, psCmdP, -1, psCmdPW, MAX_PATH);
305 if ((buffer = wine_get_unix_file_name(psCmdPW)))
307 if ((fd = open(buffer, O_CREAT | O_TRUNC | O_WRONLY, 0666)) < 0)
309 ERR("Failed to create spool file '%s' ('%s'). (error %s)\n",
310 buffer, psCmdP, strerror(errno));
312 HeapFree(GetProcessHeap(), 0, buffer);
318 static int FreePrintJob(HANDLE16 hJob)
323 pPrintJob = FindPrintJobFromHandle(hJob);
324 if (pPrintJob != NULL)
327 gPrintJobsTable[pPrintJob->nIndex] = NULL;
328 HeapFree(GetProcessHeap(), 0, pPrintJob->pszOutput);
329 HeapFree(GetProcessHeap(), 0, pPrintJob->pszTitle);
330 if (pPrintJob->fd >= 0) close(pPrintJob->fd);
331 if (pPrintJob->pid > 0)
336 wret = waitpid(pPrintJob->pid, &status, 0);
337 } while (wret < 0 && errno == EINTR);
338 if (wret < 0 || !WIFEXITED(status) || WEXITSTATUS(status))
341 HeapFree(GetProcessHeap(), 0, pPrintJob);
346 /**********************************************************************
350 HPJOB16 WINAPI OpenJob16(LPCSTR lpOutput, LPCSTR lpTitle, HDC16 hDC)
352 HPJOB16 hHandle = (HPJOB16)SP_ERROR;
355 TRACE("'%s' '%s' %04x\n", lpOutput, lpTitle, hDC);
357 pPrintJob = gPrintJobsTable[0];
358 if (pPrintJob == NULL)
363 /* Try and create a spool file */
364 fd = CreateSpoolFile(lpOutput, &pid);
367 pPrintJob = HeapAlloc(GetProcessHeap(), 0, sizeof(PRINTJOB));
368 if(pPrintJob == NULL) {
369 WARN("Memory exausted!\n");
375 pPrintJob->pszOutput = HeapAlloc(GetProcessHeap(), 0, strlen(lpOutput)+1);
376 strcpy( pPrintJob->pszOutput, lpOutput );
379 pPrintJob->pszTitle = HeapAlloc(GetProcessHeap(), 0, strlen(lpTitle)+1);
380 strcpy( pPrintJob->pszTitle, lpTitle );
382 pPrintJob->hDC = hDC;
384 pPrintJob->pid = pid;
385 pPrintJob->nIndex = 0;
386 pPrintJob->hHandle = hHandle;
387 gPrintJobsTable[pPrintJob->nIndex] = pPrintJob;
390 TRACE("return %04x\n", hHandle);
394 /**********************************************************************
398 INT16 WINAPI CloseJob16(HPJOB16 hJob)
403 TRACE("%04x\n", hJob);
405 pPrintJob = FindPrintJobFromHandle(hJob);
406 if (pPrintJob != NULL)
414 /**********************************************************************
415 * WriteSpool (GDI.241)
418 INT16 WINAPI WriteSpool16(HPJOB16 hJob, LPSTR lpData, INT16 cch)
423 TRACE("%04x %p %04x\n", hJob, lpData, cch);
425 pPrintJob = FindPrintJobFromHandle(hJob);
426 if (pPrintJob != NULL && pPrintJob->fd >= 0 && cch)
428 if (write(pPrintJob->fd, lpData, cch) != cch)
433 /* FIXME: We just cannot call 16 bit functions from here, since we
434 * have acquired several locks (DC). And we do not really need to.
436 if (pPrintJob->hDC == 0) {
437 TRACE("hDC == 0 so no QueryAbort\n");
439 else if (!(QueryAbort16(pPrintJob->hDC, (nRet == SP_OUTOFDISK) ? nRet : 0 )))
441 CloseJob16(hJob); /* printing aborted */
449 typedef INT (WINAPI *MSGBOX_PROC)( HWND, LPCSTR, LPCSTR, UINT );
451 /**********************************************************************
452 * WriteDialog (GDI.242)
455 INT16 WINAPI WriteDialog16(HPJOB16 hJob, LPSTR lpMsg, INT16 cchMsg)
458 MSGBOX_PROC pMessageBoxA;
461 TRACE("%04x %04x '%s'\n", hJob, cchMsg, lpMsg);
463 if ((mod = GetModuleHandleA("user32.dll")))
465 if ((pMessageBoxA = (MSGBOX_PROC)GetProcAddress( mod, "MessageBoxA" )))
466 ret = pMessageBoxA(0, lpMsg, "Printing Error", MB_OKCANCEL);
472 /**********************************************************************
473 * DeleteJob (GDI.244)
476 INT16 WINAPI DeleteJob16(HPJOB16 hJob, INT16 nNotUsed)
480 TRACE("%04x\n", hJob);
482 nRet = FreePrintJob(hJob);
487 * The following two function would allow a page to be sent to the printer
488 * when it has been processed. For simplicity they haven't been implemented.
489 * This means a whole job has to be processed before it is sent to the printer.
492 /**********************************************************************
493 * StartSpoolPage (GDI.246)
496 INT16 WINAPI StartSpoolPage16(HPJOB16 hJob)
498 FIXME("StartSpoolPage GDI.246 unimplemented\n");
504 /**********************************************************************
505 * EndSpoolPage (GDI.247)
508 INT16 WINAPI EndSpoolPage16(HPJOB16 hJob)
510 FIXME("EndSpoolPage GDI.247 unimplemented\n");
515 /**********************************************************************
516 * GetSpoolJob (GDI.245)
519 DWORD WINAPI GetSpoolJob16(int nOption, LONG param)
522 TRACE("In GetSpoolJob param 0x%x noption %d\n",param, nOption);
527 /******************************************************************
528 * DrvGetPrinterDataInternal
530 * Helper for DrvGetPrinterData
532 static DWORD DrvGetPrinterDataInternal(LPCSTR RegStr_Printer,
533 LPBYTE lpPrinterData, int cbData, int what)
537 DWORD dwType, cbQueryData;
539 if (!(RegOpenKeyA(HKEY_LOCAL_MACHINE, RegStr_Printer, &hkey))) {
540 if (what == INT_PD_DEFAULT_DEVMODE) { /* "Default DevMode" */
541 if (!(RegQueryValueExA(hkey, DefaultDevMode, 0, &dwType, 0, &cbQueryData))) {
544 else if ((cbQueryData) && (cbQueryData <= cbData)) {
545 cbQueryData = cbData;
546 if (RegQueryValueExA(hkey, DefaultDevMode, 0,
547 &dwType, lpPrinterData, &cbQueryData))
551 } else { /* "Printer Driver" */
553 RegQueryValueExA(hkey, "Printer Driver", 0,
554 &dwType, lpPrinterData, &cbQueryData);
558 if (hkey) RegCloseKey(hkey);
562 /******************************************************************
563 * DrvGetPrinterData (GDI.282)
566 DWORD WINAPI DrvGetPrinterData16(LPSTR lpPrinter, LPSTR lpProfile,
567 LPDWORD lpType, LPBYTE lpPrinterData,
568 int cbData, LPDWORD lpNeeded)
570 LPSTR RegStr_Printer;
571 HKEY hkey = 0, hkey2 = 0;
573 DWORD dwType, PrinterAttr, cbPrinterAttr, SetData, size;
575 if (HIWORD(lpPrinter))
576 TRACE("printer %s\n",lpPrinter);
578 TRACE("printer %p\n",lpPrinter);
579 if (HIWORD(lpProfile))
580 TRACE("profile %s\n",lpProfile);
582 TRACE("profile %p\n",lpProfile);
583 TRACE("lpType %p\n",lpType);
585 if ((!lpPrinter) || (!lpProfile) || (!lpNeeded))
586 return ERROR_INVALID_PARAMETER;
588 RegStr_Printer = HeapAlloc(GetProcessHeap(), 0,
589 strlen(Printers) + strlen(lpPrinter) + 2);
590 strcpy(RegStr_Printer, Printers);
591 strcat(RegStr_Printer, lpPrinter);
593 if ((PtrToUlong(lpProfile) == INT_PD_DEFAULT_DEVMODE) || (HIWORD(lpProfile) &&
594 (!strcmp(lpProfile, DefaultDevMode)))) {
595 size = DrvGetPrinterDataInternal(RegStr_Printer, lpPrinterData, cbData,
596 INT_PD_DEFAULT_DEVMODE);
599 if ((lpPrinterData) && (*lpNeeded > cbData))
600 res = ERROR_MORE_DATA;
602 else res = ERROR_INVALID_PRINTER_NAME;
605 if ((PtrToUlong(lpProfile) == INT_PD_DEFAULT_MODEL) || (HIWORD(lpProfile) &&
606 (!strcmp(lpProfile, PrinterModel)))) {
608 if (!lpPrinterData) goto failed;
610 res = ERROR_MORE_DATA;
613 size = DrvGetPrinterDataInternal(RegStr_Printer, lpPrinterData, cbData,
614 INT_PD_DEFAULT_MODEL);
615 if ((size+1) && (lpType))
618 res = ERROR_INVALID_PRINTER_NAME;
622 if ((res = RegOpenKeyA(HKEY_LOCAL_MACHINE, RegStr_Printer, &hkey)))
625 if ((res = RegQueryValueExA(hkey, "Attributes", 0,
626 &dwType, (LPBYTE)&PrinterAttr, &cbPrinterAttr)))
628 if ((res = RegOpenKeyA(hkey, PrinterDriverData, &hkey2)))
631 res = RegQueryValueExA(hkey2, lpProfile, 0,
632 lpType, lpPrinterData, lpNeeded);
633 if ((res != ERROR_CANTREAD) &&
635 (PRINTER_ATTRIBUTE_ENABLE_BIDI|PRINTER_ATTRIBUTE_NETWORK))
636 == PRINTER_ATTRIBUTE_NETWORK))
638 if (!(res) && (*lpType == REG_DWORD) && (*(LPDWORD)lpPrinterData == -1))
639 res = ERROR_INVALID_DATA;
644 RegSetValueExA(hkey2, lpProfile, 0, REG_DWORD, (LPBYTE)&SetData, 4); /* no result returned */
649 if (hkey2) RegCloseKey(hkey2);
650 if (hkey) RegCloseKey(hkey);
651 HeapFree(GetProcessHeap(), 0, RegStr_Printer);
656 /******************************************************************
657 * DrvSetPrinterData (GDI.281)
660 DWORD WINAPI DrvSetPrinterData16(LPSTR lpPrinter, LPSTR lpProfile,
661 DWORD lpType, LPBYTE lpPrinterData,
664 LPSTR RegStr_Printer;
668 if (HIWORD(lpPrinter))
669 TRACE("printer %s\n",lpPrinter);
671 TRACE("printer %p\n",lpPrinter);
672 if (HIWORD(lpProfile))
673 TRACE("profile %s\n",lpProfile);
675 TRACE("profile %p\n",lpProfile);
676 TRACE("lpType %08x\n",lpType);
678 if ((!lpPrinter) || (!lpProfile) ||
679 (PtrToUlong(lpProfile) == INT_PD_DEFAULT_MODEL) || (HIWORD(lpProfile) &&
680 (!strcmp(lpProfile, PrinterModel))))
681 return ERROR_INVALID_PARAMETER;
683 RegStr_Printer = HeapAlloc(GetProcessHeap(), 0,
684 strlen(Printers) + strlen(lpPrinter) + 2);
685 strcpy(RegStr_Printer, Printers);
686 strcat(RegStr_Printer, lpPrinter);
688 if ((PtrToUlong(lpProfile) == INT_PD_DEFAULT_DEVMODE) || (HIWORD(lpProfile) &&
689 (!strcmp(lpProfile, DefaultDevMode)))) {
690 if ( RegOpenKeyA(HKEY_LOCAL_MACHINE, RegStr_Printer, &hkey)
692 RegSetValueExA(hkey, DefaultDevMode, 0, REG_BINARY,
693 lpPrinterData, dwSize) != ERROR_SUCCESS )
694 res = ERROR_INVALID_PRINTER_NAME;
698 strcat(RegStr_Printer, "\\");
700 if( (res = RegOpenKeyA(HKEY_LOCAL_MACHINE, RegStr_Printer, &hkey)) ==
704 res = RegDeleteValueA(hkey, lpProfile);
706 res = RegSetValueExA(hkey, lpProfile, 0, lpType,
707 lpPrinterData, dwSize);
711 if (hkey) RegCloseKey(hkey);
712 HeapFree(GetProcessHeap(), 0, RegStr_Printer);