main.c 21 KB

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