ds_ctrl.c 13.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * Copyright 2000 Corel Corporation
 *
 * 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
16
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17 18
 */

19 20 21 22 23
#include "config.h"

#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif
24
#include <stdlib.h>
25
#include "sane_i.h"
26 27 28 29 30
#include "wine/debug.h"

WINE_DEFAULT_DEBUG_CHANNEL(twain);

/* DG_CONTROL/DAT_CAPABILITY/MSG_GET */
31
TW_UINT16 SANE_CapabilityGet (pTW_IDENTITY pOrigin, TW_MEMREF pData)
32 33 34
{
    TW_UINT16 twRC = TWRC_SUCCESS, twCC = TWCC_SUCCESS;
    pTW_CAPABILITY pCapability = (pTW_CAPABILITY) pData;
35

36
    TRACE("DG_CONTROL/DAT_CAPABILITY/MSG_GET\n");
37

38
    if (activeDS.currentState < 4 || activeDS.currentState > 7)
39 40
    {
        twRC = TWRC_FAILURE;
41
        activeDS.twCC = TWCC_SEQERROR;
42 43 44
    }
    else
    {
45
        twCC = SANE_SaneCapability (pCapability, MSG_GET);
46
        twRC = (twCC == TWCC_SUCCESS)?TWRC_SUCCESS:TWRC_FAILURE;
47
        activeDS.twCC = twCC;
48 49 50 51 52 53
    }

    return twRC;
}

/* DG_CONTROL/DAT_CAPABILITY/MSG_GETCURRENT */
54
TW_UINT16 SANE_CapabilityGetCurrent (pTW_IDENTITY pOrigin, TW_MEMREF pData)
55 56 57 58 59 60
{
    TW_UINT16 twRC = TWRC_SUCCESS, twCC = TWCC_SUCCESS;
    pTW_CAPABILITY pCapability = (pTW_CAPABILITY) pData;

    TRACE("DG_CONTROL/DAT_CAPABILITY/MSG_GETCURRENT\n");

61
    if (activeDS.currentState < 4 || activeDS.currentState > 7)
62 63
    {
        twRC = TWRC_FAILURE;
64
        activeDS.twCC = TWCC_SEQERROR;
65 66 67
    }
    else
    {
68
        twCC = SANE_SaneCapability (pCapability, MSG_GETCURRENT);
69
        twRC = (twCC == TWCC_SUCCESS)?TWRC_SUCCESS:TWRC_FAILURE;
70
        activeDS.twCC = twCC;
71
    }
72 73 74 75 76

    return twRC;
}

/* DG_CONTROL/DAT_CAPABILITY/MSG_GETDEFAULT */
77
TW_UINT16 SANE_CapabilityGetDefault (pTW_IDENTITY pOrigin, TW_MEMREF pData)
78 79 80 81 82 83
{
    TW_UINT16 twRC = TWRC_SUCCESS, twCC = TWCC_SUCCESS;
    pTW_CAPABILITY pCapability = (pTW_CAPABILITY) pData;

    TRACE("DG_CONTROL/DAT_CAPABILITY/MSG_GETDEFAULT\n");

84
    if (activeDS.currentState < 4 || activeDS.currentState > 7)
85 86
    {
        twRC = TWRC_FAILURE;
87
        activeDS.twCC = TWCC_SEQERROR;
88 89 90
    }
    else
    {
91
        twCC = SANE_SaneCapability (pCapability, MSG_GETDEFAULT);
92
        twRC = (twCC == TWCC_SUCCESS)?TWRC_SUCCESS:TWRC_FAILURE;
93
        activeDS.twCC = twCC;
94 95 96 97 98 99
    }

    return twRC;
}

/* DG_CONTROL/DAT_CAPABILITY/MSG_QUERYSUPPORT */
100 101
TW_UINT16 SANE_CapabilityQuerySupport (pTW_IDENTITY pOrigin,
                                        TW_MEMREF pData)
