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"
52 WINE_DEFAULT_DEBUG_CHANNEL(print);
54 static const char PrinterModel[] = "Printer Model";
55 static const char DefaultDevMode[] = "Default DevMode";
56 static const char PrinterDriverData[] = "PrinterDriverData";
57 static const char Printers[] = "System\\CurrentControlSet\\Control\\Print\\Printers\\";
59 /****************** misc. printer related functions */
62 * The following function should implement a queing system
71 static struct hpq *hpqueue;
73 /**********************************************************************
77 HPQ16 WINAPI CreatePQ16(INT16 size)
85 if (!(hpq = GlobalAlloc16(GMEM_SHARE|GMEM_MOVEABLE, tmp_size + 8)))
87 pPQ = GlobalLock16(hpq);
96 FIXME("(%d): stub\n",size);
101 /**********************************************************************
105 INT16 WINAPI DeletePQ16(HPQ16 hPQ)
107 return GlobalFree16(hPQ);
110 /**********************************************************************
111 * ExtractPQ (GDI.232)
114 INT16 WINAPI ExtractPQ16(HPQ16 hPQ)
116 struct hpq *queue, *prev, *current, *currentPrev;
117 int key = 0, tag = -1;
118 currentPrev = prev = NULL;
119 queue = current = hpqueue;
125 currentPrev = current;
126 current = current->next;
129 if (current->key < key)
141 prev->next = queue->next;
143 hpqueue = queue->next;
144 HeapFree(GetProcessHeap(), 0, queue);
147 TRACE("%x got tag %d key %d\n", hPQ, tag, key);
152 /**********************************************************************
156 INT16 WINAPI InsertPQ16(HPQ16 hPQ, INT16 tag, INT16 key)
158 struct hpq *queueItem = HeapAlloc(GetProcessHeap(), 0, sizeof(struct hpq));
159 if(queueItem == NULL) {
160 ERR("Memory exausted!\n");
163 queueItem->next = hpqueue;
165 queueItem->key = key;
166 queueItem->tag = tag;
168 FIXME("(%x %d %d): stub???\n", hPQ, tag, key);
172 /**********************************************************************
176 INT16 WINAPI MinPQ16(HPQ16 hPQ)
178 FIXME("(%x): stub\n", hPQ);
182 /**********************************************************************
186 INT16 WINAPI SizePQ16(HPQ16 hPQ, INT16 sizechange)
188 FIXME("(%x %d): stub\n", hPQ, sizechange);
195 * The following functions implement part of the spooling process to
196 * print manager. I would like to see wine have a version of print managers
197 * that used LPR/LPD. For simplicity print jobs will be sent to a file for
200 typedef struct PRINTJOB
208 } PRINTJOB, *PPRINTJOB;
210 #define MAX_PRINT_JOBS 1
213 static PPRINTJOB gPrintJobsTable[MAX_PRINT_JOBS];
216 static PPRINTJOB FindPrintJobFromHandle(HANDLE16 hHandle)
218 return gPrintJobsTable[0];
221 static int CreateSpoolFile(LPCSTR pszOutput)
225 const char *psCmdP = psCmd;
228 /* TTD convert the 'output device' into a spool file name */
230 if (pszOutput == NULL || *pszOutput == '\0')
234 /* @@ Wine registry key: HKCU\Software\Wine\Printing\Spooler */
235 if(!RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Printing\\Spooler", &hkey))
237 DWORD type, count = sizeof(psCmd);
238 RegQueryValueExA(hkey, pszOutput, 0, &type, (LPBYTE)psCmd, &count);
241 if (!psCmd[0] && !strncmp("LPR:",pszOutput,4))
242 sprintf(psCmd,"|lpr -P'%s'",pszOutput+4);
244 TRACE("Got printerSpoolCommand '%s' for output device '%s'\n",
250 while (*psCmdP && isspace(*psCmdP))
257 TRACE("command: '%s'\n", psCmdP);
263 ERR("pipe() failed!\n");
270 TRACE("In child need to exec %s\n",psCmdP);
275 /* reset signals that we previously set to SIG_IGN */
276 signal( SIGPIPE, SIG_DFL );
277 signal( SIGCHLD, SIG_DFL );
279 execl("/bin/sh", "/bin/sh", "-c", psCmdP, NULL);
285 TRACE("Need to execute a cmnd and pipe the output to it\n");
291 WCHAR psCmdPW[MAX_PATH];
293 TRACE("Just assume it's a file\n");
296 * The file name can be dos based, we have to find its
297 * corresponding Unix file name.
299 MultiByteToWideChar(CP_ACP, 0, psCmdP, -1, psCmdPW, MAX_PATH);
300 if ((buffer = wine_get_unix_file_name(psCmdPW)))
302 if ((fd = open(buffer, O_CREAT | O_TRUNC | O_WRONLY, 0666)) < 0)
304 ERR("Failed to create spool file '%s' ('%s'). (error %s)\n",
305 buffer, psCmdP, strerror(errno));
307 HeapFree(GetProcessHeap(), 0, buffer);
313 static int FreePrintJob(HANDLE16 hJob)
318 pPrintJob = FindPrintJobFromHandle(hJob);
319 if (pPrintJob != NULL)
321 gPrintJobsTable[pPrintJob->nIndex] = NULL;
322 HeapFree(GetProcessHeap(), 0, pPrintJob->pszOutput);
323 HeapFree(GetProcessHeap(), 0, pPrintJob->pszTitle);
324 if (pPrintJob->fd >= 0) close(pPrintJob->fd);
325 HeapFree(GetProcessHeap(), 0, pPrintJob);
331 /**********************************************************************
335 HPJOB16 WINAPI OpenJob16(LPCSTR lpOutput, LPCSTR lpTitle, HDC16 hDC)
337 HPJOB16 hHandle = (HPJOB16)SP_ERROR;
340 TRACE("'%s' '%s' %04x\n", lpOutput, lpTitle, hDC);
342 pPrintJob = gPrintJobsTable[0];
343 if (pPrintJob == NULL)
347 /* Try and create a spool file */
348 fd = CreateSpoolFile(lpOutput);
351 pPrintJob = HeapAlloc(GetProcessHeap(), 0, sizeof(PRINTJOB));
352 if(pPrintJob == NULL) {
353 WARN("Memory exausted!\n");
359 pPrintJob->pszOutput = HeapAlloc(GetProcessHeap(), 0, strlen(lpOutput)+1);
360 strcpy( pPrintJob->pszOutput, lpOutput );
363 pPrintJob->pszTitle = HeapAlloc(GetProcessHeap(), 0, strlen(lpTitle)+1);
364 strcpy( pPrintJob->pszTitle, lpTitle );
366 pPrintJob->hDC = hDC;
368 pPrintJob->nIndex = 0;
369 pPrintJob->hHandle = hHandle;
370 gPrintJobsTable[pPrintJob->nIndex] = pPrintJob;
373 TRACE("return %04x\n", hHandle);
377 /**********************************************************************
381 INT16 WINAPI CloseJob16(HPJOB16 hJob)
384 PPRINTJOB pPrintJob = NULL;
386 TRACE("%04x\n", hJob);
388 pPrintJob = FindPrintJobFromHandle(hJob);
389 if (pPrintJob != NULL)
391 /* Close the spool file */
392 close(pPrintJob->fd);
399 /**********************************************************************
400 * WriteSpool (GDI.241)
403 INT16 WINAPI WriteSpool16(HPJOB16 hJob, LPSTR lpData, INT16 cch)
406 PPRINTJOB pPrintJob = NULL;
408 TRACE("%04x %p %04x\n", hJob, lpData, cch);
410 pPrintJob = FindPrintJobFromHandle(hJob);
411 if (pPrintJob != NULL && pPrintJob->fd >= 0 && cch)
413 if (write(pPrintJob->fd, lpData, cch) != cch)
418 /* FIXME: We just cannot call 16 bit functions from here, since we
419 * have acquired several locks (DC). And we do not really need to.
421 if (pPrintJob->hDC == 0) {
422 TRACE("hDC == 0 so no QueryAbort\n");
424 else if (!(QueryAbort16(pPrintJob->hDC, (nRet == SP_OUTOFDISK) ? nRet : 0 )))
426 CloseJob16(hJob); /* printing aborted */
434 typedef INT (WINAPI *MSGBOX_PROC)( HWND, LPCSTR, LPCSTR, UINT );
436 /**********************************************************************
437 * WriteDialog (GDI.242)
440 INT16 WINAPI WriteDialog16(HPJOB16 hJob, LPSTR lpMsg, INT16 cchMsg)
443 MSGBOX_PROC pMessageBoxA;
446 TRACE("%04x %04x '%s'\n", hJob, cchMsg, lpMsg);
448 if ((mod = GetModuleHandleA("user32.dll")))
450 if ((pMessageBoxA = (MSGBOX_PROC)GetProcAddress( mod, "MessageBoxA" )))
451 ret = pMessageBoxA(0, lpMsg, "Printing Error", MB_OKCANCEL);
457 /**********************************************************************
458 * DeleteJob (GDI.244)
461 INT16 WINAPI DeleteJob16(HPJOB16 hJob, INT16 nNotUsed)
465 TRACE("%04x\n", hJob);
467 nRet = FreePrintJob(hJob);
472 * The following two function would allow a page to be sent to the printer
473 * when it has been processed. For simplicity they haven't been implemented.
474 * This means a whole job has to be processed before it is sent to the printer.
477 /**********************************************************************
478 * StartSpoolPage (GDI.246)
481 INT16 WINAPI StartSpoolPage16(HPJOB16 hJob)
483 FIXME("StartSpoolPage GDI.246 unimplemented\n");
489 /**********************************************************************
490 * EndSpoolPage (GDI.247)
493 INT16 WINAPI EndSpoolPage16(HPJOB16 hJob)
495 FIXME("EndSpoolPage GDI.247 unimplemented\n");
500 /**********************************************************************
501 * GetSpoolJob (GDI.245)
504 DWORD WINAPI GetSpoolJob16(int nOption, LONG param)
507 TRACE("In GetSpoolJob param 0x%x noption %d\n",param, nOption);
512 /******************************************************************
513 * DrvGetPrinterDataInternal
515 * Helper for DrvGetPrinterData
517 static DWORD DrvGetPrinterDataInternal(LPSTR RegStr_Printer,
518 LPBYTE lpPrinterData, int cbData, int what)
522 DWORD dwType, cbQueryData;
524 if (!(RegOpenKeyA(HKEY_LOCAL_MACHINE, RegStr_Printer, &hkey))) {
525 if (what == INT_PD_DEFAULT_DEVMODE) { /* "Default DevMode" */
526 if (!(RegQueryValueExA(hkey, DefaultDevMode, 0, &dwType, 0, &cbQueryData))) {
529 else if ((cbQueryData) && (cbQueryData <= cbData)) {
530 cbQueryData = cbData;
531 if (RegQueryValueExA(hkey, DefaultDevMode, 0,
532 &dwType, lpPrinterData, &cbQueryData))
536 } else { /* "Printer Driver" */
538 RegQueryValueExA(hkey, "Printer Driver", 0,
539 &dwType, lpPrinterData, &cbQueryData);
543 if (hkey) RegCloseKey(hkey);
547 /******************************************************************
548 * DrvGetPrinterData (GDI.282)
551 DWORD WINAPI DrvGetPrinterData16(LPSTR lpPrinter, LPSTR lpProfile,
552 LPDWORD lpType, LPBYTE lpPrinterData,
553 int cbData, LPDWORD lpNeeded)
555 LPSTR RegStr_Printer;
556 HKEY hkey = 0, hkey2 = 0;
558 DWORD dwType, PrinterAttr, cbPrinterAttr, SetData, size;
560 if (HIWORD(lpPrinter))
561 TRACE("printer %s\n",lpPrinter);
563 TRACE("printer %p\n",lpPrinter);
564 if (HIWORD(lpProfile))
565 TRACE("profile %s\n",lpProfile);
567 TRACE("profile %p\n",lpProfile);
568 TRACE("lpType %p\n",lpType);
570 if ((!lpPrinter) || (!lpProfile) || (!lpNeeded))
571 return ERROR_INVALID_PARAMETER;
573 RegStr_Printer = HeapAlloc(GetProcessHeap(), 0,
574 strlen(Printers) + strlen(lpPrinter) + 2);
575 strcpy(RegStr_Printer, Printers);
576 strcat(RegStr_Printer, lpPrinter);
578 if ((PtrToUlong(lpProfile) == INT_PD_DEFAULT_DEVMODE) || (HIWORD(lpProfile) &&
579 (!strcmp(lpProfile, DefaultDevMode)))) {
580 size = DrvGetPrinterDataInternal(RegStr_Printer, lpPrinterData, cbData,
581 INT_PD_DEFAULT_DEVMODE);
584 if ((lpPrinterData) && (*lpNeeded > cbData))
585 res = ERROR_MORE_DATA;
587 else res = ERROR_INVALID_PRINTER_NAME;
590 if ((PtrToUlong(lpProfile) == INT_PD_DEFAULT_MODEL) || (HIWORD(lpProfile) &&
591 (!strcmp(lpProfile, PrinterModel)))) {
593 if (!lpPrinterData) goto failed;
595 res = ERROR_MORE_DATA;
598 size = DrvGetPrinterDataInternal(RegStr_Printer, lpPrinterData, cbData,
599 INT_PD_DEFAULT_MODEL);
600 if ((size+1) && (lpType))
603 res = ERROR_INVALID_PRINTER_NAME;
607 if ((res = RegOpenKeyA(HKEY_LOCAL_MACHINE, RegStr_Printer, &hkey)))
610 if ((res = RegQueryValueExA(hkey, "Attributes", 0,
611 &dwType, (LPBYTE)&PrinterAttr, &cbPrinterAttr)))
613 if ((res = RegOpenKeyA(hkey, PrinterDriverData, &hkey2)))
616 res = RegQueryValueExA(hkey2, lpProfile, 0,
617 lpType, lpPrinterData, lpNeeded);
618 if ((res != ERROR_CANTREAD) &&
620 (PRINTER_ATTRIBUTE_ENABLE_BIDI|PRINTER_ATTRIBUTE_NETWORK))
621 == PRINTER_ATTRIBUTE_NETWORK))
623 if (!(res) && (*lpType == REG_DWORD) && (*(LPDWORD)lpPrinterData == -1))
624 res = ERROR_INVALID_DATA;
629 RegSetValueExA(hkey2, lpProfile, 0, REG_DWORD, (LPBYTE)&SetData, 4); /* no result returned */
634 if (hkey2) RegCloseKey(hkey2);
635 if (hkey) RegCloseKey(hkey);
636 HeapFree(GetProcessHeap(), 0, RegStr_Printer);
641 /******************************************************************
642 * DrvSetPrinterData (GDI.281)
645 DWORD WINAPI DrvSetPrinterData16(LPSTR lpPrinter, LPSTR lpProfile,
646 DWORD lpType, LPBYTE lpPrinterData,
649 LPSTR RegStr_Printer;
653 if (HIWORD(lpPrinter))
654 TRACE("printer %s\n",lpPrinter);
656 TRACE("printer %p\n",lpPrinter);
657 if (HIWORD(lpProfile))
658 TRACE("profile %s\n",lpProfile);
660 TRACE("profile %p\n",lpProfile);
661 TRACE("lpType %08x\n",lpType);
663 if ((!lpPrinter) || (!lpProfile) ||
664 (PtrToUlong(lpProfile) == INT_PD_DEFAULT_MODEL) || (HIWORD(lpProfile) &&
665 (!strcmp(lpProfile, PrinterModel))))
666 return ERROR_INVALID_PARAMETER;
668 RegStr_Printer = HeapAlloc(GetProcessHeap(), 0,
669 strlen(Printers) + strlen(lpPrinter) + 2);
670 strcpy(RegStr_Printer, Printers);
671 strcat(RegStr_Printer, lpPrinter);
673 if ((PtrToUlong(lpProfile) == INT_PD_DEFAULT_DEVMODE) || (HIWORD(lpProfile) &&
674 (!strcmp(lpProfile, DefaultDevMode)))) {
675 if ( RegOpenKeyA(HKEY_LOCAL_MACHINE, RegStr_Printer, &hkey)
677 RegSetValueExA(hkey, DefaultDevMode, 0, REG_BINARY,
678 lpPrinterData, dwSize) != ERROR_SUCCESS )
679 res = ERROR_INVALID_PRINTER_NAME;
683 strcat(RegStr_Printer, "\\");
685 if( (res = RegOpenKeyA(HKEY_LOCAL_MACHINE, RegStr_Printer, &hkey)) ==
689 res = RegDeleteValueA(hkey, lpProfile);
691 res = RegSetValueExA(hkey, lpProfile, 0, lpType,
692 lpPrinterData, dwSize);
696 if (hkey) RegCloseKey(hkey);
697 HeapFree(GetProcessHeap(), 0, RegStr_Printer);