main.c 50 KB

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