forked from darktable-org/darktable
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcamera_control.c
More file actions
2428 lines (2178 loc) · 82.2 KB
/
camera_control.c
File metadata and controls
2428 lines (2178 loc) · 82.2 KB
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
This file is part of darktable,
Copyright (C) 2010-2025 darktable developers.
darktable is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
darktable is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with darktable. If not, see <http://www.gnu.org/licenses/>.
*/
#include "common/camera_control.h"
#include "common/exif.h"
#include "common/image.h"
#include "control/control.h"
#include "imageio/imageio_jpeg.h"
#include <gphoto2/gphoto2-file.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
/***/
typedef enum _camctl_camera_job_type_t
{
/** Start a scan of devices and announce new and removed. */
_JOB_TYPE_DETECT_DEVICES,
/** Remotly executes a capture. */
_JOB_TYPE_EXECUTE_CAPTURE,
/** Fetch a preview for live view. */
_JOB_TYPE_EXECUTE_LIVE_VIEW,
/** Read a copy of remote camera config into cache. */
_JOB_TYPE_READ_CONFIG,
/** Writes changed properties in cache to camera */
_JOB_TYPE_WRITE_CONFIG,
/** Set's a property in config cache. \todo This shouldn't be a job in jobqueue !? */
_JOB_TYPE_SET_PROPERTY_STRING,
_JOB_TYPE_SET_PROPERTY_TOGGLE,
_JOB_TYPE_SET_PROPERTY_CHOICE,
/** For some reason stopping live view needs to pass an int, not a string. */
_JOB_TYPE_SET_PROPERTY_INT,
_JOB_TYPE_SET_PROPERTY_FLOAT,
/** gets a property from config cache. \todo This shouldn't be a job in jobqueue !? */
_JOB_TYPE_GET_PROPERTY
} _camctl_camera_job_type_t;
typedef struct _camctl_camera_job_t
{
_camctl_camera_job_type_t type;
} _camctl_camera_job_t;
typedef struct _camctl_camera_set_property_string_job_t
{
_camctl_camera_job_type_t type;
char *name;
char *value;
} _camctl_camera_set_property_string_job_t;
typedef struct _camctl_camera_set_property_toggle_job_t
{
_camctl_camera_job_type_t type;
char *name;
} _camctl_camera_set_property_toggle_job_t;
typedef struct _camctl_camera_set_property_choice_job_t
{
_camctl_camera_job_type_t type;
char *name;
int value;
} _camctl_camera_set_property_choice_job_t;
typedef struct _camctl_camera_set_property_int_job_t
{
_camctl_camera_job_type_t type;
char *name;
int value;
} _camctl_camera_set_property_int_job_t;
typedef struct _camctl_camera_set_property_float_job_t
{
_camctl_camera_job_type_t type;
char *name;
float value;
} _camctl_camera_set_property_float_job_t;
/** Initializes camera */
static gboolean _camera_initialize(const dt_camctl_t *c,
dt_camera_t *cam);
/** Poll camera events, this one is called from the thread handling the camera. */
static void _camera_poll_events(const dt_camctl_t *c,
const dt_camera_t *cam);
/** Lock camera control and notify listener. \note Locks mutex and
* signals CAMERA_CONTROL_BUSY. \remarks all interface functions
* available to host application should lock/unlock its operation. */
static void _camctl_lock(const dt_camctl_t *c,
const dt_camera_t *cam);
/** Lock camera control and notify listener. \note Locks mutex and
* signals CAMERA_CONTROL_AVAILABLE. \see _camctl_lock() */
static void _camctl_unlock(const dt_camctl_t *c);
/** Updates the cached configuration with a copy of camera configuration */
static void _camera_configuration_update(const dt_camctl_t *c,
const dt_camera_t *camera);
static void _camera_configuration_single_update(const dt_camctl_t *c,
const dt_camera_t *camera,
const char* name);
/** Compares new_config with old_config and notifies listeners of the changes. */
static void _camera_configuration_notify_change(const dt_camctl_t *c,
const dt_camera_t *camera,
CameraWidget *new_config,
CameraWidget *old_config);
/** Put a job on the queue */
static void _camera_add_job(const dt_camctl_t *c,
const dt_camera_t *camera,
gpointer job);
/** Get a job from the queue */
static gpointer _camera_get_job(const dt_camctl_t *c,
const dt_camera_t *camera);
static void _camera_process_job(const dt_camctl_t *c,
const dt_camera_t *camera,
gpointer job);
/** Dispatch functions for listener interfaces */
static const char *_dispatch_request_image_path(const dt_camctl_t *c,
const dt_image_basic_exif_t *basic_exif,
const dt_camera_t *camera);
static const char *_dispatch_request_image_filename(const dt_camctl_t *c,
const char *filename,
const dt_image_basic_exif_t *basic_exif,
const dt_camera_t *camera);
static void _dispatch_camera_image_downloaded(const dt_camctl_t *c,
const dt_camera_t *camera,
const char *in_path,
const char *in_filename,
const char *filename);
static void _dispatch_camera_connected(const dt_camctl_t *c,
const dt_camera_t *camera);
static void _dispatch_camera_disconnected(const dt_camctl_t *c,
const dt_camera_t *camera);
static void _dispatch_control_status(const dt_camctl_t *c,
dt_camctl_status_t status);
static void _dispatch_camera_error(const dt_camctl_t *c,
const dt_camera_t *camera,
dt_camera_error_t error);
static void _dispatch_camera_property_value_changed(const dt_camctl_t *c,
const dt_camera_t *camera,
const char *name,
const char *value);
/** Helper function to destroy a dt_camera_t object */
static void dt_camctl_camera_destroy(dt_camera_t *cam);
static int logid = 0;
static void _gphoto_log25(GPLogLevel level,
const char *domain,
const char *log,
void *data)
{
dt_print(DT_DEBUG_CAMCTL, "[camera_control] %s %s", domain, log);
}
static void _enable_debug() __attribute__((unused));
static void _disable_debug() __attribute__((unused));
static void _enable_debug()
{
logid = gp_log_add_func(GP_LOG_DATA, (GPLogFunc)_gphoto_log25, NULL);
}
static void _disable_debug()
{
gp_log_remove_func(logid);
}
static void _error_func_dispatch25(GPContext *context,
const char *text,
void *data)
{
dt_camctl_t *camctl = (dt_camctl_t *)data;
dt_print(DT_DEBUG_CAMCTL, "[camera_control] gphoto2 error: %s", text);
if(strstr(text, "PTP"))
{
/* the camera updating thread should get a note about an error from here */
GList *ci = g_list_find(camctl->cameras, camctl->active_camera);
if(ci)
{
dt_camera_t *cam = (dt_camera_t *)ci->data;
dt_print(DT_DEBUG_CAMCTL,
"[camera_control] PTP error `%s' for camera %s on port %s",
text, cam->model, cam->port);
dt_control_log
(_("camera `%s' on port `%s' error %s\n"
"\nmake sure your camera allows access and is not mounted otherwise"),
cam->model, cam->port, text);
cam->ptperror = TRUE;
}
/* notify client of camera connection broken */
_dispatch_camera_error(camctl, camctl->active_camera, CAMERA_CONNECTION_BROKEN);
/* notify client of camera disconnection */
_dispatch_camera_disconnected(camctl, camctl->active_camera);
}
}
static void _status_func_dispatch25(GPContext *context,
const char *text,
void *data)
{
dt_print(DT_DEBUG_CAMCTL, "[camera_control] gphoto2 status: %s", text);
}
static void _message_func_dispatch25(GPContext *context,
const char *text,
void *data)
{
dt_print(DT_DEBUG_CAMCTL, "[camera_control] gphoto2 message: %s", text);
}
static gboolean _camera_timeout_job(gpointer data)
{
dt_camera_t *cam = (dt_camera_t *)data;
dt_print(DT_DEBUG_CAMCTL,
"[camera_control] Calling timeout func for camera %p", cam);
cam->timeout(cam->gpcam, cam->gpcontext);
return TRUE;
}
static int _camera_start_timeout_func(Camera *c,
const unsigned int timeout,
CameraTimeoutFunc func,
void *data)
{
dt_print(DT_DEBUG_CAMCTL,
"[camera_control] start timeout %d seconds for camera %p requested by driver.",
timeout, data);
dt_camera_t *cam = (dt_camera_t *)data;
cam->timeout = func;
return g_timeout_add_seconds(timeout, _camera_timeout_job, cam);
}
static void _camera_stop_timeout_func(Camera *c,
const int id,
void *data)
{
dt_camera_t *cam = (dt_camera_t *)data;
dt_print(DT_DEBUG_CAMCTL,
"[camera_control] Removing timeout %d for camera %p", id, cam);
g_source_remove(id);
cam->timeout = NULL;
}
static void _camera_add_job(const dt_camctl_t *c,
const dt_camera_t *camera,
gpointer job)
{
dt_camera_t *cam = (dt_camera_t *)camera;
dt_pthread_mutex_lock(&cam->jobqueue_lock);
cam->jobqueue = g_list_append(cam->jobqueue, job);
dt_pthread_mutex_unlock(&cam->jobqueue_lock);
}
static gpointer _camera_get_job(const dt_camctl_t *c,
const dt_camera_t *camera)
{
dt_camera_t *cam = (dt_camera_t *)camera;
dt_pthread_mutex_lock(&cam->jobqueue_lock);
gpointer job = NULL;
if(cam->jobqueue) // are there any queued jobs?
{
job = cam->jobqueue->data;
cam->jobqueue = g_list_remove(cam->jobqueue, job);
}
dt_pthread_mutex_unlock(&cam->jobqueue_lock);
return job;
}
static void _camera_process_job(const dt_camctl_t *c,
const dt_camera_t *camera,
gpointer job)
{
dt_camera_t *cam = (dt_camera_t *)camera;
_camctl_camera_job_t *j = (_camctl_camera_job_t *)job;
switch(j->type)
{
case _JOB_TYPE_EXECUTE_CAPTURE:
{
dt_print(DT_DEBUG_CAMCTL,
"[camera_control] executing remote camera capture job");
CameraFilePath fp;
int res = GP_OK;
if((res = gp_camera_capture(camera->gpcam, GP_CAPTURE_IMAGE, &fp, c->gpcontext)) == GP_OK)
{
CameraFile *destination;
const char *output_path = _dispatch_request_image_path(c, NULL, camera);
if(!output_path) output_path = "/tmp";
const char *fname = _dispatch_request_image_filename(c, fp.name, NULL, cam);
if(!fname) break;
char *output = g_build_filename(output_path, fname, (char *)NULL);
int handle = g_open(output, O_CREAT | O_WRONLY | O_BINARY, 0666);
if(handle != -1)
{
gp_file_new_from_fd(&destination, handle);
if(gp_camera_file_get(camera->gpcam, fp.folder,
fp.name, GP_FILE_TYPE_NORMAL, destination,
c->gpcontext) == GP_OK)
{
// Notify listeners of captured image
_dispatch_camera_image_downloaded(c, camera, NULL, NULL, output);
}
else
dt_print(DT_DEBUG_CAMCTL,
"[camera_control] failed to download file %s", output);
close(handle);
}
else
dt_print(DT_DEBUG_CAMCTL,
"[camera_control] failed to download file %s", output);
g_free(output);
}
else
dt_print(DT_DEBUG_CAMCTL,
"[camera_control] capture job failed to capture image: %s",
gp_result_as_string(res));
}
break;
case _JOB_TYPE_EXECUTE_LIVE_VIEW:
{
CameraFile *fp = NULL;
int res = GP_OK;
const gchar *data = NULL;
unsigned long int data_size = 0;
gp_file_new(&fp);
if((res = gp_camera_capture_preview(cam->gpcam, fp, c->gpcontext)) != GP_OK)
{
dt_print(DT_DEBUG_CAMCTL,
"[camera_control] live view failed to capture preview: %s",
gp_result_as_string(res));
}
else if((res = gp_file_get_data_and_size(fp, &data, &data_size)) != GP_OK)
{
dt_print(DT_DEBUG_CAMCTL,
"[camera_control] live view failed to get preview data: %s",
gp_result_as_string(res));
}
// to avoid a crash check that data is not null and data_size >
// 0. there is cases where this occurs even if the above call to
// gp_file_get_data_and_size returns GP_OK.
else if(data && data_size > 0)
{
// everything worked
dt_imageio_jpeg_t jpg;
if(dt_imageio_jpeg_decompress_header(data, data_size, &jpg))
{
dt_print(DT_DEBUG_CAMCTL,
"[camera_control] live view failed to decompress jpeg header");
}
else
{
// FIXME: is the live view ever tagged with a profile?
// testing so far (limited to Canon EOS 5D Mark III) hasn't
// found one
// dt_colorspaces_color_profile_type_t color_space = dt_imageio_jpeg_read_color_space(&jpg);
//if(color_space == DT_COLORSPACE_DISPLAY)
// color_space = DT_COLORSPACE_SRGB;
// no embedded colorspace, assume is sRGB
uint8_t *const buffer =
(uint8_t *)dt_alloc_align_uint8(4 * jpg.width * jpg.height);
if(!buffer)
{
dt_print(DT_DEBUG_CAMCTL,
"[camera_control] live view could not allocate image buffer");
}
else if(dt_imageio_jpeg_decompress(&jpg, buffer))
{
dt_print(DT_DEBUG_CAMCTL,
"[camera_control] live view failed to decompress jpeg");
}
else
{
dt_pthread_mutex_lock(&cam->live_view_buffer_mutex);
// FIXME: don't need to alloc/dealloc if the image dimensions haven't changed
if(cam->live_view_buffer) dt_free_align(cam->live_view_buffer);
cam->live_view_buffer = buffer;
cam->live_view_width = jpg.width;
cam->live_view_height = jpg.height;
//cam->live_view_color_space = color_space;
dt_pthread_mutex_unlock(&cam->live_view_buffer_mutex);
}
}
}
if(fp) gp_file_free(fp);
dt_pthread_mutex_BAD_unlock(&cam->live_view_synch);
dt_control_queue_redraw_center();
}
break;
case _JOB_TYPE_SET_PROPERTY_STRING:
{
_camctl_camera_set_property_string_job_t *spj =
(_camctl_camera_set_property_string_job_t *)job;
dt_print(DT_DEBUG_CAMCTL,
"[camera_control] executing set camera config job %s=%s",
spj->name, spj->value);
dt_pthread_mutex_lock(&cam->config_lock);
CameraWidget *widget;
if(gp_widget_get_child_by_name(camera->configuration, spj->name, &widget) == GP_OK)
{
gp_widget_set_value(widget , spj->value);
gp_camera_set_single_config(cam->gpcam, spj->name, widget, c->gpcontext);
}
dt_pthread_mutex_unlock(&cam->config_lock);
g_free(spj->name);
g_free(spj->value);
}
break;
case _JOB_TYPE_SET_PROPERTY_CHOICE:
{
_camctl_camera_set_property_choice_job_t *spj =
(_camctl_camera_set_property_choice_job_t *)job;
dt_print(DT_DEBUG_CAMCTL,
"[camera_control] executing set camera config job %s=%d",
spj->name, spj->value);
dt_pthread_mutex_lock(&cam->config_lock);
CameraWidget *widget;
if(gp_widget_get_child_by_name(camera->configuration, spj->name, &widget) == GP_OK)
{
if(spj->value >= 0 && spj->value < gp_widget_count_choices(widget))
{
const char *choice;
gp_widget_get_choice(widget, spj->value, &choice);
dt_print(DT_DEBUG_CAMCTL, " (%s)", choice);
gp_widget_set_value(widget, choice);
gp_camera_set_single_config(cam->gpcam, spj->name, widget, c->gpcontext);
}
}
dt_pthread_mutex_unlock(&cam->config_lock);
dt_print(DT_DEBUG_CAMCTL, "\n");
g_free(spj->name);
}
break;
case _JOB_TYPE_SET_PROPERTY_TOGGLE:
{
_camctl_camera_set_property_toggle_job_t *spj =
(_camctl_camera_set_property_toggle_job_t *)job;
dt_print(DT_DEBUG_CAMCTL,
"[camera_control] executing camera config job to toggle %s",
spj->name);
dt_pthread_mutex_lock(&cam->config_lock);
CameraWidget *widget;
if(gp_widget_get_child_by_name(cam->configuration, spj->name, &widget) == GP_OK)
{
const int value = 1;
gp_widget_set_value(widget, &value);
gp_camera_set_single_config(cam->gpcam, spj->name, widget, c->gpcontext);
}
dt_pthread_mutex_unlock(&cam->config_lock);
g_free(spj->name);
}
break;
case _JOB_TYPE_SET_PROPERTY_INT:
{
_camctl_camera_set_property_int_job_t *spj =
(_camctl_camera_set_property_int_job_t *)job;
dt_print(DT_DEBUG_CAMCTL,
"[camera_control] executing set camera config job %s=%d",
spj->name, spj->value);
dt_pthread_mutex_lock(&cam->config_lock);
CameraWidget *widget;
if(gp_widget_get_child_by_name(cam->configuration, spj->name, &widget) == GP_OK)
{
const int value = spj->value;
const int set_value_succeeds = gp_widget_set_value(widget, &value);
if(set_value_succeeds != GP_OK)
{
dt_print(DT_DEBUG_CAMCTL,
"[camera_control] setting int value %d on %s failed with code %d",
spj->value, spj->name, set_value_succeeds);
}
const int set_config_succeeds =
gp_camera_set_single_config(cam->gpcam, spj->name, widget, c->gpcontext);
if(set_value_succeeds != GP_OK)
{
dt_print(DT_DEBUG_CAMCTL,
"[camera_control] setting config failed with code %d",
set_config_succeeds);
}
}
dt_pthread_mutex_unlock(&cam->config_lock);
g_free(spj->name);
}
break;
case _JOB_TYPE_SET_PROPERTY_FLOAT:
{
_camctl_camera_set_property_float_job_t *spj =
(_camctl_camera_set_property_float_job_t *)job;
dt_print(DT_DEBUG_CAMCTL,
"[camera_control] executing set camera config float job %s=%.2f",
spj->name, spj->value);
dt_pthread_mutex_lock(&cam->config_lock);
CameraWidget *widget;
if(gp_widget_get_child_by_name(cam->configuration, spj->name, &widget) == GP_OK)
{
const float value = spj->value;
const int set_value_succeeds = gp_widget_set_value(widget, &value);
if(set_value_succeeds != GP_OK)
{
dt_print(DT_DEBUG_CAMCTL,
"[camera_control] setting int value %.2f on %s failed with code %d",
spj->value, spj->name, set_value_succeeds);
}
const int set_config_succeeds =
gp_camera_set_single_config(cam->gpcam, spj->name, widget, c->gpcontext);
if(set_value_succeeds != GP_OK)
{
dt_print(DT_DEBUG_CAMCTL,
"[camera_control] setting config failed with code %d",
set_config_succeeds);
}
}
dt_pthread_mutex_unlock(&cam->config_lock);
g_free(spj->name);
}
break;
default:
dt_print(DT_DEBUG_CAMCTL,
"[camera_control] process of unknown job type 0x%x", j->type);
break;
}
g_free(j);
}
/*************/
/* LIVE VIEW */
/*************/
static void *dt_camctl_camera_get_live_view(void *data)
{
dt_camctl_t *camctl = (dt_camctl_t *)data;
dt_camera_t *cam = (dt_camera_t *)camctl->active_camera;
dt_pthread_setname("live view");
dt_print(DT_DEBUG_CAMCTL, "[camera_control] live view thread started");
int frames = 0;
double capture_time = dt_get_wtime();
const int fps = dt_conf_get_int("plugins/capture/camera/live_view_fps");
while(cam->is_live_viewing == TRUE)
{
dt_pthread_mutex_BAD_lock(&cam->live_view_synch);
// calculate FPS
double current_time = dt_get_wtime();
if(current_time - capture_time >= 1.0)
{
// a second has passed
dt_print(DT_DEBUG_CAMCTL, "%d fps", frames + 1);
frames = 0;
capture_time = current_time;
}
else
{
// just increase the frame counter
frames++;
}
_camctl_camera_job_t *job = g_malloc(sizeof(_camctl_camera_job_t));
job->type = _JOB_TYPE_EXECUTE_LIVE_VIEW;
_camera_add_job(camctl, cam, job);
g_usleep((1.0 / fps) * G_USEC_PER_SEC); // going too fast will result in
// too many redraws without a real benefit
}
dt_print(DT_DEBUG_CAMCTL, "[camera_control] live view thread stopped");
return NULL;
}
gboolean dt_camctl_camera_start_live_view(const dt_camctl_t *c)
{
dt_camctl_t *camctl = (dt_camctl_t *)c;
dt_camera_t *cam = (dt_camera_t *)camctl->active_camera;
if(cam == NULL)
{
dt_print(DT_DEBUG_CAMCTL,
"[camera_control] Failed to start live view, camera==NULL");
return FALSE;
}
else
dt_print(DT_DEBUG_CAMCTL,
"[camera_control] Starting live view");
if(cam->can_live_view == FALSE)
{
dt_print(DT_DEBUG_CAMCTL,
"[camera_control] Camera does not support live view");
return FALSE;
}
cam->is_live_viewing = TRUE;
dt_camctl_camera_set_property_int(camctl, NULL, "eosviewfinder", 1);
dt_camctl_camera_set_property_int(camctl, NULL, "viewfinder", 1);
dt_pthread_create(&cam->live_view_thread,
&dt_camctl_camera_get_live_view, (void *)camctl);
return TRUE;
}
void dt_camctl_camera_stop_live_view(const dt_camctl_t *c)
{
dt_camctl_t *camctl = (dt_camctl_t *)c;
dt_camera_t *cam = (dt_camera_t *)camctl->active_camera;
if(!cam) return;
if(cam->is_live_viewing == FALSE)
{
dt_print(DT_DEBUG_CAMCTL,
"[camera_control] Not in live view mode, nothing to stop");
return;
}
dt_print(DT_DEBUG_CAMCTL, "[camera_control] Stopping live view");
cam->is_live_viewing = FALSE;
dt_pthread_join(cam->live_view_thread);
// tell camera to get back to normal state (close mirror)
dt_camctl_camera_set_property_int(camctl, NULL, "eosviewfinder", 0);
dt_camctl_camera_set_property_int(camctl, NULL, "viewfinder", 0);
}
static void _camctl_lock(const dt_camctl_t *c, const dt_camera_t *cam)
{
dt_camctl_t *camctl = (dt_camctl_t *)c;
dt_pthread_mutex_BAD_lock(&camctl->lock);
dt_print(DT_DEBUG_CAMCTL,
"[camera_control] camera control locked for %s", cam->model);
camctl->active_camera = cam;
_dispatch_control_status(c, CAMERA_CONTROL_BUSY);
}
static void _camctl_unlock(const dt_camctl_t *c)
{
dt_camctl_t *camctl = (dt_camctl_t *)c;
const dt_camera_t *cam = camctl->active_camera;
camctl->active_camera = NULL;
dt_pthread_mutex_BAD_unlock(&camctl->lock);
if(cam)
dt_print(DT_DEBUG_CAMCTL,
"[camera_control] camera control un-locked for %s", cam->model);
else
dt_print(DT_DEBUG_CAMCTL,
"[camera_control] camera control un-locked for unknown camera");
_dispatch_control_status(c, CAMERA_CONTROL_AVAILABLE);
}
dt_camctl_t *dt_camctl_new()
{
dt_camctl_t *camctl = g_malloc0(sizeof(dt_camctl_t));
dt_print(DT_DEBUG_CAMCTL, "[camera_control] creating new context %p", camctl);
// Initialize gphoto2 context and setup dispatch callbacks
camctl->gpcontext = gp_context_new();
camctl->ticker = 1;
camctl->tickmask = 0x0F;
gp_context_set_status_func(camctl->gpcontext,
(GPContextStatusFunc)_status_func_dispatch25, camctl);
gp_context_set_error_func(camctl->gpcontext,
(GPContextErrorFunc)_error_func_dispatch25, camctl);
gp_context_set_message_func(camctl->gpcontext,
(GPContextMessageFunc)_message_func_dispatch25, camctl);
// Load all camera drivers we know...
gp_abilities_list_new(&camctl->gpcams);
gp_abilities_list_load(camctl->gpcams, camctl->gpcontext);
dt_print(DT_DEBUG_CAMCTL,
"[camera_control] loaded %d camera drivers",
gp_abilities_list_count(camctl->gpcams));
dt_pthread_mutex_init(&camctl->lock, NULL);
dt_pthread_mutex_init(&camctl->listeners_lock, NULL);
return camctl;
}
static void dt_camctl_camera_destroy_struct(dt_camera_t *cam)
{
if(!cam) return;
if(cam->live_view_buffer)
{
dt_free_align(cam->live_view_buffer);
cam->live_view_buffer = NULL; // just in case someone else is using this
}
g_free(cam->model);
g_free(cam->port);
dt_pthread_mutex_destroy(&cam->jobqueue_lock);
dt_pthread_mutex_destroy(&cam->config_lock);
dt_pthread_mutex_destroy(&cam->live_view_buffer_mutex);
dt_pthread_mutex_destroy(&cam->live_view_synch);
// TODO: cam->jobqueue
g_free(cam);
}
static void dt_camctl_camera_destroy(dt_camera_t *cam)
{
if(!cam) return;
dt_print(DT_DEBUG_CAMCTL,
"[camera_control] destroy %s on port %s", cam->model, cam->port);
for(GList *it = cam->open_gpfiles; it; it = g_list_delete_link(it, it))
{
gp_file_free((CameraFile *)it->data);
}
gp_camera_exit(cam->gpcam, cam->gpcontext);
gp_camera_unref(cam->gpcam);
gp_widget_unref(cam->configuration);
dt_camctl_camera_destroy_struct(cam);
}
static void dt_camctl_unused_camera_destroy(dt_camera_unused_t *cam)
{
if(!cam) return;
g_free(cam->model);
g_free(cam->port);
g_free(cam);
}
void dt_camctl_destroy(dt_camctl_t *camctl)
{
if(!camctl) return;
// Go thru all c->cameras and release them..
dt_print(DT_DEBUG_CAMCTL, "[camera_control] destroy darktable camcontrol");
gp_context_cancel(camctl->gpcontext);
for(GList *it = camctl->cameras; it; it = g_list_delete_link(it, it))
{
dt_camctl_camera_destroy((dt_camera_t *)it->data);
}
// Go thru all c->unused_cameras and free them
for(GList *itl = camctl->unused_cameras; itl; itl = g_list_delete_link(itl, itl))
{
dt_camctl_unused_camera_destroy((dt_camera_unused_t *)itl->data);
}
gp_context_unref(camctl->gpcontext);
gp_abilities_list_free(camctl->gpcams);
gp_port_info_list_free(camctl->gpports);
dt_pthread_mutex_destroy(&camctl->lock);
dt_pthread_mutex_destroy(&camctl->listeners_lock);
g_free(camctl);
}
gboolean dt_camctl_have_cameras(const dt_camctl_t *c)
{
return (c->cameras) ? TRUE : FALSE;
}
gboolean dt_camctl_have_unused_cameras(const dt_camctl_t *c)
{
return (c->unused_cameras) ? TRUE : FALSE;
}
void dt_camctl_register_listener(const dt_camctl_t *c,
dt_camctl_listener_t *listener)
{
dt_camctl_t *camctl = (dt_camctl_t *)c;
// Just locking mutex and prevent signalling CAMERA_CONTROL_BUSY
dt_pthread_mutex_lock(&camctl->listeners_lock);
if(g_list_find(camctl->listeners, listener) == NULL)
{
camctl->listeners = g_list_append(camctl->listeners, listener);
dt_print(DT_DEBUG_CAMCTL,
"[camera_control] registering listener %p", listener);
}
else
dt_print(DT_DEBUG_CAMCTL,
"[camera_control] registering already registered listener %p", listener);
dt_pthread_mutex_unlock(&camctl->listeners_lock);
}
void dt_camctl_unregister_listener(const dt_camctl_t *c,
dt_camctl_listener_t *listener)
{
dt_camctl_t *camctl = (dt_camctl_t *)c;
// Just locking mutex and prevent signalling CAMERA_CONTROL_BUSY
dt_pthread_mutex_lock(&camctl->listeners_lock);
dt_print(DT_DEBUG_CAMCTL, "[camera_control] unregistering listener %p", listener);
camctl->listeners = g_list_remove(camctl->listeners, listener);
dt_pthread_mutex_unlock(&camctl->listeners_lock);
}
static gboolean _have_camera_on_port(const dt_camera_unused_t *const testcam,
const GList *const cameras)
{
for(const GList *camera = cameras; camera; camera = g_list_next(camera))
{
const dt_camera_unused_t *caminfo = (dt_camera_unused_t*)camera->data;
if(g_strcmp0(testcam->model, caminfo->model) == 0 &&
g_strcmp0(testcam->port, caminfo->port) == 0)
return TRUE;
}
return FALSE;
}
static int cameras_cnt = -1;
static int ports_cnt = -1;
static gboolean dt_camctl_update_cameras(const dt_camctl_t *c)
{
dt_camctl_t *camctl = (dt_camctl_t *)c;
if(!camctl) return FALSE;
dt_pthread_mutex_lock(&camctl->lock);
gboolean changed_camera = FALSE;
/* reload portdrivers */
if(camctl->gpports) gp_port_info_list_free(camctl->gpports);
gp_port_info_list_new(&camctl->gpports);
gp_port_info_list_load(camctl->gpports);
const int ports_available = gp_port_info_list_count(camctl->gpports);
if(ports_available != ports_cnt)
{
dt_print(DT_DEBUG_CAMCTL,
"[camera_control] loaded %d port drivers", ports_available);
ports_cnt = ports_available;
}
CameraList *available_cameras = NULL;
gp_list_new(&available_cameras);
gp_abilities_list_detect(c->gpcams, c->gpports, available_cameras, c->gpcontext);
const int connected_cnt = gp_list_count(available_cameras);
if(connected_cnt != cameras_cnt)
{
dt_print(DT_DEBUG_CAMCTL,
"[camera_control] %d cameras connected", connected_cnt);
cameras_cnt = connected_cnt;
}
for(int i = 0; i < gp_list_count(available_cameras); i++)
{
dt_camera_unused_t *testcam = g_malloc0(sizeof(dt_camera_unused_t));
const gchar *s;
gp_list_get_name(available_cameras, i, &s);
testcam->model = g_strdup(s);
gp_list_get_value(available_cameras, i, &s);
testcam->port = g_strdup(s);
// FIXME we might better test elsewhere for special port drivers,
// have it active while debugging
if(!(strncmp(testcam->port, "disk:", 5)) && !(darktable.unmuted & DT_DEBUG_CAMCTL))
{
g_free(testcam);
continue;
}
// look for freshly connected cameras -- neither in mounted-cameras list nor unmounted-cameras list
if(!_have_camera_on_port(testcam, c->cameras) &&
!_have_camera_on_port(testcam, c->unused_cameras))
{
dt_camera_unused_t *unused_camera = g_malloc0(sizeof(dt_camera_unused_t));
unused_camera->model = g_strdup(testcam->model);
unused_camera->port = g_strdup(testcam->port);
camctl->unused_cameras = g_list_append(camctl->unused_cameras, unused_camera);
dt_print(DT_DEBUG_CAMCTL,
"[camera_control] found new %s on port %s",
testcam->model, testcam->port);
changed_camera = TRUE;
}
g_free(testcam);
}
/* check unused_cameras being unplugged */
if(dt_camctl_have_unused_cameras(camctl))
{
GList *unused_item = c->unused_cameras;
do
{
dt_camera_unused_t *cam = (dt_camera_unused_t *)unused_item->data;
gboolean removed = TRUE;
for(int i = 0; i < gp_list_count(available_cameras); i++)
{
const gchar *mymodel;
const gchar *myport;
gp_list_get_name(available_cameras, i, &mymodel);
gp_list_get_value(available_cameras, i, &myport);
if((g_strcmp0(mymodel, cam->model) == 0) && (g_strcmp0(myport, cam->port) == 0))
removed = FALSE;
}
if(removed)
{
dt_print(DT_DEBUG_CAMCTL,
"[camera_control] remove %s on port %s from unused camera list",
cam->model, cam->port);
dt_camera_unused_t *oldcam = (dt_camera_unused_t *)unused_item->data;
camctl->unused_cameras = unused_item =
g_list_delete_link(c->unused_cameras, unused_item);
dt_camctl_unused_camera_destroy(oldcam);
changed_camera = TRUE;
}
else
{
if(cam->trymount)
{
cam->trymount = FALSE;
dt_camera_t *camera = g_malloc0(sizeof(dt_camera_t));
camera->model = g_strdup(cam->model);
camera->port = g_strdup(cam->port);
if(_camera_initialize(camctl, camera) == FALSE)
{
dt_print(DT_DEBUG_CAMCTL,
"[camera_control] failed to initialize %s on port %s, likely "
"causes are: locked by another application, no access to udev etc",
camera->model, camera->port);
dt_control_log
(_("failed to initialize `%s' on port `%s', likely "
"causes are: locked by another application, no access to devices etc"),
camera->model, camera->port);
g_free(camera);
cam->used = TRUE;
continue;
}
if(camera->can_import == FALSE && camera->can_tether == FALSE)
{
dt_print(DT_DEBUG_CAMCTL,
"[camera_control] %s on port %s doesn't support import or tether",
camera->model, camera->port);
dt_control_log(_("`%s' on port `%s' is not interesting"
" because it supports neither tethering nor import"),
camera->model, camera->port);
g_free(camera);
cam->boring = TRUE;
continue;
}
// Fetch some summary of camera
if(gp_camera_get_summary(camera->gpcam, &camera->summary, c->gpcontext) == GP_OK)
{
// Remove device property summary:
char *eos = strstr(camera->summary.text, "Device Property Summary:\n");
if(eos) eos[0] = '\0';
}
// Add to camera list
camctl->cameras = g_list_append(camctl->cameras, camera);
changed_camera = TRUE;
dt_print(DT_DEBUG_CAMCTL,
"[camera_control] remove %s on port %s from"
" unused camera list as mounted",
cam->model, cam->port);
dt_camera_unused_t *oldcam = (dt_camera_unused_t *)unused_item->data;
camctl->unused_cameras = unused_item =
g_list_delete_link(c->unused_cameras, unused_item);
dt_camctl_unused_camera_destroy(oldcam);
// Notify listeners of connected camera
_dispatch_camera_connected(camctl, camera);
}
}
} while(unused_item && (unused_item = g_list_next(unused_item)) != NULL);
}
if(dt_camctl_have_cameras(camctl))
{
GList *citem = c->cameras;
do
{
dt_camera_t *cam = (dt_camera_t *)citem->data;
gboolean removed = TRUE;