printdrv.c 23.5 KB
Newer Older
1
/*
Alexandre Julliard's avatar
Alexandre Julliard committed
2
 * Implementation of some printer driver bits
3
 *
Alexandre Julliard's avatar
Alexandre Julliard committed
4
 * Copyright 1996 John Harvey
5
 * Copyright 1998 Huw Davies
Alexandre Julliard's avatar
Alexandre Julliard committed
6
 * Copyright 1998 Andreas Mohr
7
 * Copyright 1999 Klaas van Gend
8 9 10 11 12 13 14 15 16 17 18 19 20
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * 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
Alexandre Julliard's avatar
Alexandre Julliard committed
22 23
 */

24
#include "config.h"
25
#include "wine/port.h"
26

27
#include <stdarg.h>
28
#include <stdio.h>
29
#include <signal.h>
Alexandre Julliard's avatar
Alexandre Julliard committed
30 31 32
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
33
#include <errno.h>
34 35 36
#ifdef HAVE_IO_H
# include <io.h>
#endif
37 38 39
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#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"
Alexandre Julliard's avatar
Alexandre Julliard committed
47
#include "winerror.h"
Alexandre Julliard's avatar
Alexandre Julliard committed
48
#include "winreg.h"
49
#include "wownt32.h"
50
#include "wine/debug.h"
51
#include "gdi_private.h"
Alexandre Julliard's avatar
Alexandre Julliard committed
52

53
WINE_DEFAULT_DEBUG_CHANNEL(print);
54

55 56 57 58
static const char PrinterModel[]      = "Printer Model";
static const char DefaultDevMode[]    = "Default DevMode";
static const char PrinterDriverData[] = "PrinterDriverData";
static const char Printers[]          = "System\\CurrentControlSet\\Control\\Print\\Printers\\";
Alexandre Julliard's avatar
Alexandre Julliard committed
59

Alexandre Julliard's avatar
Alexandre Julliard committed
60

61
/******************************************************************
62
 *                  StartDocA  [GDI32.@]
63
 *
64 65 66
 * StartDoc calls the STARTDOC Escape with the input data pointing to DocName
 * and the output data (which is used as a second input parameter).pointing at
 * the whole docinfo structure.  This seems to be an undocumented feature of
67
 * the STARTDOC Escape.
68 69
 *
 * Note: we now do it the other way, with the STARTDOC Escape calling StartDoc.
70
 */
71
INT WINAPI StartDocW(HDC hdc, const DOCINFOW* doc)
72
{
73
    INT ret = 0;
74 75
    DC *dc = DC_GetDCPtr( hdc );

76 77 78
    TRACE("DocName = %s Output = %s Datatype = %s\n",
          debugstr_w(doc->lpszDocName), debugstr_w(doc->lpszOutput),
          debugstr_w(doc->lpszDatatype));
79

80
    if(!dc) return SP_ERROR;
81

82
    if (dc->funcs->pStartDoc) ret = dc->funcs->pStartDoc( dc->physDev, doc );
83 84
    GDI_ReleaseObj( hdc );
    return ret;
85 86 87
}

/*************************************************************************
88
 *                  StartDocA [GDI32.@]
89
 *
90
 */
91
INT WINAPI StartDocA(HDC hdc, const DOCINFOA* doc)
92
{
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
    LPWSTR szDocName = NULL, szOutput = NULL, szDatatype = NULL;
    DOCINFOW docW;
    INT ret, len;

    docW.cbSize = doc->cbSize;
    if (doc->lpszDocName)
    {
        len = MultiByteToWideChar(CP_ACP,0,doc->lpszDocName,-1,NULL,0);
        szDocName = HeapAlloc(GetProcessHeap(),0,len*sizeof(WCHAR));
        MultiByteToWideChar(CP_ACP,0,doc->lpszDocName,-1,szDocName,len);
    }
    if (doc->lpszOutput)
    {
        len = MultiByteToWideChar(CP_ACP,0,doc->lpszOutput,-1,NULL,0);
        szOutput = HeapAlloc(GetProcessHeap(),0,len*sizeof(WCHAR));
        MultiByteToWideChar(CP_ACP,0,doc->lpszOutput,-1,szOutput,len);
    }
    if (doc->lpszDatatype)
    {
        len = MultiByteToWideChar(CP_ACP,0,doc->lpszDatatype,-1,NULL,0);
        szDatatype = HeapAlloc(GetProcessHeap(),0,len*sizeof(WCHAR));
        MultiByteToWideChar(CP_ACP,0,doc->lpszDatatype,-1,szDatatype,len);
    }

    docW.lpszDocName = szDocName;
    docW.lpszOutput = szOutput;
    docW.lpszDatatype = szDatatype;
    docW.fwType = doc->fwType;

    ret = StartDocW(hdc, &docW);

    HeapFree( GetProcessHeap(), 0, szDocName );
    HeapFree( GetProcessHeap(), 0, szOutput );
    HeapFree( GetProcessHeap(), 0, szDatatype );
127 128

    return ret;
129 130
}

