main.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245
  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 "config.h"
  19. #include "ini.h"
  20. #include "quickdebayer.h"
  21. enum io_method {
  22. IO_METHOD_READ,
  23. IO_METHOD_MMAP,
  24. IO_METHOD_USERPTR,
  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. };
  50. static float colormatrix_srgb[] = {
  51. 3.2409, -1.5373, -0.4986,
  52. -0.9692, 1.8759, 0.0415,
  53. 0.0556, -0.2039, 1.0569
  54. };
  55. struct buffer *buffers;
  56. static unsigned int n_buffers;
  57. struct camerainfo rear_cam;
  58. struct camerainfo front_cam;
  59. struct camerainfo current;
  60. // Camera interface
  61. static char *media_drv_name;
  62. static unsigned int interface_entity_id;
  63. static char dev_name[260];
  64. static int media_fd;
  65. static int video_fd;
  66. static char *exif_make;
  67. static char *exif_model;
  68. // State
  69. static int ready = 0;
  70. static int capture = 0;
  71. static int current_is_rear = 1;
  72. static cairo_surface_t *surface = NULL;
  73. static int preview_width = -1;
  74. static int preview_height = -1;
  75. static char *last_path = NULL;
  76. static int auto_exposure = 1;
  77. static int auto_gain = 1;
  78. static int burst_length = 5;
  79. static char burst_dir[20];
  80. static char processing_script[512];
  81. // Widgets
  82. GtkWidget *preview;
  83. GtkWidget *error_box;
  84. GtkWidget *error_message;
  85. GtkWidget *main_stack;
  86. GtkWidget *thumb_last;
  87. static int
  88. xioctl(int fd, int request, void *arg)
  89. {
  90. int r;
  91. do {
  92. r = ioctl(fd, request, arg);
  93. } while (r == -1 && errno == EINTR);
  94. return r;
  95. }
  96. static void
  97. errno_exit(const char *s)
  98. {
  99. fprintf(stderr, "%s error %d, %s\n", s, errno, strerror(errno));
  100. exit(EXIT_FAILURE);
  101. }
  102. static void
  103. show_error(const char *s)
  104. {
  105. gtk_label_set_text(GTK_LABEL(error_message), s);
  106. gtk_widget_show(error_box);
  107. }
  108. static void
  109. start_capturing(int fd)
  110. {
  111. enum v4l2_buf_type type;
  112. for (int i = 0; i < n_buffers; ++i) {
  113. struct v4l2_buffer buf = {
  114. .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
  115. .memory = V4L2_MEMORY_MMAP,
  116. .index = i,
  117. };
  118. if (xioctl(fd, VIDIOC_QBUF, &buf) == -1) {
  119. errno_exit("VIDIOC_QBUF");
  120. }
  121. }
  122. type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  123. if (xioctl(fd, VIDIOC_STREAMON, &type) == -1) {
  124. errno_exit("VIDIOC_STREAMON");
  125. }
  126. ready = 1;
  127. }
  128. static void
  129. stop_capturing(int fd)
  130. {
  131. int i;
  132. ready = 0;
  133. printf("Stopping capture\n");
  134. enum v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  135. if (xioctl(fd, VIDIOC_STREAMOFF, &type) == -1) {
  136. errno_exit("VIDIOC_STREAMOFF");
  137. }
  138. for (i = 0; i < n_buffers; ++i) {
  139. munmap(buffers[i].start, buffers[i].length);
  140. }
  141. }
  142. static void
  143. init_mmap(int fd)
  144. {
  145. struct v4l2_requestbuffers req = {0};
  146. req.count = 4;
  147. req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  148. req.memory = V4L2_MEMORY_MMAP;
  149. if (xioctl(fd, VIDIOC_REQBUFS, &req) == -1) {
  150. if (errno == EINVAL) {
  151. fprintf(stderr, "%s does not support memory mapping",
  152. dev_name);
  153. exit(EXIT_FAILURE);
  154. } else {
  155. errno_exit("VIDIOC_REQBUFS");
  156. }
  157. }
  158. if (req.count < 2) {
  159. fprintf(stderr, "Insufficient buffer memory on %s\n",
  160. dev_name);
  161. exit(EXIT_FAILURE);
  162. }
  163. buffers = calloc(req.count, sizeof(buffers[0]));
  164. if (!buffers) {
  165. fprintf(stderr, "Out of memory\\n");
  166. exit(EXIT_FAILURE);
  167. }
  168. for (n_buffers = 0; n_buffers < req.count; ++n_buffers) {
  169. struct v4l2_buffer buf = {
  170. .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
  171. .memory = V4L2_MEMORY_MMAP,
  172. .index = n_buffers,
  173. };
  174. if (xioctl(fd, VIDIOC_QUERYBUF, &buf) == -1) {
  175. errno_exit("VIDIOC_QUERYBUF");
  176. }
  177. buffers[n_buffers].length = buf.length;
  178. buffers[n_buffers].start = mmap(NULL /* start anywhere */,
  179. buf.length,
  180. PROT_READ | PROT_WRITE /* required */,
  181. MAP_SHARED /* recommended */,
  182. fd, buf.m.offset);
  183. if (MAP_FAILED == buffers[n_buffers].start) {
  184. errno_exit("mmap");
  185. }
  186. }
  187. }
  188. static int
  189. v4l2_ctrl_set(int fd, uint32_t id, int val)
  190. {
  191. struct v4l2_control ctrl = {0};
  192. ctrl.id = id;
  193. ctrl.value = val;
  194. if (xioctl(fd, VIDIOC_S_CTRL, &ctrl) == -1) {
  195. g_printerr("Failed to set control %d to %d\n", id, val);
  196. return -1;
  197. }
  198. return 0;
  199. }
  200. static void
  201. init_sensor(char *fn, int width, int height, int mbus, int rate)
  202. {
  203. int fd;
  204. struct v4l2_subdev_frame_interval interval;
  205. struct v4l2_subdev_format fmt;
  206. fd = open(fn, O_RDWR);
  207. g_printerr("Setting sensor rate to %d\n", rate);
  208. interval.pad = 0;
  209. interval.interval.numerator = 1;
  210. interval.interval.denominator = rate;
  211. if (xioctl(fd, VIDIOC_SUBDEV_S_FRAME_INTERVAL, &interval) == -1) {
  212. errno_exit("VIDIOC_SUBDEV_S_FRAME_INTERVAL");
  213. }
  214. g_printerr("Driver returned %d/%d frameinterval\n",
  215. interval.interval.numerator, interval.interval.denominator);
  216. g_printerr("Setting sensor to %dx%d fmt %d\n",
  217. width, height, mbus);
  218. fmt.pad = 0;
  219. fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
  220. fmt.format.code = mbus;
  221. fmt.format.width = width;
  222. fmt.format.height = height;
  223. fmt.format.field = V4L2_FIELD_ANY;
  224. if (xioctl(fd, VIDIOC_SUBDEV_S_FMT, &fmt) == -1) {
  225. errno_exit("VIDIOC_SUBDEV_S_FMT");
  226. }
  227. g_printerr("Driver returned %dx%d fmt %d\n",
  228. fmt.format.width, fmt.format.height,
  229. fmt.format.code);
  230. if (auto_exposure) {
  231. v4l2_ctrl_set(fd, V4L2_CID_EXPOSURE_AUTO, V4L2_EXPOSURE_AUTO);
  232. } else {
  233. v4l2_ctrl_set(fd, V4L2_CID_EXPOSURE_AUTO, V4L2_EXPOSURE_MANUAL);
  234. v4l2_ctrl_set(fd, V4L2_CID_EXPOSURE, height/2);
  235. }
  236. if (auto_gain) {
  237. v4l2_ctrl_set(fd, V4L2_CID_AUTOGAIN, 1);
  238. } else {
  239. v4l2_ctrl_set(fd, V4L2_CID_AUTOGAIN, 0);
  240. v4l2_ctrl_set(fd, V4L2_CID_GAIN, 0);
  241. }
  242. close(current.fd);
  243. current.fd = fd;
  244. }
  245. static int
  246. init_device(int fd)
  247. {
  248. struct v4l2_capability cap;
  249. if (xioctl(fd, VIDIOC_QUERYCAP, &cap) == -1) {
  250. if (errno == EINVAL) {
  251. fprintf(stderr, "%s is no V4L2 device\n",
  252. dev_name);
  253. exit(EXIT_FAILURE);
  254. } else {
  255. errno_exit("VIDIOC_QUERYCAP");
  256. }
  257. }
  258. if (!(cap.capabilities & V4L2_CAP_VIDEO_CAPTURE)) {
  259. fprintf(stderr, "%s is no video capture device\n",
  260. dev_name);
  261. exit(EXIT_FAILURE);
  262. }
  263. if (!(cap.capabilities & V4L2_CAP_STREAMING)) {
  264. fprintf(stderr, "%s does not support streaming i/o\n",
  265. dev_name);
  266. exit(EXIT_FAILURE);
  267. }
  268. /* Select video input, video standard and tune here. */
  269. struct v4l2_cropcap cropcap = {
  270. .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
  271. };
  272. struct v4l2_crop crop = {0};
  273. if (xioctl(fd, VIDIOC_CROPCAP, &cropcap) == 0) {
  274. crop.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  275. crop.c = cropcap.defrect; /* reset to default */
  276. if (xioctl(fd, VIDIOC_S_CROP, &crop) == -1) {
  277. switch (errno) {
  278. case EINVAL:
  279. /* Cropping not supported. */
  280. break;
  281. default:
  282. /* Errors ignored. */
  283. break;
  284. }
  285. }
  286. } else {
  287. /* Errors ignored. */
  288. }
  289. struct v4l2_format fmt = {
  290. .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
  291. };
  292. if (current.width > 0) {
  293. g_printerr("Setting camera to %dx%d fmt %d\n",
  294. current.width, current.height, current.fmt);
  295. fmt.fmt.pix.width = current.width;
  296. fmt.fmt.pix.height = current.height;
  297. fmt.fmt.pix.pixelformat = current.fmt;
  298. fmt.fmt.pix.field = V4L2_FIELD_ANY;
  299. if (xioctl(fd, VIDIOC_S_FMT, &fmt) == -1) {
  300. g_printerr("VIDIOC_S_FMT failed");
  301. show_error("Could not set camera mode");
  302. return -1;
  303. }
  304. g_printerr("Driver returned %dx%d fmt %d\n",
  305. fmt.fmt.pix.width, fmt.fmt.pix.height,
  306. fmt.fmt.pix.pixelformat);
  307. /* Note VIDIOC_S_FMT may change width and height. */
  308. } else {
  309. g_printerr("Querying camera format\n");
  310. /* Preserve original settings as set by v4l2-ctl for example */
  311. if (xioctl(fd, VIDIOC_G_FMT, &fmt) == -1) {
  312. errno_exit("VIDIOC_G_FMT");
  313. }
  314. g_printerr("Driver returned %dx%d fmt %d\n",
  315. fmt.fmt.pix.width, fmt.fmt.pix.height,
  316. fmt.fmt.pix.pixelformat);
  317. current.width = fmt.fmt.pix.width;
  318. current.height = fmt.fmt.pix.height;
  319. }
  320. current.fmt = fmt.fmt.pix.pixelformat;
  321. /* Buggy driver paranoia. */
  322. unsigned int min = fmt.fmt.pix.width * 2;
  323. if (fmt.fmt.pix.bytesperline < min) {
  324. fmt.fmt.pix.bytesperline = min;
  325. }
  326. min = fmt.fmt.pix.bytesperline * fmt.fmt.pix.height;
  327. if (fmt.fmt.pix.sizeimage < min) {
  328. fmt.fmt.pix.sizeimage = min;
  329. }
  330. init_mmap(fd);
  331. return 0;
  332. }
  333. static void
  334. register_custom_tiff_tags(TIFF *tif)
  335. {
  336. static const TIFFFieldInfo custom_fields[] = {
  337. {TIFFTAG_FORWARDMATRIX1, -1, -1, TIFF_SRATIONAL, FIELD_CUSTOM, 1, 1, "ForwardMatrix1"},
  338. };
  339. // Add missing dng fields
  340. TIFFMergeFieldInfo(tif, custom_fields, sizeof(custom_fields) / sizeof(custom_fields[0]));
  341. }
  342. static void
  343. process_image(const int *p, int size)
  344. {
  345. time_t rawtime;
  346. char datetime[20] = {0};
  347. struct tm tim;
  348. uint8_t *pixels;
  349. char fname[255];
  350. char fname_target[255];
  351. char command[1024];
  352. char timestamp[30];
  353. char uniquecameramodel[255];
  354. GdkPixbuf *pixbuf;
  355. GdkPixbuf *pixbufrot;
  356. GdkPixbuf *thumb;
  357. GError *error = NULL;
  358. double scale;
  359. cairo_t *cr;
  360. TIFF *tif;
  361. int skip = 2;
  362. long sub_offset = 0;
  363. uint64 exif_offset = 0;
  364. static const short cfapatterndim[] = {2, 2};
  365. static const float neutral[] = {1.0, 1.0, 1.0};
  366. // Only process preview frames when not capturing
  367. if (capture == 0) {
  368. if(current.width > 1280) {
  369. skip = 3;
  370. }
  371. pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8, current.width / (skip*2), current.height / (skip*2));
  372. pixels = gdk_pixbuf_get_pixels(pixbuf);
  373. quick_debayer_bggr8((const uint8_t *)p, pixels, current.width, current.height, skip);
  374. if (current.rotate == 0) {
  375. pixbufrot = pixbuf;
  376. } else if (current.rotate == 90) {
  377. pixbufrot = gdk_pixbuf_rotate_simple(pixbuf, GDK_PIXBUF_ROTATE_COUNTERCLOCKWISE);
  378. } else if (current.rotate == 180) {
  379. pixbufrot = gdk_pixbuf_rotate_simple(pixbuf, GDK_PIXBUF_ROTATE_UPSIDEDOWN);
  380. } else if (current.rotate == 270) {
  381. pixbufrot = gdk_pixbuf_rotate_simple(pixbuf, GDK_PIXBUF_ROTATE_CLOCKWISE);
  382. }
  383. scale = (double) preview_width / gdk_pixbuf_get_width(pixbufrot);
  384. cr = cairo_create(surface);
  385. cairo_set_source_rgb(cr, 0, 0, 0);
  386. cairo_paint(cr);
  387. cairo_scale(cr, scale, scale);
  388. gdk_cairo_set_source_pixbuf(cr, pixbufrot, 0, 0);
  389. cairo_pattern_set_extend(cairo_get_source(cr), CAIRO_EXTEND_NONE);
  390. cairo_paint(cr);
  391. gtk_widget_queue_draw_area(preview, 0, 0, preview_width, preview_height);
  392. } else {
  393. capture--;
  394. time(&rawtime);
  395. tim = *(localtime(&rawtime));
  396. strftime(timestamp, 30, "%Y%m%d%H%M%S", &tim);
  397. strftime(datetime, 20, "%Y:%m:%d %H:%M:%S", &tim);
  398. sprintf(fname_target, "%s/Pictures/IMG%s", getenv("HOME"), timestamp);
  399. sprintf(fname, "%s/%d.dng", burst_dir, burst_length - capture);
  400. if(!(tif = TIFFOpen(fname, "w"))) {
  401. printf("Could not open tiff\n");
  402. }
  403. // Define TIFF thumbnail
  404. TIFFSetField(tif, TIFFTAG_SUBFILETYPE, 1);
  405. TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, current.width >> 4);
  406. TIFFSetField(tif, TIFFTAG_IMAGELENGTH, current.height >> 4);
  407. TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 8);
  408. TIFFSetField(tif, TIFFTAG_COMPRESSION, COMPRESSION_NONE);
  409. TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
  410. TIFFSetField(tif, TIFFTAG_MAKE, exif_make);
  411. TIFFSetField(tif, TIFFTAG_MODEL, exif_model);
  412. TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
  413. TIFFSetField(tif, TIFFTAG_DATETIME, datetime);
  414. TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 3);
  415. TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
  416. TIFFSetField(tif, TIFFTAG_SOFTWARE, "Megapixels");
  417. TIFFSetField(tif, TIFFTAG_SUBIFD, 1, &sub_offset);
  418. TIFFSetField(tif, TIFFTAG_DNGVERSION, "\001\001\0\0");
  419. TIFFSetField(tif, TIFFTAG_DNGBACKWARDVERSION, "\001\0\0\0");
  420. sprintf(uniquecameramodel, "%s %s", exif_make, exif_model);
  421. TIFFSetField(tif, TIFFTAG_UNIQUECAMERAMODEL, uniquecameramodel);
  422. if(current.colormatrix[0]) {
  423. TIFFSetField(tif, TIFFTAG_COLORMATRIX1, 9, current.colormatrix);
  424. } else {
  425. TIFFSetField(tif, TIFFTAG_COLORMATRIX1, 9, colormatrix_srgb);
  426. }
  427. if(current.forwardmatrix[0]) {
  428. TIFFSetField(tif, TIFFTAG_FORWARDMATRIX1, 9, current.forwardmatrix);
  429. }
  430. TIFFSetField(tif, TIFFTAG_ASSHOTNEUTRAL, 3, neutral);
  431. TIFFSetField(tif, TIFFTAG_CALIBRATIONILLUMINANT1, 21);
  432. // Write black thumbnail, only windows uses this
  433. {
  434. unsigned char *buf = (unsigned char *)calloc(1, (int)current.width >> 4);
  435. for (int row = 0; row < current.height>>4; row++) {
  436. TIFFWriteScanline(tif, buf, row, 0);
  437. }
  438. free(buf);
  439. }
  440. TIFFWriteDirectory(tif);
  441. // Define main photo
  442. TIFFSetField(tif, TIFFTAG_SUBFILETYPE, 0);
  443. TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, current.width);
  444. TIFFSetField(tif, TIFFTAG_IMAGELENGTH, current.height);
  445. TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 8);
  446. TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_CFA);
  447. TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 1);
  448. TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
  449. TIFFSetField(tif, TIFFTAG_CFAREPEATPATTERNDIM, cfapatterndim);
  450. TIFFSetField(tif, TIFFTAG_CFAPATTERN, "\002\001\001\000"); // BGGR
  451. if(current.whitelevel) {
  452. TIFFSetField(tif, TIFFTAG_WHITELEVEL, 1, &current.whitelevel);
  453. }
  454. if(current.blacklevel) {
  455. TIFFSetField(tif, TIFFTAG_BLACKLEVEL, 1, &current.blacklevel);
  456. }
  457. TIFFCheckpointDirectory(tif);
  458. printf("Writing frame to %s\n", fname);
  459. unsigned char *pLine = (unsigned char*)malloc(current.width);
  460. for(int row = 0; row < current.height; row++){
  461. TIFFWriteScanline(tif, ((uint8_t *)p)+(row*current.width), row, 0);
  462. }
  463. free(pLine);
  464. TIFFWriteDirectory(tif);
  465. // Add an EXIF block to the tiff
  466. TIFFCreateEXIFDirectory(tif);
  467. // 1 = manual, 2 = full auto, 3 = aperture priority, 4 = shutter priority
  468. TIFFSetField(tif, EXIFTAG_EXPOSUREPROGRAM, 2);
  469. TIFFSetField(tif, EXIFTAG_DATETIMEORIGINAL, datetime);
  470. TIFFSetField(tif, EXIFTAG_DATETIMEDIGITIZED, datetime);
  471. if(current.fnumber) {
  472. TIFFSetField(tif, EXIFTAG_FNUMBER, current.fnumber);
  473. }
  474. if(current.focallength) {
  475. TIFFSetField(tif, EXIFTAG_FOCALLENGTH, current.focallength);
  476. }
  477. if(current.focallength && current.cropfactor) {
  478. TIFFSetField(tif, EXIFTAG_FOCALLENGTHIN35MMFILM, (short)(current.focallength * current.cropfactor));
  479. }
  480. TIFFWriteCustomDirectory(tif, &exif_offset);
  481. TIFFFreeDirectory(tif);
  482. // Update exif pointer
  483. TIFFSetDirectory(tif, 0);
  484. TIFFSetField(tif, TIFFTAG_EXIFIFD, exif_offset);
  485. TIFFRewriteDirectory(tif);
  486. TIFFClose(tif);
  487. if (capture == 0) {
  488. // Update the thumbnail if this is the last frame
  489. pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8, current.width / (skip*2), current.height / (skip*2));
  490. pixels = gdk_pixbuf_get_pixels(pixbuf);
  491. quick_debayer_bggr8((const uint8_t *)p, pixels, current.width, current.height, skip);
  492. if (current.rotate == 0) {
  493. pixbufrot = pixbuf;
  494. } else if (current.rotate == 90) {
  495. pixbufrot = gdk_pixbuf_rotate_simple(pixbuf, GDK_PIXBUF_ROTATE_COUNTERCLOCKWISE);
  496. } else if (current.rotate == 180) {
  497. pixbufrot = gdk_pixbuf_rotate_simple(pixbuf, GDK_PIXBUF_ROTATE_UPSIDEDOWN);
  498. } else if (current.rotate == 270) {
  499. pixbufrot = gdk_pixbuf_rotate_simple(pixbuf, GDK_PIXBUF_ROTATE_CLOCKWISE);
  500. }
  501. thumb = gdk_pixbuf_scale_simple(pixbufrot, 24, 24, GDK_INTERP_BILINEAR);
  502. gtk_image_set_from_pixbuf(GTK_IMAGE(thumb_last), thumb);
  503. last_path = strdup(fname);
  504. if (error != NULL) {
  505. g_printerr("%s\n", error->message);
  506. g_clear_error(&error);
  507. }
  508. // Start post-processing the captured burst
  509. g_printerr("Post process %s to %s.ext\n", burst_dir, fname_target);
  510. sprintf(command, "%s %s %s &", processing_script, burst_dir, fname_target);
  511. system(command);
  512. }
  513. }
  514. }
  515. static gboolean
  516. preview_draw(GtkWidget *widget, cairo_t *cr, gpointer data)
  517. {
  518. cairo_set_source_surface(cr, surface, 0, 0);
  519. cairo_paint(cr);
  520. return FALSE;
  521. }
  522. static gboolean
  523. preview_configure(GtkWidget *widget, GdkEventConfigure *event)
  524. {
  525. cairo_t *cr;
  526. if (surface)
  527. cairo_surface_destroy(surface);
  528. surface = gdk_window_create_similar_surface(gtk_widget_get_window(widget),
  529. CAIRO_CONTENT_COLOR,
  530. gtk_widget_get_allocated_width(widget),
  531. gtk_widget_get_allocated_height(widget));
  532. preview_width = gtk_widget_get_allocated_width(widget);
  533. preview_height = gtk_widget_get_allocated_height(widget);
  534. cr = cairo_create(surface);
  535. cairo_set_source_rgb(cr, 0, 0, 0);
  536. cairo_paint(cr);
  537. cairo_destroy(cr);
  538. return TRUE;
  539. }
  540. static int
  541. read_frame(int fd)
  542. {
  543. struct v4l2_buffer buf = {0};
  544. buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  545. buf.memory = V4L2_MEMORY_MMAP;
  546. if (xioctl(fd, VIDIOC_DQBUF, &buf) == -1) {
  547. switch (errno) {
  548. case EAGAIN:
  549. return 0;
  550. case EIO:
  551. /* Could ignore EIO, see spec. */
  552. /* fallthrough */
  553. default:
  554. errno_exit("VIDIOC_DQBUF");
  555. break;
  556. }
  557. }
  558. //assert(buf.index < n_buffers);
  559. process_image(buffers[buf.index].start, buf.bytesused);
  560. if (xioctl(fd, VIDIOC_QBUF, &buf) == -1) {
  561. errno_exit("VIDIOC_QBUF");
  562. }
  563. return 1;
  564. }
  565. gboolean
  566. get_frame()
  567. {
  568. if (ready == 0)
  569. return TRUE;
  570. while (1) {
  571. fd_set fds;
  572. struct timeval tv;
  573. int r;
  574. FD_ZERO(&fds);
  575. FD_SET(video_fd, &fds);
  576. /* Timeout. */
  577. tv.tv_sec = 2;
  578. tv.tv_usec = 0;
  579. r = select(video_fd + 1, &fds, NULL, NULL, &tv);
  580. if (r == -1) {
  581. if (EINTR == errno) {
  582. continue;
  583. }
  584. errno_exit("select");
  585. } else if (r == 0) {
  586. fprintf(stderr, "select timeout\\n");
  587. exit(EXIT_FAILURE);
  588. }
  589. if (read_frame(video_fd)) {
  590. break;
  591. }
  592. /* EAGAIN - continue select loop. */
  593. }
  594. return TRUE;
  595. }
  596. int
  597. strtoint(const char *nptr, char **endptr, int base)
  598. {
  599. long x = strtol(nptr, endptr, base);
  600. assert(x <= INT_MAX);
  601. return (int) x;
  602. }
  603. static int
  604. config_ini_handler(void *user, const char *section, const char *name,
  605. const char *value)
  606. {
  607. struct camerainfo *cc;
  608. if (strcmp(section, "rear") == 0 || strcmp(section, "front") == 0) {
  609. if (strcmp(section, "rear") == 0) {
  610. cc = &rear_cam;
  611. } else {
  612. cc = &front_cam;
  613. }
  614. if (strcmp(name, "width") == 0) {
  615. cc->width = strtoint(value, NULL, 10);
  616. } else if (strcmp(name, "height") == 0) {
  617. cc->height = strtoint(value, NULL, 10);
  618. } else if (strcmp(name, "rate") == 0) {
  619. cc->rate = strtoint(value, NULL, 10);
  620. } else if (strcmp(name, "rotate") == 0) {
  621. cc->rotate = strtoint(value, NULL, 10);
  622. } else if (strcmp(name, "fmt") == 0) {
  623. if (strcmp(value, "RGGB8") == 0) {
  624. cc->fmt = V4L2_PIX_FMT_SRGGB8;
  625. } else if (strcmp(value, "BGGR8") == 0) {
  626. cc->fmt = V4L2_PIX_FMT_SBGGR8;
  627. cc->mbus = MEDIA_BUS_FMT_SBGGR8_1X8;
  628. } else if (strcmp(value, "GRBG8") == 0) {
  629. cc->fmt = V4L2_PIX_FMT_SGRBG8;
  630. } else if (strcmp(value, "GBRG8") == 0) {
  631. cc->fmt = V4L2_PIX_FMT_SGBRG8;
  632. } else {
  633. g_printerr("Unsupported pixelformat %s\n", value);
  634. exit(1);
  635. }
  636. } else if (strcmp(name, "driver") == 0) {
  637. strcpy(cc->dev_name, value);
  638. } else if (strcmp(name, "colormatrix") == 0) {
  639. sscanf(value, "%f,%f,%f,%f,%f,%f,%f,%f,%f",
  640. cc->colormatrix+0,
  641. cc->colormatrix+1,
  642. cc->colormatrix+2,
  643. cc->colormatrix+3,
  644. cc->colormatrix+4,
  645. cc->colormatrix+5,
  646. cc->colormatrix+6,
  647. cc->colormatrix+7,
  648. cc->colormatrix+8
  649. );
  650. } else if (strcmp(name, "forwardmatrix") == 0) {
  651. sscanf(value, "%f,%f,%f,%f,%f,%f,%f,%f,%f",
  652. cc->forwardmatrix+0,
  653. cc->forwardmatrix+1,
  654. cc->forwardmatrix+2,
  655. cc->forwardmatrix+3,
  656. cc->forwardmatrix+4,
  657. cc->forwardmatrix+5,
  658. cc->forwardmatrix+6,
  659. cc->forwardmatrix+7,
  660. cc->forwardmatrix+8
  661. );
  662. } else if (strcmp(name, "whitelevel") == 0) {
  663. cc->whitelevel = strtoint(value, NULL, 10);
  664. } else if (strcmp(name, "blacklevel") == 0) {
  665. cc->blacklevel = strtoint(value, NULL, 10);
  666. } else if (strcmp(name, "focallength") == 0) {
  667. cc->focallength = strtof(value, NULL);
  668. } else if (strcmp(name, "cropfactor") == 0) {
  669. cc->cropfactor = strtof(value, NULL);
  670. } else if (strcmp(name, "fnumber") == 0) {
  671. cc->fnumber = strtod(value, NULL);
  672. } else {
  673. g_printerr("Unknown key '%s' in [%s]\n", name, section);
  674. exit(1);
  675. }
  676. } else if (strcmp(section, "device") == 0) {
  677. if (strcmp(name, "csi") == 0) {
  678. media_drv_name = strdup(value);
  679. } else if (strcmp(name, "make") == 0) {
  680. exif_make = strdup(value);
  681. } else if (strcmp(name, "model") == 0) {
  682. exif_model = strdup(value);
  683. } else {
  684. g_printerr("Unknown key '%s' in [device]\n", name);
  685. exit(1);
  686. }
  687. } else {
  688. g_printerr("Unknown section '%s' in config file\n", section);
  689. exit(1);
  690. }
  691. return 1;
  692. }
  693. int
  694. find_dev_node(int maj, int min, char *fnbuf)
  695. {
  696. DIR *d;
  697. struct dirent *dir;
  698. struct stat info;
  699. d = opendir("/dev");
  700. while ((dir = readdir(d)) != NULL) {
  701. sprintf(fnbuf, "/dev/%s", dir->d_name);
  702. stat(fnbuf, &info);
  703. if (!S_ISCHR(info.st_mode)) {
  704. continue;
  705. }
  706. if (major(info.st_rdev) == maj && minor(info.st_rdev) == min) {
  707. return 0;
  708. }
  709. }
  710. return -1;
  711. }
  712. int
  713. setup_rear()
  714. {
  715. struct media_link_desc link = {0};
  716. // Disable the interface<->front link
  717. link.flags = 0;
  718. link.source.entity = front_cam.entity_id;
  719. link.source.index = 0;
  720. link.sink.entity = interface_entity_id;
  721. link.sink.index = 0;
  722. if (xioctl(media_fd, MEDIA_IOC_SETUP_LINK, &link) < 0) {
  723. g_printerr("Could not disable front camera link\n");
  724. return -1;
  725. }
  726. // Enable the interface<->rear link
  727. link.flags = MEDIA_LNK_FL_ENABLED;
  728. link.source.entity = rear_cam.entity_id;
  729. link.source.index = 0;
  730. link.sink.entity = interface_entity_id;
  731. link.sink.index = 0;
  732. if (xioctl(media_fd, MEDIA_IOC_SETUP_LINK, &link) < 0) {
  733. g_printerr("Could not enable rear camera link\n");
  734. return -1;
  735. }
  736. current = rear_cam;
  737. // Find camera node
  738. init_sensor(current.dev, current.width, current.height, current.mbus, current.rate);
  739. return 0;
  740. }
  741. int
  742. setup_front()
  743. {
  744. struct media_link_desc link = {0};
  745. // Disable the interface<->rear link
  746. link.flags = 0;
  747. link.source.entity = rear_cam.entity_id;
  748. link.source.index = 0;
  749. link.sink.entity = interface_entity_id;
  750. link.sink.index = 0;
  751. if (xioctl(media_fd, MEDIA_IOC_SETUP_LINK, &link) < 0) {
  752. g_printerr("Could not disable rear camera link\n");
  753. return -1;
  754. }
  755. // Enable the interface<->rear link
  756. link.flags = MEDIA_LNK_FL_ENABLED;
  757. link.source.entity = front_cam.entity_id;
  758. link.source.index = 0;
  759. link.sink.entity = interface_entity_id;
  760. link.sink.index = 0;
  761. if (xioctl(media_fd, MEDIA_IOC_SETUP_LINK, &link) < 0) {
  762. g_printerr("Could not enable front camera link\n");
  763. return -1;
  764. }
  765. current = front_cam;
  766. // Find camera node
  767. init_sensor(current.dev, current.width, current.height, current.mbus, current.rate);
  768. return 0;
  769. }
  770. int
  771. find_cameras()
  772. {
  773. struct media_entity_desc entity = {0};
  774. int ret;
  775. int found = 0;
  776. while (1) {
  777. entity.id = entity.id | MEDIA_ENT_ID_FLAG_NEXT;
  778. ret = xioctl(media_fd, MEDIA_IOC_ENUM_ENTITIES, &entity);
  779. if (ret < 0) {
  780. break;
  781. }
  782. printf("At node %s, (0x%x)\n", entity.name, entity.type);
  783. if (strncmp(entity.name, front_cam.dev_name, strlen(front_cam.dev_name)) == 0) {
  784. front_cam.entity_id = entity.id;
  785. find_dev_node(entity.dev.major, entity.dev.minor, front_cam.dev);
  786. printf("Found front cam, is %s at %s\n", entity.name, front_cam.dev);
  787. found++;
  788. }
  789. if (strncmp(entity.name, rear_cam.dev_name, strlen(rear_cam.dev_name)) == 0) {
  790. rear_cam.entity_id = entity.id;
  791. find_dev_node(entity.dev.major, entity.dev.minor, rear_cam.dev);
  792. printf("Found rear cam, is %s at %s\n", entity.name, rear_cam.dev);
  793. found++;
  794. }
  795. if (entity.type == MEDIA_ENT_F_IO_V4L) {
  796. interface_entity_id = entity.id;
  797. find_dev_node(entity.dev.major, entity.dev.minor, dev_name);
  798. printf("Found v4l2 interface node at %s\n", dev_name);
  799. }
  800. }
  801. if (found < 2) {
  802. return -1;
  803. }
  804. return 0;
  805. }
  806. int
  807. find_media_fd()
  808. {
  809. DIR *d;
  810. struct dirent *dir;
  811. int fd;
  812. char fnbuf[261];
  813. struct media_device_info mdi = {0};
  814. d = opendir("/dev");
  815. while ((dir = readdir(d)) != NULL) {
  816. if (strncmp(dir->d_name, "media", 5) == 0) {
  817. sprintf(fnbuf, "/dev/%s", dir->d_name);
  818. printf("Checking %s\n", fnbuf);
  819. fd = open(fnbuf, O_RDWR);
  820. xioctl(fd, MEDIA_IOC_DEVICE_INFO, &mdi);
  821. printf("Found media device: %s\n", mdi.driver);
  822. if (strcmp(mdi.driver, media_drv_name) == 0) {
  823. media_fd = fd;
  824. return 0;
  825. }
  826. close(fd);
  827. }
  828. }
  829. return 1;
  830. }
  831. void
  832. on_open_last_clicked(GtkWidget *widget, gpointer user_data)
  833. {
  834. char uri[270];
  835. GError *error = NULL;
  836. if(!last_path) {
  837. return;
  838. }
  839. sprintf(uri, "file://%s", last_path);
  840. if(!g_app_info_launch_default_for_uri(uri, NULL, &error)){
  841. g_printerr("Could not launch image viewer: %s\n", error->message);
  842. }
  843. }
  844. void
  845. on_open_directory_clicked(GtkWidget *widget, gpointer user_data)
  846. {
  847. char uri[270];
  848. GError *error = NULL;
  849. sprintf(uri, "file://%s/Pictures", getenv("HOME"));
  850. if(!g_app_info_launch_default_for_uri(uri, NULL, &error)){
  851. g_printerr("Could not launch image viewer: %s\n", error->message);
  852. }
  853. }
  854. void
  855. on_shutter_clicked(GtkWidget *widget, gpointer user_data)
  856. {
  857. char template[] = "/tmp/megapixels.XXXXXX";
  858. char *tempdir;
  859. tempdir = mkdtemp(template);
  860. if (tempdir == NULL) {
  861. g_printerr("Could not make capture directory %s\n", template);
  862. exit (EXIT_FAILURE);
  863. }
  864. strcpy(burst_dir, tempdir);
  865. capture = burst_length;
  866. }
  867. void
  868. on_error_close_clicked(GtkWidget *widget, gpointer user_data)
  869. {
  870. gtk_widget_hide(error_box);
  871. }
  872. void
  873. on_camera_switch_clicked(GtkWidget *widget, gpointer user_data)
  874. {
  875. stop_capturing(video_fd);
  876. close(current.fd);
  877. if (current_is_rear == 1) {
  878. setup_front();
  879. current_is_rear = 0;
  880. } else {
  881. setup_rear();
  882. current_is_rear = 1;
  883. }
  884. close(video_fd);
  885. video_fd = open(dev_name, O_RDWR);
  886. if (video_fd == -1) {
  887. g_printerr("Error opening video device: %s\n", dev_name);
  888. return;
  889. }
  890. init_device(video_fd);
  891. start_capturing(video_fd);
  892. }
  893. void
  894. on_settings_btn_clicked(GtkWidget *widget, gpointer user_data)
  895. {
  896. gtk_stack_set_visible_child_name(GTK_STACK(main_stack), "settings");
  897. }
  898. void
  899. on_back_clicked(GtkWidget *widget, gpointer user_data)
  900. {
  901. gtk_stack_set_visible_child_name(GTK_STACK(main_stack), "main");
  902. }
  903. int
  904. find_config(char *conffile)
  905. {
  906. char buf[512];
  907. char *xdg_config_home;
  908. wordexp_t exp_result;
  909. FILE *fp;
  910. // Resolve XDG stuff
  911. if ((xdg_config_home = getenv("XDG_CONFIG_HOME")) == NULL) {
  912. xdg_config_home = "~/.config";
  913. }
  914. wordexp(xdg_config_home, &exp_result, 0);
  915. xdg_config_home = strdup(exp_result.we_wordv[0]);
  916. wordfree(&exp_result);
  917. if(access("/proc/device-tree/compatible", F_OK) != -1) {
  918. // Reads to compatible string of the current device tree, looks like:
  919. // pine64,pinephone-1.2\0allwinner,sun50i-a64\0
  920. fp = fopen("/proc/device-tree/compatible", "r");
  921. fgets(buf, 512, fp);
  922. fclose(fp);
  923. // Check config/%dt.ini in the current working directory
  924. sprintf(conffile, "config/%s.ini", buf);
  925. if(access(conffile, F_OK) != -1) {
  926. printf("Found config file at %s\n", conffile);
  927. return 0;
  928. }
  929. // Check for a config file in XDG_CONFIG_HOME
  930. sprintf(conffile, "%s/megapixels/config/%s.ini", xdg_config_home, buf);
  931. if(access(conffile, F_OK) != -1) {
  932. printf("Found config file at %s\n", conffile);
  933. return 0;
  934. }
  935. // Check user overridden /etc/megapixels/config/$dt.ini
  936. sprintf(conffile, "%s/megapixels/config/%s.ini", SYSCONFDIR, buf);
  937. if(access(conffile, F_OK) != -1) {
  938. printf("Found config file at %s\n", conffile);
  939. return 0;
  940. }
  941. // Check packaged /usr/share/megapixels/config/$dt.ini
  942. sprintf(conffile, "%s/megapixels/config/%s.ini", DATADIR, buf);
  943. if(access(conffile, F_OK) != -1) {
  944. printf("Found config file at %s\n", conffile);
  945. return 0;
  946. }
  947. printf("%s not found\n", conffile);
  948. } else {
  949. printf("Could not read device name from device tree\n");
  950. }
  951. // If all else fails, fall back to /etc/megapixels.ini
  952. conffile = "/etc/megapixels.ini";
  953. if(access(conffile, F_OK) != -1) {
  954. printf("Found config file at %s\n", conffile);
  955. return 0;
  956. }
  957. return -1;
  958. }
  959. int
  960. find_processor(char *script)
  961. {
  962. char *xdg_config_home;
  963. char filename[] = "postprocess.sh";
  964. wordexp_t exp_result;
  965. // Resolve XDG stuff
  966. if ((xdg_config_home = getenv("XDG_CONFIG_HOME")) == NULL) {
  967. xdg_config_home = "~/.config";
  968. }
  969. wordexp(xdg_config_home, &exp_result, 0);
  970. xdg_config_home = strdup(exp_result.we_wordv[0]);
  971. wordfree(&exp_result);
  972. // Check postprocess.h in the current working directory
  973. sprintf(script, "%s", filename);
  974. if(access(script, F_OK) != -1) {
  975. sprintf(script, "./%s", filename);
  976. printf("Found postprocessor script at %s\n", script);
  977. return 0;
  978. }
  979. // Check for a script in XDG_CONFIG_HOME
  980. sprintf(script, "%s/megapixels/%s", xdg_config_home, filename);
  981. if(access(script, F_OK) != -1) {
  982. printf("Found postprocessor script at %s\n", script);
  983. return 0;
  984. }
  985. // Check user overridden /etc/megapixels/postprocessor.sh
  986. sprintf(script, "%s/megapixels/%s", SYSCONFDIR, filename);
  987. if(access(script, F_OK) != -1) {
  988. printf("Found postprocessor script at %s\n", script);
  989. return 0;
  990. }
  991. // Check packaged /usr/share/megapixels/postprocessor.sh
  992. sprintf(script, "%s/megapixels/%s", DATADIR, filename);
  993. if(access(script, F_OK) != -1) {
  994. printf("Found postprocessor script at %s\n", script);
  995. return 0;
  996. }
  997. return -1;
  998. }
  999. int
  1000. main(int argc, char *argv[])
  1001. {
  1002. int ret;
  1003. char conffile[512];
  1004. ret = find_config(conffile);
  1005. if (ret) {
  1006. g_printerr("Could not find any config file\n");
  1007. return ret;
  1008. }
  1009. ret = find_processor(processing_script);
  1010. if (ret) {
  1011. g_printerr("Could not find any post-process script\n");
  1012. return ret;
  1013. }
  1014. TIFFSetTagExtender(register_custom_tiff_tags);
  1015. gtk_init(&argc, &argv);
  1016. g_object_set(gtk_settings_get_default(), "gtk-application-prefer-dark-theme", TRUE, NULL);
  1017. GtkBuilder *builder = gtk_builder_new_from_resource("/org/postmarketos/Megapixels/camera.glade");
  1018. GtkWidget *window = GTK_WIDGET(gtk_builder_get_object(builder, "window"));
  1019. GtkWidget *preview_box = GTK_WIDGET(gtk_builder_get_object(builder, "preview_box"));
  1020. GtkWidget *shutter = GTK_WIDGET(gtk_builder_get_object(builder, "shutter"));
  1021. GtkWidget *switch_btn = GTK_WIDGET(gtk_builder_get_object(builder, "switch_camera"));
  1022. GtkWidget *settings_btn = GTK_WIDGET(gtk_builder_get_object(builder, "settings"));
  1023. GtkWidget *settings_back = GTK_WIDGET(gtk_builder_get_object(builder, "settings_back"));
  1024. GtkWidget *error_close = GTK_WIDGET(gtk_builder_get_object(builder, "error_close"));
  1025. GtkWidget *open_last = GTK_WIDGET(gtk_builder_get_object(builder, "open_last"));
  1026. GtkWidget *open_directory = GTK_WIDGET(gtk_builder_get_object(builder, "open_directory"));
  1027. preview = GTK_WIDGET(gtk_builder_get_object(builder, "preview"));
  1028. error_box = GTK_WIDGET(gtk_builder_get_object(builder, "error_box"));
  1029. error_message = GTK_WIDGET(gtk_builder_get_object(builder, "error_message"));
  1030. main_stack = GTK_WIDGET(gtk_builder_get_object(builder, "main_stack"));
  1031. thumb_last = GTK_WIDGET(gtk_builder_get_object(builder, "thumb_last"));
  1032. g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);
  1033. g_signal_connect(shutter, "clicked", G_CALLBACK(on_shutter_clicked), NULL);
  1034. g_signal_connect(error_close, "clicked", G_CALLBACK(on_error_close_clicked), NULL);
  1035. g_signal_connect(switch_btn, "clicked", G_CALLBACK(on_camera_switch_clicked), NULL);
  1036. g_signal_connect(settings_btn, "clicked", G_CALLBACK(on_settings_btn_clicked), NULL);
  1037. g_signal_connect(settings_back, "clicked", G_CALLBACK(on_back_clicked), NULL);
  1038. g_signal_connect(open_last, "clicked", G_CALLBACK(on_open_last_clicked), NULL);
  1039. g_signal_connect(open_directory, "clicked", G_CALLBACK(on_open_directory_clicked), NULL);
  1040. g_signal_connect(preview, "draw", G_CALLBACK(preview_draw), NULL);
  1041. g_signal_connect(preview, "configure-event", G_CALLBACK(preview_configure), NULL);
  1042. GtkCssProvider *provider = gtk_css_provider_new();
  1043. if (access("camera.css", F_OK) != -1) {
  1044. gtk_css_provider_load_from_path(provider, "camera.css", NULL);
  1045. } else {
  1046. gtk_css_provider_load_from_resource(provider, "/org/postmarketos/Megapixels/camera.css");
  1047. }
  1048. GtkStyleContext *context = gtk_widget_get_style_context(preview_box);
  1049. gtk_style_context_add_provider(context,
  1050. GTK_STYLE_PROVIDER(provider),
  1051. GTK_STYLE_PROVIDER_PRIORITY_USER);
  1052. context = gtk_widget_get_style_context(error_box);
  1053. gtk_style_context_add_provider(context,
  1054. GTK_STYLE_PROVIDER(provider),
  1055. GTK_STYLE_PROVIDER_PRIORITY_USER);
  1056. int result = ini_parse(conffile, config_ini_handler, NULL);
  1057. if (result == -1) {
  1058. g_printerr("Config file not found\n");
  1059. return 1;
  1060. } else if (result == -2) {
  1061. g_printerr("Could not allocate memory to parse config file\n");
  1062. return 1;
  1063. } else if (result != 0) {
  1064. g_printerr("Could not parse config file\n");
  1065. return 1;
  1066. }
  1067. if (find_media_fd() == -1) {
  1068. g_printerr("Could not find the media node\n");
  1069. show_error("Could not find the media node");
  1070. goto failed;
  1071. }
  1072. if (find_cameras() == -1) {
  1073. g_printerr("Could not find the cameras\n");
  1074. show_error("Could not find the cameras");
  1075. goto failed;
  1076. }
  1077. setup_rear();
  1078. int fd = open(dev_name, O_RDWR);
  1079. if (fd == -1) {
  1080. g_printerr("Error opening video device: %s\n", dev_name);
  1081. show_error("Error opening the video device");
  1082. goto failed;
  1083. }
  1084. video_fd = fd;
  1085. if(init_device(fd) < 0){
  1086. goto failed;
  1087. }
  1088. start_capturing(fd);
  1089. failed:
  1090. printf("window show\n");
  1091. gtk_widget_show(window);
  1092. g_idle_add((GSourceFunc)get_frame, NULL);
  1093. gtk_main();
  1094. return 0;
  1095. }