main.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137
  1. #include "main.h"
  2. #include "camera_config.h"
  3. #include "flash.h"
  4. #include "gl_util.h"
  5. #include "io_pipeline.h"
  6. #include "process_pipeline.h"
  7. #include <asm/errno.h>
  8. #include <assert.h>
  9. #include <errno.h>
  10. #include <fcntl.h>
  11. #include <gtk/gtk.h>
  12. #include <limits.h>
  13. #include <linux/kdev_t.h>
  14. #include <linux/media.h>
  15. #include <linux/v4l2-subdev.h>
  16. #include <linux/videodev2.h>
  17. #include <locale.h>
  18. #include <sys/ioctl.h>
  19. #include <sys/mman.h>
  20. #include <sys/stat.h>
  21. #include <sys/sysmacros.h>
  22. #include <time.h>
  23. #include <wordexp.h>
  24. #include <zbar.h>
  25. // #define RENDERDOC
  26. #ifdef RENDERDOC
  27. #include <dlfcn.h>
  28. #include <renderdoc/app.h>
  29. RENDERDOC_API_1_1_2 *rdoc_api = NULL;
  30. #endif
  31. enum user_control { USER_CONTROL_ISO, USER_CONTROL_SHUTTER };
  32. static bool camera_is_initialized = false;
  33. static const struct mp_camera_config *camera = NULL;
  34. static MPMode mode;
  35. static int preview_width = -1;
  36. static int preview_height = -1;
  37. static int device_rotation = 0;
  38. static bool gain_is_manual = false;
  39. static int gain;
  40. static int gain_max;
  41. static bool exposure_is_manual = false;
  42. static int exposure;
  43. static bool has_auto_focus_continuous;
  44. static bool has_auto_focus_start;
  45. static bool flash_enabled = false;
  46. static bool setting_save_dng;
  47. static MPProcessPipelineBuffer *current_preview_buffer = NULL;
  48. static int preview_buffer_width = -1;
  49. static int preview_buffer_height = -1;
  50. static char last_path[260] = "";
  51. static MPZBarScanResult *zbar_result = NULL;
  52. static int burst_length = 0;
  53. // Widgets
  54. GtkWidget *preview;
  55. GtkWidget *main_stack;
  56. GtkWidget *open_last_stack;
  57. GtkWidget *thumb_last;
  58. GtkWidget *process_spinner;
  59. GtkWidget *scanned_codes;
  60. GtkWidget *preview_top_box;
  61. GtkWidget *preview_bottom_box;
  62. GtkWidget *flash_button;
  63. GSettings *settings;
  64. int
  65. remap(int value, int input_min, int input_max, int output_min, int output_max)
  66. {
  67. const long long factor = 1000000000;
  68. long long output_spread = output_max - output_min;
  69. long long input_spread = input_max - input_min;
  70. long long zero_value = value - input_min;
  71. zero_value *= factor;
  72. long long percentage = zero_value / input_spread;
  73. long long zero_output = percentage * output_spread / factor;
  74. long long result = output_min + zero_output;
  75. return (int)result;
  76. }
  77. static void
  78. update_io_pipeline()
  79. {
  80. struct mp_io_pipeline_state io_state = {
  81. .camera = camera,
  82. .burst_length = burst_length,
  83. .preview_width = preview_width,
  84. .preview_height = preview_height,
  85. .device_rotation = device_rotation,
  86. .gain_is_manual = gain_is_manual,
  87. .gain = gain,
  88. .exposure_is_manual = exposure_is_manual,
  89. .exposure = exposure,
  90. .save_dng = setting_save_dng,
  91. .flash_enabled = flash_enabled,
  92. };
  93. mp_io_pipeline_update_state(&io_state);
  94. // Make the right settings available for the camera
  95. gtk_widget_set_visible(flash_button, camera->has_flash);
  96. }
  97. static bool
  98. update_state(const struct mp_main_state *state)
  99. {
  100. if (!camera_is_initialized) {
  101. camera_is_initialized = true;
  102. }
  103. if (camera == state->camera) {
  104. mode = state->mode;
  105. if (!gain_is_manual) {
  106. gain = state->gain;
  107. }
  108. gain_max = state->gain_max;
  109. if (!exposure_is_manual) {
  110. exposure = state->exposure;
  111. }
  112. has_auto_focus_continuous = state->has_auto_focus_continuous;
  113. has_auto_focus_start = state->has_auto_focus_start;
  114. }
  115. preview_buffer_width = state->image_width;
  116. preview_buffer_height = state->image_height;
  117. return false;
  118. }
  119. void
  120. mp_main_update_state(const struct mp_main_state *state)
  121. {
  122. struct mp_main_state *state_copy = malloc(sizeof(struct mp_main_state));
  123. *state_copy = *state;
  124. g_main_context_invoke_full(g_main_context_default(),
  125. G_PRIORITY_DEFAULT_IDLE,
  126. (GSourceFunc)update_state,
  127. state_copy,
  128. free);
  129. }
  130. static bool
  131. set_zbar_result(MPZBarScanResult *result)
  132. {
  133. if (zbar_result) {
  134. for (uint8_t i = 0; i < zbar_result->size; ++i) {
  135. free(zbar_result->codes[i].data);
  136. }
  137. free(zbar_result);
  138. }
  139. zbar_result = result;
  140. gtk_widget_queue_draw(preview);
  141. return false;
  142. }
  143. void
  144. mp_main_set_zbar_result(MPZBarScanResult *result)
  145. {
  146. g_main_context_invoke_full(g_main_context_default(),
  147. G_PRIORITY_DEFAULT_IDLE,
  148. (GSourceFunc)set_zbar_result,
  149. result,
  150. NULL);
  151. }
  152. static bool
  153. set_preview(MPProcessPipelineBuffer *buffer)
  154. {
  155. if (current_preview_buffer) {
  156. mp_process_pipeline_buffer_unref(current_preview_buffer);
  157. }
  158. current_preview_buffer = buffer;
  159. gtk_widget_queue_draw(preview);
  160. return false;
  161. }
  162. void
  163. mp_main_set_preview(MPProcessPipelineBuffer *buffer)
  164. {
  165. g_main_context_invoke_full(g_main_context_default(),
  166. G_PRIORITY_DEFAULT_IDLE,
  167. (GSourceFunc)set_preview,
  168. buffer,
  169. NULL);
  170. }
  171. struct capture_completed_args {
  172. GdkTexture *thumb;
  173. char *fname;
  174. };
  175. static bool
  176. capture_completed(struct capture_completed_args *args)
  177. {
  178. strncpy(last_path, args->fname, 259);
  179. gtk_image_set_from_paintable(GTK_IMAGE(thumb_last),
  180. GDK_PAINTABLE(args->thumb));
  181. gtk_spinner_stop(GTK_SPINNER(process_spinner));
  182. gtk_stack_set_visible_child(GTK_STACK(open_last_stack), thumb_last);
  183. g_object_unref(args->thumb);
  184. g_free(args->fname);
  185. return false;
  186. }
  187. void
  188. mp_main_capture_completed(GdkTexture *thumb, const char *fname)
  189. {
  190. struct capture_completed_args *args =
  191. malloc(sizeof(struct capture_completed_args));
  192. args->thumb = thumb;
  193. args->fname = g_strdup(fname);
  194. g_main_context_invoke_full(g_main_context_default(),
  195. G_PRIORITY_DEFAULT_IDLE,
  196. (GSourceFunc)capture_completed,
  197. args,
  198. free);
  199. }
  200. static GLuint blit_program;
  201. static GLuint blit_uniform_transform;
  202. static GLuint blit_uniform_texture;
  203. static GLuint solid_program;
  204. static GLuint solid_uniform_color;
  205. static GLuint quad;
  206. static void
  207. preview_realize(GtkGLArea *area)
  208. {
  209. gtk_gl_area_make_current(area);
  210. if (gtk_gl_area_get_error(area) != NULL) {
  211. return;
  212. }
  213. // Make a VAO for OpenGL
  214. if (!gtk_gl_area_get_use_es(area)) {
  215. GLuint vao;
  216. glGenVertexArrays(1, &vao);
  217. glBindVertexArray(vao);
  218. check_gl();
  219. }
  220. GLuint blit_shaders[] = {
  221. gl_util_load_shader("/org/postmarketos/Megapixels/blit.vert",
  222. GL_VERTEX_SHADER,
  223. NULL,
  224. 0),
  225. gl_util_load_shader("/org/postmarketos/Megapixels/blit.frag",
  226. GL_FRAGMENT_SHADER,
  227. NULL,
  228. 0),
  229. };
  230. blit_program = gl_util_link_program(blit_shaders, 2);
  231. glBindAttribLocation(blit_program, GL_UTIL_VERTEX_ATTRIBUTE, "vert");
  232. glBindAttribLocation(blit_program, GL_UTIL_TEX_COORD_ATTRIBUTE, "tex_coord");
  233. check_gl();
  234. blit_uniform_transform = glGetUniformLocation(blit_program, "transform");
  235. blit_uniform_texture = glGetUniformLocation(blit_program, "texture");
  236. GLuint solid_shaders[] = {
  237. gl_util_load_shader("/org/postmarketos/Megapixels/solid.vert",
  238. GL_VERTEX_SHADER,
  239. NULL,
  240. 0),
  241. gl_util_load_shader("/org/postmarketos/Megapixels/solid.frag",
  242. GL_FRAGMENT_SHADER,
  243. NULL,
  244. 0),
  245. };
  246. solid_program = gl_util_link_program(solid_shaders, 2);
  247. glBindAttribLocation(solid_program, GL_UTIL_VERTEX_ATTRIBUTE, "vert");
  248. check_gl();
  249. solid_uniform_color = glGetUniformLocation(solid_program, "color");
  250. quad = gl_util_new_quad();
  251. }
  252. static void
  253. position_preview(float *offset_x, float *offset_y, float *size_x, float *size_y)
  254. {
  255. int buffer_width, buffer_height;
  256. if (device_rotation == 0 || device_rotation == 180) {
  257. buffer_width = preview_buffer_width;
  258. buffer_height = preview_buffer_height;
  259. } else {
  260. buffer_width = preview_buffer_height;
  261. buffer_height = preview_buffer_width;
  262. }
  263. int scale_factor = gtk_widget_get_scale_factor(preview);
  264. int top_height =
  265. gtk_widget_get_allocated_height(preview_top_box) * scale_factor;
  266. int bottom_height =
  267. gtk_widget_get_allocated_height(preview_bottom_box) * scale_factor;
  268. int inner_height = preview_height - top_height - bottom_height;
  269. double scale = MIN(preview_width / (float)buffer_width,
  270. preview_height / (float)buffer_height);
  271. *size_x = scale * buffer_width;
  272. *size_y = scale * buffer_height;
  273. *offset_x = (preview_width - *size_x) / 2.0;
  274. if (*size_y > inner_height) {
  275. *offset_y = (preview_height - *size_y) / 2.0;
  276. } else {
  277. *offset_y = top_height + (inner_height - *size_y) / 2.0;
  278. }
  279. }
  280. static gboolean
  281. preview_draw(GtkGLArea *area, GdkGLContext *ctx, gpointer data)
  282. {
  283. if (gtk_gl_area_get_error(area) != NULL) {
  284. return FALSE;
  285. }
  286. if (!camera_is_initialized) {
  287. return FALSE;
  288. }
  289. #ifdef RENDERDOC
  290. if (rdoc_api) {
  291. rdoc_api->StartFrameCapture(NULL, NULL);
  292. }
  293. #endif
  294. glClearColor(0, 0, 0, 1);
  295. glClear(GL_COLOR_BUFFER_BIT);
  296. float offset_x, offset_y, size_x, size_y;
  297. position_preview(&offset_x, &offset_y, &size_x, &size_y);
  298. glViewport(offset_x, preview_height - size_y - offset_y, size_x, size_y);
  299. if (current_preview_buffer) {
  300. glUseProgram(blit_program);
  301. GLfloat rotation_list[4] = { 0, -1, 0, 1 };
  302. int rotation_index = device_rotation / 90;
  303. GLfloat sin_rot = rotation_list[rotation_index];
  304. GLfloat cos_rot = rotation_list[(4 + rotation_index - 1) % 4];
  305. GLfloat matrix[9] = {
  306. // clang-format off
  307. cos_rot, sin_rot, 0,
  308. -sin_rot, cos_rot, 0,
  309. 0, 0, 1,
  310. // clang-format on
  311. };
  312. glUniformMatrix3fv(blit_uniform_transform, 1, GL_FALSE, matrix);
  313. check_gl();
  314. glActiveTexture(GL_TEXTURE0);
  315. glBindTexture(GL_TEXTURE_2D,
  316. mp_process_pipeline_buffer_get_texture_id(
  317. current_preview_buffer));
  318. glUniform1i(blit_uniform_texture, 0);
  319. check_gl();
  320. gl_util_bind_quad(quad);
  321. gl_util_draw_quad(quad);
  322. }
  323. if (zbar_result) {
  324. GLuint buffer;
  325. if (!gtk_gl_area_get_use_es(area)) {
  326. glGenBuffers(1, &buffer);
  327. glBindBuffer(GL_ARRAY_BUFFER, buffer);
  328. check_gl();
  329. }
  330. glUseProgram(solid_program);
  331. glEnable(GL_BLEND);
  332. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  333. glUniform4f(solid_uniform_color, 1, 0, 0, 0.5);
  334. for (uint8_t i = 0; i < zbar_result->size; ++i) {
  335. MPZBarCode *code = &zbar_result->codes[i];
  336. GLfloat vertices[] = {
  337. code->bounds_x[0], code->bounds_y[0],
  338. code->bounds_x[1], code->bounds_y[1],
  339. code->bounds_x[3], code->bounds_y[3],
  340. code->bounds_x[2], code->bounds_y[2],
  341. };
  342. for (int i = 0; i < 4; ++i) {
  343. vertices[i * 2] =
  344. 2 * vertices[i * 2] / preview_buffer_width -
  345. 1.0;
  346. vertices[i * 2 + 1] =
  347. 1.0 - 2 * vertices[i * 2 + 1] /
  348. preview_buffer_height;
  349. }
  350. if (gtk_gl_area_get_use_es(area)) {
  351. glVertexAttribPointer(GL_UTIL_VERTEX_ATTRIBUTE,
  352. 2,
  353. GL_FLOAT,
  354. 0,
  355. 0,
  356. vertices);
  357. check_gl();
  358. glEnableVertexAttribArray(GL_UTIL_VERTEX_ATTRIBUTE);
  359. check_gl();
  360. } else {
  361. glBufferData(GL_ARRAY_BUFFER,
  362. sizeof(vertices),
  363. vertices,
  364. GL_STREAM_DRAW);
  365. check_gl();
  366. glVertexAttribPointer(GL_UTIL_VERTEX_ATTRIBUTE,
  367. 2,
  368. GL_FLOAT,
  369. GL_FALSE,
  370. 0,
  371. 0);
  372. glEnableVertexAttribArray(GL_UTIL_VERTEX_ATTRIBUTE);
  373. check_gl();
  374. }
  375. glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
  376. check_gl();
  377. }
  378. glDisable(GL_BLEND);
  379. glBindBuffer(GL_ARRAY_BUFFER, 0);
  380. }
  381. glFlush();
  382. #ifdef RENDERDOC
  383. if (rdoc_api) {
  384. rdoc_api->EndFrameCapture(NULL, NULL);
  385. }
  386. #endif
  387. return FALSE;
  388. }
  389. static gboolean
  390. preview_resize(GtkWidget *widget, int width, int height, gpointer data)
  391. {
  392. if (preview_width != width || preview_height != height) {
  393. preview_width = width;
  394. preview_height = height;
  395. update_io_pipeline();
  396. }
  397. return TRUE;
  398. }
  399. void
  400. run_open_last_action(GSimpleAction *action, GVariant *param, gpointer user_data)
  401. {
  402. char uri[275];
  403. g_autoptr(GError) error = NULL;
  404. if (strlen(last_path) == 0) {
  405. return;
  406. }
  407. sprintf(uri, "file://%s", last_path);
  408. if (!g_app_info_launch_default_for_uri(uri, NULL, &error)) {
  409. g_printerr("Could not launch image viewer for '%s': %s\n",
  410. uri,
  411. error->message);
  412. }
  413. }
  414. void
  415. run_open_photos_action(GSimpleAction *action, GVariant *param, gpointer user_data)
  416. {
  417. char uri[270];
  418. g_autoptr(GError) error = NULL;
  419. sprintf(uri, "file://%s", g_get_user_special_dir(G_USER_DIRECTORY_PICTURES));
  420. if (!g_app_info_launch_default_for_uri(uri, NULL, &error)) {
  421. g_printerr("Could not launch image viewer: %s\n", error->message);
  422. }
  423. }
  424. void
  425. run_capture_action(GSimpleAction *action, GVariant *param, gpointer user_data)
  426. {
  427. gtk_spinner_start(GTK_SPINNER(process_spinner));
  428. gtk_stack_set_visible_child(GTK_STACK(open_last_stack), process_spinner);
  429. mp_io_pipeline_capture();
  430. }
  431. void
  432. run_about_action(GSimpleAction *action, GVariant *param, GApplication *app)
  433. {
  434. GtkWindow *parent = gtk_application_get_active_window(GTK_APPLICATION(app));
  435. gtk_show_about_dialog(parent,
  436. "program-name",
  437. "Megapixels",
  438. "title",
  439. "Megapixels",
  440. "logo-icon-name",
  441. "org.postmarketos.Megapixels",
  442. "comments",
  443. "The postmarketOS camera application",
  444. "website",
  445. "https://gitlab.com/postmarketOS/megapixels",
  446. "version",
  447. VERSION,
  448. "license-type",
  449. GTK_LICENSE_GPL_3_0_ONLY,
  450. NULL);
  451. }
  452. void
  453. run_quit_action(GSimpleAction *action, GVariant *param, GApplication *app)
  454. {
  455. g_application_quit(app);
  456. }
  457. static bool
  458. check_point_inside_bounds(int x, int y, int *bounds_x, int *bounds_y)
  459. {
  460. bool right = false, left = false, top = false, bottom = false;
  461. for (int i = 0; i < 4; ++i) {
  462. if (x <= bounds_x[i])
  463. left = true;
  464. if (x >= bounds_x[i])
  465. right = true;
  466. if (y <= bounds_y[i])
  467. top = true;
  468. if (y >= bounds_y[i])
  469. bottom = true;
  470. }
  471. return right && left && top && bottom;
  472. }
  473. static void
  474. on_zbar_dialog_response(GtkDialog *dialog, int response, char *data)
  475. {
  476. g_autoptr(GError) error = NULL;
  477. switch (response) {
  478. case GTK_RESPONSE_YES:
  479. if (!g_app_info_launch_default_for_uri(data, NULL, &error)) {
  480. g_printerr("Could not launch application: %s\n",
  481. error->message);
  482. }
  483. case GTK_RESPONSE_ACCEPT: {
  484. GdkDisplay *display = gtk_widget_get_display(GTK_WIDGET(dialog));
  485. gdk_clipboard_set_text(gdk_display_get_clipboard(display), data);
  486. }
  487. case GTK_RESPONSE_CANCEL:
  488. break;
  489. default:
  490. g_printerr("Wrong dialog response: %d\n", response);
  491. }
  492. g_free(data);
  493. gtk_window_destroy(GTK_WINDOW(dialog));
  494. }
  495. static void
  496. on_zbar_code_tapped(GtkWidget *widget, const MPZBarCode *code)
  497. {
  498. GtkWidget *dialog;
  499. GtkDialogFlags flags = GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT;
  500. bool data_is_url =
  501. g_uri_is_valid(code->data, G_URI_FLAGS_PARSE_RELAXED, NULL);
  502. char *data = strdup(code->data);
  503. if (data_is_url) {
  504. dialog = gtk_message_dialog_new(
  505. GTK_WINDOW(gtk_widget_get_root(widget)),
  506. flags,
  507. GTK_MESSAGE_QUESTION,
  508. GTK_BUTTONS_NONE,
  509. "Found a URL '%s' encoded in a %s.",
  510. code->data,
  511. code->type);
  512. gtk_dialog_add_buttons(
  513. GTK_DIALOG(dialog), "_Open URL", GTK_RESPONSE_YES, NULL);
  514. } else {
  515. dialog = gtk_message_dialog_new(
  516. GTK_WINDOW(gtk_widget_get_root(widget)),
  517. flags,
  518. GTK_MESSAGE_QUESTION,
  519. GTK_BUTTONS_NONE,
  520. "Found data encoded in a %s.",
  521. code->type);
  522. gtk_message_dialog_format_secondary_markup(
  523. GTK_MESSAGE_DIALOG(dialog), "<small>%s</small>", code->data);
  524. }
  525. gtk_dialog_add_buttons(GTK_DIALOG(dialog),
  526. "_Copy",
  527. GTK_RESPONSE_ACCEPT,
  528. "_Cancel",
  529. GTK_RESPONSE_CANCEL,
  530. NULL);
  531. g_signal_connect(
  532. dialog, "response", G_CALLBACK(on_zbar_dialog_response), data);
  533. gtk_widget_show(GTK_WIDGET(dialog));
  534. }
  535. static void
  536. preview_pressed(GtkGestureClick *gesture, int n_press, double x, double y)
  537. {
  538. GtkWidget *widget =
  539. gtk_event_controller_get_widget(GTK_EVENT_CONTROLLER(gesture));
  540. int scale_factor = gtk_widget_get_scale_factor(widget);
  541. // Tapped zbar result
  542. if (zbar_result) {
  543. // Transform the event coordinates to the image
  544. float offset_x, offset_y, size_x, size_y;
  545. position_preview(&offset_x, &offset_y, &size_x, &size_y);
  546. int zbar_x = (x - offset_x) * scale_factor / size_x *
  547. preview_buffer_width;
  548. int zbar_y = (y - offset_y) * scale_factor / size_y *
  549. preview_buffer_height;
  550. for (uint8_t i = 0; i < zbar_result->size; ++i) {
  551. MPZBarCode *code = &zbar_result->codes[i];
  552. if (check_point_inside_bounds(zbar_x,
  553. zbar_y,
  554. code->bounds_x,
  555. code->bounds_y)) {
  556. on_zbar_code_tapped(widget, code);
  557. return;
  558. }
  559. }
  560. }
  561. // Tapped preview image itself, try focussing
  562. if (has_auto_focus_start) {
  563. mp_io_pipeline_focus();
  564. }
  565. }
  566. static void
  567. run_camera_switch_action(GSimpleAction *action, GVariant *param, gpointer user_data)
  568. {
  569. size_t next_index = camera->index + 1;
  570. const struct mp_camera_config *next_camera =
  571. mp_get_camera_config(next_index);
  572. if (!next_camera) {
  573. next_index = 0;
  574. next_camera = mp_get_camera_config(next_index);
  575. }
  576. camera = next_camera;
  577. update_io_pipeline();
  578. }
  579. static void
  580. run_open_settings_action(GSimpleAction *action, GVariant *param, gpointer user_data)
  581. {
  582. gtk_stack_set_visible_child_name(GTK_STACK(main_stack), "settings");
  583. }
  584. static void
  585. run_close_settings_action(GSimpleAction *action, GVariant *param, gpointer user_data)
  586. {
  587. gtk_stack_set_visible_child_name(GTK_STACK(main_stack), "main");
  588. // Update settings
  589. bool save_dng = g_settings_get_boolean(settings, "save-raw");
  590. if (save_dng != setting_save_dng) {
  591. setting_save_dng = save_dng;
  592. update_io_pipeline();
  593. }
  594. }
  595. static void
  596. on_controls_scale_changed(GtkAdjustment *adjustment, void (*set_fn)(double))
  597. {
  598. set_fn(gtk_adjustment_get_value(adjustment));
  599. }
  600. static void
  601. update_value(GtkAdjustment *adjustment, GtkLabel *label)
  602. {
  603. char buf[12];
  604. snprintf(buf, 12, "%.0f", gtk_adjustment_get_value(adjustment));
  605. gtk_label_set_label(label, buf);
  606. }
  607. static void
  608. on_auto_controls_toggled(GtkToggleButton *button, void (*set_auto_fn)(bool))
  609. {
  610. set_auto_fn(gtk_toggle_button_get_active(button));
  611. }
  612. static void
  613. update_scale(GtkToggleButton *button, GtkScale *scale)
  614. {
  615. gtk_widget_set_sensitive(GTK_WIDGET(scale),
  616. !gtk_toggle_button_get_active(button));
  617. }
  618. static void
  619. open_controls(GtkWidget *parent,
  620. const char *title_name,
  621. double min_value,
  622. double max_value,
  623. double current,
  624. bool auto_enabled,
  625. void (*set_fn)(double),
  626. void (*set_auto_fn)(bool))
  627. {
  628. GtkBuilder *builder = gtk_builder_new_from_resource(
  629. "/org/postmarketos/Megapixels/controls-popover.ui");
  630. GtkPopover *popover =
  631. GTK_POPOVER(gtk_builder_get_object(builder, "controls"));
  632. GtkScale *scale = GTK_SCALE(gtk_builder_get_object(builder, "scale"));
  633. GtkLabel *title = GTK_LABEL(gtk_builder_get_object(builder, "title"));
  634. GtkLabel *value_label =
  635. GTK_LABEL(gtk_builder_get_object(builder, "value-label"));
  636. GtkToggleButton *auto_button =
  637. GTK_TOGGLE_BUTTON(gtk_builder_get_object(builder, "auto-button"));
  638. gtk_label_set_label(title, title_name);
  639. GtkAdjustment *adjustment = gtk_range_get_adjustment(GTK_RANGE(scale));
  640. gtk_adjustment_set_lower(adjustment, min_value);
  641. gtk_adjustment_set_upper(adjustment, max_value);
  642. gtk_adjustment_set_value(adjustment, current);
  643. update_value(adjustment, value_label);
  644. gtk_toggle_button_set_active(auto_button, auto_enabled);
  645. update_scale(auto_button, scale);
  646. g_signal_connect(adjustment,
  647. "value-changed",
  648. G_CALLBACK(on_controls_scale_changed),
  649. set_fn);
  650. g_signal_connect(
  651. adjustment, "value-changed", G_CALLBACK(update_value), value_label);
  652. g_signal_connect(auto_button,
  653. "toggled",
  654. G_CALLBACK(on_auto_controls_toggled),
  655. set_auto_fn);
  656. g_signal_connect(auto_button, "toggled", G_CALLBACK(update_scale), scale);
  657. gtk_widget_set_parent(GTK_WIDGET(popover), parent);
  658. gtk_popover_popup(popover);
  659. // g_object_unref(popover);
  660. }
  661. static void
  662. set_gain(double value)
  663. {
  664. if (gain != (int)value) {
  665. gain = value;
  666. update_io_pipeline();
  667. }
  668. }
  669. static void
  670. set_gain_auto(bool is_auto)
  671. {
  672. if (gain_is_manual != !is_auto) {
  673. gain_is_manual = !is_auto;
  674. update_io_pipeline();
  675. }
  676. }
  677. static void
  678. open_iso_controls(GtkWidget *button, gpointer user_data)
  679. {
  680. open_controls(button,
  681. "ISO",
  682. 0,
  683. gain_max,
  684. gain,
  685. !gain_is_manual,
  686. set_gain,
  687. set_gain_auto);
  688. }
  689. static void
  690. set_shutter(double value)
  691. {
  692. int new_exposure = (int)(value / 360.0 * camera->capture_mode.height);
  693. if (new_exposure != exposure) {
  694. exposure = new_exposure;
  695. update_io_pipeline();
  696. }
  697. }
  698. static void
  699. set_shutter_auto(bool is_auto)
  700. {
  701. if (exposure_is_manual != !is_auto) {
  702. exposure_is_manual = !is_auto;
  703. update_io_pipeline();
  704. }
  705. }
  706. static void
  707. open_shutter_controls(GtkWidget *button, gpointer user_data)
  708. {
  709. open_controls(button,
  710. "Shutter",
  711. 1.0,
  712. 360.0,
  713. exposure,
  714. !exposure_is_manual,
  715. set_shutter,
  716. set_shutter_auto);
  717. }
  718. static void
  719. flash_button_clicked(GtkWidget *button, gpointer user_data)
  720. {
  721. flash_enabled = !flash_enabled;
  722. update_io_pipeline();
  723. const char *icon_name =
  724. flash_enabled ? "flash-enabled-symbolic" : "flash-disabled-symbolic";
  725. gtk_button_set_icon_name(GTK_BUTTON(button), icon_name);
  726. }
  727. static void
  728. on_realize(GtkWidget *window, gpointer *data)
  729. {
  730. GtkNative *native = gtk_widget_get_native(window);
  731. mp_process_pipeline_init_gl(gtk_native_get_surface(native));
  732. camera = mp_get_camera_config(0);
  733. update_io_pipeline();
  734. }
  735. static GSimpleAction *
  736. create_simple_action(GtkApplication *app, const char *name, GCallback callback)
  737. {
  738. GSimpleAction *action = g_simple_action_new(name, NULL);
  739. g_signal_connect(action, "activate", callback, app);
  740. g_action_map_add_action(G_ACTION_MAP(app), G_ACTION(action));
  741. return action;
  742. }
  743. static void
  744. update_ui_rotation()
  745. {
  746. if (device_rotation == 0 || device_rotation == 180) {
  747. // Portrait
  748. gtk_widget_set_halign(preview_top_box, GTK_ALIGN_FILL);
  749. gtk_orientable_set_orientation(GTK_ORIENTABLE(preview_top_box),
  750. GTK_ORIENTATION_VERTICAL);
  751. gtk_widget_set_halign(preview_bottom_box, GTK_ALIGN_FILL);
  752. gtk_orientable_set_orientation(GTK_ORIENTABLE(preview_bottom_box),
  753. GTK_ORIENTATION_HORIZONTAL);
  754. if (device_rotation == 0) {
  755. gtk_widget_set_valign(preview_top_box, GTK_ALIGN_START);
  756. gtk_widget_set_valign(preview_bottom_box, GTK_ALIGN_END);
  757. } else {
  758. gtk_widget_set_valign(preview_top_box, GTK_ALIGN_END);
  759. gtk_widget_set_valign(preview_bottom_box, GTK_ALIGN_START);
  760. }
  761. } else {
  762. // Landscape
  763. gtk_widget_set_valign(preview_top_box, GTK_ALIGN_FILL);
  764. gtk_orientable_set_orientation(GTK_ORIENTABLE(preview_top_box),
  765. GTK_ORIENTATION_HORIZONTAL);
  766. gtk_widget_set_valign(preview_bottom_box, GTK_ALIGN_FILL);
  767. gtk_orientable_set_orientation(GTK_ORIENTABLE(preview_bottom_box),
  768. GTK_ORIENTATION_VERTICAL);
  769. if (device_rotation == 90) {
  770. gtk_widget_set_halign(preview_top_box, GTK_ALIGN_END);
  771. gtk_widget_set_halign(preview_bottom_box, GTK_ALIGN_START);
  772. } else {
  773. gtk_widget_set_halign(preview_top_box, GTK_ALIGN_START);
  774. gtk_widget_set_halign(preview_bottom_box, GTK_ALIGN_END);
  775. }
  776. }
  777. }
  778. static void
  779. display_config_received(GDBusConnection *conn, GAsyncResult *res, gpointer user_data)
  780. {
  781. g_autoptr(GError) error = NULL;
  782. g_autoptr(GVariant) result =
  783. g_dbus_connection_call_finish(conn, res, &error);
  784. if (!result) {
  785. printf("Failed to get display configuration: %s\n", error->message);
  786. return;
  787. }
  788. g_autoptr(GVariant) configs = g_variant_get_child_value(result, 1);
  789. if (g_variant_n_children(configs) == 0) {
  790. return;
  791. }
  792. g_autoptr(GVariant) config = g_variant_get_child_value(configs, 0);
  793. g_autoptr(GVariant) rot_config = g_variant_get_child_value(config, 7);
  794. uint32_t rotation_index = g_variant_get_uint32(rot_config);
  795. assert(rotation_index < 4);
  796. int new_rotation = rotation_index * 90;
  797. if (new_rotation != device_rotation) {
  798. device_rotation = new_rotation;
  799. update_io_pipeline();
  800. update_ui_rotation();
  801. }
  802. }
  803. static void
  804. update_screen_rotation(GDBusConnection *conn)
  805. {
  806. g_dbus_connection_call(conn,
  807. "org.gnome.Mutter.DisplayConfig",
  808. "/org/gnome/Mutter/DisplayConfig",
  809. "org.gnome.Mutter.DisplayConfig",
  810. "GetResources",
  811. NULL,
  812. NULL,
  813. G_DBUS_CALL_FLAGS_NO_AUTO_START,
  814. -1,
  815. NULL,
  816. (GAsyncReadyCallback)display_config_received,
  817. NULL);
  818. }
  819. static void
  820. on_screen_rotate(GDBusConnection *conn,
  821. const gchar *sender_name,
  822. const gchar *object_path,
  823. const gchar *interface_name,
  824. const gchar *signal_name,
  825. GVariant *parameters,
  826. gpointer user_data)
  827. {
  828. update_screen_rotation(conn);
  829. }
  830. static void
  831. activate(GtkApplication *app, gpointer data)
  832. {
  833. g_object_set(gtk_settings_get_default(),
  834. "gtk-application-prefer-dark-theme",
  835. TRUE,
  836. NULL);
  837. GdkDisplay *display = gdk_display_get_default();
  838. GtkIconTheme *icon_theme = gtk_icon_theme_get_for_display(display);
  839. gtk_icon_theme_add_resource_path(icon_theme, "/org/postmarketos/Megapixels");
  840. GtkCssProvider *provider = gtk_css_provider_new();
  841. gtk_css_provider_load_from_resource(
  842. provider, "/org/postmarketos/Megapixels/camera.css");
  843. gtk_style_context_add_provider_for_display(
  844. display,
  845. GTK_STYLE_PROVIDER(provider),
  846. GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
  847. GtkBuilder *builder = gtk_builder_new_from_resource(
  848. "/org/postmarketos/Megapixels/camera.ui");
  849. GtkWidget *window = GTK_WIDGET(gtk_builder_get_object(builder, "window"));
  850. GtkWidget *iso_button =
  851. GTK_WIDGET(gtk_builder_get_object(builder, "iso-controls-button"));
  852. GtkWidget *shutter_button = GTK_WIDGET(
  853. gtk_builder_get_object(builder, "shutter-controls-button"));
  854. flash_button =
  855. GTK_WIDGET(gtk_builder_get_object(builder, "flash-controls-button"));
  856. GtkWidget *setting_dng_button =
  857. GTK_WIDGET(gtk_builder_get_object(builder, "setting-raw"));
  858. preview = GTK_WIDGET(gtk_builder_get_object(builder, "preview"));
  859. main_stack = GTK_WIDGET(gtk_builder_get_object(builder, "main_stack"));
  860. open_last_stack =
  861. GTK_WIDGET(gtk_builder_get_object(builder, "open_last_stack"));
  862. thumb_last = GTK_WIDGET(gtk_builder_get_object(builder, "thumb_last"));
  863. process_spinner =
  864. GTK_WIDGET(gtk_builder_get_object(builder, "process_spinner"));
  865. scanned_codes = GTK_WIDGET(gtk_builder_get_object(builder, "scanned-codes"));
  866. preview_top_box = GTK_WIDGET(gtk_builder_get_object(builder, "top-box"));
  867. preview_bottom_box =
  868. GTK_WIDGET(gtk_builder_get_object(builder, "bottom-box"));
  869. g_signal_connect(window, "realize", G_CALLBACK(on_realize), NULL);
  870. g_signal_connect(preview, "realize", G_CALLBACK(preview_realize), NULL);
  871. g_signal_connect(preview, "render", G_CALLBACK(preview_draw), NULL);
  872. g_signal_connect(preview, "resize", G_CALLBACK(preview_resize), NULL);
  873. GtkGesture *click = gtk_gesture_click_new();
  874. g_signal_connect(click, "pressed", G_CALLBACK(preview_pressed), NULL);
  875. gtk_widget_add_controller(preview, GTK_EVENT_CONTROLLER(click));
  876. g_signal_connect(iso_button, "clicked", G_CALLBACK(open_iso_controls), NULL);
  877. g_signal_connect(
  878. shutter_button, "clicked", G_CALLBACK(open_shutter_controls), NULL);
  879. g_signal_connect(
  880. flash_button, "clicked", G_CALLBACK(flash_button_clicked), NULL);
  881. // Setup actions
  882. create_simple_action(app, "capture", G_CALLBACK(run_capture_action));
  883. create_simple_action(
  884. app, "switch-camera", G_CALLBACK(run_camera_switch_action));
  885. create_simple_action(
  886. app, "open-settings", G_CALLBACK(run_open_settings_action));
  887. create_simple_action(
  888. app, "close-settings", G_CALLBACK(run_close_settings_action));
  889. create_simple_action(app, "open-last", G_CALLBACK(run_open_last_action));
  890. create_simple_action(app, "open-photos", G_CALLBACK(run_open_photos_action));
  891. create_simple_action(app, "about", G_CALLBACK(run_about_action));
  892. create_simple_action(app, "quit", G_CALLBACK(run_quit_action));
  893. // Setup shortcuts
  894. const char *capture_accels[] = { "space", NULL };
  895. gtk_application_set_accels_for_action(app, "app.capture", capture_accels);
  896. const char *quit_accels[] = { "<Ctrl>q", "<Ctrl>w", NULL };
  897. gtk_application_set_accels_for_action(app, "app.quit", quit_accels);
  898. // Setup settings
  899. settings = g_settings_new("org.postmarketos.Megapixels");
  900. g_settings_bind(settings,
  901. "save-raw",
  902. setting_dng_button,
  903. "active",
  904. G_SETTINGS_BIND_DEFAULT);
  905. setting_save_dng = g_settings_get_boolean(settings, "save-raw");
  906. // Listen for phosh rotation
  907. GDBusConnection *conn =
  908. g_application_get_dbus_connection(G_APPLICATION(app));
  909. g_dbus_connection_signal_subscribe(conn,
  910. NULL,
  911. "org.gnome.Mutter.DisplayConfig",
  912. "MonitorsChanged",
  913. "/org/gnome/Mutter/DisplayConfig",
  914. NULL,
  915. G_DBUS_SIGNAL_FLAGS_NONE,
  916. &on_screen_rotate,
  917. NULL,
  918. NULL);
  919. update_screen_rotation(conn);
  920. // Initialize display flash
  921. mp_flash_gtk_init(conn);
  922. mp_io_pipeline_start();
  923. gtk_application_add_window(app, GTK_WINDOW(window));
  924. gtk_widget_show(window);
  925. }
  926. static void
  927. shutdown(GApplication *app, gpointer data)
  928. {
  929. // Only do cleanup in development, let the OS clean up otherwise
  930. #ifdef DEBUG
  931. mp_io_pipeline_stop();
  932. mp_flash_gtk_clean();
  933. #endif
  934. }
  935. int
  936. main(int argc, char *argv[])
  937. {
  938. #ifdef RENDERDOC
  939. {
  940. void *mod = dlopen("librenderdoc.so", RTLD_NOW | RTLD_NOLOAD);
  941. if (mod) {
  942. pRENDERDOC_GetAPI RENDERDOC_GetAPI =
  943. (pRENDERDOC_GetAPI)dlsym(mod, "RENDERDOC_GetAPI");
  944. int ret = RENDERDOC_GetAPI(eRENDERDOC_API_Version_1_1_2,
  945. (void **)&rdoc_api);
  946. assert(ret == 1);
  947. } else {
  948. printf("Renderdoc not found\n");
  949. }
  950. }
  951. #endif
  952. if (!mp_load_config())
  953. return 1;
  954. setenv("LC_NUMERIC", "C", 1);
  955. GtkApplication *app = gtk_application_new("org.postmarketos.Megapixels", 0);
  956. g_signal_connect(app, "activate", G_CALLBACK(activate), NULL);
  957. g_signal_connect(app, "shutdown", G_CALLBACK(shutdown), NULL);
  958. g_application_run(G_APPLICATION(app), argc, argv);
  959. return 0;
  960. }