main.c 41 KB

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