git.jasLogic.tech
- added uninit function to photobox_photo
authorjasLogic <jaslo@jaslogic.tech>
Mon, 19 Aug 2019 10:28:10 +0000 (12:28 +0200)
committerjasLogic <jaslo@jaslogic.tech>
Mon, 19 Aug 2019 10:28:10 +0000 (12:28 +0200)
- added error logging function
- much better error handling in photobox_photo.c

Makefile
src/photobox_photo.c
src/photobox_photo.h

index a92a999b59857d0ab958e484ecc63a9cfd415e30..151fd1e5d638b927be32a81baf46152ca38039cb 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -7,7 +7,7 @@ SRCDIR=src
 SRCS=$(wildcard $(SRCDIR)/*.c)
 OBJS=$(SRCS:.c=.o)
 
-LIBS=-lgphoto2 -lqrencode -lmipea `pkg-config --cflags --libs gtk+-3.0` `curl-config --cflags --libs`
+LIBS=-lgphoto2 -lgphoto2_port -lqrencode -lmipea `pkg-config --cflags --libs gtk+-3.0` `curl-config --cflags --libs`
 
 %.o: %.c
        $(CC) -c -o $@ $< $(CFLAGS) $(LIBS)
index 8d8bee41a57277ebf057c0f9baa13a448cdae0fd..87bc9ded8bfa40e79eaca629738abe3e9c140a9e 100644 (file)
 #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;
 
-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);
-    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;
 }
index 2e4066e2f3214979cb862edd15561c9be6ee209c..11c9147c3bf540d74ab11a58b74f404af966bee4 100644 (file)
@@ -7,6 +7,7 @@ typedef struct pb_ph_buffer {
 } pb_ph_buffer;
 
 int pb_ph_init(void);
+int pb_ph_uninit(void);
 int pb_ph_capture(pb_ph_buffer *buf);
 int pb_ph_capture_file(const char *fn);