/* sane - Scanner Access Now Easy.

   Copyright (C) 2009 Ondrej Zary

   This file is part of the SANE package.

   This program 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 should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place - Suite 330, Boston,
   MA 02111-1307, USA.

   As a special exception, the authors of SANE give permission for
   additional uses of the libraries contained in this release of SANE.

   The exception is that, if you link a SANE library with other files
   to produce an executable, this does not by itself cause the
   resulting executable to be covered by the GNU General Public
   License.  Your use of that executable is in no way restricted on
   account of linking the SANE library code into it.

   This exception does not, however, invalidate any other reasons why
   the executable file might be covered by the GNU General Public
   License.

   If you submit changes to SANE to the maintainers to be included in
   a subsequent release, you agree by submitting the changes that
   those changes may be distributed with this exception intact.

   If you write modifications of your own for SANE, it is your choice
   whether to permit this exception to apply to your modifications.
   If you do not wish that, delete this exception notice.

   This file implements a SANE backend for Optrox flatbed scanners
   with proprietary ISA card.
*/

#include "../include/sane/config.h"

#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "../include/sane/sane.h"
#include "../include/sane/sanei.h"
#include "../include/sane/saneopts.h"

#define BACKEND_NAME	optrox
#define BACKEND_BUILD	0
#include "../include/sane/sanei_backend.h"

#include "optrox.h"

static const SANE_Device **devlist = NULL;	/* for sane_get_devices */

static const SANE_Device optrox_device = 
  { "photomaker_3f", "Optrox", "Photomaker 3F", "flatbed scanner" };

enum options {
  OPT_NUMOPTIONS = 0,
  NUM_OPTIONS
};

static const SANE_Word num_options = NUM_OPTIONS;

static SANE_Option_Descriptor **options = NULL;

SANE_Status
sane_init (SANE_Int * version_code, SANE_Auth_Callback authorize)
{
  DBG_INIT ();
  DBG (2, "%s: optrox build %d\n", __FUNCTION__, BACKEND_BUILD);

  if (version_code)
    *version_code = SANE_VERSION_CODE (SANE_CURRENT_MAJOR, V_MINOR,
                                       BACKEND_BUILD);
  authorize = authorize;

  return SANE_STATUS_GOOD;
}

void
sane_exit (void)
{
  DBG (2, "%s\n", __FUNCTION__);
  if (devlist)
    free(devlist);
}

SANE_Status
sane_get_devices (const SANE_Device *** device_list, SANE_Bool local_only)
{
  DBG (2, "%s: %p, %d\n", __FUNCTION__, (void *)device_list, local_only);

  if (!device_list)
    return SANE_STATUS_INVAL;

  if (devlist)
    free(devlist);

  devlist = malloc (2 * sizeof (SANE_Device *));
  if (!devlist) {
    DBG (1, "%s: out of memory\n", __FUNCTION__);
    return SANE_STATUS_NO_MEM;
  }
  devlist[0] = &optrox_device;
  devlist[1] = NULL;
  *device_list = devlist;

  return SANE_STATUS_GOOD;
}

SANE_Status
sane_open (SANE_String_Const devicename, SANE_Handle * handle)
{
  options = malloc ((NUM_OPTIONS+1) * sizeof (SANE_Option_Descriptor *));
  options[OPT_NUMOPTIONS] = malloc (sizeof (SANE_Option_Descriptor));
  options[OPT_NUMOPTIONS]->name = SANE_NAME_NUM_OPTIONS;
  options[OPT_NUMOPTIONS]->title = SANE_TITLE_NUM_OPTIONS;
  options[OPT_NUMOPTIONS]->desc = SANE_DESC_NUM_OPTIONS;
  options[OPT_NUMOPTIONS]->type = SANE_TYPE_INT;
  options[OPT_NUMOPTIONS]->unit = SANE_UNIT_NONE;
  options[OPT_NUMOPTIONS]->size = sizeof(SANE_Word);
  options[OPT_NUMOPTIONS]->cap = SANE_CAP_SOFT_DETECT;
  
  options[NUM_OPTIONS] = NULL;

  return SANE_STATUS_GOOD;
}

void
sane_close (SANE_Handle handle)
{
  int i = 0;

  if (options)
    while (options[i])
      free(options[i++]);

  free(options);
  options = NULL;
}

const SANE_Option_Descriptor *
sane_get_option_descriptor (SANE_Handle handle, SANE_Int option)
{
  if (option > NUM_OPTIONS)
    return NULL;
  return options[option];
}

SANE_Status
sane_control_option (SANE_Handle handle, SANE_Int option,
		     SANE_Action action, void *val, SANE_Int * info)
{
  switch (option) {
    case OPT_NUMOPTIONS:
      if (!val || action != SANE_ACTION_GET_VALUE)
        return SANE_STATUS_INVAL;
      memcpy(val, &num_options, sizeof (num_options));
      return SANE_STATUS_GOOD;
  }
  return SANE_STATUS_INVAL;
}

SANE_Status
sane_get_parameters (SANE_Handle handle, SANE_Parameters * params)
{
  return SANE_STATUS_GOOD;
}

SANE_Status
sane_start (SANE_Handle handle)
{
  return SANE_STATUS_GOOD;
}

SANE_Status
sane_read (SANE_Handle handle, SANE_Byte * buf, SANE_Int max_len,
	   SANE_Int * len)
{
  *len = 0;
  return SANE_STATUS_GOOD;
}

void
sane_cancel (SANE_Handle handle)
{
  return;
}

SANE_Status
sane_set_io_mode (SANE_Handle handle, SANE_Bool non_blocking)
{
  return SANE_STATUS_UNSUPPORTED;
}

SANE_Status
sane_get_select_fd (SANE_Handle handle, SANE_Int * fd)
{
  return SANE_STATUS_UNSUPPORTED;
}