131

132
/******************************************************************
133
 *                  EndDoc  [GDI32.@]
134
 *
135
 */
136 137
INT WINAPI EndDoc(HDC hdc)
{
138
    INT ret = 0;
139
    DC *dc = DC_GetDCPtr( hdc );
140
    if(!dc) return SP_ERROR;
141

142
    if (dc->funcs->pEndDoc) ret = dc->funcs->pEndDoc( dc->physDev );
143 144
    GDI_ReleaseObj( hdc );
    return ret;
145 146
}

147 148

/******************************************************************
149
 *                  StartPage  [GDI32.@]
150 151
 *
 */
152
INT WINAPI StartPage(HDC hdc)
153
{
154
    INT ret = 1;
155
    DC *dc = DC_GetDCPtr( hdc );
156
    if(!dc) return SP_ERROR;
157 158

    if(dc->funcs->pStartPage)
159
        ret = dc->funcs->pStartPage( dc->physDev );
160 161 162 163
    else
        FIXME("stub\n");
    GDI_ReleaseObj( hdc );
    return ret;
164 165
}

Alexandre Julliard's avatar
Alexandre Julliard committed
166

167
/******************************************************************
168
 *                  EndPage  [GDI32.@]
169 170
 *
 */
171
INT WINAPI EndPage(HDC hdc)
172
{
173
    ABORTPROC abort_proc;
174
    INT ret = 0;
175
    DC *dc = DC_GetDCPtr( hdc );
176
    if(!dc) return SP_ERROR;
177

178
    if (dc->funcs->pEndPage) ret = dc->funcs->pEndPage( dc->physDev );
179
    abort_proc = dc->pAbortProc;
180
    GDI_ReleaseObj( hdc );
181
    if (abort_proc && !abort_proc( hdc, 0 ))
182 183 184 185
    {
        EndDoc( hdc );
        ret = 0;
    }
186
    return ret;
187 188
}

189 190

/******************************************************************************
191
 *                 AbortDoc  [GDI32.@]
192
 */
193
INT WINAPI AbortDoc(HDC hdc)
194
{
195
    INT ret = 0;
196
    DC *dc = DC_GetDCPtr( hdc );
197
    if(!dc) return SP_ERROR;
198

199
    if (dc->funcs->pAbortDoc) ret = dc->funcs->pAbortDoc( dc->physDev );
200 201
    GDI_ReleaseObj( hdc );
    return ret;
202 203 204
}

/**********************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
205
 *           QueryAbort   (GDI.155)
206 207 208 209 210 211 212
 *
 *  Calls the app's AbortProc function if avail.
 *
 * RETURNS
 * TRUE if no AbortProc avail or AbortProc wants to continue printing.
 * FALSE if AbortProc wants to abort printing.
 */
213
BOOL16 WINAPI QueryAbort16(HDC16 hdc16, INT16 reserved)
214
{
215
    BOOL ret = TRUE;
216
    HDC hdc = HDC_32( hdc16 );
217
    DC *dc = DC_GetDCPtr( hdc );
218
    ABORTPROC abproc;
219 220

    if(!dc) {
221
        ERR("Invalid hdc %p\n", hdc);
222 223 224
	return FALSE;
    }

225
    abproc = dc->pAbortProc;
226
    GDI_ReleaseObj( hdc );
227 228 229

    if (abproc)
	ret = abproc(hdc, 0);
230
    return ret;
231 232
}

233

234 235 236 237 238 239 240 241 242 243 244
/**********************************************************************
 *           call_abort_proc16
 */
static BOOL CALLBACK call_abort_proc16( HDC hdc, INT code )
{
    ABORTPROC16 proc16;
    DC *dc = DC_GetDCPtr( hdc );

    if (!dc) return FALSE;
    proc16 = dc->pAbortProc16;
    GDI_ReleaseObj( hdc );
245 246 247 248 249 250 251 252 253 254
    if (proc16)
    {
        WORD args[2];
        DWORD ret;

        args[1] = HDC_16(hdc);
        args[0] = code;
        WOWCallback16Ex( (DWORD)proc16, WCB16_PASCAL, sizeof(args), args, &ret );
        return LOWORD(ret);
    }
255 256 257 258
    return TRUE;
}


259
/**********************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
260
 *           SetAbortProc   (GDI.381)
261
 */
