dplayx: Code to forward player creation
[wine/gsoc_dplay.git] / dlls / gdi32 / printdrv16.c
blobe7954eedab600881126af394260c21c934b03f1b
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"
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.
65 * RETURNS
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)
71 BOOL ret = TRUE;
72 HDC hdc = HDC_32( hdc16 );
73 DC *dc = get_dc_ptr( hdc );
75 if(!dc) {
76 ERR("Invalid hdc %p\n", hdc);
77 return FALSE;
79 if (dc->pAbortProc) ret = dc->pAbortProc(hdc, 0);
80 release_dc_ptr( dc );
81 return ret;
85 /**********************************************************************
86 * call_abort_proc16
88 static BOOL CALLBACK call_abort_proc16( HDC hdc, INT code )
90 ABORTPROC16 proc16;
91 DC *dc = get_dc_ptr( hdc );
93 if (!dc) return FALSE;
94 proc16 = dc->pAbortProc16;
95 release_dc_ptr( dc );
96 if (proc16)
98 WORD args[2];
99 DWORD ret;
101 args[1] = HDC_16(hdc);
102 args[0] = code;
103 WOWCallback16Ex( (DWORD)proc16, WCB16_PASCAL, sizeof(args), args, &ret );
104 return LOWORD(ret);
106 return TRUE;
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 );
122 return TRUE;
126 /****************** misc. printer related functions */
129 * The following function should implement a queing system
131 struct hpq
133 struct hpq *next;
134 int tag;
135 int key;
138 static struct hpq *hpqueue;
140 /**********************************************************************
141 * CreatePQ (GDI.230)
144 HPQ16 WINAPI CreatePQ16(INT16 size)
146 #if 0
147 HGLOBAL16 hpq = 0;
148 WORD tmp_size;
149 LPWORD pPQ;
151 tmp_size = size << 2;
152 if (!(hpq = GlobalAlloc16(GMEM_SHARE|GMEM_MOVEABLE, tmp_size + 8)))
153 return 0xffff;
154 pPQ = GlobalLock16(hpq);
155 *pPQ++ = 0;
156 *pPQ++ = tmp_size;
157 *pPQ++ = 0;
158 *pPQ++ = 0;
159 GlobalUnlock16(hpq);
161 return (HPQ16)hpq;
162 #else
163 FIXME("(%d): stub\n",size);
164 return 1;
165 #endif
168 /**********************************************************************
169 * DeletePQ (GDI.235)
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;
187 if (current)
188 key = current->key;
190 while (current)
192 currentPrev = current;
193 current = current->next;
194 if (current)
196 if (current->key < key)
198 queue = current;
199 prev = currentPrev;
203 if (queue)
205 tag = queue->tag;
207 if (prev)
208 prev->next = queue->next;
209 else
210 hpqueue = queue->next;
211 HeapFree(GetProcessHeap(), 0, queue);
214 TRACE("%x got tag %d key %d\n", hPQ, tag, key);
216 return tag;
219 /**********************************************************************
220 * InsertPQ (GDI.233)
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");
228 return FALSE;
230 queueItem->next = hpqueue;
231 hpqueue = queueItem;
232 queueItem->key = key;
233 queueItem->tag = tag;
235 FIXME("(%x %d %d): stub???\n", hPQ, tag, key);
236 return TRUE;
239 /**********************************************************************
240 * MinPQ (GDI.231)
243 INT16 WINAPI MinPQ16(HPQ16 hPQ)
245 FIXME("(%x): stub\n", hPQ);
246 return 0;
249 /**********************************************************************
250 * SizePQ (GDI.234)
253 INT16 WINAPI SizePQ16(HPQ16 hPQ, INT16 sizechange)
255 FIXME("(%x %d): stub\n", hPQ, sizechange);
256 return -1;
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
265 * now.
267 typedef struct PRINTJOB
269 char *pszOutput;
270 char *pszTitle;
271 HDC16 hDC;
272 HANDLE16 hHandle;
273 int nIndex;
274 int fd;
275 } PRINTJOB, *PPRINTJOB;
277 #define MAX_PRINT_JOBS 1
278 #define SP_OK 1
280 static PPRINTJOB gPrintJobsTable[MAX_PRINT_JOBS];
283 static PPRINTJOB FindPrintJobFromHandle(HANDLE16 hHandle)
285 return gPrintJobsTable[0];
288 static int CreateSpoolFile(LPCSTR pszOutput)
290 int fd=-1;
291 char psCmd[1024];
292 const char *psCmdP = psCmd;
293 HKEY hkey;
295 /* TTD convert the 'output device' into a spool file name */
297 if (pszOutput == NULL || *pszOutput == '\0')
298 return -1;
300 psCmd[0] = 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);
306 RegCloseKey(hkey);
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",
312 psCmd, pszOutput);
313 if (!*psCmd)
314 psCmdP = pszOutput;
315 else
317 while (*psCmdP && isspace(*psCmdP))
319 psCmdP++;
321 if (!*psCmdP)
322 return -1;
324 TRACE("command: '%s'\n", psCmdP);
325 #ifdef HAVE_FORK
326 if (*psCmdP == '|')
328 int fds[2];
329 if (pipe(fds)) {
330 ERR("pipe() failed!\n");
331 return -1;
333 if (fork() == 0)
335 psCmdP++;
337 TRACE("In child need to exec %s\n",psCmdP);
338 close(0);
339 dup2(fds[0],0);
340 close (fds[1]);
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);
347 _exit(1);
350 close (fds[0]);
351 fd = fds[1];
352 TRACE("Need to execute a cmnd and pipe the output to it\n");
354 else
355 #endif
357 char *buffer;
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);
377 return fd;
380 static int FreePrintJob(HANDLE16 hJob)
382 int nRet = SP_ERROR;
383 PPRINTJOB pPrintJob;
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);
393 nRet = SP_OK;
395 return nRet;
398 /**********************************************************************
399 * OpenJob (GDI.240)
402 HPJOB16 WINAPI OpenJob16(LPCSTR lpOutput, LPCSTR lpTitle, HDC16 hDC)
404 HPJOB16 hHandle = (HPJOB16)SP_ERROR;
405 PPRINTJOB pPrintJob;
407 TRACE("'%s' '%s' %04x\n", lpOutput, lpTitle, hDC);
409 pPrintJob = gPrintJobsTable[0];
410 if (pPrintJob == NULL)
412 int fd;
414 /* Try and create a spool file */
415 fd = CreateSpoolFile(lpOutput);
416 if (fd >= 0)
418 pPrintJob = HeapAlloc(GetProcessHeap(), 0, sizeof(PRINTJOB));
419 if(pPrintJob == NULL) {
420 WARN("Memory exausted!\n");
421 return hHandle;
424 hHandle = 1;
426 pPrintJob->pszOutput = HeapAlloc(GetProcessHeap(), 0, strlen(lpOutput)+1);
427 strcpy( pPrintJob->pszOutput, lpOutput );
428 if(lpTitle)
430 pPrintJob->pszTitle = HeapAlloc(GetProcessHeap(), 0, strlen(lpTitle)+1);
431 strcpy( pPrintJob->pszTitle, lpTitle );
433 pPrintJob->hDC = hDC;
434 pPrintJob->fd = fd;
435 pPrintJob->nIndex = 0;
436 pPrintJob->hHandle = hHandle;
437 gPrintJobsTable[pPrintJob->nIndex] = pPrintJob;
440 TRACE("return %04x\n", hHandle);
441 return hHandle;
444 /**********************************************************************
445 * CloseJob (GDI.243)
448 INT16 WINAPI CloseJob16(HPJOB16 hJob)
450 int nRet = SP_ERROR;
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);
460 FreePrintJob(hJob);
461 nRet = 1;
463 return nRet;
466 /**********************************************************************
467 * WriteSpool (GDI.241)
470 INT16 WINAPI WriteSpool16(HPJOB16 hJob, LPSTR lpData, INT16 cch)
472 int nRet = SP_ERROR;
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)
481 nRet = SP_OUTOFDISK;
482 else
483 nRet = cch;
484 #if 0
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 */
494 nRet = SP_APPABORT;
496 #endif
498 return nRet;
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)
509 HMODULE mod;
510 MSGBOX_PROC pMessageBoxA;
511 INT16 ret = 0;
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);
520 return ret;
524 /**********************************************************************
525 * DeleteJob (GDI.244)
528 INT16 WINAPI DeleteJob16(HPJOB16 hJob, INT16 nNotUsed)
530 int nRet;
532 TRACE("%04x\n", hJob);
534 nRet = FreePrintJob(hJob);
535 return nRet;
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");
551 return 1;
556 /**********************************************************************
557 * EndSpoolPage (GDI.247)
560 INT16 WINAPI EndSpoolPage16(HPJOB16 hJob)
562 FIXME("EndSpoolPage GDI.247 unimplemented\n");
563 return 1;
567 /**********************************************************************
568 * GetSpoolJob (GDI.245)
571 DWORD WINAPI GetSpoolJob16(int nOption, LONG param)
573 DWORD retval = 0;
574 TRACE("In GetSpoolJob param 0x%x noption %d\n",param, nOption);
575 return retval;
579 /******************************************************************
580 * DrvGetPrinterDataInternal
582 * Helper for DrvGetPrinterData
584 static DWORD DrvGetPrinterDataInternal(LPSTR RegStr_Printer,
585 LPBYTE lpPrinterData, int cbData, int what)
587 DWORD res = -1;
588 HKEY hkey;
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))) {
594 if (!lpPrinterData)
595 res = cbQueryData;
596 else if ((cbQueryData) && (cbQueryData <= cbData)) {
597 cbQueryData = cbData;
598 if (RegQueryValueExA(hkey, DefaultDevMode, 0,
599 &dwType, lpPrinterData, &cbQueryData))
600 res = cbQueryData;
603 } else { /* "Printer Driver" */
604 cbQueryData = 32;
605 RegQueryValueExA(hkey, "Printer Driver", 0,
606 &dwType, lpPrinterData, &cbQueryData);
607 res = cbQueryData;
610 if (hkey) RegCloseKey(hkey);
611 return res;
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;
624 DWORD res = 0;
625 DWORD dwType, PrinterAttr, cbPrinterAttr, SetData, size;
627 if (HIWORD(lpPrinter))
628 TRACE("printer %s\n",lpPrinter);
629 else
630 TRACE("printer %p\n",lpPrinter);
631 if (HIWORD(lpProfile))
632 TRACE("profile %s\n",lpProfile);
633 else
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);
649 if (size+1) {
650 *lpNeeded = size;
651 if ((lpPrinterData) && (*lpNeeded > cbData))
652 res = ERROR_MORE_DATA;
654 else res = ERROR_INVALID_PRINTER_NAME;
656 else
657 if ((PtrToUlong(lpProfile) == INT_PD_DEFAULT_MODEL) || (HIWORD(lpProfile) &&
658 (!strcmp(lpProfile, PrinterModel)))) {
659 *lpNeeded = 32;
660 if (!lpPrinterData) goto failed;
661 if (cbData < 32) {
662 res = ERROR_MORE_DATA;
663 goto failed;
665 size = DrvGetPrinterDataInternal(RegStr_Printer, lpPrinterData, cbData,
666 INT_PD_DEFAULT_MODEL);
667 if ((size+1) && (lpType))
668 *lpType = REG_SZ;
669 else
670 res = ERROR_INVALID_PRINTER_NAME;
672 else
674 if ((res = RegOpenKeyA(HKEY_LOCAL_MACHINE, RegStr_Printer, &hkey)))
675 goto failed;
676 cbPrinterAttr = 4;
677 if ((res = RegQueryValueExA(hkey, "Attributes", 0,
678 &dwType, (LPBYTE)&PrinterAttr, &cbPrinterAttr)))
679 goto failed;
680 if ((res = RegOpenKeyA(hkey, PrinterDriverData, &hkey2)))
681 goto failed;
682 *lpNeeded = cbData;
683 res = RegQueryValueExA(hkey2, lpProfile, 0,
684 lpType, lpPrinterData, lpNeeded);
685 if ((res != ERROR_CANTREAD) &&
686 ((PrinterAttr &
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;
693 else
695 SetData = -1;
696 RegSetValueExA(hkey2, lpProfile, 0, REG_DWORD, (LPBYTE)&SetData, 4); /* no result returned */
700 failed:
701 if (hkey2) RegCloseKey(hkey2);
702 if (hkey) RegCloseKey(hkey);
703 HeapFree(GetProcessHeap(), 0, RegStr_Printer);
704 return res;
708 /******************************************************************
709 * DrvSetPrinterData (GDI.281)
712 DWORD WINAPI DrvSetPrinterData16(LPSTR lpPrinter, LPSTR lpProfile,
713 DWORD lpType, LPBYTE lpPrinterData,
714 DWORD dwSize)
716 LPSTR RegStr_Printer;
717 HKEY hkey = 0;
718 DWORD res = 0;
720 if (HIWORD(lpPrinter))
721 TRACE("printer %s\n",lpPrinter);
722 else
723 TRACE("printer %p\n",lpPrinter);
724 if (HIWORD(lpProfile))
725 TRACE("profile %s\n",lpProfile);
726 else
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)
743 != ERROR_SUCCESS ||
744 RegSetValueExA(hkey, DefaultDevMode, 0, REG_BINARY,
745 lpPrinterData, dwSize) != ERROR_SUCCESS )
746 res = ERROR_INVALID_PRINTER_NAME;
748 else
750 strcat(RegStr_Printer, "\\");
752 if( (res = RegOpenKeyA(HKEY_LOCAL_MACHINE, RegStr_Printer, &hkey)) ==
753 ERROR_SUCCESS ) {
755 if (!lpPrinterData)
756 res = RegDeleteValueA(hkey, lpProfile);
757 else
758 res = RegSetValueExA(hkey, lpProfile, 0, lpType,
759 lpPrinterData, dwSize);
763 if (hkey) RegCloseKey(hkey);
764 HeapFree(GetProcessHeap(), 0, RegStr_Printer);
765 return res;