コード例 #1
0
ファイル: gphoto2-camera.c プロジェクト: Jay314/libgphoto2
/**
 * Initiate a connection to the \c camera. 
 *
 * @param camera a #Camera
 * @param context a #GPContext
 * @return a gphoto2 error code
 *
 * Before calling this function, the
 * \c camera 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.
 *
 */
int
gp_camera_init (Camera *camera, GPContext *context)
{
	CameraAbilities a;
	const char *model, *port;
	CameraLibraryInitFunc init_func;
	int result;

	GP_LOG_D ("Initializing camera...");

	C_PARAMS (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;
		char		*ppath;
		GPPortType	ptype;
		GPPortInfo	info;
        	CameraList	*list;

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

		GP_LOG_D ("Neither port nor model set. Trying auto-detection...");

		/* Call auto-detect and choose the first camera */
		CRSL (camera, gp_abilities_list_new (&al), context, list);
		CRSL (camera, gp_abilities_list_load (al, context), context, list);
		CRSL (camera, gp_port_info_list_new (&il), context, list);
		CRSL (camera, gp_port_info_list_load (il), context, list);
		CRSL (camera, gp_abilities_list_detect (al, il, list, context), context, list);
		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);
		}
		p = 0;
		CRSL (camera, gp_port_get_info (camera->port, &info), context, list);
		CRSL (camera, gp_port_info_get_path (info, &ppath), context, list);
		CRSL (camera, gp_port_info_get_type (info, &ptype), context, list);
		/* if the port was set before, then use that entry, but not if it is "usb:" */
		if ((ptype == GP_PORT_USB) && strlen(ppath) && strcmp(ppath, "usb:")) {
			for (p = gp_list_count (list);p--;) {
				const char *xp;

				gp_list_get_value (list, p, &xp);
				if (!strcmp (xp, ppath))
					break;
			}
			if (p<0) {
				gp_abilities_list_free (al);
				gp_port_info_list_free (il);
				gp_context_error (context, _("Could not detect any camera at port %s"), ppath);
				gp_list_free (list);
				return (GP_ERROR_FILE_NOT_FOUND);
			}
		}

		CRSL (camera, gp_list_get_name  (list, p, &model), context, list);
		m = gp_abilities_list_lookup_model (al, model);
		CRSL (camera, m, context, list);
		CRSL (camera, gp_abilities_list_get_abilities (al, m, &a), context, list);
		gp_abilities_list_free (al);
		CRSL (camera, gp_camera_set_abilities (camera, a), context, list);
		CRSL (camera, gp_list_get_value (list, p, &port), context, list);
		p = gp_port_info_list_lookup_path (il, port);
		CRSL (camera, p, context, list);
		CRSL (camera, gp_port_info_list_get_info (il, p, &info), context, list);
		CRSL (camera, gp_camera_set_port_info (camera, info), context, list);
		gp_port_info_list_free (il);
		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 to 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_D ("Loading '%s'...", camera->pc->a.library);
	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);
	}

	/* Initialize the camera */
	init_func = lt_dlsym (camera->pc->lh, "camera_init");
	if (!init_func) {
		lt_dlclose (camera->pc->lh);
		lt_dlexit ();
		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) {
			lt_dlclose (camera->pc->lh);
			lt_dlexit ();
			camera->pc->lh = NULL;
			return (result);
		}
	}

	result = init_func (camera, context);
	if (result < 0) {
		gp_port_close (camera->port);
		lt_dlclose (camera->pc->lh);
		lt_dlexit ();
		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);
}
コード例 #2
0
int
gp_abilities_list_load_dir (CameraAbilitiesList *list, const char *dir,
			    GPContext *context)
{
	CameraLibraryIdFunc id;
	CameraLibraryAbilitiesFunc ab;
	CameraText text;
	int ret, x, old_count, new_count;
	int i, p;
	const char *filename;
	CameraList *flist;
	int count;
	lt_dlhandle lh;

	C_PARAMS (list && dir);

	GP_LOG_D ("Using ltdl to load camera libraries from '%s'...", dir);
	CHECK_RESULT (gp_list_new (&flist));
	ret = gp_list_reset (flist);
	if (ret < GP_OK) {
		gp_list_free (flist);
		return ret;
	}
	if (1) { /* a new block in which we can define a temporary variable */
		foreach_data_t foreach_data = { NULL, GP_OK };
		foreach_data.list = flist;
		lt_dlinit ();
		lt_dladdsearchdir (dir);
		ret = lt_dlforeachfile (dir, foreach_func, &foreach_data);
		lt_dlexit ();
		if (ret != 0) {
			gp_list_free (flist);
			GP_LOG_E ("Internal error looking for camlibs (%d)", ret);
			gp_context_error (context,
					  _("Internal error looking for camlibs. "
					    "(path names too long?)"));
			return (foreach_data.result!=GP_OK)?foreach_data.result:GP_ERROR;
		}
	}
	count = gp_list_count (flist);
	if (count < GP_OK) {
		gp_list_free (flist);
		return ret;
	}
	GP_LOG_D ("Found %i camera drivers.", count);
	lt_dlinit ();
	p = gp_context_progress_start (context, count,
		_("Loading camera drivers from '%s'..."), dir);
	for (i = 0; i < count; i++) {
		ret = gp_list_get_name (flist, i, &filename);
		if (ret < GP_OK) {
			gp_list_free (flist);
			return ret;
		}
		lh = lt_dlopenext (filename);
		if (!lh) {
			GP_LOG_D ("Failed to load '%s': %s.", filename,
				lt_dlerror ());
			continue;
		}

		/* camera_id */
		id = lt_dlsym (lh, "camera_id");
		if (!id) {
			GP_LOG_D ("Library '%s' does not seem to "
				"contain a camera_id function: %s",
				filename, lt_dlerror ());
			lt_dlclose (lh);
			continue;
		}

		/*
		 * Make sure the camera driver hasn't been
		 * loaded yet.
		 */
		if (id (&text) != GP_OK) {
			lt_dlclose (lh);
			continue;
		}
		if (gp_abilities_list_lookup_id (list, text.text) >= 0) {
			lt_dlclose (lh);
			continue;
		} 

		/* camera_abilities */
		ab = lt_dlsym (lh, "camera_abilities");
		if (!ab) {
			GP_LOG_D ("Library '%s' does not seem to "
				"contain a camera_abilities function: "
				"%s", filename, lt_dlerror ());
			lt_dlclose (lh);
			continue;
		}

		old_count = gp_abilities_list_count (list);
		if (old_count < 0) {
			lt_dlclose (lh);
			continue;
		}

		if (ab (list) != GP_OK) {
			lt_dlclose (lh);
			continue;
		}

		/* do not free the library in valgrind mode */
#if !defined(VALGRIND) 
		lt_dlclose (lh);
#endif

		new_count = gp_abilities_list_count (list);
		if (new_count < 0)
			continue;

		/* Copy in the core-specific information */
		for (x = old_count; x < new_count; x++) {
			strcpy (list->abilities[x].id, text.text);
			strcpy (list->abilities[x].library, filename);
		}

		gp_context_progress_update (context, p, i);
		if (gp_context_cancel (context) == GP_CONTEXT_FEEDBACK_CANCEL) {
			lt_dlexit ();
			gp_list_free (flist);
			return (GP_ERROR_CANCEL); 
		}
	}
	gp_context_progress_stop (context, p);
	lt_dlexit ();
	gp_list_free (flist);

	return (GP_OK);
}
コード例 #3
0
ファイル: camera-method.c プロジェクト: pinkavaj/gphoto2
static GnomeVFSResult do_get_file_info (
    GnomeVFSMethod*                 method,
    GnomeVFSURI*                    uri,
    GnomeVFSFileInfo*               info,
    GnomeVFSFileInfoOptions         options,
    GnomeVFSContext*                context)
{
    GnomeVFSResult result;
    GnomeVFSURI *parent;
    Camera *camera;
    CameraFileInfo camera_info;
    CameraList *list;
    const gchar *path, *name;
    char *basename;
    guint i;
    int count;

    printf ("ENTER: do_get_file_info (%s)\n", camera_uri_get_path (uri));

    G_LOCK (cameras);

    /* Is this root? */
    if (!gnome_vfs_uri_has_parent (uri)) {
        info->name = g_strdup ("/");
        info->valid_fields = GNOME_VFS_FILE_INFO_FIELDS_TYPE |
                             GNOME_VFS_FILE_INFO_FIELDS_MIME_TYPE;
        info->type = GNOME_VFS_FILE_TYPE_DIRECTORY;
        info->mime_type = g_strdup ("x-directory/normal");
        G_UNLOCK (cameras);
        return (GNOME_VFS_OK);
    }

    printf ("Getting camera (do_get_file_info)...\n");
    result = get_camera (uri, &camera);
    if (result != GNOME_VFS_OK) {
        G_UNLOCK (cameras);
        return (result);
    }

    result = GNOME_VFS_RESULT (gp_list_new (&list));
    if (result != GNOME_VFS_OK) {
        unref_camera (camera);
        G_UNLOCK (cameras);
        return (result);
    }

    /*
     * We cannot use get_basename here because of potential trailing
     * slashes.
     */
    basename = gnome_vfs_uri_extract_short_name (uri);

    parent = camera_uri_get_parent (uri);
    path = camera_uri_get_path (parent);

    result = GNOME_VFS_RESULT (gp_camera_folder_list_folders (camera, path,
                               list, NULL));
    if (result != GNOME_VFS_OK) {
        gnome_vfs_uri_unref (parent);
        unref_camera (camera);
        gp_list_free (list);
        g_free (basename);
        G_UNLOCK (cameras);
        return (result);
    }

    count = gp_list_count (list);
    if (count < 0) {
        gnome_vfs_uri_unref (parent);
        unref_camera (camera);
        gp_list_free (list);
        g_free (basename);
        G_UNLOCK (cameras);
        return (GNOME_VFS_RESULT (count));
    }

    for (i = 0; i < count; i++) {
        result = GNOME_VFS_RESULT (gp_list_get_name (list, i, &name));
        if (result != GNOME_VFS_OK) {
            gnome_vfs_uri_unref (parent);
            unref_camera (camera);
            gp_list_free (list);
            g_free (basename);
            G_UNLOCK (cameras);
            return (result);
        }
        if (!strcmp (name, basename)) {
            info->name = g_strdup (basename);
            info->valid_fields = GNOME_VFS_FILE_INFO_FIELDS_TYPE |
                                 GNOME_VFS_FILE_INFO_FIELDS_MIME_TYPE;
            info->type = GNOME_VFS_FILE_TYPE_DIRECTORY;
            info->mime_type = g_strdup ("x-directory/normal");
            gnome_vfs_uri_unref (parent);
            unref_camera (camera);
            gp_list_free (list);
            g_free (basename);
            G_UNLOCK (cameras);
            printf ("Found folder!\n");
            return (GNOME_VFS_OK);
        }
    }

    result = GNOME_VFS_RESULT (gp_camera_folder_list_files (camera, path,
                               list, NULL));
    if (result != GNOME_VFS_OK) {
        gnome_vfs_uri_unref (parent);
        unref_camera (camera);
        gp_list_free (list);
        g_free (basename);
        G_UNLOCK (cameras);
        return (result);
    }

    count = gp_list_count (list);
    if (count < 0) {
        gnome_vfs_uri_unref (parent);
        unref_camera (camera);
        gp_list_free (list);
        g_free (basename);
        G_UNLOCK (cameras);
        return (GNOME_VFS_RESULT (count));
    }

    for (i = 0; i < count; i++) {
        result = GNOME_VFS_RESULT (gp_list_get_name (list, i, &name));
        if (result != GNOME_VFS_OK) {
            gnome_vfs_uri_unref (parent);
            unref_camera (camera);
            gp_list_free (list);
            g_free (basename);
            G_UNLOCK (cameras);
            return (result);
        }
        if (!strcmp (name, basename)) {
            result = GNOME_VFS_RESULT (gp_camera_file_get_info (
                                           camera, path, basename, &camera_info,
                                           NULL));
            g_free (basename);
            unref_camera (camera);
            gp_list_free (list);
            if (result != GNOME_VFS_OK) {
                G_UNLOCK (cameras);
                return (result);
            }
            get_info_from_camera_info (&camera_info, FALSE, info);
            gnome_vfs_uri_unref (parent);
            G_UNLOCK (cameras);
            printf ("Found file!\n");
            return (GNOME_VFS_OK);
        }
    }

    gnome_vfs_uri_unref (parent);
    g_free (basename);
    unref_camera (camera);
    gp_list_free (list);

    G_UNLOCK (cameras);

    return (GNOME_VFS_ERROR_NOT_FOUND);
}