twain32_main.c 19.3 KB
Newer Older
1 2
/*
 * TWAIN32 functions
3
 *
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
 * Copyright 2000 Shi Quan He <shiquan@cyberdude.com>
 *
 * 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
 */

21 22
#include <stdarg.h>

23 24 25 26 27 28 29 30
#include "windef.h"
#include "winbase.h"
#include "twain.h"
#include "twain_i.h"
#include "wine/debug.h"

WINE_DEFAULT_DEBUG_CHANNEL(twain);

31
BOOL WINAPI DllMain (HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
32
{
33
    TRACE("%p,%lx,%p\n", hinstDLL, fdwReason, lpvReserved);
34 35 36 37

    switch (fdwReason)
    {
        case DLL_PROCESS_ATTACH:
38
            DisableThreadLibraryCalls(hinstDLL);
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
            DSM_currentState = 2;
            break;

        case DLL_PROCESS_DETACH:
            DSM_currentState = 1;
            break;
    }

    return TRUE;
}

TW_UINT16 TWAIN_SourceManagerHandler (
           pTW_IDENTITY pOrigin,
           TW_UINT16   DAT,
           TW_UINT16   MSG,
           TW_MEMREF   pData)
{
    TW_UINT16 twRC = TWRC_SUCCESS;

    switch (DAT)
    {
        case DAT_IDENTITY:
            switch (MSG)
            {
                case MSG_CLOSEDS:
                    twRC = TWAIN_CloseDS (pOrigin, pData);
                    break;

                case MSG_GETDEFAULT:
                    twRC = TWAIN_IdentityGetDefault (pOrigin, pData);
                    break;

                case MSG_GETFIRST:
                    twRC = TWAIN_IdentityGetFirst (pOrigin, pData);
                    break;

                case MSG_GETNEXT:
                    twRC = TWAIN_IdentityGetNext (pOrigin, pData);
                    break;

                case MSG_OPENDS:
                    twRC = TWAIN_OpenDS (pOrigin, pData);
                    break;

                case MSG_USERSELECT:
                    twRC = TWAIN_UserSelect (pOrigin, pData);
                    break;

                default:
                    /* Unrecognized operation triplet */
                    twRC = TWRC_FAILURE;
                    DSM_twCC = TWCC_BADPROTOCOL;
                    WARN("unrecognized operation triplet\n");
                    break;
            }
            break;

        case DAT_PARENT:
            switch (MSG)
            {
                case MSG_CLOSEDSM:
                    twRC = TWAIN_CloseDSM (pOrigin, pData);
                    break;

                case MSG_OPENDSM:
                    twRC = TWAIN_OpenDSM (pOrigin, pData);
                    break;

                default:
                    /* Unrecognized operation triplet */
                    twRC = TWRC_FAILURE;
                    DSM_twCC = TWCC_BADPROTOCOL;
                    WARN("unrecognized operation triplet\n");
112
            }
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 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 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525
            break;

        case DAT_STATUS:
            if (MSG == MSG_GET)
            {
                twRC = TWAIN_GetDSMStatus (pOrigin, pData);
            }
            else
            {
                twRC = TWRC_FAILURE;
                DSM_twCC = TWCC_BADPROTOCOL;
                WARN("unrecognized operation triplet\n");
            }
            break;

        default:
            twRC = TWRC_FAILURE;
            DSM_twCC = TWCC_BADPROTOCOL;
            WARN("unrecognized operation triplet\n");
            break;
    }

    return twRC;
}

TW_UINT16 TWAIN_SourceControlHandler (
           pTW_IDENTITY pOrigin,
           pTW_IDENTITY pDest,
           TW_UINT16    DAT,
           TW_UINT16    MSG,
           TW_MEMREF    pData)
{
    TW_UINT16 twRC = TWRC_SUCCESS;

    switch (DAT)
    {
        case DAT_CAPABILITY:
            switch (MSG)
            {
                case MSG_GET:
                    twRC = TWAIN_CapabilityGet (pOrigin, pDest, pData);
                    break;
                case MSG_GETCURRENT:
                    twRC = TWAIN_CapabilityGetCurrent (pOrigin, pDest, pData);
                    break;
                case MSG_GETDEFAULT:
                    twRC = TWAIN_CapabilityGetDefault (pOrigin, pDest, pData);
                    break;
                case MSG_QUERYSUPPORT:
                    twRC = TWAIN_CapabilityQuerySupport (pOrigin, pDest, pData);
                    break;
                case MSG_RESET:
                    twRC = TWAIN_CapabilityReset (pOrigin, pDest, pData);
                    break;
                case MSG_SET:
                    twRC = TWAIN_CapabilitySet (pOrigin, pDest, pData);
                    break;
                default:
                    twRC = TWRC_FAILURE;
                    WARN("unrecognized opertion triplet\n");
            }
            break;

        case DAT_CUSTOMDSDATA:
            switch (MSG)
            {
                case MSG_GET:
                    twRC = TWAIN_CustomDSDataGet (pOrigin, pDest, pData);
                    break;
                case MSG_SET:
                    twRC = TWAIN_CustomDSDataSet (pOrigin, pDest, pData);
                    break;
                default:
                    break;
            }
            break;

        case DAT_FILESYSTEM:
            switch (MSG)
            {
                /*case MSG_AUTOMATICCAPTUREDIRECTORY:
                    twRC = TWAIN_AutomaticCaptureDirectory
                               (pOrigin, pDest, pData);
                    break;*/
                case MSG_CHANGEDIRECTORY:
                    twRC = TWAIN_ChangeDirectory (pOrigin, pDest, pData);
                    break;
                /*case MSG_COPY:
                    twRC = TWAIN_FileSystemCopy (pOrigin, pDest, pData);
                    break;*/
                case MSG_CREATEDIRECTORY:
                    twRC = TWAIN_CreateDirectory (pOrigin, pDest, pData);
                    break;
                case MSG_DELETE:
                    twRC = TWAIN_FileSystemDelete (pOrigin, pDest, pData);
                    break;
                case MSG_FORMATMEDIA:
                    twRC = TWAIN_FormatMedia (pOrigin, pDest, pData);
                    break;
                case MSG_GETCLOSE:
                    twRC = TWAIN_FileSystemGetClose (pOrigin, pDest, pData);
                    break;
                case MSG_GETFIRSTFILE:
                    twRC = TWAIN_FileSystemGetFirstFile
                               (pOrigin, pDest, pData);
                    break;
                case MSG_GETINFO:
                    twRC = TWAIN_FileSystemGetInfo (pOrigin, pDest, pData);
                    break;
                case MSG_GETNEXTFILE:
                    twRC = TWAIN_FileSystemGetNextFile
                               (pOrigin, pDest, pData);
                    break;
                case MSG_RENAME:
                    twRC = TWAIN_FileSystemRename (pOrigin, pDest, pData);
                    break;
                default:
                    twRC = TWRC_FAILURE;
                    break;
            }
            break;

        case DAT_EVENT:
            if (MSG == MSG_PROCESSEVENT)
                twRC = TWAIN_ProcessEvent (pOrigin, pDest, pData);
            else
                twRC = TWRC_FAILURE;
            break;

        case DAT_PASSTHRU:
            if (MSG == MSG_PASSTHRU)
                twRC = TWAIN_PassThrough (pOrigin, pDest, pData);
            else
                twRC = TWRC_FAILURE;
            break;

        case DAT_PENDINGXFERS:
            switch (MSG)
            {
                case MSG_ENDXFER:
                    twRC = TWAIN_PendingXfersEndXfer (pOrigin, pDest, pData);
                    break;
                case MSG_GET:
                    twRC = TWAIN_PendingXfersGet (pOrigin, pDest, pData);
                    break;
                case MSG_RESET:
                    twRC = TWAIN_PendingXfersReset (pOrigin, pDest, pData);
                    break;
                /*case MSG_STOPFEEDER:
                    twRC = TWAIN_PendingXfersStopFeeder
                               (pOrigin, pDest, pData);
                    break;*/
                default:
                    twRC = TWRC_FAILURE;
            }
            break;

        case DAT_SETUPFILEXFER:
            switch (MSG)
            {
                case MSG_GET:
                    twRC = TWAIN_SetupFileXferGet (pOrigin, pDest, pData);
                    break;
                case MSG_GETDEFAULT:
                    twRC = TWAIN_SetupFileXferGetDefault
                               (pOrigin, pDest, pData);
                    break;
                case MSG_RESET:
                    twRC = TWAIN_SetupFileXferReset (pOrigin, pDest, pData);
                    break;
                case MSG_SET:
                    twRC = TWAIN_SetupFileXferSet (pOrigin, pDest, pData);
                    break;
                default:
                    twRC = TWRC_FAILURE;
                    break;
            }
            break;

        /*case DAT_SETUPFILEXFER2:
            switch (MSG)
            {
                case MSG_GET:
                    twRC = TWAIN_SetupFileXfer2Get (pOrigin, pDest, pData);
                    break;
                case MSG_GETDEFAULT:
                    twRC = TWAIN_SetupFileXfer2GetDefault
                               (pOrigin, pDest, pData);
                    break;
                case MSG_RESET:
                    twRC = TWAIN_SetupFileXfer2Reset (pOrigin, pDest, pData);
                    break;
                case MSG_SET:
                    twRC = TWAIN_SetupFileXfer2Set (pOrigin, pDest, pData);
                    break;
            }
            break;*/
        case DAT_SETUPMEMXFER:
            if (MSG == MSG_GET)
                twRC = TWAIN_SetupMemXferGet (pOrigin, pDest, pData);
            else
                twRC = TWRC_FAILURE;
            break;

        case DAT_STATUS:
            if (MSG == MSG_GET)
                twRC = TWAIN_GetDSStatus (pOrigin, pDest, pData);
            else
                twRC = TWRC_FAILURE;
            break;

        case DAT_USERINTERFACE:
            switch (MSG)
            {
                case MSG_DISABLEDS:
                    twRC = TWAIN_DisableDSUserInterface
                               (pOrigin, pDest, pData);
                    break;
                case MSG_ENABLEDS:
                    twRC = TWAIN_EnableDSUserInterface
                               (pOrigin, pDest, pData);
                    break;
                case MSG_ENABLEDSUIONLY:
                    twRC = TWAIN_EnableDSUIOnly (pOrigin, pDest, pData);
                    break;
                default:
                    twRC = TWRC_FAILURE;
                    break;
            }
            break;

        case DAT_XFERGROUP:
            switch (MSG)
            {
                case MSG_GET:
                    twRC = TWAIN_XferGroupGet (pOrigin, pDest, pData);
                    break;
                case MSG_SET:
                    twRC = TWAIN_XferGroupSet (pOrigin, pDest, pData);
                    break;
                default:
                    twRC = TWRC_FAILURE;
                    break;
            }
            break;

        default:
            twRC = TWRC_FAILURE;
            break;
    }

    return twRC;
}

TW_UINT16 TWAIN_ControlGroupHandler (
           pTW_IDENTITY pOrigin,
           pTW_IDENTITY pDest,
           TW_UINT16    DAT,
           TW_UINT16    MSG,
           TW_MEMREF    pData)
{
    TW_UINT16 twRC = TWRC_SUCCESS;

    if (pDest)
    {
        /* This operation's destination is a source */
        twRC = TWAIN_SourceControlHandler (pOrigin, pDest, DAT, MSG, pData);
    }
    else
    {
        /* This operation's destination is the Source Manager */
        twRC = TWAIN_SourceManagerHandler (pOrigin, DAT, MSG, pData);
    }

    return twRC;
}

TW_UINT16 TWAIN_ImageGroupHandler (
           pTW_IDENTITY pOrigin,
           pTW_IDENTITY pDest,
           TW_UINT16    DAT,
           TW_UINT16    MSG,
           TW_MEMREF    pData)
{
    TW_UINT16 twRC = TWRC_SUCCESS;

    if (pDest == NULL)
    {
        DSM_twCC = TWCC_BADDEST;
        return TWRC_FAILURE;
    }

    switch (DAT)
    {
        case DAT_CIECOLOR:
            if (MSG == MSG_GET)
                twRC = TWAIN_CIEColorGet (pOrigin, pDest, pData);
            else
                twRC = TWRC_FAILURE;
            break;

        case DAT_EXTIMAGEINFO:
            if (MSG == MSG_GET)
                twRC = TWAIN_ExtImageInfoGet (pOrigin, pDest, pData);
            else
                twRC = TWRC_FAILURE;
            break;

        case DAT_GRAYRESPONSE:
            switch (MSG)
            {
                case MSG_RESET:
                    twRC = TWAIN_GrayResponseReset (pOrigin, pDest, pData);
                    break;
                case MSG_SET:
                    twRC = TWAIN_GrayResponseSet (pOrigin, pDest, pData);
                    break;
                default:
                    twRC = TWRC_FAILURE;
                    DSM_twCC = TWCC_BADPROTOCOL;
                    WARN("unrecognized operation triplet\n");
                    break;
            }
            break;
        case DAT_IMAGEFILEXFER:
            if (MSG == MSG_GET)
                twRC = TWAIN_ImageFileXferGet (pOrigin, pDest, pData);
            else
                twRC = TWRC_FAILURE;
            break;

        case DAT_IMAGEINFO:
            if (MSG == MSG_GET)
                twRC = TWAIN_ImageInfoGet (pOrigin, pDest, pData);
            else
                twRC = TWRC_FAILURE;
            break;

        case DAT_IMAGELAYOUT:
            switch (MSG)
            {
                case MSG_GET:
                    twRC = TWAIN_ImageLayoutGet (pOrigin, pDest, pData);
                    break;
                case MSG_GETDEFAULT:
                    twRC = TWAIN_ImageLayoutGetDefault (pOrigin, pDest, pData);
                    break;
                case MSG_RESET:
                    twRC = TWAIN_ImageLayoutReset (pOrigin, pDest, pData);
                    break;
                case MSG_SET:
                    twRC = TWAIN_ImageLayoutSet (pOrigin, pDest, pData);
                    break;
                default:
                    twRC = TWRC_FAILURE;
                    DSM_twCC = TWCC_BADPROTOCOL;
                    WARN("unrecognized operation triplet\n");
                    break;
            }
            break;

        case DAT_IMAGEMEMXFER:
            if (MSG == MSG_GET)
                twRC = TWAIN_ImageMemXferGet (pOrigin, pDest, pData);
            else
                twRC = TWRC_FAILURE;
            break;

        case DAT_IMAGENATIVEXFER:
            if (MSG == MSG_GET)
                twRC = TWAIN_ImageNativeXferGet (pOrigin, pDest, pData);
            else
                twRC = TWRC_FAILURE;
            break;

        case DAT_JPEGCOMPRESSION:
            switch (MSG)
            {
                case MSG_GET:
                    twRC = TWAIN_JPEGCompressionGet (pOrigin, pDest, pData);
                    break;
                case MSG_GETDEFAULT:
                    twRC = TWAIN_JPEGCompressionGetDefault
                               (pOrigin, pDest, pData);
                    break;
                case MSG_RESET:
                    twRC = TWAIN_JPEGCompressionReset (pOrigin, pDest, pData);
                    break;
                case MSG_SET:
                    twRC = TWAIN_JPEGCompressionSet (pOrigin, pDest, pData);
                    break;
                default:
                    twRC = TWRC_FAILURE;
                    DSM_twCC = TWCC_BADPROTOCOL;
                    WARN("unrecognized operation triplet\n");
                    break;
            }
            break;

        case DAT_PALETTE8:
            switch (MSG)
            {
                case MSG_GET:
                    twRC = TWAIN_Palette8Get (pOrigin, pDest, pData);
                    break;
                case MSG_GETDEFAULT:
                    twRC = TWAIN_Palette8GetDefault (pOrigin, pDest, pData);
                    break;
                case MSG_RESET:
                    twRC = TWAIN_Palette8Reset (pOrigin, pDest, pData);
                    break;
                case MSG_SET:
                    twRC = TWAIN_Palette8Set (pOrigin, pDest, pData);
526
                    break;
527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589
                default:
                    twRC = TWRC_FAILURE;
                    DSM_twCC = TWCC_BADPROTOCOL;
                    WARN("unrecognized operation triplet\n");
            }
            break;

        case DAT_RGBRESPONSE:
            switch (MSG)
            {
                case MSG_RESET:
                    twRC = TWAIN_RGBResponseReset (pOrigin, pDest, pData);
                    break;
                case MSG_SET:
                    twRC = TWAIN_RGBResponseSet (pOrigin, pDest, pData);
                    break;
                default:
                    twRC = TWRC_FAILURE;
                    DSM_twCC = TWCC_BADPROTOCOL;
                    WARN("unrecognized operation triplet\n");
                    break;
            }
            break;

        default:
            twRC = TWRC_FAILURE;
            DSM_twCC = TWCC_BADPROTOCOL;
            WARN("unrecognized operation triplet\n");
    }
    return twRC;
}

TW_UINT16 TWAIN_AudioGroupHandler (
           pTW_IDENTITY pOrigin,
           pTW_IDENTITY pDest,
           TW_UINT16    DAT,
           TW_UINT16    MSG,
           TW_MEMREF    pData)
{
    TW_UINT16 twRC = TWRC_FAILURE;

    switch (DAT)
    {
        case DAT_AUDIOFILEXFER:
            if (MSG == MSG_GET)
                twRC = TWAIN_AudioFileXferGet (pOrigin, pDest, pData);
            break;

        case DAT_AUDIOINFO:
            if (MSG == MSG_GET)
                twRC = TWAIN_AudioInfoGet (pOrigin, pDest, pData);
            break;

        case DAT_AUDIONATIVEXFER:
            if (MSG == MSG_GET)
                twRC = TWAIN_AudioNativeXferGet (pOrigin, pDest, pData);
            break;

        default:
            DSM_twCC = TWCC_BADPROTOCOL;
            twRC = TWRC_FAILURE;
            break;
    }
590

591 592 593 594 595 596 597 598 599 600 601 602 603 604 605
    return twRC;
}

/* Main entry point for the TWAIN library */
TW_UINT16 WINAPI
DSM_Entry (pTW_IDENTITY pOrigin,
           pTW_IDENTITY pDest,
           TW_UINT32    DG,
           TW_UINT16    DAT,
           TW_UINT16    MSG,
           TW_MEMREF    pData)
{
    TW_UINT16 twRC = TWRC_SUCCESS;  /* Return Code */

    TRACE("(DG=%ld DAT=%d MSG=%d)\n", DG, DAT, MSG);
606

607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625
    switch (DG)
    {
        case DG_CONTROL:
            twRC = TWAIN_ControlGroupHandler (pOrigin,pDest,DAT,MSG,pData);
            break;
        case DG_IMAGE:
            twRC = TWAIN_ImageGroupHandler (pOrigin,pDest,DAT,MSG,pData);
            break;
        case DG_AUDIO:
            twRC = TWAIN_AudioGroupHandler (pOrigin,pDest,DAT,MSG,pData);
            break;
        default:
            DSM_twCC = TWCC_BADPROTOCOL;
            twRC = TWRC_FAILURE;
    }

    return twRC;
}

626
/* A helper function that looks up a destination identity in the active
627 628 629 630 631 632 633 634 635 636 637
   source list */
activeDS *TWAIN_LookupSource (pTW_IDENTITY pDest)
{
    activeDS *pSource;

    for (pSource = activeSources; pSource; pSource = pSource->next)
        if (pSource->identity.Id == pDest->Id)
            break;

    return pSource;
}