1 // Copyright (C) 2019 Jaslo Ziska
3 #include "photobox_photo.h"
11 #include <gphoto2/gphoto2.h>
12 #include <gphoto2/gphoto2-camera.h>
14 static Camera
*camera
;
15 static GPContext
*context
;
17 static void dumperror(GPLogLevel level
, const char *domain
, const char *str
, void *data
)
19 // suppress unused parameter errors (ugly):
23 fprintf(stderr
, "[Error Log] %s: %s\n", domain
, str
);
31 ret
= gp_log_add_func(GP_LOG_ERROR
, (GPLogFunc
) dumperror
, 0);
33 perror("Failed to add logging function");
38 context
= gp_context_new();
40 ret
= gp_camera_new(&camera
);
44 ret
= gp_camera_init(camera
, context
);
51 gp_context_unref(context
);
54 void pb_ph_uninit(void)
56 gp_camera_exit(camera
, context
);
57 gp_context_unref(context
);
60 // unused, unmaintained:
62 int pb_ph_capture(pb_ph_buffer *buf)
66 CameraFilePath camera_file_path;
68 ret = gp_camera_capture(camera, GP_CAPTURE_IMAGE, &camera_file_path, context);
70 perror_inf("Failed to capture image");
74 ret = gp_file_new(&file);
76 perror_inf("Failed to create new file");
80 ret = gp_camera_file_get(camera, camera_file_path.folder, camera_file_path.name, GP_FILE_TYPE_NORMAL,
83 perror_inf("Failed to get image");
87 ret = gp_camera_file_delete(camera, camera_file_path.folder, camera_file_path.name, context);
89 perror_inf("Failed to delete image from camera");
93 gp_file_get_data_and_size(file, (const char **)&buf->base, &buf->size);
95 perror_inf("Failed to get data and size");
103 int pb_ph_capture_file(const char *fn
)
107 CameraFilePath camera_file_path
;
109 ret
= gp_camera_capture(camera
, GP_CAPTURE_IMAGE
, &camera_file_path
, context
);
113 fd
= open(fn
, O_CREAT
| O_WRONLY
, 0644);
116 perror("Failed to open/create file");
120 ret
= gp_file_new_from_fd(&file
, fd
);
124 ret
= gp_camera_file_get(camera
, camera_file_path
.folder
,
125 camera_file_path
.name
, GP_FILE_TYPE_NORMAL
, file
, context
);
130 // close fd? done in gphoto?
134 ret
= gp_camera_file_delete(camera
, camera_file_path
.folder
,
135 camera_file_path
.name
, context
);