added a sample framework

git-svn-id: https://svn.code.sf.net/p/gphoto/code/trunk/libgphoto2@11341 67ed7778-7388-44ab-90cf-0a291f65f57c
This commit is contained in:
Marcus Meissner
2008-09-21 19:38:27 +00:00
parent 95f014533c
commit 9dd5dc2a1e
6 changed files with 295 additions and 0 deletions

101
examples/Makefile.am Normal file
View File

@@ -0,0 +1,101 @@
EXTRA_DIST = README
EXTRA_PROGRAMS =
check_PROGRAMS =
check_SCRIPTS =
TESTS =
INSTALL_TESTS =
noinst_PROGRAMS =
# Now that we build all the camlibs in one directory, we can run our checks
# with CAMLIBS set to the camlib build directory.
TESTS_ENVIRONMENT = env \
CAMLIBS="$(top_builddir)/camlibs"
# After installation, this will be CAMLIBS = $(DESTDIR)$(camlibdir)
INSTALL_TESTS_ENVIRONMENT = env \
CAMLIBS="$(DESTDIR)$(camlibdir)" \
LD_LIBRARY_PATH="$(DESTDIR)$(libdir)$${LD_LIBRARY_PATH+:$${LD_LIBRARY_PATH}}"
CLEANFILES = $(check_SCRIPTS)
noinst_PROGRAMS += sample-autodetect
sample_autodetect_SOURCES = sample-autodetect.c samples.h autodetect.c context.c
sample_autodetect_LDADD = \
$(top_builddir)/libgphoto2/libgphoto2.la \
$(top_builddir)/libgphoto2_port/libgphoto2_port/libgphoto2_port.la \
$(LIBLTDL) \
$(LIBEXIF_LIBS) \
$(INTLLIBS)
#noinst_PROGRAMS += test-filesys
#test_filesys_SOURCE = test-filesys.c
#test_filesys_LDADD = \
# $(top_builddir)/libgphoto2/libgphoto2.la \
# $(top_builddir)/libgphoto2_port/libgphoto2_port/libgphoto2_port.la \
# $(LIBLTDL) \
# $(LIBEXIF_LIBS) \
# $(INTLLIBS)
#TESTS += test-camera-list
#INSTALL_TESTS += test-camera-list
#check_PROGRAMS += test-camera-list
#test_camera_list_SOURCE = test-camera-list.c
#test_camera_list_LDADD = \
# $(top_builddir)/libgphoto2/libgphoto2.la \
# $(top_builddir)/libgphoto2_port/libgphoto2_port/libgphoto2_port.la \
# $(LIBLTDL) \
# $(LIBEXIF_LIBS) \
# $(INTLLIBS)
#if HAVE_GCC
#PEDANTIC_CFLAGS = -std=c99 -pedantic-errors -W -Wall -Wextra -Werror
#PEDANTIC_CXXFLAGS = -std=c++98 -pedantic-errors -W -Wall -Wextra -Werror
#else
#PEDANTIC_CFLAGS =
#PEDANTIC_CXXFLAGS =
#endif
#
#TESTS += test-pedantic-c
#check_PROGRAMS += test-pedantic-c
#test_pedantic_c_SOURCES = test-pedantic-compilation.c
#test_pedantic_c_CPPFLAGS = \
# $(AM_CPPFLAGS) $(CPPFLAGS) \
# -U_GPHOTO2_INTERNAL_CODE
#test_pedantic_c_CFLAGS = \
# $(AM_CFLAGS) $(PEDANTIC_CFLAGS) $(CFLAGS) \
# -U_GPHOTO2_INTERNAL_CODE
#test_pedantic_c_LDADD = \
# $(top_builddir)/libgphoto2/libgphoto2.la \
# $(top_builddir)/libgphoto2_port/libgphoto2_port/libgphoto2_port.la \
# $(LIBLTDL) \
# $(LIBEXIF_LIBS) \
# $(INTLLIBS)
#if HAVE_CXX
#check_PROGRAMS += test-pedantic-cxx
#TESTS += test-pedantic-cxx
#else
#EXTRA_PROGRAMS += test-pedantic-cxx
#endif
#test_pedantic_cxx_SOURCES = test-pedantic-compilation.cxx
#test_pedantic_cxx_CPPFLAGS = \
# $(AM_CPPFLAGS) $(CPPFLAGS) \
# -U_GPHOTO2_INTERNAL_CODE
#test_pedantic_cxx_CXXFLAGS = \
# $(AM_CXXFLAGS) $(PEDANTIC_CXXFLAGS) $(CXXFLAGS) \
# -U_GPHOTO2_INTERNAL_CODE
#test_pedantic_cxx_LDADD = \
# $(top_builddir)/libgphoto2/libgphoto2.la \
# $(top_builddir)/libgphoto2_port/libgphoto2_port/libgphoto2_port.la \
# $(LIBLTDL) \
# $(LIBEXIF_LIBS) \
# $(INTLLIBS)
include $(top_srcdir)/installcheck.mk

11
examples/README Normal file
View File

@@ -0,0 +1,11 @@
Sample snippets for programmatic camera usage.
This directory contains sample snippets for your C / C++ program accessing
cameras for various purposes.
If you find a common thing to do missing, please report them (or do them
yourselves and send patches).
Please do not keep it for yourself.
Marcus Meissner <marcus@jet.franken.de>

91
examples/autodetect.c Normal file
View File