262
INT16 WINAPI SetAbortProc16(HDC16 hdc16, ABORTPROC16 abrtprc)
263
{
264
    HDC hdc = HDC_32( hdc16 );
265 266 267 268 269 270
    DC *dc = DC_GetDCPtr( hdc );

    if (!dc) return FALSE;
    dc->pAbortProc16 = abrtprc;
    GDI_ReleaseObj( hdc );
    return SetAbortProc( hdc, call_abort_proc16 );
271
}
272 273

/**********************************************************************
274
 *           SetAbortProc   (GDI32.@)
275 276 277 278
 *
 */
INT WINAPI SetAbortProc(HDC hdc, ABORTPROC abrtprc)
{
279 280
    DC *dc = DC_GetDCPtr( hdc );

281
    if (!dc) return FALSE;
282
    dc->pAbortProc = abrtprc;
283
    GDI_ReleaseObj( hdc );
284
    return TRUE;
285 286 287 288 289 290 291 292
}


/****************** misc. printer related functions */

/*
 * The following function should implement a queing system
 */
293
struct hpq
294 295 296 297 298 299 300 301 302 303 304 305
{
    struct hpq 	*next;
    int		 tag;
    int		 key;
};

static struct hpq *hpqueue;

/**********************************************************************
 *           CreatePQ   (GDI.230)
 *
 */
306
HPQ16 WINAPI CreatePQ16(INT16 size)
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
{
#if 0
    HGLOBAL16 hpq = 0;
    WORD tmp_size;
    LPWORD pPQ;

    tmp_size = size << 2;
    if (!(hpq = GlobalAlloc16(GMEM_SHARE|GMEM_MOVEABLE, tmp_size + 8)))
       return 0xffff;
    pPQ = GlobalLock16(hpq);
    *pPQ++ = 0;
    *pPQ++ = tmp_size;
    *pPQ++ = 0;
    *pPQ++ = 0;
    GlobalUnlock16(hpq);

323
    return (HPQ16)hpq;
324 325 326 327 328 329 330 331 332 333
#else
    FIXME("(%d): stub\n",size);
    return 1;
#endif
}

/**********************************************************************
 *           DeletePQ   (GDI.235)
 *
 */
334
INT16 WINAPI DeletePQ16(HPQ16 hPQ)
335 336 337 338 339 340 341 342
{
    return GlobalFree16((HGLOBAL16)hPQ);
}

/**********************************************************************
 *           ExtractPQ   (GDI.232)
 *
 */
343 344
INT16 WINAPI ExtractPQ16(HPQ16 hPQ)
{
345 346 347 348 349 350
    struct hpq *queue, *prev, *current, *currentPrev;
    int key = 0, tag = -1;
    currentPrev = prev = NULL;
    queue = current = hpqueue;
    if (current)
        key = current->key;
351

352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367
    while (current)
    {
        currentPrev = current;
        current = current->next;
        if (current)
        {
            if (current->key < key)
            {
                queue = current;
                prev = currentPrev;
            }
        }
    }
    if (queue)
    {
        tag = queue->tag;
368

369 370 371 372
        if (prev)
            prev->next = queue->next;
        else
            hpqueue = queue->next;
373
        HeapFree(GetProcessHeap(), 0, queue);
374
    }
375 376

    TRACE("%x got tag %d key %d\n", hPQ, tag, key);
377 378 379 380 381 382 383 384

    return tag;
}

/**********************************************************************
 *           InsertPQ   (GDI.233)
 *
 */
385
INT16 WINAPI InsertPQ16(HPQ16 hPQ, INT16 tag, INT16 key)
386
{
387 388
    struct hpq *queueItem = HeapAlloc(GetProcessHeap(), 0, sizeof(struct hpq));
    if(queueItem == NULL) {
389
        ERR("Memory exausted!\n");
390 391
        return FALSE;
    }
392 393 394 395
    queueItem->next = hpqueue;
    hpqueue = queueItem;
    queueItem->key = key;
    queueItem->tag = tag;
396

397 398 399 400 401 402 403 404
    FIXME("(%x %d %d): stub???\n", hPQ, tag, key);
    return TRUE;
}

/**********************************************************************
 *           MinPQ   (GDI.231)
 *
 */
405
INT16 WINAPI MinPQ16(HPQ16 hPQ)
406
{
407
    FIXME("(%x): stub\n", hPQ);
408 409 410 411 412 413 414
    return 0;
}

/**********************************************************************
 *           SizePQ   (GDI.234)
 *
 */
415 416 417 418
INT16 WINAPI SizePQ16(HPQ16 hPQ, INT16 sizechange)
{
    FIXME("(%x %d): stub\n", hPQ, sizechange);
    return -1;
419 420 421 422
}



