From 9dd5dc2a1e772ce2cf7d7cb49d21cb53dffafaed Mon Sep 17 00:00:00 2001 From: Marcus Meissner Date: Sun, 21 Sep 2008 19:38:27 +0000 Subject: [PATCH] added a sample framework git-svn-id: https://svn.code.sf.net/p/gphoto/code/trunk/libgphoto2@11341 67ed7778-7388-44ab-90cf-0a291f65f57c --- examples/Makefile.am | 101 +++++++++++++++++++++++++++++++++++ examples/README | 11 ++++ examples/autodetect.c | 91 +++++++++++++++++++++++++++++++ examples/context.c | 41 ++++++++++++++ examples/sample-autodetect.c | 43 +++++++++++++++ examples/samples.h | 8 +++ 6 files changed, 295 insertions(+) create mode 100644 examples/Makefile.am create mode 100644 examples/README create mode 100644 examples/autodetect.c create mode 100644 examples/context.c create mode 100644 examples/sample-autodetect.c create mode 100644 examples/samples.h diff --git a/examples/Makefile.am b/examples/Makefile.am new file mode 100644 index 000000000..3da3ea9e2 --- /dev/null +++ b/examples/Makefile.am @@ -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 diff --git a/examples/README b/examples/README new file mode 100644 index 000000000..1b85fabd7 --- /dev/null +++ b/examples/README @@ -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 diff --git a/examples/autodetect.c b/examples/autodetect.c new file mode 100644 index 000000000..0cc93c920 --- /dev/null +++ b/examples/autodetect.c @@ -0,0 +1,91 @@ +#include +#include + +#include + +#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 +#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; +} diff --git a/examples/sample-autodetect.c b/examples/sample-autodetect.c new file mode 100644 index 000000000..66e57f25d --- /dev/null +++ b/examples/sample-autodetect.c @@ -0,0 +1,43 @@ +#include +#include + + +#include + +#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; +} diff --git a/examples/samples.h b/examples/samples.h new file mode 100644 index 000000000..8d80881b5 --- /dev/null +++ b/examples/samples.h @@ -0,0 +1,8 @@ +#ifndef __SAMPLES_H +#define __SAMPLES_H +#include + +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