aboutsummaryrefslogtreecommitdiff
path: root/backend/backend.h
blob: 848f5a7f4d049c5784ead6fece56b2a735c23bef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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
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
/*  backend.h -- SANE backend implementation boiler plate
 *  Copyright (C) 2007  EPSON AVASYS CORPORATION
 *  Copyright (C) 2008  SEIKO EPSON CORPORATION
 *
 *  License: GPLv2+|iscan
 *  Authors: AVASYS CORPORATION
 *
 *  This file is part of the SANE backend distributed with Image Scan!
 *
 *  Image Scan!'s SANE backend is free software.
 *  You can redistribute it and/or modify it under the terms of the GNU
 *  General Public License as published by the Free Software Foundation;
 *  either version 2 of the License or at your option any later version.
 *
 *    This program 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 General Public License for more details.
 *
 *  You ought to have received a copy of the GNU General Public License
 *  along with this package.  If not, see <http://www.gnu.org/licenses/>.
 *
 *
 *  Linking Image Scan!'s SANE backend statically or dynamically with
 *  other modules is making a combined work based on this SANE backend.
 *  Thus, the terms and conditions of the GNU General Public License
 *  cover the whole combination.
 *
 *  As a special exception, the copyright holders of Image Scan!'s SANE
 *  backend give you permission to link Image Scan!'s SANE backend with
 *  SANE frontends that communicate with Image Scan!'s SANE backend
 *  solely through the SANE Application Programming Interface,
 *  regardless of the license terms of these SANE frontends, and to
 *  copy and distribute the resulting combined work under terms of your
 *  choice, provided that every copy of the combined work is
 *  accompanied by a complete copy of the source code of Image Scan!'s
 *  SANE backend (the version of Image Scan!'s SANE backend used to
 *  produce the combined work), being distributed under the terms of
 *  the GNU General Public License plus this exception.  An independent
 *  module is a module which is not derived from or based on Image
 *  Scan!'s SANE backend.
 *
 *  As a special exception, the copyright holders of Image Scan!'s SANE
 *  backend give you permission to link Image Scan!'s SANE backend with
 *  independent modules that communicate with Image Scan!'s SANE
 *  backend solely through the "Interpreter" interface, regardless of
 *  the license terms of these independent modules, and to copy and
 *  distribute the resulting combined work under terms of your choice,
 *  provided that every copy of the combined work is accompanied by a
 *  complete copy of the source code of Image Scan!'s SANE backend (the
 *  version of Image Scan!'s SANE backend used to produce the combined
 *  work), being distributed under the terms of the GNU General Public
 *  License plus this exception.  An independent module is a module
 *  which is not derived from or based on Image Scan!'s SANE backend.
 *
 *  Note that people who make modified versions of Image Scan!'s SANE
 *  backend are not obligated to grant special exceptions for their
 *  modified versions; it is their choice whether to do so.  The GNU
 *  General Public License gives permission to release a modified
 *  version without this exception; this exception also makes it
 *  possible to release a modified version which carries forward this
 *  exception.
 */


#ifndef included_backend_h
#define included_backend_h

/*! \file
 *  \brief  Provides backend implementation boiler plate.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <sane/sane.h>
#include <stdio.h>


#ifndef BACKEND_NAME
#error "Define the BACKEND_NAME macro to match your backend's name"
#error "before including this file (backend.h)."
#error "The value should not be quoted.  Characters in your backend"
#error "name that are not allowed in C variables should be replaced"
#error "by underscores, one for each such character."
#endif /* !defined (BACKEND_NAME) */


