main.c 26 KB

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