102
{
103 104
    TW_UINT16 twRC = TWRC_SUCCESS, twCC = TWCC_SUCCESS;
    pTW_CAPABILITY pCapability = (pTW_CAPABILITY) pData;
105

106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
    TRACE("DG_CONTROL/DAT_CAPABILITY/MSG_QUERYSUPPORT\n");

    if (activeDS.currentState < 4 || activeDS.currentState > 7)
    {
        twRC = TWRC_FAILURE;
        activeDS.twCC = TWCC_SEQERROR;
    }
    else
    {
        twCC = SANE_SaneCapability (pCapability, MSG_QUERYSUPPORT);
        twRC = (twCC == TWCC_SUCCESS)?TWRC_SUCCESS:TWRC_FAILURE;
        activeDS.twCC = twCC;
    }

    return twRC;
121 122 123
}

/* DG_CONTROL/DAT_CAPABILITY/MSG_RESET */
124
TW_UINT16 SANE_CapabilityReset (pTW_IDENTITY pOrigin, 
125 126 127 128 129 130 131
                                 TW_MEMREF pData)
{
    TW_UINT16 twRC = TWRC_SUCCESS, twCC = TWCC_SUCCESS;
    pTW_CAPABILITY pCapability = (pTW_CAPABILITY) pData;

    TRACE("DG_CONTROL/DAT_CAPABILITY/MSG_RESET\n");

132
    if (activeDS.currentState < 4 || activeDS.currentState > 7)
133 134
    {
        twRC = TWRC_FAILURE;
135
        activeDS.twCC = TWCC_SEQERROR;
136 137 138
    }
    else
    {
139
        twCC = SANE_SaneCapability (pCapability, MSG_RESET);
140
        twRC = (twCC == TWCC_SUCCESS)?TWRC_SUCCESS:TWRC_FAILURE;
141
        activeDS.twCC = twCC;
142 143 144 145 146 147
    }

    return twRC;
}

/* DG_CONTROL/DAT_CAPABILITY/MSG_SET */
148
TW_UINT16 SANE_CapabilitySet (pTW_IDENTITY pOrigin, 
149 150 151 152 153 154 155
                               TW_MEMREF pData)
{
    TW_UINT16 twRC = TWRC_SUCCESS, twCC = TWCC_SUCCESS;
    pTW_CAPABILITY pCapability = (pTW_CAPABILITY) pData;

    TRACE ("DG_CONTROL/DAT_CAPABILITY/MSG_SET\n");

156
    if (activeDS.currentState != 4)
157 158
    {
        twRC = TWRC_FAILURE;
159
        activeDS.twCC = TWCC_SEQERROR;
160 161 162
    }
    else
    {
163
        twCC = SANE_SaneCapability (pCapability, MSG_SET);
164 165 166 167 168 169 170
        if (twCC == TWCC_CHECKSTATUS)
        {
            twCC = TWCC_SUCCESS;
            twRC = TWRC_CHECKSTATUS;
        }
        else
            twRC = (twCC == TWCC_SUCCESS)?TWRC_SUCCESS:TWRC_FAILURE;
171
        activeDS.twCC = twCC;
172 173 174 175 176
    }
    return twRC;
}

/* DG_CONTROL/DAT_EVENT/MSG_PROCESSEVENT */
177
TW_UINT16 SANE_ProcessEvent (pTW_IDENTITY pOrigin, 
178 179
                              TW_MEMREF pData)
{
180
    TW_UINT16 twRC = TWRC_NOTDSEVENT;
181
    pTW_EVENT pEvent = (pTW_EVENT) pData;
182
    MSG *pMsg = pEvent->pEvent;
183

184
    TRACE("DG_CONTROL/DAT_EVENT/MSG_PROCESSEVENT  msg 0x%x, wParam 0x%lx\n", pMsg->message, pMsg->wParam);
185

186 187
    activeDS.twCC = TWCC_SUCCESS;
    if (pMsg->message == activeDS.windowMessage && activeDS.windowMessage)
188
    {
189 190
        twRC = TWRC_DSEVENT;
        pEvent->TWMessage = pMsg->wParam;
191 192
    }
    else
193 194 195
        pEvent->TWMessage = MSG_NULL;  /* no message to the application */

    if (activeDS.currentState < 5 || activeDS.currentState > 7)
196
    {
197 198
        twRC = TWRC_FAILURE;
        activeDS.twCC = TWCC_SEQERROR;
199 200 201 202 203 204
    }

    return twRC;
}