#ifdef __cplusplus
extern "C"
{
#endif
/*  Set up the machinery used to create the "shadow" API entries.
 *  FIXME: Move these #define's into their own header file so that
 *         multi-file plug-ins can use them too.
 *  FIXME: Add an api_entry() function that mimicks the API_ENTRY()
 *         #define so that plug-ins can use dlsym() and friends more
 *         easily.  You need a way to create names at run-time.  The
 *         #define only works at compile-time!
 */
#define CONCAT_INTERNAL(s1,s2)  s1##s2
#define CONCAT(s1,s2)           CONCAT_INTERNAL(s1,s2)

#define API_ENTRY(backend,function) \
  CONCAT(sane_,CONCAT(backend,CONCAT(_,function)))

/*  For each API entry, set up "shadow" API entries with a unique name.
 */

SANE_Status
API_ENTRY (BACKEND_NAME, init) (SANE_Int *version,
                                SANE_Auth_Callback authoriser);
void
API_ENTRY (BACKEND_NAME, exit) (void);

SANE_Status
API_ENTRY (BACKEND_NAME, get_devices) (const SANE_Device ***device_list,
                                       SANE_Bool local_only);

SANE_Status
API_ENTRY (BACKEND_NAME, open) (SANE_String_Const device_name,
                                SANE_Handle *handle);
void
API_ENTRY (BACKEND_NAME, close) (SANE_Handle handle);

const SANE_Option_Descriptor *
API_ENTRY (BACKEND_NAME, get_option_descriptor) (SANE_Handle handle,
                                                 SANE_Int index);
SANE_Status
API_ENTRY (BACKEND_NAME, control_option) (SANE_Handle handle, SANE_Int index,
                                          SANE_Action action, void *value,
                                          SANE_Word *info);
SANE_Status
API_ENTRY (BACKEND_NAME, get_parameters) (SANE_Handle handle,
                                          SANE_Parameters *parameters);

SANE_Status
API_ENTRY (BACKEND_NAME, start) (SANE_Handle handle);
SANE_Status
API_ENTRY (BACKEND_NAME, read) (SANE_Handle handle, SANE_Byte *buffer,
                                SANE_Int max_length, SANE_Int *length);
void
API_ENTRY (BACKEND_NAME, cancel) (SANE_Handle handle);

SANE_Status
API_ENTRY (BACKEND_NAME, set_io_mode) (SANE_Handle handle,
                                       SANE_Bool non_blocking);
SANE_Status
API_ENTRY (BACKEND_NAME, get_select_fd) (SANE_Handle handle,
                                         SANE_Int *fdp);


SANE_String_Const
API_ENTRY (BACKEND_NAME, strstatus) (SANE_Status status);


#ifdef BACKEND_CREATE_FORWARDERS

/*  For each API entry, forward the original call to the "shadow" API
 *  entry just declared.
 *  Most of the argument validation can be added here.
 */

SANE_Status
sane_init (SANE_Int *version, SANE_Auth_Callback authoriser)
{
  SANE_Status status;

  status = API_ENTRY (BACKEND_NAME, init) (version, authoriser);
  return status;
}

void
sane_exit (void)
{
  API_ENTRY (BACKEND_NAME, exit) ();
  return;
}


SANE_Status
sane_get_devices (const SANE_Device ***device_list, SANE_Bool local_only)
{
  SANE_Status status;

  status = API_ENTRY (BACKEND_NAME, get_devices) (device_list, local_only);
  return status;
}


SANE_Status
sane_open (SANE_String_Const device_name, SANE_Handle *handle)
{
  SANE_Status status;

  status = API_ENTRY (BACKEND_NAME, open) (device_name, handle);
  return status;
}

void
sane_close (SANE_Handle handle)
{
  API_ENTRY (BACKEND_NAME, close) (handle);
  return;
}


const SANE_Option_Descriptor *
sane_get_option_descriptor (SANE_Handle handle, SANE_Int index)
{
  const SANE_Option_Descriptor *desc;

  desc = API_ENTRY (BACKEND_NAME, get_option_descriptor) (handle, index);
  return desc;
}

SANE_Status
sane_control_option (SANE_Handle handle, SANE_Int index, SANE_Action action,
                     void *value, SANE_Word *info)
{
  SANE_Status status;

  status = API_ENTRY (BACKEND_NAME, control_option) (handle, index, action,
                                                     value, info);
  return status;
}

SANE_Status
sane_get_parameters (SANE_Handle handle, SANE_Parameters *parameters)
{
  SANE_Status status;

  status = API_ENTRY (BACKEND_NAME, get_parameters) (handle, parameters);
  return status;
}


SANE_Status
sane_start (SANE_Handle handle)
{
  SANE_Status status;

  status = API_ENTRY (BACKEND_NAME, start) (handle);
  return status;
}

SANE_Status
sane_read (SANE_Handle handle, SANE_Byte *buffer, SANE_Int max_length,
           SANE_Int *length)
{
  SANE_Status status;

  status = API_ENTRY (BACKEND_NAME, read) (handle, buffer, max_length,
                                           length);
  return status;
}

void
sane_cancel (SANE_Handle handle)
{
  API_ENTRY (BACKEND_NAME, cancel) (handle);
  return;
}


SANE_Status
sane_set_io_mode (SANE_Handle handle, SANE_Bool non_blocking)
{
  SANE_Status status;

  status = API_ENTRY (BACKEND_NAME, set_io_mode) (handle, non_blocking);
  return status;
}

SANE_Status
sane_get_select_fd (SANE_Handle handle, SANE_Int *fdp)
{
  SANE_Status status;

  status = API_ENTRY (BACKEND_NAME, get_select_fd) (handle, fdp);
  return status;
}

SANE_String_Const
sane_strstatus (SANE_Status status)
{
  SANE_String_Const str;

  str = API_ENTRY (BACKEND_NAME, strstatus) (status);
  return str;
}

/*! \brief  Marks a string for the translation tools.
 */
#define N(string)       (string)

/*  Backends should not be bothered with implementing their own
 *  version of this bit of SANE API.  At best, all will do the same
 *  thing, at worst every one has its own implementation returning
 *  different strings for the same status.  This implementation will
 *  be used by every backend without them even noticing.
 */
SANE_String_Const
API_ENTRY (BACKEND_NAME, strstatus) (SANE_Status status)
{
  switch (status)
    {
    case SANE_STATUS_GOOD:
      return N("Success");
    case SANE_STATUS_UNSUPPORTED:
      return N("Operation not supported");
    case SANE_STATUS_CANCELLED:
      return N("Operation was cancelled");
    case SANE_STATUS_DEVICE_BUSY:
      return N("Device busy");
    case SANE_STATUS_INVAL:
      return N("Invalid argument");
    case SANE_STATUS_EOF:
      return N("End of file reached");
    case SANE_STATUS_JAMMED:
      return N("Document feeder jammed");
    case SANE_STATUS_NO_DOCS:
      return N("Document feeder out of documents");
    case SANE_STATUS_COVER_OPEN:
      return N("Scanner cover is open");
    case SANE_STATUS_IO_ERROR:
      return N("Error during device I/O");
    case SANE_STATUS_NO_MEM:
      return N("Out of memory");
    case SANE_STATUS_ACCESS_DENIED:
      return N("Access to resource has been denied");

    default:
      {
        static char msg[80];    /* not re-entrant! */

        snprintf (msg, 80, N("Unknown status code (%d)"), status);
        return msg;
      }
    }
}

#undef N                        /* no longer needed, clean up */


#endif  /* !defined (BACKEND_CREATE_FORWARDERS) */


/*  Use the preprocessor to rename API entries in the backend to match
 *  the "shadow" API entries we have set up above.
 *  This way, the implementer can ignore all these name playing games.
 */

#define sane_init       API_ENTRY (BACKEND_NAME, init)
#define sane_exit       API_ENTRY (BACKEND_NAME, exit)
#define sane_get_devices \
                        API_ENTRY (BACKEND_NAME, get_devices)
#define sane_open       API_ENTRY (BACKEND_NAME, open)
#define sane_close      API_ENTRY (BACKEND_NAME, close)
#define sane_get_option_descriptor \
                        API_ENTRY (BACKEND_NAME, get_option_descriptor)
#define sane_control_option \
                        API_ENTRY (BACKEND_NAME, control_option)
#define sane_get_parameters \
                        API_ENTRY (BACKEND_NAME, get_parameters)
#define sane_start      API_ENTRY (BACKEND_NAME, start)
#define sane_read       API_ENTRY (BACKEND_NAME, read)
#define sane_cancel     API_ENTRY (BACKEND_NAME, cancel)
#define sane_set_io_mode \
                        API_ENTRY (BACKEND_NAME, set_io_mode)
#define sane_get_select_fd \
                        API_ENTRY (BACKEND_NAME, get_select_fd)


#ifdef __cplusplus
}       /* extern "C" */
#endif

#endif  /* !defined (included_backend_h) */