main.c 49 KB

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