/* DG_CONTROL/DAT_PENDINGXFERS/MSG_ENDXFER */
205
TW_UINT16 SANE_PendingXfersEndXfer (pTW_IDENTITY pOrigin, 
206 207
                                     TW_MEMREF pData)
{
208 209 210
#ifndef SONAME_LIBSANE
    return TWRC_FAILURE;
#else
211 212
    TW_UINT16 twRC = TWRC_SUCCESS;
    pTW_PENDINGXFERS pPendingXfers = (pTW_PENDINGXFERS) pData;
213
    SANE_Status status;
214 215 216

    TRACE("DG_CONTROL/DAT_PENDINGXFERS/MSG_ENDXFER\n");

217
    if (activeDS.currentState != 6 && activeDS.currentState != 7)
218 219
    {
        twRC = TWRC_FAILURE;
220
        activeDS.twCC = TWCC_SEQERROR;
221 222 223
    }
    else
    {
224 225 226
        pPendingXfers->Count = -1;
        activeDS.currentState = 6;
        if (! activeDS.sane_started)
227
        {
228 229 230 231 232 233 234 235 236 237 238 239
            status = psane_start (activeDS.deviceHandle);
            if (status != SANE_STATUS_GOOD)
            {
                TRACE("PENDINGXFERS/MSG_ENDXFER sane_start returns %s\n", psane_strstatus(status));
                pPendingXfers->Count = 0;
                activeDS.currentState = 5;
                /* Notify the application that it can close the data source */
                if (activeDS.windowMessage)
                    PostMessageA(activeDS.hwndOwner, activeDS.windowMessage, MSG_CLOSEDSREQ, 0);
            }
            else
                activeDS.sane_started = TRUE;
240 241
        }
        twRC = TWRC_SUCCESS;
242
        activeDS.twCC = TWCC_SUCCESS;
243 244 245
    }

    return twRC;
246
#endif
247 248 249
}

/* DG_CONTROL/DAT_PENDINGXFERS/MSG_GET */
250
TW_UINT16 SANE_PendingXfersGet (pTW_IDENTITY pOrigin, 
251 252
                                 TW_MEMREF pData)
{
253 254 255
#ifndef SONAME_LIBSANE
    return TWRC_FAILURE;
#else
256 257
    TW_UINT16 twRC = TWRC_SUCCESS;
    pTW_PENDINGXFERS pPendingXfers = (pTW_PENDINGXFERS) pData;
258
    SANE_Status status;
259 260 261

    TRACE("DG_CONTROL/DAT_PENDINGXFERS/MSG_GET\n");

262
    if (activeDS.currentState < 4 || activeDS.currentState > 7)
263 264
    {
        twRC = TWRC_FAILURE;
265
        activeDS.twCC = TWCC_SEQERROR;
266 267 268
    }
    else
    {
269 270 271 272 273 274 275 276 277 278 279 280
        pPendingXfers->Count = -1;
        if (! activeDS.sane_started)
        {
            status = psane_start (activeDS.deviceHandle);
            if (status != SANE_STATUS_GOOD)
            {
                TRACE("PENDINGXFERS/MSG_GET sane_start returns %s\n", psane_strstatus(status));
                pPendingXfers->Count = 0;
            }
            else
                activeDS.sane_started = TRUE;
        }
281
        twRC = TWRC_SUCCESS;
282
        activeDS.twCC = TWCC_SUCCESS;
283 284 285
    }

    return twRC;
286
#endif
287 288 289
}

