1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
use std::borrow::Cow;
use std::ffi::CStr;
use std::mem;
use ::context::Context;
use ::abilities::Abilities;
use ::media::Media;
use ::port::Port;
use ::storage::Storage;
use ::handle::prelude::*;
pub struct Camera {
camera: *mut ::gphoto2::Camera,
}
impl Drop for Camera {
fn drop(&mut self) {
unsafe {
::gphoto2::gp_camera_unref(self.camera);
}
}
}
impl Camera {
pub fn autodetect(context: &mut Context) -> ::Result<Self> {
let mut ptr = unsafe { mem::uninitialized() };
try_unsafe!(::gphoto2::gp_camera_new(&mut ptr));
let camera = Camera { camera: ptr };
try_unsafe!(::gphoto2::gp_camera_init(camera.camera, context.as_mut_ptr()));
Ok(camera)
}
pub fn capture_image(&mut self, context: &mut Context) -> ::Result<CameraFile> {
let mut file_path = unsafe { mem::uninitialized() };
try_unsafe! {
::gphoto2::gp_camera_capture(self.camera,
::gphoto2::GP_CAPTURE_IMAGE,
&mut file_path,
context.as_mut_ptr())
};
Ok(CameraFile { inner: file_path })
}
pub fn download<T: Media>(&mut self, context: &mut Context, source: &CameraFile, destination: &mut T) -> ::Result<()> {
try_unsafe! {
::gphoto2::gp_camera_file_get(self.camera,
source.inner.folder.as_ptr(),
source.inner.name.as_ptr(),
::gphoto2::GP_FILE_TYPE_NORMAL,
destination.as_mut_ptr(),
context.as_mut_ptr())
};
Ok(())
}
pub fn port<'a>(&'a self) -> Port<'a> {
let mut ptr = unsafe { mem::uninitialized() };
unsafe {
assert_eq!(::gphoto2::GP_OK, ::gphoto2::gp_camera_get_port_info(self.camera, &mut ptr));
}
::port::from_libgphoto2(self, ptr)
}
pub fn abilities(&self) -> Abilities {
let mut abilities = unsafe { mem::uninitialized() };
unsafe {
assert_eq!(::gphoto2::GP_OK, ::gphoto2::gp_camera_get_abilities(self.camera, &mut abilities));
}
::abilities::from_libgphoto2(abilities)
}
pub fn storage(&mut self, context: &mut Context) -> ::Result<Vec<Storage>> {
let mut ptr = unsafe { mem::uninitialized() };
let mut len = unsafe { mem::uninitialized() };
try_unsafe! {
::gphoto2::gp_camera_get_storageinfo(self.camera,
&mut ptr,
&mut len,
context.as_mut_ptr())
};
let storage = ptr as *mut Storage;
let length = len as usize;
Ok(unsafe { Vec::from_raw_parts(storage, length, length) })
}
pub fn summary(&mut self, context: &mut Context) -> ::Result<String> {
let mut summary = unsafe { mem::uninitialized() };
try_unsafe!(::gphoto2::gp_camera_get_summary(self.camera, &mut summary, context.as_mut_ptr()));
util::camera_text_to_string(summary)
}
pub fn manual(&mut self, context: &mut Context) -> ::Result<String> {
let mut manual = unsafe { mem::uninitialized() };
try_unsafe!(::gphoto2::gp_camera_get_manual(self.camera, &mut manual, context.as_mut_ptr()));
util::camera_text_to_string(manual)
}
pub fn about_driver(&mut self, context: &mut Context) -> ::Result<String> {
let mut about = unsafe { mem::uninitialized() };
try_unsafe!(::gphoto2::gp_camera_get_about(self.camera, &mut about, context.as_mut_ptr()));
util::camera_text_to_string(about)
}
}
pub struct CameraFile {
inner: ::gphoto2::CameraFilePath,
}
impl CameraFile {
pub fn directory(&self) -> Cow<str> {
unsafe {
String::from_utf8_lossy(CStr::from_ptr(self.inner.folder.as_ptr()).to_bytes())
}
}
pub fn basename(&self) -> Cow<str> {
unsafe {
String::from_utf8_lossy(CStr::from_ptr(self.inner.name.as_ptr()).to_bytes())
}
}
}
mod util {
use std::ffi::CStr;
pub fn camera_text_to_string(mut camera_text: ::gphoto2::CameraText) -> ::Result<String> {
let length = unsafe {
CStr::from_ptr(camera_text.text.as_ptr()).to_bytes().len()
};
let vec = unsafe {
Vec::<u8>::from_raw_parts(camera_text.text.as_mut_ptr() as *mut u8, length, camera_text.text.len())
};
String::from_utf8(vec).map_err(|_| {
::error::from_libgphoto2(::gphoto2::GP_ERROR_CORRUPTED_DATA)
})
}
}