summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarcus Meissner <marcus@jet.franken.de>2013-01-09 18:53:14 +0000
committerMarcus Meissner <marcus@jet.franken.de>2013-01-09 18:53:14 +0000
commit8f84b9c22ba5eff163b11a42b1323ad50a296417 (patch)
treee012299e42dac490ff954740650d4e4134a526d6
parentabb3cb7837fc0837a04f2ab7dcceec3b1f65b880 (diff)
downloadlibgphoto2-8f84b9c22ba5eff163b11a42b1323ad50a296417.tar.gz
added a focus sample
git-svn-id: https://svn.code.sf.net/p/gphoto/code/trunk/libgphoto2@14172 67ed7778-7388-44ab-90cf-0a291f65f57c
-rw-r--r--examples/focus.c140
1 files changed, 140 insertions, 0 deletions
diff --git a/examples/focus.c b/examples/focus.c
new file mode 100644
index 000000000..953c55e1d
--- /dev/null
+++ b/examples/focus.c
@@ -0,0 +1,140 @@
+#include "samples.h"
+
+#include <string.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+/*
+ * This function looks up a label or key entry of
+ * a configuration widget.
+ * The functions descend recursively, so you can just
+ * specify the last component.
+ */
+
+static int
+_lookup_widget(CameraWidget*widget, const char *key, CameraWidget **child) {
+ int ret;
+ ret = gp_widget_get_child_by_name (widget, key, child);
+ if (ret < GP_OK)
+ ret = gp_widget_get_child_by_label (widget, key, child);
+ return ret;
+}
+
+/* calls the Nikon DSLR or Canon DSLR autofocus method. */
+int
+camera_auto_focus(Camera *camera, GPContext *context) {
+ CameraWidget *widget = NULL, *child = NULL;
+ CameraWidgetType type;
+ int ret,val;
+
+ ret = gp_camera_get_config (camera, &widget, context);
+ if (ret < GP_OK) {
+ fprintf (stderr, "camera_get_config failed: %d\n", ret);
+ return ret;
+ }
+ ret = _lookup_widget (widget, "autofocusdrive", &child);
+ if (ret < GP_OK) {
+ fprintf (stderr, "lookup 'autofocusdrive' failed: %d\n", ret);
+ goto out;
+ }
+
+ /* check that this is a toggle */
+ ret = gp_widget_get_type (child, &type);
+ if (ret < GP_OK) {
+ fprintf (stderr, "widget get type failed: %d\n", ret);
+ goto out;
+ }
+ switch (type) {
+ case GP_WIDGET_TOGGLE:
+ break;
+ default:
+ fprintf (stderr, "widget has bad type %d\n", type);
+ ret = GP_ERROR_BAD_PARAMETERS;
+ goto out;
+ }
+
+ ret = gp_widget_get_value (child, &val);
+ if (ret < GP_OK) {
+ fprintf (stderr, "could not get widget value: %d\n", ret);
+ goto out;
+ }
+ val++;
+ ret = gp_widget_set_value (child, &val);
+ if (ret < GP_OK) {
+ fprintf (stderr, "could not set widget value to 1: %d\n", ret);
+ goto out;
+ }
+
+ ret = gp_camera_get_config (camera, &widget, context);
+ if (ret < GP_OK) {
+ fprintf (stderr, "could not set config tree to autofocus: %d\n", ret);
+ goto out;
+ }
+out:
+ gp_widget_free (widget);
+ return ret;
+}
+
+
+/* Sets a string configuration value.
+ * This can set for:
+ * - A Text widget
+ * - The current selection of a Radio Button choice
+ * - The current selection of a Menu choice
+ *
+ * Sample (for Canons eg):
+ * get_config_value_string (camera, "owner", &ownerstr, context);
+ */
+int
+_set_config_value_string (Camera *camera, const char *key, const char *val, GPContext *context) {
+ CameraWidget *widget = NULL, *child = NULL;
+ CameraWidgetType type;
+ int ret;
+
+ ret = gp_camera_get_config (camera, &widget, context);
+ if (ret < GP_OK) {
+ fprintf (stderr, "camera_get_config failed: %d\n", ret);
+ return ret;
+ }
+ ret = _lookup_widget (widget, key, &child);
+ if (ret < GP_OK) {
+ fprintf (stderr, "lookup widget failed: %d\n", ret);
+ goto out;
+ }
+
+ /* This type check is optional, if you know what type the label
+ * has already. If you are not sure, better check. */
+ ret = gp_widget_get_type (child, &type);
+ if (ret < GP_OK) {
+ fprintf (stderr, "widget get type failed: %d\n", ret);
+ goto out;
+ }
+ switch (type) {
+ case GP_WIDGET_MENU:
+ case GP_WIDGET_RADIO:
+ case GP_WIDGET_TEXT:
+ break;
+ default:
+ fprintf (stderr, "widget has bad type %d\n", type);
+ ret = GP_ERROR_BAD_PARAMETERS;
+ goto out;
+ }
+
+ /* This is the actual set call. Note that we keep
+ * ownership of the string and have to free it if necessary.
+ */
+ ret = gp_widget_set_value (child, val);
+ if (ret < GP_OK) {
+ fprintf (stderr, "could not set widget value: %d\n", ret);
+ goto out;
+ }
+ /* This stores it on the camera again */
+ ret = gp_camera_set_config (camera, widget, context);
+ if (ret < GP_OK) {
+ fprintf (stderr, "camera_set_config failed: %d\n", ret);
+ return ret;
+ }
+out:
+ gp_widget_free (widget);
+ return ret;
+}