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 /****************** misc. printer related functions */
63 * The following function should implement a queing system
72 static struct hpq *hpqueue;
74 /**********************************************************************
78 HPQ16 WINAPI CreatePQ16(INT16 size)
86 if (!(hpq = GlobalAlloc16(GMEM_SHARE|GMEM_MOVEABLE, tmp_size + 8)))
88 pPQ = GlobalLock16(hpq);
97 FIXME("(%d): stub\n",size);
102 /**********************************************************************
106 INT16 WINAPI DeletePQ16(HPQ16 hPQ)
108 return GlobalFree16(hPQ);
111 /**********************************************************************
112 * ExtractPQ (GDI.232)
115 INT16 WINAPI ExtractPQ16(HPQ16 hPQ)
117 struct hpq *queue, *prev, *current, *currentPrev;
118 int key = 0, tag = -1;
119 currentPrev = prev = NULL;
120 queue = current = hpqueue;
126 currentPrev = current;
127 current = current->next;
130 if (current->key < key)
142 prev->next = queue->next;
144 hpqueue = queue->next;
145 HeapFree(GetProcessHeap(), 0, queue);
148 TRACE("%x got tag %d key %d\n", hPQ, tag, key);
153 /**********************************************************************
157 INT16 WINAPI InsertPQ16(HPQ16 hPQ, INT16 tag, INT16 key)
159 struct hpq *queueItem = HeapAlloc(GetProcessHeap(), 0, sizeof(struct hpq));
160 if(queueItem == NULL) {
161 ERR("Memory exausted!\n");
164 queueItem->next = hpqueue;
166 queueItem->key = key;
167 queueItem->tag = tag;
169 FIXME("(%x %d %d): stub???\n", hPQ, tag, key);
173 /**********************************************************************
177 INT16 WINAPI MinPQ16(HPQ16 hPQ)
179 FIXME("(%x): stub\n", hPQ);
183 /**********************************************************************
187 INT16 WINAPI SizePQ16(HPQ16 hPQ, INT16 sizechange)
189 FIXME("(%x %d): stub\n", hPQ, sizechange);
196 * The following functions implement part of the spooling process to
197 * print manager. I would like to see wine have a version of print managers
198 * that used LPR/LPD. For simplicity print jobs will be sent to a file for
201 typedef struct PRINTJOB
209 } PRINTJOB, *PPRINTJOB;
211 #define MAX_PRINT_JOBS 1
214 static PPRINTJOB gPrintJobsTable[MAX_PRINT_JOBS];
217 static PPRINTJOB FindPrintJobFromHandle(HANDLE16 hHandle)
219 return gPrintJobsTable[0];
222 static int CreateSpoolFile(LPCSTR pszOutput)
226 const char *psCmdP = psCmd;
229 /* TTD convert the 'output device' into a spool file name */
231 if (pszOutput == NULL || *pszOutput == '\0')
235 /* @@ Wine registry key: HKCU\Software\Wine\Printing\Spooler */
236 if(!RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Printing\\Spooler", &hkey))
238 DWORD type, count = sizeof(psCmd);
239 RegQueryValueExA(hkey, pszOutput, 0, &type, (LPBYTE)psCmd, &count);
242 if (!psCmd[0] && !strncmp("LPR:",pszOutput,4))
243 sprintf(psCmd,"|lpr -P'%s'",pszOutput+4);
245 TRACE("Got printerSpoolCommand '%s' for output device '%s'\n",
251 while (*psCmdP && isspace(*psCmdP))
258 TRACE("command: '%s'\n", psCmdP);
264 ERR("pipe() failed!\n");
271 TRACE("In child need to exec %s\n",psCmdP);
276 /* reset signals that we previously set to SIG_IGN */
277 signal( SIGPIPE, SIG_DFL );
278 signal( SIGCHLD, SIG_DFL );
280 execl("/bin/sh", "/bin/sh", "-c", psCmdP, NULL);
286 TRACE("Need to execute a cmnd and pipe the output to it\n");
292 WCHAR psCmdPW[MAX_PATH];
294 TRACE("Just assume it's a file\n");
297 * The file name can be dos based, we have to find its
298 * corresponding Unix file name.
300 MultiByteToWideChar(CP_ACP, 0, psCmdP, -1, psCmdPW, MAX_PATH);
301 if ((buffer = wine_get_unix_file_name(psCmdPW)))
303 if ((fd = open(buffer, O_CREAT | O_TRUNC | O_WRONLY, 0666)) < 0)
305 ERR("Failed to create spool file '%s' ('%s'). (error %s)\n",
306 buffer, psCmdP, strerror(errno));
308 HeapFree(GetProcessHeap(), 0, buffer);
314 static int FreePrintJob(HANDLE16 hJob)
319 pPrintJob = FindPrintJobFromHandle(hJob);
320 if (pPrintJob != NULL)
322 gPrintJobsTable[pPrintJob->nIndex] = NULL;
323 HeapFree(GetProcessHeap(), 0, pPrintJob->pszOutput);
324 HeapFree(GetProcessHeap(), 0, pPrintJob->pszTitle);
325 if (pPrintJob->fd >= 0) close(pPrintJob->fd);
326 HeapFree(GetProcessHeap(), 0, pPrintJob);
332 /**********************************************************************
336 HPJOB16 WINAPI OpenJob16(LPCSTR lpOutput, LPCSTR lpTitle, HDC16 hDC)
338 HPJOB16 hHandle = (HPJOB16)SP_ERROR;
341 TRACE("'%s' '%s' %04x\n", lpOutput, lpTitle, hDC);
343 pPrintJob = gPrintJobsTable[0];
344 if (pPrintJob == NULL)
348 /* Try and create a spool file */
349 fd = CreateSpoolFile(lpOutput);
352 pPrintJob = HeapAlloc(GetProcessHeap(), 0, sizeof(PRINTJOB));
353 if(pPrintJob == NULL) {
354 WARN("Memory exausted!\n");
360 pPrintJob->pszOutput = HeapAlloc(GetProcessHeap(), 0, strlen(lpOutput)+1);
361 strcpy( pPrintJob->pszOutput, lpOutput );
364 pPrintJob->pszTitle = HeapAlloc(GetProcessHeap(), 0, strlen(lpTitle)+1);
365 strcpy( pPrintJob->pszTitle, lpTitle );
367 pPrintJob->hDC = hDC;
369 pPrintJob->nIndex = 0;
370 pPrintJob->hHandle = hHandle;
371 gPrintJobsTable[pPrintJob->nIndex] = pPrintJob;
374 TRACE("return %04x\n", hHandle);
378 /**********************************************************************
382 INT16 WINAPI CloseJob16(HPJOB16 hJob)
385 PPRINTJOB pPrintJob = NULL;
387 TRACE("%04x\n", hJob);
389 pPrintJob = FindPrintJobFromHandle(hJob);
390 if (pPrintJob != NULL)
392 /* Close the spool file */
393 close(pPrintJob->fd);
400 /**********************************************************************
401 * WriteSpool (GDI.241)
404 INT16 WINAPI WriteSpool16(HPJOB16 hJob, LPSTR lpData, INT16 cch)
407 PPRINTJOB pPrintJob = NULL;
409 TRACE("%04x %p %04x\n", hJob, lpData, cch);
411 pPrintJob = FindPrintJobFromHandle(hJob);
412 if (pPrintJob != NULL && pPrintJob->fd >= 0 && cch)
414 if (write(pPrintJob->fd, lpData, cch) != cch)
419 /* FIXME: We just cannot call 16 bit functions from here, since we
420 * have acquired several locks (DC). And we do not really need to.
422 if (pPrintJob->hDC == 0) {
423 TRACE("hDC == 0 so no QueryAbort\n");
425 else if (!(QueryAbort16(pPrintJob->hDC, (nRet == SP_OUTOFDISK) ? nRet : 0 )))
427 CloseJob16(hJob); /* printing aborted */
435 typedef INT (WINAPI *MSGBOX_PROC)( HWND, LPCSTR, LPCSTR, UINT );
437 /**********************************************************************
438 * WriteDialog (GDI.242)
441 INT16 WINAPI WriteDialog16(HPJOB16 hJob, LPSTR lpMsg, INT16 cchMsg)
444 MSGBOX_PROC pMessageBoxA;
447 TRACE("%04x %04x '%s'\n", hJob, cchMsg, lpMsg);
449 if ((mod = GetModuleHandleA("user32.dll")))
451 if ((pMessageBoxA = (MSGBOX_PROC)GetProcAddress( mod, "MessageBoxA" )))
452 ret = pMessageBoxA(0, lpMsg, "Printing Error", MB_OKCANCEL);
458 /**********************************************************************
459 * DeleteJob (GDI.244)
462 INT16 WINAPI DeleteJob16(HPJOB16 hJob, INT16 nNotUsed)
466 TRACE("%04x\n", hJob);
468 nRet = FreePrintJob(hJob);
473 * The following two function would allow a page to be sent to the printer
474 * when it has been processed. For simplicity they haven't been implemented.
475 * This means a whole job has to be processed before it is sent to the printer.
478 /**********************************************************************
479 * StartSpoolPage (GDI.246)
482 INT16 WINAPI StartSpoolPage16(HPJOB16 hJob)
484 FIXME("StartSpoolPage GDI.246 unimplemented\n");
490 /**********************************************************************
491 * EndSpoolPage (GDI.247)
494 INT16 WINAPI EndSpoolPage16(HPJOB16 hJob)
496 FIXME("EndSpoolPage GDI.247 unimplemented\n");
501 /**********************************************************************
502 * GetSpoolJob (GDI.245)
505 DWORD WINAPI GetSpoolJob16(int nOption, LONG param)
508 TRACE("In GetSpoolJob param 0x%x noption %d\n",param, nOption);
513 /******************************************************************
514 * DrvGetPrinterDataInternal
516 * Helper for DrvGetPrinterData
518 static DWORD DrvGetPrinterDataInternal(LPSTR RegStr_Printer,
519 LPBYTE lpPrinterData, int cbData, int what)
523 DWORD dwType, cbQueryData;
525 if (!(RegOpenKeyA(HKEY_LOCAL_MACHINE, RegStr_Printer, &hkey))) {
526 if (what == INT_PD_DEFAULT_DEVMODE) { /* "Default DevMode" */
527 if (!(RegQueryValueExA(hkey, DefaultDevMode, 0, &dwType, 0, &cbQueryData))) {
530 else if ((cbQueryData) && (cbQueryData <= cbData)) {
531 cbQueryData = cbData;
532 if (RegQueryValueExA(hkey, DefaultDevMode, 0,
533 &dwType, lpPrinterData, &cbQueryData))
537 } else { /* "Printer Driver" */
539 RegQueryValueExA(hkey, "Printer Driver", 0,
540 &dwType, lpPrinterData, &cbQueryData);
544 if (hkey) RegCloseKey(hkey);
548 /******************************************************************
549 * DrvGetPrinterData (GDI.282)
552 DWORD WINAPI DrvGetPrinterData16(LPSTR lpPrinter, LPSTR lpProfile,
553 LPDWORD lpType, LPBYTE lpPrinterData,
554 int cbData, LPDWORD lpNeeded)
556 LPSTR RegStr_Printer;
557 HKEY hkey = 0, hkey2 = 0;
559 DWORD dwType, PrinterAttr, cbPrinterAttr, SetData, size;
561 if (HIWORD(lpPrinter))
562 TRACE("printer %s\n",lpPrinter);
564 TRACE("printer %p\n",lpPrinter);
565 if (HIWORD(lpProfile))
566 TRACE("profile %s\n",lpProfile);
568 TRACE("profile %p\n",lpProfile);
569 TRACE("lpType %p\n",lpType);
571 if ((!lpPrinter) || (!lpProfile) || (!lpNeeded))
572 return ERROR_INVALID_PARAMETER;
574 RegStr_Printer = HeapAlloc(GetProcessHeap(), 0,
575 strlen(Printers) + strlen(lpPrinter) + 2);
576 strcpy(RegStr_Printer, Printers);
577 strcat(RegStr_Printer, lpPrinter);
579 if ((PtrToUlong(lpProfile) == INT_PD_DEFAULT_DEVMODE) || (HIWORD(lpProfile) &&
580 (!strcmp(lpProfile, DefaultDevMode)))) {
581 size = DrvGetPrinterDataInternal(RegStr_Printer, lpPrinterData, cbData,
582 INT_PD_DEFAULT_DEVMODE);
585 if ((lpPrinterData) && (*lpNeeded > cbData))
586 res = ERROR_MORE_DATA;
588 else res = ERROR_INVALID_PRINTER_NAME;
591 if ((PtrToUlong(lpProfile) == INT_PD_DEFAULT_MODEL) || (HIWORD(lpProfile) &&
592 (!strcmp(lpProfile, PrinterModel)))) {
594 if (!lpPrinterData) goto failed;
596 res = ERROR_MORE_DATA;
599 size = DrvGetPrinterDataInternal(RegStr_Printer, lpPrinterData, cbData,
600 INT_PD_DEFAULT_MODEL);
601 if ((size+1) && (lpType))
604 res = ERROR_INVALID_PRINTER_NAME;
608 if ((res = RegOpenKeyA(HKEY_LOCAL_MACHINE, RegStr_Printer, &hkey)))
611 if ((res = RegQueryValueExA(hkey, "Attributes", 0,
612 &dwType, (LPBYTE)&PrinterAttr, &cbPrinterAttr)))
614 if ((res = RegOpenKeyA(hkey, PrinterDriverData, &hkey2)))
617 res = RegQueryValueExA(hkey2, lpProfile, 0,
618 lpType, lpPrinterData, lpNeeded);
619 if ((res != ERROR_CANTREAD) &&
621 (PRINTER_ATTRIBUTE_ENABLE_BIDI|PRINTER_ATTRIBUTE_NETWORK))
622 == PRINTER_ATTRIBUTE_NETWORK))
624 if (!(res) && (*lpType == REG_DWORD) && (*(LPDWORD)lpPrinterData == -1))
625 res = ERROR_INVALID_DATA;
630 RegSetValueExA(hkey2, lpProfile, 0, REG_DWORD, (LPBYTE)&SetData, 4); /* no result returned */
635 if (hkey2) RegCloseKey(hkey2);
636 if (hkey) RegCloseKey(hkey);
637 HeapFree(GetProcessHeap(), 0, RegStr_Printer);
642 /******************************************************************
643 * DrvSetPrinterData (GDI.281)
646 DWORD WINAPI DrvSetPrinterData16(LPSTR lpPrinter, LPSTR lpProfile,
647 DWORD lpType, LPBYTE lpPrinterData,
650 LPSTR RegStr_Printer;
654 if (HIWORD(lpPrinter))
655 TRACE("printer %s\n",lpPrinter);
657 TRACE("printer %p\n",lpPrinter);
658 if (HIWORD(lpProfile))
659 TRACE("profile %s\n",lpProfile);
661 TRACE("profile %p\n",lpProfile);
662 TRACE("lpType %08x\n",lpType);
664 if ((!lpPrinter) || (!lpProfile) ||
665 (PtrToUlong(lpProfile) == INT_PD_DEFAULT_MODEL) || (HIWORD(lpProfile) &&
666 (!strcmp(lpProfile, PrinterModel))))
667 return ERROR_INVALID_PARAMETER;
669 RegStr_Printer = HeapAlloc(GetProcessHeap(), 0,
670 strlen(Printers) + strlen(lpPrinter) + 2);
671 strcpy(RegStr_Printer, Printers);
672 strcat(RegStr_Printer, lpPrinter);
674 if ((PtrToUlong(lpProfile) == INT_PD_DEFAULT_DEVMODE) || (HIWORD(lpProfile) &&
675 (!strcmp(lpProfile, DefaultDevMode)))) {
676 if ( RegOpenKeyA(HKEY_LOCAL_MACHINE, RegStr_Printer, &hkey)
678 RegSetValueExA(hkey, DefaultDevMode, 0, REG_BINARY,
679 lpPrinterData, dwSize) != ERROR_SUCCESS )
680 res = ERROR_INVALID_PRINTER_NAME;
684 strcat(RegStr_Printer, "\\");
686 if( (res = RegOpenKeyA(HKEY_LOCAL_MACHINE, RegStr_Printer, &hkey)) ==
690 res = RegDeleteValueA(hkey, lpProfile);
692 res = RegSetValueExA(hkey, lpProfile, 0, lpType,
693 lpPrinterData, dwSize);
697 if (hkey) RegCloseKey(hkey);
698 HeapFree(GetProcessHeap(), 0, RegStr_Printer);