@@ -0,0 +1,91 @@
#include <stdio.h>
#include <string.h>
#include <gphoto2/gphoto2-camera.h>
#include "samples.h"
static GPPortInfoList *portinfolist = NULL;
static CameraAbilitiesList *abilities = NULL;
/*
* This detects all currently attached cameras.
*/
int
sample_autodetect (CameraList *list, GPContext *context) {
int ret, i;
CameraList *xlist = NULL;
ret = gp_list_new (&xlist);
if (ret < GP_OK) goto out;
if (!portinfolist) {
ret = gp_port_info_list_new (&portinfolist);
if (ret < GP_OK) goto out;
ret = gp_port_info_list_load (portinfolist);
if (ret < 0) goto out;
ret = gp_port_info_list_count (portinfolist);
if (ret < 0) goto out;
}
ret = gp_abilities_list_new (&abilities);
if (ret < GP_OK) goto out;
ret = gp_abilities_list_load (abilities, context);
if (ret < GP_OK) goto out;
ret = gp_abilities_list_detect (abilities, portinfolist, xlist, context);
if (ret < GP_OK) goto out;
ret = gp_list_count (xlist);
if (ret < GP_OK) goto out;
for (i=0;i<ret;i++) {
const char *name, *value;
gp_list_get_name (xlist, i, &name);
gp_list_get_value (xlist, i, &value);
if (!strcmp ("usb:",value)) continue;
gp_list_append (list, name, value);
}
out:
gp_list_free (xlist);
return gp_list_count(list);
}
int
sample_open_camera (Camera ** camera, const char *model, const char *port) {
int ret, m, p;
CameraAbilities a;
GPPortInfo pi;
ret = gp_camera_new (camera);
if (ret < GP_OK) return ret;
/* First the model / driver */
m = gp_abilities_list_lookup_model (abilities, model);
if (m < GP_OK) return ret;
ret = gp_abilities_list_get_abilities (abilities, m, &a);
if (ret < GP_OK) return ret;
ret = gp_camera_set_abilities (*camera, a);
if (ret < GP_OK) return ret;
/* Then associate with a port */
p = gp_port_info_list_lookup_path (portinfolist, port);
if (ret < GP_OK) return ret;
switch (p) {
case GP_ERROR_UNKNOWN_PORT:
fprintf (stderr, "The port you specified "
"('%s') can not be found. Please "
"specify one of the ports found by "
"'gphoto2 --list-ports' and make "
"sure the spelling is correct "
"(i.e. with prefix 'serial:' or 'usb:').",
port);
break;
default:
break;
}
if (ret < GP_OK) return ret;
ret = gp_port_info_list_get_info (portinfolist, p, &pi);
if (ret < GP_OK) return ret;
ret = gp_camera_set_port_info (*camera, pi);
if (ret < GP_OK) return ret;
return GP_OK;
}

41
examples/context.c Normal file
View File

@@ -0,0 +1,41 @@
#include <stdio.h>
#include "samples.h"
static void
ctx_error_func (GPContext *context, const char *format, va_list args, void *data)
{
fprintf (stderr, "\n");
fprintf (stderr, "*** Contexterror *** \n");
vfprintf (stderr, format, args);
fprintf (stderr, "\n");
fflush (stderr);
}
static void
ctx_status_func (GPContext *context, const char *format, va_list args, void *data)
{
vfprintf (stderr, format, args);
fprintf (stderr, "\n");
fflush (stderr);
}
GPContext* sample_create_context() {
GPContext *context;
/* This is the mandatory part */
context = gp_context_new();
/* All the parts below are optional! */
gp_context_set_error_func (context, ctx_error_func, NULL);
gp_context_set_status_func (context, ctx_status_func, NULL);
/* also:
gp_context_set_cancel_func (p->context, ctx_cancel_func, p);
gp_context_set_message_func (p->context, ctx_message_func, p);
if (isatty (STDOUT_FILENO))
gp_context_set_progress_funcs (p->context,
ctx_progress_start_func, ctx_progress_update_func,
ctx_progress_stop_func, p);
*/
return context;
}

View File

@@ -0,0 +1,43 @@
#include <stdio.h>
#include <string.h>
#include <gphoto2/gphoto2-camera.h>
#include "samples.h"
int main(int argc, char **argv) {
CameraList *list;
Camera **cams;
int ret, i, count;
const char *name, *value;
GPContext *context;
context = sample_create_context ();
ret = gp_list_new (&list);
if (ret < GP_OK) return 1;
count = sample_autodetect (list, context);
printf("Number of cameras: %d\n", count);
cams = calloc (sizeof (Camera*),count);
for (i = 0; i < count; i++) {
gp_list_get_name (list, i, &name);
gp_list_get_value (list, i, &value);
ret = sample_open_camera (&cams[i], name, value);
if (ret < GP_OK) fprintf(stderr,"Camera %s on port %s failed to open\n", name, value);
}
for (i = 0; i < count; i++) {
CameraText text;
ret = gp_camera_get_summary (cams[i], &text, context);
if (ret < GP_OK) {
fprintf (stderr, "Failed to get summary.\n");
continue;
}
gp_list_get_name (list, i, &name);
gp_list_get_value (list, i, &value);
printf("%-30s %-16s\n", name, value);
printf("Summary:\n%s\n", text.text);
}
return 0;
}

8
examples/samples.h Normal file
View File

@@ -0,0 +1,8 @@
#ifndef __SAMPLES_H
#define __SAMPLES_H
#include <gphoto2/gphoto2-camera.h>
extern int sample_autodetect (CameraList *list, GPContext *context);
extern int sample_open_camera (Camera ** camera, const char *model, const char *port);
extern GPContext* sample_create_context(void);
#endif