/* DG_CONTROL/DAT_PENDINGXFERS/MSG_RESET */
290
TW_UINT16 SANE_PendingXfersReset (pTW_IDENTITY pOrigin, 
291 292
                                   TW_MEMREF pData)
{
293 294 295
#ifndef SONAME_LIBSANE
    return TWRC_FAILURE;
#else
296 297 298 299 300
    TW_UINT16 twRC = TWRC_SUCCESS;
    pTW_PENDINGXFERS pPendingXfers = (pTW_PENDINGXFERS) pData;

    TRACE("DG_CONTROL/DAT_PENDINGXFERS/MSG_RESET\n");

301
    if (activeDS.currentState != 6)
302 303
    {
        twRC = TWRC_FAILURE;
304
        activeDS.twCC = TWCC_SEQERROR;
305 306 307 308
    }
    else
    {
        pPendingXfers->Count = 0;
309
        activeDS.currentState = 5;
310
        twRC = TWRC_SUCCESS;
311
        activeDS.twCC = TWCC_SUCCESS;
312 313 314 315 316 317

        if (activeDS.sane_started)
        {
            psane_cancel (activeDS.deviceHandle);
            activeDS.sane_started = FALSE;
        }
318 319 320
    }

    return twRC;
321
#endif
322 323 324
}

/* DG_CONTROL/DAT_SETUPMEMXFER/MSG_GET */
325
TW_UINT16 SANE_SetupMemXferGet (pTW_IDENTITY pOrigin, 
326 327
                                  TW_MEMREF pData)
{
328
#ifndef SONAME_LIBSANE
329
    return TWRC_FAILURE;
330 331 332 333
#else
    pTW_SETUPMEMXFER  pSetupMemXfer = (pTW_SETUPMEMXFER)pData;

    TRACE("DG_CONTROL/DAT_SETUPMEMXFER/MSG_GET\n");
334
    if (activeDS.sane_param_valid)
335
    {
336 337 338
        pSetupMemXfer->MinBufSize = activeDS.sane_param.bytes_per_line;
        pSetupMemXfer->MaxBufSize = activeDS.sane_param.bytes_per_line * 8;
        pSetupMemXfer->Preferred = activeDS.sane_param.bytes_per_line * 2;
339 340 341 342 343 344 345 346 347 348 349
    }
    else
    {
        /* Guessing */
        pSetupMemXfer->MinBufSize = 2000;
        pSetupMemXfer->MaxBufSize = 8000;
        pSetupMemXfer->Preferred = 4000;
    }

    return TWRC_SUCCESS;
#endif
350 351 352
}

/* DG_CONTROL/DAT_STATUS/MSG_GET */
353
TW_UINT16 SANE_GetDSStatus (pTW_IDENTITY pOrigin, 
354 355 356 357 358
                             TW_MEMREF pData)
{
    pTW_STATUS pSourceStatus = (pTW_STATUS) pData;

    TRACE ("DG_CONTROL/DAT_STATUS/MSG_GET\n");
359 360 361 362
    pSourceStatus->ConditionCode = activeDS.twCC;
    /* Reset the condition code */
    activeDS.twCC = TWCC_SUCCESS;
    return TWRC_SUCCESS;
363 364 365
}

/* DG_CONTROL/DAT_USERINTERFACE/MSG_DISABLEDS */
366 367
TW_UINT16 SANE_DisableDSUserInterface (pTW_IDENTITY pOrigin,
                                        TW_MEMREF pData)
368 369 370 371 372
{
    TW_UINT16 twRC = TWRC_SUCCESS;

    TRACE ("DG_CONTROL/DAT_USERINTERFACE/MSG_DISABLEDS\n");

373
    if (activeDS.currentState != 5)
374 375
    {
        twRC = TWRC_FAILURE;
376
        activeDS.twCC = TWCC_SEQERROR;
377 378 379
    }
    else
    {
380
        activeDS.currentState = 4;
381
        twRC = TWRC_SUCCESS;
382
        activeDS.twCC = TWCC_SUCCESS;
383 384 385 386 387 388
    }

    return twRC;
}

