sane.ds: Add support for DG_IMAGE/DAT_IMAGELAYOUT/GET and SET. Enables Acrobat to...
[wine] / dlls / sane.ds / ds_ctrl.c
1 /*
2  * Copyright 2000 Corel Corporation
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * 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 #include "config.h"
20
21 #ifdef HAVE_UNISTD_H
22 # include <unistd.h>
23 #endif
24 #include <stdlib.h>
25 #include "twain.h"
26 #include "sane_i.h"
27 #include "wine/debug.h"
28
29 WINE_DEFAULT_DEBUG_CHANNEL(twain);
30
31 /* DG_CONTROL/DAT_CAPABILITY/MSG_GET */
32 TW_UINT16 SANE_CapabilityGet (pTW_IDENTITY pOrigin, TW_MEMREF pData)
33 {
34     TW_UINT16 twRC = TWRC_SUCCESS, twCC = TWCC_SUCCESS;
35     pTW_CAPABILITY pCapability = (pTW_CAPABILITY) pData;
36
37     TRACE("DG_CONTROL/DAT_CAPABILITY/MSG_GET\n");
38
39     if (activeDS.currentState < 4 || activeDS.currentState > 7)
40     {
41         twRC = TWRC_FAILURE;
42         activeDS.twCC = TWCC_SEQERROR;
43     }
44     else
45     {
46         twCC = SANE_SaneCapability (pCapability, MSG_GET);
47         twRC = (twCC == TWCC_SUCCESS)?TWRC_SUCCESS:TWRC_FAILURE;
48         activeDS.twCC = twCC;
49     }
50
51     return twRC;
52 }
53
54 /* DG_CONTROL/DAT_CAPABILITY/MSG_GETCURRENT */
55 TW_UINT16 SANE_CapabilityGetCurrent (pTW_IDENTITY pOrigin, TW_MEMREF pData)
56 {
57     TW_UINT16 twRC = TWRC_SUCCESS, twCC = TWCC_SUCCESS;
58     pTW_CAPABILITY pCapability = (pTW_CAPABILITY) pData;
59
60     TRACE("DG_CONTROL/DAT_CAPABILITY/MSG_GETCURRENT\n");
61
62     if (activeDS.currentState < 4 || activeDS.currentState > 7)
63     {
64         twRC = TWRC_FAILURE;
65         activeDS.twCC = TWCC_SEQERROR;
66     }
67     else
68     {
69         twCC = SANE_SaneCapability (pCapability, MSG_GETCURRENT);
70         twRC = (twCC == TWCC_SUCCESS)?TWRC_SUCCESS:TWRC_FAILURE;
71         activeDS.twCC = twCC;
72     }
73
74     return twRC;
75 }
76
77 /* DG_CONTROL/DAT_CAPABILITY/MSG_GETDEFAULT */
78 TW_UINT16 SANE_CapabilityGetDefault (pTW_IDENTITY pOrigin, TW_MEMREF pData)
79 {
80     TW_UINT16 twRC = TWRC_SUCCESS, twCC = TWCC_SUCCESS;
81     pTW_CAPABILITY pCapability = (pTW_CAPABILITY) pData;
82
83     TRACE("DG_CONTROL/DAT_CAPABILITY/MSG_GETDEFAULT\n");
84
85     if (activeDS.currentState < 4 || activeDS.currentState > 7)
86     {
87         twRC = TWRC_FAILURE;
88         activeDS.twCC = TWCC_SEQERROR;
89     }
90     else
91     {
92         twCC = SANE_SaneCapability (pCapability, MSG_GETDEFAULT);
93         twRC = (twCC == TWCC_SUCCESS)?TWRC_SUCCESS:TWRC_FAILURE;
94         activeDS.twCC = twCC;
95     }
96
97     return twRC;
98 }
99
100 /* DG_CONTROL/DAT_CAPABILITY/MSG_QUERYSUPPORT */
101 TW_UINT16 SANE_CapabilityQuerySupport (pTW_IDENTITY pOrigin,
102                                         TW_MEMREF pData)
103 {
104     TW_UINT16 twRC = TWRC_SUCCESS, twCC = TWCC_SUCCESS;
105     pTW_CAPABILITY pCapability = (pTW_CAPABILITY) pData;
106
107     TRACE("DG_CONTROL/DAT_CAPABILITY/MSG_QUERYSUPPORT\n");
108
109     if (activeDS.currentState < 4 || activeDS.currentState > 7)
110     {
111         twRC = TWRC_FAILURE;
112         activeDS.twCC = TWCC_SEQERROR;
113     }
114     else
115     {
116         twCC = SANE_SaneCapability (pCapability, MSG_QUERYSUPPORT);
117         twRC = (twCC == TWCC_SUCCESS)?TWRC_SUCCESS:TWRC_FAILURE;
118         activeDS.twCC = twCC;
119     }
120
121     return twRC;
122 }
123
124 /* DG_CONTROL/DAT_CAPABILITY/MSG_RESET */
125 TW_UINT16 SANE_CapabilityReset (pTW_IDENTITY pOrigin, 
126                                  TW_MEMREF pData)
127 {
128     TW_UINT16 twRC = TWRC_SUCCESS, twCC = TWCC_SUCCESS;
129     pTW_CAPABILITY pCapability = (pTW_CAPABILITY) pData;
130
131     TRACE("DG_CONTROL/DAT_CAPABILITY/MSG_RESET\n");
132
133     if (activeDS.currentState < 4 || activeDS.currentState > 7)
134     {
135         twRC = TWRC_FAILURE;
136         activeDS.twCC = TWCC_SEQERROR;
137     }
138     else
139     {
140         twCC = SANE_SaneCapability (pCapability, MSG_RESET);
141         twRC = (twCC == TWCC_SUCCESS)?TWRC_SUCCESS:TWRC_FAILURE;
142         activeDS.twCC = twCC;
143     }
144
145     return twRC;
146 }
147
148 /* DG_CONTROL/DAT_CAPABILITY/MSG_SET */
149 TW_UINT16 SANE_CapabilitySet (pTW_IDENTITY pOrigin, 
150                                TW_MEMREF pData)
151 {
152     TW_UINT16 twRC = TWRC_SUCCESS, twCC = TWCC_SUCCESS;
153     pTW_CAPABILITY pCapability = (pTW_CAPABILITY) pData;
154
155     TRACE ("DG_CONTROL/DAT_CAPABILITY/MSG_SET\n");
156
157     if (activeDS.currentState != 4)
158     {
159         twRC = TWRC_FAILURE;
160         activeDS.twCC = TWCC_SEQERROR;
161     }
162     else
163     {
164         twCC = SANE_SaneCapability (pCapability, MSG_SET);
165         if (twCC == TWCC_CHECKSTATUS)
166         {
167             twCC = TWCC_SUCCESS;
168             twRC = TWRC_CHECKSTATUS;
169         }
170         else
171             twRC = (twCC == TWCC_SUCCESS)?TWRC_SUCCESS:TWRC_FAILURE;
172         activeDS.twCC = twCC;
173     }
174     return twRC;
175 }
176
177 /* DG_CONTROL/DAT_EVENT/MSG_PROCESSEVENT */
178 TW_UINT16 SANE_ProcessEvent (pTW_IDENTITY pOrigin, 
179                               TW_MEMREF pData)
180 {
181     TW_UINT16 twRC = TWRC_NOTDSEVENT;
182     pTW_EVENT pEvent = (pTW_EVENT) pData;
183     MSG *pMsg = pEvent->pEvent;
184
185     TRACE("DG_CONTROL/DAT_EVENT/MSG_PROCESSEVENT  msg 0x%x, wParam 0x%lx\n", pMsg->message, pMsg->wParam);
186
187     activeDS.twCC = TWCC_SUCCESS;
188     if (pMsg->message == activeDS.windowMessage && activeDS.windowMessage)
189     {
190         twRC = TWRC_DSEVENT;
191         pEvent->TWMessage = pMsg->wParam;
192     }
193     else
194         pEvent->TWMessage = MSG_NULL;  /* no message to the application */
195
196     if (activeDS.currentState < 5 || activeDS.currentState > 7)
197     {
198         twRC = TWRC_FAILURE;
199         activeDS.twCC = TWCC_SEQERROR;
200     }
201
202     return twRC;
203 }
204
205 /* DG_CONTROL/DAT_PENDINGXFERS/MSG_ENDXFER */
206 TW_UINT16 SANE_PendingXfersEndXfer (pTW_IDENTITY pOrigin, 
207                                      TW_MEMREF pData)
208 {
209 #ifndef SONAME_LIBSANE
210     return TWRC_FAILURE;
211 #else
212     TW_UINT16 twRC = TWRC_SUCCESS;
213     pTW_PENDINGXFERS pPendingXfers = (pTW_PENDINGXFERS) pData;
214     SANE_Status status;
215
216     TRACE("DG_CONTROL/DAT_PENDINGXFERS/MSG_ENDXFER\n");
217
218     if (activeDS.currentState != 6 && activeDS.currentState != 7)
219     {
220         twRC = TWRC_FAILURE;
221         activeDS.twCC = TWCC_SEQERROR;
222     }
223     else
224     {
225         pPendingXfers->Count = -1;
226         activeDS.currentState = 6;
227         if (! activeDS.sane_started)
228         {
229             status = psane_start (activeDS.deviceHandle);
230             if (status != SANE_STATUS_GOOD)
231             {
232                 TRACE("PENDINGXFERS/MSG_ENDXFER sane_start returns %s\n", psane_strstatus(status));
233                 pPendingXfers->Count = 0;
234                 activeDS.currentState = 5;
235                 /* Notify the application that it can close the data source */
236                 if (activeDS.windowMessage)
237                     PostMessageA(activeDS.hwndOwner, activeDS.windowMessage, MSG_CLOSEDSREQ, 0);
238             }
239             else
240                 activeDS.sane_started = TRUE;
241         }
242         twRC = TWRC_SUCCESS;
243         activeDS.twCC = TWCC_SUCCESS;
244     }
245
246     return twRC;
247 #endif
248 }
249
250 /* DG_CONTROL/DAT_PENDINGXFERS/MSG_GET */
251 TW_UINT16 SANE_PendingXfersGet (pTW_IDENTITY pOrigin, 
252                                  TW_MEMREF pData)
253 {
254 #ifndef SONAME_LIBSANE
255     return TWRC_FAILURE;
256 #else
257     TW_UINT16 twRC = TWRC_SUCCESS;
258     pTW_PENDINGXFERS pPendingXfers = (pTW_PENDINGXFERS) pData;
259     SANE_Status status;
260
261     TRACE("DG_CONTROL/DAT_PENDINGXFERS/MSG_GET\n");
262
263     if (activeDS.currentState < 4 || activeDS.currentState > 7)
264     {
265         twRC = TWRC_FAILURE;
266         activeDS.twCC = TWCC_SEQERROR;
267     }
268     else
269     {
270         pPendingXfers->Count = -1;
271         if (! activeDS.sane_started)
272         {
273             status = psane_start (activeDS.deviceHandle);
274             if (status != SANE_STATUS_GOOD)
275             {
276                 TRACE("PENDINGXFERS/MSG_GET sane_start returns %s\n", psane_strstatus(status));
277                 pPendingXfers->Count = 0;
278             }
279             else
280                 activeDS.sane_started = TRUE;
281         }
282         twRC = TWRC_SUCCESS;
283         activeDS.twCC = TWCC_SUCCESS;
284     }
285
286     return twRC;
287 #endif
288 }
289
290 /* DG_CONTROL/DAT_PENDINGXFERS/MSG_RESET */
291 TW_UINT16 SANE_PendingXfersReset (pTW_IDENTITY pOrigin, 
292                                    TW_MEMREF pData)
293 {
294 #ifndef SONAME_LIBSANE
295     return TWRC_FAILURE;
296 #else
297     TW_UINT16 twRC = TWRC_SUCCESS;
298     pTW_PENDINGXFERS pPendingXfers = (pTW_PENDINGXFERS) pData;
299
300     TRACE("DG_CONTROL/DAT_PENDINGXFERS/MSG_RESET\n");
301
302     if (activeDS.currentState != 6)
303     {
304         twRC = TWRC_FAILURE;
305         activeDS.twCC = TWCC_SEQERROR;
306     }
307     else
308     {
309         pPendingXfers->Count = 0;
310         activeDS.currentState = 5;
311         twRC = TWRC_SUCCESS;
312         activeDS.twCC = TWCC_SUCCESS;
313
314         if (activeDS.sane_started)
315         {
316             psane_cancel (activeDS.deviceHandle);
317             activeDS.sane_started = FALSE;
318         }
319     }
320
321     return twRC;
322 #endif
323 }
324
325 /* DG_CONTROL/DAT_SETUPMEMXFER/MSG_GET */
326 TW_UINT16 SANE_SetupMemXferGet (pTW_IDENTITY pOrigin, 
327                                   TW_MEMREF pData)
328 {
329 #ifndef SONAME_LIBSANE
330     return TWRC_FAILURE;
331 #else
332     pTW_SETUPMEMXFER  pSetupMemXfer = (pTW_SETUPMEMXFER)pData;
333
334     TRACE("DG_CONTROL/DAT_SETUPMEMXFER/MSG_GET\n");
335     if (activeDS.sane_param_valid)
336     {
337         pSetupMemXfer->MinBufSize = activeDS.sane_param.bytes_per_line;
338         pSetupMemXfer->MaxBufSize = activeDS.sane_param.bytes_per_line * 8;
339         pSetupMemXfer->Preferred = activeDS.sane_param.bytes_per_line * 2;
340     }
341     else
342     {
343         /* Guessing */
344         pSetupMemXfer->MinBufSize = 2000;
345         pSetupMemXfer->MaxBufSize = 8000;
346         pSetupMemXfer->Preferred = 4000;
347     }
348
349     return TWRC_SUCCESS;
350 #endif
351 }
352
353 /* DG_CONTROL/DAT_STATUS/MSG_GET */
354 TW_UINT16 SANE_GetDSStatus (pTW_IDENTITY pOrigin, 
355                              TW_MEMREF pData)
356 {
357     pTW_STATUS pSourceStatus = (pTW_STATUS) pData;
358
359     TRACE ("DG_CONTROL/DAT_STATUS/MSG_GET\n");
360     pSourceStatus->ConditionCode = activeDS.twCC;
361     /* Reset the condition code */
362     activeDS.twCC = TWCC_SUCCESS;
363     return TWRC_SUCCESS;
364 }
365
366 /* DG_CONTROL/DAT_USERINTERFACE/MSG_DISABLEDS */
367 TW_UINT16 SANE_DisableDSUserInterface (pTW_IDENTITY pOrigin,
368                                         TW_MEMREF pData)
369 {
370     TW_UINT16 twRC = TWRC_SUCCESS;
371
372     TRACE ("DG_CONTROL/DAT_USERINTERFACE/MSG_DISABLEDS\n");
373
374     if (activeDS.currentState != 5)
375     {
376         twRC = TWRC_FAILURE;
377         activeDS.twCC = TWCC_SEQERROR;
378     }
379     else
380     {
381         activeDS.currentState = 4;
382         twRC = TWRC_SUCCESS;
383         activeDS.twCC = TWCC_SUCCESS;
384     }
385
386     return twRC;
387 }
388
389 /* DG_CONTROL/DAT_USERINTERFACE/MSG_ENABLEDS */
390 TW_UINT16 SANE_EnableDSUserInterface (pTW_IDENTITY pOrigin,
391                                        TW_MEMREF pData)
392 {
393     TW_UINT16 twRC = TWRC_SUCCESS;
394     pTW_USERINTERFACE pUserInterface = (pTW_USERINTERFACE) pData;
395
396     TRACE ("DG_CONTROL/DAT_USERINTERFACE/MSG_ENABLEDS\n");
397
398     if (activeDS.currentState != 4)
399     {
400         twRC = TWRC_FAILURE;
401         activeDS.twCC = TWCC_SEQERROR;
402         WARN("sequence error %d\n", activeDS.currentState);
403     }
404     else
405     {
406         activeDS.hwndOwner = pUserInterface->hParent;
407         if (! activeDS.windowMessage)
408             activeDS.windowMessage = RegisterWindowMessageA("SANE.DS ACTIVITY MESSAGE");
409         if (pUserInterface->ShowUI)
410         {
411             BOOL rc;
412             activeDS.currentState = 5; /* Transitions to state 5 */
413             rc = DoScannerUI();
414             pUserInterface->ModalUI = TRUE;
415             if (!rc)
416             {
417                 if (activeDS.windowMessage)
418                     PostMessageA(activeDS.hwndOwner, activeDS.windowMessage, MSG_CLOSEDSREQ, 0);
419             }
420 #ifdef SONAME_LIBSANE
421             else
422             {
423                 psane_get_parameters (activeDS.deviceHandle, &activeDS.sane_param);
424                 activeDS.sane_param_valid = TRUE;
425             }
426 #endif
427         }
428         else
429         {
430             /* no UI will be displayed, so source is ready to transfer data */
431             activeDS.currentState = 6; /* Transitions to state 6 directly */
432             if (activeDS.windowMessage)
433                 PostMessageA(activeDS.hwndOwner, activeDS.windowMessage, MSG_XFERREADY, 0);
434         }
435
436         twRC = TWRC_SUCCESS;
437         activeDS.twCC = TWCC_SUCCESS;
438     }
439
440     return twRC;
441 }
442
443 /* DG_CONTROL/DAT_USERINTERFACE/MSG_ENABLEDSUIONLY */
444 TW_UINT16 SANE_EnableDSUIOnly (pTW_IDENTITY pOrigin, 
445                                 TW_MEMREF pData)
446 {
447     TW_UINT16 twRC = TWRC_SUCCESS;
448
449     TRACE("DG_CONTROL/DAT_USERINTERFACE/MSG_ENABLEDSUIONLY\n");
450
451     if (activeDS.currentState != 4)
452     {
453         twRC = TWRC_FAILURE;
454         activeDS.twCC = TWCC_SEQERROR;
455     }
456     else
457     {
458         /* FIXME: we should replace xscanimage with our own UI */
459         system ("xscanimage");
460         activeDS.currentState = 5;
461         twRC = TWRC_SUCCESS;
462         activeDS.twCC = TWCC_SUCCESS;
463     }
464
465     return twRC;
466 }
467
468 /* DG_CONTROL/DAT_XFERGROUP/MSG_GET */
469 TW_UINT16 SANE_XferGroupGet (pTW_IDENTITY pOrigin, 
470                               TW_MEMREF pData)
471 {
472     FIXME ("stub!\n");
473
474     return TWRC_FAILURE;
475 }
476
477 /* DG_CONTROL/DAT_XFERGROUP/MSG_SET */
478 TW_UINT16 SANE_XferGroupSet (pTW_IDENTITY pOrigin, 
479                                   TW_MEMREF pData)
480 {
481     FIXME ("stub!\n");
482
483     return TWRC_FAILURE;
484 }