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 21
 *
 * 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
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  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.h"
52
#include "gdi_private.h"
Alexandre Julliard's avatar
Alexandre Julliard committed
53

54
WINE_DEFAULT_DEBUG_CHANNEL(print);
55

56 57 58 59
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
60

Alexandre Julliard's avatar
Alexandre Julliard committed
61

62
/******************************************************************
63
 *                  StartDocA  [GDI32.@]
64
 *
65 66 67
 * 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
68
 * the STARTDOC Escape.
69 70
 *
 * Note: we now do it the other way, with the STARTDOC Escape calling StartDoc.
71
 */
72
INT WINAPI StartDocW(HDC hdc, const DOCINFOW* doc)
73
{
74
    INT ret = 0;
75 76
    DC *dc = DC_GetDCPtr( hdc );

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

81
    if(!dc) return SP_ERROR;
82

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

/*************************************************************************
89
 *                  StartDocA [GDI32.@]
90
 *
91
 */
92
INT WINAPI StartDocA(HDC hdc, const DOCINFOA* doc)
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 127
    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 );
128 129

    return ret;
130 131
}

132

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

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

148 149

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

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

Alexandre Julliard's avatar
Alexandre Julliard committed
167

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

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

190 191

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

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

/**********************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
206
 *           QueryAbort   (GDI.155)
207 208 209 210 211 212 213
 *
 *  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.
 */
214
BOOL16 WINAPI QueryAbort16(HDC16 hdc16, INT16 reserved)
215
{
216
    BOOL ret = TRUE;
217
    HDC hdc = HDC_32( hdc16 );
218
    DC *dc = DC_GetDCPtr( hdc );
219
    ABORTPROC abproc;
220 221

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

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

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

234

235 236 237 238 239 240 241 242 243 244 245
/**********************************************************************
 *           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 );
246 247 248 249 250 251 252 253 254 255
    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);
    }
256 257 258 259
    return TRUE;
}


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

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

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

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


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

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

static struct hpq *hpqueue;

/**********************************************************************
 *           CreatePQ   (GDI.230)
 *
 */
307
HPQ16 WINAPI CreatePQ16(INT16 size)
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
{
#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);

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

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

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

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

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

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

    return tag;
}

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

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

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

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



424 425
/*
 * The following functions implement part of the spooling process to
426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450
 * 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];
}

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

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

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

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

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

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

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

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

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

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

525 526 527 528
        /**
         * The file name can be dos based, we have to find its
         * Unix correspondant file name
         */
529
        MultiByteToWideChar(CP_ACP, 0, psCmdP, -1, psCmdPW, MAX_PATH);
530
        if ((buffer = wine_get_unix_file_name(psCmdPW)))
531
        {
532 533 534 535 536 537
            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);
538 539 540 541 542 543 544 545 546 547 548 549 550 551
        }
    }
    return fd;
}

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

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

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

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

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

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

587
            hHandle = 1;
588

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

/**********************************************************************
 *           CloseJob   (GDI.243)
 *
 */
611
INT16 WINAPI CloseJob16(HPJOB16 hJob)
612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632
{
    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)
 *
 */
633
INT16 WINAPI WriteSpool16(HPJOB16 hJob, LPSTR lpData, INT16 cch)
634 635 636 637 638 639 640 641 642 643 644 645 646
{
    int nRet = SP_ERROR;
    PPRINTJOB pPrintJob = NULL;

    TRACE("%04x %08lx %04x\n", hJob, (DWORD)lpData, cch);

    pPrintJob = FindPrintJobFromHandle(hJob);
    if (pPrintJob != NULL && pPrintJob->fd >= 0 && cch)
    {
	if (write(pPrintJob->fd, lpData, cch) != cch)
	  nRet = SP_OUTOFDISK;
	else
	  nRet = cch;
647 648 649 650
#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.
	 */
651 652 653 654 655 656 657 658
	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;
	}
659
#endif
660 661 662 663
    }
    return nRet;
}

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

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

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

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


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

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

    nRet = FreePrintJob(hJob);
    return nRet;
}

701
/*
702 703 704 705 706 707 708 709 710
 * 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)
 *
 */
711
INT16 WINAPI StartSpoolPage16(HPJOB16 hJob)
712 713
{
    FIXME("StartSpoolPage GDI.246 unimplemented\n");
714
    return 1;
715

716 717
}

718 719 720 721 722

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


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


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

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

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

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

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

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


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

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

    if ((!lpPrinter) || (!lpProfile) ||
Alexandre Julliard's avatar
Alexandre Julliard committed
894 895
    ((DWORD)lpProfile == INT_PD_DEFAULT_MODEL) || (HIWORD(lpProfile) &&
    (!strcmp(lpProfile, PrinterModel))))
Alexandre Julliard's avatar
Alexandre Julliard committed
896 897 898 899 900 901 902
	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
903 904
    if (((DWORD)lpProfile == INT_PD_DEFAULT_DEVMODE) || (HIWORD(lpProfile) &&
    (!strcmp(lpProfile, DefaultDevMode)))) {
905
	if ( RegOpenKeyA(HKEY_LOCAL_MACHINE, RegStr_Printer, &hkey)
Alexandre Julliard's avatar
Alexandre Julliard committed
906
	     != ERROR_SUCCESS ||
907
	     RegSetValueExA(hkey, DefaultDevMode, 0, REG_BINARY,
Alexandre Julliard's avatar
Alexandre Julliard committed
908
			      lpPrinterData, dwSize) != ERROR_SUCCESS )
Alexandre Julliard's avatar
Alexandre Julliard committed
909 910 911 912 913 914
	        res = ERROR_INVALID_PRINTER_NAME;
    }
    else
    {
	strcat(RegStr_Printer, "\\");

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

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

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