From: jasLogic Date: Mon, 19 Aug 2019 10:28:10 +0000 (+0200) Subject: - added uninit function to photobox_photo X-Git-Url: https://git.jaslogic.tech/photobox.git/commitdiff_plain/12b752365988978d0398a0b8852d60c1f81a9c49?ds=inline - added uninit function to photobox_photo - added error logging function - much better error handling in photobox_photo.c --- diff --git a/Makefile b/Makefile index a92a999..151fd1e 100644 --- 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) diff --git a/src/photobox_photo.c b/src/photobox_photo.c index 8d8bee4..87bc9de 100644 --- a/src/photobox_photo.c +++ b/src/photobox_photo.c @@ -9,115 +9,133 @@ #include #include -#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; } diff --git a/src/photobox_photo.h b/src/photobox_photo.h index 2e4066e..11c9147 100644 --- a/src/photobox_photo.h +++ b/src/photobox_photo.h @@ -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);