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 MPCameraMode 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 = true;
  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 = 3;
  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. gtk_show_about_dialog(NULL,
  435. "program-name",
  436. "Megapixels",
  437. "title",
  438. "Megapixels",
  439. "logo-icon-name",
  440. "org.postmarketos.Megapixels",
  441. "comments",
  442. "The postmarketOS camera application",
  443. "website",
  444. "https://sr.ht/~martijnbraam/megapixels",
  445. "version",
  446. VERSION,
  447. "license-type",
  448. GTK_LICENSE_GPL_3_0_ONLY,
  449. NULL);
  450. }
  451. void
  452. run_quit_action(GSimpleAction *action, GVariant *param, GApplication *app)
  453. {
  454. g_application_quit(app);
  455. }
  456. static bool
  457. check_point_inside_bounds(int x, int y, int *bounds_x, int *bounds_y)
  458. {
  459. bool right = false, left = false, top = false, bottom = false;
  460. for (int i = 0; i < 4; ++i) {
  461. if (x <= bounds_x[i])
  462. left = true;
  463. if (x >= bounds_x[i])
  464. right = true;
  465. if (y <= bounds_y[i])
  466. top = true;
  467. if (y >= bounds_y[i])
  468. bottom = true;
  469. }
  470. return right && left && top && bottom;
  471. }
  472. static void
  473. on_zbar_dialog_response(GtkDialog *dialog, int response, char *data)
  474. {
  475. g_autoptr(GError) error = NULL;
  476. switch (response) {
  477. case GTK_RESPONSE_YES:
  478. if (!g_app_info_launch_default_for_uri(data, NULL, &error)) {
  479. g_printerr("Could not launch application: %s\n",
  480. error->message);
  481. }
  482. case GTK_RESPONSE_ACCEPT: {
  483. GdkDisplay *display = gtk_widget_get_display(GTK_WIDGET(dialog));
  484. gdk_clipboard_set_text(gdk_display_get_clipboard(display), data);
  485. }
  486. case GTK_RESPONSE_CANCEL:
  487. break;
  488. default:
  489. g_printerr("Wrong dialog response: %d\n", response);
  490. }
  491. g_free(data);
  492. gtk_window_destroy(GTK_WINDOW(dialog));
  493. }
  494. static void
  495. on_zbar_code_tapped(GtkWidget *widget, const MPZBarCode *code)
  496. {
  497. GtkWidget *dialog;
  498. GtkDialogFlags flags = GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT;
  499. bool data_is_url =
  500. g_uri_is_valid(code->data, G_URI_FLAGS_PARSE_RELAXED, NULL);
  501. char *data = strdup(code->data);
  502. if (data_is_url) {
  503. dialog = gtk_message_dialog_new(
  504. GTK_WINDOW(gtk_widget_get_root(widget)),
  505. flags,
  506. GTK_MESSAGE_QUESTION,
  507. GTK_BUTTONS_NONE,
  508. "Found a URL '%s' encoded in a %s.",
  509. code->data,
  510. code->type);
  511. gtk_dialog_add_buttons(
  512. GTK_DIALOG(dialog), "_Open URL", GTK_RESPONSE_YES, NULL);
  513. } else {
  514. dialog = gtk_message_dialog_new(
  515. GTK_WINDOW(gtk_widget_get_root(widget)),
  516. flags,
  517. GTK_MESSAGE_QUESTION,
  518. GTK_BUTTONS_NONE,
  519. "Found data encoded in a %s.",
  520. code->type);
  521. gtk_message_dialog_format_secondary_markup(
  522. GTK_MESSAGE_DIALOG(dialog), "<small>%s</small>", code->data);
  523. }
  524. gtk_dialog_add_buttons(GTK_DIALOG(dialog),
  525. "_Copy",
  526. GTK_RESPONSE_ACCEPT,
  527. "_Cancel",
  528. GTK_RESPONSE_CANCEL,
  529. NULL);
  530. g_signal_connect(
  531. dialog, "response", G_CALLBACK(on_zbar_dialog_response), data);
  532. gtk_widget_show(GTK_WIDGET(dialog));
  533. }
  534. static void
  535. preview_pressed(GtkGestureClick *gesture, int n_press, double x, double y)
  536. {
  537. GtkWidget *widget =
  538. gtk_event_controller_get_widget(GTK_EVENT_CONTROLLER(gesture));
  539. int scale_factor = gtk_widget_get_scale_factor(widget);
  540. // Tapped zbar result
  541. if (zbar_result) {
  542. // Transform the event coordinates to the image
  543. float offset_x, offset_y, size_x, size_y;
  544. position_preview(&offset_x, &offset_y, &size_x, &size_y);
  545. int zbar_x = (x - offset_x) * scale_factor / size_x *
  546. preview_buffer_width;
  547. int zbar_y = (y - offset_y) * scale_factor / size_y *
  548. preview_buffer_height;
  549. for (uint8_t i = 0; i < zbar_result->size; ++i) {
  550. MPZBarCode *code = &zbar_result->codes[i];
  551. if (check_point_inside_bounds(zbar_x,
  552. zbar_y,
  553. code->bounds_x,
  554. code->bounds_y)) {
  555. on_zbar_code_tapped(widget, code);
  556. return;
  557. }
  558. }
  559. }
  560. // Tapped preview image itself, try focussing
  561. if (has_auto_focus_start) {
  562. mp_io_pipeline_focus();
  563. }
  564. }
  565. static void
  566. run_camera_switch_action(GSimpleAction *action, GVariant *param, gpointer user_data)
  567. {
  568. size_t next_index = camera->index + 1;
  569. const struct mp_camera_config *next_camera =
  570. mp_get_camera_config(next_index);
  571. if (!next_camera) {
  572. next_index = 0;
  573. next_camera = mp_get_camera_config(next_index);
  574. }
  575. camera = next_camera;
  576. update_io_pipeline();
  577. }
  578. static void
  579. run_open_settings_action(GSimpleAction *action, GVariant *param, gpointer user_data)
  580. {
  581. gtk_stack_set_visible_child_name(GTK_STACK(main_stack), "settings");
  582. }
  583. static void
  584. run_close_settings_action(GSimpleAction *action, GVariant *param, gpointer user_data)
  585. {
  586. gtk_stack_set_visible_child_name(GTK_STACK(main_stack), "main");
  587. // Update settings
  588. bool save_dng = g_settings_get_boolean(settings, "save-raw");
  589. if (save_dng != setting_save_dng) {
  590. setting_save_dng = save_dng;
  591. update_io_pipeline();
  592. }
  593. }
  594. static void
  595. on_controls_scale_changed(GtkAdjustment *adjustment, void (*set_fn)(double))
  596. {
  597. set_fn(gtk_adjustment_get_value(adjustment));
  598. }
  599. static void
  600. update_value(GtkAdjustment *adjustment, GtkLabel *label)
  601. {
  602. char buf[12];
  603. snprintf(buf, 12, "%.0f", gtk_adjustment_get_value(adjustment));
  604. gtk_label_set_label(label, buf);
  605. }
  606. static void
  607. on_auto_controls_toggled(GtkToggleButton *button, void (*set_auto_fn)(bool))
  608. {
  609. set_auto_fn(gtk_toggle_button_get_active(button));
  610. }
  611. static void
  612. update_scale(GtkToggleButton *button, GtkScale *scale)
  613. {
  614. gtk_widget_set_sensitive(GTK_WIDGET(scale),
  615. !gtk_toggle_button_get_active(button));
  616. }
  617. static void
  618. open_controls(GtkWidget *parent,
  619. const char *title_name,
  620. double min_value,
  621. double max_value,
  622. double current,
  623. bool auto_enabled,
  624. void (*set_fn)(double),
  625. void (*set_auto_fn)(bool))
  626. {
  627. GtkBuilder *builder = gtk_builder_new_from_resource(
  628. "/org/postmarketos/Megapixels/controls-popover.ui");
  629. GtkPopover *popover =
  630. GTK_POPOVER(gtk_builder_get_object(builder, "controls"));
  631. GtkScale *scale = GTK_SCALE(gtk_builder_get_object(builder, "scale"));
  632. GtkLabel *title = GTK_LABEL(gtk_builder_get_object(builder, "title"));
  633. GtkLabel *value_label =
  634. GTK_LABEL(gtk_builder_get_object(builder, "value-label"));
  635. GtkToggleButton *auto_button =
  636. GTK_TOGGLE_BUTTON(gtk_builder_get_object(builder, "auto-button"));
  637. gtk_label_set_label(title, title_name);
  638. GtkAdjustment *adjustment = gtk_range_get_adjustment(GTK_RANGE(scale));
  639. gtk_adjustment_set_lower(adjustment, min_value);
  640. gtk_adjustment_set_upper(adjustment, max_value);
  641. gtk_adjustment_set_value(adjustment, current);
  642. update_value(adjustment, value_label);
  643. gtk_toggle_button_set_active(auto_button, auto_enabled);
  644. update_scale(auto_button, scale);
  645. g_signal_connect(adjustment,
  646. "value-changed",
  647. G_CALLBACK(on_controls_scale_changed),
  648. set_fn);
  649. g_signal_connect(
  650. adjustment, "value-changed", G_CALLBACK(update_value), value_label);
  651. g_signal_connect(auto_button,
  652. "toggled",
  653. G_CALLBACK(on_auto_controls_toggled),
  654. set_auto_fn);
  655. g_signal_connect(auto_button, "toggled", G_CALLBACK(update_scale), scale);
  656. gtk_widget_set_parent(GTK_WIDGET(popover), parent);
  657. gtk_popover_popup(popover);
  658. // g_object_unref(popover);
  659. }
  660. static void
  661. set_gain(double value)
  662. {
  663. if (gain != (int)value) {
  664. gain = value;
  665. update_io_pipeline();
  666. }
  667. }
  668. static void
  669. set_gain_auto(bool is_auto)
  670. {
  671. if (gain_is_manual != !is_auto) {
  672. gain_is_manual = !is_auto;
  673. update_io_pipeline();
  674. }
  675. }
  676. static void
  677. open_iso_controls(GtkWidget *button, gpointer user_data)
  678. {
  679. open_controls(button,
  680. "ISO",
  681. 0,
  682. gain_max,
  683. gain,
  684. !gain_is_manual,
  685. set_gain,
  686. set_gain_auto);
  687. }
  688. static void
  689. set_shutter(double value)
  690. {
  691. int new_exposure = (int)(value / 360.0 * camera->capture_mode.height);
  692. if (new_exposure != exposure) {
  693. exposure = new_exposure;
  694. update_io_pipeline();
  695. }
  696. }
  697. static void
  698. set_shutter_auto(bool is_auto)
  699. {
  700. if (exposure_is_manual != !is_auto) {
  701. exposure_is_manual = !is_auto;
  702. update_io_pipeline();
  703. }
  704. }
  705. static void
  706. open_shutter_controls(GtkWidget *button, gpointer user_data)
  707. {
  708. open_controls(button,
  709. "Shutter",
  710. 1.0,
  711. 360.0,
  712. exposure,
  713. !exposure_is_manual,
  714. set_shutter,
  715. set_shutter_auto);
  716. }
  717. static void
  718. flash_button_clicked(GtkWidget *button, gpointer user_data)
  719. {
  720. flash_enabled = !flash_enabled;
  721. update_io_pipeline();
  722. const char *icon_name =
  723. flash_enabled ? "flash-enabled-symbolic" : "flash-disabled-symbolic";
  724. gtk_button_set_icon_name(GTK_BUTTON(button), icon_name);
  725. }
  726. static void
  727. on_realize(GtkWidget *window, gpointer *data)
  728. {
  729. GtkNative *native = gtk_widget_get_native(window);
  730. mp_process_pipeline_init_gl(gtk_native_get_surface(native));
  731. camera = mp_get_camera_config(0);
  732. update_io_pipeline();
  733. }
  734. static GSimpleAction *
  735. create_simple_action(GtkApplication *app, const char *name, GCallback callback)
  736. {
  737. GSimpleAction *action = g_simple_action_new(name, NULL);
  738. g_signal_connect(action, "activate", callback, app);
  739. g_action_map_add_action(G_ACTION_MAP(app), G_ACTION(action));
  740. return action;
  741. }
  742. static void
  743. update_ui_rotation()
  744. {
  745. if (device_rotation == 0 || device_rotation == 180) {
  746. // Portrait
  747. gtk_widget_set_halign(preview_top_box, GTK_ALIGN_FILL);
  748. gtk_orientable_set_orientation(GTK_ORIENTABLE(preview_top_box),
  749. GTK_ORIENTATION_VERTICAL);
  750. gtk_widget_set_halign(preview_bottom_box, GTK_ALIGN_FILL);
  751. gtk_orientable_set_orientation(GTK_ORIENTABLE(preview_bottom_box),
  752. GTK_ORIENTATION_HORIZONTAL);
  753. if (device_rotation == 0) {
  754. gtk_widget_set_valign(preview_top_box, GTK_ALIGN_START);
  755. gtk_widget_set_valign(preview_bottom_box, GTK_ALIGN_END);
  756. } else {
  757. gtk_widget_set_valign(preview_top_box, GTK_ALIGN_END);
  758. gtk_widget_set_valign(preview_bottom_box, GTK_ALIGN_START);
  759. }
  760. } else {
  761. // Landscape
  762. gtk_widget_set_valign(preview_top_box, GTK_ALIGN_FILL);
  763. gtk_orientable_set_orientation(GTK_ORIENTABLE(preview_top_box),
  764. GTK_ORIENTATION_HORIZONTAL);
  765. gtk_widget_set_valign(preview_bottom_box, GTK_ALIGN_FILL);
  766. gtk_orientable_set_orientation(GTK_ORIENTABLE(preview_bottom_box),
  767. GTK_ORIENTATION_VERTICAL);
  768. if (device_rotation == 90) {
  769. gtk_widget_set_halign(preview_top_box, GTK_ALIGN_END);
  770. gtk_widget_set_halign(preview_bottom_box, GTK_ALIGN_START);
  771. } else {
  772. gtk_widget_set_halign(preview_top_box, GTK_ALIGN_START);
  773. gtk_widget_set_halign(preview_bottom_box, GTK_ALIGN_END);
  774. }
  775. }
  776. }
  777. static void
  778. display_config_received(GDBusConnection *conn, GAsyncResult *res, gpointer user_data)
  779. {
  780. g_autoptr(GError) error = NULL;
  781. GVariant *result = g_dbus_connection_call_finish(conn, res, &error);
  782. if (!result) {
  783. printf("Failed to get display configuration: %s\n", error->message);
  784. return;
  785. }
  786. GVariant *configs = g_variant_get_child_value(result, 1);
  787. if (g_variant_n_children(configs) == 0) {
  788. return;
  789. }
  790. GVariant *config = g_variant_get_child_value(configs, 0);
  791. GVariant *rot_config = g_variant_get_child_value(config, 7);
  792. uint32_t rotation_index = g_variant_get_uint32(rot_config);
  793. assert(rotation_index < 4);
  794. int new_rotation = rotation_index * 90;
  795. if (new_rotation != device_rotation) {
  796. device_rotation = new_rotation;
  797. update_io_pipeline();
  798. update_ui_rotation();
  799. }
  800. g_variant_unref(result);
  801. }
  802. static void
  803. update_screen_rotation(GDBusConnection *conn)
  804. {
  805. g_dbus_connection_call(conn,
  806. "org.gnome.Mutter.DisplayConfig",
  807. "/org/gnome/Mutter/DisplayConfig",
  808. "org.gnome.Mutter.DisplayConfig",
  809. "GetResources",
  810. NULL,
  811. NULL,
  812. G_DBUS_CALL_FLAGS_NO_AUTO_START,
  813. -1,
  814. NULL,
  815. (GAsyncReadyCallback)display_config_received,
  816. NULL);
  817. }
  818. static void
  819. on_screen_rotate(GDBusConnection *conn,
  820. const gchar *sender_name,
  821. const gchar *object_path,
  822. const gchar *interface_name,
  823. const gchar *signal_name,
  824. GVariant *parameters,
  825. gpointer user_data)
  826. {
  827. update_screen_rotation(conn);
  828. }
  829. static void
  830. activate(GtkApplication *app, gpointer data)
  831. {
  832. g_object_set(gtk_settings_get_default(),
  833. "gtk-application-prefer-dark-theme",
  834. TRUE,
  835. NULL);
  836. GdkDisplay *display = gdk_display_get_default();
  837. GtkIconTheme *icon_theme = gtk_icon_theme_get_for_display(display);
  838. gtk_icon_theme_add_resource_path(icon_theme, "/org/postmarketos/Megapixels");
  839. GtkCssProvider *provider = gtk_css_provider_new();
  840. gtk_css_provider_load_from_resource(
  841. provider, "/org/postmarketos/Megapixels/camera.css");
  842. gtk_style_context_add_provider_for_display(
  843. display,
  844. GTK_STYLE_PROVIDER(provider),
  845. GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
  846. GtkBuilder *builder = gtk_builder_new_from_resource(
  847. "/org/postmarketos/Megapixels/camera.ui");
  848. GtkWidget *window = GTK_WIDGET(gtk_builder_get_object(builder, "window"));
  849. GtkWidget *iso_button =
  850. GTK_WIDGET(gtk_builder_get_object(builder, "iso-controls-button"));
  851. GtkWidget *shutter_button = GTK_WIDGET(
  852. gtk_builder_get_object(builder, "shutter-controls-button"));
  853. flash_button =
  854. GTK_WIDGET(gtk_builder_get_object(builder, "flash-controls-button"));
  855. GtkWidget *setting_dng_button =
  856. GTK_WIDGET(gtk_builder_get_object(builder, "setting-raw"));
  857. preview = GTK_WIDGET(gtk_builder_get_object(builder, "preview"));
  858. main_stack = GTK_WIDGET(gtk_builder_get_object(builder, "main_stack"));
  859. open_last_stack =
  860. GTK_WIDGET(gtk_builder_get_object(builder, "open_last_stack"));
  861. thumb_last = GTK_WIDGET(gtk_builder_get_object(builder, "thumb_last"));
  862. process_spinner =
  863. GTK_WIDGET(gtk_builder_get_object(builder, "process_spinner"));
  864. scanned_codes = GTK_WIDGET(gtk_builder_get_object(builder, "scanned-codes"));
  865. preview_top_box = GTK_WIDGET(gtk_builder_get_object(builder, "top-box"));
  866. preview_bottom_box =
  867. GTK_WIDGET(gtk_builder_get_object(builder, "bottom-box"));
  868. g_signal_connect(window, "realize", G_CALLBACK(on_realize), NULL);
  869. g_signal_connect(preview, "realize", G_CALLBACK(preview_realize), NULL);
  870. g_signal_connect(preview, "render", G_CALLBACK(preview_draw), NULL);
  871. g_signal_connect(preview, "resize", G_CALLBACK(preview_resize), NULL);
  872. GtkGesture *click = gtk_gesture_click_new();
  873. g_signal_connect(click, "pressed", G_CALLBACK(preview_pressed), NULL);
  874. gtk_widget_add_controller(preview, GTK_EVENT_CONTROLLER(click));
  875. g_signal_connect(iso_button, "clicked", G_CALLBACK(open_iso_controls), NULL);
  876. g_signal_connect(
  877. shutter_button, "clicked", G_CALLBACK(open_shutter_controls), NULL);
  878. g_signal_connect(
  879. flash_button, "clicked", G_CALLBACK(flash_button_clicked), NULL);
  880. // Setup actions
  881. create_simple_action(app, "capture", G_CALLBACK(run_capture_action));
  882. create_simple_action(
  883. app, "switch-camera", G_CALLBACK(run_camera_switch_action));
  884. create_simple_action(
  885. app, "open-settings", G_CALLBACK(run_open_settings_action));
  886. create_simple_action(
  887. app, "close-settings", G_CALLBACK(run_close_settings_action));
  888. create_simple_action(app, "open-last", G_CALLBACK(run_open_last_action));
  889. create_simple_action(app, "open-photos", G_CALLBACK(run_open_photos_action));
  890. create_simple_action(app, "about", G_CALLBACK(run_about_action));
  891. create_simple_action(app, "quit", G_CALLBACK(run_quit_action));
  892. // Setup shortcuts
  893. const char *capture_accels[] = { "space", NULL };
  894. gtk_application_set_accels_for_action(app, "app.capture", capture_accels);
  895. const char *quit_accels[] = { "<Ctrl>q", "<Ctrl>w", NULL };
  896. gtk_application_set_accels_for_action(app, "app.quit", quit_accels);
  897. // Setup settings
  898. settings = g_settings_new("org.postmarketos.Megapixels");
  899. g_settings_bind(settings,
  900. "save-raw",
  901. setting_dng_button,
  902. "active",
  903. G_SETTINGS_BIND_DEFAULT);
  904. setting_save_dng = g_settings_get_boolean(settings, "save-raw");
  905. // Listen for phosh rotation
  906. GDBusConnection *conn =
  907. g_application_get_dbus_connection(G_APPLICATION(app));
  908. g_dbus_connection_signal_subscribe(conn,
  909. NULL,
  910. "org.gnome.Mutter.DisplayConfig",
  911. "MonitorsChanged",
  912. "/org/gnome/Mutter/DisplayConfig",
  913. NULL,
  914. G_DBUS_SIGNAL_FLAGS_NONE,
  915. &on_screen_rotate,
  916. NULL,
  917. NULL);
  918. update_screen_rotation(conn);
  919. // Initialize display flash
  920. mp_flash_gtk_init(conn);
  921. mp_io_pipeline_start();
  922. gtk_application_add_window(app, GTK_WINDOW(window));
  923. gtk_widget_show(window);
  924. }
  925. static void
  926. shutdown(GApplication *app, gpointer data)
  927. {
  928. // Only do cleanup in development, let the OS clean up otherwise
  929. #ifdef DEBUG
  930. mp_io_pipeline_stop();
  931. mp_flash_gtk_clean();
  932. #endif
  933. }
  934. int
  935. main(int argc, char *argv[])
  936. {
  937. #ifdef RENDERDOC
  938. {
  939. void *mod = dlopen("librenderdoc.so", RTLD_NOW | RTLD_NOLOAD);
  940. if (mod) {
  941. pRENDERDOC_GetAPI RENDERDOC_GetAPI =
  942. (pRENDERDOC_GetAPI)dlsym(mod, "RENDERDOC_GetAPI");
  943. int ret = RENDERDOC_GetAPI(eRENDERDOC_API_Version_1_1_2,
  944. (void **)&rdoc_api);
  945. assert(ret == 1);
  946. } else {
  947. printf("Renderdoc not found\n");
  948. }
  949. }
  950. #endif
  951. if (!mp_load_config())
  952. return 1;
  953. setenv("LC_NUMERIC", "C", 1);
  954. GtkApplication *app = gtk_application_new("org.postmarketos.Megapixels", 0);
  955. g_signal_connect(app, "activate", G_CALLBACK(activate), NULL);
  956. g_signal_connect(app, "shutdown", G_CALLBACK(shutdown), NULL);
  957. g_application_run(G_APPLICATION(app), argc, argv);
  958. return 0;
  959. }