git.jasLogic.tech
- added uninit function to photobox_photo
[photobox.git] / src / photobox_photo.c
index 8d8bee41a57277ebf057c0f9baa13a448cdae0fd..87bc9ded8bfa40e79eaca629738abe3e9c140a9e 100644 (file)
 #include <gphoto2/gphoto2.h>
 #include <gphoto2/gphoto2-camera.h>
 
 #include <gphoto2/gphoto2.h>
 #include <gphoto2/gphoto2-camera.h>
 
-#define perror_inf()   fprintf(stderr, "%s:%d: In function %s:\n", __FILE__,  \
-    __LINE__, __func__)
-
 static Camera *camera;
 static GPContext *context;
 
 static Camera *camera;
 static GPContext *context;
 
-int pb_ph_init(void) {
-    context = gp_context_new();
-    gp_camera_new(&camera);
+void dumperror(GPLogLevel level, const char *domain, const char *str, void *data)
+{
+        // suppress unused parameter errors (ugly):
+        (void) level;
+        (void) data;
+
+        fprintf(stderr, "[Error Log] %s: %s\n", domain, str);
+}
+
+int pb_ph_init(void)
+{
+        int ret;
+
+
+        ret = gp_log_add_func(GP_LOG_ERROR, (GPLogFunc) dumperror, 0);
+        if (ret < GP_OK) {
+                perror("Failed to add logging function");
+                return ret;
+        }
+
+
+        context = gp_context_new();
+
+        ret = gp_camera_new(&camera);
+        if (ret != GP_OK)
+                goto error;
 
 
-    int ret = gp_camera_init(camera, context);
-    if (ret != GP_OK) {
-        perror_inf();
-        perror("Failed to initialize camera");
-        return -1;
-    }
+        ret = gp_camera_init(camera, context);
+        if (ret != GP_OK)
+                goto error;
 
 
-    return 0;
+        return 0;
+
+        error:
+        gp_context_unref(context);
+        return ret;
 }
 }
+int pb_ph_uninit(void)
+{
+        int ret;
 
 
-int pb_ph_capture(pb_ph_buffer *buf) {
-    int ret;
-    CameraFile *file;
-    CameraFilePath camera_file_path;
-
-    ret = gp_camera_capture(camera, GP_CAPTURE_IMAGE, &camera_file_path, context);
-    if (ret != 0) {
-        perror_inf();
-        perror("Failed to capture image");
-        return -1;
-    }
-
-    ret = gp_file_new(&file);
-    if (ret != 0) {
-        perror_inf();
-        perror("Failed to create new file");
-        return -1;
-    }
-
-    ret = gp_camera_file_get(camera, camera_file_path.folder, camera_file_path.name, GP_FILE_TYPE_NORMAL,
-        file, context);
-    if (ret != 0) {
-        perror_inf();
-        perror("Failed to get image");
-        return -1;
-    }
-
-    ret = gp_camera_file_delete(camera, camera_file_path.folder, camera_file_path.name, context);
-    if (ret != 0) {
-        perror_inf();
-        perror("Failed to delete image from camera");
-        return -1;
-    }
-
-    gp_file_get_data_and_size(file, (const char **)&buf->base, &buf->size);
-    if (ret != 0) {
-        perror_inf();
-        perror("Failed to get data and size");
-        return -1;
-    }
-
-    return 0;
+        ret = gp_camera_exit(camera, context);
+        gp_context_unref(context);
+
+        return ret;
 }
 
 }
 
-int pb_ph_capture_file(const char *fn) {
-    int ret, fd;
-    CameraFile *file;
-    CameraFilePath camera_file_path;
-
-    ret = gp_camera_capture(camera, GP_CAPTURE_IMAGE, &camera_file_path, context);
-    if (ret != 0) {
-        perror_inf();
-        perror("Failed to capture image");
-        return -1;
-    }
-
-    fd = open(fn, O_CREAT | O_WRONLY, 0644);
-    if (fd == -1) {
-        perror_inf();
-        perror("Failed to open file");
-        return -1;
-    }
-
-    ret = gp_file_new_from_fd(&file, fd);
-    if (ret != 0) {
-        perror_inf();
-        perror("Failed to create image file");
-        return -1;
-    }
-
-    ret = gp_camera_file_get(camera, camera_file_path.folder, camera_file_path.name, GP_FILE_TYPE_NORMAL,
+// unused, unmaintained:
+/*
+int pb_ph_capture(pb_ph_buffer *buf)
+{
+        int ret;
+        CameraFile *file;
+        CameraFilePath camera_file_path;
+
+        ret = gp_camera_capture(camera, GP_CAPTURE_IMAGE, &camera_file_path, context);
+        if (ret != GP_OK) {
+                perror_inf("Failed to capture image");
+                return ret;
+        }
+
+        ret = gp_file_new(&file);
+        if (ret != 0) {
+                perror_inf("Failed to create new file");
+                return ret;
+        }
+
+        ret = gp_camera_file_get(camera, camera_file_path.folder, camera_file_path.name, GP_FILE_TYPE_NORMAL,
         file, context);
         file, context);
-    if (ret != 0) {
-        perror_inf();
-        perror("Failed to get image");
-        return -1;
-    }
+        if (ret != 0) {
+                perror_inf("Failed to get image");
+                return ret;
+        }
+
+        ret = gp_camera_file_delete(camera, camera_file_path.folder, camera_file_path.name, context);
+        if (ret != 0) {
+                perror_inf("Failed to delete image from camera");
+                return ret;
+        }
+
+        gp_file_get_data_and_size(file, (const char **)&buf->base, &buf->size);
+        if (ret != 0) {
+                perror_inf("Failed to get data and size");
+                return ret;
+        }
+
+        return 0;
+}
+*/
+
+int pb_ph_capture_file(const char *fn)
+{
+        int ret, fd;
+        CameraFile *file;
+        CameraFilePath camera_file_path;
+
+        ret = gp_camera_capture(camera, GP_CAPTURE_IMAGE, &camera_file_path, context);
+        if (ret != GP_OK)
+                return ret;
+
+        fd = open(fn, O_CREAT | O_WRONLY, 0644);
+        if (fd == -1) {
+                ret = GP_ERROR;
+                perror("Failed to open/create file");
+                goto error_delete;
+        }
+
+        ret = gp_file_new_from_fd(&file, fd);
+        if (ret != GP_OK)
+                goto error_fd;
+
+        //
+        ret = gp_camera_file_get(camera, camera_file_path.folder,
+                camera_file_path.name, GP_FILE_TYPE_NORMAL, file, context);
 
 
-    gp_file_free(file);
+        gp_file_free(file);
 
 
-    ret = gp_camera_file_delete(camera, camera_file_path.folder, camera_file_path.name, context);
-    if (ret != 0) {
-        perror_inf();
-        perror("Failed to delete image from camera");
-        return -1;
-    }
+        error_fd:
+        // close fd? done in gphoto?
+        close(fd);
 
 
-    // close fd? done in gphoto?
-    close(fd);
+        error_delete:
+        ret = gp_camera_file_delete(camera, camera_file_path.folder,
+                camera_file_path.name, context);
 
 
-    return 0;
+        return ret;
 }
 }