/* DG_CONTROL/DAT_USERINTERFACE/MSG_ENABLEDS */
389 390
TW_UINT16 SANE_EnableDSUserInterface (pTW_IDENTITY pOrigin,
                                       TW_MEMREF pData)
391 392 393
{
    TW_UINT16 twRC = TWRC_SUCCESS;
    pTW_USERINTERFACE pUserInterface = (pTW_USERINTERFACE) pData;
394

395
    TRACE ("DG_CONTROL/DAT_USERINTERFACE/MSG_ENABLEDS\n");
396

397
    if (activeDS.currentState != 4)
398 399
    {
        twRC = TWRC_FAILURE;
400
        activeDS.twCC = TWCC_SEQERROR;
401
	WARN("sequence error %d\n", activeDS.currentState);
402 403 404
    }
    else
    {
405
        activeDS.hwndOwner = pUserInterface->hParent;
406 407
        if (! activeDS.windowMessage)
            activeDS.windowMessage = RegisterWindowMessageA("SANE.DS ACTIVITY MESSAGE");
408 409
        if (pUserInterface->ShowUI)
        {
410
            BOOL rc;
411 412
            activeDS.currentState = 5; /* Transitions to state 5 */
            rc = DoScannerUI();
413
            pUserInterface->ModalUI = TRUE;
414 415
            if (!rc)
            {
416 417
                if (activeDS.windowMessage)
                    PostMessageA(activeDS.hwndOwner, activeDS.windowMessage, MSG_CLOSEDSREQ, 0);
418
            }
419
#ifdef SONAME_LIBSANE
420 421
            else
            {
422
                psane_get_parameters (activeDS.deviceHandle, &activeDS.sane_param);
423
                activeDS.sane_param_valid = TRUE;
424 425
            }
#endif
426 427 428 429
        }
        else
        {
            /* no UI will be displayed, so source is ready to transfer data */
430
            activeDS.currentState = 6; /* Transitions to state 6 directly */
431 432
            if (activeDS.windowMessage)
                PostMessageA(activeDS.hwndOwner, activeDS.windowMessage, MSG_XFERREADY, 0);
433
        }
434

435
        twRC = TWRC_SUCCESS;
436
        activeDS.twCC = TWCC_SUCCESS;
437 438 439 440 441 442
    }

    return twRC;
}

/* DG_CONTROL/DAT_USERINTERFACE/MSG_ENABLEDSUIONLY */
443
TW_UINT16 SANE_EnableDSUIOnly (pTW_IDENTITY pOrigin, 
444 445 446 447 448 449
                                TW_MEMREF pData)
{
    TW_UINT16 twRC = TWRC_SUCCESS;

    TRACE("DG_CONTROL/DAT_USERINTERFACE/MSG_ENABLEDSUIONLY\n");

450
    if (activeDS.currentState != 4)
451 452
    {
        twRC = TWRC_FAILURE;
453
        activeDS.twCC = TWCC_SEQERROR;
454 455 456 457 458
    }
    else
    {
        /* FIXME: we should replace xscanimage with our own UI */
        system ("xscanimage");
459
        activeDS.currentState = 5;
460
        twRC = TWRC_SUCCESS;
461
        activeDS.twCC = TWCC_SUCCESS;
462 463 464 465 466 467
    }

    return twRC;
}

/* DG_CONTROL/DAT_XFERGROUP/MSG_GET */
468
TW_UINT16 SANE_XferGroupGet (pTW_IDENTITY pOrigin, 
469 470 471 472 473 474 475 476
                              TW_MEMREF pData)
{
    FIXME ("stub!\n");

    return TWRC_FAILURE;
}

/* DG_CONTROL/DAT_XFERGROUP/MSG_SET */
477
TW_UINT16 SANE_XferGroupSet (pTW_IDENTITY pOrigin, 
478 479 480 481 482 483
                                  TW_MEMREF pData)
{
    FIXME ("stub!\n");

    return TWRC_FAILURE;
}