diff options
author | Marcus Meissner <marcus@jet.franken.de> | 2008-10-12 19:43:22 +0000 |
---|---|---|
committer | Marcus Meissner <marcus@jet.franken.de> | 2008-10-12 19:43:22 +0000 |
commit | 75172742469926ab0ce317030bba33504c139e1a (patch) | |
tree | d0779377acba60edefbe7a47faa15de6e3012058 /examples | |
parent | 80c1c8f109d5bae0efcf9039dcfade6961e5caa6 (diff) | |
download | libgphoto2-75172742469926ab0ce317030bba33504c139e1a.tar.gz |
added
git-svn-id: https://svn.code.sf.net/p/gphoto/code/trunk/libgphoto2@11376 67ed7778-7388-44ab-90cf-0a291f65f57c
Diffstat (limited to 'examples')
-rw-r--r-- | examples/config.c | 198 | ||||
-rw-r--r-- | examples/sample-multi-detect.c | 64 | ||||
-rw-r--r-- | examples/sample-owner.c | 57 |
3 files changed, 319 insertions, 0 deletions
diff --git a/examples/config.c b/examples/config.c new file mode 100644 index 000000000..828a00524 --- /dev/null +++ b/examples/config.c @@ -0,0 +1,198 @@ +#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; +} + +/* Gets a string configuration value. + * This can be: + * - 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 +get_config_value_string (Camera *camera, const char *key, char **str, GPContext *context) { + CameraWidget *widget = NULL, *child = NULL; + CameraWidgetType type; + int ret; + char *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, 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 query call. Note that we just + * a pointer reference to the string, not a copy... */ + ret = gp_widget_get_value (child, &val); + if (ret < GP_OK) { + fprintf (stderr, "could not query widget value: %d\n", ret); + goto out; + } + /* Create a new copy for our caller. */ + *str = strdup (val); +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; +} + + +/* + * This enables/disables the specific canon capture mode. + * + * For non canons this is not required, and will just return + * with an error (but without negative effects). + */ +int +canon_enable_capture (Camera *camera, int onoff, 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, "capture", &child); + if (ret < GP_OK) { + /*fprintf (stderr, "lookup widget failed: %d\n", ret);*/ + goto out; + } + + 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; + } + /* Now set the toggle to the wanted value */ + ret = gp_widget_set_value (child, &onoff); + if (ret < GP_OK) { + fprintf (stderr, "toggling Canon capture to %d failed with %d\n", onoff, ret); + goto out; + } + /* OK */ + 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; +} diff --git a/examples/sample-multi-detect.c b/examples/sample-multi-detect.c new file mode 100644 index 000000000..e738defc6 --- /dev/null +++ b/examples/sample-multi-detect.c @@ -0,0 +1,64 @@ +#include <stdio.h> +#include <string.h> +#include <stdlib.h> + +#include <gphoto2/gphoto2-camera.h> + +#include "samples.h" + +/* Sample autodetection program. + * + * This program can autodetect multiple cameras and then calls a + * simple function in each of them (summary). + */ + +int main(int argc, char **argv) { + CameraList *list; + Camera **cams; + int ret, i, count; + const char *name, *value; + GPContext *context; + + context = sample_create_context (); /* see context.c */ + + /* Detect all the cameras that can be autodetected... */ + ret = gp_list_new (&list); + if (ret < GP_OK) return 1; + count = sample_autodetect (list, context); + + /* Now open all cameras we autodected for usage */ + 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); + } + /* Now call a simple function in each of those cameras. */ + for (i = 0; i < count; i++) { + CameraText text; + char *owner; + 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); + + /* Query a simple string configuration variable. */ + ret = get_config_value_string (cams[i], "owner", &owner, context); + if (ret >= GP_OK) { + printf("Owner: %s\n", owner); + free (owner); + } else { + printf("Owner: No owner found.\n"); + } + + } + return 0; +} diff --git a/examples/sample-owner.c b/examples/sample-owner.c new file mode 100644 index 000000000..d14d07201 --- /dev/null +++ b/examples/sample-owner.c @@ -0,0 +1,57 @@ +#include <stdio.h> +#include <string.h> +#include <stdlib.h> + +#include <gphoto2/gphoto2-camera.h> + +#include "samples.h" + +/* Set the owner of the first camera. + * + * This program can autodetect a single camera and then sets its + * owner to the string passed on the cmdline. + * + * Same as: + * gphoto2 --get-config owner + * gphoto2 --set-config owner="Owner Name" + */ + +int main(int argc, char **argv) { + Camera *camera; + int ret; + char *owner; + GPContext *context; + + context = sample_create_context (); /* see context.c */ + gp_camera_new (&camera); + + /* This call will autodetect cameras, take the + * first one from the list and use it. It will ignore + * any others... See the *multi* examples on how to + * detect and use more than the first one. + */ + ret = gp_camera_init (camera, context); + if (ret < GP_OK) { + printf("No camera auto detected.\n"); + gp_camera_free (camera); + return 0; + } + + ret = get_config_value_string (camera, "owner", &owner, context); + if (ret < GP_OK) { + printf ("Could not query owner.\n"); + goto out; + } + printf("Current owner: %s\n", owner); + if (argc > 1) { + ret = set_config_value_string (camera, "owner", argv[1], context); + if (ret < GP_OK) { + fprintf (stderr, "Failed to set camera owner to %s; %d\n", argv[1], ret); + } else + printf("New owner: %s\n", argv[1]); + } +out: + gp_camera_exit (camera, context); + gp_camera_free (camera); + return 0; +} |