main.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995
  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 "config.h"
  18. #include "ini.h"
  19. #include "bayer.h"
  20. #include "quickdebayer.h"
  21. enum io_method {
  22. IO_METHOD_READ,
  23. IO_METHOD_MMAP,
  24. IO_METHOD_USERPTR,
  25. };
  26. struct buffer {
  27. void *start;
  28. size_t length;
  29. };
  30. struct buffer *buffers;
  31. static unsigned int n_buffers;
  32. // Rear camera
  33. static char *rear_dev_name;
  34. static unsigned int rear_entity_id;
  35. static char rear_dev[260];
  36. static int rear_width = -1;
  37. static int rear_height = -1;
  38. static int rear_rate = 30;
  39. static int rear_rotate = 0;
  40. static int rear_fmt = V4L2_PIX_FMT_RGB24;
  41. static int rear_mbus = MEDIA_BUS_FMT_RGB888_1X24;
  42. // Front camera
  43. static char *front_dev_name;
  44. static unsigned int front_entity_id;
  45. static char front_dev[260];
  46. static int front_width = -1;
  47. static int front_height = -1;
  48. static int front_rate = 30;
  49. static int front_rotate = 0;
  50. static int front_fmt = V4L2_PIX_FMT_RGB24;
  51. static int front_mbus = MEDIA_BUS_FMT_RGB888_1X24;
  52. // Camera interface
  53. static char *media_drv_name;
  54. static unsigned int interface_entity_id;
  55. static char dev_name[260];
  56. static int media_fd;
  57. static int video_fd;
  58. // State
  59. static int ready = 0;
  60. static int current_width = -1;
  61. static int current_height = -1;
  62. static int current_fmt = 0;
  63. static int current_rotate = 0;
  64. static int current_fd;
  65. static int capture = 0;
  66. static int current_is_rear = 1;
  67. static cairo_surface_t *surface = NULL;
  68. static int preview_width = -1;
  69. static int preview_height = -1;
  70. // Widgets
  71. GtkWidget *preview;
  72. GtkWidget *error_box;
  73. GtkWidget *error_message;
  74. static int
  75. xioctl(int fd, int request, void *arg)
  76. {
  77. int r;
  78. do {
  79. r = ioctl(fd, request, arg);
  80. } while (r == -1 && errno == EINTR);
  81. return r;
  82. }
  83. static void
  84. errno_exit(const char *s)
  85. {
  86. fprintf(stderr, "%s error %d, %s\n", s, errno, strerror(errno));
  87. exit(EXIT_FAILURE);
  88. }
  89. static void
  90. show_error(const char *s)
  91. {
  92. gtk_label_set_text(GTK_LABEL(error_message), s);
  93. gtk_widget_show(error_box);
  94. }
  95. static void
  96. start_capturing(int fd)
  97. {
  98. enum v4l2_buf_type type;
  99. for (int i = 0; i < n_buffers; ++i) {
  100. struct v4l2_buffer buf = {
  101. .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
  102. .memory = V4L2_MEMORY_MMAP,
  103. .index = i,
  104. };
  105. if (xioctl(fd, VIDIOC_QBUF, &buf) == -1) {
  106. errno_exit("VIDIOC_QBUF");
  107. }
  108. }
  109. type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  110. if (xioctl(fd, VIDIOC_STREAMON, &type) == -1) {
  111. errno_exit("VIDIOC_STREAMON");
  112. }
  113. ready = 1;
  114. }
  115. static void
  116. stop_capturing(int fd)
  117. {
  118. int i;
  119. ready = 0;
  120. printf("Stopping capture\n");
  121. enum v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  122. if (xioctl(fd, VIDIOC_STREAMOFF, &type) == -1) {
  123. errno_exit("VIDIOC_STREAMOFF");
  124. }
  125. for (i = 0; i < n_buffers; ++i) {
  126. munmap(buffers[i].start, buffers[i].length);
  127. }
  128. }
  129. static void
  130. init_mmap(int fd)
  131. {
  132. struct v4l2_requestbuffers req = {0};
  133. req.count = 4;
  134. req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  135. req.memory = V4L2_MEMORY_MMAP;
  136. if (xioctl(fd, VIDIOC_REQBUFS, &req) == -1) {
  137. if (errno == EINVAL) {
  138. fprintf(stderr, "%s does not support memory mapping",
  139. dev_name);
  140. exit(EXIT_FAILURE);
  141. } else {
  142. errno_exit("VIDIOC_REQBUFS");
  143. }
  144. }
  145. if (req.count < 2) {
  146. fprintf(stderr, "Insufficient buffer memory on %s\n",
  147. dev_name);
  148. exit(EXIT_FAILURE);
  149. }
  150. buffers = calloc(req.count, sizeof(buffers[0]));
  151. if (!buffers) {
  152. fprintf(stderr, "Out of memory\\n");
  153. exit(EXIT_FAILURE);
  154. }
  155. for (n_buffers = 0; n_buffers < req.count; ++n_buffers) {
  156. struct v4l2_buffer buf = {
  157. .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
  158. .memory = V4L2_MEMORY_MMAP,
  159. .index = n_buffers,
  160. };
  161. if (xioctl(fd, VIDIOC_QUERYBUF, &buf) == -1) {
  162. errno_exit("VIDIOC_QUERYBUF");
  163. }
  164. buffers[n_buffers].length = buf.length;
  165. buffers[n_buffers].start = mmap(NULL /* start anywhere */,
  166. buf.length,
  167. PROT_READ | PROT_WRITE /* required */,
  168. MAP_SHARED /* recommended */,
  169. fd, buf.m.offset);
  170. if (MAP_FAILED == buffers[n_buffers].start) {
  171. errno_exit("mmap");
  172. }
  173. }
  174. }
  175. static int
  176. v4l2_ctrl_set(int fd, uint32_t id, int val)
  177. {
  178. struct v4l2_control ctrl = {0};
  179. ctrl.id = id;
  180. ctrl.value = val;
  181. if (xioctl(fd, VIDIOC_S_CTRL, &ctrl) == -1) {
  182. g_printerr("Failed to set control %d to %d\n", id, val);
  183. return -1;
  184. }
  185. return 0;
  186. }
  187. static void
  188. init_sensor(char *fn, int width, int height, int mbus, int rate)
  189. {
  190. int fd;
  191. struct v4l2_subdev_frame_interval interval;
  192. struct v4l2_subdev_format fmt;
  193. fd = open(fn, O_RDWR);
  194. g_printerr("Setting sensor rate to %d\n", rate);
  195. interval.pad = 0;
  196. interval.interval.numerator = 1;
  197. interval.interval.denominator = rate;
  198. if (xioctl(fd, VIDIOC_SUBDEV_S_FRAME_INTERVAL, &interval) == -1) {
  199. errno_exit("VIDIOC_SUBDEV_S_FRAME_INTERVAL");
  200. }
  201. g_printerr("Driver returned %d/%d frameinterval\n",
  202. interval.interval.numerator, interval.interval.denominator);
  203. g_printerr("Setting sensor to %dx%d fmt %d\n",
  204. width, height, mbus);
  205. fmt.pad = 0;
  206. fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
  207. fmt.format.code = mbus;
  208. fmt.format.width = width;
  209. fmt.format.height = height;
  210. fmt.format.field = V4L2_FIELD_ANY;
  211. if (xioctl(fd, VIDIOC_SUBDEV_S_FMT, &fmt) == -1) {
  212. errno_exit("VIDIOC_SUBDEV_S_FMT");
  213. }
  214. g_printerr("Driver returned %dx%d fmt %d\n",
  215. fmt.format.width, fmt.format.height,
  216. fmt.format.code);
  217. // Placeholder, default is also 1
  218. //v4l2_ctrl_set(fd, V4L2_CID_AUTOGAIN, 1);
  219. close(current_fd);
  220. current_fd = fd;
  221. }
  222. static int
  223. init_device(int fd)
  224. {
  225. struct v4l2_capability cap;
  226. if (xioctl(fd, VIDIOC_QUERYCAP, &cap) == -1) {
  227. if (errno == EINVAL) {
  228. fprintf(stderr, "%s is no V4L2 device\n",
  229. dev_name);
  230. exit(EXIT_FAILURE);
  231. } else {
  232. errno_exit("VIDIOC_QUERYCAP");
  233. }
  234. }
  235. if (!(cap.capabilities & V4L2_CAP_VIDEO_CAPTURE)) {
  236. fprintf(stderr, "%s is no video capture device\n",
  237. dev_name);
  238. exit(EXIT_FAILURE);
  239. }
  240. if (!(cap.capabilities & V4L2_CAP_STREAMING)) {
  241. fprintf(stderr, "%s does not support streaming i/o\n",
  242. dev_name);
  243. exit(EXIT_FAILURE);
  244. }
  245. /* Select video input, video standard and tune here. */
  246. struct v4l2_cropcap cropcap = {
  247. .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
  248. };
  249. struct v4l2_crop crop = {0};
  250. if (xioctl(fd, VIDIOC_CROPCAP, &cropcap) == 0) {
  251. crop.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  252. crop.c = cropcap.defrect; /* reset to default */
  253. if (xioctl(fd, VIDIOC_S_CROP, &crop) == -1) {
  254. switch (errno) {
  255. case EINVAL:
  256. /* Cropping not supported. */
  257. break;
  258. default:
  259. /* Errors ignored. */
  260. break;
  261. }
  262. }
  263. } else {
  264. /* Errors ignored. */
  265. }
  266. struct v4l2_format fmt = {
  267. .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
  268. };
  269. if (current_width > 0) {
  270. g_printerr("Setting camera to %dx%d fmt %d\n",
  271. current_width, current_height, current_fmt);
  272. fmt.fmt.pix.width = current_width;
  273. fmt.fmt.pix.height = current_height;
  274. fmt.fmt.pix.pixelformat = current_fmt;
  275. fmt.fmt.pix.field = V4L2_FIELD_ANY;
  276. if (xioctl(fd, VIDIOC_S_FMT, &fmt) == -1) {
  277. g_printerr("VIDIOC_S_FMT failed");
  278. show_error("Could not set camera mode");
  279. return -1;
  280. }
  281. g_printerr("Driver returned %dx%d fmt %d\n",
  282. fmt.fmt.pix.width, fmt.fmt.pix.height,
  283. fmt.fmt.pix.pixelformat);
  284. /* Note VIDIOC_S_FMT may change width and height. */
  285. } else {
  286. g_printerr("Querying camera format\n");
  287. /* Preserve original settings as set by v4l2-ctl for example */
  288. if (xioctl(fd, VIDIOC_G_FMT, &fmt) == -1) {
  289. errno_exit("VIDIOC_G_FMT");
  290. }
  291. g_printerr("Driver returned %dx%d fmt %d\n",
  292. fmt.fmt.pix.width, fmt.fmt.pix.height,
  293. fmt.fmt.pix.pixelformat);
  294. current_width = fmt.fmt.pix.width;
  295. current_height = fmt.fmt.pix.height;
  296. }
  297. current_fmt = fmt.fmt.pix.pixelformat;
  298. /* Buggy driver paranoia. */
  299. unsigned int min = fmt.fmt.pix.width * 2;
  300. if (fmt.fmt.pix.bytesperline < min) {
  301. fmt.fmt.pix.bytesperline = min;
  302. }
  303. min = fmt.fmt.pix.bytesperline * fmt.fmt.pix.height;
  304. if (fmt.fmt.pix.sizeimage < min) {
  305. fmt.fmt.pix.sizeimage = min;
  306. }
  307. init_mmap(fd);
  308. return 0;
  309. }
  310. static void
  311. process_image(const int *p, int size)
  312. {
  313. clock_t t;
  314. time_t rawtime;
  315. struct tm tim;
  316. uint8_t *pixels;
  317. double time_taken;
  318. char fname[255];
  319. char timestamp[30];
  320. GdkPixbuf *pixbuf;
  321. GdkPixbuf *pixbufrot;
  322. GError *error = NULL;
  323. double scale;
  324. cairo_t *cr;
  325. t = clock();
  326. int skip = 2;
  327. dc1394bayer_method_t method = DC1394_BAYER_METHOD_DOWNSAMPLE;
  328. dc1394color_filter_t filter = DC1394_COLOR_FILTER_BGGR;
  329. if (capture) {
  330. method = DC1394_BAYER_METHOD_SIMPLE;
  331. // method = DC1394_BAYER_METHOD_VNG is slightly sharper but takes 10 seconds;
  332. pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8, current_width, current_height);
  333. pixels = gdk_pixbuf_get_pixels(pixbuf);
  334. dc1394_bayer_decoding_8bit((const uint8_t *) p, pixels, current_width, current_height, filter, method);
  335. } else {
  336. if(current_width > 1280) {
  337. skip = 3;
  338. }
  339. pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8, current_width / (skip*2), current_height / (skip*2));
  340. pixels = gdk_pixbuf_get_pixels(pixbuf);
  341. quick_debayer_bggr8((const uint8_t *)p, pixels, current_width, current_height, skip);
  342. }
  343. if (current_rotate == 0) {
  344. pixbufrot = pixbuf;
  345. } else if (current_rotate == 90) {
  346. pixbufrot = gdk_pixbuf_rotate_simple(pixbuf, GDK_PIXBUF_ROTATE_COUNTERCLOCKWISE);
  347. } else if (current_rotate == 180) {
  348. pixbufrot = gdk_pixbuf_rotate_simple(pixbuf, GDK_PIXBUF_ROTATE_UPSIDEDOWN);
  349. } else if (current_rotate == 270) {
  350. pixbufrot = gdk_pixbuf_rotate_simple(pixbuf, GDK_PIXBUF_ROTATE_CLOCKWISE);
  351. }
  352. if (capture) {
  353. time(&rawtime);
  354. tim = *(localtime(&rawtime));
  355. strftime(timestamp, 30, "%Y%m%d%H%M%S", &tim);
  356. sprintf(fname, "%s/Pictures/IMG%s.jpg", getenv("HOME"), timestamp);
  357. printf("Saving image\n");
  358. gdk_pixbuf_save(pixbufrot, fname, "jpeg", &error, "quality", "95", NULL);
  359. if (error != NULL) {
  360. g_printerr("%s\n", error->message);
  361. g_clear_error(&error);
  362. }
  363. } else {
  364. scale = (double) preview_width / gdk_pixbuf_get_width(pixbufrot);
  365. cr = cairo_create(surface);
  366. cairo_set_source_rgb(cr, 0, 0, 0);
  367. cairo_paint(cr);
  368. cairo_scale(cr, scale, scale);
  369. gdk_cairo_set_source_pixbuf(cr, pixbufrot, 0, 0);
  370. cairo_pattern_set_extend(cairo_get_source(cr), CAIRO_EXTEND_NONE);
  371. cairo_paint(cr);
  372. gtk_widget_queue_draw_area(preview, 0, 0, preview_width, preview_height);
  373. }
  374. capture = 0;
  375. t = clock() - t;
  376. time_taken = ((double) t) / CLOCKS_PER_SEC;
  377. //printf("%f fps\n", 1.0 / time_taken);
  378. }
  379. static gboolean
  380. preview_draw(GtkWidget *widget, cairo_t *cr, gpointer data)
  381. {
  382. cairo_set_source_surface(cr, surface, 0, 0);
  383. cairo_paint(cr);
  384. return FALSE;
  385. }
  386. static gboolean
  387. preview_configure(GtkWidget *widget, GdkEventConfigure *event)
  388. {
  389. cairo_t *cr;
  390. if (surface)
  391. cairo_surface_destroy(surface);
  392. surface = gdk_window_create_similar_surface(gtk_widget_get_window(widget),
  393. CAIRO_CONTENT_COLOR,
  394. gtk_widget_get_allocated_width(widget),
  395. gtk_widget_get_allocated_height(widget));
  396. preview_width = gtk_widget_get_allocated_width(widget);
  397. preview_height = gtk_widget_get_allocated_height(widget);
  398. cr = cairo_create(surface);
  399. cairo_set_source_rgb(cr, 0, 0, 0);
  400. cairo_paint(cr);
  401. cairo_destroy(cr);
  402. return TRUE;
  403. }
  404. static int
  405. read_frame(int fd)
  406. {
  407. struct v4l2_buffer buf = {0};
  408. buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  409. buf.memory = V4L2_MEMORY_MMAP;
  410. if (xioctl(fd, VIDIOC_DQBUF, &buf) == -1) {
  411. switch (errno) {
  412. case EAGAIN:
  413. return 0;
  414. case EIO:
  415. /* Could ignore EIO, see spec. */
  416. /* fallthrough */
  417. default:
  418. errno_exit("VIDIOC_DQBUF");
  419. break;
  420. }
  421. }
  422. //assert(buf.index < n_buffers);
  423. process_image(buffers[buf.index].start, buf.bytesused);
  424. if (xioctl(fd, VIDIOC_QBUF, &buf) == -1) {
  425. errno_exit("VIDIOC_QBUF");
  426. }
  427. return 1;
  428. }
  429. gboolean
  430. get_frame()
  431. {
  432. if (ready == 0)
  433. return TRUE;
  434. while (1) {
  435. fd_set fds;
  436. struct timeval tv;
  437. int r;
  438. FD_ZERO(&fds);
  439. FD_SET(video_fd, &fds);
  440. /* Timeout. */
  441. tv.tv_sec = 2;
  442. tv.tv_usec = 0;
  443. r = select(video_fd + 1, &fds, NULL, NULL, &tv);
  444. if (r == -1) {
  445. if (EINTR == errno) {
  446. continue;
  447. }
  448. errno_exit("select");
  449. } else if (r == 0) {
  450. fprintf(stderr, "select timeout\\n");
  451. exit(EXIT_FAILURE);
  452. }
  453. if (read_frame(video_fd)) {
  454. break;
  455. }
  456. /* EAGAIN - continue select loop. */
  457. }
  458. return TRUE;
  459. }
  460. int
  461. strtoint(const char *nptr, char **endptr, int base)
  462. {
  463. long x = strtol(nptr, endptr, base);
  464. assert(x <= INT_MAX);
  465. return (int) x;
  466. }
  467. static int
  468. config_ini_handler(void *user, const char *section, const char *name,
  469. const char *value)
  470. {
  471. if (strcmp(section, "rear") == 0) {
  472. if (strcmp(name, "width") == 0) {
  473. rear_width = strtoint(value, NULL, 10);
  474. } else if (strcmp(name, "height") == 0) {
  475. rear_height = strtoint(value, NULL, 10);
  476. } else if (strcmp(name, "rate") == 0) {
  477. rear_rate = strtoint(value, NULL, 10);
  478. } else if (strcmp(name, "rotate") == 0) {
  479. rear_rotate = strtoint(value, NULL, 10);
  480. } else if (strcmp(name, "fmt") == 0) {
  481. if (strcmp(value, "RGB") == 0) {
  482. rear_fmt = V4L2_PIX_FMT_RGB24;
  483. } else if (strcmp(value, "UYVY") == 0) {
  484. rear_fmt = V4L2_PIX_FMT_UYVY;
  485. } else if (strcmp(value, "YUYV") == 0) {
  486. rear_fmt = V4L2_PIX_FMT_YUYV;
  487. } else if (strcmp(value, "JPEG") == 0) {
  488. rear_fmt = V4L2_PIX_FMT_JPEG;
  489. } else if (strcmp(value, "NV12") == 0) {
  490. rear_fmt = V4L2_PIX_FMT_NV12;
  491. } else if (strcmp(value, "YUV420") == 0
  492. || strcmp(value, "I420") == 0
  493. || strcmp(value, "YU12") == 0) {
  494. rear_fmt = V4L2_PIX_FMT_YUV420;
  495. } else if (strcmp(value, "YVU420") == 0
  496. || strcmp(value, "YV12") == 0) {
  497. rear_fmt = V4L2_PIX_FMT_YVU420;
  498. } else if (strcmp(value, "RGGB8") == 0) {
  499. rear_fmt = V4L2_PIX_FMT_SRGGB8;
  500. } else if (strcmp(value, "BGGR8") == 0) {
  501. rear_fmt = V4L2_PIX_FMT_SBGGR8;
  502. rear_mbus = MEDIA_BUS_FMT_SBGGR8_1X8;
  503. } else if (strcmp(value, "GRBG8") == 0) {
  504. rear_fmt = V4L2_PIX_FMT_SGRBG8;
  505. } else if (strcmp(value, "GBRG8") == 0) {
  506. rear_fmt = V4L2_PIX_FMT_SGBRG8;
  507. } else {
  508. g_printerr("Unsupported pixelformat %s\n", value);
  509. exit(1);
  510. }
  511. } else if (strcmp(name, "driver") == 0) {
  512. rear_dev_name = strdup(value);
  513. } else {
  514. g_printerr("Unknown key '%s' in [rear]\n", name);
  515. exit(1);
  516. }
  517. } else if (strcmp(section, "front") == 0) {
  518. if (strcmp(name, "width") == 0) {
  519. front_width = strtoint(value, NULL, 10);
  520. } else if (strcmp(name, "height") == 0) {
  521. front_height = strtoint(value, NULL, 10);
  522. } else if (strcmp(name, "rate") == 0) {
  523. front_rate = strtoint(value, NULL, 10);
  524. } else if (strcmp(name, "rotate") == 0) {
  525. front_rotate = strtoint(value, NULL, 10);
  526. } else if (strcmp(name, "fmt") == 0) {
  527. if (strcmp(value, "RGB") == 0) {
  528. front_fmt = V4L2_PIX_FMT_RGB24;
  529. } else if (strcmp(value, "UYVY") == 0) {
  530. front_fmt = V4L2_PIX_FMT_UYVY;
  531. } else if (strcmp(value, "YUYV") == 0) {
  532. front_fmt = V4L2_PIX_FMT_YUYV;
  533. } else if (strcmp(value, "JPEG") == 0) {
  534. front_fmt = V4L2_PIX_FMT_JPEG;
  535. } else if (strcmp(value, "NV12") == 0) {
  536. front_fmt = V4L2_PIX_FMT_NV12;
  537. } else if (strcmp(value, "YUV420") == 0
  538. || strcmp(value, "I420") == 0
  539. || strcmp(value, "YU12") == 0) {
  540. front_fmt = V4L2_PIX_FMT_YUV420;
  541. } else if (strcmp(value, "YVU420") == 0
  542. || strcmp(value, "YV12") == 0) {
  543. front_fmt = V4L2_PIX_FMT_YVU420;
  544. } else if (strcmp(value, "RGGB8") == 0) {
  545. front_fmt = V4L2_PIX_FMT_SRGGB8;
  546. } else if (strcmp(value, "BGGR8") == 0) {
  547. front_fmt = V4L2_PIX_FMT_SBGGR8;
  548. front_mbus = MEDIA_BUS_FMT_SBGGR8_1X8;
  549. } else if (strcmp(value, "GRBG8") == 0) {
  550. front_fmt = V4L2_PIX_FMT_SGRBG8;
  551. } else if (strcmp(value, "GBRG8") == 0) {
  552. front_fmt = V4L2_PIX_FMT_SGBRG8;
  553. } else {
  554. g_printerr("Unsupported pixelformat %s\n", value);
  555. exit(1);
  556. }
  557. } else if (strcmp(name, "driver") == 0) {
  558. front_dev_name = strdup(value);
  559. } else {
  560. g_printerr("Unknown key '%s' in [front]\n", name);
  561. exit(1);
  562. }
  563. } else if (strcmp(section, "device") == 0) {
  564. if (strcmp(name, "csi") == 0) {
  565. media_drv_name = strdup(value);
  566. } else {
  567. g_printerr("Unknown key '%s' in [device]\n", name);
  568. exit(1);
  569. }
  570. } else {
  571. g_printerr("Unknown section '%s' in config file\n", section);
  572. exit(1);
  573. }
  574. return 1;
  575. }
  576. int
  577. find_dev_node(int maj, int min, char *fnbuf)
  578. {
  579. DIR *d;
  580. struct dirent *dir;
  581. struct stat info;
  582. d = opendir("/dev");
  583. while ((dir = readdir(d)) != NULL) {
  584. sprintf(fnbuf, "/dev/%s", dir->d_name);
  585. stat(fnbuf, &info);
  586. if (!S_ISCHR(info.st_mode)) {
  587. continue;
  588. }
  589. if (major(info.st_rdev) == maj && minor(info.st_rdev) == min) {
  590. return 0;
  591. }
  592. }
  593. return -1;
  594. }
  595. int
  596. setup_rear()
  597. {
  598. struct media_link_desc link = {0};
  599. // Disable the interface<->front link
  600. link.flags = 0;
  601. link.source.entity = front_entity_id;
  602. link.source.index = 0;
  603. link.sink.entity = interface_entity_id;
  604. link.sink.index = 0;
  605. if (xioctl(media_fd, MEDIA_IOC_SETUP_LINK, &link) < 0) {
  606. g_printerr("Could not disable front camera link\n");
  607. return -1;
  608. }
  609. // Enable the interface<->rear link
  610. link.flags = MEDIA_LNK_FL_ENABLED;
  611. link.source.entity = rear_entity_id;
  612. link.source.index = 0;
  613. link.sink.entity = interface_entity_id;
  614. link.sink.index = 0;
  615. if (xioctl(media_fd, MEDIA_IOC_SETUP_LINK, &link) < 0) {
  616. g_printerr("Could not enable rear camera link\n");
  617. return -1;
  618. }
  619. current_width = rear_width;
  620. current_height = rear_height;
  621. current_fmt = rear_fmt;
  622. current_rotate = rear_rotate;
  623. // Find camera node
  624. init_sensor(rear_dev, rear_width, rear_height, rear_mbus, rear_rate);
  625. return 0;
  626. }
  627. int
  628. setup_front()
  629. {
  630. struct media_link_desc link = {0};
  631. // Disable the interface<->rear link
  632. link.flags = 0;
  633. link.source.entity = rear_entity_id;
  634. link.source.index = 0;
  635. link.sink.entity = interface_entity_id;
  636. link.sink.index = 0;
  637. if (xioctl(media_fd, MEDIA_IOC_SETUP_LINK, &link) < 0) {
  638. g_printerr("Could not disable rear camera link\n");
  639. return -1;
  640. }
  641. // Enable the interface<->rear link
  642. link.flags = MEDIA_LNK_FL_ENABLED;
  643. link.source.entity = front_entity_id;
  644. link.source.index = 0;
  645. link.sink.entity = interface_entity_id;
  646. link.sink.index = 0;
  647. if (xioctl(media_fd, MEDIA_IOC_SETUP_LINK, &link) < 0) {
  648. g_printerr("Could not enable front camera link\n");
  649. return -1;
  650. }
  651. current_width = front_width;
  652. current_height = front_height;
  653. current_fmt = front_fmt;
  654. current_rotate = front_rotate;
  655. // Find camera node
  656. init_sensor(front_dev, front_width, front_height, front_mbus, front_rate);
  657. return 0;
  658. }
  659. int
  660. find_cameras()
  661. {
  662. struct media_entity_desc entity = {0};
  663. int ret;
  664. int found = 0;
  665. while (1) {
  666. entity.id = entity.id | MEDIA_ENT_ID_FLAG_NEXT;
  667. ret = xioctl(media_fd, MEDIA_IOC_ENUM_ENTITIES, &entity);
  668. if (ret < 0) {
  669. break;
  670. }
  671. printf("At node %s, (0x%x)\n", entity.name, entity.type);
  672. if (strncmp(entity.name, front_dev_name, strlen(front_dev_name)) == 0) {
  673. front_entity_id = entity.id;
  674. find_dev_node(entity.dev.major, entity.dev.minor, front_dev);
  675. printf("Found front cam, is %s at %s\n", entity.name, front_dev);
  676. found++;
  677. }
  678. if (strncmp(entity.name, rear_dev_name, strlen(rear_dev_name)) == 0) {
  679. rear_entity_id = entity.id;
  680. find_dev_node(entity.dev.major, entity.dev.minor, rear_dev);
  681. printf("Found rear cam, is %s at %s\n", entity.name, rear_dev);
  682. found++;
  683. }
  684. if (entity.type == MEDIA_ENT_F_IO_V4L) {
  685. interface_entity_id = entity.id;
  686. find_dev_node(entity.dev.major, entity.dev.minor, dev_name);
  687. printf("Found v4l2 interface node at %s\n", dev_name);
  688. }
  689. }
  690. if (found < 2) {
  691. return -1;
  692. }
  693. return 0;
  694. }
  695. int
  696. find_media_fd()
  697. {
  698. DIR *d;
  699. struct dirent *dir;
  700. int fd;
  701. char fnbuf[261];
  702. struct media_device_info mdi = {0};
  703. d = opendir("/dev");
  704. while ((dir = readdir(d)) != NULL) {
  705. if (strncmp(dir->d_name, "media", 5) == 0) {
  706. sprintf(fnbuf, "/dev/%s", dir->d_name);
  707. printf("Checking %s\n", fnbuf);
  708. fd = open(fnbuf, O_RDWR);
  709. xioctl(fd, MEDIA_IOC_DEVICE_INFO, &mdi);
  710. printf("Found media device: %s\n", mdi.driver);
  711. if (strcmp(mdi.driver, media_drv_name) == 0) {
  712. media_fd = fd;
  713. return 0;
  714. }
  715. close(fd);
  716. }
  717. }
  718. return 1;
  719. }
  720. void
  721. on_shutter_clicked(GtkWidget *widget, gpointer user_data)
  722. {
  723. capture = 1;
  724. }
  725. void
  726. on_error_close_clicked(GtkWidget *widget, gpointer user_data)
  727. {
  728. gtk_widget_hide(error_box);
  729. }
  730. void
  731. on_camera_switch_clicked(GtkWidget *widget, gpointer user_data)
  732. {
  733. stop_capturing(video_fd);
  734. close(current_fd);
  735. if (current_is_rear == 1) {
  736. setup_front();
  737. current_is_rear = 0;
  738. } else {
  739. setup_rear();
  740. current_is_rear = 1;
  741. }
  742. close(video_fd);
  743. video_fd = open(dev_name, O_RDWR);
  744. if (video_fd == -1) {
  745. g_printerr("Error opening video device: %s\n", dev_name);
  746. return;
  747. }
  748. init_device(video_fd);
  749. start_capturing(video_fd);
  750. }
  751. int
  752. find_config(char *conffile)
  753. {
  754. char buf[512];
  755. char *xdg_config_home;
  756. wordexp_t exp_result;
  757. FILE *fp;
  758. // Resolve XDG stuff
  759. if ((xdg_config_home = getenv("XDG_CONFIG_HOME")) == NULL) {
  760. xdg_config_home = "~/.config";
  761. }
  762. wordexp(xdg_config_home, &exp_result, 0);
  763. xdg_config_home = strdup(exp_result.we_wordv[0]);
  764. wordfree(&exp_result);
  765. if(access("/proc/device-tree/compatible", F_OK) != -1) {
  766. // Reads to compatible string of the current device tree, looks like:
  767. // pine64,pinephone-1.2\0allwinner,sun50i-a64\0
  768. fp = fopen("/proc/device-tree/compatible", "r");
  769. fgets(buf, 512, fp);
  770. fclose(fp);
  771. // Check for a config file in XDG_CONFIG_HOME
  772. sprintf(conffile, "%s/megapixels/config/%s.ini", xdg_config_home, buf);
  773. if(access(conffile, F_OK) != -1) {
  774. printf("Found config file at %s\n", conffile);
  775. return 0;
  776. }
  777. // Check user overridden /etc/megapixels/config/$dt.ini
  778. sprintf(conffile, "%s/megapixels/config/%s.ini", SYSCONFDIR, buf);
  779. if(access(conffile, F_OK) != -1) {
  780. printf("Found config file at %s\n", conffile);
  781. return 0;
  782. }
  783. // Check packaged /usr/share/megapixels/config/$dt.ini
  784. sprintf(conffile, "%s/megapixels/config/%s.ini", DATADIR, buf);
  785. if(access(conffile, F_OK) != -1) {
  786. printf("Found config file at %s\n", conffile);
  787. return 0;
  788. }
  789. // Check config/%dt.ini in the current working directory
  790. sprintf(conffile, "config/%s.ini", buf);
  791. if(access(conffile, F_OK) != -1) {
  792. printf("Found config file at %s\n", conffile);
  793. return 0;
  794. }
  795. printf("%s not found\n", conffile);
  796. } else {
  797. printf("Could not read device name from device tree\n");
  798. }
  799. // If all else fails, fall back to /etc/megapixels.ini
  800. conffile = "/etc/megapixels.ini";
  801. if(access(conffile, F_OK) != -1) {
  802. printf("Found config file at %s\n", conffile);
  803. return 0;
  804. }
  805. return -1;
  806. }
  807. int
  808. main(int argc, char *argv[])
  809. {
  810. char conffile[512];
  811. find_config(conffile);
  812. gtk_init(&argc, &argv);
  813. g_object_set(gtk_settings_get_default(), "gtk-application-prefer-dark-theme", TRUE, NULL);
  814. GtkBuilder *builder = gtk_builder_new_from_resource("/org/postmarketos/Megapixels/camera.glade");
  815. GtkWidget *window = GTK_WIDGET(gtk_builder_get_object(builder, "window"));
  816. GtkWidget *preview_box = GTK_WIDGET(gtk_builder_get_object(builder, "preview_box"));
  817. GtkWidget *shutter = GTK_WIDGET(gtk_builder_get_object(builder, "shutter"));
  818. GtkWidget *switch_btn = GTK_WIDGET(gtk_builder_get_object(builder, "switch_camera"));
  819. GtkWidget *settings_btn = GTK_WIDGET(gtk_builder_get_object(builder, "settings"));
  820. GtkWidget *error_close = GTK_WIDGET(gtk_builder_get_object(builder, "error_close"));
  821. preview = GTK_WIDGET(gtk_builder_get_object(builder, "preview"));
  822. error_box = GTK_WIDGET(gtk_builder_get_object(builder, "error_box"));
  823. error_message = GTK_WIDGET(gtk_builder_get_object(builder, "error_message"));
  824. g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);
  825. g_signal_connect(shutter, "clicked", G_CALLBACK(on_shutter_clicked), NULL);
  826. g_signal_connect(error_close, "clicked", G_CALLBACK(on_error_close_clicked), NULL);
  827. g_signal_connect(switch_btn, "clicked", G_CALLBACK(on_camera_switch_clicked), NULL);
  828. g_signal_connect(preview, "draw", G_CALLBACK(preview_draw), NULL);
  829. g_signal_connect(preview, "configure-event", G_CALLBACK(preview_configure), NULL);
  830. GtkCssProvider *provider = gtk_css_provider_new();
  831. if (access("camera.css", F_OK) != -1) {
  832. gtk_css_provider_load_from_path(provider, "camera.css", NULL);
  833. } else {
  834. gtk_css_provider_load_from_resource(provider, "/org/postmarketos/Megapixels/camera.css");
  835. }
  836. GtkStyleContext *context = gtk_widget_get_style_context(preview_box);
  837. gtk_style_context_add_provider(context,
  838. GTK_STYLE_PROVIDER(provider),
  839. GTK_STYLE_PROVIDER_PRIORITY_USER);
  840. context = gtk_widget_get_style_context(error_box);
  841. gtk_style_context_add_provider(context,
  842. GTK_STYLE_PROVIDER(provider),
  843. GTK_STYLE_PROVIDER_PRIORITY_USER);
  844. int result = ini_parse(conffile, config_ini_handler, NULL);
  845. if (result == -1) {
  846. g_printerr("Config file not found\n");
  847. return 1;
  848. } else if (result == -2) {
  849. g_printerr("Could not allocate memory to parse config file\n");
  850. return 1;
  851. } else if (result != 0) {
  852. g_printerr("Could not parse config file\n");
  853. return 1;
  854. }
  855. if (find_media_fd() == -1) {
  856. g_printerr("Could not find the media node\n");
  857. show_error("Could not find the media node");
  858. goto failed;
  859. }
  860. if (find_cameras() == -1) {
  861. g_printerr("Could not find the cameras\n");
  862. show_error("Could not find the cameras");
  863. goto failed;
  864. }
  865. setup_rear();
  866. int fd = open(dev_name, O_RDWR);
  867. if (fd == -1) {
  868. g_printerr("Error opening video device: %s\n", dev_name);
  869. show_error("Error opening the video device");
  870. goto failed;
  871. }
  872. video_fd = fd;
  873. if(init_device(fd) < 0){
  874. goto failed;
  875. }
  876. start_capturing(fd);
  877. failed:
  878. printf("window show\n");
  879. gtk_widget_show(window);
  880. g_idle_add((GSourceFunc)get_frame, NULL);
  881. gtk_main();
  882. return 0;
  883. }