423 424
/*
 * The following functions implement part of the spooling process to
425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449
 * print manager.  I would like to see wine have a version of print managers
 * that used LPR/LPD.  For simplicity print jobs will be sent to a file for
 * now.
 */
typedef struct PRINTJOB
{
    char	*pszOutput;
    char 	*pszTitle;
    HDC16  	hDC;
    HANDLE16 	hHandle;
    int		nIndex;
    int		fd;
} PRINTJOB, *PPRINTJOB;

#define MAX_PRINT_JOBS 1
#define SP_OK 1

PPRINTJOB gPrintJobsTable[MAX_PRINT_JOBS];


static PPRINTJOB FindPrintJobFromHandle(HANDLE16 hHandle)
{
    return gPrintJobsTable[0];
}

450
static int CreateSpoolFile(LPCSTR pszOutput)
451 452 453
{
    int fd=-1;
    char psCmd[1024];
454
    const char *psCmdP = psCmd;
455
    HKEY hkey;
456 457 458 459 460 461

    /* TTD convert the 'output device' into a spool file name */

    if (pszOutput == NULL || *pszOutput == '\0')
      return -1;

462
    psCmd[0] = 0;
463 464
    /* @@ Wine registry key: HKCU\Software\Wine\Printing\Spooler */
    if(!RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Printing\\Spooler", &hkey))
465
    {
466
        DWORD type, count = sizeof(psCmd);
467
        RegQueryValueExA(hkey, pszOutput, 0, &type, (LPBYTE)psCmd, &count);
468
        RegCloseKey(hkey);
469
    }
470 471 472
    if (!psCmd[0] && !strncmp("LPR:",pszOutput,4))
        sprintf(psCmd,"|lpr -P%s",pszOutput+4);

473 474 475
    TRACE("Got printerSpoolCommand '%s' for output device '%s'\n",
	  psCmd, pszOutput);
    if (!*psCmd)
476
        psCmdP = pszOutput;
477 478 479 480 481
    else
    {
        while (*psCmdP && isspace(*psCmdP))
        {
            psCmdP++;
482
        }
483 484 485
        if (!*psCmdP)
            return -1;
    }
486
    TRACE("command: '%s'\n", psCmdP);
487
#ifdef HAVE_FORK
488 489 490
    if (*psCmdP == '|')
    {
        int fds[2];
491 492
        if (pipe(fds)) {
	    ERR("pipe() failed!\n"); 
493
            return -1;
494
	}
495 496 497 498 499 500 501 502
        if (fork() == 0)
        {
            psCmdP++;

            TRACE("In child need to exec %s\n",psCmdP);
            close(0);
            dup2(fds[0],0);
            close (fds[1]);
503 504 505 506 507

            /* reset signals that we previously set to SIG_IGN */
            signal( SIGPIPE, SIG_DFL );
            signal( SIGCHLD, SIG_DFL );

508 509
            system(psCmdP);
            exit(0);
510

511 512 513 514 515 516
        }
        close (fds[0]);
        fd = fds[1];
        TRACE("Need to execute a cmnd and pipe the output to it\n");
    }
    else
517
#endif
518
    {
519
        char *buffer;
520
        WCHAR psCmdPW[MAX_PATH];
521

522
        TRACE("Just assume it's a file\n");
523

524 525 526 527
        /**
         * The file name can be dos based, we have to find its
         * Unix correspondant file name
         */
528
        MultiByteToWideChar(CP_ACP, 0, psCmdP, -1, psCmdPW, MAX_PATH);
529
        if ((buffer = wine_get_unix_file_name(psCmdPW)))
530
        {
531 532 533 534 535 536
            if ((fd = open(buffer, O_CREAT | O_TRUNC | O_WRONLY , 0600)) < 0)
            {
                ERR("Failed to create spool file '%s' ('%s'). (error %s)\n",
                    buffer, psCmdP, strerror(errno));
            }
            HeapFree(GetProcessHeap(), 0, buffer);
537 538 539 540 541 542 543 544 545 546 547 548 549 550
        }
    }
    return fd;
}

static int FreePrintJob(HANDLE16 hJob)
{
    int nRet = SP_ERROR;
    PPRINTJOB pPrintJob;

    pPrintJob = FindPrintJobFromHandle(hJob);
    if (pPrintJob != NULL)
    {
	gPrintJobsTable[pPrintJob->nIndex] = NULL;
551 552
	HeapFree(GetProcessHeap(), 0, pPrintJob->pszOutput);
	HeapFree(GetProcessHeap(), 0, pPrintJob->pszTitle);
553
	if (pPrintJob->fd >= 0) close(pPrintJob->fd);
554
	HeapFree(GetProcessHeap(), 0, pPrintJob);
555 556 557 558 559 560 561 562 563
	nRet = SP_OK;
    }
    return nRet;
}

/**********************************************************************
 *           OpenJob   (GDI.240)
 *
 */
564
HPJOB16 WINAPI OpenJob16(LPCSTR lpOutput, LPCSTR lpTitle, HDC16 hDC)
565
{
566
    HPJOB16 hHandle = (HPJOB16)SP_ERROR;
567 568 569 570 571 572 573 574 575
    PPRINTJOB pPrintJob;

    TRACE("'%s' '%s' %04x\n", lpOutput, lpTitle, hDC);

    pPrintJob = gPrintJobsTable[0];
    if (pPrintJob == NULL)
    {
	int fd;

576
	/* Try and create a spool file */
577 578 579
	fd = CreateSpoolFile(lpOutput);
	if (fd >= 0)
	{
580 581
	    pPrintJob = HeapAlloc(GetProcessHeap(), 0, sizeof(PRINTJOB));
            if(pPrintJob == NULL) {
582
                WARN("Memory exausted!\n");
583 584
                return hHandle;
            }
585

586
            hHandle = 1;
587

588 589
	    pPrintJob->pszOutput = HeapAlloc(GetProcessHeap(), 0, strlen(lpOutput)+1);
	    strcpy( pPrintJob->pszOutput, lpOutput );
590
	    if(lpTitle)
591 592 593 594
            {
	        pPrintJob->pszTitle = HeapAlloc(GetProcessHeap(), 0, strlen(lpTitle)+1);
	        strcpy( pPrintJob->pszTitle, lpTitle );
            }
595 596 597
	    pPrintJob->hDC = hDC;
	    pPrintJob->fd = fd;
	    pPrintJob->nIndex = 0;
598 599
	    pPrintJob->hHandle = hHandle;
	    gPrintJobsTable[pPrintJob->nIndex] = pPrintJob;
600 601 602 603 604 605 606 607 608 609
	}
    }
    TRACE("return %04x\n", hHandle);
    return hHandle;
}

/**********************************************************************
 *           CloseJob   (GDI.243)
 *
 */
610
INT16 WINAPI CloseJob16(HPJOB16 hJob)
611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631
{
    int nRet = SP_ERROR;
    PPRINTJOB pPrintJob = NULL;

    TRACE("%04x\n", hJob);

    pPrintJob = FindPrintJobFromHandle(hJob);
    if (pPrintJob != NULL)
    {
	/* Close the spool file */
	close(pPrintJob->fd);
	FreePrintJob(hJob);
	nRet  = 1;
    }
    return nRet;
}

/**********************************************************************
 *           WriteSpool   (GDI.241)
 *
 */
632
INT16 WINAPI WriteSpool16(HPJOB16 hJob, LPSTR lpData, INT16 cch)
633 634 635 636
{
    int nRet = SP_ERROR;
    PPRINTJOB pPrintJob = NULL;

637
    TRACE("%04x %p %04x\n", hJob, lpData, cch);
638 639 640 641 642 643 644 645

    pPrintJob = FindPrintJobFromHandle(hJob);
    if (pPrintJob != NULL && pPrintJob->fd >= 0 && cch)
    {
	if (write(pPrintJob->fd, lpData, cch) != cch)
	  nRet = SP_OUTOFDISK;
	else
	  nRet = cch;
646 647 648 649
#if 0
	/* FIXME: We just cannot call 16 bit functions from here, since we
	 * have acquired several locks (DC). And we do not really need to.
	 */
650 651 652 653 654 655 656 657
	if (pPrintJob->hDC == 0) {
	    TRACE("hDC == 0 so no QueryAbort\n");
	}
        else if (!(QueryAbort16(pPrintJob->hDC, (nRet == SP_OUTOFDISK) ? nRet : 0 )))
	{
	    CloseJob16(hJob); /* printing aborted */
	    nRet = SP_APPABORT;
	}
658
#endif
659 660 661 662
    }
    return nRet;
}

663
typedef INT (WINAPI *MSGBOX_PROC)( HWND, LPCSTR, LPCSTR, UINT );
664

665 666 667 668
/**********************************************************************
 *           WriteDialog   (GDI.242)
 *
 */
669
INT16 WINAPI WriteDialog16(HPJOB16 hJob, LPSTR lpMsg, INT16 cchMsg)
670
{
671 672 673 674
    HMODULE mod;
    MSGBOX_PROC pMessageBoxA;
    INT16 ret = 0;

675 676
    TRACE("%04x %04x '%s'\n", hJob,  cchMsg, lpMsg);

677 678 679 680 681 682
    if ((mod = GetModuleHandleA("user32.dll")))
    {
        if ((pMessageBoxA = (MSGBOX_PROC)GetProcAddress( mod, "MessageBoxA" )))
            ret = pMessageBoxA(0, lpMsg, "Printing Error", MB_OKCANCEL);
    }
    return ret;
683 684 685 686 687 688 689
}


/**********************************************************************
 *           DeleteJob  (GDI.244)
 *
 */
690
INT16 WINAPI DeleteJob16(HPJOB16 hJob, INT16 nNotUsed)
691 692 693 694 695 696 697 698 699
{
    int nRet;

    TRACE("%04x\n", hJob);

    nRet = FreePrintJob(hJob);
    return nRet;
}

700
/*
701 702 703 704 705 706 707 708 709
 * The following two function would allow a page to be sent to the printer
 * when it has been processed.  For simplicity they havn't been implemented.
 * This means a whole job has to be processed before it is sent to the printer.
 */

/**********************************************************************
 *           StartSpoolPage   (GDI.246)
 *
 */
710
INT16 WINAPI StartSpoolPage16(HPJOB16 hJob)
711 712
{
    FIXME("StartSpoolPage GDI.246 unimplemented\n");
713
    return 1;
714

715 716
}

717 718 719 720 721

/**********************************************************************
 *           EndSpoolPage   (GDI.247)
 *
 */
722
INT16 WINAPI EndSpoolPage16(HPJOB16 hJob)
723 724 725 726 727 728 729 730 731 732 733 734 735
{
    FIXME("EndSpoolPage GDI.247 unimplemented\n");
    return 1;
}


/**********************************************************************
 *           GetSpoolJob   (GDI.245)
 *
 */
DWORD WINAPI GetSpoolJob16(int nOption, LONG param)
{
    DWORD retval = 0;
736
    TRACE("In GetSpoolJob param 0x%x noption %d\n",param, nOption);
737 738 739 740
    return retval;
}


741 742 743 744 745 746
/******************************************************************
 *                  DrvGetPrinterDataInternal
 *
 * Helper for DrvGetPrinterData
 */
static DWORD DrvGetPrinterDataInternal(LPSTR RegStr_Printer,
747
LPBYTE lpPrinterData, int cbData, int what)
Alexandre Julliard's avatar
Alexandre Julliard committed
748
{
Alexandre Julliard's avatar
Alexandre Julliard committed
749
    DWORD res = -1;
Alexandre Julliard's avatar
Alexandre Julliard committed
750 751 752
    HKEY hkey;
    DWORD dwType, cbQueryData;

753
    if (!(RegOpenKeyA(HKEY_LOCAL_MACHINE, RegStr_Printer, &hkey))) {
754
        if (what == INT_PD_DEFAULT_DEVMODE) { /* "Default DevMode" */
755
            if (!(RegQueryValueExA(hkey, DefaultDevMode, 0, &dwType, 0, &cbQueryData))) {
Alexandre Julliard's avatar
Alexandre Julliard committed
756 757 758
                if (!lpPrinterData)
		    res = cbQueryData;
		else if ((cbQueryData) && (cbQueryData <= cbData)) {
Alexandre Julliard's avatar
Alexandre Julliard committed
759
		    cbQueryData = cbData;
760
		    if (RegQueryValueExA(hkey, DefaultDevMode, 0,
Alexandre Julliard's avatar
Alexandre Julliard committed
761 762 763 764
				&dwType, lpPrinterData, &cbQueryData))
		        res = cbQueryData;
		}
	    }
Alexandre Julliard's avatar
Alexandre Julliard committed
765 766
	} else { /* "Printer Driver" */
	    cbQueryData = 32;
767
	    RegQueryValueExA(hkey, "Printer Driver", 0,
Alexandre Julliard's avatar
Alexandre Julliard committed
768 769
			&dwType, lpPrinterData, &cbQueryData);
	    res = cbQueryData;
Alexandre Julliard's avatar
Alexandre Julliard committed
770 771 772 773 774
	}
    }
    if (hkey) RegCloseKey(hkey);
    return res;
}
Alexandre Julliard's avatar
Alexandre Julliard committed
775

776
/******************************************************************
777
 *                DrvGetPrinterData     (GDI.282)
778 779
 *
 */
780
DWORD WINAPI DrvGetPrinterData16(LPSTR lpPrinter, LPSTR lpProfile,
Alexandre Julliard's avatar
Alexandre Julliard committed
781 782
                               LPDWORD lpType, LPBYTE lpPrinterData,
                               int cbData, LPDWORD lpNeeded)
Alexandre Julliard's avatar
Alexandre Julliard committed
783
{
Alexandre Julliard's avatar
Alexandre Julliard committed
784 785 786 787 788
    LPSTR RegStr_Printer;
    HKEY hkey = 0, hkey2 = 0;
    DWORD res = 0;
    DWORD dwType, PrinterAttr, cbPrinterAttr, SetData, size;

Alexandre Julliard's avatar
Alexandre Julliard committed
789
    if (HIWORD(lpPrinter))
790
            TRACE("printer %s\n",lpPrinter);
Alexandre Julliard's avatar
Alexandre Julliard committed
791
    else
792
            TRACE("printer %p\n",lpPrinter);
Alexandre Julliard's avatar
Alexandre Julliard committed
793
    if (HIWORD(lpProfile))
794
            TRACE("profile %s\n",lpProfile);
Alexandre Julliard's avatar
Alexandre Julliard committed
795
    else
796 797
            TRACE("profile %p\n",lpProfile);
    TRACE("lpType %p\n",lpType);
Alexandre Julliard's avatar
Alexandre Julliard committed
798 799 800 801 802 803 804 805 806

    if ((!lpPrinter) || (!lpProfile) || (!lpNeeded))
	return ERROR_INVALID_PARAMETER;

    RegStr_Printer = HeapAlloc(GetProcessHeap(), 0,
                               strlen(Printers) + strlen(lpPrinter) + 2);
    strcpy(RegStr_Printer, Printers);
    strcat(RegStr_Printer, lpPrinter);

Alexandre Julliard's avatar
Alexandre Julliard committed
807 808
    if (((DWORD)lpProfile == INT_PD_DEFAULT_DEVMODE) || (HIWORD(lpProfile) &&
    (!strcmp(lpProfile, DefaultDevMode)))) {
809 810
	size = DrvGetPrinterDataInternal(RegStr_Printer, lpPrinterData, cbData,
					 INT_PD_DEFAULT_DEVMODE);
Alexandre Julliard's avatar
Alexandre Julliard committed
811 812 813 814 815 816 817 818
	if (size+1) {
	    *lpNeeded = size;
	    if ((lpPrinterData) && (*lpNeeded > cbData))
		res = ERROR_MORE_DATA;
	}
	else res = ERROR_INVALID_PRINTER_NAME;
    }
    else
Alexandre Julliard's avatar
Alexandre Julliard committed
819 820
    if (((DWORD)lpProfile == INT_PD_DEFAULT_MODEL) || (HIWORD(lpProfile) &&
    (!strcmp(lpProfile, PrinterModel)))) {
Alexandre Julliard's avatar
Alexandre Julliard committed
821 822 823 824 825 826
	*lpNeeded = 32;
	if (!lpPrinterData) goto failed;
	if (cbData < 32) {
	    res = ERROR_MORE_DATA;
	    goto failed;
	}
827 828
	size = DrvGetPrinterDataInternal(RegStr_Printer, lpPrinterData, cbData,
					 INT_PD_DEFAULT_MODEL);
Alexandre Julliard's avatar
Alexandre Julliard committed
829 830 831 832 833 834 835
	if ((size+1) && (lpType))
	    *lpType = REG_SZ;
	else
	    res = ERROR_INVALID_PRINTER_NAME;
    }
    else
    {
836
	if ((res = RegOpenKeyA(HKEY_LOCAL_MACHINE, RegStr_Printer, &hkey)))
Alexandre Julliard's avatar
Alexandre Julliard committed
837 838
	    goto failed;
        cbPrinterAttr = 4;
839
        if ((res = RegQueryValueExA(hkey, "Attributes", 0,
Alexandre Julliard's avatar
Alexandre Julliard committed
840
                        &dwType, (LPBYTE)&PrinterAttr, &cbPrinterAttr)))
Alexandre Julliard's avatar
Alexandre Julliard committed
841
	    goto failed;
842
	if ((res = RegOpenKeyA(hkey, PrinterDriverData, &hkey2)))
Alexandre Julliard's avatar
Alexandre Julliard committed
843 844
	    goto failed;
        *lpNeeded = cbData;
845
        res = RegQueryValueExA(hkey2, lpProfile, 0,
Alexandre Julliard's avatar
Alexandre Julliard committed
846 847 848 849 850
                lpType, lpPrinterData, lpNeeded);
        if ((res != ERROR_CANTREAD) &&
         ((PrinterAttr &
        (PRINTER_ATTRIBUTE_ENABLE_BIDI|PRINTER_ATTRIBUTE_NETWORK))
        == PRINTER_ATTRIBUTE_NETWORK))
Alexandre Julliard's avatar
Alexandre Julliard committed
851 852 853 854 855 856 857
        {
	    if (!(res) && (*lpType == REG_DWORD) && (*(LPDWORD)lpPrinterData == -1))
	        res = ERROR_INVALID_DATA;
	}
	else
        {
	    SetData = -1;
858
	    RegSetValueExA(hkey2, lpProfile, 0, REG_DWORD, (LPBYTE)&SetData, 4); /* no result returned */
Alexandre Julliard's avatar
Alexandre Julliard committed
859 860
	}
    }
861

Alexandre Julliard's avatar
Alexandre Julliard committed
862 863 864 865 866
failed:
    if (hkey2) RegCloseKey(hkey2);
    if (hkey) RegCloseKey(hkey);
    HeapFree(GetProcessHeap(), 0, RegStr_Printer);
    return res;
Alexandre Julliard's avatar
Alexandre Julliard committed
867 868 869
}


870
/******************************************************************
871
 *                 DrvSetPrinterData     (GDI.281)
872 873
 *
 */
874
DWORD WINAPI DrvSetPrinterData16(LPSTR lpPrinter, LPSTR lpProfile,
Alexandre Julliard's avatar
Alexandre Julliard committed
875
                               DWORD lpType, LPBYTE lpPrinterData,
Alexandre Julliard's avatar
Alexandre Julliard committed
876
                               DWORD dwSize)
Alexandre Julliard's avatar
Alexandre Julliard committed
877
{
Alexandre Julliard's avatar
Alexandre Julliard committed
878 879 880 881
    LPSTR RegStr_Printer;
    HKEY hkey = 0;
    DWORD res = 0;

Alexandre Julliard's avatar
Alexandre Julliard committed
882
    if (HIWORD(lpPrinter))
883
            TRACE("printer %s\n",lpPrinter);
Alexandre Julliard's avatar
Alexandre Julliard committed
884
    else
885
            TRACE("printer %p\n",lpPrinter);
Alexandre Julliard's avatar
Alexandre Julliard committed
886
    if (HIWORD(lpProfile))
887
            TRACE("profile %s\n",lpProfile);
Alexandre Julliard's avatar
Alexandre Julliard committed
888
    else
889
            TRACE("profile %p\n",lpProfile);
890
    TRACE("lpType %08x\n",lpType);
Alexandre Julliard's avatar
Alexandre Julliard committed
891 892

    if ((!lpPrinter) || (!lpProfile) ||
Alexandre Julliard's avatar
Alexandre Julliard committed
893 894
    ((DWORD)lpProfile == INT_PD_DEFAULT_MODEL) || (HIWORD(lpProfile) &&
    (!strcmp(lpProfile, PrinterModel))))
Alexandre Julliard's avatar
Alexandre Julliard committed
895 896 897 898 899 900 901
	return ERROR_INVALID_PARAMETER;

    RegStr_Printer = HeapAlloc(GetProcessHeap(), 0,
			strlen(Printers) + strlen(lpPrinter) + 2);
    strcpy(RegStr_Printer, Printers);
    strcat(RegStr_Printer, lpPrinter);

Alexandre Julliard's avatar
Alexandre Julliard committed
902 903
    if (((DWORD)lpProfile == INT_PD_DEFAULT_DEVMODE) || (HIWORD(lpProfile) &&
    (!strcmp(lpProfile, DefaultDevMode)))) {
904
	if ( RegOpenKeyA(HKEY_LOCAL_MACHINE, RegStr_Printer, &hkey)
Alexandre Julliard's avatar
Alexandre Julliard committed
905
	     != ERROR_SUCCESS ||
906
	     RegSetValueExA(hkey, DefaultDevMode, 0, REG_BINARY,
Alexandre Julliard's avatar
Alexandre Julliard committed
907
			      lpPrinterData, dwSize) != ERROR_SUCCESS )
Alexandre Julliard's avatar
Alexandre Julliard committed
908 909 910 911 912 913
	        res = ERROR_INVALID_PRINTER_NAME;
    }
    else
    {
	strcat(RegStr_Printer, "\\");

914
	if( (res = RegOpenKeyA(HKEY_LOCAL_MACHINE, RegStr_Printer, &hkey)) ==
Alexandre Julliard's avatar
Alexandre Julliard committed
915
	    ERROR_SUCCESS ) {
Alexandre Julliard's avatar
Alexandre Julliard committed
916

917
	    if (!lpPrinterData)
918
	        res = RegDeleteValueA(hkey, lpProfile);
Alexandre Julliard's avatar
Alexandre Julliard committed
919
	    else
920
                res = RegSetValueExA(hkey, lpProfile, 0, lpType,
Alexandre Julliard's avatar
Alexandre Julliard committed
921
				       lpPrinterData, dwSize);
Alexandre Julliard's avatar
Alexandre Julliard committed
922 923 924 925 926 927
	}
    }

    if (hkey) RegCloseKey(hkey);
    HeapFree(GetProcessHeap(), 0, RegStr_Printer);
    return res;
Alexandre Julliard's avatar
Alexandre Julliard committed
928
}