quartz: Free two assert calls from having side effects.
[wine/testsucceed.git] / dlls / gdi.exe16 / printdrv.c
blobf00eaa1c1477741fc9c54eca7491de8542ce85c7
1 /*
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
24 #include "config.h"
25 #include "wine/port.h"
27 #include <stdarg.h>
28 #include <stdio.h>
29 #include <signal.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <ctype.h>
33 #include <errno.h>
34 #ifdef HAVE_IO_H
35 # include <io.h>
36 #endif
37 #ifdef HAVE_UNISTD_H
38 # include <unistd.h>
39 #endif
40 #include <fcntl.h>
41 #include "windef.h"
42 #include "winbase.h"
43 #include "winuser.h"
44 #include "wine/winbase16.h"
45 #include "wine/wingdi16.h"
46 #include "winspool.h"
47 #include "winerror.h"
48 #include "winreg.h"
49 #include "wownt32.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
64 struct hpq
66 struct hpq *next;
67 int tag;
68 int key;
71 static struct hpq *hpqueue;
73 /**********************************************************************
74 * CreatePQ (GDI.230)
77 HPQ16 WINAPI CreatePQ16(INT16 size)
79 #if 0
80 HGLOBAL16 hpq = 0;
81 WORD tmp_size;
82 LPWORD pPQ;
84 tmp_size = size << 2;
85 if (!(hpq = GlobalAlloc16(GMEM_SHARE|GMEM_MOVEABLE, tmp_size + 8)))
86 return 0xffff;
87 pPQ = GlobalLock16(hpq);
88 *pPQ++ = 0;
89 *pPQ++ = tmp_size;
90 *pPQ++ = 0;
91 *pPQ++ = 0;
92 GlobalUnlock16(hpq);
94 return (HPQ16)hpq;
95 #else
96 FIXME("(%d): stub\n",size);
97 return 1;
98 #endif
101 /**********************************************************************
102 * DeletePQ (GDI.235)
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 prev = NULL;
119 queue = current = hpqueue;
120 if (current)
121 key = current->key;
123 while (current)
125 currentPrev = current;
126 current = current->next;
127 if (current)
129 if (current->key < key)
131 queue = current;
132 prev = currentPrev;
136 if (queue)
138 tag = queue->tag;
140 if (prev)
141 prev->next = queue->next;
142 else
143 hpqueue = queue->next;
144 HeapFree(GetProcessHeap(), 0, queue);
147 TRACE("%x got tag %d key %d\n", hPQ, tag, key);
149 return tag;
152 /**********************************************************************
153 * InsertPQ (GDI.233)
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");
161 return FALSE;
163 queueItem->next = hpqueue;
164 hpqueue = queueItem;
165 queueItem->key = key;
166 queueItem->tag = tag;
168 FIXME("(%x %d %d): stub???\n", hPQ, tag, key);
169 return TRUE;
172 /**********************************************************************
173 * MinPQ (GDI.231)
176 INT16 WINAPI MinPQ16(HPQ16 hPQ)
178 FIXME("(%x): stub\n", hPQ);
179 return 0;
182 /**********************************************************************
183 * SizePQ (GDI.234)
186 INT16 WINAPI SizePQ16(HPQ16 hPQ, INT16 sizechange)
188 FIXME("(%x %d): stub\n", hPQ, sizechange);
189 return -1;
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
198 * now.
200 typedef struct PRINTJOB
202 char *pszOutput;
203 char *pszTitle;
204 HDC16 hDC;
205 HANDLE16 hHandle;
206 int nIndex;
207 int fd;
208 } PRINTJOB, *PPRINTJOB;
210 #define MAX_PRINT_JOBS 1
211 #define SP_OK 1
213 static PPRINTJOB gPrintJobsTable[MAX_PRINT_JOBS];
216 static PPRINTJOB FindPrintJobFromHandle(HANDLE16 hHandle)
218 return gPrintJobsTable[0];
221 static int CreateSpoolFile(LPCSTR pszOutput)
223 int fd=-1;
224 char psCmd[1024];
225 const char *psCmdP = psCmd;
226 HKEY hkey;
228 /* TTD convert the 'output device' into a spool file name */
230 if (pszOutput == NULL || *pszOutput == '\0')
231 return -1;
233 psCmd[0] = 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);
239 RegCloseKey(hkey);
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",
245 psCmd, pszOutput);
246 if (!*psCmd)
247 psCmdP = pszOutput;
248 else
250 while (*psCmdP && isspace(*psCmdP))
252 psCmdP++;
254 if (!*psCmdP)
255 return -1;
257 TRACE("command: '%s'\n", psCmdP);
258 #ifdef HAVE_FORK
259 if (*psCmdP == '|')
261 int fds[2];
262 if (pipe(fds)) {
263 ERR("pipe() failed!\n");
264 return -1;
266 if (fork() == 0)
268 psCmdP++;
270 TRACE("In child need to exec %s\n",psCmdP);
271 close(0);
272 dup2(fds[0],0);
273 close (fds[1]);
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);
280 _exit(1);
283 close (fds[0]);
284 fd = fds[1];
285 TRACE("Need to execute a cmnd and pipe the output to it\n");
287 else
288 #endif
290 char *buffer;
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);
310 return fd;
313 static int FreePrintJob(HANDLE16 hJob)
315 int nRet = SP_ERROR;
316 PPRINTJOB pPrintJob;
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);
326 nRet = SP_OK;
328 return nRet;
331 /**********************************************************************
332 * OpenJob (GDI.240)
335 HPJOB16 WINAPI OpenJob16(LPCSTR lpOutput, LPCSTR lpTitle, HDC16 hDC)
337 HPJOB16 hHandle = (HPJOB16)SP_ERROR;
338 PPRINTJOB pPrintJob;
340 TRACE("'%s' '%s' %04x\n", lpOutput, lpTitle, hDC);
342 pPrintJob = gPrintJobsTable[0];
343 if (pPrintJob == NULL)
345 int fd;
347 /* Try and create a spool file */
348 fd = CreateSpoolFile(lpOutput);
349 if (fd >= 0)
351 pPrintJob = HeapAlloc(GetProcessHeap(), 0, sizeof(PRINTJOB));
352 if(pPrintJob == NULL) {
353 WARN("Memory exausted!\n");
354 return hHandle;
357 hHandle = 1;
359 pPrintJob->pszOutput = HeapAlloc(GetProcessHeap(), 0, strlen(lpOutput)+1);
360 strcpy( pPrintJob->pszOutput, lpOutput );
361 if(lpTitle)
363 pPrintJob->pszTitle = HeapAlloc(GetProcessHeap(), 0, strlen(lpTitle)+1);
364 strcpy( pPrintJob->pszTitle, lpTitle );
366 pPrintJob->hDC = hDC;
367 pPrintJob->fd = fd;
368 pPrintJob->nIndex = 0;
369 pPrintJob->hHandle = hHandle;
370 gPrintJobsTable[pPrintJob->nIndex] = pPrintJob;
373 TRACE("return %04x\n", hHandle);
374 return hHandle;
377 /**********************************************************************
378 * CloseJob (GDI.243)
381 INT16 WINAPI CloseJob16(HPJOB16 hJob)
383 int nRet = SP_ERROR;
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);
393 FreePrintJob(hJob);
394 nRet = 1;
396 return nRet;
399 /**********************************************************************
400 * WriteSpool (GDI.241)
403 INT16 WINAPI WriteSpool16(HPJOB16 hJob, LPSTR lpData, INT16 cch)
405 int nRet = SP_ERROR;
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)
414 nRet = SP_OUTOFDISK;
415 else
416 nRet = cch;
417 #if 0
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 */
427 nRet = SP_APPABORT;
429 #endif
431 return nRet;
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)
442 HMODULE mod;
443 MSGBOX_PROC pMessageBoxA;
444 INT16 ret = 0;
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);
453 return ret;
457 /**********************************************************************
458 * DeleteJob (GDI.244)
461 INT16 WINAPI DeleteJob16(HPJOB16 hJob, INT16 nNotUsed)
463 int nRet;
465 TRACE("%04x\n", hJob);
467 nRet = FreePrintJob(hJob);
468 return nRet;
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");
484 return 1;
489 /**********************************************************************
490 * EndSpoolPage (GDI.247)
493 INT16 WINAPI EndSpoolPage16(HPJOB16 hJob)
495 FIXME("EndSpoolPage GDI.247 unimplemented\n");
496 return 1;
500 /**********************************************************************
501 * GetSpoolJob (GDI.245)
504 DWORD WINAPI GetSpoolJob16(int nOption, LONG param)
506 DWORD retval = 0;
507 TRACE("In GetSpoolJob param 0x%x noption %d\n",param, nOption);
508 return retval;
512 /******************************************************************
513 * DrvGetPrinterDataInternal
515 * Helper for DrvGetPrinterData
517 static DWORD DrvGetPrinterDataInternal(LPCSTR RegStr_Printer,
518 LPBYTE lpPrinterData, int cbData, int what)
520 DWORD res = -1;
521 HKEY hkey;
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))) {
527 if (!lpPrinterData)
528 res = cbQueryData;
529 else if ((cbQueryData) && (cbQueryData <= cbData)) {
530 cbQueryData = cbData;
531 if (RegQueryValueExA(hkey, DefaultDevMode, 0,
532 &dwType, lpPrinterData, &cbQueryData))
533 res = cbQueryData;
536 } else { /* "Printer Driver" */
537 cbQueryData = 32;
538 RegQueryValueExA(hkey, "Printer Driver", 0,
539 &dwType, lpPrinterData, &cbQueryData);
540 res = cbQueryData;
543 if (hkey) RegCloseKey(hkey);
544 return res;
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;
557 DWORD res = 0;
558 DWORD dwType, PrinterAttr, cbPrinterAttr, SetData, size;
560 if (HIWORD(lpPrinter))
561 TRACE("printer %s\n",lpPrinter);
562 else
563 TRACE("printer %p\n",lpPrinter);
564 if (HIWORD(lpProfile))
565 TRACE("profile %s\n",lpProfile);
566 else
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);
582 if (size+1) {
583 *lpNeeded = size;
584 if ((lpPrinterData) && (*lpNeeded > cbData))
585 res = ERROR_MORE_DATA;
587 else res = ERROR_INVALID_PRINTER_NAME;
589 else
590 if ((PtrToUlong(lpProfile) == INT_PD_DEFAULT_MODEL) || (HIWORD(lpProfile) &&
591 (!strcmp(lpProfile, PrinterModel)))) {
592 *lpNeeded = 32;
593 if (!lpPrinterData) goto failed;
594 if (cbData < 32) {
595 res = ERROR_MORE_DATA;
596 goto failed;
598 size = DrvGetPrinterDataInternal(RegStr_Printer, lpPrinterData, cbData,
599 INT_PD_DEFAULT_MODEL);
600 if ((size+1) && (lpType))
601 *lpType = REG_SZ;
602 else
603 res = ERROR_INVALID_PRINTER_NAME;
605 else
607 if ((res = RegOpenKeyA(HKEY_LOCAL_MACHINE, RegStr_Printer, &hkey)))
608 goto failed;
609 cbPrinterAttr = 4;
610 if ((res = RegQueryValueExA(hkey, "Attributes", 0,
611 &dwType, (LPBYTE)&PrinterAttr, &cbPrinterAttr)))
612 goto failed;
613 if ((res = RegOpenKeyA(hkey, PrinterDriverData, &hkey2)))
614 goto failed;
615 *lpNeeded = cbData;
616 res = RegQueryValueExA(hkey2, lpProfile, 0,
617 lpType, lpPrinterData, lpNeeded);
618 if ((res != ERROR_CANTREAD) &&
619 ((PrinterAttr &
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;
626 else
628 SetData = -1;
629 RegSetValueExA(hkey2, lpProfile, 0, REG_DWORD, (LPBYTE)&SetData, 4); /* no result returned */
633 failed:
634 if (hkey2) RegCloseKey(hkey2);
635 if (hkey) RegCloseKey(hkey);
636 HeapFree(GetProcessHeap(), 0, RegStr_Printer);
637 return res;
641 /******************************************************************
642 * DrvSetPrinterData (GDI.281)
645 DWORD WINAPI DrvSetPrinterData16(LPSTR lpPrinter, LPSTR lpProfile,
646 DWORD lpType, LPBYTE lpPrinterData,
647 DWORD dwSize)
649 LPSTR RegStr_Printer;
650 HKEY hkey = 0;
651 DWORD res = 0;
653 if (HIWORD(lpPrinter))
654 TRACE("printer %s\n",lpPrinter);
655 else
656 TRACE("printer %p\n",lpPrinter);
657 if (HIWORD(lpProfile))
658 TRACE("profile %s\n",lpProfile);
659 else
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)
676 != ERROR_SUCCESS ||
677 RegSetValueExA(hkey, DefaultDevMode, 0, REG_BINARY,
678 lpPrinterData, dwSize) != ERROR_SUCCESS )
679 res = ERROR_INVALID_PRINTER_NAME;
681 else
683 strcat(RegStr_Printer, "\\");
685 if( (res = RegOpenKeyA(HKEY_LOCAL_MACHINE, RegStr_Printer, &hkey)) ==
686 ERROR_SUCCESS ) {
688 if (!lpPrinterData)
689 res = RegDeleteValueA(hkey, lpProfile);
690 else
691 res = RegSetValueExA(hkey, lpProfile, 0, lpType,
692 lpPrinterData, dwSize);
696 if (hkey) RegCloseKey(hkey);
697 HeapFree(GetProcessHeap(), 0, RegStr_Printer);
698 return res;