Logo Search packages:      
Sourcecode: libgphoto2 version File versions  Download package

int gp_camera_init ( Camera *  camera,
GPContext context 
)

gp_camera_init: : a camera : a GPContext

Initiates a connection to the camera. Before calling this function, the should be set up using gp_camera_set_port_path or gp_camera_set_port_name and gp_camera_set_abilities. If that has been omitted, gphoto2 tries to autodetect any cameras and chooses the first one if any cameras are found. It is generally a good idea to call gp_camera_exit after transactions have been completed in order to give other applications the chance to access the camera, too.

Return value: a gphoto2 error code

Definition at line 641 of file gphoto2-camera.c.

References gp_camera_set_abilities().

{
      CameraAbilities a;
      const char *model, *port;
      CameraLibraryInitFunc init_func;
      int result;

      gp_log (GP_LOG_DEBUG, "gphoto2-camera", "Initializing camera...");

      CHECK_NULL (camera);
      /*
       * Reset the exit_requested flag. If this flag is set, 
       * gp_camera_exit will be called as soon as the camera is no
       * longer in use (used flag).
       */
      camera->pc->exit_requested = 0;

      /*
       * If the model hasn't been indicated, try to
       * figure it out (USB only). Beware of "Directory Browse".
       */
      if (strcasecmp (camera->pc->a.model, "Directory Browse") &&
          !strcmp ("", camera->pc->a.model)) {
            CameraAbilitiesList *al;
            GPPortInfoList *il;
            int m, p;
            GPPortInfo info;
            CameraList *list;

            result = gp_list_new (&list);
            if (result < GP_OK)
                  return result;

            gp_log (GP_LOG_DEBUG, "gphoto2-camera", "Neither "
                  "port nor model set. Trying auto-detection...");

            /* Call auto-detect and choose the first camera */
            gp_abilities_list_new (&al);
            gp_abilities_list_load (al, context);
            gp_port_info_list_new (&il);
            gp_port_info_list_load (il);
            gp_abilities_list_detect (al, il, list, context);
            if (!gp_list_count (list)) {
                  gp_abilities_list_free (al);
                  gp_port_info_list_free (il);
                  gp_context_error (context, _("Could not detect "
                                   "any camera"));
                  gp_list_free (list);
                  return (GP_ERROR_MODEL_NOT_FOUND);
            }

            gp_list_get_name  (list, 0, &model);
            m = gp_abilities_list_lookup_model (al, model);
            gp_abilities_list_get_abilities (al, m, &a);
            gp_abilities_list_free (al);
            CRSL (camera, gp_camera_set_abilities (camera, a), context, list);
            CRSL (camera, gp_list_get_value (list, 0, &port), context, list);
            p = gp_port_info_list_lookup_path (il, port);
            gp_port_info_list_get_info (il, p, &info);
            gp_port_info_list_free (il);
            CRSL (camera, gp_camera_set_port_info (camera, info), context, list);
            gp_list_free (list);
      }

      if (strcasecmp (camera->pc->a.model, "Directory Browse")) {
            switch (camera->port->type) {
            case GP_PORT_NONE:
                  gp_context_error (context, _("You have to set the "
                        "port prior initialization of the camera."));
                  return (GP_ERROR_UNKNOWN_PORT);
            case GP_PORT_USB:
                  if (gp_port_usb_find_device (camera->port,
                              camera->pc->a.usb_vendor,
                              camera->pc->a.usb_product) != GP_OK) {
                        CRS (camera, gp_port_usb_find_device_by_class
                              (camera->port,
                              camera->pc->a.usb_class,
                              camera->pc->a.usb_subclass,
                              camera->pc->a.usb_protocol), context);
                              }
                  break;
            default:
                  break;
            }
      }

      /* Load the library. */
      gp_log (GP_LOG_DEBUG, "gphoto2-camera", "Loading '%s'...",
            camera->pc->a.library);
#ifdef HAVE_LTDL
      lt_dlinit ();
      camera->pc->lh = lt_dlopenext (camera->pc->a.library);
      if (!camera->pc->lh) {
            gp_context_error (context, _("Could not load required "
                  "camera driver '%s' (%s)."), camera->pc->a.library,
                  lt_dlerror ());
            lt_dlexit ();
            return (GP_ERROR_LIBRARY);
      }
#else
      camera->pc->lh = GP_SYSTEM_DLOPEN (camera->pc->a.library);
      if (!camera->pc->lh) {
            gp_context_error (context, _("Could not load required "
                  "camera driver '%s'."), camera->pc->a.library);
            return (GP_ERROR_LIBRARY);
      }
#endif

      /* Initialize the camera */
#ifdef HAVE_LTDL
      init_func = lt_dlsym (camera->pc->lh, "camera_init");
#else
      init_func = GP_SYSTEM_DLSYM (camera->pc->lh, "camera_init");
#endif
      if (!init_func) {
#ifdef HAVE_LTDL
            lt_dlclose (camera->pc->lh);
            lt_dlexit ();
#else
            GP_SYSTEM_DLCLOSE (camera->pc->lh);
#endif
            camera->pc->lh = NULL;
            gp_context_error (context, _("Camera driver '%s' is "
                  "missing the 'camera_init' function."), 
                  camera->pc->a.library);
            return (GP_ERROR_LIBRARY);
      }

      if (strcasecmp (camera->pc->a.model, "Directory Browse")) {
            result = gp_port_open (camera->port);
            if (result < 0) {
#ifdef HAVE_LTDL
                  lt_dlclose (camera->pc->lh);
                  lt_dlexit ();
#else
                  GP_SYSTEM_DLCLOSE (camera->pc->lh);
#endif
                  camera->pc->lh = NULL;
                  return (result);
            }
      }

      result = init_func (camera, context);
      if (result < 0) {
            gp_port_close (camera->port);
#ifdef HAVE_LTDL
            lt_dlclose (camera->pc->lh);
            lt_dlexit ();
#else
            GP_SYSTEM_DLCLOSE (camera->pc->lh);
#endif
            camera->pc->lh = NULL;
            memset (camera->functions, 0, sizeof (CameraFunctions));
            return (result);
      }

      /* We don't care if that goes wrong */
#ifdef HAVE_MULTI
      gp_port_close (camera->port);
#endif

      return (GP_OK);
}


Generated by  Doxygen 1.6.0   Back to index