main.c 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752
  1. #include <errno.h>
  2. #include <fcntl.h>
  3. #include <linux/videodev2.h>
  4. #include <linux/media.h>
  5. #include <linux/v4l2-subdev.h>
  6. #include <sys/ioctl.h>
  7. #include <sys/mman.h>
  8. #include <sys/stat.h>
  9. #include <time.h>
  10. #include <assert.h>
  11. #include <limits.h>
  12. #include <linux/kdev_t.h>
  13. #include <sys/sysmacros.h>
  14. #include <asm/errno.h>
  15. #include <wordexp.h>
  16. #include <gtk/gtk.h>
  17. #include <tiffio.h>
  18. #include <locale.h>
  19. #include "config.h"
  20. #include "ini.h"
  21. #include "quickpreview.h"
  22. #define NUM_CAMERAS 5
  23. #define NUM_LINKS 10
  24. enum user_control {
  25. USER_CONTROL_ISO,
  26. USER_CONTROL_SHUTTER
  27. };
  28. #define TIFFTAG_FORWARDMATRIX1 50964
  29. struct buffer {
  30. void *start;
  31. size_t length;
  32. };
  33. struct mp_media_link {
  34. char source_name[100];
  35. char target_name[100];
  36. int source_port;
  37. int target_port;
  38. unsigned int source_entity_id;
  39. unsigned int target_entity_id;
  40. int valid;
  41. };
  42. struct camerainfo {
  43. char cfg_name[100];
  44. int exists;
  45. char dev_name[260];
  46. char dev_fname[260];
  47. unsigned int entity_id;
  48. int width;
  49. int height;
  50. int rate;
  51. int rotate;
  52. int fmt;
  53. int mbus;
  54. int fd;
  55. char media_dev_name[260];
  56. char media_dev_fname[260];
  57. char video_dev_fname[260];
  58. int media_fd;
  59. int video_fd;
  60. unsigned int interface_entity_id;
  61. struct mp_media_link media_links[10];
  62. float colormatrix[9];
  63. float forwardmatrix[9];
  64. int blacklevel;
  65. int whitelevel;
  66. float focallength;
  67. float cropfactor;
  68. double fnumber;
  69. int iso_min;
  70. int iso_max;
  71. int gain_ctrl;
  72. int gain_max;
  73. int has_af_c;
  74. int has_af_s;
  75. };
  76. struct camerainfo cameras[NUM_CAMERAS];
  77. static float colormatrix_srgb[] = {
  78. 3.2409, -1.5373, -0.4986,
  79. -0.9692, 1.8759, 0.0415,
  80. 0.0556, -0.2039, 1.0569
  81. };
  82. struct buffer *buffers;
  83. static unsigned int n_buffers;
  84. struct camerainfo current;
  85. // General info
  86. static char *exif_make;
  87. static char *exif_model;
  88. // State
  89. static int ready = 0;
  90. static int capture = 0;
  91. static int current_cid = -1;
  92. static cairo_surface_t *surface = NULL;
  93. static cairo_surface_t *status_surface = NULL;
  94. static int preview_width = -1;
  95. static int preview_height = -1;
  96. static char last_path[260] = "";
  97. static int auto_exposure = 1;
  98. static int exposure = 1;
  99. static int auto_gain = 1;
  100. static int gain = 1;
  101. static int burst_length = 10;
  102. static char burst_dir[23];
  103. static char processing_script[512];
  104. static enum user_control current_control;
  105. // Widgets
  106. GtkWidget *preview;
  107. GtkWidget *error_box;
  108. GtkWidget *error_message;
  109. GtkWidget *main_stack;
  110. GtkWidget *thumb_last;
  111. GtkWidget *control_box;
  112. GtkWidget *control_name;
  113. GtkAdjustment *control_slider;
  114. GtkWidget *control_auto;
  115. static int
  116. xioctl(int fd, int request, void *arg)
  117. {
  118. int r;
  119. do {
  120. r = ioctl(fd, request, arg);
  121. } while (r == -1 && errno == EINTR);
  122. return r;
  123. }
  124. static void
  125. errno_exit(const char *s)
  126. {
  127. fprintf(stderr, "%s error %d, %s\n", s, errno, strerror(errno));
  128. exit(EXIT_FAILURE);
  129. }
  130. static void
  131. show_error(const char *s)
  132. {
  133. gtk_label_set_text(GTK_LABEL(error_message), s);
  134. gtk_widget_show(error_box);
  135. }
  136. int
  137. remap(int value, int input_min, int input_max, int output_min, int output_max)
  138. {
  139. const long long factor = 1000000000;
  140. long long output_spread = output_max - output_min;
  141. long long input_spread = input_max - input_min;
  142. long long zero_value = value - input_min;
  143. zero_value *= factor;
  144. long long percentage = zero_value / input_spread;
  145. long long zero_output = percentage * output_spread / factor;
  146. long long result = output_min + zero_output;
  147. return (int)result;
  148. }
  149. static void
  150. start_capturing(int fd)
  151. {
  152. enum v4l2_buf_type type;
  153. for (int i = 0; i < n_buffers; ++i) {
  154. struct v4l2_buffer buf = {
  155. .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
  156. .memory = V4L2_MEMORY_MMAP,
  157. .index = i,
  158. };
  159. if (xioctl(fd, VIDIOC_QBUF, &buf) == -1) {
  160. errno_exit("VIDIOC_QBUF");
  161. }
  162. }
  163. type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  164. if (xioctl(fd, VIDIOC_STREAMON, &type) == -1) {
  165. errno_exit("VIDIOC_STREAMON");
  166. }
  167. ready = 1;
  168. }
  169. static void
  170. stop_capturing(int fd)
  171. {
  172. int i;
  173. ready = 0;
  174. printf("Stopping capture\n");
  175. enum v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  176. if (xioctl(fd, VIDIOC_STREAMOFF, &type) == -1) {
  177. errno_exit("VIDIOC_STREAMOFF");
  178. }
  179. for (i = 0; i < n_buffers; ++i) {
  180. munmap(buffers[i].start, buffers[i].length);
  181. }
  182. }
  183. static void
  184. init_mmap(int fd)
  185. {
  186. struct v4l2_requestbuffers req = {0};
  187. req.count = 4;
  188. req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  189. req.memory = V4L2_MEMORY_MMAP;
  190. if (xioctl(fd, VIDIOC_REQBUFS, &req) == -1) {
  191. if (errno == EINVAL) {
  192. fprintf(stderr, "%s does not support memory mapping",
  193. current.dev_name);
  194. exit(EXIT_FAILURE);
  195. } else {
  196. errno_exit("VIDIOC_REQBUFS");
  197. }
  198. }
  199. if (req.count < 2) {
  200. fprintf(stderr, "Insufficient buffer memory on %s\n",
  201. current.dev_name);
  202. exit(EXIT_FAILURE);
  203. }
  204. buffers = calloc(req.count, sizeof(buffers[0]));
  205. if (!buffers) {
  206. fprintf(stderr, "Out of memory\\n");
  207. exit(EXIT_FAILURE);
  208. }
  209. for (n_buffers = 0; n_buffers < req.count; ++n_buffers) {
  210. struct v4l2_buffer buf = {
  211. .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
  212. .memory = V4L2_MEMORY_MMAP,
  213. .index = n_buffers,
  214. };
  215. if (xioctl(fd, VIDIOC_QUERYBUF, &buf) == -1) {
  216. errno_exit("VIDIOC_QUERYBUF");
  217. }
  218. buffers[n_buffers].length = buf.length;
  219. buffers[n_buffers].start = mmap(NULL /* start anywhere */,
  220. buf.length,
  221. PROT_READ | PROT_WRITE /* required */,
  222. MAP_SHARED /* recommended */,
  223. fd, buf.m.offset);
  224. if (MAP_FAILED == buffers[n_buffers].start) {
  225. errno_exit("mmap");
  226. }
  227. }
  228. }
  229. static int
  230. v4l2_ctrl_set(int fd, uint32_t id, int val)
  231. {
  232. struct v4l2_control ctrl = {0};
  233. ctrl.id = id;
  234. ctrl.value = val;
  235. if (xioctl(fd, VIDIOC_S_CTRL, &ctrl) == -1) {
  236. g_printerr("Failed to set control %d to %d\n", id, val);
  237. return -1;
  238. }
  239. return 0;
  240. }
  241. static int
  242. v4l2_ctrl_get(int fd, uint32_t id)
  243. {
  244. struct v4l2_control ctrl = {0};
  245. ctrl.id = id;
  246. if (xioctl(fd, VIDIOC_G_CTRL, &ctrl) == -1) {
  247. g_printerr("Failed to get control %d\n", id);
  248. return -1;
  249. }
  250. return ctrl.value;
  251. }
  252. static int
  253. v4l2_ctrl_get_max(int fd, uint32_t id)
  254. {
  255. struct v4l2_queryctrl queryctrl;
  256. int ret;
  257. memset(&queryctrl, 0, sizeof(queryctrl));
  258. queryctrl.id = id;
  259. ret = xioctl(fd, VIDIOC_QUERYCTRL, &queryctrl);
  260. if (ret)
  261. return 0;
  262. if (queryctrl.flags & V4L2_CTRL_FLAG_DISABLED) {
  263. return 0;
  264. }
  265. return queryctrl.maximum;
  266. }
  267. static int
  268. v4l2_has_control(int fd, int control_id)
  269. {
  270. struct v4l2_queryctrl queryctrl;
  271. int ret;
  272. memset(&queryctrl, 0, sizeof(queryctrl));
  273. queryctrl.id = control_id;
  274. ret = xioctl(fd, VIDIOC_QUERYCTRL, &queryctrl);
  275. if (ret)
  276. return 0;
  277. if (queryctrl.flags & V4L2_CTRL_FLAG_DISABLED) {
  278. return 0;
  279. }
  280. return 1;
  281. }
  282. static void
  283. draw_controls()
  284. {
  285. cairo_t *cr;
  286. char iso[6];
  287. int temp;
  288. char shutterangle[6];
  289. if (auto_exposure) {
  290. sprintf(shutterangle, "auto");
  291. } else {
  292. temp = (int)((float)exposure / (float)current.height * 360);
  293. sprintf(shutterangle, "%d\u00b0", temp);
  294. }
  295. if (auto_gain) {
  296. sprintf(iso, "auto");
  297. } else {
  298. temp = remap(gain - 1, 0, current.gain_max, current.iso_min, current.iso_max);
  299. sprintf(iso, "%d", temp);
  300. }
  301. if (status_surface)
  302. cairo_surface_destroy(status_surface);
  303. // Make a service to show status of controls, 32px high
  304. if (gtk_widget_get_window(preview) == NULL) {
  305. return;
  306. }
  307. status_surface = gdk_window_create_similar_surface(gtk_widget_get_window(preview),
  308. CAIRO_CONTENT_COLOR_ALPHA,
  309. preview_width, 32);
  310. cr = cairo_create(status_surface);
  311. cairo_set_source_rgba(cr, 0, 0, 0, 0.0);
  312. cairo_paint(cr);
  313. // Draw the outlines for the headings
  314. cairo_select_font_face(cr, "sans-serif", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_BOLD);
  315. cairo_set_font_size(cr, 9);
  316. cairo_set_source_rgba(cr, 0, 0, 0, 1);
  317. cairo_move_to(cr, 16, 16);
  318. cairo_text_path(cr, "ISO");
  319. cairo_stroke(cr);
  320. cairo_move_to(cr, 60, 16);
  321. cairo_text_path(cr, "Shutter");
  322. cairo_stroke(cr);
  323. // Draw the fill for the headings
  324. cairo_set_source_rgba(cr, 1, 1, 1, 1);
  325. cairo_move_to(cr, 16, 16);
  326. cairo_show_text(cr, "ISO");
  327. cairo_move_to(cr, 60, 16);
  328. cairo_show_text(cr, "Shutter");
  329. // Draw the outlines for the values
  330. cairo_select_font_face(cr, "sans-serif", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL);
  331. cairo_set_font_size(cr, 11);
  332. cairo_set_source_rgba(cr, 0, 0, 0, 1);
  333. cairo_move_to(cr, 16, 26);
  334. cairo_text_path(cr, iso);
  335. cairo_stroke(cr);
  336. cairo_move_to(cr, 60, 26);
  337. cairo_text_path(cr, shutterangle);
  338. cairo_stroke(cr);
  339. // Draw the fill for the values
  340. cairo_set_source_rgba(cr, 1, 1, 1, 1);
  341. cairo_move_to(cr, 16, 26);
  342. cairo_show_text(cr, iso);
  343. cairo_move_to(cr, 60, 26);
  344. cairo_show_text(cr, shutterangle);
  345. cairo_destroy(cr);
  346. }
  347. static void
  348. init_sensor(char *fn, int width, int height, int mbus, int rate)
  349. {
  350. int fd;
  351. struct v4l2_subdev_frame_interval interval = {};
  352. struct v4l2_subdev_format fmt = {};
  353. fd = open(fn, O_RDWR);
  354. g_print("Setting sensor rate to %d\n", rate);
  355. interval.pad = 0;
  356. interval.interval.numerator = 1;
  357. interval.interval.denominator = rate;
  358. if (xioctl(fd, VIDIOC_SUBDEV_S_FRAME_INTERVAL, &interval) == -1) {
  359. errno_exit("VIDIOC_SUBDEV_S_FRAME_INTERVAL");
  360. }
  361. if (interval.interval.numerator != 1 || interval.interval.denominator != rate)
  362. g_printerr("Driver chose %d/%d instead\n",
  363. interval.interval.numerator, interval.interval.denominator);
  364. g_print("Setting sensor to %dx%d fmt %d\n",
  365. width, height, mbus);
  366. fmt.pad = 0;
  367. fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
  368. fmt.format.code = mbus;
  369. fmt.format.width = width;
  370. fmt.format.height = height;
  371. fmt.format.field = V4L2_FIELD_ANY;
  372. if (xioctl(fd, VIDIOC_SUBDEV_S_FMT, &fmt) == -1) {
  373. errno_exit("VIDIOC_SUBDEV_S_FMT");
  374. }
  375. if (fmt.format.width != width || fmt.format.height != height || fmt.format.code != mbus)
  376. g_printerr("Driver chose %dx%d fmt %d instead\n",
  377. fmt.format.width, fmt.format.height,
  378. fmt.format.code);
  379. // Trigger continuous auto focus if the sensor supports it
  380. if (v4l2_has_control(fd, V4L2_CID_FOCUS_AUTO)) {
  381. current.has_af_c = 1;
  382. v4l2_ctrl_set(fd, V4L2_CID_FOCUS_AUTO, 1);
  383. }
  384. if (v4l2_has_control(fd, V4L2_CID_AUTO_FOCUS_START)) {
  385. current.has_af_s = 1;
  386. }
  387. if (v4l2_has_control(fd, V4L2_CID_GAIN)) {
  388. current.gain_ctrl = V4L2_CID_GAIN;
  389. current.gain_max = v4l2_ctrl_get_max(fd, V4L2_CID_GAIN);
  390. }
  391. if (v4l2_has_control(fd, V4L2_CID_ANALOGUE_GAIN)) {
  392. current.gain_ctrl = V4L2_CID_ANALOGUE_GAIN;
  393. current.gain_max = v4l2_ctrl_get_max(fd, V4L2_CID_ANALOGUE_GAIN);
  394. }
  395. auto_exposure = 1;
  396. auto_gain = 1;
  397. draw_controls();
  398. close(current.fd);
  399. current.fd = fd;
  400. }
  401. static int
  402. init_device(int fd)
  403. {
  404. struct v4l2_capability cap;
  405. if (xioctl(fd, VIDIOC_QUERYCAP, &cap) == -1) {
  406. if (errno == EINVAL) {
  407. fprintf(stderr, "%s is no V4L2 device\n",
  408. current.dev_name);
  409. exit(EXIT_FAILURE);
  410. } else {
  411. errno_exit("VIDIOC_QUERYCAP");
  412. }
  413. }
  414. if (!(cap.capabilities & V4L2_CAP_VIDEO_CAPTURE)) {
  415. fprintf(stderr, "%s is no video capture device\n",
  416. current.dev_name);
  417. exit(EXIT_FAILURE);
  418. }
  419. if (!(cap.capabilities & V4L2_CAP_STREAMING)) {
  420. fprintf(stderr, "%s does not support streaming i/o\n",
  421. current.dev_name);
  422. exit(EXIT_FAILURE);
  423. }
  424. /* Select video input, video standard and tune here. */
  425. struct v4l2_cropcap cropcap = {
  426. .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
  427. };
  428. struct v4l2_crop crop = {0};
  429. if (xioctl(fd, VIDIOC_CROPCAP, &cropcap) == 0) {
  430. crop.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  431. crop.c = cropcap.defrect; /* reset to default */
  432. if (xioctl(fd, VIDIOC_S_CROP, &crop) == -1) {
  433. switch (errno) {
  434. case EINVAL:
  435. /* Cropping not supported. */
  436. break;
  437. default:
  438. /* Errors ignored. */
  439. break;
  440. }
  441. }
  442. } else {
  443. /* Errors ignored. */
  444. }
  445. struct v4l2_format fmt = {
  446. .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
  447. };
  448. if (current.width > 0) {
  449. g_print("Setting camera to %dx%d fmt %d\n",
  450. current.width, current.height, current.fmt);
  451. fmt.fmt.pix.width = current.width;
  452. fmt.fmt.pix.height = current.height;
  453. fmt.fmt.pix.pixelformat = current.fmt;
  454. fmt.fmt.pix.field = V4L2_FIELD_ANY;
  455. if (xioctl(fd, VIDIOC_S_FMT, &fmt) == -1) {
  456. g_printerr("VIDIOC_S_FMT failed");
  457. show_error("Could not set camera mode");
  458. return -1;
  459. }
  460. if (fmt.fmt.pix.width != current.width ||
  461. fmt.fmt.pix.height != current.height ||
  462. fmt.fmt.pix.pixelformat != current.fmt)
  463. g_printerr("Driver returned %dx%d fmt %d\n",
  464. fmt.fmt.pix.width, fmt.fmt.pix.height,
  465. fmt.fmt.pix.pixelformat);
  466. /* Note VIDIOC_S_FMT may change width and height. */
  467. } else {
  468. if (xioctl(fd, VIDIOC_G_FMT, &fmt) == -1) {
  469. errno_exit("VIDIOC_G_FMT");
  470. }
  471. g_print("Got %dx%d fmt %d from the driver\n",
  472. fmt.fmt.pix.width, fmt.fmt.pix.height,
  473. fmt.fmt.pix.pixelformat);
  474. current.width = fmt.fmt.pix.width;
  475. current.height = fmt.fmt.pix.height;
  476. }
  477. current.fmt = fmt.fmt.pix.pixelformat;
  478. /* Buggy driver paranoia. */
  479. unsigned int min = fmt.fmt.pix.width * 2;
  480. if (fmt.fmt.pix.bytesperline < min) {
  481. fmt.fmt.pix.bytesperline = min;
  482. }
  483. min = fmt.fmt.pix.bytesperline * fmt.fmt.pix.height;
  484. if (fmt.fmt.pix.sizeimage < min) {
  485. fmt.fmt.pix.sizeimage = min;
  486. }
  487. init_mmap(fd);
  488. return 0;
  489. }
  490. static void
  491. register_custom_tiff_tags(TIFF *tif)
  492. {
  493. static const TIFFFieldInfo custom_fields[] = {
  494. {TIFFTAG_FORWARDMATRIX1, -1, -1, TIFF_SRATIONAL, FIELD_CUSTOM, 1, 1, "ForwardMatrix1"},
  495. };
  496. // Add missing dng fields
  497. TIFFMergeFieldInfo(tif, custom_fields, sizeof(custom_fields) / sizeof(custom_fields[0]));
  498. }
  499. static void
  500. process_image(const int *p, int size)
  501. {
  502. time_t rawtime;
  503. char datetime[20] = {0};
  504. struct tm tim;
  505. uint8_t *pixels;
  506. char fname[255];
  507. char fname_target[255];
  508. char command[1024];
  509. char timestamp[30];
  510. char uniquecameramodel[255];
  511. GdkPixbuf *pixbuf;
  512. GdkPixbuf *pixbufrot;
  513. GdkPixbuf *thumb;
  514. GError *error = NULL;
  515. double scale;
  516. cairo_t *cr;
  517. TIFF *tif;
  518. int skip = 2;
  519. long sub_offset = 0;
  520. uint64 exif_offset = 0;
  521. static const short cfapatterndim[] = {2, 2};
  522. static const float neutral[] = {1.0, 1.0, 1.0};
  523. static uint16_t isospeed[] = {0};
  524. /*
  525. int sizing = -1;
  526. switch(current.fmt) {
  527. case V4L2_PIX_FMT_SBGGR8:
  528. case V4L2_PIX_FMT_SGBRG8:
  529. case V4L2_PIX_FMT_SGRBG8:
  530. case V4L2_PIX_FMT_SRGGB8:
  531. sizing = 1;
  532. break;
  533. case V4L2_PIX_FMT_UYVY:
  534. sizing = 2;
  535. break;
  536. }
  537. */
  538. // Only process preview frames when not capturing
  539. if (capture == 0) {
  540. if(current.width > 1920) {
  541. skip = 3;
  542. }
  543. if(current.width > 1281) {
  544. skip = 2;
  545. }
  546. pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8, current.width / (skip*2), current.height / (skip*2));
  547. pixels = gdk_pixbuf_get_pixels(pixbuf);
  548. switch(current.fmt) {
  549. case V4L2_PIX_FMT_SBGGR8:
  550. case V4L2_PIX_FMT_SGBRG8:
  551. case V4L2_PIX_FMT_SGRBG8:
  552. case V4L2_PIX_FMT_SRGGB8:
  553. quick_debayer((const uint8_t *)p, pixels, current.fmt,
  554. current.width, current.height, skip,
  555. current.blacklevel);
  556. break;
  557. case V4L2_PIX_FMT_UYVY:
  558. case V4L2_PIX_FMT_YUYV:
  559. quick_yuv2rgb((const uint8_t *)p, pixels, current.fmt,
  560. current.width, current.height, skip);
  561. break;
  562. }
  563. if (current.rotate == 0) {
  564. pixbufrot = pixbuf;
  565. } else if (current.rotate == 90) {
  566. pixbufrot = gdk_pixbuf_rotate_simple(pixbuf, GDK_PIXBUF_ROTATE_COUNTERCLOCKWISE);
  567. } else if (current.rotate == 180) {
  568. pixbufrot = gdk_pixbuf_rotate_simple(pixbuf, GDK_PIXBUF_ROTATE_UPSIDEDOWN);
  569. } else if (current.rotate == 270) {
  570. pixbufrot = gdk_pixbuf_rotate_simple(pixbuf, GDK_PIXBUF_ROTATE_CLOCKWISE);
  571. }
  572. // Draw preview image
  573. scale = (double) preview_width / gdk_pixbuf_get_width(pixbufrot);
  574. cr = cairo_create(surface);
  575. cairo_set_source_rgb(cr, 0, 0, 0);
  576. cairo_paint(cr);
  577. cairo_scale(cr, scale, scale);
  578. gdk_cairo_set_source_pixbuf(cr, pixbufrot, 0, 0);
  579. cairo_pattern_set_extend(cairo_get_source(cr), CAIRO_EXTEND_NONE);
  580. cairo_paint(cr);
  581. // Draw controls over preview
  582. cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
  583. cairo_set_source_surface(cr, status_surface, 0, 0);
  584. cairo_paint(cr);
  585. // Queue gtk3 repaint of the preview area
  586. gtk_widget_queue_draw_area(preview, 0, 0, preview_width, preview_height);
  587. cairo_destroy(cr);
  588. g_object_unref(pixbufrot);
  589. g_object_unref(pixbuf);
  590. } else {
  591. capture--;
  592. time(&rawtime);
  593. tim = *(localtime(&rawtime));
  594. strftime(timestamp, 30, "%Y%m%d%H%M%S", &tim);
  595. strftime(datetime, 20, "%Y:%m:%d %H:%M:%S", &tim);
  596. sprintf(fname_target, "%s/Pictures/IMG%s", getenv("HOME"), timestamp);
  597. sprintf(fname, "%s/%d.dng", burst_dir, burst_length - capture);
  598. // Get latest exposure and gain now the auto gain/exposure is disabled while capturing
  599. gain = v4l2_ctrl_get(current.fd, current.gain_ctrl);
  600. exposure = v4l2_ctrl_get(current.fd, V4L2_CID_EXPOSURE);
  601. if(!(tif = TIFFOpen(fname, "w"))) {
  602. printf("Could not open tiff\n");
  603. }
  604. // Define TIFF thumbnail
  605. TIFFSetField(tif, TIFFTAG_SUBFILETYPE, 1);
  606. TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, current.width >> 4);
  607. TIFFSetField(tif, TIFFTAG_IMAGELENGTH, current.height >> 4);
  608. TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 8);
  609. TIFFSetField(tif, TIFFTAG_COMPRESSION, COMPRESSION_NONE);
  610. TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
  611. TIFFSetField(tif, TIFFTAG_MAKE, exif_make);
  612. TIFFSetField(tif, TIFFTAG_MODEL, exif_model);
  613. TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
  614. TIFFSetField(tif, TIFFTAG_DATETIME, datetime);
  615. TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 3);
  616. TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
  617. TIFFSetField(tif, TIFFTAG_SOFTWARE, "Megapixels");
  618. TIFFSetField(tif, TIFFTAG_SUBIFD, 1, &sub_offset);
  619. TIFFSetField(tif, TIFFTAG_DNGVERSION, "\001\001\0\0");
  620. TIFFSetField(tif, TIFFTAG_DNGBACKWARDVERSION, "\001\0\0\0");
  621. sprintf(uniquecameramodel, "%s %s", exif_make, exif_model);
  622. TIFFSetField(tif, TIFFTAG_UNIQUECAMERAMODEL, uniquecameramodel);
  623. if(current.colormatrix[0]) {
  624. TIFFSetField(tif, TIFFTAG_COLORMATRIX1, 9, current.colormatrix);
  625. } else {
  626. TIFFSetField(tif, TIFFTAG_COLORMATRIX1, 9, colormatrix_srgb);
  627. }
  628. if(current.forwardmatrix[0]) {
  629. TIFFSetField(tif, TIFFTAG_FORWARDMATRIX1, 9, current.forwardmatrix);
  630. }
  631. TIFFSetField(tif, TIFFTAG_ASSHOTNEUTRAL, 3, neutral);
  632. TIFFSetField(tif, TIFFTAG_CALIBRATIONILLUMINANT1, 21);
  633. // Write black thumbnail, only windows uses this
  634. {
  635. unsigned char *buf = (unsigned char *)calloc(1, (int)current.width >> 4);
  636. for (int row = 0; row < current.height>>4; row++) {
  637. TIFFWriteScanline(tif, buf, row, 0);
  638. }
  639. free(buf);
  640. }
  641. TIFFWriteDirectory(tif);
  642. // Define main photo
  643. TIFFSetField(tif, TIFFTAG_SUBFILETYPE, 0);
  644. TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, current.width);
  645. TIFFSetField(tif, TIFFTAG_IMAGELENGTH, current.height);
  646. TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 8);
  647. TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_CFA);
  648. TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 1);
  649. TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
  650. TIFFSetField(tif, TIFFTAG_CFAREPEATPATTERNDIM, cfapatterndim);
  651. TIFFSetField(tif, TIFFTAG_CFAPATTERN, "\002\001\001\000"); // BGGR
  652. if(current.whitelevel) {
  653. TIFFSetField(tif, TIFFTAG_WHITELEVEL, 1, &current.whitelevel);
  654. }
  655. if(current.blacklevel) {
  656. TIFFSetField(tif, TIFFTAG_BLACKLEVEL, 1, &current.blacklevel);
  657. }
  658. TIFFCheckpointDirectory(tif);
  659. printf("Writing frame to %s\n", fname);
  660. unsigned char *pLine = (unsigned char*)malloc(current.width);
  661. for(int row = 0; row < current.height; row++){
  662. TIFFWriteScanline(tif, ((uint8_t *)p)+(row*current.width), row, 0);
  663. }
  664. free(pLine);
  665. TIFFWriteDirectory(tif);
  666. // Add an EXIF block to the tiff
  667. TIFFCreateEXIFDirectory(tif);
  668. // 1 = manual, 2 = full auto, 3 = aperture priority, 4 = shutter priority
  669. if (auto_exposure) {
  670. TIFFSetField(tif, EXIFTAG_EXPOSUREPROGRAM, 2);
  671. } else {
  672. TIFFSetField(tif, EXIFTAG_EXPOSUREPROGRAM, 1);
  673. }
  674. TIFFSetField(tif, EXIFTAG_EXPOSURETIME, (1.0/current.rate) / ((float)current.height / (float)exposure));
  675. isospeed[0] = (uint16_t)remap(gain - 1, 0, current.gain_max, current.iso_min, current.iso_max);
  676. TIFFSetField(tif, EXIFTAG_ISOSPEEDRATINGS, 1, isospeed);
  677. TIFFSetField(tif, EXIFTAG_FLASH, 0);
  678. TIFFSetField(tif, EXIFTAG_DATETIMEORIGINAL, datetime);
  679. TIFFSetField(tif, EXIFTAG_DATETIMEDIGITIZED, datetime);
  680. if(current.fnumber) {
  681. TIFFSetField(tif, EXIFTAG_FNUMBER, current.fnumber);
  682. }
  683. if(current.focallength) {
  684. TIFFSetField(tif, EXIFTAG_FOCALLENGTH, current.focallength);
  685. }
  686. if(current.focallength && current.cropfactor) {
  687. TIFFSetField(tif, EXIFTAG_FOCALLENGTHIN35MMFILM, (short)(current.focallength * current.cropfactor));
  688. }
  689. TIFFWriteCustomDirectory(tif, &exif_offset);
  690. TIFFFreeDirectory(tif);
  691. // Update exif pointer
  692. TIFFSetDirectory(tif, 0);
  693. TIFFSetField(tif, TIFFTAG_EXIFIFD, exif_offset);
  694. TIFFRewriteDirectory(tif);
  695. TIFFClose(tif);
  696. if (capture == 0) {
  697. // Update the thumbnail if this is the last frame
  698. pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8, current.width / (skip*2), current.height / (skip*2));
  699. pixels = gdk_pixbuf_get_pixels(pixbuf);
  700. quick_debayer((const uint8_t *)p, pixels, current.fmt,
  701. current.width, current.height, skip,
  702. current.blacklevel);
  703. if (current.rotate == 0) {
  704. pixbufrot = pixbuf;
  705. } else if (current.rotate == 90) {
  706. pixbufrot = gdk_pixbuf_rotate_simple(pixbuf, GDK_PIXBUF_ROTATE_COUNTERCLOCKWISE);
  707. } else if (current.rotate == 180) {
  708. pixbufrot = gdk_pixbuf_rotate_simple(pixbuf, GDK_PIXBUF_ROTATE_UPSIDEDOWN);
  709. } else if (current.rotate == 270) {
  710. pixbufrot = gdk_pixbuf_rotate_simple(pixbuf, GDK_PIXBUF_ROTATE_CLOCKWISE);
  711. }
  712. thumb = gdk_pixbuf_scale_simple(pixbufrot, 24, 24, GDK_INTERP_BILINEAR);
  713. gtk_image_set_from_pixbuf(GTK_IMAGE(thumb_last), thumb);
  714. sprintf(last_path, "%s.jpg", fname_target);
  715. if (error != NULL) {
  716. g_printerr("%s\n", error->message);
  717. g_clear_error(&error);
  718. }
  719. g_object_unref(pixbufrot);
  720. g_object_unref(pixbuf);
  721. // Start post-processing the captured burst
  722. g_print("Post process %s to %s.ext\n", burst_dir, fname_target);
  723. sprintf(command, "%s %s %s &", processing_script, burst_dir, fname_target);
  724. system(command);
  725. // Restore the auto exposure and gain if needed
  726. if (auto_exposure) {
  727. v4l2_ctrl_set(current.fd, V4L2_CID_EXPOSURE_AUTO, V4L2_EXPOSURE_AUTO);
  728. }
  729. if (auto_gain) {
  730. v4l2_ctrl_set(current.fd, V4L2_CID_AUTOGAIN, 1);
  731. }
  732. }
  733. }
  734. }
  735. static gboolean
  736. preview_draw(GtkWidget *widget, cairo_t *cr, gpointer data)
  737. {
  738. cairo_set_source_surface(cr, surface, 0, 0);
  739. cairo_paint(cr);
  740. return FALSE;
  741. }
  742. static gboolean
  743. preview_configure(GtkWidget *widget, GdkEventConfigure *event)
  744. {
  745. cairo_t *cr;
  746. if (surface)
  747. cairo_surface_destroy(surface);
  748. surface = gdk_window_create_similar_surface(gtk_widget_get_window(widget),
  749. CAIRO_CONTENT_COLOR,
  750. gtk_widget_get_allocated_width(widget),
  751. gtk_widget_get_allocated_height(widget));
  752. preview_width = gtk_widget_get_allocated_width(widget);
  753. preview_height = gtk_widget_get_allocated_height(widget);
  754. cr = cairo_create(surface);
  755. cairo_set_source_rgb(cr, 0, 0, 0);
  756. cairo_paint(cr);
  757. cairo_destroy(cr);
  758. draw_controls();
  759. return TRUE;
  760. }
  761. static int
  762. read_frame(int fd)
  763. {
  764. struct v4l2_buffer buf = {0};
  765. buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  766. buf.memory = V4L2_MEMORY_MMAP;
  767. if (xioctl(fd, VIDIOC_DQBUF, &buf) == -1) {
  768. switch (errno) {
  769. case EAGAIN:
  770. return 0;
  771. case EIO:
  772. /* Could ignore EIO, see spec. */
  773. /* fallthrough */
  774. default:
  775. errno_exit("VIDIOC_DQBUF");
  776. break;
  777. }
  778. }
  779. //assert(buf.index < n_buffers);
  780. process_image(buffers[buf.index].start, buf.bytesused);
  781. if (xioctl(fd, VIDIOC_QBUF, &buf) == -1) {
  782. errno_exit("VIDIOC_QBUF");
  783. }
  784. return 1;
  785. }
  786. gboolean
  787. get_frame()
  788. {
  789. if (ready == 0)
  790. return TRUE;
  791. while (1) {
  792. fd_set fds;
  793. struct timeval tv;
  794. int r;
  795. FD_ZERO(&fds);
  796. FD_SET(current.video_fd, &fds);
  797. /* Timeout. */
  798. tv.tv_sec = 2;
  799. tv.tv_usec = 0;
  800. r = select(current.video_fd + 1, &fds, NULL, NULL, &tv);
  801. if (r == -1) {
  802. if (EINTR == errno) {
  803. continue;
  804. }
  805. errno_exit("select");
  806. } else if (r == 0) {
  807. g_printerr("get_frame: select timeout\n");
  808. return TRUE;
  809. }
  810. if (read_frame(current.video_fd)) {
  811. break;
  812. }
  813. /* EAGAIN - continue select loop. */
  814. }
  815. return TRUE;
  816. }
  817. int
  818. strtoint(const char *nptr, char **endptr, int base)
  819. {
  820. long x = strtol(nptr, endptr, base);
  821. assert(x <= INT_MAX);
  822. return (int) x;
  823. }
  824. static int
  825. config_ini_handler(void *user, const char *section, const char *name,
  826. const char *value)
  827. {
  828. struct camerainfo *cc;
  829. int cid;
  830. int found;
  831. int first_free;
  832. int i;
  833. if (strcmp(section, "device") == 0) {
  834. if (strcmp(name, "make") == 0) {
  835. exif_make = strdup(value);
  836. } else if (strcmp(name, "model") == 0) {
  837. exif_model = strdup(value);
  838. } else {
  839. g_printerr("Unknown key '%s' in [device]\n", name);
  840. exit(1);
  841. }
  842. } else {
  843. found = 0;
  844. first_free = -1;
  845. for (int i=0; i<NUM_CAMERAS; i++) {
  846. if(cameras[i].exists == 1 && strcmp(cameras[i].cfg_name, section) == 0) {
  847. cid = i;
  848. found = 1;
  849. break;
  850. }
  851. if(first_free == -1 && cameras[i].exists != 1) {
  852. first_free = i;
  853. }
  854. }
  855. if (first_free == -1 && found == 0) {
  856. g_printerr("More cameras defined than NUM_CAMERAS\n");
  857. exit(1);
  858. }
  859. if (!found) {
  860. cid = first_free;
  861. strcpy(cameras[cid].cfg_name, section);
  862. cameras[cid].exists = 1;
  863. printf("Adding camera %s from config\n", section);
  864. }
  865. cc = &cameras[cid];
  866. if (strcmp(name, "width") == 0) {
  867. cc->width = strtoint(value, NULL, 10);
  868. } else if (strcmp(name, "height") == 0) {
  869. cc->height = strtoint(value, NULL, 10);
  870. } else if (strcmp(name, "rate") == 0) {
  871. cc->rate = strtoint(value, NULL, 10);
  872. } else if (strcmp(name, "rotate") == 0) {
  873. cc->rotate = strtoint(value, NULL, 10);
  874. } else if (strcmp(name, "fmt") == 0) {
  875. if (strcmp(value, "RGGB8") == 0) {
  876. cc->fmt = V4L2_PIX_FMT_SRGGB8;
  877. cc->mbus = MEDIA_BUS_FMT_SRGGB8_1X8;
  878. } else if (strcmp(value, "BGGR8") == 0) {
  879. cc->fmt = V4L2_PIX_FMT_SBGGR8;
  880. cc->mbus = MEDIA_BUS_FMT_SBGGR8_1X8;
  881. } else if (strcmp(value, "GRBG8") == 0) {
  882. cc->fmt = V4L2_PIX_FMT_SGRBG8;
  883. cc->mbus = MEDIA_BUS_FMT_SGRBG8_1X8;
  884. } else if (strcmp(value, "GBRG8") == 0) {
  885. cc->fmt = V4L2_PIX_FMT_SGBRG8;
  886. cc->mbus = MEDIA_BUS_FMT_SGBRG8_1X8;
  887. } else if (strcmp(value, "UYVY") == 0) {
  888. cc->fmt = V4L2_PIX_FMT_UYVY;
  889. cc->mbus = MEDIA_BUS_FMT_UYVY8_2X8;
  890. } else {
  891. g_printerr("Unsupported pixelformat %s\n", value);
  892. exit(1);
  893. }
  894. } else if (strcmp(name, "driver") == 0) {
  895. strcpy(cc->dev_name, value);
  896. } else if (strcmp(name, "media-driver") == 0) {
  897. strcpy(cc->media_dev_name, value);
  898. } else if (strcmp(name, "media-links") == 0) {
  899. char **linkdefs;
  900. linkdefs = g_strsplit(value, ",", 0);
  901. i = 0;
  902. while (linkdefs[i] != NULL) {
  903. char **linkdef = g_strsplit(linkdefs[i], "->", 2);
  904. char **porta = g_strsplit(linkdef[0], ":", 2);
  905. char **portb = g_strsplit(linkdef[1], ":", 2);
  906. cc->media_links[i].valid = 1;
  907. strcpy(cc->media_links[i].source_name, porta[0]);
  908. strcpy(cc->media_links[i].target_name, portb[0]);
  909. cc->media_links[i].source_port = strtoint(porta[1], NULL, 10);
  910. cc->media_links[i].target_port = strtoint(portb[1], NULL, 10);
  911. g_strfreev(portb);
  912. g_strfreev(porta);
  913. g_strfreev(linkdef);
  914. i++;
  915. }
  916. g_strfreev(linkdefs);
  917. } else if (strcmp(name, "colormatrix") == 0) {
  918. sscanf(value, "%f,%f,%f,%f,%f,%f,%f,%f,%f",
  919. cc->colormatrix+0,
  920. cc->colormatrix+1,
  921. cc->colormatrix+2,
  922. cc->colormatrix+3,
  923. cc->colormatrix+4,
  924. cc->colormatrix+5,
  925. cc->colormatrix+6,
  926. cc->colormatrix+7,
  927. cc->colormatrix+8
  928. );
  929. } else if (strcmp(name, "forwardmatrix") == 0) {
  930. sscanf(value, "%f,%f,%f,%f,%f,%f,%f,%f,%f",
  931. cc->forwardmatrix+0,
  932. cc->forwardmatrix+1,
  933. cc->forwardmatrix+2,
  934. cc->forwardmatrix+3,
  935. cc->forwardmatrix+4,
  936. cc->forwardmatrix+5,
  937. cc->forwardmatrix+6,
  938. cc->forwardmatrix+7,
  939. cc->forwardmatrix+8
  940. );
  941. } else if (strcmp(name, "whitelevel") == 0) {
  942. cc->whitelevel = strtoint(value, NULL, 10);
  943. } else if (strcmp(name, "blacklevel") == 0) {
  944. cc->blacklevel = strtoint(value, NULL, 10);
  945. } else if (strcmp(name, "focallength") == 0) {
  946. cc->focallength = strtof(value, NULL);
  947. } else if (strcmp(name, "cropfactor") == 0) {
  948. cc->cropfactor = strtof(value, NULL);
  949. } else if (strcmp(name, "fnumber") == 0) {
  950. cc->fnumber = strtod(value, NULL);
  951. } else if (strcmp(name, "iso-min") == 0) {
  952. cc->iso_min = strtod(value, NULL);
  953. } else if (strcmp(name, "iso-max") == 0) {
  954. cc->iso_max = strtod(value, NULL);
  955. } else {
  956. g_printerr("Unknown key '%s' in [%s]\n", name, section);
  957. exit(1);
  958. }
  959. }
  960. return 1;
  961. }
  962. int
  963. find_dev_node(int maj, int min, char *fnbuf)
  964. {
  965. DIR *d;
  966. struct dirent *dir;
  967. struct stat info;
  968. d = opendir("/dev");
  969. while ((dir = readdir(d)) != NULL) {
  970. sprintf(fnbuf, "/dev/%s", dir->d_name);
  971. stat(fnbuf, &info);
  972. if (!S_ISCHR(info.st_mode)) {
  973. continue;
  974. }
  975. if (major(info.st_rdev) == maj && minor(info.st_rdev) == min) {
  976. return 0;
  977. }
  978. }
  979. return -1;
  980. }
  981. int
  982. setup_camera(int cid)
  983. {
  984. struct media_link_desc link = {0};
  985. // Kill existing links for cameras in the same graph
  986. for(int i=0; i<NUM_CAMERAS; i++) {
  987. if(!cameras[i].exists)
  988. continue;
  989. if(i == cid)
  990. continue;
  991. if(strcmp(cameras[i].media_dev_fname, cameras[cid].media_dev_fname) != 0)
  992. continue;
  993. // Disable the interface<->front link
  994. link.flags = 0;
  995. link.source.entity = cameras[i].entity_id;
  996. link.source.index = 0;
  997. link.sink.entity = cameras[i].interface_entity_id;
  998. link.sink.index = 0;
  999. if (xioctl(cameras[cid].media_fd, MEDIA_IOC_SETUP_LINK, &link) < 0) {
  1000. g_printerr("Could not disable [%s] camera link\n", cameras[i].cfg_name);
  1001. return -1;
  1002. }
  1003. }
  1004. // Enable the interface<->sensor link
  1005. link.flags = MEDIA_LNK_FL_ENABLED;
  1006. link.source.entity = cameras[cid].entity_id;
  1007. link.source.index = 0;
  1008. link.sink.entity = cameras[cid].interface_entity_id;
  1009. link.sink.index = 0;
  1010. if (xioctl(cameras[cid].media_fd, MEDIA_IOC_SETUP_LINK, &link) < 0) {
  1011. g_printerr("[%s] Could not enable direct sensor->if link\n", cameras[cid].cfg_name);
  1012. for(int i=0;i<10; i++) {
  1013. if (!cameras[cid].media_links[i].valid)
  1014. continue;
  1015. if (cameras[cid].media_links[i].source_entity_id < 1) {
  1016. g_printerr("[%s] media entry [%s] not found\n",
  1017. cameras[cid].cfg_name,
  1018. cameras[cid].media_links[i].source_name);
  1019. }
  1020. if (cameras[cid].media_links[i].target_entity_id < 1) {
  1021. g_printerr("[%s] media entry [%s] not found\n",
  1022. cameras[cid].cfg_name,
  1023. cameras[cid].media_links[i].target_name);
  1024. }
  1025. link.flags = MEDIA_LNK_FL_ENABLED;
  1026. link.source.entity = cameras[cid].media_links[i].source_entity_id;
  1027. link.source.index = cameras[cid].media_links[i].source_port;
  1028. link.sink.entity = cameras[cid].media_links[i].target_entity_id;
  1029. link.sink.index = cameras[cid].media_links[i].target_port;
  1030. if (xioctl(cameras[cid].media_fd, MEDIA_IOC_SETUP_LINK, &link) < 0) {
  1031. g_printerr("[%s] Could not link [%s:%d] -> [%s:%d]\n",
  1032. cameras[cid].cfg_name,
  1033. cameras[cid].media_links[i].source_name,
  1034. cameras[cid].media_links[i].source_port,
  1035. cameras[cid].media_links[i].target_name,
  1036. cameras[cid].media_links[i].target_port);
  1037. }
  1038. }
  1039. }
  1040. current = cameras[cid];
  1041. // Find camera node
  1042. init_sensor(current.dev_fname, current.width, current.height, current.mbus, current.rate);
  1043. return 0;
  1044. }
  1045. int
  1046. find_camera(int cid)
  1047. {
  1048. struct media_entity_desc entity = {0};
  1049. DIR *d;
  1050. struct dirent *dir;
  1051. int fd;
  1052. char fnbuf[261];
  1053. struct media_device_info mdi = {0};
  1054. int ret;
  1055. int found_subdev = 0;
  1056. int found_interface = 0;
  1057. // find the /dev/media node for the camera media-driver
  1058. d = opendir("/dev");
  1059. while ((dir = readdir(d)) != NULL) {
  1060. if (strncmp(dir->d_name, "media", 5) == 0) {
  1061. sprintf(fnbuf, "/dev/%s", dir->d_name);
  1062. fd = open(fnbuf, O_RDWR);
  1063. xioctl(fd, MEDIA_IOC_DEVICE_INFO, &mdi);
  1064. if (strcmp(mdi.driver, cameras[cid].media_dev_name) == 0) {
  1065. printf("[%s] media device: %s (%s)\n", cameras[cid].cfg_name, fnbuf, mdi.driver);
  1066. cameras[cid].media_fd = fd;
  1067. goto find_camera_found_media;
  1068. }
  1069. close(fd);
  1070. }
  1071. }
  1072. g_printerr("Could not find /dev/media* node matching '%s'\n", cameras[cid].media_dev_name);
  1073. return 0;
  1074. find_camera_found_media:
  1075. // inspect the media node and find the sensor
  1076. while (1) {
  1077. entity.id = entity.id | MEDIA_ENT_ID_FLAG_NEXT;
  1078. ret = xioctl(fd, MEDIA_IOC_ENUM_ENTITIES, &entity);
  1079. if (ret < 0) {
  1080. break;
  1081. }
  1082. if (!found_subdev && strncmp(entity.name, cameras[cid].dev_name, strlen(cameras[cid].dev_name)) == 0) {
  1083. cameras[cid].entity_id = entity.id;
  1084. find_dev_node(entity.dev.major, entity.dev.minor, cameras[cid].dev_fname);
  1085. printf("[%s] subdev: %s (%s)\n", cameras[cid].cfg_name, cameras[cid].dev_fname, entity.name);
  1086. found_subdev = 1;
  1087. }
  1088. if (!found_interface && entity.type == MEDIA_ENT_F_IO_V4L) {
  1089. cameras[cid].interface_entity_id = entity.id;
  1090. find_dev_node(entity.dev.major, entity.dev.minor, cameras[cid].video_dev_fname);
  1091. printf("[%s] video: %s (%s)\n", cameras[cid].cfg_name, cameras[cid].video_dev_fname, entity.name);
  1092. found_interface = 1;
  1093. }
  1094. for (int i=0; i<NUM_LINKS; i++) {
  1095. if(!cameras[cid].media_links[i].valid)
  1096. continue;
  1097. if(strncmp(entity.name, cameras[cid].media_links[i].source_name,
  1098. strlen(cameras[cid].media_links[i].source_name)) == 0) {
  1099. cameras[cid].media_links[i].source_entity_id = entity.id;
  1100. }
  1101. if(strncmp(entity.name, cameras[cid].media_links[i].target_name,
  1102. strlen(cameras[cid].media_links[i].target_name)) == 0) {
  1103. cameras[cid].media_links[i].target_entity_id = entity.id;
  1104. }
  1105. }
  1106. }
  1107. if (!found_subdev) {
  1108. g_printerr("[%s] Could not find subdev '%s'\n", cameras[cid].cfg_name, cameras[cid].dev_name);
  1109. return 0;
  1110. }
  1111. if (!found_interface) {
  1112. g_printerr("[%s] Could not find interface node\n", cameras[cid].cfg_name);
  1113. return 0;
  1114. }
  1115. return 1;
  1116. }
  1117. int
  1118. find_cameras()
  1119. {
  1120. int found_one = 0;
  1121. for(int i=0; i<NUM_CAMERAS; i++) {
  1122. if(!cameras[i].exists)
  1123. continue;
  1124. if(find_camera(i)) {
  1125. found_one = 1;
  1126. } else {
  1127. cameras[i].exists = 0;
  1128. }
  1129. }
  1130. return found_one;
  1131. }
  1132. void
  1133. on_open_last_clicked(GtkWidget *widget, gpointer user_data)
  1134. {
  1135. char uri[270];
  1136. GError *error = NULL;
  1137. if(strlen(last_path) == 0) {
  1138. return;
  1139. }
  1140. sprintf(uri, "file://%s", last_path);
  1141. if(!g_app_info_launch_default_for_uri(uri, NULL, &error)){
  1142. g_printerr("Could not launch image viewer: %s\n", error->message);
  1143. }
  1144. }
  1145. void
  1146. on_open_directory_clicked(GtkWidget *widget, gpointer user_data)
  1147. {
  1148. char uri[270];
  1149. GError *error = NULL;
  1150. sprintf(uri, "file://%s/Pictures", getenv("HOME"));
  1151. if(!g_app_info_launch_default_for_uri(uri, NULL, &error)){
  1152. g_printerr("Could not launch image viewer: %s\n", error->message);
  1153. }
  1154. }
  1155. void
  1156. on_shutter_clicked(GtkWidget *widget, gpointer user_data)
  1157. {
  1158. char template[] = "/tmp/megapixels.XXXXXX";
  1159. char *tempdir;
  1160. tempdir = mkdtemp(template);
  1161. if (tempdir == NULL) {
  1162. g_printerr("Could not make capture directory %s\n", template);
  1163. exit (EXIT_FAILURE);
  1164. }
  1165. strcpy(burst_dir, tempdir);
  1166. // Disable the autogain/exposure while taking the burst
  1167. v4l2_ctrl_set(current.fd, V4L2_CID_AUTOGAIN, 0);
  1168. v4l2_ctrl_set(current.fd, V4L2_CID_EXPOSURE_AUTO, V4L2_EXPOSURE_MANUAL);
  1169. capture = burst_length;
  1170. }
  1171. void
  1172. on_preview_tap(GtkWidget *widget, GdkEventButton *event, gpointer user_data)
  1173. {
  1174. if (event->type != GDK_BUTTON_PRESS)
  1175. return;
  1176. // Handle taps on the controls
  1177. if (event->y < 32) {
  1178. if (gtk_widget_is_visible(control_box)) {
  1179. gtk_widget_hide(control_box);
  1180. return;
  1181. } else {
  1182. gtk_widget_show(control_box);
  1183. }
  1184. if (event->x < 60 ) {
  1185. // ISO
  1186. current_control = USER_CONTROL_ISO;
  1187. gtk_label_set_text(GTK_LABEL(control_name), "ISO");
  1188. gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(control_auto), auto_gain);
  1189. gtk_adjustment_set_lower(control_slider, 0.0);
  1190. gtk_adjustment_set_upper(control_slider, (float)current.gain_max);
  1191. gtk_adjustment_set_value(control_slider, (double)gain);
  1192. } else if (event->x > 60 && event->x < 120) {
  1193. // Shutter angle
  1194. current_control = USER_CONTROL_SHUTTER;
  1195. gtk_label_set_text(GTK_LABEL(control_name), "Shutter");
  1196. gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(control_auto), auto_exposure);
  1197. gtk_adjustment_set_lower(control_slider, 1.0);
  1198. gtk_adjustment_set_upper(control_slider, 360.0);
  1199. gtk_adjustment_set_value(control_slider, (double)exposure);
  1200. }
  1201. return;
  1202. }
  1203. // Tapped preview image itself, try focussing
  1204. if (current.has_af_s) {
  1205. v4l2_ctrl_set(current.fd, V4L2_CID_AUTO_FOCUS_STOP, 1);
  1206. v4l2_ctrl_set(current.fd, V4L2_CID_AUTO_FOCUS_START, 1);
  1207. }
  1208. }
  1209. void
  1210. on_error_close_clicked(GtkWidget *widget, gpointer user_data)
  1211. {
  1212. gtk_widget_hide(error_box);
  1213. }
  1214. void
  1215. on_camera_switch_clicked(GtkWidget *widget, gpointer user_data)
  1216. {
  1217. int found_next = 0;
  1218. int old_cid = current_cid;
  1219. int next_cid = -1;
  1220. for(int i=current_cid; i<NUM_CAMERAS; i++) {
  1221. if(i == current_cid)
  1222. continue;
  1223. if(!cameras[i].exists)
  1224. continue;
  1225. found_next = 1;
  1226. next_cid = i;
  1227. }
  1228. if(!found_next) {
  1229. for(int i=0; i<current_cid; i++) {
  1230. if(i == current_cid)
  1231. continue;
  1232. if(!cameras[i].exists)
  1233. continue;
  1234. found_next = 1;
  1235. next_cid = i;
  1236. }
  1237. }
  1238. if(!found_next) {
  1239. g_printerr("Could not find a candidate camera to switch to\n");
  1240. return;
  1241. }
  1242. printf("Switching from [%s] to [%s]\n", cameras[current_cid].cfg_name, cameras[next_cid].cfg_name);
  1243. stop_capturing(cameras[current_cid].video_fd);
  1244. close(cameras[current_cid].fd);
  1245. setup_camera(next_cid);
  1246. close(cameras[old_cid].video_fd);
  1247. cameras[next_cid].video_fd = open(cameras[next_cid].video_dev_fname, O_RDWR);
  1248. if (cameras[next_cid].video_fd == -1) {
  1249. g_printerr("Error opening video device: %s\n", cameras[next_cid].video_dev_fname);
  1250. return;
  1251. }
  1252. current_cid = next_cid;
  1253. init_device(cameras[current_cid].video_fd);
  1254. start_capturing(cameras[current_cid].video_fd);
  1255. }
  1256. void
  1257. on_settings_btn_clicked(GtkWidget *widget, gpointer user_data)
  1258. {
  1259. gtk_stack_set_visible_child_name(GTK_STACK(main_stack), "settings");
  1260. }
  1261. void
  1262. on_back_clicked(GtkWidget *widget, gpointer user_data)
  1263. {
  1264. gtk_stack_set_visible_child_name(GTK_STACK(main_stack), "main");
  1265. }
  1266. void
  1267. on_control_auto_toggled(GtkToggleButton *widget, gpointer user_data)
  1268. {
  1269. int fd = current.fd;
  1270. switch (current_control) {
  1271. case USER_CONTROL_ISO:
  1272. auto_gain = gtk_toggle_button_get_active(widget);
  1273. if (auto_gain) {
  1274. v4l2_ctrl_set(fd, V4L2_CID_AUTOGAIN, 1);
  1275. } else {
  1276. v4l2_ctrl_set(fd, V4L2_CID_AUTOGAIN, 0);
  1277. gain = v4l2_ctrl_get(fd, V4L2_CID_GAIN);
  1278. gtk_adjustment_set_value(control_slider, (double)gain);
  1279. }
  1280. break;
  1281. case USER_CONTROL_SHUTTER:
  1282. auto_exposure = gtk_toggle_button_get_active(widget);
  1283. if (auto_exposure) {
  1284. v4l2_ctrl_set(fd, V4L2_CID_EXPOSURE_AUTO, V4L2_EXPOSURE_AUTO);
  1285. } else {
  1286. v4l2_ctrl_set(fd, V4L2_CID_EXPOSURE_AUTO, V4L2_EXPOSURE_MANUAL);
  1287. exposure = v4l2_ctrl_get(fd, V4L2_CID_EXPOSURE);
  1288. gtk_adjustment_set_value(control_slider, (double)exposure);
  1289. }
  1290. break;
  1291. }
  1292. draw_controls();
  1293. }
  1294. void
  1295. on_control_slider_changed(GtkAdjustment *widget, gpointer user_data)
  1296. {
  1297. double value = gtk_adjustment_get_value(widget);
  1298. switch (current_control) {
  1299. case USER_CONTROL_ISO:
  1300. gain = (int)value;
  1301. v4l2_ctrl_set(current.fd, current.gain_ctrl, gain);
  1302. break;
  1303. case USER_CONTROL_SHUTTER:
  1304. // So far all sensors use exposure time in number of sensor rows
  1305. exposure = (int)(value / 360.0 * current.height);
  1306. v4l2_ctrl_set(current.fd, V4L2_CID_EXPOSURE, exposure);
  1307. break;
  1308. }
  1309. draw_controls();
  1310. }
  1311. int
  1312. find_config(char *conffile)
  1313. {
  1314. char buf[512];
  1315. char *xdg_config_home;
  1316. wordexp_t exp_result;
  1317. FILE *fp;
  1318. // Resolve XDG stuff
  1319. if ((xdg_config_home = getenv("XDG_CONFIG_HOME")) == NULL) {
  1320. xdg_config_home = "~/.config";
  1321. }
  1322. wordexp(xdg_config_home, &exp_result, 0);
  1323. xdg_config_home = strdup(exp_result.we_wordv[0]);
  1324. wordfree(&exp_result);
  1325. if(access("/proc/device-tree/compatible", F_OK) != -1) {
  1326. // Reads to compatible string of the current device tree, looks like:
  1327. // pine64,pinephone-1.2\0allwinner,sun50i-a64\0
  1328. fp = fopen("/proc/device-tree/compatible", "r");
  1329. fgets(buf, 512, fp);
  1330. fclose(fp);
  1331. // Check config/%dt.ini in the current working directory
  1332. sprintf(conffile, "config/%s.ini", buf);
  1333. if(access(conffile, F_OK) != -1) {
  1334. printf("Found config file at %s\n", conffile);
  1335. return 0;
  1336. }
  1337. // Check for a config file in XDG_CONFIG_HOME
  1338. sprintf(conffile, "%s/megapixels/config/%s.ini", xdg_config_home, buf);
  1339. if(access(conffile, F_OK) != -1) {
  1340. printf("Found config file at %s\n", conffile);
  1341. return 0;
  1342. }
  1343. // Check user overridden /etc/megapixels/config/$dt.ini
  1344. sprintf(conffile, "%s/megapixels/config/%s.ini", SYSCONFDIR, buf);
  1345. if(access(conffile, F_OK) != -1) {
  1346. printf("Found config file at %s\n", conffile);
  1347. return 0;
  1348. }
  1349. // Check packaged /usr/share/megapixels/config/$dt.ini
  1350. sprintf(conffile, "%s/megapixels/config/%s.ini", DATADIR, buf);
  1351. if(access(conffile, F_OK) != -1) {
  1352. printf("Found config file at %s\n", conffile);
  1353. return 0;
  1354. }
  1355. printf("%s not found\n", conffile);
  1356. } else {
  1357. printf("Could not read device name from device tree\n");
  1358. }
  1359. // If all else fails, fall back to /etc/megapixels.ini
  1360. sprintf(conffile, "/etc/megapixels.ini");
  1361. if(access(conffile, F_OK) != -1) {
  1362. printf("Found config file at %s\n", conffile);
  1363. return 0;
  1364. }
  1365. return -1;
  1366. }
  1367. int
  1368. find_processor(char *script)
  1369. {
  1370. char *xdg_config_home;
  1371. char filename[] = "postprocess.sh";
  1372. wordexp_t exp_result;
  1373. // Resolve XDG stuff
  1374. if ((xdg_config_home = getenv("XDG_CONFIG_HOME")) == NULL) {
  1375. xdg_config_home = "~/.config";
  1376. }
  1377. wordexp(xdg_config_home, &exp_result, 0);
  1378. xdg_config_home = strdup(exp_result.we_wordv[0]);
  1379. wordfree(&exp_result);
  1380. // Check postprocess.h in the current working directory
  1381. sprintf(script, "%s", filename);
  1382. if(access(script, F_OK) != -1) {
  1383. sprintf(script, "./%s", filename);
  1384. printf("Found postprocessor script at %s\n", script);
  1385. return 0;
  1386. }
  1387. // Check for a script in XDG_CONFIG_HOME
  1388. sprintf(script, "%s/megapixels/%s", xdg_config_home, filename);
  1389. if(access(script, F_OK) != -1) {
  1390. printf("Found postprocessor script at %s\n", script);
  1391. return 0;
  1392. }
  1393. // Check user overridden /etc/megapixels/postprocessor.sh
  1394. sprintf(script, "%s/megapixels/%s", SYSCONFDIR, filename);
  1395. if(access(script, F_OK) != -1) {
  1396. printf("Found postprocessor script at %s\n", script);
  1397. return 0;
  1398. }
  1399. // Check packaged /usr/share/megapixels/postprocessor.sh
  1400. sprintf(script, "%s/megapixels/%s", DATADIR, filename);
  1401. if(access(script, F_OK) != -1) {
  1402. printf("Found postprocessor script at %s\n", script);
  1403. return 0;
  1404. }
  1405. return -1;
  1406. }
  1407. int
  1408. main(int argc, char *argv[])
  1409. {
  1410. int ret;
  1411. char conffile[512];
  1412. ret = find_config(conffile);
  1413. if (ret) {
  1414. g_printerr("Could not find any config file\n");
  1415. return ret;
  1416. }
  1417. ret = find_processor(processing_script);
  1418. if (ret) {
  1419. g_printerr("Could not find any post-process script\n");
  1420. return ret;
  1421. }
  1422. setenv("LC_NUMERIC", "C", 1);
  1423. TIFFSetTagExtender(register_custom_tiff_tags);
  1424. gtk_init(&argc, &argv);
  1425. g_object_set(gtk_settings_get_default(), "gtk-application-prefer-dark-theme", TRUE, NULL);
  1426. GtkBuilder *builder = gtk_builder_new_from_resource("/org/postmarketos/Megapixels/camera.glade");
  1427. GtkWidget *window = GTK_WIDGET(gtk_builder_get_object(builder, "window"));
  1428. GtkWidget *shutter = GTK_WIDGET(gtk_builder_get_object(builder, "shutter"));
  1429. GtkWidget *switch_btn = GTK_WIDGET(gtk_builder_get_object(builder, "switch_camera"));
  1430. GtkWidget *settings_btn = GTK_WIDGET(gtk_builder_get_object(builder, "settings"));
  1431. GtkWidget *settings_back = GTK_WIDGET(gtk_builder_get_object(builder, "settings_back"));
  1432. GtkWidget *error_close = GTK_WIDGET(gtk_builder_get_object(builder, "error_close"));
  1433. GtkWidget *open_last = GTK_WIDGET(gtk_builder_get_object(builder, "open_last"));
  1434. GtkWidget *open_directory = GTK_WIDGET(gtk_builder_get_object(builder, "open_directory"));
  1435. preview = GTK_WIDGET(gtk_builder_get_object(builder, "preview"));
  1436. error_box = GTK_WIDGET(gtk_builder_get_object(builder, "error_box"));
  1437. error_message = GTK_WIDGET(gtk_builder_get_object(builder, "error_message"));
  1438. main_stack = GTK_WIDGET(gtk_builder_get_object(builder, "main_stack"));
  1439. thumb_last = GTK_WIDGET(gtk_builder_get_object(builder, "thumb_last"));
  1440. control_box = GTK_WIDGET(gtk_builder_get_object(builder, "control_box"));
  1441. control_name = GTK_WIDGET(gtk_builder_get_object(builder, "control_name"));
  1442. control_slider = GTK_ADJUSTMENT(gtk_builder_get_object(builder, "control_adj"));
  1443. control_auto = GTK_WIDGET(gtk_builder_get_object(builder, "control_auto"));
  1444. g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);
  1445. g_signal_connect(shutter, "clicked", G_CALLBACK(on_shutter_clicked), NULL);
  1446. g_signal_connect(error_close, "clicked", G_CALLBACK(on_error_close_clicked), NULL);
  1447. g_signal_connect(switch_btn, "clicked", G_CALLBACK(on_camera_switch_clicked), NULL);
  1448. g_signal_connect(settings_btn, "clicked", G_CALLBACK(on_settings_btn_clicked), NULL);
  1449. g_signal_connect(settings_back, "clicked", G_CALLBACK(on_back_clicked), NULL);
  1450. g_signal_connect(open_last, "clicked", G_CALLBACK(on_open_last_clicked), NULL);
  1451. g_signal_connect(open_directory, "clicked", G_CALLBACK(on_open_directory_clicked), NULL);
  1452. g_signal_connect(preview, "draw", G_CALLBACK(preview_draw), NULL);
  1453. g_signal_connect(preview, "configure-event", G_CALLBACK(preview_configure), NULL);
  1454. gtk_widget_set_events(preview, gtk_widget_get_events(preview) |
  1455. GDK_BUTTON_PRESS_MASK | GDK_POINTER_MOTION_MASK);
  1456. g_signal_connect(preview, "button-press-event", G_CALLBACK(on_preview_tap), NULL);
  1457. g_signal_connect(control_auto, "toggled", G_CALLBACK(on_control_auto_toggled), NULL);
  1458. g_signal_connect(control_slider, "value-changed", G_CALLBACK(on_control_slider_changed), NULL);
  1459. GtkCssProvider *provider = gtk_css_provider_new();
  1460. if (access("camera.css", F_OK) != -1) {
  1461. gtk_css_provider_load_from_path(provider, "camera.css", NULL);
  1462. } else {
  1463. gtk_css_provider_load_from_resource(provider, "/org/postmarketos/Megapixels/camera.css");
  1464. }
  1465. GtkStyleContext *context = gtk_widget_get_style_context(error_box);
  1466. gtk_style_context_add_provider(context,
  1467. GTK_STYLE_PROVIDER(provider),
  1468. GTK_STYLE_PROVIDER_PRIORITY_USER);
  1469. context = gtk_widget_get_style_context(control_box);
  1470. gtk_style_context_add_provider(context,
  1471. GTK_STYLE_PROVIDER(provider),
  1472. GTK_STYLE_PROVIDER_PRIORITY_USER);
  1473. int result = ini_parse(conffile, config_ini_handler, NULL);
  1474. if (result == -1) {
  1475. g_printerr("Config file not found\n");
  1476. return 1;
  1477. } else if (result == -2) {
  1478. g_printerr("Could not allocate memory to parse config file\n");
  1479. return 1;
  1480. } else if (result != 0) {
  1481. g_printerr("Could not parse config file\n");
  1482. return 1;
  1483. }
  1484. if (find_cameras() == 0) {
  1485. g_printerr("Could not find the cameras\n");
  1486. show_error("Could not find the cameras");
  1487. goto failed;
  1488. }
  1489. // Disable the camera switch button if only one camera exists
  1490. int camera_count = 0;
  1491. for (int i=0; i<NUM_CAMERAS; i++) {
  1492. if(cameras[i].exists)
  1493. camera_count++;
  1494. }
  1495. if (camera_count < 2) {
  1496. gtk_widget_set_sensitive(switch_btn, FALSE);
  1497. }
  1498. // Setup first defined camera
  1499. for(int i=0;i<NUM_CAMERAS; i++) {
  1500. if(cameras[i].exists){
  1501. setup_camera(i);
  1502. current_cid = i;
  1503. cameras[i].video_fd = open(cameras[i].video_dev_fname, O_RDWR);
  1504. if (cameras[i].video_fd == -1) {
  1505. g_printerr("Error opening video device: %s\n", cameras[i].video_dev_fname);
  1506. show_error("Error opening the video device");
  1507. goto failed;
  1508. }
  1509. if(init_device(cameras[i].video_fd) < 0){
  1510. goto failed;
  1511. }
  1512. start_capturing(cameras[i].video_fd);
  1513. break;
  1514. }
  1515. }
  1516. failed:
  1517. printf("window show\n");
  1518. gtk_widget_show(window);
  1519. g_idle_add((GSourceFunc)get_frame, NULL);
  1520. gtk_main();
  1521. return 0;
  1522. }